[Gameplay] Reduce loom cost
[0ad.git] / source / graphics / ObjectManager.h
blobb32056c93aa9c3c956836968a516efc66b86ce04
1 /* Copyright (C) 2021 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
18 #ifndef INCLUDED_OBJECTMANAGER
19 #define INCLUDED_OBJECTMANAGER
21 #include "ps/CStr.h"
22 #include "lib/file/vfs/vfs_path.h"
24 #include <set>
25 #include <map>
26 #include <memory>
27 #include <unordered_map>
28 #include <vector>
30 class CActorDef;
31 class CConfigDBHook;
32 class CMeshManager;
33 class CObjectBase;
34 class CObjectEntry;
35 class CSkeletonAnimManager;
36 class CSimulation2;
37 class CTerrain;
39 ///////////////////////////////////////////////////////////////////////////////////////////
40 // CObjectManager: manager class for all possible actor types
41 class CObjectManager
43 NONCOPYABLE(CObjectManager);
44 public:
45 // Unique identifier of an actor variation
46 struct ObjectKey
48 ObjectKey(const CStr& identifier, const std::vector<u8>& var)
49 : ObjectBaseIdentifier(identifier), ActorVariation(var) {}
51 bool operator< (const CObjectManager::ObjectKey& a) const;
53 private:
54 CStr ObjectBaseIdentifier;
55 std::vector<u8> ActorVariation;
58 /**
59 * Governs how random variants are selected by ObjectBase
61 enum class VariantDiversity
63 NONE,
64 LIMITED,
65 FULL
68 public:
70 // constructor, destructor
71 CObjectManager(CMeshManager& meshManager, CSkeletonAnimManager& skeletonAnimManager, CSimulation2& simulation);
72 ~CObjectManager();
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 CMeshManager& GetMeshManager() const { return m_MeshManager; }
78 CSkeletonAnimManager& GetSkeletonAnimManager() const { return m_SkeletonAnimManager; }
80 void UnloadObjects();
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.
87 std::pair<bool, CActorDef&> FindActorDef(const CStrW& actorName);
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.
94 CObjectEntry* FindObjectVariation(const CActorDef* actor, const std::vector<std::set<CStr>>& selections, uint32_t seed);
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).
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);
105 * Get the terrain object that actors managed by this manager should be linked
106 * with (primarily for the purpose of decals)
108 CTerrain* GetTerrain();
110 VariantDiversity GetVariantDiversity() const;
113 * Reload any scripts that were loaded from the given filename.
114 * (This is used to implement hotloading.)
116 Status ReloadChangedFile(const VfsPath& path);
119 * Reload actors that have a quality setting. Used when changing the actor quality.
121 void ActorQualityChanged();
124 * Reload actors. Used when changing the variant diversity.
126 void VariantDiversityChanged();
128 CMeshManager& m_MeshManager;
129 CSkeletonAnimManager& m_SkeletonAnimManager;
130 CSimulation2& m_Simulation;
132 u8 m_QualityLevel = 100;
133 std::unique_ptr<CConfigDBHook> m_QualityHook;
135 VariantDiversity m_VariantDiversity = VariantDiversity::FULL;
136 std::unique_ptr<CConfigDBHook> m_VariantDiversityHook;
138 template<typename T>
139 struct Hotloadable
141 Hotloadable() = default;
142 Hotloadable(std::unique_ptr<T>&& ptr) : obj(std::move(ptr)) {}
143 bool outdated = false;
144 std::unique_ptr<T> obj;
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;
151 #endif