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