diff --git a/source/block/block_door.c b/source/block/block_door.c index ade6588..e1510c8 100644 --- a/source/block/block_door.c +++ b/source/block/block_door.c @@ -17,6 +17,7 @@ along with CavEX. If not, see . */ +#include "../network/server_local.h" #include "blocks.h" static enum block_material getMaterial1(struct block_info* this) { @@ -76,8 +77,35 @@ static uint8_t getTextureIndex2(struct block_info* this, enum side side) { static size_t getDroppedItem(struct block_info* this, struct item_data* it, struct random_gen* g) { - // TODO - return 0; + if(it) { + it->id = (this->block->type == BLOCK_WOODEN_DOOR) ? ITEM_DOOR_WOOD : + ITEM_DOOR_IRON; + it->durability = 0; + it->count = 1; + } + + // drop item only for top block + return (this->block->metadata & 0x08) ? 1 : 0; +} + +static void onRightClick(struct server_local* s, struct item_data* it, + struct block_info* where, struct block_info* on, + enum side on_side) { + bool is_top = on->block->metadata & 0x08; + struct block_data other; + + if(!server_world_get_block(&s->world, on->x, is_top ? on->y - 1 : on->y + 1, + on->z, &other) + || on->block->type != other.type) + return; + + // flip open/closed state + on->block->metadata ^= 0x04; + other.metadata ^= 0x04; + + server_world_set_block(&s->world, on->x, on->y, on->z, *on->block); + server_world_set_block(&s->world, on->x, is_top ? on->y - 1 : on->y + 1, + on->z, other); } struct block block_wooden_door = { @@ -88,7 +116,7 @@ struct block block_wooden_door = { .getTextureIndex = getTextureIndex1, .getDroppedItem = getDroppedItem, .onRandomTick = NULL, - .onRightClick = NULL, + .onRightClick = onRightClick, .transparent = false, .renderBlock = render_block_door, .renderBlockAlways = NULL, diff --git a/source/block/blocks_data.h b/source/block/blocks_data.h index e2489b9..6f6551c 100644 --- a/source/block/blocks_data.h +++ b/source/block/blocks_data.h @@ -94,6 +94,7 @@ enum block_type { BLOCK_DIAMOND_CAST = 57, BLOCK_WORKBENCH = 58, BLOCK_FURNACE = 61, + BLOCK_WOODEN_DOOR = 64, BLOCK_LADDER = 65, BLOCK_RAIL = 66, BLOCK_STONE_STAIRS = 67,