Lodestone.Level
Loading...
Searching...
No Matches
lodestone::level::chunk::ChunkContainer Class Referenceabstract

Holds chunks. More...

#include <ChunkContainer.h>

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

Public Member Functions

 ~ChunkContainer () override=default
 ChunkContainer ()=default
 ChunkContainer (ChunkContainer &&)=default
ChunkContainer & operator= (ChunkContainer &&)=default
std::string toString () const override
virtual bool isChunkInBounds (const coords::ChunkCoordinates &coords)=0
bool hasChunk (const coords::ChunkCoordinates &coords) const
 Returns true if chunk at coords exist.
bool hasChunk (const int x, const int z) const
 Returns true if chunk at coords exist.
Chunk * createChunk (const coords::ChunkCoordinates &coords, int height=256)
 Creates a chunk at the given coordinates with the given height.
Chunk * createChunk (const int x, const int z, const int height=256)
 Creates a chunk at the given coordinates with the given height.
void addChunk (std::unique_ptr< Chunk > chunk)
void addChunk (const coords::ChunkCoordinates &coords, std::unique_ptr< Chunk > chunk)
void addChunk (const int x, const int z, std::unique_ptr< Chunk > chunk)
Chunk * getChunk (const coords::ChunkCoordinates &coords)
Chunk * getChunk (const int x, const int z)
Chunk * getChunkFromBlockCoords (const signed_size_t blockX, const signed_size_t blockZ)
Chunk * getChunkCreate (const coords::ChunkCoordinates &coords, int height=256)
Chunk * getChunkCreate (const int x, const int z, const int height=256)
Chunk * getChunkFromBlockCoordsCreate (const int blockX, const int blockZ, const int height=256)
const Chunk * getChunk (const coords::ChunkCoordinates &coords) const
const Chunk * getChunk (const int x, const int z) const
const Chunk * getChunkFromBlockCoords (const signed_size_t blockX, const signed_size_t blockZ) const
std::unique_ptr< Chunk > detachChunk (const coords::ChunkCoordinates &coords, bool shouldInvalidateCoords=true)
std::unique_ptr< Chunk > detachChunk (const int x, const int z, const bool shouldInvalidateCoords=true)
void removeChunk (const coords::ChunkCoordinates &coords)
void removeChunk (const int x, const int z)
void merge (std::unique_ptr< ChunkContainer > rhs)
map_t< coords::ChunkCoordinates, std::unique_ptr< Chunk > > & getChunks ()
const map_t< coords::ChunkCoordinates, std::unique_ptr< Chunk > > & getChunks () const
types::Bounds3i getChunkBounds () const
 ChunkContainer (const ChunkContainer &)=delete
ChunkContainer & operator= (const ChunkContainer &)=delete
Public Member Functions inherited from lodestone::common::string::StringSerializable
constexpr StringSerializable ()=default
virtual constexpr ~StringSerializable ()=default

Protected Attributes

map_t< coords::ChunkCoordinates, std::unique_ptr< Chunk > > m_chunks

Detailed Description

Holds chunks.

Constructor & Destructor Documentation

◆ ~ChunkContainer()

lodestone::level::chunk::ChunkContainer::~ChunkContainer ( )
overridedefault

◆ ChunkContainer() [1/3]

lodestone::level::chunk::ChunkContainer::ChunkContainer ( )
default

◆ ChunkContainer() [2/3]

lodestone::level::chunk::ChunkContainer::ChunkContainer ( ChunkContainer && )
default

◆ ChunkContainer() [3/3]

lodestone::level::chunk::ChunkContainer::ChunkContainer ( const ChunkContainer & )
delete

Member Function Documentation

◆ addChunk() [1/3]

void lodestone::level::chunk::ChunkContainer::addChunk ( const coords::ChunkCoordinates & coords,
std::unique_ptr< Chunk > chunk )
61 {
62 if (m_chunks.contains(coords))
63 throw std::runtime_error(std::format(
64 "Tried to place chunk over existing chunk at coordinates {}",
65 coords.toString()));
66 if (!isChunkInBounds(coords))
67 throw new std::runtime_error("Attempted to place a chunk past the "
68 "bounds of the ChunkContainer");
69
70 chunk->attach(this, coords);
71 m_chunks[coords] = std::move(chunk);
72 }
virtual bool isChunkInBounds(const coords::ChunkCoordinates &coords)=0
map_t< coords::ChunkCoordinates, std::unique_ptr< Chunk > > m_chunks
Definition ChunkContainer.h:132

◆ addChunk() [2/3]

void lodestone::level::chunk::ChunkContainer::addChunk ( const int x,
const int z,
std::unique_ptr< Chunk > chunk )
inline
70 {
71 addChunk({x, z}, std::move(chunk));
72 }
void addChunk(std::unique_ptr< Chunk > chunk)
Definition ChunkContainer.cpp:40

◆ addChunk() [3/3]

void lodestone::level::chunk::ChunkContainer::addChunk ( std::unique_ptr< Chunk > chunk)
40 {
41 const std::optional<coords::ChunkCoordinates> &optCoords = chunk->getCoords();
42
43 if (!optCoords.has_value())
44 throw std::runtime_error(
45 "Tried to place chunk that does not contain coords");
46 const coords::ChunkCoordinates &coords = optCoords.value();
47
48 if (m_chunks.contains(coords))
49 throw std::runtime_error(std::format(
50 "Tried to place chunk over existing chunk at coordinates {}",
51 coords.toString()));
52 if (!isChunkInBounds(coords))
53 throw new std::runtime_error("Attempted to place a chunk past the "
54 "bounds of the ChunkContainer");
55
56 chunk->attach(this);
57 m_chunks[coords] = std::move(chunk);
58 }

◆ createChunk() [1/2]

chunk::Chunk * lodestone::level::chunk::ChunkContainer::createChunk ( const coords::ChunkCoordinates & coords,
int height = 256 )

Creates a chunk at the given coordinates with the given height.

Parameters
coordsThe coordinates you want to create a chunk at
heightThe height you'd like to make the new chunk
Returns
The new chunk
20 {
21 if (hasChunk(coords))
22 throw std::runtime_error(
23 "Attempted to place a chunk where there already was one");
24 if (!isChunkInBounds(coords))
25 throw std::runtime_error("Attempted to place a chunk past the "
26 "bounds of the ChunkContainer");
27
28 std::unique_ptr<chunk::LevelChunk> ch =
29 std::make_unique<chunk::LevelChunk>(height);
30 ch->attach(this, coords);
31
32 chunk::Chunk *c = ch.get();
33 m_chunks[coords] = std::move(ch);
34
35 LOG_DEBUG("Created chunk at X: " << coords.x << ", Z: " << coords.y);
36
37 return c;
38 }
#define LOG_DEBUG(...)
bool hasChunk(const coords::ChunkCoordinates &coords) const
Returns true if chunk at coords exist.
Definition ChunkContainer.cpp:15

◆ createChunk() [2/2]

Chunk * lodestone::level::chunk::ChunkContainer::createChunk ( const int x,
const int z,
const int height = 256 )
inline

Creates a chunk at the given coordinates with the given height.

Parameters
xThe x coordinate you want to create a chunk at
zThe z coordinate you want to create a chunk at
heightThe height you'd like to make the new chunk
Returns
The new chunk
62 {
63 return createChunk({x, z}, height);
64 };
Chunk * createChunk(const coords::ChunkCoordinates &coords, int height=256)
Creates a chunk at the given coordinates with the given height.
Definition ChunkContainer.cpp:19

◆ detachChunk() [1/2]

std::unique_ptr< chunk::Chunk > lodestone::level::chunk::ChunkContainer::detachChunk ( const coords::ChunkCoordinates & coords,
bool shouldInvalidateCoords = true )
99 {
100 const auto it = m_chunks.find(coords);
101 if (it == m_chunks.end())
102 throw std::runtime_error(
103 std::format("Tried to detach nonexistent chunk at coords {}",
104 coords.toString()));
105
106 std::unique_ptr<chunk::Chunk> chunk = std::move(it->second);
107 chunk->detach(shouldInvalidateCoords);
108
109 m_chunks.erase(it);
110
111 return chunk;
112 }
it
Definition ItemInstance.cpp:26

◆ detachChunk() [2/2]

std::unique_ptr< Chunk > lodestone::level::chunk::ChunkContainer::detachChunk ( const int x,
const int z,
const bool shouldInvalidateCoords = true )
inline
107 {
108 return detachChunk({x, z}, shouldInvalidateCoords);
109 };
std::unique_ptr< Chunk > detachChunk(const coords::ChunkCoordinates &coords, bool shouldInvalidateCoords=true)
Definition ChunkContainer.cpp:98

◆ getChunk() [1/4]

chunk::Chunk * lodestone::level::chunk::ChunkContainer::getChunk ( const coords::ChunkCoordinates & coords)
74 {
75 if (const auto it = m_chunks.find(coords); it != m_chunks.end())
76 return it->second.get();
77
78 return nullptr;
79 }

◆ getChunk() [2/4]

const chunk::Chunk * lodestone::level::chunk::ChunkContainer::getChunk ( const coords::ChunkCoordinates & coords) const
90 {
91 if (const auto it = m_chunks.find(coords); it != m_chunks.end())
92 return it->second.get();
93
94 return nullptr;
95 }

◆ getChunk() [3/4]

Chunk * lodestone::level::chunk::ChunkContainer::getChunk ( const int x,
const int z )
inline
76{ return getChunk({x, z}); };
Chunk * getChunk(const coords::ChunkCoordinates &coords)
Definition ChunkContainer.cpp:74

◆ getChunk() [4/4]

const Chunk * lodestone::level::chunk::ChunkContainer::getChunk ( const int x,
const int z ) const
inline
94 {
95 return getChunk({x, z});
96 };

◆ getChunkBounds()

types::Bounds3i lodestone::level::chunk::ChunkContainer::getChunkBounds ( ) const
131 {
132 int minX = INT_MAX;
133 int minY = INT_MAX;
134 int minZ = INT_MAX;
135 int maxX = INT_MIN;
136 int maxY = INT_MIN;
137 int maxZ = INT_MIN;
138
139 if (m_chunks.empty()) {
140 return {0, 0, 0, 0, 0, 0};
141 }
142
143 for (const auto &[coord, chonk] : m_chunks) {
144 minX = std::min(minX, coord.x);
145 maxX = std::max(maxX, coord.x);
146 minY = std::min(minY, 0);
147 maxY = std::max(maxY, chonk->getChunkHeight());
148 minZ = std::min(minZ, coord.y);
149 maxZ = std::max(maxZ, coord.y);
150 }
151
152 return {minX,
153 minY,
154 minZ,
155 common::util::Math::ceilDiv(maxX - minX + 1, 1),
156 common::util::Math::ceilDiv(maxY - minY + 1, 1),
157 common::util::Math::ceilDiv(maxZ - minZ + 1, 1)};
158 }
static constexpr int ceilDiv(const int x, const int y)

◆ getChunkCreate() [1/2]

chunk::Chunk * lodestone::level::chunk::ChunkContainer::getChunkCreate ( const coords::ChunkCoordinates & coords,
int height = 256 )
82 {
83 if (const auto it = m_chunks.find(coords); it != m_chunks.end())
84 return it->second.get();
85
86 return createChunk(coords, height);
87 }

◆ getChunkCreate() [2/2]

Chunk * lodestone::level::chunk::ChunkContainer::getChunkCreate ( const int x,
const int z,
const int height = 256 )
inline
84 {
85 return getChunkCreate({x, z}, height);
86 };
Chunk * getChunkCreate(const coords::ChunkCoordinates &coords, int height=256)
Definition ChunkContainer.cpp:81

◆ getChunkFromBlockCoords() [1/2]

Chunk * lodestone::level::chunk::ChunkContainer::getChunkFromBlockCoords ( const signed_size_t blockX,
const signed_size_t blockZ )
inline
78 {
80 };
static constexpr ChunkCoordinates fromBlockCoordinates(const signed_size_t blockX, const signed_size_t blockZ)
Definition ChunkCoordinates.h:30

◆ getChunkFromBlockCoords() [2/2]

const Chunk * lodestone::level::chunk::ChunkContainer::getChunkFromBlockCoords ( const signed_size_t blockX,
const signed_size_t blockZ ) const
inline
98 {
100 };

◆ getChunkFromBlockCoordsCreate()

Chunk * lodestone::level::chunk::ChunkContainer::getChunkFromBlockCoordsCreate ( const int blockX,
const int blockZ,
const int height = 256 )
inline
88 {
90 };

◆ getChunks() [1/2]

map_t< coords::ChunkCoordinates, std::unique_ptr< Chunk > > & lodestone::level::chunk::ChunkContainer::getChunks ( )
inline
117 {
118 return m_chunks;
119 }

◆ getChunks() [2/2]

const map_t< coords::ChunkCoordinates, std::unique_ptr< Chunk > > & lodestone::level::chunk::ChunkContainer::getChunks ( ) const
inline
121 {
122 return m_chunks;
123 }

◆ hasChunk() [1/2]

bool lodestone::level::chunk::ChunkContainer::hasChunk ( const coords::ChunkCoordinates & coords) const

Returns true if chunk at coords exist.

Parameters
coordsThe coordinates you want to check for a chunk
Returns
true if a chunk at given coords exist
15 {
16 return m_chunks.contains(coords);
17 }

◆ hasChunk() [2/2]

bool lodestone::level::chunk::ChunkContainer::hasChunk ( const int x,
const int z ) const
inline

Returns true if chunk at coords exist.

Parameters
xThe X coordinate you want to check for a chunk
zThe Z coordinate you want to check for a chunk
Returns
true if a chunk at given coords exist
43 {
44 return hasChunk({x, z});
45 };

◆ isChunkInBounds()

virtual bool lodestone::level::chunk::ChunkContainer::isChunkInBounds ( const coords::ChunkCoordinates & coords)
pure virtual

◆ merge()

void lodestone::level::chunk::ChunkContainer::merge ( std::unique_ptr< ChunkContainer > rhs)
118 {
119 for (auto it = rhs->m_chunks.begin(); it != rhs->m_chunks.end();) {
120 if (auto dest = m_chunks.find(it->first);
121 dest == m_chunks.end() && this->isChunkInBounds(it->first)) {
122 it->second->attach(this);
123 this->m_chunks.emplace(it->first, std::move(it->second));
124 }
125
126 it = rhs->m_chunks.erase(it);
127 }
128 }

◆ operator=() [1/2]

ChunkContainer & lodestone::level::chunk::ChunkContainer::operator= ( ChunkContainer && )
default

◆ operator=() [2/2]

ChunkContainer & lodestone::level::chunk::ChunkContainer::operator= ( const ChunkContainer & )
delete

◆ removeChunk() [1/2]

void lodestone::level::chunk::ChunkContainer::removeChunk ( const coords::ChunkCoordinates & coords)
114 {
115 m_chunks.erase(coords);
116 }

◆ removeChunk() [2/2]

void lodestone::level::chunk::ChunkContainer::removeChunk ( const int x,
const int z )
inline
113{ removeChunk({x, z}); };
void removeChunk(const coords::ChunkCoordinates &coords)
Definition ChunkContainer.cpp:114

◆ toString()

std::string lodestone::level::chunk::ChunkContainer::toString ( ) const
inlineoverridevirtual

Implements lodestone::common::string::StringSerializable.

24 {
25 return std::format("ChunkContainer[chunks={}, bounds={}]", m_chunks.size(), getChunkBounds().toString());
26 };
types::Bounds3i getChunkBounds() const
Definition ChunkContainer.cpp:131
std::string toString() const override
Definition ChunkContainer.h:24

Member Data Documentation

◆ m_chunks

map_t<coords::ChunkCoordinates, std::unique_ptr<Chunk> > lodestone::level::chunk::ChunkContainer::m_chunks
protected

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