From 00f9b4f18f2b5b187d184f555412743b1826c7df Mon Sep 17 00:00:00 2001 From: xtreme8000 Date: Tue, 3 Sep 2024 19:24:09 +0200 Subject: [PATCH] Add door item place action --- CMakeLists.txt | 1 + source/block/blocks_data.h | 1 + source/item/items/item_door.c | 95 +++++++++++++++++++++++++++++++++++ source/item/items_object.h | 34 ++----------- 4 files changed, 100 insertions(+), 31 deletions(-) create mode 100644 source/item/items/item_door.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 69e200a..d070cd1 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -101,6 +101,7 @@ add_executable(cavex source/item/tool.c source/item/recipe.c source/item/items/item_sugarcane.c + source/item/items/item_door.c source/network/client_interface.c source/network/level_archive.c diff --git a/source/block/blocks_data.h b/source/block/blocks_data.h index 6f6551c..8c534ea 100644 --- a/source/block/blocks_data.h +++ b/source/block/blocks_data.h @@ -99,6 +99,7 @@ enum block_type { BLOCK_RAIL = 66, BLOCK_STONE_STAIRS = 67, BLOCK_STONE_PRESSURE_PLATE = 70, + BLOCK_IRON_DOOR = 71, BLOCK_WOOD_PRESSURE_PLATE = 72, BLOCK_REDSTONE_TORCH = 75, BLOCK_SNOW = 78, diff --git a/source/item/items/item_door.c b/source/item/items/item_door.c new file mode 100644 index 0000000..f11e3c4 --- /dev/null +++ b/source/item/items/item_door.c @@ -0,0 +1,95 @@ +/* + Copyright (c) 2024 ByteBit/xtreme8000 + + This file is part of CavEX. + + CavEX is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + CavEX is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with CavEX. If not, see . +*/ + +#include "../../network/server_local.h" + +static bool onItemPlace(struct server_local* s, struct item_data* it, + struct block_info* where, struct block_info* on, + enum side on_side) { + struct block_data b_support; + if(!server_world_get_block(&s->world, where->x, where->y - 1, where->z, + &b_support)) + return false; + + if(!blocks[b_support.type] || blocks[b_support.type]->can_see_through) + return false; + + struct block_data b_top; + if(!server_world_get_block(&s->world, where->x, where->y + 1, where->z, + &b_top)) + return false; + + if(blocks[b_top.type] && !blocks[b_top.type]->place_ignore) + return false; + + int type = (it->id == ITEM_DOOR_WOOD) ? BLOCK_WOODEN_DOOR : BLOCK_IRON_DOOR; + int metadata = 0; + double dx = s->player.x - (where->x + 0.5); + double dz = s->player.z - (where->z + 0.5); + + if(fabs(dx) > fabs(dz)) { + metadata = (dx >= 0) ? 2 : 0; + } else { + metadata = (dz >= 0) ? 3 : 1; + } + + server_world_set_block(&s->world, where->x, where->y, where->z, + (struct block_data) { + .type = type, + .metadata = metadata, + }); + server_world_set_block(&s->world, where->x, where->y + 1, where->z, + (struct block_data) { + .type = type, + .metadata = metadata | 0x08, + }); + return true; +} + +struct item item_door_wood = { + .name = "Wooden Door", + .has_damage = false, + .max_stack = 1, + .renderItem = render_item_flat, + .onItemPlace = onItemPlace, + .armor.is_armor = false, + .tool.type = TOOL_TYPE_ANY, + .render_data = { + .item = { + .texture_x = 11, + .texture_y = 2, + }, + }, +}; + +struct item item_door_iron = { + .name = "Iron Door", + .has_damage = false, + .max_stack = 1, + .renderItem = render_item_flat, + .onItemPlace = onItemPlace, + .armor.is_armor = false, + .tool.type = TOOL_TYPE_ANY, + .render_data = { + .item = { + .texture_x = 12, + .texture_y = 2, + }, + }, +}; diff --git a/source/item/items_object.h b/source/item/items_object.h index 6c4b881..83cb1d7 100644 --- a/source/item/items_object.h +++ b/source/item/items_object.h @@ -1340,36 +1340,6 @@ static struct item item_apple_golden = { }, }; -static struct item item_door_wood = { - .name = "Wooden Door", - .has_damage = false, - .max_stack = 1, - .renderItem = render_item_flat, - .armor.is_armor = false, - .tool.type = TOOL_TYPE_ANY, - .render_data = { - .item = { - .texture_x = 11, - .texture_y = 2, - }, - }, -}; - -static struct item item_door_iron = { - .name = "Iron Door", - .has_damage = false, - .max_stack = 1, - .renderItem = render_item_flat, - .armor.is_armor = false, - .tool.type = TOOL_TYPE_ANY, - .render_data = { - .item = { - .texture_x = 12, - .texture_y = 2, - }, - }, -}; - static struct item item_slime_ball = { .name = "Slimeball", .has_damage = false, @@ -1430,4 +1400,6 @@ static struct item item_saddle = { }, }; -extern struct item item_sugarcane; \ No newline at end of file +extern struct item item_sugarcane; +extern struct item item_door_wood; +extern struct item item_door_iron; \ No newline at end of file