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