Create a new series of classes to store block definitions on the server.

* Added ServerBlockAtlas class
* Added ServerBlockDef class
* Added ServerBlockModel class
* Added ServerMeshPart class
* Added ServerDefs class
master
aurailus 2019-06-10 23:53:24 -07:00
parent 6a66282eea
commit eb66f9d3e6
63 changed files with 735 additions and 335 deletions

View File

@ -15,16 +15,16 @@ set(ZEUS_SRC_FILES
game/scene/world/graph/MeshGenerator.h
util/Timer.cpp
util/Timer.h
def/block/BlockAtlas.cpp
def/block/BlockAtlas.h
def/block/BlockDef.cpp
def/block/BlockDef.h
def/block/graph/MeshPart.cpp
def/block/graph/MeshPart.h
def/block/graph/MeshMod.h
def/block/client/LocalBlockAtlas.cpp
def/block/client/LocalBlockAtlas.h
def/block/client/LocalBlockDef.cpp
def/block/client/LocalBlockDef.h
def/block/client/LocalMeshPart.cpp
def/block/client/LocalMeshPart.h
def/block/MeshMod.h
game/scene/world/graph/MeshVertex.h
def/block/graph/BlockModel.cpp
def/block/graph/BlockModel.h
def/block/client/LocalBlockModel.cpp
def/block/client/LocalBlockModel.h
game/scene/world/LocalWorld.cpp
game/scene/world/LocalWorld.h
world/chunk/BlockChunk.cpp
@ -106,7 +106,7 @@ set(ZEUS_SRC_FILES
world/region/Region.h
game/entity/world/WireframeEntity.cpp
game/entity/world/WireframeEntity.h
def/block/graph/SelectionBox.h
def/block/SelectionBox.h
game/graph/drawable/Drawable.h
game/graph/drawable/DrawableGroup.cpp
game/graph/drawable/DrawableGroup.h
@ -117,8 +117,8 @@ set(ZEUS_SRC_FILES
def/texture/TextureAtlas.cpp
def/texture/TextureAtlas.h
def/texture/AtlasRef.h
world/region/MapBlock.h def/GameDefs.cpp
def/GameDefs.h
world/region/MapBlock.h def/LocalDefs.cpp
def/LocalDefs.h
api/func/LModuleUtil.cpp
api/func/LModuleUtil.h
world/region/Region.cpp
@ -126,6 +126,6 @@ set(ZEUS_SRC_FILES
game/entity/hud/StatGraph.cpp
game/entity/hud/StatGraph.h
util/Util.h
world/block/PointedThing.h game/entity/world/ParticleEntity.cpp game/entity/world/ParticleEntity.h)
world/block/PointedThing.h game/entity/world/ParticleEntity.cpp game/entity/world/ParticleEntity.h def/ServerDefs.cpp def/ServerDefs.h def/block/server/ServerBlockAtlas.cpp def/block/server/ServerBlockAtlas.h def/block/server/ServerBlockDef.cpp def/block/server/ServerBlockDef.h def/block/server/ServerBlockModel.cpp def/block/server/ServerBlockModel.h util/Dir.h def/block/server/ServerMeshPart.cpp def/block/server/ServerMeshPart.h)
add_library (zeusCore ${ZEUS_SRC_FILES})

View File

@ -3,11 +3,11 @@
//
#include "LuaApi.h"
#include "../def/GameDefs.h"
#include "../def/LocalDefs.h"
//todo: add security using doFileSandboxed
void LuaApi::init(GameDefs& defs) {
void LuaApi::init(LocalDefs& defs) {
lua.open_libraries(sol::lib::base, sol::lib::string, sol::lib::math);
lua.script_file("/home/aurailus/C++/GlProject/res/lua/util.lua");

View File

@ -12,13 +12,13 @@
#include "func/LModuleRegister.h"
#include "func/LModuleUtil.h"
class GameDefs;
class LocalDefs;
class LuaApi {
public:
LuaApi() = default;
void init(GameDefs& defs);
void init(LocalDefs& defs);
void loadMod(std::string file);

View File

@ -3,15 +3,15 @@
//
#include "LModuleRegister.h"
#include "../../def/GameDefs.h"
#include "../../def/LocalDefs.h"
LModuleRegister::LModuleRegister(sol::state &lua, sol::table &zeus, GameDefs &defs) {
LModuleRegister::LModuleRegister(sol::state &lua, sol::table &zeus, LocalDefs &defs) {
//Define registered_blockmodels table
zeus["registered_blockmodels"] = lua.create_table();
//
// # Register BlockModel
// # Register LocalBlockModel
// `zeus.register_blockmodel(string identifier, table definition)`
//
// Definition is stored as `zeus.registered_blockmodels[identifier] = definition`.
@ -97,8 +97,8 @@ LModuleRegister::LModuleRegister(sol::state &lua, sol::table &zeus, GameDefs &de
sbox = {{def[1], def[2], def[3]}, {def[4], def[5], def[6]}};
}
BlockModel blockModel = BlockModel::create(*model, texturesVector, defs.textures(), visible, culls);
BlockDef def(identifier, std::move(blockModel), solid, sbox);
LocalBlockModel blockModel = LocalBlockModel::create(*model, texturesVector, defs.textures(), visible, culls);
LocalBlockDef def(identifier, std::move(blockModel), solid, sbox);
defs.blocks().registerBlock(std::move(def));
}

View File

@ -5,16 +5,16 @@
#ifndef ZEUS_LREGISTERBLOCK_H
#define ZEUS_LREGISTERBLOCK_H
#include "../../def/block/graph/SelectionBox.h"
#include "../../def/block/BlockDef.h"
#include "../../def/block/BlockAtlas.h"
#include "../../def/block/SelectionBox.h"
#include "../../def/block/client/LocalBlockDef.h"
#include "../../def/block/client/LocalBlockAtlas.h"
#include <sol.hpp>
class GameDefs;
class LocalDefs;
class LModuleRegister {
public:
LModuleRegister(sol::state& lua, sol::table& zeus, GameDefs& defs);
LModuleRegister(sol::state& lua, sol::table& zeus, LocalDefs& defs);
};

View File

@ -4,7 +4,7 @@
#include "LModuleUtil.h"
LModuleUtil::LModuleUtil(sol::state &lua, sol::table &zeus, GameDefs &defs) {
LModuleUtil::LModuleUtil(sol::state &lua, sol::table &zeus, LocalDefs &defs) {
zeus.set_function("get_path", [&](std::string modname) {
try {
if (modname.length() == 0) throw "No mod name specified!";

View File

@ -5,16 +5,16 @@
#ifndef ZEUS_LMODULEUTIL_H
#define ZEUS_LMODULEUTIL_H
#include "../../def/block/graph/SelectionBox.h"
#include "../../def/block/BlockDef.h"
#include "../../def/block/BlockAtlas.h"
#include "../../def/block/SelectionBox.h"
#include "../../def/block/client/LocalBlockDef.h"
#include "../../def/block/client/LocalBlockAtlas.h"
#include <sol.hpp>
class GameDefs;
class LocalDefs;
class LModuleUtil {
public:
LModuleUtil(sol::state& lua, sol::table& zeus, GameDefs& defs);
LModuleUtil(sol::state& lua, sol::table& zeus, LocalDefs& defs);
};

View File

@ -2,24 +2,24 @@
// Created by aurailus on 18/04/19.
//
#include "GameDefs.h"
#include "LocalDefs.h"
GameDefs::GameDefs(std::string tex_path) {
LocalDefs::LocalDefs(std::string tex_path) {
textureAtlas = TextureAtlas(1024);
textureAtlas.loadDirectory(tex_path + "/game");
textureAtlas.loadDirectory(tex_path + "/gui");
textureAtlas.loadDirectory(tex_path + "/ent");
blockAtlas = BlockAtlas();
blockAtlas = LocalBlockAtlas();
luaApi.init(*this);
luaApi.loadMod("../res/lua/default/init.lua");
}
BlockAtlas &GameDefs::blocks() {
LocalBlockAtlas &LocalDefs::blocks() {
return blockAtlas;
}
TextureAtlas &GameDefs::textures() {
TextureAtlas &LocalDefs::textures() {
return textureAtlas;
}

View File

@ -6,21 +6,21 @@
#define ZEUS_GAMEDEFS_H
#include "texture/TextureAtlas.h"
#include "block/BlockAtlas.h"
#include "block/client/LocalBlockAtlas.h"
#include "../api/LuaApi.h"
class GameDefs {
class LocalDefs {
public:
GameDefs() = default;
explicit GameDefs(std::string tex_path);
LocalDefs() = default;
explicit LocalDefs(std::string tex_path);
BlockAtlas& blocks();
LocalBlockAtlas& blocks();
TextureAtlas& textures();
~GameDefs() = default;
~LocalDefs() = default;
private:
TextureAtlas textureAtlas;
BlockAtlas blockAtlas;
LocalBlockAtlas blockAtlas;
LuaApi luaApi;
};

17
src/def/ServerDefs.cpp Normal file
View File

@ -0,0 +1,17 @@
//
// Created by aurailus on 10/06/19.
//
#include "ServerDefs.h"
ServerDefs::ServerDefs() {
blockAtlas = ServerBlockAtlas();
// luaApi.init(*this);
// luaApi.loadMod("../res/lua/default/init.lua");
}
ServerBlockAtlas &ServerDefs::blocks() {
return blockAtlas;
}

24
src/def/ServerDefs.h Normal file
View File

@ -0,0 +1,24 @@
//
// Created by aurailus on 10/06/19.
//
#ifndef ZEUS_SERVERDEFS_H
#define ZEUS_SERVERDEFS_H
#include "block/server/ServerBlockAtlas.h"
#include "../api/LuaApi.h"
class ServerDefs {
public:
ServerDefs();
ServerBlockAtlas& blocks();
~ServerDefs() = default;
private:
ServerBlockAtlas blockAtlas;
// LuaApi luaApi;
};
#endif //ZEUS_SERVERDEFS_H

View File

@ -1,27 +0,0 @@
//
// Created by aurailus on 02/12/18.
//
#include "BlockAtlas.h"
BlockAtlas::BlockAtlas() {
//Register Air Node
BlockModel nullModel;
nullModel.visible = false;
nullModel.culls = false;
BlockDef air("builtin:air", nullModel, false, {{0, 0, 0}, {1, 1, 1}});
registerBlock(std::move(air));
}
void BlockAtlas::registerBlock(BlockDef def) {
definitions.push_back(def);
}
BlockDef& BlockAtlas::getBlock(int id) {
if (id >= 0 && id < definitions.size()) {
return definitions.at((unsigned long)id);
}
std::cerr << "Block ID #" << id << " (undefined) requested! Returning air." << std::endl;
return definitions.at(0);
}

View File

@ -1,24 +0,0 @@
//
// Created by aurailus on 02/12/18.
//
#ifndef GLPROJECT_BLOCKATLAS_H
#define GLPROJECT_BLOCKATLAS_H
#include <vector>
#include "BlockDef.h"
#include "../texture/TextureAtlas.h"
class BlockAtlas {
public:
BlockAtlas();
void registerBlock(BlockDef def);
BlockDef& getBlock(int id);
private:
std::vector<BlockDef> definitions;
};
#endif //GLPROJECT_BLOCKATLAS_H

View File

@ -1,32 +0,0 @@
//
// Created by aurailus on 02/12/18.
//
#include "BlockDef.h"
BlockModel& BlockDef::getModel() {
return model;
}
BlockDef::BlockDef(std::string identifier, BlockModel model, bool solid, SelectionBox selectionBox) :
identifier(std::move(identifier)),
culls(model.culls),
model(std::move(model)),
solid(solid),
selectionBox(selectionBox) {}
bool BlockDef::isSolid() {
return solid;
}
std::string BlockDef::getIdentifier() {
return identifier;
}
bool BlockDef::isCulling() {
return culls;
}
SelectionBox BlockDef::getSelectionBox() {
return selectionBox;
}

View File

@ -10,7 +10,7 @@
#include <iostream>
#include <vec3.hpp>
#include <glm.hpp>
#include "../../../util/Util.h"
#include "../../util/Util.h"
class SelectionBox {
public:

View File

@ -0,0 +1,34 @@
//
// Created by aurailus on 02/12/18.
//
#include "LocalBlockAtlas.h"
LocalBlockAtlas::LocalBlockAtlas() {
//Register Air Node
LocalBlockModel nullModel; nullModel.visible = false, nullModel.culls = false;
LocalBlockDef air("builtin:air", nullModel, false, {{0, 0, 0}, {1, 1, 1}});
registerBlock(std::move(air));
}
void LocalBlockAtlas::registerBlock(LocalBlockDef def) {
definitions.push_back(def);
}
LocalBlockDef& LocalBlockAtlas::fromIndex(int id) {
if (id >= 0 && id < definitions.size()) {
return definitions.at((unsigned long)id);
}
std::cerr << "Block ID #" << id << " (undefined) requested! Returning air." << std::endl;
return definitions.at(0);
}
LocalBlockDef &LocalBlockAtlas::fromIdentifier(std::string identifier) {
if (identifierIndexTable.count(identifier) > 0) {
return definitions.at(static_cast<unsigned long>(identifierIndexTable.at(identifier)));
}
std::cerr << "Block Identifier \"" << identifier << "\" (undefined) requested! Returning air." << std::endl;
return definitions.at(0);
}

View File

@ -0,0 +1,29 @@
//
// Created by aurailus on 02/12/18.
//
#ifndef GLPROJECT_BLOCKATLAS_H
#define GLPROJECT_BLOCKATLAS_H
#include <vector>
#include "LocalBlockDef.h"
#include "../../texture/TextureAtlas.h"
class LocalBlockAtlas {
public:
LocalBlockAtlas();
void registerBlock(LocalBlockDef def);
LocalBlockDef& fromIndex(int id);
LocalBlockDef& fromIdentifier(std::string identifier);
private:
const static int AIR = 0;
std::vector<LocalBlockDef> definitions;
std::unordered_map<std::string, int> identifierIndexTable;
};
#endif //GLPROJECT_BLOCKATLAS_H

View File

@ -0,0 +1,32 @@
//
// Created by aurailus on 02/12/18.
//
#include "LocalBlockDef.h"
LocalBlockModel& LocalBlockDef::getModel() {
return model;
}
LocalBlockDef::LocalBlockDef(std::string identifier, LocalBlockModel model, bool solid, SelectionBox selectionBox) :
identifier(std::move(identifier)),
culls(model.culls),
model(std::move(model)),
solid(solid),
selectionBox(selectionBox) {}
bool LocalBlockDef::isSolid() {
return solid;
}
std::string LocalBlockDef::getIdentifier() {
return identifier;
}
bool LocalBlockDef::isCulling() {
return culls;
}
SelectionBox LocalBlockDef::getSelectionBox() {
return selectionBox;
}

View File

@ -6,24 +6,24 @@
#define GLPROJECT_BLOCKDEF_H
#include <string>
#include "graph/BlockModel.h"
#include "graph/SelectionBox.h"
#include "LocalBlockModel.h"
#include "../SelectionBox.h"
class BlockDef {
class LocalBlockDef {
public:
BlockDef() = default;
BlockDef(std::string identifier, BlockModel model, bool solid, SelectionBox selectionBox);
LocalBlockDef() = default;
LocalBlockDef(std::string identifier, LocalBlockModel model, bool solid, SelectionBox selectionBox);
bool isCulling();
bool isSolid();
BlockModel& getModel();
LocalBlockModel& getModel();
std::string getIdentifier();
SelectionBox getSelectionBox();
private:
bool culls = false;
bool solid = false;
SelectionBox selectionBox;
BlockModel model;
LocalBlockModel model;
std::string identifier;
};

View File

@ -0,0 +1,87 @@
//
// Created by aurailus on 04/12/18.
//
#include "LocalBlockModel.h"
LocalBlockModel LocalBlockModel::create(sol::table model, std::vector<std::string> textures, TextureAtlas& atlas, bool visible, bool culls) {
LocalBlockModel blockModel;
try {
blockModel.culls = culls;
blockModel.visible = visible;
//Convert all of the mesh parts to C++ Objects and add them to blockModel
model.for_each([&](sol::object key, sol::object value) {
//Make sure LocalMeshPart is in fact a table
if (!value.is<sol::table>()) throw "Meshpart is not a table";
sol::table meshPartTable = value.as<sol::table>();
//Get The points table, and make sure it's valid
auto points_optional = meshPartTable.get<sol::optional<sol::table>>("points");
if (!points_optional) throw "Meshpart is missing a points table";
sol::table points = *points_optional;
if (points.size() % 20 != 0) throw "Points array is ill-formed. (Not a multiple of 20 values)";
//Populate the Vertices and Indices vectors from the points table
std::vector<MeshVertex> vertices;
std::vector<unsigned int> indices;
for (int i = 1; i <= points.size()/5; i++) {
int offset = (i - 1) * 5 + 1;
glm::vec3 pos(points[offset], points[offset + 1], points[offset + 2]);
glm::vec2 tex(points[offset + 3], points[offset + 4]);
vertices.push_back({pos, {0, 0, 0}, tex, {0, 0}});
}
int ind = 0;
for (int i = 1; i <= points.size() / 20; i++) {
indices.push_back(ind);
indices.push_back(ind + 1);
indices.push_back(ind + 2);
indices.push_back(ind + 2);
indices.push_back(ind + 3);
indices.push_back(ind);
ind += 4;
}
//Get the texture for the part
int tex = std::max((int)meshPartTable.get_or<float>("tex", 1), 1);
auto texture = textures[std::min(tex - 1, (int)textures.size() - 1)];
auto textureRef = atlas.getTextureRef(texture);
//Add a reference to the texture to the blockModel's list of required textures to keep it in memory.
blockModel.textureRefs.insert(textureRef);
//Create a LocalMeshPart object
LocalMeshPart meshPart(std::move(vertices), std::move(indices), textureRef);
//Add the meshpart to the proper face vector
std::string face = meshPartTable.get_or<std::string>("face", "nocull");
Dir d = face == "top" ? TOP :
face == "bottom" ? BOTTOM :
face == "left" ? LEFT :
face == "right" ? RIGHT :
face == "front" ? FRONT :
face == "back" ? BACK :
face == "nocull" ? NO_CULL :
INVALID;
if (d == INVALID)
throw "Face value \"" + face + "\" is not one of 'top', 'bottom', 'left', 'right', 'front', 'back', 'nocull'.";
blockModel.parts[d].push_back(meshPart);
});
}
catch (const std::string& e) {
std::cerr << "Exception on LocalBlockModel constructor: " << e << std::endl;
}
return std::move(blockModel);
}

View File

@ -0,0 +1,26 @@
//
// Created by aurailus on 04/12/18.
//
#ifndef GLPROJECT_BLOCKMODEL_H
#define GLPROJECT_BLOCKMODEL_H
#include <vector>
#include <sol.hpp>
#include <set>
#include "LocalMeshPart.h"
#include "../../../game/scene/world/graph/MeshVertex.h"
#include "../../../util/Dir.h"
struct LocalBlockModel {
std::array<std::vector<LocalMeshPart>, 7> parts;
std::set<std::shared_ptr<AtlasRef>> textureRefs;
bool culls;
bool visible;
static LocalBlockModel create(sol::table model, std::vector<std::string> textures, TextureAtlas& atlas, bool visible, bool culls);
};
#endif //GLPROJECT_BLOCKMODEL_H

View File

@ -2,9 +2,9 @@
// Created by aurailus on 02/12/18.
//
#include "MeshPart.h"
#include "LocalMeshPart.h"
MeshPart::MeshPart(std::vector<MeshVertex> vertices, std::vector<unsigned int> indices, std::shared_ptr<AtlasRef> texture) {
LocalMeshPart::LocalMeshPart(std::vector<MeshVertex> vertices, std::vector<unsigned int> indices, std::shared_ptr<AtlasRef> texture) {
this->vertices = std::move(vertices);
this->indices = std::move(indices);

View File

@ -14,12 +14,12 @@
#include <vec4.hpp>
#include <gtx/normal.hpp>
#include "MeshMod.h"
#include "../MeshMod.h"
#include "../../../game/scene/world/graph/MeshVertex.h"
#include "../../texture/TextureAtlas.h"
struct MeshPart {
MeshPart(std::vector<MeshVertex> vertices, std::vector<unsigned int> indices, std::shared_ptr<AtlasRef> texture);
struct LocalMeshPart {
LocalMeshPart(std::vector<MeshVertex> vertices, std::vector<unsigned int> indices, std::shared_ptr<AtlasRef> texture);
std::vector<MeshVertex> vertices;
std::vector<unsigned int> indices;

View File

@ -1,77 +0,0 @@
//
// Created by aurailus on 04/12/18.
//
#include "BlockModel.h"
//TODO: Refactor to not take lua properties to allow for unit tests
BlockModel BlockModel::create(sol::table model, std::vector<std::string> textures, TextureAtlas& atlas, bool visible, bool culls) {
BlockModel blockModel;
try {
blockModel.culls = culls;
blockModel.visible = visible;
model.for_each([&](sol::object key, sol::object value) {
if (!value.is<sol::table>()) throw "Meshpart is not a table";
sol::table meshPart = value.as<sol::table>();
//This value is sanitized later
std::string face = meshPart.get_or<std::string>("face", "nocull");
int tex = std::max((int)meshPart.get_or<float>("tex", 1), 1);
auto oPoints = meshPart.get<sol::optional<sol::table>>("points");
if (!oPoints) throw "Meshpart is missing points field";
sol::table points = *oPoints;
int texturesLength = textures.size();
std::vector<MeshVertex> vertices;
std::vector<unsigned int> indices;
if (points.size() % 20 != 0) throw "Points array is not well formed. (Not a multiple of 20 values)";
for (int i = 1; i <= points.size()/5; i++) {
int offset = (i - 1) * 5 + 1;
glm::vec3 pos(points[offset], points[offset + 1], points[offset + 2]);
glm::vec2 tex(points[offset + 3], points[offset + 4]);
vertices.push_back({pos, {0, 0, 0}, tex, {0, 0}});
}
int ind = 0;
for (int i = 1; i <= points.size() / 20; i++) {
indices.push_back(ind);
indices.push_back(ind + 1);
indices.push_back(ind + 2);
indices.push_back(ind + 2);
indices.push_back(ind + 3);
indices.push_back(ind);
ind += 4;
}
auto texture = textures[std::min(tex - 1, (int)textures.size() - 1)];
auto textureRef = atlas.getTextureRef(texture);
blockModel.textureRefs.insert(textureRef);
MeshPart partObject(std::move(vertices), std::move(indices), textureRef);
if (face == "top") blockModel.topFaces.push_back(partObject);
else if (face == "bottom") blockModel.bottomFaces.push_back(partObject);
else if (face == "left") blockModel.leftFaces.push_back(partObject);
else if (face == "right") blockModel.rightFaces.push_back(partObject);
else if (face == "front") blockModel.frontFaces.push_back(partObject);
else if (face == "back") blockModel.backFaces.push_back(partObject);
else if (face == "nocull") blockModel.noCulledFaces.push_back(partObject);
else throw "Face value is not one of 'top', 'bottom', 'left', 'right', 'front', 'back', 'nocull'.";
});
}
catch (const std::string& e) {
std::cerr << "Exception on BlockModel constructor: " << e << std::endl;
}
return std::move(blockModel);
}

View File

@ -1,32 +0,0 @@
//
// Created by aurailus on 04/12/18.
//
#ifndef GLPROJECT_BLOCKMODEL_H
#define GLPROJECT_BLOCKMODEL_H
#include <vector>
#include <sol.hpp>
#include <set>
#include "MeshPart.h"
#include "../../../game/scene/world/graph/MeshVertex.h"
struct BlockModel {
std::vector<MeshPart> leftFaces;
std::vector<MeshPart> rightFaces;
std::vector<MeshPart> topFaces;
std::vector<MeshPart> bottomFaces;
std::vector<MeshPart> frontFaces;
std::vector<MeshPart> backFaces;
std::vector<MeshPart> noCulledFaces;
std::set<std::shared_ptr<AtlasRef>> textureRefs;
bool culls;
bool visible;
static BlockModel create(sol::table model, std::vector<std::string> textures, TextureAtlas& atlas, bool visible, bool culls);
};
#endif //GLPROJECT_BLOCKMODEL_H

View File

@ -0,0 +1,34 @@
//
// Created by aurailus on 10/06/19.
//
#include "ServerBlockAtlas.h"
ServerBlockAtlas::ServerBlockAtlas() {
//Register Air Node
ServerBlockModel nullModel; nullModel.visible = false, nullModel.culls = false;
ServerBlockDef air("builtin:air", nullModel, false, {{0, 0, 0}, {1, 1, 1}});
registerBlock(std::move(air));
}
void ServerBlockAtlas::registerBlock(ServerBlockDef def) {
definitions.push_back(def);
}
ServerBlockDef& ServerBlockAtlas::fromIndex(int id) {
if (id >= 0 && id < definitions.size()) {
return definitions.at((unsigned long)id);
}
std::cerr << "Block ID #" << id << " (undefined) requested! Returning air." << std::endl;
return definitions.at(0);
}
ServerBlockDef &ServerBlockAtlas::fromIdentifier(std::string identifier) {
if (identifierIndexTable.count(identifier) > 0) {
return definitions.at(static_cast<unsigned long>(identifierIndexTable.at(identifier)));
}
std::cerr << "Block Identifier \"" << identifier << "\" (undefined) requested! Returning air." << std::endl;
return definitions.at(0);
}

View File

@ -0,0 +1,29 @@
//
// Created by aurailus on 10/06/19.
//
#ifndef ZEUS_SERVERBLOCKATLAS_H
#define ZEUS_SERVERBLOCKATLAS_H
#include <string>
#include <vector>
#include <iostream>
#include <unordered_map>
#include "ServerBlockDef.h"
class ServerBlockAtlas {
public:
ServerBlockAtlas();
void registerBlock(ServerBlockDef def);
ServerBlockDef& fromIndex(int id);
ServerBlockDef& fromIdentifier(std::string identifier);
private:
const static int AIR = 0;
std::vector<ServerBlockDef> definitions;
std::unordered_map<std::string, int> identifierIndexTable;
};
#endif //ZEUS_SERVERBLOCKATLAS_H

View File

@ -0,0 +1,32 @@
//
// Created by aurailus on 10/06/19.
//
#include "ServerBlockDef.h"
ServerBlockModel& ServerBlockDef::getModel() {
return model;
}
ServerBlockDef::ServerBlockDef(std::string identifier, ServerBlockModel model, bool solid, SelectionBox selectionBox) :
identifier(std::move(identifier)),
solid(solid),
culls(model.culls),
model(std::move(model)),
selectionBox(selectionBox) {}
bool ServerBlockDef::isSolid() {
return solid;
}
std::string ServerBlockDef::getIdentifier() {
return identifier;
}
bool ServerBlockDef::isCulling() {
return culls;
}
SelectionBox ServerBlockDef::getSelectionBox() {
return selectionBox;
}

View File

@ -0,0 +1,35 @@
//
// Created by aurailus on 10/06/19.
//
#ifndef ZEUS_SERVERBLOCKDEF_H
#define ZEUS_SERVERBLOCKDEF_H
#include <string>
#include "../SelectionBox.h"
#include "ServerBlockModel.h"
class ServerBlockDef {
public:
ServerBlockDef() = default;
ServerBlockDef(std::string identifier, ServerBlockModel model, bool solid, SelectionBox selectionBox);
std::string getIdentifier();
bool isCulling();
bool isSolid();
ServerBlockModel& getModel();
SelectionBox getSelectionBox();
private:
std::string identifier;
bool culls = false;
bool solid = false;
ServerBlockModel model;
SelectionBox selectionBox;
};
#endif //ZEUS_SERVERBLOCKDEF_H

View File

@ -0,0 +1,82 @@
//
// Created by aurailus on 10/06/19.
//
#include "ServerBlockModel.h"
ServerBlockModel ServerBlockModel::create(sol::table model, std::vector<std::string> textures, bool visible, bool culls) {
ServerBlockModel blockModel;
try {
blockModel.culls = culls;
blockModel.visible = visible;
//Convert all of the mesh parts to C++ Objects and add them to blockModel
model.for_each([&](sol::object key, sol::object value) {
//Make sure LocalMeshPart is in fact a table
if (!value.is<sol::table>()) throw "Meshpart is not a table";
sol::table meshPartTable = value.as<sol::table>();
//Get The points table, and make sure it's valid
auto points_optional = meshPartTable.get<sol::optional<sol::table>>("points");
if (!points_optional) throw "Meshpart is missing a points table";
sol::table points = *points_optional;
if (points.size() % 20 != 0) throw "Points array is ill-formed. (Not a multiple of 20 values)";
//Populate the Vertices and Indices vectors from the points table
std::vector<MeshVertex> vertices;
std::vector<unsigned int> indices;
for (int i = 1; i <= points.size()/5; i++) {
int offset = (i - 1) * 5 + 1;
glm::vec3 pos(points[offset], points[offset + 1], points[offset + 2]);
glm::vec2 tex(points[offset + 3], points[offset + 4]);
vertices.push_back({pos, {0, 0, 0}, tex, {0, 0}});
}
int ind = 0;
for (int i = 1; i <= points.size() / 20; i++) {
indices.push_back(ind);
indices.push_back(ind + 1);
indices.push_back(ind + 2);
indices.push_back(ind + 2);
indices.push_back(ind + 3);
indices.push_back(ind);
ind += 4;
}
//Get the texture string for the part
int tex = std::max((int)meshPartTable.get_or<float>("tex", 1), 1);
auto texture = textures[std::min(tex - 1, (int)textures.size() - 1)];
//Create a ServerMeshPart object
ServerMeshPart meshPart(std::move(vertices), std::move(indices), texture);
//Add the meshpart to the proper face vector
std::string face = meshPartTable.get_or<std::string>("face", "nocull");
Dir d = face == "top" ? TOP :
face == "bottom" ? BOTTOM :
face == "left" ? LEFT :
face == "right" ? RIGHT :
face == "front" ? FRONT :
face == "back" ? BACK :
face == "nocull" ? NO_CULL :
INVALID;
if (d == INVALID)
throw "Face value is not one of 'top', 'bottom', 'left', 'right', 'front', 'back', 'nocull'.";
blockModel.parts[d].push_back(meshPart);
});
}
catch (const std::string& e) {
std::cerr << "Exception on LocalBlockModel constructor: " << e << std::endl;
}
return std::move(blockModel);
}

View File

@ -0,0 +1,25 @@
//
// Created by aurailus on 10/06/19.
//
#ifndef ZEUS_SERVERBLOCKMODEL_H
#define ZEUS_SERVERBLOCKMODEL_H
#include <vector>
#include <sol.hpp>
#include <set>
#include "ServerMeshPart.h"
#include "../../../game/scene/world/graph/MeshVertex.h"
#include "../../../util/Dir.h"
struct ServerBlockModel {
std::array<std::vector<ServerMeshPart>, 7> parts;
std::set<std::shared_ptr<AtlasRef>> textureRefs;
bool culls;
bool visible;
static ServerBlockModel create(sol::table model, std::vector<std::string> textures, bool visible, bool culls);
};
#endif //ZEUS_SERVERBLOCKMODEL_H

View File

@ -0,0 +1,44 @@
//
// Created by aurailus on 10/06/19.
//
#include "ServerMeshPart.h"
ServerMeshPart::ServerMeshPart(std::vector<MeshVertex> vertices, std::vector<unsigned int> indices, std::string texture) {
this->vertices = std::move(vertices);
this->indices = std::move(indices);
this->meshMod = NONE;
this->modValue = 0;
//These vertex structs do (should) not have normals, so we will generate them here from the triangle information
//To do this, we have to assume that each group of 3 indices is a triangle (which it would be hard for it to not be)
//and that no vertexes are shared on corners or places where vectors should be interpolated.
//Iterate through the indices to find all used vertices to add normals and adjust texture coordinates.
MeshVertex *p1, *p2, *p3;
glm::vec3 normal;
for (int i = 0; i < this->indices.size()/3; i++) {
//Get the three vertices
p1 = &this->vertices[this->indices[i*3]];
p2 = &this->vertices[this->indices[i*3 + 1]];
p3 = &this->vertices[this->indices[i*3 + 2]];
//Get the normal of the formed triangle
normal = glm::triangleNormal(p1->pos, p2->pos, p3->pos);
//Set the normal on the vertices
p1->nml = normal;
p2->nml = normal;
p3->nml = normal;
}
//Unlike LocalMeshPart, we will just copy the UVs as-is, as we do not have a Texture Atlas to adjust them for.
//Such adjustment will happen on the client side after textures and definitions have been transferred.
for (MeshVertex &vertex : this->vertices) {
vertex.texUVs.x = vertex.tex.x;
vertex.texUVs.y = vertex.tex.y;
}
}

View File

@ -0,0 +1,37 @@
//
// Created by aurailus on 10/06/19.
//
#pragma clang diagnostic push
#pragma ide diagnostic ignored "OCUnusedMacroInspection"
#ifndef ZEUS_SERVERMESHPART_H
#define ZEUS_SERVERMESHPART_H
#define GLM_ENABLE_EXPERIMENTAL
#include <vector>
#include <vec4.hpp>
#include <gtx/normal.hpp>
#include "../MeshMod.h"
#include "../../../game/scene/world/graph/MeshVertex.h"
#include "../../texture/TextureAtlas.h"
struct ServerMeshPart {
ServerMeshPart(std::vector<MeshVertex> vertices, std::vector<unsigned int> indices, std::string texture);
std::vector<MeshVertex> vertices;
std::vector<unsigned int> indices;
std::string texture;
MeshMod meshMod;
float modValue;
};
#endif //ZEUS_SERVERMESHPART_H
#pragma clang diagnostic pop

View File

@ -12,6 +12,9 @@
MapGen::MapGen(unsigned int seed) {
this->seed = seed;
// GRASS_BLOCK = defs.blocks().fromIndex(1);
terrainGeneralElevation.SetFrequency(0.05);
terrainGeneralElevation.SetPersistence(0.4);
terrainGeneralElevation.SetOctaveCount(6);
@ -37,7 +40,6 @@ MapGen::MapGen(unsigned int seed) {
terrainPreElevation.SetControlModule(terrainType);
terrainPreElevation.SetBounds(0.0, 1000.0);
terrainPreElevation.SetEdgeFalloff(0.1);
// terrainPreElevation.SetEdgeFalloff(0.02);
terrainFinal.SetSourceModule(0, terrainPreElevation);
terrainFinal.SetSourceModule(1, terrainGeneralElevation);

View File

@ -10,6 +10,7 @@
#include <vector>
#include "MapGenJob.h"
#include "../LocalDefs.h"
#include "../../world/chunk/BlockChunk.h"
#include <noise/noise.h>
@ -21,6 +22,11 @@ public:
explicit MapGen(unsigned int seed);
BlockChunk* generate(glm::vec3 pos);
private:
int GRASS_BLOCK = 0, DIRT_BLOCK = 0, STONE_BLOCK = 0;
int PLANT_STEM_BLOCK = 0, LEAVES_BLOCK = 0;
int TALLGRASSES[5];
int FLOWERS[9];
void getDensityMap(MapGenJob &job);
void getElevation(MapGenJob &j);
void fillChunk(MapGenJob &j);

View File

@ -60,7 +60,7 @@ void DebugGui::positionElements(glm::vec2 bufferSize) {
vRamGraph .setPosition({bufferWidth - 254, 10 });
}
void DebugGui::update(Player& player, LocalWorld& world, GameDefs& defs, double fps, int chunks, int drawCalls, int ssGen, int ssPack) {
void DebugGui::update(Player& player, LocalWorld& world, LocalDefs& defs, double fps, int chunks, int drawCalls, int ssGen, int ssPack) {
{ //VRam Usage Graph (Top Right)
int videoMemAvail, videoMemTotal;
@ -90,7 +90,7 @@ void DebugGui::update(Player& player, LocalWorld& world, GameDefs& defs, double
glm::vec3 footPos = TransPos::roundPos(player.getPos()) + glm::vec3(0, -2, 0);
int blockID = world.getBlock(footPos);
std::string on = (blockID > 0) ? defs.blocks().getBlock(blockID).getIdentifier() : "invalid";
std::string on = (blockID > 0) ? defs.blocks().fromIndex(blockID).getIdentifier() : "invalid";
glm::vec3 playerPos = TransPos::roundPos(player.getPos());

View File

@ -22,7 +22,7 @@ public:
void bufferResized(glm::vec2 bufferSize);
void update(Player& player, LocalWorld& world, GameDefs& defs, double fps, int chunks, int drawCalls, int ssGen, int ssPack);
void update(Player& player, LocalWorld& world, LocalDefs& defs, double fps, int chunks, int drawCalls, int ssGen, int ssPack);
void draw(Renderer &renderer) override;
void changeVisibilityState(int state);

View File

@ -5,7 +5,7 @@
#include "BlockCrackEntity.h"
BlockCrackEntity::BlockCrackEntity(GameDefs &defs, glm::vec3 blockPos, unsigned int blockID) :
BlockCrackEntity::BlockCrackEntity(LocalDefs &defs, glm::vec3 blockPos, unsigned int blockID) :
defs(defs),
blockPos(blockPos),
blockID(blockID) {
@ -22,7 +22,7 @@ void BlockCrackEntity::update() {
if (crackLevel != this->crackLevel) {
this->crackLevel = crackLevel;
auto model = defs.blocks().getBlock(blockID).getModel();
auto model = defs.blocks().fromIndex(blockID).getModel();
auto m = new Mesh();
std::vector<Vertex> vertices;
@ -32,13 +32,9 @@ void BlockCrackEntity::update() {
crackedFaces.clear();
addFaces(indOffset, vertices, indices, model.leftFaces);
addFaces(indOffset, vertices, indices, model.rightFaces);
addFaces(indOffset, vertices, indices, model.topFaces);
addFaces(indOffset, vertices, indices, model.bottomFaces);
addFaces(indOffset, vertices, indices, model.frontFaces);
addFaces(indOffset, vertices, indices, model.backFaces);
addFaces(indOffset, vertices, indices, model.noCulledFaces);
for (int i = 0; i < 7; i++) {
addFaces(indOffset, vertices, indices, model.parts[i]);
}
m->create(vertices, indices);
setMesh(m);
@ -49,8 +45,8 @@ void BlockCrackEntity::setNewDamage(float damage) {
this->targetDamage = damage;
}
void BlockCrackEntity::addFaces(unsigned int &indOffset, std::vector<Vertex> &vertices, std::vector<unsigned int> &indices, std::vector<MeshPart> &meshParts) {
for (const MeshPart& mp : meshParts) {
void BlockCrackEntity::addFaces(unsigned int &indOffset, std::vector<Vertex> &vertices, std::vector<unsigned int> &indices, std::vector<LocalMeshPart> &meshParts) {
for (const LocalMeshPart& mp : meshParts) {
glm::vec4 uv;
auto ref = defs.textures().generateCrackImage(mp.texture->name, crackLevel);
if (ref == nullptr) {

View File

@ -7,12 +7,12 @@
#include "../../../game/entity/Entity.h"
#include "../../../def/block/graph/BlockModel.h"
#include "../../../def/GameDefs.h"
#include "../../../def/block/client/LocalBlockModel.h"
#include "../../../def/LocalDefs.h"
class BlockCrackEntity : public Entity {
public:
explicit BlockCrackEntity(GameDefs &defs, glm::vec3 blockPos, unsigned int blockID);
explicit BlockCrackEntity(LocalDefs &defs, glm::vec3 blockPos, unsigned int blockID);
void update();
void setNewDamage(float damage);
@ -25,10 +25,10 @@ public:
private:
short crackLevel = -1;
void addFaces(unsigned int &indOffset, std::vector<Vertex> &vertices, std::vector<unsigned int> &indices, std::vector<MeshPart> &meshParts);
void addFaces(unsigned int &indOffset, std::vector<Vertex> &vertices, std::vector<unsigned int> &indices, std::vector<LocalMeshPart> &meshParts);
std::vector<std::shared_ptr<AtlasRef>> crackedFaces;
GameDefs& defs;
LocalDefs& defs;
};

View File

@ -6,7 +6,7 @@
#include "ParticleEntity.h"
ParticleEntity::ParticleEntity(glm::vec3 pos, BlockDef &block) {
ParticleEntity::ParticleEntity(glm::vec3 pos, LocalBlockDef &block) {
this->position = pos + glm::vec3(.5,.3,.5);
this->position += glm::vec3(

View File

@ -7,11 +7,11 @@
#include "../Entity.h"
#include "../../../def/block/BlockDef.h"
#include "../../../def/block/client/LocalBlockDef.h"
class ParticleEntity : public Entity {
public:
ParticleEntity(glm::vec3 pos, BlockDef& block);
ParticleEntity(glm::vec3 pos, LocalBlockDef& block);
void update(double delta, glm::vec3 player);
void draw(Renderer& renderer) override;

View File

@ -15,7 +15,7 @@
#include "../entity/world/PlayerEntity.h"
#include "../entity/world/WireframeEntity.h"
#include "../entity/world/BlockCrackEntity.h"
#include "../../def/GameDefs.h"
#include "../../def/LocalDefs.h"
class GameScene : public Scene {
public:
@ -26,7 +26,7 @@ public:
void cleanup() override;
public:
GameDefs defs;
LocalDefs defs;
glm::vec3 playerPos;

View File

@ -8,11 +8,11 @@
#include "LocalWorld.h"
#include "../net/ServerConnection.h"
LocalWorld::LocalWorld(GameDefs& defs, glm::vec3* playerPos, ServerConnection* server) :
LocalWorld::LocalWorld(LocalDefs& defs, glm::vec3* playerPos, ServerConnection* server) :
playerPos(playerPos),
dimension(&playerChunkPos),
meshGenStream(defs, dimension),
worldGenStream(55),
worldGenStream(55, defs),
server(server),
defs(defs) {}
@ -54,7 +54,7 @@ void LocalWorld::damageBlock(glm::vec3 pos, float amount) {
block->setNewDamage(block->targetDamage + amount);
block->time = 0;
auto def = defs.blocks().getBlock(getBlock(pos));
auto def = defs.blocks().fromIndex(getBlock(pos));
for (int i = 0; i < 40 * amount; i++) {
auto p = new ParticleEntity(pos, def);
particles.push_back(p);
@ -224,5 +224,5 @@ void LocalWorld::setBlock(glm::vec3 pos, int block) {
bool LocalWorld::solidAt(glm::vec3 pos) {
int blockId = getBlock(pos);
if (blockId == -1) return true;
return defs.blocks().getBlock(blockId).isSolid();
return defs.blocks().fromIndex(blockId).isSolid();
}

View File

@ -13,13 +13,13 @@
#include <vec3.hpp>
#include <gtc/type_ptr.hpp>
#include "../../../def/block/BlockAtlas.h"
#include "../../../def/block/client/LocalBlockAtlas.h"
#include "../../../world/chunk/BlockChunk.h"
#include "../../../def/gen/MapGen.h"
#include "../../../util/TransPos.h"
#include "../../../util/Vec.h"
#include "../../../world/Dimension.h"
#include "../../../def/GameDefs.h"
#include "../../../def/LocalDefs.h"
#include "../../../world/block/PointedThing.h"
#include "../../../game/graph/drawable/DrawableGroup.h"
#include "../../entity/world/BlockCrackEntity.h"
@ -32,7 +32,7 @@ class ServerConnection;
class LocalWorld {
public:
LocalWorld(GameDefs& defs, glm::vec3* playerPos, ServerConnection* server);
LocalWorld(LocalDefs& defs, glm::vec3* playerPos, ServerConnection* server);
void update(double delta);
@ -65,7 +65,7 @@ public:
int lastGenUpdates = 0, lastMeshUpdates = 0;
private:
GameDefs& defs;
LocalDefs& defs;
glm::vec3* playerPos;
glm::vec3 playerChunkPos {};

View File

@ -4,7 +4,7 @@
#include "MeshGenStream.h"
MeshGenStream::MeshGenStream(GameDefs &defs, Dimension &dimension) :
MeshGenStream::MeshGenStream(LocalDefs &defs, Dimension &dimension) :
defs(defs),
dimension(dimension) {
@ -73,7 +73,7 @@ std::vector<MeshGenStream::MeshDetails> MeshGenStream::update() {
#pragma clang diagnostic push
#pragma ide diagnostic ignored "OCUnusedGlobalDeclarationInspection"
MeshGenStream::Thread::Thread(BlockAtlas &atlas) : atlas(atlas) {
MeshGenStream::Thread::Thread(LocalBlockAtlas &atlas) : atlas(atlas) {
thread = new std::thread(MeshGenStream::threadFunction, this);
}
#pragma clang diagnostic pop
@ -133,7 +133,7 @@ std::vector<bool>* MeshGenStream::getAdjacentsCull(glm::vec3 pos) {
int z = (i == 4) ? 0 : (i == 5) ? (TransPos::CHUNK_SIZE-1) : k;
auto block = chunk->getBlock(x, y, z);
culls->push_back(defs.blocks().getBlock(block).isCulling());
culls->push_back(defs.blocks().fromIndex(block).isCulling());
}
}
}

View File

@ -10,11 +10,11 @@
#include <thread>
#include <unordered_set>
#include "../../../world/chunk/BlockChunk.h"
#include "../../../def/block/BlockAtlas.h"
#include "../../../def/block/client/LocalBlockAtlas.h"
#include "graph/MeshGenerator.h"
#include "../../../util/Vec.h"
#include "../../../world/Dimension.h"
#include "../../../def/GameDefs.h"
#include "../../../def/LocalDefs.h"
class MeshGenStream {
public:
@ -22,7 +22,7 @@ public:
static const int THREADS = 4;
static const int TOTAL_QUEUE_SIZE = THREADS * THREAD_QUEUE_SIZE;
explicit MeshGenStream(GameDefs& defs, Dimension& dimension);
explicit MeshGenStream(LocalDefs& defs, Dimension& dimension);
~MeshGenStream();
bool spaceInQueue();
@ -58,9 +58,9 @@ public:
};
struct Thread {
explicit Thread(BlockAtlas &atlas);
explicit Thread(LocalBlockAtlas &atlas);
BlockAtlas &atlas;
LocalBlockAtlas &atlas;
std::thread* thread;
bool keepAlive = true;
@ -74,7 +74,7 @@ private:
static void threadFunction(Thread* thread);
Dimension& dimension;
GameDefs& defs;
LocalDefs& defs;
std::vector<glm::vec3> queuedTasks;
std::unordered_set<glm::vec3, VecUtils::compareFunc> queuedMap;

View File

@ -5,7 +5,7 @@
#include "Player.h"
#include "../../../util/Ray.h"
Player::Player(LocalWorld& world, GameDefs& defs, Camera& camera) :
Player::Player(LocalWorld& world, LocalDefs& defs, Camera& camera) :
world(world),
camera(camera),
defs(defs),
@ -106,12 +106,12 @@ void Player::pointerUpdate(InputManager &input, double delta) {
auto blockID = world.getBlock(rayEnd);
if (blockID > 0) {
auto sBox = defs.blocks().getBlock(blockID).getSelectionBox();
auto sBox = defs.blocks().fromIndex(blockID).getSelectionBox();
SelectionBox::Face intersects = sBox.intersects(*ray.getEnd(), pointedPos);
if (intersects != SelectionBox::NONE) {
pointedThing.blockID = static_cast<unsigned int>(blockID);
pointedThing.blockDef = &defs.blocks().getBlock(blockID);
pointedThing.blockDef = &defs.blocks().fromIndex(blockID);
pointedThing.pos = pointedPos;
pointedThing.face = intersects;

View File

@ -22,7 +22,7 @@ public:
static constexpr float LOOK_PRECISION = 0.01f;
static constexpr float EYE_HEIGHT = 1.65f;
Player(LocalWorld& world, GameDefs& defs, Camera& camera);
Player(LocalWorld& world, LocalDefs& defs, Camera& camera);
void update(InputManager &input, double delta, double mouseX, double mouseY);
@ -47,7 +47,7 @@ public:
PointedThing& getPointedThing();
private:
Camera& camera;
GameDefs& defs;
LocalDefs& defs;
LocalWorld& world;
glm::vec3 pos {};

View File

@ -4,7 +4,7 @@
#include "WorldInterpolationStream.h"
WorldInterpolationStream::WorldInterpolationStream(unsigned int seed) : gen(seed) {
WorldInterpolationStream::WorldInterpolationStream(unsigned int seed, LocalDefs& defs) : gen(seed) {
queuedTasks.reserve(1024);
threads.reserve(THREADS);

View File

@ -17,8 +17,7 @@ public:
static const int THREAD_QUEUE_SIZE = 32;
static const int THREADS = 4;
WorldInterpolationStream() : gen(0) {};
explicit WorldInterpolationStream(unsigned int seed);
WorldInterpolationStream(unsigned int seed, LocalDefs& defs);
~WorldInterpolationStream();
//Add `p` to the pre-thread queue.

View File

@ -8,15 +8,15 @@ MeshGenerator::MeshGenerator() {
indOffset = 0;
}
BlockDef& blockData(int ind, BlockChunk &chunk, BlockAtlas& atlas) {
return atlas.getBlock(chunk.getBlock(ind));
LocalBlockDef& blockData(int ind, BlockChunk &chunk, LocalBlockAtlas& atlas) {
return atlas.fromIndex(chunk.getBlock(ind));
}
BlockDef& blockData(glm::vec3 &pos, BlockChunk &chunk, BlockAtlas &atlas) {
return atlas.getBlock(chunk.getBlock(&pos));
LocalBlockDef& blockData(glm::vec3 &pos, BlockChunk &chunk, LocalBlockAtlas &atlas) {
return atlas.fromIndex(chunk.getBlock(&pos));
}
bool faceOcculudedAt(glm::vec3 &pos, BlockChunk &chunk, BlockAtlas &atlas, std::vector<bool> &bools) {
bool faceOcculudedAt(glm::vec3 &pos, BlockChunk &chunk, LocalBlockAtlas &atlas, std::vector<bool> &bools) {
auto off = TransPos::CHUNK_SIZE*TransPos::CHUNK_SIZE; //CHUNK_SIZE ^ 2
if (pos.x < 0 || pos.x >= TransPos::CHUNK_SIZE || pos.y < 0 || pos.y >= TransPos::CHUNK_SIZE || pos.z < 0 || pos.z >= TransPos::CHUNK_SIZE) {
@ -34,7 +34,7 @@ bool faceOcculudedAt(glm::vec3 &pos, BlockChunk &chunk, BlockAtlas &atlas, std::
return blockData(pos, chunk, atlas).isCulling();
}
void MeshGenerator::build(const std::shared_ptr<BlockChunk> &chunk, BlockAtlas &atlas, std::vector<bool> &adjacents,
void MeshGenerator::build(const std::shared_ptr<BlockChunk> &chunk, LocalBlockAtlas &atlas, std::vector<bool> &adjacents,
std::vector<Vertex> &vertices, std::vector<unsigned int> &indices) {
Timer t("Mesh Generation");
@ -51,37 +51,39 @@ void MeshGenerator::build(const std::shared_ptr<BlockChunk> &chunk, BlockAtlas &
VecUtils::indAssignVec(i, off);
vis = off;
// if (chunk->getBlock(i) >= 6 && chunk->getBlock(i) <= 10) {
// if (chunk->getBlock(i) >= 6 && chunk->fromIndex(i) <= 10) {
// vis += glm::vec3((((double)rand() / RAND_MAX) - 0.5f) / 3.f, 0, (((double)rand() / RAND_MAX) - 0.5f) / 3.f);
// }
BlockModel& model = blockData(i, *chunk, atlas).getModel();
//TODO: Better way to do this, surely
LocalBlockModel& model = blockData(i, *chunk, atlas).getModel();
check.x = off.x - 1; check.y = off.y; check.z = off.z;
if (!faceOcculudedAt(check, *chunk, atlas, adjacents))
addFaces(vis, vertices, indices, model.leftFaces);
addFaces(vis, vertices, indices, model.parts[XNEG]);
check.x = off.x + 1; check.y = off.y; check.z = off.z;
if (!faceOcculudedAt(check, *chunk, atlas, adjacents))
addFaces(vis, vertices, indices, model.rightFaces);
addFaces(vis, vertices, indices, model.parts[XPOS]);
check.x = off.x; check.y = off.y - 1; check.z = off.z;
if (!faceOcculudedAt(check, *chunk, atlas, adjacents))
addFaces(vis, vertices, indices, model.bottomFaces);
addFaces(vis, vertices, indices, model.parts[YNEG]);
check.x = off.x; check.y = off.y + 1; check.z = off.z;
if (!faceOcculudedAt(check, *chunk, atlas, adjacents))
addFaces(vis, vertices, indices, model.topFaces);
addFaces(vis, vertices, indices, model.parts[YPOS]);
check.x = off.x; check.y = off.y; check.z = off.z - 1;
if (!faceOcculudedAt(check, *chunk, atlas, adjacents))
addFaces(vis, vertices, indices, model.backFaces);
addFaces(vis, vertices, indices, model.parts[ZNEG]);
check.x = off.x; check.y = off.y; check.z = off.z + 1;
if (!faceOcculudedAt(check, *chunk, atlas, adjacents))
addFaces(vis, vertices, indices, model.frontFaces);
addFaces(vis, vertices, indices, model.parts[ZPOS]);
addFaces(vis, vertices, indices, model.noCulledFaces);
addFaces(vis, vertices, indices, model.parts[NO_CULL]);
}
}
@ -89,8 +91,8 @@ void MeshGenerator::build(const std::shared_ptr<BlockChunk> &chunk, BlockAtlas &
indices.shrink_to_fit();
}
void MeshGenerator::addFaces(glm::vec3 &offset, vector<Vertex> &vertices, vector<unsigned int> &indices, vector<MeshPart> &meshParts) {
for (const MeshPart& mp : meshParts) {
void MeshGenerator::addFaces(glm::vec3 &offset, vector<Vertex> &vertices, vector<unsigned int> &indices, vector<LocalMeshPart> &meshParts) {
for (const LocalMeshPart& mp : meshParts) {
for (const MeshVertex &vertex : mp.vertices) {
vertices.push_back({{vertex.pos + offset}, 1, {vertex.tex.x, vertex.tex.y, 0, 0}, vertex.nml});

View File

@ -15,22 +15,22 @@
#include <cstdio>
#include "../../../../util/Timer.h"
#include "../../../../def/block/BlockDef.h"
#include "../../../../def/block/client/LocalBlockDef.h"
#include "../../../../world/chunk/BlockChunk.h"
#include "../../../../def/block/BlockAtlas.h"
#include "../../../../def/block/client/LocalBlockAtlas.h"
#include "../../../../def/block/client/LocalBlockModel.h"
#include "../../../../util/Vec.h"
#include "../../../../def/block/graph/BlockModel.h"
#include "MeshVertex.h"
class MeshGenerator {
public:
MeshGenerator();
void build(const std::shared_ptr<BlockChunk> &chunk, BlockAtlas &atlas, std::vector<bool> &adjacents,
void build(const std::shared_ptr<BlockChunk> &chunk, LocalBlockAtlas &atlas, std::vector<bool> &adjacents,
std::vector<Vertex> &vertices, std::vector<unsigned int> &indices);
private:
unsigned int indOffset;
void addFaces(glm::vec3 &offset, vector<Vertex> &vertices, vector<unsigned int> &indices, vector<MeshPart> &meshParts);
void addFaces(glm::vec3 &offset, vector<Vertex> &vertices, vector<unsigned int> &indices, vector<LocalMeshPart> &meshParts);
};
#endif //GLPROJECT_MESHGENERATOR_H

View File

@ -5,7 +5,8 @@
#include "Server.h"
Server::Server(unsigned short port) :
world(55),
defs(),
world(55, defs),
connections(&world),
port(port),
handler(port, 32) {

View File

@ -27,6 +27,7 @@ public:
private:
bool alive = true;
ServerDefs defs;
ServerWorld world;
NetHandler handler;
ConnectionList connections;

View File

@ -9,7 +9,7 @@
#include "../../util/Timer.h"
#include "../../util/Util.h"
ServerWorld::ServerWorld(unsigned int seed) : genStream(seed) {
ServerWorld::ServerWorld(unsigned int seed, ServerDefs& defs) : genStream(seed, defs.blocks()) {
//Pregenerate chunk generation order
generateOrder.reserve((unsigned long)pow(ServerPlayer::ACTIVE_RANGE_H * 2, 3));

View File

@ -13,10 +13,11 @@
#include "WorldGenStream.h"
#include "../../util/Vec.h"
#include "../../world/Dimension.h"
#include "../../def/ServerDefs.h"
class ServerWorld {
public:
explicit ServerWorld(unsigned int seed);
explicit ServerWorld(unsigned int seed, ServerDefs& defs);
void addPlayer(ServerPlayer* player);
void update();

View File

@ -7,7 +7,8 @@
#pragma clang diagnostic push
#pragma ide diagnostic ignored "OCUnusedGlobalDeclarationInspection"
WorldGenStream::WorldGenStream(unsigned int seed) : gen(seed) {
//TODO: Give Gen Atlas
WorldGenStream::WorldGenStream(unsigned int seed, ServerBlockAtlas& atlas) : gen(seed) {
queuedTasks.reserve((unsigned long) TOTAL_QUEUE_SIZE);
threads.reserve(THREADS);

View File

@ -11,9 +11,10 @@
#include <vec3.hpp>
#include <thread>
#include <unordered_set>
#include "../../world/chunk/BlockChunk.h"
#include "../../def/gen/MapGen.h"
#include "../../util/Vec.h"
#include "../../def/gen/MapGen.h"
#include "../../world/chunk/BlockChunk.h"
#include "../../def/block/server/ServerBlockAtlas.h"
class WorldGenStream {
public:
@ -21,8 +22,7 @@ public:
static const int THREADS = 4;
static const int TOTAL_QUEUE_SIZE = THREADS * THREAD_QUEUE_SIZE;
WorldGenStream() : gen(0) {};
explicit WorldGenStream(unsigned int seed);
explicit WorldGenStream(unsigned int seed, ServerBlockAtlas& atlas);
~WorldGenStream();
//Attempt to add `pos` to the pre-thread queue.

16
src/util/Dir.h Normal file
View File

@ -0,0 +1,16 @@
//
// Created by aurailus on 10/06/19.
//
#ifndef ZEUS_DIRS_H
#define ZEUS_DIRS_H
enum Dir {
LEFT = 0, RIGHT = 1, TOP = 2, BOTTOM = 3, FRONT = 4, BACK = 5,
XNEG = 0, XPOS = 1, YPOS = 2, YNEG = 3, ZPOS = 4, ZNEG = 5,
INVALID = -1,
NO_CULL = 6,
};
#endif //ZEUS_DIRS_H

View File

@ -5,8 +5,8 @@
#ifndef ZEUS_POSBLOCK_H
#define ZEUS_POSBLOCK_H
#include "../../def/block/BlockDef.h"
#include "../../def/GameDefs.h"
#include "../../def/block/client/LocalBlockDef.h"
#include "../../def/LocalDefs.h"
struct PointedThing {
PointedThing() = default;
@ -29,7 +29,7 @@ struct PointedThing {
public:
//Properties
unsigned int blockID = 0;
BlockDef* blockDef = nullptr;
LocalBlockDef* blockDef = nullptr;
glm::vec3 pos {};
SelectionBox::Face face = SelectionBox::NONE;

View File

@ -28,7 +28,7 @@ TEST_CASE("Blockchunks", "[networking]") {
// b2->rleDecode(rle);
//
// for (int j = 0; j < 4096; j++) {
// REQUIRE(b2->getBlock(j) == b->getBlock(j));
// REQUIRE(b2->getBlock(j) == b->fromIndex(j));
// }
//
// delete b2;

View File

@ -4,7 +4,7 @@
#include <catch.hpp>
#include <iostream>
#include "../../src/def/block/graph/SelectionBox.h"
#include "../../src/def/block/SelectionBox.h"
TEST_CASE("Test Intersection", "math") {
SelectionBox s({0, 0, 0}, {1, 1, 1});