aboutsummaryrefslogtreecommitdiff
path: root/Northstar.CustomServers/mod/scripts/vscripts/weapons/_ball_lightning.gnut
blob: 9aae59e549473facb6dbc1a7d9ff66257a638bba (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
untyped

global function BallLightning_Init
global function AttachBallLightning
global function AttachBallLightningToProp
global function CreateBallLightning
global function DestroyBallLightningOnEnt
global function GetBallLightningFromEnt

global function RegisterBallLightningDamage

global function BallLightningZapFX
global function BallLightningZapTargets
global function BallLightningZapConnectionFX

struct {
	table< string, float > uniqueStrings
} file

function BallLightning_Init()
{
	PrecacheParticleSystem( BALL_LIGHTNING_ZAP_FX )

	if ( BALL_LIGHTNING_FX_TABLE != "" )
		PrecacheImpactEffectTable( BALL_LIGHTNING_FX_TABLE )

	RegisterBallLightningDamage( eDamageSourceId.mp_weapon_arc_launcher )
	RegisterBallLightningDamage( eDamageSourceId.mp_titanweapon_arc_ball )
	RegisterBallLightningDamage( eDamageSourceId.mp_weapon_arc_trap )
}

function AttachBallLightning( entity weapon, entity projectile )
{
	Assert( !( "ballLightning" in projectile.s ) )

	int damageSourceId
	entity owner

	if ( weapon.IsProjectile() )
	{
		owner = weapon.GetOwner()
		damageSourceId = weapon.ProjectileGetDamageSourceID()
	}
	else
	{
		owner = weapon.GetWeaponOwner()
		damageSourceId = weapon.GetDamageSourceID()
	}


	entity ball = CreateBallLightning( owner, damageSourceId, projectile.GetOrigin(), projectile.GetAngles() )
	ball.SetParent( projectile )
	projectile.s.ballLightning <- ball
}

void function DestroyBallLightningOnEnt( entity prop )
{
	if ( "ballLightning" in prop.s )
	{
		prop.s.ballLightning.Destroy()
		delete prop.s.ballLightning
	}
}


entity function AttachBallLightningToProp( entity prop, entity owner, int damageSourceId )
{
	entity ball = CreateBallLightning( owner, damageSourceId, prop.GetOrigin(), prop.GetAngles() )
	ball.SetParent( prop )
	prop.s.ballLightning <- ball
	return ball
}

entity function CreateBallLightning( entity owner, int damageSourceId, vector origin, vector angles )
{
	entity ballLightning = CreateScriptMover( origin, angles )
	ballLightning.SetOwner( owner )
	SetTeam( ballLightning, owner.GetTeam() )

	thread BallLightningThink( ballLightning, damageSourceId )
	return ballLightning
}

void function RegisterBallLightningDamage( int damageSourceId )
{
	AddDamageCallbackSourceID( damageSourceId, OnBallLightningDamage )
}

void function OnBallLightningDamage( entity victim, var damageInfo )
{
	float damage = DamageInfo_GetDamage( damageInfo )

	if ( damage <= 0 )
		return

	if ( victim.IsWorld() )
		return

	if ( victim.IsProjectile() )
		return

	if ( DamageInfo_GetCustomDamageType( damageInfo ) & (DF_EXPLOSION | DF_IMPACT) )
		return

	// if ( IsHumanSized( victim ) )
	// {
	// 	DamageInfo_SetDamage( damageInfo, 0 )
	// 	return
	// }

	entity ballLightning = DamageInfo_GetInflictor( damageInfo )

	if ( victim == ballLightning )
		return

	if ( victim.GetParent() == ballLightning )
		return

	if ( !IsTargetEntValid( ballLightning, victim, ballLightning.e.ballLightningData ) )
	{
		DamageInfo_SetDamage( damageInfo, 0 )
		return
	}

	vector origin = DamageInfo_GetDamagePosition( damageInfo )
	int hitBox = DamageInfo_GetHitBox( damageInfo )

	string tag = GetEntityCenterTag( victim )
	thread BallLightningZapConnectionFX( ballLightning, victim, tag, ballLightning.e.ballLightningData )
	BallLightningZapFX( ballLightning, victim, tag, ballLightning.e.ballLightningData )
}

void function BallLightningThink( entity ballLightning, int damageSourceId )
{
	ballLightning.EndSignal( "OnDestroy" )

	EmitSoundOnEntity( ballLightning, "Weapon_Arc_Ball_Loop" )

	local data = {}

	OnThreadEnd(
		function() : ( ballLightning, data )
		{
			if ( IsValid( ballLightning ) )
				StopSoundOnEntity( ballLightning, "Weapon_Arc_Ball_Loop" )
		}
	)

	int inflictorTeam = ballLightning.GetTeam()
	ballLightning.e.ballLightningTargetsIdx = CreateScriptManagedEntArray()

	WaitEndFrame()

	while( 1 )
	{
		for( int i=0; i<BALL_LIGHTNING_BURST_NUM; i++ )
		{
			if ( BALL_LIGHTNING_BURST_NUM > 1 )
				wait BALL_LIGHTNING_BURST_PAUSE

			vector origin = ballLightning.GetOrigin()
			BallLightningZapTargets( ballLightning, origin, inflictorTeam, damageSourceId, ballLightning.e.ballLightningData, false )
		}
		wait BALL_LIGHTNING_BURST_DELAY
	}
}

void function BallLightningZapTargets( entity ballLightning, vector origin, int inflictorTeam, int damageSourceId, BallLightningData fxData, bool single )
{
	RadiusDamage(
	    origin,				// origin
	    ballLightning.GetOwner(),		// owner
	    ballLightning,		 			// inflictor
	    fxData.damageToPilots,								// normal damage
	    fxData.damage,					// heavy armor damage
	    fxData.radius,							// inner radius
	    fxData.radius,							// outer radius
	    SF_ENVEXPLOSION_NO_DAMAGEOWNER,	// explosion flags
	    0, 								// distanceFromAttacker
	    0, 								// explosionForce
	    fxData.deathPackage,				// damage flags
	    damageSourceId	// damage source id
	)
}

string function GetEntityCenterTag( entity target )
{
	string tag = "center"

	if ( IsHumanSized( target ) )
		tag = "CHESTFOCUS"
	else if ( target.IsTitan() )
		tag = "HIJACK"
	else if ( IsSuperSpectre( target ) || IsAirDrone( target ) )
		tag = "CHESTFOCUS"
	else if ( IsDropship( target ) )
		tag = "ORIGIN"
	else if ( target.GetClassName() == "npc_turret_mega" )
		tag = "ATTACH"

	return tag
}

bool function IsTargetEntValid( entity ballLightning, entity target, BallLightningData fxData )
{
	if ( !IsValid( target ) )
		return false

	vector origin = ballLightning.GetOrigin()

	if ( target == ballLightning )
		return false

	if ( target == ballLightning.GetParent() )
		return false

	if ( target.GetParent() == ballLightning.GetParent() )
		return false

	// if ( target.IsPlayer() && !target.IsTitan() )
	// 	return false

	if ( fabs( origin.z - target.GetOrigin().z ) > fxData.height )
		return false

	if ( GetBugReproNum() != 131703 )
	{
		if ( target.GetModelName() == $"" )
			return false
	}

	if ( !( target.GetClassName() in ArcCannonTargetClassnames ) )
		return false

	vector entityCenter = target.GetWorldSpaceCenter()

	if ( target.GetModelName() != $"" )
	{
		string tag = GetEntityCenterTag( target )
		int index = target.LookupAttachment( tag )

		if ( index == 0 )
			return false

		entityCenter = target.GetAttachmentOrigin( index )
	}

	vector fwd = AnglesToForward( ballLightning.GetAngles() )
	vector fwdToEnemy = Normalize( entityCenter - ballLightning.GetOrigin() )

	float dot = DotProduct( fwd, fwdToEnemy )

	if ( dot < fxData.minDot )
		return false


	if ( IsHumanSized( target ) )
	{
		float maxDist = fxData.humanRadius
		if ( Distance( entityCenter, ballLightning.GetOrigin() ) > maxDist )
			return false
	}

	// array<entity> ignoreEnts = [ target, ballLightning ]
	// if ( ballLightning.GetParent() != null )
	// 	ignoreEnts.append( ballLightning.GetParent() )

	// TraceResults trace = TraceLine( ballLightning.GetOrigin(), entityCenter, ignoreEnts, TRACE_MASK_SHOT, TRACE_COLLISION_GROUP_BLOCK_WEAPONS )

	// if ( trace.fraction < 1 )
	// 	return false

	VortexBulletHit ornull vortexHit = VortexBulletHitCheck( ballLightning.GetOwner(), ballLightning.GetOrigin(), entityCenter )

	if ( vortexHit )
		return false

	return true
}

void function BallLightningZapConnectionFX( entity ballLightning, entity target, string tag, BallLightningData fxData )
{
	if ( fxData.zapFx != $"" )
	{
		// Control point sets the end position of the effect
		entity cpEnd = CreateEntity( "info_placement_helper" )
		SetTargetName( cpEnd, GetUniqueCpString() )
		cpEnd.SetParent( target, tag, false, 0.0 )
		DispatchSpawn( cpEnd )

		entity zapBeam = CreateEntity( "info_particle_system" )
		zapBeam.kv.cpoint1 = cpEnd.GetTargetName()

		zapBeam.SetValueForEffectNameKey( fxData.zapFx )
		zapBeam.kv.start_active = 0
		zapBeam.SetOwner( ballLightning )
		zapBeam.kv.VisibilityFlags = ENTITY_VISIBLE_TO_EVERYONE
		zapBeam.SetParent( ballLightning, "", false, 0.0 )
		DispatchSpawn( zapBeam )

		zapBeam.Fire( "Start" )

		OnThreadEnd(
		function() : ( zapBeam, cpEnd )
			{
				if ( IsValid( zapBeam ) )
					zapBeam.Destroy()
				if ( IsValid( cpEnd ) )
					cpEnd.Destroy()
			}
		)

		ballLightning.EndSignal( "OnDestroy" )
		target.EndSignal( "OnDestroy" )
		target.EndSignal( "OnDeath" )

		if ( fxData.zapLifetime > 0 )
		{
			wait fxData.zapLifetime
		}
	}
}

void function BallLightningZapFX( entity ballLightning, entity target, string tag, BallLightningData fxData )
{
	int index = target.LookupAttachment( tag )

	vector entityCenter = target.GetAttachmentOrigin( index )

	if ( fxData.zapImpactTable != "" )
		PlayImpactFXTable( entityCenter, ballLightning.GetOwner(), fxData.zapImpactTable, SF_ENVEXPLOSION_INCLUDE_ENTITIES )

	EmitSoundOnEntity( ballLightning, fxData.zapSound )
	thread FadeOutSoundOnEntityAfterDelay( ballLightning, fxData.zapSound, 0.2, 0.2 )
}

// This is to minimize creation of new Unique Strings
string function GetUniqueCpString()
{
	foreach ( string uString, float useTime in file.uniqueStrings )
	{
		if ( useTime + BALL_LIGHTNING_ZAP_LIFETIME*2 > Time() )
			continue

		file.uniqueStrings[ uString ] = Time()
		return uString
	}

	string newString = UniqueString( "ball_lightning_cpEnd" )

	// printt( "Generated new string " + newString )

	file.uniqueStrings[ newString ] <- Time()
	return newString
}

entity function GetBallLightningFromEnt( entity ent )
{
	if ( "ballLightning" in ent.s )
		return expect entity( ent.s.ballLightning )

	return null
}