aboutsummaryrefslogtreecommitdiff
path: root/Northstar.CustomServers/mod/scripts/vscripts/mp/_revive.gnut
blob: b2f5c467431230ca9167063a49b3d6689517b64a (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
untyped


global function Revive_Init

global function PlayerRevivesOrBleedsOut
global function DeathPackage_PlayerRevive
global function ShouldRevivePlayer

const float REVIVE_BLEED_OUT_TIME		= 15.0
global const float REVIVE_DEATH_TIME	= 2.0
const float REVIVE_DIST_OUTER 			= 135.0
const float REVIVE_DIST_INNER 			= 120.0

struct
{
	table fakePlayers
} file

function Revive_Init()
{
	if ( !ReviveEnabled() )
		return

	RegisterSignal( "KillReviveNag" )
	RegisterSignal( "DoneBleedingOut" )
	RegisterSignal( "ReviveSucceeded" )
	RegisterSignal( "ReviveFailed" )
	RegisterSignal( "ForceBleedOut" )

	AddCallback_OnClientDisconnected( ReviveOnClientDisconnect )
}

void function PlayerRevivesOrBleedsOut( entity player )
{
	player.EndSignal( "OnDestroy" )
	player.EndSignal( "ForceBleedOut" )
	svGlobal.levelEnt.EndSignal( "RoundEnd" )

	table e = { revived = false }
	//thread PlayerReviveVONag( player, 0.5 )

	OnThreadEnd(
		function() : ( player, e )
		{
			if ( !IsValid( player ) )
				return

			player.Signal( "KillReviveNag" )
			player.Signal( "DoneBleedingOut" )
			player.nv.reviveBleedingOut = -1.0 //-1 means off

			if ( e.revived )
			{
				player.Signal( "ReviveSucceeded")
				thread PlayerStandsBackUp( player )
			}
			else
			{
				file.fakePlayers[ player ].Destroy()
				player.Signal( "ReviveFailed" )
				DecideRespawnPlayer( player )
			}
		}
	)

	wait( REVIVE_DEATH_TIME )
	player.StartObserverMode( OBS_MODE_DEATHCAM )

	ForceRespawnIfEntireTeamIsDead( player )

	float endTime = Time() + REVIVE_BLEED_OUT_TIME
	player.nv.reviveBleedingOut = endTime

	bool reviving = false
	float doneReviveTime = Time() + 100

	float distOuterSqr = pow( REVIVE_DIST_OUTER, 2 )
	float distInnerSqr = pow( REVIVE_DIST_INNER, 2 )

	while ( true )
	{
		array<entity> healers = Revive_GetAvailablePlayerHealers( player )

		//we were reviving but aren't anymore - set revive to false.
		if ( reviving && !FriendlyIsReviving( healers, player, distOuterSqr ) )
		{
			//thread PlayerReviveVONag( player )
			reviving = false
			player.nv.reviveHealedTime = -1.0 //-1 means off
		}
		//we were not reviving but now we are? set the new revive done time.
		else if ( !reviving && FriendlyIsReviving( healers, player, distInnerSqr ) )
		{
			player.Signal( "KillReviveNag" )
			doneReviveTime = Time() + REVIVE_TIME_TO_REVIVE
			player.nv.reviveHealedTime = doneReviveTime
			reviving = true
		}

		//are we done reviving? then set the value and return
		if ( reviving && Time() > doneReviveTime )
		{
			e.revived = true
			return
		}

		//we didn't make it
		if ( !reviving && Time() > endTime )
			return

		wait 0.2
	}
}

void function ForceRespawnIfEntireTeamIsDead( entity player )
{
	int playerTeam = player.GetTeam()
	array<entity> victimTeamMembers = GetPlayerArrayOfTeam( playerTeam )
	foreach ( member in victimTeamMembers )
	{
		if ( member.p.isReviving || IsAlive( member ) )
			return
	}
	foreach ( member in victimTeamMembers )
	{
		if ( player != member && member.p.isReviving == false )
			member.Signal( "ForceBleedOut" )
	}
	MessageToTeam( GetOtherTeam( playerTeam ), eEventNotifications.EnemyTeamEliminated )
	player.Signal( "ForceBleedOut" )
}

void function PlayerReviveVONag( entity player, float delay = 0.5 )
{
	player.EndSignal( "OnDestroy" )
	player.EndSignal( "KillReviveNag" )

	OnThreadEnd(
		function() : ( player )
		{
			if ( IsValid( player ) )
				StopSoundOnEntity( player, "diag_coop_bleedout_help" )
		}
	)

	if ( delay > 0 )
		wait delay

	while ( true )
	{
		float time = EmitSoundOnEntity( player, "diag_coop_bleedout_help" )
		wait time

		wait RandomFloatRange( 10, 15 )
	}
}

bool function FriendlyIsReviving( array<entity> healers, entity player, float distSqr )
{
	vector origin = player.GetOrigin()

	foreach ( friend in healers )
	{
		if ( !IsAlive( friend ) )
			continue

		if ( DistanceSqr( friend.GetOrigin(), origin ) < distSqr )
			return true
	}

	return false
}

array<entity> function Revive_GetAvailablePlayerHealers( entity player )
{
	int team = player.GetTeam()
	array<entity> players = GetPlayerArrayOfTeam( team )
	array<entity> playersCanRevive = []
	foreach ( player in players )
	{
		if ( !IsAlive( player ) )
			continue

		playersCanRevive.append( player )
	}

	return playersCanRevive
}

bool function ShouldRevivePlayer( entity player, var damageInfo )
{
	if ( !ReviveEnabled() )
		return false

	if ( !GamePlaying() )
		return false

	if ( player.ContextAction_IsMeleeExecution() )
		return false

	if ( player.IsTitan() )
		return false

	int source = DamageInfo_GetDamageSourceIdentifier( damageInfo )

	if ( source == eDamageSourceId.fall ||
		 source == eDamageSourceId.submerged ||
		 source == eDamageSourceId.outOfBounds ||
		 source == eDamageSourceId.indoor_inferno )
		return false

	return true
}

entity function SpawnFakePlayer( entity player, int team, vector origin, vector angles, asset weaponModel, asset model )
{
	float fadeDist 	= 10000.0
	int solidType = 0// 0 = no collision, 2 = bounding box, 6 = use vPhysics, 8 = hitboxes only

	entity fakePlayer = CreatePropDynamic( model, origin, angles, solidType, fadeDist )
	if ( !( player in file.fakePlayers ) )
	{
		file.fakePlayers[ player ] <- null
	}
	file.fakePlayers[ player ] = fakePlayer

	thread FakePlayerTrack( fakePlayer, player )

	if ( weaponModel != $"" )
	{
		entity gun = CreatePropDynamic( weaponModel, origin, angles, 0, fadeDist )
		gun.SetParent( fakePlayer, "PROPGUN" )
	}

	return fakePlayer
}

void function FakePlayerTrack( entity fakePlayer, entity player )
{
	fakePlayer.EndSignal( "OnDestroy" )
	player.EndSignal( "OnDestroy" )
	vector lastPlayerOrg = Vector( 0.0, 0.0, 0.0 )

	while ( true )
	{
		if ( player.GetOrigin() == lastPlayerOrg )
			player.SetVelocity( Vector( 0.0, 0.0, 0.0 ) )
		lastPlayerOrg = player.GetOrigin()

		fakePlayer.SetOrigin( player.GetOrigin() )
		WaitFrame()
	}
}

void function DeathPackage_PlayerRevive( entity player )
{
	player.kv.VisibilityFlags = ENTITY_VISIBLE_TO_NOBODY

	vector deathOrg = player.GetOrigin()

	vector mins = Vector( -16.0, -16.0, 0.0 )
	vector maxs = Vector( 16.0, 16.0, 72.0 )
	TraceResults result = TraceHull( deathOrg + Vector( 0.0, 0.0, 8.0 ), deathOrg + Vector( 0.0, 0.0, -16000.0 ), mins, maxs, player, ( TRACE_MASK_PLAYERSOLID_BRUSHONLY | TRACE_MASK_BLOCKLOS ), TRACE_COLLISION_GROUP_NONE )

	player.SetVelocity( Vector( 0.0, 0.0, 0.0 ) )
	thread ReviveLerpToOrigin( player, deathOrg, result.endPos )

	entity activeWeapon = player.GetActiveWeapon()

	asset weaponModel = activeWeapon == null ? $"" : activeWeapon.GetModelName()

	entity fakePlayer = SpawnFakePlayer( player, player.GetTeam(), deathOrg, player.GetAngles(), weaponModel, player.GetModelName() )
	fakePlayer.Anim_Play( "pt_wounded_drag_zinger_A_idle" )
	player.Anim_Play( "pt_wounded_drag_zinger_A_idle" )
}

void function ReviveLerpToOrigin( entity player, vector deathOrg, vector endPos )
{
	player.EndSignal( "DoneBleedingOut" )
	player.EndSignal( "OnDestroy" )

	entity mover = CreateScriptMover()

	OnThreadEnd(
		function() : ( player, mover )
		{
			if ( IsValid( player ) )
				player.ClearParent()

			if ( IsValid( mover ) )
				mover.Destroy()
		}
	)

	mover.SetOrigin( deathOrg )
	player.SetOrigin( deathOrg )
	player.SetParent( mover )

	float moveTime = GraphCapped( deathOrg.z - endPos.z, 0.0, 768.0, 0.1, 2.0 )

	mover.NonPhysicsMoveTo( endPos, moveTime, moveTime, 0.0 )
	wait( moveTime )
	player.ClearParent()

	while ( true )
	{
		player.SetOrigin( endPos )
		WaitFrame()
	}
}

void function PlayerStandsBackUp( entity player )
{
	player.EndSignal( "OnDestroy" )
	svGlobal.levelEnt.EndSignal( "RoundEnd" )

	entity fakePlayer = expect entity( file.fakePlayers[ player ] )
	file.fakePlayers[ player ] = null

	player.p.isReviving = true

	OnThreadEnd(
		function() : ( player, fakePlayer )
		{
			if ( IsValid( fakePlayer ) )
				fakePlayer.Destroy()

			if ( IsValid( player ) )
				player.p.isReviving = false
		}
	)

	fakePlayer.Anim_Play( "CQB_knockback_pain_react" )
	fakePlayer.Anim_SetInitialTime( 2.0 )
	wait( 1.5 )

	var settings = player.GetPlayerSettings()
	player.SetPlayerSettings( "spectator" )
	player.SetPlayerSettings( settings )
	player.RespawnPlayer( null )
}

void function ReviveOnClientDisconnect( entity player )
{
	if ( player in file.fakePlayers )
	{
		if ( IsValid( file.fakePlayers[ player ] ) )
			file.fakePlayers[ player ].Destroy()
		delete file.fakePlayers[ player ]
	}
}