Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
ObjectBase.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_OBJECTBASE
19#define INCLUDED_OBJECTBASE
20
21#include "lib/code_annotation.h"
23#include "lib/path.h"
24#include "ps/CStr.h"
25#include "ps/CStrIntern.h"
26
27#include <cstdint>
28#include <map>
29#include <memory>
30#include <random>
31#include <set>
32#include <unordered_set>
33#include <vector>
34
35class CActorDef;
36class CObjectManager;
37class CXeromyces;
38class XMBElement;
39
40/**
41 * Maintains the tree of possible objects from a specific actor definition at a given quality level.
42 * An Object Base is made of:
43 * - a material
44 * - a few properties (float on water / casts shadow / ...)
45 * - a number of variant groups.
46 * Any actual object in game will pick a variant from each group (see ObjectEntry).
47 */
49{
50 friend CActorDef;
51
52 // See CopyWithQuality() below.
54public:
55 struct Anim
56 {
57 // constructor
58 Anim() : m_Frequency(0), m_Speed(1.f), m_ActionPos(-1.f), m_ActionPos2(-1.f), m_SoundPos(-1.f) {}
59 // name of the animation - "Idle", "Run", etc
61 // ID of the animation: if not empty, something specific to sync with props.
62 CStr m_ID = "";
64 // filename of the animation - manidle.psa, manrun.psa, etc
66 // animation speed, as specified in XML actor file
67 float m_Speed;
68 // fraction [0.0, 1.0] of the way through the animation that the interesting bit(s)
69 // happens, or -1.0 if unspecified
73 };
74
75 struct Prop
76 {
77 // constructor
78 Prop() : m_minHeight(0.f), m_maxHeight(0.f), m_selectable(true) {}
79 // name of the prop point to attach to - "Prop01", "Prop02", "Head", "LeftHand", etc ..
81 // name of the model file - art/actors/props/sword.xml or whatever
83 // allow the prop to ajust the height from minHeight to maxHeight relative to the main model
87 };
88
89 struct Samp
90 {
91 // identifier name of sampler in GLSL shaders
93 // path to load from
95 };
96
97 struct Decal
98 {
99 Decal() : m_SizeX(0.f), m_SizeZ(0.f), m_Angle(0.f), m_OffsetX(0.f), m_OffsetZ(0.f) {}
100
101 float m_SizeX;
102 float m_SizeZ;
103 float m_Angle;
106 };
107
108 struct Variant
109 {
111
112 CStr m_VariantName; // lowercase name
118
119 std::vector<Anim> m_Anims;
120 std::vector<Prop> m_Props;
121 std::vector<Samp> m_Samplers;
122 };
123
125 {
129 CStr color;
130 std::multimap<CStr, Prop> props;
131 std::multimap<CStr, Anim> anims;
132 std::multimap<CStr, Samp> samplers;
133 };
134
135 CObjectBase(CObjectManager& objectManager, CActorDef& actorDef, std::uint8_t QualityLevel);
136
137 // Returns a set of selection such that, added to initialSelections, CalculateVariationKey can proceed.
138 std::set<CStr> CalculateRandomRemainingSelections(uint32_t seed, const std::vector<std::set<CStr>>& initialSelections) const;
139
140 // Get the variation key (indices of chosen variants from each group)
141 // based on the selection strings.
142 // Should not have to make a random choice: the selections should be complete.
143 std::vector<std::uint8_t> CalculateVariationKey(const std::vector<const std::set<CStr>*>& selections) const;
144
145 // Get the final actor data, combining all selected variants
146 const Variation BuildVariation(const std::vector<std::uint8_t>& variationKey) const;
147
148 // Get a list of variant groups for this object, plus for all possible
149 // props. Duplicated groups are removed, if several props share the same
150 // variant names.
151 std::vector<std::vector<CStr> > GetVariantGroups() const;
152
153 // Return a string identifying this actor uniquely (includes quality level information);
154 const CStr& GetIdentifier() const;
155
156 /**
157 * Returns whether this object (including any possible props)
158 * uses the given file. (This is used for hotloading.)
159 */
160 bool UsesFile(const VfsPath& pathname) const;
161
162
163 struct {
164 // whether and how to adapt the rotation to the terrrain slope below
166 // cast shadows from this object
168 // float on top of water
171
172 // the material file
174
175 // Quality level - part of the data resource path.
176 std::uint8_t m_QualityLevel;
177
178private:
179 // Private interface for CActorDef/ObjectEntry
180
181 /**
182 * Acts as an explicit copy constructor, for a new quality level.
183 * Note that this does not reload the actor, so this setting will only change props.
184 */
185 std::unique_ptr<CObjectBase> CopyWithQuality(std::uint8_t newQualityLevel) const;
186
187 // A low-quality RNG like rand48 causes visible non-random patterns (particularly
188 // in large grids of the same actor with consecutive seeds, e.g. forests),
189 // so use a better one that appears to avoid those patterns
190 using rng_t = std::mt19937;
191 std::set<CStr> CalculateRandomRemainingSelections(rng_t& rng, const std::vector<std::set<CStr>>& initialSelections) const;
192
193 /**
194 * Get all quality levels at which this object changes (includes props).
195 * Intended to be called by CActorFef.
196 * @param splits - a sorted vector of unique quality splits.
197 */
198 void GetQualitySplits(std::vector<std::uint8_t>& splits) const;
199
200 [[nodiscard]] bool Load(const CXeromyces& XeroFile, const XMBElement& base);
201 [[nodiscard]] bool LoadVariant(const CXeromyces& XeroFile, const XMBElement& variant, Variant& currentVariant);
202
203private:
204 // Backref to the owning actor.
206
207 // Used to identify this actor uniquely in the ObjectManager (and for debug).
209
210 std::vector< std::vector<Variant> > m_VariantGroups;
212};
213
214/**
215 * Represents an actor file. Actors can contain various quality levels.
216 * An ActorDef maintains a CObjectBase for each specified quality level, and provides access to it.
217 */
219{
220 // Friend these three so they can use GetBase.
221 friend class CObjectManager;
222 friend class CObjectBase;
223 friend class CObjectEntry;
224
226public:
227
228 CActorDef(CObjectManager& objectManager);
229
230 std::vector<std::uint8_t> QualityLevels() const;
231
232 VfsPath GetPathname() const { return m_Pathname; }
233
234 /**
235 * Return a list of selections specifying a particular variant in all groups, based on the seed.
236 */
237 std::set<CStr> PickSelectionsAtRandom(uint32_t seed) const;
238
239// Interface accessible from CObjectManager / CObjectBase
240protected:
241 /**
242 * Return the Object base matching the given quality level.
243 */
244 const std::shared_ptr<CObjectBase>& GetBase(std::uint8_t QualityLevel) const;
245
246 /**
247 * Initialise this object by loading from the given file.
248 * Returns false on error.
249 */
250 bool Load(const VfsPath& pathname);
251
252 /**
253 * Initialise this object with a default placeholder actor,
254 * pretending to be the actor at pathname.
255 */
256 void LoadErrorPlaceholder(const VfsPath& pathname);
257
258 /**
259 * Returns whether this actor (including any possible props)
260 * uses the given file. (This is used for hotloading.)
261 */
262 bool UsesFile(const VfsPath& pathname) const;
263
264 // filename that this was loaded from
266
267private:
269
270 // std::shared_ptr to avoid issues during hotloading.
271 std::vector<std::shared_ptr<CObjectBase>> m_ObjectBases;
272
273 std::unordered_set<VfsPath> m_UsedFiles;
274};
275
276#endif
Represents an actor file.
Definition: ObjectBase.h:219
std::vector< std::shared_ptr< CObjectBase > > m_ObjectBases
Definition: ObjectBase.h:271
void LoadErrorPlaceholder(const VfsPath &pathname)
Initialise this object with a default placeholder actor, pretending to be the actor at pathname.
Definition: ObjectBase.cpp:1037
VfsPath GetPathname() const
Definition: ObjectBase.h:232
bool UsesFile(const VfsPath &pathname) const
Returns whether this actor (including any possible props) uses the given file.
Definition: ObjectBase.cpp:1032
const std::shared_ptr< CObjectBase > & GetBase(std::uint8_t QualityLevel) const
Return the Object base matching the given quality level.
Definition: ObjectBase.cpp:814
std::vector< std::uint8_t > QualityLevels() const
Definition: ObjectBase.cpp:805
NONCOPYABLE(CActorDef)
VfsPath m_Pathname
Definition: ObjectBase.h:265
std::set< CStr > PickSelectionsAtRandom(uint32_t seed) const
Return a list of selections specifying a particular variant in all groups, based on the seed.
Definition: ObjectBase.cpp:797
CActorDef(CObjectManager &objectManager)
Definition: ObjectBase.cpp:793
bool Load(const VfsPath &pathname)
Initialise this object by loading from the given file.
Definition: ObjectBase.cpp:828
CObjectManager & m_ObjectManager
Definition: ObjectBase.h:268
std::unordered_set< VfsPath > m_UsedFiles
Definition: ObjectBase.h:273
Maintains the tree of possible objects from a specific actor definition at a given quality level.
Definition: ObjectBase.h:49
NONCOPYABLE(CObjectBase)
std::set< CStr > CalculateRandomRemainingSelections(uint32_t seed, const std::vector< std::set< CStr > > &initialSelections) const
Definition: ObjectBase.cpp:522
VfsPath m_Material
Definition: ObjectBase.h:173
bool UsesFile(const VfsPath &pathname) const
Returns whether this object (including any possible props) uses the given file.
Definition: ObjectBase.cpp:787
std::mt19937 rng_t
Definition: ObjectBase.h:190
std::uint8_t m_QualityLevel
Definition: ObjectBase.h:176
friend CActorDef
Definition: ObjectBase.h:50
struct CObjectBase::@7 m_Properties
std::vector< std::vector< Variant > > m_VariantGroups
Definition: ObjectBase.h:210
const Variation BuildVariation(const std::vector< std::uint8_t > &variationKey) const
Definition: ObjectBase.cpp:447
CActorDef & m_ActorDef
Definition: ObjectBase.h:205
void GetQualitySplits(std::vector< std::uint8_t > &splits) const
Get all quality levels at which this object changes (includes props).
Definition: ObjectBase.cpp:737
std::vector< std::uint8_t > CalculateVariationKey(const std::vector< const std::set< CStr > * > &selections) const
Definition: ObjectBase.cpp:357
bool LoadVariant(const CXeromyces &XeroFile, const XMBElement &variant, Variant &currentVariant)
Definition: ObjectBase.cpp:168
bool m_CastShadows
Definition: ObjectBase.h:167
std::unique_ptr< CObjectBase > CopyWithQuality(std::uint8_t newQualityLevel) const
Acts as an explicit copy constructor, for a new quality level.
Definition: ObjectBase.cpp:75
const CStr & GetIdentifier() const
Definition: ObjectBase.cpp:782
CStr m_Identifier
Definition: ObjectBase.h:208
CStr m_AnchorType
Definition: ObjectBase.h:165
CObjectBase(CObjectManager &objectManager, CActorDef &actorDef, std::uint8_t QualityLevel)
Definition: ObjectBase.cpp:63
bool m_FloatOnWater
Definition: ObjectBase.h:169
std::vector< std::vector< CStr > > GetVariantGroups() const
Definition: ObjectBase.cpp:672
CObjectManager & m_ObjectManager
Definition: ObjectBase.h:211
bool Load(const CXeromyces &XeroFile, const XMBElement &base)
Definition: ObjectBase.cpp:85
Definition: ObjectEntry.h:39
Definition: ObjectManager.h:46
Interned 8-bit strings.
Definition: CStrIntern.h:42
Definition: Xeromyces.h:42
Definition: path.h:84
Definition: XMBData.h:135
Definition: ObjectBase.h:56
int m_Frequency
Definition: ObjectBase.h:63
float m_SoundPos
Definition: ObjectBase.h:72
float m_Speed
Definition: ObjectBase.h:67
float m_ActionPos2
Definition: ObjectBase.h:71
VfsPath m_FileName
Definition: ObjectBase.h:65
CStr m_ID
Definition: ObjectBase.h:62
float m_ActionPos
Definition: ObjectBase.h:70
CStr m_AnimName
Definition: ObjectBase.h:60
Anim()
Definition: ObjectBase.h:58
Definition: ObjectBase.h:98
float m_SizeX
Definition: ObjectBase.h:101
Decal()
Definition: ObjectBase.h:99
float m_Angle
Definition: ObjectBase.h:103
float m_SizeZ
Definition: ObjectBase.h:102
float m_OffsetZ
Definition: ObjectBase.h:105
float m_OffsetX
Definition: ObjectBase.h:104
Definition: ObjectBase.h:76
Prop()
Definition: ObjectBase.h:78
CStrW m_ModelName
Definition: ObjectBase.h:82
CStr m_PropPointName
Definition: ObjectBase.h:80
bool m_selectable
Definition: ObjectBase.h:86
float m_minHeight
Definition: ObjectBase.h:84
float m_maxHeight
Definition: ObjectBase.h:85
Definition: ObjectBase.h:90
CStrIntern m_SamplerName
Definition: ObjectBase.h:92
VfsPath m_SamplerFile
Definition: ObjectBase.h:94
Definition: ObjectBase.h:109
std::vector< Samp > m_Samplers
Definition: ObjectBase.h:121
CStr m_Color
Definition: ObjectBase.h:117
VfsPath m_Particles
Definition: ObjectBase.h:116
VfsPath m_ModelFilename
Definition: ObjectBase.h:114
int m_Frequency
Definition: ObjectBase.h:113
std::vector< Anim > m_Anims
Definition: ObjectBase.h:119
Variant()
Definition: ObjectBase.h:110
Decal m_Decal
Definition: ObjectBase.h:115
std::vector< Prop > m_Props
Definition: ObjectBase.h:120
CStr m_VariantName
Definition: ObjectBase.h:112
Definition: ObjectBase.h:125
std::multimap< CStr, Samp > samplers
Definition: ObjectBase.h:132
std::multimap< CStr, Prop > props
Definition: ObjectBase.h:130
CStr color
Definition: ObjectBase.h:129
std::multimap< CStr, Anim > anims
Definition: ObjectBase.h:131
Decal decal
Definition: ObjectBase.h:127
VfsPath particles
Definition: ObjectBase.h:128
VfsPath model
Definition: ObjectBase.h:126