diff --git a/src/def/texture/Font.cpp b/src/def/texture/Font.cpp index f458566f..52871df2 100644 --- a/src/def/texture/Font.cpp +++ b/src/def/texture/Font.cpp @@ -13,7 +13,7 @@ Font::Font(TextureAtlas& atlas, std::shared_ptr tex) : uint Font::getCharWidth(char c) { uint index = static_cast(c) - 32; - if (index >= AMOUNT_CHARS) { + if (index >= amountOfChars) { std::cout << Log::err << "Invalid char index!" << std::endl; return 0; } @@ -25,17 +25,17 @@ void Font::getCharWidths(TextureAtlas &atlas) { charWidths[0] = 2; - for (unsigned int i = 1; i < AMOUNT_CHARS; i++) { - glm::vec2 charPos = {i % 18 * CHAR_WIDTH, std::floor(i / 18) * CHAR_HEIGHT}; + for (unsigned int i = 1; i < amountOfChars; i++) { + glm::vec2 charPos = {i % 18 * charWidth, std::floor(i / 18) * charHeight}; uint xBase = static_cast(fontTex->pos.x) + static_cast(charPos.x); uint yBase = static_cast(fontTex->pos.y) + static_cast(charPos.y); unsigned short width = 0; - for (uint j = 0; j < CHAR_WIDTH; j++) { + for (uint j = 0; j < charWidth; j++) { bool empty = true; - for (uint k = 0; k < CHAR_HEIGHT; k++) { + for (uint k = 0; k < charHeight; k++) { uint xx = xBase + j; uint yy = yBase + k; @@ -55,20 +55,20 @@ void Font::getCharWidths(TextureAtlas &atlas) { glm::vec4 Font::getCharUVs(char c) { uint index = static_cast(c) - 32; - if (index >= AMOUNT_CHARS) { + if (index >= amountOfChars) { std::cout << Log::err << "Invalid char index!" << std::endl; return {}; } glm::vec4 uv; - glm::vec2 charPos = {((index % 18) * CHAR_WIDTH), - (std::floor(index / 18) * CHAR_HEIGHT)}; + glm::vec2 charPos = {((index % 18) * charWidth), + (std::floor(index / 18) * charHeight)}; uv.x = fontTex->uv.x + (charPos.x) / atlasSize.x; uv.y = fontTex->uv.y + (charPos.y) / atlasSize.y; uv.z = fontTex->uv.x + (charPos.x + getCharWidth(c) + 1) / atlasSize.x; - uv.w = fontTex->uv.y + (charPos.y + CHAR_HEIGHT) / atlasSize.y; + uv.w = fontTex->uv.y + (charPos.y + charHeight) / atlasSize.y; return uv; } diff --git a/src/def/texture/Font.h b/src/def/texture/Font.h index 80a3c269..78c1b3da 100644 --- a/src/def/texture/Font.h +++ b/src/def/texture/Font.h @@ -15,9 +15,9 @@ public: uint getCharWidth(char c); glm::vec4 getCharUVs(char c); - const static unsigned int AMOUNT_CHARS = 95; - const static unsigned int CHAR_WIDTH = 7; - const static unsigned int CHAR_HEIGHT = 9; + const static unsigned int amountOfChars = 95; + const static unsigned int charWidth = 7; + const static unsigned int charHeight = 9; private: void getCharWidths(TextureAtlas& atlas); diff --git a/src/game/hud/components/basic/GUIText.cpp b/src/game/hud/components/basic/GUIText.cpp index 59f010e7..8ca74c0f 100644 --- a/src/game/hud/components/basic/GUIText.cpp +++ b/src/game/hud/components/basic/GUIText.cpp @@ -37,7 +37,7 @@ void GUIText::setText(std::string text) { //Draw background & Measure Line Width int lineWidth = 0; int xOffset = 0, yOffset = 0; - int h = Font::CHAR_HEIGHT; + int h = Font::charHeight; for (unsigned int i = 0; i < this->text.length() + 1; i++) { char c = this->text[i]; @@ -82,7 +82,7 @@ void GUIText::setText(std::string text) { for (unsigned int i = 0; i < this->text.length() + 1; i++) { char c = this->text[i]; - unsigned int h = Font::CHAR_HEIGHT; + unsigned int h = Font::charHeight; if (c == '\n' || i == this->text.length()) { yOffset += (emptyLine) ? h / 2 : h + 2; diff --git a/src/server/conn/ServerPlayer.cpp b/src/server/conn/ServerPlayer.cpp index 4a606034..3f8ea8e2 100644 --- a/src/server/conn/ServerPlayer.cpp +++ b/src/server/conn/ServerPlayer.cpp @@ -42,4 +42,13 @@ float ServerPlayer::getAngle() { glm::vec3 ServerPlayer::getChunkPos() { return Space::Chunk::world::fromBlock(pos); -} \ No newline at end of file +} + +void ServerPlayer::setMapBlockIntegrity(glm::vec3 pos, unsigned long long integrity) { + mapBlockIntegrity[pos] = integrity; +} + +unsigned long long ServerPlayer::getMapBlockIntegrity(glm::vec3 pos) { + if (mapBlockIntegrity.count(pos)) return mapBlockIntegrity[pos]; + return 0; +} diff --git a/src/server/conn/ServerPlayer.h b/src/server/conn/ServerPlayer.h index c41aa29b..6569b844 100644 --- a/src/server/conn/ServerPlayer.h +++ b/src/server/conn/ServerPlayer.h @@ -5,6 +5,8 @@ #pragma once #include +#include +#include "../../util/Vec.h" #include "../../util/Space.h" class ServerPlayer { @@ -23,11 +25,16 @@ public: glm::vec3 mapBlock; glm::vec3 lastMapBlock; bool changedMapBlocks = true; + + void setMapBlockIntegrity(glm::vec3 pos, unsigned long long integrity); + unsigned long long getMapBlockIntegrity(glm::vec3 pos); private: std::string username; unsigned int connectID; glm::vec3 pos {}; float angle = 0; + + std::unordered_map mapBlockIntegrity {}; }; diff --git a/src/server/world/ServerWorld.cpp b/src/server/world/ServerWorld.cpp index fff0a20c..bcb8b4fe 100644 --- a/src/server/world/ServerWorld.cpp +++ b/src/server/world/ServerWorld.cpp @@ -90,21 +90,27 @@ void ServerWorld::update() { } auto finished = genStream->update(); - generatedChunks = (int)finished.size(); + generatedChunks = static_cast(finished.size()); + //TODO: Make this finish MapBlocks @ a time for (const auto& chunk : finished) { dimension.setChunk(chunk); + // TODO: Make this only happen once per mapblock, not for every chunk + glm::vec3 mapBlockPos = Space::MapBlock::world::fromChunk(chunk->pos); + unsigned long long mapBlockIntegrity = dimension.getMapBlockIntegrity(mapBlockPos); + for (auto& client : clientList.clients) { if (client->hasPlayer()) { - auto mapBlock = client->getPlayer().mapBlock; + auto playerMapBlock = client->getPlayer().mapBlock; std::pair bounds = { - {mapBlock.x - MB_GEN_H, mapBlock.y - MB_GEN_V, mapBlock.z - MB_GEN_H}, - {mapBlock.x + MB_GEN_H, mapBlock.y + MB_GEN_V, mapBlock.z + MB_GEN_H}}; + {playerMapBlock.x - MB_GEN_H, playerMapBlock.y - MB_GEN_V, playerMapBlock.z - MB_GEN_H}, + {playerMapBlock.x + MB_GEN_H, playerMapBlock.y + MB_GEN_V, playerMapBlock.z + MB_GEN_H}}; - if (isInBounds(Space::MapBlock::world::fromChunk(chunk->pos), bounds)) { + if (isInBounds(mapBlockPos, bounds) && client->getPlayer().getMapBlockIntegrity(mapBlockPos) < mapBlockIntegrity) { sendChunk(chunk->pos, *client); + client->getPlayer().setMapBlockIntegrity(mapBlockPos, mapBlockIntegrity); } } } @@ -139,11 +145,15 @@ void ServerWorld::sendChunk(const glm::vec3& pos, ServerClient &peer) { } void ServerWorld::sendMapBlock(const glm::vec3& pos, ServerClient &peer) { - auto mapBlock = dimension.getMapBlock(pos); - assert(mapBlock != nullptr); + //TODO: Make this a real function that sends an entire mapblock packet + unsigned long long mapBlockIntegrity = dimension.getMapBlockIntegrity(pos); + if (peer.getPlayer().getMapBlockIntegrity(pos) < mapBlockIntegrity) { + auto mapBlock = dimension.getMapBlock(pos); + assert(mapBlock != nullptr); - for (unsigned short i = 0; i < 63; i++) { - sendChunk((*mapBlock)[i], peer); + for (unsigned short i = 0; i < 63; i++) { + sendChunk((*mapBlock)[i], peer); + } } } diff --git a/src/world/ServerDimension.cpp b/src/world/ServerDimension.cpp index faf0398d..dca29e4b 100644 --- a/src/world/ServerDimension.cpp +++ b/src/world/ServerDimension.cpp @@ -2,4 +2,23 @@ // Created by aurailus on 01/10/19. // -#include "ServerDimension.h" \ No newline at end of file +#include "ServerDimension.h" + +bool ServerDimension::setBlock(glm::vec3 pos, unsigned int block) { + bool manip = Dimension::setBlock(pos, block); + if (!manip) return false; + glm::vec3 mb = Space::MapBlock::world::fromBlock(pos); + mapBlockIntegrity[mb] = mapBlockIntegrity[mb] + 1; + return true; +} + +void ServerDimension::setChunk(std::shared_ptr chunk) { + Dimension::setChunk(chunk); + glm::vec3 mb = Space::MapBlock::world::fromChunk(chunk->pos); + mapBlockIntegrity[mb] = mapBlockIntegrity[mb] + 1; +} + +unsigned long long ServerDimension::getMapBlockIntegrity(glm::vec3 mapBlock) { + if (mapBlockIntegrity.count(mapBlock)) return mapBlockIntegrity[mapBlock]; + return 0; +} diff --git a/src/world/ServerDimension.h b/src/world/ServerDimension.h index 93d77fd7..e5244aa3 100644 --- a/src/world/ServerDimension.h +++ b/src/world/ServerDimension.h @@ -9,5 +9,13 @@ class ServerDimension : public Dimension { public: ServerDimension() = default; + + void setChunk(std::shared_ptr chunk) override; + bool setBlock(glm::vec3 pos, unsigned int block) override; + + unsigned long long getMapBlockIntegrity(glm::vec3 mapBlock); + +private: + std::unordered_map mapBlockIntegrity {}; };