aboutsummaryrefslogtreecommitdiff
path: root/Northstar.Coop/scripts/vscripts/sp/sh_sp_objective.gnut
blob: d98d4a6e142c3579295d64b419893ce2de8e89b6 (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
global function SPObjectiveInit

const OBJECTIVE_DISPLAY_TIME = 8.0
const FADE_IN_TIME = 0.2
const FADE_OUT_TIME = 0.5
const OBJECTIVE_DISPLAY_TOTAL_TIME = FADE_IN_TIME + OBJECTIVE_DISPLAY_TIME + FADE_OUT_TIME

#if SERVER
	global function Objective_Set
	global function Objective_SetSilent
	global function Objective_Set_WithAltHighlight
	global function Objective_SetSilent_WithAltHighlight
	global function Objective_AddHighlightEntity
	global function Objective_Clear
	global function Objective_Hide
	global function Objective_Update
	global function Objective_InitEntity
	global function Objective_Remind
	global function Objective_AddKilometers
	global function Objective_StaticModelHighlightOverrideEntity // if set it will highlight this entity instead, and also hide/show the entity when highlighting. This helps us get around not being able to do model highlight on a static model
	global function Objective_StaticModelHighlightOverrideEntityArray
	global function Objective_GetMarkerEntity // Dont use this if you're not working on S2S
	global function Objective_SetFastball// Don't use this unless you grab the marker ent and clear it's parent after you're done
	global function Objective_WayPointEneable
	global function Objective_LastShownTime
	global function Objective_SuppressCloseMenuShowsObjective

	struct
	{
		array<entity> objectiveEntities
		entity objectiveMarkerEnt
		array<entity> objectiveHighlightEntArray
		array<entity> objectiveAltHighlightEntArray
		bool showingObjective
		float objectiveLastShownTime
		bool suppressCloseMenuShowsObjective
		bool objectiveAltHighlight = false
	} file
#endif

#if CLIENT
	global function ShowObjectiveLineChanged
	global function ObjectiveStringChanged
	global function ShowObjectiveChanged

	const OBJECTIVE_MARKER_MODEL = $"models/dev/editor_ref.mdl"
	const BLING_DURATION = 0.2

	struct
	{
		bool onTimeshiftLevel
		var objectiveRUI
	} file
#endif

void function SPObjectiveInit()
{
	#if SERVER
		RegisterSignal( "ShowingObjective" )
		RegisterSignal( "HidingObjective" )
		AddClientCommandCallback( "ToggleObjective", ClientCommandToggleObjective )
		AddClientCommandCallback( "ShowObjective", ClientCommandShowObjective )
	#endif

	#if CLIENT
		PrecacheModel( OBJECTIVE_MARKER_MODEL )
		RegisterSignal( "ShowingObjectiveRUI" )
		
		file.onTimeshiftLevel = GetMapName().find( "sp_timeshift" ) == 0
		if ( file.onTimeshiftLevel )
			thread TimeshiftClientMarkerPositionThink()
	#endif
}

#if SERVER
	void function Objective_Remind()
	{
		foreach( entity player in GetPlayerArray() )
			ClientCommand( player, "ShowObjective" )
	}

	void function Objective_InitEntity( entity ent )
	{
		if ( ent.GetClassName() == "func_brush" )
			return

		// Add the entity to a valid list of highlightable objective entities
		// (allows highlighting to be enabled without errors before everyone updates their script to call this function)
		file.objectiveEntities.append( ent )

		// Make the entity always render so the highlighting wont stop working
		ent.DisableHibernation()
		ent.EnableRenderAlways()
		ent.kv.fadedist = 99999

		// Highlight and unhighlight to precache the highlight ability on this entity (required by code)
		Highlight_SetNeutralHighlight( ent, OBJECTIVE_HIGHLIGHT )
		Highlight_ClearNeutralHighlight( ent )
	}

	void function Objective_Set_WithAltHighlight( string objective, vector position = < 0, 0, 0 >, entity ent = null )
	{
		file.objectiveAltHighlight = true
		_ObjectiveSet( objective, position, ent, false )
	}

	void function Objective_SetSilent_WithAltHighlight( string objective, vector position = < 0, 0, 0 >, entity ent = null )
	{
		file.objectiveAltHighlight = true
		_ObjectiveSet( objective, position, ent, true )
	}

	void function Objective_Set( string objective, vector position = < 0, 0, 0 >, entity ent = null )
	{
		file.objectiveAltHighlight = false
		_ObjectiveSet( objective, position, ent, false )
	}

	void function Objective_SetSilent( string objective, vector position = < 0, 0, 0 >, entity ent = null )
	{
		file.objectiveAltHighlight = false
		_ObjectiveSet( objective, position, ent, true )
	}

	void function _ObjectiveSet( string objective, vector position, entity ent, bool silent )
	{
		UpdateMarkerEnt( position, ent )
		SetGlobalNetInt( "objectiveStringIndex", GetObjectiveStringID( objective ) )

		ClearObjectiveHighlight()
		file.objectiveHighlightEntArray = [ ent ]
		file.objectiveAltHighlightEntArray = []
		SetGlobalNetBool( "hilightingObjective", IsValid( ent ) )
		SetGlobalNetFloat( "additionalKilometers", 0.0 )

		if ( !silent )
			thread ShowNewObjective()

		int stringIndex = GetGlobalNetInt( "objectiveStringIndex" )
		foreach( entity player in GetPlayerArray() )
			Remote_CallFunction_UI( player, "ServerCallback_UI_ObjectiveUpdated", stringIndex )
	}

	void function ShowNewObjective()
	{
		wait 0.1 // give time to sent position and entity to client
		foreach( entity player in GetPlayerArray() )
			thread ShowObjectiveForDuration( player, OBJECTIVE_DISPLAY_TOTAL_TIME )
	}

	void function Objective_AddKilometers( float kilometers )
	{
		Assert( kilometers >= 0.0 && kilometers <= 128.0 )
		SetGlobalNetFloat( "additionalKilometers", kilometers )
	}

	void function Objective_AddHighlightEntity( entity ent )
	{
		file.objectiveHighlightEntArray.append( ent )
	}

	void function Objective_StaticModelHighlightOverrideEntity( entity ent )
	{
		ent.kv.rendermode = 3
		ent.kv.renderamt = 1
		ent.Hide()
		file.objectiveAltHighlightEntArray.append( ent )
	}

	void function Objective_StaticModelHighlightOverrideEntityArray( array<entity> ents )
	{
		foreach ( ent in ents )
			Objective_StaticModelHighlightOverrideEntity( ent )
	}

	void function Objective_Clear()
	{
		file.objectiveAltHighlight = false
		ClearObjectiveHighlight()
		file.objectiveHighlightEntArray = []
		file.objectiveAltHighlightEntArray = []
		SetGlobalNetBool( "hilightingObjective", false )
		SetGlobalNetInt( "objectiveStringIndex", 0 )
		SetGlobalNetFloat( "additionalKilometers", 0.0 )

		foreach( entity player in GetPlayerArray() )
		{
			Objective_Hide( player )
			Remote_CallFunction_UI( player, "ServerCallback_UI_ObjectiveUpdated", -1 )  //-1 is no objective
		}
	}

	void function Objective_Update( vector position, entity ent = null )
	{
		UpdateMarkerEnt( position, ent )
		ClearObjectiveHighlight()
		file.objectiveHighlightEntArray = [ ent ]
		file.objectiveAltHighlightEntArray = []
		SetGlobalNetBool( "hilightingObjective", IsValid( ent ) )
	}

	void function UpdateMarkerEnt( vector position, entity ent )
	{
		vector newPos = position
		bool showLine = IsValid( ent ) || (position != <0,0,0>)

		SetGlobalNetBool( "showObjectiveLine", showLine )

		if ( IsValid( ent ) )
		{
			newPos = ent.GetOrigin() + position
		}

		// Create marker ent every time
		RefreshMarkerEnt( newPos )

		if ( IsValid( ent ) )
		{
			if ( ent.GetClassName() != "info_target" )
				file.objectiveMarkerEnt.SetParent( ent, "", true )
			else
				CodeWarning( "Tried to set objective entity to an info_target. This isn't supported because they aren't sent to the client. Try using a script_mover_lightweight or prop_dynamic." )
		}
	}

	void function RefreshMarkerEnt( vector position )
	{
		if ( !IsValid( file.objectiveMarkerEnt ) )
		{
			file.objectiveMarkerEnt = CreateEntity( "info_target" )
			file.objectiveMarkerEnt.kv.spawnflags = SF_INFOTARGET_ALWAYS_TRANSMIT_TO_CLIENT
			DispatchSpawn( file.objectiveMarkerEnt )

			SetGlobalNetEnt( "objectiveMarkerEntity", file.objectiveMarkerEnt )
		}

		file.objectiveMarkerEnt.ClearParent()
		file.objectiveMarkerEnt.SetOrigin( position )
	}

	void function ShowObjectiveForDuration( entity player, float duration )
	{
		EndSignal( player, "OnDeath" )
		EndSignal( player, "OnDestroy" )

		// Make sure thread only runs once
		Signal( player, "ShowingObjective" )
		EndSignal( player, "ShowingObjective" )
		EndSignal( player, "HidingObjective" )

		player.SetPlayerNetBool( "showObjective", true )
		SetObjectiveHighlight()
		file.objectiveLastShownTime = Time()

		wait duration
		Objective_Hide( player )
	}

	void function Objective_Hide( entity player )
	{
		player.SetPlayerNetBool( "showObjective", false )
		ClearObjectiveHighlight()
		Signal( player, "HidingObjective" )
	}

	float function Objective_LastShownTime()
	{
		return file.objectiveLastShownTime
	}

	bool function ClientCommandToggleObjective( entity player, array<string> args )
	{
		bool isShowing = player.GetPlayerNetBool( "showObjective" )

		if ( isShowing )
			thread Objective_Hide( player )
		else
			thread ShowObjectiveForDuration( player, OBJECTIVE_DISPLAY_TOTAL_TIME )

		return true
	}

	bool function ClientCommandShowObjective( entity player, array<string> args )
	{
		if ( args.len() > 0 )
		{
			if ( args[0] == "closedSPMenu" && file.suppressCloseMenuShowsObjective )
				return true
		}

		if ( IsAlive( player ) )
			thread ShowObjectiveForDuration( player, OBJECTIVE_DISPLAY_TOTAL_TIME )
		return true
	}

	void function Objective_SuppressCloseMenuShowsObjective( bool enabled )
	{
		file.suppressCloseMenuShowsObjective = enabled
	}

	void function SetObjectiveHighlight()
	{
		// Alt highlight ent?
		if ( file.objectiveAltHighlightEntArray.len() )
		{
			foreach ( ent in file.objectiveAltHighlightEntArray )
			{
				if ( !IsValid( ent ) )
					continue
				Assert( file.objectiveEntities.contains( ent ), "Tried to use Objective_StaticModelHighlightOverrideEntity on entity that didn't call Objective_InitEntity" )
				ent.Show()
				Highlight_SetNeutralHighlight( ent, OBJECTIVE_HIGHLIGHT )
			}
		}
		else if ( file.objectiveHighlightEntArray.len() )
		{
			foreach ( ent in file.objectiveHighlightEntArray )
			{
				if ( !IsValid( ent ) )
					continue

				if ( file.objectiveEntities.contains( ent ) )
					Highlight_SetNeutralHighlight( ent, OBJECTIVE_HIGHLIGHT )
			}
		}
	}

	void function ClearObjectiveHighlight()
	{
		// Alt highlight ent?
		if ( file.objectiveAltHighlightEntArray.len() )
		{
			foreach ( ent in file.objectiveAltHighlightEntArray )
			{
				if ( !IsValid( ent ) )
					continue
				Assert( file.objectiveEntities.contains( ent ), "Tried to use Objective_StaticModelHighlightOverrideEntity on entity that didn't call Objective_InitEntity" )
				ClearObjectiveHighlight_Internal( ent )
				ent.Hide()
			}
		}
		else if ( file.objectiveHighlightEntArray.len() )
		{
			foreach ( ent in file.objectiveHighlightEntArray )
			{
				if ( !IsValid( ent ) )
					continue
				if ( file.objectiveEntities.contains( ent ) )
					ClearObjectiveHighlight_Internal( ent )
			}
			}
		}

	void function ClearObjectiveHighlight_Internal( entity ent )
	{
		if ( !file.objectiveAltHighlight )
			Highlight_ClearNeutralHighlight( ent )
		else
			Highlight_SetNeutralHighlight( ent, OBJECTIVE_HIGHLIGHT_ALT )
	}

	entity function Objective_GetMarkerEntity()
	{
		return file.objectiveMarkerEnt
	}

	entity function Objective_WayPointEneable( bool enable )
	{
		SetGlobalNetBool( "objectiveMarkerIsWayPoint", enable )
	}

	// Don't use this unless you grab the marker ent and clear it's parent after you're done
	void function Objective_SetFastball( entity bt )
	{
		entity marker = Objective_GetMarkerEntity()
		string tag = "HAND_R"
		int attachID = bt.LookupAttachment( tag )
		vector angles = bt.GetAttachmentAngles( attachID )
		vector offset = AnglesToRight( angles ) * 20
		marker.SetOrigin( bt.GetAttachmentOrigin( attachID ) + offset )
		marker.SetParent( bt, tag, true, 0  )
	}


#endif










#if CLIENT
	void function ObjectiveStringChanged( entity player, int oldString, int newString, bool actuallyChanged )
	{
		if ( file.objectiveRUI == null )
			return

		if ( newString == 0 )
			return

		string objectiveText = GetObjectiveStringFromID( newString )
		RuiSetString( file.objectiveRUI, "objectiveText", objectiveText )
	}

	void function ShowObjectiveLineChanged( entity player, bool old, bool new, bool actuallyChanged )
	{
		if ( file.objectiveRUI == null )
			return

		if ( !new )
			RuiSetBool( file.objectiveRUI, "showLine", false )
		else
			RuiSetBool( file.objectiveRUI, "showLine", true )

		if ( GetGlobalNetBool( "hilightingObjective" ) == true || !new )
			RuiSetBool( file.objectiveRUI, "showMarker", false )
		else
			RuiSetBool( file.objectiveRUI, "showMarker", true )
	}

	void function ShowObjectiveChanged( entity player, bool wasShowing, bool isShowing, bool actuallyChanged )
	{
		if ( !actuallyChanged )
			return

		if ( isShowing )
			thread ShowObjective( true )
		else
			HideObjective()
	}

	void function ShowObjective( bool newObjective )
	{
		// Make sure only one RUI is displayed at a time
		Signal( clGlobal.levelEnt, "ShowingObjectiveRUI" )
		EndSignal( clGlobal.levelEnt, "ShowingObjectiveRUI" )

		entity marker = GetGlobalNetEnt( "objectiveMarkerEntity" )
		if ( !IsValid( marker ) )
			return

		OnThreadEnd(
			function() : (  )
			{
				HideObjective()
			}
		)

		int stringIndex = GetGlobalNetInt( "objectiveStringIndex" )
		if ( stringIndex == 0 )
			return

		string titleText = newObjective ? "#OBJECTIVE_UPDATED" : "#OBJECTIVE_REMIND"
		bool isWayPoint = GetGlobalNetBool( "objectiveMarkerIsWayPoint" )
		if( isWayPoint )
			titleText = "#OBJECTIVE_WAYPOINT"

		string objectiveText = GetObjectiveStringFromID( stringIndex )
		float additionalKilometers = GetGlobalNetFloat( "additionalKilometers" )

		HideObjective()
		file.objectiveRUI = CreatePermanentCockpitRui( $"ui/sp_objective.rpak", 0 )

		RuiSetGameTime( file.objectiveRUI, "startTime", Time() )
		RuiSetGameTime( file.objectiveRUI, "endTime", Time() + OBJECTIVE_DISPLAY_TIME )
		RuiSetFloat( file.objectiveRUI, "fadeInDuration", FADE_IN_TIME )
		RuiSetFloat( file.objectiveRUI, "fadeOutDuration", FADE_OUT_TIME )
		RuiSetFloat( file.objectiveRUI, "blingDuration", newObjective ? BLING_DURATION : 0.0 )
		RuiSetString( file.objectiveRUI, "objectiveTitleText", titleText )
		RuiSetString( file.objectiveRUI, "objectiveText", objectiveText )
		RuiSetBool( file.objectiveRUI, "showButtonHint", newObjective )
		
		// handle objective position in code on timeshift levels 
		if ( !file.onTimeshiftLevel )
			RuiTrackFloat3( file.objectiveRUI, "pos", marker, RUI_TRACK_ABSORIGIN_FOLLOW )
			
		RuiSetFloat( file.objectiveRUI, "additionalKilometers", additionalKilometers )

		if ( !GetGlobalNetBool( "showObjectiveLine" ) )
			RuiSetBool( file.objectiveRUI, "showLine", false )

		if ( GetGlobalNetBool( "hilightingObjective" ) == true || !GetGlobalNetBool( "showObjectiveLine" ) )
			RuiSetBool( file.objectiveRUI, "showMarker", false )

		EmitSoundOnEntity( GetLocalClientPlayer(), "ui_holotutorial_analyzingfinish" )

		wait OBJECTIVE_DISPLAY_TOTAL_TIME
	}

	void function HideObjective()
	{
		if ( file.objectiveRUI != null )
		{
			RuiDestroyIfAlive( file.objectiveRUI )
			file.objectiveRUI = null
		}
	}
	
	void function TimeshiftClientMarkerPositionThink()
	{
		while ( true )
		{
			WaitFrame()
			
			if ( IsValid( file.objectiveRUI ) )
			{
				// marker is how the server tells us the objective pos
				entity marker = GetGlobalNetEnt( "objectiveMarkerEntity" )
				if ( !IsValid( marker ) ) 
					continue // unsure if this can even ever be hit
			
				// get our current timeline and get zoffset for it
				int zOffset = 0
				if ( marker.GetOrigin().z < -6000 ) // frozen time
				{
					RuiTrackFloat3( file.objectiveRUI, "pos", marker, RUI_TRACK_ABSORIGIN_FOLLOW ) 
					break // once we hit frozen time, all objectives from now will be on the same timeline as players so we can use normal objective pos logic
				}
				else if ( marker.GetOrigin().z < 5300 && GetLocalClientPlayer().GetOrigin().z > 5300 ) // marker in night, player in day
					zOffset = TIME_ZOFFSET 
				else if ( marker.GetOrigin().z > 5300 && GetLocalClientPlayer().GetOrigin().z < 5300 ) // marker in day, player in night
					zOffset = TIME_ZOFFSET * -1
					
				// set objective pos
				vector pos = marker.GetOrigin() + Vector( 0, 0, zOffset )
				//RuiSetString( file.objectiveRUI, "objectiveTitleText", "marker: " + marker.GetOrigin() + ", player: " + GetLocalClientPlayer().GetOrigin() + ", offset: " + zOffset )
				RuiSetFloat3( file.objectiveRUI, "pos", pos )
			}
		}
	}

#endif