2024-10-28 15:57:39 +01:00
|
|
|
// Luanti
|
|
|
|
// SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
// Copyright (C) 2010-2015 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
|
2015-05-19 04:30:25 +02:00
|
|
|
|
2017-08-17 22:19:39 +02:00
|
|
|
#pragma once
|
2015-05-19 04:30:25 +02:00
|
|
|
|
2015-10-31 03:06:36 +01:00
|
|
|
#include "util/basic_macros.h"
|
2015-05-22 18:26:18 +02:00
|
|
|
#include "porting.h"
|
2024-10-28 19:40:28 +01:00
|
|
|
#include <vector>
|
2015-05-19 04:30:25 +02:00
|
|
|
|
|
|
|
class IGameDef;
|
2018-02-10 21:04:16 +01:00
|
|
|
class NodeDefManager;
|
2015-05-19 04:30:25 +02:00
|
|
|
|
|
|
|
#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:
|
2017-08-19 11:30:46 +02:00
|
|
|
virtual ~ObjDef() = default;
|
2015-05-19 04:30:25 +02:00
|
|
|
|
2020-04-09 23:40:12 +02:00
|
|
|
// Only implemented by child classes (leafs in class hierarchy)
|
|
|
|
// Should create new object of its own type, call cloneTo() of parent class
|
|
|
|
// and copy its own instance variables over
|
|
|
|
virtual ObjDef *clone() const = 0;
|
|
|
|
|
2015-05-19 04:30:25 +02:00
|
|
|
u32 index;
|
|
|
|
u32 uid;
|
|
|
|
ObjDefHandle handle;
|
|
|
|
std::string name;
|
2020-04-09 23:40:12 +02:00
|
|
|
|
|
|
|
protected:
|
|
|
|
// Only implemented by classes that have children themselves
|
|
|
|
// by copying the defintion and changing that argument type (!!!)
|
|
|
|
// Should defer to parent class cloneTo() if applicable and then copy
|
|
|
|
// over its own properties
|
|
|
|
void cloneTo(ObjDef *def) const;
|
2015-05-19 04:30:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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
|
2020-04-10 02:43:49 +02:00
|
|
|
// TODO: const correctness for getter methods
|
2015-05-19 04:30:25 +02:00
|
|
|
class ObjDefManager {
|
|
|
|
public:
|
|
|
|
ObjDefManager(IGameDef *gamedef, ObjDefType type);
|
|
|
|
virtual ~ObjDefManager();
|
2017-06-10 13:49:15 +02:00
|
|
|
DISABLE_CLASS_COPY(ObjDefManager);
|
2015-05-19 04:30:25 +02:00
|
|
|
|
2020-04-09 23:40:12 +02:00
|
|
|
// T *clone() const; // implemented in child class with correct type
|
|
|
|
|
2015-05-19 04:30:25 +02:00
|
|
|
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; }
|
2018-02-10 21:04:16 +01:00
|
|
|
const NodeDefManager *getNodeDef() const { return m_ndef; }
|
2015-05-19 04:30:25 +02:00
|
|
|
|
|
|
|
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:
|
2020-04-09 23:40:12 +02:00
|
|
|
ObjDefManager() {};
|
|
|
|
// Helper for child classes to implement clone()
|
|
|
|
void cloneTo(ObjDefManager *mgr) const;
|
|
|
|
|
2018-02-10 21:04:16 +01:00
|
|
|
const NodeDefManager *m_ndef;
|
2015-05-19 04:30:25 +02:00
|
|
|
std::vector<ObjDef *> m_objects;
|
|
|
|
ObjDefType m_objtype;
|
|
|
|
};
|