From 646f77ec6bc27af231b6ff8974e631b86188beb6 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Sun, 19 May 2019 23:03:48 +0500 Subject: Implemented block-api --- cwd/assets/altcraft/scripts/blocks.lua | 22 +++++++++++++++-- cwd/assets/altcraft/scripts/init.lua | 9 +++++-- src/AssetManager.cpp | 8 +++--- src/Block.cpp | 19 ++++++++++++++ src/Block.hpp | 14 ++++++++++- src/Plugin.cpp | 45 +++++++++++++++++++++++++++++++++- src/Plugin.hpp | 6 +++++ src/World.cpp | 6 ++--- 8 files changed, 116 insertions(+), 13 deletions(-) diff --git a/cwd/assets/altcraft/scripts/blocks.lua b/cwd/assets/altcraft/scripts/blocks.lua index ad00742..07a1f77 100644 --- a/cwd/assets/altcraft/scripts/blocks.lua +++ b/cwd/assets/altcraft/scripts/blocks.lua @@ -1,2 +1,20 @@ -print("Hello from altcraft/blocks!") -return "CONTENT OF ac/blocks" \ No newline at end of file +local function GetBlockInfo(blockPos) + print("Request data for "..blockPos) + local bi = BlockInfo.new() + bi.collides = true + bi.blockstate = "" + bi.variant = "" + return bi +end + +local function RegisterBlocks() + AC.RegisterBlock(BlockId.new(1,0), true, "stone", "normal") + AC.RegisterBlock(BlockId.new(2,0), true, "grass", "snowy=false") + AC.RegisterBlock(BlockId.new(3,0), true, "dirt", "normal") + AC.RegisterBlock(BlockId.new(31,1), false, "tall_grass", "normal") +end + +return { + GetBlockInfo = GetBlockInfo, + RegisterBlocks = RegisterBlocks, +} \ No newline at end of file diff --git a/cwd/assets/altcraft/scripts/init.lua b/cwd/assets/altcraft/scripts/init.lua index f001017..5021a00 100644 --- a/cwd/assets/altcraft/scripts/init.lua +++ b/cwd/assets/altcraft/scripts/init.lua @@ -5,6 +5,7 @@ local plugin = { onUnload = nil, onChangeState = nil, onTick = nil, + onRequestBlockInfo = nil, } function plugin.onLoad () @@ -32,8 +33,12 @@ function plugin.onTick (deltaTime) end end -blocks = require("altcraft/blocks") -print("From init: "..blocks) +local blocks = require("altcraft/blocks") +blocks.RegisterBlocks() + +function plugin.onRequestBlockInfo(blockPos) + return blocks.GetBlockInfo(blockPos) +end AC.RegisterPlugin(plugin) plugin = nil \ No newline at end of file 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("/minecraft/blockstates/" + blockStateName.first); + BlockInfo blockInfo = GetBlockInfo(block); + AssetBlockState *asset = GetAsset("/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 + +#include "Plugin.hpp" + std::pair TransformBlockIdToBlockStateName(BlockId blockId) { switch (blockId.id) { case 1: { @@ -523,3 +527,18 @@ std::pair TransformBlockIdToBlockStateName(BlockId blo return std::make_pair("", ""); } + +std::map 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 #include +#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 TransformBlockIdToBlockStateName(BlockId blockId); \ No newline at end of file +std::pair 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 onUnload; const std::function onChangeState; const std::function onTick; + const std::function onRequestBlockInfo; }; @@ -37,7 +38,8 @@ namespace PluginApi { plugin["onLoad"].get_or(std::function()), plugin["onUnload"].get_or(std::function()), plugin["onChangeState"].get_or(std::function()), - plugin["onTick"].get_or(std::function()) + plugin["onTick"].get_or(std::function()), + plugin["onRequestBlockInfo"].get_or(std::function()), }; 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", + "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", + "collides", &BlockInfo::collides, + "blockstate", &BlockInfo::blockstate, + "variant", &BlockInfo::variant); + sol::table apiTable = lua["AC"].get_or_create(); 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 +#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 }; -- cgit v1.2.3