home · contact · privacy
Applied new style and documentation requirements to map library; also some minor...
[plomrogue] / src / map.c
1 #include "map.h"
2 #include <stdlib.h>      /* for malloc() */
3 #include <stdint.h>      /* for uint16_t, uint32_t */
4 #include "misc.h"        /* for rrand() and center_offset() */
5 #include "map_objects.h" /* for Player struct */
6 #include "yx_uint16.h"   /* for yx_uint16 and dir enums */
7
8
9
10 struct Map init_map ()
11 {
12     struct Map map;
13     map.size.x = 64;
14     map.size.y = 64;
15     map.offset.x = 0;
16     map.offset.y = 0;
17     uint32_t size = map.size.x * map.size.y;
18     map.cells = malloc(size);
19     uint16_t y, x;
20     for (y = 0; y < map.size.y; y++)
21     {
22         for (x = 0; x < map.size.x; x++)
23         {
24             map.cells[(y * map.size.x) + x] = '~';
25         }
26     }
27     map.cells[size / 2 + (map.size.x / 2)] = '.';
28     uint32_t curpos;
29     while (1)
30     {
31         y = rrand(0, 0) % map.size.y;
32         x = rrand(0, 0) % map.size.x;
33         curpos = y * map.size.x + x;
34         if ('~' == map.cells[curpos]
35             && ((curpos >= map.size.x && '.' == map.cells[curpos - map.size.x])
36                 || (curpos < map.size.x * (map.size.y-1)
37                      && '.' == map.cells[curpos + map.size.x])
38                 || (curpos > 0 && curpos % map.size.x != 0
39                     && '.' == map.cells[curpos-1])
40                 || (curpos < (map.size.x * map.size.y)
41                     && (curpos+1) % map.size.x != 0
42                     && '.' == map.cells[curpos+1])))
43         {
44             if (y == 0 || y == map.size.y - 1 || x == 0 || x == map.size.x - 1)
45             {
46                 break;
47             }
48             map.cells[y * map.size.x + x] = '.';
49         }
50     }
51     return map;
52 }
53
54
55
56 void map_scroll (struct Map * map, enum dir d, struct yx_uint16 win_size)
57 {
58     if      (NORTH == d && map->offset.y > 0)
59     {
60         map->offset.y--;
61     }
62     else if (WEST  == d && map->offset.x > 0)
63     {
64         map->offset.x--;
65     }
66     else if (SOUTH == d && map->offset.y + win_size.y < map->size.y)
67     {
68         map->offset.y++;
69     }
70     else if (EAST  == d && map->offset.x + win_size.x < map->size.x)
71     {
72         map->offset.x++;
73     }
74 }
75
76
77
78 void map_center_player(struct Map * map, struct Player * player,
79                        struct yx_uint16 win_size)
80 {
81     map->offset.y = center_offset (player->pos.y, map->size.y, win_size.y);
82     map->offset.x = center_offset (player->pos.x, map->size.x, win_size.x);
83 }