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.

518 lines
18 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "singleplay_gamerules.h"
  8. #ifdef CLIENT_DLL
  9. #else
  10. #include "globalstate.h"
  11. #include "player.h"
  12. #include "basecombatweapon.h"
  13. #include "gamerules.h"
  14. #include "game.h"
  15. #include "items.h"
  16. #endif
  17. // memdbgon must be the last include file in a .cpp file!!!
  18. #include "tier0/memdbgon.h"
  19. #ifndef CLIENT_DLL
  20. // This allows us to test regenerative health systems on the fly
  21. void RegenerationForceOnChangeCallback( IConVar *pConVar, const char *pOldValue, float flOldValue )
  22. {
  23. ConVarRef var( pConVar );
  24. if ( !GlobalEntity_IsInTable( "player_regenerates_health" ) )
  25. GlobalEntity_Add( MAKE_STRING("player_regenerates_health"), gpGlobals->mapname, ( var.GetBool() ) ? ( GLOBAL_ON ) : ( GLOBAL_OFF ) );
  26. else
  27. GlobalEntity_SetState( MAKE_STRING("player_regenerates_health"), ( var.GetBool() ) ? ( GLOBAL_ON ) : ( GLOBAL_OFF ) );
  28. }
  29. ConVar sv_regeneration_force_on( "sv_regeneration_force_on", "0", FCVAR_CHEAT, "Cheat to test regenerative health systems", RegenerationForceOnChangeCallback );
  30. #endif
  31. //=========================================================
  32. //=========================================================
  33. bool CSingleplayRules::IsMultiplayer( void )
  34. {
  35. return false;
  36. }
  37. // Needed during the conversion, but once DMG_* types have been fixed, this isn't used anymore.
  38. //-----------------------------------------------------------------------------
  39. // Purpose:
  40. //-----------------------------------------------------------------------------
  41. int CSingleplayRules::Damage_GetTimeBased( void )
  42. {
  43. int iDamage = ( DMG_PARALYZE | DMG_NERVEGAS | DMG_POISON | DMG_RADIATION | DMG_DROWNRECOVER | DMG_ACID | DMG_SLOWBURN );
  44. return iDamage;
  45. }
  46. //-----------------------------------------------------------------------------
  47. // Purpose:
  48. //-----------------------------------------------------------------------------
  49. int CSingleplayRules::Damage_GetShouldGibCorpse( void )
  50. {
  51. int iDamage = ( DMG_CRUSH | DMG_FALL | DMG_BLAST | DMG_SONIC | DMG_CLUB );
  52. return iDamage;
  53. }
  54. //-----------------------------------------------------------------------------
  55. // Purpose:
  56. //-----------------------------------------------------------------------------
  57. int CSingleplayRules::Damage_GetShowOnHud( void )
  58. {
  59. int iDamage = ( DMG_POISON | DMG_ACID | DMG_DROWN | DMG_BURN | DMG_SLOWBURN | DMG_NERVEGAS | DMG_RADIATION | DMG_SHOCK );
  60. return iDamage;
  61. }
  62. //-----------------------------------------------------------------------------
  63. // Purpose:
  64. //-----------------------------------------------------------------------------
  65. int CSingleplayRules::Damage_GetNoPhysicsForce( void )
  66. {
  67. int iTimeBasedDamage = Damage_GetTimeBased();
  68. int iDamage = ( DMG_FALL | DMG_BURN | DMG_PLASMA | DMG_DROWN | iTimeBasedDamage | DMG_CRUSH | DMG_PHYSGUN | DMG_PREVENT_PHYSICS_FORCE );
  69. return iDamage;
  70. }
  71. //-----------------------------------------------------------------------------
  72. // Purpose:
  73. //-----------------------------------------------------------------------------
  74. int CSingleplayRules::Damage_GetShouldNotBleed( void )
  75. {
  76. int iDamage = ( DMG_POISON | DMG_ACID );
  77. return iDamage;
  78. }
  79. //-----------------------------------------------------------------------------
  80. // Purpose:
  81. // Input : iDmgType -
  82. // Output : Returns true on success, false on failure.
  83. //-----------------------------------------------------------------------------
  84. bool CSingleplayRules::Damage_IsTimeBased( int iDmgType )
  85. {
  86. // Damage types that are time-based.
  87. return ( ( iDmgType & ( DMG_PARALYZE | DMG_NERVEGAS | DMG_POISON | DMG_RADIATION | DMG_DROWNRECOVER | DMG_ACID | DMG_SLOWBURN ) ) != 0 );
  88. }
  89. //-----------------------------------------------------------------------------
  90. // Purpose:
  91. // Input : iDmgType -
  92. // Output : Returns true on success, false on failure.
  93. //-----------------------------------------------------------------------------
  94. bool CSingleplayRules::Damage_ShouldGibCorpse( int iDmgType )
  95. {
  96. // Damage types that gib the corpse.
  97. return ( ( iDmgType & ( DMG_CRUSH | DMG_FALL | DMG_BLAST | DMG_SONIC | DMG_CLUB ) ) != 0 );
  98. }
  99. //-----------------------------------------------------------------------------
  100. // Purpose:
  101. // Input : iDmgType -
  102. // Output : Returns true on success, false on failure.
  103. //-----------------------------------------------------------------------------
  104. bool CSingleplayRules::Damage_ShowOnHUD( int iDmgType )
  105. {
  106. // Damage types that have client HUD art.
  107. return ( ( iDmgType & ( DMG_POISON | DMG_ACID | DMG_DROWN | DMG_BURN | DMG_SLOWBURN | DMG_NERVEGAS | DMG_RADIATION | DMG_SHOCK ) ) != 0 );
  108. }
  109. //-----------------------------------------------------------------------------
  110. // Purpose:
  111. // Input : iDmgType -
  112. // Output : Returns true on success, false on failure.
  113. //-----------------------------------------------------------------------------
  114. bool CSingleplayRules::Damage_NoPhysicsForce( int iDmgType )
  115. {
  116. // Damage types that don't have to supply a physics force & position.
  117. int iTimeBasedDamage = Damage_GetTimeBased();
  118. return ( ( iDmgType & ( DMG_FALL | DMG_BURN | DMG_PLASMA | DMG_DROWN | iTimeBasedDamage | DMG_CRUSH | DMG_PHYSGUN | DMG_PREVENT_PHYSICS_FORCE ) ) != 0 );
  119. }
  120. //-----------------------------------------------------------------------------
  121. // Purpose:
  122. // Input : iDmgType -
  123. // Output : Returns true on success, false on failure.
  124. //-----------------------------------------------------------------------------
  125. bool CSingleplayRules::Damage_ShouldNotBleed( int iDmgType )
  126. {
  127. // Damage types that don't make the player bleed.
  128. return ( ( iDmgType & ( DMG_POISON | DMG_ACID ) ) != 0 );
  129. }
  130. #ifdef CLIENT_DLL
  131. #else
  132. extern CGameRules *g_pGameRules;
  133. extern bool g_fGameOver;
  134. //=========================================================
  135. //=========================================================
  136. CSingleplayRules::CSingleplayRules( void )
  137. {
  138. }
  139. //=========================================================
  140. //=========================================================
  141. void CSingleplayRules::Think ( void )
  142. {
  143. BaseClass::Think();
  144. }
  145. //=========================================================
  146. //=========================================================
  147. bool CSingleplayRules::IsDeathmatch ( void )
  148. {
  149. return false;
  150. }
  151. //=========================================================
  152. //=========================================================
  153. bool CSingleplayRules::IsCoOp( void )
  154. {
  155. return false;
  156. }
  157. //-----------------------------------------------------------------------------
  158. // Purpose: Determine whether the player should switch to the weapon passed in
  159. // Output : Returns true on success, false on failure.
  160. //-----------------------------------------------------------------------------
  161. bool CSingleplayRules::FShouldSwitchWeapon( CBasePlayer *pPlayer, CBaseCombatWeapon *pWeapon )
  162. {
  163. //Must have ammo
  164. if ( ( pWeapon->HasAnyAmmo() == false ) && ( pWeapon->GetReserveAmmoCount( AMMO_POSITION_PRIMARY ) <= 0 ) )
  165. return false;
  166. //Always take a loaded gun if we have nothing else
  167. if ( pPlayer->GetActiveWeapon() == NULL )
  168. return true;
  169. // The given weapon must allow autoswitching to it from another weapon.
  170. if ( !pWeapon->AllowsAutoSwitchTo() )
  171. return false;
  172. // The active weapon must allow autoswitching from it.
  173. if ( !pPlayer->GetActiveWeapon()->AllowsAutoSwitchFrom() )
  174. return false;
  175. //Don't switch if our current gun doesn't want to be holstered
  176. if ( pPlayer->GetActiveWeapon()->CanHolster() == false )
  177. return false;
  178. //Only switch if the weapon is better than what we're using
  179. if ( ( pWeapon != pPlayer->GetActiveWeapon() ) && ( pWeapon->GetWeight() <= pPlayer->GetActiveWeapon()->GetWeight() ) )
  180. return false;
  181. return true;
  182. }
  183. //-----------------------------------------------------------------------------
  184. // Purpose: Find the next best weapon to use and return it.
  185. //-----------------------------------------------------------------------------
  186. CBaseCombatWeapon *CSingleplayRules::GetNextBestWeapon( CBaseCombatCharacter *pPlayer, CBaseCombatWeapon *pCurrentWeapon )
  187. {
  188. if ( pCurrentWeapon && !pCurrentWeapon->AllowsAutoSwitchFrom() )
  189. return NULL;
  190. CBaseCombatWeapon *pBestWeapon = NULL;
  191. CBaseCombatWeapon *pWeapon;
  192. int nBestWeight = -1;
  193. //Search for the best weapon to use next based on its weight
  194. for ( int i = 0; i < pPlayer->WeaponCount(); i++ )
  195. {
  196. pWeapon = pPlayer->GetWeapon(i);
  197. if ( pWeapon == NULL )
  198. continue;
  199. #ifdef PORTAL2
  200. if ( pWeapon == pCurrentWeapon )
  201. continue;
  202. #endif // PORTAL2
  203. // If we have an active weapon and this weapon doesn't allow autoswitching away
  204. // from another weapon, skip it.
  205. if ( pCurrentWeapon && !pWeapon->AllowsAutoSwitchTo() )
  206. continue;
  207. // Must be eligible for switching to.
  208. if (!pPlayer->Weapon_CanSwitchTo(pWeapon))
  209. continue;
  210. // Must be of higher quality.
  211. if ( pWeapon->GetWeight() <= nBestWeight )
  212. continue;
  213. // We must have primary ammo
  214. if ( pWeapon->UsesClipsForAmmo1() && pWeapon->Clip1() <= 0 && !pWeapon->GetReserveAmmoCount( AMMO_POSITION_PRIMARY ) )
  215. continue;
  216. // This is a better candidate than what we had.
  217. nBestWeight = pWeapon->GetWeight();
  218. pBestWeapon = pWeapon;
  219. }
  220. return pBestWeapon;
  221. }
  222. //-----------------------------------------------------------------------------
  223. // Purpose:
  224. // Output : Returns true on success, false on failure.
  225. //-----------------------------------------------------------------------------
  226. bool CSingleplayRules::SwitchToNextBestWeapon( CBaseCombatCharacter *pPlayer, CBaseCombatWeapon *pCurrentWeapon )
  227. {
  228. CBaseCombatWeapon *pWeapon = GetNextBestWeapon( pPlayer, pCurrentWeapon );
  229. if ( pWeapon != NULL )
  230. return pPlayer->Weapon_Switch( pWeapon );
  231. return false;
  232. }
  233. //=========================================================
  234. //=========================================================
  235. bool CSingleplayRules::ClientConnected( edict_t *pEntity, const char *pszName, const char *pszAddress, char *reject, int maxrejectlen )
  236. {
  237. return true;
  238. }
  239. void CSingleplayRules::InitHUD( CBasePlayer *pl )
  240. {
  241. }
  242. //=========================================================
  243. //=========================================================
  244. void CSingleplayRules::ClientDisconnected( edict_t *pClient )
  245. {
  246. }
  247. //=========================================================
  248. //=========================================================
  249. float CSingleplayRules::FlPlayerFallDamage( CBasePlayer *pPlayer )
  250. {
  251. // subtract off the speed at which a player is allowed to fall without being hurt,
  252. // so damage will be based on speed beyond that, not the entire fall
  253. return (pPlayer->m_Local.m_flFallVelocity - PLAYER_MAX_SAFE_FALL_SPEED) * DAMAGE_FOR_FALL_SPEED;
  254. }
  255. //=========================================================
  256. //=========================================================
  257. bool CSingleplayRules::AllowDamage( CBaseEntity *pVictim, const CTakeDamageInfo &info )
  258. {
  259. return true;
  260. }
  261. //=========================================================
  262. //=========================================================
  263. void CSingleplayRules::PlayerSpawn( CBasePlayer *pPlayer )
  264. {
  265. // Player no longer gets all weapons to start.
  266. // He has to pick them up now. Use impulse 101
  267. // to give him all weapons
  268. }
  269. //=========================================================
  270. //=========================================================
  271. bool CSingleplayRules::AllowAutoTargetCrosshair( void )
  272. {
  273. return ( IsSkillLevel(SKILL_EASY) );
  274. }
  275. //=========================================================
  276. //=========================================================
  277. int CSingleplayRules::GetAutoAimMode()
  278. {
  279. return sk_autoaim_mode.GetInt();
  280. }
  281. //=========================================================
  282. //=========================================================
  283. bool CSingleplayRules::FPlayerCanRespawn( CBasePlayer *pPlayer )
  284. {
  285. return true;
  286. }
  287. //=========================================================
  288. //=========================================================
  289. float CSingleplayRules::FlPlayerSpawnTime( CBasePlayer *pPlayer )
  290. {
  291. return gpGlobals->curtime;//now!
  292. }
  293. //=========================================================
  294. // IPointsForKill - how many points awarded to anyone
  295. // that kills this player?
  296. //=========================================================
  297. int CSingleplayRules::IPointsForKill( CBasePlayer *pAttacker, CBasePlayer *pKilled )
  298. {
  299. return 1;
  300. }
  301. //=========================================================
  302. // PlayerKilled - someone/something killed this player
  303. //=========================================================
  304. void CSingleplayRules::PlayerKilled( CBasePlayer *pVictim, const CTakeDamageInfo &info )
  305. {
  306. }
  307. //=========================================================
  308. // Deathnotice
  309. //=========================================================
  310. void CSingleplayRules::DeathNotice( CBasePlayer *pVictim, const CTakeDamageInfo &info )
  311. {
  312. }
  313. //=========================================================
  314. // FlWeaponRespawnTime - what is the time in the future
  315. // at which this weapon may spawn?
  316. //=========================================================
  317. float CSingleplayRules::FlWeaponRespawnTime( CBaseCombatWeapon *pWeapon )
  318. {
  319. return -1;
  320. }
  321. //=========================================================
  322. // FlWeaponRespawnTime - Returns 0 if the weapon can respawn
  323. // now, otherwise it returns the time at which it can try
  324. // to spawn again.
  325. //=========================================================
  326. float CSingleplayRules::FlWeaponTryRespawn( CBaseCombatWeapon *pWeapon )
  327. {
  328. return 0;
  329. }
  330. //=========================================================
  331. // VecWeaponRespawnSpot - where should this weapon spawn?
  332. // Some game variations may choose to randomize spawn locations
  333. //=========================================================
  334. Vector CSingleplayRules::VecWeaponRespawnSpot( CBaseCombatWeapon *pWeapon )
  335. {
  336. return pWeapon->GetAbsOrigin();
  337. }
  338. //=========================================================
  339. // WeaponShouldRespawn - any conditions inhibiting the
  340. // respawning of this weapon?
  341. //=========================================================
  342. int CSingleplayRules::WeaponShouldRespawn( CBaseCombatWeapon *pWeapon )
  343. {
  344. return GR_WEAPON_RESPAWN_NO;
  345. }
  346. //=========================================================
  347. //=========================================================
  348. bool CSingleplayRules::CanHaveItem( CBasePlayer *pPlayer, CItem *pItem )
  349. {
  350. return true;
  351. }
  352. //=========================================================
  353. //=========================================================
  354. void CSingleplayRules::PlayerGotItem( CBasePlayer *pPlayer, CItem *pItem )
  355. {
  356. }
  357. //=========================================================
  358. //=========================================================
  359. int CSingleplayRules::ItemShouldRespawn( CItem *pItem )
  360. {
  361. return GR_ITEM_RESPAWN_NO;
  362. }
  363. //=========================================================
  364. // At what time in the future may this Item respawn?
  365. //=========================================================
  366. float CSingleplayRules::FlItemRespawnTime( CItem *pItem )
  367. {
  368. return -1;
  369. }
  370. //=========================================================
  371. // Where should this item respawn?
  372. // Some game variations may choose to randomize spawn locations
  373. //=========================================================
  374. Vector CSingleplayRules::VecItemRespawnSpot( CItem *pItem )
  375. {
  376. return pItem->GetAbsOrigin();
  377. }
  378. //=========================================================
  379. // What angles should this item use to respawn?
  380. //=========================================================
  381. QAngle CSingleplayRules::VecItemRespawnAngles( CItem *pItem )
  382. {
  383. return pItem->GetAbsAngles();
  384. }
  385. //=========================================================
  386. //=========================================================
  387. bool CSingleplayRules::IsAllowedToSpawn( CBaseEntity *pEntity )
  388. {
  389. return true;
  390. }
  391. //=========================================================
  392. //=========================================================
  393. void CSingleplayRules::PlayerGotAmmo( CBaseCombatCharacter *pPlayer, char *szName, int iCount )
  394. {
  395. }
  396. //=========================================================
  397. //=========================================================
  398. float CSingleplayRules::FlHealthChargerRechargeTime( void )
  399. {
  400. return 0;// don't recharge
  401. }
  402. //=========================================================
  403. //=========================================================
  404. int CSingleplayRules::DeadPlayerWeapons( CBasePlayer *pPlayer )
  405. {
  406. return GR_PLR_DROP_GUN_NO;
  407. }
  408. //=========================================================
  409. //=========================================================
  410. int CSingleplayRules::DeadPlayerAmmo( CBasePlayer *pPlayer )
  411. {
  412. return GR_PLR_DROP_AMMO_NO;
  413. }
  414. //=========================================================
  415. //=========================================================
  416. int CSingleplayRules::PlayerRelationship( CBaseEntity *pPlayer, CBaseEntity *pTarget )
  417. {
  418. // why would a single player in half life need this?
  419. return GR_NOTTEAMMATE;
  420. }
  421. //=========================================================
  422. //=========================================================
  423. bool CSingleplayRules::PlayerCanHearChat( CBasePlayer *pListener, CBasePlayer *pSpeaker, bool bTeamOnly )
  424. {
  425. return !bTeamOnly || PlayerRelationship( pListener, pSpeaker ) == GR_TEAMMATE;
  426. }
  427. //=========================================================
  428. //=========================================================
  429. bool CSingleplayRules::FAllowNPCs( void )
  430. {
  431. return true;
  432. }
  433. #endif