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

A level holds Chunks. More...

#include <Level.h>

Inheritance diagram for lodestone::level::Level:
Collaboration diagram for lodestone::level::Level:

Public Member Functions

const block::instance::BlockInstance & getBlock (signed_size_t blockX, signed_size_t blockY, signed_size_t blockZ) const
void setBlock (block::instance::BlockInstance &&block, signed_size_t blockX, signed_size_t blockY, signed_size_t blockZ)
void setBlockRaw (block::instance::BlockInstance &&block, signed_size_t blockX, signed_size_t blockY, signed_size_t blockZ)
void setBlockCreate (block::instance::BlockInstance &&block, signed_size_t blockX, signed_size_t blockY, signed_size_t blockZ, int height=256)
void setBlockCreateRaw (block::instance::BlockInstance &&block, signed_size_t blockX, signed_size_t blockY, signed_size_t blockZ, int height=256)
int16_t getHeightAt (signed_size_t blockX, signed_size_t blockZ) const
void setHeightAt (int16_t height, signed_size_t blockX, signed_size_t blockZ)
void setHeightAtCreate (int16_t height, signed_size_t blockX, signed_size_t blockZ, int newChunkHeight=256)
const block::instance::BlockInstance & getBlockAtHeight (signed_size_t blockX, signed_size_t blockZ) const
void setBlockAtHeight (block::instance::BlockInstance &&block, signed_size_t blockX, signed_size_t blockZ)
bool isChunkInBounds (const coords::ChunkCoordinates &chunkCoordinates) override
size_t getBlockCount () const
types::Bounds3i getBlockBounds () const
world::World * getWorld () const
bool isInWorld () const
void setWorld (world::World *world)
types::Vec3i generateSpawnPos (unsigned int radius=21) const
virtual const level::types::Vec3i & getSpawnPos () const
virtual void setSpawnPos (const level::types::Vec3i &spawnPos)
virtual std::uint64_t getCreationTime () const
virtual void setCreationTime (std::uint64_t creationTime)
std::unique_ptr< properties::AbstractProperty > getProperty (const std::string &name) override
Public Member Functions inherited from lodestone::level::chunk::ChunkContainer
 ~ChunkContainer () override=default
 ChunkContainer ()=default
 ChunkContainer (ChunkContainer &&)=default
ChunkContainer & operator= (ChunkContainer &&)=default
std::string toString () const override
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
Public Member Functions inherited from lodestone::level::properties::ReflectiveProperties
virtual ~ReflectiveProperties ()=default
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)

Private Attributes

level::types::Vec3i m_spawnPos {0, 64, 0}
world::World * m_world = nullptr
std::uint64_t m_creationTime = lodestone::common::util::Util::getCurrentTimeMillis()

Additional Inherited Members

Protected Attributes inherited from lodestone::level::chunk::ChunkContainer
map_t< coords::ChunkCoordinates, std::unique_ptr< Chunk > > m_chunks

Detailed Description

A level holds Chunks.

See also
Chunk

Member Function Documentation

◆ generateSpawnPos()

types::Vec3i lodestone::level::Level::generateSpawnPos ( unsigned int radius = 21) const
162 {
164
165 if (radius != 0) {
166 std::random_device rd;
167 std::mt19937 gen(rd());
168 std::uniform_int_distribution<> distrib(-radius, radius);
169
170 int iter = 0;
171 while ((pos.y == 0 || getBlock(pos.x, pos.y - 1, pos.z) == block::BlockRegistry::s_defaultBlock) && iter < 20) {
172 // 20 tries ig
173 // spread the player
174 pos.x = distrib(gen);
175 pos.z = distrib(gen);
176
177 // make sure we spawn on top, maybe we should also check if the player's height in blocks is valid too
178 pos.y = getHeightAt(pos.x, pos.z);
179
180 iter++;
181 }
182 }
183
184 return pos;
185 }
int16_t getHeightAt(signed_size_t blockX, signed_size_t blockZ) const
Definition Level.cpp:75
const block::instance::BlockInstance & getBlock(signed_size_t blockX, signed_size_t blockY, signed_size_t blockZ) const
Definition Level.cpp:19
static const Block * s_defaultBlock
Definition BlockRegistry.h:27
Vec3< int > Vec3i
Definition Vec3.h:175
constexpr Vec3< T > VEC3_ZERO
Definition Vec3.h:170

◆ getBlock()

const block::instance::BlockInstance & lodestone::level::Level::getBlock ( signed_size_t blockX,
signed_size_t blockY,
signed_size_t blockZ ) const
21 {
22 if (const chunk::Chunk *c = getChunkFromBlockCoords(blockX, blockZ))
24
26 }
static ImmutableBlockInstance * getInstance()
Definition ImmutableBlockInstance.cpp:10
Chunk * getChunkFromBlockCoords(const signed_size_t blockX, const signed_size_t blockZ)
Definition ChunkContainer.h:78
static constexpr int blockToLocalChunkZ(const signed_size_t blockZ)
Definition ChunkCoordinates.h:54
static constexpr int blockToLocalChunkX(const signed_size_t blockX)
Definition ChunkCoordinates.h:50

◆ getBlockAtHeight()

const block::instance::BlockInstance & lodestone::level::Level::getBlockAtHeight ( signed_size_t blockX,
signed_size_t blockZ ) const
106 {
107 const chunk::Chunk *c = getChunkFromBlockCoords(blockX, blockZ);
108 if (c == nullptr) {
110 }
111
112 return c->getBlockAtHeight(coords::ChunkCoordinates::blockToLocalChunkX(blockX),
114 }
static ImmutableChunk * getInstance()
Definition ImmutableChunk.cpp:7

◆ getBlockBounds()

types::Bounds3i lodestone::level::Level::getBlockBounds ( ) const
133 {
134 int minX = INT_MAX;
135 int minY = INT_MAX;
136 int minZ = INT_MAX;
137 int maxX = INT_MIN;
138 int maxY = INT_MIN;
139 int maxZ = INT_MIN;
140
141 for (const auto &[coord, chonk] : m_chunks) {
142 int mX = coord.x * common::constants::CHUNK_WIDTH;
143 int mZ = coord.y * common::constants::CHUNK_DEPTH;
144
145 minX = std::min(minX, mX);
146 maxX = std::max(maxX, mX + common::constants::CHUNK_WIDTH - 1);
147 minY = std::min(minY, 0); // TODO: minimum block height
148 maxY = std::max(maxY, chonk->getChunkBlockHeight());
149 minZ = std::min(minZ, mZ);
150 maxZ = std::max(maxZ, mZ + common::constants::CHUNK_DEPTH - 1);
151 }
152
153 return {minX, minY, minZ, maxX, maxY, maxZ};
154 }
map_t< coords::ChunkCoordinates, std::unique_ptr< Chunk > > m_chunks
Definition ChunkContainer.h:132
static constexpr int CHUNK_WIDTH
static constexpr int CHUNK_DEPTH

◆ getBlockCount()

size_t lodestone::level::Level::getBlockCount ( ) const
127 {
128 auto [min, max] = getBlockBounds();
129
130 return (max.x - min.x + 1) * (max.y - min.y) * (max.z - min.z + 1);
131 }
types::Bounds3i getBlockBounds() const
Definition Level.cpp:133

◆ getCreationTime()

std::uint64_t lodestone::level::Level::getCreationTime ( ) const
virtual
195 {
196 return m_creationTime;
197 }
std::uint64_t m_creationTime
Definition Level.h:81

◆ getHeightAt()

int16_t lodestone::level::Level::getHeightAt ( signed_size_t blockX,
signed_size_t blockZ ) const
76 {
77 if (const chunk::Chunk *c = getChunkFromBlockCoords(blockX, blockZ))
78 return c->getHeightAt(
81
82 return 0;
83 }

◆ getProperty()

std::unique_ptr< level::properties::AbstractProperty > lodestone::level::Level::getProperty ( const std::string & name)
overridevirtual

Reimplemented from lodestone::level::properties::ReflectiveProperties.

203 {
204 return nullptr;
205 }

◆ getSpawnPos()

const level::types::Vec3i & lodestone::level::Level::getSpawnPos ( ) const
virtual
187 {
188 return this->m_spawnPos;
189 }
level::types::Vec3i m_spawnPos
Definition Level.h:78

◆ getWorld()

world::World * lodestone::level::Level::getWorld ( ) const
156{ return m_world; }
world::World * m_world
Definition Level.h:79

◆ isChunkInBounds()

bool lodestone::level::Level::isChunkInBounds ( const coords::ChunkCoordinates & chunkCoordinates)
overridevirtual

Implements lodestone::level::chunk::ChunkContainer.

17{ return true; }

◆ isInWorld()

bool lodestone::level::Level::isInWorld ( ) const
158{ return getWorld(); }
world::World * getWorld() const
Definition Level.cpp:156

◆ setBlock()

void lodestone::level::Level::setBlock ( block::instance::BlockInstance && block,
signed_size_t blockX,
signed_size_t blockY,
signed_size_t blockZ )
30 {
31 if (chunk::Chunk *c = getChunkFromBlockCoords(blockX, blockZ))
32 c->setBlock(std::move(block),
35 }

◆ setBlockAtHeight()

void lodestone::level::Level::setBlockAtHeight ( block::instance::BlockInstance && block,
signed_size_t blockX,
signed_size_t blockZ )
116 {
117 chunk::Chunk *c = getChunkFromBlockCoords(blockX, blockZ);
118 if (c == nullptr) {
120 }
121
122 c->setBlockAtHeight(std::move(block), coords::ChunkCoordinates::blockToLocalChunkX(blockX),
124 }

◆ setBlockCreate()

void lodestone::level::Level::setBlockCreate ( block::instance::BlockInstance && block,
signed_size_t blockX,
signed_size_t blockY,
signed_size_t blockZ,
int height = 256 )
39 {
40 chunk::Chunk *c = getChunkFromBlockCoords(blockX, blockZ);
41
42 if (!c)
44
45 c->setBlock(std::move(block),
48 }
Chunk * createChunk(const coords::ChunkCoordinates &coords, int height=256)
Creates a chunk at the given coordinates with the given height.
Definition ChunkContainer.cpp:19
static constexpr ChunkCoordinates fromBlockCoordinates(const signed_size_t blockX, const signed_size_t blockZ)
Definition ChunkCoordinates.h:30

◆ setBlockCreateRaw()

void lodestone::level::Level::setBlockCreateRaw ( block::instance::BlockInstance && block,
signed_size_t blockX,
signed_size_t blockY,
signed_size_t blockZ,
int height = 256 )
62 {
63 chunk::Chunk *c = getChunkFromBlockCoords(blockX, blockZ);
64
65 if (!c)
67
68 c->setBlockRaw(std::move(block),
71 }

◆ setBlockRaw()

void lodestone::level::Level::setBlockRaw ( block::instance::BlockInstance && block,
signed_size_t blockX,
signed_size_t blockY,
signed_size_t blockZ )
52 {
53 if (chunk::Chunk *c = getChunkFromBlockCoords(blockX, blockZ))
54 c->setBlockRaw(std::move(block),
56 blockY,
58 }

◆ setCreationTime()

void lodestone::level::Level::setCreationTime ( std::uint64_t creationTime)
virtual
199 {
200 m_creationTime = creationTime;
201 }

◆ setHeightAt()

void lodestone::level::Level::setHeightAt ( int16_t height,
signed_size_t blockX,
signed_size_t blockZ )
86 {
87 if (chunk::Chunk *c = getChunkFromBlockCoords(blockX, blockZ))
88 c->setHeightAt(height,
91 }

◆ setHeightAtCreate()

void lodestone::level::Level::setHeightAtCreate ( int16_t height,
signed_size_t blockX,
signed_size_t blockZ,
int newChunkHeight = 256 )
94 {
95 chunk::Chunk *c = getChunkFromBlockCoords(blockX, blockZ);
96
97 if (!c)
98 c = createChunk(coords::ChunkCoordinates::fromBlockCoordinates(blockX, blockZ), newChunkHeight);
99
100 c->setHeightAt(height, coords::ChunkCoordinates::blockToLocalChunkX(blockX),
102 }

◆ setSpawnPos()

void lodestone::level::Level::setSpawnPos ( const level::types::Vec3i & spawnPos)
virtual
191 {
192 this->m_spawnPos = spawnPos;
193 }

◆ setWorld()

void lodestone::level::Level::setWorld ( world::World * world)
160{ this->m_world = world; }

Member Data Documentation

◆ m_creationTime

std::uint64_t lodestone::level::Level::m_creationTime = lodestone::common::util::Util::getCurrentTimeMillis()
private

◆ m_spawnPos

level::types::Vec3i lodestone::level::Level::m_spawnPos {0, 64, 0}
private
78{0, 64, 0};

◆ m_world

world::World* lodestone::level::Level::m_world = nullptr
private

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