Pyrogenesis  trunk
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"
22 #include "lib/file/vfs/vfs_path.h"
23 
24 #include <set>
25 #include <map>
26 #include <memory>
27 #include <unordered_map>
28 #include <vector>
29 
30 class CActorDef;
31 class CConfigDBHook;
32 class CMeshManager;
33 class CObjectBase;
34 class CObjectEntry;
36 class CSimulation2;
37 class CTerrain;
38 
39 ///////////////////////////////////////////////////////////////////////////////////////////
40 // CObjectManager: manager class for all possible actor types
42 {
44 public:
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  */
61  enum class VariantDiversity
62  {
63  NONE,
64  LIMITED,
65  FULL
66  };
67 
68 public:
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  */
108  CTerrain* GetTerrain();
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>
139  struct Hotloadable
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
VariantDiversity GetVariantDiversity() const
Definition: ObjectManager.cpp:147
std::map< ObjectKey, Hotloadable< CObjectEntry > > m_Objects
Definition: ObjectManager.h:147
CMeshManager & GetMeshManager() const
Definition: ObjectManager.h:77
bool operator<(const CObjectManager::ObjectKey &a) const
Definition: ObjectManager.cpp:34
void VariantDiversityChanged()
Reload actors.
Definition: ObjectManager.cpp:214
u8 m_QualityLevel
Definition: ObjectManager.h:132
Definition: ObjectManager.h:41
std::vector< u8 > ActorVariation
Definition: ObjectManager.h:55
Definition: Terrain.h:51
Definition: ShaderDefines.cpp:30
CStr ObjectBaseIdentifier
Definition: ObjectManager.h:54
Represents an actor file.
Definition: ObjectBase.h:214
std::unique_ptr< CConfigDBHook > m_QualityHook
Definition: ObjectManager.h:133
VariantDiversity
Governs how random variants are selected by ObjectBase.
Definition: ObjectManager.h:61
uint8_t u8
Definition: types.h:37
Hotloadable(std::unique_ptr< T > &&ptr)
Definition: ObjectManager.h:142
Public API for simulation system.
Definition: Simulation2.h:46
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
std::unordered_map< CStrW, Hotloadable< CActorDef > > m_ActorDefs
Definition: ObjectManager.h:148
CMeshManager & m_MeshManager
Definition: ObjectManager.h:128
std::pair< bool, CActorDef & > FindActorDef(const CStrW &actorName)
Get the actor definition for the given path name.
Definition: ObjectManager.cpp:68
Definition: path.h:79
~CObjectManager()
Definition: ObjectManager.cpp:61
CSkeletonAnimManager & GetSkeletonAnimManager() const
Definition: ObjectManager.h:78
CSkeletonAnimManager & m_SkeletonAnimManager
Definition: ObjectManager.h:129
i64 Status
Error handling system.
Definition: status.h:169
VariantDiversity m_VariantDiversity
Definition: ObjectManager.h:135
void UnloadObjects()
Definition: ObjectManager.cpp:152
void ActorQualityChanged()
Reload actors that have a quality setting.
Definition: ObjectManager.cpp:196
Definition: SkeletonAnimManager.h:38
ObjectKey(const CStr &identifier, const std::vector< u8 > &var)
Definition: ObjectManager.h:48
unsigned int uint32_t
Definition: wposix_types.h:53
Maintains the tree of possible objects from a specific actor definition at a given quality level...
Definition: ObjectBase.h:46
Definition: MeshManager.h:31
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
NONCOPYABLE(CObjectManager)
Definition: ConfigDB.h:211
std::unique_ptr< T > obj
Definition: ObjectManager.h:144
Definition: ObjectManager.h:139
CObjectManager(CMeshManager &meshManager, CSkeletonAnimManager &skeletonAnimManager, CSimulation2 &simulation)
Definition: ObjectManager.cpp:49
CSimulation2 & m_Simulation
Definition: ObjectManager.h:130
Definition: ObjectEntry.h:38
std::unique_ptr< CConfigDBHook > m_VariantDiversityHook
Definition: ObjectManager.h:136
Definition: ObjectManager.h:46