2014-05-06 10:41:55 -04:00
|
|
|
|
|
|
|
// GridStructGen.cpp
|
|
|
|
|
|
|
|
// Implements the cGridStructGen class representing a common base class for structure generators that place structures in a semi-random grid
|
|
|
|
|
|
|
|
#include "Globals.h"
|
|
|
|
#include "GridStructGen.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-17 16:15:34 -04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2014-05-15 10:03:45 -04:00
|
|
|
// cEmptyStructure:
|
|
|
|
|
|
|
|
/** A cStructure descendant representing an empty structure.
|
|
|
|
Used when the generator descended from cGridStructGen doesn't return any structure, to keep at least the
|
|
|
|
Origin coords so that the structure isn't queried over and over again. */
|
|
|
|
class cEmptyStructure :
|
|
|
|
public cGridStructGen::cStructure
|
|
|
|
{
|
|
|
|
typedef cGridStructGen::cStructure super;
|
|
|
|
|
|
|
|
public:
|
2014-06-16 10:12:50 -04:00
|
|
|
cEmptyStructure(int a_GridX, int a_GridZ, int a_OriginX, int a_OriginZ) :
|
|
|
|
super(a_GridX, a_GridZ, a_OriginX, a_OriginZ)
|
2014-05-15 10:03:45 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void DrawIntoChunk(cChunkDesc & a_ChunkDesc) override
|
|
|
|
{
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-06 10:41:55 -04:00
|
|
|
cGridStructGen::cGridStructGen(
|
|
|
|
int a_Seed,
|
|
|
|
int a_GridSizeX, int a_GridSizeZ,
|
2014-06-16 10:12:50 -04:00
|
|
|
int a_MaxOffsetX, int a_MaxOffsetZ,
|
2014-05-06 10:41:55 -04:00
|
|
|
int a_MaxStructureSizeX, int a_MaxStructureSizeZ,
|
|
|
|
size_t a_MaxCacheSize
|
|
|
|
) :
|
2014-07-15 02:28:10 -04:00
|
|
|
m_Seed(a_Seed),
|
2014-06-16 10:12:50 -04:00
|
|
|
m_Noise(a_Seed),
|
2014-05-06 10:41:55 -04:00
|
|
|
m_GridSizeX(a_GridSizeX),
|
|
|
|
m_GridSizeZ(a_GridSizeZ),
|
2014-06-16 10:12:50 -04:00
|
|
|
m_MaxOffsetX(a_MaxOffsetX),
|
|
|
|
m_MaxOffsetZ(a_MaxOffsetZ),
|
2014-05-06 10:41:55 -04:00
|
|
|
m_MaxStructureSizeX(a_MaxStructureSizeX),
|
|
|
|
m_MaxStructureSizeZ(a_MaxStructureSizeZ),
|
|
|
|
m_MaxCacheSize(a_MaxCacheSize)
|
|
|
|
{
|
2014-06-26 14:04:56 -04:00
|
|
|
if (m_GridSizeX == 0)
|
|
|
|
{
|
|
|
|
LOG("Grid Size cannot be zero, setting to 1");
|
|
|
|
m_GridSizeX = 1;
|
|
|
|
}
|
|
|
|
if (m_GridSizeZ == 0)
|
|
|
|
{
|
|
|
|
LOG("Grid Size cannot be zero, setting to 1");
|
|
|
|
m_GridSizeZ = 1;
|
|
|
|
}
|
2014-06-16 10:12:50 -04:00
|
|
|
size_t NumStructuresPerQuery = (size_t)(((m_MaxStructureSizeX + m_MaxOffsetX) / m_GridSizeX + 1) * ((m_MaxStructureSizeZ + m_MaxOffsetZ) / m_GridSizeZ + 1));
|
2014-05-23 07:01:06 -04:00
|
|
|
if (NumStructuresPerQuery > m_MaxCacheSize)
|
|
|
|
{
|
|
|
|
m_MaxCacheSize = NumStructuresPerQuery * 4;
|
|
|
|
LOGINFO(
|
|
|
|
"cGridStructGen: The cache size is too small (%u), increasing the cache size to %u to avoid inefficiency.",
|
|
|
|
(unsigned)a_MaxCacheSize, (unsigned)m_MaxCacheSize
|
|
|
|
);
|
|
|
|
}
|
2014-05-06 10:41:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cGridStructGen::GetStructuresForChunk(int a_ChunkX, int a_ChunkZ, cStructurePtrs & a_Structures)
|
|
|
|
{
|
|
|
|
// Calculate the min and max grid coords of the structures to be returned:
|
2014-06-16 10:12:50 -04:00
|
|
|
int MinBlockX = a_ChunkX * cChunkDef::Width - m_MaxStructureSizeX - m_MaxOffsetX;
|
|
|
|
int MinBlockZ = a_ChunkZ * cChunkDef::Width - m_MaxStructureSizeZ - m_MaxOffsetZ;
|
|
|
|
int MaxBlockX = a_ChunkX * cChunkDef::Width + m_MaxStructureSizeX + m_MaxOffsetX + cChunkDef::Width - 1;
|
|
|
|
int MaxBlockZ = a_ChunkZ * cChunkDef::Width + m_MaxStructureSizeZ + m_MaxOffsetZ + cChunkDef::Width - 1;
|
2014-05-06 10:41:55 -04:00
|
|
|
int MinGridX = MinBlockX / m_GridSizeX;
|
|
|
|
int MinGridZ = MinBlockZ / m_GridSizeZ;
|
2014-05-07 16:34:06 -04:00
|
|
|
int MaxGridX = (MaxBlockX + m_GridSizeX - 1) / m_GridSizeX;
|
|
|
|
int MaxGridZ = (MaxBlockZ + m_GridSizeZ - 1) / m_GridSizeZ;
|
2014-05-08 15:10:55 -04:00
|
|
|
int MinX = MinGridX * m_GridSizeX;
|
|
|
|
int MaxX = MaxGridX * m_GridSizeX;
|
|
|
|
int MinZ = MinGridZ * m_GridSizeZ;
|
|
|
|
int MaxZ = MaxGridZ * m_GridSizeZ;
|
2014-05-06 10:41:55 -04:00
|
|
|
|
|
|
|
// Walk the cache, move each structure that we want into a_Structures:
|
|
|
|
for (cStructurePtrs::iterator itr = m_Cache.begin(), end = m_Cache.end(); itr != end;)
|
|
|
|
{
|
|
|
|
if (
|
2014-06-21 14:17:17 -04:00
|
|
|
((*itr)->m_GridX >= MinX) && ((*itr)->m_GridX < MaxX) &&
|
|
|
|
((*itr)->m_GridZ >= MinZ) && ((*itr)->m_GridZ < MaxZ)
|
2014-05-06 10:41:55 -04:00
|
|
|
)
|
|
|
|
{
|
|
|
|
// want
|
|
|
|
a_Structures.push_back(*itr);
|
|
|
|
itr = m_Cache.erase(itr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// don't want
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
} // for itr - m_Cache[]
|
|
|
|
|
|
|
|
// Create those structures that haven't been in the cache:
|
|
|
|
for (int x = MinGridX; x < MaxGridX; x++)
|
|
|
|
{
|
2014-06-16 10:12:50 -04:00
|
|
|
int GridX = x * m_GridSizeX;
|
2014-05-06 10:41:55 -04:00
|
|
|
for (int z = MinGridZ; z < MaxGridZ; z++)
|
|
|
|
{
|
2014-06-16 10:12:50 -04:00
|
|
|
int GridZ = z * m_GridSizeZ;
|
2014-05-06 10:41:55 -04:00
|
|
|
bool Found = false;
|
|
|
|
for (cStructurePtrs::const_iterator itr = a_Structures.begin(), end = a_Structures.end(); itr != end; ++itr)
|
|
|
|
{
|
2014-06-16 10:12:50 -04:00
|
|
|
if (((*itr)->m_GridX == GridX) && ((*itr)->m_GridZ == GridZ))
|
2014-05-06 10:41:55 -04:00
|
|
|
{
|
|
|
|
Found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} // for itr - a_Structures[]
|
|
|
|
if (!Found)
|
|
|
|
{
|
2014-06-16 10:12:50 -04:00
|
|
|
int OriginX = GridX + ((m_Noise.IntNoise2DInt(GridX + 3, GridZ + 5) / 7) % (m_MaxOffsetX * 2)) - m_MaxOffsetX;
|
|
|
|
int OriginZ = GridZ + ((m_Noise.IntNoise2DInt(GridX + 5, GridZ + 3) / 7) % (m_MaxOffsetZ * 2)) - m_MaxOffsetZ;
|
|
|
|
cStructurePtr Structure = CreateStructure(GridX, GridZ, OriginX, OriginZ);
|
2014-10-20 16:55:07 -04:00
|
|
|
if (Structure.get() == nullptr)
|
2014-05-15 10:03:45 -04:00
|
|
|
{
|
2014-06-16 10:12:50 -04:00
|
|
|
Structure.reset(new cEmptyStructure(GridX, GridZ, OriginX, OriginZ));
|
2014-05-15 10:03:45 -04:00
|
|
|
}
|
|
|
|
a_Structures.push_back(Structure);
|
2014-05-06 10:41:55 -04:00
|
|
|
}
|
|
|
|
} // for z
|
|
|
|
} // for x
|
|
|
|
|
|
|
|
// Copy a_Forts into m_Cache to the beginning:
|
|
|
|
cStructurePtrs StructuresCopy (a_Structures);
|
|
|
|
m_Cache.splice(m_Cache.begin(), StructuresCopy, StructuresCopy.begin(), StructuresCopy.end());
|
|
|
|
|
|
|
|
// Trim the cache if it's too long:
|
|
|
|
size_t CacheSize = 0;
|
|
|
|
for (cStructurePtrs::iterator itr = m_Cache.begin(), end = m_Cache.end(); itr != end; ++itr)
|
|
|
|
{
|
|
|
|
CacheSize += (*itr)->GetCacheCost();
|
|
|
|
if (CacheSize > m_MaxCacheSize)
|
|
|
|
{
|
|
|
|
// Erase all items from this one till the cache end
|
|
|
|
m_Cache.erase(itr, m_Cache.end());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cGridStructGen::GenFinish(cChunkDesc & a_ChunkDesc)
|
|
|
|
{
|
|
|
|
int ChunkX = a_ChunkDesc.GetChunkX();
|
|
|
|
int ChunkZ = a_ChunkDesc.GetChunkZ();
|
|
|
|
cStructurePtrs Structures;
|
|
|
|
GetStructuresForChunk(ChunkX, ChunkZ, Structures);
|
|
|
|
for (cStructurePtrs::const_iterator itr = Structures.begin(); itr != Structures.end(); ++itr)
|
|
|
|
{
|
|
|
|
(*itr)->DrawIntoChunk(a_ChunkDesc);
|
|
|
|
} // for itr - Structures[]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|