objdef.h 3.5 KB

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