home · contact · privacy
Server: Refactor yx_uint8 moving; handle actors hitting the map edge.
[plomrogue] / src / server / map.c
1 /* src/server/map.c */
2
3 #include "map.h"
4 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, UINT16_MAX */
5 #include <stdlib.h> /* free() */
6 #include "../common/rexit.h" /* exit_err() */
7 #include "../common/try_malloc.h" /* try_malloc() */
8 #include "../common/yx_uint8.h" /* struct yx_uint8 */
9 #include "rrand.h" /* rrand() */
10 #include "yx_uint8.h" /* mv_yx_in_dir_wrap() */
11 #include "world.h" /* global world */
12
13
14
15 /* Call this too often with "init" of 0 and the game exits with an error message
16  * about reaching an iteration limit. An "init" of 1 sets the iteration counter
17  * to 0. Iteration limit is currently 256 * UINT16_MAX.
18  */
19 static uint8_t iter_limit(uint8_t init);
20
21 /* Return 1 if cell on "pos" is neighbor to a cell of "type", else return 0. */
22 static uint8_t is_neighbor(struct yx_uint8 pos, char type);
23
24 /* Fill map with '~' cells. */
25 static void make_sea();
26
27 /* Put island of '.' cells inside map sea. */
28 static void make_sea();
29
30 /* Put tree cells of 'X' on island. */
31 static void make_trees();
32
33
34
35 static uint8_t iter_limit(uint8_t init)
36 {
37     static uint32_t i = 0;
38     char * err = "Map generation reached iteration limit. Change map size?";
39     if (init)
40     {
41         i = 0;
42         return 0;
43     }
44     i++;
45     exit_err(256 * UINT16_MAX == i, err);
46     return 1;
47 }
48
49
50
51 static uint8_t is_neighbor(struct yx_uint8 pos, char type)
52 {
53     uint8_t ind = pos.y % 2;
54     uint8_t diag_west = pos.x + ind > 0;
55     uint8_t diag_east = pos.x + ind <= world.map.length - 1;
56     uint16_t pos_i = (pos.y * world.map.length) + pos.x;
57     if (   (   pos.y > 0                    && diag_east
58             && type == world.map.cells[pos_i - world.map.length + ind])
59         || (   pos.x < world.map.length - 1
60             && type == world.map.cells[pos_i + 1])
61         || (   pos.y < world.map.length - 1 && diag_east
62             && type == world.map.cells[pos_i + world.map.length + ind])
63         || (   pos.y > 0                    && diag_west
64             && type == world.map.cells[pos_i - world.map.length - !ind])
65         || (   pos.x > 0
66             && type == world.map.cells[pos_i - 1])
67         || (   pos.y < world.map.length - 1 && diag_west
68             && type == world.map.cells[pos_i + world.map.length - !ind]))
69     {
70         return 1;
71     }
72     return 0;
73 }
74
75
76
77 static void make_sea()
78 {
79     uint16_t y, x;
80     for (y = 0; y < world.map.length; y++)
81     {
82         for (x = 0;
83              x < world.map.length;
84              world.map.cells[(y * world.map.length) + x] = '~', x++);
85     }
86 }
87
88
89
90 static void make_island()
91 {
92     char type = '.';
93     uint8_t add_half_width = !(world.map.length % 2) * (world.map.length / 2);
94     uint32_t size = world.map.length * world.map.length;
95     world.map.cells[(size / 2) + add_half_width] = type;
96     struct yx_uint8 pos;
97     iter_limit(1);
98     while (iter_limit(0))
99     {
100         pos.y = rrand() % world.map.length;
101         pos.x = rrand() % world.map.length;
102         uint16_t pos_i = (pos.y * world.map.length) + pos.x;
103         if ('~' == world.map.cells[pos_i] && is_neighbor(pos, type))
104         {
105             if (   pos.y == 0 || pos.y == world.map.length - 1
106                 || pos.x == 0 || pos.x == world.map.length - 1)
107             {
108                 break;
109             }
110             world.map.cells[pos_i] = type;
111         }
112     }
113 }
114
115
116
117 static void make_trees()
118 {
119     char type = 'X';
120     struct yx_uint8 pos;
121     uint16_t n_trees = (world.map.length * world.map.length) / 16;
122     uint16_t i_trees = 0;
123     iter_limit(1);
124     while (i_trees <= n_trees && iter_limit(0))
125     {
126         uint8_t single_allowed = rrand() % 32;
127         pos.y = rrand() % world.map.length;
128         pos.x = rrand() % world.map.length;
129         uint16_t pos_i = (pos.y * world.map.length) + pos.x;
130         if ('.' == world.map.cells[pos_i]
131             && (!single_allowed || is_neighbor(pos, type)))
132         {
133             world.map.cells[pos_i] = type;
134             i_trees++;
135         }
136     }
137 }
138
139
140
141 extern void remake_map()
142 {
143     free(world.map.cells);
144     world.map.cells = try_malloc(world.map.length * world.map.length, __func__);
145     uint32_t store_seed = world.seed;
146     world.seed = world.seed_map;
147     make_sea();
148     make_island();
149     make_trees();
150     world.seed = store_seed;
151 }
152
153
154
155 extern uint8_t is_passable(struct yx_uint8 pos)
156 {
157     uint8_t passable = 0;
158     if (pos.x < world.map.length && pos.y < world.map.length)
159     {
160         passable = ('.' == world.map.cells[(pos.y * world.map.length) + pos.x]);
161     }
162     return passable;
163 }
164
165
166
167 extern uint8_t mv_yx_in_dir_legal(char dir, struct yx_uint8 * yx)
168 {
169     uint8_t wraptest = mv_yx_in_dir_wrap(dir, yx, 0);
170     if (!wraptest && yx->x < world.map.length && yx->y < world.map.length)
171     {
172         return 1;
173     }
174     return 0;
175 }