aboutsummaryrefslogtreecommitdiff
path: root/Northstar.Client/mod/scripts/vscripts/ui/menu_match_settings.nut
blob: fb6cde1a953337b47ceac226e821ea3c00d3ff54 (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
untyped

global function InitMatchSettingsMenu

global function UpdateMatchSettingsForGamemode
global function MatchSettings_FormatPlaylistVarValue

global const table< string, string > MatchSettings_PlaylistVarLabels = {
	match_visibility = "#PM_MATCH_VISIBILITY",
	roundscorelimit = "#PM_SCORE_LIMIT",
	scorelimit = "#PM_SCORE_LIMIT",
	roundtimelimit = "#PM_TIME_LIMIT",
	timelimit = "#PM_TIME_LIMIT",
	boosts_enabled = "#PM_PILOT_BOOSTS",
	earn_meter_pilot_overdrive = "#PM_PILOT_OVERDRIVE",
	earn_meter_pilot_multiplier = "#PM_PILOT_EARN",
	pilot_health_multiplier = "#PM_PILOT_HEALTH",
	respawn_delay = "#PM_PILOT_RESPAWN_DELAY",
	riff_titan_availability = "#PM_TITAN_AVAILABILITY",
	earn_meter_titan_multiplier = "#PM_TITAN_EARN",
}

struct PlaylistVarValueFormat
{
	string localizeStr
	string formatStr
	table< int, string > enumStrs
}

const string FORMAT_PERCENTAGE = "%d%%"

struct {
	var menu = null
	var matchVisibilityButton = null
	var scoreLimitButton = null
	var scoreLimitLabel = null
	var timeLimitButton = null
	var timeLimitLabel = null
	var pilotBoostsButton = null
	var pilotOverdriveButton = null
	var pilotEarnButton = null
	var pilotEarnLabel = null
	var pilotHealthButton = null
	var pilotHealthLabel = null
	var pilotRespawnDelayButton = null
	var pilotRespawnDelayLabel = null
	var titanAvailabilityButton = null
	var titanEarnButton = null
	var titanEarnLabel = null
	var gameModeLabel = null
	var matchSettingDescLabel = null
	string modeSettingsName
	bool isModeRoundBased
	table< string, PlaylistVarValueFormat > playlistVarValueFormats
} file

struct
{
	table< string, float > timelimit = {
		min = 1.0,
		max = 40.0,
		step = 1.0,
	}

	// pilot
	table< string, float > pilot_health_multiplier = {
		min = 0.25,
		max = 5.0,
		step = 0.25,
	}

	table< string, float > respawn_delay = {
		min = 0.0,
		max = 40.0,
		step = 0.5,
	}

	table< string, float > earn_meter_pilot_multiplier = {
		min = 0.25,
		max = 5.0,
		step = 0.25,
	}

	// titan
	table< string, float > earn_meter_titan_multiplier = {
		min = 0.25,
		max = 5.0,
		step = 0.25,
	}

	// gamemode
	table< string, table< string, float > > scorelimit = {
		at = {
			min = 4000.0,
			max = 6000.0,
			step = 200.0,
		}
		ctf = {
			min = 2.0,
			max = 10.0,
			step = 1.0,
		},
		lts = {
			min = 2.0,
			max = 9.0,
			step = 1.0,
		},
		cp = {
			min = 150.0,
			max = 750.0,
			step = 50.0,
		},
		mfd = {
			min = 1.0,
			max = 25.0,
			step = 2.0,
		},
	}
} customMatchSettings

void function InitMatchSettingsMenu()
{
	RegisterUIVarChangeCallback( "privatematch_starting", Privatematch_starting_Changed )

	var menu = GetMenu( "MatchSettingsMenu" )
	file.menu = menu

	AddMenuEventHandler( menu, eUIEvent.MENU_OPEN, OnOpenMatchSettingsMenu )
	AddMenuEventHandler( menu, eUIEvent.MENU_NAVIGATE_BACK, OnMatchSettingsMenu_NavigateBack )

	file.matchVisibilityButton = Hud_GetChild( menu, "BtnMatchVisibility" )
	SetButtonRuiText( file.matchVisibilityButton, MatchSettings_PlaylistVarLabels["match_visibility"] )
	PlaylistVarValueFormat matchVisibilityFormat = {
		enumStrs = {
			[MATCHVISIBILITY_PRIVATE] = "#MATCH_VISIBILITY_PRIVATE",
			[MATCHVISIBILITY_LOCKED] = "#MATCH_VISIBILITY_LOCKED",
		},
		...
	}
	file.playlistVarValueFormats["match_visibility"] <- matchVisibilityFormat
	foreach ( int enum_, string val in matchVisibilityFormat.enumStrs )
		Hud_DialogList_AddListItem( file.matchVisibilityButton, val, string( enum_ ) )

	file.scoreLimitButton = Hud_GetChild( menu, "BtnScoreLimit" )
	Hud_AddEventHandler( file.scoreLimitButton, UIE_CHANGE, SetScoreLimitText )
	SetButtonRuiText( Hud_GetChild( file.scoreLimitButton, "BtnDropButton" ), MatchSettings_PlaylistVarLabels["scorelimit"] )
	file.scoreLimitLabel = Hud_GetChild( menu, "LblScoreLimit" )
	PlaylistVarValueFormat roundScoreLimitFormat = {
		localizeStr = "#N_ROUND_WINS",
		...
	}
	file.playlistVarValueFormats["roundscorelimit"] <- roundScoreLimitFormat
	PlaylistVarValueFormat scoreLimitFormat = {
		localizeStr = "#N_POINTS",
		...
	}
	file.playlistVarValueFormats["scorelimit"] <- scoreLimitFormat

	file.timeLimitButton = Hud_GetChild( menu, "BtnTimeLimit" )
	Hud_AddEventHandler( file.timeLimitButton, UIE_CHANGE, SetTimeLimitText )
	SetButtonRuiText( Hud_GetChild( file.timeLimitButton, "BtnDropButton" ), MatchSettings_PlaylistVarLabels["timelimit"] )
	file.timeLimitLabel = Hud_GetChild( menu, "LblTimeLimit" )
	PlaylistVarValueFormat roundTimeLimitFormat = {
		localizeStr = "#N_MINUTE_ROUNDS",
		...
	}
	file.playlistVarValueFormats["roundtimelimit"] <- roundTimeLimitFormat
	PlaylistVarValueFormat timeLimitFormat = {
		localizeStr = "#N_MINUTES",
		...
	}
	file.playlistVarValueFormats["timelimit"] <- timeLimitFormat

	// pilot

	file.pilotBoostsButton = Hud_GetChild( menu, "BtnPilotBoosts" )
	SetButtonRuiText( file.pilotBoostsButton, MatchSettings_PlaylistVarLabels["boosts_enabled"] )
	PlaylistVarValueFormat pilotBoostsFormat = {
		enumStrs = {
			[eBoostAvailability.Default] = "#PILOT_BOOSTS_DEFAULT",
			[eBoostAvailability.Disabled] = "#PILOT_BOOSTS_DISABLED",
		},
		...
	}
	file.playlistVarValueFormats["boosts_enabled"] <- pilotBoostsFormat
	foreach ( int enum_, string val in pilotBoostsFormat.enumStrs )
		Hud_DialogList_AddListItem( file.pilotBoostsButton, val, string( enum_ ) )

	file.pilotOverdriveButton = Hud_GetChild( menu, "BtnPilotOverdrive" )
	SetButtonRuiText( file.pilotOverdriveButton, MatchSettings_PlaylistVarLabels["earn_meter_pilot_overdrive"] )
	PlaylistVarValueFormat pilotOverdriveFormat = {
		enumStrs = {
			[ePilotOverdrive.Enabled] = "#PILOT_OVERDRIVE_ON",
			[ePilotOverdrive.Disabled] = "#PILOT_OVERDRIVE_OFF",
			[ePilotOverdrive.Only] = "#PILOT_OVERDRIVE_ONLY",
		},
		...
	}
	file.playlistVarValueFormats["earn_meter_pilot_overdrive"] <- pilotOverdriveFormat
	foreach ( int enum_, string val in pilotOverdriveFormat.enumStrs )
		Hud_DialogList_AddListItem( file.pilotOverdriveButton, val, string( enum_ ) )

	file.pilotEarnButton = Hud_GetChild( menu, "BtnPilotEarn" )
	Hud_AddEventHandler( file.pilotEarnButton, UIE_CHANGE, SetPilotEarnText )
	SetButtonRuiText( Hud_GetChild( file.pilotEarnButton, "BtnDropButton" ), MatchSettings_PlaylistVarLabels["earn_meter_pilot_multiplier"] )
	file.pilotEarnLabel = Hud_GetChild( menu, "LblPilotEarn" )
	PlaylistVarValueFormat pilotEarnFormat = {
		formatStr = FORMAT_PERCENTAGE,
		...
	}
	file.playlistVarValueFormats["earn_meter_pilot_multiplier"] <- pilotEarnFormat

	file.pilotHealthButton = Hud_GetChild( menu, "BtnPilotHealth" )
	Hud_AddEventHandler( file.pilotHealthButton, UIE_CHANGE, SetPilotHealthText )
	SetButtonRuiText( Hud_GetChild( file.pilotHealthButton, "BtnDropButton" ), MatchSettings_PlaylistVarLabels["pilot_health_multiplier"] )
	file.pilotHealthLabel = Hud_GetChild( menu, "LblPilotHealth" )
	PlaylistVarValueFormat pilotHealthFormat = {
		formatStr = FORMAT_PERCENTAGE,
		...
	}
	file.playlistVarValueFormats["pilot_health_multiplier"] <- pilotHealthFormat

	file.pilotRespawnDelayButton = Hud_GetChild( menu, "BtnPilotRespawnDelay" )
	Hud_AddEventHandler( file.pilotRespawnDelayButton, UIE_CHANGE, SetPilotRespawnDelayText )
	SetButtonRuiText( Hud_GetChild( file.pilotRespawnDelayButton, "BtnDropButton" ), MatchSettings_PlaylistVarLabels["respawn_delay"] )
	file.pilotRespawnDelayLabel = Hud_GetChild( menu, "LblPilotRespawnDelay" )
	PlaylistVarValueFormat pilotRespawnDelayFormat = {
		localizeStr = "#N_SECONDS",
		formatStr = "%2.1f",
		...
	}
	file.playlistVarValueFormats["respawn_delay"] <- pilotRespawnDelayFormat

	// titan

	file.titanAvailabilityButton = Hud_GetChild( menu, "BtnTitanAvailability" )
	SetButtonRuiText( file.titanAvailabilityButton, MatchSettings_PlaylistVarLabels["riff_titan_availability"] )
	PlaylistVarValueFormat titanAvailabilityFormat = {
		enumStrs = {
			[eTitanAvailability.Default] = "#TITAN_AVAILABILITY_DEFAULT",
			[eTitanAvailability.Never] = "#TITAN_AVAILABILITY_NEVER",
		},
		...
	}
	file.playlistVarValueFormats["riff_titan_availability"] <- titanAvailabilityFormat
	foreach ( int enum_, string val in titanAvailabilityFormat.enumStrs )
		Hud_DialogList_AddListItem( file.titanAvailabilityButton, val, string( enum_ ) )

	file.titanEarnButton = Hud_GetChild( menu, "BtnTitanEarn" )
	Hud_AddEventHandler( file.titanEarnButton, UIE_CHANGE, SetTitanEarnText )
	SetButtonRuiText( Hud_GetChild( file.titanEarnButton, "BtnDropButton" ), MatchSettings_PlaylistVarLabels["earn_meter_titan_multiplier"] )
	file.titanEarnLabel = Hud_GetChild( menu, "LblTitanEarn" )
	PlaylistVarValueFormat titanEarnFormat = {
		formatStr = FORMAT_PERCENTAGE,
		...
	}
	file.playlistVarValueFormats["earn_meter_titan_multiplier"] <- titanEarnFormat

	file.gameModeLabel = Hud_GetChild( menu, "LblModeSubheaderText" )

	file.matchSettingDescLabel = Hud_GetChild( menu, "LblMenuItemDescription" )

	AddDescFocusHandler( file.matchVisibilityButton, "#PM_DESC_MATCH_VISIBILITY" )
	AddDescFocusHandler( file.scoreLimitButton, "#PM_DESC_SCORE_LIMIT" )
	AddDescFocusHandler( file.timeLimitButton, "#PM_DESC_TIME_LIMIT" )
	AddDescFocusHandler( file.pilotBoostsButton, "#PM_DESC_PILOT_BOOSTS" )
	AddDescFocusHandler( file.pilotOverdriveButton, "#PM_DESC_PILOT_OVERDRIVE" )
	AddDescFocusHandler( file.pilotEarnButton, "#PM_DESC_PILOT_EARN" )
	AddDescFocusHandler( file.pilotHealthButton, "#PM_DESC_PILOT_HEALTH" )
	AddDescFocusHandler( file.pilotRespawnDelayButton, "#PM_DESC_PILOT_RESPAWN_DELAY" )
	AddDescFocusHandler( file.titanAvailabilityButton, "#PM_DESC_TITAN_AVAILABILITY" )
	AddDescFocusHandler( file.titanEarnButton, "#PM_DESC_TITAN_EARN" )

	AddMenuFooterOption( menu, BUTTON_B, "#B_BUTTON_BACK", "#BACK" )
	AddMenuFooterOption( menu, BUTTON_Y, "#Y_BUTTON_RESTORE_DEFAULTS", "#RESTORE_DEFAULTS", ResetMatchSettingsToDefaultDialog )
}

void function SetControlGamemodeAndPlaylistVar( var button, int gamemodeIdx, string playlistVar )
{
	Hud_SetGamemodeIdx( button, gamemodeIdx )
	Hud_SetPlaylistVarName( button, playlistVar )
}

void function UpdateMatchSettingsForGamemode()
{
	file.modeSettingsName = PrivateMatch_GetSelectedMode()
	Hud_SetText( file.gameModeLabel, GetGameModeDisplayName( file.modeSettingsName ) )

	Hud_SetPlaylistVarName( file.matchVisibilityButton, "match_visibility" )
	SetGameModeSettings()

	int gamemodeIdx = expect int( level.ui.privatematch_mode )

	// pilot

	SetControlGamemodeAndPlaylistVar( file.pilotBoostsButton, gamemodeIdx, "boosts_enabled" )
	SetControlGamemodeAndPlaylistVar( file.pilotOverdriveButton, gamemodeIdx, "earn_meter_pilot_overdrive" )
	SetSliderSettings( file.pilotHealthButton, customMatchSettings.pilot_health_multiplier, gamemodeIdx, "pilot_health_multiplier" )
	SetSliderSettings( file.pilotRespawnDelayButton, customMatchSettings.respawn_delay, gamemodeIdx, "respawn_delay" )
	SetSliderSettings( file.pilotEarnButton, customMatchSettings.earn_meter_pilot_multiplier, gamemodeIdx, "earn_meter_pilot_multiplier" )

	// titan

	SetControlGamemodeAndPlaylistVar( file.titanAvailabilityButton, gamemodeIdx, "riff_titan_availability" )
	SetSliderSettings( file.titanEarnButton, customMatchSettings.earn_meter_titan_multiplier, gamemodeIdx, "earn_meter_titan_multiplier" )
}

void function OnOpenMatchSettingsMenu()
{
	UpdateMatchSettingsForGamemode()
	Hud_SetFocused( file.matchVisibilityButton )
}

void function OnMatchSettingsMenu_NavigateBack()
{
	CloseActiveMenu()
}

function Privatematch_starting_Changed()
{
	if ( GetActiveMenu() == file.menu )
		CloseActiveMenu()
}

void function SetSliderSettings( var button, table< string, float > settings, int gamemodeIdx, string playlistVar )
{
	Hud_SliderControl_SetStepSize( button, settings.step )
	Hud_SliderControl_SetMin( button, settings.min )
	Hud_SliderControl_SetMax( button, settings.max )
	Hud_SetGamemodeIdx( button, gamemodeIdx )
	Hud_SetPlaylistVarName( button, playlistVar )
	// TODO: the label should be part of the SliderControl, so there is a direct
	// reference between the elements that make up the control. See the callbacks
	// listening for this event and find that they have to specify their elements
	// explicity as opposed to generically referencing releative child element(s).
	// XXX: fire the change event to update a label with the current value
	Hud_HandleEvent( button, UIE_CHANGE )
}

void function SetGameModeSettings()
{
	table< string, float > scoreSettings = {
		min = -1.0,
		max = -1.0,
		step = -1.0,
	}
	if ( file.modeSettingsName in customMatchSettings.scorelimit )
		scoreSettings = customMatchSettings.scorelimit[file.modeSettingsName]

	int gamemodeIdx = expect int( level.ui.privatematch_mode )
	string scoreLimitVar = "roundscorelimit"
	string scoreLimitStr = GetCurrentPlaylistGamemodeByIndexVar( gamemodeIdx, scoreLimitVar, false )
	string timeLimitVar = "roundtimelimit"
	if ( scoreLimitStr.len() )
	{
		file.isModeRoundBased = true
	}
	else
	{
		file.isModeRoundBased = false
		scoreLimitVar = "scorelimit"
		scoreLimitStr = GetCurrentPlaylistGamemodeByIndexVar( gamemodeIdx, scoreLimitVar, false )
		timeLimitVar = "timelimit"
	}

	int scoreLimit = scoreLimitStr.len() ? int( scoreLimitStr ) : 100;
	if ( scoreSettings.min <= 0 )
	{
		scoreSettings.min = max( scoreLimit / 5.0, 1.0 )
		scoreSettings.max = max( scoreLimit + (scoreLimit - scoreSettings.min), 1.0 )
		scoreSettings.step = scoreSettings.min
	}

	SetSliderSettings( file.scoreLimitButton, scoreSettings, gamemodeIdx, scoreLimitVar )

	table< string, float > timeSettings = customMatchSettings.timelimit
	SetSliderSettings( file.timeLimitButton, timeSettings, gamemodeIdx, timeLimitVar )
}

string function MatchSettings_FormatPlaylistVarValue( string playlistVar, float value )
{
	if ( !(playlistVar in file.playlistVarValueFormats) )
		return "#" + playlistVar + " unknown"

	PlaylistVarValueFormat fmt = file.playlistVarValueFormats[playlistVar]
	if ( fmt.formatStr.len() )
	{
		if ( fmt.formatStr == FORMAT_PERCENTAGE )
			return format( fmt.formatStr, int( value * 100 ) )
		else
			return Localize( fmt.localizeStr, format( fmt.formatStr, value ) )
	}

	if ( fmt.enumStrs.len() )
		return Localize( fmt.enumStrs[int( value )] )

	return Localize( fmt.localizeStr, int( value ) )
}

void function SetScoreLimitText( var button )
{
	string playlistVar = file.isModeRoundBased ? "roundscorelimit" : "scorelimit"
	float scoreLimit = Hud_SliderControl_GetCurrentValue( button )
	string labelText = MatchSettings_FormatPlaylistVarValue( playlistVar, scoreLimit )
	Hud_SetText( file.scoreLimitLabel, labelText )
}

void function SetTimeLimitText( var button )
{
	string playlistVar = file.isModeRoundBased ? "roundtimelimit" : "timelimit"
	float timeLimit = Hud_SliderControl_GetCurrentValue( button )
	string labelText = MatchSettings_FormatPlaylistVarValue( playlistVar, timeLimit )
	Hud_SetText( file.timeLimitLabel, labelText )
}

void function SetPilotHealthText( var button )
{
	float healthMultiplier = Hud_SliderControl_GetCurrentValue( button )
	string labelText = MatchSettings_FormatPlaylistVarValue( "pilot_health_multiplier", healthMultiplier )
	Hud_SetText( file.pilotHealthLabel, labelText )
}

void function SetPilotRespawnDelayText( var button )
{
	float respawnDelay = Hud_SliderControl_GetCurrentValue( button )
	string labelText = MatchSettings_FormatPlaylistVarValue( "respawn_delay", respawnDelay )
	Hud_SetText( file.pilotRespawnDelayLabel, labelText )
}

void function SetPilotEarnText( var button )
{
	float earnMultiplier = Hud_SliderControl_GetCurrentValue( button )
	string labelText = MatchSettings_FormatPlaylistVarValue( "earn_meter_pilot_multiplier", earnMultiplier )
	Hud_SetText( file.pilotEarnLabel, labelText )
}

void function SetTitanEarnText( var button )
{
	float earnMultiplier = Hud_SliderControl_GetCurrentValue( button )
	string labelText = MatchSettings_FormatPlaylistVarValue( "earn_meter_titan_multiplier", earnMultiplier )
	Hud_SetText( file.titanEarnLabel, labelText )
}

void function AddDescFocusHandler( var button, string descText )
{
	button.s.descText <- descText
	Hud_AddEventHandler( button, UIE_GET_FOCUS, MatchSettingsFocusUpdate )
	if ( !Hud_HasChild( button, "BtnDropButton" ) )
		return

	var child = Hud_GetChild( button, "BtnDropButton" )
	child.s.descText <- descText
	Hud_AddEventHandler( child, UIE_GET_FOCUS, MatchSettingsFocusUpdate )

	child = Hud_GetChild( button, "PnlDefaultMark" )
	child.SetColor( [0,0,0,0] )
	Hud_Hide( child )
}

void function MatchSettingsFocusUpdate( var button )
{
	RuiSetString( Hud_GetRui( file.matchSettingDescLabel ), "description", button.s.descText )
}

void function ResetMatchSettingsToDefaultDialog( var button )
{
	ClientCommand( "ResetMatchSettingsToDefault" )
}