inventory reindexing
This commit is contained in:
@@ -7,10 +7,11 @@
|
||||
#include "../files/files.h"
|
||||
#include "../coders/json.h"
|
||||
#include "../voxels/Block.h"
|
||||
#include "../items/ItemDef.h"
|
||||
|
||||
#include "../data/dynamic.h"
|
||||
|
||||
ContentLUT::ContentLUT(size_t blocksCount, const Content* content) {
|
||||
ContentLUT::ContentLUT(const Content* content, size_t blocksCount, size_t itemsCount) {
|
||||
auto* indices = content->getIndices();
|
||||
for (size_t i = 0; i < blocksCount; i++) {
|
||||
blocks.push_back(i);
|
||||
@@ -18,23 +19,37 @@ ContentLUT::ContentLUT(size_t blocksCount, const Content* content) {
|
||||
for (size_t i = 0; i < indices->countBlockDefs(); i++) {
|
||||
blockNames.push_back(indices->getBlockDef(i)->name);
|
||||
}
|
||||
|
||||
for (size_t i = indices->countBlockDefs(); i < blocksCount; i++) {
|
||||
blockNames.push_back("");
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < itemsCount; i++) {
|
||||
items.push_back(i);
|
||||
}
|
||||
for (size_t i = 0; i < indices->countItemDefs(); i++) {
|
||||
itemNames.push_back(indices->getItemDef(i)->name);
|
||||
}
|
||||
for (size_t i = indices->countItemDefs(); i < itemsCount; i++) {
|
||||
itemNames.push_back("");
|
||||
}
|
||||
}
|
||||
|
||||
ContentLUT* ContentLUT::create(const fs::path& filename,
|
||||
const Content* content) {
|
||||
auto root = files::read_json(filename);
|
||||
auto blocklist = root->list("blocks");
|
||||
auto itemlist = root->list("items");
|
||||
|
||||
auto* indices = content->getIndices();
|
||||
size_t blocks_c = blocklist
|
||||
? std::max(blocklist->size(), indices->countBlockDefs())
|
||||
: indices->countBlockDefs();
|
||||
|
||||
auto lut = std::make_unique<ContentLUT>(blocks_c, content);
|
||||
size_t items_c = itemlist
|
||||
? std::max(itemlist->size(), indices->countItemDefs())
|
||||
: indices->countItemDefs();
|
||||
|
||||
auto lut = std::make_unique<ContentLUT>(content, blocks_c, items_c);
|
||||
|
||||
if (blocklist) {
|
||||
for (size_t i = 0; i < blocklist->size(); i++) {
|
||||
std::string name = blocklist->str(i);
|
||||
@@ -46,6 +61,19 @@ ContentLUT* ContentLUT::create(const fs::path& filename,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (itemlist) {
|
||||
for (size_t i = 0; i < itemlist->size(); i++) {
|
||||
std::string name = itemlist->str(i);
|
||||
ItemDef* def = content->findItem(name);
|
||||
if (def) {
|
||||
lut->setItem(i, name, def->rt.id);
|
||||
} else {
|
||||
lut->setItem(i, name, ITEM_VOID);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (lut->hasContentReorder() || lut->hasMissingContent()) {
|
||||
return lut.release();
|
||||
} else {
|
||||
|
||||
@@ -12,6 +12,8 @@ namespace fs = std::filesystem;
|
||||
|
||||
class Content;
|
||||
|
||||
// TODO: make it unified for all types of content
|
||||
|
||||
/* Content indices lookup table or report
|
||||
used to convert world with different indices
|
||||
Building with indices.json */
|
||||
@@ -19,10 +21,13 @@ class ContentLUT {
|
||||
std::vector<blockid_t> blocks;
|
||||
std::vector<std::string> blockNames;
|
||||
|
||||
std::vector<itemid_t> items;
|
||||
std::vector<std::string> itemNames;
|
||||
|
||||
bool reorderContent = false;
|
||||
bool missingContent = false;
|
||||
public:
|
||||
ContentLUT(size_t blocks, const Content* content);
|
||||
ContentLUT(const Content* content, size_t blocks, size_t items);
|
||||
|
||||
inline const std::string& getBlockName(blockid_t index) const {
|
||||
return blockNames[index];
|
||||
@@ -42,8 +47,28 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
static ContentLUT* create(const fs::path& filename,
|
||||
const Content* content);
|
||||
inline const std::string& getItemName(blockid_t index) const {
|
||||
return itemNames[index];
|
||||
}
|
||||
|
||||
inline itemid_t getItemId(itemid_t index) const {
|
||||
return items[index];
|
||||
}
|
||||
|
||||
inline void setItem(itemid_t index, std::string name, itemid_t id) {
|
||||
items[index] = id;
|
||||
itemNames[index] = name;
|
||||
if (id == ITEM_VOID) {
|
||||
missingContent = true;
|
||||
} else if (index != id) {
|
||||
reorderContent = true;
|
||||
}
|
||||
}
|
||||
|
||||
static ContentLUT* create(
|
||||
const fs::path& filename,
|
||||
const Content* content
|
||||
);
|
||||
|
||||
inline bool hasContentReorder() const {
|
||||
return reorderContent;
|
||||
@@ -55,6 +80,10 @@ public:
|
||||
inline size_t countBlocks() const {
|
||||
return blocks.size();
|
||||
}
|
||||
|
||||
inline size_t countItems() const {
|
||||
return items.size();
|
||||
}
|
||||
};
|
||||
|
||||
#endif // CONTENT_CONTENT_LUT_H_
|
||||
|
||||
Reference in New Issue
Block a user