lua: inventory library
This commit is contained in:
@@ -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;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
#ifndef ITEMS_INVENTORIES_H_
|
||||
#define ITEMS_INVENTORIES_H_
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "Inventory.h"
|
||||
#include "../maths/util.h"
|
||||
|
||||
class Level;
|
||||
|
||||
using inventories_map = std::unordered_map<int64_t, std::shared_ptr<Inventory>>;
|
||||
|
||||
/* Inventories runtime storage */
|
||||
class Inventories {
|
||||
Level& level;
|
||||
inventories_map map;
|
||||
PseudoRandom random;
|
||||
public:
|
||||
Inventories(Level& level);
|
||||
~Inventories();
|
||||
|
||||
/* Create new inventory with new id */
|
||||
std::shared_ptr<Inventory> create(size_t size);
|
||||
|
||||
/* Create runtime-only inventory (has negative id) */
|
||||
std::shared_ptr<Inventory> createVirtual(size_t size);
|
||||
|
||||
/* Store inventory */
|
||||
void store(std::shared_ptr<Inventory> inv);
|
||||
|
||||
/* Get inventory by id (works with both real and virtual)*/
|
||||
std::shared_ptr<Inventory> get(int64_t id);
|
||||
|
||||
const inventories_map& getMap() const;
|
||||
};
|
||||
|
||||
#endif // ITEMS_INVENTORIES_H_
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
#include "../content/ContentLUT.h"
|
||||
|
||||
Inventory::Inventory(uint id, size_t size) : id(id), slots(size) {
|
||||
Inventory::Inventory(int64_t id, size_t size) : id(id), slots(size) {
|
||||
}
|
||||
|
||||
ItemStack& Inventory::getSlot(size_t index) {
|
||||
|
||||
@@ -14,10 +14,10 @@ class ContentLUT;
|
||||
class ContentIndices;
|
||||
|
||||
class Inventory : Serializable {
|
||||
uint id;
|
||||
int64_t id;
|
||||
std::vector<ItemStack> slots;
|
||||
public:
|
||||
Inventory(uint id, size_t size);
|
||||
Inventory(int64_t id, size_t size);
|
||||
|
||||
ItemStack& getSlot(size_t index);
|
||||
size_t findEmptySlot(size_t begin=0, size_t end=-1) const;
|
||||
@@ -40,10 +40,14 @@ public:
|
||||
|
||||
static void convert(dynamic::Map* data, const ContentLUT* lut);
|
||||
|
||||
inline uint getId() const {
|
||||
inline int64_t getId() const {
|
||||
return id;
|
||||
}
|
||||
|
||||
inline bool isVirtual() const {
|
||||
return id < 0;
|
||||
}
|
||||
|
||||
static const size_t npos;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user