aboutsummaryrefslogtreecommitdiff
path: root/Northstar.CustomServers/mod/scripts/vscripts/gamemodes/_gamemode_at.nut
blob: 3d4f2d0c32807aecc0081ed220cbd9eae6db9fc0 (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
untyped
global function GamemodeAt_Init
global function RateSpawnpoints_AT

const int BH_AI_TEAM = TEAM_BOTH
const int BOUNTY_TITAN_DAMAGE_POOL = 400 // Rewarded for damage
const int BOUNTY_TITAN_KILL_REWARD = 100 // Rewarded for kill
const float WAVE_STATE_TRANSITION_TIME = 5.0
const float BANK_OPEN_TIME = 40.0

const array<string> VALID_BOUNTY_TITAN_SETTINGS = [
	"npc_titan_atlas_stickybomb_bounty",
	"npc_titan_atlas_tracker_bounty",
	"npc_titan_ogre_minigun_bounty",
	"npc_titan_ogre_meteor_bounty",
	"npc_titan_stryder_leadwall_bounty",
	"npc_titan_stryder_sniper_bounty",
	"npc_titan_atlas_vanguard_bounty"
]


// IMPLEMENTATION NOTES:
// bounty hunt is a mode that was clearly pretty heavily developed, and had alot of scrapped concepts (i.e. most wanted player bounties, turret bounties, collectable blackbox objectives)
// in the interest of time, this script isn't gonna support any of that atm
// alot of the remote functions also take parameters that aren't used, i'm not gonna populate these and just use default values for now instead
// however, if you do want to mess with this stuff, almost all the remote functions for this stuff are still present in cl_gamemode_at, and should work fine with minimal fuckery in my experience

struct {
	array<entity> campsToRegisterOnEntitiesDidLoad

	array<entity> banks 
	array<AT_WaveOrigin> camps
	
	table< int, table< string, int > > trackedCampNPCSpawns
} file

void function GamemodeAt_Init()
{
	AddCallback_GameStateEnter( eGameState.Playing, RunATGame )
	
	AddCallback_OnClientConnected( InitialiseATPlayer )

	AddSpawnCallbackEditorClass( "info_target", "info_attrition_bank", CreateATBank )
	AddSpawnCallbackEditorClass( "info_target", "info_attrition_camp", CreateATCamp )
	AddCallback_EntitiesDidLoad( CreateATCamps_Delayed )
}

void function RateSpawnpoints_AT( int checkclass, array<entity> spawnpoints, int team, entity player )
{
	RateSpawnpoints_Generic( checkclass, spawnpoints, team, player ) // temp 
}

// world and player inits

void function InitialiseATPlayer( entity player )
{
	Remote_CallFunction_NonReplay( player, "ServerCallback_AT_OnPlayerConnected" )
}

void function CreateATBank( entity spawnpoint )
{
	entity bank = CreatePropDynamic( spawnpoint.GetModelName(), spawnpoint.GetOrigin(), spawnpoint.GetAngles(), SOLID_VPHYSICS )
	bank.SetScriptName( "AT_Bank" )
	

	bank.SetUsableByGroup( "pilot" )
	bank.SetUsePrompts( "#HOLD_TO_USE_GENERIC", "#PRESS_TO_USE_GENERIC" )
	bank.UnsetUsable()
	
	// create tracker ent
	// we don't need to store these at all, client just needs to get them
	DispatchSpawn( GetAvailableBankTracker( bank ) )
	
	thread PlayAnim( bank, "mh_inactive_idle" )
	
	file.banks.append( bank )
}

void function CreateATCamp( entity spawnpoint )
{
	// delay this so we don't do stuff before all spawns are initialised and that
	file.campsToRegisterOnEntitiesDidLoad.append( spawnpoint )
}

void function CreateATCamps_Delayed()
{
	// we delay registering camps until EntitiesDidLoad since they rely on spawnpoints and stuff, which might not all be ready in the creation callback
	// unsure if this would be an issue in practice, but protecting against it in case it would be
	foreach ( entity camp in file.campsToRegisterOnEntitiesDidLoad )
	{
		AT_WaveOrigin campStruct
		campStruct.ent = camp
		campStruct.origin = camp.GetOrigin()
		campStruct.radius = expect string( camp.kv.radius ).tofloat()
		campStruct.height = expect string( camp.kv.height ).tofloat()
		
		// assumes every info_attrition_camp will have all 9 phases, possibly not a good idea?
		for ( int i = 0; i < 9; i++ )
			campStruct.phaseAllowed.append( expect string( camp.kv[ "phase_" + ( i + 1 ) ] ) == "1" )
		
		// get droppod spawns
		foreach ( entity spawnpoint in SpawnPoints_GetDropPod() )
			if ( Distance( camp.GetOrigin(), spawnpoint.GetOrigin() ) < 1500.0 )
				campStruct.dropPodSpawnPoints.append( spawnpoint )
		
		foreach ( entity spawnpoint in SpawnPoints_GetTitan() )
			if ( Distance( camp.GetOrigin(), spawnpoint.GetOrigin() ) < 1500.0 )
				campStruct.titanSpawnPoints.append( spawnpoint )
	
		// todo: turret spawns someday maybe
	
		file.camps.append( campStruct )
	}
	
	file.campsToRegisterOnEntitiesDidLoad.clear()
}

// scoring funcs

// don't use this where possible as it doesn't set score and stuff
void function AT_SetPlayerCash( entity player, int amount )
{
	// split into stacks of 256 where necessary
	int stacks = amount / 256 // automatically rounds down because int division

	player.SetPlayerNetInt( "AT_bonusPoints256", stacks )
	player.SetPlayerNetInt( "AT_bonusPoints", amount - stacks * 256 )
}

int function AT_GetPlayerCash( entity player )
{
	return player.GetPlayerNetInt( "AT_bonusPoints256" ) * 256 + player.GetPlayerNetInt( "AT_bonusPoints" )
}

void function AT_AddPlayerCash( entity player, int amount )
{
	// update score difference
	AddTeamScore( player.GetTeam(), amount / 2 )
	AT_SetPlayerCash( player, player.GetPlayerNetInt( "AT_bonusPoints" ) + ( player.GetPlayerNetInt( "AT_bonusPoints256" ) * 256 ) + amount )
}

// run gamestate

void function RunATGame()
{
	thread RunATGame_Threaded()
}

void function RunATGame_Threaded()
{
	svGlobal.levelEnt.EndSignal( "GameStateChanged" )
	
	OnThreadEnd( function()
	{
		SetGlobalNetBool( "banksOpen", false )
	})
	
	wait WAVE_STATE_TRANSITION_TIME // initial wait before first wave
		
	for ( int waveCount = 1; ; waveCount++  )
	{
		wait WAVE_STATE_TRANSITION_TIME
	
		// cap to number of real waves
		int waveId = ( waveCount / 2 ) 
		// last wave is clearly unfinished so don't use, just cap to last actually used one
		if ( waveId >= GetWaveDataSize() - 1 )
		{
			waveId = GetWaveDataSize() - 2	
			waveCount = waveId * 2
		}
			
		SetGlobalNetInt( "AT_currentWave", waveId )
		bool isBossWave = waveCount / float( 2 ) > waveId // odd number waveCount means boss wave
		
		// announce the wave 
		foreach ( entity player in GetPlayerArray() )
		{
			if ( isBossWave )
				Remote_CallFunction_NonReplay( player, "ServerCallback_AT_AnnounceBoss" )
			else
				Remote_CallFunction_NonReplay( player, "ServerCallback_AT_AnnouncePreParty", 0.0, waveId )	
		}
		
		wait WAVE_STATE_TRANSITION_TIME
		
		// run the wave
		
		AT_WaveData wave = GetWaveData( waveId )
		array< array<AT_SpawnData> > campSpawnData
		
		if ( isBossWave )
			campSpawnData = wave.bossSpawnData
		else
			campSpawnData = wave.spawnDataArrays
		
		// initialise pending spawns
		foreach ( array< AT_SpawnData > campData in campSpawnData )
		{
			foreach ( AT_SpawnData spawnData in campData )
				spawnData.pendingSpawns = spawnData.totalToSpawn
		}
		
		// clear tracked spawns
		file.trackedCampNPCSpawns = {}
		while ( true )
		{		
			// if this is ever 0 by the end of this loop, wave is complete
			int numActiveCampSpawners = 0
		
			// iterate over camp data for wave
			for ( int campIdx = 0; campIdx < campSpawnData.len() && campIdx < file.camps.len(); campIdx++ )
			{
				if ( !( campIdx in file.trackedCampNPCSpawns ) )
					file.trackedCampNPCSpawns[ campIdx ] <- {}
			
				// iterate over ai spawn data for camp
				foreach ( AT_SpawnData spawnData in campSpawnData[ campIdx ] )
				{
					if ( !( spawnData.aitype in file.trackedCampNPCSpawns[ campIdx ] ) )
						file.trackedCampNPCSpawns[ campIdx ][ spawnData.aitype ] <- 0
				
					if ( spawnData.pendingSpawns > 0 || file.trackedCampNPCSpawns[ campIdx ][ spawnData.aitype ] > 0 )
						numActiveCampSpawners++
				
					// try to spawn as many ai as we can, as long as the camp doesn't already have too many spawned
					int spawnCount
					for ( spawnCount = 0; spawnCount < spawnData.pendingSpawns && spawnCount < spawnData.totalAllowedOnField - file.trackedCampNPCSpawns[ campIdx ][ spawnData.aitype ]; )
					{						
						// not doing this in a generic way atm, but could be good for the future if we want to support more ai
						switch ( spawnData.aitype )
						{
							case "npc_soldier":
							case "npc_spectre":
							case "npc_stalker":
								thread AT_SpawnDroppodSquad( campIdx, spawnData.aitype )
								spawnCount += 4
								break
							
							case "npc_super_spectre":
								thread AT_SpawnReaper( campIdx )
								spawnCount += 1
								break
								
							case "npc_titan":
								thread AT_SpawnBountyTitan( campIdx )
								spawnCount += 1
								break
							
							default:
								print( "BOUNTY HUNT: Tried to spawn unsupported ai of type \"" + "\" at camp " + campIdx )
						}
					}
					
					// track spawns
					file.trackedCampNPCSpawns[ campIdx ][ spawnData.aitype ] += spawnCount
					spawnData.pendingSpawns -= spawnCount
				}
			}
			
			if ( numActiveCampSpawners == 0 )
				break
		
			wait 0.5
		}
		
		wait WAVE_STATE_TRANSITION_TIME
		
		waitthread BanksOpen_Threaded()
	}
}

// Think function for banks
void function BanksOpen_Threaded()
{
	// Open
	SetGlobalNetBool( "banksOpen", true )
	
	foreach ( entity player in GetPlayerArray() )
		Remote_CallFunction_NonReplay( player, "ServerCallback_AT_BankOpen" )
	
	foreach ( entity bank in file.banks )
	{
		bank.SetUsable()
		thread PlayAnim( bank, "mh_inactive_2_active" )
	}
	
	// Think
	// table < player, moneyDeposited >
	table < entity, int > depositingPlayers
	foreach ( entity bank in file.banks )
	{
		AddCallback_OnUseEntity( bank, function( b, p ) : ( depositingPlayers ) {
			expect entity( b )
			expect entity( p )
			
			if ( !( p in depositingPlayers ) )
			{
				depositingPlayers[p] <- 0
				p.SetPlayerNetBool( "AT_playerUploading", true )
			}
		} )
	}
	
	
	float bankOpenTime = Time()
	
	SetGlobalNetTime( "AT_bankStartTime", Time() )
	SetGlobalNetTime( "AT_bankEndTime", Time() + BANK_OPEN_TIME )
	
	while ( Time() - bankOpenTime < BANK_OPEN_TIME )
	{
		foreach ( player, amount in depositingPlayers )
		{
			entity closestBank = GetClosest( file.banks, player.GetOrigin(), 300 )
			
			// we're close to a bank
			if ( closestBank != null && AT_GetPlayerCash( player ) > 0 )
			{
				depositingPlayers[player] += DepositMoney( player )
				continue
			}
			
			// we've failed the check above, either no more money left or too far from bank
			Remote_CallFunction_NonReplay( player, "ServerCallback_AT_FinishDeposit", depositingPlayers[player] )
			player.SetPlayerNetBool( "AT_playerUploading", false )
			delete depositingPlayers[ player ]
		}
		wait 0.1
	}
	
	
	// remove players after closing banks
	foreach ( player, amount in depositingPlayers )
	{
		Remote_CallFunction_NonReplay( player, "ServerCallback_AT_FinishDeposit", depositingPlayers[player] )
		player.SetPlayerNetBool( "AT_playerUploading", false )
		delete depositingPlayers[ player ]
	}
	
	
	// Close
	foreach ( entity bank in file.banks )
	{
		bank.UnsetUsable()
		thread PlayAnim( bank, "mh_active_2_inactive" )
	}
	
	foreach ( entity player in GetPlayerArray() )
		Remote_CallFunction_NonReplay( player, "ServerCallback_AT_BankClose" )
	
	SetGlobalNetBool( "banksOpen", false )
}

// deposits money
// returns the amount deposited, this wont always be 10
int function DepositMoney( entity player )
{
	int newValue = AT_GetPlayerCash( player ) - 10
	
	if ( newValue < 0 )
		newValue = 0
	
	int deposited = AT_GetPlayerCash( player ) - newValue
	
	AT_SetPlayerCash( player, newValue )
	player.SetPlayerGameStat( PGS_SCORE, newValue ) 
	player.AddToPlayerGameStat( PGS_ASSAULT_SCORE, deposited ) 
	AddTeamScore( player.GetTeam(), deposited )
	
	return deposited
}

// splits money between player and their team bank
void function GiveMoneyToPlayer( entity player, int money )
{
	player.AddToPlayerGameStat( PGS_ASSAULT_SCORE, money / 2 ) // score
	player.AddToPlayerGameStat( PGS_SCORE, money / 2 ) // bonus
	AddTeamScore( player.GetTeam(), money / 2 )
	AT_SetPlayerCash( player, AT_GetPlayerCash( player ) + money / 2 )
}

// entity funcs

void function AT_SpawnDroppodSquad( int camp, string aiType )
{
	entity spawnpoint
	if ( file.camps[ camp ].dropPodSpawnPoints.len() == 0 )
		spawnpoint = file.camps[ camp ].ent
	else
		spawnpoint = file.camps[ camp ].dropPodSpawnPoints.getrandom()
	
	// add variation to spawns
	wait RandomFloat( 1.0 )
	
	AiGameModes_SpawnDropPod( spawnpoint.GetOrigin(), spawnpoint.GetAngles(), BH_AI_TEAM, aiType, void function( array<entity> guys ) : ( camp, aiType ) 
	{
		AT_HandleSquadSpawn( guys, camp, aiType )
	})
}

void function AT_HandleSquadSpawn( array<entity> guys, int camp, string aiType )
{
	foreach ( entity guy in guys )
	{
		guy.EnableNPCFlag( NPC_ALLOW_PATROL | NPC_ALLOW_INVESTIGATE | NPC_ALLOW_HAND_SIGNALS | NPC_ALLOW_FLEE )
		
		// untrack them on death
		thread AT_WaitToUntrackNPC( guy, camp, aiType )
	}
}

void function AT_SpawnReaper( int camp )
{
	entity spawnpoint
	if ( file.camps[ camp ].dropPodSpawnPoints.len() == 0 )
		spawnpoint = file.camps[ camp ].ent
	else
		spawnpoint = file.camps[ camp ].titanSpawnPoints.getrandom()

	// add variation to spawns
	wait RandomFloat( 1.0 )
	
	AiGameModes_SpawnReaper( spawnpoint.GetOrigin(), spawnpoint.GetAngles(), BH_AI_TEAM, "npc_super_spectre",void function( entity reaper ) : ( camp ) 
	{
		thread AT_WaitToUntrackNPC( reaper, camp, "npc_super_spectre" )
	})
}

void function AT_SpawnBountyTitan( int camp )
{
	entity spawnpoint
	if ( file.camps[ camp ].dropPodSpawnPoints.len() == 0 )
		spawnpoint = file.camps[ camp ].ent
	else
		spawnpoint = file.camps[ camp ].titanSpawnPoints.getrandom()

	// add variation to spawns
	wait RandomFloat( 1.0 )
	
	// look up titan to use
	int bountyID = 0
	try 
	{
		bountyID = ReserveBossID( VALID_BOUNTY_TITAN_SETTINGS.getrandom() )
	}
	catch ( ex ) {} // if we go above the expected wave count that vanilla supports, there's basically no way to ensure that this func won't error, so default 0 after that point
	
	string aisettings = GetTypeFromBossID( bountyID )
	string titanClass = expect string( Dev_GetAISettingByKeyField_Global( aisettings, "npc_titan_player_settings" ) )
	
	
	AiGameModes_SpawnTitan( spawnpoint.GetOrigin(), spawnpoint.GetAngles(), BH_AI_TEAM, titanClass, aisettings, void function( entity titan ) : ( camp, bountyID ) 
	{
		// set up titan-specific death/damage callbacks
		AddEntityCallback_OnDamaged( titan, OnBountyDamaged)
		AddEntityCallback_OnKilled( titan, OnBountyKilled )
		
		titan.GetTitanSoul().soul.skipDoomState = true
		// i feel like this should be localised, but there's nothing for it in r1_english?
		titan.SetTitle( GetNameFromBossID( bountyID ) )
		thread AT_WaitToUntrackNPC( titan, camp, "npc_titan" )
	} )
}

// Tracked entities will require their own "wallet"
// for titans it should be used for rounding error compenstation
// for infantry it sould be used to store money if the npc kills a player
void function OnBountyDamaged( entity titan, var damageInfo )
{
	entity attacker = DamageInfo_GetAttacker( damageInfo )
	if ( !attacker.IsPlayer() )
		attacker = GetLatestAssistingPlayerInfo( titan ).player
	
	if ( IsValid( attacker ) && attacker.IsPlayer() )
	{
			int reward = int ( BOUNTY_TITAN_DAMAGE_POOL * DamageInfo_GetDamage( damageInfo ) / titan.GetMaxHealth() )
			printt ( titan.GetMaxHealth(), DamageInfo_GetDamage( damageInfo ) )
			
			AT_AddPlayerCash( attacker, reward )
	}
}

void function OnBountyKilled( entity titan, var damageInfo )
{
	entity attacker = DamageInfo_GetAttacker( damageInfo )
	if ( !attacker.IsPlayer() )
		attacker = GetLatestAssistingPlayerInfo( titan ).player
	
	if ( IsValid( attacker ) && attacker.IsPlayer() )
		AT_AddPlayerCash( attacker, BOUNTY_TITAN_KILL_REWARD )
}

void function AT_WaitToUntrackNPC( entity guy, int camp, string aiType )
{
	guy.WaitSignal( "OnDeath", "OnDestroy" )
	file.trackedCampNPCSpawns[ camp ][ aiType ]--
}