Split ObjDef/ObjDefManager out to objdef.cpp

This commit is contained in:
kwolekr 2015-05-18 22:30:25 -04:00 committed by OttoLidenbrock
parent f9e1035882
commit 49087eab2f
11 changed files with 298 additions and 247 deletions

View File

@ -182,6 +182,7 @@ LOCAL_SRC_FILES := \
jni/src/nodemetadata.cpp \
jni/src/nodetimer.cpp \
jni/src/noise.cpp \
jni/src/objdef.cpp \
jni/src/object_properties.cpp \
jni/src/particles.cpp \
jni/src/pathfinder.cpp \

View File

@ -330,6 +330,7 @@ set(common_SRCS
nodemetadata.cpp
nodetimer.cpp
noise.cpp
objdef.cpp
object_properties.cpp
pathfinder.cpp
player.cpp

View File

@ -1,6 +1,7 @@
/*
Minetest
Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
Copyright (C) 2010-2015 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
Copyright (C) 2010-2015 celeron55, Perttu Ahola <celeron55@gmail.com>
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
@ -427,175 +428,8 @@ void GenerateNotifier::getEvents(
m_notify_events.clear();
}
///////////////////////////////////////////////////////////////////////////////
ObjDefManager::ObjDefManager(IGameDef *gamedef, ObjDefType type)
{
m_objtype = type;
m_ndef = gamedef ? gamedef->getNodeDefManager() : NULL;
}
ObjDefManager::~ObjDefManager()
{
for (size_t i = 0; i != m_objects.size(); i++)
delete m_objects[i];
}
ObjDefHandle ObjDefManager::add(ObjDef *obj)
{
assert(obj);
if (obj->name.length() && getByName(obj->name))
return OBJDEF_INVALID_HANDLE;
u32 index = addRaw(obj);
if (index == OBJDEF_INVALID_INDEX)
return OBJDEF_INVALID_HANDLE;
obj->handle = createHandle(index, m_objtype, obj->uid);
return obj->handle;
}
ObjDef *ObjDefManager::get(ObjDefHandle handle) const
{
u32 index = validateHandle(handle);
return (index != OBJDEF_INVALID_INDEX) ? getRaw(index) : NULL;
}
ObjDef *ObjDefManager::set(ObjDefHandle handle, ObjDef *obj)
{
u32 index = validateHandle(handle);
if (index == OBJDEF_INVALID_INDEX)
return NULL;
ObjDef *oldobj = setRaw(index, obj);
obj->uid = oldobj->uid;
obj->index = oldobj->index;
obj->handle = oldobj->handle;
return oldobj;
}
u32 ObjDefManager::addRaw(ObjDef *obj)
{
size_t nobjects = m_objects.size();
if (nobjects >= OBJDEF_MAX_ITEMS)
return -1;
obj->index = nobjects;
// Ensure UID is nonzero so that a valid handle == OBJDEF_INVALID_HANDLE
// is not possible. The slight randomness bias isn't very significant.
obj->uid = myrand() & OBJDEF_UID_MASK;
if (obj->uid == 0)
obj->uid = 1;
m_objects.push_back(obj);
infostream << "ObjDefManager: added " << getObjectTitle()
<< ": name=\"" << obj->name
<< "\" index=" << obj->index
<< " uid=" << obj->uid
<< std::endl;
return nobjects;
}
ObjDef *ObjDefManager::getRaw(u32 index) const
{
return m_objects[index];
}
ObjDef *ObjDefManager::setRaw(u32 index, ObjDef *obj)
{
ObjDef *old_obj = m_objects[index];
m_objects[index] = obj;
return old_obj;
}
ObjDef *ObjDefManager::getByName(const std::string &name) const
{
for (size_t i = 0; i != m_objects.size(); i++) {
ObjDef *obj = m_objects[i];
if (obj && !strcasecmp(name.c_str(), obj->name.c_str()))
return obj;
}
return NULL;
}
void ObjDefManager::clear()
{
for (size_t i = 0; i != m_objects.size(); i++)
delete m_objects[i];
m_objects.clear();
}
u32 ObjDefManager::validateHandle(ObjDefHandle handle) const
{
ObjDefType type;
u32 index;
u32 uid;
bool is_valid =
(handle != OBJDEF_INVALID_HANDLE) &&
decodeHandle(handle, &index, &type, &uid) &&
(type == m_objtype) &&
(index < m_objects.size()) &&
(m_objects[index]->uid == uid);
return is_valid ? index : -1;
}
ObjDefHandle ObjDefManager::createHandle(u32 index, ObjDefType type, u32 uid)
{
ObjDefHandle handle = 0;
set_bits(&handle, 0, 18, index);
set_bits(&handle, 18, 6, type);
set_bits(&handle, 24, 7, uid);
u32 parity = calc_parity(handle);
set_bits(&handle, 31, 1, parity);
return handle ^ OBJDEF_HANDLE_SALT;
}
bool ObjDefManager::decodeHandle(ObjDefHandle handle, u32 *index,
ObjDefType *type, u32 *uid)
{
handle ^= OBJDEF_HANDLE_SALT;
u32 parity = get_bits(handle, 31, 1);
set_bits(&handle, 31, 1, 0);
if (parity != calc_parity(handle))
return false;
*index = get_bits(handle, 0, 18);
*type = (ObjDefType)get_bits(handle, 18, 6);
*uid = get_bits(handle, 24, 7);
return true;
}
///////////////////////////////////////////////////////////////////////////////
void MapgenParams::load(const Settings &settings)
{
std::string seed_str;

View File

@ -186,72 +186,4 @@ struct MapgenFactory {
virtual ~MapgenFactory() {}
};
typedef std::map<std::string, std::string> StringMap;
typedef u32 ObjDefHandle;
#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)
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;
};
#endif

View File

@ -20,9 +20,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#ifndef MG_BIOME_HEADER
#define MG_BIOME_HEADER
#include "mapgen.h"
struct NoiseParams;
#include "objdef.h"
#include "nodedef.h"
enum BiomeType
{

View File

@ -21,9 +21,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#define MG_DECORATION_HEADER
#include <set>
#include "mapgen.h"
#include "objdef.h"
#include "noise.h"
#include "nodedef.h"
struct NoiseParams;
class Mapgen;
class MMVManip;
class PseudoRandom;

View File

@ -20,10 +20,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#ifndef MG_ORE_HEADER
#define MG_ORE_HEADER
#include "util/string.h"
#include "mapgen.h"
#include "objdef.h"
#include "noise.h"
#include "nodedef.h"
struct NoiseParams;
class Noise;
class Mapgen;
class MMVManip;

185
src/objdef.cpp Normal file
View File

@ -0,0 +1,185 @@
/*
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.
*/
#include "objdef.h"
#include "util/numeric.h"
#include "debug.h"
#include "log.h"
#include "gamedef.h"
ObjDefManager::ObjDefManager(IGameDef *gamedef, ObjDefType type)
{
m_objtype = type;
m_ndef = gamedef ? gamedef->getNodeDefManager() : NULL;
}
ObjDefManager::~ObjDefManager()
{
for (size_t i = 0; i != m_objects.size(); i++)
delete m_objects[i];
}
ObjDefHandle ObjDefManager::add(ObjDef *obj)
{
assert(obj);
if (obj->name.length() && getByName(obj->name))
return OBJDEF_INVALID_HANDLE;
u32 index = addRaw(obj);
if (index == OBJDEF_INVALID_INDEX)
return OBJDEF_INVALID_HANDLE;
obj->handle = createHandle(index, m_objtype, obj->uid);
return obj->handle;
}
ObjDef *ObjDefManager::get(ObjDefHandle handle) const
{
u32 index = validateHandle(handle);
return (index != OBJDEF_INVALID_INDEX) ? getRaw(index) : NULL;
}
ObjDef *ObjDefManager::set(ObjDefHandle handle, ObjDef *obj)
{
u32 index = validateHandle(handle);
if (index == OBJDEF_INVALID_INDEX)
return NULL;
ObjDef *oldobj = setRaw(index, obj);
obj->uid = oldobj->uid;
obj->index = oldobj->index;
obj->handle = oldobj->handle;
return oldobj;
}
u32 ObjDefManager::addRaw(ObjDef *obj)
{
size_t nobjects = m_objects.size();
if (nobjects >= OBJDEF_MAX_ITEMS)
return -1;
obj->index = nobjects;
// Ensure UID is nonzero so that a valid handle == OBJDEF_INVALID_HANDLE
// is not possible. The slight randomness bias isn't very significant.
obj->uid = myrand() & OBJDEF_UID_MASK;
if (obj->uid == 0)
obj->uid = 1;
m_objects.push_back(obj);
infostream << "ObjDefManager: added " << getObjectTitle()
<< ": name=\"" << obj->name
<< "\" index=" << obj->index
<< " uid=" << obj->uid
<< std::endl;
return nobjects;
}
ObjDef *ObjDefManager::getRaw(u32 index) const
{
return m_objects[index];
}
ObjDef *ObjDefManager::setRaw(u32 index, ObjDef *obj)
{
ObjDef *old_obj = m_objects[index];
m_objects[index] = obj;
return old_obj;
}
ObjDef *ObjDefManager::getByName(const std::string &name) const
{
for (size_t i = 0; i != m_objects.size(); i++) {
ObjDef *obj = m_objects[i];
if (obj && !strcasecmp(name.c_str(), obj->name.c_str()))
return obj;
}
return NULL;
}
void ObjDefManager::clear()
{
for (size_t i = 0; i != m_objects.size(); i++)
delete m_objects[i];
m_objects.clear();
}
u32 ObjDefManager::validateHandle(ObjDefHandle handle) const
{
ObjDefType type;
u32 index;
u32 uid;
bool is_valid =
(handle != OBJDEF_INVALID_HANDLE) &&
decodeHandle(handle, &index, &type, &uid) &&
(type == m_objtype) &&
(index < m_objects.size()) &&
(m_objects[index]->uid == uid);
return is_valid ? index : -1;
}
ObjDefHandle ObjDefManager::createHandle(u32 index, ObjDefType type, u32 uid)
{
ObjDefHandle handle = 0;
set_bits(&handle, 0, 18, index);
set_bits(&handle, 18, 6, type);
set_bits(&handle, 24, 7, uid);
u32 parity = calc_parity(handle);
set_bits(&handle, 31, 1, parity);
return handle ^ OBJDEF_HANDLE_SALT;
}
bool ObjDefManager::decodeHandle(ObjDefHandle handle, u32 *index,
ObjDefType *type, u32 *uid)
{
handle ^= OBJDEF_HANDLE_SALT;
u32 parity = get_bits(handle, 31, 1);
set_bits(&handle, 31, 1, 0);
if (parity != calc_parity(handle))
return false;
*index = get_bits(handle, 0, 18);
*type = (ObjDefType)get_bits(handle, 18, 6);
*uid = get_bits(handle, 24, 7);
return true;
}

97
src/objdef.h Normal file
View File

@ -0,0 +1,97 @@
/*
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
#include <string>
#include <vector>
#include "irrlichttypes.h"
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;
};
#endif

View File

@ -20,8 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "test.h"
#include "exceptions.h"
#include "mapgen.h"
#include "objdef.h"
class TestObjDef : public TestBase {
public:

View File

@ -25,18 +25,20 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <sstream>
#include <cctype>
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
typedef std::map<std::string, std::string> StringMap;
struct FlagDesc {
const char *name;
u32 flag;
};
// You must free the returned string!
// The returned string is allocated using new
wchar_t *narrow_to_wide_c(const char *str);