Introduced Content, ContentIndices, no more integer ids hardcoded, common refactor

This commit is contained in:
MihailRis
2023-11-21 11:38:59 +03:00
parent f2f9343737
commit 3b03464d27
35 changed files with 472 additions and 279 deletions
+5 -2
View File
@@ -2,12 +2,14 @@
#include <assert.h>
#include "LightSolver.h"
#include "Lightmap.h"
#include "../content/Content.h"
#include "../voxels/Chunks.h"
#include "../voxels/Chunk.h"
#include "../voxels/voxel.h"
#include "../voxels/Block.h"
LightSolver::LightSolver(Chunks* chunks, int channel) : chunks(chunks), channel(channel) {
LightSolver::LightSolver(const ContentIndices* contentIds, Chunks* chunks, int channel)
: contentIds(contentIds), chunks(chunks), channel(channel) {
}
void LightSolver::add(int x, int y, int z, int emission) {
@@ -93,6 +95,7 @@ void LightSolver::solve(){
}
}
const Block* const* blockDefs = contentIds->getBlockDefs();
while (!addqueue.empty()){
const lightentry entry = addqueue.front();
addqueue.pop();
@@ -108,7 +111,7 @@ void LightSolver::solve(){
if (chunk) {
int light = chunks->getLight(x,y,z, channel);
voxel* v = chunks->get(x,y,z);
Block* block = Block::blocks[v->id];
const Block* block = blockDefs[v->id];
if (block->lightPassing && light+2 <= entry.light){
chunk->lightmap->set(x-chunk->x*CHUNK_W, y, z-chunk->z*CHUNK_D, channel, entry.light-1);
chunk->setModified(true);
+3 -1
View File
@@ -4,6 +4,7 @@
#include <queue>
class Chunks;
class ContentIndices;
struct lightentry {
int x;
@@ -15,10 +16,11 @@ struct lightentry {
class LightSolver {
std::queue<lightentry> addqueue;
std::queue<lightentry> remqueue;
const ContentIndices* const contentIds;
Chunks* chunks;
int channel;
public:
LightSolver(Chunks* chunks, int channel);
LightSolver(const ContentIndices* contentIds, Chunks* chunks, int channel);
void add(int x, int y, int z);
void add(int x, int y, int z, int emission);
+20 -12
View File
@@ -1,22 +1,25 @@
#include "Lighting.h"
#include "LightSolver.h"
#include "Lightmap.h"
#include "../content/Content.h"
#include "../voxels/Chunks.h"
#include "../voxels/Chunk.h"
#include "../voxels/voxel.h"
#include "../voxels/Block.h"
#include "../constants.h"
#include <memory>
#include <iostream>
using std::shared_ptr;
Lighting::Lighting(Chunks* chunks){
this->chunks = chunks;
solverR = new LightSolver(chunks, 0);
solverG = new LightSolver(chunks, 1);
solverB = new LightSolver(chunks, 2);
solverS = new LightSolver(chunks, 3);
Lighting::Lighting(const Content* content, Chunks* chunks)
: content(content), chunks(chunks) {
const ContentIndices* contentIds = content->indices;
solverR = new LightSolver(contentIds, chunks, 0);
solverG = new LightSolver(contentIds, chunks, 1);
solverB = new LightSolver(contentIds, chunks, 2);
solverS = new LightSolver(contentIds, chunks, 3);
}
Lighting::~Lighting(){
@@ -39,6 +42,8 @@ void Lighting::clear(){
}
void Lighting::prebuildSkyLight(int cx, int cz){
const Block* const* blockDefs = content->indices->getBlockDefs();
Chunk* chunk = chunks->getChunk(cx, cz);
int highestPoint = 0;
for (int z = 0; z < CHUNK_D; z++){
@@ -47,7 +52,7 @@ void Lighting::prebuildSkyLight(int cx, int cz){
if (y < 0)
break;
voxel* vox = &(chunk->voxels[(y * CHUNK_D + z) * CHUNK_W + x]);
Block* block = Block::blocks[vox->id];
const Block* block = blockDefs[vox->id];
if (!block->skyLightPassing) {
if (highestPoint < y)
highestPoint = y;
@@ -63,13 +68,15 @@ void Lighting::prebuildSkyLight(int cx, int cz){
}
void Lighting::buildSkyLight(int cx, int cz){
const Block* const* blockDefs = content->indices->getBlockDefs();
Chunk* chunk = chunks->getChunk(cx, cz);
for (int z = 0; z < CHUNK_D; z++){
for (int x = 0; x < CHUNK_W; x++){
for (int y = chunk->lightmap->highestPoint; y >= 0; y--){
int gx = x + cx * CHUNK_W;
int gz = z + cz * CHUNK_D;
while (y > 0 && !Block::blocks[chunk->voxels[(y * CHUNK_D + z) * CHUNK_W + x].id]->lightPassing) {
while (y > 0 && !blockDefs[chunk->voxels[vox_index(x, y, z)].id]->lightPassing) {
y--;
}
if (chunk->lightmap->getS(x, y, z) != 15) {
@@ -88,13 +95,14 @@ void Lighting::buildSkyLight(int cx, int cz){
}
void Lighting::onChunkLoaded(int cx, int cz){
const Block* const* blockDefs = content->indices->getBlockDefs();
const Chunk* chunk = chunks->getChunk(cx, cz);
for (unsigned int y = 0; y < CHUNK_H; y++){
for (unsigned int z = 0; z < CHUNK_D; z++){
for (unsigned int x = 0; x < CHUNK_W; x++){
voxel vox = chunk->voxels[(y * CHUNK_D + z) * CHUNK_W + x];
Block* block = Block::blocks[vox.id];
const Block* block = blockDefs[vox.id];
int gx = x + cx * CHUNK_W;
int gz = z + cz * CHUNK_D;
if (block->emission[0] || block->emission[1] || block->emission[2]){
@@ -128,8 +136,8 @@ void Lighting::onChunkLoaded(int cx, int cz){
solverS->solve();
}
void Lighting::onBlockSet(int x, int y, int z, int id){
Block* block = Block::blocks[id];
void Lighting::onBlockSet(int x, int y, int z, int const id){
Block* block = content->indices->getBlockDef(id);
if (id == 0){
solverR->remove(x,y,z);
solverG->remove(x,y,z);
@@ -140,7 +148,7 @@ void Lighting::onBlockSet(int x, int y, int z, int id){
if (chunks->getLight(x,y+1,z, 3) == 0xF){
for (int i = y; i >= 0; i--){
voxel* vox = chunks->get(x,i,z);
if ((vox == nullptr || vox->id != 0) && Block::blocks[id]->skyLightPassing)
if ((vox == nullptr || vox->id != 0) && block->skyLightPassing)
break;
solverS->add(x,i,z, 0xF);
}
+8 -6
View File
@@ -1,17 +1,19 @@
#ifndef LIGHTING_LIGHTING_H_
#define LIGHTING_LIGHTING_H_
class Content;
class Chunks;
class LightSolver;
class Lighting {
Chunks* chunks = nullptr;
LightSolver* solverR = nullptr;
LightSolver* solverG = nullptr;
LightSolver* solverB = nullptr;
LightSolver* solverS = nullptr;
const Content* const content;
Chunks* chunks;
LightSolver* solverR;
LightSolver* solverG;
LightSolver* solverB;
LightSolver* solverS;
public:
Lighting(Chunks* chunks);
Lighting(const Content* content, Chunks* chunks);
~Lighting();
void clear();