forked from nillerusr/source-engine
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenericmonster.cpp
472 lines (396 loc) · 13.2 KB
/
genericmonster.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//=============================================================================//
//=========================================================
// Generic NPC - purely for scripted sequence work.
//=========================================================
#include "cbase.h"
#include "npcevent.h"
#include "ai_basenpc.h"
#include "ai_hull.h"
#include "KeyValues.h"
#include "engine/IEngineSound.h"
#include "physics_bone_follower.h"
#include "ai_baseactor.h"
#include "ai_senses.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
// For holograms, make them not solid so the player can walk through them
#define SF_GENERICNPC_NOTSOLID (1 << 16)
//=========================================================
// NPC's Anim Events Go Here
//=========================================================
class CGenericNPC : public CAI_BaseNPC
{
public:
DECLARE_CLASS( CGenericNPC, CAI_BaseNPC );
void Spawn( void );
void Precache( void );
float MaxYawSpeed( void );
Class_T Classify ( void );
void HandleAnimEvent( animevent_t *pEvent );
int GetSoundInterests ( void );
void TempGunEffect( void );
};
LINK_ENTITY_TO_CLASS( monster_generic, CGenericNPC );
//=========================================================
// Classify - indicates this NPC's place in the
// relationship table.
//=========================================================
Class_T CGenericNPC::Classify ( void )
{
return CLASS_NONE;
}
//=========================================================
// MaxYawSpeed - allows each sequence to have a different
// turn rate associated with it.
//=========================================================
float CGenericNPC::MaxYawSpeed ( void )
{
return 90;
}
//---------------------------------------------------------
// !!!TEMP
// !!!TEMP
// !!!TEMP
// !!!TEMP
//
// (sjb)
//---------------------------------------------------------
void CGenericNPC::TempGunEffect( void )
{
QAngle vecAngle;
Vector vecDir, vecShot;
Vector vecMuzzle, vecButt;
GetAttachment( 2, vecMuzzle, vecAngle );
GetAttachment( 3, vecButt, vecAngle );
vecDir = vecMuzzle - vecButt;
VectorNormalize( vecDir );
// CPVSFilter filter( GetAbsOrigin() );
//te->ShowLine( filter, 0.0, vecSpot, vecSpot + vecForward );
//UTIL_Sparks( vecMuzzle );
bool fSound = false;
if( random->RandomInt( 0, 3 ) == 0 )
{
fSound = true;
}
Vector start = vecMuzzle + vecDir * 64;
Vector end = vecMuzzle + vecDir * 4096;
UTIL_Tracer( start, end, 0, TRACER_DONT_USE_ATTACHMENT, 5500, fSound );
CPASAttenuationFilter filter2( this, "GenericNPC.GunSound" );
EmitSound( filter2, entindex(), "GenericNPC.GunSound" );
}
//=========================================================
// HandleAnimEvent - catches the NPC-specific messages
// that occur when tagged animation frames are played.
//=========================================================
void CGenericNPC::HandleAnimEvent( animevent_t *pEvent )
{
switch( pEvent->event )
{
case 1:
// TEMPORARLY. Makes the May 2001 sniper demo work (sjb)
TempGunEffect();
break;
default:
BaseClass::HandleAnimEvent( pEvent );
break;
}
}
//=========================================================
// GetSoundInterests - generic NPC can't hear.
//=========================================================
int CGenericNPC::GetSoundInterests ( void )
{
return 0;
}
//=========================================================
// Spawn
//=========================================================
void CGenericNPC::Spawn()
{
Precache();
SetModel( STRING( GetModelName() ) );
/*
if ( FStrEq( STRING( GetModelName() ), "models/player.mdl" ) )
UTIL_SetSize(this, VEC_HUMAN_HULL_MIN, VEC_HUMAN_HULL_MAX);
else
UTIL_SetSize(this, VEC_HULL_MIN, VEC_HULL_MAX);
*/
if ( FStrEq( STRING( GetModelName() ), "models/player.mdl" ) || FStrEq( STRING( GetModelName() ), "models/holo.mdl" ) )
UTIL_SetSize(this, VEC_HULL_MIN, VEC_HULL_MAX);
else
UTIL_SetSize(this, NAI_Hull::Mins(HULL_HUMAN), NAI_Hull::Maxs(HULL_HUMAN));
SetSolid( SOLID_BBOX );
AddSolidFlags( FSOLID_NOT_STANDABLE );
SetMoveType( MOVETYPE_STEP );
m_bloodColor = BLOOD_COLOR_RED;
m_iHealth = 8;
m_flFieldOfView = 0.5;// indicates the width of this NPC's forward view cone ( as a dotproduct result )
m_NPCState = NPC_STATE_NONE;
CapabilitiesAdd( bits_CAP_MOVE_GROUND | bits_CAP_OPEN_DOORS );
NPCInit();
if ( !HasSpawnFlags(SF_GENERICNPC_NOTSOLID) )
{
trace_t tr;
UTIL_TraceEntity( this, GetAbsOrigin(), GetAbsOrigin(), MASK_SOLID, &tr );
if ( tr.startsolid )
{
Msg("Placed npc_generic in solid!!! (%s)\n", STRING(GetModelName()) );
m_spawnflags |= SF_GENERICNPC_NOTSOLID;
}
}
if ( HasSpawnFlags(SF_GENERICNPC_NOTSOLID) )
{
AddSolidFlags( FSOLID_NOT_SOLID );
m_takedamage = DAMAGE_NO;
VPhysicsDestroyObject();
}
}
//-----------------------------------------------------------------------------
// Purpose: precaches all resources this NPC needs
//-----------------------------------------------------------------------------
void CGenericNPC::Precache()
{
BaseClass::Precache();
PrecacheModel( STRING( GetModelName() ) );
PrecacheScriptSound( "GenericNPC.GunSound" );
}
// a really large health is set to make sure these never die.
const int TOO_MUCH_HEALTH_TO_DIE = 1000;
//=======================================================================================
// Furniture: A dumb "NPC" that is uses in scripted sequences
// where an NPC needs to be frame locked with a prop.
//=======================================================================================
class CNPC_Furniture : public CAI_BaseActor
{
DECLARE_CLASS( CNPC_Furniture, CAI_BaseActor );
DECLARE_DATADESC();
public:
void Spawn( void );
void Precache( void );
void Die( void );
void UpdateEfficiency( bool bInPVS ) { SetEfficiency( ( GetSleepState() != AISS_AWAKE ) ? AIE_DORMANT : AIE_NORMAL ); SetMoveEfficiency( AIME_NORMAL ); }
Class_T Classify ( void );
float MaxYawSpeed( void ){ return 0; }
virtual int ObjectCaps( void );
bool CreateVPhysics( void );
void NPCThink( void );
void UpdateOnRemove( void );
int SelectSchedule( void );
void OnRestore( void );
int OnTakeDamage( const CTakeDamageInfo &info )
{
if ( m_iHealth <= info.GetDamage() )
m_iHealth = info.GetDamage() + TOO_MUCH_HEALTH_TO_DIE;
return BaseClass::OnTakeDamage(info);
}
void DrawDebugGeometryOverlays(void);
void SetPlayerAvoidState( void );
void InputDisablePlayerCollision( inputdata_t &inputdata );
void InputEnablePlayerCollision( inputdata_t &inputdata );
void UpdateBoneFollowerState( void );
private:
// Contained Bone Follower manager
CBoneFollowerManager m_BoneFollowerManager;
};
LINK_ENTITY_TO_CLASS( monster_furniture, CNPC_Furniture );
LINK_ENTITY_TO_CLASS( npc_furniture, CNPC_Furniture );
//-----------------------------------------------------------------------------
// Save/load
//-----------------------------------------------------------------------------
BEGIN_DATADESC( CNPC_Furniture )
DEFINE_EMBEDDED( m_BoneFollowerManager ),
DEFINE_INPUTFUNC( FIELD_VOID, "DisablePlayerCollision", InputDisablePlayerCollision ),
DEFINE_INPUTFUNC( FIELD_VOID, "EnablePlayerCollision", InputEnablePlayerCollision ),
END_DATADESC()
//-----------------------------------------------------------------------------
// Purpose: This used to have something to do with bees flying, but
// now it only initializes moving furniture in scripted sequences
//-----------------------------------------------------------------------------
void CNPC_Furniture::Spawn( )
{
Precache();
SetModel( STRING(GetModelName()) );
SetMoveType( MOVETYPE_STEP );
SetSolid( SOLID_BBOX );
// Our collision, if needed, will be done through bone followers
AddSolidFlags( FSOLID_NOT_SOLID );
SetBloodColor( DONT_BLEED );
m_iHealth = TOO_MUCH_HEALTH_TO_DIE; //wow
m_takedamage = DAMAGE_AIM;
SetSequence( 0 );
SetCycle( 0 );
SetNavType( NAV_FLY );
AddFlag( FL_FLY );
CapabilitiesAdd( bits_CAP_MOVE_FLY | bits_CAP_TURN_HEAD | bits_CAP_ANIMATEDFACE );
AddEFlags( EFL_NO_MEGAPHYSCANNON_RAGDOLL );
// pev->nextthink += 1.0;
// SetThink (WalkMonsterDelay);
ResetSequenceInfo( );
SetCycle( 0 );
NPCInit();
// Furniture needs to block LOS
SetBlocksLOS( true );
// Furniture just wastes CPU doing sensing code, since all they do is idle and play scripts
GetSenses()->AddSensingFlags( SENSING_FLAGS_DONT_LOOK | SENSING_FLAGS_DONT_LISTEN );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CNPC_Furniture::Precache( void )
{
PrecacheModel( STRING( GetModelName() ) );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int CNPC_Furniture::ObjectCaps( void )
{
// HL2 furniture transitions
#ifdef HL2_DLL
return CAI_BaseNPC::ObjectCaps();
#else
return (CAI_BaseNPC::ObjectCaps() & ~FCAP_ACROSS_TRANSITION);
#endif
}
//-----------------------------------------------------------------------------
// Purpose: Furniture is killed
//-----------------------------------------------------------------------------
void CNPC_Furniture::Die( void )
{
SetThink ( &CNPC_Furniture::SUB_Remove );
SetNextThink( gpGlobals->curtime );
}
//-----------------------------------------------------------------------------
// Purpose: ID's Furniture as neutral (noone will attack it)
//-----------------------------------------------------------------------------
Class_T CNPC_Furniture::Classify ( void )
{
return CLASS_NONE;
}
//------------------------------------------------------------------------------
// Purpose:
//------------------------------------------------------------------------------
bool CNPC_Furniture::CreateVPhysics( void )
{
#ifndef HL2_DLL
return false;
#endif
if ( !m_BoneFollowerManager.GetNumBoneFollowers() )
{
KeyValues *modelKeyValues = new KeyValues("");
if ( modelKeyValues->LoadFromBuffer( modelinfo->GetModelName( GetModel() ), modelinfo->GetModelKeyValueText( GetModel() ) ) )
{
// Do we have a bone follower section?
KeyValues *pkvBoneFollowers = modelKeyValues->FindKey("bone_followers");
if ( pkvBoneFollowers )
{
// Loop through the list and create the bone followers
KeyValues *pBone = pkvBoneFollowers->GetFirstSubKey();
while ( pBone )
{
// Add it to the list
const char *pBoneName = pBone->GetString();
m_BoneFollowerManager.AddBoneFollower( this, pBoneName );
pBone = pBone->GetNextKey();
}
}
}
modelKeyValues->deleteThis();
}
return true;
}
void CNPC_Furniture::InputDisablePlayerCollision( inputdata_t &inputdata )
{
SetCollisionGroup( COLLISION_GROUP_NPC_ACTOR );
UpdateBoneFollowerState();
}
void CNPC_Furniture::InputEnablePlayerCollision( inputdata_t &inputdata )
{
SetCollisionGroup( COLLISION_GROUP_NPC );
UpdateBoneFollowerState();
}
void CNPC_Furniture::UpdateBoneFollowerState( void )
{
if ( m_BoneFollowerManager.GetNumBoneFollowers() )
{
physfollower_t* pBone = m_BoneFollowerManager.GetBoneFollower( 0 );
if ( pBone && pBone->hFollower && pBone->hFollower->GetCollisionGroup() != GetCollisionGroup() )
{
for ( int i = 0; i < m_BoneFollowerManager.GetNumBoneFollowers(); i++ )
{
pBone = m_BoneFollowerManager.GetBoneFollower( i );
if ( pBone && pBone->hFollower )
{
pBone->hFollower->SetCollisionGroup( GetCollisionGroup() );
}
}
}
}
}
void CNPC_Furniture::SetPlayerAvoidState( void )
{
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CNPC_Furniture::NPCThink( void )
{
BaseClass::NPCThink();
// Update follower bones
m_BoneFollowerManager.UpdateBoneFollowers(this);
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CNPC_Furniture::UpdateOnRemove( void )
{
m_BoneFollowerManager.DestroyBoneFollowers();
BaseClass::UpdateOnRemove();
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : int
//-----------------------------------------------------------------------------
int CNPC_Furniture::SelectSchedule( void )
{
switch( m_NPCState )
{
case NPC_STATE_NONE:
case NPC_STATE_PRONE:
case NPC_STATE_IDLE:
case NPC_STATE_ALERT:
case NPC_STATE_COMBAT:
case NPC_STATE_DEAD:
return SCHED_WAIT_FOR_SCRIPT;
case NPC_STATE_SCRIPT:
return BaseClass::SelectSchedule();
default:
DevWarning( 2, "Invalid State for SelectSchedule!\n" );
break;
}
return SCHED_FAIL;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CNPC_Furniture::OnRestore( void )
{
// Recreate any bone followers we have
CreateVPhysics();
BaseClass::OnRestore();
}
void CNPC_Furniture::DrawDebugGeometryOverlays( void )
{
//ugh
if ( m_debugOverlays & OVERLAY_NPC_ZAP_BIT )
{
m_debugOverlays &= ~OVERLAY_NPC_ZAP_BIT;
}
BaseClass::DrawDebugGeometryOverlays();
}