Clean up BlockChunk
parent
e60f197c7e
commit
7cdd9bc270
|
@ -22,9 +22,9 @@ set(ZEPHA_SRC
|
|||
def/item/ShaderMod.h
|
||||
def/item/BlockModelVertex.h
|
||||
game/scene/world/LocalWorld.cpp
|
||||
game/scene/world/LocalWorld.h
|
||||
world/chunk/BlockChunk.cpp
|
||||
world/chunk/BlockChunk.h
|
||||
game/scene/world/LocalWorld.h
|
||||
world/chunk/Chunk.cpp
|
||||
world/chunk/Chunk.h
|
||||
game/scene/world/graph/MeshChunk.cpp
|
||||
game/scene/world/graph/MeshChunk.h
|
||||
game/scene/GameScene.cpp
|
||||
|
@ -91,8 +91,8 @@ set(ZEPHA_SRC
|
|||
game/scene/world/MeshGenStream.h
|
||||
util/Vec.h
|
||||
world/LocalDimension.cpp
|
||||
world/LocalDimension.h
|
||||
world/region/Region.h
|
||||
world/LocalDimension.h
|
||||
world/chunk/Region.h
|
||||
game/entity/engine/WireframeEntity.cpp
|
||||
game/entity/engine/WireframeEntity.h
|
||||
def/item/SelectionBox.h
|
||||
|
@ -105,12 +105,12 @@ set(ZEPHA_SRC
|
|||
game/entity/engine/BlockCrackEntity.h
|
||||
def/texture/TextureAtlas.cpp
|
||||
def/texture/TextureAtlas.h
|
||||
def/texture/AtlasRef.h
|
||||
world/region/MapBlock.h
|
||||
def/texture/AtlasRef.h
|
||||
world/chunk/MapBlock.h
|
||||
def/ClientGame.cpp
|
||||
def/ClientGame.h
|
||||
world/region/Region.cpp
|
||||
world/region/MapBlock.cpp
|
||||
def/ClientGame.h
|
||||
world/chunk/Region.cpp
|
||||
world/chunk/MapBlock.cpp
|
||||
util/Util.h
|
||||
world/block/PointedThing.h
|
||||
game/hud/components/compound/GuiLabelledGraph.cpp
|
||||
|
|
|
@ -39,7 +39,7 @@ MapGen::chunk_partials_map MapGen::generateMapBlock(glm::ivec3 mbPos) {
|
|||
}
|
||||
|
||||
void MapGen::generateChunk(chunk_partials_map& chunks, glm::ivec3 worldPos) {
|
||||
if (chunks.count(worldPos) == 0) chunks.insert(std::pair<glm::ivec3, chunk_partial>{worldPos, {new MapGenJob(), new BlockChunk()}});
|
||||
if (chunks.count(worldPos) == 0) chunks.insert(std::pair<glm::ivec3, chunk_partial>{worldPos, {new MapGenJob(), new Chunk()}});
|
||||
auto& chunk = chunks.at(worldPos);
|
||||
chunk.second->pos = worldPos;
|
||||
|
||||
|
@ -49,7 +49,7 @@ void MapGen::generateChunk(chunk_partials_map& chunks, glm::ivec3 worldPos) {
|
|||
generateBlocks(chunk);
|
||||
generateStructures(chunks, chunk);
|
||||
|
||||
chunk.second->calcNonAirBlocks();
|
||||
chunk.second->recalculateRenderableBlocks();
|
||||
chunk.second->generated = true;
|
||||
}
|
||||
|
||||
|
@ -125,8 +125,8 @@ void MapGen::buildElevationMap(chunk_partials_map& chunks, chunk_partial& chunk)
|
|||
}
|
||||
|
||||
void MapGen::generateBlocks(chunk_partial& chunk) {
|
||||
std::unique_ptr<BlockChunk> dupe = nullptr;
|
||||
if (chunk.second->partial) dupe = std::make_unique<BlockChunk>(*chunk.second);
|
||||
std::unique_ptr<Chunk> dupe = nullptr;
|
||||
if (chunk.second->partial) dupe = std::make_unique<Chunk>(*chunk.second);
|
||||
|
||||
chunk.second->blocks = {};
|
||||
chunk.second->biomes = {};
|
||||
|
@ -218,7 +218,7 @@ void MapGen::generateSunlight(MapGen::chunk_partials_map &chunks, glm::ivec3 mbP
|
|||
for (c.x = 0; c.x < 4; c.x++) {
|
||||
for (c.z = 0; c.z < 4; c.z++) {
|
||||
c.y = 3;
|
||||
BlockChunk* chunk = chunks[mbPos * 4 + c].second;
|
||||
Chunk* chunk = chunks[mbPos * 4 + c].second;
|
||||
|
||||
glm::ivec3 b {};
|
||||
for (b.x = 0; b.x < 16; b.x++) {
|
||||
|
@ -228,7 +228,7 @@ void MapGen::generateSunlight(MapGen::chunk_partials_map &chunks, glm::ivec3 mbP
|
|||
while (true) {
|
||||
unsigned int ind = Space::Block::index(b);
|
||||
if (defs.blockFromId(chunk->getBlock(ind)).lightPropagates) {
|
||||
chunk->setSunlight(ind, 15);
|
||||
chunk->setLight(ind, 3, 15);
|
||||
sunlightQueue.emplace(ind, chunk);
|
||||
}
|
||||
else {
|
||||
|
@ -253,7 +253,7 @@ void MapGen::generateSunlight(MapGen::chunk_partials_map &chunks, glm::ivec3 mbP
|
|||
propogateSunlightNodes(chunks, sunlightQueue);
|
||||
}
|
||||
|
||||
bool MapGen::containsWorldPos(BlockChunk *chunk, glm::ivec3 pos) {
|
||||
bool MapGen::containsWorldPos(Chunk *chunk, glm::ivec3 pos) {
|
||||
return chunk && Space::Chunk::world::fromBlock(pos) == chunk->pos;
|
||||
}
|
||||
|
||||
|
@ -261,13 +261,13 @@ void MapGen::propogateSunlightNodes(MapGen::chunk_partials_map &chunks, std::que
|
|||
while (!queue.empty()) {
|
||||
SunlightNode& node = queue.front();
|
||||
|
||||
unsigned char lightLevel = node.chunk->getSunlight(node.index);
|
||||
unsigned char lightLevel = node.chunk->getLight(node.index, 3);
|
||||
glm::ivec3 worldPos = node.chunk->pos * 16 + Space::Block::fromIndex(node.index);
|
||||
|
||||
for (const auto& i : Vec::adj) {
|
||||
glm::ivec3 check = worldPos + i;
|
||||
|
||||
BlockChunk* chunk;
|
||||
Chunk* chunk;
|
||||
if (containsWorldPos(node.chunk, check)) chunk = node.chunk;
|
||||
else {
|
||||
glm::ivec3 worldPos = Space::Chunk::world::fromBlock(check);
|
||||
|
@ -277,8 +277,8 @@ void MapGen::propogateSunlightNodes(MapGen::chunk_partials_map &chunks, std::que
|
|||
}
|
||||
|
||||
auto ind = Space::Block::index(check);
|
||||
if (defs.blockFromId(chunk->getBlock(ind)).lightPropagates && chunk->getSunlight(ind) + 2 <= lightLevel) {
|
||||
chunk->setSunlight(ind, lightLevel - static_cast<int>(!(lightLevel == 15 && i.y == -1)));
|
||||
if (defs.blockFromId(chunk->getBlock(ind)).lightPropagates && chunk->getLight(ind, 3) + 2 <= lightLevel) {
|
||||
chunk->setLight(ind, 3, lightLevel - static_cast<int>(!(lightLevel == 15 && i.y == -1)));
|
||||
queue.emplace(ind, chunk);
|
||||
}
|
||||
}
|
||||
|
@ -291,12 +291,11 @@ void MapGen::setBlock(glm::ivec3 worldPos, unsigned int block, MapGen::chunk_par
|
|||
if (block == DefinitionAtlas::INVALID) return;
|
||||
|
||||
glm::ivec3 chunkPos = Space::Chunk::world::fromBlock(worldPos);
|
||||
BlockChunk* chunk = nullptr;
|
||||
Chunk* chunk = nullptr;
|
||||
|
||||
if (chunks.count(chunkPos)) chunk = chunks.at(chunkPos).second;
|
||||
else {
|
||||
chunk = new BlockChunk();
|
||||
chunk->initializeEmpty();
|
||||
chunk = new Chunk();
|
||||
chunk->pos = chunkPos;
|
||||
chunk->partial = true;
|
||||
chunks.insert(std::pair<glm::ivec3, chunk_partial>{chunkPos, {new MapGenJob(), chunk}});
|
||||
|
@ -306,9 +305,9 @@ void MapGen::setBlock(glm::ivec3 worldPos, unsigned int block, MapGen::chunk_par
|
|||
if (chunk->getBlock(index) <= DefinitionAtlas::AIR) chunk->setBlock(index, block);
|
||||
}
|
||||
|
||||
std::shared_ptr<BlockChunk> MapGen::combinePartials(std::shared_ptr<BlockChunk> a, std::shared_ptr<BlockChunk> b) {
|
||||
std::shared_ptr<BlockChunk> src;
|
||||
std::shared_ptr<BlockChunk> res;
|
||||
std::shared_ptr<Chunk> MapGen::combinePartials(std::shared_ptr<Chunk> a, std::shared_ptr<Chunk> b) {
|
||||
std::shared_ptr<Chunk> src;
|
||||
std::shared_ptr<Chunk> res;
|
||||
|
||||
if (a->generated) {
|
||||
res = a;
|
||||
|
@ -325,6 +324,6 @@ std::shared_ptr<BlockChunk> MapGen::combinePartials(std::shared_ptr<BlockChunk>
|
|||
|
||||
res->generated = src->generated || res->generated;
|
||||
res->partial = !res->generated;
|
||||
res->calcNonAirBlocks();
|
||||
res->recalculateRenderableBlocks();
|
||||
return res;
|
||||
}
|
||||
|
|
|
@ -9,11 +9,11 @@
|
|||
#include "MapGenJob.h"
|
||||
#include "MapGenProps.h"
|
||||
#include "../../util/Vec.h"
|
||||
#include "../../world/chunk/BlockChunk.h"
|
||||
#include "../../world/chunk/Chunk.h"
|
||||
|
||||
class MapGen {
|
||||
public:
|
||||
typedef std::pair<MapGenJob*, BlockChunk*> chunk_partial;
|
||||
typedef std::pair<MapGenJob*, Chunk*> chunk_partial;
|
||||
typedef std::unordered_map<glm::ivec3, chunk_partial, Vec::ivec3> chunk_partials_map;
|
||||
|
||||
MapGen(unsigned int seed, DefinitionAtlas& atlas, BiomeAtlas& biome, std::shared_ptr<MapGenProps> props);
|
||||
|
@ -21,12 +21,12 @@ public:
|
|||
|
||||
// Combine two chunk partials, or a chunk and a chunk partial.
|
||||
// If both are partials `b` takes preference, if one is a fully generated chunk the partial takes preference.
|
||||
static std::shared_ptr<BlockChunk> combinePartials(std::shared_ptr<BlockChunk> a, std::shared_ptr<BlockChunk> b);
|
||||
static std::shared_ptr<Chunk> combinePartials(std::shared_ptr<Chunk> a, std::shared_ptr<Chunk> b);
|
||||
private:
|
||||
struct SunlightNode {
|
||||
SunlightNode(unsigned short index, BlockChunk* chunk) : index(index), chunk(chunk) {};
|
||||
SunlightNode(unsigned short index, Chunk* chunk) : index(index), chunk(chunk) {};
|
||||
unsigned short index;
|
||||
BlockChunk* chunk;
|
||||
Chunk* chunk;
|
||||
};
|
||||
|
||||
// Generate a chunk at `worldPos`, and place it and any partials in `chunks`.
|
||||
|
@ -44,7 +44,7 @@ private:
|
|||
|
||||
// Generate sunlight on the mapgen threads to speed up perf
|
||||
void generateSunlight(chunk_partials_map& chunks, glm::ivec3 mbPos);
|
||||
static bool containsWorldPos(BlockChunk *chunk, glm::ivec3 pos);
|
||||
static bool containsWorldPos(Chunk *chunk, glm::ivec3 pos);
|
||||
void propogateSunlightNodes(chunk_partials_map& chunks, std::queue<SunlightNode>& queue);
|
||||
|
||||
// Place block in the `chunks` array, creates a partial if necessary.
|
||||
|
|
|
@ -41,7 +41,7 @@ void LocalWorld::loadChunkPacket(std::unique_ptr<PacketView> p) {
|
|||
worldGenStream->queuePacket(std::move(p));
|
||||
}
|
||||
|
||||
void LocalWorld::commitChunk(std::shared_ptr<BlockChunk> c) {
|
||||
void LocalWorld::commitChunk(std::shared_ptr<Chunk> c) {
|
||||
dimension.setChunk(std::move(c));
|
||||
}
|
||||
|
||||
|
@ -104,7 +104,7 @@ unsigned short LocalWorld::getBiome(glm::vec3 pos) {
|
|||
return BiomeAtlas::INVALID;
|
||||
}
|
||||
|
||||
std::shared_ptr<BlockChunk> LocalWorld::getChunk(glm::ivec3 pos) {
|
||||
std::shared_ptr<Chunk> LocalWorld::getChunk(glm::ivec3 pos) {
|
||||
return dimension.getChunk(pos);
|
||||
}
|
||||
|
||||
|
|
|
@ -24,7 +24,7 @@ public:
|
|||
void update(double delta) override;
|
||||
|
||||
void loadChunkPacket(std::unique_ptr<PacketView> p);
|
||||
void commitChunk(std::shared_ptr<BlockChunk> chunk);
|
||||
void commitChunk(std::shared_ptr<Chunk> chunk);
|
||||
|
||||
unsigned int getBlock(glm::ivec3 pos) override;
|
||||
void setBlock(glm::ivec3 pos, unsigned int block) override;
|
||||
|
@ -33,7 +33,7 @@ public:
|
|||
void damageBlock(glm::vec3 pos, float amount);
|
||||
|
||||
unsigned short getBiome(glm::vec3 pos);
|
||||
std::shared_ptr<BlockChunk> getChunk(glm::ivec3 pos);
|
||||
std::shared_ptr<Chunk> getChunk(glm::ivec3 pos);
|
||||
|
||||
int getMeshChunkCount();
|
||||
int renderChunks(Renderer &render);
|
||||
|
|
|
@ -51,15 +51,15 @@ std::vector<ChunkMeshDetails*> MeshGenStream::update() {
|
|||
queuedMap.erase(pos);
|
||||
|
||||
u.meshDetails->pos = pos;
|
||||
std::shared_ptr<BlockChunk> chunk = dimension.getChunk(pos);
|
||||
std::shared_ptr<Chunk> chunk = dimension.getChunk(pos);
|
||||
if (chunk == nullptr) goto breakAddTask;
|
||||
|
||||
u.thisChunk = std::shared_ptr<BlockChunk>(chunk);
|
||||
u.thisChunk = std::shared_ptr<Chunk>(chunk);
|
||||
|
||||
int ind = 0;
|
||||
for (const glm::ivec3& dir : Vec::adj) {
|
||||
std::shared_ptr<BlockChunk> adjacent = dimension.getChunk(pos + dir);
|
||||
u.adjacentChunks[ind++] = std::shared_ptr<BlockChunk>(adjacent);
|
||||
std::shared_ptr<Chunk> adjacent = dimension.getChunk(pos + dir);
|
||||
u.adjacentChunks[ind++] = std::shared_ptr<Chunk>(adjacent);
|
||||
if (adjacent == nullptr) goto breakAddTask;
|
||||
}
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
#include "../../../util/Vec.h"
|
||||
#include "../../../def/ClientGame.h"
|
||||
#include "../../../def/gen/NoiseSample.h"
|
||||
#include "../../../world/chunk/BlockChunk.h"
|
||||
#include "../../../world/chunk/Chunk.h"
|
||||
|
||||
class LocalDimension;
|
||||
|
||||
|
@ -37,8 +37,8 @@ public:
|
|||
std::vector<ChunkMeshDetails*> update();
|
||||
|
||||
struct Unit {
|
||||
std::shared_ptr<BlockChunk> thisChunk = nullptr;
|
||||
std::array<std::shared_ptr<BlockChunk>, 6> adjacentChunks {};
|
||||
std::shared_ptr<Chunk> thisChunk = nullptr;
|
||||
std::array<std::shared_ptr<Chunk>, 6> adjacentChunks {};
|
||||
|
||||
ChunkMeshDetails* meshDetails = new ChunkMeshDetails();
|
||||
|
||||
|
|
|
@ -140,7 +140,7 @@ void Player::updateCamera() {
|
|||
|
||||
void Player::findPointedThing(Input &input) {
|
||||
glm::ivec3 chunkPos = {};
|
||||
std::shared_ptr<BlockChunk> blockChunk = nullptr;
|
||||
std::shared_ptr<Chunk> blockChunk = nullptr;
|
||||
|
||||
for (Ray ray(this); ray.getLength() < LOOK_DISTANCE; ray.step(LOOK_PRECISION)) {
|
||||
glm::vec3 rayEnd = ray.getEnd();
|
||||
|
|
|
@ -27,8 +27,8 @@ bool WorldInterpolationStream::queuePosition(glm::vec3 pos){
|
|||
return false;
|
||||
}
|
||||
|
||||
std::unique_ptr<std::vector<std::shared_ptr<BlockChunk>>> WorldInterpolationStream::update() {
|
||||
auto finishedChunks = std::make_unique<std::vector<std::shared_ptr<BlockChunk>>>();
|
||||
std::unique_ptr<std::vector<std::shared_ptr<Chunk>>> WorldInterpolationStream::update() {
|
||||
auto finishedChunks = std::make_unique<std::vector<std::shared_ptr<Chunk>>>();
|
||||
auto finishedMapBlocks = std::make_unique<std::vector<std::shared_ptr<MeshFarMap>>>();
|
||||
|
||||
for (auto& t : threads) {
|
||||
|
@ -81,7 +81,7 @@ void WorldInterpolationStream::Thread::exec() {
|
|||
if (u.locked) {
|
||||
if (u.job == JobType::PACKET) {
|
||||
empty = false;
|
||||
u.chunk = std::make_shared<BlockChunk>();
|
||||
u.chunk = std::make_shared<Chunk>();
|
||||
u.chunk->deserialize(*u.packet);
|
||||
u.locked = false;
|
||||
break;
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
|
||||
#include "graph/MeshFarMap.h"
|
||||
#include "../../../def/gen/MapGen.h"
|
||||
#include "../../../world/chunk/BlockChunk.h"
|
||||
#include "../../../world/chunk/Chunk.h"
|
||||
|
||||
class ClientGame;
|
||||
|
||||
|
@ -28,7 +28,7 @@ public:
|
|||
bool queuePosition(glm::vec3 pos);
|
||||
// Returns a vector of BlockChunks that have finished processing,
|
||||
// and gives the threads new data to work with.
|
||||
std::unique_ptr<std::vector<std::shared_ptr<BlockChunk>>> update();
|
||||
std::unique_ptr<std::vector<std::shared_ptr<Chunk>>> update();
|
||||
|
||||
private:
|
||||
enum class JobType {
|
||||
|
@ -42,7 +42,7 @@ private:
|
|||
JobType job = JobType::EMPTY;
|
||||
|
||||
std::unique_ptr<PacketView> packet = nullptr;
|
||||
std::shared_ptr<BlockChunk> chunk = nullptr;
|
||||
std::shared_ptr<Chunk> chunk = nullptr;
|
||||
|
||||
std::shared_ptr<MeshFarMap> mapblock = nullptr;
|
||||
glm::vec3 mapBlockPos = {0, 0, 0};
|
||||
|
|
|
@ -9,10 +9,10 @@
|
|||
|
||||
#include "../ChunkMeshDetails.h"
|
||||
#include "../../../../util/Vec.h"
|
||||
#include "../../../../world/chunk/BlockChunk.h"
|
||||
#include "../../../../world/chunk/Chunk.h"
|
||||
|
||||
ChunkMeshGenerator::ChunkMeshGenerator(ChunkMeshDetails* meshDetails, LocalDefinitionAtlas& defs, LocalBiomeAtlas& biomes,
|
||||
std::shared_ptr<BlockChunk> chunk, std::array<std::shared_ptr<BlockChunk>, 6> adjacent,
|
||||
std::shared_ptr<Chunk> chunk, std::array<std::shared_ptr<Chunk>, 6> adjacent,
|
||||
std::array<NoiseSample, 3>& blockOffsets) :
|
||||
|
||||
defs(defs),
|
||||
|
|
|
@ -13,12 +13,12 @@
|
|||
#include "../../../../def/gen/NoiseSample.h"
|
||||
|
||||
class ChunkMeshDetails;
|
||||
class BlockChunk;
|
||||
class Chunk;
|
||||
|
||||
class ChunkMeshGenerator {
|
||||
public:
|
||||
ChunkMeshGenerator(ChunkMeshDetails* meshDetails, LocalDefinitionAtlas& defs, LocalBiomeAtlas& biomes,
|
||||
std::shared_ptr<BlockChunk> chunk, std::array<std::shared_ptr<BlockChunk>, 6> adjacent,
|
||||
std::shared_ptr<Chunk> chunk, std::array<std::shared_ptr<Chunk>, 6> adjacent,
|
||||
std::array<NoiseSample, 3>& blockOffsets);
|
||||
private:
|
||||
inline BlockDef& getBlockAt(const glm::ivec3& pos);
|
||||
|
@ -32,6 +32,6 @@ private:
|
|||
unsigned int indOffset = 0;
|
||||
ChunkMeshDetails* meshDetails;
|
||||
|
||||
std::shared_ptr<BlockChunk> chunk;
|
||||
std::array<std::shared_ptr<BlockChunk>, 6> adjacent;
|
||||
std::shared_ptr<Chunk> chunk;
|
||||
std::array<std::shared_ptr<Chunk>, 6> adjacent;
|
||||
};
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
#include "../FarMapMeshDetails.h"
|
||||
#include "../../../../util/Vec.h"
|
||||
#include "../../../../world/chunk/BlockChunk.h"
|
||||
#include "../../../../world/chunk/Chunk.h"
|
||||
|
||||
//FarMeshGenerator::FarMeshGenerator(LocalDefinitionAtlas& defs, LocalBiomeAtlas& biomes,
|
||||
// std::vector<unsigned int>& blocks, unsigned int width) :
|
||||
|
|
|
@ -36,15 +36,17 @@ public:
|
|||
|
||||
// C++ Functions and Properties
|
||||
Any getAsAny(const std::string& key) const;
|
||||
|
||||
template <typename T> const T& get(const std::string& key) const {
|
||||
Any a = getAsAny(key);
|
||||
return a.get<T>();
|
||||
return getAsAny(key).get<T>();
|
||||
}
|
||||
|
||||
template <typename T> const T& get_or(const std::string& key, const T& other) const noexcept {
|
||||
Any a = getAsAny(key);
|
||||
if (a.empty() || !a.is<T>()) return other;
|
||||
return a.get<T>();
|
||||
}
|
||||
|
||||
template <typename T> const bool has(const std::string& key) const noexcept {
|
||||
Any a = getAsAny(key);
|
||||
return !a.empty() && a.is<T>();
|
||||
|
|
|
@ -32,7 +32,7 @@ std::unique_ptr<std::vector<ServerGenStream::FinishedBlockJob>> ServerGenStream:
|
|||
|
||||
if (!u.chunks.empty()) {
|
||||
finishedChunks->push_back({u.pos, {}});
|
||||
for (auto chunk : u.chunks) finishedChunks->back().chunks.push_back(std::shared_ptr<BlockChunk>(chunk.second.second));
|
||||
for (auto chunk : u.chunks) finishedChunks->back().chunks.push_back(std::shared_ptr<Chunk>(chunk.second.second));
|
||||
u.chunks.clear();
|
||||
}
|
||||
|
||||
|
|
|
@ -20,7 +20,7 @@ public:
|
|||
|
||||
struct FinishedBlockJob {
|
||||
glm::ivec3 pos;
|
||||
std::vector<std::shared_ptr<BlockChunk>> chunks;
|
||||
std::vector<std::shared_ptr<Chunk>> chunks;
|
||||
};
|
||||
|
||||
explicit ServerGenStream(unsigned int seed, ServerGame& game);
|
||||
|
|
|
@ -160,7 +160,7 @@ bool ServerWorld::generateMapBlock(glm::ivec3 pos) {
|
|||
return false;
|
||||
}
|
||||
|
||||
void ServerWorld::sendChunk(const std::shared_ptr<BlockChunk>& chunk, ServerClient &peer) {
|
||||
void ServerWorld::sendChunk(const std::shared_ptr<Chunk>& chunk, ServerClient &peer) {
|
||||
if (chunk == nullptr || !chunk->generated) return;
|
||||
|
||||
Packet r = chunk->serialize();
|
||||
|
|
|
@ -30,7 +30,7 @@ private:
|
|||
bool generateMapBlock(glm::ivec3 pos);
|
||||
|
||||
void sendChunk(const glm::ivec3& pos, ServerClient& client);
|
||||
static void sendChunk(const std::shared_ptr<BlockChunk>& chunk, ServerClient& client);
|
||||
static void sendChunk(const std::shared_ptr<Chunk>& chunk, ServerClient& client);
|
||||
void sendMapBlock(const glm::ivec3& pos, ServerClient& client);
|
||||
|
||||
static bool isInBounds(glm::ivec3 pos, std::pair<glm::ivec3, glm::ivec3>& bounds);
|
||||
|
|
|
@ -21,27 +21,26 @@ public:
|
|||
|
||||
template <typename T, typename T_ = T> void set(T_&& val) {
|
||||
isEmpty = false;
|
||||
// if (sizeof(T) < STACK_SIZE) {
|
||||
// data.h = nullptr;
|
||||
// new(data.s) T(std::forward<T_>(val));
|
||||
// stack_destructor = std::bind(&Any::delete_stack<T>, this);
|
||||
// type = typeid(T).hash_code();
|
||||
// }
|
||||
// else {
|
||||
if (sizeof(T) < STACK_SIZE) {
|
||||
data.h = nullptr;
|
||||
new(data.s) T(std::forward<T_>(val));
|
||||
stack_destructor = std::bind(&Any::delete_stack<T>, this);
|
||||
type = typeid(T).hash_code();
|
||||
}
|
||||
else {
|
||||
if (stack_destructor) stack_destructor();
|
||||
data.h = std::make_shared<T>(std::forward<T_>(val));
|
||||
stack_destructor = nullptr;
|
||||
type = typeid(T).hash_code();
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T> const T& get() const {
|
||||
if (empty()) throw std::logic_error("Tried to get empty Any.");
|
||||
if (type != typeid(T).hash_code()) throw std::logic_error("Any is not of type specified.");
|
||||
|
||||
// if (sizeof(T) < STACK_SIZE) return *reinterpret_cast<T*>(const_cast<char*>(data.s));
|
||||
// else
|
||||
return *std::static_pointer_cast<T>(data.h);
|
||||
if (sizeof(T) < STACK_SIZE) return *reinterpret_cast<T*>(const_cast<char*>(data.s));
|
||||
else return *std::static_pointer_cast<T>(data.h);
|
||||
}
|
||||
|
||||
template<typename T> const T& get_or(const T& other) const noexcept {
|
||||
|
@ -66,12 +65,13 @@ public:
|
|||
}
|
||||
|
||||
~Any() {
|
||||
if (stack_destructor) stack_destructor();
|
||||
// if (stack_destructor) stack_destructor();
|
||||
}
|
||||
|
||||
private:
|
||||
template <typename T> void delete_stack() {
|
||||
reinterpret_cast<T*>(data.s)->~T();
|
||||
memset(data.s, 0, STACK_SIZE);
|
||||
stack_destructor = nullptr;
|
||||
type = 0;
|
||||
}
|
||||
|
|
|
@ -63,7 +63,7 @@ std::unordered_set<glm::ivec3, Vec::ivec3> Dimension::propogateAddNodes() {
|
|||
for (const auto& i : Vec::adj) {
|
||||
glm::ivec3 check = worldPos + i;
|
||||
unsigned int ind = Space::Block::index(check);
|
||||
BlockChunk* chunk;
|
||||
Chunk* chunk;
|
||||
if (containsWorldPos(node.chunk, check)) chunk = node.chunk;
|
||||
else {
|
||||
chunk = getChunk(Space::Chunk::world::fromBlock(check)).get();
|
||||
|
@ -98,7 +98,7 @@ std::unordered_set<glm::ivec3, Vec::ivec3> Dimension::propogateRemoveNodes() {
|
|||
for (const auto& i : Vec::adj) {
|
||||
glm::ivec3 check = worldPos + i;
|
||||
unsigned int ind = Space::Block::index(check);
|
||||
BlockChunk* chunk;
|
||||
Chunk* chunk;
|
||||
if (containsWorldPos(node.chunk, check)) chunk = node.chunk;
|
||||
else {
|
||||
chunk = getChunk(Space::Chunk::world::fromBlock(check)).get();
|
||||
|
@ -133,17 +133,17 @@ std::unordered_set<glm::ivec3, Vec::ivec3> Dimension::propogateRemoveNodes() {
|
|||
return chunksUpdated;
|
||||
}
|
||||
|
||||
bool Dimension::containsWorldPos(BlockChunk *chunk, glm::ivec3 pos) {
|
||||
bool Dimension::containsWorldPos(Chunk *chunk, glm::ivec3 pos) {
|
||||
return chunk && Space::Chunk::world::fromBlock(pos) == chunk->pos;
|
||||
}
|
||||
|
||||
glm::ivec4 Dimension::getLight(glm::ivec3 worldPos, BlockChunk *chunk) {
|
||||
glm::ivec4 Dimension::getLight(glm::ivec3 worldPos, Chunk *chunk) {
|
||||
if (containsWorldPos(chunk, worldPos)) return chunk->getLight(Space::Block::index(worldPos));
|
||||
auto oChunk = getChunk(Space::Chunk::world::fromBlock(worldPos)).get();
|
||||
return (oChunk ? oChunk->getLight(Space::Block::index(worldPos)) : glm::ivec4 {});
|
||||
}
|
||||
|
||||
void Dimension::calculateHorizontalEdge(std::shared_ptr<BlockChunk> a, std::shared_ptr<BlockChunk> b) {
|
||||
void Dimension::calculateHorizontalEdge(std::shared_ptr<Chunk> a, std::shared_ptr<Chunk> b) {
|
||||
for (unsigned int j = 0; j < 256; j++) {
|
||||
glm::ivec3 diff = a->pos - b->pos;
|
||||
|
||||
|
@ -154,21 +154,21 @@ void Dimension::calculateHorizontalEdge(std::shared_ptr<BlockChunk> a, std::shar
|
|||
(diff.x == 0 ? j % 16 : diff.x == 1 ? 15 : 0), j / 16,
|
||||
(diff.z == 0 ? j % 16 : diff.z == 1 ? 15 : 0) };
|
||||
|
||||
auto lightA = a->getSunlight(Space::Block::index(aPos));
|
||||
auto lightB = b->getSunlight(Space::Block::index(bPos));
|
||||
auto lightA = a->getLight(Space::Block::index(aPos), 3);
|
||||
auto lightB = b->getLight(Space::Block::index(bPos), 3);
|
||||
|
||||
if (lightA > lightB + 1) setAndReflowSunlight(b->pos * 16 + bPos, lightA - 1);
|
||||
else if (lightB > lightA + 1) setAndReflowSunlight(a->pos * 16 + aPos, lightB - 1);
|
||||
}
|
||||
}
|
||||
|
||||
void Dimension::calculateVerticalEdge(std::shared_ptr<BlockChunk> above, std::shared_ptr<BlockChunk> below) {
|
||||
void Dimension::calculateVerticalEdge(std::shared_ptr<Chunk> above, std::shared_ptr<Chunk> below) {
|
||||
for (unsigned int j = 0; j < 256; j++) {
|
||||
unsigned int xx = j / 16;
|
||||
unsigned int zz = j % 16;
|
||||
|
||||
auto lightAbove = above->getSunlight(Space::Block::index({xx, 0, zz}));
|
||||
auto lightBelow = below->getSunlight(Space::Block::index({xx, 15, zz}));
|
||||
auto lightAbove = above->getLight(Space::Block::index({xx, 0, zz}), 3);
|
||||
auto lightBelow = below->getLight(Space::Block::index({xx, 15, zz}), 3);
|
||||
|
||||
if (lightBelow > lightAbove) removeSunlight(below->pos * 16 + glm::ivec3{xx, 15, zz});
|
||||
}
|
||||
|
@ -178,7 +178,10 @@ void Dimension::addBlockLight(glm::ivec3 pos, glm::ivec3 light) {
|
|||
auto startChunk = getChunk(Space::Chunk::world::fromBlock(pos));
|
||||
auto ind = Space::Block::index(pos);
|
||||
|
||||
startChunk->setBlockLight(ind, light);
|
||||
startChunk->setLight(ind, 0, light.x);
|
||||
startChunk->setLight(ind, 1, light.y);
|
||||
startChunk->setLight(ind, 2, light.z);
|
||||
|
||||
lightAddQueue[0].emplace(ind, startChunk.get());
|
||||
lightAddQueue[1].emplace(ind, startChunk.get());
|
||||
lightAddQueue[2].emplace(ind, startChunk.get());
|
||||
|
@ -186,11 +189,13 @@ void Dimension::addBlockLight(glm::ivec3 pos, glm::ivec3 light) {
|
|||
|
||||
void Dimension::removeBlockLight(glm::ivec3 pos) {
|
||||
auto startChunk = getChunk(Space::Chunk::world::fromBlock(pos));
|
||||
auto ind = Space::Block::index(pos);
|
||||
|
||||
unsigned int ind = Space::Block::index(pos);
|
||||
glm::ivec4 val = startChunk->getLight(ind);
|
||||
|
||||
startChunk->setBlockLight(Space::Block::index(pos), {});
|
||||
startChunk->setLight(ind, 0, 0);
|
||||
startChunk->setLight(ind, 1, 0);
|
||||
startChunk->setLight(ind, 2, 0);
|
||||
|
||||
lightRemoveQueue[0].emplace(ind, val.x, startChunk.get());
|
||||
lightRemoveQueue[1].emplace(ind, val.y, startChunk.get());
|
||||
lightRemoveQueue[2].emplace(ind, val.z, startChunk.get());
|
||||
|
@ -213,8 +218,7 @@ void Dimension::reflowLight(glm::ivec3 pos) {
|
|||
placeLight.w = fmax(placeLight.w, adjLight.w - (i.y == 1 ? 0 : 1));
|
||||
}
|
||||
|
||||
chunk->setBlockLight(ind, {placeLight.x, placeLight.y, placeLight.z});
|
||||
chunk->setSunlight(ind, placeLight.w);
|
||||
chunk->setLight(ind, placeLight);
|
||||
|
||||
lightAddQueue[0].emplace(ind, chunk.get());
|
||||
lightAddQueue[1].emplace(ind, chunk.get());
|
||||
|
@ -225,9 +229,9 @@ void Dimension::reflowLight(glm::ivec3 pos) {
|
|||
void Dimension::removeSunlight(glm::ivec3 pos) {
|
||||
auto chunk = getChunk(Space::Chunk::world::fromBlock(pos));
|
||||
unsigned int ind = Space::Block::index(pos);
|
||||
unsigned int light = chunk->getSunlight(ind);
|
||||
unsigned int light = chunk->getLight(ind, 3);
|
||||
|
||||
chunk->setSunlight(ind, 0);
|
||||
chunk->setLight(ind, 3, 0);
|
||||
lightRemoveQueue[SUNLIGHT_CHANNEL].emplace(ind, light, chunk.get());
|
||||
}
|
||||
|
||||
|
@ -235,6 +239,6 @@ void Dimension::setAndReflowSunlight(glm::ivec3 pos, unsigned char level) {
|
|||
auto chunk = getChunk(Space::Chunk::world::fromBlock(pos));
|
||||
unsigned int ind = Space::Block::index(pos);
|
||||
|
||||
chunk->setSunlight(ind, level);
|
||||
chunk->setLight(ind, 3, level);
|
||||
lightAddQueue[SUNLIGHT_CHANNEL].emplace(ind, chunk.get());
|
||||
}
|
|
@ -32,11 +32,11 @@ protected:
|
|||
private:
|
||||
// Lighting functions
|
||||
|
||||
static inline bool containsWorldPos(BlockChunk* chunk, glm::ivec3 pos);
|
||||
inline glm::ivec4 getLight(glm::ivec3 worldPos, BlockChunk* chunk = nullptr);
|
||||
static inline bool containsWorldPos(Chunk* chunk, glm::ivec3 pos);
|
||||
inline glm::ivec4 getLight(glm::ivec3 worldPos, Chunk* chunk = nullptr);
|
||||
|
||||
void calculateHorizontalEdge(std::shared_ptr<BlockChunk> a, std::shared_ptr<BlockChunk> b);
|
||||
void calculateVerticalEdge(std::shared_ptr<BlockChunk> above, std::shared_ptr<BlockChunk> below);
|
||||
void calculateHorizontalEdge(std::shared_ptr<Chunk> a, std::shared_ptr<Chunk> b);
|
||||
void calculateVerticalEdge(std::shared_ptr<Chunk> above, std::shared_ptr<Chunk> below);
|
||||
|
||||
inline void addBlockLight(glm::ivec3 pos, glm::ivec3 light);
|
||||
inline void removeBlockLight(glm::ivec3 pos);
|
||||
|
@ -46,12 +46,12 @@ private:
|
|||
inline void setAndReflowSunlight(glm::ivec3 pos, unsigned char level);
|
||||
|
||||
struct LightAddNode {
|
||||
LightAddNode(unsigned short index, BlockChunk* chunk) : index(index), chunk(chunk) {};
|
||||
unsigned short index; BlockChunk* chunk;
|
||||
LightAddNode(unsigned short index, Chunk* chunk) : index(index), chunk(chunk) {};
|
||||
unsigned short index; Chunk* chunk;
|
||||
};
|
||||
struct LightRemoveNode {
|
||||
LightRemoveNode(unsigned short index, unsigned short value, BlockChunk* chunk) : index(index), value(value), chunk(chunk) {};
|
||||
unsigned short index, value; BlockChunk* chunk;
|
||||
LightRemoveNode(unsigned short index, unsigned short value, Chunk* chunk) : index(index), value(value), chunk(chunk) {};
|
||||
unsigned short index, value; Chunk* chunk;
|
||||
};
|
||||
|
||||
static constexpr unsigned char SUNLIGHT_CHANNEL = 3;
|
||||
|
|
|
@ -34,13 +34,13 @@ bool DimensionBase::mapBlockGenerated(glm::ivec3 mapBlockPosition) {
|
|||
return mb && mb->generated;
|
||||
}
|
||||
|
||||
std::shared_ptr<BlockChunk> DimensionBase::getChunk(glm::ivec3 chunkPosition) {
|
||||
std::shared_ptr<Chunk> DimensionBase::getChunk(glm::ivec3 chunkPosition) {
|
||||
auto mapBlock = getMapBlock(Space::MapBlock::world::fromChunk(chunkPosition));
|
||||
if (!mapBlock) return nullptr;
|
||||
return (*mapBlock)[Space::Chunk::index(chunkPosition)];
|
||||
}
|
||||
|
||||
void DimensionBase::setChunk(std::shared_ptr<BlockChunk> chunk) {
|
||||
void DimensionBase::setChunk(std::shared_ptr<Chunk> chunk) {
|
||||
auto mapBlock = getOrCreateMapBlock(Space::MapBlock::world::fromChunk(chunk->pos));
|
||||
(*mapBlock).set(Space::Chunk::index(chunk->pos), chunk);
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
#include <memory>
|
||||
|
||||
#include "../util/Vec.h"
|
||||
#include "region/Region.h"
|
||||
#include "chunk/Region.h"
|
||||
|
||||
class DimensionBase {
|
||||
public:
|
||||
|
@ -23,8 +23,8 @@ public:
|
|||
|
||||
bool mapBlockGenerated(glm::ivec3 mapBlockPosition);
|
||||
|
||||
std::shared_ptr<BlockChunk> getChunk(glm::ivec3 chunkPosition);
|
||||
virtual void setChunk(std::shared_ptr<BlockChunk> chunk);
|
||||
std::shared_ptr<Chunk> getChunk(glm::ivec3 chunkPosition);
|
||||
virtual void setChunk(std::shared_ptr<Chunk> chunk);
|
||||
virtual void removeChunk(glm::ivec3 pos);
|
||||
|
||||
unsigned int getBlock(glm::ivec3 pos);
|
||||
|
|
|
@ -103,7 +103,7 @@ void LocalDimension::renderEntities(Renderer &renderer) {
|
|||
for (auto& entity : playerEntities) entity.draw(renderer);
|
||||
}
|
||||
|
||||
void LocalDimension::setChunk(std::shared_ptr<BlockChunk> chunk) {
|
||||
void LocalDimension::setChunk(std::shared_ptr<Chunk> chunk) {
|
||||
Dimension::setChunk(chunk);
|
||||
attemptMeshChunk(chunk);
|
||||
}
|
||||
|
@ -208,7 +208,7 @@ bool LocalDimension::setBlock(glm::ivec3 pos, unsigned int block) {
|
|||
return true;
|
||||
}
|
||||
|
||||
void LocalDimension::attemptMeshChunk(const std::shared_ptr<BlockChunk>& chunk, bool updateAdjacents) {
|
||||
void LocalDimension::attemptMeshChunk(const std::shared_ptr<Chunk>& chunk, bool updateAdjacents) {
|
||||
static const std::vector<glm::ivec3> dirs {
|
||||
glm::ivec3 {1, 0, 0}, glm::ivec3 {-1, 0, 0},
|
||||
glm::ivec3 {0, 1, 0}, glm::ivec3 {0, -1, 0},
|
||||
|
|
|
@ -22,7 +22,7 @@ public:
|
|||
explicit LocalDimension(ClientGame& game);
|
||||
void update(double delta, glm::vec3 playerPos);
|
||||
|
||||
void setChunk(std::shared_ptr<BlockChunk> chunk) override;
|
||||
void setChunk(std::shared_ptr<Chunk> chunk) override;
|
||||
bool setBlock(glm::ivec3 pos, unsigned int block) override;
|
||||
|
||||
void setMeshChunk(std::shared_ptr<MeshChunk> chunk);
|
||||
|
@ -53,7 +53,7 @@ private:
|
|||
void finishMeshes();
|
||||
void queueMeshes();
|
||||
|
||||
void attemptMeshChunk(const std::shared_ptr<BlockChunk>& chunk, bool updateAdjacents = true);
|
||||
void attemptMeshChunk(const std::shared_ptr<Chunk>& chunk, bool updateAdjacents = true);
|
||||
bool getAdjacentExists(glm::vec3 pos, bool updateAdjacents);
|
||||
|
||||
ClientGame& game;
|
||||
|
|
|
@ -42,9 +42,9 @@ bool ServerDimension::setBlock(glm::ivec3 pos, unsigned int block) {
|
|||
return true;
|
||||
}
|
||||
|
||||
void ServerDimension::setChunk(std::shared_ptr<BlockChunk> chunk) {
|
||||
void ServerDimension::setChunk(std::shared_ptr<Chunk> chunk) {
|
||||
// Combine partials if there are any
|
||||
std::shared_ptr<BlockChunk> existing = getChunk(chunk->pos);
|
||||
std::shared_ptr<Chunk> existing = getChunk(chunk->pos);
|
||||
if (existing != nullptr) chunk = MapGen::combinePartials(chunk, existing);
|
||||
|
||||
Dimension::setChunk(chunk);
|
||||
|
|
|
@ -16,7 +16,7 @@ public:
|
|||
|
||||
void update(const std::vector<std::shared_ptr<ServerClient>>& clients);
|
||||
|
||||
void setChunk(std::shared_ptr<BlockChunk> chunk) override;
|
||||
void setChunk(std::shared_ptr<Chunk> chunk) override;
|
||||
bool setBlock(glm::ivec3 pos, unsigned int block) override;
|
||||
|
||||
void addLuaEntity(std::shared_ptr<ServerLuaEntity>& entity);
|
||||
|
|
|
@ -6,27 +6,23 @@
|
|||
#include <gzip/decompress.hpp>
|
||||
#include <gzip/utils.hpp>
|
||||
|
||||
#include "BlockChunk.h"
|
||||
#include "Chunk.h"
|
||||
|
||||
#include "../../util/net/Serializer.h"
|
||||
#include "../../util/net/PacketView.h"
|
||||
|
||||
BlockChunk::BlockChunk(const std::vector<unsigned int>& blocks, const std::vector<unsigned short>& biomes) :
|
||||
BlockChunk(blocks, biomes, {0, 0, 0}) {}
|
||||
Chunk::Chunk(const std::vector<unsigned int>& blocks, const std::vector<unsigned short>& biomes) :
|
||||
Chunk(blocks, biomes, {0, 0, 0}) {}
|
||||
|
||||
BlockChunk::BlockChunk(const std::vector<unsigned int>& blocks, const std::vector<unsigned short>& biomes, glm::ivec3 pos) :
|
||||
blocks(std::move(blocks)),
|
||||
biomes(std::move(biomes)),
|
||||
pos(pos),
|
||||
generated(true) {
|
||||
memset(blocklight.data(), 0, sizeof(blocklight));
|
||||
calcNonAirBlocks();
|
||||
Chunk::Chunk(const std::vector<unsigned int>& blocks, const std::vector<unsigned short>& biomes, glm::ivec3 pos) :
|
||||
blocks(std::move(blocks)), biomes(std::move(biomes)),
|
||||
generated(true), pos(pos) {
|
||||
recalculateRenderableBlocks();
|
||||
}
|
||||
|
||||
bool BlockChunk::setBlock(unsigned int ind, unsigned int blk) {
|
||||
bool Chunk::setBlock(unsigned int ind, unsigned int blk) {
|
||||
if (!RIE::write(ind, blk, blocks, 4096)) return false;
|
||||
|
||||
// Mesh emptiness manipulation
|
||||
if (blk == DefinitionAtlas::AIR) {
|
||||
if ((nonAirBlocks = fmax(nonAirBlocks - 1, 0)) == 0) {
|
||||
empty = true;
|
||||
|
@ -42,15 +38,15 @@ bool BlockChunk::setBlock(unsigned int ind, unsigned int blk) {
|
|||
return true;
|
||||
}
|
||||
|
||||
const std::vector<unsigned int> &BlockChunk::cGetBlocks() const {
|
||||
const std::vector<unsigned int> &Chunk::cGetBlocks() const {
|
||||
return blocks;
|
||||
}
|
||||
|
||||
const std::vector<unsigned short> &BlockChunk::cGetBiomes() const {
|
||||
const std::vector<unsigned short> &Chunk::cGetBiomes() const {
|
||||
return biomes;
|
||||
}
|
||||
|
||||
Packet BlockChunk::serialize() {
|
||||
Packet Chunk::serialize() {
|
||||
Serializer s;
|
||||
s.append(pos);
|
||||
|
||||
|
@ -74,7 +70,7 @@ Packet BlockChunk::serialize() {
|
|||
return s.packet(PacketType::CHUNK);
|
||||
}
|
||||
|
||||
void BlockChunk::deserialize(PacketView& packet) {
|
||||
void Chunk::deserialize(PacketView& packet) {
|
||||
|
||||
pos = packet.d.read<glm::ivec3>();
|
||||
|
||||
|
@ -83,7 +79,7 @@ void BlockChunk::deserialize(PacketView& packet) {
|
|||
gzip = gzip::decompress(gzip.data(), gzip.length());
|
||||
|
||||
blocks = Deserializer(gzip).read<std::vector<unsigned int>>();
|
||||
calcNonAirBlocks();
|
||||
recalculateRenderableBlocks();
|
||||
|
||||
gzip = packet.d.read<std::string>();
|
||||
if (!gzip::is_compressed(gzip.data(), gzip.length())) throw "Invalid Biomes GZip Data.";
|
||||
|
@ -102,10 +98,9 @@ void BlockChunk::deserialize(PacketView& packet) {
|
|||
blocklight[i].b = lightsVec[i * 4 + 2];
|
||||
setSunlight(i, lightsVec[i * 4 + 3]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void BlockChunk::calcNonAirBlocks() {
|
||||
void Chunk::recalculateRenderableBlocks() {
|
||||
nonAirBlocks = 0;
|
||||
empty = true;
|
||||
|
||||
|
@ -122,8 +117,3 @@ void BlockChunk::calcNonAirBlocks() {
|
|||
|
||||
shouldHaveMesh = !empty;
|
||||
}
|
||||
|
||||
void BlockChunk::initializeEmpty() {
|
||||
blocks = {0, 0};
|
||||
biomes = {0, 0};
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
//
|
||||
// The Chunk data class that contains the block, biome, and light data.
|
||||
// Created by aurailus on 14/12/18.
|
||||
//
|
||||
|
||||
|
@ -14,143 +15,140 @@
|
|||
#include "../../def/DefinitionAtlas.h"
|
||||
#include "../../util/net/PacketView.h"
|
||||
|
||||
class BlockChunk {
|
||||
public:
|
||||
BlockChunk() = default;
|
||||
explicit BlockChunk(const std::vector<unsigned int>& blocks, const std::vector<unsigned short>& biomes);
|
||||
BlockChunk(const std::vector<unsigned int>& blocks, const std::vector<unsigned short>& biomes, glm::ivec3 pos);
|
||||
class Chunk {
|
||||
friend class MapGen;
|
||||
|
||||
void initializeEmpty();
|
||||
|
||||
// Blocks
|
||||
|
||||
inline unsigned int getBlock(unsigned int ind) const;
|
||||
inline unsigned int getBlock(const glm::ivec3& pos) const;
|
||||
|
||||
bool setBlock(unsigned int ind, unsigned int blk);
|
||||
inline bool setBlock(const glm::ivec3& pos, unsigned int blk);
|
||||
|
||||
const std::vector<unsigned int>& cGetBlocks() const;
|
||||
|
||||
// Biomes
|
||||
|
||||
inline unsigned short getBiome(unsigned int ind) const;
|
||||
inline unsigned short getBiome(const glm::ivec3& pos) const;
|
||||
|
||||
inline bool setBiome(unsigned int ind, unsigned short bio);
|
||||
inline bool setBiome(const glm::ivec3& pos, unsigned short bio);
|
||||
const std::vector<unsigned short>& cGetBiomes() const;
|
||||
|
||||
// Light
|
||||
|
||||
inline glm::ivec4 getLight(unsigned int ind);
|
||||
inline unsigned char getLight(unsigned int ind, unsigned char channel);
|
||||
inline void setLight(unsigned int ind, unsigned char channel, unsigned char light);
|
||||
inline void setBlockLight(unsigned int ind, glm::ivec3 light);
|
||||
|
||||
inline unsigned char getSunlight(unsigned int ind);
|
||||
inline void setSunlight(unsigned int ind, unsigned char val);
|
||||
|
||||
|
||||
// Serialization
|
||||
|
||||
Packet serialize();
|
||||
void deserialize(PacketView& packet);
|
||||
|
||||
bool generated = false;
|
||||
bool shouldHaveMesh = true;
|
||||
bool dirty = true;
|
||||
glm::ivec3 pos;
|
||||
private:
|
||||
struct blocklight_bits {
|
||||
struct BlockLight {
|
||||
// 16 bits - 1 short
|
||||
unsigned char r: 5;
|
||||
unsigned char g: 5;
|
||||
unsigned char b: 5, :1;
|
||||
};
|
||||
struct sunlight_bits {
|
||||
|
||||
struct Sunlight {
|
||||
// 8 bits for two values - 1 char
|
||||
unsigned char a: 4;
|
||||
unsigned char b: 4;
|
||||
};
|
||||
|
||||
std::vector<unsigned int> blocks {};
|
||||
std::vector<unsigned short> biomes {};
|
||||
std::array <blocklight_bits, 4096> blocklight {};
|
||||
std::array <sunlight_bits, 2048> sunlight {};
|
||||
public:
|
||||
Chunk() = default;
|
||||
explicit Chunk(const std::vector<unsigned int>& blocks, const std::vector<unsigned short>& biomes);
|
||||
Chunk(const std::vector<unsigned int>& blocks, const std::vector<unsigned short>& biomes, glm::ivec3 pos);
|
||||
|
||||
inline unsigned int getBlock(unsigned int ind) const;
|
||||
bool setBlock(unsigned int ind, unsigned int blk);
|
||||
|
||||
inline unsigned int getBlock(const glm::ivec3& pos) const;
|
||||
inline bool setBlock(const glm::ivec3& pos, unsigned int blk);
|
||||
|
||||
inline unsigned short getBiome(unsigned int ind) const;
|
||||
inline bool setBiome(unsigned int ind, unsigned short bio);
|
||||
|
||||
inline unsigned short getBiome(const glm::ivec3& pos) const;
|
||||
inline bool setBiome(const glm::ivec3& pos, unsigned short bio);
|
||||
|
||||
const std::vector<unsigned int>& cGetBlocks() const;
|
||||
const std::vector<unsigned short>& cGetBiomes() const;
|
||||
|
||||
inline glm::ivec4 getLight(unsigned int ind);
|
||||
inline void setLight(unsigned int ind, glm::ivec4 light);
|
||||
|
||||
inline unsigned char getLight(unsigned int ind, unsigned char channel);
|
||||
inline void setLight(unsigned int ind, unsigned char channel, unsigned char light);
|
||||
|
||||
Packet serialize();
|
||||
void deserialize(PacketView& packet);
|
||||
|
||||
void recalculateRenderableBlocks();
|
||||
|
||||
bool partial = false;
|
||||
bool generated = false;
|
||||
|
||||
bool dirty = true;
|
||||
bool shouldHaveMesh = true;
|
||||
|
||||
glm::ivec3 pos;
|
||||
|
||||
private:
|
||||
std::vector<unsigned int> blocks {0, 0};
|
||||
std::vector<unsigned short> biomes {0, 0};
|
||||
|
||||
std::array<Sunlight, 2048> sunlight {};
|
||||
std::array<BlockLight, 4096> blocklight {};
|
||||
|
||||
bool empty = true;
|
||||
bool partial = false;
|
||||
unsigned short nonAirBlocks = 0;
|
||||
|
||||
friend class MapGen;
|
||||
void calcNonAirBlocks();
|
||||
inline unsigned char getSunlight(unsigned int ind);
|
||||
inline void setSunlight(unsigned int ind, unsigned char val);
|
||||
};
|
||||
|
||||
inline bool BlockChunk::setBlock(const glm::ivec3& pos, unsigned int blk) {
|
||||
inline bool Chunk::setBlock(const glm::ivec3& pos, unsigned int blk) {
|
||||
if (pos.x > 15 || pos.x < 0 || pos.y > 15 || pos.y < 0 || pos.z > 15 || pos.z < 0) return false;
|
||||
return setBlock(Space::Block::index(pos), blk);
|
||||
}
|
||||
|
||||
inline unsigned int BlockChunk::getBlock(unsigned int ind) const {
|
||||
inline unsigned int Chunk::getBlock(unsigned int ind) const {
|
||||
if (ind >= 4096) return DefinitionAtlas::INVALID;
|
||||
return RIE::read<unsigned int>(ind, blocks, 4096);
|
||||
}
|
||||
|
||||
inline unsigned int BlockChunk::getBlock(const glm::ivec3& pos) const {
|
||||
inline unsigned int Chunk::getBlock(const glm::ivec3& pos) const {
|
||||
if (pos.x > 15 || pos.x < 0 || pos.y > 15 || pos.y < 0 || pos.z > 15 || pos.z < 0) return DefinitionAtlas::INVALID;
|
||||
return getBlock(Space::Block::index(pos));
|
||||
}
|
||||
|
||||
inline bool BlockChunk::setBiome(unsigned int ind, unsigned short bio) {
|
||||
RIE::write(ind, bio, biomes, 4096);
|
||||
inline bool Chunk::setBiome(unsigned int ind, unsigned short bio) {
|
||||
return RIE::write(ind, bio, biomes, 4096);
|
||||
}
|
||||
|
||||
inline bool BlockChunk::setBiome(const glm::ivec3& pos, unsigned short bio) {
|
||||
inline bool Chunk::setBiome(const glm::ivec3& pos, unsigned short bio) {
|
||||
if (pos.x > 15 || pos.x < 0 || pos.y > 15 || pos.y < 0 || pos.z > 15 || pos.z < 0) return false;
|
||||
return setBiome(Space::Block::index(pos), bio);
|
||||
}
|
||||
|
||||
inline unsigned short BlockChunk::getBiome(unsigned int ind) const {
|
||||
inline unsigned short Chunk::getBiome(unsigned int ind) const {
|
||||
if (ind >= 4096) return BiomeAtlas::INVALID;
|
||||
return RIE::read<unsigned short>(ind, biomes, 4096);
|
||||
}
|
||||
|
||||
inline unsigned short BlockChunk::getBiome(const glm::ivec3& pos) const {
|
||||
inline unsigned short Chunk::getBiome(const glm::ivec3& pos) const {
|
||||
if (pos.x > 15 || pos.x < 0 || pos.y > 15 || pos.y < 0 || pos.z > 15 || pos.z < 0) return BiomeAtlas::INVALID;
|
||||
return getBiome(Space::Block::index(pos));
|
||||
}
|
||||
|
||||
inline glm::ivec4 BlockChunk::getLight(unsigned int ind) {
|
||||
inline glm::ivec4 Chunk::getLight(unsigned int ind) {
|
||||
return { blocklight[ind].r, blocklight[ind].g, blocklight[ind].b, getSunlight(ind) };
|
||||
}
|
||||
|
||||
inline unsigned char BlockChunk::getLight(unsigned int ind, unsigned char channel) {
|
||||
inline unsigned char Chunk::getLight(unsigned int ind, unsigned char channel) {
|
||||
return channel == 0 ? blocklight[ind].r :
|
||||
channel == 1 ? blocklight[ind].g :
|
||||
channel == 2 ? blocklight[ind].b :
|
||||
getSunlight(ind);
|
||||
}
|
||||
|
||||
inline void BlockChunk::setLight(unsigned int ind, unsigned char channel, unsigned char l) {
|
||||
channel == 0 ? blocklight[ind].r = l:
|
||||
channel == 1 ? blocklight[ind].g = l:
|
||||
channel == 2 ? blocklight[ind].b = l:
|
||||
inline void Chunk::setLight(unsigned int ind, unsigned char channel, unsigned char l) {
|
||||
channel == 0 ? blocklight[ind].r = l :
|
||||
channel == 1 ? blocklight[ind].g = l :
|
||||
channel == 2 ? blocklight[ind].b = l :
|
||||
(setSunlight(ind, l), 0);
|
||||
}
|
||||
|
||||
inline void BlockChunk::setBlockLight(unsigned int ind, glm::ivec3 l) {
|
||||
inline void Chunk::setLight(unsigned int ind, glm::ivec4 l) {
|
||||
blocklight[ind].r = l.x;
|
||||
blocklight[ind].g = l.y;
|
||||
blocklight[ind].b = l.z;
|
||||
setSunlight(ind, l.w);
|
||||
}
|
||||
|
||||
inline unsigned char BlockChunk::getSunlight(unsigned int ind) {
|
||||
inline unsigned char Chunk::getSunlight(unsigned int ind) {
|
||||
if (ind % 2 == 0) return sunlight[ind / 2].a;
|
||||
else return sunlight[ind / 2].b;
|
||||
}
|
||||
|
||||
inline void BlockChunk::setSunlight(unsigned int ind, unsigned char val) {
|
||||
inline void Chunk::setSunlight(unsigned int ind, unsigned char val) {
|
||||
if (ind % 2 == 0) sunlight[ind / 2].a = val;
|
||||
else sunlight[ind / 2].b = val;
|
||||
}
|
|
@ -11,11 +11,11 @@ MapBlock::MapBlock(glm::ivec3 pos) :
|
|||
for(unsigned short i = 0; i < 64; i++) blockChunks[i] = nullptr;
|
||||
}
|
||||
|
||||
std::shared_ptr<BlockChunk> MapBlock::operator[](unsigned short index) {
|
||||
std::shared_ptr<Chunk> MapBlock::operator[](unsigned short index) {
|
||||
return blockChunks[index];
|
||||
}
|
||||
|
||||
void MapBlock::set(unsigned short index, std::shared_ptr<BlockChunk> chunk) {
|
||||
void MapBlock::set(unsigned short index, std::shared_ptr<Chunk> chunk) {
|
||||
if (blockChunks[index] == nullptr) count++;
|
||||
blockChunks[index] = chunk;
|
||||
}
|
|
@ -8,19 +8,19 @@
|
|||
#include <memory>
|
||||
#include <glm/vec3.hpp>
|
||||
|
||||
#include "../chunk/BlockChunk.h"
|
||||
#include "Chunk.h"
|
||||
|
||||
class MapBlock {
|
||||
public:
|
||||
MapBlock(glm::ivec3 pos);
|
||||
|
||||
std::shared_ptr<BlockChunk> operator[](unsigned short index);
|
||||
void set(unsigned short index, std::shared_ptr<BlockChunk> chunk);
|
||||
std::shared_ptr<Chunk> operator[](unsigned short index);
|
||||
void set(unsigned short index, std::shared_ptr<Chunk> chunk);
|
||||
void remove(unsigned short index);
|
||||
|
||||
glm::ivec3 pos {};
|
||||
bool generated = false;
|
||||
unsigned short count = 0;
|
||||
private:
|
||||
std::array<std::shared_ptr<BlockChunk>, 64> blockChunks;
|
||||
std::array<std::shared_ptr<Chunk>, 64> blockChunks;
|
||||
};
|
|
@ -17,7 +17,7 @@ zepha.register_block("@aurailus:tnt:tnt", {
|
|||
for i = -amp, amp do
|
||||
for j = -amp, amp do
|
||||
for k = -amp, amp do
|
||||
if V{i, j, k}:distance(V{}) < amp then
|
||||
if V{i, j, k}:distance(V()) < amp then
|
||||
zepha.set_block(pos + offset, "air")
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,14 +4,14 @@
|
|||
|
||||
#include <catch2/catch.hpp>
|
||||
#include <iostream>
|
||||
#include "../../src/world/chunk/BlockChunk.h"
|
||||
#include "../../src/world/chunk/Chunk.h"
|
||||
|
||||
#pragma clang diagnostic push
|
||||
#pragma ide diagnostic ignored "OCUnusedGlobalDeclarationInspection"
|
||||
|
||||
TEST_CASE("BlockChunk", "[engine]") {
|
||||
TEST_CASE("Chunk", "[engine]") {
|
||||
// SECTION("Lighting") {
|
||||
// BlockChunk b;
|
||||
// Chunk b;
|
||||
//
|
||||
// b.setSunlight(1, 4);
|
||||
// b.setSunlight(2, 1);
|
||||
|
@ -49,7 +49,7 @@ TEST_CASE("BlockChunk", "[engine]") {
|
|||
SECTION("Blocks") {
|
||||
|
||||
SECTION("Exact index = 0, strip one after") {
|
||||
BlockChunk b {{0, 1, 1, 0}, {}};
|
||||
Chunk b {{0, 1, 1, 0}, {}};
|
||||
|
||||
b.setBlock(0, 2);
|
||||
|
||||
|
@ -61,7 +61,7 @@ TEST_CASE("BlockChunk", "[engine]") {
|
|||
}
|
||||
|
||||
SECTION("Exact index, last block same, strip one after") {
|
||||
BlockChunk b {{0, 1, 1, 0, 2, 5}, {}};
|
||||
Chunk b {{0, 1, 1, 0, 2, 5}, {}};
|
||||
|
||||
b.setBlock(1, 1);
|
||||
|
||||
|
@ -73,7 +73,7 @@ TEST_CASE("BlockChunk", "[engine]") {
|
|||
}
|
||||
|
||||
SECTION("Exact index, last block same, strip two after") {
|
||||
BlockChunk b {{0, 1, 2, 0, 4, 5}, {}};
|
||||
Chunk b {{0, 1, 2, 0, 4, 5}, {}};
|
||||
|
||||
b.setBlock(2, 1);
|
||||
|
||||
|
@ -97,7 +97,7 @@ TEST_CASE("BlockChunk", "[engine]") {
|
|||
}
|
||||
|
||||
SECTION("Exact index, last block same, no strip after") {
|
||||
BlockChunk b {{0, 1, 1, 0}, {}};
|
||||
Chunk b {{0, 1, 1, 0}, {}};
|
||||
|
||||
b.setBlock(1, 1);
|
||||
|
||||
|
@ -109,7 +109,7 @@ TEST_CASE("BlockChunk", "[engine]") {
|
|||
}
|
||||
|
||||
SECTION("Exact index, last block different, strip one after") {
|
||||
BlockChunk b {{0, 1, 1, 0, 2, 5}, {}};
|
||||
Chunk b {{0, 1, 1, 0, 2, 5}, {}};
|
||||
|
||||
b.setBlock(1, 2);
|
||||
|
||||
|
@ -123,7 +123,7 @@ TEST_CASE("BlockChunk", "[engine]") {
|
|||
}
|
||||
|
||||
SECTION("Exact index, last block different, strip two after") {
|
||||
BlockChunk b {{0, 1, 2, 0, 4, 5}, {}};
|
||||
Chunk b {{0, 1, 2, 0, 4, 5}, {}};
|
||||
|
||||
b.setBlock(2, 2);
|
||||
|
||||
|
@ -165,7 +165,7 @@ TEST_CASE("BlockChunk", "[engine]") {
|
|||
}
|
||||
|
||||
SECTION("Exact index, last block different, no strip after") {
|
||||
BlockChunk b {{0, 1, 1, 0}, {}};
|
||||
Chunk b {{0, 1, 1, 0}, {}};
|
||||
|
||||
b.setBlock(1, 2);
|
||||
|
||||
|
@ -179,7 +179,7 @@ TEST_CASE("BlockChunk", "[engine]") {
|
|||
}
|
||||
|
||||
SECTION("Greater index, last block same, strip one after") {
|
||||
BlockChunk b {{0, 1, 1, 0, 3, 5}, {}};
|
||||
Chunk b {{0, 1, 1, 0, 3, 5}, {}};
|
||||
|
||||
b.setBlock(2, 0);
|
||||
|
||||
|
@ -194,7 +194,7 @@ TEST_CASE("BlockChunk", "[engine]") {
|
|||
}
|
||||
|
||||
SECTION("Greater index, last block same, strip two after") {
|
||||
BlockChunk b {{0, 1, 2, 0, 5, 5}, {}};
|
||||
Chunk b {{0, 1, 2, 0, 5, 5}, {}};
|
||||
|
||||
b.setBlock(3, 0);
|
||||
|
||||
|
@ -209,7 +209,7 @@ TEST_CASE("BlockChunk", "[engine]") {
|
|||
}
|
||||
|
||||
SECTION("Greater index, last block same, no strip after") {
|
||||
BlockChunk b {{0, 1, 3, 0}, {}};
|
||||
Chunk b {{0, 1, 3, 0}, {}};
|
||||
|
||||
b.setBlock(1, 1);
|
||||
|
||||
|
@ -223,7 +223,7 @@ TEST_CASE("BlockChunk", "[engine]") {
|
|||
|
||||
SECTION("Greater index, last block different, strip one after") {
|
||||
SECTION("Indexed block different") {
|
||||
BlockChunk b {{0, 1, 1, 0, 3, 5}, {}};
|
||||
Chunk b {{0, 1, 1, 0, 3, 5}, {}};
|
||||
|
||||
b.setBlock(2, 2);
|
||||
|
||||
|
@ -239,7 +239,7 @@ TEST_CASE("BlockChunk", "[engine]") {
|
|||
}
|
||||
|
||||
SECTION("Indexed block same") {
|
||||
BlockChunk b {{0, 1, 1, 0, 3, 5}, {}};
|
||||
Chunk b {{0, 1, 1, 0, 3, 5}, {}};
|
||||
|
||||
b.setBlock(2, 5);
|
||||
|
||||
|
@ -254,7 +254,7 @@ TEST_CASE("BlockChunk", "[engine]") {
|
|||
}
|
||||
|
||||
SECTION("Greater index, last block different, strip three after") {
|
||||
BlockChunk b {{0, 1, 2, 0, 6, 6}, {}};
|
||||
Chunk b {{0, 1, 2, 0, 6, 6}, {}};
|
||||
|
||||
b.setBlock(3, 5);
|
||||
|
||||
|
@ -306,7 +306,7 @@ TEST_CASE("BlockChunk", "[engine]") {
|
|||
}
|
||||
|
||||
SECTION("No index found in loop, last block different, not end of chunk") {
|
||||
BlockChunk b {{0, 1, 2, 0}, {}};
|
||||
Chunk b {{0, 1, 2, 0}, {}};
|
||||
|
||||
b.setBlock(6, 6);
|
||||
|
||||
|
@ -322,7 +322,7 @@ TEST_CASE("BlockChunk", "[engine]") {
|
|||
}
|
||||
|
||||
SECTION("No index found in loop, last block different, end of chunk") {
|
||||
BlockChunk b {{0, 1, 2, 0}, {}};
|
||||
Chunk b {{0, 1, 2, 0}, {}};
|
||||
|
||||
b.setBlock(4095, 6);
|
||||
|
||||
|
|
Loading…
Reference in New Issue