[ChunkLightmap] Now handled by both client and server.
This commit is contained in:
parent
8c847a4eb6
commit
824c6d30c0
@ -16,69 +16,26 @@
|
||||
|
||||
#include <gk/gl/IDrawable.hpp>
|
||||
|
||||
#include "Chunk.hpp"
|
||||
#include "ChunkBuilder.hpp"
|
||||
#include "ChunkLightmap.hpp"
|
||||
#include "Config.hpp"
|
||||
|
||||
class ClientChunk {
|
||||
class ClientChunk : public Chunk {
|
||||
public:
|
||||
enum {
|
||||
Left,
|
||||
Right,
|
||||
Front,
|
||||
Back,
|
||||
Bottom,
|
||||
Top
|
||||
};
|
||||
|
||||
public:
|
||||
ClientChunk(s32 x, s32 y, s32 z, gk::Texture &texture);
|
||||
ClientChunk(s32 x, s32 y, s32 z, gk::Texture &texture)
|
||||
: Chunk(x, y, z), m_texture(texture) {}
|
||||
|
||||
void update();
|
||||
|
||||
u16 getBlock(int x, int y, int z) const;
|
||||
u16 getData(int x, int y, int z) const;
|
||||
void setBlock(int x, int y, int z, u16 type);
|
||||
void setData(int x, int y, int z, u16 data);
|
||||
|
||||
s32 x() const { return m_x; }
|
||||
s32 y() const { return m_y; }
|
||||
s32 z() const { return m_z; }
|
||||
|
||||
ClientChunk *getSurroundingChunk(u8 i) { return (i > 5) ? nullptr : m_surroundingChunks[i]; }
|
||||
const ClientChunk *getSurroundingChunk(u8 i) const { return (i > 5) ? nullptr : m_surroundingChunks[i]; }
|
||||
void setSurroundingChunk(u8 i, ClientChunk *chunk) { if (i < 6) m_surroundingChunks[i] = chunk; }
|
||||
|
||||
bool isInitialized() { return m_isInitialized; }
|
||||
|
||||
void setChanged(bool hasChanged) { m_hasChanged = hasChanged; }
|
||||
void setInitialized(bool isInitialized) { m_isInitialized = isInitialized; }
|
||||
|
||||
ChunkLightmap &lightmap() { return m_lightmap; }
|
||||
const ChunkLightmap &lightmap() const { return m_lightmap; }
|
||||
|
||||
void drawLayer(gk::RenderTarget &target, gk::RenderStates states, u8 layer) const;
|
||||
|
||||
private:
|
||||
s32 m_x;
|
||||
s32 m_y;
|
||||
s32 m_z;
|
||||
|
||||
gk::Texture &m_texture;
|
||||
|
||||
using DataArray = u32[CHUNK_WIDTH][CHUNK_HEIGHT][CHUNK_DEPTH];
|
||||
DataArray m_data;
|
||||
|
||||
ChunkBuilder m_builder;
|
||||
ChunkLightmap m_lightmap{this};
|
||||
|
||||
std::array<gk::VertexBuffer, ChunkBuilder::layers> m_vbo;
|
||||
std::array<std::size_t, ChunkBuilder::layers> m_verticesCount;
|
||||
|
||||
ClientChunk *m_surroundingChunks[6]{nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
|
||||
|
||||
bool m_hasChanged = false;
|
||||
bool m_isInitialized = false;
|
||||
};
|
||||
|
||||
#endif // CLIENTCHUNK_HPP_
|
||||
|
@ -13,16 +13,8 @@
|
||||
*/
|
||||
#include "ClientChunk.hpp"
|
||||
|
||||
ClientChunk::ClientChunk(s32 x, s32 y, s32 z, gk::Texture &texture) : m_texture(texture) {
|
||||
m_x = x;
|
||||
m_y = y;
|
||||
m_z = z;
|
||||
|
||||
std::memset(m_data, 0, sizeof(m_data));
|
||||
}
|
||||
|
||||
void ClientChunk::update() {
|
||||
if (m_hasChanged) {
|
||||
if (m_isInitialized && m_hasChanged) {
|
||||
m_hasChanged = false;
|
||||
m_lightmap.updateLights();
|
||||
|
||||
@ -30,53 +22,6 @@ void ClientChunk::update() {
|
||||
}
|
||||
}
|
||||
|
||||
u16 ClientChunk::getBlock(int x, int y, int z) const {
|
||||
if(x < 0) return m_surroundingChunks[0] ? m_surroundingChunks[0]->getBlock(x + CHUNK_WIDTH, y, z) : 0;
|
||||
if(x >= CHUNK_WIDTH) return m_surroundingChunks[1] ? m_surroundingChunks[1]->getBlock(x - CHUNK_WIDTH, y, z) : 0;
|
||||
if(y < 0) return m_surroundingChunks[4] ? m_surroundingChunks[4]->getBlock(x, y + CHUNK_HEIGHT, z) : 0;
|
||||
if(y >= CHUNK_HEIGHT) return m_surroundingChunks[5] ? m_surroundingChunks[5]->getBlock(x, y - CHUNK_HEIGHT, z) : 0;
|
||||
if(z < 0) return m_surroundingChunks[2] ? m_surroundingChunks[2]->getBlock(x, y, z + CHUNK_DEPTH) : 0;
|
||||
if(z >= CHUNK_DEPTH) return m_surroundingChunks[3] ? m_surroundingChunks[3]->getBlock(x, y, z - CHUNK_DEPTH) : 0;
|
||||
return m_data[x][y][z];
|
||||
}
|
||||
|
||||
u16 ClientChunk::getData(int x, int y, int z) const {
|
||||
if(x < 0) return m_surroundingChunks[0] ? m_surroundingChunks[0]->getData(x + CHUNK_WIDTH, y, z) : 0;
|
||||
if(x >= CHUNK_WIDTH) return m_surroundingChunks[1] ? m_surroundingChunks[1]->getData(x - CHUNK_WIDTH, y, z) : 0;
|
||||
if(y < 0) return m_surroundingChunks[4] ? m_surroundingChunks[4]->getData(x, y + CHUNK_HEIGHT, z) : 0;
|
||||
if(y >= CHUNK_HEIGHT) return m_surroundingChunks[5] ? m_surroundingChunks[5]->getData(x, y - CHUNK_HEIGHT, z) : 0;
|
||||
if(z < 0) return m_surroundingChunks[2] ? m_surroundingChunks[2]->getData(x, y, z + CHUNK_DEPTH) : 0;
|
||||
if(z >= CHUNK_DEPTH) return m_surroundingChunks[3] ? m_surroundingChunks[3]->getData(x, y, z - CHUNK_DEPTH) : 0;
|
||||
return (m_data[x][y][z] >> 16) & 0xffff;
|
||||
}
|
||||
|
||||
void ClientChunk::setBlock(int x, int y, int z, u16 type) {
|
||||
if(x < 0) { if(m_surroundingChunks[0]) m_surroundingChunks[0]->setBlock(x + CHUNK_WIDTH, y, z, type); return; }
|
||||
if(x >= CHUNK_WIDTH) { if(m_surroundingChunks[1]) m_surroundingChunks[1]->setBlock(x - CHUNK_WIDTH, y, z, type); return; }
|
||||
if(y < 0) { if(m_surroundingChunks[4]) m_surroundingChunks[4]->setBlock(x, y + CHUNK_HEIGHT, z, type); return; }
|
||||
if(y >= CHUNK_HEIGHT) { if(m_surroundingChunks[5]) m_surroundingChunks[5]->setBlock(x, y - CHUNK_HEIGHT, z, type); return; }
|
||||
if(z < 0) { if(m_surroundingChunks[2]) m_surroundingChunks[2]->setBlock(x, y, z + CHUNK_DEPTH, type); return; }
|
||||
if(z >= CHUNK_DEPTH) { if(m_surroundingChunks[3]) m_surroundingChunks[3]->setBlock(x, y, z - CHUNK_DEPTH, type); return; }
|
||||
|
||||
m_data[x][y][z] = type;
|
||||
|
||||
m_hasChanged = true;
|
||||
}
|
||||
|
||||
void ClientChunk::setData(int x, int y, int z, u16 data) {
|
||||
if(x < 0) { if(m_surroundingChunks[0]) m_surroundingChunks[0]->setData(x + CHUNK_WIDTH, y, z, data); return; }
|
||||
if(x >= CHUNK_WIDTH) { if(m_surroundingChunks[1]) m_surroundingChunks[1]->setData(x - CHUNK_WIDTH, y, z, data); return; }
|
||||
if(y < 0) { if(m_surroundingChunks[4]) m_surroundingChunks[4]->setData(x, y + CHUNK_HEIGHT, z, data); return; }
|
||||
if(y >= CHUNK_HEIGHT) { if(m_surroundingChunks[5]) m_surroundingChunks[5]->setData(x, y - CHUNK_HEIGHT, z, data); return; }
|
||||
if(z < 0) { if(m_surroundingChunks[2]) m_surroundingChunks[2]->setData(x, y, z + CHUNK_DEPTH, data); return; }
|
||||
if(z >= CHUNK_DEPTH) { if(m_surroundingChunks[3]) m_surroundingChunks[3]->setData(x, y, z - CHUNK_DEPTH, data); return; }
|
||||
|
||||
m_data[x][y][z] &= 0xffff;
|
||||
m_data[x][y][z] |= (data << 16);
|
||||
|
||||
m_hasChanged = true;
|
||||
}
|
||||
|
||||
void ClientChunk::drawLayer(gk::RenderTarget &target, gk::RenderStates states, u8 layer) const {
|
||||
if (m_verticesCount.at(layer) == 0) return;
|
||||
|
||||
|
@ -59,10 +59,13 @@ void ClientWorld::receiveChunkData(sf::Packet &packet) {
|
||||
for (u16 y = 0 ; y < CHUNK_HEIGHT ; ++y) {
|
||||
for (u16 x = 0 ; x < CHUNK_WIDTH ; ++x) {
|
||||
u16 block;
|
||||
packet >> block;
|
||||
u8 light;
|
||||
|
||||
chunk->setBlock(x, y, z, block);
|
||||
chunk->lightmap().addSunlight(x, y, z, 15);
|
||||
packet >> block >> light;
|
||||
|
||||
chunk->setBlock(x, y, z, block & 0xffff);
|
||||
// chunk->setData(x, y, z, block >> 16);
|
||||
chunk->lightmap().setLightData(x, y, z, light);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -26,8 +26,7 @@
|
||||
|
||||
#include "Block.hpp"
|
||||
#include "BlockData.hpp"
|
||||
// #include "ChunkBuilder.hpp"
|
||||
// #include "ChunkLightmap.hpp"
|
||||
#include "ChunkLightmap.hpp"
|
||||
|
||||
class Chunk : public gk::NonCopyable {
|
||||
public:
|
||||
@ -41,69 +40,58 @@ class Chunk : public gk::NonCopyable {
|
||||
};
|
||||
|
||||
public:
|
||||
// Chunk(s32 x, s32 y, s32 z, gk::Texture &texture);
|
||||
Chunk(s32 x, s32 y, s32 z);
|
||||
|
||||
void update(Player &player, World &world);
|
||||
virtual void update() = 0;
|
||||
|
||||
// u16 getBlock(int x, int y, int z) const;
|
||||
// u16 getData(int x, int y, int z) const;
|
||||
// void setBlock(int x, int y, int z, u16 type);
|
||||
// void setData(int x, int y, int z, u16 data);
|
||||
u16 getBlock(int x, int y, int z) const;
|
||||
u16 getData(int x, int y, int z) const;
|
||||
void setBlock(int x, int y, int z, u16 type);
|
||||
void setData(int x, int y, int z, u16 data);
|
||||
|
||||
BlockData *getBlockData(int x, int y, int z);
|
||||
// BlockData *getBlockData(int x, int y, int z);
|
||||
|
||||
// s32 x() const { return m_x; }
|
||||
// s32 y() const { return m_y; }
|
||||
// s32 z() const { return m_z; }
|
||||
s32 x() const { return m_x; }
|
||||
s32 y() const { return m_y; }
|
||||
s32 z() const { return m_z; }
|
||||
|
||||
// Chunk *getSurroundingChunk(u8 i) { return (i > 5) ? nullptr : m_surroundingChunks[i]; }
|
||||
// const Chunk *getSurroundingChunk(u8 i) const { return (i > 5) ? nullptr : m_surroundingChunks[i]; }
|
||||
// void setSurroundingChunk(u8 i, Chunk *chunk) { if (i < 6) m_surroundingChunks[i] = chunk; }
|
||||
Chunk *getSurroundingChunk(u8 i) { return (i > 5) ? nullptr : m_surroundingChunks[i]; }
|
||||
const Chunk *getSurroundingChunk(u8 i) const { return (i > 5) ? nullptr : m_surroundingChunks[i]; }
|
||||
void setSurroundingChunk(u8 i, Chunk *chunk) { if (i < 6) m_surroundingChunks[i] = chunk; }
|
||||
|
||||
bool isGenerated() const { return m_isGenerated; }
|
||||
bool isInitialized() const { return m_isInitialized; }
|
||||
|
||||
// void setChanged(bool isChanged) { m_isChanged = isChanged; }
|
||||
void setGenerated(bool isGenerated) { m_isGenerated = isGenerated; }
|
||||
void setChanged(bool hasChanged) { m_hasChanged = hasChanged; }
|
||||
void setInitialized(bool isInitialized) { m_isInitialized = isInitialized; }
|
||||
|
||||
// ChunkLightmap &lightmap() { return m_lightmap; }
|
||||
// const ChunkLightmap &lightmap() const { return m_lightmap; }
|
||||
ChunkLightmap &lightmap() { return m_lightmap; }
|
||||
const ChunkLightmap &lightmap() const { return m_lightmap; }
|
||||
|
||||
// static constexpr u8 width = CHUNK_WIDTH;
|
||||
// static constexpr u8 height = CHUNK_HEIGHT;
|
||||
// static constexpr u8 depth = CHUNK_DEPTH;
|
||||
static constexpr u8 width = CHUNK_WIDTH;
|
||||
static constexpr u8 height = CHUNK_HEIGHT;
|
||||
static constexpr u8 depth = CHUNK_DEPTH;
|
||||
|
||||
void drawLayer(gk::RenderTarget &target, gk::RenderStates states, u16 layer) const;
|
||||
protected:
|
||||
// void updateNeighbours(int x, int y, int z);
|
||||
|
||||
private:
|
||||
void updateNeighbours(int x, int y, int z);
|
||||
s32 m_x;
|
||||
s32 m_y;
|
||||
s32 m_z;
|
||||
|
||||
// s32 m_x;
|
||||
// s32 m_y;
|
||||
// s32 m_z;
|
||||
using DataArray = u32[Chunk::width][Chunk::height][Chunk::depth];
|
||||
DataArray m_data;
|
||||
|
||||
// gk::Texture &m_texture;
|
||||
ChunkLightmap m_lightmap{this};
|
||||
|
||||
// using DataArray = u32[Chunk::width][Chunk::height][Chunk::depth];
|
||||
// DataArray m_data;
|
||||
Chunk *m_surroundingChunks[6]{nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
|
||||
|
||||
// ChunkBuilder m_builder;
|
||||
// ChunkLightmap m_lightmap{this};
|
||||
|
||||
// std::array<gk::VertexBuffer, ChunkBuilder::layers> m_vbo;
|
||||
// std::array<std::size_t, ChunkBuilder::layers> m_verticesCount;
|
||||
|
||||
// Chunk *m_surroundingChunks[6]{nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
|
||||
|
||||
// bool m_isChanged = false;
|
||||
bool m_hasChanged = false;
|
||||
bool m_isInitialized = false;
|
||||
bool m_isGenerated = false;
|
||||
|
||||
u32 m_lastTick = 0;
|
||||
std::unordered_map<std::size_t, const Block&> m_tickingBlocks;
|
||||
|
||||
std::unordered_map<gk::Vector3i, BlockData> m_blockData;
|
||||
// u32 m_lastTick = 0;
|
||||
// std::unordered_map<std::size_t, const Block&> m_tickingBlocks;
|
||||
//
|
||||
// std::unordered_map<gk::Vector3i, BlockData> m_blockData;
|
||||
};
|
||||
|
||||
#endif // CHUNK_HPP_
|
||||
|
@ -35,11 +35,11 @@ struct LightRemovalNode {
|
||||
int value;
|
||||
};
|
||||
|
||||
class ClientChunk;
|
||||
class Chunk;
|
||||
|
||||
class ChunkLightmap {
|
||||
public:
|
||||
ChunkLightmap(ClientChunk *chunk);
|
||||
ChunkLightmap(Chunk *chunk);
|
||||
|
||||
void addLight(int x, int y, int z, int val);
|
||||
void addSunlight(int x, int y, int z, int val);
|
||||
@ -50,14 +50,19 @@ class ChunkLightmap {
|
||||
void updateTorchlight();
|
||||
void updateSunlight();
|
||||
|
||||
int getSunlight(int x, int y, int z) const;
|
||||
int getTorchlight(int x, int y, int z) const;
|
||||
u8 getSunlight(int x, int y, int z) const;
|
||||
u8 getTorchlight(int x, int y, int z) const;
|
||||
u8 getLightData(int x, int y, int z) const;
|
||||
|
||||
void setLightData(int x, int y, int z, u8 val);
|
||||
|
||||
private:
|
||||
void setTorchlight(int x, int y, int z, int val);
|
||||
void setSunlight(int x, int y, int z, int val);
|
||||
void setTorchlight(int x, int y, int z, u8 val);
|
||||
void setSunlight(int x, int y, int z, u8 val);
|
||||
|
||||
ClientChunk *m_chunk = nullptr;
|
||||
void updateSurroundingChunks(int x, int y, int z);
|
||||
|
||||
Chunk *m_chunk = nullptr;
|
||||
|
||||
using LightMapArray = u8[CHUNK_WIDTH][CHUNK_HEIGHT][CHUNK_DEPTH];
|
||||
LightMapArray m_lightMap;
|
@ -19,179 +19,143 @@
|
||||
#include "Config.hpp"
|
||||
#include "Registry.hpp"
|
||||
|
||||
// Chunk::Chunk(s32 x, s32 y, s32 z, gk::Texture &texture) : m_texture(texture) {
|
||||
// m_x = x;
|
||||
// m_y = y;
|
||||
// m_z = z;
|
||||
//
|
||||
// std::memset(m_data, 0, sizeof(m_data));
|
||||
// }
|
||||
Chunk::Chunk(s32 x, s32 y, s32 z) {
|
||||
m_x = x;
|
||||
m_y = y;
|
||||
m_z = z;
|
||||
|
||||
void Chunk::update(Player &player, World &world) {
|
||||
if (!m_tickingBlocks.empty() && m_lastTick < gk::GameClock::getTicks() / 50) {
|
||||
m_lastTick = gk::GameClock::getTicks() / 50;
|
||||
std::memset(m_data, 0, sizeof(m_data));
|
||||
}
|
||||
|
||||
// FIXME
|
||||
// for (auto &it : m_tickingBlocks) {
|
||||
// int z = it.first / (width * height);
|
||||
// int y = (it.first - z * width * height) / width;
|
||||
// int x = (it.first - z * width * height) % width;
|
||||
// it.second.onTick(glm::ivec3{x + m_x * width, y + m_y * height, z + m_z * depth}, player, *this, world);
|
||||
// }
|
||||
u16 Chunk::getBlock(int x, int y, int z) const {
|
||||
if(x < 0) return m_surroundingChunks[0] ? m_surroundingChunks[0]->getBlock(x + Chunk::width, y, z) : 0;
|
||||
if(x >= Chunk::width) return m_surroundingChunks[1] ? m_surroundingChunks[1]->getBlock(x - Chunk::width, y, z) : 0;
|
||||
if(y < 0) return m_surroundingChunks[4] ? m_surroundingChunks[4]->getBlock(x, y + Chunk::height, z) : 0;
|
||||
if(y >= Chunk::height) return m_surroundingChunks[5] ? m_surroundingChunks[5]->getBlock(x, y - Chunk::height, z) : 0;
|
||||
if(z < 0) return m_surroundingChunks[2] ? m_surroundingChunks[2]->getBlock(x, y, z + Chunk::depth) : 0;
|
||||
if(z >= Chunk::depth) return m_surroundingChunks[3] ? m_surroundingChunks[3]->getBlock(x, y, z - Chunk::depth) : 0;
|
||||
return m_data[x][y][z] & 0xffff;
|
||||
}
|
||||
|
||||
u16 Chunk::getData(int x, int y, int z) const {
|
||||
if(x < 0) return m_surroundingChunks[0] ? m_surroundingChunks[0]->getData(x + Chunk::width, y, z) : 0;
|
||||
if(x >= Chunk::width) return m_surroundingChunks[1] ? m_surroundingChunks[1]->getData(x - Chunk::width, y, z) : 0;
|
||||
if(y < 0) return m_surroundingChunks[4] ? m_surroundingChunks[4]->getData(x, y + Chunk::height, z) : 0;
|
||||
if(y >= Chunk::height) return m_surroundingChunks[5] ? m_surroundingChunks[5]->getData(x, y - Chunk::height, z) : 0;
|
||||
if(z < 0) return m_surroundingChunks[2] ? m_surroundingChunks[2]->getData(x, y, z + Chunk::depth) : 0;
|
||||
if(z >= Chunk::depth) return m_surroundingChunks[3] ? m_surroundingChunks[3]->getData(x, y, z - Chunk::depth) : 0;
|
||||
return (m_data[x][y][z] >> 16) & 0xffff;
|
||||
}
|
||||
|
||||
// #include "Debug.hpp"
|
||||
|
||||
void Chunk::setBlock(int x, int y, int z, u16 type) {
|
||||
if(x < 0) { if(m_surroundingChunks[0]) m_surroundingChunks[0]->setBlock(x + Chunk::width, y, z, type); return; }
|
||||
if(x >= Chunk::width) { if(m_surroundingChunks[1]) m_surroundingChunks[1]->setBlock(x - Chunk::width, y, z, type); return; }
|
||||
if(y < 0) { if(m_surroundingChunks[4]) m_surroundingChunks[4]->setBlock(x, y + Chunk::height, z, type); return; }
|
||||
if(y >= Chunk::height) { if(m_surroundingChunks[5]) m_surroundingChunks[5]->setBlock(x, y - Chunk::height, z, type); return; }
|
||||
if(z < 0) { if(m_surroundingChunks[2]) m_surroundingChunks[2]->setBlock(x, y, z + Chunk::depth, type); return; }
|
||||
if(z >= Chunk::depth) { if(m_surroundingChunks[3]) m_surroundingChunks[3]->setBlock(x, y, z - Chunk::depth, type); return; }
|
||||
|
||||
// FIXME
|
||||
// const Block &block = Registry::getInstance().getBlock(type);
|
||||
// // if (type == 8)
|
||||
// // DEBUG("at (", m_x, m_y, m_z, ")", "(", x, y, z, ")", type, "is", block.canUpdate());
|
||||
// if (block.canUpdate()) {
|
||||
// m_tickingBlocks.emplace(x + y * width + z * width * height, block);
|
||||
// }
|
||||
// else {
|
||||
// auto it = m_tickingBlocks.find(x + y * width + z * width * height);
|
||||
// if (it != m_tickingBlocks.end())
|
||||
// m_tickingBlocks.erase(it);
|
||||
// }
|
||||
|
||||
if (type == BlockType::Glowstone)
|
||||
m_lightmap.addLight(x, y, z, 14);
|
||||
else {
|
||||
// else if (m_data[x][y][z] == BlockType::Glowstone)
|
||||
m_lightmap.removeLight(x, y, z);
|
||||
// else {
|
||||
m_lightmap.removeSunlight(x, y, z);
|
||||
}
|
||||
|
||||
// if (m_isChanged) {
|
||||
// m_isChanged = false;
|
||||
// m_lightmap.updateLights();
|
||||
// FIXME
|
||||
// if (type == BlockType::Workbench)
|
||||
// m_blockData.emplace(gk::Vector3i{x, y, z}, BlockData{3, 3});
|
||||
// else if (type == BlockType::Furnace)
|
||||
// m_blockData.emplace(gk::Vector3i{x, y, z}, BlockData{3, 1});
|
||||
//
|
||||
// m_verticesCount = m_builder.buildChunk(*this, m_vbo);
|
||||
// if (m_data[x][y][z] == BlockType::Workbench || m_data[x][y][z] == BlockType::Furnace) {
|
||||
// auto it = m_blockData.find(gk::Vector3i{x, y, z});
|
||||
// if (it != m_blockData.end())
|
||||
// m_blockData.erase(it);
|
||||
// }
|
||||
|
||||
m_data[x][y][z] = type;
|
||||
|
||||
m_hasChanged = true;
|
||||
|
||||
// FIXME
|
||||
// updateNeighbours(x, y, z);
|
||||
|
||||
if(x == 0 && m_surroundingChunks[Left]) { m_surroundingChunks[Left]->m_hasChanged = true; }
|
||||
if(x == width - 1 && m_surroundingChunks[Right]) { m_surroundingChunks[Right]->m_hasChanged = true; }
|
||||
if(y == 0 && m_surroundingChunks[Bottom]) { m_surroundingChunks[Bottom]->m_hasChanged = true; }
|
||||
if(y == height - 1 && m_surroundingChunks[Top]) { m_surroundingChunks[Top]->m_hasChanged = true; }
|
||||
if(z == 0 && m_surroundingChunks[Front]) { m_surroundingChunks[Front]->m_hasChanged = true; }
|
||||
if(z == depth - 1 && m_surroundingChunks[Back]) { m_surroundingChunks[Back]->m_hasChanged = true; }
|
||||
}
|
||||
|
||||
void Chunk::setData(int x, int y, int z, u16 data) {
|
||||
if(x < 0) { if(m_surroundingChunks[0]) m_surroundingChunks[0]->setData(x + Chunk::width, y, z, data); return; }
|
||||
if(x >= Chunk::width) { if(m_surroundingChunks[1]) m_surroundingChunks[1]->setData(x - Chunk::width, y, z, data); return; }
|
||||
if(y < 0) { if(m_surroundingChunks[4]) m_surroundingChunks[4]->setData(x, y + Chunk::height, z, data); return; }
|
||||
if(y >= Chunk::height) { if(m_surroundingChunks[5]) m_surroundingChunks[5]->setData(x, y - Chunk::height, z, data); return; }
|
||||
if(z < 0) { if(m_surroundingChunks[2]) m_surroundingChunks[2]->setData(x, y, z + Chunk::depth, data); return; }
|
||||
if(z >= Chunk::depth) { if(m_surroundingChunks[3]) m_surroundingChunks[3]->setData(x, y, z - Chunk::depth, data); return; }
|
||||
|
||||
m_data[x][y][z] &= 0xffff;
|
||||
m_data[x][y][z] |= (data << 16);
|
||||
|
||||
m_hasChanged = true;
|
||||
}
|
||||
|
||||
// u16 Chunk::getBlock(int x, int y, int z) const {
|
||||
// if(x < 0) return m_surroundingChunks[0] ? m_surroundingChunks[0]->getBlock(x + Chunk::width, y, z) : 0;
|
||||
// if(x >= Chunk::width) return m_surroundingChunks[1] ? m_surroundingChunks[1]->getBlock(x - Chunk::width, y, z) : 0;
|
||||
// if(y < 0) return m_surroundingChunks[4] ? m_surroundingChunks[4]->getBlock(x, y + Chunk::height, z) : 0;
|
||||
// if(y >= Chunk::height) return m_surroundingChunks[5] ? m_surroundingChunks[5]->getBlock(x, y - Chunk::height, z) : 0;
|
||||
// if(z < 0) return m_surroundingChunks[2] ? m_surroundingChunks[2]->getBlock(x, y, z + Chunk::depth) : 0;
|
||||
// if(z >= Chunk::depth) return m_surroundingChunks[3] ? m_surroundingChunks[3]->getBlock(x, y, z - Chunk::depth) : 0;
|
||||
// return m_data[x][y][z] & 0xffff;
|
||||
// }
|
||||
//
|
||||
// u16 Chunk::getData(int x, int y, int z) const {
|
||||
// if(x < 0) return m_surroundingChunks[0] ? m_surroundingChunks[0]->getData(x + Chunk::width, y, z) : 0;
|
||||
// if(x >= Chunk::width) return m_surroundingChunks[1] ? m_surroundingChunks[1]->getData(x - Chunk::width, y, z) : 0;
|
||||
// if(y < 0) return m_surroundingChunks[4] ? m_surroundingChunks[4]->getData(x, y + Chunk::height, z) : 0;
|
||||
// if(y >= Chunk::height) return m_surroundingChunks[5] ? m_surroundingChunks[5]->getData(x, y - Chunk::height, z) : 0;
|
||||
// if(z < 0) return m_surroundingChunks[2] ? m_surroundingChunks[2]->getData(x, y, z + Chunk::depth) : 0;
|
||||
// if(z >= Chunk::depth) return m_surroundingChunks[3] ? m_surroundingChunks[3]->getData(x, y, z - Chunk::depth) : 0;
|
||||
// return (m_data[x][y][z] >> 16) & 0xffff;
|
||||
// }
|
||||
//
|
||||
// // #include "Debug.hpp"
|
||||
//
|
||||
// void Chunk::setBlock(int x, int y, int z, u16 type) {
|
||||
// if(x < 0) { if(m_surroundingChunks[0]) m_surroundingChunks[0]->setBlock(x + Chunk::width, y, z, type); return; }
|
||||
// if(x >= Chunk::width) { if(m_surroundingChunks[1]) m_surroundingChunks[1]->setBlock(x - Chunk::width, y, z, type); return; }
|
||||
// if(y < 0) { if(m_surroundingChunks[4]) m_surroundingChunks[4]->setBlock(x, y + Chunk::height, z, type); return; }
|
||||
// if(y >= Chunk::height) { if(m_surroundingChunks[5]) m_surroundingChunks[5]->setBlock(x, y - Chunk::height, z, type); return; }
|
||||
// if(z < 0) { if(m_surroundingChunks[2]) m_surroundingChunks[2]->setBlock(x, y, z + Chunk::depth, type); return; }
|
||||
// if(z >= Chunk::depth) { if(m_surroundingChunks[3]) m_surroundingChunks[3]->setBlock(x, y, z - Chunk::depth, type); return; }
|
||||
//
|
||||
// FIXME
|
||||
// const Block &block = Registry::getInstance().getBlock(type);
|
||||
// // if (type == 8)
|
||||
// // DEBUG("at (", m_x, m_y, m_z, ")", "(", x, y, z, ")", type, "is", block.canUpdate());
|
||||
// if (block.canUpdate()) {
|
||||
// m_tickingBlocks.emplace(x + y * width + z * width * height, block);
|
||||
// }
|
||||
// else {
|
||||
// auto it = m_tickingBlocks.find(x + y * width + z * width * height);
|
||||
// if (it != m_tickingBlocks.end())
|
||||
// m_tickingBlocks.erase(it);
|
||||
// }
|
||||
// BlockData *Chunk::getBlockData(int x, int y, int z) {
|
||||
// if(x < 0) return m_surroundingChunks[0] ? m_surroundingChunks[0]->getBlockData(x + CHUNK_WIDTH, y, z) : 0;
|
||||
// if(x >= CHUNK_WIDTH) return m_surroundingChunks[1] ? m_surroundingChunks[1]->getBlockData(x - CHUNK_WIDTH, y, z) : 0;
|
||||
// if(y < 0) return m_surroundingChunks[4] ? m_surroundingChunks[4]->getBlockData(x, y + CHUNK_HEIGHT, z) : 0;
|
||||
// if(y >= CHUNK_HEIGHT) return m_surroundingChunks[5] ? m_surroundingChunks[5]->getBlockData(x, y - CHUNK_HEIGHT, z) : 0;
|
||||
// if(z < 0) return m_surroundingChunks[2] ? m_surroundingChunks[2]->getBlockData(x, y, z + CHUNK_DEPTH) : 0;
|
||||
// if(z >= CHUNK_DEPTH) return m_surroundingChunks[3] ? m_surroundingChunks[3]->getBlockData(x, y, z - CHUNK_DEPTH) : 0;
|
||||
//
|
||||
// if (type == BlockType::Glowstone)
|
||||
// m_lightmap.addLight(x, y, z, 14);
|
||||
// else {
|
||||
// // else if (m_data[x][y][z] == BlockType::Glowstone)
|
||||
// m_lightmap.removeLight(x, y, z);
|
||||
// // else {
|
||||
// m_lightmap.removeSunlight(x, y, z);
|
||||
// }
|
||||
// auto it = m_blockData.find(gk::Vector3i{x, y, z});
|
||||
// if (it == m_blockData.end())
|
||||
// return nullptr;
|
||||
//
|
||||
// if (type == BlockType::Workbench)
|
||||
// m_blockData.emplace(gk::Vector3i{x, y, z}, BlockData{3, 3});
|
||||
// else if (type == BlockType::Furnace)
|
||||
// m_blockData.emplace(gk::Vector3i{x, y, z}, BlockData{3, 1});
|
||||
//
|
||||
// if (m_data[x][y][z] == BlockType::Workbench || m_data[x][y][z] == BlockType::Furnace) {
|
||||
// auto it = m_blockData.find(gk::Vector3i{x, y, z});
|
||||
// if (it != m_blockData.end())
|
||||
// m_blockData.erase(it);
|
||||
// }
|
||||
//
|
||||
// m_data[x][y][z] = type;
|
||||
//
|
||||
// m_isChanged = true;
|
||||
//
|
||||
// updateNeighbours(x, y, z);
|
||||
//
|
||||
// if(x == 0 && m_surroundingChunks[Left]) { m_surroundingChunks[Left]->m_isChanged = true; }
|
||||
// if(x == width - 1 && m_surroundingChunks[Right]) { m_surroundingChunks[Right]->m_isChanged = true; }
|
||||
// if(y == 0 && m_surroundingChunks[Bottom]) { m_surroundingChunks[Bottom]->m_isChanged = true; }
|
||||
// if(y == height - 1 && m_surroundingChunks[Top]) { m_surroundingChunks[Top]->m_isChanged = true; }
|
||||
// if(z == 0 && m_surroundingChunks[Front]) { m_surroundingChunks[Front]->m_isChanged = true; }
|
||||
// if(z == depth - 1 && m_surroundingChunks[Back]) { m_surroundingChunks[Back]->m_isChanged = true; }
|
||||
// }
|
||||
//
|
||||
// void Chunk::setData(int x, int y, int z, u16 data) {
|
||||
// if(x < 0) { if(m_surroundingChunks[0]) m_surroundingChunks[0]->setData(x + Chunk::width, y, z, data); return; }
|
||||
// if(x >= Chunk::width) { if(m_surroundingChunks[1]) m_surroundingChunks[1]->setData(x - Chunk::width, y, z, data); return; }
|
||||
// if(y < 0) { if(m_surroundingChunks[4]) m_surroundingChunks[4]->setData(x, y + Chunk::height, z, data); return; }
|
||||
// if(y >= Chunk::height) { if(m_surroundingChunks[5]) m_surroundingChunks[5]->setData(x, y - Chunk::height, z, data); return; }
|
||||
// if(z < 0) { if(m_surroundingChunks[2]) m_surroundingChunks[2]->setData(x, y, z + Chunk::depth, data); return; }
|
||||
// if(z >= Chunk::depth) { if(m_surroundingChunks[3]) m_surroundingChunks[3]->setData(x, y, z - Chunk::depth, data); return; }
|
||||
//
|
||||
// m_data[x][y][z] &= 0xffff;
|
||||
// m_data[x][y][z] |= (data << 16);
|
||||
//
|
||||
// m_isChanged = true;
|
||||
// return &it->second;
|
||||
// }
|
||||
|
||||
BlockData *Chunk::getBlockData(int x, int y, int z) {
|
||||
// FIXME
|
||||
// if(x < 0) return m_surroundingChunks[0] ? m_surroundingChunks[0]->getBlockData(x + CHUNK_WIDTH, y, z) : 0;
|
||||
// if(x >= CHUNK_WIDTH) return m_surroundingChunks[1] ? m_surroundingChunks[1]->getBlockData(x - CHUNK_WIDTH, y, z) : 0;
|
||||
// if(y < 0) return m_surroundingChunks[4] ? m_surroundingChunks[4]->getBlockData(x, y + CHUNK_HEIGHT, z) : 0;
|
||||
// if(y >= CHUNK_HEIGHT) return m_surroundingChunks[5] ? m_surroundingChunks[5]->getBlockData(x, y - CHUNK_HEIGHT, z) : 0;
|
||||
// if(z < 0) return m_surroundingChunks[2] ? m_surroundingChunks[2]->getBlockData(x, y, z + CHUNK_DEPTH) : 0;
|
||||
// if(z >= CHUNK_DEPTH) return m_surroundingChunks[3] ? m_surroundingChunks[3]->getBlockData(x, y, z - CHUNK_DEPTH) : 0;
|
||||
|
||||
auto it = m_blockData.find(gk::Vector3i{x, y, z});
|
||||
if (it == m_blockData.end())
|
||||
return nullptr;
|
||||
|
||||
return &it->second;
|
||||
}
|
||||
|
||||
void Chunk::updateNeighbours(int x, int y, int z) {
|
||||
int neighbours[7][3] = {
|
||||
{x, y, z},
|
||||
{x - 1, y, z},
|
||||
{x + 1, y, z},
|
||||
{x, y - 1, z},
|
||||
{x, y + 1, z},
|
||||
{x, y, z - 1},
|
||||
{x, y, z + 1},
|
||||
};
|
||||
|
||||
// FIXME
|
||||
// for (u32 i = 0 ; i < 7 ; ++i) {
|
||||
// u32 blockID = getBlock(neighbours[i][0], neighbours[i][1], neighbours[i][2]);
|
||||
// if (blockID) {
|
||||
// const Block &block = Registry::getInstance().getBlock(blockID);
|
||||
// block.onNeighbourUpdate(glm::vec3{x, y, z},
|
||||
// glm::vec3{neighbours[i][0], neighbours[i][1], neighbours[i][2]},
|
||||
// *this);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
void Chunk::drawLayer(gk::RenderTarget &target, gk::RenderStates states, u16 layer) const {
|
||||
// if (m_verticesCount.at(layer) == 0) return;
|
||||
//
|
||||
// states.texture = &m_texture;
|
||||
//
|
||||
// if (layer == ChunkBuilder::Layer::Other)
|
||||
// glDisable(GL_CULL_FACE);
|
||||
// else
|
||||
// glEnable(GL_CULL_FACE);
|
||||
// glEnable(GL_DEPTH_TEST);
|
||||
//
|
||||
// if(Config::isWireframeModeEnabled) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
// // target.draw(m_vbo.at(layer), GL_QUADS, 0, m_verticesCount.at(layer), states);
|
||||
// target.draw(m_vbo.at(layer), GL_TRIANGLES, 0, m_verticesCount.at(layer), states);
|
||||
// if(Config::isWireframeModeEnabled) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||
}
|
||||
// FIXME
|
||||
// void Chunk::updateNeighbours(int x, int y, int z) {
|
||||
// int neighbours[7][3] = {
|
||||
// {x, y, z},
|
||||
// {x - 1, y, z},
|
||||
// {x + 1, y, z},
|
||||
// {x, y - 1, z},
|
||||
// {x, y + 1, z},
|
||||
// {x, y, z - 1},
|
||||
// {x, y, z + 1},
|
||||
// };
|
||||
//
|
||||
// for (u32 i = 0 ; i < 7 ; ++i) {
|
||||
// u32 blockID = getBlock(neighbours[i][0], neighbours[i][1], neighbours[i][2]);
|
||||
// if (blockID) {
|
||||
// const Block &block = Registry::getInstance().getBlock(blockID);
|
||||
// block.onNeighbourUpdate(glm::vec3{x, y, z},
|
||||
// glm::vec3{neighbours[i][0], neighbours[i][1], neighbours[i][2]},
|
||||
// *this);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
|
@ -13,11 +13,11 @@
|
||||
*/
|
||||
#include <cstring>
|
||||
|
||||
#include "ClientChunk.hpp"
|
||||
#include "Chunk.hpp"
|
||||
#include "ChunkLightmap.hpp"
|
||||
#include "Registry.hpp"
|
||||
|
||||
ChunkLightmap::ChunkLightmap(ClientChunk *chunk) : m_chunk(chunk) {
|
||||
ChunkLightmap::ChunkLightmap(Chunk *chunk) : m_chunk(chunk) {
|
||||
std::memset(m_lightMap, 0, sizeof(m_lightMap));
|
||||
}
|
||||
|
||||
@ -151,7 +151,7 @@ void ChunkLightmap::updateSunlight() {
|
||||
}
|
||||
}
|
||||
|
||||
int ChunkLightmap::getSunlight(int x, int y, int z) const {
|
||||
u8 ChunkLightmap::getSunlight(int x, int y, int z) const {
|
||||
if(x < 0) return m_chunk->getSurroundingChunk(0) ? m_chunk->getSurroundingChunk(0)->lightmap().getSunlight(x + CHUNK_WIDTH, y, z) : 0;
|
||||
if(x >= CHUNK_WIDTH) return m_chunk->getSurroundingChunk(1) ? m_chunk->getSurroundingChunk(1)->lightmap().getSunlight(x - CHUNK_WIDTH, y, z) : 0;
|
||||
if(y < 0) return m_chunk->getSurroundingChunk(4) ? m_chunk->getSurroundingChunk(4)->lightmap().getSunlight(x, y + CHUNK_HEIGHT, z) : 0;
|
||||
@ -162,7 +162,7 @@ int ChunkLightmap::getSunlight(int x, int y, int z) const {
|
||||
return (m_lightMap[x][y][z] >> 4) & 0xf;
|
||||
}
|
||||
|
||||
int ChunkLightmap::getTorchlight(int x, int y, int z) const {
|
||||
u8 ChunkLightmap::getTorchlight(int x, int y, int z) const {
|
||||
if(x < 0) return m_chunk->getSurroundingChunk(0) ? m_chunk->getSurroundingChunk(0)->lightmap().getTorchlight(x + CHUNK_WIDTH, y, z) : 0;
|
||||
if(x >= CHUNK_WIDTH) return m_chunk->getSurroundingChunk(1) ? m_chunk->getSurroundingChunk(1)->lightmap().getTorchlight(x - CHUNK_WIDTH, y, z) : 0;
|
||||
if(y < 0) return m_chunk->getSurroundingChunk(4) ? m_chunk->getSurroundingChunk(4)->lightmap().getTorchlight(x, y + CHUNK_HEIGHT, z) : 0;
|
||||
@ -173,7 +173,33 @@ int ChunkLightmap::getTorchlight(int x, int y, int z) const {
|
||||
return m_lightMap[x][y][z] & 0xf;
|
||||
}
|
||||
|
||||
void ChunkLightmap::setSunlight(int x, int y, int z, int val) {
|
||||
u8 ChunkLightmap::getLightData(int x, int y, int z) const {
|
||||
if(x < 0) return m_chunk->getSurroundingChunk(0) ? m_chunk->getSurroundingChunk(0)->lightmap().getLightData(x + CHUNK_WIDTH, y, z) : 0;
|
||||
if(x >= CHUNK_WIDTH) return m_chunk->getSurroundingChunk(1) ? m_chunk->getSurroundingChunk(1)->lightmap().getLightData(x - CHUNK_WIDTH, y, z) : 0;
|
||||
if(y < 0) return m_chunk->getSurroundingChunk(4) ? m_chunk->getSurroundingChunk(4)->lightmap().getLightData(x, y + CHUNK_HEIGHT, z) : 0;
|
||||
if(y >= CHUNK_HEIGHT) return m_chunk->getSurroundingChunk(5) ? m_chunk->getSurroundingChunk(5)->lightmap().getLightData(x, y - CHUNK_HEIGHT, z) : 0;
|
||||
if(z < 0) return m_chunk->getSurroundingChunk(2) ? m_chunk->getSurroundingChunk(2)->lightmap().getLightData(x, y, z + CHUNK_DEPTH) : 0;
|
||||
if(z >= CHUNK_DEPTH) return m_chunk->getSurroundingChunk(3) ? m_chunk->getSurroundingChunk(3)->lightmap().getLightData(x, y, z - CHUNK_DEPTH) : 0;
|
||||
|
||||
return m_lightMap[x][y][z];
|
||||
}
|
||||
|
||||
void ChunkLightmap::setLightData(int x, int y, int z, u8 val) {
|
||||
if(x < 0) { if(m_chunk->getSurroundingChunk(0)) m_chunk->getSurroundingChunk(0)->lightmap().setLightData(x + CHUNK_WIDTH, y, z, val); return; }
|
||||
if(x >= CHUNK_WIDTH) { if(m_chunk->getSurroundingChunk(1)) m_chunk->getSurroundingChunk(1)->lightmap().setLightData(x - CHUNK_WIDTH, y, z, val); return; }
|
||||
if(y < 0) { if(m_chunk->getSurroundingChunk(4)) m_chunk->getSurroundingChunk(4)->lightmap().setLightData(x, y + CHUNK_HEIGHT, z, val); return; }
|
||||
if(y >= CHUNK_HEIGHT) { if(m_chunk->getSurroundingChunk(5)) m_chunk->getSurroundingChunk(5)->lightmap().setLightData(x, y - CHUNK_HEIGHT, z, val); return; }
|
||||
if(z < 0) { if(m_chunk->getSurroundingChunk(2)) m_chunk->getSurroundingChunk(2)->lightmap().setLightData(x, y, z + CHUNK_DEPTH, val); return; }
|
||||
if(z >= CHUNK_DEPTH) { if(m_chunk->getSurroundingChunk(3)) m_chunk->getSurroundingChunk(3)->lightmap().setLightData(x, y, z - CHUNK_DEPTH, val); return; }
|
||||
|
||||
m_lightMap[x][y][z] = val;
|
||||
|
||||
m_chunk->setChanged(true);
|
||||
|
||||
updateSurroundingChunks(x, y, z);
|
||||
}
|
||||
|
||||
void ChunkLightmap::setSunlight(int x, int y, int z, u8 val) {
|
||||
if(x < 0) { if(m_chunk->getSurroundingChunk(0)) m_chunk->getSurroundingChunk(0)->lightmap().setSunlight(x + CHUNK_WIDTH, y, z, val); return; }
|
||||
if(x >= CHUNK_WIDTH) { if(m_chunk->getSurroundingChunk(1)) m_chunk->getSurroundingChunk(1)->lightmap().setSunlight(x - CHUNK_WIDTH, y, z, val); return; }
|
||||
if(y < 0) { if(m_chunk->getSurroundingChunk(4)) m_chunk->getSurroundingChunk(4)->lightmap().setSunlight(x, y + CHUNK_HEIGHT, z, val); return; }
|
||||
@ -185,15 +211,10 @@ void ChunkLightmap::setSunlight(int x, int y, int z, int val) {
|
||||
|
||||
m_chunk->setChanged(true);
|
||||
|
||||
if(x == 0 && m_chunk->getSurroundingChunk(ClientChunk::Left)) { m_chunk->getSurroundingChunk(ClientChunk::Left)->setChanged(true); }
|
||||
if(x == CHUNK_WIDTH - 1 && m_chunk->getSurroundingChunk(ClientChunk::Right)) { m_chunk->getSurroundingChunk(ClientChunk::Right)->setChanged(true); }
|
||||
if(y == 0 && m_chunk->getSurroundingChunk(ClientChunk::Bottom)) { m_chunk->getSurroundingChunk(ClientChunk::Bottom)->setChanged(true); }
|
||||
if(y == CHUNK_HEIGHT - 1 && m_chunk->getSurroundingChunk(ClientChunk::Top)) { m_chunk->getSurroundingChunk(ClientChunk::Top)->setChanged(true); }
|
||||
if(z == 0 && m_chunk->getSurroundingChunk(ClientChunk::Front)) { m_chunk->getSurroundingChunk(ClientChunk::Front)->setChanged(true); }
|
||||
if(z == CHUNK_DEPTH - 1 && m_chunk->getSurroundingChunk(ClientChunk::Back)) { m_chunk->getSurroundingChunk(ClientChunk::Back)->setChanged(true); }
|
||||
updateSurroundingChunks(x, y, z);
|
||||
};
|
||||
|
||||
void ChunkLightmap::setTorchlight(int x, int y, int z, int val) {
|
||||
void ChunkLightmap::setTorchlight(int x, int y, int z, u8 val) {
|
||||
if(x < 0) { if(m_chunk->getSurroundingChunk(0)) m_chunk->getSurroundingChunk(0)->lightmap().setTorchlight(x + CHUNK_WIDTH, y, z, val); return; }
|
||||
if(x >= CHUNK_WIDTH) { if(m_chunk->getSurroundingChunk(1)) m_chunk->getSurroundingChunk(1)->lightmap().setTorchlight(x - CHUNK_WIDTH, y, z, val); return; }
|
||||
if(y < 0) { if(m_chunk->getSurroundingChunk(4)) m_chunk->getSurroundingChunk(4)->lightmap().setTorchlight(x, y + CHUNK_HEIGHT, z, val); return; }
|
||||
@ -205,11 +226,15 @@ void ChunkLightmap::setTorchlight(int x, int y, int z, int val) {
|
||||
|
||||
m_chunk->setChanged(true);
|
||||
|
||||
if(x == 0 && m_chunk->getSurroundingChunk(ClientChunk::Left)) { m_chunk->getSurroundingChunk(ClientChunk::Left)->setChanged(true); }
|
||||
if(x == CHUNK_WIDTH - 1 && m_chunk->getSurroundingChunk(ClientChunk::Right)) { m_chunk->getSurroundingChunk(ClientChunk::Right)->setChanged(true); }
|
||||
if(y == 0 && m_chunk->getSurroundingChunk(ClientChunk::Bottom)) { m_chunk->getSurroundingChunk(ClientChunk::Bottom)->setChanged(true); }
|
||||
if(y == CHUNK_HEIGHT - 1 && m_chunk->getSurroundingChunk(ClientChunk::Top)) { m_chunk->getSurroundingChunk(ClientChunk::Top)->setChanged(true); }
|
||||
if(z == 0 && m_chunk->getSurroundingChunk(ClientChunk::Front)) { m_chunk->getSurroundingChunk(ClientChunk::Front)->setChanged(true); }
|
||||
if(z == CHUNK_DEPTH - 1 && m_chunk->getSurroundingChunk(ClientChunk::Back)) { m_chunk->getSurroundingChunk(ClientChunk::Back)->setChanged(true); }
|
||||
updateSurroundingChunks(x, y, z);
|
||||
}
|
||||
|
||||
void ChunkLightmap::updateSurroundingChunks(int x, int y, int z) {
|
||||
if(x == 0 && m_chunk->getSurroundingChunk(Chunk::Left)) { m_chunk->getSurroundingChunk(Chunk::Left)->setChanged(true); }
|
||||
if(x == CHUNK_WIDTH - 1 && m_chunk->getSurroundingChunk(Chunk::Right)) { m_chunk->getSurroundingChunk(Chunk::Right)->setChanged(true); }
|
||||
if(y == 0 && m_chunk->getSurroundingChunk(Chunk::Bottom)) { m_chunk->getSurroundingChunk(Chunk::Bottom)->setChanged(true); }
|
||||
if(y == CHUNK_HEIGHT - 1 && m_chunk->getSurroundingChunk(Chunk::Top)) { m_chunk->getSurroundingChunk(Chunk::Top)->setChanged(true); }
|
||||
if(z == 0 && m_chunk->getSurroundingChunk(Chunk::Front)) { m_chunk->getSurroundingChunk(Chunk::Front)->setChanged(true); }
|
||||
if(z == CHUNK_DEPTH - 1 && m_chunk->getSurroundingChunk(Chunk::Back)) { m_chunk->getSurroundingChunk(Chunk::Back)->setChanged(true); }
|
||||
}
|
||||
|
@ -17,54 +17,26 @@
|
||||
#include <gk/core/IntTypes.hpp>
|
||||
|
||||
#include "Config.hpp"
|
||||
#include "Chunk.hpp"
|
||||
#include "TerrainGenerator.hpp"
|
||||
|
||||
class ServerChunk {
|
||||
using DataArray = u32[CHUNK_WIDTH][CHUNK_HEIGHT][CHUNK_DEPTH];
|
||||
|
||||
class ServerChunk : public Chunk {
|
||||
public:
|
||||
enum {
|
||||
Left,
|
||||
Right,
|
||||
Front,
|
||||
Back,
|
||||
Bottom,
|
||||
Top
|
||||
};
|
||||
|
||||
public:
|
||||
ServerChunk(s32 x, s32 y, s32 z);
|
||||
ServerChunk(s32 x, s32 y, s32 z) : Chunk(x, y, z) {}
|
||||
|
||||
void update();
|
||||
void generate();
|
||||
|
||||
u16 getBlock(int x, int y, int z) const;
|
||||
u16 getData(int x, int y, int z) const;
|
||||
void setBlock(int x, int y, int z, u16 type);
|
||||
void setData(int x, int y, int z, u16 data);
|
||||
|
||||
s32 x() const { return m_x; }
|
||||
s32 y() const { return m_y; }
|
||||
s32 z() const { return m_z; }
|
||||
|
||||
ServerChunk *getSurroundingChunk(u8 i) { return (i > 5) ? nullptr : m_surroundingChunks[i]; }
|
||||
const ServerChunk *getSurroundingChunk(u8 i) const { return (i > 5) ? nullptr : m_surroundingChunks[i]; }
|
||||
void setSurroundingChunk(u8 i, ServerChunk *chunk) { if (i < 6) m_surroundingChunks[i] = chunk; }
|
||||
bool isGenerated() const { return m_isGenerated; }
|
||||
void setGenerated(bool isGenerated) { m_isGenerated = isGenerated; }
|
||||
|
||||
const DataArray &data() const { return m_data; }
|
||||
|
||||
private:
|
||||
DataArray m_data;
|
||||
|
||||
s32 m_x;
|
||||
s32 m_y;
|
||||
s32 m_z;
|
||||
|
||||
bool m_hasChanged = false;
|
||||
bool m_isGenerated = false;
|
||||
|
||||
TerrainGenerator m_terrainGenerator;
|
||||
|
||||
ServerChunk *m_surroundingChunks[6]{nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
|
||||
};
|
||||
|
||||
#endif // SERVERCHUNK_HPP_
|
||||
|
@ -15,8 +15,18 @@
|
||||
|
||||
#include "ServerChunk.hpp"
|
||||
|
||||
ServerChunk::ServerChunk(s32 x, s32 y, s32 z) : m_x(x), m_y(y), m_z(z) {
|
||||
std::memset(m_data, 0, sizeof(m_data));
|
||||
void ServerChunk::update() {
|
||||
// FIXME
|
||||
// if (!m_tickingBlocks.empty() && m_lastTick < gk::GameClock::getTicks() / 50) {
|
||||
// m_lastTick = gk::GameClock::getTicks() / 50;
|
||||
//
|
||||
// for (auto &it : m_tickingBlocks) {
|
||||
// int z = it.first / (width * height);
|
||||
// int y = (it.first - z * width * height) / width;
|
||||
// int x = (it.first - z * width * height) % width;
|
||||
// it.second.onTick(glm::ivec3{x + m_x * width, y + m_y * height, z + m_z * depth}, player, *this, world);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
void ServerChunk::generate() {
|
||||
@ -28,50 +38,3 @@ void ServerChunk::generate() {
|
||||
m_hasChanged = true;
|
||||
}
|
||||
|
||||
u16 ServerChunk::getBlock(int x, int y, int z) const {
|
||||
if(x < 0) return m_surroundingChunks[0] ? m_surroundingChunks[0]->getBlock(x + CHUNK_WIDTH, y, z) : 0;
|
||||
if(x >= CHUNK_WIDTH) return m_surroundingChunks[1] ? m_surroundingChunks[1]->getBlock(x - CHUNK_WIDTH, y, z) : 0;
|
||||
if(y < 0) return m_surroundingChunks[4] ? m_surroundingChunks[4]->getBlock(x, y + CHUNK_HEIGHT, z) : 0;
|
||||
if(y >= CHUNK_HEIGHT) return m_surroundingChunks[5] ? m_surroundingChunks[5]->getBlock(x, y - CHUNK_HEIGHT, z) : 0;
|
||||
if(z < 0) return m_surroundingChunks[2] ? m_surroundingChunks[2]->getBlock(x, y, z + CHUNK_DEPTH) : 0;
|
||||
if(z >= CHUNK_DEPTH) return m_surroundingChunks[3] ? m_surroundingChunks[3]->getBlock(x, y, z - CHUNK_DEPTH) : 0;
|
||||
return m_data[x][y][z];
|
||||
}
|
||||
|
||||
u16 ServerChunk::getData(int x, int y, int z) const {
|
||||
if(x < 0) return m_surroundingChunks[0] ? m_surroundingChunks[0]->getData(x + CHUNK_WIDTH, y, z) : 0;
|
||||
if(x >= CHUNK_WIDTH) return m_surroundingChunks[1] ? m_surroundingChunks[1]->getData(x - CHUNK_WIDTH, y, z) : 0;
|
||||
if(y < 0) return m_surroundingChunks[4] ? m_surroundingChunks[4]->getData(x, y + CHUNK_HEIGHT, z) : 0;
|
||||
if(y >= CHUNK_HEIGHT) return m_surroundingChunks[5] ? m_surroundingChunks[5]->getData(x, y - CHUNK_HEIGHT, z) : 0;
|
||||
if(z < 0) return m_surroundingChunks[2] ? m_surroundingChunks[2]->getData(x, y, z + CHUNK_DEPTH) : 0;
|
||||
if(z >= CHUNK_DEPTH) return m_surroundingChunks[3] ? m_surroundingChunks[3]->getData(x, y, z - CHUNK_DEPTH) : 0;
|
||||
return (m_data[x][y][z] >> 16) & 0xffff;
|
||||
}
|
||||
|
||||
void ServerChunk::setBlock(int x, int y, int z, u16 type) {
|
||||
if(x < 0) { if(m_surroundingChunks[0]) m_surroundingChunks[0]->setBlock(x + CHUNK_WIDTH, y, z, type); return; }
|
||||
if(x >= CHUNK_WIDTH) { if(m_surroundingChunks[1]) m_surroundingChunks[1]->setBlock(x - CHUNK_WIDTH, y, z, type); return; }
|
||||
if(y < 0) { if(m_surroundingChunks[4]) m_surroundingChunks[4]->setBlock(x, y + CHUNK_HEIGHT, z, type); return; }
|
||||
if(y >= CHUNK_HEIGHT) { if(m_surroundingChunks[5]) m_surroundingChunks[5]->setBlock(x, y - CHUNK_HEIGHT, z, type); return; }
|
||||
if(z < 0) { if(m_surroundingChunks[2]) m_surroundingChunks[2]->setBlock(x, y, z + CHUNK_DEPTH, type); return; }
|
||||
if(z >= CHUNK_DEPTH) { if(m_surroundingChunks[3]) m_surroundingChunks[3]->setBlock(x, y, z - CHUNK_DEPTH, type); return; }
|
||||
|
||||
m_data[x][y][z] = type;
|
||||
|
||||
m_hasChanged = true;
|
||||
}
|
||||
|
||||
void ServerChunk::setData(int x, int y, int z, u16 data) {
|
||||
if(x < 0) { if(m_surroundingChunks[0]) m_surroundingChunks[0]->setData(x + CHUNK_WIDTH, y, z, data); return; }
|
||||
if(x >= CHUNK_WIDTH) { if(m_surroundingChunks[1]) m_surroundingChunks[1]->setData(x - CHUNK_WIDTH, y, z, data); return; }
|
||||
if(y < 0) { if(m_surroundingChunks[4]) m_surroundingChunks[4]->setData(x, y + CHUNK_HEIGHT, z, data); return; }
|
||||
if(y >= CHUNK_HEIGHT) { if(m_surroundingChunks[5]) m_surroundingChunks[5]->setData(x, y - CHUNK_HEIGHT, z, data); return; }
|
||||
if(z < 0) { if(m_surroundingChunks[2]) m_surroundingChunks[2]->setData(x, y, z + CHUNK_DEPTH, data); return; }
|
||||
if(z >= CHUNK_DEPTH) { if(m_surroundingChunks[3]) m_surroundingChunks[3]->setData(x, y, z - CHUNK_DEPTH, data); return; }
|
||||
|
||||
m_data[x][y][z] &= 0xffff;
|
||||
m_data[x][y][z] |= (data << 16);
|
||||
|
||||
m_hasChanged = true;
|
||||
}
|
||||
|
||||
|
@ -62,6 +62,7 @@ void ServerWorld::sendChunkData(Client &client, ServerChunk *chunk) {
|
||||
for (u16 y = 0 ; y < CHUNK_HEIGHT ; ++y) {
|
||||
for (u16 x = 0 ; x < CHUNK_WIDTH ; ++x) {
|
||||
packet << u16(chunk->data()[x][y][z]);
|
||||
packet << chunk->lightmap().getLightData(x, y, z);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -50,10 +50,12 @@ void TerrainGenerator::testCraftGeneration(ServerChunk &chunk) const {
|
||||
|
||||
// Land blocks
|
||||
for(int y = 0 ; y < CHUNK_HEIGHT ; y++) {
|
||||
// Wood planks layer
|
||||
// if (y == 0 && chunk.y() == 0) {
|
||||
// chunk.setBlock(x, y, z, 16);
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// Are we above "ground" level?
|
||||
if(y + chunk.y() * CHUNK_HEIGHT >= h) {
|
||||
// if we are not yet up to sea level, fill with water blocks
|
||||
@ -68,6 +70,7 @@ void TerrainGenerator::testCraftGeneration(ServerChunk &chunk) const {
|
||||
h = (rand() & 0x3) + 3;
|
||||
for(int i = 0 ; i < h ; i++) {
|
||||
chunk.setBlock(x, y + i, z, BlockType::Wood);
|
||||
chunk.lightmap().addSunlight(x, y + i, z, 15);
|
||||
}
|
||||
|
||||
// Leaves
|
||||
@ -77,9 +80,8 @@ void TerrainGenerator::testCraftGeneration(ServerChunk &chunk) const {
|
||||
if(ix * ix + iy * iy + iz * iz < 8 + (rand() & 1) && !chunk.getBlock(x + ix, y + h + iy, z + iz)) {
|
||||
chunk.setBlock(x + ix, y + h + iy, z + iz, BlockType::Leaves);
|
||||
|
||||
// FIXME
|
||||
// if (iy == 2)
|
||||
// chunk.lightmap().addSunlight(x + ix, y + h + iy, z + iz, 15);
|
||||
chunk.lightmap().addSunlight(x + ix, y + h + iy, z + iz, 15);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -89,12 +91,10 @@ void TerrainGenerator::testCraftGeneration(ServerChunk &chunk) const {
|
||||
chunk.setBlock(x, y, z, BlockType::Flower);
|
||||
}
|
||||
else {
|
||||
// FIXME
|
||||
// chunk.lightmap().addSunlight(x, y - 1, z, 15);
|
||||
chunk.lightmap().addSunlight(x, y - 1, z, 15);
|
||||
|
||||
// FIXME: Temporary fix for air blocks light level
|
||||
// FIXME
|
||||
// chunk.lightmap().addSunlight(x, y, z, 15);
|
||||
chunk.lightmap().addSunlight(x, y, z, 15);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user