aboutsummaryrefslogtreecommitdiff
path: root/Northstar.CustomServers/mod/scripts/vscripts/ai/_ai_utility.gnut
blob: 67c68600389343361522834828f4012b0a5d1151 (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
untyped

globalize_all_functions

function AiUtility_Init()
{
	RegisterSignal( "OnNewOwner" )
	RegisterSignal( "squadInCombat" )
	RegisterSignal( "OnEndFollow" )
	RegisterSignal( "OnStunned" )

}
////////////////////////////////////////////////////////////////////////////////
// Cloaks npc forever (to be used by anim events)
function NpcCloakOn( npc )
{
	//SetCloakDuration( fade in, duration, fade out )
	npc.SetCloakDuration( 2.0, -1, 0 )
	EmitSoundOnEntity( npc, CLOAKED_DRONE_CLOAK_START_SFX )
	EmitSoundOnEntity( npc, CLOAKED_DRONE_CLOAK_LOOP_SFX )
	npc.Minimap_Hide( TEAM_IMC, null )
	npc.Minimap_Hide( TEAM_MILITIA, null )
}
////////////////////////////////////////////////////////////////////////////////
// De-cloaks npc
function NpcCloakOff( npc)
{
	npc.SetCloakDuration( 0, 0, 1.5 )
	StopSoundOnEntity( npc, CLOAKED_DRONE_CLOAK_LOOP_SFX )
	npc.Minimap_AlwaysShow( TEAM_IMC, null )
	npc.Minimap_AlwaysShow( TEAM_MILITIA, null )
}

int function GetDefaultNPCFollowBehavior( npc )
{
	switch ( npc.GetAIClass() )
	{
		case AIC_FLYING_DRONE:
			return AIF_SUPPORT_DRONE

		case AIC_VEHICLE:
			return AIF_GUNSHIP

		case AIC_TITAN:
		case AIC_TITAN_BUDDY:
			return AIF_TITAN_FOLLOW_PILOT
	}

	return AIF_FIRETEAM
}

void function DieOnPlayerDisconnect( entity npc, entity player )
{
	Assert( IsNewThread(), "Must be threaded off" )
	Assert( npc.IsNPC() )
	Assert( player.IsPlayer() )
	Assert( IsAlive( npc ) )
	Assert( npc.GetBossPlayer() == player )
	Assert( !IsDisconnected( player ) )
	npc.EndSignal( "OnDeath" )

	player.WaitSignal( "OnDestroy" )

	// my boss quit the server!
	if ( IsAlive( npc ) && npc.GetBossPlayer() == player )
		npc.Die()
}

void function NPCFollowsPlayer( entity npc, entity leader )
{
	Assert( IsAlive( npc ) )
	Assert( leader.IsPlayer() )

	npc.SetBossPlayer( leader )

	// team
	SetTeam( npc, leader.GetTeam() )

	if ( IsSpectre( npc ) )
	{
		string squadName = GetPlayerSpectreSquadName( leader )
		SetSquad( npc, squadName )
	}

	thread DieOnPlayerDisconnect( npc, leader )
	#if SP
	Highlight_SetFriendlyHighlight( npc, "friendly_ai" )
	#else
	Highlight_SetOwnedHighlight( npc, "friendly_ai" )
	#endif

	NpcFollowsEntity( npc, leader )
}

void function NPCFollowsNPC( entity npc, entity leader )
{
	Assert( IsAlive( npc ) )
	Assert( IsAlive( leader ) )
	Assert( leader.IsNPC() )

	// team
	SetTeam( npc, leader.GetTeam() )

	// squad
	string squadNameOwner = expect string( leader.Get( "squadname" ) )
	if ( squadNameOwner != "" && leader.GetClassName() == npc.GetClassName() )
		SetSquad( npc, squadNameOwner )

	NpcFollowsEntity( npc, leader )
}

void function NpcFollowsEntity( entity npc, entity leader )
{
	// stop scripted things
	if ( IsMultiplayer() )
		npc.Signal( "StopHardpointBehavior" )

	if ( leader.IsPlayer() && leader.p.followPlayerOverride != null )
	{
		leader.p.followPlayerOverride( npc, leader )
		return
	}

	// follow!
	int followBehavior = GetDefaultNPCFollowBehavior( npc )
	npc.InitFollowBehavior( leader, followBehavior )
	npc.DisableBehavior( "Assault" )
	npc.DisableNPCFlag( NPC_ALLOW_PATROL | NPC_ALLOW_INVESTIGATE | NPC_USE_SHOOTING_COVER )
	npc.EnableBehavior( "Follow" )
}


/////////////////////////////////////////////////////////////////////////////////////////////////
bool function HasEnemyWithinDist( entity npc, float dist )
{
	float distSq = dist * dist

	array<entity> enemies
	entity closestEnemy = npc.GetClosestEnemy()
	if ( closestEnemy )
		enemies.append( closestEnemy )

	entity currentEnemy = npc.GetEnemy()
	if ( currentEnemy && currentEnemy != closestEnemy )
		enemies.append( currentEnemy )

	if ( !enemies.len() )
		return false

	vector origin = npc.GetOrigin()
	foreach ( enemy in enemies )
	{
		if ( DistanceSqr( origin, enemy.GetOrigin() ) < distSq )
			return true
	}

	return false
}

SpawnPointFP function FindSpawnPointForNpcCallin( entity npc, asset model, string anim )
{
	float yaw = npc.EyeAngles().y

	vector npcView 				= AnglesToForward( npc.EyeAngles() )
	FlightPath flightPath 		= GetAnalysisForModel( model, anim )

	CallinData drop
	InitCallinData( drop )
	SetCallinStyle( drop, eDropStyle.NEAREST_YAW_FALLBACK )
	SetCallinOwnerEyePos( drop, npc.EyePosition() )
	drop.dist 			= 800
	drop.origin 		= npc.GetOrigin() + npcView * 250
	drop.yaw 			= yaw

	vector angles = Vector( 0, yaw, 0 )
 	SpawnPointFP spawnPoint = GetSpawnPointForStyle( flightPath, drop )
	if ( spawnPoint.valid )
		return spawnPoint

	//if it didn't find one where he was looking - try near him
	drop.origin 		= npc.GetOrigin()
	spawnPoint = GetSpawnPointForStyle( flightPath, drop )

	return spawnPoint
}

function WaitForSquadInCombat( squad )
{
	local master = {}

	//when the thread ends, let child threads now
	OnThreadEnd(
		function() : ( master )
		{
			Signal( master, "OnDestroy" )
		}
	)

	// this internal function keeps track of each guy
	local combatTracker =
		function( guy, master )
		{
			expect entity( guy )
			expect entity( master )

			EndSignal( master, "OnDestroy" )
			EndSignal( guy, "OnDeath", "OnDestroy" )
			if ( !IsAlive( guy ) )
				return

			while ( guy.GetNPCState() != "combat" )
				guy.WaitSignal( "OnStateChange" )

			Signal( master, "squadInCombat" )
		}

	foreach ( guy in squad )
	{
		thread combatTracker( guy, master )
	}

	WaitSignal( master, "squadInCombat" )
}

function WaitForNpcInCombat( npc )
{
	while ( npc.GetNPCState() != "combat" )
		npc.WaitSignal( "OnStateChange" )
}

int function GetNpcHullType( entity npc )
{
	string aiSettings = npc.GetAISettingsName()
	return int ( Dev_GetAISettingByKeyField_Global( aiSettings, "HullType" ) )
}

//////////////////////////////////////////////////////////////////////////////////////////////////////
// "SPAWN AI" DEV MENU Fuctions
//////////////////////////////////////////////////////////////////////////////////////////////////////
const float CROSSHAIR_VERT_OFFSET = 32

vector function GetPlayerCrosshairOriginRaw( entity player )
{
	vector angles = player.EyeAngles()
	vector forward = AnglesToForward( angles )
	vector origin = player.EyePosition()

	vector start = origin
	vector end = origin + forward * 50000
	TraceResults result = TraceLine( start, end )
	vector crosshairOrigin = result.endPos

	return crosshairOrigin
}

vector function GetPlayerCrosshairOrigin( entity player )
{
	return (GetPlayerCrosshairOriginRaw( player ) + Vector( 0, 0, CROSSHAIR_VERT_OFFSET ))
}

void function DEV_SpawnBTAtCrosshair( bool hotdrop = false )
{
	DisablePrecacheErrors()
	wait 0.2
	entity player = GetPlayerArray()[ 0 ]

	entity pet_titan = player.GetPetTitan()
	if ( IsValid(pet_titan) )
		pet_titan.Destroy()

	vector origin = GetPlayerCrosshairOrigin( player )
	vector angles = Vector( 0, 0, 0 )

	TitanLoadoutDef loadout = GetTitanLoadoutForCurrentMap()
	entity npc = CreateAutoTitanForPlayer_FromTitanLoadout( player, loadout, origin, angles )

	SetSpawnOption_AISettings( npc, "npc_titan_buddy" )

	DispatchSpawn( npc )

	SetPlayerPetTitan( player, npc )

	if ( hotdrop )
		thread NPCTitanHotdrops( npc, false )
}

void function DEV_SpawnAllNPCsWithTeam( int team )
{
	printt( "script thread DEV_SpawnAllNPCsWithTeam( " + team + " )" )
	Assert( IsNewThread(), "Must be threaded off due to precache issues" )
	bool restoreHostThreadMode = GetConVarInt( "host_thread_mode" ) != 0
	if ( restoreHostThreadMode )
	{
		DisablePrecacheErrors()
		wait 0.5
	}

	entity player = GetPlayerArray()[ 0 ]
	vector origin = GetPlayerCrosshairOrigin( player )
	array<string> aiSettings = GetAllNPCSettings()

	foreach ( settings in aiSettings )
	{
		vector angles = < 0, RandomFloat( 360 ), 0 >
		entity npc = CreateNPCFromAISettings( settings, team, origin, angles )
		DispatchSpawn( npc )
	}

	if ( restoreHostThreadMode )
	{
		wait 0.2
		RestorePrecacheErrors()
	}
}

void function DEV_SpawnNPCWithWeaponAtCrosshair( string baseClass, string aiSettings, int team, string weaponName = "" )
{
	printt( "script thread DEV_SpawnNPCWithWeaponAtCrosshair( \"" + baseClass + "\", \"" + aiSettings + "\", " + team + ", \"" + weaponName + "\")" )
	Assert( IsNewThread(), "Must be threaded off due to precache issues" )
	bool restoreHostThreadMode = GetConVarInt( "host_thread_mode" ) != 0
	entity npc = DEV_SpawnNPCWithWeaponAtCrosshairStart( restoreHostThreadMode, baseClass, aiSettings, team, weaponName )
	DispatchSpawn( npc )
	DEV_SpawnNPCWithWeaponAtCrosshairEnd( restoreHostThreadMode )
}

void function DEV_SpawnMercTitanAtCrosshair( string mercName )
{
	printt( "script thread DEV_SpawnMercTitanAtCrosshair( \"" + mercName + "\")" )
	Assert( IsNewThread(), "Must be threaded off due to precache issues" )
	TitanLoadoutDef ornull loadout = GetTitanLoadoutForBossCharacter( mercName )
	if ( loadout == null )
		return
	expect TitanLoadoutDef( loadout )
	string baseClass = "npc_titan"
	string aiSettings = GetNPCSettingsFileForTitanPlayerSetFile( loadout.setFile )

	bool restoreHostThreadMode = GetConVarInt( "host_thread_mode" ) != 0
	entity npc = DEV_SpawnNPCWithWeaponAtCrosshairStart( restoreHostThreadMode, baseClass, aiSettings, TEAM_IMC )
	SetSpawnOption_NPCTitan( npc, TITAN_MERC )
	SetSpawnOption_TitanLoadout( npc, loadout )
	npc.ai.bossTitanPlayIntro = false

	DispatchSpawn( npc )
	DEV_SpawnNPCWithWeaponAtCrosshairEnd( restoreHostThreadMode )
}

void function DEV_SpawnWeaponAtCrosshair( string weaponName )
{
	printt( "script thread DEV_SpawnWeaponAtCrosshair( \"" + weaponName + "\")" )

	Assert( IsNewThread(), "Must be threaded off due to precache issues" )

	entity player = GetPlayerArray()[ 0 ]
	if ( !IsValid( player ) )
		return
	vector origin = GetPlayerCrosshairOrigin( player )
	vector angles = Vector( 0, 0, 0 )
	entity weapon = CreateWeaponEntityByNameWithPhysics( weaponName, origin, angles )

#if SP
	bool isTitanWeapon = weaponName.find( "mp_titanweapon_" ) != null
	if ( isTitanWeapon )
		thread TitanLoadoutWaitsForPickup( weapon, SPTitanLoadoutPickup )
#endif

}

string function GetAISettingsFromPlayerSetFile( string playerSetfile )
{
	TitanLoadoutDef ornull loadout = GetTitanLoadoutForColumn( "setFile", playerSetfile )
	Assert( loadout != null, "Couldn't find loadout with set file " + playerSetfile )
	expect TitanLoadoutDef( loadout )

	return expect string( Dev_GetPlayerSettingByKeyField_Global( playerSetfile, GetAISettingsStringForMode() ) )
}


void function DEV_SpawnBossTitanAtCrosshair( string playerSetfile )
{
	string aiSettings = GetAISettingsFromPlayerSetFile( playerSetfile )
	printt( "script thread DEV_SpawnBossTitanAtCrosshair( \"" + aiSettings + "\")" )
	Assert( IsNewThread(), "Must be threaded off due to precache issues" )

	string baseClass = "npc_titan"
	bool restoreHostThreadMode = GetConVarInt( "host_thread_mode" ) != 0
	entity npc = DEV_SpawnNPCWithWeaponAtCrosshairStart( restoreHostThreadMode, baseClass, aiSettings, TEAM_IMC )
	SetSpawnOption_NPCTitan( npc, TITAN_BOSS )
//	SetSpawnOption_TitanLoadout( npc, loadout )

	string builtInLoadout = expect string( Dev_GetAISettingByKeyField_Global( aiSettings, "npc_titan_player_settings" ) )
//	SetTitanSettings( npc.ai.titanSettings, builtInLoadout )
	npc.ai.titanSpawnLoadout.setFile = builtInLoadout
	OverwriteLoadoutWithDefaultsForSetFile( npc.ai.titanSpawnLoadout ) // get the entire loadout, including defensive and tactical

	DispatchSpawn( npc )
	DEV_SpawnNPCWithWeaponAtCrosshairEnd( restoreHostThreadMode )
}

entity function DEV_SpawnNPCWithWeaponAtCrosshairStart( bool restoreHostThreadMode, string baseClass, string aiSettings, int team, string weaponName = "" )
{
	if ( restoreHostThreadMode )
	{
		DisablePrecacheErrors()
		wait 0.5
	}

	float time = Time()
	for ( ;; )
	{
		if ( Time() > time )
			break
		WaitFrame()
	}
	entity player = GetPlayerArray()[ 0 ]
	if ( !IsValid( player ) )
		return

	vector origin = GetPlayerCrosshairOrigin( player )
	vector angles = Vector( 0, 0, 0 )

	entity npc = CreateNPC( baseClass, team, origin, angles )
	if ( IsTurret( npc ) )
		npc.kv.origin -= Vector( 0, 0, CROSSHAIR_VERT_OFFSET )
	SetSpawnOption_AISettings( npc, aiSettings )

	if ( npc.GetClassName() == "npc_soldier" || npc.GetClassName() == "npc_spectre" )
		npc.kv.squadname = "crosshairSpawnSquad_team_" + team + "_" + baseClass + "_" + aiSettings

	if ( weaponName != "" )
		SetSpawnOption_Weapon( npc, weaponName )

	if ( npc.GetClassName() == "npc_titan" )
	{
		string builtInLoadout = expect string( Dev_GetAISettingByKeyField_Global( aiSettings, "npc_titan_player_settings" ) )
		SetTitanSettings( npc.ai.titanSettings, builtInLoadout )
		npc.ai.titanSpawnLoadout.setFile = builtInLoadout
		OverwriteLoadoutWithDefaultsForSetFile( npc.ai.titanSpawnLoadout ) // get the entire loadout, including defensive and tactical
	}

	return npc
}

void function DEV_SpawnNPCWithWeaponAtCrosshairEnd( bool restoreHostThreadMode )
{
	if ( restoreHostThreadMode )
	{
		wait 0.2
		RestorePrecacheErrors()
	}
}


function SetAISettingsWrapper( entity npc, string settings )
{
	npc.SetAISettings( settings )
	Assert( settings.find( npc.GetClassName() ) == 0, "NPC classname " + npc.GetClassName() + " not found in " + settings )

	if ( IsSingleplayer() )
	{
		FixupTitle( npc )
	}
}

bool function WithinEngagementRange( entity npc, vector origin )
{
	entity weapon = npc.GetActiveWeapon()
	if ( weapon == null )
		return false

	float dist = Distance( npc.GetOrigin(), origin )
	if ( dist < weapon.GetWeaponInfoFileKeyField( "npc_min_engage_range" ) )
		return false

	return dist <= weapon.GetWeaponInfoFileKeyField( "npc_max_engage_range" )
}


function DEV_AITitanDuel()
{
	thread DEV_AITitanDuelThread()
}

entity function DEV_AITitanDuelSpawn( entity player, int team, vector origin, vector angles, aiSetting )
{
	entity titan = CreateNPC( "npc_titan", team, origin, angles )
	SetSpawnOption_AISettings( titan, aiSetting )
	DispatchSpawn( titan )

	vector ornull clampedPos = NavMesh_ClampPointForAI( origin, titan )
	if ( clampedPos != null )
	{
		titan.SetOrigin( expect vector( clampedPos ) )
	}
	else
	{
		array<entity> spawnpoints = SpawnPoints_GetTitan()
		if ( spawnpoints.len() )
		{
			entity spawnpoint = GetClosest( spawnpoints, origin )
			titan.SetOrigin( spawnpoint.GetOrigin() )
		}
	}

	return titan
}

function DEV_AITitanDuelThread()
{
	DisablePrecacheErrors()
	wait 0.5

	array<string> aiSettings = GetAllowedTitanAISettings()

	aiSettings.randomize()

	entity player = GetPlayerArray()[ 0 ]

	entity imcTitan = null
	entity militiaTitan = null

	int currentSetting = 0


	while ( 1 )
	{
		if ( !IsValid( imcTitan ) )
		{
			vector origin = GetPlayerCrosshairOrigin( player ) + < -300, -300, 0 >
			vector angles = Vector( 0, 0, 0 )

			imcTitan = DEV_AITitanDuelSpawn( player, TEAM_IMC, origin, angles, aiSettings[currentSetting] )
			currentSetting = (currentSetting + 1) % aiSettings.len()

			if ( IsValid( militiaTitan ) )
			{
				imcTitan.SetEnemyLKP( militiaTitan, militiaTitan.GetOrigin() )
				militiaTitan.SetEnemyLKP( imcTitan, imcTitan.GetOrigin() )
			}
		}

		if ( !IsValid( militiaTitan ) )
		{
			vector origin = GetPlayerCrosshairOrigin( player ) + < 300, 300, 0 >
			vector angles = Vector( 0, 180, 0 )

			militiaTitan = DEV_AITitanDuelSpawn( player, TEAM_MILITIA, origin, angles, aiSettings[currentSetting] )
			currentSetting = (currentSetting + 1) % aiSettings.len()

			if ( IsValid( imcTitan ) )
			{
				militiaTitan.SetEnemyLKP( imcTitan, imcTitan.GetOrigin() )
				imcTitan.SetEnemyLKP( militiaTitan, militiaTitan.GetOrigin() )
			}
		}

		wait 2
	}
}