From e561e652ccae7d7fd214930000892cc24281f96c Mon Sep 17 00:00:00 2001 From: UIS Date: Fri, 14 Aug 2020 01:38:23 +0300 Subject: Use 12+4 bits for BlockId, GetBlockInfo return pointer instead value --- src/AssetManager.cpp | 37 +++++++++++++++++-------------------- src/Block.cpp | 19 ++++++++++++------- src/Block.hpp | 4 ++-- src/World.cpp | 6 +++--- 4 files changed, 34 insertions(+), 32 deletions(-) diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 7bcfaae..e3e0e05 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -27,6 +27,8 @@ std::unique_ptr assetTree; std::unique_ptr atlas; std::map blockIdToBlockFaces; +BlockFaces errorFaces; + void LoadIds(); void LoadAssets(); void LoadTextures(); @@ -68,6 +70,13 @@ void AssetManager::InitAssetManager() void AssetManager::InitPostRml() { LoadScripts(); + + errorFaces.transform = glm::mat4(1.0); + errorFaces.faces = GetAsset("/minecraft/models/block/error")->blockModel.parsedFaces; + errorFaces.isBlock = GetAsset("/minecraft/models/block/error")->blockModel.IsBlock; + for (int i = 0; i < FaceDirection::none; i++) { + errorFaces.faceDirectionVector[i] = FaceDirectionVector[i]; + } } void LoadIds() { @@ -606,35 +615,23 @@ BlockFaces &AssetManager::GetBlockModelByBlockId(BlockId block) { if (it != blockIdToBlockFaces.end()) return it->second; - if (block.id == 7788) { - BlockFaces blockFaces; - blockFaces.transform = glm::mat4(1.0); - blockFaces.faces = GetAsset("/minecraft/models/block/error")->blockModel.parsedFaces; - blockFaces.isBlock = GetAsset("/minecraft/models/block/error")->blockModel.IsBlock; - for (int i = 0; i < FaceDirection::none; i++) { - blockFaces.faceDirectionVector[i] = FaceDirectionVector[i]; - } - blockIdToBlockFaces.insert(std::make_pair(block, blockFaces)); - return blockIdToBlockFaces.find(block)->second; - } - - BlockInfo blockInfo = GetBlockInfo(block); - AssetBlockState *asset = GetAsset("/minecraft/blockstates/" + blockInfo.blockstate); + BlockInfo *blockInfo = GetBlockInfo(block); + AssetBlockState *asset = GetAsset("/minecraft/blockstates/" + blockInfo->blockstate); if (!asset) - return GetBlockModelByBlockId(BlockId{ 7788,0 }); + return errorFaces; BlockState &blockState = asset->blockState; - if (blockState.variants.find(blockInfo.variant) == blockState.variants.end()) - return GetBlockModelByBlockId(BlockId{ 7788,0 }); + if (blockState.variants.find(blockInfo->variant) == blockState.variants.end()) + return errorFaces; - BlockStateVariant &variant = blockState.variants[blockInfo.variant]; + BlockStateVariant &variant = blockState.variants[blockInfo->variant]; if (variant.models.empty()) - return GetBlockModelByBlockId(BlockId{ 7788,0 }); + return errorFaces; BlockStateVariant::Model &model = variant.models[0]; AssetBlockModel *assetModel = GetAsset("/minecraft/models/block/" + model.modelName); if (!assetModel) - return GetBlockModelByBlockId(BlockId{ 7788,0 }); + return errorFaces; BlockFaces blockFaces; blockFaces.transform = glm::mat4(1.0); diff --git a/src/Block.cpp b/src/Block.cpp index 48c099d..b81a762 100644 --- a/src/Block.cpp +++ b/src/Block.cpp @@ -1,20 +1,25 @@ #include "Block.hpp" #include +#include #include "Plugin.hpp" -std::map staticBlockInfo; +static std::vector blocks; +static std::map staticBlockInfo; + +BlockInfo WTFBlock{ true, "", "" }; void RegisterStaticBlockInfo(BlockId blockId, BlockInfo blockInfo) { - staticBlockInfo[blockId] = blockInfo; + //NOTE: It can be made thread-safe using incrementer + staticBlockInfo[blockId] = blocks.size(); + blocks.push_back(blockInfo); } -BlockInfo GetBlockInfo(BlockId blockId, Vector blockPos) { +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); + return &blocks.data()[it->second]; + else + return &WTFBlock; } diff --git a/src/Block.hpp b/src/Block.hpp index fbeeaeb..a9fd881 100644 --- a/src/Block.hpp +++ b/src/Block.hpp @@ -6,7 +6,7 @@ #include "Vector.hpp" struct BlockId { - unsigned short id : 13; + unsigned short id : 12; unsigned char state : 4; }; @@ -49,4 +49,4 @@ struct BlockInfo { void RegisterStaticBlockInfo(BlockId blockId, BlockInfo blockInfo); -BlockInfo GetBlockInfo(BlockId blockId, Vector blockPos = Vector(0,0,0)); \ No newline at end of file +BlockInfo* GetBlockInfo(BlockId blockId, Vector blockPos = Vector(0,0,0)); diff --git a/src/World.cpp b/src/World.cpp index e5e3fe8..6fcbdcd 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -114,7 +114,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 (!GetBlockInfo(block).collides) + if (!GetBlockInfo(block)->collides) continue; AABB blockColl{ (x + it.x * 16.0), (y + it.y * 16.0), @@ -198,8 +198,8 @@ void World::UpdatePhysics(float delta) { for (int z = blockZBegin; z <= blockZEnd; z++) { for (int x = blockXBegin; x <= blockXEnd; x++) { OPTICK_EVENT("testCollision"); - BlockId block = this->GetBlockId(Vector(x, y, z)); - if (block.id == 0 || !GetBlockInfo(block).collides) + BlockId block = this->GetBlockId(Vector(x, y, z)); + if (block.id == 0 || !GetBlockInfo(block)->collides) continue; AABB blockColl{ (double)x,(double)y,(double)z,1.0,1.0,1.0 }; if (TestCollision(entityCollBox, blockColl)) { -- cgit v1.2.3