aboutsummaryrefslogtreecommitdiff
path: root/Northstar.CustomServers/mod/scripts/vscripts/ai/_ai_stalker.gnut
blob: f49560e0295a5c2abad3bd7faa9caf5d0311a6a3 (plain)
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
global function AiStalker_Init
global function GetDeathForce
global function StalkerGearOverloads
global function StalkerMeltingDown

global function IsStalkerLimbBlownOff

const float STALKER_DAMAGE_REQUIRED_TO_HEADSHOT = 0.3
//
// Base npc script shared between all npc types (regular, suicide, etc.)
//

const STALKER_REACTOR_CRITIMPACT_SOUND_1P_VS_3P = "ai_stalker_bulletimpact_nukecrit_1p_vs_3p"
const STALKER_REACTOR_CRITIMPACT_SOUND_3P_VS_3P = "ai_stalker_bulletimpact_nukecrit_3p_vs_3p"
const STALKER_REACTOR_CRITICAL_SOUND = "ai_stalker_nukedestruct_warmup_3p"
const STALKER_REACTOR_CRITICAL_FX = $"P_spectre_suicide_warn"

void function AiStalker_Init()
{
	PrecacheImpactEffectTable( "exp_stalker_powersupply" )
	PrecacheImpactEffectTable( "exp_small_stalker_powersupply" )
	PrecacheParticleSystem( STALKER_REACTOR_CRITICAL_FX )
	AddDamageCallback( "npc_stalker", StalkerOnDamaged )
	AddDeathCallback( "npc_stalker", StalkerOnDeath )
	AddSpawnCallback( "npc_stalker", StalkerOnSpawned )
}

void function StalkerOnSpawned( entity npc )
{
	StalkerOnSpawned_Think( npc )
}

void function StalkerOnSpawned_Think( entity npc )
{
	npc.SetCanBeMeleeExecuted( false )

	for ( int hitGroup = 0; hitGroup < HITGROUP_COUNT; hitGroup++ )
	{
		npc.ai.stalkerHitgroupDamageAccumulated[ hitGroup ] <- 0
		npc.ai.stalkerHitgroupLastHitTime[ hitGroup ] <- 0
	}

	if ( npc.Dev_GetAISettingByKeyField( "ScriptSpawnAsCrawler" ) == 1 )
	{
		EnableStalkerCrawlingBehavior( npc )
		PlayCrawlingAnim( npc, "ACT_RUN" )
		npc.Anim_Stop() // start playing a crawl anim then cut it off so it doesnt loop
	}
}

void function StalkerOnDeath( entity npc, var damageInfo )
{
	thread StalkerOnDeath_Internal( npc, damageInfo )

	#if MP
	int sourceId = DamageInfo_GetDamageSourceIdentifier( damageInfo )
	if ( sourceId == eDamageSourceId.damagedef_titan_step )
	{
		Explosion_DamageDefSimple(
			damagedef_stalker_powersupply_explosion_large_at,
			npc.GetOrigin(),
			npc,
			npc,
			npc.GetOrigin()
		)
	}
	#endif

}

void function StalkerOnDeath_Internal( entity npc, var damageInfo )
{
	int customDamageFlags = DamageInfo_GetCustomDamageType( damageInfo )
	bool allowDismemberment = bool( customDamageFlags & DF_DISMEMBERMENT )
	if ( allowDismemberment )
	{
		int hitGroup = GetHitGroupFromDamageInfo( npc, damageInfo )
		if ( hitGroup >= HITGROUP_GENERIC )
		{
			entity attacker = DamageInfo_GetAttacker( damageInfo )
			TryDismemberStalker( npc, damageInfo, attacker, hitGroup )
		}
	}

	if ( IsCrawling( npc ) )
	{
		WaitFrame() // or head won't disappear
		if ( IsValid( npc ) )
			npc.BecomeRagdoll( Vector( 0, 0, 0 ), false )
		return
	}
}


// All damage to stalkers comes here for modification and then either branches out to other npc types (Suicide, etc) for custom stuff or it just continues like normal.
void function StalkerOnDamaged( entity npc, var damageInfo )
{
	StalkerOnDamaged_Internal( npc, damageInfo )
}

void function StalkerOnDamaged_Internal( entity npc, var damageInfo )
{
	if ( !IsAlive( npc ) )
		return

	if ( StalkerMeltingDown( npc ) )
	{
		DamageInfo_ScaleDamage( damageInfo, 0.0 )
		return
	}

	// can't shoot, don't blow off limbs
	if ( IsCrawling( npc ) )
	{
		if ( Time() - npc.ai.startCrawlingTime < 0.75 )
		{
			DamageInfo_SetDamage( damageInfo, 0 )
			return
		}
	}

	int hitGroup = GetHitGroupFromDamageInfo( npc, damageInfo )
	if ( hitGroup < HITGROUP_GENERIC )
		hitGroup = HITGROUP_GENERIC

	float damage = DamageInfo_GetDamage( damageInfo )

	// limb dead yet?
	npc.ai.stalkerHitgroupDamageAccumulated[ hitGroup ] += int( damage )
	npc.ai.stalkerHitgroupLastHitTime[ hitGroup ] = Time()

	entity attacker = DamageInfo_GetAttacker( damageInfo )

	if ( PlayerHitGear( npc, damageInfo, hitGroup ) )
	{
		// don't die from damage
		float damage = DamageInfo_GetDamage( damageInfo )
		damage = npc.GetHealth() - 1.0
		DamageInfo_SetDamage( damageInfo, damage )

		thread StalkerGearOverloads( npc, attacker )
		return
	}

	int customDamageFlags = DamageInfo_GetCustomDamageType( damageInfo )
	bool allowDismemberment = bool( customDamageFlags & DF_DISMEMBERMENT )
	if ( !allowDismemberment )
		return

	bool canBeStaggered = TryDismemberStalker( npc, damageInfo, attacker, hitGroup )

	if ( canBeStaggered && !IsCrawling( npc ) && !npc.ai.transitioningToCrawl )
	{
		if ( npc.GetHealth().tofloat() / npc.GetMaxHealth().tofloat() <= 0.5 )
		{
			thread AttemptStandToStaggerAnimation( npc )
			npc.SetActivityModifier( ACT_MODIFIER_STAGGER, true )
		}
	}
}

bool function TryDismemberStalker( entity npc, var damageInfo, entity attacker, int hitGroup )
{
	string fpSound
	string tpSound

	switch ( hitGroup )
	{
		case HITGROUP_CHEST:
		case HITGROUP_STOMACH:
			fpSound = "AndroidArmored.BulletImpact_1P_vs_3P"
			tpSound = "AndroidArmored.BulletImpact_3P_vs_3P"
			break

		default:
			fpSound = "AndroidVulnerable.BulletImpact_1P_vs_3P"
			tpSound = "AndroidVulnerable.BulletImpact_3P_vs_3P"
			break
	}

	if ( IsAlive( attacker ) && attacker.IsPlayer() )
	{
		EmitSoundOnEntityOnlyToPlayer( npc, attacker, fpSound )
		EmitSoundOnEntityExceptToPlayer( npc, attacker, tpSound )
	}
	else
	{
		EmitSoundOnEntity( npc, tpSound )
	}

	bool justAFleshWound = true

	switch ( hitGroup )
	{
		case HITGROUP_HEAD:
			thread StalkerHeadShot( npc, damageInfo, hitGroup )
			justAFleshWound = false
			break

		case HITGROUP_LEFTARM:
			if ( StalkerLimbBlownOff( npc, damageInfo, hitGroup, 0.085, "left_arm", [ "left_arm", "l_hand" ], "Spectre.Arm.Explode" ) )
			{
				npc.SetActivityModifier( ACT_MODIFIER_ONEHANDED, true )

				// Some of his synced melees depend on using his left arm
				npc.SetCapabilityFlag( bits_CAP_SYNCED_MELEE_ATTACK, false )
			}
			break

		case HITGROUP_LEFTLEG:
			justAFleshWound = TryLegBlownOff( npc, damageInfo, hitGroup, 0.17, "left_leg", [ "left_leg", "foot_L_sole" ], "Spectre.Leg.Explode" )
			break

		case HITGROUP_RIGHTLEG:
			justAFleshWound = TryLegBlownOff( npc, damageInfo, hitGroup, 0.17, "right_leg", [ "right_leg", "foot_R_sole" ], "Spectre.Leg.Explode" )
			break
	}

	return justAFleshWound
}

bool function PlayerHitGear( entity npc, var damageInfo, int hitGroup )
{
	entity attacker = DamageInfo_GetAttacker( damageInfo )

	if ( !attacker.IsPlayer() )
		return false

	if ( hitGroup != HITGROUP_GEAR )
		return false

	if ( !( DamageInfo_GetCustomDamageType( damageInfo ) & DF_BULLET ) )
		return false

	return true
}

int function GetHitGroupFromDamageInfo( entity npc, var damageInfo )
{
	int hitGroup = DamageInfo_GetHitGroup( damageInfo )

	if ( hitGroup <= HITGROUP_GENERIC )
	{
		int hitBox = DamageInfo_GetHitBox( damageInfo )
		if ( hitBox >= 0 )
			return GetHitgroupForHitboxOnEntity( npc, hitBox )
	}

	return hitGroup
}

bool function StalkerMeltingDown( entity npc )
{
	int bodyGroup = npc.FindBodyGroup( "gear" )
	Assert( bodyGroup != -1 )

	// gear already blown up?
	return npc.GetBodyGroupState( bodyGroup ) != 0
}

void function StalkerGearOverloads( entity npc, entity attacker = null )
{
	Assert( !StalkerMeltingDown( npc ) )

	if ( !IsCrawling( npc ) && StalkerCanCrawl( npc ) )
		thread FallAndBecomeCrawlingStalker( npc )

	int bodyGroup = npc.FindBodyGroup( "gear" )

	// hide gear
	npc.SetBodygroup( bodyGroup, 1 )

	string attachment = "CHESTFOCUS"

	npc.EndSignal( "OnDestroy" )
	npc.EndSignal( "OnDeath" )

	entity nukeFXInfoTarget = CreateEntity( "info_target" )
	nukeFXInfoTarget.kv.spawnflags = SF_INFOTARGET_ALWAYS_TRANSMIT_TO_CLIENT
	DispatchSpawn( nukeFXInfoTarget )

	nukeFXInfoTarget.SetParent( npc, attachment )

	if ( attacker != null )
	{
		EmitSoundOnEntityOnlyToPlayer( nukeFXInfoTarget, attacker, STALKER_REACTOR_CRITIMPACT_SOUND_1P_VS_3P )
		EmitSoundOnEntityExceptToPlayer( nukeFXInfoTarget, attacker, STALKER_REACTOR_CRITIMPACT_SOUND_3P_VS_3P )
	}
	else
	{
		EmitSoundOnEntity( nukeFXInfoTarget, STALKER_REACTOR_CRITIMPACT_SOUND_3P_VS_3P )
	}

	EmitSoundOnEntity( nukeFXInfoTarget, STALKER_REACTOR_CRITICAL_SOUND )

	AI_CreateDangerousArea_DamageDef( damagedef_stalker_powersupply_explosion_small, nukeFXInfoTarget, TEAM_INVALID, true, false )

	entity fx = PlayFXOnEntity( STALKER_REACTOR_CRITICAL_FX, nukeFXInfoTarget )

	OnThreadEnd(
	function() : ( nukeFXInfoTarget, fx, npc, attacker )
		{
			if ( IsValid( npc ) )
				StopSoundOnEntity( nukeFXInfoTarget, STALKER_REACTOR_CRITICAL_SOUND )

			if ( IsValid( nukeFXInfoTarget ) )
				nukeFXInfoTarget.Destroy()

			if ( IsValid( fx ) )
				fx.Destroy()

			if ( IsAlive( npc ) )
			{
				entity damageAttacker
				if ( IsValid( attacker ) )
					damageAttacker = attacker
				else
					damageAttacker = npc

				vector force = GetDeathForce()
				npc.Die( damageAttacker, npc, { force = force, scriptType = DF_GIB, damageSourceId = eDamageSourceId.suicideSpectreAoE } )
			}
		}
	)

	wait 1.0

	float duration = 2.1
	float endTime = Time() + duration
	float startTime = Time()

	int tagID = npc.LookupAttachment( "CHESTFOCUS" )

	for ( ;; )
	{
		float timePassed = Time() - startTime
		float explodeMin = Graph( timePassed, 0, duration, 0.4, 0.1 )
		float explodeMax = explodeMin + Graph( timePassed, 0, duration, 0.21, 0.1 )
		wait RandomFloatRange( explodeMin, explodeMax )

		entity damageAttacker = GetNPCAttackerEnt( npc, attacker )

		// origin = npc.GetWorldSpaceCenter()
		vector origin = npc.GetAttachmentOrigin( tagID )

		if ( Time() >= endTime )
		{
			Explosion_DamageDefSimple( damagedef_stalker_powersupply_explosion_large, origin, damageAttacker, npc, origin )
			break
		}
		else
		{
			Explosion_DamageDefSimple( damagedef_stalker_powersupply_explosion_small, origin, damageAttacker, npc, origin )
		}
	}
}

bool function StalkerCanCrawl( entity npc )
{
	if ( !IsAlive( npc ) )
		return false

	if ( npc.Anim_IsActive() )
		return false

	return true
}

bool function TryLegBlownOff( entity npc, var damageInfo, int hitGroup, float limbHealthPercentOfMax, string leg, array<string> fxTags, string sound )
{
	if ( IsCrawling( npc ) )
	{
		// can blow off leg if stalker is already crawling
		StalkerLimbBlownOff( npc, damageInfo, hitGroup, limbHealthPercentOfMax, leg, fxTags, sound )
		return true
	}

	if ( !StalkerCanCrawl( npc ) )
		return true

	if ( StalkerLimbBlownOff( npc, damageInfo, hitGroup, limbHealthPercentOfMax, leg, fxTags, sound ) )
	{
		thread FallAndBecomeCrawlingStalker( npc )
		return false
	}

	return true
}

void function EnableStalkerCrawlingBehavior( entity npc )
{
	Assert( StalkerCanCrawl( npc ) )
	Assert( !IsCrawling( npc ) )

	DisableLeeching( npc )

	DisableMinionUsesHeavyWeapons( npc )

	string crawlingSettings = string ( npc.Dev_GetAISettingByKeyField( "crawlingSettingsWrapper" ) )

	// Changing the setting file includes changing the behavior file to	"behavior_stalker_crawling"
	SetAISettingsWrapper( npc, crawlingSettings )

	npc.ai.crawling = true
	npc.ai.startCrawlingTime = Time()
	npc.DisableGrappleAttachment()
	npc.EnableNPCMoveFlag( NPCMF_DISABLE_ARRIVALS )
	npc.SetCapabilityFlag( bits_CAP_MOVE_TRAVERSE | bits_CAP_MOVE_SHOOT | bits_CAP_WEAPON_RANGE_ATTACK1 | bits_CAP_AIM_GUN, false )
	npc.SetActivityModifier( ACT_MODIFIER_CRAWL, true )
	npc.SetActivityModifier( ACT_MODIFIER_STAGGER, false )
	npc.SetCanBeGroundExecuted( true )
	npc.ClearMoveAnim()

	npc.SetHealth( npc.GetMaxHealth() * 0.5 )

	npc.SetAimAssistForcePullPitchEnabled( true )

	thread SelfTerminateAfterDelay( npc )
}

void function SelfTerminateAfterDelay( entity npc )
{
	const float lifeSupportDuration = 8
	float deathTime = Time() + (lifeSupportDuration * 2)

	npc.EndSignal( "OnDeath" )
	for ( ;; )
	{
		entity enemy = npc.GetEnemy()
		if ( IsAlive( enemy ) )
		{
			if ( Distance( npc.GetEnemyLKP(), npc.GetOrigin() ) < 500 )
			{
				if ( npc.TimeSinceSeen( enemy ) < 3 )
					deathTime = max( Time() + lifeSupportDuration, deathTime )
			}
		}

		if ( Time() > deathTime )
		{
			npc.Die()
			return
		}

		wait 1.0
	}
}

void function FallAndBecomeCrawlingStalker( entity npc )
{
	// finish what he's doing
	npc.EndSignal( "OnDeath" )

	npc.ai.transitioningToCrawl = true

	// Workaround for Bug 114372
	WaitFrame()

	for ( ;; )
	{
		if ( npc.IsInterruptable() )
			break
		WaitFrame()
	}

	if ( !StalkerCanCrawl( npc ) )
		return

	if ( IsCrawling( npc ) )
		return

	EnableStalkerCrawlingBehavior( npc )

	npc.Anim_Stop() // stop leeching, etc.

	PlayCrawlingAnim( npc, "ACT_STAND_TO_CRAWL" )
}

void function PlayCrawlingAnim( entity npc, string animation )
{
	npc.Anim_ScriptedPlayActivityByName( animation, true, 0.1 )
	npc.UseSequenceBounds( true )
}

void function AttemptStandToStaggerAnimation( entity npc )
{
	// Check if we are already staggered
	if ( npc.IsActivityModifierActive( ACT_MODIFIER_STAGGER ) )
		return

	if ( !npc.IsInterruptable() )
		return

	if ( npc.ContextAction_IsBusy() )
		return

	// Are we blocking additional pain animations
	if ( npc.GetNPCFlag( NPC_NO_PAIN ) )
		return

	// finish what he's doing
	npc.EndSignal( "OnDeath" )

	// Workaround for Bug 114372
	WaitFrame()

	for ( ;; )
	{
		if ( npc.IsInterruptable() )
			break

		WaitFrame()
	}

	if ( IsCrawling( npc ) || npc.ai.transitioningToCrawl )
		return

	npc.Anim_ScriptedPlayActivityByName( "ACT_STAND_TO_STAGGER", true, 0.1 )
	npc.UseSequenceBounds( true )
	npc.EnableNPCFlag( NPC_PAIN_IN_SCRIPTED_ANIM )
}

bool function IsStalkerLimbBlownOff( entity npc, string limbName )
{
	int bodyGroup = npc.FindBodyGroup( limbName )
	if ( npc.GetBodyGroupState( bodyGroup ) != 0 )
		return true

	return false
}

bool function StalkerLimbBlownOff( entity npc, var damageInfo, int hitGroup, float limbHealthPercentOfMax, string limbName, array<string> fxTags, string sound )
{
	int damageSourceId = DamageInfo_GetDamageSourceIdentifier( damageInfo )
	switch ( damageSourceId )
	{
		case eDamageSourceId.mp_weapon_grenade_emp:
		case eDamageSourceId.mp_weapon_proximity_mine:
			return false
	}

	int bodyGroup = npc.FindBodyGroup( limbName )
	if ( bodyGroup == -1 )
		return false

	if ( IsStalkerLimbBlownOff( npc, limbName ) )
		return false

	EmitSoundOnEntity( npc, sound )

	// blow off limb
	npc.SetBodygroup( bodyGroup, 1 )

	return true
}

void function StalkerHeadShot( entity npc, var damageInfo, int hitGroup )
{
	// random chance to blow up head
//	if ( DamageInfo_GetDamage( damageInfo ) < 100 && RandomFloat( 100 ) <= 66 )
//		return

	if ( !IsValidHeadShot( damageInfo, npc ) )
		return

	// only players score headshots
	entity attacker = DamageInfo_GetAttacker( damageInfo )
	if ( !IsAlive( attacker ) )
		return
	if ( !attacker.IsPlayer() )
		return

	if ( DamageInfo_GetDamage( damageInfo ) < npc.GetHealth() )
	{
		// force lethal if we have done more than this much damage
		if ( npc.ai.stalkerHitgroupDamageAccumulated[ hitGroup ] < npc.GetMaxHealth() * STALKER_DAMAGE_REQUIRED_TO_HEADSHOT )
			return
	}

	npc.Anim_Stop() // stop leeching, etc.
	npc.ClearParent()

	//DisableLeeching( npc )

	// No pain anims
	//DamageInfo_AddDamageFlags( damageInfo, DAMAGEFLAG_NOPAIN )

	// Set these so cl_player knows to kill the eye glow and play the right SFX
	DamageInfo_AddCustomDamageType( damageInfo, DF_HEADSHOT )
	DamageInfo_AddCustomDamageType( damageInfo, DF_KILLSHOT )

	EmitSoundOnEntityExceptToPlayer( npc, attacker, "SuicideSpectre.BulletImpact_HeadShot_3P_vs_3P" )

	int bodyGroupIndex = npc.FindBodyGroup( "removableHead" )
	int stateIndex = 1  // 0 = show, 1 = hide
	npc.SetBodygroup( bodyGroupIndex, stateIndex )

	DamageInfo_SetDamage( damageInfo, npc.GetMaxHealth() )
}

vector function GetDeathForce()
{
	vector angles = <RandomFloatRange(-45,-75),RandomFloat(360),0>
	vector forward = AnglesToForward( angles )
	return forward * RandomFloatRange( 0.25, 0.75 )
}