Team Fortress 2 Source Code as on 22/4/2020
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
17 KiB

  1. //========= Copyright 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. #include "c_baseentity.h"
  16. #define CGameRules C_GameRules
  17. #define CGameRulesProxy C_GameRulesProxy
  18. #else
  19. #include "baseentity.h"
  20. #include "recipientfilter.h"
  21. #endif
  22. #include "igamesystem.h"
  23. #include "gamerules_register.h"
  24. //#include "items.h"
  25. class CBaseCombatWeapon;
  26. class CBaseCombatCharacter;
  27. class CBasePlayer;
  28. class CItem;
  29. class CAmmoDef;
  30. class CTacticalMissionManager;
  31. extern ConVar sk_autoaim_mode;
  32. // Autoaiming modes
  33. enum
  34. {
  35. AUTOAIM_NONE = 0, // No autoaim at all.
  36. AUTOAIM_ON, // Autoaim is on.
  37. AUTOAIM_ON_CONSOLE, // Autoaim is on, including enhanced features for Console gaming (more assistance, etc)
  38. };
  39. // weapon respawning return codes
  40. enum
  41. {
  42. GR_NONE = 0,
  43. GR_WEAPON_RESPAWN_YES,
  44. GR_WEAPON_RESPAWN_NO,
  45. GR_AMMO_RESPAWN_YES,
  46. GR_AMMO_RESPAWN_NO,
  47. GR_ITEM_RESPAWN_YES,
  48. GR_ITEM_RESPAWN_NO,
  49. GR_PLR_DROP_GUN_ALL,
  50. GR_PLR_DROP_GUN_ACTIVE,
  51. GR_PLR_DROP_GUN_NO,
  52. GR_PLR_DROP_AMMO_ALL,
  53. GR_PLR_DROP_AMMO_ACTIVE,
  54. GR_PLR_DROP_AMMO_NO,
  55. };
  56. // Player relationship return codes
  57. enum
  58. {
  59. GR_NOTTEAMMATE = 0,
  60. GR_TEAMMATE,
  61. GR_ENEMY,
  62. GR_ALLY,
  63. GR_NEUTRAL,
  64. };
  65. // This class has the data tables and gets the CGameRules data to the client.
  66. class CGameRulesProxy : public CBaseEntity
  67. {
  68. public:
  69. DECLARE_CLASS( CGameRulesProxy, CBaseEntity );
  70. DECLARE_NETWORKCLASS();
  71. CGameRulesProxy();
  72. ~CGameRulesProxy();
  73. // UNDONE: Is this correct, Mike?
  74. // Don't carry these across a transition, they are recreated.
  75. virtual int ObjectCaps( void ) { return BaseClass::ObjectCaps() & ~FCAP_ACROSS_TRANSITION; }
  76. // ALWAYS transmit to all clients.
  77. virtual int UpdateTransmitState( void );
  78. // CGameRules chains its NetworkStateChanged calls to here, since this
  79. // is the actual entity that will send the data.
  80. static void NotifyNetworkStateChanged();
  81. private:
  82. static CGameRulesProxy *s_pGameRulesProxy;
  83. };
  84. abstract_class CGameRules : public CAutoGameSystemPerFrame
  85. {
  86. public:
  87. DECLARE_CLASS_GAMEROOT( CGameRules, CAutoGameSystemPerFrame );
  88. virtual char const *Name() { return "CGameRules"; }
  89. // Stuff shared between client and server.
  90. CGameRules(void);
  91. virtual ~CGameRules( void );
  92. // Damage Queries - these need to be implemented by the various subclasses (single-player, multi-player, etc).
  93. // The queries represent queries against damage types and properties.
  94. virtual bool Damage_IsTimeBased( int iDmgType ) = 0; // Damage types that are time-based.
  95. virtual bool Damage_ShouldGibCorpse( int iDmgType ) = 0; // Damage types that gib the corpse.
  96. virtual bool Damage_ShowOnHUD( int iDmgType ) = 0; // Damage types that have client HUD art.
  97. virtual bool Damage_NoPhysicsForce( int iDmgType ) = 0; // Damage types that don't have to supply a physics force & position.
  98. virtual bool Damage_ShouldNotBleed( int iDmgType ) = 0; // Damage types that don't make the player bleed.
  99. //Temp: These will go away once DamageTypes become enums.
  100. virtual int Damage_GetTimeBased( void ) = 0; // Actual bit-fields.
  101. virtual int Damage_GetShouldGibCorpse( void ) = 0;
  102. virtual int Damage_GetShowOnHud( void ) = 0;
  103. virtual int Damage_GetNoPhysicsForce( void )= 0;
  104. virtual int Damage_GetShouldNotBleed( void ) = 0;
  105. // Ammo Definitions
  106. //CAmmoDef* GetAmmoDef();
  107. virtual bool SwitchToNextBestWeapon( CBaseCombatCharacter *pPlayer, CBaseCombatWeapon *pCurrentWeapon ); // Switch to the next best weapon
  108. virtual CBaseCombatWeapon *GetNextBestWeapon( CBaseCombatCharacter *pPlayer, CBaseCombatWeapon *pCurrentWeapon ); // I can't use this weapon anymore, get me the next best one.
  109. virtual bool ShouldCollide( int collisionGroup0, int collisionGroup1 );
  110. virtual int DefaultFOV( void ) { return 90; }
  111. // This function is here for our CNetworkVars.
  112. inline void NetworkStateChanged()
  113. {
  114. // Forward the call to the entity that will send the data.
  115. CGameRulesProxy::NotifyNetworkStateChanged();
  116. }
  117. inline void NetworkStateChanged( void *pVar )
  118. {
  119. // Forward the call to the entity that will send the data.
  120. CGameRulesProxy::NotifyNetworkStateChanged();
  121. }
  122. // Get the view vectors for this mod.
  123. virtual const CViewVectors* GetViewVectors() const;
  124. // Damage rules for ammo types
  125. virtual float GetAmmoDamage( CBaseEntity *pAttacker, CBaseEntity *pVictim, int nAmmoType );
  126. virtual float GetDamageMultiplier( void ) { return 1.0f; }
  127. // Functions to verify the single/multiplayer status of a game
  128. virtual bool IsMultiplayer( void ) = 0;// is this a multiplayer game? (either coop or deathmatch)
  129. virtual const unsigned char *GetEncryptionKey() { return NULL; }
  130. virtual bool InRoundRestart( void ) { return false; }
  131. //Allow thirdperson camera.
  132. virtual bool AllowThirdPersonCamera( void ) { return false; }
  133. virtual void ClientCommandKeyValues( edict_t *pEntity, KeyValues *pKeyValues ) {}
  134. // IsConnectedUserInfoChangeAllowed allows the clients to change
  135. // cvars with the FCVAR_NOT_CONNECTED rule if it returns true
  136. virtual bool IsConnectedUserInfoChangeAllowed( CBasePlayer *pPlayer )
  137. {
  138. Assert( !IsMultiplayer() );
  139. return true;
  140. }
  141. #ifdef CLIENT_DLL
  142. virtual bool IsBonusChallengeTimeBased( void );
  143. virtual bool AllowMapParticleEffect( const char *pszParticleEffect ) { return true; }
  144. virtual bool AllowWeatherParticles( void ) { return true; }
  145. virtual bool AllowMapVisionFilterShaders( void ) { return false; }
  146. virtual const char* TranslateEffectForVisionFilter( const char *pchEffectType, const char *pchEffectName ) { return pchEffectName; }
  147. virtual bool IsLocalPlayer( int nEntIndex );
  148. virtual void ModifySentChat( char *pBuf, int iBufSize ) { return; }
  149. virtual bool ShouldConfirmOnDisconnect() { return false; }
  150. #else
  151. virtual void Status( void (*print) (const char *fmt, ...) ) {}
  152. virtual void GetTaggedConVarList( KeyValues *pCvarTagList ) {}
  153. // NVNT see if the client of the player entered is using a haptic device.
  154. virtual void CheckHaptics(CBasePlayer* pPlayer);
  155. // CBaseEntity overrides.
  156. public:
  157. // Setup
  158. // Called when game rules are destroyed by CWorld
  159. virtual void LevelShutdown( void ) { return; };
  160. virtual void Precache( void ) { return; };
  161. virtual void RefreshSkillData( bool forceUpdate );// fill skill data struct with proper values
  162. // Called each frame. This just forwards the call to Think().
  163. virtual void FrameUpdatePostEntityThink();
  164. virtual void Think( void ) = 0;// GR_Think - runs every server frame, should handle any timer tasks, periodic events, etc.
  165. virtual bool IsAllowedToSpawn( CBaseEntity *pEntity ) = 0; // Can this item spawn (eg NPCs don't spawn in deathmatch).
  166. // Called at the end of GameFrame (i.e. after all game logic has run this frame)
  167. virtual void EndGameFrame( void );
  168. virtual bool IsSkillLevel( int iLevel ) { return GetSkillLevel() == iLevel; }
  169. virtual int GetSkillLevel() { return g_iSkillLevel; }
  170. virtual void OnSkillLevelChanged( int iNewLevel ) {};
  171. virtual void SetSkillLevel( int iLevel )
  172. {
  173. int oldLevel = g_iSkillLevel;
  174. if ( iLevel < 1 )
  175. {
  176. iLevel = 1;
  177. }
  178. else if ( iLevel > 3 )
  179. {
  180. iLevel = 3;
  181. }
  182. g_iSkillLevel = iLevel;
  183. if( g_iSkillLevel != oldLevel )
  184. {
  185. OnSkillLevelChanged( g_iSkillLevel );
  186. }
  187. }
  188. virtual bool FAllowFlashlight( void ) = 0;// Are players allowed to switch on their flashlight?
  189. virtual bool FShouldSwitchWeapon( CBasePlayer *pPlayer, CBaseCombatWeapon *pWeapon ) = 0;// should the player switch to this weapon?
  190. // Functions to verify the single/multiplayer status of a game
  191. virtual bool IsDeathmatch( void ) = 0;//is this a deathmatch game?
  192. virtual bool IsTeamplay( void ) { return FALSE; };// is this deathmatch game being played with team rules?
  193. virtual bool IsCoOp( void ) = 0;// is this a coop game?
  194. virtual const char *GetGameDescription( void ) { return "Half-Life 2"; } // this is the game name that gets seen in the server browser
  195. // Client connection/disconnection
  196. 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)
  197. virtual void InitHUD( CBasePlayer *pl ) = 0; // the client dll is ready for updating
  198. virtual void ClientDisconnected( edict_t *pClient ) = 0;// a client just disconnected from the server
  199. // Client damage rules
  200. virtual float FlPlayerFallDamage( CBasePlayer *pPlayer ) = 0;// this client just hit the ground after a fall. How much damage?
  201. virtual bool FPlayerCanTakeDamage( CBasePlayer *pPlayer, CBaseEntity *pAttacker, const CTakeDamageInfo &info ) {return TRUE;};// can this player take damage from this attacker?
  202. virtual bool ShouldAutoAim( CBasePlayer *pPlayer, edict_t *target ) { return TRUE; }
  203. virtual float GetAutoAimScale( CBasePlayer *pPlayer ) { return 1.0f; }
  204. virtual int GetAutoAimMode() { return AUTOAIM_ON; }
  205. virtual bool ShouldUseRobustRadiusDamage(CBaseEntity *pEntity) { return false; }
  206. virtual void RadiusDamage( const CTakeDamageInfo &info, const Vector &vecSrc, float flRadius, int iClassIgnore, CBaseEntity *pEntityIgnore );
  207. // Let the game rules specify if fall death should fade screen to black
  208. virtual bool FlPlayerFallDeathDoesScreenFade( CBasePlayer *pl ) { return TRUE; }
  209. virtual bool AllowDamage( CBaseEntity *pVictim, const CTakeDamageInfo &info ) = 0;
  210. // Client spawn/respawn control
  211. virtual void PlayerSpawn( CBasePlayer *pPlayer ) = 0;// called by CBasePlayer::Spawn just before releasing player into the game
  212. virtual void PlayerThink( CBasePlayer *pPlayer ) = 0; // called by CBasePlayer::PreThink every frame, before physics are run and after keys are accepted
  213. virtual bool FPlayerCanRespawn( CBasePlayer *pPlayer ) = 0;// is this player allowed to respawn now?
  214. virtual float FlPlayerSpawnTime( CBasePlayer *pPlayer ) = 0;// When in the future will this player be able to spawn?
  215. virtual CBaseEntity *GetPlayerSpawnSpot( CBasePlayer *pPlayer );// Place this player on their spawnspot and face them the proper direction.
  216. virtual bool IsSpawnPointValid( CBaseEntity *pSpot, CBasePlayer *pPlayer );
  217. virtual bool AllowAutoTargetCrosshair( void ) { return TRUE; };
  218. virtual bool ClientCommand( CBaseEntity *pEdict, const CCommand &args ); // handles the user commands; returns TRUE if command handled properly
  219. virtual void ClientSettingsChanged( CBasePlayer *pPlayer ); // the player has changed cvars
  220. // Client kills/scoring
  221. virtual int IPointsForKill( CBasePlayer *pAttacker, CBasePlayer *pKilled ) = 0;// how many points do I award whoever kills this player?
  222. virtual void PlayerKilled( CBasePlayer *pVictim, const CTakeDamageInfo &info ) = 0;// Called each time a player dies
  223. virtual void DeathNotice( CBasePlayer *pVictim, const CTakeDamageInfo &info )= 0;// Call this from within a GameRules class to report an obituary.
  224. virtual const char *GetDamageCustomString( const CTakeDamageInfo &info ) { return NULL; }
  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. // Healthcharger respawn control
  253. virtual float FlHealthChargerRechargeTime( void ) = 0;// how long until a depleted HealthCharger recharges itself?
  254. virtual float FlHEVChargerRechargeTime( void ) { return 0; }// how long until a depleted HealthCharger recharges itself?
  255. // What happens to a dead player's weapons
  256. virtual int DeadPlayerWeapons( CBasePlayer *pPlayer ) = 0;// what do I do with a player's weapons when he's killed?
  257. // What happens to a dead player's ammo
  258. virtual int DeadPlayerAmmo( CBasePlayer *pPlayer ) = 0;// Do I drop ammo when the player dies? How much?
  259. // Teamplay stuff
  260. virtual const char *GetTeamID( CBaseEntity *pEntity ) = 0;// what team is this entity on?
  261. virtual int PlayerRelationship( CBaseEntity *pPlayer, CBaseEntity *pTarget ) = 0;// What is the player's relationship with this entity?
  262. virtual bool PlayerCanHearChat( CBasePlayer *pListener, CBasePlayer *pSpeaker ) = 0;
  263. virtual void CheckChatText( CBasePlayer *pPlayer, char *pText ) { return; }
  264. virtual int GetTeamIndex( const char *pTeamName ) { return -1; }
  265. virtual const char *GetIndexedTeamName( int teamIndex ) { return ""; }
  266. virtual bool IsValidTeam( const char *pTeamName ) { return true; }
  267. virtual void ChangePlayerTeam( CBasePlayer *pPlayer, const char *pTeamName, bool bKill, bool bGib ) {}
  268. virtual const char *SetDefaultPlayerTeam( CBasePlayer *pPlayer ) { return ""; }
  269. virtual void UpdateClientData( CBasePlayer *pPlayer ) { };
  270. // Sounds
  271. virtual bool PlayTextureSounds( void ) { return TRUE; }
  272. virtual bool PlayFootstepSounds( CBasePlayer *pl ) { return TRUE; }
  273. // NPCs
  274. virtual bool FAllowNPCs( void ) = 0;//are NPCs allowed
  275. // Immediately end a multiplayer game
  276. virtual void EndMultiplayerGame( void ) {}
  277. // trace line rules
  278. virtual float WeaponTraceEntity( CBaseEntity *pEntity, const Vector &vecStart, const Vector &vecEnd, unsigned int mask, trace_t *ptr );
  279. // Setup g_pPlayerResource (some mods use a different entity type here).
  280. virtual void CreateStandardEntities();
  281. // Team name, etc shown in chat and dedicated server console
  282. virtual const char *GetChatPrefix( bool bTeamOnly, CBasePlayer *pPlayer );
  283. // Location name shown in chat
  284. virtual const char *GetChatLocation( bool bTeamOnly, CBasePlayer *pPlayer ) { return NULL; }
  285. // VGUI format string for chat, if desired
  286. virtual const char *GetChatFormat( bool bTeamOnly, CBasePlayer *pPlayer ) { return NULL; }
  287. // Whether props that are on fire should get a DLIGHT.
  288. virtual bool ShouldBurningPropsEmitLight() { return false; }
  289. virtual bool CanEntityBeUsePushed( CBaseEntity *pEnt ) { return true; }
  290. virtual void CreateCustomNetworkStringTables( void ) { }
  291. // Game Achievements (server version)
  292. virtual void MarkAchievement ( IRecipientFilter& filter, char const *pchAchievementName );
  293. virtual void ResetMapCycleTimeStamp( void ){ return; }
  294. virtual void OnNavMeshLoad( void ) { return; }
  295. // game-specific factories
  296. virtual CTacticalMissionManager *TacticalMissionManagerFactory( void );
  297. virtual void ProcessVerboseLogOutput( void ){}
  298. #endif
  299. virtual const char *GetGameTypeName( void ){ return NULL; }
  300. virtual int GetGameType( void ){ return 0; }
  301. virtual bool ShouldDrawHeadLabels(){ return true; }
  302. virtual void ClientSpawned( edict_t * pPlayer ) { return; }
  303. virtual void OnFileReceived( const char * fileName, unsigned int transferID ) { return; }
  304. virtual bool IsHolidayActive( /*EHoliday*/ int eHoliday ) const { return false; }
  305. virtual bool IsManualMapChangeOkay( const char **pszReason ){ return true; }
  306. #ifndef CLIENT_DLL
  307. private:
  308. float m_flNextVerboseLogOutput;
  309. #endif // CLIENT_DLL
  310. };
  311. #ifndef CLIENT_DLL
  312. void InstallGameRules();
  313. // Create user messages for game here, calls into static player class creation functions
  314. void RegisterUserMessages( void );
  315. #endif
  316. extern ConVar g_Language;
  317. //-----------------------------------------------------------------------------
  318. // Gets us at the game rules
  319. //-----------------------------------------------------------------------------
  320. extern CGameRules *g_pGameRules;
  321. inline CGameRules* GameRules()
  322. {
  323. return g_pGameRules;
  324. }
  325. #endif // GAMERULES_H