2 #include <stdint.h> /* for uint8_t, uint16_t, uint32_t */
3 #include "misc.h" /* for try_malloc(), center_offset(), rrand() */
4 #include "map_objects.h" /* for get_player() */
5 #include "yx_uint16.h" /* for yx_uint16, dir enums */
6 #include "windows.h" /* for struct Win */
7 #include "main.h" /* for world global */
8 #include "wincontrol.h" /* for get_win_by_id() */
12 extern struct Map init_map()
14 char * f_name = "init_map()";
18 uint32_t size = map.size.x * map.size.y;
19 map.cells = try_malloc(size, f_name);
21 for (y = 0; y < map.size.y; y++)
23 for (x = 0; x < map.size.x; map.cells[(y * map.size.x) + x] = '~', x++);
25 map.cells[size / 2 + (map.size.x / 2)] = '.';
29 y = rrand() % map.size.y;
30 x = rrand() % map.size.x;
31 curpos = y * map.size.x + x;
32 if ('~' == map.cells[curpos]
33 && ((curpos >= map.size.x && '.' == map.cells[curpos - map.size.x])
34 || (curpos < map.size.x * (map.size.y-1)
35 && '.' == map.cells[curpos + map.size.x])
36 || (curpos > 0 && curpos % map.size.x != 0
37 && '.' == map.cells[curpos-1])
38 || (curpos < (map.size.x * map.size.y)
39 && (curpos+1) % map.size.x != 0
40 && '.' == map.cells[curpos+1])))
42 if (y == 0 || y == map.size.y - 1 || x == 0 || x == map.size.x - 1)
46 map.cells[y * map.size.x + x] = '.';
54 extern void map_scroll(char d)
56 struct Win * win = get_win_by_id('m');
58 if (('N' == d || 'S' == d) && world.map->size.y > win->framesize.y)
60 offset = center_offset(win->center.y,
61 world.map->size.y, win->framesize.y);
62 win->center.y = offset + (win->framesize.y / 2);
63 if ('S' == d && win->center.y < world.map->size.y - 1)
68 win->center.y = win->center.y - ('N' == d && win->center.y > 0);
70 else if (('W' == d || 'E' == d) && world.map->size.x > win->framesize.x)
72 offset = center_offset(win->center.x,
73 world.map->size.x, win->framesize.x);
74 win->center.x = offset + (win->framesize.x / 2);
75 if ('E' == d && win->center.x < world.map->size.x - 1)
80 win->center.x = win->center.x - ('W' == d && win->center.x > 0);
86 extern void map_center()
88 struct MapObj * player = get_player();
89 struct Win * win_map = get_win_by_id('m');
90 win_map->center = player->pos;
95 extern uint8_t is_passable(struct Map * map, struct yx_uint16 pos)
98 if (0 <= pos.x && pos.x < map->size.x && 0 <= pos.y && pos.y < map->size.y)
100 passable = (('.' == map->cells[pos.y * map->size.x + pos.x]));