diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/AssetManager.cpp | 8 | ||||
-rw-r--r-- | src/Block.cpp | 19 | ||||
-rw-r--r-- | src/Block.hpp | 14 | ||||
-rw-r--r-- | src/Plugin.cpp | 45 | ||||
-rw-r--r-- | src/Plugin.hpp | 6 | ||||
-rw-r--r-- | src/World.cpp | 6 |
6 files changed, 89 insertions, 9 deletions
diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index be69dd0..19cd452 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -613,16 +613,16 @@ BlockFaces &AssetManager::GetBlockModelByBlockId(BlockId block) { return blockIdToBlockFaces.find(block)->second; } - auto blockStateName = TransformBlockIdToBlockStateName(block); - AssetBlockState *asset = GetAsset<AssetBlockState>("/minecraft/blockstates/" + blockStateName.first); + BlockInfo blockInfo = GetBlockInfo(block); + AssetBlockState *asset = GetAsset<AssetBlockState>("/minecraft/blockstates/" + blockInfo.blockstate); if (!asset) return GetBlockModelByBlockId(BlockId{ 7788,0 }); BlockState &blockState = asset->blockState; - if (blockState.variants.find(blockStateName.second) == blockState.variants.end()) + if (blockState.variants.find(blockInfo.variant) == blockState.variants.end()) return GetBlockModelByBlockId(BlockId{ 7788,0 }); - BlockStateVariant &variant = blockState.variants[blockStateName.second]; + BlockStateVariant &variant = blockState.variants[blockInfo.variant]; if (variant.models.empty()) return GetBlockModelByBlockId(BlockId{ 7788,0 }); diff --git a/src/Block.cpp b/src/Block.cpp index a12db9d..421cb5d 100644 --- a/src/Block.cpp +++ b/src/Block.cpp @@ -1,5 +1,9 @@ #include "Block.hpp" +#include <map> + +#include "Plugin.hpp" + std::pair<std::string, std::string> TransformBlockIdToBlockStateName(BlockId blockId) { switch (blockId.id) { case 1: { @@ -523,3 +527,18 @@ std::pair<std::string, std::string> TransformBlockIdToBlockStateName(BlockId blo return std::make_pair("", ""); } + +std::map<BlockId, BlockInfo> staticBlockInfo; + +void RegisterStaticBlockInfo(BlockId blockId, BlockInfo blockInfo) { + staticBlockInfo[blockId] = blockInfo; +} + +BlockInfo GetBlockInfo(BlockId blockId, Vector blockPos) { + auto it = staticBlockInfo.find(blockId); + if (it != staticBlockInfo.end()) + return it->second; + if (blockPos == Vector()) + return BlockInfo{ true, "", "" }; + return PluginSystem::RequestBlockInfo(blockPos); +} diff --git a/src/Block.hpp b/src/Block.hpp index fa8b51a..d8a78a8 100644 --- a/src/Block.hpp +++ b/src/Block.hpp @@ -3,6 +3,8 @@ #include <utility> #include <string> +#include "Vector.hpp" + struct BlockId { unsigned short id : 13; unsigned char state : 4; @@ -40,4 +42,14 @@ namespace std { } //returns name of blockstate and name of variant -std::pair<std::string, std::string> TransformBlockIdToBlockStateName(BlockId blockId);
\ No newline at end of file +std::pair<std::string, std::string> TransformBlockIdToBlockStateName(BlockId blockId); + +struct BlockInfo { + bool collides; + std::string blockstate; + std::string variant; +}; + +void RegisterStaticBlockInfo(BlockId blockId, BlockInfo blockInfo); + +BlockInfo GetBlockInfo(BlockId blockId, Vector blockPos = Vector(0,0,0));
\ No newline at end of file diff --git a/src/Plugin.cpp b/src/Plugin.cpp index 6acfb08..8d2de94 100644 --- a/src/Plugin.cpp +++ b/src/Plugin.cpp @@ -20,6 +20,7 @@ struct Plugin { const std::function<void()> onUnload; const std::function<void(std::string)> onChangeState; const std::function<void(double)> onTick; + const std::function<BlockInfo(Vector)> onRequestBlockInfo; }; @@ -37,7 +38,8 @@ namespace PluginApi { plugin["onLoad"].get_or(std::function<void()>()), plugin["onUnload"].get_or(std::function<void()>()), plugin["onChangeState"].get_or(std::function<void(std::string)>()), - plugin["onTick"].get_or(std::function<void(double)>()) + plugin["onTick"].get_or(std::function<void(double)>()), + plugin["onRequestBlockInfo"].get_or(std::function<BlockInfo(Vector)>()), }; plugins.push_back(nativePlugin); nativePlugin.onLoad(); @@ -60,6 +62,14 @@ namespace PluginApi { GameState *GetGameState() { return ::GetGameState(); } + + void RegisterBlock(BlockId blockId, bool collides, std::string blockstate, std::string variant) { + RegisterStaticBlockInfo(blockId, BlockInfo{ + collides, + blockstate, + variant + }); + } } int LoadFileRequire(lua_State* L) { @@ -149,7 +159,16 @@ void PluginSystem::Init() { "GetBlockId", &World::GetBlockId, "SetBlockId", &World::SetBlockId); + auto bidFactory1 = []() { + return BlockId{ 0,0 }; + }; + auto bidFactory2 = [](unsigned short id, unsigned char state) { + return BlockId{ id,state }; + }; + lua.new_usertype<BlockId>("BlockId", + "new", sol::factories([]() {return BlockId{ 0,0 };}, + [](unsigned short id, unsigned char state) {return BlockId{ id, state };}), "id", sol::property( [](BlockId & bid) { return bid.id; }, [](BlockId & bid, unsigned short id) { bid.id = id; }), @@ -169,6 +188,11 @@ void PluginSystem::Init() { "y", &VectorF::y, "z", &VectorF::z); + lua.new_usertype<BlockInfo>("BlockInfo", + "collides", &BlockInfo::collides, + "blockstate", &BlockInfo::blockstate, + "variant", &BlockInfo::variant); + sol::table apiTable = lua["AC"].get_or_create<sol::table>(); apiTable["RegisterPlugin"] = PluginApi::RegisterPlugin; @@ -176,6 +200,7 @@ void PluginSystem::Init() { apiTable["LogInfo"] = PluginApi::LogInfo; apiTable["LogError"] = PluginApi::LogError; apiTable["GetGameState"] = PluginApi::GetGameState; + apiTable["RegisterBlock"] = PluginApi::RegisterBlock; } void PluginSystem::Execute(const std::string &luaCode, bool except) { @@ -216,3 +241,21 @@ void PluginSystem::CallOnTick(double deltaTime) { } } } + +BlockInfo PluginSystem::RequestBlockInfo(Vector blockPos) { + OPTICK_EVENT(); + BlockInfo ret; + for (Plugin& plugin : plugins) { + if (plugin.onRequestBlockInfo && plugin.errors < 10) + try { + ret = plugin.onRequestBlockInfo(blockPos); + if (!ret.blockstate.empty()) + break; + } + catch (sol::error & e) { + LOG(ERROR) << e.what(); + plugin.errors++; + } + } + return ret; +} diff --git a/src/Plugin.hpp b/src/Plugin.hpp index 3b61011..a849f5c 100644 --- a/src/Plugin.hpp +++ b/src/Plugin.hpp @@ -2,6 +2,10 @@ #include <string> +#include "Vector.hpp" + +class BlockInfo; + namespace PluginSystem { void Init(); @@ -10,4 +14,6 @@ namespace PluginSystem { void CallOnChangeState(std::string newState); void CallOnTick(double deltaTime); + + BlockInfo RequestBlockInfo(Vector blockPos); }
\ No newline at end of file diff --git a/src/World.cpp b/src/World.cpp index fa281f1..00a1a19 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -98,7 +98,7 @@ bool World::isPlayerCollides(double X, double Y, double Z) const { for (int y = 0; y < 16; y++) { for (int z = 0; z < 16; z++) { BlockId block = section.GetBlockId(Vector(x, y, z)); - if (block.id == 0 || block.id == 31 || block.id == 37 || block.id == 38 || block.id == 175) + if (!GetBlockInfo(block).collides) continue; AABB blockColl{ (x + it.x * 16.0), (y + it.y * 16.0), @@ -183,8 +183,8 @@ void World::UpdatePhysics(float delta) { for (int x = blockXBegin; x <= blockXEnd; x++) { OPTICK_EVENT("testCollision"); BlockId block = this->GetBlockId(Vector(x, y, z)); - if (block.id == 0 || block.id == 31 || block.id == 37 || block.id == 38 || block.id == 175 || block.id == 78) - continue; + if (block.id == 0 || !GetBlockInfo(block).collides) + continue; AABB blockColl{ x,y,z,1.0,1.0,1.0 }; if (TestCollision(entityCollBox, blockColl)) { return { true }; |