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.

420 lines
12 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Implements a brush model entity that moves along a linear path.
  4. // Water whose level can be changed is implemented using the same entity.
  5. //
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "func_movelinear.h"
  9. #include "entitylist.h"
  10. #include "locksounds.h"
  11. #include "ndebugoverlay.h"
  12. #include "engine/IEngineSound.h"
  13. #include "physics_saverestore.h"
  14. // memdbgon must be the last include file in a .cpp file!!!
  15. #include "tier0/memdbgon.h"
  16. // -------------------------------
  17. // SPAWN_FLAGS
  18. // -------------------------------
  19. #define SF_MOVELINEAR_NOTSOLID 8
  20. LINK_ENTITY_TO_CLASS( func_movelinear, CFuncMoveLinear );
  21. LINK_ENTITY_TO_CLASS( momentary_door, CFuncMoveLinear ); // For backward compatibility
  22. //
  23. // func_water_analog is implemented as a linear mover so we can raise/lower the water level.
  24. //
  25. LINK_ENTITY_TO_CLASS( func_water_analog, CFuncMoveLinear );
  26. BEGIN_DATADESC( CFuncMoveLinear )
  27. DEFINE_KEYFIELD( m_vecMoveDir, FIELD_VECTOR, "movedir" ),
  28. DEFINE_KEYFIELD( m_soundStart, FIELD_SOUNDNAME, "StartSound" ),
  29. DEFINE_KEYFIELD( m_soundStop, FIELD_SOUNDNAME, "StopSound" ),
  30. DEFINE_FIELD( m_currentSound, FIELD_SOUNDNAME ),
  31. DEFINE_KEYFIELD( m_flBlockDamage, FIELD_FLOAT, "BlockDamage"),
  32. DEFINE_KEYFIELD( m_flStartPosition, FIELD_FLOAT, "StartPosition"),
  33. DEFINE_KEYFIELD( m_flMoveDistance, FIELD_FLOAT, "MoveDistance"),
  34. // DEFINE_PHYSPTR( m_pFluidController ),
  35. // Inputs
  36. DEFINE_INPUTFUNC( FIELD_VOID, "Open", InputOpen ),
  37. DEFINE_INPUTFUNC( FIELD_VOID, "Close", InputClose ),
  38. DEFINE_INPUTFUNC( FIELD_FLOAT, "SetPosition", InputSetPosition ),
  39. DEFINE_INPUTFUNC( FIELD_FLOAT, "SetSpeed", InputSetSpeed ),
  40. // Outputs
  41. DEFINE_OUTPUT( m_OnFullyOpen, "OnFullyOpen" ),
  42. DEFINE_OUTPUT( m_OnFullyClosed, "OnFullyClosed" ),
  43. // Functions
  44. DEFINE_FUNCTION( StopMoveSound ),
  45. END_DATADESC()
  46. void SendProxy_CropFlagsToConveyorFlagBitsLength( const SendProp *pProp, const void *pStruct, const void *pVarData, DVariant *pOut, int iElement, int objectID)
  47. {
  48. int mask = (1<<13) - 1;
  49. int data = *(int *)pVarData;
  50. pOut->m_Int = ( data & mask );
  51. }
  52. IMPLEMENT_SERVERCLASS_ST(CFuncMoveLinear, DT_FuncMoveLinear)
  53. SendPropVector( SENDINFO( m_vecVelocity ), 0, SPROP_NOSCALE ),
  54. SendPropInt ( SENDINFO(m_fFlags), 0, SPROP_UNSIGNED ),
  55. END_SEND_TABLE()
  56. //------------------------------------------------------------------------------
  57. // Purpose: Called before spawning, after keyvalues have been parsed.
  58. //------------------------------------------------------------------------------
  59. void CFuncMoveLinear::Spawn( void )
  60. {
  61. // Convert movedir from angles to a vector
  62. QAngle angMoveDir = QAngle( m_vecMoveDir.x, m_vecMoveDir.y, m_vecMoveDir.z );
  63. AngleVectors( angMoveDir, &m_vecMoveDir );
  64. SetMoveType( MOVETYPE_PUSH );
  65. SetModel( STRING( GetModelName() ) );
  66. // Don't allow zero or negative speeds
  67. if (m_flSpeed <= 0)
  68. {
  69. m_flSpeed = 100;
  70. }
  71. // If move distance is set to zero, use with width of the
  72. // brush to determine the size of the move distance
  73. if (m_flMoveDistance <= 0)
  74. {
  75. Vector vecOBB = CollisionProp()->OBBSize();
  76. vecOBB -= Vector( 2, 2, 2 );
  77. m_flMoveDistance = DotProductAbs( m_vecMoveDir, vecOBB ) - m_flLip;
  78. }
  79. m_vecPosition1 = GetLocalOrigin() - (m_vecMoveDir * m_flMoveDistance * m_flStartPosition);
  80. m_vecPosition2 = m_vecPosition1 + (m_vecMoveDir * m_flMoveDistance);
  81. m_vecFinalDest = GetLocalOrigin();
  82. SetTouch( NULL );
  83. Precache();
  84. // Disabling this flag, since it seems to just make the player move twice as fast when on one of these. -Brett
  85. //AddFlag( FL_CONVEYOR );
  86. // It is solid?
  87. SetSolid( SOLID_VPHYSICS );
  88. if ( FClassnameIs( this, "func_water_analog" ) )
  89. {
  90. AddSolidFlags( FSOLID_VOLUME_CONTENTS );
  91. }
  92. if ( !FClassnameIs( this, "func_water_analog" ) && FBitSet (m_spawnflags, SF_MOVELINEAR_NOTSOLID) )
  93. {
  94. AddSolidFlags( FSOLID_NOT_SOLID );
  95. }
  96. CreateVPhysics();
  97. }
  98. bool CFuncMoveLinear::ShouldSavePhysics( void )
  99. {
  100. // don't save physics for func_water_analog, regen
  101. return !FClassnameIs( this, "func_water_analog" );
  102. }
  103. //-----------------------------------------------------------------------------
  104. // Purpose:
  105. //-----------------------------------------------------------------------------
  106. bool CFuncMoveLinear::CreateVPhysics( void )
  107. {
  108. if ( !FClassnameIs( this, "func_water_analog" ) )
  109. {
  110. //normal door
  111. if ( !IsSolidFlagSet( FSOLID_NOT_SOLID ) )
  112. {
  113. VPhysicsInitShadow( false, false );
  114. }
  115. }
  116. else
  117. {
  118. // special contents
  119. AddSolidFlags( FSOLID_VOLUME_CONTENTS );
  120. //SETBITS( m_spawnflags, SF_DOOR_SILENT ); // water is silent for now
  121. IPhysicsObject *pPhysics = VPhysicsInitShadow( false, false );
  122. fluidparams_t fluid;
  123. Assert( CollisionProp()->GetCollisionAngles() == vec3_angle );
  124. fluid.damping = 0.01f;
  125. fluid.surfacePlane[0] = 0;
  126. fluid.surfacePlane[1] = 0;
  127. fluid.surfacePlane[2] = 1;
  128. fluid.surfacePlane[3] = CollisionProp()->GetCollisionOrigin().z + CollisionProp()->OBBMaxs().z - 1;
  129. fluid.currentVelocity.Init(0,0,0);
  130. fluid.torqueFactor = 0.1f;
  131. fluid.viscosityFactor = 0.01f;
  132. fluid.pGameData = static_cast<void *>(this);
  133. //FIXME: Currently there's no way to specify that you want slime
  134. fluid.contents = CONTENTS_WATER;
  135. m_pFluidController = physenv->CreateFluidController( pPhysics, &fluid );
  136. }
  137. return true;
  138. }
  139. //------------------------------------------------------------------------------
  140. // Purpose:
  141. //------------------------------------------------------------------------------
  142. void CFuncMoveLinear::Precache( void )
  143. {
  144. if (m_soundStart != NULL_STRING)
  145. {
  146. PrecacheScriptSound( (char *) STRING(m_soundStart) );
  147. }
  148. if (m_soundStop != NULL_STRING)
  149. {
  150. PrecacheScriptSound( (char *) STRING(m_soundStop) );
  151. }
  152. m_currentSound = NULL_STRING;
  153. }
  154. //------------------------------------------------------------------------------
  155. // Purpose:
  156. //------------------------------------------------------------------------------
  157. void CFuncMoveLinear::MoveTo(Vector vPosition, float flSpeed)
  158. {
  159. if ( flSpeed != 0 )
  160. {
  161. if ( m_soundStart != NULL_STRING )
  162. {
  163. if (m_currentSound == m_soundStart)
  164. {
  165. StopSound(entindex(), CHAN_BODY, (char*)STRING(m_soundStop));
  166. }
  167. else
  168. {
  169. m_currentSound = m_soundStart;
  170. CPASAttenuationFilter filter( this );
  171. EmitSound_t ep;
  172. ep.m_nChannel = CHAN_BODY;
  173. ep.m_pSoundName = (char*)STRING(m_soundStart);
  174. ep.m_flVolume = 1;
  175. ep.m_SoundLevel = SNDLVL_NORM;
  176. EmitSound( filter, entindex(), ep );
  177. }
  178. }
  179. LinearMove( vPosition, flSpeed );
  180. if ( m_pFluidController )
  181. {
  182. m_pFluidController->WakeAllSleepingObjects();
  183. }
  184. // Clear think (that stops sounds)
  185. SetThink(NULL);
  186. }
  187. }
  188. //------------------------------------------------------------------------------
  189. // Purpose:
  190. //------------------------------------------------------------------------------
  191. void CFuncMoveLinear::StopMoveSound( void )
  192. {
  193. if ( m_soundStart != NULL_STRING && ( m_currentSound == m_soundStart ) )
  194. {
  195. StopSound(entindex(), CHAN_BODY, (char*)STRING(m_soundStart) );
  196. }
  197. if ( m_soundStop != NULL_STRING && ( m_currentSound != m_soundStop ) )
  198. {
  199. m_currentSound = m_soundStop;
  200. CPASAttenuationFilter filter( this );
  201. EmitSound_t ep;
  202. ep.m_nChannel = CHAN_BODY;
  203. ep.m_pSoundName = (char*)STRING(m_soundStop);
  204. ep.m_flVolume = 1;
  205. ep.m_SoundLevel = SNDLVL_NORM;
  206. EmitSound( filter, entindex(), ep );
  207. }
  208. SetThink(NULL);
  209. }
  210. //------------------------------------------------------------------------------
  211. // Purpose:
  212. //------------------------------------------------------------------------------
  213. void CFuncMoveLinear::MoveDone( void )
  214. {
  215. // Stop sounds at the next think, rather than here as another
  216. // SetPosition call might immediately follow the end of this move
  217. SetThink(&CFuncMoveLinear::StopMoveSound);
  218. SetNextThink( gpGlobals->curtime + 0.1f );
  219. BaseClass::MoveDone();
  220. if ( GetLocalOrigin() == m_vecPosition2 )
  221. {
  222. m_OnFullyOpen.FireOutput( this, this );
  223. }
  224. else if ( GetLocalOrigin() == m_vecPosition1 )
  225. {
  226. m_OnFullyClosed.FireOutput( this, this );
  227. }
  228. }
  229. //------------------------------------------------------------------------------
  230. // Purpose:
  231. //------------------------------------------------------------------------------
  232. void CFuncMoveLinear::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
  233. {
  234. if ( useType != USE_SET ) // Momentary buttons will pass down a float in here
  235. return;
  236. if ( value > 1.0 )
  237. value = 1.0;
  238. Vector move = m_vecPosition1 + (value * (m_vecPosition2 - m_vecPosition1));
  239. Vector delta = move - GetLocalOrigin();
  240. float speed = delta.Length() * 10;
  241. MoveTo(move, speed);
  242. }
  243. //-----------------------------------------------------------------------------
  244. // Purpose: Sets the position as a value from [0..1].
  245. //-----------------------------------------------------------------------------
  246. void CFuncMoveLinear::SetPosition( float flPosition )
  247. {
  248. Vector vTargetPos = m_vecPosition1 + ( flPosition * (m_vecPosition2 - m_vecPosition1));
  249. if ((vTargetPos - GetLocalOrigin()).Length() > 0.001)
  250. {
  251. MoveTo(vTargetPos, m_flSpeed);
  252. }
  253. }
  254. //------------------------------------------------------------------------------
  255. // Purpose:
  256. //------------------------------------------------------------------------------
  257. void CFuncMoveLinear::InputOpen( inputdata_t &inputdata )
  258. {
  259. if (GetLocalOrigin() != m_vecPosition2)
  260. {
  261. MoveTo(m_vecPosition2, m_flSpeed);
  262. }
  263. }
  264. //------------------------------------------------------------------------------
  265. // Purpose:
  266. //------------------------------------------------------------------------------
  267. void CFuncMoveLinear::InputClose( inputdata_t &inputdata )
  268. {
  269. if (GetLocalOrigin() != m_vecPosition1)
  270. {
  271. MoveTo(m_vecPosition1, m_flSpeed);
  272. }
  273. }
  274. //------------------------------------------------------------------------------
  275. // Purpose: Input handler for setting the position from [0..1].
  276. // Input : Float position.
  277. //-----------------------------------------------------------------------------
  278. void CFuncMoveLinear::InputSetPosition( inputdata_t &inputdata )
  279. {
  280. SetPosition( inputdata.value.Float() );
  281. }
  282. //-----------------------------------------------------------------------------
  283. // Purpose: Called every frame when the bruch is blocked while moving
  284. // Input : pOther - The blocking entity.
  285. //-----------------------------------------------------------------------------
  286. void CFuncMoveLinear::Blocked( CBaseEntity *pOther )
  287. {
  288. // Hurt the blocker
  289. if ( m_flBlockDamage )
  290. {
  291. if ( pOther->m_takedamage == DAMAGE_EVENTS_ONLY )
  292. {
  293. if ( FClassnameIs( pOther, "gib" ) )
  294. UTIL_Remove( pOther );
  295. }
  296. else
  297. pOther->TakeDamage( CTakeDamageInfo( this, this, m_flBlockDamage, DMG_CRUSH ) );
  298. }
  299. }
  300. //-----------------------------------------------------------------------------
  301. // Purpose:
  302. // Input : &inputdata -
  303. //-----------------------------------------------------------------------------
  304. void CFuncMoveLinear::InputSetSpeed( inputdata_t &inputdata )
  305. {
  306. // Set the new speed
  307. m_flSpeed = inputdata.value.Float();
  308. // FIXME: This is a little questionable. Do we want to fix the speed, or let it continue on at the old speed?
  309. float flDistToGoalSqr = ( m_vecFinalDest - GetLocalOrigin() ).LengthSqr();
  310. if ( flDistToGoalSqr > Square( FLT_EPSILON ) )
  311. {
  312. if ( fabs( m_flSpeed ) > FLT_EPSILON )
  313. {
  314. // NOTE: We do NOT want to call sound functions here, just vanilla position changes
  315. LinearMove( m_vecFinalDest, m_flSpeed );
  316. }
  317. else
  318. {
  319. // If we set our speed to 0, just move to the current position to stop.
  320. m_flSpeed = 1.0f;
  321. LinearMove( GetLocalOrigin(), m_flSpeed );
  322. }
  323. }
  324. }
  325. //-----------------------------------------------------------------------------
  326. // Purpose: Draw any debug text overlays
  327. // Output : Current text offset from the top
  328. //-----------------------------------------------------------------------------
  329. int CFuncMoveLinear::DrawDebugTextOverlays(void)
  330. {
  331. int text_offset = BaseClass::DrawDebugTextOverlays();
  332. if (m_debugOverlays & OVERLAY_TEXT_BIT)
  333. {
  334. char tempstr[512];
  335. float flTravelDist = (m_vecPosition1 - m_vecPosition2).Length();
  336. float flCurDist = (m_vecPosition1 - GetLocalOrigin()).Length();
  337. Q_snprintf(tempstr,sizeof(tempstr),"Current Pos: %3.3f",flCurDist/flTravelDist);
  338. EntityText(text_offset,tempstr,0);
  339. text_offset++;
  340. float flTargetDist = (m_vecPosition1 - m_vecFinalDest).Length();
  341. Q_snprintf(tempstr,sizeof(tempstr),"Target Pos: %3.3f",flTargetDist/flTravelDist);
  342. EntityText(text_offset,tempstr,0);
  343. text_offset++;
  344. }
  345. return text_offset;
  346. }