Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

452 lines
18 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef GAMERULES_H
  8. #define GAMERULES_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. // Debug history should be disabled in release builds
  13. //#define DISABLE_DEBUG_HISTORY
  14. #ifdef CLIENT_DLL
  15. // CEG interface functions:
  16. #define ALLOW_TEXTCHAT_FLAG 0xAD1A
  17. DWORD InitHudAllowTextChatFlag( void );
  18. #define ALLOW_PROPER_TINT_FLAG 0x8E3C
  19. DWORD InitUiAllowProperTintFlag( void );
  20. #endif
  21. #ifdef CLIENT_DLL
  22. #include "c_baseentity.h"
  23. #define CGameRules C_GameRules
  24. #define CGameRulesProxy C_GameRulesProxy
  25. #else
  26. #include "baseentity.h"
  27. #include "recipientfilter.h"
  28. #endif
  29. #include "igamesystem.h"
  30. #include "gamerules_register.h"
  31. //#include "items.h"
  32. class CBaseCombatWeapon;
  33. class CBaseCombatCharacter;
  34. class CBasePlayer;
  35. class CItem;
  36. class CAmmoDef;
  37. extern ConVar sk_autoaim_mode;
  38. // Autoaiming modes
  39. enum
  40. {
  41. AUTOAIM_NONE = 0, // No autoaim at all.
  42. AUTOAIM_ON, // Autoaim is on.
  43. AUTOAIM_ON_CONSOLE, // Autoaim is on, including enhanced features for Console gaming (more assistance, etc)
  44. };
  45. // weapon respawning return codes
  46. enum
  47. {
  48. GR_NONE = 0,
  49. GR_WEAPON_RESPAWN_YES,
  50. GR_WEAPON_RESPAWN_NO,
  51. GR_AMMO_RESPAWN_YES,
  52. GR_AMMO_RESPAWN_NO,
  53. GR_ITEM_RESPAWN_YES,
  54. GR_ITEM_RESPAWN_NO,
  55. GR_PLR_DROP_GUN_ALL,
  56. GR_PLR_DROP_GUN_ACTIVE,
  57. GR_PLR_DROP_GUN_NO,
  58. GR_PLR_DROP_AMMO_ALL,
  59. GR_PLR_DROP_AMMO_ACTIVE,
  60. GR_PLR_DROP_AMMO_NO,
  61. };
  62. // Player relationship return codes
  63. enum
  64. {
  65. GR_NOTTEAMMATE = 0,
  66. GR_TEAMMATE,
  67. GR_ENEMY,
  68. GR_ALLY,
  69. GR_NEUTRAL,
  70. };
  71. // This class has the data tables and gets the CGameRules data to the client.
  72. class CGameRulesProxy : public CBaseEntity
  73. {
  74. public:
  75. DECLARE_CLASS( CGameRulesProxy, CBaseEntity );
  76. DECLARE_NETWORKCLASS();
  77. CGameRulesProxy();
  78. ~CGameRulesProxy();
  79. // UNDONE: Is this correct, Mike?
  80. // Don't carry these across a transition, they are recreated.
  81. virtual int ObjectCaps( void ) { return BaseClass::ObjectCaps() & ~FCAP_ACROSS_TRANSITION; }
  82. // ALWAYS transmit to all clients.
  83. virtual int UpdateTransmitState( void );
  84. // CGameRules chains its NetworkStateChanged calls to here, since this
  85. // is the actual entity that will send the data.
  86. static void NotifyNetworkStateChanged();
  87. private:
  88. static CGameRulesProxy *s_pGameRulesProxy;
  89. };
  90. abstract_class CGameRules : public CMemZeroOnNew, public CAutoGameSystemPerFrame
  91. {
  92. public:
  93. DECLARE_CLASS_GAMEROOT( CGameRules, CAutoGameSystemPerFrame );
  94. virtual char const *Name() { return "CGameRules"; }
  95. // Stuff shared between client and server.
  96. CGameRules(void);
  97. virtual ~CGameRules( void );
  98. virtual bool Init();
  99. // Damage Queries - these need to be implemented by the various subclasses (single-player, multi-player, etc).
  100. // The queries represent queries against damage types and properties.
  101. virtual bool Damage_IsTimeBased( int iDmgType ) = 0; // Damage types that are time-based.
  102. virtual bool Damage_ShouldGibCorpse( int iDmgType ) = 0; // Damage types that gib the corpse.
  103. virtual bool Damage_ShowOnHUD( int iDmgType ) = 0; // Damage types that have client HUD art.
  104. virtual bool Damage_NoPhysicsForce( int iDmgType ) = 0; // Damage types that don't have to supply a physics force & position.
  105. virtual bool Damage_ShouldNotBleed( int iDmgType ) = 0; // Damage types that don't make the player bleed.
  106. //Temp: These will go away once DamageTypes become enums.
  107. virtual int Damage_GetTimeBased( void ) = 0; // Actual bit-fields.
  108. virtual int Damage_GetShouldGibCorpse( void ) = 0;
  109. virtual int Damage_GetShowOnHud( void ) = 0;
  110. virtual int Damage_GetNoPhysicsForce( void )= 0;
  111. virtual int Damage_GetShouldNotBleed( void ) = 0;
  112. // Ammo Definitions
  113. //CAmmoDef* GetAmmoDef();
  114. virtual bool SwitchToNextBestWeapon( CBaseCombatCharacter *pPlayer, CBaseCombatWeapon *pCurrentWeapon ); // Switch to the next best weapon
  115. virtual CBaseCombatWeapon *GetNextBestWeapon( CBaseCombatCharacter *pPlayer, CBaseCombatWeapon *pCurrentWeapon ); // I can't use this weapon anymore, get me the next best one.
  116. virtual bool ShouldCollide( int collisionGroup0, int collisionGroup1 );
  117. virtual int DefaultFOV( void ) { return 90; }
  118. // This function is here for our CNetworkVars.
  119. inline void NetworkStateChanged()
  120. {
  121. // Forward the call to the entity that will send the data.
  122. CGameRulesProxy::NotifyNetworkStateChanged();
  123. }
  124. inline void NetworkStateChanged( void *pVar )
  125. {
  126. // Forward the call to the entity that will send the data.
  127. CGameRulesProxy::NotifyNetworkStateChanged();
  128. }
  129. // Get the view vectors for this mod.
  130. virtual const CViewVectors* GetViewVectors() const;
  131. // Damage rules for ammo types
  132. virtual float GetAmmoDamage( CBaseEntity *pAttacker, CBaseEntity *pVictim, int nAmmoType );
  133. virtual float GetDamageMultiplier( void ) { return 1.0f; }
  134. // Functions to verify the single/multiplayer status of a game
  135. virtual bool IsMultiplayer( void ) = 0;// is this a multiplayer game? (either coop or deathmatch)
  136. virtual const unsigned char *GetEncryptionKey() { return NULL; }
  137. virtual bool InRoundRestart( void ) { return false; }
  138. virtual bool CheckAchievementsEnabled( int iAchievementID ) { return true; }
  139. virtual void RegisterScriptFunctions( void ){ };
  140. virtual void ClientCommandKeyValues( edict_t *pEntity, KeyValues *pKeyValues ) {}
  141. // IsConnectedUserInfoChangeAllowed allows to override FCVAR_NOT_CONNECTED rule when
  142. // player is on team spectator or team unassigned for example
  143. // Default and previous engine implementation will never allow FCVAR_NOT_CONNECTED cvars
  144. // to be changed while connected to a game server
  145. virtual bool IsConnectedUserInfoChangeAllowed( CBasePlayer *pPlayer ) { return false; }
  146. #ifdef CLIENT_DLL
  147. virtual bool IsBonusChallengeTimeBased( void );
  148. virtual bool AllowThirdPersonCamera() { return true; }
  149. #else
  150. virtual void GetTaggedConVarList( KeyValues *pCvarTagList ) {}
  151. // CBaseEntity overrides.
  152. public:
  153. // Setup
  154. virtual void OnBeginChangeLevel( const char *nextMapName, KeyValues *saveData ) {} ///< called just before a trigger_changelevel starts a changelevel
  155. // Called when game rules are destroyed by CWorld
  156. virtual void LevelShutdown( void ) { return; };
  157. virtual void Precache( void );
  158. virtual void RefreshSkillData( bool forceUpdate );// fill skill data struct with proper values
  159. // Called each frame. This just forwards the call to Think().
  160. virtual void FrameUpdatePostEntityThink();
  161. virtual void Think( void ); // GR_Think - runs every server frame, should handle any timer tasks, periodic events, etc.
  162. virtual bool IsAllowedToSpawn( CBaseEntity *pEntity ) = 0; // Can this item spawn (eg NPCs don't spawn in deathmatch).
  163. // Called at the end of GameFrame (i.e. after all game logic has run this frame)
  164. virtual void EndGameFrame( void );
  165. virtual bool IsSkillLevel( int iLevel ) { return GetSkillLevel() == iLevel; }
  166. virtual int GetSkillLevel() { return g_iSkillLevel; }
  167. virtual void OnSkillLevelChanged( int iNewLevel ) {};
  168. virtual void SetSkillLevel( int iLevel )
  169. {
  170. int oldLevel = g_iSkillLevel;
  171. if ( iLevel < 1 )
  172. {
  173. iLevel = 1;
  174. }
  175. else if ( iLevel > 3 )
  176. {
  177. iLevel = 3;
  178. }
  179. g_iSkillLevel = iLevel;
  180. if( g_iSkillLevel != oldLevel )
  181. {
  182. OnSkillLevelChanged( g_iSkillLevel );
  183. }
  184. }
  185. virtual bool FAllowFlashlight( void ) = 0;// Are players allowed to switch on their flashlight?
  186. virtual bool FShouldSwitchWeapon( CBasePlayer *pPlayer, CBaseCombatWeapon *pWeapon ) = 0;// should the player switch to this weapon?
  187. // Functions to verify the single/multiplayer status of a game
  188. virtual bool IsDeathmatch( void ) = 0;//is this a deathmatch game?
  189. virtual bool IsTeamplay( void ) { return FALSE; };// is this deathmatch game being played with team rules?
  190. virtual bool IsCoOp( void ) = 0;// is this a coop game?
  191. virtual const char *GetGameDescription( void ) { return "Half-Life 2"; } // this is the game name that gets seen in the server browser
  192. // Client connection/disconnection
  193. virtual bool ClientConnected( edict_t *pEntity, const char *pszName, const char *pszAddress, char *reject, int maxrejectlen ) = 0;// a client just connected to the server (player hasn't spawned yet)
  194. virtual void InitHUD( CBasePlayer *pl ) = 0; // the client dll is ready for updating
  195. virtual void ClientDisconnected( edict_t *pClient ) = 0;// a client just disconnected from the server
  196. virtual bool ShouldTimeoutClient( int nUserID, float flTimeSinceLastReceived ) { return false; } // return true to disconnect client due to timeout (used to do stricter timeouts when the game is sure the client isn't loading a map)
  197. // Client damage rules
  198. virtual float FlPlayerFallDamage( CBasePlayer *pPlayer ) = 0;// this client just hit the ground after a fall. How much damage?
  199. virtual bool FPlayerCanTakeDamage( CBasePlayer *pPlayer, CBaseEntity *pAttacker ) {return TRUE;};// can this player take damage from this attacker?
  200. virtual bool ShouldAutoAim( CBasePlayer *pPlayer, edict_t *target ) { return TRUE; }
  201. virtual float GetAutoAimScale( CBasePlayer *pPlayer ) { return 1.0f; }
  202. virtual int GetAutoAimMode() { return AUTOAIM_ON; }
  203. virtual bool ShouldUseRobustRadiusDamage(CBaseEntity *pEntity) { return false; }
  204. virtual void RadiusDamage( const CTakeDamageInfo &info, const Vector &vecSrc, float flRadius, int iClassIgnore, CBaseEntity *pEntityIgnore );
  205. // Let the game rules specify if fall death should fade screen to black
  206. virtual bool FlPlayerFallDeathDoesScreenFade( CBasePlayer *pl ) { return TRUE; }
  207. virtual bool AllowDamage( CBaseEntity *pVictim, const CTakeDamageInfo &info ) = 0;
  208. // Client spawn/respawn control
  209. virtual void PlayerSpawn( CBasePlayer *pPlayer ) = 0;// called by CBasePlayer::Spawn just before releasing player into the game
  210. virtual void PlayerThink( CBasePlayer *pPlayer ) = 0; // called by CBasePlayer::PreThink every frame, before physics are run and after keys are accepted
  211. virtual bool FPlayerCanRespawn( CBasePlayer *pPlayer ) = 0;// is this player allowed to respawn now?
  212. virtual float FlPlayerSpawnTime( CBasePlayer *pPlayer ) = 0;// When in the future will this player be able to spawn?
  213. virtual CBaseEntity *GetPlayerSpawnSpot( CBasePlayer *pPlayer );// Place this player on their spawnspot and face them the proper direction.
  214. virtual bool IsSpawnPointValid( CBaseEntity *pSpot, CBasePlayer *pPlayer );
  215. virtual bool AllowAutoTargetCrosshair( void ) { return TRUE; };
  216. virtual bool ClientCommand( CBaseEntity *pEdict, const CCommand &args ); // handles the user commands; returns TRUE if command handled properly
  217. virtual void ClientSettingsChanged( CBasePlayer *pPlayer ); // the player has changed cvars
  218. virtual bool CanClientCustomizeOwnIdentity() { return true; }
  219. // Client kills/scoring
  220. virtual int IPointsForKill( CBasePlayer *pAttacker, CBasePlayer *pKilled ) = 0;// how many points do I award whoever kills this player?
  221. virtual void PlayerKilled( CBasePlayer *pVictim, const CTakeDamageInfo &info ) = 0;// Called each time a player dies
  222. virtual void DeathNotice( CBasePlayer *pVictim, const CTakeDamageInfo &info )= 0;// Call this from within a GameRules class to report an obituary.
  223. virtual const char *GetDamageCustomString( const CTakeDamageInfo &info ) { return NULL; }
  224. virtual bool IgnorePlayerKillCommand( void ) const { return false; }
  225. // Weapon Damage
  226. // Determines how much damage Player's attacks inflict, based on skill level.
  227. virtual float AdjustPlayerDamageInflicted( float damage ) { return damage; }
  228. virtual void AdjustPlayerDamageTaken( CTakeDamageInfo *pInfo ) {}; // Base class does nothing.
  229. // Weapon retrieval
  230. virtual bool CanHavePlayerItem( CBasePlayer *pPlayer, CBaseCombatWeapon *pWeapon );// The player is touching an CBaseCombatWeapon, do I give it to him?
  231. // Weapon spawn/respawn control
  232. virtual int WeaponShouldRespawn( CBaseCombatWeapon *pWeapon ) = 0;// should this weapon respawn?
  233. virtual float FlWeaponRespawnTime( CBaseCombatWeapon *pWeapon ) = 0;// when may this weapon respawn?
  234. virtual float FlWeaponTryRespawn( CBaseCombatWeapon *pWeapon ) = 0; // can i respawn now, and if not, when should i try again?
  235. virtual Vector VecWeaponRespawnSpot( CBaseCombatWeapon *pWeapon ) = 0;// where in the world should this weapon respawn?
  236. // Item retrieval
  237. virtual bool CanHaveItem( CBasePlayer *pPlayer, CItem *pItem ) = 0;// is this player allowed to take this item?
  238. virtual void PlayerGotItem( CBasePlayer *pPlayer, CItem *pItem ) = 0;// call each time a player picks up an item (battery, healthkit)
  239. // Item spawn/respawn control
  240. virtual int ItemShouldRespawn( CItem *pItem ) = 0;// Should this item respawn?
  241. virtual float FlItemRespawnTime( CItem *pItem ) = 0;// when may this item respawn?
  242. virtual Vector VecItemRespawnSpot( CItem *pItem ) = 0;// where in the world should this item respawn?
  243. virtual QAngle VecItemRespawnAngles( CItem *pItem ) = 0;// what angles should this item use when respawing?
  244. // Ammo retrieval
  245. virtual bool CanHaveAmmo( CBaseCombatCharacter *pPlayer, int iAmmoIndex ); // can this player take more of this ammo?
  246. virtual bool CanHaveAmmo( CBaseCombatCharacter *pPlayer, const char *szName );
  247. virtual void PlayerGotAmmo( CBaseCombatCharacter *pPlayer, char *szName, int iCount ) = 0;// called each time a player picks up some ammo in the world
  248. virtual float GetAmmoQuantityScale( int iAmmoIndex ) { return 1.0f; }
  249. // AI Definitions
  250. virtual void InitDefaultAIRelationships( void ) { return; }
  251. virtual const char* AIClassText(int classType) { return NULL; }
  252. virtual int NumEntityClasses() const { return LAST_SHARED_ENTITY_CLASS; }
  253. virtual int NumFactions() const { return LAST_SHARED_FACTION; }
  254. // Healthcharger respawn control
  255. virtual float FlHealthChargerRechargeTime( void ) = 0;// how long until a depleted HealthCharger recharges itself?
  256. virtual float FlHEVChargerRechargeTime( void ) { return 0; }// how long until a depleted HealthCharger recharges itself?
  257. // What happens to a dead player's weapons
  258. virtual int DeadPlayerWeapons( CBasePlayer *pPlayer ) = 0;// what do I do with a player's weapons when he's killed?
  259. // What happens to a dead player's ammo
  260. virtual int DeadPlayerAmmo( CBasePlayer *pPlayer ) = 0;// Do I drop ammo when the player dies? How much?
  261. // Teamplay stuff
  262. virtual int PlayerRelationship( CBaseEntity *pPlayer, CBaseEntity *pTarget ) = 0;// What is the player's relationship with this entity?
  263. virtual bool PlayerCanHearChat( CBasePlayer *pListener, CBasePlayer *pSpeaker, bool bTeamOnly ) = 0;
  264. virtual void CheckChatText( CBasePlayer *pPlayer, char *pText ) { return; }
  265. virtual int GetTeamIndex( const char *pTeamName ) { return -1; }
  266. virtual const char *GetIndexedTeamName( int teamIndex ) { return ""; }
  267. virtual bool IsValidTeam( const char *pTeamName ) { return true; }
  268. virtual void ChangePlayerTeam( CBasePlayer *pPlayer, const char *pTeamName, bool bKill, bool bGib ) {}
  269. virtual const char *SetDefaultPlayerTeam( CBasePlayer *pPlayer ) { return ""; }
  270. virtual void UpdateClientData( CBasePlayer *pPlayer ) { };
  271. virtual bool IsTeamChangeSilent( CBasePlayer *pPlayer, int iTeamNum, bool bAutoTeam, bool bSilent ) { return bSilent; }
  272. // Sounds
  273. virtual bool PlayTextureSounds( void ) { return TRUE; }
  274. virtual bool PlayFootstepSounds( CBasePlayer *pl ) { return TRUE; }
  275. virtual bool AllowSoundscapes( void ) { return TRUE; }
  276. // NPCs
  277. virtual bool FAllowNPCs( void ) = 0;//are NPCs allowed
  278. // Immediately end a multiplayer game
  279. virtual void EndMultiplayerGame( void ) {}
  280. // trace line rules
  281. virtual float WeaponTraceEntity( CBaseEntity *pEntity, const Vector &vecStart, const Vector &vecEnd, unsigned int mask, trace_t *ptr );
  282. // Setup g_pPlayerResource (some mods use a different entity type here).
  283. virtual void CreateStandardEntities();
  284. // Team name, etc shown in chat and dedicated server console
  285. virtual const char *GetChatPrefix( bool bTeamOnly, CBasePlayer *pPlayer );
  286. // Location name shown in chat
  287. virtual const char *GetChatLocation( bool bTeamOnly, CBasePlayer *pPlayer ) { return NULL; }
  288. // VGUI format string for chat, if desired
  289. virtual const char *GetChatFormat( bool bTeamOnly, CBasePlayer *pPlayer ) { return NULL; }
  290. // Whether props that are on fire should get a DLIGHT.
  291. virtual bool ShouldBurningPropsEmitLight() { return false; }
  292. virtual bool CanEntityBeUsePushed( CBaseEntity *pEnt ) { return true; }
  293. virtual void CreateCustomNetworkStringTables( void ) { }
  294. // Game Achievements (server version)
  295. virtual void MarkAchievement ( IRecipientFilter& filter, char const *pchAchievementName );
  296. virtual void ResetMapCycleTimeStamp( void ){ return; }
  297. virtual void OnNavMeshLoad( void ) { return; }
  298. virtual void UpdateGameplayStatsFromSteam( void ) { return; }
  299. virtual edict_t *DoFindClientInPVS( edict_t *pEdict, unsigned char *pvs, unsigned pvssize );
  300. #endif
  301. virtual const char *GetGameTypeName( void ){ return NULL; }
  302. virtual int GetGameType( void ){ return 0; }
  303. virtual bool ForceSplitScreenPlayersOnToSameTeam() { return true; }
  304. virtual bool IsTopDown() { return false; }
  305. virtual const QAngle& GetTopDownMovementAxis() { return vec3_angle; }
  306. // Assume the game doesn't care
  307. virtual int GetMaxHumanPlayers() const { return -1; }
  308. };
  309. #ifndef CLIENT_DLL
  310. void InstallGameRules();
  311. // Create user messages for game here, calls into static player class creation functions
  312. void RegisterUserMessages( void );
  313. #endif
  314. extern ConVar g_Language;
  315. //-----------------------------------------------------------------------------
  316. // Gets us at the game rules
  317. //-----------------------------------------------------------------------------
  318. extern CGameRules *g_pGameRules;
  319. inline CGameRules* GameRules()
  320. {
  321. return g_pGameRules;
  322. }
  323. #ifdef PORTAL2
  324. bool IsGameRulesMultiplayer();
  325. #endif
  326. #endif // GAMERULES_H