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
+1 -3
View File
@@ -1,8 +1,6 @@
#include "Block.h"
Block* Block::blocks[256];
Block::Block(unsigned int id, int texture) : id(id),
Block::Block(std::string name, int texture) : name(name),
textureFaces{texture,texture,texture,texture,texture,texture},
emission{0,0,0}{
}
+5 -4
View File
@@ -1,6 +1,8 @@
#ifndef VOXELS_BLOCK_H_
#define VOXELS_BLOCK_H_
#include <string>
#define FACE_MX 0
#define FACE_PX 1
#define FACE_MY 2
@@ -14,9 +16,8 @@ enum class BlockModel {
class Block {
public:
static Block* blocks[256];
const unsigned int id;
std::string const name;
unsigned int id;
// 0 1 2 3 4 5
int textureFaces[6]; // -x,x, -y,y, -z,z
unsigned char emission[3];
@@ -30,7 +31,7 @@ public:
bool rotatable = false;
float hitboxScale = 1;
Block(unsigned int id, int texture);
Block(std::string name, int texture);
};
#endif /* VOXELS_BLOCK_H_ */
+13 -4
View File
@@ -3,6 +3,7 @@
#include "voxel.h"
#include "Block.h"
#include "WorldGenerator.h"
#include "../content/Content.h"
#include "../lighting/Lightmap.h"
#include "../files/WorldFiles.h"
#include "../world/LevelEvents.h"
@@ -16,8 +17,16 @@
using glm::vec3;
using std::shared_ptr;
Chunks::Chunks(int w, int d, int ox, int oz, WorldFiles* wfile, LevelEvents* events)
: w(w), d(d), ox(ox), oz(oz), worldFiles(wfile), events(events) {
Chunks::Chunks(int w, int d,
int ox, int oz,
WorldFiles* wfile,
LevelEvents* events,
const Content* content)
: content(content),
contentIds(content->indices),
w(w), d(d), ox(ox), oz(oz),
worldFiles(wfile),
events(events) {
volume = (size_t)w*(size_t)d;
chunks = new shared_ptr<Chunk>[volume];
chunksSecond = new shared_ptr<Chunk>[volume];
@@ -59,7 +68,7 @@ bool Chunks::isObstacle(int x, int y, int z){
voxel* v = get(x,y,z);
if (v == nullptr)
return true; // void - is obstacle
return Block::blocks[v->id]->obstacle;
return contentIds->getBlockDef(v->id)->obstacle;
}
ubyte Chunks::getLight(int x, int y, int z, int channel){
@@ -191,7 +200,7 @@ voxel* Chunks::rayCast(vec3 start,
while (t <= maxDist){
voxel* voxel = get(ix, iy, iz);
if (voxel == nullptr || Block::blocks[voxel->id]->selectable){
if (voxel == nullptr || contentIds->getBlockDef(voxel->id)->selectable){
end.x = px + t * dx;
end.y = py + t * dy;
end.z = pz + t * dz;
+5 -1
View File
@@ -8,6 +8,8 @@
class VoxelRenderer;
class Content;
class ContentIndices;
class Chunk;
class voxel;
class WorldFiles;
@@ -15,6 +17,8 @@ class LevelEvents;
/* Player-centred chunks matrix */
class Chunks {
const Content* const content;
const ContentIndices* const contentIds;
public:
std::shared_ptr<Chunk>* chunks;
std::shared_ptr<Chunk>* chunksSecond;
@@ -27,7 +31,7 @@ public:
LevelEvents* events;
Chunks(int w, int d, int ox, int oz,
WorldFiles* worldFiles, LevelEvents* events);
WorldFiles* worldFiles, LevelEvents* events, const Content* content);
~Chunks();
bool putChunk(std::shared_ptr<Chunk> chunk);
+11 -3
View File
@@ -4,6 +4,7 @@
#include "Chunks.h"
#include "ChunksStorage.h"
#include "WorldGenerator.h"
#include "../content/Content.h"
#include "../graphics/Mesh.h"
#include "../lighting/Lighting.h"
#include "../files/WorldFiles.h"
@@ -26,10 +27,15 @@ using std::chrono::microseconds;
ChunksController::ChunksController(Level* level, Chunks* chunks, Lighting* lighting, uint padding)
: level(level), chunks(chunks), lighting(lighting), padding(padding) {
: level(level),
chunks(chunks),
lighting(lighting),
padding(padding),
generator(new WorldGenerator(level->content)) {
}
ChunksController::~ChunksController(){
delete generator;
}
void ChunksController::update(int64_t maxDuration) {
@@ -50,6 +56,7 @@ void ChunksController::update(int64_t maxDuration) {
}
bool ChunksController::loadVisible(){
const Content* content = level->content;
const int w = chunks->w;
const int d = chunks->d;
const int ox = chunks->ox;
@@ -99,15 +106,16 @@ bool ChunksController::loadVisible(){
chunks->putChunk(chunk);
if (!chunk->isLoaded()) {
WorldGenerator::generate(chunk->voxels, chunk->x, chunk->z, level->world->seed);
generator->generate(chunk->voxels, chunk->x, chunk->z, level->world->seed);
chunk->setUnsaved(true);
}
chunk->updateHeights();
ContentIndices* indices = content->indices;
for (size_t i = 0; i < CHUNK_VOL; i++) {
blockid_t id = chunk->voxels[i].id;
if (Block::blocks[id] == nullptr) {
if (indices->getBlockDef(id) == nullptr) {
std::cout << "corruped block detected at " << i << " of chunk ";
std::cout << chunk->x << "x" << chunk->z;
std::cout << " -> " << (int)id << std::endl;
+2
View File
@@ -9,6 +9,7 @@ class Lighting;
class WorldFiles;
class VoxelRenderer;
class ChunksLoader;
class WorldGenerator;
class ChunksController {
private:
@@ -17,6 +18,7 @@ private:
Lighting* lighting;
int64_t avgDurationMcs = 1000;
uint padding;
WorldGenerator* generator;
public:
ChunksController(Level* level, Chunks* chunks, Lighting* lighting, uint padding);
~ChunksController();
+25 -10
View File
@@ -1,6 +1,7 @@
#include "WorldGenerator.h"
#include "voxel.h"
#include "Chunk.h"
#include "Block.h"
#include <iostream>
#include <time.h>
@@ -11,8 +12,9 @@
#define FNL_IMPL
#include "../maths/FastNoiseLite.h"
#include "../definitions.h"
#include "../content/Content.h"
#include "../maths/voxmaths.h"
#include "../core_defs.h"
#define SEA_LEVEL 55
@@ -94,6 +96,19 @@ float calc_height(fnl_state *noise, int real_x, int real_z){
return height;
}
WorldGenerator::WorldGenerator(const Content* content)
: idStone(content->require("base:stone")->id),
idDirt(content->require("base:dirt")->id),
idGrassBlock(content->require("base:grass_block")->id),
idSand(content->require("base:sand")->id),
idWater(content->require("base:water")->id),
idWood(content->require("base:wood")->id),
idLeaves(content->require("base:leaves")->id),
idGrass(content->require("base:grass")->id),
idFlower(content->require("base:flower")->id),
idBedrock(content->require("base:bedrock")->id) {;
}
int generate_tree(fnl_state *noise,
PseudoRandom* random,
Map2D& heights,
@@ -179,14 +194,14 @@ void WorldGenerator::generate(voxel* voxels, int cx, int cz, int seed){
for (int y = 0; y < CHUNK_H; y++){
int real_y = y;
int id = real_y < SEA_LEVEL ? BLOCK_WATER : BLOCK_AIR;
int id = real_y < SEA_LEVEL ? idWater : BLOCK_AIR;
int states = 0;
if ((real_y == (int)height) && (SEA_LEVEL-2 < real_y)) {
id = BLOCK_GRASS_BLOCK;
id = idGrassBlock;
} else if (real_y < (height - 6)){
id = BLOCK_STONE;
id = idStone;
} else if (real_y < height){
id = BLOCK_DIRT;
id = idDirt;
} else {
int tree = generate_tree(&noise, &randomtree, heights, humidity, real_x, real_y, real_z, treesTile);
if (tree) {
@@ -195,20 +210,20 @@ void WorldGenerator::generate(voxel* voxels, int cx, int cz, int seed){
}
}
if (((height - (1.5 - 0.2 * pow(height - 54, 4))) < real_y) && (real_y < height) && humidity.get(real_x, real_z) < 0.1){
id = BLOCK_SAND;
id = idSand;
}
if (real_y <= 2)
id = BLOCK_BEDROCK;
id = idBedrock;
randomgrass.setSeed(real_x,real_z);
if ((id == 0) && (height > SEA_LEVEL+0.5) && ((int)(height + 1) == real_y) && ((unsigned short)randomgrass.rand() > 56000)){
id = BLOCK_GRASS;
id = idGrass;
}
if ((id == 0) && (height > SEA_LEVEL+0.5) && ((int)(height + 1) == real_y) && ((unsigned short)randomgrass.rand() > 65000)){
id = BLOCK_FLOWER;
id = idFlower;
}
if ((height > SEA_LEVEL+1) && ((int)(height + 1) == real_y) && ((unsigned short)randomgrass.rand() > 65533)){
id = BLOCK_WOOD;
id = idWood;
states = BLOCK_DIR_Y;
}
voxels[(y * CHUNK_D + z) * CHUNK_W + x].id = id;
+15 -1
View File
@@ -1,11 +1,25 @@
#ifndef VOXELS_WORLDGENERATOR_H_
#define VOXELS_WORLDGENERATOR_H_
#include "../typedefs.h"
class voxel;
class Content;
class WorldGenerator {
blockid_t const idStone;
blockid_t const idDirt;
blockid_t const idGrassBlock;
blockid_t const idSand;
blockid_t const idWater;
blockid_t const idWood;
blockid_t const idLeaves;
blockid_t const idGrass;
blockid_t const idFlower;
blockid_t const idBedrock;
public:
static void generate(voxel* voxels, int x, int z, int seed);
WorldGenerator(const Content* content);
void generate(voxel* voxels, int x, int z, int seed);
};
#endif /* VOXELS_WORLDGENERATOR_H_ */