Lodestone.Level
Loading...
Searching...
No Matches
lodestone::level::chunk::LevelChunk Class Reference

#include <LevelChunk.h>

Inheritance diagram for lodestone::level::chunk::LevelChunk:
Collaboration diagram for lodestone::level::chunk::LevelChunk:

Public Member Functions

 LevelChunk (int height)
 Creates a new LevelChunk with preallocated chunk section array for the given height.
 LevelChunk (int height, const coords::ChunkCoordinates &coords, ChunkContainer *container=nullptr)
 Creates a new LevelChunk under given parent container, with given coordinates and height.
ChunkType type () override
int getChunkHeight () const override
 Gets the height of the chunk in Sections, can be used for getting section count.
bool hasSection (const coords::SectionCoordinates &sectionY) const
 Checks if a section at the given section relative y level exists (NOT BLOCK Y).
section::Section * getSectionCreate (const coords::SectionCoordinates &sectionY) override
 Gets/Creates a section at the given section relative y level (NOT BLOCK Y).
section::Section * getSection (const coords::SectionCoordinates &sectionY) const override
 Creates a section at the given section relative y level (NOT BLOCK Y).
void calculateHeightmap () override
void calculateHeightmapAtColumn (int localX, int localZ, int startHeight) override
const block::instance::BlockInstance & getBlock (const int localX, const int blockY, const int localZ) const override
void setBlock (block::instance::BlockInstance &&block, int localX, int y, int localZ) override
 Sets a block at the given X, Y, and Z coordinates.
void setBlockRaw (block::instance::BlockInstance &&block, int localX, int y, int localZ) override
 Sets a block at the given X, Y, and Z coordinates.
int getSectionCount () const override
Public Member Functions inherited from lodestone::level::chunk::Chunk
 Chunk ()
std::string toString () const override
 Chunk (const coords::ChunkCoordinates &coords, ChunkContainer *container=nullptr)
 ~Chunk () override
int getChunkBlockHeight () const
 Gets the height of the chunk in Blocks.
virtual section::Section * getSectionFromBlockY (const signed_size_t &blockY) const
virtual section::Section * getSectionFromBlockYCreate (const signed_size_t &blockY)
virtual const int16_t * getHeightmap () const
virtual int16_t getHeightAt (int localX, int localZ) const
virtual void setHeightAt (int16_t height, int localX, int localZ)
virtual const block::instance::BlockInstance & getBlockAtHeight (int localX, int localZ) const
virtual void setBlockAtHeight (block::instance::BlockInstance &&block, int localX, int localZ)
bool hasCoords () const
const std::optional< coords::ChunkCoordinates > & getCoords () const
void setCoords (const std::optional< coords::ChunkCoordinates > &coords)
void invalidateCoords ()
void attach (ChunkContainer *container)
void attach (ChunkContainer *container, const coords::ChunkCoordinates &coords)
void detach (bool shouldInvalidateCoords)
bool isInContainer () const
ChunkContainer * getContainer () const
virtual void postProcess ()
Public Member Functions inherited from lodestone::common::string::StringSerializable
constexpr StringSerializable ()=default
virtual constexpr ~StringSerializable ()=default
Public Member Functions inherited from lodestone::level::properties::ReflectiveProperties
virtual ~ReflectiveProperties ()=default
virtual std::unique_ptr< AbstractProperty > getProperty (const std::string &name)
template<typename T>
std::unique_ptr< TemplatedProperty< T > > getProperty (const std::string &name)
template<typename T>
std::unique_ptr< TemplatedProperty< T > > getPropertyOr (const std::string &name, T value)
Public Member Functions inherited from lodestone::common::util::IHasDerivedTypes< ChunkType >
constexpr IHasDerivedTypes ()=default
virtual constexpr ~IHasDerivedTypes ()=default
bool constexpr instanceOf (EType type)

Protected Attributes

std::vector< std::unique_ptr< section::Section > > m_sections
 Chunk Sections.
Protected Attributes inherited from lodestone::level::chunk::Chunk
std::optional< coords::ChunkCoordinates > m_coords
ChunkContainer * m_container
 The parent container which may hold the chunk.
int16_t * m_heightMap = new int16_t[TOTAL_CHUNK_2D_SIZE] {}
 Heightmap.

Additional Inherited Members

Static Public Attributes inherited from lodestone::level::chunk::Chunk
static constexpr int TOTAL_CHUNK_2D_SIZE = common::constants::CHUNK_WIDTH * common::constants::CHUNK_DEPTH

Constructor & Destructor Documentation

◆ LevelChunk() [1/2]

lodestone::level::chunk::LevelChunk::LevelChunk ( int height)
explicit

Creates a new LevelChunk with preallocated chunk section array for the given height.

11 {
12 this->m_sections = std::vector<std::unique_ptr<section::Section>>(
15 }
static constexpr int ceilDiv(const int x, const int y)
std::vector< std::unique_ptr< section::Section > > m_sections
Chunk Sections.
Definition LevelChunk.h:63
static constexpr int SECTION_HEIGHT

◆ LevelChunk() [2/2]

lodestone::level::chunk::LevelChunk::LevelChunk ( int height,
const coords::ChunkCoordinates & coords,
ChunkContainer * container = nullptr )

Creates a new LevelChunk under given parent container, with given coordinates and height.

Parameters
heightThe total height in blocks of the LevelChunk (will be rounded to the nearest 16!!)
coordsThe coordinates of the chunk in level space
containerThe container that holds the chunk
19 : Chunk(coords, container) {
20 this->m_sections = std::vector<std::unique_ptr<section::Section>>(
23 }
Chunk()
Definition Chunk.cpp:11

Member Function Documentation

◆ calculateHeightmap()

void lodestone::level::chunk::LevelChunk::calculateHeightmap ( )
overridevirtual

Implements lodestone::level::chunk::Chunk.

57 {
58 const int height = getChunkBlockHeight();
59
60 for (int z = 0; z < common::constants::CHUNK_DEPTH; z++) {
61 for (int x = 0; x < common::constants::CHUNK_WIDTH; x++) {
62 this->calculateHeightmapAtColumn(x, z, height);
63 }
64 }
65 }
int getChunkBlockHeight() const
Gets the height of the chunk in Blocks.
Definition Chunk.cpp:24
void calculateHeightmapAtColumn(int localX, int localZ, int startHeight) override
Definition LevelChunk.cpp:67
static constexpr int CHUNK_WIDTH
static constexpr int CHUNK_DEPTH

◆ calculateHeightmapAtColumn()

void lodestone::level::chunk::LevelChunk::calculateHeightmapAtColumn ( int localX,
int localZ,
int startHeight )
overridevirtual

Implements lodestone::level::chunk::Chunk.

68 {
69 for (int y = startHeight; y >= 0; y--) {
70 const block::instance::BlockInstance &s = getBlock(localX, y, localZ);
71 if (!s.getBlock()->heightmapShouldIgnore()) {
72 setHeightAt(y + 1, localX, localZ);
73 break;
74 }
75 }
76 }
virtual void setHeightAt(int16_t height, int localX, int localZ)
Definition Chunk.cpp:44
const block::instance::BlockInstance & getBlock(const int localX, const int blockY, const int localZ) const override
Definition LevelChunk.cpp:53

◆ getBlock()

const block::instance::BlockInstance & lodestone::level::chunk::LevelChunk::getBlock ( const int localX,
const int blockY,
const int localZ ) const
nodiscardoverridevirtual

Implements lodestone::level::chunk::Chunk.

53 {
55 }
virtual section::Section * getSectionFromBlockY(const signed_size_t &blockY) const
Definition Chunk.cpp:28
virtual const block::instance::BlockInstance & getBlock(const int localX, const int localY, const int localZ) const =0
static constexpr int blockToLocalSectionY(const signed_size_t blockX)
Definition SectionCoordinates.h:38

◆ getChunkHeight()

int lodestone::level::chunk::LevelChunk::getChunkHeight ( ) const
nodiscardoverridevirtual

Gets the height of the chunk in Sections, can be used for getting section count.

Implements lodestone::level::chunk::Chunk.

29{ return m_sections.size(); }

◆ getSection()

section::Section * lodestone::level::chunk::LevelChunk::getSection ( const coords::SectionCoordinates & sectionY) const
nodiscardoverridevirtual

Creates a section at the given section relative y level (NOT BLOCK Y).

Implements lodestone::level::chunk::Chunk.

38 {
39 // if non-existent, return fake one
40 if (!hasSection(sectionY))
42
43 return m_sections[sectionY].get();
44 }
bool hasSection(const coords::SectionCoordinates &sectionY) const
Checks if a section at the given section relative y level exists (NOT BLOCK Y).
Definition LevelChunk.cpp:31
static ImmutableSection * getInstance()
Definition ImmutableSection.cpp:11

◆ getSectionCount()

int lodestone::level::chunk::LevelChunk::getSectionCount ( ) const
overridevirtual

Implements lodestone::level::chunk::Chunk.

114 {
115 return this->m_sections.size();
116 }

◆ getSectionCreate()

section::Section * lodestone::level::chunk::LevelChunk::getSectionCreate ( const coords::SectionCoordinates & sectionY)
overridevirtual

Gets/Creates a section at the given section relative y level (NOT BLOCK Y).

Implements lodestone::level::chunk::Chunk.

46 {
47 if (!m_sections[sectionY])
48 m_sections[sectionY] = std::make_unique<section::LevelSection>();
49
50 return m_sections[sectionY].get();
51 }

◆ hasSection()

bool lodestone::level::chunk::LevelChunk::hasSection ( const coords::SectionCoordinates & sectionY) const
nodiscard

Checks if a section at the given section relative y level exists (NOT BLOCK Y).

31 {
32 if (sectionY < 0 || sectionY >= m_sections.size())
33 return false;
34
35 return (m_sections[sectionY] != nullptr);
36 }

◆ setBlock()

void lodestone::level::chunk::LevelChunk::setBlock ( block::instance::BlockInstance && block,
int localX,
int y,
int localZ )
overridevirtual

Sets a block at the given X, Y, and Z coordinates.

Also updates the blockmap and heightmap

Implements lodestone::level::chunk::Chunk.

79 {
80 setBlockRaw(std::move(block), localX, y, localZ);
81
82 const int height = getChunkBlockHeight();
83 if (!block.getBlock()->heightmapShouldIgnore()) {
84 // if our block is higher than the current height, and isn't air,
85 // then it's obviously higher up. so we set the new height
86 if (y + 1 > getHeightAt(localX, localZ)) {
87 setHeightAt(std::min(y + 1, height - 1), localX, localZ);
88 };
89 } else {
90 // if our air block's position is the topmost block of any column
91 if (y + 1 == getHeightAt(localX, localZ)) {
92 // then we get the new topmost block
93 for (int i = y; i >= 0; i--) {
94 const block::instance::BlockInstance &s = getBlock(localX, i, localZ);
95
96 if (s.getBlock() && !s.getBlock()->heightmapShouldIgnore()) {
97 setHeightAt(std::min(i + 1, height - 1), localX, localZ);
98
99 return;
100 }
101 }
102
103 // there were no blocks
104 setHeightAt(0, localX, localZ);
105 }
106 }
107 }
virtual int16_t getHeightAt(int localX, int localZ) const
Definition Chunk.cpp:40
void setBlockRaw(block::instance::BlockInstance &&block, int localX, int y, int localZ) override
Sets a block at the given X, Y, and Z coordinates.
Definition LevelChunk.cpp:109

◆ setBlockRaw()

void lodestone::level::chunk::LevelChunk::setBlockRaw ( block::instance::BlockInstance && block,
int localX,
int y,
int localZ )
overridevirtual

Sets a block at the given X, Y, and Z coordinates.

Does not update blockmap

Implements lodestone::level::chunk::Chunk.

110 {
111 getSectionCreate(y >> 4)->setBlock(std::move(block), localX, y & 15, localZ);
112 }
section::Section * getSectionCreate(const coords::SectionCoordinates &sectionY) override
Gets/Creates a section at the given section relative y level (NOT BLOCK Y).
Definition LevelChunk.cpp:46
virtual void setBlock(block::instance::BlockInstance &&block, int localX, int localY, int localZ)=0

◆ type()

ChunkType lodestone::level::chunk::LevelChunk::type ( )
overridevirtual

Member Data Documentation

◆ m_sections

std::vector<std::unique_ptr<section::Section> > lodestone::level::chunk::LevelChunk::m_sections
protected

Chunk Sections.

Each chunk is made up of x amount of Sections, with each section storing 16x16x16 blocks. This dictates the height of the chunk as each section is 16 blocks in height.


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