aboutsummaryrefslogtreecommitdiff
path: root/Northstar.CustomServers/mod/scripts/vscripts/titan/_titan_triple_health.gnut
blob: 7515b868fd53b6c6a3835a8d673467aad97ebad4 (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
untyped

global function HealthRegenInit

global function TitanLoseSegementFX //JFS: Only being used for Rodeo now, rename later if needed
global function GibBodyPart

const SEGMENT_DOWN_SOUNDS_3P = [
	"titan_healthbar_tier3_down_3P_vs_3P", // 0 left (doom)
	"titan_healthbar_tier2_down_3P_vs_3P", // 1 left
	"titan_healthbar_tier1_down_3P_vs_3P", // 2 left
	"titan_healthbar_tier1_down_3P_vs_3P" // shield gone
]

const SEGMENT_DOWN_SOUNDS_3P_ATTACKER = [
	"titan_healthbar_tier3_down_1P_vs_3P", // 0 left (doom)
	"titan_healthbar_tier2_down_1P_vs_3P", // 1 left
	"titan_healthbar_tier1_down_1P_vs_3P", // 2 left
	"titan_healthbar_tier1_down_1P_vs_3P" // shield gone
]

const SEGMENT_DOWN_SOUNDS_1P = [
	"titan_healthbar_tier3_down_1P", // 0 left (doom)
	"titan_healthbar_tier2_down_1P", // 1 left
	"titan_healthbar_tier1_down_1P", // 2 left
	"titan_healthbar_tier1_down_1P" // shield gone
]

const DAMAGE_FORGIVENESS_CEILING = 1200.0
const DAMAGE_FORGIVENESS_FLOOR = 500.0
const LOW_HEALTH_WARNING_SOUND = "Weapon_Vortex_Gun.ExplosiveWarningBeep"

const TITAN_DAMAGE_MITIGATION_DAMAGESCALE = 0.5

struct {
	int shieldDecayRate = 2

	table< entity, table< entity, float > > soulToSoulDamageMemory
} file;

function HealthRegenInit()
{
	if ( GetCurrentPlaylistVarInt( "titan_health_decay_amount", 0 ) > 0 )
	{
		AddSoulInitFunc( TitanHealthDecayThink )
	}

	AddSoulInitFunc( TitanHealthRegenThink )

	if ( TitanShieldDecayEnabled() )
	{
		AddSoulInitFunc( TitanShieldDecayThink )
	}

	AddDamageCallback( "player", TitanSegmentedHealth_OnDamage )
	AddDamageCallback( "npc_titan", TitanSegmentedHealth_OnDamage )
	AddCallback_OnTitanDoomed( OnTitanDoomed )

	RegisterSignal( "HealthSegmentLost" )
}


void function TitanHealthDecayThink( entity soul )
{
	thread TitanHealthDecayThinkInternal( soul )
}

void function TitanHealthDecayThinkInternal( entity soul )
{
	soul.EndSignal( "OnDestroy" )
	soul.EndSignal( "OnTitanDeath" )

	soul.SetShieldHealth( 0 )

	while ( 1 )
	{
		entity titan = soul.GetTitan()
		int damageAmout = GetCurrentPlaylistVarInt( "titan_health_decay_amount", 0 )
		titan.TakeDamage( damageAmout, null, null, { scriptType = DF_DOOMED_HEALTH_LOSS, damageSourceId = damagedef_suicide } )
		WaitFrame()
	}
}

void function TitanHealthRegenThink( entity soul )
{
	thread TitanHealthRegenThink_Internal( soul )
}

void function TitanHealthRegenThink_Internal( entity soul )
{
	soul.EndSignal( SIGNAL_TITAN_HEALTH_REGEN )
	soul.EndSignal( "OnTitanDeath" )
	soul.EndSignal( "OnDestroy" )

	if ( !soul.soul.regensHealth )
		return

	entity titan = soul.GetTitan()

	if ( !IsValid( titan ) )
		return

	int healthPerTab = GetSegmentHealthForTitan( titan )

	// set this if AI titans need to be aware of segment health. Not used currently
	//titan.SetHealthPerSegment( healthPerTab )

	int lastTitanHealth = titan.GetHealth()
	bool regenSound = false
	int maxHealth = titan.GetMaxHealth()
	float lastTime = Time()

	while ( 1 )
	{
		titan = soul.GetTitan()
		if ( !IsAlive( titan ) )
			return
		int titanHealth = titan.GetHealth()
		Assert( titan )

		if ( !titan.IsTitan() )
			return

		if ( !soul.soul.regensHealth )
			return

		int currentRegenTab = GetTitanCurrentRegenTab( titan )

		if ( currentRegenTab != GetSoulBatteryCount( soul ) )
			SetSoulBatteryCount( soul, GetTitanCurrentRegenTab( titan ) )

		int maxHealthForCurrentTab = currentRegenTab * healthPerTab

		if ( titanHealth == maxHealthForCurrentTab )
		{
			if ( regenSound )
			{
			 	StopSoundOnEntity( titan, "titan_energyshield_up" )
			 	regenSound = false
		 	}
		}

		lastTitanHealth = titanHealth
		lastTime = Time()
		WaitFrame()
	}
}

void function TitanSegmentedHealth_OnDamage( entity titan, var damageInfo )
{
	if ( !titan.IsTitan() )
		return

	entity soul = titan.GetTitanSoul()

	if ( !IsValid( soul ) )
		return

	if ( ShouldReduceDamageForSegmentedHealth( soul, damageInfo ) )
		DamageInfo_ScaleDamage( damageInfo, 0.3 )

	thread TitanSegmentedHealth_OnDamage_Thread( soul, damageInfo )
}

bool function ShouldReduceDamageForSegmentedHealth( entity soul, damageInfo )
{
	if ( !soul.soul.rebooting )
		return false

	if ( IsRodeoDamageFromBatteryPack( soul, damageInfo ) )
		return false

	if ( DamageInfo_GetCustomDamageType( damageInfo ) & DF_DOOMED_HEALTH_LOSS )
		return false

	if ( DamageInfo_GetCustomDamageType( damageInfo ) & DF_DOOM_FATALITY )
		return false

	return true
}

function TitanSegmentedHealth_OnDamage_Thread( entity soul, damageInfo )
{
	soul.EndSignal( "OnTitanDeath" )
	soul.EndSignal( "OnDestroy" )
	soul.EndSignal( "Doomed" )

	entity titan = soul.GetTitan()

	vector damageOrigin = GetDamageOrigin( damageInfo, titan )
	float damageAmount = DamageInfo_GetDamage( damageInfo )
	entity attacker = DamageInfo_GetAttacker( damageInfo )
	int hitBox = DamageInfo_GetHitBox( damageInfo )

	int healthFloor = CalculateHealthFloorForDamage( soul, titan, damageInfo )

	bool skipDoom = ( DamageInfo_GetCustomDamageType( damageInfo ) & DF_SKIPS_DOOMED_STATE ) > 0


	WaitEndFrame()

	titan = soul.GetTitan()
	Assert( IsValid( titan ) )

	if ( soul.soul.lastSegmentLossTime >= Time() )
		return

	if ( GetDoomedState( titan ) )
		return

	if ( titan.GetHealth() > healthFloor )
		return

	if ( !IsAlive( titan ) )
		return

	string settings = GetSoulPlayerSettings( soul )
	if ( Dev_GetPlayerSettingByKeyField_Global( settings, "use_damage_states" ) == 1 )
		UpdateDamageStateForTab( titan, GetTitanCurrentRegenTab( titan ), hitBox )

	thread TitanLoseSegement( soul, titan, damageOrigin, damageAmount, attacker )
}

int function CalculateHealthFloorForDamage( entity soul, entity titan, damageInfo )
{
	//Lets you bypass the health segment limitation and remove an entire health segment.
	/*if ( IsRodeoDamageFromBatteryPack( soul, damageInfo ) )
		return minint( 0, int ( titan.GetHealth() - DamageInfo_GetDamage( damageInfo ) ) )
	*/
	int oldTab = GetTitanCurrentRegenTab( titan )
	return ( oldTab - 1 ) * GetSegmentHealthForTitan( titan )
}

void function TitanLoseSegement( entity soul, entity titan, vector damageOrigin, float damageAmount, entity attacker )
{
	if ( !IsValid( soul ) )
		return

	if ( !IsValid( titan ) )
		return

	if ( soul.soul.lastSegmentLossTime >= Time() )
		return

	soul.soul.lastSegmentLossTime = Time()

	entity player
	if ( titan.IsPlayer() )
		player = titan

	foreach ( callbackFunc in svGlobal.onTitanHealthSegmentLostCallbacks )
	{
		callbackFunc( titan, attacker )
	}

	// Added via AddTitanCallback_OnHealthSegmentLost
	foreach ( callbackFunc in titan.e.entSegmentLostCallbacks )
	{
		callbackFunc( titan, attacker )
	}

	GiveDefenderAmmo( titan )

	titan.Signal( "HealthSegmentLost" )

	soul.EndSignal( "OnTitanDeath" )
	soul.EndSignal( "OnDestroy" )

	if ( GetCurrentPlaylistVarInt( "titan_health_chicklet_fx", 0 ) == 1 )
		TitanLoseSegementFX( titan, attacker, damageOrigin )

	SetSoulBatteryCount( soul, GetTitanCurrentRegenTab( titan ) )
}

void function TitanLoseSegementFX( entity titan, entity attacker, vector damageOrigin )
{
	int handle = titan.GetEncodedEHandle()
	int handleAttacker = -1

	if ( IsValid( attacker ) )
		handleAttacker = attacker.GetEncodedEHandle()

	array<entity> players = GetPlayerArray()
	foreach ( player in players )
	{
		Remote_CallFunction_Replay( player, "ServerCallback_TitanLostHealthSegment", handle, handleAttacker, damageOrigin.x, damageOrigin.y, damageOrigin.z )
	}

	if ( !IsAlive( titan ) )
		return

	int currentRegenTab = minint( GetTitanCurrentRegenTab( titan ), SEGMENT_DOWN_SOUNDS_3P_ATTACKER.len()-1 )
	if ( currentRegenTab < SEGMENT_DOWN_SOUNDS_3P_ATTACKER.len() )
	{
		if ( titan.IsPlayer() && IsAlive( attacker ) && attacker.IsPlayer() )
		{
			EmitSoundOnEntityOnlyToPlayer( titan, attacker, SEGMENT_DOWN_SOUNDS_3P_ATTACKER[ currentRegenTab ] )
			EmitSoundOnEntityOnlyToPlayer( titan, titan, SEGMENT_DOWN_SOUNDS_1P[ currentRegenTab ] )

			// need a command here to play for not victim and not attacker
			EmitSoundOnEntityExceptToPlayer( titan, titan, SEGMENT_DOWN_SOUNDS_3P[ currentRegenTab ] )
		}
		else if ( IsAlive( attacker ) && attacker.IsPlayer() )
		{
			EmitSoundOnEntityOnlyToPlayer( titan, attacker, SEGMENT_DOWN_SOUNDS_3P_ATTACKER[ currentRegenTab ] )
			EmitSoundOnEntityExceptToPlayer( titan, attacker, SEGMENT_DOWN_SOUNDS_3P[ currentRegenTab ] )
		}
		else if ( titan.IsPlayer() )
		{
			EmitSoundOnEntityOnlyToPlayer( titan, titan, SEGMENT_DOWN_SOUNDS_1P[ currentRegenTab ] )
			EmitSoundOnEntityExceptToPlayer( titan, titan, SEGMENT_DOWN_SOUNDS_3P[ currentRegenTab ] )
		}
		else
		{
			EmitSoundOnEntity( titan, SEGMENT_DOWN_SOUNDS_3P[ currentRegenTab ] )
		}
	}
}


void function UpdateDamageStateForTab( entity titan, int tab, int hitBox )
{
	if ( hitBox == -1 ) // not every hitbox has data defined
		return

	var bodyGroup = titan.GetBodyGroupNameFromHitboxId( hitBox ) // can be null

	if ( bodyGroup == null )
	{
		printt( "bodyGroup was null" )
		return
	}

#if MP
	// these are flipped on purpose to prevent both legs or arms from being blown up
	switch ( bodyGroup )
	{
		case "left_leg":
			if ( IsBroken( titan, "right_leg" ) )
				return
			break

		case "right_leg":
			if ( IsBroken( titan, "left_leg" ) )
				return
			break

		case "left_arm":
			if ( IsBroken( titan, "right_arm" ) )
				return
			break

		case "right_arm":
			if ( IsBroken( titan, "left_arm" ) )
				return
			break

		default:
			return
	}

	GibBodyPart( titan, bodyGroup )
#else
	int maxTab = 3
	int count = maxTab - tab

	RecursiveGibBodyPart( titan, bodyGroup, count )
#endif
}

void function RecursiveGibBodyPart( entity titan, var bodyGroup, int count )
{
	GibBodyPart( titan, bodyGroup )

	count -= 1
	if ( count <= 0 )
		return

	foreach ( siblingName in titan.s.skeletonData[bodyGroup].siblings )
	{
		// printt( count + " recurse: " + siblingName )
		RecursiveGibBodyPart( titan, siblingName, count )
	}
}

bool function IsBroken( entity titan, var bodyGroup )
{
	local bodyGroupIndex = titan.FindBodyGroup( bodyGroup )
	local stateCount = GetStateCountForBodyGroup( titan, bodyGroup )
	local bodyGroupState = titan.GetBodyGroupState( bodyGroupIndex )

	//return ( bodyGroupState >= (stateCount - 1) )
	return ( bodyGroupState > 0 )
}

void function GibBodyPart( entity titan, var bodyGroup )
{
	// if ( IsBodyGroupBroken( titan, bodyGroup ) )
	// 	return

	// titan.s.damageStateInfo[bodyGroup] = 1

	local bodyGroupIndex = titan.FindBodyGroup( bodyGroup )
	local stateCount = GetStateCountForBodyGroup( titan, bodyGroup )
	local bodyGroupState = titan.GetBodyGroupState( bodyGroupIndex )

	if ( bodyGroupState >= (stateCount - 1) )
		return

	titan.SetBodygroup( bodyGroupIndex, bodyGroupState + 1 )
	// printt( "break: " + bodyGroup )
}

function GiveAttackerAmmo( entity titan )
{
}

void function TemporaryInvul( entity titan )
{
	titan.EndSignal( "OnDestroy" )
	titan.EndSignal( "OnDeath" )
	if ( titan.IsPlayer() )
	{
		titan.EndSignal( "DisembarkingTitan" )
		titan.EndSignal( "TitanEjectionStarted" )
	}

	OnThreadEnd(
	function() : ( titan )
		{
			if ( IsValid( titan ) )
				titan.ClearInvulnerable()
		}
	)

	titan.SetInvulnerable()
	wait 0.25
}

void function GiveDefenderAmmo( entity titan )
{
	entity soul = titan.GetTitanSoul()

	if ( IsSingleplayer() )
	{
		if ( titan.IsNPC() )
		{
			soul.SetNextCoreChargeAvailable( soul.GetNextCoreChargeAvailable() + 0.5 ) // shave time off core timer
		}
	}
}

void function OnTitanDoomed( entity titan, var damageInfo )
{

	if ( !IsAlive( titan ) )
		return

	entity soul = titan.GetTitanSoul()

	if ( titan.IsPlayer() )
	{
		if ( SoulHasPassive( soul, ePassives.PAS_RONIN_AUTOSHIFT ) )
			PhaseShift( titan, 0, 3.0 )

 		if ( SoulHasPassive( soul, ePassives.PAS_AUTO_EJECT ) )
			return
	}

	soul.nextHealthRegenTime = Time()

	vector damageOrigin = GetDamageOrigin( damageInfo, titan )
	float damageAmount = DamageInfo_GetDamage( damageInfo )
	entity attacker = DamageInfo_GetAttacker( damageInfo )

	if ( TitanDamageRewardsTitanCoreTime() && (titan != attacker) )
	{
		AddCreditToTitanCoreBuilderForDoomEntered( titan )
		if ( attacker.IsTitan() )
			AddCreditToTitanCoreBuilderForDoomInflicted( attacker )
	}

	thread TitanLoseSegement( soul, titan, damageOrigin, damageAmount, attacker )

	if ( SoulHasPassive( soul, ePassives.PAS_DOOMED_TIME ) )
		return

	if ( NoWeaponDoomState() )
		TakeAllWeapons( titan )
}

void function OnTitanDeath( entity titan, var damageInfo )
{
	if ( !titan.IsTitan() )
		return

	if ( !PROTO_AlternateDoomedState() )
		return

	entity soul = titan.GetTitanSoul()
	vector damageOrigin = GetDamageOrigin( damageInfo, titan )
	float damageAmount = DamageInfo_GetDamage( damageInfo )
	entity attacker = DamageInfo_GetAttacker( damageInfo )

	thread TitanLoseSegement( soul, titan, damageOrigin, damageAmount, attacker )
}

void function TitanShieldDecayThink( entity soul )
{
	thread TitanShieldDecayThinkInternal( soul )
}

void function TitanShieldDecayThinkInternal( entity soul )
{
	soul.EndSignal( "OnDestroy" ) //This needs to be OnDestroy instead of OnDeath because souls don't have a death animation
	soul.EndSignal( "OnTitanDeath" )

	while ( 1 )
	{
		if ( Time() >= soul.e.nextShieldDecayTime && !TitanHasRegenningShield( soul ) )
			soul.SetShieldHealth( maxint( soul.GetShieldHealth() - file.shieldDecayRate, 0 ) )
		WaitFrame()
	}
}