Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
ObjectManager.h
Go to the documentation of this file.
1/* Copyright (C) 2021 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_OBJECTMANAGER
19#define INCLUDED_OBJECTMANAGER
20
21#include "ps/CStr.h"
23
24#include <set>
25#include <map>
26#include <memory>
27#include <unordered_map>
28#include <vector>
29
30class CActorDef;
31class CConfigDBHook;
32class CMeshManager;
33class CObjectBase;
34class CObjectEntry;
36class CSimulation2;
37class CTerrain;
38
39///////////////////////////////////////////////////////////////////////////////////////////
40// CObjectManager: manager class for all possible actor types
42{
44public:
45 // Unique identifier of an actor variation
46 struct ObjectKey
47 {
48 ObjectKey(const CStr& identifier, const std::vector<u8>& var)
49 : ObjectBaseIdentifier(identifier), ActorVariation(var) {}
50
51 bool operator< (const CObjectManager::ObjectKey& a) const;
52
53 private:
55 std::vector<u8> ActorVariation;
56 };
57
58 /**
59 * Governs how random variants are selected by ObjectBase
60 */
62 {
63 NONE,
64 LIMITED,
65 FULL
66 };
67
68public:
69
70 // constructor, destructor
71 CObjectManager(CMeshManager& meshManager, CSkeletonAnimManager& skeletonAnimManager, CSimulation2& simulation);
73
74 // Provide access to the manager classes for meshes and animations - they're
75 // needed when objects are being created and so this seems like a convenient
76 // place to centralise access.
79
80 void UnloadObjects();
81
82 /**
83 * Get the actor definition for the given path name.
84 * If the actor cannot be loaded, this will return a placeholder actor.
85 * @return Success/failure boolean and a valid actor definition.
86 */
87 std::pair<bool, CActorDef&> FindActorDef(const CStrW& actorName);
88
89 /**
90 * Get the object entry for a given actor & the given selections list.
91 * @param selections - a possibly incomplete list of selections.
92 * @param seed - the randomness seed to use to complete the random selections.
93 */
94 CObjectEntry* FindObjectVariation(const CActorDef* actor, const std::vector<std::set<CStr>>& selections, uint32_t seed);
95
96 /**
97 * @see FindObjectVariation.
98 * These take a complete selection. These are pointers to sets that are
99 * guaranteed to exist (pointers are used to avoid copying the sets).
100 */
101 CObjectEntry* FindObjectVariation(const std::shared_ptr<CObjectBase>& base, const std::vector<const std::set<CStr>*>& completeSelections);
102 CObjectEntry* FindObjectVariation(const CStrW& objname, const std::vector<const std::set<CStr>*>& completeSelections);
103
104 /**
105 * Get the terrain object that actors managed by this manager should be linked
106 * with (primarily for the purpose of decals)
107 */
109
111
112 /**
113 * Reload any scripts that were loaded from the given filename.
114 * (This is used to implement hotloading.)
115 */
116 Status ReloadChangedFile(const VfsPath& path);
117
118 /**
119 * Reload actors that have a quality setting. Used when changing the actor quality.
120 */
121 void ActorQualityChanged();
122
123 /**
124 * Reload actors. Used when changing the variant diversity.
125 */
127
131
133 std::unique_ptr<CConfigDBHook> m_QualityHook;
134
136 std::unique_ptr<CConfigDBHook> m_VariantDiversityHook;
137
138 template<typename T>
140 {
141 Hotloadable() = default;
142 Hotloadable(std::unique_ptr<T>&& ptr) : obj(std::move(ptr)) {}
143 bool outdated = false;
144 std::unique_ptr<T> obj;
145 };
146 // TODO: define a hash and switch to unordered_map
147 std::map<ObjectKey, Hotloadable<CObjectEntry>> m_Objects;
148 std::unordered_map<CStrW, Hotloadable<CActorDef>> m_ActorDefs;
149};
150
151#endif
Represents an actor file.
Definition: ObjectBase.h:215
Definition: ConfigDB.h:212
Definition: MeshManager.h:32
Maintains the tree of possible objects from a specific actor definition at a given quality level.
Definition: ObjectBase.h:47
Definition: ObjectEntry.h:38
Definition: ObjectManager.h:42
CMeshManager & m_MeshManager
Definition: ObjectManager.h:128
CMeshManager & GetMeshManager() const
Definition: ObjectManager.h:77
CObjectManager(CMeshManager &meshManager, CSkeletonAnimManager &skeletonAnimManager, CSimulation2 &simulation)
Definition: ObjectManager.cpp:49
std::map< ObjectKey, Hotloadable< CObjectEntry > > m_Objects
Definition: ObjectManager.h:147
CSkeletonAnimManager & m_SkeletonAnimManager
Definition: ObjectManager.h:129
std::unordered_map< CStrW, Hotloadable< CActorDef > > m_ActorDefs
Definition: ObjectManager.h:148
CSimulation2 & m_Simulation
Definition: ObjectManager.h:130
CObjectEntry * FindObjectVariation(const CActorDef *actor, const std::vector< std::set< CStr > > &selections, uint32_t seed)
Get the object entry for a given actor & the given selections list.
Definition: ObjectManager.cpp:94
void ActorQualityChanged()
Reload actors that have a quality setting.
Definition: ObjectManager.cpp:196
VariantDiversity
Governs how random variants are selected by ObjectBase.
Definition: ObjectManager.h:62
VariantDiversity GetVariantDiversity() const
Definition: ObjectManager.cpp:147
VariantDiversity m_VariantDiversity
Definition: ObjectManager.h:135
void UnloadObjects()
Definition: ObjectManager.cpp:152
~CObjectManager()
Definition: ObjectManager.cpp:61
std::pair< bool, CActorDef & > FindActorDef(const CStrW &actorName)
Get the actor definition for the given path name.
Definition: ObjectManager.cpp:68
u8 m_QualityLevel
Definition: ObjectManager.h:132
std::unique_ptr< CConfigDBHook > m_VariantDiversityHook
Definition: ObjectManager.h:136
NONCOPYABLE(CObjectManager)
std::unique_ptr< CConfigDBHook > m_QualityHook
Definition: ObjectManager.h:133
Status ReloadChangedFile(const VfsPath &path)
Reload any scripts that were loaded from the given filename.
Definition: ObjectManager.cpp:158
CTerrain * GetTerrain()
Get the terrain object that actors managed by this manager should be linked with (primarily for the p...
Definition: ObjectManager.cpp:139
void VariantDiversityChanged()
Reload actors.
Definition: ObjectManager.cpp:214
CObjectEntry * FindObjectVariation(const CStrW &objname, const std::vector< const std::set< CStr > * > &completeSelections)
CSkeletonAnimManager & GetSkeletonAnimManager() const
Definition: ObjectManager.h:78
Public API for simulation system.
Definition: Simulation2.h:47
Definition: SkeletonAnimManager.h:39
Definition: Terrain.h:52
Definition: path.h:80
Definition: ShaderDefines.cpp:31
i64 Status
Error handling system.
Definition: status.h:173
Definition: ObjectManager.h:140
bool outdated
Definition: ObjectManager.h:143
std::unique_ptr< T > obj
Definition: ObjectManager.h:144
Hotloadable(std::unique_ptr< T > &&ptr)
Definition: ObjectManager.h:142
Definition: ObjectManager.h:47
CStr ObjectBaseIdentifier
Definition: ObjectManager.h:54
ObjectKey(const CStr &identifier, const std::vector< u8 > &var)
Definition: ObjectManager.h:48
std::vector< u8 > ActorVariation
Definition: ObjectManager.h:55
bool operator<(const CObjectManager::ObjectKey &a) const
Definition: ObjectManager.cpp:34
uint8_t u8
Definition: types.h:37
unsigned int uint32_t
Definition: wposix_types.h:53