From 04f36197e4cb4b9ba5944e1c70b884ef035c6cbc Mon Sep 17 00:00:00 2001 From: UIS Date: Sun, 2 Aug 2020 00:05:43 +0300 Subject: Added CMakeLists.txt.user to .gitignore --- .gitignore | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 7df2bc0..5f653e6 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,7 @@ cmake-build-release/ #Visual Studio .vs/ -CMakeSettings.json \ No newline at end of file +CMakeSettings.json + +#Qt Creator +CMakeLists.txt.user -- cgit v1.2.3 From b16afa14fe7ce4f8f0d8706358830f3ec518ce61 Mon Sep 17 00:00:00 2001 From: UIS Date: Thu, 6 Aug 2020 19:47:19 +0300 Subject: Minor network fixes --- src/Network.cpp | 4 ++-- src/NetworkClient.hpp | 4 +--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Network.cpp b/src/Network.cpp index 4280b50..4332c29 100644 --- a/src/Network.cpp +++ b/src/Network.cpp @@ -224,6 +224,8 @@ std::shared_ptr Network::ParsePacketPlay(PacketNamePlayCB id) { break; case OpenSignEditor: break; + case CraftRecipeResponse: + break; case PlayerAbilitiesCB: break; case CombatEvent: @@ -258,7 +260,6 @@ std::shared_ptr Network::ParsePacketPlay(PacketNamePlayCB id) { break; case EntityVelocity: return std::make_shared(); - break; case EntityEquipment: break; case SetExperience: @@ -287,7 +288,6 @@ std::shared_ptr Network::ParsePacketPlay(PacketNamePlayCB id) { break; case EntityTeleport: return std::make_shared(); - break; case EntityProperties: break; case EntityEffect: diff --git a/src/NetworkClient.hpp b/src/NetworkClient.hpp index f372468..bde5fdf 100644 --- a/src/NetworkClient.hpp +++ b/src/NetworkClient.hpp @@ -12,8 +12,6 @@ enum ConnectionState : unsigned char; class NetworkClient { std::unique_ptr network; - std::queue > toSend; - std::queue > toReceive; ConnectionState state; int compressionThreshold = -1; std::chrono::steady_clock::time_point timeOfLastKeepAlivePacket; @@ -23,4 +21,4 @@ class NetworkClient { public: NetworkClient(std::string address, unsigned short port, std::string username); ~NetworkClient(); -}; \ No newline at end of file +}; -- cgit v1.2.3 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 From 70c3ade4dd87907506ab40eb8e1cf04e4db3779a Mon Sep 17 00:00:00 2001 From: UIS Date: Sun, 20 Sep 2020 00:59:00 +0300 Subject: Shader optimization, removed unused argument from GetBlockInfo function --- cwd/assets/altcraft/shaders/vert/face.vs | 8 +++----- src/Block.cpp | 4 ++-- src/Block.hpp | 2 +- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/cwd/assets/altcraft/shaders/vert/face.vs b/cwd/assets/altcraft/shaders/vert/face.vs index 044c012..30ae0d7 100644 --- a/cwd/assets/altcraft/shaders/vert/face.vs +++ b/cwd/assets/altcraft/shaders/vert/face.vs @@ -22,11 +22,9 @@ uniform mat4 projView; vec3 TransformTextureCoord(vec4 TextureAtlasCoords, vec2 UvCoords, float Layer) { float x = TextureAtlasCoords.x; float y = TextureAtlasCoords.y; - float w = TextureAtlasCoords.z; +// float w = TextureAtlasCoords.z; float h = TextureAtlasCoords.w; - vec2 A = vec2(x, 1 - y - h); - vec2 B = vec2(x + w, 1 - y); - vec2 transformed = A + UvCoords * (B - A); + vec2 transformed = vec2(x, 1 - y - h) + UvCoords * TextureAtlasCoords.zw; return vec3(transformed.x, transformed.y, Layer); } @@ -42,7 +40,7 @@ void main() texturePos.w = frameHeight; texturePos.y = texturePos.y + currentFrame * frameHeight; - vs_out.UvPosition = vec2(UvCoordinates.x,UvCoordinates.y); + vs_out.UvPosition = UvCoordinates; vs_out.Texture = TransformTextureCoord(texturePos,UvCoordinates,TextureLayer); vs_out.Color = color; vs_out.Light = light; diff --git a/src/Block.cpp b/src/Block.cpp index b81a762..85870f6 100644 --- a/src/Block.cpp +++ b/src/Block.cpp @@ -11,12 +11,12 @@ static std::map staticBlockInfo; BlockInfo WTFBlock{ true, "", "" }; void RegisterStaticBlockInfo(BlockId blockId, BlockInfo blockInfo) { - //NOTE: It can be made thread-safe using incrementer + //NOTE: It can be made thread-safe by using atomic incrementer staticBlockInfo[blockId] = blocks.size(); blocks.push_back(blockInfo); } -BlockInfo* GetBlockInfo(BlockId blockId, Vector blockPos) { +BlockInfo* GetBlockInfo(BlockId blockId) { auto it = staticBlockInfo.find(blockId); if (it != staticBlockInfo.end()) return &blocks.data()[it->second]; diff --git a/src/Block.hpp b/src/Block.hpp index a9fd881..0fd0e89 100644 --- a/src/Block.hpp +++ b/src/Block.hpp @@ -49,4 +49,4 @@ struct BlockInfo { void RegisterStaticBlockInfo(BlockId blockId, BlockInfo blockInfo); -BlockInfo* GetBlockInfo(BlockId blockId, Vector blockPos = Vector(0,0,0)); +BlockInfo* GetBlockInfo(BlockId blockId); -- cgit v1.2.3 From 33c756b38e94cef68b3f842c1c2f8bad9d4447a2 Mon Sep 17 00:00:00 2001 From: UIS Date: Thu, 10 Dec 2020 15:23:59 +0300 Subject: Fix multithreading Reorder locks in Event.cpp --- src/AssetManager.cpp | 4 +--- src/Event.cpp | 43 +++++++++++++++++++++++-------------------- src/Event.hpp | 2 +- src/RendererWorld.cpp | 2 +- 4 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index e3e0e05..63dc596 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -658,9 +658,7 @@ BlockFaces &AssetManager::GetBlockModelByBlockId(BlockId block) { blockFaces.faceDirectionVector[i] = Vector(roundf(vec.x), roundf(vec.y), roundf(vec.z)); } - blockIdToBlockFaces.insert(std::make_pair(block, blockFaces)); - - return blockIdToBlockFaces.find(block)->second; + return blockIdToBlockFaces.insert(std::make_pair(block, blockFaces)).first->second; } std::string AssetManager::GetAssetNameByBlockId(BlockId block) { diff --git a/src/Event.cpp b/src/Event.cpp index ea09af3..1ca933f 100644 --- a/src/Event.cpp +++ b/src/Event.cpp @@ -17,7 +17,7 @@ EventListener::~EventListener() { void EventListener::HandleEvent() { OPTICK_EVENT(); - if (!NotEmpty()) + if (Empty()) return; std::lock_guard eventsLock (eventsMutex); @@ -31,11 +31,14 @@ void EventListener::HandleEvent() { void EventListener::HandleAllEvents() { OPTICK_EVENT(); - if (!NotEmpty()) - return; + //This mutexes will locked in PollEvents std::lock_guard eventsLock (eventsMutex); - std::lock_guard handlersLock (handlersMutex); + std::lock_guard handlersLock (handlersMutex); + + if (Empty()) + return; + while (!events.empty()) { Event event = events.front(); events.pop(); @@ -45,11 +48,10 @@ void EventListener::HandleAllEvents() { } } -bool EventListener::NotEmpty() { - PollEvents(); +bool EventListener::Empty() { std::lock_guard eventsLock (eventsMutex); - bool ret = !events.empty(); - return ret; + PollEvents(); + return events.empty(); } void EventListener::RegisterHandler(size_t eventId, const EventListener::HandlerType &data) { @@ -59,16 +61,17 @@ void EventListener::RegisterHandler(size_t eventId, const EventListener::Handler void EventListener::PollEvents() { OPTICK_EVENT(); - std::lock_guard rawLock (rawEventsMutex); - if (rawEvents.empty()) - return; - - std::lock_guard eventsLock (eventsMutex); - std::lock_guard handlersLock (handlersMutex); - while (!rawEvents.empty()) { - Event event = rawEvents.front(); - rawEvents.pop(); - if (handlers[event.id]) - events.push(event); - } + std::lock_guard eventsLock (eventsMutex); + std::lock_guard handlersLock (handlersMutex);//To prevent inverse lock order + + std::lock_guard rawLock (rawEventsMutex); + if (rawEvents.empty()) + return; + + while (!rawEvents.empty()) { + Event event = rawEvents.front(); + rawEvents.pop(); + if (handlers[event.id]) + events.push(event); + } } diff --git a/src/Event.hpp b/src/Event.hpp index 4e04a5a..b1594e7 100644 --- a/src/Event.hpp +++ b/src/Event.hpp @@ -79,7 +79,7 @@ public: void HandleAllEvents(); - bool NotEmpty(); + bool Empty(); void RegisterHandler(size_t eventId, const HandlerType &data); diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index b433609..e3ef738 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -33,7 +33,7 @@ void RendererWorld::WorkerFunction(size_t workerId) { LoopExecutionTimeController timer(std::chrono::milliseconds(50)); while (isRunning) { - while (tasksListener.NotEmpty() && isRunning) + while (!tasksListener.Empty() && isRunning) tasksListener.HandleEvent(); timer.Update(); } -- cgit v1.2.3 From 8080f3732ead03094e764fe733f054417772eafd Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Wed, 23 Jun 2021 06:02:26 +0500 Subject: Fixed entity bug found by @uis246 --- src/GameState.cpp | 3 --- src/Packet.hpp | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/GameState.cpp b/src/GameState.cpp index 3d1714b..be408dd 100644 --- a/src/GameState.cpp +++ b/src/GameState.cpp @@ -306,8 +306,6 @@ void GameState::UpdatePacket(std::shared_ptr ptr) { auto packet = std::static_pointer_cast(ptr); Entity &entity = world.GetEntity(packet->EntityId); entity.pos = entity.pos + Entity::DecodeDeltaPos(packet->DeltaX, packet->DeltaY, packet->DeltaZ); - if (entity.entityId != 0) - LOG(INFO) << "M: " << packet->EntityId; break; } @@ -325,7 +323,6 @@ void GameState::UpdatePacket(std::shared_ptr ptr) { Entity &entity = world.GetEntity(packet->EntityId); entity.pitch = packet->Pitch / 256.0; entity.yaw = packet->Yaw / 256.0; - //LOG(INFO) << "L: " << packet->EntityId; break; } diff --git a/src/Packet.hpp b/src/Packet.hpp index ccc486f..56a2e5e 100644 --- a/src/Packet.hpp +++ b/src/Packet.hpp @@ -89,10 +89,10 @@ enum PacketNamePlayCB { Particle, JoinGame, Map, + EntityCB, EntityRelativeMove, EntityLookAndRelativeMove, EntityLook, - EntityCB, VehicleMove, OpenSignEditor, CraftRecipeResponse, -- cgit v1.2.3