Geometry.h 7.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: Geometry.h ///////////////////////////////////////////////////////////////////////////////
  24. // Author: Colin Day, November 2001
  25. // Desc: Geometry descriptions
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. #pragma once
  28. #ifndef __GEOMETRY_H_
  29. #define __GEOMETRY_H_
  30. #include "Lib/BaseType.h"
  31. #include "Common/AsciiString.h"
  32. #include "Common/Snapshot.h"
  33. class INI;
  34. //-------------------------------------------------------------------------------------------------
  35. /** Geometry type descriptions, keep this in the same order as GeometryNames[] below
  36. *
  37. * NOTE: Do *NOT* change the order of these defines unless you update the
  38. * partition manager ... in particular theCollideTestProcs depend on the
  39. * order of this geometry and the fact that the values start at 1
  40. */
  41. //-------------------------------------------------------------------------------------------------
  42. enum GeometryType
  43. {
  44. GEOMETRY_SPHERE = 0, ///< partition/collision testing as sphere. (majorRadius = radius)
  45. GEOMETRY_CYLINDER, ///< partition/collision testing as cylinder. (majorRadius = radius, height = height)
  46. GEOMETRY_BOX, ///< partition/collision testing as rectangular box (majorRadius = half len in forward dir; minorRadius = half len in side dir; height = height)
  47. GEOMETRY_NUM_TYPES, // keep this last
  48. GEOMETRY_FIRST = GEOMETRY_SPHERE
  49. };
  50. #ifdef DEFINE_GEOMETRY_NAMES
  51. static const char *GeometryNames[] =
  52. {
  53. "SPHERE",
  54. "CYLINDER",
  55. "BOX",
  56. NULL
  57. };
  58. #endif // end DEFINE_GEOMETRY_NAMES
  59. //-------------------------------------------------------------------------------------------------
  60. #if defined(_DEBUG) || defined(_INTERNAL)
  61. enum ExtentModType
  62. {
  63. EXTENTMOD_INVALID = 0,
  64. EXTENTMOD_TYPE = 1,
  65. EXTENTMOD_MAJOR = 2,
  66. EXTENTMOD_MINOR = 3,
  67. EXTENTMOD_HEIGHT = 4,
  68. };
  69. static const Real EXTENT_BIG_CHANGE = 10.0f;
  70. #endif
  71. //-------------------------------------------------------------------------------------------------
  72. /** Geometry information */
  73. //-------------------------------------------------------------------------------------------------
  74. class GeometryInfo : public Snapshot
  75. {
  76. private:
  77. GeometryType m_type;
  78. Bool m_isSmall; ///< if true, geometry is assumed to fit in a single partition cell
  79. Real m_height;
  80. Real m_majorRadius;
  81. Real m_minorRadius;
  82. Real m_boundingCircleRadius; ///< not in INI file -- size of bounding circle (2d)
  83. Real m_boundingSphereRadius; ///< not in INI -- size of bounding sphere (3d)
  84. void calcBoundingStuff();
  85. protected:
  86. // snapshot methods
  87. virtual void crc( Xfer *xfer );
  88. virtual void xfer( Xfer *xfer );
  89. virtual void loadPostProcess( void );
  90. public:
  91. static void parseGeometryType( INI* ini, void * /*instance*/, void *store, const void* /*userData*/ );
  92. static void parseGeometryIsSmall( INI* ini, void * /*instance*/, void *store, const void* /*userData*/ );
  93. static void parseGeometryHeight( INI* ini, void * /*instance*/, void *store, const void* /*userData*/ );
  94. static void parseGeometryMajorRadius( INI* ini, void * /*instance*/, void *store, const void* /*userData*/ );
  95. static void parseGeometryMinorRadius( INI* ini, void * /*instance*/, void *store, const void* /*userData*/ );
  96. GeometryInfo(GeometryType type, Bool isSmall, Real height, Real majorRadius, Real minorRadius)
  97. {
  98. // Added by Sadullah Nader
  99. // Initializations missing and needed
  100. m_boundingCircleRadius = 0.0f;
  101. m_boundingSphereRadius = 0.0f;
  102. //
  103. set(type, isSmall, height, majorRadius, minorRadius);
  104. }
  105. void set(GeometryType type, Bool isSmall, Real height, Real majorRadius, Real minorRadius);
  106. // bleah, icky but needed for legacy code
  107. inline void setMajorRadius(Real majorRadius)
  108. {
  109. m_majorRadius = majorRadius;
  110. calcBoundingStuff();
  111. }
  112. // bleah, icky but needed for legacy code
  113. inline void setMinorRadius(Real minorRadius)
  114. {
  115. m_minorRadius = minorRadius;
  116. calcBoundingStuff();
  117. }
  118. inline GeometryType getGeomType() const { return m_type; }
  119. inline Bool getIsSmall() const { return m_isSmall; }
  120. inline Real getMajorRadius() const { return m_majorRadius; } // x-axis
  121. inline Real getMinorRadius() const { return m_minorRadius; } // y-axis
  122. // this has been removed and should never need to be called...
  123. // you should generally call getMaxHeightAbovePosition() instead. (srj)
  124. //inline Real getGeomHeight() const { return m_height; } // z-axis
  125. inline Real getBoundingCircleRadius() const { return m_boundingCircleRadius; }
  126. inline Real getBoundingSphereRadius() const { return m_boundingSphereRadius; }
  127. Bool isIntersectedByLineSegment(const Coord3D& loc, const Coord3D& from, const Coord3D& to) const;
  128. Real getFootprintArea() const;
  129. // given an object with this geom, how far above the object's canonical position does its max z extend?
  130. Real getMaxHeightAbovePosition() const;
  131. // given an object with this geom, how far below the object's canonical position does its max z extend?
  132. Real getMaxHeightBelowPosition() const;
  133. // given an object with this geom, how far above/below the object's canonical position is its center?
  134. Real getZDeltaToCenterPosition() const;
  135. // given an object with this geom, located at 'pos', where is the "center" of the geometry?
  136. void getCenterPosition(const Coord3D& pos, Coord3D& center) const;
  137. // given an object with this geom, located at 'pos', and another obj with the given
  138. // pos & geom, calc the min and max pitches from this to that.
  139. void calcPitches(const Coord3D& thisPos, const GeometryInfo& that, const Coord3D& thatPos,
  140. Real& minPitch, Real& maxPitch) const;
  141. /// expand the 2d footprint
  142. void expandFootprint(Real radius);
  143. /// get the 2d bounding box
  144. void get2DBounds(const Coord3D& geomCenter, Real angle, Region2D& bounds ) const;
  145. /// note that the pt is generated using game logic random, not game client random!
  146. void makeRandomOffsetWithinFootprint(Coord3D& pt) const;
  147. void makeRandomOffsetOnPerimeter(Coord3D& pt) const; //Chooses a random point on the extent border.
  148. void clipPointToFootprint(const Coord3D& geomCenter, Coord3D& ptToClip) const;
  149. Bool isPointInFootprint(const Coord3D& geomCenter, const Coord3D& pt) const;
  150. // given an object with this geom, SET how far above the object's canonical position its max z should extend.
  151. void setMaxHeightAbovePosition(Real z);
  152. #if defined(_DEBUG) || defined(_INTERNAL)
  153. void tweakExtents(ExtentModType extentModType, Real extentModAmount);
  154. AsciiString getDescriptiveString() const;
  155. #endif
  156. };
  157. #endif