1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-15 19:19:55 +00:00
openmw-tes3mp/apps/openmw/mwworld/store.hpp

423 lines
11 KiB
C++
Raw Normal View History

2012-10-24 08:17:49 +00:00
#ifndef OPENMW_MWWORLD_STORE_H
#define OPENMW_MWWORLD_STORE_H
#include <string>
#include <vector>
#include <map>
2012-10-24 08:17:49 +00:00
2015-07-09 17:22:04 +00:00
#include "recordcmp.hpp"
2013-12-07 12:17:28 +00:00
2015-07-09 17:22:04 +00:00
namespace ESM
{
struct Land;
}
2015-07-09 17:22:04 +00:00
namespace Loading
{
class Listener;
}
2012-11-03 16:20:16 +00:00
2012-10-24 08:17:49 +00:00
namespace MWWorld
{
struct RecordId
{
std::string mId;
bool mIsDeleted;
RecordId(const std::string &id = "", bool isDeleted = false);
};
class StoreBase
2012-10-24 08:17:49 +00:00
{
public:
2012-10-24 08:17:49 +00:00
virtual ~StoreBase() {}
virtual void setUp() {}
2015-12-11 14:55:45 +00:00
/// List identifiers of records contained in this Store (case-smashed). No-op for Stores that don't use string IDs.
2012-10-24 08:17:49 +00:00
virtual void listIdentifier(std::vector<std::string> &list) const {}
virtual size_t getSize() const = 0;
2013-12-07 12:17:28 +00:00
virtual int getDynamicSize() const { return 0; }
virtual RecordId load(ESM::ESMReader &esm) = 0;
virtual bool eraseStatic(const std::string &id) {return false;}
2013-05-15 15:54:18 +00:00
virtual void clearDynamic() {}
2013-12-07 12:17:28 +00:00
2015-03-13 19:07:12 +00:00
virtual void write (ESM::ESMWriter& writer, Loading::Listener& progress) const {}
2013-12-07 12:17:28 +00:00
virtual RecordId read (ESM::ESMReader& reader) { return RecordId(); }
2013-12-07 12:17:28 +00:00
///< Read into dynamic storage
2012-10-24 08:17:49 +00:00
};
2015-07-09 17:22:04 +00:00
template <class T>
class IndexedStore
{
protected:
typedef typename std::map<int, T> Static;
Static mStatic;
public:
typedef typename std::map<int, T>::const_iterator iterator;
IndexedStore();
iterator begin() const;
iterator end() const;
void load(ESM::ESMReader &esm);
int getSize() const;
void setUp();
const T *search(int index) const;
const T *find(int index) const;
};
2012-10-24 08:17:49 +00:00
template <class T>
class SharedIterator
{
typedef typename std::vector<T *>::const_iterator Iter;
Iter mIter;
public:
SharedIterator() {}
SharedIterator(const SharedIterator &orig)
: mIter(orig.mIter)
{}
SharedIterator(const Iter &iter)
: mIter(iter)
{}
SharedIterator& operator=(const SharedIterator&) = default;
2012-10-24 08:17:49 +00:00
SharedIterator &operator++() {
++mIter;
return *this;
}
SharedIterator operator++(int) {
SharedIterator iter = *this;
2013-07-31 16:46:32 +00:00
++mIter;
2012-10-24 08:17:49 +00:00
return iter;
}
SharedIterator &operator--() {
--mIter;
return *this;
}
SharedIterator operator--(int) {
SharedIterator iter = *this;
2013-07-31 16:46:32 +00:00
--mIter;
2012-10-24 08:17:49 +00:00
return iter;
}
bool operator==(const SharedIterator &x) const {
2012-10-24 08:17:49 +00:00
return mIter == x.mIter;
}
bool operator!=(const SharedIterator &x) const {
2012-10-24 08:17:49 +00:00
return !(*this == x);
}
const T &operator*() const {
return **mIter;
}
const T *operator->() const {
return &(**mIter);
}
};
2012-11-06 11:26:55 +00:00
class ESMStore;
2012-10-24 08:17:49 +00:00
template <class T>
class Store : public StoreBase
{
std::map<std::string, T> mStatic;
std::vector<T *> mShared; // Preserves the record order as it came from the content files (this
// is relevant for the spell autocalc code and selection order
// for heads/hairs in the character creation)
std::map<std::string, T> mDynamic;
typedef std::map<std::string, T> Dynamic;
typedef std::map<std::string, T> Static;
2012-10-24 08:17:49 +00:00
2012-11-06 11:26:55 +00:00
friend class ESMStore;
2012-10-24 08:17:49 +00:00
public:
2015-07-09 17:22:04 +00:00
Store();
Store(const Store<T> &orig);
2012-10-24 08:17:49 +00:00
typedef SharedIterator<T> iterator;
2013-05-15 15:54:18 +00:00
// setUp needs to be called again after
2015-07-09 17:22:04 +00:00
virtual void clearDynamic();
void setUp();
2015-07-09 17:22:04 +00:00
const T *search(const std::string &id) const;
2012-10-24 08:17:49 +00:00
/**
* Does the record with this ID come from the dynamic store?
*/
2015-07-09 17:22:04 +00:00
bool isDynamic(const std::string &id) const;
2018-10-09 06:21:12 +00:00
/** Returns a random record that starts with the named ID, or nullptr if not found. */
2015-07-09 17:22:04 +00:00
const T *searchRandom(const std::string &id) const;
2013-08-11 07:35:19 +00:00
2015-07-09 17:22:04 +00:00
const T *find(const std::string &id) const;
2012-10-24 08:17:49 +00:00
2013-08-11 07:35:19 +00:00
/** Returns a random record that starts with the named ID. An exception is thrown if none
* are found. */
2015-07-09 17:22:04 +00:00
const T *findRandom(const std::string &id) const;
2015-07-09 17:22:04 +00:00
iterator begin() const;
iterator end() const;
2015-07-09 17:22:04 +00:00
size_t getSize() const;
int getDynamicSize() const;
/// @note The record identifiers are listed in the order that the records were defined by the content files.
2015-07-09 17:22:04 +00:00
void listIdentifier(std::vector<std::string> &list) const;
2015-07-09 17:22:04 +00:00
T *insert(const T &item);
T *insertStatic(const T &item);
2013-12-07 12:17:28 +00:00
2015-07-09 17:22:04 +00:00
bool eraseStatic(const std::string &id);
bool erase(const std::string &id);
bool erase(const T &item);
2013-12-07 12:17:28 +00:00
RecordId load(ESM::ESMReader &esm);
2015-07-09 17:22:04 +00:00
void write(ESM::ESMWriter& writer, Loading::Listener& progress) const;
RecordId read(ESM::ESMReader& reader);
};
template <>
2012-10-24 08:17:49 +00:00
class Store<ESM::LandTexture> : public StoreBase
{
// For multiple ESM/ESP files we need one list per file.
typedef std::vector<ESM::LandTexture> LandTextureList;
std::vector<LandTextureList> mStatic;
2012-10-24 08:17:49 +00:00
public:
2015-07-09 17:22:04 +00:00
Store();
2012-10-24 08:17:49 +00:00
typedef std::vector<ESM::LandTexture>::const_iterator iterator;
// Must be threadsafe! Called from terrain background loading threads.
// Not a big deal here, since ESM::LandTexture can never be modified or inserted/erased
2015-07-09 17:22:04 +00:00
const ESM::LandTexture *search(size_t index, size_t plugin) const;
const ESM::LandTexture *find(size_t index, size_t plugin) const;
/// Resize the internal store to hold at least \a num plugins.
void resize(size_t num);
2015-07-09 17:22:04 +00:00
size_t getSize() const;
size_t getSize(size_t plugin) const;
2012-10-24 08:17:49 +00:00
RecordId load(ESM::ESMReader &esm, size_t plugin);
RecordId load(ESM::ESMReader &esm);
2012-10-24 08:17:49 +00:00
2015-07-09 17:22:04 +00:00
iterator begin(size_t plugin) const;
iterator end(size_t plugin) const;
2012-10-24 08:17:49 +00:00
};
template <>
class Store<ESM::Land> : public StoreBase
{
std::vector<ESM::Land *> mStatic;
2012-10-24 08:17:49 +00:00
public:
typedef SharedIterator<ESM::Land> iterator;
2012-10-24 08:17:49 +00:00
2015-07-09 17:22:04 +00:00
virtual ~Store();
2012-12-09 23:59:39 +00:00
2015-07-09 17:22:04 +00:00
size_t getSize() const;
iterator begin() const;
iterator end() const;
2012-10-24 08:17:49 +00:00
// Must be threadsafe! Called from terrain background loading threads.
// Not a big deal here, since ESM::Land can never be modified or inserted/erased
2017-02-13 00:29:22 +00:00
const ESM::Land *search(int x, int y) const;
const ESM::Land *find(int x, int y) const;
2012-10-24 08:17:49 +00:00
RecordId load(ESM::ESMReader &esm);
2015-07-09 17:22:04 +00:00
void setUp();
private:
bool mBuilt = false;
2012-10-24 08:17:49 +00:00
};
template <>
class Store<ESM::Cell> : public StoreBase
{
struct DynamicExtCmp
{
bool operator()(const std::pair<int, int> &left, const std::pair<int, int> &right) const {
if (left.first == right.first && left.second == right.second)
return false;
if (left.first == right.first)
return left.second > right.second;
2015-03-02 22:29:33 +00:00
// Exterior cells are listed in descending, row-major order,
// this is a workaround for an ambiguous chargen_plank reference in the vanilla game.
// there is one at -22,16 and one at -2,-9, the latter should be used.
return left.first > right.first;
}
};
typedef std::map<std::string, ESM::Cell> DynamicInt;
typedef std::map<std::pair<int, int>, ESM::Cell, DynamicExtCmp> DynamicExt;
DynamicInt mInt;
DynamicExt mExt;
2012-10-26 14:52:44 +00:00
2012-11-05 08:11:53 +00:00
std::vector<ESM::Cell *> mSharedInt;
std::vector<ESM::Cell *> mSharedExt;
DynamicInt mDynamicInt;
DynamicExt mDynamicExt;
2015-07-09 17:22:04 +00:00
const ESM::Cell *search(const ESM::Cell &cell) const;
void handleMovedCellRefs(ESM::ESMReader& esm, ESM::Cell* cell);
2012-10-24 08:17:49 +00:00
public:
2012-11-05 08:11:53 +00:00
typedef SharedIterator<ESM::Cell> iterator;
2015-07-09 17:22:04 +00:00
const ESM::Cell *search(const std::string &id) const;
const ESM::Cell *search(int x, int y) const;
const ESM::Cell *searchOrCreate(int x, int y);
2012-10-24 08:17:49 +00:00
2015-07-09 17:22:04 +00:00
const ESM::Cell *find(const std::string &id) const;
const ESM::Cell *find(int x, int y) const;
2012-10-26 14:52:44 +00:00
virtual void clearDynamic();
2015-07-09 17:22:04 +00:00
void setUp();
RecordId load(ESM::ESMReader &esm);
2015-07-09 17:22:04 +00:00
iterator intBegin() const;
iterator intEnd() const;
iterator extBegin() const;
iterator extEnd() const;
2012-10-26 14:52:44 +00:00
// Return the northernmost cell in the easternmost column.
2015-07-09 17:22:04 +00:00
const ESM::Cell *searchExtByName(const std::string &id) const;
2012-10-26 14:52:44 +00:00
// Return the northernmost cell in the easternmost column.
2015-07-09 17:22:04 +00:00
const ESM::Cell *searchExtByRegion(const std::string &id) const;
2012-10-26 14:52:44 +00:00
2015-07-09 17:22:04 +00:00
size_t getSize() const;
2019-11-24 13:40:19 +00:00
size_t getExtSize() const;
size_t getIntSize() const;
2012-11-05 08:11:53 +00:00
2015-07-09 17:22:04 +00:00
void listIdentifier(std::vector<std::string> &list) const;
2012-11-05 08:11:53 +00:00
2015-07-09 17:22:04 +00:00
ESM::Cell *insert(const ESM::Cell &cell);
2012-11-05 08:11:53 +00:00
2015-07-09 17:22:04 +00:00
bool erase(const ESM::Cell &cell);
bool erase(const std::string &id);
2012-11-05 08:11:53 +00:00
2015-07-09 17:22:04 +00:00
bool erase(int x, int y);
2012-10-24 08:17:49 +00:00
};
template <>
class Store<ESM::Pathgrid> : public StoreBase
{
2012-10-26 16:46:30 +00:00
private:
typedef std::map<std::string, ESM::Pathgrid> Interior;
typedef std::map<std::pair<int, int>, ESM::Pathgrid> Exterior;
Interior mInt;
Exterior mExt;
Store<ESM::Cell>* mCells;
2012-10-26 16:46:30 +00:00
public:
Store();
2012-10-26 16:46:30 +00:00
2015-07-09 17:22:04 +00:00
void setCells(Store<ESM::Cell>& cells);
RecordId load(ESM::ESMReader &esm);
2015-07-09 17:22:04 +00:00
size_t getSize() const;
2015-07-09 17:22:04 +00:00
void setUp();
2015-07-09 17:22:04 +00:00
const ESM::Pathgrid *search(int x, int y) const;
const ESM::Pathgrid *search(const std::string& name) const;
const ESM::Pathgrid *find(int x, int y) const;
const ESM::Pathgrid* find(const std::string& name) const;
const ESM::Pathgrid *search(const ESM::Cell &cell) const;
const ESM::Pathgrid *find(const ESM::Cell &cell) const;
2012-10-24 08:17:49 +00:00
};
template <>
class Store<ESM::Skill> : public IndexedStore<ESM::Skill>
{
public:
2015-07-09 17:22:04 +00:00
Store();
};
template <>
class Store<ESM::MagicEffect> : public IndexedStore<ESM::MagicEffect>
{
public:
2015-07-09 17:22:04 +00:00
Store();
};
template <>
class Store<ESM::Attribute> : public IndexedStore<ESM::Attribute>
{
std::vector<ESM::Attribute> mStatic;
public:
typedef std::vector<ESM::Attribute>::const_iterator iterator;
2015-07-09 17:22:04 +00:00
Store();
2015-07-09 17:22:04 +00:00
const ESM::Attribute *search(size_t index) const;
const ESM::Attribute *find(size_t index) const;
2015-07-09 17:22:04 +00:00
void setUp();
2015-07-09 17:22:04 +00:00
size_t getSize() const;
iterator begin() const;
iterator end() const;
};
template <>
class Store<ESM::WeaponType> : public StoreBase
{
std::map<int, ESM::WeaponType> mStatic;
public:
typedef std::map<int, ESM::WeaponType>::const_iterator iterator;
Store();
const ESM::WeaponType *search(const int id) const;
const ESM::WeaponType *find(const int id) const;
RecordId load(ESM::ESMReader &esm) { return RecordId(0, false); }
ESM::WeaponType* insert(const ESM::WeaponType &weaponType);
void setUp();
size_t getSize() const;
iterator begin() const;
iterator end() const;
};
2012-10-24 08:17:49 +00:00
} //end namespace
#endif