Skip to content

Commit

Permalink
Speed up logistics of all air mapblocks
Browse files Browse the repository at this point in the history
  • Loading branch information
LizzyFleckenstein03 committed Feb 13, 2022
1 parent 29abff1 commit 681bf87
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 4 deletions.
2 changes: 1 addition & 1 deletion deps/dragonnet
Submodule dragonnet updated 1 files
+3 −0 recv.c
2 changes: 1 addition & 1 deletion deps/dragontype
1 change: 1 addition & 0 deletions src/client/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ static void on_ToClientBlock(unused DragonnetPeer *peer, ToClientBlock *pkt)
MapBlock *block = map_get_block(client_map.map, pkt->pos, true);

map_deserialize_block(block, pkt->data);
((MapBlockExtraData *) block->extra)->all_air = (pkt->data.siz == 0);
client_map_block_received(block);
}

Expand Down
13 changes: 11 additions & 2 deletions src/client/client_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ static void on_create_block(MapBlock *block)
extra->queue = false;
extra->last_synced = 0;
extra->obj = NULL;
extra->all_air = false;
}

// callback for deleting a block
Expand Down Expand Up @@ -236,9 +237,17 @@ void client_map_schedule_update_block_mesh(MapBlock *block)

pthread_mutex_lock(&block->mtx);
MapBlockExtraData *extra = block->extra;

if (! extra->queue) {
extra->queue = true;
queue_enqueue(client_map.queue, block);
if (extra->all_air) {
if (extra->obj) {
extra->obj->remove = true;
extra->obj = NULL;
}
} else {
extra->queue = true;
queue_enqueue(client_map.queue, block);
}
}
pthread_mutex_unlock(&block->mtx);
}
1 change: 1 addition & 0 deletions src/client/client_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ typedef struct
bool queue; // whether the block is in meshgen queue
u64 last_synced; // keep track of when a block was synced the last time (used to detect when a block got out of and then back into range)
Object *obj; // mesh object, generated by blockmesh file
bool all_air; // no thoughts brain empty
} MapBlockExtraData;

extern struct ClientMap
Expand Down
19 changes: 19 additions & 0 deletions src/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,18 @@ void map_free_block(MapBlock *block)

Blob map_serialize_block(MapBlock *block)
{
bool all_air = true;

ITERATE_MAPBLOCK {
if (block->data[x][y][z].type != NODE_AIR) {
all_air = false;
break;
}
}

if (all_air)
return (Blob) {0, NULL};

SerializedMapBlock block_data;

ITERATE_MAPBLOCK {
Expand Down Expand Up @@ -174,6 +186,13 @@ Blob map_serialize_block(MapBlock *block)

bool map_deserialize_block(MapBlock *block, Blob buffer)
{
if (buffer.siz == 0) {
ITERATE_MAPBLOCK
block->data[x][y][z] = map_node_create(NODE_AIR, (Blob) {0, NULL});

return true;
}

// it's important to copy Blobs that have been malloc'd before reading from them
// because reading from a Blob modifies its data and size pointer,
// but does not free anything
Expand Down

0 comments on commit 681bf87

Please sign in to comment.