aboutsummaryrefslogtreecommitdiff
path: root/Northstar.CustomServers/scripts/vscripts/ai/_ai_personal_shield.gnut
blob: f1fbdb80f248ef0b654e6e9e8ead0a64a30017d3 (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
global function AiPersonalShield
global function ActivatePersonalShield
const FX_DRONE_SHIELD_WALL_HUMAN 	= $"P_drone_shield_wall_sm"
const SHIELD_BREAK_FX = $"P_xo_armor_break_CP"
const SHIELD_HEALTH = 620
global const AI_PERSONAL_SHIELD_PAIN_SHIELD_STYLE = true
const float PERSONAL_SHIELD_HEALTH_FRAC_DAMAGED = 0.5  // below what frac of total health will the personal shield owner want to chatter about shield damage?

struct
{
	table<entity, entity> npcVortexSpheres
} file


void function AiPersonalShield()
{
	PrecacheParticleSystem( FX_DRONE_SHIELD_WALL_HUMAN )
	PrecacheParticleSystem( SHIELD_BREAK_FX )
	AddSyncedMeleeServerCallback( GetSyncedMeleeChooser( "human", "human" ), DisableShieldOnExecution )
}

void function DisableShieldOnExecution( SyncedMeleeChooser actions, SyncedMelee action, entity player, entity target )
{
	if ( !( target in file.npcVortexSpheres ) )
		return

	entity vortex = file.npcVortexSpheres[ target ]
	vortex.Destroy()
}

void function ActivatePersonalShield( entity owner )
{
	owner.EndSignal( "OnDeath" )
	for ( ;; )
	{
		waitthread ActivatePersonalShield_Recreate( owner )

		// got stunned? make new shield after awhile
		wait 15
	}
}

void function ShieldProtectsOwnerFromMelee( entity ent, var damageInfo )
{
	entity attacker = DamageInfo_GetAttacker( damageInfo )
	if ( !IsAlive( attacker ) )
		return
	if ( !attacker.IsPlayer() )
		return
	if ( !IsPilot( attacker ) )
		return
	entity weapon = DamageInfo_GetWeapon( damageInfo )
	if ( !IsValid( weapon ) )
		weapon = attacker.GetActiveWeapon()
	if ( !IsValid( weapon ) )
		return
	var weaponType = weapon.GetWeaponInfoFileKeyField( "weaponType" )
	if ( weaponType != "melee" )
		return

	Assert( ent in file.npcVortexSpheres )
	entity vortexSphere = file.npcVortexSpheres[ ent ]

	float radius = float( vortexSphere.kv.radius )
	float height = float( vortexSphere.kv.height )
	float bullet_fov = float( vortexSphere.kv.bullet_fov )
	float dot = cos( bullet_fov * 0.5 )

	vector origin = vortexSphere.GetOrigin()
	vector angles = vortexSphere.GetAngles()
	vector forward = AnglesToForward( angles )
	int team = vortexSphere.GetTeam()

	if ( ProtectedFromShield( attacker, origin, height, radius, bullet_fov, dot, forward ) )
	{
		DamageInfo_SetDamage( damageInfo, 0 )
		StunPushBack( attacker, forward )
	}
}

entity function ActivatePersonalShield_Recreate( entity owner )
{
	if ( !AI_PERSONAL_SHIELD_PAIN_SHIELD_STYLE )
		AddEntityCallback_OnDamaged( owner, ShieldProtectsOwnerFromMelee )
	//------------------------------
	// Shield vars
	//------------------------------
	vector origin = owner.GetOrigin()
	vector angles = owner.GetAngles() + Vector( 0, 0, 180 )

	float shieldWallRadius = 45 // 90
	asset shieldFx = FX_DRONE_SHIELD_WALL_HUMAN
	float wallFOV = DRONE_SHIELD_WALL_FOV_HUMAN
	float shieldWallHeight = 102

	//------------------------------
	// Vortex to block the actual bullets
	//------------------------------
	entity vortexSphere = CreateEntity( "vortex_sphere" )

	Assert( !( owner in file.npcVortexSpheres ), owner + " already has a shield" )
	file.npcVortexSpheres[ owner ] <- vortexSphere
	vortexSphere.kv.spawnflags = SF_ABSORB_BULLETS | SF_BLOCK_OWNER_WEAPON | SF_BLOCK_NPC_WEAPON_LOF | SF_ABSORB_CYLINDER
	vortexSphere.kv.enabled = 0
	vortexSphere.kv.radius = shieldWallRadius
	vortexSphere.kv.height = shieldWallHeight
	vortexSphere.kv.bullet_fov = wallFOV
	vortexSphere.kv.physics_pull_strength = 25
	vortexSphere.kv.physics_side_dampening = 6
	vortexSphere.kv.physics_fov = 360
	vortexSphere.kv.physics_max_mass = 2
	vortexSphere.kv.physics_max_size = 6

	StatusEffect_AddEndless( vortexSphere, eStatusEffect.destroyed_by_emp, 1.0 )

	vortexSphere.SetAngles( angles ) // viewvec?
	vortexSphere.SetOrigin( origin + Vector( 0, 0, shieldWallRadius - 64 ) )
	vortexSphere.SetMaxHealth( SHIELD_HEALTH )
	vortexSphere.SetHealth( SHIELD_HEALTH )
	SetTeam( vortexSphere, owner.GetTeam() )

	thread PROTO_VortexSlowsPlayers_PersonalShield( owner, vortexSphere )

	DispatchSpawn( vortexSphere )

	EntFireByHandle( vortexSphere, "Enable", "", 0, null, null )

	vortexSphere.SetTakeDamageType( DAMAGE_YES )
	vortexSphere.ClearInvulnerable() // make particle wall invulnerable to weapon damage. It will still drain over time

	//------------------------------------------
	// Shield wall fx for visuals/health drain
	//------------------------------------------
	entity cpoint = CreateEntity( "info_placement_helper" )
	SetTargetName( cpoint, UniqueString( "shield_wall_controlpoint" ) )
	DispatchSpawn( cpoint )

	entity mover = CreateScriptMover()
	mover.SetOrigin( owner.GetOrigin() )
	vector moverAngles = owner.GetAngles()
	mover.SetAngles( AnglesCompose( moverAngles, <0,0,180> ) )

	int fxid = GetParticleSystemIndex( FX_DRONE_SHIELD_WALL_HUMAN )
	entity shieldWallFX = StartParticleEffectOnEntity_ReturnEntity( mover, fxid, FX_PATTACH_ABSORIGIN_FOLLOW, 0 )
	shieldWallFX.DisableHibernation()
	EffectSetControlPointEntity( shieldWallFX, 0, mover )

	//thread DrawArrowOnTag( mover )
	vortexSphere.e.shieldWallFX = shieldWallFX
	vector color = GetShieldTriLerpColor( 0.0 )

	cpoint.SetOrigin( color )
	EffectSetControlPointEntity( shieldWallFX, 1, cpoint )
	SetVortexSphereShieldWallCPoint( vortexSphere, cpoint )

	#if GRUNTCHATTER_ENABLED
		// have to do this, vortex shield isn't an entity that works with AddEntityCallback_OnDamaged
		thread PersonalShieldOwner_ReactsToDamage( owner, vortexSphere )
	#endif

	//-----------------------
	// Attach shield to owner
	//------------------------
	vortexSphere.SetParent( mover )

	vortexSphere.EndSignal( "OnDestroy" )
	Assert( IsAlive( owner ) )
	owner.EndSignal( "OnDeath" )
	owner.EndSignal( "ArcStunned" )
	mover.EndSignal( "OnDestroy" )
	#if MP
	shieldWallFX.EndSignal( "OnDestroy" )
	#endif

	OnThreadEnd(
	function() : ( owner, mover, vortexSphere )
		{
			delete file.npcVortexSpheres[ owner ]
			if ( IsValid( owner ) )
			{
				owner.kv.defenseActive = false
				if ( !AI_PERSONAL_SHIELD_PAIN_SHIELD_STYLE )
					RemoveEntityCallback_OnDamaged( owner, ShieldProtectsOwnerFromMelee )
			}

			StopShieldWallFX( vortexSphere )

			if ( IsValid( vortexSphere ) )
				vortexSphere.Destroy()

			if ( IsValid( mover ) )
			{
				//PlayFX( SHIELD_BREAK_FX, mover.GetOrigin(), mover.GetAngles() )
				mover.Destroy()
			}
		}
	)

	owner.kv.defenseActive = true

	for ( ;; )
	{
		Assert( IsAlive( owner ) )
		UpdateShieldPosition( mover, owner )

		#if MP
		if ( IsCloaked( owner ) )
			EntFireByHandle( shieldWallFX, "Stop", "", 0, null, null )
		else
			EntFireByHandle( shieldWallFX, "Start", "", 0, null, null )
		#endif
	}
}

#if GRUNTCHATTER_ENABLED
void function PersonalShieldOwner_ReactsToDamage( entity owner, entity vortexSphere )
{
	EndSignal( owner, "OnDeath" )
	EndSignal( vortexSphere, "OnDestroy" )

	float alertHealth = vortexSphere.GetMaxHealth() * PERSONAL_SHIELD_HEALTH_FRAC_DAMAGED

	while ( vortexSphere.GetHealth() >= alertHealth )
		wait 0.25

	GruntChatter_TryPersonalShieldDamaged( owner ) //Commenting out to unblock tree. See bug  186062
}
#endif

float function GetYawForEnemyOrLKP( entity owner )
{
	entity enemy = owner.GetEnemy()
	if ( !IsValid( enemy ) )
		return owner.GetAngles().y

	vector ornull lkp = owner.LastKnownPosition( enemy )
	if ( lkp == null )
		return owner.GetAngles().y

	expect vector( lkp )
	vector dif = lkp - owner.GetOrigin()
	return VectorToAngles( dif ).y
}

void function UpdateShieldPosition( entity mover, entity owner )
{
	mover.NonPhysicsMoveTo( owner.GetOrigin(), 0.1, 0.0, 0.0 )
	vector angles = owner.EyeAngles()
	float yaw = angles.y
	yaw %= 360
	mover.NonPhysicsRotateTo( <0,yaw,180>, 1.35, 0, 0 )

//	float yaw = GetYawForEnemyOrLKP( owner )
//	float boost = sin( Time() * 1.5 ) * 65
//	yaw += boost
//	yaw %= 360
//	mover.NonPhysicsRotateTo( <0,yaw,0>, 0.95, 0, 0 )

	WaitFrame()
}

void function PROTO_VortexSlowsPlayers_PersonalShield( entity owner, entity vortexSphere )
{
	owner.EndSignal( "OnDeath" )
	vortexSphere.EndSignal( "OnDestroy" )

	float radius = float(vortexSphere.kv.radius )
	float height = float(vortexSphere.kv.height )
	float bullet_fov = float( vortexSphere.kv.bullet_fov )
	float dot = cos( bullet_fov * 0.5 )

	for ( ;; )
	{
		vector origin = vortexSphere.GetOrigin()
		vector angles = vortexSphere.GetAngles()
		vector forward = AnglesToForward( angles )
		int team = vortexSphere.GetTeam()

		foreach ( player in GetPlayerArray() )
		{
			if ( !IsAlive( player ) )
				continue
			if ( player.GetTeam() == team )
				continue
			if ( VortexStunCheck_PersonalShield( player, origin, height, radius, bullet_fov, dot, forward ) )
			{
				player.p.lastDroneShieldStunPushTime = Time()

				if ( AI_PERSONAL_SHIELD_PAIN_SHIELD_STYLE )
				{
					Explosion_DamageDefSimple( damagedef_shield_captain_arc_shield, player.GetOrigin(),owner, owner, player.GetOrigin() )
				}
			}
		}
		WaitFrame()
	}
}

bool function ProtectedFromShield( entity player, vector origin, float height, float radius, float bullet_fov, float dotLimit, vector forward )
{
	vector playerOrg = player.GetOrigin()
	vector dif = Normalize( playerOrg - origin )

	float dot = DotProduct2D( dif, forward )
	return dot >= dotLimit
}

bool function VortexStunCheck_PersonalShield( entity player, vector origin, float height, float radius, float bullet_fov, float dot, vector forward )
{
	if ( !IsPilot( player ) )
		return false

	if ( player.IsGodMode() )
		return false

	if ( AI_PERSONAL_SHIELD_PAIN_SHIELD_STYLE )
	{
		if ( Time() - player.p.lastDroneShieldStunPushTime < 1.00 )
			return false
	}
	else
	{
		if ( Time() - player.p.lastDroneShieldStunPushTime < 1.75 )
			return false
	}

	vector playerOrg = player.GetOrigin()
	float dist2d = Distance2D( playerOrg, origin )

	if ( dist2d > radius + 5 )
		return false
	if ( dist2d < radius - 15 )
		return false

	float heightOffset = fabs( playerOrg.z - origin.z )

	if ( heightOffset < 0 || heightOffset > height )
		return false

	if ( !ProtectedFromShield( player, origin, height, radius, bullet_fov, dot, forward ) )
		return false

	if ( AI_PERSONAL_SHIELD_PAIN_SHIELD_STYLE )
	{
		const float VORTEX_STUN_DURATION = 1.0
		GiveEMPStunStatusEffects( player, VORTEX_STUN_DURATION + 0.5 )
		float strength = 0.4
		StatusEffect_AddTimed( player, eStatusEffect.emp, strength, VORTEX_STUN_DURATION, 0.5 )
		EmitSoundOnEntityOnlyToPlayer( player, player, "flesh_electrical_damage_1p" )
	}
	else
	{
		StunPushBack( player, forward )
	}

	return true
}

void function StunPushBack( entity player, vector forward )
{
	const float VORTEX_STUN_DURATION = 1.0
	GiveEMPStunStatusEffects( player, VORTEX_STUN_DURATION + 0.5 )
	float strength = 0.4
	StatusEffect_AddTimed( player, eStatusEffect.emp, strength, VORTEX_STUN_DURATION, 0.5 )
	thread TempLossOfAirControl( player, VORTEX_STUN_DURATION )
	vector velocity = forward * 300
	velocity.z = 400

	EmitSoundOnEntityOnlyToPlayer( player, player, "flesh_electrical_damage_1p" )
	player.SetVelocity( velocity )
}