Chunk: move CX, CY, CZ into class, more documentation

master
Dorian Wouters 2017-03-16 13:29:56 -04:00
parent 24fbabd6d3
commit 710ff2537f
No known key found for this signature in database
GPG Key ID: 6E9DA8063322434B
10 changed files with 94 additions and 51 deletions

View File

@ -69,6 +69,7 @@ void CaveGenerator::Generate(WorldRef wr, const GenConf &gc, ChunkRef cr) {
c.setBlock(x, y, CZ-1, Content::BlockUnknownId);
}*/
constexpr auto CX = Chunk::CX, CY = Chunk::CY, CZ = Chunk::CZ;
const glm::ivec3 cp = c.getWorldChunkPos() * glm::ivec3(CX, CY, CZ);
for (int ly = 0; ly < CY; ++ly) {
int y = cp.y + ly;
@ -122,4 +123,4 @@ void CaveGenerator::Generate(WorldRef wr, const GenConf &gc, ChunkRef cr) {
c.state = Chunk::State::Ready;
}
}
}

View File

@ -34,9 +34,9 @@ static const char *TAG = "Chunk";
constexpr float Chunk::CullSphereRadius;
constexpr float Chunk::MidX, Chunk::MidY, Chunk::MidZ;
static constexpr int CXY = CX*CY;
static constexpr int CXY = Chunk::CX*Chunk::CY;
static constexpr int I(int x, int y, int z) {
return x + y*CX + z*CXY;
return x + y*Chunk::CX + z*CXY;
}
void Chunk::Data::clear() {

View File

@ -30,9 +30,6 @@ struct BlockUpdateNotify;
}
}
constexpr int CX = 16, CY = 16, CZ = 16;
class Chunk {
private:
friend World;
@ -41,6 +38,15 @@ private:
uintptr_t rendererData;
public:
constexpr static int
CX = 16, /**< Chunk size along X axis, in blocks. */
CY = 16, /**< Chunk size along Y axis, in blocks. */
CZ = 16; /**< Chunk size along Z axis, in blocks. */
constexpr static float CullSphereRadius =
(CZ > (CX > CY ? CX : CY) ? CZ : (CX > CY ? CX : CY));
// * 1.4142135623f; but we're already at 2x the radius (i.e. diameter)
constexpr static float MidX = CX/2.f, MidY = CY/2.f, MidZ = CZ/2.f;
const int wcx, wcy, wcz;
struct Data {
@ -56,11 +62,11 @@ public:
constexpr static int AllocaSize = sizeof(Data);
enum class State : uint8 {
Unavailable,
Generating,
Loading,
Ready,
Evicted
Unavailable, /**< The Chunk is unavailable right now. */
Generating, /**< The Chunk is generating. */
Loading, /**< The Chunk is loading from storage or network. */
Ready, /**< The Chunk is loaded properly and ready to use. */
Evicted /**< The Chunk has been evicted from memory. */
};
struct Vertex {
@ -93,10 +99,6 @@ public:
void imcUncompress();
#endif
constexpr static float CullSphereRadius =
(CZ > (CX > CY ? CX : CY) ? CZ : (CX > CY ? CX : CY));
// * 1.4142135623f; but we're already at 2x the radius (i.e. diameter)
constexpr static float MidX = CX/2.f, MidY = CY/2.f, MidZ = CZ/2.f;
uint blkMem;
State getState();
@ -125,6 +127,10 @@ public:
/* ============ Getters ============ */
/**
* @brief Get the World in which this Chunk resides.
* @return Reference to belonging World.
*/
inline WorldRef getWorld() const {
return W;
}
@ -133,27 +139,45 @@ public:
return glm::ivec3(wcx, wcy, wcz);
}
///
/// @returns The block ID at specified location.
///
/**
* @brief Get the block ID at specified location.
* @param x X block coordinate within the Chunk.
* @param y Y block coordinate within the Chunk.
* @param z Z block coordinate within the Chunk.
* @return The block ID at specified location.
*/
BlockId getBlockId(int x, int y, int z);
///
/// @returns Block's data integer.
///
/**
* @brief Get the block data at specified location.
* @param x X block coordinate within the Chunk.
* @param y Y block coordinate within the Chunk.
* @param z Z block coordinate within the Chunk.
* @return Block's data integer.
*/
BlockData getBlockData(int x, int y, int z);
///
/// @returns `true` if block has extdata, `false` otherwise.
///
/**
* @brief Get if the block has extdata.
* @param x X block coordinate within the Chunk.
* @param y Y block coordinate within the Chunk.
* @param z Z block coordinate within the Chunk.
* @return `true` if block has extdata, `false` otherwise.
*/
bool blockHasExtdata(int x, int y, int z);
///
/// @brief Gets a block's extdata.
/// Gets a block's extdata store, to save advanced state values.
/// @returns Block's extdata.
/// @throws NoExtdataOnBlock if the targeted block doesn't have extdata
///
// TODO msgpack::object& getBlockExtdata(int x, int y, int z);
#if 0
/**
* @brief Gets a block's extdata.
* Gets a block's extdata store, to save advanced state values.
* @param x
* @param y
* @param z
* @return Block's extdata.
* @throws NoExtdataOnBlock if the targeted block doesn't have extdata
*/
goodform::object& getBlockExtdata(int x, int y, int z);
#endif
bool isDirty() const {
return dirty;
@ -161,19 +185,32 @@ public:
/* ============ Setters ============ */
///
/// @brief Sets the block at specified location, replacing its ID and data.
///
/**
* @brief Set the block at specified location, replacing its ID and data.
* @param x X block coordinate within the Chunk.
* @param y Y block coordinate within the Chunk.
* @param z Z block coordinate within the Chunk.
* @param id Block ID to set.
* @param data Block data to set.
*/
void setBlock(int x, int y, int z, BlockId id, BlockData data = 0);
///
/// @brief Sets the block ID at specified location, keeping its (meta)data.
///
/**
* @brief Set the block ID at specified location, keeping its (meta)data.
* @param x X block coordinate within the Chunk.
* @param y Y block coordinate within the Chunk.
* @param z Z block coordinate within the Chunk.
* @param id Block ID to set.
*/
void setBlockId(int x, int y, int z, BlockId id);
///
/// @brief Sets the block data at specified location, keeping its ID.
///
/**
* @brief Set the block data at specified location, keeping its ID.
* @param x X block coordinate within the Chunk.
* @param y Y block coordinate within the Chunk.
* @param z Z block coordinate within the Chunk.
* @param data Block data to set.
*/
void setBlockData(int x, int y, int z, BlockData data);
// Copies extdata tree
@ -183,10 +220,10 @@ public:
void notifyChange(int x, int y, int z);
///
/// @brief Marks chunk as dirty.
/// Marks chunk as dirty so it is re-rendered.
///
/**
* @brief Marks chunk as dirty.
* Marks chunk as dirty so it is re-rendered.
*/
void markAsDirty();
void updateClient();

View File

@ -612,6 +612,7 @@ void GameState::updateUI() {
maxChunkMem += Chunk::AllocaSize;
}
}
constexpr auto CX = Chunk::CX, CY = Chunk::CY, CZ = Chunk::CZ;
std::ostringstream oss;
oss << std::setprecision(3) <<
"HP: " << LP.health << std::endl <<
@ -625,7 +626,7 @@ void GameState::updateUI() {
std::endl <<
"Pointing at: " << LP.W->getBlockId(m_pointedBlock.x, m_pointedBlock.y, m_pointedBlock.z) <<
" @ " << m_pointedBlock.x << ' ' << m_pointedBlock.y << ' ' << m_pointedBlock.z <<
" C: " << divrd(m_pointedBlock.x, CX) << ' ' << divrd(m_pointedBlock.y, CZ) << ' ' <<
" C: " << divrd(m_pointedBlock.x, CX) << ' ' << divrd(m_pointedBlock.y, CY) << ' ' <<
divrd(m_pointedBlock.z, CZ) << std::endl <<
"RX: " << G->H.getRxBytes() << std::endl <<
"TX: " << G->H.getTxBytes() << std::endl <<

View File

@ -276,7 +276,7 @@ void LocalPlayer::jump() {
bool LocalPlayer::raytracePointed(glm::ivec3 *pointed, glm::ivec3 *facing) {
// TODO: renderdistance
return raytracePointed(CX*2, pointed, facing);
return raytracePointed(Chunk::CX*2, pointed, facing);
}
bool LocalPlayer::raytracePointed(float range, glm::ivec3 *pointed, glm::ivec3 *facing) {

View File

@ -253,6 +253,7 @@ void Server::handlePlayerMapUpdate(InMessage &msg, Player &plr) {
// TODO: distance & tool check, i.e. legitimate update
using namespace Net::MsgTypes;
using S = BlockUpdateSubtype;
constexpr auto CX = Chunk::CX, CY = Chunk::CY, CZ = Chunk::CZ;
switch (msg.getSubtype<S>()) {
case S::Notify: {
; // No-op

View File

@ -109,6 +109,8 @@ ChunkRef World::getLoadChunk(int cx, int cy, int cz) {
}
constexpr auto CX = Chunk::CX, CY = Chunk::CY, CZ = Chunk::CZ;
bool World::setBlock(int x, int y, int z, BlockId id, BlockData data) {
iterator it = find(glm::ivec3(divrd(x, CX), divrd(y, CY), divrd(z, CZ)));
if (it != end()) {

View File

@ -83,10 +83,10 @@ public:
///
ChunkRef getChunk(int cx, int cy, int cz);
inline ChunkRef getChunkAtCoords(int x, int y, int z) {
return getChunk(divrd(x, CX), divrd(y, CY), divrd(z, CZ));
return getChunk(divrd(x, Chunk::CX), divrd(y, Chunk::CY), divrd(z, Chunk::CZ));
}
inline ChunkRef getChunkAtCoords(const glm::ivec3 &v) {
return getChunk(divrd(v.x, CX), divrd(v.y, CY), divrd(v.z, CZ));
return getChunk(divrd(v.x, Chunk::CX), divrd(v.y, Chunk::CY), divrd(v.z, Chunk::CZ));
}
///
@ -97,10 +97,10 @@ public:
///
ChunkRef getLoadChunk(int cx, int cy, int cz);
inline ChunkRef getLoadChunkAtCoords(int x, int y, int z) {
return getLoadChunk(divrd(x, CX), divrd(y, CY), divrd(z, CZ));
return getLoadChunk(divrd(x, Chunk::CX), divrd(y, Chunk::CY), divrd(z, Chunk::CZ));
}
inline ChunkRef getLoadChunkAtCoords(const glm::ivec3 &v) {
return getLoadChunk(divrd(v.x, CX), divrd(v.y, CY), divrd(v.z, CZ));
return getLoadChunk(divrd(v.x, Chunk::CX), divrd(v.y, Chunk::CY), divrd(v.z, Chunk::CZ));
}
///

View File

@ -13,6 +13,7 @@ using namespace Net::MsgTypes;
bool BlockUpdateHandler::handle(GameState &GS, InMessage &msg) {
// TODO handle that in Chunk's ChangeHelper
using S = BlockUpdateSubtype;
constexpr auto CX = Chunk::CX, CY = Chunk::CY, CZ = Chunk::CZ;
switch (msg.getSubtype<S>()) {
case S::Notify: {
BlockUpdateNotify bun;

View File

@ -100,7 +100,7 @@ void GLWorldRenderer::render(RenderParams &rp) {
c->imcCompress();
#endif
ChunkEntry &ce = *reinterpret_cast<ChunkEntry*>(getRendererData(c.get()));
glm::vec3 translate(pos.x * CX, pos.y * CY, pos.z * CZ);
glm::vec3 translate(pos.x * Chunk::CX, pos.y * Chunk::CY, pos.z * Chunk::CZ);
if (rp.frustum.sphereInFrustum(translate + cShift, Chunk::CullSphereRadius)) {
chunkTransform = glm::translate(rp.transform, translate);
#if SHOW_CHUNK_UPDATES