]> git.lizzy.rs Git - dragonfireclient.git/blob - src/objdef.h
Merge branch 'master' into master
[dragonfireclient.git] / src / objdef.h
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 #pragma once
21
22 #include "util/basic_macros.h"
23 #include "porting.h"
24
25 class IGameDef;
26 class NodeDefManager;
27
28 #define OBJDEF_INVALID_INDEX ((u32)(-1))
29 #define OBJDEF_INVALID_HANDLE 0
30 #define OBJDEF_HANDLE_SALT 0x00585e6fu
31 #define OBJDEF_MAX_ITEMS (1 << 18)
32 #define OBJDEF_UID_MASK ((1 << 7) - 1)
33
34 typedef u32 ObjDefHandle;
35
36 enum ObjDefType
37 {
38         OBJDEF_GENERIC,
39         OBJDEF_BIOME,
40         OBJDEF_ORE,
41         OBJDEF_DECORATION,
42         OBJDEF_SCHEMATIC,
43 };
44
45 class ObjDef
46 {
47 public:
48         virtual ~ObjDef() = default;
49
50         // Only implemented by child classes (leafs in class hierarchy)
51         // Should create new object of its own type, call cloneTo() of parent class
52         // and copy its own instance variables over
53         virtual ObjDef *clone() const = 0;
54
55         u32 index;
56         u32 uid;
57         ObjDefHandle handle;
58         std::string name;
59
60 protected:
61         // Only implemented by classes that have children themselves
62         // by copying the defintion and changing that argument type (!!!)
63         // Should defer to parent class cloneTo() if applicable and then copy
64         // over its own properties
65         void cloneTo(ObjDef *def) const;
66 };
67
68 // WARNING: Ownership of ObjDefs is transferred to the ObjDefManager it is
69 // added/set to.  Note that ObjDefs managed by ObjDefManager are NOT refcounted,
70 // so the same ObjDef instance must not be referenced multiple
71 // TODO: const correctness for getter methods
72 class ObjDefManager
73 {
74 public:
75         ObjDefManager(IGameDef *gamedef, ObjDefType type);
76         virtual ~ObjDefManager();
77         DISABLE_CLASS_COPY(ObjDefManager);
78
79         // T *clone() const; // implemented in child class with correct type
80
81         virtual const char *getObjectTitle() const { return "ObjDef"; }
82
83         virtual void clear();
84         virtual ObjDef *getByName(const std::string &name) const;
85
86         //// Add new/get/set object definitions by handle
87         virtual ObjDefHandle add(ObjDef *obj);
88         virtual ObjDef *get(ObjDefHandle handle) const;
89         virtual ObjDef *set(ObjDefHandle handle, ObjDef *obj);
90
91         //// Raw variants that work on indexes
92         virtual u32 addRaw(ObjDef *obj);
93
94         // It is generally assumed that getRaw() will always return a valid object
95         // This won't be true if people do odd things such as call setRaw() with NULL
96         virtual ObjDef *getRaw(u32 index) const;
97         virtual ObjDef *setRaw(u32 index, ObjDef *obj);
98
99         size_t getNumObjects() const { return m_objects.size(); }
100         ObjDefType getType() const { return m_objtype; }
101         const NodeDefManager *getNodeDef() const { return m_ndef; }
102
103         u32 validateHandle(ObjDefHandle handle) const;
104         static ObjDefHandle createHandle(u32 index, ObjDefType type, u32 uid);
105         static bool decodeHandle(
106                         ObjDefHandle handle, u32 *index, ObjDefType *type, u32 *uid);
107
108 protected:
109         ObjDefManager(){};
110         // Helper for child classes to implement clone()
111         void cloneTo(ObjDefManager *mgr) const;
112
113         const NodeDefManager *m_ndef;
114         std::vector<ObjDef *> m_objects;
115         ObjDefType m_objtype;
116 };