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 --- src/Block.cpp | 32 ++++++++++++++++++-------------- src/Block.hpp | 9 +++++++++ src/Plugin.cpp | 8 ++++++++ 3 files changed, 35 insertions(+), 14 deletions(-) (limited to 'src') 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