LCOV - code coverage report
Current view: top level - source/graphics - ObjectBase.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 2 7 28.6 %
Date: 2026-12-03 01:19:39 Functions: 1 2 50.0 %

          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_OBJECTBASE
      19             : #define INCLUDED_OBJECTBASE
      20             : 
      21             : #include "lib/code_annotation.h"
      22             : #include "lib/file/vfs/vfs_path.h"
      23             : #include "lib/path.h"
      24             : #include "ps/CStr.h"
      25             : #include "ps/CStrIntern.h"
      26             : 
      27             : #include <cstdint>
      28             : #include <map>
      29             : #include <memory>
      30             : #include <random>
      31             : #include <set>
      32             : #include <unordered_set>
      33             : #include <vector>
      34             : 
      35             : class CActorDef;
      36             : class CObjectManager;
      37             : class CXeromyces;
      38             : class XMBElement;
      39             : 
      40             : /**
      41             :  * Maintains the tree of possible objects from a specific actor definition at a given quality level.
      42             :  * An Object Base is made of:
      43             :  *  - a material
      44             :  *  - a few properties (float on water / casts shadow / ...)
      45             :  *  - a number of variant groups.
      46             :  * Any actual object in game will pick a variant from each group (see ObjectEntry).
      47             :  */
      48             : class CObjectBase
      49             : {
      50             :     friend CActorDef;
      51             : 
      52             :     // See CopyWithQuality() below.
      53             :     NONCOPYABLE(CObjectBase);
      54             : public:
      55             :     struct Anim
      56             :     {
      57             :         // constructor
      58           0 :         Anim() : m_Frequency(0), m_Speed(1.f), m_ActionPos(-1.f), m_ActionPos2(-1.f), m_SoundPos(-1.f) {}
      59             :         // name of the animation - "Idle", "Run", etc
      60             :         CStr m_AnimName;
      61             :         // ID of the animation: if not empty, something specific to sync with props.
      62             :         CStr m_ID = "";
      63             :         int m_Frequency;
      64             :         // filename of the animation - manidle.psa, manrun.psa, etc
      65             :         VfsPath m_FileName;
      66             :         // animation speed, as specified in XML actor file
      67             :         float m_Speed;
      68             :         // fraction [0.0, 1.0] of the way through the animation that the interesting bit(s)
      69             :         // happens, or -1.0 if unspecified
      70             :         float m_ActionPos;
      71             :         float m_ActionPos2;
      72             :         float m_SoundPos;
      73             :     };
      74             : 
      75             :     struct Prop
      76             :     {
      77             :         // constructor
      78           0 :         Prop() : m_minHeight(0.f), m_maxHeight(0.f), m_selectable(true) {}
      79             :         // name of the prop point to attach to - "Prop01", "Prop02", "Head", "LeftHand", etc ..
      80             :         CStr m_PropPointName;
      81             :         // name of the model file - art/actors/props/sword.xml or whatever
      82             :         CStrW m_ModelName;
      83             :         // allow the prop to ajust the height from minHeight to maxHeight relative to the main model
      84             :         float m_minHeight;
      85             :         float m_maxHeight;
      86             :         bool m_selectable;
      87             :     };
      88             : 
      89           0 :     struct Samp
      90             :     {
      91             :         // identifier name of sampler in GLSL shaders
      92             :         CStrIntern m_SamplerName;
      93             :         // path to load from
      94             :         VfsPath m_SamplerFile;
      95             :     };
      96             : 
      97             :     struct Decal
      98             :     {
      99           0 :         Decal() : m_SizeX(0.f), m_SizeZ(0.f), m_Angle(0.f), m_OffsetX(0.f), m_OffsetZ(0.f) {}
     100             : 
     101             :         float m_SizeX;
     102             :         float m_SizeZ;
     103             :         float m_Angle;
     104             :         float m_OffsetX;
     105             :         float m_OffsetZ;
     106             :     };
     107             : 
     108             :     struct Variant
     109             :     {
     110           1 :         Variant() : m_Frequency(0) {}
     111             : 
     112             :         CStr m_VariantName; // lowercase name
     113             :         int m_Frequency;
     114             :         VfsPath m_ModelFilename;
     115             :         Decal m_Decal;
     116             :         VfsPath m_Particles;
     117             :         CStr m_Color;
     118             : 
     119             :         std::vector<Anim> m_Anims;
     120             :         std::vector<Prop> m_Props;
     121             :         std::vector<Samp> m_Samplers;
     122             :     };
     123             : 
     124             :     struct Variation
     125             :     {
     126             :         VfsPath model;
     127             :         Decal decal;
     128             :         VfsPath particles;
     129             :         CStr color;
     130             :         std::multimap<CStr, Prop> props;
     131             :         std::multimap<CStr, Anim> anims;
     132             :         std::multimap<CStr, Samp> samplers;
     133             :     };
     134             : 
     135             :     CObjectBase(CObjectManager& objectManager, CActorDef& actorDef, std::uint8_t QualityLevel);
     136             : 
     137             :     // Returns a set of selection such that, added to initialSelections, CalculateVariationKey can proceed.
     138             :     std::set<CStr> CalculateRandomRemainingSelections(uint32_t seed, const std::vector<std::set<CStr>>& initialSelections) const;
     139             : 
     140             :     // Get the variation key (indices of chosen variants from each group)
     141             :     // based on the selection strings.
     142             :     // Should not have to make a random choice: the selections should be complete.
     143             :     std::vector<std::uint8_t> CalculateVariationKey(const std::vector<const std::set<CStr>*>& selections) const;
     144             : 
     145             :     // Get the final actor data, combining all selected variants
     146             :     const Variation BuildVariation(const std::vector<std::uint8_t>& variationKey) const;
     147             : 
     148             :     // Get a list of variant groups for this object, plus for all possible
     149             :     // props. Duplicated groups are removed, if several props share the same
     150             :     // variant names.
     151             :     std::vector<std::vector<CStr> > GetVariantGroups() const;
     152             : 
     153             :     // Return a string identifying this actor uniquely (includes quality level information);
     154             :     const CStr& GetIdentifier() const;
     155             : 
     156             :     /**
     157             :      * Returns whether this object (including any possible props)
     158             :      * uses the given file. (This is used for hotloading.)
     159             :      */
     160             :     bool UsesFile(const VfsPath& pathname) const;
     161             : 
     162             : 
     163           1 :     struct {
     164             :         // whether and how to adapt the rotation to the terrrain slope below
     165             :         CStr m_AnchorType;
     166             :         // cast shadows from this object
     167             :         bool m_CastShadows;
     168             :         // float on top of water
     169             :         bool m_FloatOnWater;
     170             :     } m_Properties;
     171             : 
     172             :     // the material file
     173             :     VfsPath m_Material;
     174             : 
     175             :     // Quality level - part of the data resource path.
     176             :     std::uint8_t m_QualityLevel;
     177             : 
     178             : private:
     179             :     // Private interface for CActorDef/ObjectEntry
     180             : 
     181             :     /**
     182             :      * Acts as an explicit copy constructor, for a new quality level.
     183             :      * Note that this does not reload the actor, so this setting will only change props.
     184             :      */
     185             :     std::unique_ptr<CObjectBase> CopyWithQuality(std::uint8_t newQualityLevel) const;
     186             : 
     187             :     // A low-quality RNG like rand48 causes visible non-random patterns (particularly
     188             :     // in large grids of the same actor with consecutive seeds, e.g. forests),
     189             :     // so use a better one that appears to avoid those patterns
     190             :     using rng_t = std::mt19937;
     191             :     std::set<CStr> CalculateRandomRemainingSelections(rng_t& rng, const std::vector<std::set<CStr>>& initialSelections) const;
     192             : 
     193             :     /**
     194             :      * Get all quality levels at which this object changes (includes props).
     195             :      * Intended to be called by CActorFef.
     196             :      * @param splits - a sorted vector of unique quality splits.
     197             :      */
     198             :     void GetQualitySplits(std::vector<std::uint8_t>& splits) const;
     199             : 
     200             :     [[nodiscard]] bool Load(const CXeromyces& XeroFile, const XMBElement& base);
     201             :     [[nodiscard]] bool LoadVariant(const CXeromyces& XeroFile, const XMBElement& variant, Variant& currentVariant);
     202             : 
     203             : private:
     204             :     // Backref to the owning actor.
     205             :     CActorDef& m_ActorDef;
     206             : 
     207             :     // Used to identify this actor uniquely in the ObjectManager (and for debug).
     208             :     CStr m_Identifier;
     209             : 
     210             :     std::vector< std::vector<Variant> > m_VariantGroups;
     211             :     CObjectManager& m_ObjectManager;
     212             : };
     213             : 
     214             : /**
     215             :  * Represents an actor file. Actors can contain various quality levels.
     216             :  * An ActorDef maintains a CObjectBase for each specified quality level, and provides access to it.
     217             :  */
     218             : class CActorDef
     219             : {
     220             :     // Friend these three so they can use GetBase.
     221             :     friend class CObjectManager;
     222             :     friend class CObjectBase;
     223             :     friend class CObjectEntry;
     224             : 
     225             :     NONCOPYABLE(CActorDef);
     226             : public:
     227             : 
     228             :     CActorDef(CObjectManager& objectManager);
     229             : 
     230             :     std::vector<std::uint8_t> QualityLevels() const;
     231             : 
     232           0 :     VfsPath GetPathname() const { return m_Pathname; }
     233             : 
     234             :     /**
     235             :      * Return a list of selections specifying a particular variant in all groups, based on the seed.
     236             :      */
     237             :     std::set<CStr> PickSelectionsAtRandom(uint32_t seed) const;
     238             : 
     239             : // Interface accessible from CObjectManager / CObjectBase
     240             : protected:
     241             :     /**
     242             :      * Return the Object base matching the given quality level.
     243             :      */
     244             :     const std::shared_ptr<CObjectBase>& GetBase(std::uint8_t QualityLevel) const;
     245             : 
     246             :     /**
     247             :      * Initialise this object by loading from the given file.
     248             :      * Returns false on error.
     249             :      */
     250             :     bool Load(const VfsPath& pathname);
     251             : 
     252             :     /**
     253             :      * Initialise this object with a default placeholder actor,
     254             :      * pretending to be the actor at pathname.
     255             :      */
     256             :     void LoadErrorPlaceholder(const VfsPath& pathname);
     257             : 
     258             :     /**
     259             :      * Returns whether this actor (including any possible props)
     260             :      * uses the given file. (This is used for hotloading.)
     261             :      */
     262             :     bool UsesFile(const VfsPath& pathname) const;
     263             : 
     264             :     // filename that this was loaded from
     265             :     VfsPath m_Pathname;
     266             : 
     267             : private:
     268             :     CObjectManager& m_ObjectManager;
     269             : 
     270             :     // std::shared_ptr to avoid issues during hotloading.
     271             :     std::vector<std::shared_ptr<CObjectBase>> m_ObjectBases;
     272             : 
     273             :     std::unordered_set<VfsPath> m_UsedFiles;
     274             : };
     275             : 
     276             : #endif

Generated by: LCOV version 1.16