home · contact · privacy
Overhauled large parts of window system to universalize scroll hints.
[plomrogue] / src / map.c
1 #include "map.h"
2 #include <stdint.h>      /* for uint16_t, uint32_t */
3 #include "misc.h"        /* for try_malloc(), center_offset() */
4 #include "map_objects.h" /* for Player struct */
5 #include "yx_uint16.h"   /* for yx_uint16 and dir enums */
6 #include "rrand.h"       /* for rrand() */
7 #include "windows.h"     /* for struct Win */
8 struct World;
9
10
11
12 struct Map init_map(struct World * world)
13 {
14     char * f_name = "init_map()";
15     struct Map map;
16     map.size.x = 64;
17     map.size.y = 64;
18     uint32_t size = map.size.x * map.size.y;
19     map.cells = try_malloc(size, world, f_name);
20     uint16_t y, x;
21     for (y = 0; y < map.size.y; y++)
22     {
23         for (x = 0; x < map.size.x; x++)
24         {
25             map.cells[(y * map.size.x) + x] = '~';
26         }
27     }
28     map.cells[size / 2 + (map.size.x / 2)] = '.';
29     uint32_t curpos;
30     while (1)
31     {
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])))
44         {
45             if (y == 0 || y == map.size.y - 1 || x == 0 || x == map.size.x - 1)
46             {
47                 break;
48             }
49             map.cells[y * map.size.x + x] = '.';
50         }
51     }
52     return map;
53 }
54
55
56
57 void map_scroll(struct Win * win, struct yx_uint16 map_size, enum dir d)
58 {
59     uint16_t offset;
60     if ((NORTH == d || SOUTH == d) && map_size.y > win->frame.size.y)
61     {
62         offset = center_offset(win->center.y, map_size.y, win->frame.size.y);
63         win->center.y = offset + (win->frame.size.y / 2);
64         if      (NORTH == d && win->center.y > 0)
65         {
66             win->center.y--;
67         }
68         else if (SOUTH == d && win->center.y < map_size.y - 1)
69         {
70             win->center.y++;
71         }
72     }
73     else if ((WEST == d || EAST == d) && map_size.x > win->frame.size.x)
74     {
75         offset = center_offset(win->center.x, map_size.x, win->frame.size.x);
76         win->center.x = offset + (win->frame.size.x / 2);
77         if      (WEST == d && win->center.x > 0)
78         {
79             win->center.x--;
80         }
81         else if (EAST == d && win->center.x < map_size.x - 1)
82         {
83             win->center.x++;
84         }
85     }
86 }