]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/server/server_item.c
70c4c67628f76610384431320499f98d05f0a667
[dragonblocks_alpha.git] / src / server / server_item.c
1 #include <assert.h>
2 #include "node.h"
3 #include "server/server_item.h"
4 #include "server/server_node.h"
5 #include "server/server_terrain.h"
6 #include "server/tree_physics.h"
7
8 static void use_dig(__attribute__((unused)) ServerPlayer *player, ItemStack *stack, bool pointed, v3s32 pos)
9 {
10         if (!pointed)
11                 return;
12
13         v3s32 offset;
14         TerrainChunk *chunk = terrain_get_chunk_nodep(server_terrain, pos, &offset, CHUNK_MODE_PASSIVE);
15         if (!chunk)
16                 return;
17         TerrainChunkMeta *meta = chunk->extra;
18         assert(pthread_rwlock_wrlock(&chunk->lock) == 0);
19
20         TerrainNode *node = &chunk->data[offset.x][offset.y][offset.z];
21
22         if (!(node_def[node->type].dig_class & item_def[stack->type].dig_class)) {
23                 pthread_rwlock_unlock(&chunk->lock);
24                 return;
25         }
26
27         *node = server_node_create(NODE_AIR);
28         meta->tgsb.raw.nodes[offset.x][offset.y][offset.z] = STAGE_PLAYER;
29
30         pthread_rwlock_unlock(&chunk->lock);
31
32         server_terrain_lock_and_send_chunk(chunk);
33
34         // destroy trees if they have no connection to ground
35         // todo: run in seperate thread to not block client connection
36         tree_physics_check(pos);
37 }
38
39 ServerItemDef server_item_def[COUNT_ITEM] = {
40         // unknown
41         {
42                 .use = NULL,
43         },
44         // none
45         {
46                 .use = NULL,
47         },
48         // pickaxe
49         {
50                 .use = &use_dig,
51         },
52         // axe
53         {
54                 .use = &use_dig,
55         },
56         // shovel
57         {
58                 .use = &use_dig,
59         },
60 };