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