summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLaG1924 <12997935+LaG1924@users.noreply.github.com>2021-06-27 05:46:22 +0200
committerGitHub <noreply@github.com>2021-06-27 05:46:22 +0200
commitdd1323d398733f0f4a3b285b396ed3a47fb8eb96 (patch)
tree3064b292efd41a36b5655fd4d07ec42053463ffb /src
parentMerge pull request #60 from LaG1924/fix/memleak (diff)
parentFixed entity bug found by @uis246 (diff)
downloadAltCraft-dd1323d398733f0f4a3b285b396ed3a47fb8eb96.tar
AltCraft-dd1323d398733f0f4a3b285b396ed3a47fb8eb96.tar.gz
AltCraft-dd1323d398733f0f4a3b285b396ed3a47fb8eb96.tar.bz2
AltCraft-dd1323d398733f0f4a3b285b396ed3a47fb8eb96.tar.lz
AltCraft-dd1323d398733f0f4a3b285b396ed3a47fb8eb96.tar.xz
AltCraft-dd1323d398733f0f4a3b285b396ed3a47fb8eb96.tar.zst
AltCraft-dd1323d398733f0f4a3b285b396ed3a47fb8eb96.zip
Diffstat (limited to 'src')
-rw-r--r--src/AssetManager.cpp41
-rw-r--r--src/Block.cpp19
-rw-r--r--src/Block.hpp4
-rw-r--r--src/Event.cpp43
-rw-r--r--src/Event.hpp2
-rw-r--r--src/GameState.cpp3
-rw-r--r--src/Network.cpp4
-rw-r--r--src/NetworkClient.hpp4
-rw-r--r--src/Packet.hpp2
-rw-r--r--src/RendererWorld.cpp2
-rw-r--r--src/World.cpp6
11 files changed, 64 insertions, 66 deletions
diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp
index 7bcfaae..63dc596 100644
--- a/src/AssetManager.cpp
+++ b/src/AssetManager.cpp
@@ -27,6 +27,8 @@ std::unique_ptr<AssetTreeNode> assetTree;
std::unique_ptr<TextureAtlas> atlas;
std::map<BlockId, BlockFaces> 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<AssetBlockModel>("/minecraft/models/block/error")->blockModel.parsedFaces;
+ errorFaces.isBlock = GetAsset<AssetBlockModel>("/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<AssetBlockModel>("/minecraft/models/block/error")->blockModel.parsedFaces;
- blockFaces.isBlock = GetAsset<AssetBlockModel>("/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<AssetBlockState>("/minecraft/blockstates/" + blockInfo.blockstate);
+ BlockInfo *blockInfo = GetBlockInfo(block);
+ AssetBlockState *asset = GetAsset<AssetBlockState>("/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<AssetBlockModel>("/minecraft/models/block/" + model.modelName);
if (!assetModel)
- return GetBlockModelByBlockId(BlockId{ 7788,0 });
+ return errorFaces;
BlockFaces blockFaces;
blockFaces.transform = glm::mat4(1.0);
@@ -661,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/Block.cpp b/src/Block.cpp
index 48c099d..85870f6 100644
--- a/src/Block.cpp
+++ b/src/Block.cpp
@@ -1,20 +1,25 @@
#include "Block.hpp"
#include <map>
+#include <vector>
#include "Plugin.hpp"
-std::map<BlockId, BlockInfo> staticBlockInfo;
+static std::vector<BlockInfo> blocks;
+static std::map<BlockId, size_t> staticBlockInfo;
+
+BlockInfo WTFBlock{ true, "", "" };
void RegisterStaticBlockInfo(BlockId blockId, BlockInfo blockInfo) {
- staticBlockInfo[blockId] = blockInfo;
+ //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 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..0fd0e89 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);
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<std::recursive_mutex> 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<std::recursive_mutex> eventsLock (eventsMutex);
- std::lock_guard<std::recursive_mutex> handlersLock (handlersMutex);
+ std::lock_guard<std::recursive_mutex> 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<std::recursive_mutex> 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<std::recursive_mutex> rawLock (rawEventsMutex);
- if (rawEvents.empty())
- return;
-
- std::lock_guard<std::recursive_mutex> eventsLock (eventsMutex);
- std::lock_guard<std::recursive_mutex> handlersLock (handlersMutex);
- while (!rawEvents.empty()) {
- Event event = rawEvents.front();
- rawEvents.pop();
- if (handlers[event.id])
- events.push(event);
- }
+ std::lock_guard<std::recursive_mutex> eventsLock (eventsMutex);
+ std::lock_guard<std::recursive_mutex> handlersLock (handlersMutex);//To prevent inverse lock order
+
+ std::lock_guard<std::recursive_mutex> 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/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<Packet> ptr) {
auto packet = std::static_pointer_cast<PacketEntityRelativeMove>(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<Packet> 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/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<Packet> Network::ParsePacketPlay(PacketNamePlayCB id) {
break;
case OpenSignEditor:
break;
+ case CraftRecipeResponse:
+ break;
case PlayerAbilitiesCB:
break;
case CombatEvent:
@@ -258,7 +260,6 @@ std::shared_ptr<Packet> Network::ParsePacketPlay(PacketNamePlayCB id) {
break;
case EntityVelocity:
return std::make_shared<PacketEntityVelocity>();
- break;
case EntityEquipment:
break;
case SetExperience:
@@ -287,7 +288,6 @@ std::shared_ptr<Packet> Network::ParsePacketPlay(PacketNamePlayCB id) {
break;
case EntityTeleport:
return std::make_shared<PacketEntityTeleport>();
- 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> network;
- std::queue <std::shared_ptr<Packet>> toSend;
- std::queue <std::shared_ptr<Packet>> 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
+};
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,
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();
}
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)) {