GameInfo.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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: GameInfo.h //////////////////////////////////////////////////////
  24. // Generals game setup information
  25. // Author: Matthew D. Campbell, December 2001
  26. #pragma once
  27. #ifndef __GAMEINFO_H__
  28. #define __GAMEINFO_H__
  29. #include "Common/Snapshot.h"
  30. #include "Common/Money.h"
  31. #include "GameNetwork/NetworkDefs.h"
  32. #include "GameNetwork/FirewallHelper.h"
  33. enum SlotState
  34. {
  35. SLOT_OPEN,
  36. SLOT_CLOSED,
  37. SLOT_EASY_AI,
  38. SLOT_MED_AI,
  39. SLOT_BRUTAL_AI,
  40. SLOT_PLAYER
  41. };
  42. enum
  43. {
  44. PLAYERTEMPLATE_RANDOM = -1,
  45. PLAYERTEMPLATE_OBSERVER = -2,
  46. PLAYERTEMPLATE_MIN = PLAYERTEMPLATE_OBSERVER
  47. };
  48. /**
  49. * GameSlot class - maintains information about the contents of a
  50. * game slot. This persists throughout the game.
  51. */
  52. class GameSlot
  53. {
  54. public:
  55. GameSlot();
  56. virtual void reset();
  57. void setAccept( void ) { m_isAccepted = true; } ///< Accept the current options
  58. void unAccept( void ); ///< Unaccept (options changed, etc)
  59. Bool isAccepted( void ) const { return m_isAccepted; } ///< Non-human slots are always accepted
  60. void setMapAvailability( Bool hasMap ); ///< Set whether the slot has the map
  61. Bool hasMap( void ) const { return m_hasMap; } ///< Non-human slots always have the map
  62. void setState( SlotState state,
  63. UnicodeString name = UnicodeString::TheEmptyString,
  64. UnsignedInt IP = 0); ///< Set the slot's state (human, AI, open, etc)
  65. SlotState getState( void ) const { return m_state; } ///< Get the slot state
  66. void setColor( Int color ) { m_color = color; }
  67. Int getColor( void ) const { return m_color; }
  68. void setStartPos( Int startPos ) { m_startPos = startPos; }
  69. Int getStartPos( void ) const { return m_startPos; }
  70. void setPlayerTemplate( Int playerTemplate )
  71. { m_playerTemplate = playerTemplate;
  72. if (playerTemplate <= PLAYERTEMPLATE_MIN)
  73. m_startPos = -1;
  74. }
  75. Int getPlayerTemplate( void ) const { return m_playerTemplate; }
  76. void setTeamNumber( Int teamNumber ) { m_teamNumber = teamNumber; }
  77. Int getTeamNumber( void ) const { return m_teamNumber; }
  78. inline void setName( UnicodeString name ) { m_name = name; }
  79. inline UnicodeString getName( void ) const { return m_name; }
  80. inline void setIP( UnsignedInt IP ) { m_IP = IP; }
  81. inline UnsignedInt getIP( void ) const { return m_IP; }
  82. inline void setPort( UnsignedShort port ) { m_port = port; }
  83. inline UnsignedShort getPort( void ) const { return m_port; }
  84. inline void setNATBehavior( FirewallHelperClass::FirewallBehaviorType NATBehavior) { m_NATBehavior = NATBehavior; }
  85. inline FirewallHelperClass::FirewallBehaviorType getNATBehavior() const { return m_NATBehavior; }
  86. void saveOffOriginalInfo( void );
  87. inline Int getOriginalPlayerTemplate( void ) const { return m_origPlayerTemplate; }
  88. inline Int getOriginalColor( void ) const { return m_origColor; }
  89. inline Int getOriginalStartPos( void ) const { return m_origStartPos; }
  90. Int getApparentPlayerTemplate( void ) const;
  91. Int getApparentColor( void ) const;
  92. Int getApparentStartPos( void ) const;
  93. UnicodeString getApparentPlayerTemplateDisplayName( void ) const;
  94. // Various tests
  95. Bool isHuman( void ) const; ///< Is this slot occupied by a human player?
  96. Bool isOccupied( void ) const; ///< Is this slot occupied (by a human or an AI)?
  97. Bool isAI( void ) const; ///< Is this slot occupied by an AI?
  98. Bool isPlayer( AsciiString userName ) const; ///< Does this slot contain the given user?
  99. Bool isPlayer( UnicodeString userName ) const; ///< Does this slot contain the given user?
  100. Bool isPlayer( UnsignedInt ip ) const; ///< Is this slot at this IP?
  101. Bool isOpen( void ) const;
  102. void setLastFrameInGame( UnsignedInt frame ) { m_lastFrameInGame = frame; }
  103. void markAsDisconnected( void ) { m_disconnected = TRUE; }
  104. UnsignedInt lastFrameInGame( void ) const { return m_lastFrameInGame; }
  105. Bool disconnected( void ) const { return isHuman() && m_disconnected; }
  106. void mute( Bool isMuted ) { m_isMuted = isMuted; }
  107. Bool isMuted( void ) const { return m_isMuted; }
  108. protected:
  109. SlotState m_state;
  110. Bool m_isAccepted;
  111. Bool m_hasMap;
  112. Bool m_isMuted;
  113. Int m_color; ///< color, or -1 for random
  114. Int m_startPos; ///< start position, or -1 for random
  115. Int m_playerTemplate; ///< PlayerTemplate
  116. Int m_teamNumber; ///< alliance, -1 for none
  117. Int m_origColor; ///< color, or -1 for random
  118. Int m_origStartPos; ///< start position, or -1 for random
  119. Int m_origPlayerTemplate; ///< PlayerTemplate
  120. UnicodeString m_name; ///< Only valid for human players
  121. UnsignedInt m_IP; ///< Only valid for human players in LAN/WOL
  122. UnsignedShort m_port; ///< Only valid for human players in LAN/WOL
  123. FirewallHelperClass::FirewallBehaviorType m_NATBehavior; ///< The NAT behavior for this slot's player.
  124. UnsignedInt m_lastFrameInGame; // only valid for human players
  125. Bool m_disconnected; // only valid for human players
  126. };
  127. /**
  128. * GameInfo class - maintains information about the game setup and
  129. * the contents of its slot list hroughout the game.
  130. */
  131. class GameInfo
  132. {
  133. public:
  134. GameInfo();
  135. void init( void );
  136. virtual void reset( void );
  137. void clearSlotList( void );
  138. Int getNumPlayers( void ) const; ///< How many players (human and AI) are in the game?
  139. Int getNumNonObserverPlayers( void ) const; ///< How many non-observer players (human and AI) are in the game?
  140. Int getMaxPlayers( void ) const; ///< How many players (human and AI) can be in the game?
  141. void enterGame( void ); ///< Mark us as having entered the game
  142. void leaveGame( void ); ///< Mark us as having left the game
  143. virtual void startGame( Int gameID ); ///< Mark our game as started, and record the game ID
  144. void endGame( void ); ///< Mark us as out of game
  145. inline Int getGameID( void ) const; ///< Get the game ID of the current game or the last one if we're not in game
  146. inline void setInGame( void ); ///< set the m_inGame flag
  147. inline Bool isInGame( void ) const; ///< Are we (in game or in game setup)? As opposed to chatting, matching, etc
  148. inline Bool isGameInProgress( void ) const; ///< Is the game in progress?
  149. inline void setGameInProgress( Bool inProgress ); ///< Set whether the game is in progress or not.
  150. void setSlot( Int slotNum, GameSlot slotInfo ); ///< Set the slot state (human, open, AI, etc)
  151. GameSlot* getSlot( Int slotNum ); ///< Get the slot
  152. const GameSlot* getConstSlot( Int slotNum ) const; ///< Get the slot
  153. virtual Bool amIHost( void ) const; ///< Convenience function - is the local player the game host?
  154. virtual Int getLocalSlotNum( void ) const; ///< Get the local slot number, or -1 if we're not present
  155. Int getSlotNum( AsciiString userName ) const; ///< Get the slot number corresponding to a specific user, or -1 if he's not present
  156. // Game options
  157. void setMap( AsciiString mapName ); ///< Set the map to play on
  158. void setMapCRC( UnsignedInt mapCRC ); ///< Set the map CRC
  159. void setMapSize( UnsignedInt mapSize ); ///< Set the map size
  160. void setMapContentsMask( Int mask ); ///< Set the map contents mask (1=map,2=preview,4=map.ini)
  161. inline AsciiString getMap( void ) const; ///< Get the game map
  162. inline UnsignedInt getMapCRC( void ) const; ///< Get the map CRC
  163. inline UnsignedInt getMapSize( void ) const; ///< Get the map size
  164. inline Int getMapContentsMask( void ) const; ///< Get the map contents mask
  165. void setSeed( Int seed ); ///< Set the random seed for the game
  166. inline Int getSeed( void ) const; ///< Get the game seed
  167. inline Int getUseStats( void ) const; ///< Does this game count towards gamespy stats?
  168. inline void setUseStats( Int useStats );
  169. inline UnsignedShort getSuperweaponRestriction( void ) const; ///< Get any optional limits on superweapons
  170. void setSuperweaponRestriction( UnsignedShort restriction ); ///< Set the optional limits on superweapons
  171. inline const Money & getStartingCash(void) const;
  172. void setStartingCash( const Money & startingCash );
  173. void setSlotPointer( Int index, GameSlot *slot ); ///< Set the slot info pointer
  174. void setLocalIP( UnsignedInt ip ) { m_localIP =ip; } ///< Set the local IP
  175. UnsignedInt getLocalIP( void ) const { return m_localIP; } ///< Get the local IP
  176. Bool isColorTaken(Int colorIdx, Int slotToIgnore = -1 ) const;
  177. Bool isStartPositionTaken(Int positionIdx, Int slotToIgnore = -1 ) const;
  178. virtual void resetAccepted(void); ///< Reset the accepted flag on all players
  179. virtual void resetStartSpots(void); ///< reset the start spots for the new map.
  180. virtual void adjustSlotsForMap(void); ///< adjusts the slots to open and closed depending on the players in the game and the number of players the map can hold.
  181. virtual void closeOpenSlots(void); ///< close all slots that are currently unoccupied.
  182. // CRC checking hack
  183. void setCRCInterval( Int val ) { m_crcInterval = (val<100)?val:100; }
  184. inline Int getCRCInterval( void ) const { return m_crcInterval; }
  185. Bool haveWeSurrendered(void) { return m_surrendered; }
  186. void markAsSurrendered(void) { m_surrendered = TRUE; }
  187. Bool isSkirmish(void); // TRUE if 1 human & 1+ AI are present && !isSandbox()
  188. Bool isMultiPlayer(void); // TRUE if 2+ human are present
  189. Bool isSandbox(void); // TRUE if everybody is on the same team
  190. Bool isPlayerPreorder(Int index);
  191. void markPlayerAsPreorder(Int index);
  192. inline Bool oldFactionsOnly(void) const;
  193. inline void setOldFactionsOnly( Bool oldFactionsOnly );
  194. protected:
  195. Int m_preorderMask;
  196. Int m_crcInterval;
  197. Bool m_inGame;
  198. Bool m_inProgress;
  199. Bool m_surrendered;
  200. Int m_gameID;
  201. GameSlot *m_slot[MAX_SLOTS];
  202. UnsignedInt m_localIP;
  203. // Game options
  204. AsciiString m_mapName;
  205. UnsignedInt m_mapCRC;
  206. UnsignedInt m_mapSize;
  207. Int m_mapMask;
  208. Int m_seed;
  209. Int m_useStats;
  210. Money m_startingCash;
  211. UnsignedShort m_superweaponRestriction;
  212. Bool m_oldFactionsOnly; // Only USA, China, GLA -- not USA Air Force General, GLA Toxic General, et al
  213. };
  214. extern GameInfo *TheGameInfo;
  215. // Inline functions
  216. Int GameInfo::getGameID( void ) const { return m_gameID; }
  217. AsciiString GameInfo::getMap( void ) const { return m_mapName; }
  218. UnsignedInt GameInfo::getMapCRC( void ) const { return m_mapCRC; }
  219. UnsignedInt GameInfo::getMapSize( void ) const { return m_mapSize; }
  220. Int GameInfo::getMapContentsMask( void ) const { return m_mapMask; }
  221. Int GameInfo::getSeed( void ) const { return m_seed; }
  222. Bool GameInfo::isInGame( void ) const { return m_inGame; }
  223. void GameInfo::setInGame( void ) { m_inGame = true; }
  224. Bool GameInfo::isGameInProgress( void ) const { return m_inProgress; }
  225. void GameInfo::setGameInProgress( Bool inProgress ) { m_inProgress = inProgress; }
  226. Int GameInfo::getUseStats( void ) const { return m_useStats; }
  227. void GameInfo::setUseStats( Int useStats ) { m_useStats = useStats; }
  228. const Money&GameInfo::getStartingCash( void ) const { return m_startingCash; }
  229. UnsignedShort GameInfo::getSuperweaponRestriction( void ) const { return m_superweaponRestriction; }
  230. Bool GameInfo::oldFactionsOnly(void) const { return m_oldFactionsOnly; }
  231. void GameInfo::setOldFactionsOnly( Bool oldFactionsOnly ) { m_oldFactionsOnly = oldFactionsOnly; }
  232. AsciiString GameInfoToAsciiString( const GameInfo *game );
  233. Bool ParseAsciiStringToGameInfo( GameInfo *game, AsciiString options );
  234. /**
  235. * The SkirmishGameInfo class holds information about the skirmish game and
  236. * the contents of its slot list.
  237. */
  238. class SkirmishGameInfo : public GameInfo, public Snapshot
  239. {
  240. private:
  241. GameSlot m_skirmishSlot[MAX_SLOTS];
  242. protected:
  243. // snapshot methods
  244. virtual void crc( Xfer *xfer );
  245. virtual void xfer( Xfer *xfer );
  246. virtual void loadPostProcess( void );
  247. public:
  248. SkirmishGameInfo()
  249. {
  250. for (Int i = 0; i< MAX_SLOTS; ++i)
  251. setSlotPointer(i, &m_skirmishSlot[i]);
  252. }
  253. };
  254. extern SkirmishGameInfo *TheSkirmishGameInfo;
  255. extern SkirmishGameInfo *TheChallengeGameInfo;
  256. #endif // __GAMEINFO_H__