]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/server/server_map.c
Add configuration files for client and server
[dragonblocks_alpha.git] / src / server / server_map.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <unistd.h>
4 #include <stdio.h>
5 #include "map.h"
6 #include "server/database.h"
7 #include "server/mapgen.h"
8 #include "server/server_config.h"
9 #include "server/server_map.h"
10 #include "signal_handlers.h"
11 #include "util.h"
12
13 struct ServerMap server_map;
14 static Server *server;
15
16 // utility functions
17
18 // send a block to a client and reset block request
19 static void send_block(Client *client, MapBlock *block)
20 {
21         MapBlockExtraData *extra = block->extra;
22
23         pthread_mutex_lock(&client->mtx);
24         if (client->state == CS_ACTIVE)
25                 (void) (write_u32(client->fd, CC_BLOCK)
26                         && write_v3s32(client->fd, block->pos)
27                         && write_u64(client->fd, extra->size)
28                         && write_u64(client->fd, extra->rawsize)
29                         && write(client->fd, extra->data, extra->size) != -1);
30         pthread_mutex_unlock(&client->mtx);
31 }
32
33 // send block to near clients
34 // block mutex has to be locked
35 static void send_block_to_near(MapBlock *block)
36 {
37         MapBlockExtraData *extra = block->extra;
38
39         if (extra->state == MBS_GENERATING)
40                 return;
41
42         if (extra->data)
43                 free(extra->data);
44
45         map_serialize_block(block, &extra->data, &extra->size, &extra->rawsize);
46         database_save_block(block);
47
48         if (extra->state == MBS_CREATED)
49                 return;
50
51         pthread_rwlock_rdlock(&server->players_rwlck);
52         ITERATE_LIST(&server->players, pair) {
53                 Client *client = pair->value;
54
55                 if (within_simulation_distance(client->pos, block->pos, server_config.simulation_distance))
56                         send_block(client, block);
57         }
58         pthread_rwlock_unlock(&server->players_rwlck);
59 }
60
61 // list_clear_func callback for sending changed blocks to near clients
62 static void list_send_block(void *key, unused void *value, unused void *arg)
63 {
64         MapBlock *block = key;
65
66         pthread_mutex_lock(&block->mtx);
67         send_block_to_near(block);
68         pthread_mutex_unlock(&block->mtx);
69 }
70
71 // pthread start routine for mapgen thread
72 static void *mapgen_thread(void *arg)
73 {
74         MapBlock *block = arg;
75         MapBlockExtraData *extra = block->extra;
76
77         pthread_mutex_lock(&block->mtx);
78         extra->state = MBS_GENERATING;
79         pthread_mutex_unlock(&block->mtx);
80
81         List changed_blocks = list_create(NULL);
82         list_put(&changed_blocks, block, NULL);
83
84         mapgen_generate_block(block, &changed_blocks);
85
86         pthread_mutex_lock(&block->mtx);
87         extra->state = MBS_READY;
88         pthread_mutex_unlock(&block->mtx);
89
90         list_clear_func(&changed_blocks, &list_send_block, NULL);
91
92         pthread_mutex_lock(&server_map.joining_threads_mtx);
93         if (! server_map.joining_threads) {
94                 pthread_mutex_lock(&server_map.mapgen_threads_mtx);
95                 list_delete(&server_map.mapgen_threads, &extra->mapgen_thread);
96                 pthread_mutex_unlock(&server_map.mapgen_threads_mtx);
97         }
98         pthread_mutex_unlock(&server_map.joining_threads_mtx);
99
100         return NULL;
101 }
102
103 // launch mapgen thread for block
104 // block mutex has to be locked
105 static void launch_mapgen_thread(MapBlock *block)
106 {
107         pthread_mutex_lock(&server_map.mapgen_threads_mtx);
108         pthread_t *thread_ptr = &((MapBlockExtraData *) block->extra)->mapgen_thread;
109         pthread_create(thread_ptr, NULL, mapgen_thread, block);
110         list_put(&server_map.mapgen_threads, thread_ptr, NULL);
111         pthread_mutex_unlock(&server_map.mapgen_threads_mtx);
112 }
113
114 // list_clear_func callback used to join running generator threads on shutdown
115 static void list_join_thread(void *key, unused void *value, unused void *arg)
116 {
117         pthread_join(*(pthread_t *) key, NULL);
118 }
119
120 // map callbacks
121 // note: all these functions require the block mutex to be locked, which is always the case when a map callback is invoked
122
123 // callback for initializing a newly created block
124 // load block from database or initialize state, mgstage buffer and data
125 static void on_create_block(MapBlock *block)
126 {
127         MapBlockExtraData *extra = block->extra = malloc(sizeof(MapBlockExtraData));
128
129         if (! database_load_block(block)) {
130                 extra->state = MBS_CREATED;
131                 extra->data = NULL;
132
133                 ITERATE_MAPBLOCK {
134                         block->data[x][y][z] = map_node_create(NODE_AIR, NULL, 0);
135                         extra->mgs_buffer[x][y][z] = MGS_VOID;
136                 }
137         }
138 }
139
140 // callback for deleting a block
141 // free extra data
142 static void on_delete_block(MapBlock *block)
143 {
144         MapBlockExtraData *extra = block->extra;
145
146         if (extra->data)
147                 free(extra->data);
148
149         free(extra);
150 }
151
152 // callback for determining whether a block should be returned by map_get_block
153 // hold back blocks that are not fully generated except when the create flag is set to true
154 static bool on_get_block(MapBlock *block, bool create)
155 {
156         MapBlockExtraData *extra = block->extra;
157
158         if (extra->state < MBS_READY && ! create)
159                 return false;
160
161         return true;
162 }
163
164 // callback for deciding whether a set_node call succeeds or not
165 // reject set_node calls that try to override nodes placed by later mapgen stages, else update mgs buffer - also make sure block is inserted into changed blocks list
166 static bool on_set_node(MapBlock *block, v3u8 offset, unused MapNode *node, void *arg)
167 {
168         MapgenSetNodeArg *msn_arg = arg;
169
170         MapgenStage mgs;
171
172         if (msn_arg)
173                 mgs = msn_arg->mgs;
174         else
175                 mgs = MGS_PLAYER;
176
177         MapgenStage *old_mgs = &((MapBlockExtraData *) block->extra)->mgs_buffer[offset.x][offset.y][offset.z];
178
179         if (mgs >= *old_mgs) {
180                 *old_mgs = mgs;
181
182                 if (msn_arg)
183                         list_put(msn_arg->changed_blocks, block, NULL);
184
185                 return true;
186         }
187
188         return false;
189 }
190
191 // callback for when a block changes
192 // send block to near clients if not part of map generation
193 static void on_after_set_node(MapBlock *block, unused v3u8 offset, void *arg)
194 {
195         if (! arg)
196                 send_block_to_near(block);
197 }
198
199 // join all map generation threads
200 static void join_mapgen_threads()
201 {
202         pthread_mutex_lock(&server_map.joining_threads_mtx);
203         server_map.joining_threads = true;
204         pthread_mutex_unlock(&server_map.joining_threads_mtx);
205
206         pthread_mutex_lock(&server_map.mapgen_threads_mtx);
207         list_clear_func(&server_map.mapgen_threads, &list_join_thread, NULL);
208         pthread_mutex_unlock(&server_map.mapgen_threads_mtx);
209
210         pthread_mutex_lock(&server_map.joining_threads_mtx);
211         server_map.joining_threads = false;
212         pthread_mutex_unlock(&server_map.joining_threads_mtx);
213 }
214
215 // generate a hut for new players to spawn in
216 static void generate_spawn_hut()
217 {
218         f32 wood_color[3] = {0.11f, 1.0f, 0.29f};
219         List changed_blocks = list_create(NULL);
220
221         for (s32 x = -4; x <= +4; x++) {
222                 for (s32 y = 0; y <= 3; y++) {
223                         for (s32 z = -3; z <= +2; z++) {
224                                 mapgen_set_node((v3s32) {x, server_map.spawn_height + y, z}, map_node_create(NODE_AIR, NULL, 0), MGS_PLAYER, &changed_blocks);
225                         }
226                 }
227         }
228
229         for (s32 x = -5; x <= +5; x++) {
230                 for (s32 z = -4; z <= +3; z++) {
231                         mapgen_set_node((v3s32) {x, server_map.spawn_height - 1, z}, map_node_create(NODE_OAK_WOOD, wood_color, sizeof wood_color), MGS_PLAYER, &changed_blocks);
232                         mapgen_set_node((v3s32) {x, server_map.spawn_height + 4, z}, map_node_create(NODE_OAK_WOOD, wood_color, sizeof wood_color), MGS_PLAYER, &changed_blocks);
233                 }
234         }
235
236         for (s32 y = 0; y <= 3; y++) {
237                 for (s32 x = -5; x <= +5; x++) {
238                         mapgen_set_node((v3s32) {x, server_map.spawn_height + y, -4}, map_node_create(((y == 1 || y == 2) && ((x >= -3 && x <= -1) || (x >= +1 && x <= +2))) ? NODE_AIR : NODE_OAK_WOOD, wood_color, sizeof(f32) * 3), MGS_PLAYER, &changed_blocks);
239                         mapgen_set_node((v3s32) {x, server_map.spawn_height + y, +3}, map_node_create(((y == 1 || y == 2) && ((x >= -3 && x <= -2) || (x >= +1 && x <= +3))) ? NODE_AIR : NODE_OAK_WOOD, wood_color, sizeof(f32) * 3), MGS_PLAYER, &changed_blocks);
240                 }
241         }
242
243         for (s32 y = 0; y <= 3; y++) {
244                 for (s32 z = -3; z <= +2; z++) {
245                         mapgen_set_node((v3s32) {-5, server_map.spawn_height + y, z}, map_node_create(NODE_OAK_WOOD, wood_color, sizeof(f32) * 3), MGS_PLAYER, &changed_blocks);
246                         mapgen_set_node((v3s32) {+5, server_map.spawn_height + y, z}, map_node_create(((y != 3) && (z == -1 || z == +0)) ? NODE_AIR : NODE_OAK_WOOD, wood_color, sizeof(f32) * 3), MGS_PLAYER, &changed_blocks);
247                 }
248         }
249
250         v2s32 posts[6] = {
251                 {-4, -3},
252                 {-4, +2},
253                 {+4, -3},
254                 {+4, +2},
255                 {+5, -1},
256                 {+5, +0},
257         };
258
259         for (int i = 0; i < 6; i++) {
260                 for (s32 y = server_map.spawn_height - 2;; y--) {
261                         v3s32 pos = {posts[i].x, y, posts[i].y};
262                         Node node = map_get_node(server_map.map, pos).type;
263
264                         if (i >= 4) {
265                                 if (node != NODE_AIR)
266                                         break;
267
268                                 pos.y++;
269                         }
270
271                         if (node_definitions[node].solid)
272                                 break;
273
274                         mapgen_set_node(pos, map_node_create(node == NODE_LAVA ? NODE_VULCANO_STONE : NODE_OAK_WOOD, wood_color, sizeof(f32) * 3), MGS_PLAYER, &changed_blocks);
275                 }
276         }
277
278         list_clear_func(&changed_blocks, &list_send_block, NULL);
279 }
280
281 // public functions
282
283 // ServerMap singleton constructor
284 void server_map_init(Server *srv)
285 {
286         server = srv;
287
288         server_map.map = map_create((MapCallbacks) {
289                 .create_block = &on_create_block,
290                 .delete_block = &on_delete_block,
291                 .get_block = &on_get_block,
292                 .set_node = &on_set_node,
293                 .after_set_node = &on_after_set_node,
294         });
295         server_map.joining_threads = false;
296         server_map.mapgen_threads = list_create(NULL);
297         pthread_mutex_init(&server_map.joining_threads_mtx, NULL);
298         pthread_mutex_init(&server_map.mapgen_threads_mtx, NULL);
299 }
300
301 // ServerMap singleton destructor
302 void server_map_deinit()
303 {
304         join_mapgen_threads();
305         pthread_mutex_destroy(&server_map.joining_threads_mtx);
306         pthread_mutex_destroy(&server_map.mapgen_threads_mtx);
307         map_delete(server_map.map);
308 }
309
310 // handle block request from client (thread safe)
311 void server_map_requested_block(Client *client, v3s32 pos)
312 {
313         if (within_simulation_distance(client->pos, pos, server_config.simulation_distance)) {
314                 MapBlock *block = map_get_block(server_map.map, pos, true);
315
316                 pthread_mutex_lock(&block->mtx);
317                 MapBlockExtraData *extra = block->extra;
318
319                 switch (extra->state) {
320                         case MBS_CREATED:
321                                 launch_mapgen_thread(block);
322                                 break;
323
324                         case MBS_GENERATING:
325                                 break;
326
327                         case MBS_READY:
328                                 send_block(client, block);
329                 };
330                 pthread_mutex_unlock(&block->mtx);
331         }
332 }
333
334 // prepare spawn region
335 void server_map_prepare_spawn()
336 {
337         s32 done = 0;
338         s32 dist = server_config.simulation_distance;
339         s32 total = (dist * 2 + 1);
340         total *= total * total;
341         s32 last_percentage = -1;
342
343         for (s32 x = -dist; x <= (s32) dist; x++) {
344                 for (s32 y = -dist; y <= (s32) dist; y++) {
345                         for (s32 z = -dist; z <= (s32) dist; z++) {
346                                 if (interrupted) {
347                                         join_mapgen_threads();
348                                         return;
349                                 }
350
351                                 MapBlock *block = map_get_block(server_map.map, (v3s32) {x, y, z}, true);
352
353                                 pthread_mutex_lock(&block->mtx);
354                                 if (((MapBlockExtraData *) block->extra)->state == MBS_CREATED)
355                                         launch_mapgen_thread(block);
356                                 pthread_mutex_unlock(&block->mtx);
357
358                                 done++;
359
360                                 s32 percentage = 100.0 * done / total;
361
362                                 if (percentage > last_percentage) {
363                                         last_percentage = percentage;
364                                         printf("Preparing spawn... %d%%\n", percentage);
365                                 }
366                         }
367                 }
368         }
369
370         join_mapgen_threads();
371
372         s64 saved_spawn_height;
373         if (database_load_meta("spawn_height", &saved_spawn_height)) {
374                 server_map.spawn_height = saved_spawn_height;
375         } else {
376                 s32 spawn_height = -1;
377
378                 while (map_get_node(server_map.map, (v3s32) {0, ++spawn_height, 0}).type != NODE_AIR)
379                         ;
380
381                 server_map.spawn_height = spawn_height + 5;
382                 generate_spawn_hut();
383                 database_save_meta("spawn_height", server_map.spawn_height);
384         }
385 }