Skip to content

Commit 6c5a696

Browse files
committedMay 19, 2015
Split ObjDef/ObjDefManager out to objdef.cpp
1 parent 497299a commit 6c5a696

File tree

11 files changed

+298
-247
lines changed

11 files changed

+298
-247
lines changed
 

Diff for: ‎build/android/jni/Android.mk

+1
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ LOCAL_SRC_FILES := \
182182
jni/src/nodemetadata.cpp \
183183
jni/src/nodetimer.cpp \
184184
jni/src/noise.cpp \
185+
jni/src/objdef.cpp \
185186
jni/src/object_properties.cpp \
186187
jni/src/particles.cpp \
187188
jni/src/pathfinder.cpp \

Diff for: ‎src/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ set(common_SRCS
330330
nodemetadata.cpp
331331
nodetimer.cpp
332332
noise.cpp
333+
objdef.cpp
333334
object_properties.cpp
334335
pathfinder.cpp
335336
player.cpp

Diff for: ‎src/mapgen.cpp

+2-168
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
Minetest
3-
Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
3+
Copyright (C) 2010-2015 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4+
Copyright (C) 2010-2015 celeron55, Perttu Ahola <celeron55@gmail.com>
45
56
This program is free software; you can redistribute it and/or modify
67
it under the terms of the GNU Lesser General Public License as published by
@@ -427,175 +428,8 @@ void GenerateNotifier::getEvents(
427428
m_notify_events.clear();
428429
}
429430

430-
431431
///////////////////////////////////////////////////////////////////////////////
432432

433-
434-
ObjDefManager::ObjDefManager(IGameDef *gamedef, ObjDefType type)
435-
{
436-
m_objtype = type;
437-
m_ndef = gamedef ? gamedef->getNodeDefManager() : NULL;
438-
}
439-
440-
441-
ObjDefManager::~ObjDefManager()
442-
{
443-
for (size_t i = 0; i != m_objects.size(); i++)
444-
delete m_objects[i];
445-
}
446-
447-
448-
ObjDefHandle ObjDefManager::add(ObjDef *obj)
449-
{
450-
assert(obj);
451-
452-
if (obj->name.length() && getByName(obj->name))
453-
return OBJDEF_INVALID_HANDLE;
454-
455-
u32 index = addRaw(obj);
456-
if (index == OBJDEF_INVALID_INDEX)
457-
return OBJDEF_INVALID_HANDLE;
458-
459-
obj->handle = createHandle(index, m_objtype, obj->uid);
460-
return obj->handle;
461-
}
462-
463-
464-
ObjDef *ObjDefManager::get(ObjDefHandle handle) const
465-
{
466-
u32 index = validateHandle(handle);
467-
return (index != OBJDEF_INVALID_INDEX) ? getRaw(index) : NULL;
468-
}
469-
470-
471-
ObjDef *ObjDefManager::set(ObjDefHandle handle, ObjDef *obj)
472-
{
473-
u32 index = validateHandle(handle);
474-
if (index == OBJDEF_INVALID_INDEX)
475-
return NULL;
476-
477-
ObjDef *oldobj = setRaw(index, obj);
478-
479-
obj->uid = oldobj->uid;
480-
obj->index = oldobj->index;
481-
obj->handle = oldobj->handle;
482-
483-
return oldobj;
484-
}
485-
486-
487-
u32 ObjDefManager::addRaw(ObjDef *obj)
488-
{
489-
size_t nobjects = m_objects.size();
490-
if (nobjects >= OBJDEF_MAX_ITEMS)
491-
return -1;
492-
493-
obj->index = nobjects;
494-
495-
// Ensure UID is nonzero so that a valid handle == OBJDEF_INVALID_HANDLE
496-
// is not possible. The slight randomness bias isn't very significant.
497-
obj->uid = myrand() & OBJDEF_UID_MASK;
498-
if (obj->uid == 0)
499-
obj->uid = 1;
500-
501-
m_objects.push_back(obj);
502-
503-
infostream << "ObjDefManager: added " << getObjectTitle()
504-
<< ": name=\"" << obj->name
505-
<< "\" index=" << obj->index
506-
<< " uid=" << obj->uid
507-
<< std::endl;
508-
509-
return nobjects;
510-
}
511-
512-
513-
ObjDef *ObjDefManager::getRaw(u32 index) const
514-
{
515-
return m_objects[index];
516-
}
517-
518-
519-
ObjDef *ObjDefManager::setRaw(u32 index, ObjDef *obj)
520-
{
521-
ObjDef *old_obj = m_objects[index];
522-
m_objects[index] = obj;
523-
return old_obj;
524-
}
525-
526-
527-
ObjDef *ObjDefManager::getByName(const std::string &name) const
528-
{
529-
for (size_t i = 0; i != m_objects.size(); i++) {
530-
ObjDef *obj = m_objects[i];
531-
if (obj && !strcasecmp(name.c_str(), obj->name.c_str()))
532-
return obj;
533-
}
534-
535-
return NULL;
536-
}
537-
538-
539-
void ObjDefManager::clear()
540-
{
541-
for (size_t i = 0; i != m_objects.size(); i++)
542-
delete m_objects[i];
543-
544-
m_objects.clear();
545-
}
546-
547-
548-
u32 ObjDefManager::validateHandle(ObjDefHandle handle) const
549-
{
550-
ObjDefType type;
551-
u32 index;
552-
u32 uid;
553-
554-
bool is_valid =
555-
(handle != OBJDEF_INVALID_HANDLE) &&
556-
decodeHandle(handle, &index, &type, &uid) &&
557-
(type == m_objtype) &&
558-
(index < m_objects.size()) &&
559-
(m_objects[index]->uid == uid);
560-
561-
return is_valid ? index : -1;
562-
}
563-
564-
565-
ObjDefHandle ObjDefManager::createHandle(u32 index, ObjDefType type, u32 uid)
566-
{
567-
ObjDefHandle handle = 0;
568-
set_bits(&handle, 0, 18, index);
569-
set_bits(&handle, 18, 6, type);
570-
set_bits(&handle, 24, 7, uid);
571-
572-
u32 parity = calc_parity(handle);
573-
set_bits(&handle, 31, 1, parity);
574-
575-
return handle ^ OBJDEF_HANDLE_SALT;
576-
}
577-
578-
579-
bool ObjDefManager::decodeHandle(ObjDefHandle handle, u32 *index,
580-
ObjDefType *type, u32 *uid)
581-
{
582-
handle ^= OBJDEF_HANDLE_SALT;
583-
584-
u32 parity = get_bits(handle, 31, 1);
585-
set_bits(&handle, 31, 1, 0);
586-
if (parity != calc_parity(handle))
587-
return false;
588-
589-
*index = get_bits(handle, 0, 18);
590-
*type = (ObjDefType)get_bits(handle, 18, 6);
591-
*uid = get_bits(handle, 24, 7);
592-
return true;
593-
}
594-
595-
596-
///////////////////////////////////////////////////////////////////////////////
597-
598-
599433
void MapgenParams::load(const Settings &settings)
600434
{
601435
std::string seed_str;

Diff for: ‎src/mapgen.h

-68
Original file line numberDiff line numberDiff line change
@@ -186,72 +186,4 @@ struct MapgenFactory {
186186
virtual ~MapgenFactory() {}
187187
};
188188

189-
typedef std::map<std::string, std::string> StringMap;
190-
typedef u32 ObjDefHandle;
191-
192-
#define OBJDEF_INVALID_INDEX ((u32)(-1))
193-
#define OBJDEF_INVALID_HANDLE 0
194-
#define OBJDEF_HANDLE_SALT 0x00585e6fu
195-
#define OBJDEF_MAX_ITEMS (1 << 18)
196-
#define OBJDEF_UID_MASK ((1 << 7) - 1)
197-
198-
enum ObjDefType {
199-
OBJDEF_GENERIC,
200-
OBJDEF_BIOME,
201-
OBJDEF_ORE,
202-
OBJDEF_DECORATION,
203-
OBJDEF_SCHEMATIC,
204-
};
205-
206-
class ObjDef {
207-
public:
208-
virtual ~ObjDef() {}
209-
210-
u32 index;
211-
u32 uid;
212-
ObjDefHandle handle;
213-
std::string name;
214-
};
215-
216-
// WARNING: Ownership of ObjDefs is transferred to the ObjDefManager it is
217-
// added/set to. Note that ObjDefs managed by ObjDefManager are NOT refcounted,
218-
// so the same ObjDef instance must not be referenced multiple
219-
class ObjDefManager {
220-
public:
221-
ObjDefManager(IGameDef *gamedef, ObjDefType type);
222-
virtual ~ObjDefManager();
223-
224-
virtual const char *getObjectTitle() const { return "ObjDef"; }
225-
226-
virtual void clear();
227-
virtual ObjDef *getByName(const std::string &name) const;
228-
229-
//// Add new/get/set object definitions by handle
230-
virtual ObjDefHandle add(ObjDef *obj);
231-
virtual ObjDef *get(ObjDefHandle handle) const;
232-
virtual ObjDef *set(ObjDefHandle handle, ObjDef *obj);
233-
234-
//// Raw variants that work on indexes
235-
virtual u32 addRaw(ObjDef *obj);
236-
237-
// It is generally assumed that getRaw() will always return a valid object
238-
// This won't be true if people do odd things such as call setRaw() with NULL
239-
virtual ObjDef *getRaw(u32 index) const;
240-
virtual ObjDef *setRaw(u32 index, ObjDef *obj);
241-
242-
size_t getNumObjects() const { return m_objects.size(); }
243-
ObjDefType getType() const { return m_objtype; }
244-
INodeDefManager *getNodeDef() const { return m_ndef; }
245-
246-
u32 validateHandle(ObjDefHandle handle) const;
247-
static ObjDefHandle createHandle(u32 index, ObjDefType type, u32 uid);
248-
static bool decodeHandle(ObjDefHandle handle, u32 *index,
249-
ObjDefType *type, u32 *uid);
250-
251-
protected:
252-
INodeDefManager *m_ndef;
253-
std::vector<ObjDef *> m_objects;
254-
ObjDefType m_objtype;
255-
};
256-
257189
#endif

Diff for: ‎src/mg_biome.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2020
#ifndef MG_BIOME_HEADER
2121
#define MG_BIOME_HEADER
2222

23-
#include "mapgen.h"
24-
25-
struct NoiseParams;
23+
#include "objdef.h"
24+
#include "nodedef.h"
2625

2726
enum BiomeType
2827
{

Diff for: ‎src/mg_decoration.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2121
#define MG_DECORATION_HEADER
2222

2323
#include <set>
24-
#include "mapgen.h"
24+
#include "objdef.h"
25+
#include "noise.h"
26+
#include "nodedef.h"
2527

26-
struct NoiseParams;
2728
class Mapgen;
2829
class MMVManip;
2930
class PseudoRandom;

Diff for: ‎src/mg_ore.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2020
#ifndef MG_ORE_HEADER
2121
#define MG_ORE_HEADER
2222

23-
#include "util/string.h"
24-
#include "mapgen.h"
23+
#include "objdef.h"
24+
#include "noise.h"
25+
#include "nodedef.h"
2526

26-
struct NoiseParams;
2727
class Noise;
2828
class Mapgen;
2929
class MMVManip;

Diff for: ‎src/objdef.cpp

+185
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
/*
2+
Minetest
3+
Copyright (C) 2010-2015 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4+
5+
This program is free software; you can redistribute it and/or modify
6+
it under the terms of the GNU Lesser General Public License as published by
7+
the Free Software Foundation; either version 2.1 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public License along
16+
with this program; if not, write to the Free Software Foundation, Inc.,
17+
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*/
19+
20+
#include "objdef.h"
21+
#include "util/numeric.h"
22+
#include "debug.h"
23+
#include "log.h"
24+
#include "gamedef.h"
25+
26+
ObjDefManager::ObjDefManager(IGameDef *gamedef, ObjDefType type)
27+
{
28+
m_objtype = type;
29+
m_ndef = gamedef ? gamedef->getNodeDefManager() : NULL;
30+
}
31+
32+
33+
ObjDefManager::~ObjDefManager()
34+
{
35+
for (size_t i = 0; i != m_objects.size(); i++)
36+
delete m_objects[i];
37+
}
38+
39+
40+
ObjDefHandle ObjDefManager::add(ObjDef *obj)
41+
{
42+
assert(obj);
43+
44+
if (obj->name.length() && getByName(obj->name))
45+
return OBJDEF_INVALID_HANDLE;
46+
47+
u32 index = addRaw(obj);
48+
if (index == OBJDEF_INVALID_INDEX)
49+
return OBJDEF_INVALID_HANDLE;
50+
51+
obj->handle = createHandle(index, m_objtype, obj->uid);
52+
return obj->handle;
53+
}
54+
55+
56+
ObjDef *ObjDefManager::get(ObjDefHandle handle) const
57+
{
58+
u32 index = validateHandle(handle);
59+
return (index != OBJDEF_INVALID_INDEX) ? getRaw(index) : NULL;
60+
}
61+
62+
63+
ObjDef *ObjDefManager::set(ObjDefHandle handle, ObjDef *obj)
64+
{
65+
u32 index = validateHandle(handle);
66+
if (index == OBJDEF_INVALID_INDEX)
67+
return NULL;
68+
69+
ObjDef *oldobj = setRaw(index, obj);
70+
71+
obj->uid = oldobj->uid;
72+
obj->index = oldobj->index;
73+
obj->handle = oldobj->handle;
74+
75+
return oldobj;
76+
}
77+
78+
79+
u32 ObjDefManager::addRaw(ObjDef *obj)
80+
{
81+
size_t nobjects = m_objects.size();
82+
if (nobjects >= OBJDEF_MAX_ITEMS)
83+
return -1;
84+
85+
obj->index = nobjects;
86+
87+
// Ensure UID is nonzero so that a valid handle == OBJDEF_INVALID_HANDLE
88+
// is not possible. The slight randomness bias isn't very significant.
89+
obj->uid = myrand() & OBJDEF_UID_MASK;
90+
if (obj->uid == 0)
91+
obj->uid = 1;
92+
93+
m_objects.push_back(obj);
94+
95+
infostream << "ObjDefManager: added " << getObjectTitle()
96+
<< ": name=\"" << obj->name
97+
<< "\" index=" << obj->index
98+
<< " uid=" << obj->uid
99+
<< std::endl;
100+
101+
return nobjects;
102+
}
103+
104+
105+
ObjDef *ObjDefManager::getRaw(u32 index) const
106+
{
107+
return m_objects[index];
108+
}
109+
110+
111+
ObjDef *ObjDefManager::setRaw(u32 index, ObjDef *obj)
112+
{
113+
ObjDef *old_obj = m_objects[index];
114+
m_objects[index] = obj;
115+
return old_obj;
116+
}
117+
118+
119+
ObjDef *ObjDefManager::getByName(const std::string &name) const
120+
{
121+
for (size_t i = 0; i != m_objects.size(); i++) {
122+
ObjDef *obj = m_objects[i];
123+
if (obj && !strcasecmp(name.c_str(), obj->name.c_str()))
124+
return obj;
125+
}
126+
127+
return NULL;
128+
}
129+
130+
131+
void ObjDefManager::clear()
132+
{
133+
for (size_t i = 0; i != m_objects.size(); i++)
134+
delete m_objects[i];
135+
136+
m_objects.clear();
137+
}
138+
139+
140+
u32 ObjDefManager::validateHandle(ObjDefHandle handle) const
141+
{
142+
ObjDefType type;
143+
u32 index;
144+
u32 uid;
145+
146+
bool is_valid =
147+
(handle != OBJDEF_INVALID_HANDLE) &&
148+
decodeHandle(handle, &index, &type, &uid) &&
149+
(type == m_objtype) &&
150+
(index < m_objects.size()) &&
151+
(m_objects[index]->uid == uid);
152+
153+
return is_valid ? index : -1;
154+
}
155+
156+
157+
ObjDefHandle ObjDefManager::createHandle(u32 index, ObjDefType type, u32 uid)
158+
{
159+
ObjDefHandle handle = 0;
160+
set_bits(&handle, 0, 18, index);
161+
set_bits(&handle, 18, 6, type);
162+
set_bits(&handle, 24, 7, uid);
163+
164+
u32 parity = calc_parity(handle);
165+
set_bits(&handle, 31, 1, parity);
166+
167+
return handle ^ OBJDEF_HANDLE_SALT;
168+
}
169+
170+
171+
bool ObjDefManager::decodeHandle(ObjDefHandle handle, u32 *index,
172+
ObjDefType *type, u32 *uid)
173+
{
174+
handle ^= OBJDEF_HANDLE_SALT;
175+
176+
u32 parity = get_bits(handle, 31, 1);
177+
set_bits(&handle, 31, 1, 0);
178+
if (parity != calc_parity(handle))
179+
return false;
180+
181+
*index = get_bits(handle, 0, 18);
182+
*type = (ObjDefType)get_bits(handle, 18, 6);
183+
*uid = get_bits(handle, 24, 7);
184+
return true;
185+
}

Diff for: ‎src/objdef.h

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
Minetest
3+
Copyright (C) 2010-2015 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4+
5+
This program is free software; you can redistribute it and/or modify
6+
it under the terms of the GNU Lesser General Public License as published by
7+
the Free Software Foundation; either version 2.1 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public License along
16+
with this program; if not, write to the Free Software Foundation, Inc.,
17+
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*/
19+
20+
#ifndef OBJDEF_HEADER
21+
#define OBJDEF_HEADER
22+
23+
#include <string>
24+
#include <vector>
25+
#include "irrlichttypes.h"
26+
27+
class IGameDef;
28+
class INodeDefManager;
29+
30+
#define OBJDEF_INVALID_INDEX ((u32)(-1))
31+
#define OBJDEF_INVALID_HANDLE 0
32+
#define OBJDEF_HANDLE_SALT 0x00585e6fu
33+
#define OBJDEF_MAX_ITEMS (1 << 18)
34+
#define OBJDEF_UID_MASK ((1 << 7) - 1)
35+
36+
typedef u32 ObjDefHandle;
37+
38+
enum ObjDefType {
39+
OBJDEF_GENERIC,
40+
OBJDEF_BIOME,
41+
OBJDEF_ORE,
42+
OBJDEF_DECORATION,
43+
OBJDEF_SCHEMATIC,
44+
};
45+
46+
class ObjDef {
47+
public:
48+
virtual ~ObjDef() {}
49+
50+
u32 index;
51+
u32 uid;
52+
ObjDefHandle handle;
53+
std::string name;
54+
};
55+
56+
// WARNING: Ownership of ObjDefs is transferred to the ObjDefManager it is
57+
// added/set to. Note that ObjDefs managed by ObjDefManager are NOT refcounted,
58+
// so the same ObjDef instance must not be referenced multiple
59+
class ObjDefManager {
60+
public:
61+
ObjDefManager(IGameDef *gamedef, ObjDefType type);
62+
virtual ~ObjDefManager();
63+
64+
virtual const char *getObjectTitle() const { return "ObjDef"; }
65+
66+
virtual void clear();
67+
virtual ObjDef *getByName(const std::string &name) const;
68+
69+
//// Add new/get/set object definitions by handle
70+
virtual ObjDefHandle add(ObjDef *obj);
71+
virtual ObjDef *get(ObjDefHandle handle) const;
72+
virtual ObjDef *set(ObjDefHandle handle, ObjDef *obj);
73+
74+
//// Raw variants that work on indexes
75+
virtual u32 addRaw(ObjDef *obj);
76+
77+
// It is generally assumed that getRaw() will always return a valid object
78+
// This won't be true if people do odd things such as call setRaw() with NULL
79+
virtual ObjDef *getRaw(u32 index) const;
80+
virtual ObjDef *setRaw(u32 index, ObjDef *obj);
81+
82+
size_t getNumObjects() const { return m_objects.size(); }
83+
ObjDefType getType() const { return m_objtype; }
84+
INodeDefManager *getNodeDef() const { return m_ndef; }
85+
86+
u32 validateHandle(ObjDefHandle handle) const;
87+
static ObjDefHandle createHandle(u32 index, ObjDefType type, u32 uid);
88+
static bool decodeHandle(ObjDefHandle handle, u32 *index,
89+
ObjDefType *type, u32 *uid);
90+
91+
protected:
92+
INodeDefManager *m_ndef;
93+
std::vector<ObjDef *> m_objects;
94+
ObjDefType m_objtype;
95+
};
96+
97+
#endif

Diff for: ‎src/unittest/test_objdef.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2020
#include "test.h"
2121

2222
#include "exceptions.h"
23-
#include "mapgen.h"
24-
23+
#include "objdef.h"
2524

2625
class TestObjDef : public TestBase {
2726
public:

Diff for: ‎src/util/string.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,20 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2525
#include <string>
2626
#include <cstring>
2727
#include <vector>
28+
#include <map>
2829
#include <sstream>
2930
#include <cctype>
3031

3132
#define STRINGIFY(x) #x
3233
#define TOSTRING(x) STRINGIFY(x)
3334

35+
typedef std::map<std::string, std::string> StringMap;
36+
3437
struct FlagDesc {
3538
const char *name;
3639
u32 flag;
3740
};
3841

39-
4042
// You must free the returned string!
4143
// The returned string is allocated using new
4244
wchar_t *narrow_to_wide_c(const char *str);

1 commit comments

Comments
 (1)

nerzhul commented on May 19, 2015

@nerzhul
Contributor

👍

Please sign in to comment.