summaryrefslogtreecommitdiff
path: root/sourcemod/scripting/gokz-tips.sp
blob: 8a84b9e0f6bab94811347a65fef145003c79e252 (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
#include <sourcemod>

#include <gokz/core>
#include <gokz/tips>

#include <autoexecconfig>

#undef REQUIRE_EXTENSIONS
#undef REQUIRE_PLUGIN
#include <updater>

#include <gokz/kzplayer>

#pragma newdecls required
#pragma semicolon 1



public Plugin myinfo = 
{
	name = "GOKZ Tips", 
	author = "DanZay", 
	description = "Prints tips to chat periodically based on loaded plugins", 
	version = GOKZ_VERSION, 
	url = GOKZ_SOURCE_URL
};

#define UPDATER_URL GOKZ_UPDATER_BASE_URL..."gokz-tips.txt"

bool gC_PluginsWithTipsLoaded[TIPS_PLUGINS_COUNT];
ArrayList g_TipPhrases;
int gI_CurrentTip;
Handle gH_TipTimer;
TopMenu gTM_Options;
TopMenuObject gTMO_CatGeneral;
TopMenuObject gTMO_ItemTips;
ConVar gCV_gokz_tips_interval;



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

public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max)
{
	RegPluginLibrary("gokz-tips");
	return APLRes_Success;
}

public void OnPluginStart()
{
	LoadTranslations("gokz-common.phrases");
	LoadTranslations("gokz-tips.phrases");
	LoadTranslations("gokz-tips-tips.phrases");
	LoadTranslations("gokz-tips-core.phrases");

	// Load translations of tips for other GOKZ plugins
	char translation[PLATFORM_MAX_PATH];
	for (int i = 0; i < TIPS_PLUGINS_COUNT; i++)
	{
		FormatEx(translation, sizeof(translation), "gokz-tips-%s.phrases", gC_PluginsWithTips[i]);
		LoadTranslations(translation);
	}

	CreateConVars();
	RegisterCommands();
	CreateTipsTimer();
}

public void OnAllPluginsLoaded()
{
	if (LibraryExists("updater"))
	{
		Updater_AddPlugin(UPDATER_URL);
	}

	char gokzPlugin[PLATFORM_MAX_PATH];
	for (int i = 0; i < TIPS_PLUGINS_COUNT; i++)
	{
		FormatEx(gokzPlugin, sizeof(gokzPlugin), "gokz-%s", gC_PluginsWithTips[i]);
		gC_PluginsWithTipsLoaded[i] = LibraryExists(gokzPlugin);
	}

	TopMenu topMenu;
	if (LibraryExists("gokz-core") && ((topMenu = GOKZ_GetOptionsTopMenu()) != null))
	{
		GOKZ_OnOptionsMenuReady(topMenu);
	}
}

public void OnLibraryAdded(const char[] name)
{
	if (StrEqual(name, "updater"))
	{
		Updater_AddPlugin(UPDATER_URL);
	}

	char gokzPlugin[PLATFORM_MAX_PATH];
	for (int i = 0; i < TIPS_PLUGINS_COUNT; i++)
	{
		FormatEx(gokzPlugin, sizeof(gokzPlugin), "gokz-%s", gC_PluginsWithTips[i]);
		gC_PluginsWithTipsLoaded[i] = gC_PluginsWithTipsLoaded[i] || StrEqual(name, gokzPlugin);
	}
}

public void OnLibraryRemoved(const char[] name)
{
	char gokzPlugin[PLATFORM_MAX_PATH];
	for (int i = 0; i < TIPS_PLUGINS_COUNT; i++)
	{
		FormatEx(gokzPlugin, sizeof(gokzPlugin), "gokz-%s", gC_PluginsWithTips[i]);
		gC_PluginsWithTipsLoaded[i] = gC_PluginsWithTipsLoaded[i] && !StrEqual(name, gokzPlugin);
	}
}



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

public void GOKZ_OnOptionChanged(int client, const char[] option, any newValue)
{
	OnOptionChanged_Options(client, option, newValue);
}



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

public void OnMapStart()
{
	LoadTipPhrases();
}

public void GOKZ_OnOptionsMenuReady(TopMenu topMenu)
{
	OnOptionsMenuReady_Options();
	OnOptionsMenuReady_OptionsMenu(topMenu);
}



// =====[ CONVARS ]=====

void CreateConVars()
{
	AutoExecConfig_SetFile("gokz-tips", "sourcemod/gokz");
	AutoExecConfig_SetCreateFile(true);

	gCV_gokz_tips_interval = AutoExecConfig_CreateConVar("gokz_tips_interval", "75", "How often GOKZ tips are printed to chat in seconds.", _, true, 1.0, false);
	gCV_gokz_tips_interval.AddChangeHook(OnConVarChanged);

	AutoExecConfig_ExecuteFile();
	AutoExecConfig_CleanFile();
}

public void OnConVarChanged(ConVar convar, const char[] oldValue, const char[] newValue)
{
	if (convar == gCV_gokz_tips_interval)
	{
		CreateTipsTimer();
	}
}



// =====[ TIPS ]=====

void LoadTipPhrases()
{
	if (g_TipPhrases == null)
	{
		g_TipPhrases = new ArrayList(64, 0);
	}
	else
	{
		g_TipPhrases.Clear();
	}

	char tipsPath[PLATFORM_MAX_PATH];

	BuildPath(Path_SM, tipsPath, sizeof(tipsPath), "translations/%s", TIPS_TIPS);
	LoadTipPhrasesFromFile(tipsPath);

	BuildPath(Path_SM, tipsPath, sizeof(tipsPath), "translations/%s", TIPS_CORE);
	LoadTipPhrasesFromFile(tipsPath);

	// Load tips for other loaded GOKZ plugins
	for (int i = 0; i < TIPS_PLUGINS_COUNT; i++)
	{
		if (gC_PluginsWithTipsLoaded[i])
		{
			BuildPath(Path_SM, tipsPath, sizeof(tipsPath), "translations/gokz-tips-%s.phrases.txt", gC_PluginsWithTips[i]);
			LoadTipPhrasesFromFile(tipsPath);
		}
	}

	ShuffleTipPhrases();
}

void LoadTipPhrasesFromFile(const char[] filePath)
{
	KeyValues kv = new KeyValues("Phrases");
	if (!kv.ImportFromFile(filePath))
	{
		SetFailState("Failed to load file: \"%s\".", filePath);
	}

	char phraseName[64];
	kv.GotoFirstSubKey(true);
	do
	{
		kv.GetSectionName(phraseName, sizeof(phraseName));
		g_TipPhrases.PushString(phraseName);
	} while (kv.GotoNextKey(true));

	delete kv;
}

void ShuffleTipPhrases()
{
	for (int i = g_TipPhrases.Length - 1; i >= 1; i--)
	{
		int j = GetRandomInt(0, i);
		char tempStringI[64];
		g_TipPhrases.GetString(i, tempStringI, sizeof(tempStringI));
		char tempStringJ[64];
		g_TipPhrases.GetString(j, tempStringJ, sizeof(tempStringJ));
		g_TipPhrases.SetString(i, tempStringJ);
		g_TipPhrases.SetString(j, tempStringI);
	}
}

void CreateTipsTimer()
{
	if (gH_TipTimer != null)
	{
		delete gH_TipTimer;
	}
	gH_TipTimer = CreateTimer(gCV_gokz_tips_interval.FloatValue, Timer_PrintTip, _, TIMER_REPEAT);
}

public Action Timer_PrintTip(Handle timer)
{
	char tip[256];
	g_TipPhrases.GetString(gI_CurrentTip, tip, sizeof(tip));

	for (int client = 1; client <= MaxClients; client++)
	{
		KZPlayer player = KZPlayer(client);
		if (player.InGame && player.Tips != Tips_Disabled)
		{
			GOKZ_PrintToChat(client, true, "%t", tip);
		}
	}

	gI_CurrentTip = NextIndex(gI_CurrentTip, g_TipPhrases.Length);
	return Plugin_Continue;
}



// =====[ OPTIONS ]=====

void OnOptionsMenuReady_Options()
{
	RegisterOption();
}

void RegisterOption()
{
	GOKZ_RegisterOption(TIPS_OPTION_NAME, TIPS_OPTION_DESCRIPTION, 
		OptionType_Int, Tips_Enabled, 0, TIPS_COUNT - 1);
}

void OnOptionChanged_Options(int client, const char[] option, any newValue)
{
	if (StrEqual(option, TIPS_OPTION_NAME))
	{
		switch (newValue)
		{
			case Tips_Disabled:
			{
				GOKZ_PrintToChat(client, true, "%t", "Option - Tips - Disable");
			}
			case Tips_Enabled:
			{
				GOKZ_PrintToChat(client, true, "%t", "Option - Tips - Enable");
			}
		}
	}
}



// =====[ OPTIONS MENU ]=====

void OnOptionsMenuReady_OptionsMenu(TopMenu topMenu)
{
	if (gTM_Options == topMenu)
	{
		return;
	}

	gTM_Options = topMenu;
	gTMO_CatGeneral = gTM_Options.FindCategory(GENERAL_OPTION_CATEGORY);
	gTMO_ItemTips = gTM_Options.AddItem(TIPS_OPTION_NAME, TopMenuHandler_Tips, gTMO_CatGeneral);
}

public void TopMenuHandler_Tips(TopMenu topmenu, TopMenuAction action, TopMenuObject topobj_id, int param, char[] buffer, int maxlength)
{
	if (topobj_id != gTMO_ItemTips)
	{
		return;
	}

	if (action == TopMenuAction_DisplayOption)
	{
		if (GOKZ_GetOption(param, TIPS_OPTION_NAME) == Tips_Disabled)
		{
			FormatEx(buffer, maxlength, "%T - %T", 
				"Options Menu - Tips", param, 
				"Options Menu - Disabled", param);
		}
		else
		{
			FormatEx(buffer, maxlength, "%T - %T", 
				"Options Menu - Tips", param, 
				"Options Menu - Enabled", param);
		}
	}
	else if (action == TopMenuAction_SelectOption)
	{
		GOKZ_CycleOption(param, TIPS_OPTION_NAME);
		gTM_Options.Display(param, TopMenuPosition_LastCategory);
	}
}



// =====[ COMMANDS ]=====

void RegisterCommands()
{
	RegConsoleCmd("sm_tips", CommandToggleTips, "[KZ] Toggle seeing help and tips.");
}

public Action CommandToggleTips(int client, int args)
{
	if (GOKZ_GetOption(client, TIPS_OPTION_NAME) == Tips_Disabled)
	{
		GOKZ_SetOption(client, TIPS_OPTION_NAME, Tips_Enabled);
	}
	else
	{
		GOKZ_SetOption(client, TIPS_OPTION_NAME, Tips_Disabled);
	}
	return Plugin_Handled;
}