summaryrefslogtreecommitdiff
path: root/sourcemod/scripting/gokz-hud/tp_menu.sp
blob: bb78f1e4333f662ae72f8a26125975c1fdf8a417 (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
/*
	Lets players easily use teleport functionality.
	
	This menu is displayed whenever the player is alive and there is
	currently no other menu displaying.
*/



#define ITEM_INFO_CHECKPOINT "cp"
#define ITEM_INFO_TELEPORT "tp"
#define ITEM_INFO_PREV "prev"
#define ITEM_INFO_NEXT "next"
#define ITEM_INFO_UNDO "undo"
#define ITEM_INFO_PAUSE "pause"
#define ITEM_INFO_START "start"

static bool oldCanMakeCP[MAXPLAYERS + 1];
static bool oldCanTP[MAXPLAYERS + 1];
static bool oldCanPrevCP[MAXPLAYERS + 1];
static bool oldCanNextCP[MAXPLAYERS + 1];
static bool oldCanUndoTP[MAXPLAYERS + 1];
static bool oldCanPause[MAXPLAYERS + 1];
static bool oldCanResume[MAXPLAYERS + 1];
static bool forceRefresh[MAXPLAYERS + 1];

// =====[ EVENTS ]=====

void OnPlayerRunCmdPost_TPMenu(int client, int cmdnum, HUDInfo info)
{
	int updateSpeed = gB_FastUpdateRate[client] ? 3 : 6;
	if (cmdnum % updateSpeed == 2)
	{
		UpdateTPMenu(client, info);
	}
}

public int PanelHandler_Menu(Menu menu, MenuAction action, int param1, int param2)
{
	if (action == MenuAction_Cancel)
	{
		gB_MenuShowing[param1] = false;
	}
	return 0;
}

public int MenuHandler_TPMenu(Menu menu, MenuAction action, int param1, int param2)
{
	if (action == MenuAction_Select)
	{
		char info[16];
		menu.GetItem(param2, info, sizeof(info));
		
		if (StrEqual(info, ITEM_INFO_CHECKPOINT, false))
		{
			GOKZ_MakeCheckpoint(param1);
		}
		else if (StrEqual(info, ITEM_INFO_TELEPORT, false))
		{
			GOKZ_TeleportToCheckpoint(param1);
		}
		else if (StrEqual(info, ITEM_INFO_PREV, false))
		{
			GOKZ_PrevCheckpoint(param1);
		}
		else if (StrEqual(info, ITEM_INFO_NEXT, false))
		{
			GOKZ_NextCheckpoint(param1);
		}
		else if (StrEqual(info, ITEM_INFO_UNDO, false))
		{
			GOKZ_UndoTeleport(param1);
		}
		else if (StrEqual(info, ITEM_INFO_PAUSE, false))
		{
			GOKZ_TogglePause(param1);
		}
		else if (StrEqual(info, ITEM_INFO_START, false))
		{
			GOKZ_TeleportToStart(param1);
		}
		
		// Menu closes when player selects something, so...
		gB_MenuShowing[param1] = false;
	}
	else if (action == MenuAction_Cancel)
	{
		gB_MenuShowing[param1] = false;
	}
	else if (action == MenuAction_End)
	{
		delete menu;
	}
	return 0;
}

// =====[ PUBLIC ]=====
void SetForceUpdateTPMenu(int client)
{
	forceRefresh[client] = true;
}

// =====[ PRIVATE ]=====

static void UpdateTPMenu(int client, HUDInfo info)
{
	KZPlayer player = KZPlayer(client);
	
	if (player.Fake)
	{
		return;
	}
	
	bool force = forceRefresh[client]
		|| player.CanMakeCheckpoint != oldCanMakeCP[client]
		|| player.CanTeleportToCheckpoint != oldCanTP[client]
		|| player.CanPrevCheckpoint != oldCanPrevCP[client]
		|| player.CanNextCheckpoint != oldCanNextCP[client]
		|| player.CanUndoTeleport != oldCanUndoTP[client]
		|| player.CanPause != oldCanPause[client]
		|| player.CanResume != oldCanResume[client];

	
	if (player.Alive)
	{
		if (player.TPMenu != TPMenu_Disabled)
		{
			if (GetClientMenu(client) == MenuSource_None
				|| gB_MenuShowing[player.ID] && GetClientAvgLoss(player.ID, NetFlow_Both) > EPSILON
				|| gB_MenuShowing[player.ID] && player.TimerRunning && !player.Paused && player.TimerText == TimerText_TPMenu
				|| gB_MenuShowing[player.ID] && force)
			{
				ShowTPMenu(player, info);
			}
		}
		else
		{
			// There is no need to update this very often as there's no menu selection to be done here.
			if (GetClientMenu(client) == MenuSource_None
				|| gB_MenuShowing[player.ID] && player.TimerRunning && !player.Paused && player.TimerText == TimerText_TPMenu)
			{
				ShowPanel(player, info);
			}
		}
	}
	else if (player.ObserverTarget != -1) // If the player is spectating someone else
	{
		// Check if the replay plugin wants to display the replay control menu.
		if (!(IsFakeClient(player.ObserverTarget) && gB_GOKZReplays && GOKZ_RP_UpdateReplayControlMenu(client)))
		{
			ShowPanel(player, info);
		}
	}
	
	oldCanMakeCP[client] = player.CanMakeCheckpoint;
	oldCanTP[client] = player.CanTeleportToCheckpoint;
	oldCanPrevCP[client] = player.CanPrevCheckpoint;
	oldCanNextCP[client] = player.CanNextCheckpoint;
	oldCanUndoTP[client] = player.CanUndoTeleport;
	oldCanPause[client] = player.CanPause;
	oldCanResume[client] = player.CanResume;
	forceRefresh[client] = false;
}

static void ShowPanel(KZPlayer player, HUDInfo info)
{
	char panelTitle[256];
	// Spectator List
	if (player.ShowSpectators >= ShowSpecs_Number && player.SpecListPosition == SpecListPosition_TPMenu)
	{
		Format(panelTitle, sizeof(panelTitle), "%s", FormatSpectatorTextForMenu(player, info));
	}
	// Timer panel
	if (player.TimerText == TimerText_TPMenu && info.TimerRunning)
	{
		if (panelTitle[0] != '\0')
		{
			Format(panelTitle, sizeof(panelTitle), "%s \n%s", panelTitle, FormatTimerTextForMenu(player, info));
		}
		else
		{
			Format(panelTitle, sizeof(panelTitle), "%s", FormatTimerTextForMenu(player, info));
		}
		if (info.TimeType == TimeType_Nub && info.CurrentTeleport != 0)
		{
			Format(panelTitle, sizeof(panelTitle), "%s\n%t", panelTitle, "TP Menu - Spectator Teleports", info.CurrentTeleport);
		}
	}

	if (panelTitle[0] != '\0' && GetClientMenu(player.ID) == MenuSource_None || gB_MenuShowing[player.ID])
	{
		Panel panel = new Panel(null);
		panel.SetTitle(panelTitle);
		panel.Send(player.ID, PanelHandler_Menu, MENU_TIME_FOREVER);
		
		delete panel;
		gB_MenuShowing[player.ID] = true;
	}
}

static void ShowTPMenu(KZPlayer player, HUDInfo info)
{
	Menu menu = new Menu(MenuHandler_TPMenu);
	menu.OptionFlags = MENUFLAG_NO_SOUND;
	menu.ExitButton = false;
	menu.Pagination = MENU_NO_PAGINATION;
	TPMenuSetTitle(player, menu, info);
	TPMenuAddItems(player, menu);
	menu.Display(player.ID, MENU_TIME_FOREVER);
	gB_MenuShowing[player.ID] = true;
}

static void TPMenuSetTitle(KZPlayer player, Menu menu, HUDInfo info)
{
	char title[256];
	if (player.ShowSpectators >= ShowSpecs_Number && player.SpecListPosition == SpecListPosition_TPMenu)
	{
		Format(title, sizeof(title), "%s", FormatSpectatorTextForMenu(player, info));
	}
	if (player.TimerRunning && player.TimerText == TimerText_TPMenu)
	{
		if (title[0] != '\0')
		{
			Format(title, sizeof(title), "%s \n%s", title, FormatTimerTextForMenu(player, info));
		}
		else
		{
			Format(title, sizeof(title), "%s", FormatTimerTextForMenu(player, info));
		}
	}
	if (title[0] != '\0')
	{
		menu.SetTitle(title);
	}
}

static void TPMenuAddItems(KZPlayer player, Menu menu)
{
	switch (player.TPMenu)
	{
		case TPMenu_Simple:
		{
			TPMenuAddItemCheckpoint(player, menu);
			TPMenuAddItemTeleport(player, menu);
			TPMenuAddItemPause(player, menu);
			TPMenuAddItemStart(player, menu);
		}
		case TPMenu_Advanced:
		{
			TPMenuAddItemCheckpoint(player, menu);
			TPMenuAddItemTeleport(player, menu);
			TPMenuAddItemPrevCheckpoint(player, menu);
			TPMenuAddItemNextCheckpoint(player, menu);
			TPMenuAddItemUndo(player, menu);
			TPMenuAddItemPause(player, menu);
			TPMenuAddItemStart(player, menu);
		}
	}
}

static void TPMenuAddItemCheckpoint(KZPlayer player, Menu menu)
{
	char display[24];
	FormatEx(display, sizeof(display), "%T", "TP Menu - Checkpoint", player.ID);
	if (player.TimerRunning)
	{
		Format(display, sizeof(display), "%s #%d", display, player.CheckpointCount);
	}

	// Legacy behavior: Always able to make checkpoint attempts.
	if (gI_DynamicMenu[player.ID] == DynamicMenu_Enabled && !player.CanMakeCheckpoint)
	{
		menu.AddItem(ITEM_INFO_CHECKPOINT, display, ITEMDRAW_DISABLED);
	}
	else
	{
		menu.AddItem(ITEM_INFO_CHECKPOINT, display, ITEMDRAW_DEFAULT);
	}
	
}

static void TPMenuAddItemTeleport(KZPlayer player, Menu menu)
{
	char display[24];
	FormatEx(display, sizeof(display), "%T", "TP Menu - Teleport", player.ID);
	if (player.TimerRunning)
	{
		Format(display, sizeof(display), "%s #%d", display, player.TeleportCount);
	}

	// Legacy behavior: Only able to make TP attempts when there is a checkpoint.
	if (gI_DynamicMenu[player.ID] == DynamicMenu_Disabled || player.CanTeleportToCheckpoint)
	{
		menu.AddItem(ITEM_INFO_TELEPORT, display, ITEMDRAW_DEFAULT);
	}
	else
	{
		menu.AddItem(ITEM_INFO_TELEPORT, display, ITEMDRAW_DISABLED);
	}
}

static void TPMenuAddItemPrevCheckpoint(KZPlayer player, Menu menu)
{
	char display[24];
	FormatEx(display, sizeof(display), "%T", "TP Menu - Prev CP", player.ID);

	// Legacy behavior: Only able to do prev CP when there is a previous checkpoint to go back to.
	if (gI_DynamicMenu[player.ID] == DynamicMenu_Disabled || player.CanPrevCheckpoint)
	{
		menu.AddItem(ITEM_INFO_PREV, display, ITEMDRAW_DEFAULT);
	}
	else
	{
		menu.AddItem(ITEM_INFO_PREV, display, ITEMDRAW_DISABLED);
	}
}

static void TPMenuAddItemNextCheckpoint(KZPlayer player, Menu menu)
{
	char display[24];
	FormatEx(display, sizeof(display), "%T", "TP Menu - Next CP", player.ID);

	// Legacy behavior: Only able to do prev CP when there is a next checkpoint to go forward to.
	if (gI_DynamicMenu[player.ID] == DynamicMenu_Disabled || player.CanNextCheckpoint)
	{
		menu.AddItem(ITEM_INFO_NEXT, display, ITEMDRAW_DEFAULT);
	}
	else
	{
		menu.AddItem(ITEM_INFO_NEXT, display, ITEMDRAW_DISABLED);
	}
}

static void TPMenuAddItemUndo(KZPlayer player, Menu menu)
{
	char display[24];
	FormatEx(display, sizeof(display), "%T", "TP Menu - Undo TP", player.ID);

	// Legacy behavior: Only able to attempt to undo TP when it is allowed.
	if (gI_DynamicMenu[player.ID] == DynamicMenu_Disabled || player.CanUndoTeleport)
	{
		menu.AddItem(ITEM_INFO_UNDO, display, ITEMDRAW_DEFAULT);
	}
	else
	{
		menu.AddItem(ITEM_INFO_UNDO, display, ITEMDRAW_DISABLED);
	}

}

static void TPMenuAddItemPause(KZPlayer player, Menu menu)
{
	char display[24];

	// Legacy behavior: Always able to attempt to pause.
	if (gI_DynamicMenu[player.ID] == DynamicMenu_Enabled)
	{
		if (player.Paused)
		{
			FormatEx(display, sizeof(display), "%T", "TP Menu - Resume", player.ID);
			if (player.CanResume)
			{
				menu.AddItem(ITEM_INFO_PAUSE, display, ITEMDRAW_DEFAULT);
			}
			else
			{
				menu.AddItem(ITEM_INFO_PAUSE, display, ITEMDRAW_DISABLED);
			}
		}
		else
		{
			FormatEx(display, sizeof(display), "%T", "TP Menu - Pause", player.ID);
			if (player.CanPause)
			{
				menu.AddItem(ITEM_INFO_PAUSE, display, ITEMDRAW_DEFAULT);
			}
			else
			{
				menu.AddItem(ITEM_INFO_PAUSE, display, ITEMDRAW_DISABLED);
			}
		}
	}
	else
	{
		if (player.Paused)
		{
			FormatEx(display, sizeof(display), "%T", "TP Menu - Resume", player.ID);
		}
		else
		{
			FormatEx(display, sizeof(display), "%T", "TP Menu - Pause", player.ID);
		}
		menu.AddItem(ITEM_INFO_PAUSE, display, ITEMDRAW_DEFAULT);
	}
}

static void TPMenuAddItemStart(KZPlayer player, Menu menu)
{
	char display[24];
	if (player.StartPositionType == StartPositionType_Spawn)
	{
		FormatEx(display, sizeof(display), "%T", "TP Menu - Respawn", player.ID);
		menu.AddItem(ITEM_INFO_START, display, ITEMDRAW_DEFAULT);
	}
	else if (player.TimerRunning)
	{
		FormatEx(display, sizeof(display), "%T", "TP Menu - Restart", player.ID);
		menu.AddItem(ITEM_INFO_START, display, ITEMDRAW_DEFAULT);
	}
	else
	{
		FormatEx(display, sizeof(display), "%T", "TP Menu - Start", player.ID);
		menu.AddItem(ITEM_INFO_START, display, ITEMDRAW_DEFAULT);
	}
}