aboutsummaryrefslogtreecommitdiff
path: root/Northstar.CustomServers/scripts/vscripts/_trigger_functions.gnut
blob: 0f82d9a6b78424a84f893fe2ea9426263641afba (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
untyped

global function TriggerFunctions_Init

global function InitFlagMaskTriggers
global function TriggerInit
global function AddToFlagTriggers
global function AddTriggerEditorClassFunc
global function UpdateTriggerStatusFromFlagChange

const DEBUG_DEADLY_FOG = false
const asset BIRD_ALERT_FX = $"P_bird_alert_white"
struct BirdAlertInfo
{
	entity scriptRef //Also FX location
	array<entity> triggers
	float lastUseTime
}

struct
{
	//bool checkpointReached
	//vector checkpointOrigin
	//vector checkpointAngles
	table<string, array<void functionref( entity )> > triggerEditorClassFunctions

	table<string, int> flagTriggerEntArrayIndex
	array<BirdAlertInfo> birdAlerts
} file

function TriggerFunctions_Init()
{
	AddCallback_EntitiesDidLoad( InitFlagMaskTriggers )

	level._flagTriggers <- {} // triggers that can be enabled/disabled via flag

	AddTriggerEditorClassFunc( "trigger_flag_set", TriggerSetFlagOnTrigger )
	AddTriggerEditorClassFunc( "trigger_flag_clear", TriggerClearFlagOnTrigger )
	AddTriggerEditorClassFunc( "trigger_flag_touching", TriggerTouchingFlagOnTrigger )

	AddSpawnCallback( "trigger_multiple", 		TriggerInit )
	AddSpawnCallback( "trigger_once", 			TriggerInit )
	AddSpawnCallback( "trigger_hurt", 			TriggerInit )

	AddTriggerEditorClassFunc( "trigger_death_fall", TriggerDeathFall )
	AddSpawnCallbackEditorClass( "trigger_multiple", "trigger_deadly_fog", DeadlyFogTriggerInit )

	AddSpawnCallbackEditorClass( "script_ref", "script_bird_alert", BirdAlertInit )

	PrecacheParticleSystem( BIRD_ALERT_FX )

	RegisterSignal( "OutOfDeadlyFog" )

	PrecacheParticleSystem( $"Swtch_Elec_hzd_rope_end" )
}

void function AddTriggerEditorClassFunc( string editorClass, void functionref( entity ) triggerFunc )
{
	if ( !( editorClass in file.triggerEditorClassFunctions ) )
		file.triggerEditorClassFunctions[ editorClass ] <- []

	file.triggerEditorClassFunctions[ editorClass ].append( triggerFunc )
}

void function AddKeyPairFunctionality( entity trigger )
{
	table< string, void functionref( entity, string )> funcs
	funcs[ "scr_flagSet" ] <- TriggerFlagSet
	funcs[ "scr_flagClear" ] <- TriggerFlagClear

	foreach ( key, func in funcs )
	{
		if ( trigger.HasKey( key ) )
		{
			thread func( trigger, expect string( trigger.kv[ key ] ) )
		}
	}
}

function AddToFlagTriggers( entity self )
{
	level._flagTriggers[ self ] <- self
}

function GetFlagTriggers()
{
	foreach ( entity guy in clone level._flagTriggers )
	{
		if ( IsValid_ThisFrame( guy ) )
			continue

		delete level._flagTriggers[ guy ]
	}

	return level._flagTriggers
}


function AddKeyPairFunctionToClass( funcs, classname )
{
	array<entity> triggers = GetEntArrayByClass_Expensive( classname )

	foreach ( trigger in triggers )
	{
		foreach ( key, func in funcs )
		{
			if ( trigger.HasKey( key ) )
			{
				thread func( trigger, trigger.kv[ key ] )
			}
		}
	}
}

void function TriggerChangesFlagOnTrigger( entity trigger, string flag, void functionref( string ) func )
{
	trigger.EndSignal( "OnDestroy" )

	array<string> flags = GetFlagsFromString( flag )

	for ( ;; )
	{
		trigger.WaitSignal( "OnTrigger" )

		foreach ( flag in flags )
		{
			func( flag )
		}
	}
}

void function TriggerFlagSet( entity trigger, string flagString )
{
	thread TriggerChangesFlagOnTrigger( trigger, flagString, FlagSet )
}

void function TriggerFlagClear( entity trigger, string flagString )
{
	thread TriggerChangesFlagOnTrigger( trigger, flagString, FlagClear )
}

void function TriggerInit( entity trigger )
{
	if ( trigger.HasKey( "editorclass" ) )
		RunTriggerEditorClassFunctions( trigger )

	InitFlagsFromTrigger( trigger )
	AddKeyPairFunctionality( trigger )
	AddToFlagTriggers( trigger )
}

function RunTriggerEditorClassFunctions( entity trigger )
{
	string editorClass = expect string( trigger.kv.editorclass )
	if ( !( editorClass in file.triggerEditorClassFunctions ) )
		return

	foreach ( func in file.triggerEditorClassFunctions[ editorClass ] )
	{
		thread func( trigger )
	}
}

void function TriggerSetFlagOnTrigger( entity trigger )
{
	trigger.EndSignal( "OnDestroy" )

	string flag
	if ( trigger.HasKey( "script_flag" ) )
		flag = expect string( trigger.kv.script_flag )
	else if ( trigger.HasKey( "scr_flagSet" ) )
		flag = expect string( trigger.kv.scr_flagSet )

	bool triggerOnce = trigger.HasKey( "trigger_once" ) && trigger.kv.trigger_once == "1"

	Assert( flag != "", "Trigger " + GetEditorClass( trigger ) + " at " + trigger.GetOrigin() + "has empty flag value" )

	while ( true )
	{
		trigger.WaitSignal( "OnTrigger" )
		FlagSet( flag )

		if ( triggerOnce )
			return

		FlagWaitClear( flag )
	}
}

void function TriggerClearFlagOnTrigger( entity trigger )
{
	string flag
	if ( trigger.HasKey( "script_flag" ) )
		flag = expect string( trigger.kv.script_flag )
	else if ( trigger.HasKey( "scr_flagClear" ) )
		flag = expect string( trigger.kv.scr_flagClear )

	Assert( flag != "" )

	thread TriggerFlagClear( trigger, flag )
}

void function TriggerTouchingFlagOnTrigger( entity trigger )
{
	trigger.EndSignal( "OnDestroy" )
	string flag = expect string( trigger.kv.script_flag )
	Assert( flag != "" )

	while ( true )
	{
		if ( !trigger.IsTouched() )
			trigger.WaitSignal( "OnStartTouch" )

		FlagSet( flag )

		if ( trigger.IsTouched() )
			trigger.WaitSignal( "OnEndTouchAll" )

		FlagClear( flag )
	}
}

array<string> function GetFlagRelatedKeys()
{
	array<string> check
	check.append( "scr_flagTrueAll" )
	check.append( "scr_flagTrueAny" )
	check.append( "scr_flagFalseAll" )
	check.append( "scr_flagFalseAny" )
	check.append( "scr_flag" )
	check.append( "script_flag" )
	check.append( "scr_flagSet" )
	check.append( "scr_flagClear" )

	return check
}

void function InitFlagMaskTriggers()
{
	local triggers = GetFlagTriggers()
	array<string> check = GetFlagRelatedKeys()
	array<string> flags
	local allTriggersWithFlags = {}

	foreach ( trigger in triggers )
	{
		if ( trigger.HasKey( "scr_flagTrueAll" ) )
		{
			Assert( !trigger.HasKey( "scr_flagTrueAny" ), "Trigger at " + trigger.GetOrigin() + " has flag all and flag any" )
		}
		else
		if ( trigger.HasKey( "scr_flagTrueAny" ) )
		{
			Assert( !trigger.HasKey( "scr_flagTrueAll" ), "Trigger at " + trigger.GetOrigin() + " has flag all and flag any" )
		}

		if ( trigger.HasKey( "scr_flagFalseAll" ) )
		{
			Assert( !trigger.HasKey( "scr_flagFalseAny" ), "Trigger at " + trigger.GetOrigin() + " has flag all and flag any" )
		}
		else
		if ( trigger.HasKey( "scr_flagFalseAny" ) )
		{
			Assert( !trigger.HasKey( "scr_flagFalseAll" ), "Trigger at " + trigger.GetOrigin() + " has flag all and flag any" )
		}

		foreach ( field in check )
		{
			if ( trigger.HasKey( field ) )
			{
				allTriggersWithFlags[ trigger ] <- true
				flags = GetFlagsFromField( trigger, field )

				foreach ( flag in flags )
				{
					if ( !( flag in file.flagTriggerEntArrayIndex ) )
						file.flagTriggerEntArrayIndex[ flag ] <- CreateScriptManagedEntArray()

					AddToScriptManagedEntArray( file.flagTriggerEntArrayIndex[ flag ], trigger )

					// init the flag so these flags an be used in hammer more easily
					FlagInit( flag )
				}
			}
		}
	}

	foreach ( trigger, _ in allTriggersWithFlags )
	{
		expect entity( trigger )
		SetTriggerEnableFromFlag( trigger )
	}
}

void function SetTriggerEnableFromFlag( entity trigger )
{
	if ( GetTriggerEnabled( trigger ) )
		trigger.Fire( "Enable" )
	else
		trigger.Fire( "Disable" )
}

void function UpdateTriggerStatusFromFlagChange( string flag )
{
	// enable or disable triggers based on flag settings
	if ( !( flag in file.flagTriggerEntArrayIndex ) )
		return

	array<entity> triggers = GetScriptManagedEntArray( file.flagTriggerEntArrayIndex[ flag ] )
	foreach ( trigger in triggers )
	{
		SetTriggerEnableFromFlag( trigger )
	}
}

function InitFlagsFromTrigger( entity trigger )
{
	array<string> check = GetFlagRelatedKeys()
	array<string> flags

	foreach ( field in check )
	{
		if ( !trigger.HasKey( field ) )
			continue
		flags = GetFlagsFromField( trigger, field )

		foreach ( flag in flags )
		{
			// init the flag so these flags an be used in hammer more easily
			FlagInit( flag )
		}
	}
}


void function DeadlyFogTriggerInit( entity trigger )
{
	trigger.ConnectOutput( "OnStartTouch", DeadlyFogStartTouch )
	trigger.ConnectOutput( "OnEndTouch", DeadlyFogEndTouch )

	if ( trigger.HasKey( "electricEffect" ) && trigger.kv.electricEffect == "1" )
		thread DeadlyFogVisuals( trigger )
}

void function DeadlyFogStartTouch( entity trigger, entity ent, entity caller, var value )
{
	thread DeadlyFogDamagedEntity( trigger, ent )
}

void function DeadlyFogDamagedEntity( entity trigger, entity ent )
{
	if ( !IsAlive( ent ) || !IsValid( trigger ) )
		return

	EndSignal( ent, "OutOfDeadlyFog" )
	EndSignal( trigger, "OnDestroy" )
	EndSignal( ent, "OnDeath" )

	bool damagePilots = trigger.kv.damagePilots == "1"
	bool damageTitans = trigger.kv.damageTitans == "1"
	if ( !damagePilots && !damageTitans )
		return

	float tickTime = 0.5
	float timeTillDeath = 4.0

	entity worldSpawn = GetEnt( "worldspawn" )
	while( true )
	{
		if ( !IsValid( ent ) )
		{
			wait 0.5
			continue
		}

		if ( IsPilot( ent ) && !damagePilots )
		{
			wait 0.5
			continue
		}

		if ( ent.IsTitan() && !damageTitans )
		{
			wait 0.5
			continue
		}

		local isTitan = ent.IsTitan()
		local damageOrigin = ent.GetOrigin() + ( isTitan ? < 0.0, 0.0, 0.0 > : < 0.0 , 0.0, -200.0 > )
		damageOrigin += < RandomFloatRange( -300.0, 300.0 ), RandomFloatRange( -300.0, 300.0 ), RandomFloatRange( -100.0, 100.0 ) >
		local scriptTypeMask = damageTypes.dissolve | DF_STOPS_TITAN_REGEN

		local damageAmount = ( ent.GetMaxHealth() / ( timeTillDeath / tickTime ) )

		ent.TakeDamage( damageAmount, worldSpawn, worldSpawn, { origin = damageOrigin, scriptType = scriptTypeMask, damageSourceId = eDamageSourceId.deadly_fog } )

		if ( ent.IsPlayer() )
			StatusEffect_AddTimed( ent, eStatusEffect.emp, 1.0, 1.0, 0.5 )

		wait 0.5
	}
}

void function DeadlyFogEndTouch( entity trigger, entity ent, entity caller, var value )
{
	if ( IsValid( ent ) )
		Signal( ent, "OutOfDeadlyFog" )
}

void function DeadlyFogVisuals( entity trigger )
{
	wait 0.5

	// Get the trigger bounds
	vector triggerMins = trigger.GetBoundingMins()
	vector triggerMaxs = trigger.GetBoundingMaxs()
	vector triggerOrigin = trigger.GetOrigin()
	if ( DEBUG_DEADLY_FOG )
	{
		DebugDrawBox( triggerOrigin, triggerMins, triggerMaxs, 255, 255, 0, 1, 60.0 )
		DebugDrawSphere( triggerOrigin, 25.0, 255, 200, 0, true, 60.0 )
	}

	// Divide the trigger into smaller squares
	vector triggerDimension = triggerMaxs - triggerMins

	int segmentSizeX = int( max( triggerDimension.x / 2000, 1500 ) )
	int segmentSizeY = int( max( triggerDimension.y / 2000, 1500 ) )
	int segmentSizeZ = int( min( 300, triggerDimension.z ) )

	vector segmentSize = Vector( segmentSizeX, segmentSizeY, segmentSizeZ )
	vector segmentCount = Vector( triggerDimension.x / segmentSize.x, triggerDimension.y / segmentSize.y, triggerDimension.z / segmentSize.z )

	segmentCount.x = floor( segmentCount.x )
	segmentCount.y = floor( segmentCount.y )
	segmentCount.z = floor( segmentCount.z )
	segmentCount.x = segmentCount.x < 1.0 ? 1.0 : segmentCount.x
	segmentCount.y = segmentCount.y < 1.0 ? 1.0 : segmentCount.y
	segmentCount.z = segmentCount.z < 1.0 ? 1.0 : segmentCount.z

	vector startPos = triggerOrigin + triggerMins + segmentSize * 0.5
	startPos.x += (triggerDimension.x - (segmentCount.x * segmentSize.x)) * 0.5
	startPos.y += (triggerDimension.y - (segmentCount.y * segmentSize.y)) * 0.5
	startPos.z += (triggerDimension.z - (segmentCount.z * segmentSize.z)) * 0.5

	vector segmentPos = startPos
	for ( int z = 0 ; z < segmentCount.z ; z++ )
	{
		// Only do effects on the top layer of the trigger
		if ( z < ( segmentCount.z - 1 ) )
			continue

		for ( int y = 0 ; y < floor(segmentCount.y) ; y++ )
		{
			for ( int x = 0 ; x < floor(segmentCount.x) ; x++ )
			{
				vector segmentPos = startPos + Vector( segmentSize.x * x, segmentSize.y * y, segmentSize.z * z )
				thread DeadlyFogEffect( segmentPos, segmentSize )
			}
		}
	}
}

void function DeadlyFogEffect( vector origin, vector segmentSize )
{
	entity effect = CreateEntity( "info_particle_system" )
	effect.SetValueForEffectNameKey( $"Swtch_Elec_hzd_rope_end" )
	effect.kv.start_active = 0
	effect.kv.VisibilityFlags = ENTITY_VISIBLE_TO_EVERYONE
	effect.SetOrigin( origin )
	DispatchSpawn( effect )

	vector mins = origin + (segmentSize * -0.5)
	vector maxs = origin + (segmentSize * 0.5)
	if ( DEBUG_DEADLY_FOG )
	{
		DebugDrawBox( origin, mins - origin, maxs - origin, 255, 255, 0, 1, 60.0 )
		DebugDrawSphere( origin, 25.0, 255, 200, 0, true, 60.0 )
	}

	while( true )
	{
		wait RandomFloatRange( 2.1, 2.6 )

		vector org = Vector( RandomFloatRange( mins.x, maxs.x ), RandomFloatRange( mins.y, maxs.y ), RandomFloatRange( mins.z, maxs.z ) )

		if ( DEBUG_DEADLY_FOG )
			DebugDrawLine( origin, org, 255, 0, 0, true, 2.0 )

		effect.SetOrigin( org )
		effect.Fire( "Start" )
		effect.Fire( "StopPlayEndCap", "", 2.0 )
	}
}

void function TriggerDeathFall( entity trigger )
{
	EndSignal( trigger, "OnDestroy" )

	while ( true )
	{
		table results = trigger.WaitSignal( "OnTrigger" )
		entity player = expect entity( results.activator )
		if ( !IsValid( player ) || !player.IsPlayer() || !IsAlive( player ) )
			continue

		if ( player.IsGodMode() )
		{
			printt( "GOD MODE PLAYER CANT DIE" )
			continue
		}

		if ( player.p.doingQuickDeath )
			continue

		if ( IsSingleplayer() )
			FlagClear( "SaveGame_Enabled" ) // no more saving, you have lost

		player.EndSignal( "OnDeath" )

		// Go to black and fade it out after a pause
		float fadeTime = 0.5
		float holdTime = 999
		ScreenFade( player, 0, 1, 0, 255, fadeTime, holdTime, FFADE_OUT | FFADE_PURGE )

		float deathTime = Time() + fadeTime
		while ( Time() <= deathTime )
		{
			if ( player.IsOnGround() || player.IsWallRunning() || player.IsWallHanging() || player.p.doingQuickDeath )
				break
			WaitFrame()
		}

		if ( player.p.doingQuickDeath )
			continue

		if ( IsAlive( player ) )
		{
			KillPlayer( player, eDamageSourceId.fall )
			return
		}
	}
}


void function BirdAlertInit( entity ref )
{
	BirdAlertInfo info
	info.scriptRef = ref
	array<entity> linkedEntities = ref.GetLinkEntArray()
	foreach ( trigger in linkedEntities )
	{
		info.triggers.append( trigger )
		trigger.ConnectOutput( "OnStartTouch", BirdAlertStartTouch )
	}
	file.birdAlerts.append( info )
}

void function BirdAlertStartTouch( entity trigger, entity ent, entity caller, var value )
{
	array<BirdAlertInfo> birdAlerts = GetBirdAlertInfoFromTrigger( trigger )
	foreach( alert in birdAlerts )
	{
		float debounceTime = 6.0
		if ( alert.lastUseTime + debounceTime > Time() )
			return

		StartParticleEffectInWorld( GetParticleSystemIndex( BIRD_ALERT_FX ), alert.scriptRef.GetOrigin(), alert.scriptRef.GetAngles() )
		alert.lastUseTime = Time()
	}
}

array<BirdAlertInfo> function GetBirdAlertInfoFromTrigger( entity trigger )
{
	array<BirdAlertInfo> birdAlerts
	foreach ( infoStruct in file.birdAlerts )
	{
		if ( infoStruct.triggers.contains( trigger ) )
			birdAlerts.append( infoStruct )
	}

	return birdAlerts
	unreachable
}