2021-09-24 10:15:43 -04:00
|
|
|
#include "chunk.h"
|
2021-11-19 09:05:28 -05:00
|
|
|
#include "world.h"
|
2021-09-24 10:15:43 -04:00
|
|
|
|
2021-11-02 13:25:24 -04:00
|
|
|
Chunk::Chunk(int x, int y) : m_posX(x), m_posY(y) { m_blocks.Reset(BTYPE_AIR); }
|
2021-10-11 11:37:58 -04:00
|
|
|
|
2021-11-30 19:55:11 -05:00
|
|
|
Chunk::Chunk(int x, int y, char data[CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z]) : m_posX(x), m_posY(y) {
|
|
|
|
for (int x = 0; x < CHUNK_SIZE_X; ++x)
|
|
|
|
for (int z = 0; z < CHUNK_SIZE_Z; ++z)
|
|
|
|
for (int y = 0; y < CHUNK_SIZE_Y; ++y)
|
|
|
|
m_blocks.Set(x, y, z, data[x + (z * CHUNK_SIZE_X) + (y * CHUNK_SIZE_Z * CHUNK_SIZE_X)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
Chunk::~Chunk() {
|
|
|
|
if (m_isModified) {
|
|
|
|
char data[CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z];
|
|
|
|
|
|
|
|
for (int x = 0; x < CHUNK_SIZE_X; ++x)
|
|
|
|
for (int z = 0; z < CHUNK_SIZE_Z; ++z)
|
|
|
|
for (int y = 0; y < CHUNK_SIZE_Y; ++y)
|
|
|
|
data[x + (z * CHUNK_SIZE_X) + (y * CHUNK_SIZE_Z * CHUNK_SIZE_X)] = (char)GetBlock(x, y, z);
|
|
|
|
|
|
|
|
std::ostringstream pos;
|
|
|
|
pos << CHUNK_PATH << m_posX << '_' << m_posY << ".chunk";
|
|
|
|
|
|
|
|
|
|
|
|
std::ofstream output(pos.str().c_str(), std::fstream::binary);
|
|
|
|
output.write(data, sizeof(data));
|
|
|
|
output.close();
|
|
|
|
}
|
|
|
|
}
|
2021-09-24 10:15:43 -04:00
|
|
|
|
2021-11-19 11:11:08 -05:00
|
|
|
void Chunk::RemoveBlock(int x, int y, int z, World* world) {
|
2021-10-11 11:37:58 -04:00
|
|
|
m_blocks.Set(x, y, z, BTYPE_AIR);
|
2021-11-19 11:11:08 -05:00
|
|
|
|
|
|
|
CheckNeighbors(x, y, world);
|
|
|
|
|
2021-10-11 11:37:58 -04:00
|
|
|
m_isDirty = true;
|
2021-09-24 10:15:43 -04:00
|
|
|
}
|
|
|
|
|
2021-11-19 11:11:08 -05:00
|
|
|
void Chunk::SetBlock(int x, int y, int z, BlockType type, World* world) {
|
2021-10-11 11:37:58 -04:00
|
|
|
m_blocks.Set(x, y, z, type);
|
2021-11-19 11:11:08 -05:00
|
|
|
|
2021-11-26 11:59:02 -05:00
|
|
|
CheckNeighbors(x, z, world);
|
2021-11-19 11:11:08 -05:00
|
|
|
|
2021-10-11 11:37:58 -04:00
|
|
|
m_isDirty = true;
|
2021-09-24 10:15:43 -04:00
|
|
|
}
|
|
|
|
|
2021-10-11 11:37:58 -04:00
|
|
|
BlockType Chunk::GetBlock(int x, int y, int z) { return m_blocks.Get(x, y, z); }
|
|
|
|
|
2021-11-19 11:11:08 -05:00
|
|
|
void Chunk::CheckNeighbors(int x, int z, World* world) {
|
2021-11-26 11:59:02 -05:00
|
|
|
if (x == 0 && m_posX >= 0 &&
|
|
|
|
world->ChunkAt((m_posX - 1) * CHUNK_SIZE_X, 1, m_posY * CHUNK_SIZE_Z))
|
|
|
|
world->ChunkAt((m_posX - 1) * CHUNK_SIZE_X, 1, m_posY * CHUNK_SIZE_Z)->MakeDirty();
|
|
|
|
else if (x == CHUNK_SIZE_X - 1 && m_posX < WORLD_SIZE_X &&
|
2021-11-26 12:00:05 -05:00
|
|
|
world->ChunkAt((m_posX + 1) * CHUNK_SIZE_X, 1, m_posY * CHUNK_SIZE_Z))
|
|
|
|
world->ChunkAt((m_posX + 1) * CHUNK_SIZE_X, 1, m_posY * CHUNK_SIZE_Z)->MakeDirty();
|
2021-11-26 11:59:02 -05:00
|
|
|
|
|
|
|
if (z == 0 && m_posY >= 0 &&
|
|
|
|
world->ChunkAt(m_posX * CHUNK_SIZE_X, 1, (m_posY - 1) * CHUNK_SIZE_Z))
|
2021-11-19 11:11:08 -05:00
|
|
|
world->ChunkAt(m_posX * CHUNK_SIZE_X, 1, (m_posY - 1) * CHUNK_SIZE_Z)->MakeDirty();
|
2021-11-26 11:59:02 -05:00
|
|
|
else if (z == CHUNK_SIZE_X - 1 && m_posY < WORLD_SIZE_Y &&
|
2021-11-26 12:00:05 -05:00
|
|
|
world->ChunkAt(m_posX * CHUNK_SIZE_X, 1, (m_posY + 1) * CHUNK_SIZE_Z))
|
|
|
|
world->ChunkAt(m_posX * CHUNK_SIZE_X, 1, (m_posY + 1) * CHUNK_SIZE_Z)->MakeDirty();
|
2021-11-19 11:11:08 -05:00
|
|
|
}
|
|
|
|
|
2021-11-30 19:55:11 -05:00
|
|
|
void Chunk::GetPosition(int& x, int& y) const {
|
|
|
|
x = m_posX;
|
|
|
|
y = m_posY;
|
|
|
|
}
|
2021-11-26 11:59:02 -05:00
|
|
|
|
2021-11-18 19:27:16 -05:00
|
|
|
void Chunk::Update(BlockInfo* blockinfo[BTYPE_LAST], World* world) {
|
2021-10-31 00:31:08 -04:00
|
|
|
float u, v, s;
|
2021-10-11 11:37:58 -04:00
|
|
|
// Update mesh
|
|
|
|
if (m_isDirty) {
|
|
|
|
int maxVertexCount = (CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z) * (6 * 4);
|
|
|
|
VertexBuffer::VertexData* vd = new VertexBuffer::VertexData[maxVertexCount];
|
|
|
|
int count = 0;
|
|
|
|
for (int x = 0; x < CHUNK_SIZE_X; ++x) {
|
|
|
|
for (int z = 0; z < CHUNK_SIZE_Z; ++z) {
|
|
|
|
for (int y = 0; y < CHUNK_SIZE_Y; ++y) {
|
|
|
|
if (count > USHRT_MAX)
|
|
|
|
break;
|
2021-11-18 19:27:16 -05:00
|
|
|
|
2021-10-11 11:37:58 -04:00
|
|
|
BlockType bt = GetBlock(x, y, z);
|
2021-11-18 19:27:16 -05:00
|
|
|
|
2021-10-11 11:37:58 -04:00
|
|
|
if (bt != BTYPE_AIR) {
|
2021-10-31 00:31:08 -04:00
|
|
|
blockinfo[bt]->GetTexture(u, v, s);
|
2021-11-18 19:27:16 -05:00
|
|
|
AddBlockToMesh(vd, count, bt, x, y, z, u, v, s, world);
|
2021-10-11 11:37:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (count > USHRT_MAX) {
|
|
|
|
count = USHRT_MAX;
|
|
|
|
std::cout << "[ Chunk :: Update ] Chunk data truncaned , too much vertices to have a 16 bit index " << std::endl;
|
|
|
|
}
|
|
|
|
m_vertexBuffer.SetMeshData(vd, count);
|
|
|
|
delete[] vd;
|
|
|
|
}
|
|
|
|
m_isDirty = false;
|
2021-09-24 10:15:43 -04:00
|
|
|
}
|
|
|
|
|
2021-11-01 12:36:53 -04:00
|
|
|
void Chunk::AddBlockToMesh(VertexBuffer::VertexData* vd, int& count, BlockType bt,
|
2021-11-18 19:27:16 -05:00
|
|
|
int x, int y, int z, float u, float v, float s, World* world) {
|
2021-10-11 11:37:58 -04:00
|
|
|
|
2021-11-18 19:27:16 -05:00
|
|
|
int cx = x + m_posX * CHUNK_SIZE_X, cy = z + m_posY * CHUNK_SIZE_Z;
|
2021-10-11 11:37:58 -04:00
|
|
|
|
2021-11-01 12:36:53 -04:00
|
|
|
if (y == CHUNK_SIZE_Y - 1 || GetBlock(x, y + 1, z) == BTYPE_AIR) { // y
|
2021-11-27 13:35:39 -05:00
|
|
|
vd[count++] = VertexBuffer::VertexData(x, y + 1.f, z, .8f, .8f, .8f, u, v);
|
|
|
|
vd[count++] = VertexBuffer::VertexData(x, y + 1.f, z + 1.f, .8f, .8f, .8f, u, v + s);
|
|
|
|
vd[count++] = VertexBuffer::VertexData(x + 1.f, y + 1.f, z + 1.f, .8f, .8f, .8f, u + s, v + s);
|
|
|
|
vd[count++] = VertexBuffer::VertexData(x + 1.f, y + 1.f, z, .8f, .8f, .8f, u + s, v);
|
2021-10-31 00:31:08 -04:00
|
|
|
}
|
2021-11-15 20:58:13 -05:00
|
|
|
|
2021-11-01 12:36:53 -04:00
|
|
|
if (y == 0 || GetBlock(x, y - 1, z) == BTYPE_AIR) { // -y
|
2021-11-30 19:55:11 -05:00
|
|
|
vd[count++] = VertexBuffer::VertexData(x, y, z + 1.f, .2f, .2f, .2f, u, v);
|
|
|
|
vd[count++] = VertexBuffer::VertexData(x, y, z, .2f, .2f, .2f, u, v + s);
|
|
|
|
vd[count++] = VertexBuffer::VertexData(x + 1.f, y, z, .2f, .2f, .2f, u + s, v + s);
|
|
|
|
vd[count++] = VertexBuffer::VertexData(x + 1.f, y, z + 1.f, .2f, .2f, .2f, u + s, v);
|
2021-10-31 00:31:08 -04:00
|
|
|
}
|
2021-10-11 11:37:58 -04:00
|
|
|
|
2021-11-26 11:59:02 -05:00
|
|
|
if (world->BlockAt(cx + 1, y, cy) == BTYPE_AIR) { // x
|
2021-11-27 13:35:39 -05:00
|
|
|
vd[count++] = VertexBuffer::VertexData(x + 1.f, y, z, .9f, .9f, .9f, u, v);
|
|
|
|
vd[count++] = VertexBuffer::VertexData(x + 1.f, y + 1.f, z, .9f, .9f, .9f, u, v + s);
|
|
|
|
vd[count++] = VertexBuffer::VertexData(x + 1.f, y + 1.f, z + 1.f, .9f, .9f, .9f, u + s, v + s);
|
|
|
|
vd[count++] = VertexBuffer::VertexData(x + 1.f, y, z + 1.f, .9f, .9f, .9f, u + s, v);
|
2021-11-18 19:27:16 -05:00
|
|
|
}
|
|
|
|
|
2021-11-26 11:59:02 -05:00
|
|
|
if (world->BlockAt(cx - 1, y, cy) == BTYPE_AIR) { // -x
|
2021-11-30 19:55:11 -05:00
|
|
|
vd[count++] = VertexBuffer::VertexData(x, y + 1.f, z + 1.f, .5f, .5f, .5f, u, v + s);
|
|
|
|
vd[count++] = VertexBuffer::VertexData(x, y + 1.f, z, .5f, .5f, .5f, u + s, v + s);
|
|
|
|
vd[count++] = VertexBuffer::VertexData(x, y, z, .5f, .5f, .5f, u + s, v);
|
|
|
|
vd[count++] = VertexBuffer::VertexData(x, y, z + 1.f, .5f, .5f, .5f, u, v);
|
2021-11-18 19:27:16 -05:00
|
|
|
}
|
|
|
|
|
2021-11-26 11:59:02 -05:00
|
|
|
if (world->BlockAt(cx, y, cy + 1) == BTYPE_AIR) { // z
|
2021-11-30 19:55:11 -05:00
|
|
|
vd[count++] = VertexBuffer::VertexData(x, y, z + 1.f, .4f, .4f, .4f, u, v);
|
|
|
|
vd[count++] = VertexBuffer::VertexData(x + 1.f, y, z + 1.f, .4f, .4f, .4f, u + s, v);
|
|
|
|
vd[count++] = VertexBuffer::VertexData(x + 1.f, y + 1.f, z + 1.f, .4f, .4f, .4f, u + s, v + s);
|
|
|
|
vd[count++] = VertexBuffer::VertexData(x, y + 1.f, z + 1.f, .4f, .4f, .4f, u, v + s);
|
2021-10-31 00:31:08 -04:00
|
|
|
}
|
2021-10-11 11:37:58 -04:00
|
|
|
|
2021-11-26 11:59:02 -05:00
|
|
|
if (world->BlockAt(cx, y, cy - 1) == BTYPE_AIR) { // -z
|
2021-11-27 13:35:39 -05:00
|
|
|
vd[count++] = VertexBuffer::VertexData(x, y + 1.f, z, 1.f, 1.f, 1.f, u, v + s);
|
|
|
|
vd[count++] = VertexBuffer::VertexData(x + 1.f, y + 1.f, z, 1.f, 1.f, 1.f, u + s, v + s);
|
|
|
|
vd[count++] = VertexBuffer::VertexData(x + 1.f, y, z , 1.f, 1.f, 1.f, u + s, v);
|
|
|
|
vd[count++] = VertexBuffer::VertexData(x, y, z , 1.f, 1.f, 1.f, u, v);
|
2021-10-31 00:31:08 -04:00
|
|
|
}
|
2021-09-24 10:15:43 -04:00
|
|
|
}
|
|
|
|
|
2021-10-11 11:37:58 -04:00
|
|
|
void Chunk::Render() const { m_vertexBuffer.Render(); }
|
|
|
|
|
|
|
|
bool Chunk::IsDirty() const { return m_isDirty; }
|
|
|
|
|
2021-11-19 11:11:08 -05:00
|
|
|
void Chunk::MakeDirty() { m_isDirty = true; }
|
|
|
|
|
2021-11-26 11:59:02 -05:00
|
|
|
void Chunk::MakeModified() { m_isModified = true; }
|
|
|
|
|