From 203a47a527d98a4ec4f8b13da24d74dea08baf59 Mon Sep 17 00:00:00 2001 From: iProgramInCpp Date: Fri, 11 Aug 2023 20:59:30 +0300 Subject: [PATCH] * Add getChunkDontCreate. Will be useful for multiplayer. --- .../world/level/levelgen/chunk/ChunkCache.cpp | 37 +++++++++++++++++++ .../world/level/levelgen/chunk/ChunkCache.hpp | 1 + .../level/levelgen/chunk/ChunkSource.hpp | 1 + .../chunk/PerformanceTestChunkSource.cpp | 9 +++++ .../chunk/PerformanceTestChunkSource.hpp | 1 + .../levelgen/chunk/RandomLevelSource.cpp | 18 +++++++++ .../levelgen/chunk/RandomLevelSource.hpp | 1 + .../level/levelgen/chunk/TestChunkSource.cpp | 19 ++++++++++ .../level/levelgen/chunk/TestChunkSource.hpp | 1 + 9 files changed, 88 insertions(+) diff --git a/source/world/level/levelgen/chunk/ChunkCache.cpp b/source/world/level/levelgen/chunk/ChunkCache.cpp index e2875c5..772f71f 100644 --- a/source/world/level/levelgen/chunk/ChunkCache.cpp +++ b/source/world/level/levelgen/chunk/ChunkCache.cpp @@ -113,6 +113,43 @@ LevelChunk* ChunkCache::getChunk(int x, int z) return m_chunkMap[z][x]; } +LevelChunk* ChunkCache::getChunkDontCreate(int x, int z) +{ + // get the last chunk quickly if needed + if (m_LastChunkX == x && m_LastChunkZ == z) + { + if (m_pLastChunk) + return m_pLastChunk; + } + + if (x < 0 || z < 0 || x >= C_MAX_CHUNKS_Z || z >= C_MAX_CHUNKS_X) + return m_pEmptyChunk; + + if (!hasChunk(x, z)) + { + LevelChunk* pOldChunk = m_chunkMap[z][x]; + if (pOldChunk) + { + pOldChunk->unload(); + save(pOldChunk); + if (m_pChunkStorage) + m_pChunkStorage->saveEntities(m_pLevel, pOldChunk); + } + + // create an empty chunk + LevelChunk* pChunk = m_pEmptyChunk; + if (m_pChunkSource) + pChunk = m_pChunkSource->getChunkDontCreate(x, z); + + m_chunkMap[z][x] = pChunk; + } + + m_LastChunkX = x; + m_LastChunkZ = z; + m_pLastChunk = m_chunkMap[z][x]; + return m_chunkMap[z][x]; +} + bool ChunkCache::hasChunk(int x, int z) { if (x < 0 || z < 0) diff --git a/source/world/level/levelgen/chunk/ChunkCache.hpp b/source/world/level/levelgen/chunk/ChunkCache.hpp index ad63eac..bd98067 100644 --- a/source/world/level/levelgen/chunk/ChunkCache.hpp +++ b/source/world/level/levelgen/chunk/ChunkCache.hpp @@ -23,6 +23,7 @@ public: LevelChunk* create(int x, int z) override; LevelChunk* getChunk(int x, int z) override; + LevelChunk* getChunkDontCreate(int x, int z) override; bool hasChunk(int x, int z) override; std::string gatherStats() override; void postProcess(ChunkSource*, int, int) override; diff --git a/source/world/level/levelgen/chunk/ChunkSource.hpp b/source/world/level/levelgen/chunk/ChunkSource.hpp index c150a90..5993416 100644 --- a/source/world/level/levelgen/chunk/ChunkSource.hpp +++ b/source/world/level/levelgen/chunk/ChunkSource.hpp @@ -19,6 +19,7 @@ public: virtual bool hasChunk(int, int) = 0; virtual LevelChunk* getChunk(int, int) = 0; virtual LevelChunk* create(int, int) = 0; + virtual LevelChunk* getChunkDontCreate(int, int) = 0; virtual void postProcess(ChunkSource*, int, int) = 0; virtual int tick() = 0; virtual bool shouldSave() = 0; diff --git a/source/world/level/levelgen/chunk/PerformanceTestChunkSource.cpp b/source/world/level/levelgen/chunk/PerformanceTestChunkSource.cpp index 01ae50e..f38c095 100644 --- a/source/world/level/levelgen/chunk/PerformanceTestChunkSource.cpp +++ b/source/world/level/levelgen/chunk/PerformanceTestChunkSource.cpp @@ -62,6 +62,15 @@ LevelChunk* PerformanceTestChunkSource::getChunk(int x, int z) return create(x, z); } +LevelChunk* PerformanceTestChunkSource::getChunkDontCreate(int x, int z) +{ + TileID* pData = new TileID[0x8000u]; + memset(pData, 0, 0x8000u * sizeof(TileID)); + + LevelChunk* pChunk = new LevelChunk(m_pLevel, pData, x, z); + return pChunk; +} + bool PerformanceTestChunkSource::hasChunk(int x, int z) { return true; diff --git a/source/world/level/levelgen/chunk/PerformanceTestChunkSource.hpp b/source/world/level/levelgen/chunk/PerformanceTestChunkSource.hpp index 80544b0..bbe6d26 100644 --- a/source/world/level/levelgen/chunk/PerformanceTestChunkSource.hpp +++ b/source/world/level/levelgen/chunk/PerformanceTestChunkSource.hpp @@ -18,6 +18,7 @@ public: PerformanceTestChunkSource(); LevelChunk* create(int x, int z) override; LevelChunk* getChunk(int x, int z) override; + LevelChunk* getChunkDontCreate(int x, int z) override; bool hasChunk(int x, int z) override; std::string gatherStats() override; void postProcess(ChunkSource*, int, int) override; diff --git a/source/world/level/levelgen/chunk/RandomLevelSource.cpp b/source/world/level/levelgen/chunk/RandomLevelSource.cpp index 18ce325..7675534 100644 --- a/source/world/level/levelgen/chunk/RandomLevelSource.cpp +++ b/source/world/level/levelgen/chunk/RandomLevelSource.cpp @@ -94,6 +94,24 @@ LevelChunk* RandomLevelSource::getChunk(int x, int z) return pChunk; } +LevelChunk* RandomLevelSource::getChunkDontCreate(int x, int z) +{ + int hashCode = GetChunkHash(x, z); + auto iter = m_chunks.find(hashCode); + if (iter != m_chunks.end()) + return iter->second; + + // have to create the chunk. Create an empty one + + TileID* pLevelData = new TileID[32768]; + memset (pLevelData, 0, sizeof *pLevelData * 32768); + + LevelChunk* pChunk = new LevelChunk(m_pLevel, pLevelData, x, z); + m_chunks.insert(std::pair(hashCode, pChunk)); + + return pChunk; +} + float* RandomLevelSource::getHeights(float* fptr, int a3, int a4, int a5, int a6, int a7, int a8) { if (fptr == nullptr) diff --git a/source/world/level/levelgen/chunk/RandomLevelSource.hpp b/source/world/level/levelgen/chunk/RandomLevelSource.hpp index d712676..8c74ce1 100644 --- a/source/world/level/levelgen/chunk/RandomLevelSource.hpp +++ b/source/world/level/levelgen/chunk/RandomLevelSource.hpp @@ -29,6 +29,7 @@ public: bool hasChunk(int x, int z) override; LevelChunk* create(int x, int z) override; LevelChunk* getChunk(int x, int z) override; + LevelChunk* getChunkDontCreate(int x, int z) override; std::string gatherStats() override; void postProcess(ChunkSource*, int, int) override; diff --git a/source/world/level/levelgen/chunk/TestChunkSource.cpp b/source/world/level/levelgen/chunk/TestChunkSource.cpp index d132ef7..6a64a34 100644 --- a/source/world/level/levelgen/chunk/TestChunkSource.cpp +++ b/source/world/level/levelgen/chunk/TestChunkSource.cpp @@ -155,6 +155,25 @@ LevelChunk* TestChunkSource::getChunk(int x, int z) return create(x, z); } +LevelChunk* TestChunkSource::getChunkDontCreate(int x, int z) +{ + if (x < 0 || z < 0 || x >= C_MAX_CHUNKS_X || z >= C_MAX_CHUNKS_Z) + return nullptr; + + if (m_chunkMap[z][x]) + return m_chunkMap[z][x]; + + TileID* pData = new TileID[0x8000u]; + memset(pData, 0, 0x8000u * sizeof(TileID)); + + m_chunkMap[z][x] = new LevelChunk(m_pLevel, pData, x, z); + m_pLastChunk = m_chunkMap[z][x]; + m_lastChunkX = x; + m_lastChunkZ = z; + + return m_chunkMap[z][x]; +} + bool TestChunkSource::hasChunk(int x, int z) { return true; diff --git a/source/world/level/levelgen/chunk/TestChunkSource.hpp b/source/world/level/levelgen/chunk/TestChunkSource.hpp index 1f3f00c..2c437a9 100644 --- a/source/world/level/levelgen/chunk/TestChunkSource.hpp +++ b/source/world/level/levelgen/chunk/TestChunkSource.hpp @@ -23,6 +23,7 @@ public: LevelChunk* generateChunk(int x, int z); LevelChunk* create(int x, int z) override; LevelChunk* getChunk(int x, int z) override; + LevelChunk* getChunkDontCreate(int x, int z) override; bool hasChunk(int x, int z) override; std::string gatherStats() override; void postProcess(ChunkSource*, int, int) override;