ContentPackRuntime + refactor

This commit is contained in:
MihailRis
2024-02-08 20:53:12 +03:00
parent 54e6d49847
commit 241d15e349
22 changed files with 224 additions and 169 deletions
+67 -66
View File
@@ -92,7 +92,7 @@ void ContentLoader::fixPackIndices() {
}
// TODO: add basic validation and logging
void ContentLoader::loadBlock(Block* def, std::string name, fs::path file) {
void ContentLoader::loadBlock(Block& def, std::string name, fs::path file) {
auto root = files::read_json(file);
// block texturing
@@ -100,22 +100,22 @@ void ContentLoader::loadBlock(Block* def, std::string name, fs::path file) {
std::string texture;
root->str("texture", texture);
for (uint i = 0; i < 6; i++) {
def->textureFaces[i] = texture;
def.textureFaces[i] = texture;
}
} else if (root->has("texture-faces")) {
auto texarr = root->list("texture-faces");
for (uint i = 0; i < 6; i++) {
def->textureFaces[i] = texarr->str(i);
def.textureFaces[i] = texarr->str(i);
}
}
// block model
std::string model = "block";
root->str("model", model);
if (model == "block") def->model = BlockModel::block;
else if (model == "aabb") def->model = BlockModel::aabb;
if (model == "block") def.model = BlockModel::block;
else if (model == "aabb") def.model = BlockModel::aabb;
else if (model == "custom") {
def->model = BlockModel::custom;
def.model = BlockModel::custom;
if (root->has("model-primitives")) {
loadCustomBlockModel(def, root->map("model-primitives"));
}
@@ -124,30 +124,30 @@ void ContentLoader::loadBlock(Block* def, std::string name, fs::path file) {
<< name << " parsed: no \"model-primitives\" found" << std::endl;
}
}
else if (model == "X") def->model = BlockModel::xsprite;
else if (model == "none") def->model = BlockModel::none;
else if (model == "X") def.model = BlockModel::xsprite;
else if (model == "none") def.model = BlockModel::none;
else {
std::cerr << "unknown model " << model << std::endl;
def->model = BlockModel::none;
def.model = BlockModel::none;
}
// rotation profile
std::string profile = "none";
root->str("rotation", profile);
def->rotatable = profile != "none";
def.rotatable = profile != "none";
if (profile == "pipe") {
def->rotations = BlockRotProfile::PIPE;
def.rotations = BlockRotProfile::PIPE;
} else if (profile == "pane") {
def->rotations = BlockRotProfile::PANE;
def.rotations = BlockRotProfile::PANE;
} else if (profile != "none") {
std::cerr << "unknown rotation profile " << profile << std::endl;
def->rotatable = false;
def.rotatable = false;
}
// block hitbox AABB [x, y, z, width, height, depth]
auto boxarr = root->list("hitbox");
if (boxarr) {
AABB& aabb = def->hitbox;
AABB& aabb = def.hitbox;
aabb.a = glm::vec3(boxarr->num(0), boxarr->num(1), boxarr->num(2));
aabb.b = glm::vec3(boxarr->num(3), boxarr->num(4), boxarr->num(5));
aabb.b += aabb.a;
@@ -156,27 +156,27 @@ void ContentLoader::loadBlock(Block* def, std::string name, fs::path file) {
// block light emission [r, g, b] where r,g,b in range [0..15]
auto emissionarr = root->list("emission");
if (emissionarr) {
def->emission[0] = emissionarr->num(0);
def->emission[1] = emissionarr->num(1);
def->emission[2] = emissionarr->num(2);
def.emission[0] = emissionarr->num(0);
def.emission[1] = emissionarr->num(1);
def.emission[2] = emissionarr->num(2);
}
// primitive properties
root->flag("obstacle", def->obstacle);
root->flag("replaceable", def->replaceable);
root->flag("light-passing", def->lightPassing);
root->flag("breakable", def->breakable);
root->flag("selectable", def->selectable);
root->flag("grounded", def->grounded);
root->flag("hidden", def->hidden);
root->flag("sky-light-passing", def->skyLightPassing);
root->num("draw-group", def->drawGroup);
root->str("picking-item", def->pickingItem);
root->str("script-name", def->scriptName);
root->num("inventory-size", def->inventorySize);
root->flag("obstacle", def.obstacle);
root->flag("replaceable", def.replaceable);
root->flag("light-passing", def.lightPassing);
root->flag("breakable", def.breakable);
root->flag("selectable", def.selectable);
root->flag("grounded", def.grounded);
root->flag("hidden", def.hidden);
root->flag("sky-light-passing", def.skyLightPassing);
root->num("draw-group", def.drawGroup);
root->str("picking-item", def.pickingItem);
root->str("script-name", def.scriptName);
root->num("inventory-size", def.inventorySize);
}
void ContentLoader::loadCustomBlockModel(Block* def, dynamic::Map* primitives) {
void ContentLoader::loadCustomBlockModel(Block& def, dynamic::Map* primitives) {
if (primitives->has("aabbs")) {
auto modelboxes = primitives->list("aabbs");
for (uint i = 0; i < modelboxes->size(); i++ ) {
@@ -186,19 +186,19 @@ void ContentLoader::loadCustomBlockModel(Block* def, dynamic::Map* primitives) {
modelbox.a = glm::vec3(boxarr->num(0), boxarr->num(1), boxarr->num(2));
modelbox.b = glm::vec3(boxarr->num(3), boxarr->num(4), boxarr->num(5));
modelbox.b += modelbox.a;
def->modelBoxes.push_back(modelbox);
def.modelBoxes.push_back(modelbox);
if (boxarr->size() == 7)
for (uint i = 6; i < 12; i++) {
def->modelTextures.push_back(boxarr->str(6));
def.modelTextures.push_back(boxarr->str(6));
}
else if (boxarr->size() == 12)
for (uint i = 6; i < 12; i++) {
def->modelTextures.push_back(boxarr->str(i));
def.modelTextures.push_back(boxarr->str(i));
}
else
for (uint i = 6; i < 12; i++) {
def->modelTextures.push_back("notfound");
def.modelTextures.push_back("notfound");
}
}
}
@@ -210,69 +210,70 @@ void ContentLoader::loadCustomBlockModel(Block* def, dynamic::Map* primitives) {
glm::vec3 p1(tgonobj->num(0), tgonobj->num(1), tgonobj->num(2)),
xw(tgonobj->num(3), tgonobj->num(4), tgonobj->num(5)),
yh(tgonobj->num(6), tgonobj->num(7), tgonobj->num(8));
def->modelExtraPoints.push_back(p1);
def->modelExtraPoints.push_back(p1+xw);
def->modelExtraPoints.push_back(p1+xw+yh);
def->modelExtraPoints.push_back(p1+yh);
def.modelExtraPoints.push_back(p1);
def.modelExtraPoints.push_back(p1+xw);
def.modelExtraPoints.push_back(p1+xw+yh);
def.modelExtraPoints.push_back(p1+yh);
def->modelTextures.push_back(tgonobj->str(9));
def.modelTextures.push_back(tgonobj->str(9));
}
}
}
void ContentLoader::loadItem(ItemDef* def, std::string name, fs::path file) {
void ContentLoader::loadItem(ItemDef& def, std::string name, fs::path file) {
auto root = files::read_json(file);
std::string iconTypeStr = "";
root->str("icon-type", iconTypeStr);
if (iconTypeStr == "none") {
def->iconType = item_icon_type::none;
def.iconType = item_icon_type::none;
} else if (iconTypeStr == "block") {
def->iconType = item_icon_type::block;
def.iconType = item_icon_type::block;
} else if (iconTypeStr == "sprite") {
def->iconType = item_icon_type::sprite;
def.iconType = item_icon_type::sprite;
} else if (iconTypeStr.length()){
std::cerr << "unknown icon type" << iconTypeStr << std::endl;
}
root->str("icon", def->icon);
root->str("placing-block", def->placingBlock);
root->str("script-name", def->scriptName);
root->num("stack-size", def->stackSize);
root->str("icon", def.icon);
root->str("placing-block", def.placingBlock);
root->str("script-name", def.scriptName);
root->num("stack-size", def.stackSize);
// item light emission [r, g, b] where r,g,b in range [0..15]
auto emissionarr = root->list("emission");
if (emissionarr) {
def->emission[0] = emissionarr->num(0);
def->emission[1] = emissionarr->num(1);
def->emission[2] = emissionarr->num(2);
def.emission[0] = emissionarr->num(0);
def.emission[1] = emissionarr->num(1);
def.emission[2] = emissionarr->num(2);
}
}
void ContentLoader::loadBlock(Block* def, std::string full, std::string name) {
void ContentLoader::loadBlock(Block& def, std::string full, std::string name) {
auto folder = pack->folder;
fs::path configFile = folder/fs::path("blocks/"+name+".json");
loadBlock(def, full, configFile);
fs::path scriptfile = folder/fs::path("scripts/"+def->scriptName+".lua");
fs::path scriptfile = folder/fs::path("scripts/"+def.scriptName+".lua");
if (fs::is_regular_file(scriptfile)) {
scripting::load_block_script(full, scriptfile, def->rt.funcsset);
scripting::load_block_script(full, scriptfile, def.rt.funcsset);
}
}
void ContentLoader::loadItem(ItemDef* def, std::string full, std::string name) {
void ContentLoader::loadItem(ItemDef& def, std::string full, std::string name) {
auto folder = pack->folder;
fs::path configFile = folder/fs::path("items/"+name+".json");
loadItem(def, full, configFile);
fs::path scriptfile = folder/fs::path("scripts/"+def->scriptName+".lua");
fs::path scriptfile = folder/fs::path("scripts/"+def.scriptName+".lua");
if (fs::is_regular_file(scriptfile)) {
scripting::load_item_script(full, scriptfile, def->rt.funcsset);
scripting::load_item_script(full, scriptfile, def.rt.funcsset);
}
}
void ContentLoader::load(ContentBuilder* builder) {
void ContentLoader::load(ContentBuilder& builder) {
std::cout << "-- loading pack [" << pack->id << "]" << std::endl;
builder.add(new ContentPackRuntime(*pack));
fixPackIndices();
@@ -291,17 +292,17 @@ void ContentLoader::load(ContentBuilder* builder) {
for (uint i = 0; i < blocksarr->size(); i++) {
std::string name = blocksarr->str(i);
std::string full = pack->id+":"+name;
auto def = builder->createBlock(full);
auto& def = builder.createBlock(full);
loadBlock(def, full, name);
if (!def->hidden) {
auto item = builder->createItem(full+BLOCK_ITEM_SUFFIX);
item->generated = true;
item->iconType = item_icon_type::block;
item->icon = full;
item->placingBlock = full;
if (!def.hidden) {
auto& item = builder.createItem(full+BLOCK_ITEM_SUFFIX);
item.generated = true;
item.iconType = item_icon_type::block;
item.icon = full;
item.placingBlock = full;
for (uint j = 0; j < 4; j++) {
item->emission[j] = def->emission[j];
item.emission[j] = def.emission[j];
}
}
}
@@ -312,7 +313,7 @@ void ContentLoader::load(ContentBuilder* builder) {
for (uint i = 0; i < itemsarr->size(); i++) {
std::string name = itemsarr->str(i);
std::string full = pack->id+":"+name;
loadItem(builder->createItem(full), full, name);
loadItem(builder.createItem(full), full, name);
}
}
}