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_node.h"
12 #include "client/client_player.h"
13 #include "client/client_terrain.h"
14 #include "client/debug_menu.h"
15 #include "client/terrain_gfx.h"
18 #define MAX_REQUESTS 4
20 Terrain *client_terrain;
22 static atomic_bool cancel; // used to notify meshgen and sync thread about quit
23 static Queue meshgen_tasks; // TerrainCHunk * queue (thread safe)
24 static pthread_t *meshgen_threads; // consumer threads for meshgen queue
25 static pthread_t sync_thread; // this thread requests new / changed chunks from server
26 static u32 load_distance; // load distance sent by server
27 static size_t load_chunks; // cached number of facecache positions to process every sync step (matches load distance)
31 // dequeue callback to update queue state in a thread safe manner
32 static TerrainChunk *set_dequeued(TerrainChunk *chunk)
34 pthread_mutex_lock(&chunk->mtx);
35 ((TerrainChunkMeta *) chunk->extra)->queue = false;
36 pthread_mutex_unlock(&chunk->mtx);
41 // mesh generator step
42 static void meshgen_step()
44 TerrainChunk *chunk = queue_deq(&meshgen_tasks, &set_dequeued);
47 terrain_gfx_make_chunk_model(chunk);
52 // send chunk request command to server
53 static void request_chunk(v3s32 pos)
55 dragonnet_peer_send_ToServerRequestChunk(client, &(ToServerRequestChunk) {
60 // terrain synchronisation step
61 static void sync_step()
64 static v3s32 *old_requests = NULL;
65 static size_t old_num_requests = 0;
68 ClientEntity *entity = client_player_entity_local();
71 pthread_rwlock_rdlock(&entity->lock_pos_rot);
72 player_pos = entity->data.pos;
73 pthread_rwlock_unlock(&entity->lock_pos_rot);
75 refcount_drp(&entity->rc);
81 v3s32 center = terrain_chunkp((v3s32) {player_pos.x, player_pos.y, player_pos.z});
83 u64 last_tick = tick++;
85 v3s32 *requests = malloc(MAX_REQUESTS * sizeof *requests);
86 size_t num_requests = 0;
88 for (size_t i = 0; i < load_chunks; i++) {
89 v3s32 pos = v3s32_add(facecache_get(i), center);
90 TerrainChunk *chunk = terrain_get_chunk(client_terrain, pos, false);
93 pthread_mutex_lock(&chunk->mtx);
95 TerrainChunkMeta *meta = chunk->extra;
96 switch (meta->state) {
98 // re-request chunks that got back into range
99 if (meta->sync < last_tick)
101 __attribute__((fallthrough));
104 meta->state = CHUNK_READY;
108 case CHUNK_RECIEVING:
112 pthread_mutex_unlock(&chunk->mtx);
113 } else if (num_requests < MAX_REQUESTS) {
114 // avoid duplicate requests
115 bool requested = false;
117 for (size_t i = 0; i < old_num_requests; i++) {
118 if (v3s32_equals(old_requests[i], pos)) {
127 requests[num_requests++] = pos;
134 old_requests = requests;
135 old_num_requests = num_requests;
138 // pthread routine for meshgen and sync thread
140 static struct LoopThread {
143 } loop_threads[2] = {
144 {"meshgen", &meshgen_step},
145 { "sync", &sync_step},
148 static void *loop_routine(struct LoopThread *thread)
150 #ifdef __GLIBC__ // check whether bloat is enabled
151 pthread_setname_np(pthread_self(), thread->name);
154 // warning: extremely advanced logic
162 // note: all these functions require the chunk mutex to be locked, which is always the case when a terrain callback is invoked
164 // callback for initializing a newly created chunk
165 // allocate and initialize meta data
166 static void on_create_chunk(TerrainChunk *chunk)
168 TerrainChunkMeta *meta = chunk->extra = malloc(sizeof *meta);
170 meta->state = CHUNK_RECIEVING;
175 meta->has_model = false;
176 for (int i = 0; i < 6; i++)
177 meta->depends[i] = false;
180 // callback for deleting a chunk
182 static void on_delete_chunk(TerrainChunk *chunk)
187 // callback for determining whether a chunk should be returned by terrain_get_chunk
188 // hold back chunks that have not been fully read from server yet when the create flag is not set
189 static bool on_get_chunk(TerrainChunk *chunk, bool create)
191 return create || ((TerrainChunkMeta *) chunk->extra)->state > CHUNK_RECIEVING;
197 void client_terrain_init()
199 client_terrain = terrain_create();
200 client_terrain->callbacks.create_chunk = &on_create_chunk;
201 client_terrain->callbacks.delete_chunk = &on_delete_chunk;
202 client_terrain->callbacks.get_chunk = &on_get_chunk;
203 client_terrain->callbacks.delete_node = &client_node_delete;
206 queue_ini(&meshgen_tasks);
208 client_terrain_set_load_distance(10); // some initial fuck idk just in case server is stupid
211 meshgen_threads = malloc(sizeof *meshgen_threads * client_config.meshgen_threads);
212 for (unsigned int i = 0; i < client_config.meshgen_threads; i++)
213 meshgen_threads[i] = 0; // but why???
216 // called on shutdown
217 void client_terrain_deinit()
219 queue_clr(&meshgen_tasks, NULL, NULL, NULL);
220 terrain_delete(client_terrain);
223 // start meshgen and sync threads
224 void client_terrain_start()
226 for (unsigned int i = 0; i < client_config.meshgen_threads; i++)
227 pthread_create(&meshgen_threads[i], NULL, (void *) &loop_routine, &loop_threads[0]);
229 pthread_create(&sync_thread, NULL, (void *) &loop_routine, &loop_threads[1]);
232 // stop meshgen and sync threads
233 void client_terrain_stop()
236 queue_cnl(&meshgen_tasks);
238 for (unsigned int i = 0; i < client_config.meshgen_threads; i++)
239 if (meshgen_threads[i])
240 pthread_join(meshgen_threads[i], NULL);
241 free(meshgen_threads);
244 pthread_join(sync_thread, NULL);
247 // update load distance
248 void client_terrain_set_load_distance(u32 dist)
250 load_distance = dist;
251 load_chunks = facecache_count(load_distance);
252 debug_menu_changed(ENTRY_LOAD_DISTANCE);
255 // return load distance
256 u32 client_terrain_get_load_distance()
258 return load_distance;
261 // called when a chunk was recieved from server
262 void client_terrain_chunk_received(TerrainChunk *chunk)
264 pthread_mutex_lock(&chunk->mtx);
265 TerrainChunkMeta *meta = chunk->extra;
267 if (meta->state == CHUNK_RECIEVING)
268 meta->state = CHUNK_FRESH;
270 client_terrain_meshgen_task(chunk, true);
271 pthread_mutex_unlock(&chunk->mtx);
273 for (int i = 0; i < 6; i++) {
274 TerrainChunk *neighbor = terrain_get_chunk(client_terrain,
275 v3s32_sub(chunk->pos, facedir[i]), false);
279 pthread_mutex_lock(&neighbor->mtx);
280 TerrainChunkMeta *neighbor_meta = neighbor->extra;
281 if (neighbor_meta->depends[i])
282 client_terrain_meshgen_task(neighbor, true);
283 pthread_mutex_unlock(&neighbor->mtx);
287 // enqueue chunk to mesh update queue
288 void client_terrain_meshgen_task(TerrainChunk *chunk, bool changed)
290 TerrainChunkMeta *meta = chunk->extra;
295 meta->has_model = true;
298 meta->model->flags.delete = 1;
304 if (meta->has_model && changed)
305 queue_ppd(&meshgen_tasks, chunk);
307 queue_enq(&meshgen_tasks, chunk);