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.

307 lines
7.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: CTF AmmoPack.
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "items.h"
  8. #include "tf_gamerules.h"
  9. #include "tf_shareddefs.h"
  10. #include "tf_player.h"
  11. #include "tf_team.h"
  12. #include "engine/IEngineSound.h"
  13. #include "filesystem.h"
  14. #include "tf_powerup.h"
  15. #include "bot/tf_bot.h"
  16. //=============================================================================
  17. float PackRatios[POWERUP_SIZES] =
  18. {
  19. 0.2, // SMALL
  20. 0.5, // MEDIUM
  21. 1.0, // FULL
  22. };
  23. //=============================================================================
  24. //
  25. // CTF Powerup tables.
  26. //
  27. BEGIN_DATADESC( CTFPowerup )
  28. // Keyfields.
  29. DEFINE_KEYFIELD( m_bDisabled, FIELD_BOOLEAN, "StartDisabled" ),
  30. DEFINE_KEYFIELD( m_iszModel, FIELD_STRING, "powerup_model" ),
  31. DEFINE_KEYFIELD( m_bAutoMaterialize, FIELD_BOOLEAN, "AutoMaterialize" ),
  32. // Inputs.
  33. DEFINE_INPUTFUNC( FIELD_VOID, "Enable", InputEnable ),
  34. DEFINE_INPUTFUNC( FIELD_VOID, "Disable", InputDisable ),
  35. DEFINE_INPUTFUNC( FIELD_VOID, "Toggle", InputToggle ),
  36. // Outputs.
  37. END_DATADESC();
  38. //=============================================================================
  39. //
  40. // CTF Powerup functions.
  41. //
  42. //-----------------------------------------------------------------------------
  43. // Purpose:
  44. //-----------------------------------------------------------------------------
  45. CTFPowerup::CTFPowerup()
  46. {
  47. m_bDisabled = false;
  48. m_bRespawning = false;
  49. m_bAutoMaterialize = true;
  50. m_iszModel = NULL_STRING;
  51. m_flThrowerTouchTime = -1;
  52. UseClientSideAnimation();
  53. }
  54. //-----------------------------------------------------------------------------
  55. // Purpose:
  56. //-----------------------------------------------------------------------------
  57. void CTFPowerup::Spawn( void )
  58. {
  59. Precache();
  60. SetModel( GetPowerupModel() );
  61. BaseClass::Spawn();
  62. BaseClass::SetOriginalSpawnOrigin( GetAbsOrigin() );
  63. BaseClass::SetOriginalSpawnAngles( GetAbsAngles() );
  64. VPhysicsDestroyObject();
  65. SetMoveType( MOVETYPE_NONE );
  66. SetSolidFlags( FSOLID_NOT_SOLID | FSOLID_TRIGGER );
  67. if ( m_bDisabled )
  68. {
  69. SetDisabled( true );
  70. }
  71. m_bRespawning = false;
  72. ResetSequence( LookupSequence("idle") );
  73. }
  74. //-----------------------------------------------------------------------------
  75. // Purpose:
  76. //-----------------------------------------------------------------------------
  77. void CTFPowerup::Precache()
  78. {
  79. PrecacheModel( GetPowerupModel() );
  80. BaseClass::Precache();
  81. }
  82. //-----------------------------------------------------------------------------
  83. // Purpose:
  84. //-----------------------------------------------------------------------------
  85. CBaseEntity* CTFPowerup::Respawn( void )
  86. {
  87. m_bRespawning = true;
  88. CBaseEntity *pReturn = BaseClass::Respawn();
  89. // Override the respawn time
  90. SetNextThink( gpGlobals->curtime + GetRespawnDelay() );
  91. return pReturn;
  92. }
  93. //-----------------------------------------------------------------------------
  94. // Purpose:
  95. //-----------------------------------------------------------------------------
  96. void CTFPowerup::Materialize( void )
  97. {
  98. if ( !m_bAutoMaterialize )
  99. {
  100. return;
  101. }
  102. Materialize_Internal();
  103. }
  104. //-----------------------------------------------------------------------------
  105. // Purpose:
  106. //-----------------------------------------------------------------------------
  107. void CTFPowerup::Materialize_Internal( void )
  108. {
  109. if ( !m_bDisabled && IsEffectActive( EF_NODRAW ) )
  110. {
  111. // changing from invisible state to visible.
  112. EmitSound( "Item.Materialize" );
  113. RemoveEffects( EF_NODRAW );
  114. }
  115. m_bRespawning = false;
  116. SetTouch( &CItem::ItemTouch );
  117. }
  118. //-----------------------------------------------------------------------------
  119. // Purpose:
  120. //-----------------------------------------------------------------------------
  121. bool CTFPowerup::ValidTouch( CBasePlayer *pPlayer )
  122. {
  123. // Is the item enabled?
  124. if ( IsDisabled() )
  125. {
  126. return false;
  127. }
  128. // Only touch a live player.
  129. if ( !pPlayer || !pPlayer->IsPlayer() || !pPlayer->IsAlive() )
  130. {
  131. return false;
  132. }
  133. // Team number and does it match?
  134. int iTeam = GetTeamNumber();
  135. if ( iTeam && ( pPlayer->GetTeamNumber() != iTeam ) )
  136. {
  137. return false;
  138. }
  139. // enemies in mann vs machine can't pick up any powerups
  140. if ( TFGameRules()->IsMannVsMachineMode() && pPlayer->GetTeamNumber() == TF_TEAM_PVE_INVADERS )
  141. {
  142. return false;
  143. }
  144. return true;
  145. }
  146. //-----------------------------------------------------------------------------
  147. // Purpose:
  148. //-----------------------------------------------------------------------------
  149. bool CTFPowerup::MyTouch( CBasePlayer *pPlayer )
  150. {
  151. return false;
  152. }
  153. //-----------------------------------------------------------------------------
  154. // Purpose:
  155. //-----------------------------------------------------------------------------
  156. void CTFPowerup::DropSingleInstance( Vector &vecLaunchVel, CBaseCombatCharacter *pThrower, float flThrowerTouchDelay, float flResetTime /*= 0.1f*/ )
  157. {
  158. // SetSize( Vector(-8,-8,-8), Vector(8,8,8) );
  159. SetMoveType( MOVETYPE_FLYGRAVITY, MOVECOLLIDE_FLY_BOUNCE );
  160. SetAbsVelocity( vecLaunchVel );
  161. SetSolid( SOLID_BBOX );
  162. if ( flResetTime )
  163. {
  164. ActivateWhenAtRest( flResetTime );
  165. }
  166. m_bThrownSingleInstance = true;
  167. AddSpawnFlags( SF_NORESPAWN );
  168. SetOwnerEntity( pThrower );
  169. m_flThrowerTouchTime = gpGlobals->curtime + flThrowerTouchDelay;
  170. // Remove ourselves after some time
  171. SetContextThink( &CBaseEntity::SUB_Remove, gpGlobals->curtime + GetLifeTime(), "PowerupRemoveThink" );
  172. }
  173. //-----------------------------------------------------------------------------
  174. // Purpose:
  175. //-----------------------------------------------------------------------------
  176. void CTFPowerup::InputEnable( inputdata_t &inputdata )
  177. {
  178. SetDisabled( false );
  179. }
  180. //-----------------------------------------------------------------------------
  181. // Purpose:
  182. //-----------------------------------------------------------------------------
  183. void CTFPowerup::InputDisable( inputdata_t &inputdata )
  184. {
  185. SetDisabled( true );
  186. }
  187. //-----------------------------------------------------------------------------
  188. // Purpose:
  189. //-----------------------------------------------------------------------------
  190. bool CTFPowerup::IsDisabled( void )
  191. {
  192. return m_bDisabled;
  193. }
  194. //-----------------------------------------------------------------------------
  195. // Purpose:
  196. //-----------------------------------------------------------------------------
  197. void CTFPowerup::InputToggle( inputdata_t &inputdata )
  198. {
  199. if ( m_bDisabled )
  200. {
  201. SetDisabled( false );
  202. }
  203. else
  204. {
  205. SetDisabled( true );
  206. }
  207. }
  208. //-----------------------------------------------------------------------------
  209. // Purpose:
  210. //-----------------------------------------------------------------------------
  211. void CTFPowerup::SetDisabled( bool bDisabled )
  212. {
  213. m_bDisabled = bDisabled;
  214. if ( bDisabled )
  215. {
  216. AddEffects( EF_NODRAW );
  217. }
  218. else
  219. {
  220. // only turn it back on if we're not in the middle of respawning
  221. if ( !m_bRespawning )
  222. {
  223. RemoveEffects( EF_NODRAW );
  224. }
  225. else if ( !m_bAutoMaterialize )
  226. {
  227. // We wait for a set-enabled to re-materialize if we were
  228. // set to not auto-materialize
  229. Materialize_Internal();
  230. }
  231. }
  232. }
  233. //-----------------------------------------------------------------------------
  234. // Purpose:
  235. //-----------------------------------------------------------------------------
  236. const char *CTFPowerup::GetPowerupModel( void )
  237. {
  238. if ( m_iszModel != NULL_STRING )
  239. {
  240. if ( g_pFullFileSystem->FileExists( STRING( m_iszModel ), "GAME" ) )
  241. {
  242. return ( STRING( m_iszModel ) );
  243. }
  244. }
  245. return GetDefaultPowerupModel();
  246. }
  247. //-----------------------------------------------------------------------------
  248. // Purpose:
  249. //-----------------------------------------------------------------------------
  250. bool CTFPowerup::ItemCanBeTouchedByPlayer( CBasePlayer *pPlayer )
  251. {
  252. if ( pPlayer == GetOwnerEntity() )
  253. {
  254. if ( ( m_flThrowerTouchTime > 0 ) && ( gpGlobals->curtime < m_flThrowerTouchTime ) )
  255. {
  256. return false;
  257. }
  258. }
  259. return BaseClass::ItemCanBeTouchedByPlayer( pPlayer );
  260. }