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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: barnacle - stationary ceiling mounted 'fishing' monster
//
// $Workfile: $
// $Date: $
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "hl1_npc_barnacle.h"
#include "npcevent.h"
#include "gib.h"
#include "ai_default.h"
#include "activitylist.h"
#include "hl2_player.h"
#include "vstdlib/random.h"
#include "physics_saverestore.h"
#include "vcollide_parse.h"
#include "engine/IEngineSound.h"
ConVar sk_barnacle_health( "sk_barnacle_health","25");
//-----------------------------------------------------------------------------
// Private activities.
//-----------------------------------------------------------------------------
static int ACT_EAT = 0;
//-----------------------------------------------------------------------------
// Interactions
//-----------------------------------------------------------------------------
int g_interactionBarnacleVictimDangle = 0;
int g_interactionBarnacleVictimReleased = 0;
int g_interactionBarnacleVictimGrab = 0;
LINK_ENTITY_TO_CLASS( monster_barnacle, CNPC_Barnacle );
IMPLEMENT_CUSTOM_AI( monster_barnacle, CNPC_Barnacle );
//-----------------------------------------------------------------------------
// Purpose: Initialize the custom schedules
// Input :
// Output :
//-----------------------------------------------------------------------------
void CNPC_Barnacle::InitCustomSchedules(void)
{
INIT_CUSTOM_AI(CNPC_Barnacle);
ADD_CUSTOM_ACTIVITY(CNPC_Barnacle, ACT_EAT);
g_interactionBarnacleVictimDangle = CBaseCombatCharacter::GetInteractionID();
g_interactionBarnacleVictimReleased = CBaseCombatCharacter::GetInteractionID();
g_interactionBarnacleVictimGrab = CBaseCombatCharacter::GetInteractionID();
}
BEGIN_DATADESC( CNPC_Barnacle )
DEFINE_FIELD( m_flAltitude, FIELD_FLOAT ),
DEFINE_FIELD( m_flKillVictimTime, FIELD_TIME ),
DEFINE_FIELD( m_cGibs, FIELD_INTEGER ),// barnacle loads up on gibs each time it kills something.
DEFINE_FIELD( m_fLiftingPrey, FIELD_BOOLEAN ),
DEFINE_FIELD( m_flTongueAdj, FIELD_FLOAT ),
DEFINE_FIELD( m_flIgnoreTouchesUntil, FIELD_TIME ),
// Function pointers
DEFINE_THINKFUNC( BarnacleThink ),
DEFINE_THINKFUNC( WaitTillDead ),
END_DATADESC()
//=========================================================
// Classify - indicates this monster's place in the
// relationship table.
//=========================================================
Class_T CNPC_Barnacle::Classify ( void )
{
return CLASS_ALIEN_MONSTER;
}
//=========================================================
// HandleAnimEvent - catches the monster-specific messages
// that occur when tagged animation frames are played.
//
// Returns number of events handled, 0 if none.
//=========================================================
void CNPC_Barnacle::HandleAnimEvent( animevent_t *pEvent )
{
switch( pEvent->event )
{
case BARNACLE_AE_PUKEGIB:
CGib::SpawnRandomGibs( this, 1, GIB_HUMAN );
break;
default:
BaseClass::HandleAnimEvent( pEvent );
break;
}
}
//=========================================================
// Spawn
//=========================================================
void CNPC_Barnacle::Spawn()
{
Precache( );
SetModel( "models/barnacle.mdl" );
UTIL_SetSize( this, Vector(-16, -16, -32), Vector(16, 16, 0) );
SetSolid( SOLID_BBOX );
AddSolidFlags( FSOLID_NOT_STANDABLE );
SetMoveType( MOVETYPE_NONE );
SetBloodColor( BLOOD_COLOR_GREEN );
m_iHealth = sk_barnacle_health.GetFloat();
m_flFieldOfView = 0.5;// indicates the width of this monster's forward view cone ( as a dotproduct result )
m_NPCState = NPC_STATE_NONE;
m_flKillVictimTime = 0;
m_cGibs = 0;
m_fLiftingPrey = FALSE;
m_takedamage = DAMAGE_YES;
InitBoneControllers();
InitTonguePosition();
// set eye position
SetDefaultEyeOffset();
SetActivity ( ACT_IDLE );
SetThink ( &CNPC_Barnacle::BarnacleThink );
SetNextThink( gpGlobals->curtime + 0.5f );
//Do not have a shadow
AddEffects( EF_NOSHADOW );
m_flIgnoreTouchesUntil = gpGlobals->curtime;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input :
// Output :
//-----------------------------------------------------------------------------
int CNPC_Barnacle::OnTakeDamage_Alive( const CTakeDamageInfo &inputInfo )
{
CTakeDamageInfo info = inputInfo;
if ( info.GetDamageType() & DMG_CLUB )
{
info.SetDamage( m_iHealth );
}
return BaseClass::OnTakeDamage_Alive( info );
}
//-----------------------------------------------------------------------------
// Purpose: Initialize tongue position when first spawned
// Input :
// Output :
//-----------------------------------------------------------------------------
void CNPC_Barnacle::InitTonguePosition( void )
{
CBaseEntity *pTouchEnt;
float flLength;
pTouchEnt = TongueTouchEnt( &flLength );
m_flAltitude = flLength;
Vector origin;
QAngle angle;
GetAttachment( "TongueEnd", origin, angle );
m_flTongueAdj = origin.z - GetAbsOrigin().z;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CNPC_Barnacle::BarnacleThink ( void )
{
CBaseEntity *pTouchEnt;
float flLength;
SetNextThink( gpGlobals->curtime + 0.1f );
if (CAI_BaseNPC::m_nDebugBits & bits_debugDisableAI)
{
// AI Disabled, don't do anything
}
else if ( GetEnemy() != NULL )
{
// barnacle has prey.
if ( !GetEnemy()->IsAlive() )
{
// someone (maybe even the barnacle) killed the prey. Reset barnacle.
m_fLiftingPrey = FALSE;// indicate that we're not lifting prey.
SetEnemy( NULL );
return;
}
CBaseCombatCharacter* pVictim = GetEnemyCombatCharacterPointer();
Assert( pVictim );
if ( m_fLiftingPrey )
{
if ( GetEnemy() != NULL && pVictim->m_lifeState == LIFE_DEAD )
{
// crap, someone killed the prey on the way up.
SetEnemy( NULL );
m_fLiftingPrey = FALSE;
return;
}
// still pulling prey.
Vector vecNewEnemyOrigin = GetEnemy()->GetLocalOrigin();
vecNewEnemyOrigin.x = GetLocalOrigin().x;
vecNewEnemyOrigin.y = GetLocalOrigin().y;
// guess as to where their neck is
// FIXME: remove, ask victim where their neck is
vecNewEnemyOrigin.x -= 6 * cos(GetEnemy()->GetLocalAngles().y * M_PI/180.0);
vecNewEnemyOrigin.y -= 6 * sin(GetEnemy()->GetLocalAngles().y * M_PI/180.0);
m_flAltitude -= BARNACLE_PULL_SPEED;
vecNewEnemyOrigin.z += BARNACLE_PULL_SPEED;
if ( fabs( GetLocalOrigin().z - ( vecNewEnemyOrigin.z + GetEnemy()->GetViewOffset().z ) ) < BARNACLE_BODY_HEIGHT )
{
// prey has just been lifted into position ( if the victim origin + eye height + 8 is higher than the bottom of the barnacle, it is assumed that the head is within barnacle's body )
m_fLiftingPrey = FALSE;
CPASAttenuationFilter filter( this );
EmitSound( filter, entindex(), "Barnacle.Bite");
// Take a while to kill the player
m_flKillVictimTime = gpGlobals->curtime + 10;
if ( pVictim )
{
pVictim->DispatchInteraction( g_interactionBarnacleVictimDangle, NULL, this );
SetActivity ( (Activity)ACT_EAT );
}
}
CBaseEntity *pEnemy = GetEnemy();
trace_t trace;
UTIL_TraceEntity( pEnemy, pEnemy->GetAbsOrigin(), vecNewEnemyOrigin, MASK_SOLID_BRUSHONLY, pEnemy, COLLISION_GROUP_NONE, &trace );
if( trace.fraction != 1.0 )
{
// The victim cannot be moved from their current origin to this new origin. So drop them.
SetEnemy( NULL );
m_fLiftingPrey = FALSE;
if( pEnemy->MyCombatCharacterPointer() )
{
pEnemy->MyCombatCharacterPointer()->DispatchInteraction( g_interactionBarnacleVictimReleased, NULL, this );
}
// Ignore touches long enough to let the victim move away.
m_flIgnoreTouchesUntil = gpGlobals->curtime + 1.5;
SetActivity( ACT_IDLE );
return;
}
UTIL_SetOrigin ( GetEnemy(), vecNewEnemyOrigin );
}
else
{
// prey is lifted fully into feeding position and is dangling there.
if ( m_flKillVictimTime != -1 && gpGlobals->curtime > m_flKillVictimTime )
{
// kill!
if ( pVictim )
{
// DMG_CRUSH added so no physics force is generated
pVictim->TakeDamage( CTakeDamageInfo( this, this, pVictim->m_iHealth, DMG_SLASH | DMG_ALWAYSGIB | DMG_CRUSH ) );
m_cGibs = 3;
}
return;
}
// bite prey every once in a while
if ( pVictim && ( random->RandomInt( 0, 49 ) == 0 ) )
{
CPASAttenuationFilter filter( this );
EmitSound( filter, entindex(), "Barnacle.Chew" );
if ( pVictim )
{
pVictim->DispatchInteraction( g_interactionBarnacleVictimDangle, NULL, this );
}
}
}
}
else
{
// barnacle has no prey right now, so just idle and check to see if anything is touching the tongue.
// If idle and no nearby client, don't think so often. Client should be out of PVS and not within 50 feet.
if ( !UTIL_FindClientInPVS(edict()) )
{
CBasePlayer *pPlayer = UTIL_PlayerByIndex(1);
if( pPlayer )
{
Vector vecDist = pPlayer->GetAbsOrigin() - GetAbsOrigin();
if( vecDist.Length2DSqr() >= Square(600.0f) )
{
SetNextThink( gpGlobals->curtime + 1.5f );
}
}
}
if ( IsActivityFinished() )
{// this is done so barnacle will fidget.
SetActivity ( ACT_IDLE );
}
if ( m_cGibs && random->RandomInt(0,99) == 1 )
{
// cough up a gib.
CGib::SpawnRandomGibs( this, 1, GIB_HUMAN );
m_cGibs--;
CPASAttenuationFilter filter( this );
EmitSound( filter, entindex(), "Barnacle.Chew" );
}
pTouchEnt = TongueTouchEnt( &flLength );
//NDebugOverlay::Box( GetAbsOrigin() - Vector( 0, 0, flLength ), Vector( -2, -2, -2 ), Vector( 2, 2, 2 ), 255,0,0, 0, 0.1 );
if ( pTouchEnt != NULL )
{
// tongue is fully extended, and is touching someone.
CBaseCombatCharacter* pBCC = (CBaseCombatCharacter *)pTouchEnt;
// FIXME: humans should return neck position
Vector vecGrabPos = pTouchEnt->GetAbsOrigin();
if ( pBCC && pBCC->DispatchInteraction( g_interactionBarnacleVictimGrab, &vecGrabPos, this ) )
{
CPASAttenuationFilter filter( this );
EmitSound( filter, entindex(), "Barnacle.Alert" );
SetSequenceByName ( "attack1" );
SetEnemy( pTouchEnt );
pTouchEnt->SetMoveType( MOVETYPE_FLY );
pTouchEnt->SetAbsVelocity( vec3_origin );
pTouchEnt->SetBaseVelocity( vec3_origin );
Vector origin = GetAbsOrigin();
origin.z = pTouchEnt->GetAbsOrigin().z;
pTouchEnt->SetLocalOrigin( origin );
m_fLiftingPrey = TRUE;// indicate that we should be lifting prey.
m_flKillVictimTime = -1;// set this to a bogus time while the victim is lifted.
m_flAltitude = (GetAbsOrigin().z - vecGrabPos.z);
}
}
else
{
// calculate a new length for the tongue to be clear of anything else that moves under it.
if ( m_flAltitude < flLength )
{
// if tongue is higher than is should be, lower it kind of slowly.
m_flAltitude += BARNACLE_PULL_SPEED;
}
else
{
m_flAltitude = flLength;
}
}
}
// ALERT( at_console, "tounge %f\n", m_flAltitude + m_flTongueAdj );
//NDebugOverlay::Box( GetAbsOrigin() - Vector( 0, 0, m_flAltitude ), Vector( -2, -2, -2 ), Vector( 2, 2, 2 ), 255,255,255, 0, 0.1 );
SetBoneController( 0, -(m_flAltitude + m_flTongueAdj) );
StudioFrameAdvance();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CNPC_Barnacle::Event_Killed( const CTakeDamageInfo &info )
{
AddSolidFlags( FSOLID_NOT_SOLID );
m_takedamage = DAMAGE_NO;
m_lifeState = LIFE_DEAD;
if ( GetEnemy() != NULL )
{
CBaseCombatCharacter *pVictim = GetEnemyCombatCharacterPointer();
if ( pVictim )
{
pVictim->DispatchInteraction( g_interactionBarnacleVictimReleased, NULL, this );
}
}
CGib::SpawnRandomGibs( this, 4, GIB_HUMAN );
CPASAttenuationFilter filter( this );
EmitSound( filter, entindex(), "Barnacle.Die" );
SetActivity ( ACT_DIESIMPLE );
SetBoneController( 0, 0 );
StudioFrameAdvance();
SetNextThink( gpGlobals->curtime + 0.1f );
SetThink ( &CNPC_Barnacle::WaitTillDead );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CNPC_Barnacle::WaitTillDead ( void )
{
SetNextThink( gpGlobals->curtime + 0.1f );
StudioFrameAdvance();
DispatchAnimEvents ( this );
if ( IsActivityFinished() )
{
// death anim finished.
StopAnimation();
SetThink ( NULL );
}
}
//=========================================================
// Precache - precaches all resources this monster needs
//=========================================================
void CNPC_Barnacle::Precache()
{
PrecacheModel("models/barnacle.mdl");
PrecacheScriptSound( "Barnacle.Bite" );
PrecacheScriptSound( "Barnacle.Chew" );
PrecacheScriptSound( "Barnacle.Alert" );
PrecacheScriptSound( "Barnacle.Die" );
BaseClass::Precache();
}
//=========================================================
// TongueTouchEnt - does a trace along the barnacle's tongue
// to see if any entity is touching it. Also stores the length
// of the trace in the int pointer provided.
//=========================================================
#define BARNACLE_CHECK_SPACING 8
CBaseEntity *CNPC_Barnacle::TongueTouchEnt ( float *pflLength )
{
trace_t tr;
float length;
// trace once to hit architecture and see if the tongue needs to change position.
UTIL_TraceLine ( GetAbsOrigin(), GetAbsOrigin() - Vector ( 0 , 0 , 2048 ),
MASK_SOLID_BRUSHONLY, this, COLLISION_GROUP_NONE, &tr );
length = fabs( GetAbsOrigin().z - tr.endpos.z );
// Pull it up a tad
length -= 16;
if ( pflLength )
{
*pflLength = length;
}
// Don't try to touch any prey.
if ( m_flIgnoreTouchesUntil > gpGlobals->curtime )
return NULL;
Vector delta = Vector( BARNACLE_CHECK_SPACING, BARNACLE_CHECK_SPACING, 0 );
Vector mins = GetAbsOrigin() - delta;
Vector maxs = GetAbsOrigin() + delta;
maxs.z = GetAbsOrigin().z;
// Take our current tongue's length or a point higher if we hit a wall
// NOTENOTE: (this relieves the need to know if the tongue is currently moving)
mins.z -= MIN( m_flAltitude, length );
CBaseEntity *pList[10];
int count = UTIL_EntitiesInBox( pList, 10, mins, maxs, (FL_CLIENT|FL_NPC) );
if ( count )
{
for ( int i = 0; i < count; i++ )
{
CBaseCombatCharacter *pVictim = ToBaseCombatCharacter( pList[ i ] );
bool bCanHurt = false;
if ( IRelationType( pList[i] ) == D_HT || IRelationType( pList[i] ) == D_FR )
bCanHurt = true;
if ( pList[i] != this && bCanHurt == true && pVictim->m_lifeState == LIFE_ALIVE )
{
return pList[i];
}
}
}
return NULL;
}
|