1 #define _GNU_SOURCE // don't worry, GNU extensions are only used when available
2 #include <dragonstd/queue.h>
8 #include "client/client.h"
9 #include "client/facecache.h"
10 #include "client/client_config.h"
11 #include "client/client_player.h"
12 #include "client/client_terrain.h"
13 #include "client/debug_menu.h"
14 #include "client/terrain_gfx.h"
16 #define MAX_REQUESTS 4
18 Terrain *client_terrain;
20 static atomic_bool cancel; // used to notify meshgen and sync thread about quit
21 static Queue meshgen_tasks; // TerrainCHunk * queue (thread safe)
22 static pthread_t *meshgen_threads; // consumer threads for meshgen queue
23 static pthread_t sync_thread; // this thread requests new / changed chunks from server
24 static u32 load_distance; // load distance sent by server
25 static size_t load_chunks; // cached number of facecache positions to process every sync step (matches load distance)
29 // dequeue callback to update queue state in a thread safe manner
30 static TerrainChunk *set_dequeued(TerrainChunk *chunk)
32 pthread_mutex_lock(&chunk->mtx);
33 ((TerrainChunkMeta *) chunk->extra)->queue = false;
34 pthread_mutex_unlock(&chunk->mtx);
39 // mesh generator step
40 static void meshgen_step()
42 TerrainChunk *chunk = queue_deq(&meshgen_tasks, &set_dequeued);
45 terrain_gfx_make_chunk_model(chunk);
50 // send chunk request command to server
51 static void request_chunk(v3s32 pos)
53 dragonnet_peer_send_ToServerRequestChunk(client, &(ToServerRequestChunk) {
58 // terrain synchronisation step
59 static void sync_step()
62 static v3s32 *old_requests = NULL;
63 static size_t old_num_requests = 0;
66 ClientEntity *entity = client_player_entity();
69 pthread_rwlock_rdlock(&entity->lock_pos_rot);
70 player_pos = entity->data.pos;
71 pthread_rwlock_unlock(&entity->lock_pos_rot);
73 refcount_drp(&entity->rc);
79 v3s32 center = terrain_node_to_chunk_pos((v3s32) {player_pos.x, player_pos.y, player_pos.z}, NULL);
81 u64 last_tick = tick++;
83 v3s32 *requests = malloc(MAX_REQUESTS * sizeof *requests);
84 size_t num_requests = 0;
86 for (size_t i = 0; i < load_chunks; i++) {
87 v3s32 pos = v3s32_add(facecache_get(i), center);
88 TerrainChunk *chunk = terrain_get_chunk(client_terrain, pos, false);
91 pthread_mutex_lock(&chunk->mtx);
93 TerrainChunkMeta *meta = chunk->extra;
94 switch (meta->state) {
96 // re-request chunks that got back into range
97 if (meta->sync < last_tick)
99 __attribute__((fallthrough));
102 meta->state = CHUNK_READY;
106 case CHUNK_RECIEVING:
110 pthread_mutex_unlock(&chunk->mtx);
111 } else if (num_requests < MAX_REQUESTS) {
112 // avoid duplicate requests
113 bool requested = false;
115 for (size_t i = 0; i < old_num_requests; i++) {
116 if (v3s32_equals(old_requests[i], pos)) {
125 requests[num_requests++] = pos;
132 old_requests = requests;
133 old_num_requests = num_requests;
136 // pthread routine for meshgen and sync thread
138 static struct LoopThread {
141 } loop_threads[2] = {
142 {"meshgen", &meshgen_step},
143 { "sync", &sync_step},
146 static void *loop_routine(struct LoopThread *thread)
148 #ifdef __GLIBC__ // check whether bloat is enabled
149 pthread_setname_np(pthread_self(), thread->name);
152 // warning: extremely advanced logic
160 // note: all these functions require the chunk mutex to be locked, which is always the case when a terrain callback is invoked
162 // callback for initializing a newly created chunk
163 // allocate and initialize meta data
164 static void on_create_chunk(TerrainChunk *chunk)
166 TerrainChunkMeta *meta = chunk->extra = malloc(sizeof *meta);
168 meta->state = CHUNK_RECIEVING;
175 // callback for deleting a chunk
177 static void on_delete_chunk(TerrainChunk *chunk)
182 // callback for determining whether a chunk should be returned by terrain_get_chunk
183 // hold back chunks that have not been fully read from server yet when the create flag is not set
184 static bool on_get_chunk(TerrainChunk *chunk, bool create)
186 return create || ((TerrainChunkMeta *) chunk->extra)->state > CHUNK_RECIEVING;
192 void client_terrain_init()
194 client_terrain = terrain_create();
195 client_terrain->callbacks.create_chunk = &on_create_chunk;
196 client_terrain->callbacks.delete_chunk = &on_delete_chunk;
197 client_terrain->callbacks.get_chunk = &on_get_chunk;
198 client_terrain->callbacks.set_node = NULL;
199 client_terrain->callbacks.after_set_node = NULL;
202 queue_ini(&meshgen_tasks);
204 client_terrain_set_load_distance(10); // some initial fuck idk just in case server is stupid
207 meshgen_threads = malloc(sizeof *meshgen_threads * client_config.meshgen_threads);
208 for (unsigned int i = 0; i < client_config.meshgen_threads; i++)
209 meshgen_threads[i] = 0; // but why???
212 // called on shutdown
213 void client_terrain_deinit()
215 queue_clr(&meshgen_tasks, NULL, NULL, NULL);
216 terrain_delete(client_terrain);
219 // start meshgen and sync threads
220 void client_terrain_start()
222 for (unsigned int i = 0; i < client_config.meshgen_threads; i++)
223 pthread_create(&meshgen_threads[i], NULL, (void *) &loop_routine, &loop_threads[0]);
225 pthread_create(&sync_thread, NULL, (void *) &loop_routine, &loop_threads[1]);
228 // stop meshgen and sync threads
229 void client_terrain_stop()
232 queue_cnl(&meshgen_tasks);
234 for (unsigned int i = 0; i < client_config.meshgen_threads; i++)
235 if (meshgen_threads[i])
236 pthread_join(meshgen_threads[i], NULL);
237 free(meshgen_threads);
240 pthread_join(sync_thread, NULL);
243 // update load distance
244 void client_terrain_set_load_distance(u32 dist)
246 load_distance = dist;
247 load_chunks = facecache_count(load_distance);
248 debug_menu_changed(ENTRY_LOAD_DISTANCE);
251 // return load distance
252 u32 client_terrain_get_load_distance()
254 return load_distance;
257 // called when a chunk was recieved from server
258 void client_terrain_chunk_received(TerrainChunk *chunk)
260 pthread_mutex_lock(&chunk->mtx);
261 TerrainChunkMeta *extra = chunk->extra;
262 if (extra->state == CHUNK_RECIEVING)
263 extra->state = CHUNK_FRESH;
264 pthread_mutex_unlock(&chunk->mtx);
266 client_terrain_meshgen_task(chunk);
268 v3s32 neighbors[6] = {
277 for (int i = 0; i < 6; i++)
278 client_terrain_meshgen_task(terrain_get_chunk(client_terrain,
279 v3s32_add(chunk->pos, neighbors[i]), false));
282 // enqueue chunk to mesh update queue
283 void client_terrain_meshgen_task(TerrainChunk *chunk)
288 pthread_mutex_lock(&chunk->mtx);
290 TerrainChunkMeta *meta = chunk->extra;
294 meta->model->flags.delete = 1;
299 queue_enq(&meshgen_tasks, chunk);
303 pthread_mutex_unlock(&chunk->mtx);