3 #include "client/blockmesh.h"
4 #include "client/facecache.h"
5 #include "client/client_map.h"
6 #include "client/client_player.h"
7 #include "client/debug_menu.h"
9 #define MAX_BLOCK_REQUESTS 4
11 struct ClientMap client_map;
16 // dequeue callback to thread-safely update
17 static void set_dequeued(void *arg)
19 MapBlock *block = arg;
21 pthread_mutex_lock(&block->mtx);
22 ((MapBlockExtraData *) block->extra)->queue = false;
23 pthread_mutex_unlock(&block->mtx);
26 // mesh generator step
27 static void meshgen_step()
31 if ((block = queue_dequeue_callback(client_map.queue, &set_dequeued)))
32 blockmesh_make(block);
37 // pthread start routine for meshgen thread
38 static void *meshgen_thread(unused void *arg)
40 while (! client_map.cancel)
48 // send block request command to server
49 static void request_position(v3s32 pos)
51 pthread_mutex_lock(&client->mtx);
52 (void) (write_u32(client->fd, SC_REQUEST_BLOCK) && write_v3s32(client->fd, pos));
53 pthread_mutex_unlock(&client->mtx);
56 // mapblock synchronisation step
57 static void sync_step()
60 static v3s32 *old_requested_positions = NULL;
61 static size_t old_requested_positions_count = 0;
63 u64 last_tick = tick++;
65 v3f64 player_pos = client_player_get_position();
66 v3s32 center = map_node_to_block_pos((v3s32) {player_pos.x, player_pos.y, player_pos.z}, NULL);
68 v3s32 *requested_positions = malloc(sizeof(v3s32) * MAX_BLOCK_REQUESTS);
69 size_t requested_positions_count = 0;
71 for (size_t i = 0; i < client_map.blocks_count; i++) {
72 v3s32 pos = facecache_face(i, ¢er);
73 MapBlock *block = map_get_block(client_map.map, pos, false);
76 pthread_mutex_lock(&block->mtx);
77 MapBlockExtraData *extra = block->extra;
79 switch (extra->state) {
81 if (extra->last_synced < last_tick)
82 request_position(pos);
86 extra->state = MBS_READY;
87 extra->last_synced = tick;
93 pthread_mutex_unlock(&block->mtx);
94 } else if (requested_positions_count < MAX_BLOCK_REQUESTS) {
95 bool should_request = true;
97 for (size_t i = 0; i < old_requested_positions_count; i++) {
98 if (v3s32_equals(old_requested_positions[i], pos)) {
99 should_request = false;
105 request_position(pos);
107 requested_positions[requested_positions_count++] = pos;
111 if (old_requested_positions)
112 free(old_requested_positions);
114 old_requested_positions = requested_positions;
115 old_requested_positions_count = requested_positions_count;
118 // pthread start routine for sync thread
119 static void *sync_thread(unused void *arg)
121 while (! client_map.cancel)
128 // note: all these functions require the block mutex to be locked, which is always the case when a map callback is invoked
130 // callback for initializing a newly created block
131 // allocate and initialize extra data
132 static void on_create_block(MapBlock *block)
134 MapBlockExtraData *extra = block->extra = malloc(sizeof(MapBlockExtraData));
136 extra->state = MBS_RECIEVING;
137 extra->queue = false;
138 extra->last_synced = 0;
142 // callback for deleting a block
144 static void on_delete_block(MapBlock *block)
149 // callback for determining whether a block should be returned by map_get_block
150 // hold back blocks that have not been fully read from server yet when the create flag is set to true
151 static bool on_get_block(MapBlock *block, bool create)
153 return create || ((MapBlockExtraData *) block->extra)->state > MBS_RECIEVING;
158 // ClientMap singleton constructor
159 void client_map_init(Client *cli)
163 client_map.map = map_create((MapCallbacks) {
164 .create_block = &on_create_block,
165 .delete_block = &on_delete_block,
166 .get_block = &on_get_block,
168 .after_set_node = NULL,
170 client_map.queue = queue_create();
171 client_map.cancel = false;
172 client_map.sync_thread = 0;
173 client_map_set_simulation_distance(10);
175 for (int i = 0; i < NUM_MESHGEN_THREADS; i++)
176 client_map.meshgen_threads[i] = 0;
179 // ClientMap singleton destructor
180 void client_map_deinit()
182 queue_delete(client_map.queue);
183 map_delete(client_map.map);
186 // start meshgen and sync threads
187 void client_map_start()
189 for (int i = 0; i < NUM_MESHGEN_THREADS; i++)
190 pthread_create(&client_map.meshgen_threads[i], NULL, &meshgen_thread, NULL);
192 pthread_create(&client_map.sync_thread, NULL, &sync_thread, NULL);
195 // stop meshgen and sync threads
196 void client_map_stop()
198 client_map.cancel = true;
200 for (int i = 0; i < NUM_MESHGEN_THREADS; i++)
201 if (client_map.meshgen_threads[i])
202 pthread_join(client_map.meshgen_threads[i], NULL);
204 if (client_map.sync_thread)
205 pthread_join(client_map.sync_thread, NULL);
208 // update simulation distance
209 void client_map_set_simulation_distance(u32 simulation_distance)
211 client_map.simulation_distance = simulation_distance;
212 client_map.blocks_count = facecache_count(simulation_distance);
215 // called when a block was actually recieved from server
216 void client_map_block_received(MapBlock *block)
218 pthread_mutex_lock(&block->mtx);
219 MapBlockExtraData *extra = block->extra;
220 if (extra->state == MBS_RECIEVING)
221 extra->state = MBS_FRESH;
222 pthread_mutex_unlock(&block->mtx);
224 client_map_schedule_update_block_mesh(block);
226 client_map_schedule_update_block_mesh(map_get_block(client_map.map, (v3s32) {block->pos.x + 1, block->pos.y + 0, block->pos.z + 0}, false));
227 client_map_schedule_update_block_mesh(map_get_block(client_map.map, (v3s32) {block->pos.x + 0, block->pos.y + 1, block->pos.z + 0}, false));
228 client_map_schedule_update_block_mesh(map_get_block(client_map.map, (v3s32) {block->pos.x + 0, block->pos.y + 0, block->pos.z + 1}, false));
229 client_map_schedule_update_block_mesh(map_get_block(client_map.map, (v3s32) {block->pos.x - 1, block->pos.y - 0, block->pos.z - 0}, false));
230 client_map_schedule_update_block_mesh(map_get_block(client_map.map, (v3s32) {block->pos.x - 0, block->pos.y - 1, block->pos.z - 0}, false));
231 client_map_schedule_update_block_mesh(map_get_block(client_map.map, (v3s32) {block->pos.x - 0, block->pos.y - 0, block->pos.z - 1}, false));
234 // enqueue block to mesh update queue
235 void client_map_schedule_update_block_mesh(MapBlock *block)
240 pthread_mutex_lock(&block->mtx);
241 MapBlockExtraData *extra = block->extra;
242 if (! extra->queue) {
244 queue_enqueue(client_map.queue, block);
246 pthread_mutex_unlock(&block->mtx);