Generated items

This commit is contained in:
MihailRis 2024-01-08 14:16:25 +03:00
parent e4f7b6d97c
commit dd5cd55aeb
5 changed files with 75 additions and 28 deletions

View File

@ -10,6 +10,9 @@ using glm::vec3;
using std::string;
using std::unordered_map;
ContentBuilder::~ContentBuilder() {
}
void ContentBuilder::add(Block* def) {
checkIdentifier(def->name);
blockDefs[def->name] = def;
@ -22,10 +25,34 @@ void ContentBuilder::add(ItemDef* def) {
itemIds.push_back(def->name);
}
Block* ContentBuilder::createBlock(std::string id) {
auto found = blockDefs.find(id);
if (found != blockDefs.end()) {
//return found->second;
throw namereuse_error("name "+id+" is already used", contenttype::item);
}
Block* block = new Block(id);
add(block);
return block;
}
ItemDef* ContentBuilder::createItem(std::string id) {
auto found = itemDefs.find(id);
if (found != itemDefs.end()) {
if (found->second->generated) {
return found->second;
}
throw namereuse_error("name "+id+" is already used", contenttype::item);
}
ItemDef* item = new ItemDef(id);
add(item);
return item;
}
void ContentBuilder::checkIdentifier(std::string id) {
contenttype result;
if ((checkContentType(id) != contenttype::none)) {
throw contentindexreuse_error("identifier "+id+" is already used", result);
throw namereuse_error("name "+id+" is already used", result);
}
}

View File

@ -18,10 +18,10 @@ enum class contenttype {
none, block, item
};
class contentindexreuse_error: public std::runtime_error {
class namereuse_error: public std::runtime_error {
contenttype type;
public:
contentindexreuse_error(const std::string& msg, contenttype type)
namereuse_error(const std::string& msg, contenttype type)
: std::runtime_error(msg), type(type) {}
inline contenttype getType() const {
@ -36,9 +36,14 @@ class ContentBuilder {
std::unordered_map<std::string, ItemDef*> itemDefs;
std::vector<std::string> itemIds;
public:
~ContentBuilder();
void add(Block* def);
void add(ItemDef* def);
Block* createBlock(std::string id);
ItemDef* createItem(std::string id);
void checkIdentifier(std::string id);
contenttype checkContentType(std::string id);

View File

@ -92,9 +92,8 @@ void ContentLoader::fixPackIndices() {
}
// TODO: add basic validation and logging
Block* ContentLoader::loadBlock(std::string name, fs::path file) {
void ContentLoader::loadBlock(Block* def, std::string name, fs::path file) {
std::unique_ptr<json::JObject> root(files::read_json(file));
std::unique_ptr<Block> def(new Block(name));
// block texturing
if (root->has("texture")) {
@ -176,41 +175,32 @@ Block* ContentLoader::loadBlock(std::string name, fs::path file) {
root->flag("hidden", def->hidden);
root->flag("sky-light-passing", def->skyLightPassing);
root->num("draw-group", def->drawGroup);
return def.release();
}
ItemDef* ContentLoader::loadItem(std::string name, std::filesystem::path file) {
void ContentLoader::loadItem(ItemDef* def, std::string name, std::filesystem::path file) {
std::unique_ptr<json::JObject> root(files::read_json(file));
std::unique_ptr<ItemDef> def(new ItemDef(name));
return def.release();
}
Block* ContentLoader::loadBlock(std::string name) {
void ContentLoader::loadBlock(Block* def, std::string full, std::string name) {
auto folder = pack->folder;
std::string prefix = pack->id+":"+name;
fs::path configFile = folder/fs::path("blocks/"+name+".json");
fs::path scriptfile = folder/fs::path("scripts/"+name+".lua");
Block* def = loadBlock(prefix, configFile);
loadBlock(def, full, configFile);
if (fs::is_regular_file(scriptfile)) {
scripting::load_block_script(prefix, scriptfile, &def->rt.funcsset);
scripting::load_block_script(full, scriptfile, &def->rt.funcsset);
}
return def;
}
ItemDef* ContentLoader::loadItem(std::string name) {
void ContentLoader::loadItem(ItemDef* def, std::string full, std::string name) {
auto folder = pack->folder;
std::string prefix = pack->id+":"+name;
fs::path configFile = folder/fs::path("items/"+name+".json");
fs::path scriptfile = folder/fs::path("scripts/"+name+".lua");
ItemDef* def = loadItem(prefix, configFile);
loadItem(def, full, configFile);
if (fs::is_regular_file(scriptfile)) {
scripting::load_item_script(prefix, scriptfile, &def->rt.funcsset);
scripting::load_item_script(full, scriptfile, &def->rt.funcsset);
}
return def;
}
void ContentLoader::load(ContentBuilder* builder) {
@ -226,14 +216,26 @@ void ContentLoader::load(ContentBuilder* builder) {
json::JArray* blocksarr = root->arr("blocks");
if (blocksarr) {
for (uint i = 0; i < blocksarr->size(); i++) {
builder->add(loadBlock(blocksarr->str(i)));
std::string name = blocksarr->str(i);
std::string full = pack->id+":"+name;
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;
}
}
}
json::JArray* itemsarr = root->arr("items");
if (itemsarr) {
for (uint i = 0; i < itemsarr->size(); i++) {
builder->add(loadItem(itemsarr->str(i)));
std::string name = itemsarr->str(i);
std::string full = pack->id+":"+name;
loadItem(builder->createItem(full), full, name);
}
}
}

View File

@ -16,8 +16,8 @@ namespace json {
class ContentLoader {
const ContentPack* pack;
Block* loadBlock(std::string name);
ItemDef* loadItem(std::string name);
void loadBlock(Block* def, std::string full, std::string name);
void loadItem(ItemDef* def, std::string full, std::string name);
public:
ContentLoader(ContentPack* pack);
@ -25,8 +25,8 @@ public:
json::JObject* indicesRoot,
std::string contentSection);
void fixPackIndices();
Block* loadBlock(std::string name, std::filesystem::path file);
ItemDef* loadItem(std::string name, std::filesystem::path file);
void loadBlock(Block* def, std::string name, std::filesystem::path file);
void loadItem(ItemDef* def, std::string name, std::filesystem::path file);
void load(ContentBuilder* builder);
};

View File

@ -6,13 +6,26 @@
#include "../typedefs.h"
#define BLOCK_ITEM_SUFFIX ".item"
struct item_funcs_set {
bool init: 1;
};
enum class item_icon_type {
sprite, block,
};
class ItemDef {
public:
std::string name;
std::string const name;
bool generated = false;
item_icon_type iconType = item_icon_type::sprite;
std::string icon = "block:notfound";
std::string placingBlock = "none";
struct {
itemid_t id;