home · contact · privacy
Split roguelike.(c|h) into main.(c|h) and misc.(c|h).
[plomrogue] / src / map.c
1 #include "map.h"
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include "misc.h"
5 #include "objects_on_map.h"
6
7 struct Map init_map () {
8 // Initialize map with some experimental start values.
9   struct Map map;
10   map.size.x = 64;
11   map.size.y = 64;
12   map.offset.x = 0;
13   map.offset.y = 0;
14   uint32_t size = map.size.x * map.size.y;
15   map.cells = malloc(size);
16   uint16_t y, x;
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)] = '.';
21   uint32_t curpos;
22   while (1) {
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)
32         break;
33       map.cells[y * map.size.x + x] = '.'; } }
34   return map; }
35
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)
39     map->offset.y--;
40   else if (WEST  == dir && map->offset.x > 0)
41     map->offset.x--;
42   else if (SOUTH == dir && map->offset.y + win_size.y < map->size.y)
43     map->offset.y++;
44   else if (EAST  == dir && map->offset.x + win_size.x < map->size.x)
45     map->offset.x++; }
46
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); }
51