Cuberite
A lightweight, fast and extensible game server for Minecraft
Classes | Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
cGridStructGen Class Referenceabstract

Generates structures in a semi-random grid. More...

#include <GridStructGen.h>

Inheritance diagram for cGridStructGen:
Inheritance graph
[legend]
Collaboration diagram for cGridStructGen:
Collaboration graph
[legend]

Classes

class  cStructure
 Represents a single structure that occupies the grid point. More...
 

Public Types

typedef std::shared_ptr< cStructurecStructurePtr
 
typedef std::list< cStructurePtrcStructurePtrs
 

Public Member Functions

 cGridStructGen (int a_Seed)
 Creates a new instance that has the generation parameters set to defaults. More...
 
 cGridStructGen (int a_Seed, int a_GridSizeX, int a_GridSizeZ, int a_MaxOffsetX, int a_MaxOffsetZ, int a_MaxStructureSizeX, int a_MaxStructureSizeZ, size_t a_MaxCacheSize)
 
virtual void GenFinish (cChunkDesc &a_ChunkDesc) override
 
void SetGeneratorParams (const AStringMap &a_GeneratorParams)
 Sets the generator params based on the dictionary passed in. More...
 
- Public Member Functions inherited from cFinishGen
virtual ~cFinishGen ()
 

Protected Member Functions

void ClearCache (void)
 Clears everything from the cache. More...
 
virtual cStructurePtr CreateStructure (int a_GridX, int a_GridZ, int a_OriginX, int a_OriginZ)=0
 Create a new structure at the specified gridpoint. More...
 
void GetStructuresForChunk (int a_ChunkX, int a_ChunkZ, cStructurePtrs &a_Structures)
 Returns all structures that may intersect the given chunk. More...
 

Protected Attributes

int m_BaseSeed
 Base seed of the world for which the generator generates chunk. More...
 
cStructurePtrs m_Cache
 Cache for the most recently generated structures, ordered by the recentness. More...
 
int m_GridSizeX
 The size of each grid's cell in the X axis. More...
 
int m_GridSizeZ
 The size of each grid's cell in the Z axis. More...
 
size_t m_MaxCacheSize
 Maximum allowed sum of costs for items in the cache. More...
 
int m_MaxOffsetX
 The maximum offset of the structure's origin from the grid midpoint, in X coord. More...
 
int m_MaxOffsetZ
 The maximum offset of the structure's origin from the grid midpoint, in Z coord. More...
 
int m_MaxStructureSizeX
 Maximum theoretical size of the structure in the X axis. More...
 
int m_MaxStructureSizeZ
 Maximum theoretical size of the structure in the Z axis. More...
 
cNoise m_Noise
 The noise used for generating grid offsets. More...
 
int m_Seed
 Seed for generating grid offsets and also available for descendants. More...
 

Detailed Description

Generates structures in a semi-random grid.

Defines a grid in the XZ space with predefined cell size in each direction. Each cell then receives exactly one structure (provided by the descendant class). The structure is placed within the cell, but doesn't need to be bounded by the cell, it can be well outside the cell; the generator uses the MaxStructureSize parameter to determine how far away from the cell the structure can be at most. Each structure has an offset from the grid's center point, the offset is generated randomly from a range given to this class as a parameter.

Each structure thus contains the coords of its grid center (m_GridX, m_GridZ) and the actual origin from which it's built (m_OriginX, m_OriginZ).

This class provides a cache for the structures generated for successive chunks and manages that cache. It also provides the cFinishGen override that uses the cache to actually generate the structure into chunk data.

After generating each chunk the cache is checked for size, each item in the cache has a cost associated with it and the cache is trimmed (from its least-recently-used end) so that the sum of the cost in the cache is less than m_MaxCacheSize

To use this class, declare a descendant class that implements the overridable methods, then create an instance of that class. The descendant must provide the CreateStructure() function that is called to generate a structure at the specific grid cell.

The descendant must use a specific cStructure descendant to provide the actual structure that gets generated. The structure must provide the DrawIntoChunk() function that generates the structure into the chunk data, and can override the GetCacheCost() function that returns the cost of that structure in the cache.

Definition at line 44 of file GridStructGen.h.

Member Typedef Documentation

◆ cStructurePtr

typedef std::shared_ptr<cStructure> cGridStructGen::cStructurePtr

Definition at line 77 of file GridStructGen.h.

◆ cStructurePtrs

Definition at line 78 of file GridStructGen.h.

Constructor & Destructor Documentation

◆ cGridStructGen() [1/2]

cGridStructGen::cGridStructGen ( int  a_Seed,
int  a_GridSizeX,
int  a_GridSizeZ,
int  a_MaxOffsetX,
int  a_MaxOffsetZ,
int  a_MaxStructureSizeX,
int  a_MaxStructureSizeZ,
size_t  a_MaxCacheSize 
)

Definition at line 42 of file GridStructGen.cpp.

◆ cGridStructGen() [2/2]

cGridStructGen::cGridStructGen ( int  a_Seed)

Creates a new instance that has the generation parameters set to defaults.

This is used for instances that are later loaded from a file.

Definition at line 84 of file GridStructGen.cpp.

Member Function Documentation

◆ ClearCache()

void cGridStructGen::ClearCache ( void  )
protected

Clears everything from the cache.

◆ CreateStructure()

virtual cStructurePtr cGridStructGen::CreateStructure ( int  a_GridX,
int  a_GridZ,
int  a_OriginX,
int  a_OriginZ 
)
protectedpure virtual

◆ GenFinish()

void cGridStructGen::GenFinish ( cChunkDesc a_ChunkDesc)
overridevirtual

Implements cFinishGen.

Definition at line 220 of file GridStructGen.cpp.

◆ GetStructuresForChunk()

void cGridStructGen::GetStructuresForChunk ( int  a_ChunkX,
int  a_ChunkZ,
cStructurePtrs a_Structures 
)
protected

Returns all structures that may intersect the given chunk.

The structures are considered as intersecting iff their bounding box (defined by m_MaxStructureSize) around their gridpoint intersects the chunk.

Definition at line 133 of file GridStructGen.cpp.

◆ SetGeneratorParams()

void cGridStructGen::SetGeneratorParams ( const AStringMap a_GeneratorParams)

Sets the generator params based on the dictionary passed in.

Note that this must not be called anymore after generating a chunk.

Definition at line 102 of file GridStructGen.cpp.

Member Data Documentation

◆ m_BaseSeed

int cGridStructGen::m_BaseSeed
protected

Base seed of the world for which the generator generates chunk.

Definition at line 102 of file GridStructGen.h.

◆ m_Cache

cStructurePtrs cGridStructGen::m_Cache
protected

Cache for the most recently generated structures, ordered by the recentness.

Definition at line 138 of file GridStructGen.h.

◆ m_GridSizeX

int cGridStructGen::m_GridSizeX
protected

The size of each grid's cell in the X axis.

Definition at line 112 of file GridStructGen.h.

◆ m_GridSizeZ

int cGridStructGen::m_GridSizeZ
protected

The size of each grid's cell in the Z axis.

Definition at line 115 of file GridStructGen.h.

◆ m_MaxCacheSize

size_t cGridStructGen::m_MaxCacheSize
protected

Maximum allowed sum of costs for items in the cache.

Items that are over this cost are removed from the cache, oldest-first

Definition at line 135 of file GridStructGen.h.

◆ m_MaxOffsetX

int cGridStructGen::m_MaxOffsetX
protected

The maximum offset of the structure's origin from the grid midpoint, in X coord.

Definition at line 118 of file GridStructGen.h.

◆ m_MaxOffsetZ

int cGridStructGen::m_MaxOffsetZ
protected

The maximum offset of the structure's origin from the grid midpoint, in Z coord.

Definition at line 121 of file GridStructGen.h.

◆ m_MaxStructureSizeX

int cGridStructGen::m_MaxStructureSizeX
protected

Maximum theoretical size of the structure in the X axis.

This limits the structures considered for a single chunk, so the lesser the number, the better performance. Structures large than this may get cropped.

Definition at line 126 of file GridStructGen.h.

◆ m_MaxStructureSizeZ

int cGridStructGen::m_MaxStructureSizeZ
protected

Maximum theoretical size of the structure in the Z axis.

This limits the structures considered for a single chunk, so the lesser the number, the better performance. Structures large than this may get cropped.

Definition at line 131 of file GridStructGen.h.

◆ m_Noise

cNoise cGridStructGen::m_Noise
protected

The noise used for generating grid offsets.

Definition at line 109 of file GridStructGen.h.

◆ m_Seed

int cGridStructGen::m_Seed
protected

Seed for generating grid offsets and also available for descendants.

Calculated from m_BaseSeed by adding the SeedOffset parameter loaded from the cubeset file (if applicable); otherwise the same as m_BaseSeed.

Definition at line 106 of file GridStructGen.h.


The documentation for this class was generated from the following files: