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