Thing.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: Thing.h //////////////////////////////////////////////////////////////
  24. //-----------------------------------------------------------------------------
  25. //
  26. // Westwood Studios Pacific.
  27. //
  28. // Confidential Information
  29. // Copyright (C) 2001 - All Rights Reserved
  30. //
  31. //-----------------------------------------------------------------------------
  32. //
  33. // Project: RTS3
  34. //
  35. // File name: Thing.h
  36. //
  37. // Created: Colin Day, May 2001
  38. //
  39. // Desc: Things are the base class for objects and drawables, objects
  40. // are logic side representations while drawables are client
  41. // side. Common data will be held in the Thing defined here
  42. // and systems that need to work with both of them will work with
  43. // "Things"
  44. //
  45. //-----------------------------------------------------------------------------
  46. #pragma once
  47. #ifndef __THING_H_
  48. #define __THING_H_
  49. //-----------------------------------------------------------------------------
  50. //-----------------------------------------------------------------------------
  51. // Includes
  52. //-----------------------------------------------------------------------------
  53. #include "Common/GameMemory.h"
  54. #include "Common/KindOf.h"
  55. #include "Common/OVERRIDE.h"
  56. #include "WWMath/Matrix3D.h" ///< @todo Decide if we're keeping the WWMath libs (MSB)
  57. //-----------------------------------------------------------------------------
  58. // Forward References
  59. //-----------------------------------------------------------------------------
  60. class Object;
  61. class AIObject;
  62. class Drawable;
  63. class Team;
  64. class ThingTemplate;
  65. //-----------------------------------------------------------------------------
  66. // Type Defines
  67. //-----------------------------------------------------------------------------
  68. //=====================================
  69. // class Thing
  70. //=====================================
  71. /** A thing is the common base class for objects and drawables. It will
  72. * hold common information to both and systems that need to work with both
  73. * objects and drawables should work with things instead. You can not
  74. * instantiate things, they are purely virtual */
  75. //=====================================
  76. class Thing : public MemoryPoolObject
  77. {
  78. // note, it is explicitly OK to pass null for 'thing' here;
  79. // they will check for null and return null in these cases.
  80. friend inline Object *AsObject(Thing *thing) { return thing ? thing->asObjectMeth() : NULL; }
  81. friend inline Drawable *AsDrawable(Thing *thing) { return thing ? thing->asDrawableMeth() : NULL; }
  82. friend inline const Object *AsObject(const Thing *thing) { return thing ? thing->asObjectMeth() : NULL; }
  83. friend inline const Drawable *AsDrawable(const Thing *thing) { return thing ? thing->asDrawableMeth() : NULL; }
  84. MEMORY_POOL_GLUE_ABC(Thing)
  85. public:
  86. Thing( const ThingTemplate *thingTemplate );
  87. /**
  88. return the thing template for this thing.
  89. */
  90. const ThingTemplate *getTemplate() const;
  91. // convenience method for patching isKindOf thru to template.
  92. Bool isKindOf(KindOfType t) const;
  93. Bool isKindOfMulti(const KindOfMaskType& mustBeSet, const KindOfMaskType& mustBeClear) const;
  94. Bool isAnyKindOf(const KindOfMaskType& anyKindOf) const;
  95. // physical properties
  96. void setPosition( const Coord3D *pos );
  97. /// the nice thing about this is that we don't have to recalc out cached terrain stuff.
  98. void setPositionZ( Real z );
  99. // note that calling this always orients the object straight up on the Z-axis,
  100. // or aligned with the terrain if we have the magic flag set.
  101. // don't want this behavior? then call setTransformMatrix instead.
  102. void setOrientation( Real angle );
  103. inline const Coord3D *getPosition() const { return &m_cachedPos; }
  104. inline Real getOrientation() const { return m_cachedAngle; }
  105. const Coord3D *getUnitDirectionVector2D() const;
  106. void getUnitDirectionVector2D(Coord3D& dir) const;
  107. void getUnitDirectionVector3D(Coord3D& dir) const;
  108. Real getHeightAboveTerrain() const;
  109. Real getHeightAboveTerrainOrWater() const;
  110. Bool isAboveTerrain() const { return getHeightAboveTerrain() > 0.0f; }
  111. Bool isAboveTerrainOrWater() const { return getHeightAboveTerrainOrWater() > 0.0f; }
  112. /** Ground vehicles moving down a slope will get slightly above the terrain.
  113. If we treat this as airborne, then they slide down slopes. This checks whether
  114. they are high enough that we should let them act like they're flying. jba. */
  115. Bool isSignificantlyAboveTerrain() const ;
  116. void convertBonePosToWorldPos(const Coord3D* bonePos, const Matrix3D* boneTransform, Coord3D* worldPos, Matrix3D* worldTransform) const;
  117. void setTransformMatrix( const Matrix3D *mx ); ///< set the world transformation matrix
  118. const Matrix3D* getTransformMatrix() const { return &m_transform; } ///< return the world transformation matrix
  119. void transformPoint( const Coord3D *in, Coord3D *out ); ///< transform this point using the m_transform matrix of this thing
  120. protected:
  121. // Virtual method since objects can be on bridges and need to calculate heigh above terrain differently.
  122. virtual Real calculateHeightAboveTerrain(void) const; // Calculates the actual height above terrain. Doesn't use cache.
  123. virtual Object *asObjectMeth() { return NULL; }
  124. virtual Drawable *asDrawableMeth() { return NULL; }
  125. virtual const Object *asObjectMeth() const { return NULL; }
  126. virtual const Drawable *asDrawableMeth() const { return NULL; }
  127. virtual void reactToTransformChange(const Matrix3D* oldMtx, const Coord3D* oldPos, Real oldAngle) = 0;
  128. private:
  129. // note that it is declared 'const' -- the assumption being that
  130. // since ThingTemplates are shared between many, many Things, the Thing
  131. // should never be able to change it.
  132. OVERRIDE<ThingTemplate> m_template; ///< reference back to template database
  133. #if defined(_DEBUG) || defined(_INTERNAL)
  134. AsciiString m_templateName;
  135. #endif
  136. /*
  137. yes, private; it's important that some of these only be modified
  138. by going thru the right methods, thus we make 'em private to enforce this.
  139. note that "m_transform" is the true description of location; the other fields
  140. (pos, angle, etc) are all simply cached values used for efficiency and convenience.
  141. you should NEVER modify them directly, because that won't change anything!
  142. */
  143. Matrix3D m_transform; ///< the 3D orientation and position of this Thing
  144. enum
  145. {
  146. VALID_DIRVECTOR = 0x01,
  147. VALID_ALTITUDE_TERRAIN = 0x02,
  148. VALID_ALTITUDE_SEALEVEL = 0x04
  149. };
  150. mutable Coord3D m_cachedPos; ///< position of thing
  151. mutable Real m_cachedAngle; ///< orientation of thing
  152. mutable Coord3D m_cachedDirVector; ///< unit direction vector
  153. mutable Real m_cachedAltitudeAboveTerrain;
  154. mutable Real m_cachedAltitudeAboveTerrainOrWater;
  155. mutable Int m_cacheFlags;
  156. };
  157. //-----------------------------------------------------------------------------
  158. // Externals
  159. //-----------------------------------------------------------------------------
  160. #endif // $label