* Add getChunkDontCreate. Will be useful for multiplayer.

This commit is contained in:
iProgramInCpp
2023-08-11 20:59:30 +03:00
parent 35a63582df
commit 203a47a527
9 changed files with 88 additions and 0 deletions

View File

@@ -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)

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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<int, LevelChunk*>(hashCode, pChunk));
return pChunk;
}
float* RandomLevelSource::getHeights(float* fptr, int a3, int a4, int a5, int a6, int a7, int a8)
{
if (fptr == nullptr)

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;