home · contact · privacy
Improved (and simplified) pseudo-randomness.
[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(), 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() */
9
10
11
12 extern struct Map init_map()
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, 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 extern void map_scroll(char d)
58 {
59     struct Win * win = get_win_by_id('m');
60     uint16_t offset;
61     if (('N' == d || 'S' == d) && world.map->size.y > win->framesize.y)
62     {
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)
67         {
68             win->center.y--;
69         }
70         else if ('S' == d && win->center.y < world.map->size.y - 1)
71         {
72             win->center.y++;
73         }
74     }
75     else if (('W' == d || 'E' == d) && world.map->size.x > win->framesize.x)
76     {
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)
81         {
82             win->center.x--;
83         }
84         else if ('E' == d && win->center.x < world.map->size.x - 1)
85         {
86             win->center.x++;
87         }
88     }
89 }
90
91
92
93 extern void map_center()
94 {
95     struct MapObj * player = get_player();
96     struct Win * win_map   = get_win_by_id('m');
97     win_map->center = player->pos;
98 }
99
100
101
102 extern uint8_t is_passable(struct Map * map, struct yx_uint16 pos)
103 {
104     uint8_t passable = 0;
105     if (0 <= pos.x && pos.x < map->size.x && 0 <= pos.y && pos.y < map->size.y)
106     {
107         passable = (('.' == map->cells[pos.y * map->size.x + pos.x]));
108     }
109     return passable;
110 }