Line data Source code
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;
35 : class CSkeletonAnimManager;
36 : class CSimulation2;
37 : class CTerrain;
38 :
39 : ///////////////////////////////////////////////////////////////////////////////////////////
40 : // CObjectManager: manager class for all possible actor types
41 : class CObjectManager
42 : {
43 : NONCOPYABLE(CObjectManager);
44 : public:
45 : // Unique identifier of an actor variation
46 0 : struct ObjectKey
47 : {
48 0 : ObjectKey(const CStr& identifier, const std::vector<u8>& var)
49 0 : : ObjectBaseIdentifier(identifier), ActorVariation(var) {}
50 :
51 : bool operator< (const CObjectManager::ObjectKey& a) const;
52 :
53 : private:
54 : CStr ObjectBaseIdentifier;
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);
72 : ~CObjectManager();
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.
77 0 : CMeshManager& GetMeshManager() const { return m_MeshManager; }
78 0 : CSkeletonAnimManager& GetSkeletonAnimManager() const { return m_SkeletonAnimManager; }
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 :
110 : VariantDiversity GetVariantDiversity() const;
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 : */
126 : void VariantDiversityChanged();
127 :
128 : CMeshManager& m_MeshManager;
129 : CSkeletonAnimManager& m_SkeletonAnimManager;
130 : CSimulation2& m_Simulation;
131 :
132 : u8 m_QualityLevel = 100;
133 : std::unique_ptr<CConfigDBHook> m_QualityHook;
134 :
135 : VariantDiversity m_VariantDiversity = VariantDiversity::FULL;
136 : std::unique_ptr<CConfigDBHook> m_VariantDiversityHook;
137 :
138 : template<typename T>
139 0 : struct Hotloadable
140 : {
141 : Hotloadable() = default;
142 0 : 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
|