summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLaG1924 <12997935+LaG1924@users.noreply.github.com>2019-01-27 06:10:33 +0100
committerLaG1924 <12997935+LaG1924@users.noreply.github.com>2019-01-27 06:10:33 +0100
commite2479261c56c8d1a64b51ad4f084ad3c18505359 (patch)
tree5256688eaec174cebdcf873f5247ece28ec45b9c
parentAdded slow GameState sync (diff)
downloadAltCraft-e2479261c56c8d1a64b51ad4f084ad3c18505359.tar
AltCraft-e2479261c56c8d1a64b51ad4f084ad3c18505359.tar.gz
AltCraft-e2479261c56c8d1a64b51ad4f084ad3c18505359.tar.bz2
AltCraft-e2479261c56c8d1a64b51ad4f084ad3c18505359.tar.lz
AltCraft-e2479261c56c8d1a64b51ad4f084ad3c18505359.tar.xz
AltCraft-e2479261c56c8d1a64b51ad4f084ad3c18505359.tar.zst
AltCraft-e2479261c56c8d1a64b51ad4f084ad3c18505359.zip
-rw-r--r--src/GameState.cpp17
-rw-r--r--src/GameState.hpp19
-rw-r--r--src/GlobalState.cpp11
-rw-r--r--src/GlobalState.hpp4
-rw-r--r--src/Render.cpp6
-rw-r--r--src/RendererEntity.cpp4
-rw-r--r--src/RendererEntity.hpp3
-rw-r--r--src/RendererWorld.cpp26
-rw-r--r--src/RendererWorld.hpp8
-rw-r--r--src/Section.cpp2
-rw-r--r--src/World.cpp11
-rw-r--r--src/World.hpp2
12 files changed, 51 insertions, 62 deletions
diff --git a/src/GameState.cpp b/src/GameState.cpp
index df227b3..ace2488 100644
--- a/src/GameState.cpp
+++ b/src/GameState.cpp
@@ -10,8 +10,6 @@ void GameState::Update(float deltaTime) {
if (!gameStatus.isGameStarted)
return;
- std::lock_guard<std::mutex> guard(accessMutex);
-
std::chrono::steady_clock clock;
static auto timeOfPreviousSendedPacket(clock.now());
auto delta = clock.now() - timeOfPreviousSendedPacket;
@@ -69,7 +67,6 @@ void GameState::Update(float deltaTime) {
}
void GameState::UpdatePacket(std::shared_ptr<Packet> ptr) {
- std::lock_guard<std::mutex> guard(accessMutex);
switch ((PacketNamePlayCB)ptr->GetPacketId()) {
case SpawnObject: {
auto packet = std::static_pointer_cast<PacketSpawnObject>(ptr);
@@ -490,8 +487,6 @@ void GameState::HandleMovement(GameState::MoveType direction, float deltaTime) {
if (!gameStatus.isGameStarted)
return;
- std::lock_guard<std::mutex> guard(accessMutex);
-
const double playerSpeed = 43;
float velocity = playerSpeed * deltaTime;
@@ -548,8 +543,6 @@ void GameState::HandleRotation(double yaw, double pitch) {
if (!gameStatus.isGameStarted)
return;
- std::lock_guard<std::mutex> guard(accessMutex);
-
double playerYaw = Entity::DecodeYaw(player->yaw);
double playerPitch = Entity::DecodePitch(player->pitch);
playerYaw += yaw;
@@ -563,8 +556,6 @@ void GameState::HandleRotation(double yaw, double pitch) {
}
glm::mat4 GameState::GetViewMatrix() {
- std::lock_guard<std::mutex> guard(accessMutex);
-
double playerYaw = Entity::DecodeYaw(player->yaw);
double playerPitch = Entity::DecodePitch(player->pitch);
glm::vec3 front, right, worldUp, up;
@@ -589,8 +580,6 @@ void GameState::StartDigging() {
if (!selectionStatus.isBlockSelected)
return;
- std::lock_guard<std::mutex> guard(accessMutex);
-
auto packetStart = std::make_shared<PacketPlayerDigging>(0, selectionStatus.selectedBlock, 1);
auto packet = std::static_pointer_cast<Packet>(packetStart);
PUSH_EVENT("SendPacket", packet);
@@ -599,8 +588,6 @@ void GameState::StartDigging() {
}
void GameState::FinishDigging() {
- std::lock_guard<std::mutex> guard(accessMutex);
-
auto packetFinish = std::make_shared<PacketPlayerDigging>(2, selectionStatus.selectedBlock, 1);
auto packet = std::static_pointer_cast<Packet>(packetFinish);
PUSH_EVENT("SendPacket", packet);
@@ -612,8 +599,6 @@ void GameState::FinishDigging() {
// send_packet(packet_type=start_digging_packet)
// remove_delayed_action(finish_digging)
void GameState::CancelDigging() {
- std::lock_guard<std::mutex> guard(accessMutex);
-
auto packetCancel = std::make_shared<PacketPlayerDigging>(1, selectionStatus.selectedBlock, 1);
auto packet = std::static_pointer_cast<Packet>(packetCancel);
PUSH_EVENT("SendPacket", packet);
@@ -652,8 +637,6 @@ void GameState::PlaceBlock() {
if (!selectionStatus.isBlockSelected)
return;
- std::lock_guard<std::mutex> guard(accessMutex);
-
BlockFacing face = detectHitFace(selectionStatus.raycastHit, selectionStatus.selectedBlock);
auto packetPlace = std::make_shared<PacketPlayerBlockPlacement>(
selectionStatus.selectedBlock, (unsigned char)face, 0, 0, 0, 0);
diff --git a/src/GameState.hpp b/src/GameState.hpp
index dbea2c3..41e7080 100644
--- a/src/GameState.hpp
+++ b/src/GameState.hpp
@@ -68,8 +68,6 @@ class GameState {
Window playerInventory;
std::vector<Window> openedWindows;
-
- std::mutex accessMutex;
public:
void Update(float deltaTime);
@@ -95,37 +93,30 @@ public:
glm::mat4 GetViewMatrix();
inline Entity *GetPlayer() {
- std::lock_guard<std::mutex> guard(accessMutex);
return player;
}
- inline World GetWorld() {
- std::lock_guard<std::mutex> guard(accessMutex);
+ inline World &GetWorld() {
return world;
}
- inline TimeStatus GetTimeStatus() {
- std::lock_guard<std::mutex> guard(accessMutex);
+ inline TimeStatus &GetTimeStatus() {
return timeStatus;
}
- inline GameStatus GetGameStatus() {
- std::lock_guard<std::mutex> guard(accessMutex);
+ inline GameStatus &GetGameStatus() {
return gameStatus;
}
- inline PlayerStatus GetPlayerStatus() {
- std::lock_guard<std::mutex> guard(accessMutex);
+ inline PlayerStatus &GetPlayerStatus() {
return playerStatus;
}
- inline SelectionStatus GetSelectionStatus() {
- std::lock_guard<std::mutex> guard(accessMutex);
+ inline SelectionStatus &GetSelectionStatus() {
return selectionStatus;
}
inline Window &GetInventory() {
- std::lock_guard<std::mutex> guard(accessMutex);
return playerInventory;
}
};
diff --git a/src/GlobalState.cpp b/src/GlobalState.cpp
index 9675d4b..afa1a56 100644
--- a/src/GlobalState.cpp
+++ b/src/GlobalState.cpp
@@ -9,6 +9,7 @@
//Global game variables
std::unique_ptr<NetworkClient> nc;
std::unique_ptr<GameState> gs;
+std::shared_ptr<GameState> gsReadOnly;
std::unique_ptr<Render> render;
bool isRunning;
bool isPhysRunning;
@@ -16,6 +17,7 @@ EventListener listener;
bool isMoving[5] = { 0,0,0,0,0 };
std::thread threadPhys;
State state;
+std::mutex gsCopyMutex;
void PhysExec();
@@ -194,6 +196,10 @@ void PhysExec() {
listener.HandleAllEvents();
+ gsCopyMutex.lock();
+ gsReadOnly = std::make_shared<GameState>(*gs.get());
+ gsCopyMutex.unlock();
+
timer.Update();
}
}
@@ -212,8 +218,9 @@ void GlobalState::Exec() {
render.reset();
}
-GameState *GlobalState::GetGameState() {
- return gs.get();
+std::shared_ptr<GameState> GlobalState::GetGameState() {
+ std::lock_guard<std::mutex> guard(gsCopyMutex);
+ return gsReadOnly;
}
Render *GlobalState::GetRender() {
diff --git a/src/GlobalState.hpp b/src/GlobalState.hpp
index b3b4635..bc7224f 100644
--- a/src/GlobalState.hpp
+++ b/src/GlobalState.hpp
@@ -1,5 +1,7 @@
#pragma once
+#include <memory>
+
class NetworkClient;
class GameState;
class Render;
@@ -15,7 +17,7 @@ enum class State {
};
struct GlobalState {
- static GameState *GetGameState();
+ static std::shared_ptr<GameState> GetGameState();
static Render *GetRender();
static void Exec();
static State GetState();
diff --git a/src/Render.cpp b/src/Render.cpp
index 5432979..0268bee 100644
--- a/src/Render.cpp
+++ b/src/Render.cpp
@@ -348,6 +348,9 @@ void Render::SetMouseCapture(bool IsCaptured) {
}
void Render::Update() {
+ if (world)
+ world->UpdateGameState(GlobalState::GetGameState());
+
HandleEvents();
if (HasFocus && GlobalState::GetState() == State::Playing) UpdateKeyboard();
if (isMouseCaptured) HandleMouseCapture();
@@ -384,8 +387,7 @@ void Render::RenderGui() {
if (world) {
Entity *playerPtr = world->GameStatePtr()->GetPlayer();
SelectionStatus selectionStatus = world->GameStatePtr()->GetSelectionStatus();
- World worldObj = world->GameStatePtr()->GetWorld();
- World *worldPtr = &worldObj;
+ World *worldPtr = &world->GameStatePtr()->GetWorld();
ImGui::Text("TPS: %.1f (%.2fms)", 1000.0f / gameTime, gameTime);
ImGui::Text("Sections loaded: %d", (int) DebugInfo::totalSections);
diff --git a/src/RendererEntity.cpp b/src/RendererEntity.cpp
index fef7dbd..698c934 100644
--- a/src/RendererEntity.cpp
+++ b/src/RendererEntity.cpp
@@ -123,9 +123,9 @@ RendererEntity::RendererEntity(unsigned int id)
RendererEntity::~RendererEntity() {
}
-void RendererEntity::Render(RenderState & renderState) {
+void RendererEntity::Render(RenderState& renderState, World *world) {
glm::mat4 model = glm::mat4(1.0);
- Entity entity = GlobalState::GetGameState()->GetWorld().GetEntity(entityId);
+ Entity &entity = world->GetEntity(entityId);
model = glm::translate(model, entity.pos.glm());
model = glm::translate(model, glm::vec3(0, entity.height / 2.0, 0));
model = glm::scale(model, glm::vec3(entity.width, entity.height, entity.width));
diff --git a/src/RendererEntity.hpp b/src/RendererEntity.hpp
index 961ef45..7f42f7d 100644
--- a/src/RendererEntity.hpp
+++ b/src/RendererEntity.hpp
@@ -3,6 +3,7 @@
#include <GL/glew.h>
class RenderState;
+class World;
class RendererEntity {
unsigned int entityId;
@@ -10,7 +11,7 @@ public:
RendererEntity(unsigned int id);
~RendererEntity();
- void Render(RenderState& renderState);
+ void Render(RenderState& renderState, World *world);
static GLuint GetVao();
};
diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp
index d798343..1743a25 100644
--- a/src/RendererWorld.cpp
+++ b/src/RendererWorld.cpp
@@ -36,8 +36,6 @@ void RendererWorld::WorkerFunction(size_t workerId) {
}
void RendererWorld::ParseQueueUpdate() {
- World world = gs->GetWorld();
-
while (!parseQueue.empty()) {
size_t id = 0;
for (; id < RendererWorld::parsingBufferSize && parsing[id].parsing; ++id) {}
@@ -54,13 +52,13 @@ void RendererWorld::ParseQueueUpdate() {
vec.y -= 4500;
}
- parsing[id].data.section = world.GetSection(vec);
- parsing[id].data.north = world.GetSection(vec + Vector(0, 0, 1));
- parsing[id].data.south = world.GetSection(vec + Vector(0, 0, -1));
- parsing[id].data.west = world.GetSection(vec + Vector(1, 0, 0));
- parsing[id].data.east = world.GetSection(vec + Vector(-1, 0, 0));
- parsing[id].data.bottom = world.GetSection(vec + Vector(0, -1, 0));
- parsing[id].data.top = world.GetSection(vec + Vector(0, 1, 0));
+ parsing[id].data.section = gs->GetWorld().GetSection(vec);
+ parsing[id].data.north = gs->GetWorld().GetSection(vec + Vector(0, 0, 1));
+ parsing[id].data.south = gs->GetWorld().GetSection(vec + Vector(0, 0, -1));
+ parsing[id].data.west = gs->GetWorld().GetSection(vec + Vector(1, 0, 0));
+ parsing[id].data.east = gs->GetWorld().GetSection(vec + Vector(-1, 0, 0));
+ parsing[id].data.bottom = gs->GetWorld().GetSection(vec + Vector(0, -1, 0));
+ parsing[id].data.top = gs->GetWorld().GetSection(vec + Vector(0, 1, 0));
parsing[id].parsing = true;
@@ -76,8 +74,6 @@ void RendererWorld::ParseQeueueRemoveUnnecessary() {
elements.clear();
elements.reserve(size);
- World world = gs->GetWorld();
-
for (size_t i = 0; i < size; i++) {
Vector vec = parseQueue.front();
parseQueue.pop();
@@ -90,7 +86,7 @@ void RendererWorld::ParseQeueueRemoveUnnecessary() {
if (std::find(elements.begin(), elements.end(), vec) != elements.end())
continue;
- const Section& section = world.GetSection(vec);
+ const Section& section = gs->GetWorld().GetSection(vec);
bool skip = false;
@@ -150,7 +146,7 @@ void RendererWorld::UpdateAllSections(VectorF playerPos) {
}
}
-RendererWorld::RendererWorld(GameState* ptr) {
+RendererWorld::RendererWorld(std::shared_ptr<GameState> ptr) {
gs = ptr;
MaxRenderingDistance = 2;
numOfWorkers = _max(1, (signed int) std::thread::hardware_concurrency() - 2);
@@ -291,7 +287,7 @@ void RendererWorld::Render(RenderState & renderState) {
renderState.SetActiveVao(RendererEntity::GetVao());
for (auto& it : entities) {
- it.Render(renderState);
+ it.Render(renderState, &gs->GetWorld());
}
//Render selected block
@@ -449,5 +445,5 @@ void RendererWorld::Update(double timeToUpdate) {
}
GameState* RendererWorld::GameStatePtr() {
- return gs;
+ return gs.get();
}
diff --git a/src/RendererWorld.hpp b/src/RendererWorld.hpp
index dbdd852..5148964 100644
--- a/src/RendererWorld.hpp
+++ b/src/RendererWorld.hpp
@@ -27,7 +27,7 @@ class RendererWorld {
};
//General
- GameState *gs;
+ std::shared_ptr<GameState> gs;
std::unique_ptr<EventListener> listener;
size_t numOfWorkers;
size_t currentWorker = 0;
@@ -50,7 +50,7 @@ class RendererWorld {
Texture *skyTexture;
RendererSky rendererSky;
public:
- RendererWorld(GameState* ptr);
+ RendererWorld(std::shared_ptr<GameState> ptr);
~RendererWorld();
void Render(RenderState& renderState);
@@ -63,4 +63,8 @@ public:
GameState *GameStatePtr();
int culledSections = 0;
+
+ inline void UpdateGameState(std::shared_ptr<GameState> newPtr) {
+ gs = newPtr;
+ }
}; \ No newline at end of file
diff --git a/src/Section.cpp b/src/Section.cpp
index c8c67dc..29a6a0e 100644
--- a/src/Section.cpp
+++ b/src/Section.cpp
@@ -45,6 +45,7 @@ Section::Section(Vector pos, unsigned char bitsPerBlock, std::vector<unsigned sh
std::copy(skyData.begin(), skyData.end(), sky);
hash = -1;
+ CalculateHash();
}
BlockId Section::GetBlockId(Vector pos) const {
@@ -109,6 +110,7 @@ unsigned char Section::GetBlockSkyLight(Vector pos) const
void Section::SetBlockId(Vector pos, BlockId value) {
overrideList[pos] = value;
hash = -1;
+ CalculateHash();
}
Vector Section::GetPosition() const {
diff --git a/src/World.cpp b/src/World.cpp
index ffb657f..928c56a 100644
--- a/src/World.cpp
+++ b/src/World.cpp
@@ -14,7 +14,7 @@ void World::ParseChunkData(std::shared_ptr<PacketChunkData> packet) {
for (int i = 0; i < 16; i++) {
if (bitmask[i]) {
Vector chunkPosition = Vector(packet->ChunkX, i, packet->ChunkZ);
- Section section = ParseSection(&chunkData, chunkPosition);
+ auto section = std::make_shared<Section>(ParseSection(&chunkData, chunkPosition));
if (packet->GroundUpContinuous) {
if (!sections.insert(std::make_pair(chunkPosition, section)).second) {
@@ -125,7 +125,7 @@ const Section &World::GetSection(Vector sectionPos) {
return fallbackSection;
}
else {
- return result->second;
+ return *result->second;
}
}
@@ -312,7 +312,7 @@ void World::ParseChunkData(std::shared_ptr<PacketMultiBlockChange> packet) {
}
void World::ParseChunkData(std::shared_ptr<PacketUnloadChunk> packet) {
- std::vector<std::map<Vector, Section>::iterator> toRemove;
+ std::vector<std::map<Vector, std::shared_ptr<Section>>::iterator> toRemove;
for (auto it = sections.begin(); it != sections.end(); ++it) {
if (it->first.x == packet->ChunkX && it->first.z == packet->ChunkZ)
toRemove.push_back(it);
@@ -345,8 +345,9 @@ void World::SetBlockId(Vector pos, BlockId block) {
std::floor(pos.y / 16.0),
std::floor(pos.z / 16.0));
Vector blockPos = pos - (sectionPos * 16);
- Section* section = GetSectionPtr(sectionPos);
+ auto section = std::make_shared<Section>(*GetSectionPtr(sectionPos));
section->SetBlockId(blockPos, block);
+ sections[sectionPos] = section;
PUSH_EVENT("ChunkChanged",sectionPos);
if (blockPos.x == 0)
PUSH_EVENT("ChunkChangedForce", sectionPos + Vector(-1, 0, 0));
@@ -376,7 +377,7 @@ Section *World::GetSectionPtr(Vector position) {
if (it == sections.end())
return nullptr;
- return &it->second;
+ return it->second.get();
}
Entity* World::GetEntityPtr(unsigned int EntityId) {
diff --git a/src/World.hpp b/src/World.hpp
index 94e17f6..591e7a7 100644
--- a/src/World.hpp
+++ b/src/World.hpp
@@ -28,7 +28,7 @@ struct RaycastResult {
class World {
int dimension = 0;
- std::map<Vector, Section> sections;
+ std::map<Vector, std::shared_ptr<Section>> sections;
Section ParseSection(StreamInput *data, Vector position);