summaryrefslogtreecommitdiff
path: root/sourcemod/scripting/gokz-paint.sp
blob: 3de93a79ec25f09e6dad056bf23c79f251a8e9dd (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
#include <sourcemod>

#include <gokz/core>
#include <gokz/paint>

#undef REQUIRE_EXTENSIONS
#undef REQUIRE_PLUGIN
#include <updater>

#pragma newdecls required
#pragma semicolon 1



// Credit to SlidyBat for a large part of the painting code (https://forums.alliedmods.net/showthread.php?p=2541664)
// Credit to Cabbage McGravel of the MomentumMod team for making the textures

public Plugin myinfo = 
{
	name = "GOKZ Paint", 
	author = "zealain", 
	description = "Provides client sided paint for visibility", 
	version = GOKZ_VERSION, 
	url = GOKZ_SOURCE_URL
};

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

char gC_PaintColors[][32] =
{
	"paint_red",
	"paint_white",
	"paint_black",
	"paint_blue",
	"paint_brown",
	"paint_green",
	"paint_yellow",
	"paint_purple"
};

char gC_PaintSizePostfix[][8] =
{
	"_small",
	"_med",
	"_large"
};

int gI_Decals[sizeof(gC_PaintColors)][sizeof(gC_PaintSizePostfix)];
float gF_LastPaintPos[MAXPLAYERS + 1][3];
bool gB_IsPainting[MAXPLAYERS + 1];

TopMenu gTM_Options;
TopMenuObject gTMO_CatPaint;
TopMenuObject gTMO_ItemsPaint[PAINTOPTION_COUNT];


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

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

public void OnPluginStart()
{
	LoadTranslations("gokz-paint.phrases");
	
	RegisterCommands();
}

public void OnAllPluginsLoaded()
{
	if (LibraryExists("updater"))
	{
		Updater_AddPlugin(UPDATER_URL);
	}
	
	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);
	}
}



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

public void GOKZ_OnOptionsMenuCreated(TopMenu topMenu)
{
	OnOptionsMenuCreated_OptionsMenu(topMenu);
}

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

public void OnMapStart()
{
	char buffer[PLATFORM_MAX_PATH];
	
	AddFileToDownloadsTable("materials/gokz/paint/paint_decal.vtf");
	for (int color = 0; color < sizeof(gC_PaintColors); color++)
	{
		for (int size = 0; size < sizeof(gC_PaintSizePostfix); size++)
		{
			Format(buffer, sizeof(buffer), "gokz/paint/%s%s.vmt", gC_PaintColors[color], gC_PaintSizePostfix[size]);
			gI_Decals[color][size] = PrecachePaint(buffer);
		}
	}
	
	CreateTimer(0.1, Timer_Paint, _, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
}



// =====[ PAINT ]=====

void Paint(int client)
{
	if (!IsValidClient(client) ||		
		IsFakeClient(client))
	{
		return;
	}
	
	float position[3];
	bool hit = GetPlayerEyeViewPoint(client, position);
	
	if (!hit || GetVectorDistance(position, gF_LastPaintPos[client], true) < MIN_PAINT_SPACING)
	{
		return;
	}
	
	int paint = GOKZ_GetOption(client, gC_PaintOptionNames[PaintOption_Color]);
	int size = GOKZ_GetOption(client, gC_PaintOptionNames[PaintOption_Size]);
	
	TE_SetupWorldDecal(position, gI_Decals[paint][size]);
	TE_SendToClient(client);
	 
	gF_LastPaintPos[client] = position;
}

public Action Timer_Paint(Handle timer)
{
	for (int client = 1; client <= MAXPLAYERS; client++)
	{
		if (gB_IsPainting[client])
		{
			Paint(client);
		}
	}
	return Plugin_Continue;
}

void TE_SetupWorldDecal(const float origin[3], int index)
{	
	TE_Start("World Decal");
	TE_WriteVector("m_vecOrigin", origin);
	TE_WriteNum("m_nIndex", index);
}

int PrecachePaint(char[] filename)
{
	char path[PLATFORM_MAX_PATH];
	Format(path, sizeof(path), "materials/%s", filename);
	AddFileToDownloadsTable(path);
	
	return PrecacheDecal(filename, true);
}

bool GetPlayerEyeViewPoint(int client, float position[3])
{
	float angles[3];
	GetClientEyeAngles(client, angles);

	float origin[3];
	GetClientEyePosition(client, origin);

	Handle trace = TR_TraceRayFilterEx(origin, angles, MASK_PLAYERSOLID, RayType_Infinite, TraceEntityFilterPlayers);
	if (TR_DidHit(trace))
	{
		TR_GetEndPosition(position, trace);
		delete trace;
		return true;
	}
	delete trace;
	return false;
}



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

void OnOptionsMenuReady_Options()
{
	RegisterOptions();
}

void RegisterOptions()
{
	for (PaintOption option; option < PAINTOPTION_COUNT; option++)
	{
		GOKZ_RegisterOption(gC_PaintOptionNames[option], gC_PaintOptionDescriptions[option], 
			OptionType_Int, gI_PaintOptionDefaults[option], 0, gI_PaintOptionCounts[option] - 1);
	}
}



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

void OnOptionsMenuCreated_OptionsMenu(TopMenu topMenu)
{
	if (gTM_Options == topMenu && gTMO_CatPaint != INVALID_TOPMENUOBJECT)
	{
		return;
	}
	
	gTMO_CatPaint = topMenu.AddCategory(PAINT_OPTION_CATEGORY, TopMenuHandler_Categories);
}

void OnOptionsMenuReady_OptionsMenu(TopMenu topMenu)
{
	// Make sure category exists
	if (gTMO_CatPaint == INVALID_TOPMENUOBJECT)
	{
		GOKZ_OnOptionsMenuCreated(topMenu);
	}
	
	if (gTM_Options == topMenu)
	{
		return;
	}
	
	gTM_Options = topMenu;
	
	// Add gokz-paint option items	
	for (int option = 0; option < view_as<int>(PAINTOPTION_COUNT); option++)
	{
		gTMO_ItemsPaint[option] = gTM_Options.AddItem(gC_PaintOptionNames[option], TopMenuHandler_Paint, gTMO_CatPaint);
	}
}

void DisplayPaintOptionsMenu(int client)
{
	gTM_Options.DisplayCategory(gTMO_CatPaint, client);
}

public void TopMenuHandler_Categories(TopMenu topmenu, TopMenuAction action, TopMenuObject topobj_id, int param, char[] buffer, int maxlength)
{
	if (action == TopMenuAction_DisplayOption || action == TopMenuAction_DisplayTitle)
	{
		if (topobj_id == gTMO_CatPaint)
		{
			Format(buffer, maxlength, "%T", "Options Menu - Paint", param);
		}
	}
}

public void TopMenuHandler_Paint(TopMenu topmenu, TopMenuAction action, TopMenuObject topobj_id, int param, char[] buffer, int maxlength)
{
	PaintOption option = PAINTOPTION_INVALID;
	for (int i = 0; i < view_as<int>(PAINTOPTION_COUNT); i++)
	{
		if (topobj_id == gTMO_ItemsPaint[i])
		{
			option = view_as<PaintOption>(i);
			break;
		}
	}
	
	if (option == PAINTOPTION_INVALID)
	{
		return;
	}
	
	if (action == TopMenuAction_DisplayOption)
	{
		switch (option)
		{
			case PaintOption_Color:
			{
				FormatEx(buffer, maxlength, "%T - %T",
					gC_PaintOptionPhrases[option], param,
					gC_PaintColorPhrases[GOKZ_GetOption(param, gC_PaintOptionNames[option])], param);
			}
			case PaintOption_Size:
			{
				FormatEx(buffer, maxlength, "%T - %T",
					gC_PaintOptionPhrases[option], param,
					gC_PaintSizePhrases[GOKZ_GetOption(param, gC_PaintOptionNames[option])], param);
			}
		}
	}
	else if (action == TopMenuAction_SelectOption)
	{
		if (option == PaintOption_Color)
		{
			DisplayColorMenu(param);
		}
		else
		{
			GOKZ_CycleOption(param, gC_PaintOptionNames[option]);
			gTM_Options.Display(param, TopMenuPosition_LastCategory);
		}
	}
}

void DisplayColorMenu(int client)
{
	char buffer[32];
	
	Menu menu = new Menu(MenuHandler_PaintColor);
	menu.ExitButton = true;
	menu.ExitBackButton = true;
	menu.SetTitle("%T", "Paint Color Menu - Title", client);
	
	for (int i = 0; i < PAINTCOLOR_COUNT; i++)
	{
		FormatEx(buffer, sizeof(buffer), "%T", gC_PaintColorPhrases[i], client);
		menu.AddItem(gC_PaintColors[i], buffer);
	}
	
	menu.Display(client, MENU_TIME_FOREVER);
}

int MenuHandler_PaintColor(Menu menu, MenuAction action, int param1, int param2)
{
	switch (action)
	{
		case MenuAction_Select:
		{
			char item[32];
			menu.GetItem(param2, item, sizeof(item));
			
			for (int i = 0; i < PAINTCOLOR_COUNT; i++)
			{
				if (StrEqual(gC_PaintColors[i], item))
				{
					GOKZ_SetOption(param1, gC_PaintOptionNames[PaintOption_Color], i);
					DisplayPaintOptionsMenu(param1);
					return 0;
				}
			}
		}
		
		case MenuAction_Cancel:
		{
			if (param2 == MenuCancel_ExitBack)
			{
				DisplayPaintOptionsMenu(param1);
			}
		}
		
		case MenuAction_End:
		{
			delete menu;
		}
	}
	
	return 0;
}



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

void RegisterCommands()
{
	RegConsoleCmd("+paint", CommandPaintStart, "[KZ] Start painting.");
	RegConsoleCmd("-paint", CommandPaintEnd, "[KZ] Stop painting.");
	RegConsoleCmd("sm_paint", CommandPaint, "[KZ] Place a paint.");
	RegConsoleCmd("sm_paintoptions", CommandPaintOptions, "[KZ] Open the paint options.");
}

public Action CommandPaintStart(int client, int args)
{
	gB_IsPainting[client] = true;
	return Plugin_Handled;
}

public Action CommandPaintEnd(int client, int args)
{
	gB_IsPainting[client] = false;
	return Plugin_Handled;
}

public Action CommandPaint(int client, int args)
{
	Paint(client);
	return Plugin_Handled;
}

public Action CommandPaintOptions(int client, int args)
{
	DisplayPaintOptionsMenu(client);
	return Plugin_Handled;
}