5 #include "objects_on_map.h"
7 struct Map init_map () {
8 // Initialize map with some experimental start values.
14 uint32_t size = map.size.x * map.size.y;
15 map.cells = malloc(size);
17 for (y = 0; y < map.size.y; y++)
18 for (x = 0; x < map.size.x; x++)
19 map.cells[(y * map.size.x) + x] = '~';
20 map.cells[size / 2 + (map.size.x / 2)] = '.';
23 y = rrand(0, 0) % map.size.y;
24 x = rrand(0, 0) % map.size.x;
25 curpos = y * map.size.x + x;
26 if ('~' == map.cells[curpos] &&
27 ( (curpos >= map.size.x && '.' == map.cells[curpos - map.size.x])
28 || (curpos < map.size.x * (map.size.y-1) && '.' == map.cells[curpos + map.size.x])
29 || (curpos > 0 && curpos % map.size.x != 0 && '.' == map.cells[curpos-1])
30 || (curpos < (map.size.x * map.size.y) && (curpos+1) % map.size.x != 0 && '.' == map.cells[curpos+1]))) {
31 if (y == 0 || y == map.size.y - 1 || x == 0 || x == map.size.x - 1)
33 map.cells[y * map.size.x + x] = '.'; } }
36 void map_scroll (struct Map * map, char dir, struct yx_uint16 win_size) {
37 // Scroll map into direction dir if possible by changing the offset.
38 if (NORTH == dir && map->offset.y > 0)
40 else if (WEST == dir && map->offset.x > 0)
42 else if (SOUTH == dir && map->offset.y + win_size.y < map->size.y)
44 else if (EAST == dir && map->offset.x + win_size.x < map->size.x)
47 void map_center_player (struct Map * map, struct Player * player, struct yx_uint16 frame_size) {
48 // Center map on player.
49 map->offset.y = center_offset (player->pos.y, map->size.y, frame_size.y);
50 map->offset.x = center_offset (player->pos.x, map->size.x, frame_size.x); }