mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-04 18:41:51 +00:00
update ContentUnitIndices
This commit is contained in:
parent
55e4e6de02
commit
c3c343ffdb
9 changed files with 38 additions and 26 deletions
|
|
@ -15,9 +15,9 @@
|
|||
#include "ContentPack.hpp"
|
||||
|
||||
ContentIndices::ContentIndices(
|
||||
ContentUnitIndices<Block> blocks,
|
||||
ContentUnitIndices<ItemDef> items,
|
||||
ContentUnitIndices<EntityDef> entities
|
||||
ContentUnitIndices<Block, blockid_t> blocks,
|
||||
ContentUnitIndices<ItemDef, itemid_t> items,
|
||||
ContentUnitIndices<EntityDef, entitydefid_t> entities
|
||||
)
|
||||
: blocks(std::move(blocks)),
|
||||
items(std::move(items)),
|
||||
|
|
|
|||
|
|
@ -36,48 +36,57 @@ public:
|
|||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
template <class T, typename IdType>
|
||||
class ContentUnitIndices {
|
||||
std::vector<T*> defs;
|
||||
public:
|
||||
ContentUnitIndices(std::vector<T*> defs) : defs(std::move(defs)) {
|
||||
}
|
||||
|
||||
inline const T* get(blockid_t id) const {
|
||||
const T* get(IdType id) const {
|
||||
if (id >= defs.size()) {
|
||||
return nullptr;
|
||||
}
|
||||
return defs[id];
|
||||
}
|
||||
|
||||
inline const T& require(blockid_t id) const {
|
||||
return *defs.at(id);
|
||||
const T& require(IdType id) const {
|
||||
if (id >= defs.size()) {
|
||||
invalidId(id);
|
||||
}
|
||||
return *defs[id];
|
||||
}
|
||||
|
||||
inline size_t count() const {
|
||||
size_t count() const {
|
||||
return defs.size();
|
||||
}
|
||||
|
||||
inline const auto& getIterable() const {
|
||||
const auto& getIterable() const {
|
||||
return defs;
|
||||
}
|
||||
|
||||
inline const T* const* getDefs() const {
|
||||
const T* const* getDefs() const {
|
||||
return defs.data();
|
||||
}
|
||||
private:
|
||||
void invalidId(IdType id) const {
|
||||
throw std::runtime_error(
|
||||
"invalid content unit id: " + std::to_string(id)
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
/// @brief Runtime defs cache: indices
|
||||
class ContentIndices {
|
||||
public:
|
||||
ContentUnitIndices<Block> blocks;
|
||||
ContentUnitIndices<ItemDef> items;
|
||||
ContentUnitIndices<EntityDef> entities;
|
||||
ContentUnitIndices<Block, blockid_t> blocks;
|
||||
ContentUnitIndices<ItemDef, itemid_t> items;
|
||||
ContentUnitIndices<EntityDef, entitydefid_t> entities;
|
||||
|
||||
ContentIndices(
|
||||
ContentUnitIndices<Block> blocks,
|
||||
ContentUnitIndices<ItemDef> items,
|
||||
ContentUnitIndices<EntityDef> entities
|
||||
ContentUnitIndices<Block, blockid_t> blocks,
|
||||
ContentUnitIndices<ItemDef, itemid_t> items,
|
||||
ContentUnitIndices<EntityDef, entitydefid_t> entities
|
||||
);
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -22,9 +22,9 @@ ContentReport::ContentReport(
|
|||
regionsVersion(regionsVersion) {
|
||||
}
|
||||
|
||||
template <class T>
|
||||
template <class T, typename IdType>
|
||||
static constexpr size_t get_entries_count(
|
||||
const ContentUnitIndices<T>& indices, const dv::value& list
|
||||
const ContentUnitIndices<T, IdType>& indices, const dv::value& list
|
||||
) {
|
||||
return list != nullptr ? std::max(list.size(), indices.count())
|
||||
: indices.count();
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@ class ContentUnitLUT {
|
|||
public:
|
||||
ContentUnitLUT(
|
||||
size_t count,
|
||||
const ContentUnitIndices<U>& unitIndices,
|
||||
const ContentUnitIndices<U, T>& unitIndices,
|
||||
T missingValue,
|
||||
ContentType type
|
||||
)
|
||||
|
|
|
|||
|
|
@ -253,9 +253,9 @@ void scripting::process_post_runnables() {
|
|||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
template <class T, typename IdType>
|
||||
static int push_properties_tables(
|
||||
lua::State* L, const ContentUnitIndices<T>& indices
|
||||
lua::State* L, const ContentUnitIndices<T, IdType>& indices
|
||||
) {
|
||||
const auto units = indices.getDefs();
|
||||
size_t size = indices.count();
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ using ubyte = uint8_t;
|
|||
// content indices
|
||||
using itemid_t = uint32_t;
|
||||
using blockid_t = uint16_t;
|
||||
using entitydefid_t = uint16_t;
|
||||
|
||||
using entityid_t = uint64_t;
|
||||
using itemcount_t = uint32_t;
|
||||
|
|
|
|||
|
|
@ -70,7 +70,7 @@ void GlobalChunks::erase(int x, int z) {
|
|||
static inline auto load_inventories(
|
||||
WorldRegions& regions,
|
||||
const Chunk& chunk,
|
||||
const ContentUnitIndices<Block>& defs
|
||||
const ContentUnitIndices<Block, blockid_t>& defs
|
||||
) {
|
||||
auto invs = regions.fetchInventories(chunk.x, chunk.z);
|
||||
auto iterator = invs.begin();
|
||||
|
|
|
|||
|
|
@ -11,11 +11,13 @@
|
|||
#include <unordered_set>
|
||||
#include <vector>
|
||||
|
||||
#include "typedefs.hpp"
|
||||
|
||||
class Block;
|
||||
class Level;
|
||||
class GlobalChunks;
|
||||
|
||||
template <typename T>
|
||||
template <typename T, typename IdType>
|
||||
class ContentUnitIndices;
|
||||
|
||||
namespace voxels {
|
||||
|
|
@ -82,7 +84,7 @@ namespace voxels {
|
|||
private:
|
||||
const Level& level;
|
||||
const GlobalChunks& chunks;
|
||||
const ContentUnitIndices<Block>& blockDefs;
|
||||
const ContentUnitIndices<Block, blockid_t>& blockDefs;
|
||||
std::unordered_map<int, Agent> agents;
|
||||
int nextAgent = 1;
|
||||
|
||||
|
|
|
|||
|
|
@ -104,9 +104,9 @@ void WorldFiles::writePacks(const std::vector<ContentPack>& packs) {
|
|||
io::write_string(packsFile, ss.str());
|
||||
}
|
||||
|
||||
template <class T>
|
||||
template <class T, typename IdType>
|
||||
static void write_indices(
|
||||
const ContentUnitIndices<T>& indices, dv::value& list
|
||||
const ContentUnitIndices<T, IdType>& indices, dv::value& list
|
||||
) {
|
||||
for (auto unit : indices.getIterable()) {
|
||||
list.add(unit->name);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue