2015-05-18 22:30:25 -04:00
|
|
|
/*
|
|
|
|
Minetest
|
|
|
|
Copyright (C) 2010-2015 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef OBJDEF_HEADER
|
|
|
|
#define OBJDEF_HEADER
|
|
|
|
|
2015-10-30 22:06:36 -04:00
|
|
|
#include "util/basic_macros.h"
|
2015-05-22 12:26:18 -04:00
|
|
|
#include "porting.h"
|
2015-05-18 22:30:25 -04:00
|
|
|
|
|
|
|
class IGameDef;
|
|
|
|
class INodeDefManager;
|
|
|
|
|
|
|
|
#define OBJDEF_INVALID_INDEX ((u32)(-1))
|
|
|
|
#define OBJDEF_INVALID_HANDLE 0
|
|
|
|
#define OBJDEF_HANDLE_SALT 0x00585e6fu
|
|
|
|
#define OBJDEF_MAX_ITEMS (1 << 18)
|
|
|
|
#define OBJDEF_UID_MASK ((1 << 7) - 1)
|
|
|
|
|
|
|
|
typedef u32 ObjDefHandle;
|
|
|
|
|
|
|
|
enum ObjDefType {
|
|
|
|
OBJDEF_GENERIC,
|
|
|
|
OBJDEF_BIOME,
|
|
|
|
OBJDEF_ORE,
|
|
|
|
OBJDEF_DECORATION,
|
|
|
|
OBJDEF_SCHEMATIC,
|
|
|
|
};
|
|
|
|
|
|
|
|
class ObjDef {
|
|
|
|
public:
|
|
|
|
virtual ~ObjDef() {}
|
|
|
|
|
|
|
|
u32 index;
|
|
|
|
u32 uid;
|
|
|
|
ObjDefHandle handle;
|
|
|
|
std::string name;
|
|
|
|
};
|
|
|
|
|
|
|
|
// WARNING: Ownership of ObjDefs is transferred to the ObjDefManager it is
|
|
|
|
// added/set to. Note that ObjDefs managed by ObjDefManager are NOT refcounted,
|
|
|
|
// so the same ObjDef instance must not be referenced multiple
|
|
|
|
class ObjDefManager {
|
|
|
|
public:
|
|
|
|
ObjDefManager(IGameDef *gamedef, ObjDefType type);
|
|
|
|
virtual ~ObjDefManager();
|
|
|
|
|
|
|
|
virtual const char *getObjectTitle() const { return "ObjDef"; }
|
|
|
|
|
|
|
|
virtual void clear();
|
|
|
|
virtual ObjDef *getByName(const std::string &name) const;
|
|
|
|
|
|
|
|
//// Add new/get/set object definitions by handle
|
|
|
|
virtual ObjDefHandle add(ObjDef *obj);
|
|
|
|
virtual ObjDef *get(ObjDefHandle handle) const;
|
|
|
|
virtual ObjDef *set(ObjDefHandle handle, ObjDef *obj);
|
|
|
|
|
|
|
|
//// Raw variants that work on indexes
|
|
|
|
virtual u32 addRaw(ObjDef *obj);
|
|
|
|
|
|
|
|
// It is generally assumed that getRaw() will always return a valid object
|
|
|
|
// This won't be true if people do odd things such as call setRaw() with NULL
|
|
|
|
virtual ObjDef *getRaw(u32 index) const;
|
|
|
|
virtual ObjDef *setRaw(u32 index, ObjDef *obj);
|
|
|
|
|
|
|
|
size_t getNumObjects() const { return m_objects.size(); }
|
|
|
|
ObjDefType getType() const { return m_objtype; }
|
|
|
|
INodeDefManager *getNodeDef() const { return m_ndef; }
|
|
|
|
|
|
|
|
u32 validateHandle(ObjDefHandle handle) const;
|
|
|
|
static ObjDefHandle createHandle(u32 index, ObjDefType type, u32 uid);
|
|
|
|
static bool decodeHandle(ObjDefHandle handle, u32 *index,
|
|
|
|
ObjDefType *type, u32 *uid);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
INodeDefManager *m_ndef;
|
|
|
|
std::vector<ObjDef *> m_objects;
|
|
|
|
ObjDefType m_objtype;
|
2015-10-27 02:51:43 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
DISABLE_CLASS_COPY(ObjDefManager);
|
2015-05-18 22:30:25 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|