aboutsummaryrefslogtreecommitdiff
path: root/Northstar.CustomServers/mod/scripts/vscripts/mp/_titan_tether.gnut
blob: b088651abe972c87273b1cf0d520e35ee6b0475a (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
global function TitanTether_Init
global function AddTitanTether
global function TetherFlyIn
global function PROTO_GetActiveTethers
global function CodeCallback_OnTetherRemove
global function CodeCallback_OnTetherDamageMilestone
global function AddOnTetherCallback

struct TetherData
{
	entity owner
	entity[2] endpointEnts
	array<entity> tetherEnts = []
	entity anchor
	entity endEntForPlayer
	entity endEntForOthers
	int teamNum
	int codeTetherID
}


struct
{
	array<TetherData> activeTitanTethers = []
	array< void functionref( entity, entity ) > onTetherCallbacks = []
} file

void function TitanTether_Init()
{
	PrecacheImpactEffectTable( "exp_tether_trap" ) //Needs to match damagedef_fd_tether_trap
}

void function AddOnTetherCallback( void functionref( entity, entity ) callback )
{
	file.onTetherCallbacks.append( callback )
}

void function AddTitanTether( entity owner, entity startEnt, entity endEnt, array<entity> tetherEnts, entity anchor, entity tetherEndEntForPlayer, entity tetherEndEntForOthers, bool isExplosiveTether )
{
	//Run callbacks for tether trap activation.
	foreach ( callback in file.onTetherCallbacks )
	{
		callback( owner, endEnt )
	}

	TetherData tetherData
	tetherData.owner = owner

	tetherData.teamNum = owner.GetTeam()

	Assert( !startEnt.IsTitan() )
	Assert( endEnt.IsTitan()|| IsSuperSpectre( endEnt ) )

	if ( endEnt.IsTitan() || IsSuperSpectre( endEnt ) )
	{
		tetherData.codeTetherID = endEnt.AddTether( startEnt.GetOrigin() )
		if ( owner.IsPlayer() )
			EmitSoundOnEntityExceptToPlayer( startEnt, owner, "Wpn_TetherTrap_PopOpen_3p" )//Spring Sound
		else
			EmitSoundOnEntity( startEnt, "Wpn_TetherTrap_PopOpen_3p" )//Spring Sound

		if ( endEnt.IsTitan() )
			endEnt = endEnt.GetTitanSoul()
	}

	tetherData.endpointEnts[0] = startEnt
	tetherData.endpointEnts[1] = endEnt

	tetherData.tetherEnts = tetherEnts

	tetherData.anchor = anchor
	tetherData.endEntForPlayer = tetherEndEntForPlayer
	tetherData.endEntForOthers = tetherEndEntForOthers

	file.activeTitanTethers.append( tetherData )

	thread TetherCleanup( owner, startEnt, endEnt, tetherData, isExplosiveTether )
}

void function TetherCleanup( entity owner, entity startEnt, entity endEnt, TetherData tetherData, bool isExplosiveTether )
{
	startEnt.EndSignal( "OnDestroy" )
	endEnt.EndSignal( "OnDestroy" )
	endEnt.EndSignal( "OnSyncedMelee" )

	int tetherID = tetherData.codeTetherID
//	int statusEffectId = StatusEffect_AddEndless( endEnt, eStatusEffect.tethered, 1.0 )
	int statusEffectId = StatusEffect_AddTimed( endEnt, eStatusEffect.tethered, 1.0, 5.0, 0.0 )

	vector anchorOrigin = tetherData.anchor.GetOrigin()

	OnThreadEnd(
	function() : ( owner, anchorOrigin, endEnt, tetherID, statusEffectId, isExplosiveTether )
		{
			if ( isExplosiveTether && IsValid( owner ) && IsValid( endEnt ) )
			{
				Explosion_DamageDefSimple( damagedef_fd_tether_trap, anchorOrigin,owner, owner, anchorOrigin + < 0, 0, 32 > )
			}

			foreach ( index, tetherData in file.activeTitanTethers )
			{
				if ( tetherData.codeTetherID == tetherID )
				{
					thread TitanTether_Remove( tetherData )
					break
				}
			}

			if ( IsValid( endEnt ) )
				StatusEffect_Stop( endEnt, statusEffectId )
		}
	)

	WaitForever()
}

void function TetherFlyIn( entity flyFrom, entity flyTo, entity rope, entity owner )
{
	flyTo.EndSignal( "OnDestroy" )
	vector destLocal = flyTo.GetLocalOrigin()
	flyTo.SetAbsOrigin( flyFrom.GetOrigin() )
	flyTo.NonPhysicsMoveInWorldSpaceToLocalPos( destLocal, 0.3, 0, 0 )
	wait 0.3
	if ( IsValid( owner ) && owner.IsPlayer() )
	{
		EmitSoundOnEntityOnlyToPlayer( flyTo, owner, "Weapon_TetherGun_Attach_1P_VS_3P" )
		EmitSoundOnEntityExceptToPlayer( flyTo, owner, "Weapon_TetherGun_Attach_3P_VS_3P" )
	}
	else
	{
		EmitSoundOnEntity( flyTo, "Weapon_TetherGun_Attach_3P_VS_3P" )
	}
}


void function TitanTether_Remove( TetherData tetherData )
{
	entity endEnt = tetherData.endpointEnts[1]
	if ( IsValid( endEnt ) )
	{
		if ( IsSoul( endEnt ) )
			endEnt = endEnt.GetTitan()

		if ( endEnt.IsValidTetherID( tetherData.codeTetherID ) )
			endEnt.RemoveTether( tetherData.codeTetherID )
	}

	vector angvel = < RandomFloatRange( 50, 1000 ), RandomFloatRange( -200, 200 ), RandomFloatRange( -200, 200 )>

	vector velForPlayer
	vector velForOthers
	vector rotaxis
	float rotspeed
	if ( IsValid( endEnt ) )
	{
		vector forward = endEnt.GetPlayerOrNPCViewVector()
		velForPlayer = forward * 200

		rotaxis = endEnt.GetPlayerOrNPCViewRight()
		rotaxis += forward * RandomFloatRange( -0.4, 0.4 )
		rotaxis += endEnt.GetPlayerOrNPCViewUp() * RandomFloatRange( -0.4, 0.4 )
		rotspeed = RandomFloatRange( -2000, -1000 )
	}
	else
	{
		rotaxis = RandomVec( 1 )
		rotspeed = RandomFloatRange( -2000, 2000 )
	}

	vector pullDirForPlayer
	vector pullDirForOthers

	bool endEntForPlayerIsValid = IsValid( tetherData.endEntForPlayer )
	bool endEntForOthersIsValid = IsValid( tetherData.endEntForOthers )

	if ( IsValid( tetherData.anchor ) )
	{
		if ( endEntForPlayerIsValid )
			pullDirForPlayer = Normalize( tetherData.anchor.GetOrigin() - tetherData.endEntForPlayer.GetOrigin() )
		if ( endEntForOthersIsValid )
			pullDirForOthers = Normalize( tetherData.anchor.GetOrigin() - tetherData.endEntForOthers.GetOrigin() )
	}

	velForPlayer += < RandomFloatRange(-100,100), RandomFloatRange(-100,100), 0> + pullDirForPlayer * 100
	float pullDirForOthersZ = pullDirForOthers.z
	pullDirForOthers.z = 0
	pullDirForOthers += < RandomFloatRange(-1,1), RandomFloatRange(-1,1), 0>
	velForOthers = Normalize( pullDirForOthers )
	velForOthers.x *= RandomFloatRange( 100, 200 )
	velForOthers.y *= RandomFloatRange( 100, 200 )
	velForOthers.z = pullDirForOthersZ * 200

	rotaxis = Normalize( rotaxis )

	// Since we're unparenting the tethers, we need to change how they control who they're visible to
	if ( IsValid( endEnt ) )
	{
		if ( endEntForPlayerIsValid )
		{
			tetherData.endEntForPlayer.kv.VisibilityFlags = ENTITY_VISIBLE_TO_OWNER
			tetherData.endEntForPlayer.SetOwner( endEnt )
		}
		if ( endEntForOthersIsValid )
		{
			tetherData.endEntForOthers.kv.VisibilityFlags = ENTITY_VISIBLE_TO_FRIENDLY | ENTITY_VISIBLE_TO_ENEMY
			tetherData.endEntForOthers.SetOwner( endEnt )
		}
	}
	else
	{
		if ( endEntForPlayerIsValid )
			tetherData.endEntForPlayer.kv.VisibilityFlags = ENTITY_VISIBLE_TO_NOBODY
		if ( endEntForOthersIsValid )
			tetherData.endEntForOthers.kv.VisibilityFlags = ENTITY_VISIBLE_TO_EVERYONE
	}

	if ( endEntForPlayerIsValid )
	{
		tetherData.endEntForPlayer.ClearParent()
		tetherData.endEntForPlayer.NonPhysicsRotate( rotaxis, rotspeed )
		tetherData.endEntForPlayer.NonPhysicsMoveWithGravity( velForPlayer, < 0, 0, -750> )
		tetherData.endEntForPlayer.RenderWithViewModels( false )
		tetherData.endEntForPlayer.Dissolve( ENTITY_DISSOLVE_NORMAL, <0,0,0>, 100 )
	}

	if ( endEntForOthersIsValid )
	{
		tetherData.endEntForOthers.ClearParent()
		tetherData.endEntForOthers.NonPhysicsRotate( rotaxis, rotspeed )
		tetherData.endEntForOthers.NonPhysicsMoveWithGravity( velForOthers, < 0, 0, -750> )
		tetherData.endEntForOthers.Dissolve( ENTITY_DISSOLVE_NORMAL, <0,0,0>, 100 )
	}

	wait 0.5

	foreach ( index, ent in tetherData.endpointEnts )
	{
		if ( !IsValid( ent ) )
			continue

		if ( ent instanceof CBaseGrenade )
			ent.Destroy()
	}

	foreach ( entity ent in tetherData.tetherEnts )
	{
		if ( IsValid( ent ) )
			ent.Destroy()
	}
}


int function PROTO_GetActiveTethers( entity owner )
{
//	_PruneActiveTitanTethers()

	int activeTethers = 0
	foreach ( TetherData tetherData in file.activeTitanTethers )
	{
		if ( tetherData.owner == owner )
			activeTethers++
	}

	return activeTethers
}


bool removingTether

void function CodeCallback_OnTetherRemove( entity guy, int tetherID )
{
	Assert( !removingTether )
	removingTether = true

	foreach ( index, tetherData in file.activeTitanTethers )
	{
		if ( tetherData.codeTetherID == tetherID )
		{
			thread TitanTether_Remove( tetherData )
			file.activeTitanTethers.fastremove( index )
			break
		}
	}

	removingTether = false
}

TetherData function GetTetherDataForCodeID( int codeTetherID )
{
	foreach ( index, tetherData in file.activeTitanTethers )
	{
		if ( tetherData.codeTetherID == codeTetherID )
			return tetherData
	}

	unreachable
}

void function CodeCallback_OnTetherDamageMilestone( entity guy, int tetherID, int damageMilestoneIndex, float health )
{
	float healthFrac = 1.0 - health / 1000.0

	TetherData tetherData = GetTetherDataForCodeID( tetherID )

	vector ang = tetherData.endEntForPlayer.GetLocalAngles()
	tetherData.endEntForPlayer.SetLocalAngles( < healthFrac * 45, ang.y, ang.z> )
}