GameCommon.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. ** Command & Conquer Generals(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: GameCommon.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: GameCommon.h
  36. //
  37. // Created: Steven Johnson, October 2001
  38. //
  39. // Desc: This is a catchall header for some basic types and definitions
  40. // needed by various bits of the GameLogic/GameClient, but that
  41. // we haven't found a good place for yet. Hopefully this file
  42. // should go away someday, but for now is a convenient spot.
  43. //
  44. //-----------------------------------------------------------------------------
  45. #pragma once
  46. #ifndef _GAMECOMMON_H_
  47. #define _GAMECOMMON_H_
  48. // ----------------------------------------------------------------------------------------------
  49. #include "Lib/BaseType.h"
  50. // ----------------------------------------------------------------------------------------------
  51. #if defined(_INTERNAL) || defined(_DEBUG) || defined(_PLAYTEST)
  52. #define NO_DUMP_PERF_STATS
  53. #else
  54. #define NO_DUMP_PERF_STATS
  55. #endif
  56. // ----------------------------------------------------------------------------------------------
  57. enum
  58. {
  59. LOGICFRAMES_PER_SECOND = 30,
  60. MSEC_PER_SECOND = 1000
  61. };
  62. const Real LOGICFRAMES_PER_MSEC_REAL = (((Real)LOGICFRAMES_PER_SECOND) / ((Real)MSEC_PER_SECOND));
  63. const Real MSEC_PER_LOGICFRAME_REAL = (((Real)MSEC_PER_SECOND) / ((Real)LOGICFRAMES_PER_SECOND));
  64. const Real LOGICFRAMES_PER_SECONDS_REAL = (Real)LOGICFRAMES_PER_SECOND;
  65. const Real SECONDS_PER_LOGICFRAME_REAL = 1.0f / LOGICFRAMES_PER_SECONDS_REAL;
  66. // ----------------------------------------------------------------------------------------------
  67. // note that this returns a REAL value, not an int... most callers will want to
  68. // call ceil() on the result, so that partial frames get converted to full frames!
  69. inline Real ConvertDurationFromMsecsToFrames(Real msec)
  70. {
  71. return (msec * LOGICFRAMES_PER_MSEC_REAL);
  72. }
  73. // ----------------------------------------------------------------------------------------------
  74. inline Real ConvertVelocityInSecsToFrames(Real distPerMsec)
  75. {
  76. // this looks wrong, but is the correct conversion factor.
  77. return (distPerMsec * SECONDS_PER_LOGICFRAME_REAL);
  78. }
  79. // ----------------------------------------------------------------------------------------------
  80. inline Real ConvertAccelerationInSecsToFrames(Real distPerSec2)
  81. {
  82. // this looks wrong, but is the correct conversion factor.
  83. const Real SEC_PER_LOGICFRAME_SQR = (SECONDS_PER_LOGICFRAME_REAL * SECONDS_PER_LOGICFRAME_REAL);
  84. return (distPerSec2 * SEC_PER_LOGICFRAME_SQR);
  85. }
  86. // ----------------------------------------------------------------------------------------------
  87. inline Real ConvertAngularVelocityInDegreesPerSecToRadsPerFrame(Real degPerSec)
  88. {
  89. const Real RADS_PER_DEGREE = PI / 180.0f;
  90. return (degPerSec * (SECONDS_PER_LOGICFRAME_REAL * RADS_PER_DEGREE));
  91. }
  92. // ----------------------------------------------------------------------------------------------
  93. enum
  94. {
  95. MAX_PLAYER_COUNT = 16 ///< max number of Players.
  96. };
  97. // ----------------------------------------------------------------------------------------------
  98. /**
  99. a bitmask that can uniquely represent each player.
  100. */
  101. #if MAX_PLAYER_COUNT <= 16
  102. typedef UnsignedShort PlayerMaskType;
  103. const PlayerMaskType PLAYERMASK_ALL = 0xffff;
  104. const PlayerMaskType PLAYERMASK_NONE = 0x0;
  105. #else
  106. #error "this is the wrong size"
  107. #endif
  108. //-------------------------------------------------------------------------------------------------
  109. enum GameDifficulty
  110. {
  111. DIFFICULTY_EASY,
  112. DIFFICULTY_NORMAL,
  113. DIFFICULTY_HARD,
  114. DIFFICULTY_COUNT
  115. };
  116. //-------------------------------------------------------------------------------------------------
  117. enum PlayerType
  118. {
  119. PLAYER_HUMAN, ///< player is human-controlled
  120. PLAYER_COMPUTER, ///< player is computer-controlled
  121. PLAYERTYPE_COUNT
  122. };
  123. //-------------------------------------------------------------------------------------------------
  124. /// A PartitionCell can be one of three states for Shroud
  125. enum CellShroudStatus
  126. {
  127. CELLSHROUD_CLEAR,
  128. CELLSHROUD_FOGGED,
  129. CELLSHROUD_SHROUDED,
  130. CELLSHROUD_COUNT
  131. };
  132. //-------------------------------------------------------------------------------------------------
  133. /// Since an object can take up more than a single PartitionCell, this is a status that applies to the whole Object
  134. enum ObjectShroudStatus
  135. {
  136. OBJECTSHROUD_INVALID, ///< indeterminate state, will recompute
  137. OBJECTSHROUD_CLEAR, ///< object is not shrouded at all (ie, completely visible)
  138. OBJECTSHROUD_PARTIAL_CLEAR, ///< object is partly clear (rest is shroud or fog)
  139. OBJECTSHROUD_FOGGED, ///< object is completely fogged
  140. OBJECTSHROUD_SHROUDED, ///< object is completely shrouded
  141. OBJECTSHROUD_INVALID_BUT_PREVIOUS_VALID, ///< indeterminate state, will recompute, BUT previous status is valid, don't reset (used for save/load)
  142. OBJECTSHROUD_COUNT
  143. };
  144. //-------------------------------------------------------------------------------------------------
  145. enum GuardMode
  146. {
  147. GUARDMODE_NORMAL,
  148. GUARDMODE_GUARD_WITHOUT_PURSUIT, // no pursuit out of guard area
  149. GUARDMODE_GUARD_FLYING_UNITS_ONLY // ignore nonflyers
  150. };
  151. // ---------------------------------------------------
  152. enum
  153. {
  154. NEVER = 0,
  155. FOREVER = 0x3fffffff // (we use 0x3fffffff so that we can add offsets and not overflow...
  156. // at 30fps we're still pretty safe!)
  157. };
  158. //-------------------------------------------------------------------------------------------------
  159. //-------------------------------------------------------------------------------------------------
  160. //-------------------------------------------------------------------------------------------------
  161. /// Veterancy level define needed by several files that don't need the full Experience code.
  162. // NOTE NOTE NOTE: Keep TheVeterencyNames in sync with these.
  163. enum VeterancyLevel
  164. {
  165. LEVEL_REGULAR = 0,
  166. LEVEL_VETERAN,
  167. LEVEL_ELITE,
  168. LEVEL_HEROIC,
  169. LEVEL_COUNT,
  170. LEVEL_INVALID,
  171. LEVEL_FIRST = LEVEL_REGULAR,
  172. LEVEL_LAST = LEVEL_HEROIC
  173. };
  174. // TheVeterancyNames is defined in GameCommon.cpp
  175. extern const char *TheVeterancyNames[];
  176. //-------------------------------------------------------------------------------------------------
  177. //-------------------------------------------------------------------------------------------------
  178. enum CommandSourceType
  179. {
  180. CMD_FROM_PLAYER = 0,
  181. CMD_FROM_SCRIPT,
  182. CMD_FROM_AI,
  183. CMD_FROM_DOZER, // Special rare command when the dozer originates a command to attack a mine. Mines are not ai-attackable, and it seems deceitful for the dozer to generate a player or script command. jba.
  184. }; ///< the source of a command
  185. //-------------------------------------------------------------------------------------------------
  186. enum AbleToAttackType
  187. {
  188. _ATTACK_FORCED = 0x01,
  189. _ATTACK_CONTINUED = 0x02,
  190. _ATTACK_TUNNELNETWORK_GUARD = 0x04,
  191. /**
  192. can we attack if this is a new target?
  193. */
  194. ATTACK_NEW_TARGET = (0),
  195. /**
  196. can we attack if this is a new target, via force-fire?
  197. (The only current difference between this and ATTACK_NEW_TARGET is that disguised units
  198. are force-attackable even when stealthed.)
  199. */
  200. ATTACK_NEW_TARGET_FORCED= (_ATTACK_FORCED),
  201. /**
  202. can we attack if this is continuation of an existing attack?
  203. (The only current difference between this and ATTACK_NEW_TARGET is you are allowed to follow
  204. immobile shrouded units into the fog)
  205. */
  206. ATTACK_CONTINUED_TARGET = (_ATTACK_CONTINUED),
  207. /**
  208. can we attack if this is continuation of an existing attack?
  209. (The only current difference between this and ATTACK_NEW_TARGET is you are allowed to follow
  210. immobile shrouded units into the fog)
  211. */
  212. ATTACK_CONTINUED_TARGET_FORCED = (_ATTACK_FORCED | _ATTACK_CONTINUED),
  213. /**
  214. Special case that bypasses some of the checks for units guarding from within tunnel networks!
  215. For example, a unit inside couldn't normally see outside and would fail.
  216. */
  217. ATTACK_TUNNEL_NETWORK_GUARD = (_ATTACK_TUNNELNETWORK_GUARD)
  218. };
  219. //-------------------------------------------------------------------------------------------------
  220. inline Bool isForcedAttack(AbleToAttackType t)
  221. {
  222. return (((Int)t) & _ATTACK_FORCED) != 0;
  223. }
  224. //-------------------------------------------------------------------------------------------------
  225. inline Bool isContinuedAttack(AbleToAttackType t)
  226. {
  227. return (((Int)t) & _ATTACK_CONTINUED) != 0;
  228. }
  229. //-------------------------------------------------------------------------------------------------
  230. //-------------------------------------------------------------------------------------------------
  231. typedef UnsignedInt VeterancyLevelFlags;
  232. const VeterancyLevelFlags VETERANCY_LEVEL_FLAGS_ALL = 0xffffffff;
  233. const VeterancyLevelFlags VETERANCY_LEVEL_FLAGS_NONE = 0x00000000;
  234. inline Bool getVeterancyLevelFlag(VeterancyLevelFlags flags, VeterancyLevel dt)
  235. {
  236. return (flags & (1UL << (dt - 1))) != 0;
  237. }
  238. inline VeterancyLevelFlags setVeterancyLevelFlag(VeterancyLevelFlags flags, VeterancyLevel dt)
  239. {
  240. return (flags | (1UL << (dt - 1)));
  241. }
  242. inline VeterancyLevelFlags clearVeterancyLevelFlag(VeterancyLevelFlags flags, VeterancyLevel dt)
  243. {
  244. return (flags & ~(1UL << (dt - 1)));
  245. }
  246. // ----------------------------------------------------------------------------------------------
  247. #define BOGUSPTR(p) ((((unsigned int)(p)) & 1) != 0)
  248. // ----------------------------------------------------------------------------------------------
  249. #define MAKE_DLINK_HEAD(OBJCLASS, LISTNAME) \
  250. public: \
  251. inline DLINK_ITERATOR<OBJCLASS> iterate_##LISTNAME() const \
  252. { \
  253. DEBUG_ASSERTCRASH(!BOGUSPTR(m_dlinkhead_##LISTNAME.m_head), ("bogus head ptr")); \
  254. return DLINK_ITERATOR<OBJCLASS>(m_dlinkhead_##LISTNAME.m_head, OBJCLASS::dlink_next_##LISTNAME); \
  255. } \
  256. inline OBJCLASS *getFirstItemIn_##LISTNAME() const \
  257. { \
  258. DEBUG_ASSERTCRASH(!BOGUSPTR(m_dlinkhead_##LISTNAME.m_head), ("bogus head ptr")); \
  259. return m_dlinkhead_##LISTNAME.m_head; \
  260. } \
  261. inline Bool isInList_##LISTNAME(OBJCLASS* o) const \
  262. { \
  263. DEBUG_ASSERTCRASH(!BOGUSPTR(m_dlinkhead_##LISTNAME.m_head), ("bogus head ptr")); \
  264. return o->dlink_isInList_##LISTNAME(&m_dlinkhead_##LISTNAME.m_head); \
  265. } \
  266. inline void prependTo_##LISTNAME(OBJCLASS* o) \
  267. { \
  268. DEBUG_ASSERTCRASH(!BOGUSPTR(m_dlinkhead_##LISTNAME.m_head), ("bogus head ptr")); \
  269. if (!isInList_##LISTNAME(o)) \
  270. o->dlink_prependTo_##LISTNAME(&m_dlinkhead_##LISTNAME.m_head); \
  271. } \
  272. inline void removeFrom_##LISTNAME(OBJCLASS* o) \
  273. { \
  274. DEBUG_ASSERTCRASH(!BOGUSPTR(m_dlinkhead_##LISTNAME.m_head), ("bogus head ptr")); \
  275. if (isInList_##LISTNAME(o)) \
  276. o->dlink_removeFrom_##LISTNAME(&m_dlinkhead_##LISTNAME.m_head); \
  277. } \
  278. typedef void (*RemoveAllProc_##LISTNAME)(OBJCLASS* o); \
  279. inline void removeAll_##LISTNAME(RemoveAllProc_##LISTNAME p = NULL) \
  280. { \
  281. while (m_dlinkhead_##LISTNAME.m_head) \
  282. { \
  283. DEBUG_ASSERTCRASH(!BOGUSPTR(m_dlinkhead_##LISTNAME.m_head), ("bogus head ptr"));\
  284. OBJCLASS *tmp = m_dlinkhead_##LISTNAME.m_head; \
  285. removeFrom_##LISTNAME(tmp); \
  286. if (p) (*p)(tmp); \
  287. } \
  288. } \
  289. inline void reverse_##LISTNAME() \
  290. { \
  291. OBJCLASS* cur = m_dlinkhead_##LISTNAME.m_head; \
  292. OBJCLASS* prev = NULL; \
  293. while (cur) \
  294. { \
  295. OBJCLASS* originalNext = cur->dlink_next_##LISTNAME(); \
  296. cur->dlink_swapLinks_##LISTNAME(); \
  297. prev = cur; \
  298. cur = originalNext; \
  299. } \
  300. m_dlinkhead_##LISTNAME.m_head = prev; \
  301. } \
  302. private: \
  303. /* a trick: init head to zero */ \
  304. struct DLINKHEAD_##LISTNAME \
  305. { \
  306. public: \
  307. OBJCLASS* m_head; \
  308. inline DLINKHEAD_##LISTNAME() : \
  309. m_head(0) { } \
  310. inline ~DLINKHEAD_##LISTNAME() \
  311. { DEBUG_ASSERTCRASH(!m_head,("destroying dlinkhead still in a list " #LISTNAME)); } \
  312. }; \
  313. DLINKHEAD_##LISTNAME m_dlinkhead_##LISTNAME;
  314. // ----------------------------------------------------------------------------------------------
  315. #define MAKE_DLINK(OBJCLASS, LISTNAME) \
  316. public: \
  317. OBJCLASS* dlink_prev_##LISTNAME() const { return m_dlink_##LISTNAME.m_prev; } \
  318. OBJCLASS* dlink_next_##LISTNAME() const { return m_dlink_##LISTNAME.m_next; } \
  319. void dlink_swapLinks_##LISTNAME() \
  320. { \
  321. OBJCLASS* originalNext = m_dlink_##LISTNAME.m_next; \
  322. m_dlink_##LISTNAME.m_next = m_dlink_##LISTNAME.m_prev; \
  323. m_dlink_##LISTNAME.m_prev = originalNext; \
  324. } \
  325. Bool dlink_isInList_##LISTNAME(OBJCLASS* const* pListHead) const \
  326. { \
  327. DEBUG_ASSERTCRASH(!BOGUSPTR(*pListHead) && !BOGUSPTR(m_dlink_##LISTNAME.m_next) && !BOGUSPTR(m_dlink_##LISTNAME.m_prev), ("bogus ptrs")); \
  328. return *pListHead == this || m_dlink_##LISTNAME.m_prev || m_dlink_##LISTNAME.m_next; \
  329. } \
  330. void dlink_prependTo_##LISTNAME(OBJCLASS** pListHead) \
  331. { \
  332. DEBUG_ASSERTCRASH(!dlink_isInList_##LISTNAME(pListHead), ("already in list " #LISTNAME)); \
  333. DEBUG_ASSERTCRASH(!BOGUSPTR(*pListHead) && !BOGUSPTR(m_dlink_##LISTNAME.m_next) && !BOGUSPTR(m_dlink_##LISTNAME.m_prev), ("bogus ptrs")); \
  334. m_dlink_##LISTNAME.m_next = *pListHead; \
  335. if (*pListHead) \
  336. (*pListHead)->m_dlink_##LISTNAME.m_prev = this; \
  337. *pListHead = this; \
  338. DEBUG_ASSERTCRASH(!BOGUSPTR(*pListHead) && !BOGUSPTR(m_dlink_##LISTNAME.m_next) && !BOGUSPTR(m_dlink_##LISTNAME.m_prev), ("bogus ptrs")); \
  339. } \
  340. void dlink_removeFrom_##LISTNAME(OBJCLASS** pListHead) \
  341. { \
  342. DEBUG_ASSERTCRASH(dlink_isInList_##LISTNAME(pListHead), ("not in list" #LISTNAME)); \
  343. DEBUG_ASSERTCRASH(!BOGUSPTR(*pListHead) && !BOGUSPTR(m_dlink_##LISTNAME.m_next) && !BOGUSPTR(m_dlink_##LISTNAME.m_prev), ("bogus ptrs")); \
  344. if (m_dlink_##LISTNAME.m_next) \
  345. m_dlink_##LISTNAME.m_next->m_dlink_##LISTNAME.m_prev = m_dlink_##LISTNAME.m_prev; \
  346. if (m_dlink_##LISTNAME.m_prev) \
  347. m_dlink_##LISTNAME.m_prev->m_dlink_##LISTNAME.m_next = m_dlink_##LISTNAME.m_next; \
  348. else \
  349. *pListHead = m_dlink_##LISTNAME.m_next; \
  350. m_dlink_##LISTNAME.m_prev = 0; \
  351. m_dlink_##LISTNAME.m_next = 0; \
  352. DEBUG_ASSERTCRASH(!BOGUSPTR(*pListHead) && !BOGUSPTR(m_dlink_##LISTNAME.m_next) && !BOGUSPTR(m_dlink_##LISTNAME.m_prev), ("bogus ptrs")); \
  353. } \
  354. private: \
  355. /* a trick: init links to zero */ \
  356. struct DLINK_##LISTNAME \
  357. { \
  358. public: \
  359. OBJCLASS* m_prev; \
  360. OBJCLASS* m_next; \
  361. inline DLINK_##LISTNAME() : \
  362. m_prev(0), m_next(0) { } \
  363. inline ~DLINK_##LISTNAME() \
  364. { DEBUG_ASSERTCRASH(!m_prev && !m_next,("destroying dlink still in a list " #LISTNAME)); } \
  365. }; \
  366. DLINK_##LISTNAME m_dlink_##LISTNAME;
  367. // ------------------------------------------------------------------------
  368. // this is the weird C++ syntax for "call pointer-to-member-function"... see C++ FAQ LITE for details.
  369. #define callMemberFunction(object,ptrToMember) ((object).*(ptrToMember))
  370. // ------------------------------------------------------------------------
  371. template<class OBJCLASS>
  372. class DLINK_ITERATOR
  373. {
  374. public:
  375. // this is the weird C++ syntax for "pointer-to-member-function"
  376. typedef OBJCLASS* (OBJCLASS::*GetNextFunc)() const;
  377. private:
  378. OBJCLASS* m_cur;
  379. GetNextFunc m_getNextFunc; // this is the weird C++ syntax for "pointer-to-member-function"
  380. public:
  381. DLINK_ITERATOR(OBJCLASS* cur, GetNextFunc getNextFunc) : m_cur(cur), m_getNextFunc(getNextFunc)
  382. {
  383. }
  384. void advance()
  385. {
  386. if (m_cur)
  387. m_cur = callMemberFunction(*m_cur, m_getNextFunc)();
  388. }
  389. Bool done() const
  390. {
  391. return m_cur == NULL;
  392. }
  393. OBJCLASS* cur() const
  394. {
  395. return m_cur;
  396. }
  397. };
  398. // ------------------------------------------------------------------------
  399. enum WhichTurretType
  400. {
  401. TURRET_INVALID = -1,
  402. TURRET_MAIN = 0,
  403. TURRET_ALT,
  404. MAX_TURRETS
  405. };
  406. // ------------------------------------------------------------------------
  407. // this normalizes an angle to the range -PI...PI.
  408. extern Real normalizeAngle(Real angle);
  409. // ------------------------------------------------------------------------
  410. // this returns the difference between a1 and a2, normalized.
  411. inline Real stdAngleDiff(Real a1, Real a2)
  412. {
  413. return normalizeAngle(a1 - a2);
  414. }
  415. // ------------------------------------------------------------------------
  416. // NOTE NOTE NOTE: Keep TheRelationShipNames in sync with this enum
  417. enum Relationship
  418. {
  419. ENEMIES = 0,
  420. NEUTRAL,
  421. ALLIES
  422. };
  423. // TheRelationShipNames is defined in Common/GameCommon.cpp
  424. extern const char *TheRelationshipNames[];
  425. #endif // _GAMECOMMON_H_