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.

147 lines
4.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Teleports a named entity to a given position and restores
  4. // it's physics state
  5. //
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. #include "cbase.h"
  9. #include "in_buttons.h"
  10. // memdbgon must be the last include file in a .cpp file!!!
  11. #include "tier0/memdbgon.h"
  12. #define SF_TELEPORT_TO_SPAWN_POS 0x00000001
  13. #define SF_TELEPORT_INTO_DUCK 0x00000002 ///< episodic only: player should be ducked after this teleport
  14. class CPointTeleport : public CBaseEntity
  15. {
  16. DECLARE_CLASS( CPointTeleport, CBaseEntity );
  17. public:
  18. void Activate( void );
  19. void InputTeleport( inputdata_t &inputdata );
  20. private:
  21. bool EntityMayTeleport( CBaseEntity *pTarget );
  22. Vector m_vSaveOrigin;
  23. QAngle m_vSaveAngles;
  24. DECLARE_DATADESC();
  25. };
  26. LINK_ENTITY_TO_CLASS( point_teleport, CPointTeleport );
  27. BEGIN_DATADESC( CPointTeleport )
  28. DEFINE_FIELD( m_vSaveOrigin, FIELD_VECTOR ),
  29. DEFINE_FIELD( m_vSaveAngles, FIELD_VECTOR ),
  30. DEFINE_INPUTFUNC( FIELD_VOID, "Teleport", InputTeleport ),
  31. END_DATADESC()
  32. //-----------------------------------------------------------------------------
  33. // Purpose:
  34. // Input : *pTarget -
  35. // Output : Returns true if the entity may be teleported
  36. //-----------------------------------------------------------------------------
  37. bool CPointTeleport::EntityMayTeleport( CBaseEntity *pTarget )
  38. {
  39. if ( pTarget->GetMoveParent() != NULL )
  40. {
  41. // Passengers in a vehicle are allowed to teleport; their behavior handles it
  42. CBaseCombatCharacter *pBCC = pTarget->MyCombatCharacterPointer();
  43. if ( pBCC == NULL || ( pBCC != NULL && pBCC->IsInAVehicle() == false ) )
  44. return false;
  45. }
  46. return true;
  47. }
  48. //------------------------------------------------------------------------------
  49. // Purpose:
  50. //------------------------------------------------------------------------------
  51. void CPointTeleport::Activate( void )
  52. {
  53. // Start with our origin point
  54. m_vSaveOrigin = GetAbsOrigin();
  55. m_vSaveAngles = GetAbsAngles();
  56. // Save off the spawn position of the target if instructed to do so
  57. if ( m_spawnflags & SF_TELEPORT_TO_SPAWN_POS )
  58. {
  59. CBaseEntity *pTarget = gEntList.FindEntityByName( NULL, m_target );
  60. if ( pTarget )
  61. {
  62. // If teleport object is in a movement hierarchy, remove it first
  63. if ( EntityMayTeleport( pTarget ) )
  64. {
  65. // Save the points
  66. m_vSaveOrigin = pTarget->GetAbsOrigin();
  67. m_vSaveAngles = pTarget->GetAbsAngles();
  68. }
  69. else
  70. {
  71. Warning("ERROR: (%s) can't teleport object (%s) as it has a parent (%s)!\n",GetDebugName(),pTarget->GetDebugName(),pTarget->GetMoveParent()->GetDebugName());
  72. BaseClass::Activate();
  73. return;
  74. }
  75. }
  76. else
  77. {
  78. Warning("ERROR: (%s) target '%s' not found. Deleting.\n", GetDebugName(), STRING(m_target));
  79. UTIL_Remove( this );
  80. return;
  81. }
  82. }
  83. BaseClass::Activate();
  84. }
  85. //------------------------------------------------------------------------------
  86. // Purpose:
  87. //------------------------------------------------------------------------------
  88. void CPointTeleport::InputTeleport( inputdata_t &inputdata )
  89. {
  90. // Attempt to find the entity in question
  91. CBaseEntity *pTarget = gEntList.FindEntityByName( NULL, m_target, this, inputdata.pActivator, inputdata.pCaller );
  92. if ( pTarget == NULL )
  93. return;
  94. // If teleport object is in a movement hierarchy, remove it first
  95. if ( EntityMayTeleport( pTarget ) == false )
  96. {
  97. Warning("ERROR: (%s) can't teleport object (%s) as it has a parent (%s)!\n",GetDebugName(),pTarget->GetDebugName(),pTarget->GetMoveParent()->GetDebugName());
  98. return;
  99. }
  100. // in episodic, we have a special spawn flag that forces Gordon into a duck
  101. #ifdef HL2_EPISODIC
  102. if ( (m_spawnflags & SF_TELEPORT_INTO_DUCK) && pTarget->IsPlayer() )
  103. {
  104. CBasePlayer *pPlayer = ToBasePlayer( pTarget );
  105. if ( pPlayer != NULL )
  106. {
  107. pPlayer->m_nButtons |= IN_DUCK;
  108. pPlayer->AddFlag( FL_DUCKING );
  109. pPlayer->m_Local.m_bDucked = true;
  110. pPlayer->m_Local.m_bDucking = true;
  111. pPlayer->m_Local.m_flDucktime = 0.0f;
  112. pPlayer->SetViewOffset( VEC_DUCK_VIEW_SCALED( pPlayer ) );
  113. pPlayer->SetCollisionBounds( VEC_DUCK_HULL_MIN, VEC_DUCK_HULL_MAX );
  114. }
  115. }
  116. #endif
  117. pTarget->Teleport( &m_vSaveOrigin, &m_vSaveAngles, NULL );
  118. }