GameInfo.h 12 KB

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