From bb25dc91d9a83a9f9031ad364f1cbbc1b39a4c7c Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Mon, 27 Dec 2021 22:49:09 +0500 Subject: Added RegisterLiquid to lua api --- cwd/assets/altcraft/scripts/blocks.lua | 6 ++++++ src/Block.cpp | 32 ++++++++++++++++++-------------- src/Block.hpp | 9 +++++++++ src/Plugin.cpp | 8 ++++++++ 4 files changed, 41 insertions(+), 14 deletions(-) diff --git a/cwd/assets/altcraft/scripts/blocks.lua b/cwd/assets/altcraft/scripts/blocks.lua index 72a1599..dde4b57 100644 --- a/cwd/assets/altcraft/scripts/blocks.lua +++ b/cwd/assets/altcraft/scripts/blocks.lua @@ -33,6 +33,12 @@ local function RegisterBlocks() AC.RegisterBlock(BlockId.new(7,0), true, "bedrock", "normal") + AC.RegisterLiquid(BlockId.new(8,0), "blocks/water_flow", "blocks/water_still") + AC.RegisterLiquid(BlockId.new(9,0), "blocks/water_flow", "blocks/water_still") + + AC.RegisterLiquid(BlockId.new(10,0), "blocks/lava_flow", "blocks/lava_still") + AC.RegisterLiquid(BlockId.new(11,0), "blocks/lava_flow", "blocks/lava_still") + AC.RegisterBlock(BlockId.new(12,0), true, "sand", "normal") AC.RegisterBlock(BlockId.new(12,1), true, "red_sand", "normal") diff --git a/src/Block.cpp b/src/Block.cpp index 85870f6..8af5a4b 100644 --- a/src/Block.cpp +++ b/src/Block.cpp @@ -1,25 +1,29 @@ #include "Block.hpp" #include -#include -#include "Plugin.hpp" +static std::map blocks; +static std::map liquids; -static std::vector blocks; -static std::map staticBlockInfo; - -BlockInfo WTFBlock{ true, "", "" }; +static BlockInfo UnknownBlock{ true, "", "" }; +static LiquidInfo UnknownLiquid{ "", "" }; void RegisterStaticBlockInfo(BlockId blockId, BlockInfo blockInfo) { - //NOTE: It can be made thread-safe by using atomic incrementer - staticBlockInfo[blockId] = blocks.size(); - blocks.push_back(blockInfo); + blocks.emplace(blockId, blockInfo); +} + +void RegisterStaticLiquidInfo(BlockId blockId, LiquidInfo liquidInfo) { + liquids[blockId] = liquidInfo; + for (uint8_t i = 0; i < 16; i++) + blocks.emplace(BlockId{ blockId.id, i }, BlockInfo{ true, "@liquid", liquidInfo.stillTexture }); } BlockInfo* GetBlockInfo(BlockId blockId) { - auto it = staticBlockInfo.find(blockId); - if (it != staticBlockInfo.end()) - return &blocks.data()[it->second]; - else - return &WTFBlock; + auto it = blocks.find(blockId); + return it != blocks.end() ? &it->second : &UnknownBlock; +} + +const LiquidInfo& GetBlockLiquidInfo(BlockId blockId) { + auto it = liquids.find(blockId); + return it != liquids.end() ? it->second : UnknownLiquid; } diff --git a/src/Block.hpp b/src/Block.hpp index 0fd0e89..535ae68 100644 --- a/src/Block.hpp +++ b/src/Block.hpp @@ -47,6 +47,15 @@ struct BlockInfo { std::string variant; }; +struct LiquidInfo { + std::string flowTexture; + std::string stillTexture; +}; + void RegisterStaticBlockInfo(BlockId blockId, BlockInfo blockInfo); +void RegisterStaticLiquidInfo(BlockId blockId, LiquidInfo liquidInfo); + BlockInfo* GetBlockInfo(BlockId blockId); + +const LiquidInfo& GetBlockLiquidInfo(BlockId blockId); diff --git a/src/Plugin.cpp b/src/Plugin.cpp index 3e06b0c..7a3b716 100644 --- a/src/Plugin.cpp +++ b/src/Plugin.cpp @@ -78,6 +78,13 @@ namespace PluginApi { }); } + void RegisterLiquid(BlockId blockId, std::string flowTexture, std::string stillTexture) { + RegisterStaticLiquidInfo(blockId, LiquidInfo{ + flowTexture, + stillTexture + }); + } + void RegisterDimension(int dimId, Dimension dim) { RegisterNewDimension(dimId, dim); } @@ -286,6 +293,7 @@ void PluginSystem::Init() { apiTable["LogError"] = PluginApi::LogError; apiTable["GetGameState"] = PluginApi::GetGameState; apiTable["RegisterBlock"] = PluginApi::RegisterBlock; + apiTable["RegisterLiquid"] = PluginApi::RegisterLiquid; apiTable["RegisterDimension"] = PluginApi::RegisterDimension; apiTable["ConnectToServer"] = PluginApi::ConnectToServer; apiTable["Exit"] = PluginApi::Exit; -- cgit v1.2.3