]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/server/server_terrain.h
Fix client terrain crashes and performance
[dragonblocks_alpha.git] / src / server / server_terrain.h
1 #ifndef _SERVER_TERRAIN_H_
2 #define _SERVER_TERRAIN_H_
3
4 #include <dragonstd/list.h>
5 #include <pthread.h>
6 #include "server/server_player.h"
7 #include "terrain.h"
8 #include "types.h"
9
10 typedef enum {
11         CHUNK_STATE_CREATED,    // chunk exists but was not yet generated
12         CHUNK_STATE_GENERATING, // currently generating in a seperate thread
13         CHUNK_STATE_READY,      // generation finished
14 } TerrainChunkState;
15
16 typedef enum {
17         STAGE_VOID,     // initial air, can be overridden by anything
18         STAGE_TERRAIN,  // basic terrain, can be overridden by anything except the void
19         STAGE_TREES,    // trees replace terrain
20         STAGE_PLAYER,   // player-placed nodes or things placed after terrain generation
21 } TerrainGenStage;
22
23 typedef struct {
24         TerrainGenStage tgs;
25         List *changed_chunks;
26 } TerrainSetNodeArg;
27
28 typedef struct {
29         pthread_mutex_t mtx;        // UwU please hit me senpai
30         Blob data;                  // the big cum
31         TerrainChunkState state;    // generation state of the chunk
32         pthread_t gen_thread;       // thread that is generating chunk
33         TerrainGenStageBuffer tgsb; // buffer to make sure terraingen only overrides things it should
34 } TerrainChunkMeta; // OMG META VERSE WEB 3.0 VIRTUAL REALITY
35
36 /*
37         Locking conventions:
38         - chunk lock protects chunk->data and meta->tgsb
39         - meta mutex protects everything else in meta
40         - if both meta mutex and chunk are going to be locked, meta must be locked first
41         - you may not lock multiple meta mutexes at once
42         - if multiple chunk locks are being obtained at once, EDEADLK must be handled
43         - when locking a single chunk, assert return value of zero
44
45         After changing the data in a chunk:
46         1. release chunk lock
47         2.
48                 - if meta mutex is currently locked: use server_terrain_send_chunk
49                 - if meta mutex is not locked: use server_terrain_lock_and_send_chunk
50
51         If an operation affects multiple nodes (potentially in multiple chunks):
52                 - create a list changed_chunks
53                 - do job as normal, release individual chunk locks immediately after modifying their data
54                 - use server_terrain_lock_and_send_chunks to clear the list
55
56         Note: Unless changed_chunks is given to server_terrain_gen_node, it sends chunks automatically
57 */
58
59 // terrain object, data is stored here
60 extern Terrain *server_terrain;
61
62 // called on server startup
63 void server_terrain_init();
64 // called on server shutdown
65 void server_terrain_deinit();
66 // handle chunk request from client (thread safe)
67 void server_terrain_requested_chunk(ServerPlayer *player, v3s32 pos);
68 // prepare spawn region
69 void server_terrain_prepare_spawn();
70 // set node with terraingen stage
71 void server_terrain_gen_node(v3s32 pos, TerrainNode node, TerrainGenStage new_tgs, List *changed_chunks);
72 // get the spawn height because idk
73 s32 server_terrain_spawn_height();
74 // when bit chunkus changes
75 void server_terrain_send_chunk(TerrainChunk *chunk);
76 // lock and send
77 void server_terrain_lock_and_send_chunk(TerrainChunk *chunk);
78 // lock and send multiple chunks at once
79 void server_terrain_lock_and_send_chunks(List *list);
80
81 #endif // _SERVER_TERRAIN_H_