Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ObjectBase.h
Go to the documentation of this file.
1 /* Copyright (C) 2013 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 class CModel;
22 class CSkeletonAnim;
23 class CObjectManager;
24 
25 #include <vector>
26 #include <set>
27 #include <map>
28 #include <boost/unordered_set.hpp>
29 #include "lib/file/vfs/vfs_path.h"
30 #include "ps/CStr.h"
31 
32 #include <boost/random/mersenne_twister.hpp>
33 
35 {
37 public:
38 
39  struct Anim
40  {
41  // constructor
42  Anim() : m_Speed(1.f), m_ActionPos(-1.f), m_ActionPos2(-1.f), m_SoundPos(-1.f) {}
43  // name of the animation - "Idle", "Run", etc
44  CStr m_AnimName;
45  // filename of the animation - manidle.psa, manrun.psa, etc
47  // animation speed, as specified in XML actor file
48  float m_Speed;
49  // fraction [0.0, 1.0] of the way through the animation that the interesting bit(s)
50  // happens, or -1.0 if unspecified
51  float m_ActionPos;
52  float m_ActionPos2;
53  float m_SoundPos;
54  };
55 
56  struct Prop
57  {
58  // constructor
59  Prop() : m_minHeight(0.f), m_maxHeight(0.f) {}
60  // name of the prop point to attach to - "Prop01", "Prop02", "Head", "LeftHand", etc ..
62  // name of the model file - art/actors/props/sword.xml or whatever
63  CStrW m_ModelName;
64  // allow the prop to ajust the height from minHeight to maxHeight relative to the main model
65  float m_minHeight;
66  float m_maxHeight;
67  };
68 
69  struct Samp
70  {
71  // identifier name of sampler in GLSL shaders
73  // path to load from
75  };
76 
77  struct Decal
78  {
79  Decal() : m_SizeX(0.f), m_SizeZ(0.f), m_Angle(0.f), m_OffsetX(0.f), m_OffsetZ(0.f) {}
80 
81  float m_SizeX;
82  float m_SizeZ;
83  float m_Angle;
84  float m_OffsetX;
85  float m_OffsetZ;
86  };
87 
88  struct Variant
89  {
90  Variant() : m_Frequency(0) {}
91 
92  CStr m_VariantName; // lowercase name
97  CStr m_Color;
98 
99  std::vector<Anim> m_Anims;
100  std::vector<Prop> m_Props;
101  std::vector<Samp> m_Samplers;
102  };
103 
104  struct Variation
105  {
109  CStr color;
110  std::multimap<CStr, Prop> props;
111  std::multimap<CStr, Anim> anims;
112  std::multimap<CStr, Samp> samplers;
113  };
114 
115  CObjectBase(CObjectManager& objectManager);
116 
117  // Get the variation key (indices of chosen variants from each group)
118  // based on the selection strings
119  std::vector<u8> CalculateVariationKey(const std::vector<std::set<CStr> >& selections);
120 
121  // Get the final actor data, combining all selected variants
122  const Variation BuildVariation(const std::vector<u8>& variationKey);
123 
124  // Get a set of selection strings that are complete enough to specify an
125  // exact variation of the actor, using the initial selections wherever possible
126  // and choosing randomly where a choice is necessary.
127  std::set<CStr> CalculateRandomVariation(uint32_t seed, const std::set<CStr>& initialSelections);
128 
129  // Given a prioritized vector of selection string sets that partially specify
130  // a variation, calculates a remaining set of selection strings such that the resulting
131  // set merged with the initial selections fully specifies an exact variation of
132  // the actor. The resulting selections are selected randomly, but only where a choice
133  // is necessary (i.e. where there are multiple variants but the initial selections,
134  // applied in priority order, fail to select one).
135  std::set<CStr> CalculateRandomRemainingSelections(uint32_t seed, const std::vector<std::set<CStr> >& initialSelections);
136 
137  // Get a list of variant groups for this object, plus for all possible
138  // props. Duplicated groups are removed, if several props share the same
139  // variant names.
140  std::vector<std::vector<CStr> > GetVariantGroups() const;
141 
142  /**
143  * Initialise this object by loading from the given file.
144  * Returns false on error.
145  */
146  bool Load(const VfsPath& pathname);
147 
148  /**
149  * Reload this object from the file that it was previously loaded from.
150  * Returns false on error.
151  */
152  bool Reload();
153 
154  /**
155  * Returns whether this object (including any possible props)
156  * uses the given file. (This is used for hotloading.)
157  */
158  bool UsesFile(const VfsPath& pathname);
159 
160  // filename that this was loaded from
162 
163  // short human-readable name
164  CStrW m_ShortName;
165 
166  struct {
167  // cast shadows from this object
169  // float on top of water
171  } m_Properties;
172 
173  // the material file
175 
176 private:
177  // A low-quality RNG like rand48 causes visible non-random patterns (particularly
178  // in large grids of the same actor with consecutive seeds, e.g. forests),
179  // so use a better one that appears to avoid those patterns
180  typedef boost::mt19937 rng_t;
181 
182  std::set<CStr> CalculateRandomRemainingSelections(rng_t& rng, const std::vector<std::set<CStr> >& initialSelections);
183 
184  std::vector< std::vector<Variant> > m_VariantGroups;
186 
187  boost::unordered_set<VfsPath> m_UsedFiles;
188 };
189 
190 #endif
bool UsesFile(const VfsPath &pathname)
Returns whether this object (including any possible props) uses the given file.
Definition: ObjectBase.cpp:296
VfsPath m_ModelFilename
Definition: ObjectBase.h:94
VfsPath m_Material
Definition: ObjectBase.h:174
bool Load(const VfsPath &pathname)
Initialise this object by loading from the given file.
Definition: ObjectBase.cpp:41
NONCOPYABLE(CObjectBase)
CObjectBase(CObjectManager &objectManager)
Definition: ObjectBase.cpp:34
std::vector< Prop > m_Props
Definition: ObjectBase.h:100
VfsPath m_FileName
Definition: ObjectBase.h:46
std::vector< std::vector< Variant > > m_VariantGroups
Definition: ObjectBase.h:184
std::vector< u8 > CalculateVariationKey(const std::vector< std::set< CStr > > &selections)
Definition: ObjectBase.cpp:301
std::multimap< CStr, Samp > samplers
Definition: ObjectBase.h:112
Definition: path.h:75
boost::mt19937 rng
Random number generator (Boost Mersenne Twister)
Definition: Noise.cpp:34
std::vector< Anim > m_Anims
Definition: ObjectBase.h:99
std::vector< std::vector< CStr > > GetVariantGroups() const
Definition: ObjectBase.cpp:615
std::vector< Samp > m_Samplers
Definition: ObjectBase.h:101
struct CObjectBase::@7 m_Properties
VfsPath m_Pathname
Definition: ObjectBase.h:161
std::multimap< CStr, Prop > props
Definition: ObjectBase.h:110
CObjectManager & m_ObjectManager
Definition: ObjectBase.h:185
std::set< CStr > CalculateRandomVariation(uint32_t seed, const std::set< CStr > &initialSelections)
Definition: ObjectBase.cpp:467
bool Reload()
Reload this object from the file that it was previously loaded from.
Definition: ObjectBase.cpp:291
std::multimap< CStr, Anim > anims
Definition: ObjectBase.h:111
unsigned int uint32_t
Definition: wposix_types.h:53
bool m_CastShadows
Definition: ObjectBase.h:168
const Variation BuildVariation(const std::vector< u8 > &variationKey)
Definition: ObjectBase.cpp:393
bool m_FloatOnWater
Definition: ObjectBase.h:170
boost::mt19937 rng_t
Definition: ObjectBase.h:180
Definition: Model.h:50
VfsPath m_SamplerFile
Definition: ObjectBase.h:74
CStrW m_ShortName
Definition: ObjectBase.h:164
std::set< CStr > CalculateRandomRemainingSelections(uint32_t seed, const std::vector< std::set< CStr > > &initialSelections)
Definition: ObjectBase.cpp:478
boost::unordered_set< VfsPath > m_UsedFiles
Definition: ObjectBase.h:187