aboutsummaryrefslogtreecommitdiff
path: root/Northstar.Custom/mod/scripts/vscripts/melee/_melee_synced_human.gnut
blob: 068ffcfd00f20785ced4c9fc0838cfb1a072910d (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
untyped

global function MeleeThread_PilotVsEnemy
global function MeleeSyncedServer_Init

void function MeleeSyncedServer_Init()
{
	RegisterSignal( "NpcDealsExecutionDamage" )
}

bool function MeleeThread_PilotVsEnemy( SyncedMelee action, entity attacker, entity target )
{
	// function off for reload scripts
	return MeleeThread_PilotVsEnemyInternal( action, attacker, target )
}

bool function MeleeThread_PilotVsEnemyInternal( SyncedMelee action, entity attacker, entity target )
{
	Assert( IsHumanSized( target ), target + " is not human sized melee target" )
	Assert( attacker.IsPlayer() && IsHumanSized( attacker ), attacker + " is not human sized player attacker" )
	Assert( IsAlive( attacker ) )
	Assert( IsAlive( target ) )

	bool isAttackerRef = IsAttackerRef( action, target )

	vector attackerOrigin = attacker.GetOrigin()
	vector targetOrigin = target.GetOrigin()

	attacker.EndSignal( "OnDestroy" )
	target.EndSignal( "OnDestroy" )

	if ( IsSingleplayer() )
	{
		if ( attacker.IsPlayer() )
		{
			if ( IsCloaked( attacker ) )
			{
				UnlockAchievement( attacker, achievements.CLOAK_TAKEDOWN )
			}
		}
	}

	OnThreadEnd(
		function() : ( attacker, target, attackerOrigin, targetOrigin, action, isAttackerRef )
		{
			if ( IsValid( attacker ) )
				attacker.ClearParent()

			if ( IsValid( target ) )
				target.ClearParent()


			if ( IsValid( attacker ) )
			{
				attacker.PlayerMelee_SetState( PLAYER_MELEE_STATE_NONE )
			}

			// Note that the original attacker/target origins are not guarranteed to be a safe spot now because we have moving geo in the game.
			// Whoever is the 'ref' will be in a safe position though, so we can always use the origin of the person who has been designated as the 'ref'.
			if ( IsAlive( attacker ) )
			{
				if ( !isAttackerRef && IsValid( target ) )
				{
					PutEntityInSafeSpot( attacker, target, null, target.GetOrigin(), attacker.GetOrigin() )
				}
				else
				{
					PutEntityInSafeSpot( attacker, target, null, attacker.GetOrigin(), attacker.GetOrigin() )
				}

			}

			if ( IsValid( target ) )
			{
				target.ClearParent()

				if ( IsAlive( target ) )
				{
					// Note that the original target origin is not guarranteed to be a safe spot now because we have moving geo in the game now.
					if ( isAttackerRef && IsValid( attacker ) )
					{

						PutEntityInSafeSpot( target, attacker, null, attacker.GetOrigin(), target.GetOrigin() )
					}
					else
					{
						PutEntityInSafeSpot( target, attacker, null, target.GetOrigin(), target.GetOrigin() )
					}
				}
			}
		}
	)

	thread MeleeThread_PilotVsEnemy_Attacker( action, attacker, target, isAttackerRef )
	// target's sequence is longer
	waitthread MeleeThread_PilotVsEnemy_Target( action, attacker, target, isAttackerRef )

	attacker.Signal( "SyncedMeleeComplete" )
	return true
}

struct PilotVsEnemyStruct
{
	bool clearInvulnerable = false
	bool wasCloaked = false
	float cloakEndTime = 0.0
}

void function DisableCloakBeforeMelee( entity player, PilotVsEnemyStruct dataStruct )
{
	if ( IsCloaked( player ) )
	{
		dataStruct.wasCloaked = true
		dataStruct.cloakEndTime = player.GetCloakEndTime()
		DisableCloak( player, 0.0 )
	}
}

void function RestoreCloakAfterMelee( entity player, PilotVsEnemyStruct dataStruct )
{
	if ( !IsAlive( player ) )
		return

	if ( !dataStruct.wasCloaked )
		return

	float remainingCloakDuration = max( 0.0, dataStruct.cloakEndTime - Time() )
	if ( remainingCloakDuration > CLOAK_FADE_IN  ) //Has to be higher than fade in duration, otherwise will cloak forever
		EnableCloak( player, remainingCloakDuration, CLOAK_FADE_IN )
}

void function HandleCloakExecutionWithCloakedAttacker( entity player, PilotVsEnemyStruct dataStruct, SyncedMelee action  )
{
	if ( !IsCloaked( player ) )
		return //No need to run DisableCloakBeforeMelee() either

	float attackerSequenceEndTime = Time() + player.GetSequenceDuration( action.attackerAnimation3p )
	float scheduledCloakEndTime = player.GetCloakEndTime()

	//printt( "attackerSequenceEndTime: " + attackerSequenceEndTime + ", scheduledCloakEndTime: " + scheduledCloakEndTime )

	if ( scheduledCloakEndTime > attackerSequenceEndTime )
	{
		//printt( "Cloak ability lasts longer than execution sequence, just doing DisableCloakBeforeMelee" )
		player.SetCloakFlicker( 0.0, 0.0 ) //Turn off flicker; this is normally not a problem for other executions since cloak is turned off for the entirety of those executions
		DisableCloakBeforeMelee( player, dataStruct )
	}
	else
	{
		//Cloak would normally run out during the animation of this execution, which is disruptive to the presentation of cloak animation, so just stop cloak now for good and prevent it from coming back.
		//printt( "Cloak ability is shorter than execution sequence, DisableCloak now and stop it from coming back" )
		dataStruct.wasCloaked = true //Have to do this to mark player was cloaked during start of execution, so we can track the stat correctly
		dataStruct.cloakEndTime = Time()
		DisableCloak( player, 0.0 )
		player.Signal( "KillHandleCloakEnd" )
	}
}


void function MeleeThread_PilotVsEnemy_Attacker( SyncedMelee action, entity attacker, entity target, bool isAttackerRef )
{
	attacker.EndSignal( "OnAnimationDone" )
	attacker.EndSignal( "OnAnimationInterrupted" )
	attacker.EndSignal( "OnDeath" )
	attacker.EndSignal( "ScriptAnimStop" )

	attacker.EndSignal( "OnDestroy" )
	Assert( IsValid( target ) )
	target.EndSignal( "OnDestroy" )


	foreach ( AnimEventData animEventData in action.attacker3pAnimEvents )
	{
		AddAnimEvent( attacker, animEventData.eventName, animEventData.callback, animEventData.optionalVar )
	}
	AddAnimEvent( attacker, "synced_melee_enable_planting", EnablePlantingOnEntity )

	PilotVsEnemyStruct dataStruct
	OnThreadEnd(
		function() : ( attacker, target, action, dataStruct )
		{
			if ( IsValid( attacker ) )
			{
				DeleteAnimEvent( attacker, "synced_melee_enable_planting" )

				if ( dataStruct.clearInvulnerable )
				{
					attacker.ClearInvulnerable()
				}

				if ( attacker.IsPlayer() )
				{
					attacker.PlayerMelee_ExecutionEndAttacker()
					ClearPlayerAnimViewEntity( attacker )
					DeployAndEnableWeapons( attacker )

					RestoreCloakAfterMelee( attacker, dataStruct )
					#if MP
					IncrementStatForPilotExecutionWhileCloaked( attacker, target, dataStruct )
					#endif
				}

				foreach ( AnimEventData animEventData in action.attacker3pAnimEvents )
				{
					DeleteAnimEvent( attacker, animEventData.eventName )
				}
			}

			if ( !IsAlive( attacker ) )
				attacker.Anim_Stop()
		}
	)

	FirstPersonSequenceStruct attackerSequence
	attackerSequence.blendTime = 0.4
	attackerSequence.attachment = "ref"
	attackerSequence.thirdPersonAnim = action.attackerAnimation3p
	attackerSequence.firstPersonAnim = action.attackerAnimation1p
	attackerSequence.thirdPersonCameraAttachments = [action.thirdPersonCameraAttachment]
	attackerSequence.thirdPersonCameraVisibilityChecks = true

	if ( isAttackerRef )
	{
		attackerSequence.noParent = true
		attackerSequence.playerPushable = true
		attackerSequence.enablePlanting = true
	}
	else
	{
		attackerSequence.useAnimatedRefAttachment = true
	}

	float duration = attacker.GetSequenceDuration( attackerSequence.thirdPersonAnim )

	if ( attacker.IsPlayer() )
	{
		float executionEndTime = Time() + duration
		attacker.PlayerMelee_ExecutionStartAttacker( executionEndTime )
		attacker.Lunge_ClearTarget()
		HolsterViewModelAndDisableWeapons( attacker )

		if ( action.ref == "execution_cloak" ) //Special case for cloak execution
		{
			HandleCloakExecutionWithCloakedAttacker( attacker, dataStruct, action )
		}
		else
		{
			DisableCloakBeforeMelee( attacker, dataStruct )
		}

		if ( IsSingleplayer() )
		{
			dataStruct.clearInvulnerable = true
			attacker.SetInvulnerable()
			thread LowerEnemyAccuracy( attacker, duration )
		}
	}

	if ( isAttackerRef )
	{
		if ( GetCurrentPlaylistVarInt( "fp_embark_enabled", 0 ) == 1 )
			FirstPersonSequenceForce1P( attackerSequence, attacker )
		thread FirstPersonSequence( attackerSequence, attacker )
	}
	else
	{
		if ( GetCurrentPlaylistVarInt( "fp_embark_enabled", 0 ) == 1 )
			FirstPersonSequenceForce1P( attackerSequence, attacker, target )
		thread FirstPersonSequence( attackerSequence, attacker, target )
	}

	wait duration
}


void function MeleeThread_PilotVsEnemy_Target( SyncedMelee action, entity attacker, entity target, bool isAttackerRef )
{
	attacker.EndSignal( "OnAnimationDone" )
	attacker.EndSignal( "OnAnimationInterrupted" )
	attacker.EndSignal( "OnDeath" )
	attacker.EndSignal( "ScriptAnimStop" )

	attacker.EndSignal( "OnDestroy" )
	Assert( IsValid( target ) )
	target.EndSignal( "OnDestroy" )

	foreach ( AnimEventData animEventData in action.target3pAnimEvents )
	{
		AddAnimEvent( target, animEventData.eventName, animEventData.callback, animEventData.optionalVar )
	}
	AddAnimEvent( target, "synced_melee_enable_planting", EnablePlantingOnEntity )

	PilotVsEnemyStruct dataStruct

	OnThreadEnd(
		function() : ( attacker, target, action, dataStruct )
		{
			if ( IsValid( target ) )
			{
				if ( target.IsNPC() && IsMultiplayer() )
				{
					SetForceDrawWhileParented( target, false )
				}

				TargetClearedExecuted( target )
				DeleteAnimEvent( target, "mark_for_death" )
				DeleteAnimEvent( target, "phase_gib" )

				foreach ( AnimEventData animEventData in action.target3pAnimEvents )
				{
					DeleteAnimEvent( target, animEventData.eventName )
				}
				DeleteAnimEvent( target, "synced_melee_enable_planting" )

				bool isAlive = IsAlive( target )

				if ( target.IsPlayer() )
				{
					EnableOffhandWeapons( target )
					if ( isAlive )
						target.DeployWeapon()
				}

				if ( isAlive )
				{
					if ( target.e.markedForExecutionDeath ) //Kill off target if he already reached blackout part of melee
					{
						entity killCreditAttacker = null //If the attacker disconnected, we don't have a player to give credit to, that's fine. Script will not error
						if ( IsValid( target.e.syncedMeleeAttacker ) )
							killCreditAttacker = target.e.syncedMeleeAttacker
						//printt( "Killing off target "  + target + " because he already reached blackout part of execution!" )

						int damageAmount = target.GetMaxHealth() + 1
						target.TakeDamage( damageAmount, killCreditAttacker, killCreditAttacker, { forceKill = true, damageType = DMG_MELEE_EXECUTION, damageSourceId = eDamageSourceId.human_execution } )
						//markedForExecutionDeath will be cleared in MarkForDeath() which sets it in the first place
					}

					if ( target.IsPlayer() )
						RestoreCloakAfterMelee( target, dataStruct )
				}

				if ( IsValid( target.e.syncedMeleeAttacker ) )
				{
					if ( IsValid( target.e.lastSyncedMeleeAttacker ) )
					{
						target.e.lastSyncedMeleeAttacker = null
					}

					target.e.lastSyncedMeleeAttacker = target.e.syncedMeleeAttacker
					target.e.syncedMeleeAttacker = null
				}
			}
		}
	)

	TargetSetExecutedBy( target, attacker )

	AddAnimEvent( target, "mark_for_death", MarkForDeath )
	AddAnimEvent( target, "phase_gib", PhaseGib )

	FirstPersonSequenceStruct targetSequence
	targetSequence.blendTime = 0.25
	targetSequence.attachment = "ref"
	targetSequence.thirdPersonAnim = action.targetAnimation3p
	targetSequence.thirdPersonCameraAttachments = [action.thirdPersonCameraAttachment]
	targetSequence.thirdPersonCameraVisibilityChecks = true

	if ( isAttackerRef )
	{
		targetSequence.useAnimatedRefAttachment = true
		if ( target.IsNPC() && IsMultiplayer() )
		{
			SetForceDrawWhileParented( target, true )
		}
	}
	else
	{
		targetSequence.noParent = true
		targetSequence.playerPushable = true
		targetSequence.enablePlanting = true
	}


	if ( target.IsPlayer() )
	{
		HolsterViewModelAndDisableWeapons( target )
		targetSequence.firstPersonAnim = action.targetAnimation1p
		DisableCloakBeforeMelee( target, dataStruct )
	}

	if ( attacker.IsPlayer() )
	{
		if ( MeleeTargetrequiresDataKnife( target ) )
		{
			string tag = GetTagForKnifeMeleeTarget( target )
			thread AttachPlayerModelForDuration( attacker, DATA_KNIFE_MODEL, tag, 2.2 )
		}
		else if ( action.attachTag1p != "" && action.attachModel1p != $"" )
		{
			thread AttachPlayerModelForDuration( attacker, action.attachModel1p, action.attachTag1p, 2.2 )
		}
	}

	if ( isAttackerRef )
	{
		if ( GetCurrentPlaylistVarInt( "fp_embark_enabled", 0 ) == 1 )
			FirstPersonSequenceForce1P( targetSequence, target, attacker )
		
		waitthread FirstPersonSequence( targetSequence, target, attacker )
	}
	else
	{
		if ( GetCurrentPlaylistVarInt( "fp_embark_enabled", 0 ) == 1 )
			FirstPersonSequenceForce1P( targetSequence, target )
		
		waitthread FirstPersonSequence( targetSequence, target )
	}
}

#if MP
void function IncrementStatForPilotExecutionWhileCloaked( entity attacker, entity target, PilotVsEnemyStruct dataStruct )
{
	if ( !IsAlive( attacker ) )
		return

	if ( IsAlive( target ) )
		return

	if ( !target.IsPlayer() )
		return

	if ( !dataStruct.wasCloaked )
		return

	IncrementPlayerDidPilotExecutionWhileCloaked( attacker ) //Kinda clumsy we have to do it here instead of where all the other kill stats are incremented. Mainly because we turn cloak off at the start of execution so you can't do it where all the other kill stats are incremented
}
#endif

void function TargetClearedExecuted( entity target )
{
	target.ClearParent()
	target.Solid()
	if ( target.ContextAction_IsMeleeExecution() )
		target.PlayerMelee_ExecutionEndTarget()
	if ( target.IsPlayer() )
		ClearPlayerAnimViewEntity( target )
}

void function TargetSetExecutedBy( entity target, entity attacker )
{
	//Break out of context actions like hacking control panel etc
	if ( target.ContextAction_IsActive() )
		target.Anim_Stop()

	target.PlayerMelee_ExecutionStartTarget( attacker )
	target.e.syncedMeleeAttacker = attacker
	target.NotSolid()
}

bool function MeleeTargetrequiresDataKnife( entity target )
{
	if ( IsProwler( target ) )
		return true

	if ( IsPilotElite( target ) )
		return true

	return false
}

string function GetTagForKnifeMeleeTarget( entity target )
{
	Assert( MeleeTargetrequiresDataKnife( target ) )

	if ( IsProwler( target ) )
		return "PROPGUN"

	if ( IsPilotElite( target ) )
		return "KNIFE"

	unreachable
}

function AttachPlayerModelForDuration( var player, asset modelName, var tag, var time )
{
	expect entity( player )

	if ( !IsValid( player ) )
		return

	Assert( IsValid( tag ), "No tag specified for player" )

	entity viewModel = player.GetFirstPersonProxy() //JFS: Defensive fix for player not having view models sometimes
	if ( !IsValid( viewModel ) )
		return

	if ( !EntHasModelSet( viewModel ) )
		return

	entity model = CreatePropDynamic( modelName )
	model.SetParent( viewModel, tag, false, 0.0 )

	OnThreadEnd(
		function() : ( model )
		{
			if ( IsValid( model ) )
				model.Destroy()
		}
	)

	player.EndSignal( "OnDeath" )

	wait time
}

void function MarkForDeath( entity target )
{
	if ( target.IsNPC() )
	{
		//printt("Killing marked for death npc " + target )
		//Just kill off NPC now, otherwise it will play pain animations on death
		CodeCallback_OnMeleeKilled( target )
		return
	}

	//printt("marking player " + target + " for death")
	target.e.markedForExecutionDeath = true //This will kill off the player even if the execution animation is interruped from this point forward

	target.EndSignal( "OnDeath" )

	OnThreadEnd(
	function() : ( target )
		{
			target.e.markedForExecutionDeath = false
		}
	)

	WaitForever()

}

void function PhaseGib( entity target )
{
	if ( !IsAlive( target ) )
		return

	target.ClearInvulnerable()

	entity attacker
	if ( IsValid( target.e.syncedMeleeAttacker ) )
	{
		attacker = target.e.syncedMeleeAttacker
	}
	else if ( IsValid( target.e.lastSyncedMeleeAttacker ) )
	{
		attacker = target.e.lastSyncedMeleeAttacker
	}
	else
	{
		attacker = null
	}

	int damageAmount = target.GetMaxHealth() + 1
	target.TakeDamage( damageAmount , attacker, attacker, { forceKill = true, damageType = DMG_MELEE_EXECUTION, damageSourceId = eDamageSourceId.human_execution, scriptType = DF_NO_INDICATOR | DF_GIB } )
}


entity function CreateSyncedMeleeRef( entity attacker, entity target, SyncedMelee action )
{
	entity ref = CreateMeleeScriptMoverBetweenEnts( attacker, target )

	vector angles = target.GetAngles()
	angles.x = ref.GetAngles().x

	ref.SetAngles( angles )
	if ( action.animRefPos == "attacker" )
		ref.SetOrigin( attacker.GetOrigin() )
	else
		ref.SetOrigin( target.GetOrigin() )
	return ref
}

void function ApplyGruntExecutionDamage( entity ref, entity attacker, entity target, float damageDealt )
{
	ref.EndSignal( "OnDestroy" )
	attacker.EndSignal( "OnDeath" )
	target.EndSignal( "OnDeath" )

	for ( ;; )
	{
		table results = attacker.WaitSignal( "NpcDealsExecutionDamage" )
		float damage
		switch ( results.parm )
		{
			case "lethal":
				damage = float( target.GetMaxHealth() )
				break

			case "nonlethal":
				damage = min( target.GetHealth() - 10, target.GetMaxHealth() * damageDealt )
				break
		}

		target.TakeDamage( damage, attacker, attacker, { damageSourceId=eDamageSourceId.human_execution, scriptType = DF_RAGDOLL } )
	}
}