From d68e17a52123dfa61e3b0e4406a0eddcf0612f4e Mon Sep 17 00:00:00 2001 From: Quentin Bazin Date: Fri, 10 Jul 2020 23:58:07 +0200 Subject: [PATCH] Added 'Redstone Lamp' alternative using states. WIP. --- docs/lua-api-block.md | 2 +- docs/lua-api-cpp.md | 6 ++++++ mods/default/blocks.lua | 15 +++++++++++++++ source/common/world/BlockState.cpp | 1 + source/common/world/Chunk.cpp | 15 +++++++++++++++ 5 files changed, 38 insertions(+), 1 deletion(-) diff --git a/docs/lua-api-block.md b/docs/lua-api-block.md index a8ea0c4c..7f6d5d4e 100644 --- a/docs/lua-api-block.md +++ b/docs/lua-api-block.md @@ -208,7 +208,7 @@ To get the current state of a block: local block_param = world:get_data(pos.x, pos.y, pos.z) local current_state = block:param():get_param(BlockParamType.State, block_param) -- if you don't -local current_state = world:get_block_state(pos.x, pos.y, pos.z) +local current_state = world:get_block_state(pos.x, pos.y, pos.z):id() ``` To set the current state of a block: diff --git a/docs/lua-api-cpp.md b/docs/lua-api-cpp.md index f027461c..f1071f5e 100644 --- a/docs/lua-api-cpp.md +++ b/docs/lua-api-cpp.md @@ -36,6 +36,12 @@ - `BlockParamType.State` - `BlockParamType.Count` +## BlockState + +- `u16 id()` +- `std::string label()` +- `ItemStack get_item_drop()` + ## Chunk - `u16 get_block(int x, int y, int z)` diff --git a/mods/default/blocks.lua b/mods/default/blocks.lua index 739cd23b..a611e214 100644 --- a/mods/default/blocks.lua +++ b/mods/default/blocks.lua @@ -306,6 +306,21 @@ mod:block { end, } +mod:block { + id = "redstone_lamp", + name = "Redstone Lamp (with states)", + tiles = "redstone_lamp_off.png", + + states = { + { is_light_source = true, tiles = "redstone_lamp_on.png" } + }, + + on_block_activated = function(pos, block, player, world, client, server, screen_width, screen_height, gui_scale) + local current_state = math.abs(world:get_block_state(pos.x, pos.y, pos.z):id() - 1) + world:set_block_state(pos.x, pos.y, pos.z, current_state) + end +} + mod:block { id = "redstone_lamp_off", name = "Redstone Lamp", diff --git a/source/common/world/BlockState.cpp b/source/common/world/BlockState.cpp index f8bdb9d3..556bb8ab 100644 --- a/source/common/world/BlockState.cpp +++ b/source/common/world/BlockState.cpp @@ -57,6 +57,7 @@ bool BlockState::isOpaque() const { // Please update 'docs/lua-api-cpp.md' if you change this void BlockState::initUsertype(sol::state &lua) { lua.new_usertype("BlockState", + "id", &BlockState::id, "label", (const std::string &(BlockState::*)() const)&BlockState::label, "get_item_drop", &BlockState::getItemDrop ); diff --git a/source/common/world/Chunk.cpp b/source/common/world/Chunk.cpp index 4b13612f..e3ea7df2 100644 --- a/source/common/world/Chunk.cpp +++ b/source/common/world/Chunk.cpp @@ -165,6 +165,21 @@ void Chunk::setBlockState(int x, int y, int z, u16 stateID) { u16 blockID = getBlock(x, y, z); u16 blockParam = getData(x, y, z); const Block &block = Registry::getInstance().getBlock(blockID); + const BlockState &blockState = block.getState(block.param().hasParam(BlockParam::State) + ? block.param().getParam(BlockParam::State, blockParam) : 0); + const BlockState &newBlockState = block.getState(block.param().hasParam(BlockParam::State) + ? stateID : 0); + + if (!blockState.isLightSource() && newBlockState.isLightSource()) { + m_lightmap.addTorchlight(x, y, z, 14); + } + else if (blockState.isLightSource() && !newBlockState.isLightSource()) { + gkDebug() << block.stringID(); + + m_lightmap.removeTorchlight(x, y, z); + m_lightmap.removeSunlight(x, y, z); + } + if (block.param().hasParam(BlockParam::State)) setData(x, y, z, block.param().setParam(BlockParam::State, blockParam, stateID)); }