aboutsummaryrefslogtreecommitdiff
path: root/Northstar.CustomServers/scripts/vscripts/ai/_ai_pilots.gnut
blob: 3c2e36ce02f1de158016d2d2d5c90f9bef4eb666 (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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
untyped

global const NPC_TITAN_PILOT_PROTOTYPE = 0
global function AiPilots_Init

global function CaptainThink


#if NPC_TITAN_PILOT_PROTOTYPE
global function NpcPilotCallTitanThink
global function NpcPilotStopCallTitanThink
global function NpcPilotCallsInAndEmbarksTitan
global function NpcPilotRunsToAndEmbarksFallingTitan
global function NpcPilotCallsInTitan
global function NpcPilotRunsToEmbarkTitan
global function NpcPilotEmbarksTitan
global function NpcPilotDisembarksTitan
global function NpcPilotBecomesTitan
global function NpcTitanBecomesPilot
global function TitanHasNpcPilot
global function NpcPilotGetPetTitan
global function NpcPilotSetPetTitan
#endif

global function NpcSetNextTitanRespawnAvailable
global function NpcResetNextTitanRespawnAvailable

global function AddCallback_OnNpcTitanBecomesPilot
global function AddCallback_OnNpcPilotBecomesTitan

global struct NPCPilotStruct
{
	bool isValid = false

	int team
	int spawnflags
	float accuracy
	float proficieny
	float health
	float physDamageScale
	string weapon
	string squadName

	asset modelAsset
	string title

	bool isInvulnerable
}

const NPC_NEXT_TITANTIME_RESET = -1
const NPC_NEXT_TITANTIME_MIN = 45
const NPC_NEXT_TITANTIME_MAX = 60
const NPC_NEXT_TITANTIME_INTERUPT = 15

function AiPilots_Init()
{
	RegisterSignal( "grenade_throw" )
	RegisterSignal( "NpcPilotBecomesTitan" )
	RegisterSignal( "NpcTitanBecomesPilot" )
	RegisterSignal( "StopCallTitanThink" )
	RegisterSignal( "NpcTitanRespawnAvailableUpdated" )

	level.onNpcPilotBecomesTitanCallbacks	<- []
	level.onNpcTitanBecomesPilotCallbacks	<- []

}

function ScriptCallback_OnNpcPilotBecomesTitan( pilot, titan )
{
	local result = { pilot = pilot, titan = titan }
	Signal( pilot, "NpcPilotBecomesTitan", result )
	Signal( titan, "NpcPilotBecomesTitan", result )

	foreach ( callbackFunc in level.onNpcPilotBecomesTitanCallbacks )
	{
		callbackFunc( pilot, titan )
	}
}

function ScriptCallback_OnNpcTitanBecomesPilot( pilot, titan )
{
	local result = { pilot = pilot, titan = titan }
	Signal( pilot, "NpcTitanBecomesPilot", result )
	Signal( titan, "NpcTitanBecomesPilot", result )

	foreach ( callbackFunc in level.onNpcTitanBecomesPilotCallbacks )
	{
		callbackFunc( pilot, titan )
	}
}

function AddCallback_OnNpcPilotBecomesTitan( callbackFunc )
{
	Assert( "onNpcPilotBecomesTitanCallbacks" in level )
	AssertParameters( callbackFunc, 2, "pilotNPC, titanNPC" )

	level.onNpcPilotBecomesTitanCallbacks.append( callbackFunc )
}

function AddCallback_OnNpcTitanBecomesPilot( callbackFunc )
{
	Assert( "onNpcTitanBecomesPilotCallbacks" in level )
	AssertParameters( callbackFunc, 2, "pilotNPC, titanNPC" )

	level.onNpcTitanBecomesPilotCallbacks.append( callbackFunc )
}

function NpcSetNextTitanRespawnAvailable( npc, time )
{
	Assert( "nextTitanRespawnAvailable" in npc.s )
	npc.s.nextTitanRespawnAvailable = time
	npc.Signal( "NpcTitanRespawnAvailableUpdated" )
}

function NpcResetNextTitanRespawnAvailable( npc )
{
	Assert( "nextTitanRespawnAvailable" in npc.s )
	npc.s.nextTitanRespawnAvailable = NPC_NEXT_TITANTIME_RESET
	npc.Signal( "NpcTitanRespawnAvailableUpdated" )
}

function NpcPilotStopCallTitanThink( pilot )
{
	pilot.Signal( "StopCallTitanThink" )
}

/************************************************************************************************\

########  #### ##        #######  ########       ######## ##     ## #### ##    ## ##    ##
##     ##  ##  ##       ##     ##    ##             ##    ##     ##  ##  ###   ## ##   ##
##     ##  ##  ##       ##     ##    ##             ##    ##     ##  ##  ####  ## ##  ##
########   ##  ##       ##     ##    ##             ##    #########  ##  ## ## ## #####
##         ##  ##       ##     ##    ##             ##    ##     ##  ##  ##  #### ##  ##
##         ##  ##       ##     ##    ##             ##    ##     ##  ##  ##   ### ##   ##
##        #### ########  #######     ##             ##    ##     ## #### ##    ## ##    ##

\************************************************************************************************/
function CaptainThink( entity npc )
{
	npc.EndSignal( "OnDestroy" )
	npc.EndSignal( "OnDeath" )

	Assert( !( "nextTitanRespawnAvailable" in npc.s ) )
	Assert( !( "petTitan" in npc.s ) )

	npc.s.petTitan <- null
	npc.s.nextTitanRespawnAvailable <- null

	//wait for in combat...
	WaitForNpcInCombat( npc )

	//... before we call in a titan
	if ( npc.s.nextTitanRespawnAvailable == null )
		npc.s.nextTitanRespawnAvailable = Time() + RandomFloatRange( 2, 10 )

	WaitEndFrame() //wait a frame for things like petTitan and nextTitanRespawnAvailable to have a chance to be set from custom scripts
	#if NPC_TITAN_PILOT_PROTOTYPE
	thread NpcPilotCallTitanThink( npc )
	#endif
}

#if NPC_TITAN_PILOT_PROTOTYPE

function NpcPilotCallTitanThink( entity pilot )
{
	Assert( pilot.IsNPC() )
	Assert( IsAlive( pilot ) )
	Assert ( !pilot.IsTitan() )

	pilot.EndSignal( "OnDestroy" )
	pilot.EndSignal( "OnDeath" )
	pilot.Signal( "StopCallTitanThink" )
	pilot.EndSignal( "StopCallTitanThink" )


	string title = pilot.GetTitle() + "'s Titan"
	local count = 1 //1 titan call in at a time

	while ( true ) //this loop usually only happens once, unless the titan called in is destroyed before the living pilot can get to it
	{
		entity titan = NpcPilotGetPetTitan( pilot )
		if ( !IsAlive( titan ) )
		{
			//wait for ready titan
			waitthread __WaitforTitanCallinReady( pilot )

			//ready to call in - look for a good spot
			SpawnPointFP spawnPoint
			while ( true )
			{
				wait ( RandomFloatRange( 1, 2 ) )

				//dont do stuff when animating on a parent
				if ( pilot.GetParent() )
					continue

				//Don't deploy if too close to an enemy
				if ( HasEnemyWithinDist( pilot, 300.0 ) )
					continue

				// DO the opposite - only deploy if has an enemy within this distance
			//	if ( !HasEnemyWithinDist( pilot, 2000.0 ) )
			//		continue

				//don't do stuff if you dont have a spawnPoint
				spawnPoint = FindSpawnPointForNpcCallin( pilot, TITAN_MEDIUM_AJAX_MODEL, HOTDROP_TURBO_ANIM )
				if ( !spawnPoint.valid )
					continue

				break
			}

			//call in a titan, run to it, and embark
			//in SP by default, the friendlys do NOT do the beacon tell
			titan = NpcPilotCallsInAndEmbarksTitan( pilot, spawnPoint.origin, spawnPoint.angles )
			titan.SetTitle( title )
		}
		else
		{
			Assert( IsAlive( titan ) )

			if ( HasEnemyRodeo( titan ) )
			{
				while ( HasEnemyRodeo( titan ) )
				{
					WaitSignal( titan.GetTitanSoul(), "RodeoRiderChanged", "OnDestroy" )
				}

				wait 4 //don't pop back in immediately
			}

			if ( !IsAlive( titan ) )
				continue //the titan didn't make it, lets loop back up and try again

			if ( titan.GetTitanSoul().IsDoomed() )
			{
				titan.WaitSignal( "OnDestroy" )
				continue //the titan didn't make it, lets loop back up and try again
			}

			//start running to titan as it kneels
			thread NpcPilotRunsToEmbarkTitan( pilot, titan )
			thread __TitanKneelsForPilot( pilot, titan )
			wait 2.0 //wait for titan to be in position

			if ( !IsAlive( titan ) )
				continue //the titan didn't make it, lets loop back up and try again

			//run to the titan
			waitthread NpcPilotRunsToEmbarkTitan( pilot, titan )

			if ( !IsAlive( titan ) )
				continue //the titan didn't make it, lets loop back up and try again

			//embark titan
			thread NpcPilotEmbarksTitan( pilot, titan )
		}

		local result = WaitSignal( titan, "NpcPilotBecomesTitan", "OnDeath", "OnDestroy" )
		if ( result.signal != "NpcPilotBecomesTitan" )
			continue //the titan didn't make it, lets loop back up and try again
	}
}

/************************************************************************************************\

 ######     ###    ##       ##             #### ##    ##       ######## #### ########    ###    ##    ##
##    ##   ## ##   ##       ##              ##  ###   ##          ##     ##     ##      ## ##   ###   ##
##        ##   ##  ##       ##              ##  ####  ##          ##     ##     ##     ##   ##  ####  ##
##       ##     ## ##       ##              ##  ## ## ##          ##     ##     ##    ##     ## ## ## ##
##       ######### ##       ##              ##  ##  ####          ##     ##     ##    ######### ##  ####
##    ## ##     ## ##       ##              ##  ##   ###          ##     ##     ##    ##     ## ##   ###
 ######  ##     ## ######## ########       #### ##    ##          ##    ####    ##    ##     ## ##    ##

\************************************************************************************************/


entity function NpcPilotCallsInAndEmbarksTitan( entity pilot, vector origin, vector angles )
{
	entity titan = NpcPilotCallsInTitan( pilot, origin, angles )
	thread NpcPilotRunsToAndEmbarksFallingTitan( pilot, titan )

	return titan
}

function NpcPilotRunsToAndEmbarksFallingTitan( entity pilot, entity titan )
{
	titan.EndSignal( "OnDeath" )

	//wait for it to land
	waitthread WaitTillHotDropComplete( titan )
	ShowName( titan )

	if ( !IsAlive( titan ) )
		return
	titan.EndSignal( "OnDeath" )

	//titan is alive on land so clean it up on thread end
	OnThreadEnd(
		function () : ( titan )
		{
			if ( !IsAlive( titan ) )
				return

			SetStanceStand( titan.GetTitanSoul() )

			//the pilot never made it to embark - lets stand our titan up so he can fight
			if ( !TitanHasNpcPilot( titan ) )
			{
				thread PlayAnimGravity( titan, "at_hotdrop_quickstand" )
				HideName( titan )
			}
		}
	)

	//if the pilot has died, early out
	if ( !IsAlive( pilot ) )
		return

	pilot.EndSignal( "OnDeath" )

	//run to the titan
	waitthread NpcPilotRunsToEmbarkTitan( pilot, titan )

	//embark titan
	waitthread NpcPilotEmbarksTitan( pilot, titan )
}

entity function NpcPilotCallsInTitan( entity pilot, vector origin, vector angles )
{
	Assert( !pilot.IsTitan() )
	Assert( IsAlive( pilot ) )
	Assert( !NpcPilotGetPetTitan( pilot ) )

	//reset the next titan callin timer
	NpcResetNextTitanRespawnAvailable( pilot )

	//spawn a titan
	array<string> settingsArray = GetAllowedTitanAISettings()

	string titanSettings = settingsArray.getrandom()
	entity titan 		= CreateNPC( "npc_titan", pilot.GetTeam(), origin, angles )
	SetSpawnOption_AISettings( titan, titanSettings )
	DispatchSpawn( titan )

	NpcPilotSetPetTitan( pilot, titan )

	//call it in
	thread NPCTitanHotdrops( titan, false, "at_hotdrop_drop_2knee_turbo_upgraded" )
	thread __TitanKneelOrStandAfterDropin( titan, pilot )

	//get the titan ready to be embarked
	SetStanceKneel( titan.GetTitanSoul() )
	titan.SetTitle( pilot.GetTitle() + "'s Titan" )
	UpdateEnemyMemoryFromTeammates( titan )

	return titan
}

void function __TitanKneelOrStandAfterDropin( entity titan, entity pilot )
{
	Assert( IsAlive( titan ) )
	titan.EndSignal( "OnDeath" )
	titan.EndSignal( "OnDestroy" )

	titan.WaitSignal( "TitanHotDropComplete" )

	if ( IsAlive( pilot ) )
		thread PlayAnimGravity( titan, "at_MP_embark_idle" )
	//else the titan will automatically stand up
}

//HACK -> this behavior should be completely in code
void function NpcPilotRunsToEmbarkTitan( entity pilot, entity titan )
{
	titan.EndSignal( "OnDeath" )
	titan.EndSignal( "OnDestroy" )
	pilot.EndSignal( "OnDeath" )
	pilot.EndSignal( "OnDestroy" )

	pilot.SetNoTarget( true )
	pilot.Anim_Stop()
	pilot.DisableNPCMoveFlag( NPCMF_INDOOR_ACTIVITY_OVERRIDE )
	pilot.EnableNPCMoveFlag( NPCMF_IGNORE_CLUSTER_DANGER_TIME | NPCMF_PREFER_SPRINT )
	pilot.DisableArrivalOnce( true )
	bool canMoveAndShoot = pilot.GetCapabilityFlag( bits_CAP_MOVE_SHOOT )
	pilot.SetCapabilityFlag( bits_CAP_MOVE_SHOOT, false )

	OnThreadEnd(
		function () : ( pilot, canMoveAndShoot )
		{
			if ( !IsAlive( pilot ) )
				return

			pilot.SetNoTarget( false )
			pilot.EnableNPCMoveFlag( NPCMF_INDOOR_ACTIVITY_OVERRIDE )
			pilot.DisableNPCMoveFlag( NPCMF_IGNORE_CLUSTER_DANGER_TIME | NPCMF_PREFER_SPRINT )
			pilot.SetCapabilityFlag( bits_CAP_MOVE_SHOOT, canMoveAndShoot )
		}
	)

	local titanSubClass = GetSoulTitanSubClass( titan.GetTitanSoul() )
	local embarkSet = FindBestEmbarkForNpcAnim( pilot, titan )
	string pilotAnim = GetAnimFromAlias( titanSubClass, embarkSet.animSet.thirdPersonKneelingAlias )

	pilot.ClearAllEnemyMemory()
	waitthread RunToAnimStartForced_Deprecated( pilot, pilotAnim, titan, "hijack" )
}

/************************************************************************************************\

 ######  ##      ## #### ########  ######  ##     ##
##    ## ##  ##  ##  ##     ##    ##    ## ##     ##
##       ##  ##  ##  ##     ##    ##       ##     ##
 ######  ##  ##  ##  ##     ##    ##       #########
      ## ##  ##  ##  ##     ##    ##       ##     ##
##    ## ##  ##  ##  ##     ##    ##    ## ##     ##
 ######   ###  ###  ####    ##     ######  ##     ##

\************************************************************************************************/
function NpcPilotEmbarksTitan( entity pilot, entity titan )
{
	Assert( IsAlive( pilot ) )
	Assert( IsAlive( titan ) )
	Assert( !pilot.IsTitan() )
	Assert( titan.IsTitan() )

	titan.EndSignal( "OnDestroy" )
	titan.EndSignal( "OnDeath" )

	OnThreadEnd(
		function () : ( titan, pilot )
		{
			if ( IsAlive( titan ) )
			{
				if ( titan.ContextAction_IsBusy() )
					titan.ContextAction_ClearBusy()
				titan.ClearInvulnerable()

				Assert( !IsAlive( pilot ) )
			}
		}
	)

	local isInvulnerable = pilot.IsInvulnerable()
	pilot.SetInvulnerable()
	titan.SetInvulnerable()

	local titanSubClass = GetSoulTitanSubClass( titan.GetTitanSoul() )
	local embarkSet = FindBestEmbark( pilot, titan )

	while ( embarkSet == null )
	{
		wait 1.0
		embarkSet = FindBestEmbark( pilot, titan )
	}

	local pilotAnim = GetAnimFromAlias( titanSubClass, embarkSet.animSet.thirdPersonKneelingAlias )
	local titanAnim = embarkSet.animSet.titanKneelingAnim

	if ( !titan.ContextAction_IsBusy() ) //might be set from kneeling
		titan.ContextAction_SetBusy()
	pilot.ContextAction_SetBusy()

	if ( IsCloaked( pilot ) )
		pilot.SetCloakDuration( 0, 0, 1.5 )

	//pilot.SetParent( titan, "hijack", false, 0.5 ) //the time is just in case their not exactly at the right starting position
	EmitSoundOnEntity( titan, embarkSet.audioSet.thirdPersonKneelingAudioAlias )
	thread PlayAnim( pilot, pilotAnim, titan, "hijack" )
	waitthread PlayAnim( titan, titanAnim )

	if ( !isInvulnerable )
		pilot.ClearInvulnerable()

	NpcPilotBecomesTitan( pilot, titan )
}

entity function NpcPilotDisembarksTitan( entity titan )
{
	Assert( titan.IsTitan() )
	Assert( TitanHasNpcPilot( titan ) )

	entity pilot = NpcTitanBecomesPilot( titan )
	Assert( !pilot.IsTitan() )

	NpcPilotSetPetTitan( pilot, titan )

	thread __NpcPilotDisembarksTitan( pilot, titan )

	return pilot
}

function __NpcPilotDisembarksTitan( pilot, titan )
{
	expect entity( pilot )
	expect entity( titan )

	titan.ContextAction_SetBusy()
	pilot.ContextAction_SetBusy()

	if ( pilot.GetTitle() != "" )
	{
		titan.SetTitle( pilot.GetTitle() + "'s Titan" )
	}

	local isInvulnerable = pilot.IsInvulnerable()
	pilot.SetInvulnerable()
	titan.SetInvulnerable()

	local pilot3pAnim, pilot3pAudio, titanDisembarkAnim
	local titanSubClass = GetSoulTitanSubClass( titan.GetTitanSoul() )
	local standing = titan.GetTitanSoul().GetStance() >= STANCE_STANDING // STANCE_STANDING = 2, STANCE_STAND = 3

	if ( standing )
	{
		titanDisembarkAnim = "at_dismount_stand"
		pilot3pAnim = "pt_dismount_" + titanSubClass + "_stand"
		pilot3pAudio = titanSubClass + "_Disembark_Standing_3P"
	}
	else
	{
		titanDisembarkAnim = "at_dismount_crouch"
		pilot3pAnim = "pt_dismount_" + titanSubClass + "_crouch"
		pilot3pAudio = titanSubClass + "_Disembark_Kneeling_3P"
	}

//	pilot.SetParent( titan, "hijack" )
	EmitSoundOnEntity( titan, pilot3pAudio )
	thread PlayAnim( titan, titanDisembarkAnim )
	waitthread PlayAnim( pilot, pilot3pAnim, titan, "hijack" )

	//pilot.ClearParent()
	titan.ContextAction_ClearBusy()
	pilot.ContextAction_ClearBusy()
	if ( !isInvulnerable )
		pilot.ClearInvulnerable()
	titan.ClearInvulnerable()

	if ( !standing )
		SetStanceKneel( titan.GetTitanSoul() )
}

void function NpcPilotBecomesTitan( entity pilot, entity titan )
{
	Assert( IsAlive( pilot ) )
	Assert( IsAlive( titan ) )
	Assert( IsGrunt( pilot ) || IsPilotElite( pilot ) )
	Assert( titan.IsTitan() )

	entity titanSoul = titan.GetTitanSoul()

	titanSoul.soul.seatedNpcPilot.isValid				= true

	titanSoul.soul.seatedNpcPilot.team 					= pilot.GetTeam()
	titanSoul.soul.seatedNpcPilot.spawnflags 			= expect int( pilot.kv.spawnflags )
	titanSoul.soul.seatedNpcPilot.accuracy 				= expect float( pilot.kv.AccuracyMultiplier )
	titanSoul.soul.seatedNpcPilot.proficieny 			= expect float( pilot.kv.WeaponProficiency )
	titanSoul.soul.seatedNpcPilot.health 				= expect float( pilot.kv.max_health )
	titanSoul.soul.seatedNpcPilot.physDamageScale 		= expect float( pilot.kv.physdamagescale )
	titanSoul.soul.seatedNpcPilot.weapon 				= pilot.GetMainWeapons()[0].GetWeaponClassName()
	titanSoul.soul.seatedNpcPilot.squadName 			= expect string( pilot.kv.squadname )

	titanSoul.soul.seatedNpcPilot.modelAsset 			= pilot.GetModelName()
	titanSoul.soul.seatedNpcPilot.title 				= pilot.GetTitle()

	titanSoul.soul.seatedNpcPilot.isInvulnerable		= pilot.IsInvulnerable()

	titan.SetTitle( titanSoul.soul.seatedNpcPilot.title )

	thread __TitanPilotRodeoCounter( titan )

	ScriptCallback_OnNpcPilotBecomesTitan( pilot, titan )

	pilot.Destroy()
}

entity function NpcTitanBecomesPilot( entity titan )
{
	Assert( IsValid( titan ) )
	Assert( titan.IsTitan() )

	entity titanSoul = titan.GetTitanSoul()
	titanSoul.soul.seatedNpcPilot.isValid = false

	string weapon 			= titanSoul.soul.seatedNpcPilot.weapon
	string squadName 		= titanSoul.soul.seatedNpcPilot.squadName
	asset model 			= titanSoul.soul.seatedNpcPilot.modelAsset
	string title 			= titanSoul.soul.seatedNpcPilot.title
	int team 				= titanSoul.soul.seatedNpcPilot.team
	vector origin 			= titan.GetOrigin()
	vector angles 			= titan.GetAngles()
	entity pilot 			= CreateElitePilot( team, origin, angles )

	SetSpawnOption_Weapon( pilot, weapon )
	SetSpawnOption_SquadName( pilot, squadName )
	pilot.SetValueForModelKey( model )
	DispatchSpawn( pilot )
	pilot.SetModel( model ) // this is a hack, trying to avoid having a model spawn option because its easy to abuse

	NpcPilotSetPetTitan( pilot, titan )
	NpcResetNextTitanRespawnAvailable( pilot )

	pilot.kv.spawnflags 			= titanSoul.soul.seatedNpcPilot.spawnflags
	pilot.kv.AccuracyMultiplier 	= titanSoul.soul.seatedNpcPilot.accuracy
	pilot.kv.WeaponProficiency 		= titanSoul.soul.seatedNpcPilot.proficieny
	pilot.kv.health 				= titanSoul.soul.seatedNpcPilot.health
	pilot.kv.max_health 			= titanSoul.soul.seatedNpcPilot.health
	pilot.kv.physDamageScale 		= titanSoul.soul.seatedNpcPilot.physDamageScale

	if ( titanSoul.soul.seatedNpcPilot.isInvulnerable )
		pilot.SetInvulnerable()

	titan.SetOwner( pilot )
	NPCFollowsNPC( titan, pilot )

	UpdateEnemyMemoryFromTeammates( pilot )
	thread __TitanStanceThink( pilot, titan )

	ScriptCallback_OnNpcTitanBecomesPilot( pilot, titan )

	return pilot
}

bool function TitanHasNpcPilot( entity titan )
{
	Assert( titan.IsTitan() )

	entity titanSoul = titan.GetTitanSoul()
	if ( !IsValid( titanSoul ) )
		return false

	if ( !titanSoul.soul.seatedNpcPilot.isValid )
		return false

	return true
}

entity function NpcPilotGetPetTitan( entity pilot )
{
	Assert( !pilot.IsTitan() )
	Assert( "petTitan" in pilot.s )

	if ( !IsAlive( expect entity( pilot.s.petTitan ) ) )
		return null

	Assert( pilot.s.petTitan.IsTitan() )
	return expect entity( pilot.s.petTitan )
}

void function NpcPilotSetPetTitan( entity pilot, entity titan )
{
	Assert( !pilot.IsTitan() )
	Assert( titan.IsTitan() )
	Assert( "petTitan" in pilot.s )

	pilot.s.petTitan = titan
	pilot.Signal( "PetTitanUpdated" )
}
#endif // NPC_TITAN_PILOT_PROTOTYPE

function __TitanStanceThink( entity pilot, entity titan )
{
	if ( !IsAlive( titan ) )
		return

	if ( titan.GetTitanSoul().IsDoomed() )
		return

	titan.EndSignal( "OnDeath" )
	titan.EndSignal( "OnDestroy" )
	titan.EndSignal( "NpcPilotBecomesTitan" )

	WaittillAnimDone( titan ) //wait for disembark anim

	// kneel in certain circumstances
	while ( IsAlive( pilot ) )
	{
		if ( !ChangedStance( titan ) )
			waitthread TitanWaitsToChangeStance_or_PilotDeath( pilot, titan )
	}

	if ( titan.GetTitanSoul().GetStance() < STANCE_STANDING )
	{
		while ( !TitanCanStand( titan ) )
			wait 2

		TitanStandUp( titan )
	}
}

function TitanWaitsToChangeStance_or_PilotDeath( pilot, titan )
{
	pilot.EndSignal( "OnDeath" )
	pilot.EndSignal( "OnDestroy" )

	TitanWaitsToChangeStance( titan )
}

/************************************************************************************************\

########  #######   #######  ##        ######
   ##    ##     ## ##     ## ##       ##    ##
   ##    ##     ## ##     ## ##       ##
   ##    ##     ## ##     ## ##        ######
   ##    ##     ## ##     ## ##             ##
   ##    ##     ## ##     ## ##       ##    ##
   ##     #######   #######  ########  ######

\************************************************************************************************/

function __WaitforTitanCallinReady( entity pilot )
{
	pilot.EndSignal( "OnDeath" )
	pilot.EndSignal( "OnDestroy" )

	//HACK TODO: handle eTitanAvailability.Default vs custom and none, AND ALSO make a way to kill this thread

	while ( true )
	{
		if ( pilot.s.nextTitanRespawnAvailable == NPC_NEXT_TITANTIME_RESET )
			pilot.s.nextTitanRespawnAvailable = Time() + RandomFloatRange( NPC_NEXT_TITANTIME_MIN, NPC_NEXT_TITANTIME_MAX )  //this is just a random number - maybe in the future it will be based on the npc's kills...maybe also on the players if it's a slot

		if ( pilot.s.nextTitanRespawnAvailable <= Time() )
			break

		float delay = max( pilot.s.nextTitanRespawnAvailable - Time(), 0.1 ) //make sure min delay of 0.1 to account for floating point error

		thread SetSignalDelayed( pilot, "NpcTitanRespawnAvailableUpdated", delay )
		pilot.WaitSignal( "NpcTitanRespawnAvailableUpdated" )

		//keep looping backup just in case this value changes outside this function, we get an update
		continue
	}

	Assert( Time() >= pilot.s.nextTitanRespawnAvailable )
	Assert( pilot.s.nextTitanRespawnAvailable != NPC_NEXT_TITANTIME_RESET )
}

function __TitanKneelsForPilot( pilot, titan )
{
	expect entity( pilot )
	expect entity( titan )

	pilot.EndSignal( "OnDeath" )
	pilot.EndSignal( "OnDestroy" )
	titan.EndSignal( "OnDeath" )
	titan.EndSignal( "OnDestroy" )

	OnThreadEnd(
		function () : ( pilot, titan )
		{
			if ( !IsAlive( titan ) )
				return

			SetStanceStand( titan.GetTitanSoul() )

			//the pilot never made it to embark - lets stand our titan up so he can fight
			if ( !IsAlive( pilot ) )
			{
				thread PlayAnimGravity( titan, "at_hotdrop_quickstand" )
				HideName( titan )
				titan.ContextAction_ClearBusy()
			}
		}
	)

	if ( !titan.ContextAction_IsBusy() ) //might be set from kneeling
		titan.ContextAction_SetBusy()
	SetStanceKneel( titan.GetTitanSoul() )

	waitthread PlayAnimGravity( titan, "at_MP_stand2knee_straight" )
	waitthread PlayAnim( titan, "at_MP_embark_idle" )
}

function HasEnemyRodeo( titan )
{
	expect entity( titan )

	if ( !IsAlive( titan ) )
		return false

	if ( IsValid( GetEnemyRodeoPilot( titan ) ) )
		return true

	return false
}

function __TitanPilotRodeoCounter( entity titan )
{
	titan.EndSignal( "OnDeath" )
	titan.EndSignal( "OnDestroy" )

	while ( true )
	{
		while ( !HasEnemyRodeo( titan ) )
			titan.GetTitanSoul().WaitSignal( "RodeoRiderChanged" )

		wait RandomFloatRange( 3, 6 ) //give some time for debounce in case the rider jumps right off
		if ( !HasEnemyRodeo( titan ) )
			continue

		#if NPC_TITAN_PILOT_PROTOTYPE
		thread NpcPilotDisembarksTitan( titan )
		return
		#endif
	}
}