lua: inventory library

This commit is contained in:
MihailRis
2024-02-12 13:56:46 +03:00
parent 617017d979
commit e16f7567b1
14 changed files with 257 additions and 44 deletions
+42
View File
@@ -0,0 +1,42 @@
#include "Inventories.h"
#include "../world/Level.h"
#include "../world/World.h"
Inventories::Inventories(Level& level) : level(level) {
}
Inventories::~Inventories() {
}
std::shared_ptr<Inventory> Inventories::create(size_t size) {
int64_t id = level.getWorld()->getNextInventoryId();
auto inv = std::make_shared<Inventory>(id, size);
store(inv);
return inv;
}
std::shared_ptr<Inventory> Inventories::createVirtual(size_t size) {
int64_t id;
do {
id = -std::max(1L, std::abs(random.rand64()));
} while (map.find(id) != map.end());
auto inv = std::make_shared<Inventory>(id, size);
store(inv);
return inv;
}
void Inventories::store(std::shared_ptr<Inventory> inv) {
map[inv->getId()] = inv;
}
std::shared_ptr<Inventory> Inventories::get(int64_t id) {
auto found = map.find(id);
if (found == map.end())
return nullptr;
return found->second;
}
const inventories_map& Inventories::getMap() const {
return map;
}