Fix Low definition Blend Masking, Server deletes far MapBlocks.

* remove* commands in Dimension
* Remove grid from inventory - for testing
master
Nicole Collings 2020-02-13 11:36:18 -08:00
parent dad2f216fa
commit c209b2a372
24 changed files with 202 additions and 121 deletions

View File

@ -6,7 +6,7 @@
LocalDefinitionAtlas::LocalDefinitionAtlas(TextureAtlas& atlas) {
//Invalid Node
BlockModel invalidModel = BlockModel::createCube({atlas["_missing"]}, {false});
BlockModel invalidModel = BlockModel::createCube({atlas["_missing"]}, {}, {});
BlockDef* invalid = new BlockDef("invalid", 0, "Invalid (you broke the game!)", 1, invalidModel, invalidModel, true, {{}}, {{}});
defs.push_back(invalid);
defTable.insert({"invalid", 0});

View File

@ -7,7 +7,7 @@
ServerDefinitionAtlas::ServerDefinitionAtlas() {
//Invalid Node
BlockModel invalidModel = BlockModel::createCube({}, {false});
BlockModel invalidModel = BlockModel::createCube({}, {}, {});
BlockDef* invalid = new BlockDef("invalid", INVALID, "Invalid (you broke the game!)", 1, invalidModel, invalidModel, true, {{}}, {{}});
registerDef(invalid);

View File

@ -19,7 +19,9 @@ struct BlockModel {
bool culls = false;
bool visible = false;
static BlockModel createCube(std::vector<std::shared_ptr<AtlasRef>> textureRefs, std::vector<bool> biomeTints) {
static BlockModel createCube(std::vector<std::shared_ptr<AtlasRef>> textureRefs,
std::vector<unsigned int> blendInds, std::vector<std::shared_ptr<AtlasRef>> blendMaskRefs) {
BlockModel blockModel;
blockModel.visible = true;
blockModel.culls = true;
@ -28,75 +30,76 @@ struct BlockModel {
std::vector<unsigned int> indices {};
unsigned int accessInd;
for (auto& ref : textureRefs) blockModel.textureRefs.insert(ref);
for (auto& ref : textureRefs) if (ref != nullptr) blockModel.textureRefs.insert(ref);
if (textureRefs.empty()) textureRefs.emplace_back(nullptr);
if (biomeTints.empty()) biomeTints.emplace_back(false);
if (blendInds.empty()) blendInds.emplace_back(0);
if (blendMaskRefs.empty()) blendMaskRefs.emplace_back(nullptr);
//Left Face
vertices = {
{glm::vec3{0, 0, 0}, glm::vec3{}, glm::vec2{0, 1}, glm::vec2{}},
{glm::vec3{0, 0, 1}, glm::vec3{}, glm::vec2{1, 1}, glm::vec2{}},
{glm::vec3{0, 1, 1}, glm::vec3{}, glm::vec2{1, 0}, glm::vec2{}},
{glm::vec3{0, 1, 0}, glm::vec3{}, glm::vec2{0, 0}, glm::vec2{}}};
{{0, 0, 0}, {}, {0, 1}, {0, 1}},
{{0, 0, 1}, {}, {1, 1}, {1, 1}},
{{0, 1, 1}, {}, {1, 0}, {1, 0}},
{{0, 1, 0}, {}, {0, 0}, {0, 0}}};
indices = {0, 1, 2, 2, 3, 0};
accessInd = std::max(0, std::min(static_cast<int>(textureRefs.size() - 1), 2));
MeshPart leftMeshPart(vertices, indices, textureRefs[accessInd], biomeTints[accessInd], nullptr);
MeshPart leftMeshPart(vertices, indices, textureRefs[accessInd], blendInds[accessInd], blendMaskRefs[accessInd]);
blockModel.parts[static_cast<int>(Dir::LEFT)].push_back(leftMeshPart);
//Right Face
vertices = {
{glm::vec3{1, 1, 1}, glm::vec3{}, glm::vec2{1, 0}, glm::vec2{}},
{glm::vec3{1, 0, 1}, glm::vec3{}, glm::vec2{1, 1}, glm::vec2{}},
{glm::vec3{1, 0, 0}, glm::vec3{}, glm::vec2{0, 1}, glm::vec2{}},
{glm::vec3{1, 1, 0}, glm::vec3{}, glm::vec2{0, 0}, glm::vec2{}}};
{{1, 1, 1}, {}, {1, 0}, {1, 0}},
{{1, 0, 1}, {}, {1, 1}, {1, 1}},
{{1, 0, 0}, {}, {0, 1}, {0, 1}},
{{1, 1, 0}, {}, {0, 0}, {0, 0}}};
indices = {0, 1, 2, 2, 3, 0};
accessInd = std::max(0, std::min(static_cast<int>(textureRefs.size() - 1), 3));
MeshPart rightMeshPart(vertices, indices, textureRefs[accessInd], biomeTints[accessInd], nullptr);
MeshPart rightMeshPart(vertices, indices, textureRefs[accessInd], blendInds[accessInd], blendMaskRefs[accessInd]);
blockModel.parts[static_cast<int>(Dir::RIGHT)].push_back(rightMeshPart);
//Top Face
vertices = {
{glm::vec3{0, 1, 0}, glm::vec3{}, glm::vec2{0, 0}, glm::vec2{}},
{glm::vec3{0, 1, 1}, glm::vec3{}, glm::vec2{0, 1}, glm::vec2{}},
{glm::vec3{1, 1, 1}, glm::vec3{}, glm::vec2{1, 1}, glm::vec2{}},
{glm::vec3{1, 1, 0}, glm::vec3{}, glm::vec2{1, 0}, glm::vec2{}}};
{{0, 1, 0}, {}, {0, 0}, {0, 0}},
{{0, 1, 1}, {}, {0, 1}, {0, 1}},
{{1, 1, 1}, {}, {1, 1}, {1, 1}},
{{1, 1, 0}, {}, {1, 0}, {1, 0}}};
indices = {0, 1, 2, 2, 3, 0};
accessInd = std::max(0, std::min(static_cast<int>(textureRefs.size() - 1), 0));
MeshPart topMeshPart(vertices, indices, textureRefs[accessInd], biomeTints[accessInd], nullptr);
MeshPart topMeshPart(vertices, indices, textureRefs[accessInd], blendInds[accessInd], blendMaskRefs[accessInd]);
blockModel.parts[static_cast<int>(Dir::TOP)].push_back(topMeshPart);
//Bottom Face
vertices = {
{glm::vec3{0, 0, 0}, glm::vec3{}, glm::vec2{0, 0}, glm::vec2{}},
{glm::vec3{1, 0, 0}, glm::vec3{}, glm::vec2{1, 0}, glm::vec2{}},
{glm::vec3{1, 0, 1}, glm::vec3{}, glm::vec2{1, 1}, glm::vec2{}},
{glm::vec3{0, 0, 1}, glm::vec3{}, glm::vec2{0, 1}, glm::vec2{}}};
{{0, 0, 0}, {}, {0, 0}, {0, 0}},
{{1, 0, 0}, {}, {1, 0}, {1, 0}},
{{1, 0, 1}, {}, {1, 1}, {1, 1}},
{{0, 0, 1}, {}, {0, 1}, {0, 1}}};
indices = {0, 1, 2, 2, 3, 0};
accessInd = std::max(0, std::min(static_cast<int>(textureRefs.size() - 1), 1));
MeshPart bottomMeshPart(vertices, indices, textureRefs[accessInd], biomeTints[accessInd], nullptr);
MeshPart bottomMeshPart(vertices, indices, textureRefs[accessInd], blendInds[accessInd], blendMaskRefs[accessInd]);
blockModel.parts[static_cast<int>(Dir::BOTTOM)].push_back(bottomMeshPart);
//Front Face
vertices = {
{glm::vec3{0, 0, 1}, glm::vec3{}, glm::vec2{0, 1}, glm::vec2{}},
{glm::vec3{1, 0, 1}, glm::vec3{}, glm::vec2{1, 1}, glm::vec2{}},
{glm::vec3{1, 1, 1}, glm::vec3{}, glm::vec2{1, 0}, glm::vec2{}},
{glm::vec3{0, 1, 1}, glm::vec3{}, glm::vec2{0, 0}, glm::vec2{}}};
{{0, 0, 1}, {}, {0, 1}, {0, 1}},
{{1, 0, 1}, {}, {1, 1}, {1, 1}},
{{1, 1, 1}, {}, {1, 0}, {1, 0}},
{{0, 1, 1}, {}, {0, 0}, {0, 0}}};
indices = {0, 1, 2, 2, 3, 0};
accessInd = std::max(0, std::min(static_cast<int>(textureRefs.size() - 1), 4));
MeshPart frontMeshPart(vertices, indices, textureRefs[accessInd], biomeTints[accessInd], nullptr);
MeshPart frontMeshPart(vertices, indices, textureRefs[accessInd], blendInds[accessInd], blendMaskRefs[accessInd]);
blockModel.parts[static_cast<int>(Dir::FRONT)].push_back(frontMeshPart);
//Back Face
vertices = {
{glm::vec3{0, 0, 0}, glm::vec3{}, glm::vec2{0, 1}, glm::vec2{}},
{glm::vec3{0, 1, 0}, glm::vec3{}, glm::vec2{0, 0}, glm::vec2{}},
{glm::vec3{1, 1, 0}, glm::vec3{}, glm::vec2{1, 0}, glm::vec2{}},
{glm::vec3{1, 0, 0}, glm::vec3{}, glm::vec2{1, 1}, glm::vec2{}}};
{{0, 0, 0}, {}, {0, 1}, {0, 1}},
{{0, 1, 0}, {}, {0, 0}, {0, 0}},
{{1, 1, 0}, {}, {1, 0}, {1, 0}},
{{1, 0, 0}, {}, {1, 1}, {1, 1}}};
indices = {0, 1, 2, 2, 3, 0};
accessInd = std::max(0, std::min(static_cast<int>(textureRefs.size() - 1), 5));
MeshPart backMeshPart(vertices, indices, textureRefs[accessInd], biomeTints[accessInd], nullptr);
MeshPart backMeshPart(vertices, indices, textureRefs[accessInd], blendInds[accessInd], blendMaskRefs[accessInd]);
blockModel.parts[static_cast<int>(Dir::BACK)].push_back(backMeshPart);
return blockModel;

View File

@ -3,7 +3,6 @@
//
#include "MeshPart.h"
#include "../../util/Util.h"
MeshPart::MeshPart(const std::vector<BlockModelVertex>& vertices, const std::vector<unsigned int>& indices,
std::shared_ptr<AtlasRef> texture, unsigned int blendInd, std::shared_ptr<AtlasRef> blendMask) :
@ -48,7 +47,6 @@ MeshPart::MeshPart(const std::vector<BlockModelVertex>& vertices, const std::vec
vertex.tex.y = uv.y + ((uv.w - uv.y) * vertex.tex.y);
if (blendMask) {
std::cout << Util::floatVecToString({vertex.blendMask.x, vertex.blendMask.y, 0}) << std::endl;
auto bUV = blendMask->uv;
//Store the old positions in blendMaskUVs

View File

@ -87,7 +87,9 @@ void MeshGenStream::threadFunction(MeshGenStream::Thread *thread) {
for (Unit& u : thread->tasks) {
if (!u.busy) continue;
MeshGenerator m(u.meshDetails, thread->defs, u.thisChunk, u.adjacentChunks, thread->offsetSamplers);
// bool hi = glm::distance(glm::vec3{0, 0, 0}, glm::vec3(u.thisChunk->pos)) < 4;
MeshGenerator m(u.meshDetails, thread->defs, u.thisChunk, u.adjacentChunks, thread->offsetSamplers, true);
hasNoTasks = false;
u.busy = false;
}

View File

@ -5,17 +5,13 @@
#include <vector>
#include <glm/gtx/normal.hpp>
#include "../MeshDetails.h"
#include "../../../../world/chunk/BlockChunk.h"
//#include "../../../../def/item/MeshPart.h"
//#include "../../../../def/item/BlockModelVertex.h"
//#include "../../../../def/item/BlockDef.h"
//#include "../../../../util/Vec.h"
#include "../../../../util/Timer.h"
#include "../../../../world/chunk/BlockChunk.h"
#include "MeshGenerator.h"
MeshGenerator::MeshGenerator(MeshDetails* meshDetails, LocalDefs& defs, std::shared_ptr<BlockChunk> chunk,
std::array<std::shared_ptr<BlockChunk>, 6> adjacent,
std::array<NoiseSample, 3>& blockOffsets) :
std::array<NoiseSample, 3>& blockOffsets, bool hi) :
defs(defs),
chunk(chunk),
@ -33,7 +29,8 @@ MeshGenerator::MeshGenerator(MeshDetails* meshDetails, LocalDefs& defs, std::sha
glm::vec3 check;
for (unsigned short i = 0; i < 4096; i++) {
BlockModel& model = atlas.blockFromId(chunk->getBlock(i)).model;
BlockModel& model = hi ? atlas.blockFromId(chunk->getBlock(i)).model : atlas.blockFromId(chunk->getBlock(i)).farModel;
glm::vec3 biomeTint = defs.biomes.biomeFromId(chunk->getBiome(i)).biomeTint;
if (model.visible) {

View File

@ -19,7 +19,7 @@ class MeshGenerator {
public:
MeshGenerator(MeshDetails* meshDetails, LocalDefs& defs,
std::shared_ptr<BlockChunk> chunk, std::array<std::shared_ptr<BlockChunk>, 6> adjacent,
std::array<NoiseSample, 3>& blockOffsets);
std::array<NoiseSample, 3>& blockOffsets, bool hi);
private:
BlockDef& getBlockAt(const glm::ivec3 &pos);
void addFaces(const glm::vec3 &offset, const std::vector<MeshPart> &meshParts, const glm::vec3& tint);

View File

@ -25,6 +25,32 @@ namespace RegisterBlocks {
return boxes;
}
static inline void getMeshPartTexture(std::string& texture, unsigned int& blendInd, std::string& blendMask) {
if (strncmp(texture.data(), "tint(", 5) == 0 && texture.find_last_of(')') != std::string::npos) {
// Biome tinting time
texture.erase(std::remove(texture.begin(), texture.end(), ' '), texture.end());
std::string::size_type paramsBegin = texture.find_first_of('(');
std::string::size_type paramsEnd = texture.find_last_of(')');
std::string paramsString = texture.substr(paramsBegin + 1, paramsEnd - paramsBegin - 1);
std::vector<std::string> params;
std::string::size_type pos = 0;
while ((pos = paramsString.find(',')) != std::string::npos) {
params.push_back(paramsString.substr(0, pos));
paramsString.erase(0, pos + 1);
}
params.push_back(paramsString);
if (params.size() < 2) throw "Invalid biome tint values. Must have at least 2 params.";
texture = params[1];
blendInd = atoi(params[0].data()) + 1; //TODO: support multiple blend colors
blendMask = (params.size() >= 3 ? params[2] : "");
}
}
static std::pair<BlockModel, BlockModel> createBlockModel(sol::table blockTable, sol::table blockModels, TextureAtlas* atlas) {
// Get the specified block model
auto modelStr = blockTable.get_or<std::string>("model", "default:cube");
@ -119,34 +145,11 @@ namespace RegisterBlocks {
// Get the part's texture
int tex = std::max(static_cast<int>(meshPartTable.get_or<float>("tex", 1)), 1);
auto texture = textures[std::min(tex - 1, (int) textures.size() - 1)];
bool blendInd = false;
unsigned int blendInd = 0;
std::string blendMask = "";
if (strncmp(texture.data(), "tint(", 5) == 0 && texture.find_last_of(')') != std::string::npos) {
// Biome tinting time
texture.erase(std::remove(texture.begin(), texture.end(), ' '), texture.end());
std::string::size_type paramsBegin = texture.find_first_of('(');
std::string::size_type paramsEnd = texture.find_last_of(')');
std::string paramsString = texture.substr(paramsBegin + 1, paramsEnd - paramsBegin - 1);
std::vector<std::string> params;
std::string::size_type pos = 0;
while ((pos = paramsString.find(',')) != std::string::npos) {
params.push_back(paramsString.substr(0, pos));
paramsString.erase(0, pos + 1);
}
params.push_back(paramsString);
if (params.size() < 2) throw "Invalid biome tint values. Must have at least 2 params.";
texture = params[1];
blendInd = atoi(params[0].data()) + 1; //TODO: support multiple blend colors
blendMask = (params.size() >= 3 ? params[2] : "");
}
getMeshPartTexture(texture, blendInd, blendMask);
// Add texture refs to blockModel if the textures table is provided
std::shared_ptr<AtlasRef> textureRef = nullptr, blendMaskRef = nullptr;
@ -207,34 +210,36 @@ namespace RegisterBlocks {
model.parts[static_cast<int>(d)].push_back(meshPart);
});
// Create the low-def block model
BlockModel lowdefModel;
// Create the far model
BlockModel farModel;
auto ldRender = blockTable.get_or("lowdef_render", true);
if (atlas) {
std::vector<std::shared_ptr<AtlasRef>> refs;
std::vector<bool> biomeTints;
std::vector<std::shared_ptr<AtlasRef>> textureRefs;
std::vector<unsigned int> blendInds;
std::vector<std::shared_ptr<AtlasRef>> blendMaskRefs;
for (auto i = 0; i < lowdef_textures.size(); i++) {
std::string texture = lowdef_textures[i];
if (strncmp(texture.data(), "tint(", 5) == 0) {
texture = texture.substr(8, texture.length() - 8);
biomeTints.emplace_back(true);
}
else {
biomeTints.emplace_back(false);
}
refs.push_back((*atlas)[texture]);
unsigned int blendInd = 0;
std::string blendMask = "";
getMeshPartTexture(texture, blendInd, blendMask);
textureRefs.push_back((*atlas)[texture]);
blendInds.push_back(blendInd);
blendMaskRefs.push_back(blendMask != "" ? (*atlas)[blendMask] : nullptr);
}
lowdefModel = BlockModel::createCube(refs, biomeTints);
farModel = BlockModel::createCube(textureRefs, blendInds, blendMaskRefs);
}
else {
lowdefModel = BlockModel::createCube({}, {});
farModel = BlockModel::createCube({}, {}, {});
}
lowdefModel.culls = ldRender;
lowdefModel.visible = ldRender;
farModel.culls = ldRender;
farModel.visible = ldRender;
return {model, lowdefModel};
return {model, farModel};
}
static void registerBlocks(sol::table source, sol::table blockModels, DefinitionAtlas& defs, TextureAtlas* atlas) {

View File

@ -19,7 +19,7 @@ void ServerClient::initPlayer() {
player = new ServerPlayer({0, 0, 0}, connectID, "TEMPORARY");
}
bool ServerClient::hasPlayer() {
bool ServerClient::hasPlayer() const {
return player != nullptr;
}
@ -28,10 +28,16 @@ ServerPlayer& ServerClient::getPlayer() {
return *player;
}
const ServerPlayer &ServerClient::cGetPlayer() const {
if (!player) throw "getPlayer() called on client without player!";
return *player;
}
ENetPeer& ServerClient::getPeer() {
return *peer;
}
ServerClient::~ServerClient() {
delete player;
}
}

View File

@ -13,7 +13,8 @@ public:
void initPlayer();
bool hasPlayer();
bool hasPlayer() const;
const ServerPlayer& cGetPlayer() const;
ServerPlayer& getPlayer();
ENetPeer& getPeer();

View File

@ -40,7 +40,7 @@ float ServerPlayer::getAngle() {
return angle;
}
glm::vec3 ServerPlayer::getChunkPos() {
glm::ivec3 ServerPlayer::getChunkPos() const {
return Space::Chunk::world::fromBlock(pos);
}

View File

@ -19,7 +19,7 @@ public:
void setAngle(float angle);
glm::vec3 getPos();
glm::vec3 getChunkPos();
glm::ivec3 getChunkPos() const;
float getAngle();
glm::vec3 mapBlock;

View File

@ -6,9 +6,6 @@
#include <glm/glm.hpp>
#include "ServerWorld.h"
const static int MB_GEN_H = 6;
const static int MB_GEN_V = 4;
ServerWorld::ServerWorld(unsigned int seed, ServerDefs& defs, ServerClients& clients) :
clientList(clients),
seed(seed),
@ -37,6 +34,8 @@ void ServerWorld::init() {
}
void ServerWorld::update(double delta) {
dimension.update(clientList.clients);
while (!generateQueueList.empty()) {
auto it = generateQueueList.begin();
glm::vec3 pos = *it;
@ -51,10 +50,6 @@ void ServerWorld::update(double delta) {
auto finished = genStream->update();
generatedChunks = static_cast<int>(finished->size());
// TODO: Could be optimized if WorldGenStream passed structs for whole mapBlocks at a time, however
// that gets complicated quick when considering partials being passed back for other mapblocks,
// it might be worth the effort to reduce user list iterations, but it might not be.
for (const auto& chunk : *finished) {
dimension.setChunk(chunk);

View File

@ -18,6 +18,9 @@
class ServerWorld : public World {
public:
const static int MB_GEN_H = 6;
const static int MB_GEN_V = 4;
explicit ServerWorld(unsigned int seed, ServerDefs& defs, ServerClients& clients);
void init();
@ -32,7 +35,7 @@ private:
bool generateMapBlock(glm::ivec3 pos);
void sendChunk(const glm::ivec3& pos, ServerClient& client);
void sendChunk(const std::shared_ptr<BlockChunk>& chunk, ServerClient& client);
static void sendChunk(const std::shared_ptr<BlockChunk>& chunk, ServerClient& client);
void sendMapBlock(const glm::ivec3& pos, ServerClient& client);
static bool isInBounds(glm::ivec3 pos, std::pair<glm::ivec3, glm::ivec3>& bounds);

View File

@ -5,15 +5,28 @@
#include "Dimension.h"
std::shared_ptr<Region> Dimension::getRegion(glm::ivec3 regionPosition) {
if (!regions.count(regionPosition)) return nullptr;
return regions[regionPosition];
}
void Dimension::removeRegion(glm::ivec3 pos) {
regions.erase(pos);
}
std::shared_ptr<MapBlock> Dimension::getMapBlock(glm::ivec3 mapBlockPosition) {
auto region = getRegion(Space::Region::world::fromMapBlock(mapBlockPosition));
if (region == nullptr) return nullptr;
return (*region)[Space::MapBlock::index(mapBlockPosition)];
}
void Dimension::removeMapBlock(glm::ivec3 pos) {
auto region = getMapBlock(Space::Region::world::fromMapBlock(pos));
if (region == nullptr) return;
auto ind = Space::MapBlock::index(pos);
region->remove(ind);
if (region->count == 0) removeRegion(Space::Region::world::fromMapBlock(pos));
}
std::shared_ptr<BlockChunk> Dimension::getChunk(glm::ivec3 chunkPosition) {
auto mapBlock = getMapBlock(Space::MapBlock::world::fromChunk(chunkPosition));
if (mapBlock == nullptr) return nullptr;
@ -25,6 +38,14 @@ void Dimension::setChunk(std::shared_ptr<BlockChunk> chunk) {
(*mapBlock).set(Space::Chunk::index(chunk->pos), chunk);
}
void Dimension::removeChunk(glm::ivec3 pos){
auto mapBlock = getMapBlock(Space::MapBlock::world::fromChunk(pos));
if (mapBlock == nullptr) return;
auto ind = Space::Chunk::index(pos);
mapBlock->remove(ind);
if (mapBlock->count == 0) removeMapBlock(Space::MapBlock::world::fromChunk(pos));
}
unsigned int Dimension::getBlock(glm::ivec3 pos) {
auto chunk = getChunk(Space::Chunk::world::fromBlock(pos));
if (chunk != nullptr) return chunk->getBlock(Space::Block::relative::toChunk(pos));

View File

@ -7,7 +7,6 @@
#include <memory>
#include <glm/glm.hpp>
#include <unordered_map>
#include "chunk/BlockChunk.h"
#include "region/Region.h"
class Dimension {
@ -15,10 +14,14 @@ public:
Dimension() = default;
std::shared_ptr<Region> getRegion(glm::ivec3 regionPosition);
std::shared_ptr<MapBlock> getMapBlock(glm::ivec3 mapBlockPosition);
std::shared_ptr<BlockChunk> getChunk(glm::ivec3 chunkPosition);
void removeRegion(glm::ivec3 pos);
std::shared_ptr<MapBlock> getMapBlock(glm::ivec3 mapBlockPosition);
void removeMapBlock(glm::ivec3 pos);
std::shared_ptr<BlockChunk> getChunk(glm::ivec3 chunkPosition);
virtual void setChunk(std::shared_ptr<BlockChunk> chunk);
virtual void removeChunk(glm::ivec3 pos);
unsigned int getBlock(glm::ivec3 pos);
virtual bool setBlock(glm::ivec3 pos, unsigned int block);

View File

@ -2,8 +2,35 @@
// Created by aurailus on 01/10/19.
//
#include "../def/gen/MapGen.h"
#include "../server/conn/ServerClient.h"
#include "../server/world/ServerWorld.h"
#include "ServerDimension.h"
void ServerDimension::update(const std::vector<ServerClient*> &clients) {
for (const auto& region : regions) {
for (unsigned short i = 0; i < 64; i++) {
if (region.second->operator[](i) == nullptr) continue;
const auto& mapBlockPos = region.second->operator[](i)->pos;
bool clientNearby = false;
for (const auto& client : clients) {
if (client->hasPlayer()) {
auto clientPos = Space::MapBlock::world::fromChunk(client->cGetPlayer().getChunkPos());
if (abs(clientPos.x - mapBlockPos.x) <= ServerWorld::MB_GEN_H + 1
&& abs(clientPos.y - mapBlockPos.y) <= ServerWorld::MB_GEN_V + 1
&& abs(clientPos.z - mapBlockPos.z) <= ServerWorld::MB_GEN_H + 1) {
clientNearby = true;
break;
}
}
}
if (!clientNearby) region.second->remove(i);
}
}
}
bool ServerDimension::setBlock(glm::ivec3 pos, unsigned int block) {
bool manip = Dimension::setBlock(pos, block);
if (!manip) return false;
@ -42,4 +69,4 @@ unsigned long long ServerDimension::getMapBlockIntegrity(glm::ivec3 mapBlock) {
std::list<std::shared_ptr<ServerLuaEntity>> &ServerDimension::getLuaEntities() {
return luaEntities;
}
}

View File

@ -5,13 +5,16 @@
#pragma once
#include "Dimension.h"
#include "../def/gen/MapGen.h"
#include "../lua/api/type/ServerLuaEntity.h"
class ServerClient;
class ServerDimension : public Dimension {
public:
ServerDimension() = default;
void update(const std::vector<ServerClient*>& clients);
void setChunk(std::shared_ptr<BlockChunk> chunk) override;
bool setBlock(glm::ivec3 pos, unsigned int block) override;

View File

@ -7,15 +7,21 @@
MapBlock::MapBlock(glm::ivec3 pos) :
pos(pos) {
for (int i = 0; i < 64; i++) {
for (unsigned short i = 0; i < 64; i++) {
blockChunks[i] = nullptr;
}
}
std::shared_ptr<BlockChunk> MapBlock::operator[](int index) {
std::shared_ptr<BlockChunk> MapBlock::operator[](unsigned short index) {
return blockChunks[index];
}
void MapBlock::set(int index, std::shared_ptr<BlockChunk> chunk) {
void MapBlock::set(unsigned short index, std::shared_ptr<BlockChunk> chunk) {
blockChunks[index] = chunk;
}
count++;
}
void MapBlock::remove(unsigned short index) {
blockChunks[index] = nullptr;
count--;
}

View File

@ -14,11 +14,13 @@ class MapBlock {
public:
MapBlock(glm::ivec3 pos);
std::shared_ptr<BlockChunk> operator[](int index);
void set(int index, std::shared_ptr<BlockChunk> chunk);
std::shared_ptr<BlockChunk> operator[](unsigned short index);
void set(unsigned short index, std::shared_ptr<BlockChunk> chunk);
void remove(unsigned short index);
bool generated = false;
private:
glm::ivec3 pos {};
bool generated = false;
unsigned short count = 0;
private:
std::array<std::shared_ptr<BlockChunk>, 64> blockChunks;
};

View File

@ -7,15 +7,21 @@
Region::Region(glm::ivec3 pos) :
pos(pos) {
for (int i = 0; i < 64; i++) {
for (unsigned short i = 0; i < 64; i++) {
mapBlocks[i] = nullptr;
}
}
std::shared_ptr<MapBlock> Region::operator[](int index) {
std::shared_ptr<MapBlock> Region::operator[](unsigned short index) {
return mapBlocks[index];
}
void Region::set(int index, std::shared_ptr<MapBlock> block) {
void Region::set(unsigned short index, std::shared_ptr<MapBlock> block) {
mapBlocks[index] = block;
count++;
}
void Region::remove(unsigned short index) {
mapBlocks[index] = nullptr;
count--;
}

View File

@ -13,9 +13,12 @@ class Region {
public:
Region(glm::ivec3 pos);
std::shared_ptr<MapBlock> operator[](int index);
void set(int index, std::shared_ptr<MapBlock> block);
private:
std::shared_ptr<MapBlock> operator[](unsigned short index);
void set(unsigned short index, std::shared_ptr<MapBlock> block);
void remove(unsigned short index);
glm::ivec3 pos {};
unsigned short count = 0;
private:
std::array<std::shared_ptr<MapBlock>, 64> mapBlocks {};
};

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB