2 #include <stdint.h> /* for 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 and 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; x++)
25 map.cells[(y * map.size.x) + x] = '~';
28 map.cells[size / 2 + (map.size.x / 2)] = '.';
32 y = rrand() % map.size.y;
33 x = rrand() % map.size.x;
34 curpos = y * map.size.x + x;
35 if ('~' == map.cells[curpos]
36 && ((curpos >= map.size.x && '.' == map.cells[curpos - map.size.x])
37 || (curpos < map.size.x * (map.size.y-1)
38 && '.' == map.cells[curpos + map.size.x])
39 || (curpos > 0 && curpos % map.size.x != 0
40 && '.' == map.cells[curpos-1])
41 || (curpos < (map.size.x * map.size.y)
42 && (curpos+1) % map.size.x != 0
43 && '.' == map.cells[curpos+1])))
45 if (y == 0 || y == map.size.y - 1 || x == 0 || x == map.size.x - 1)
49 map.cells[y * map.size.x + x] = '.';
57 extern void map_scroll(char d)
59 struct Win * win = get_win_by_id('m');
61 if (('N' == d || 'S' == d) && world.map->size.y > win->framesize.y)
63 offset = center_offset(win->center.y,
64 world.map->size.y, win->framesize.y);
65 win->center.y = offset + (win->framesize.y / 2);
66 if ('N' == d && win->center.y > 0)
70 else if ('S' == d && win->center.y < world.map->size.y - 1)
75 else if (('W' == d || 'E' == d) && world.map->size.x > win->framesize.x)
77 offset = center_offset(win->center.x,
78 world.map->size.x, win->framesize.x);
79 win->center.x = offset + (win->framesize.x / 2);
80 if ('W' == d && win->center.x > 0)
84 else if ('E' == d && win->center.x < world.map->size.x - 1)
93 extern void map_center()
95 struct MapObj * player = get_player();
96 struct Win * win_map = get_win_by_id('m');
97 win_map->center = player->pos;
102 extern uint8_t is_passable(struct Map * map, struct yx_uint16 pos)
104 uint8_t passable = 0;
105 if (0 <= pos.x && pos.x < map->size.x && 0 <= pos.y && pos.y < map->size.y)
107 passable = (('.' == map->cells[pos.y * map->size.x + pos.x]));