-
Notifications
You must be signed in to change notification settings - Fork 14
/
stamm_distance.sp
485 lines (362 loc) · 10.1 KB
/
stamm_distance.sp
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
/**
* -----------------------------------------------------
* File stamm_distance.sp
* Authors David <popoklopsi> Ordnung
* License GPLv3
* Web http://popoklopsi.de
* -----------------------------------------------------
*
* Copyright (C) 2012-2014 David <popoklopsi> Ordnung
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
// Includes
#include <sourcemod>
#include <autoexecconfig>
#undef REQUIRE_PLUGIN
#include <stamm>
#include <updater>
#pragma semicolon 1
new g_iBlockDirection;
new g_iBlockDistance;
new g_iBlockName;
new Handle:g_hUnit;
new bool:g_bVipPlayers[MAXPLAYERS + 1][3];
public Plugin:myinfo =
{
name = "Stamm Feature Distance",
author = "Popoklopsi",
version = "1.1.1",
description = "VIP's see the distance and direction to the nearest player",
url = "https://forums.alliedmods.net/showthread.php?t=142073"
};
// Hook spawning
public OnPluginStart()
{
AutoExecConfig_SetFile("distance", "stamm/features");
AutoExecConfig_SetCreateFile(true);
g_hUnit = AutoExecConfig_CreateConVar("distance_unit", "1", "1 = Use feet as unit, 0 = Use meters as unit");
AutoExecConfig_CleanFile();
AutoExecConfig_ExecuteFile();
HookEvent("player_spawn", eventPlayerSpawn);
}
// Auto updater
public STAMM_OnFeatureLoaded(const String:basename[])
{
decl String:urlString[256];
Format(urlString, sizeof(urlString), "http://popoklopsi.de/stamm/updater/update.php?plugin=%s", basename);
if (LibraryExists("updater") && STAMM_AutoUpdate())
{
Updater_AddPlugin(urlString);
Updater_ForceUpdate();
}
// Found old config
if (STAMM_GetBlockCount() < 3)
{
g_iBlockDirection = g_iBlockDistance = g_iBlockName = 1;
}
else
{
// Get Blocks
g_iBlockDirection = STAMM_GetBlockOfName("direction");
g_iBlockDistance = STAMM_GetBlockOfName("distance");
g_iBlockName = STAMM_GetBlockOfName("name");
}
// check for nearest player
CreateTimer(0.2, checkPlayers, _, TIMER_REPEAT);
}
// Add descriptions
public STAMM_OnClientRequestFeatureInfo(client, block, &Handle:array)
{
decl String:fmt[256];
if (block == g_iBlockDirection)
{
Format(fmt, sizeof(fmt), "%T", "GetDirection", client);
PushArrayString(array, fmt);
}
if (block == g_iBlockDistance)
{
Format(fmt, sizeof(fmt), "%T", "GetDistance", client);
PushArrayString(array, fmt);
}
if (block == g_iBlockName)
{
Format(fmt, sizeof(fmt), "%T", "GetName", client);
PushArrayString(array, fmt);
}
}
// Add feature
public OnAllPluginsLoaded()
{
if (!STAMM_IsAvailable())
{
SetFailState("Can't Load Feature, Stamm is not installed!");
}
STAMM_LoadTranslation();
STAMM_RegisterFeature("VIP Distance");
}
// Disable hud hint sound
public OnConfigsExecuted()
{
if (FindConVar("sv_hudhint_sound") != INVALID_HANDLE)
{
SetConVarInt(FindConVar("sv_hudhint_sound"), 0);
}
}
// A player spawned
public Action:eventPlayerSpawn(Handle:event, const String:name[], bool:dontBroadcast)
{
new client = GetClientOfUserId(GetEventInt(event, "userid"));
// Reset
g_bVipPlayers[client][0] = false;
g_bVipPlayers[client][1] = false;
g_bVipPlayers[client][2] = false;
// Is client VIP?
if (STAMM_IsClientValid(client))
{
if (g_iBlockDirection != -1 && STAMM_HaveClientFeature(client, g_iBlockDirection))
{
g_bVipPlayers[client][2] = true;
}
if (g_iBlockDistance != -1 && STAMM_HaveClientFeature(client, g_iBlockDistance))
{
g_bVipPlayers[client][1] = true;
}
if (g_iBlockName != -1 && STAMM_HaveClientFeature(client, g_iBlockName))
{
g_bVipPlayers[client][0] = true;
}
}
}
public STAMM_OnClientBecomeVip(client, oldlevel, newlevel)
{
if (g_iBlockDirection != -1 && STAMM_HaveClientFeature(client, g_iBlockDirection))
{
g_bVipPlayers[client][2] = true;
}
if (g_iBlockDistance != -1 && STAMM_HaveClientFeature(client, g_iBlockDistance))
{
g_bVipPlayers[client][1] = true;
}
if (g_iBlockName != -1 && STAMM_HaveClientFeature(client, g_iBlockName))
{
g_bVipPlayers[client][0] = true;
}
}
// Client disconnected
public OnClientDisconnect(client)
{
g_bVipPlayers[client][0] = false;
g_bVipPlayers[client][1] = false;
g_bVipPlayers[client][2] = false;
}
// Client changed feature
public STAMM_OnClientChangedFeature(client, bool:mode, bool:isShop)
{
if (!mode)
{
g_bVipPlayers[client][0] = false;
g_bVipPlayers[client][1] = false;
g_bVipPlayers[client][2] = false;
}
else
{
if (g_iBlockDirection != -1 && STAMM_HaveClientFeature(client, g_iBlockDirection))
{
g_bVipPlayers[client][2] = true;
}
if (g_iBlockDistance != -1 && STAMM_HaveClientFeature(client, g_iBlockDistance))
{
g_bVipPlayers[client][1] = true;
}
if (g_iBlockName != -1 && STAMM_HaveClientFeature(client, g_iBlockName))
{
g_bVipPlayers[client][0] = true;
}
}
}
// Check for nearest player
public Action:checkPlayers(Handle:timer, any:data)
{
decl String:unitString[12];
decl String:unitStringOne[12];
new Float:clientOrigin[3];
new Float:searchOrigin[3];
new Float:near;
new Float:distance;
new nearest;
if (GetConVarInt(g_hUnit) == 1)
{
Format(unitString, sizeof(unitString), "feet");
Format(unitStringOne, sizeof(unitStringOne), "feet");
}
else
{
Format(unitString, sizeof(unitString), "meters");
Format(unitStringOne, sizeof(unitStringOne), "meter");
}
// Client loop
for (new client = 1; client <= MaxClients; client++)
{
// Valid client?
if ((g_bVipPlayers[client][0] || g_bVipPlayers[client][1] || g_bVipPlayers[client][2]) && IsPlayerAlive(client))
{
// Is VIP and want it?
if (!STAMM_IsClientValid(client) || !STAMM_WantClientFeature(client))
{
g_bVipPlayers[client][0] = false;
g_bVipPlayers[client][1] = false;
g_bVipPlayers[client][2] = false;
}
else
{
nearest = 0;
near = 0.0;
// Get origin
GetClientAbsOrigin(client, clientOrigin);
// Next client loop
for (new search = 1; search <= MaxClients; search++)
{
if (IsClientInGame(search) && IsPlayerAlive(search) && search != client && (GetClientTeam(client) != GetClientTeam(search)))
{
// Get distance to first client
GetClientAbsOrigin(search, searchOrigin);
distance = GetVectorDistance(clientOrigin, searchOrigin);
// Is he more near to the player as the player before?
if (near == 0.0)
{
near = distance;
nearest = search;
}
if (distance < near)
{
near = distance;
nearest = search;
}
}
}
// Found a player?
if (nearest != 0)
{
new Float:dist;
new Float:vecPoints[3];
new Float:vecAngles[3];
new Float:clientAngles[3];
decl String:directionString[64];
new String:textToPrint[64];
// Client get Direction?
if (g_bVipPlayers[client][2])
{
// Get the origin of the nearest player
GetClientAbsOrigin(nearest, searchOrigin);
// Angles
GetClientAbsAngles(client, clientAngles);
// Angles from origin
MakeVectorFromPoints(clientOrigin, searchOrigin, vecPoints);
GetVectorAngles(vecPoints, vecAngles);
// Differenz
new Float:diff = clientAngles[1] - vecAngles[1];
// Correct it
if (diff < -180)
{
diff = 360 + diff;
}
if (diff > 180)
{
diff = 360 - diff;
}
// Now geht the direction
// Up
if (diff >= -22.5 && diff < 22.5)
{
Format(directionString, sizeof(directionString), "\xe2\x86\x91");
}
// right up
else if (diff >= 22.5 && diff < 67.5)
{
Format(directionString, sizeof(directionString), "\xe2\x86\x97");
}
// right
else if (diff >= 67.5 && diff < 112.5)
{
Format(directionString, sizeof(directionString), "\xe2\x86\x92");
}
// right down
else if (diff >= 112.5 && diff < 157.5)
{
Format(directionString, sizeof(directionString), "\xe2\x86\x98");
}
// down
else if (diff >= 157.5 || diff < -157.5)
{
Format(directionString, sizeof(directionString), "\xe2\x86\x93");
}
// down left
else if (diff >= -157.5 && diff < -112.5)
{
Format(directionString, sizeof(directionString), "\xe2\x86\x99");
}
// left
else if (diff >= -112.5 && diff < -67.5)
{
Format(directionString, sizeof(directionString), "\xe2\x86\x90");
}
// left up
else if (diff >= -67.5 && diff < -22.5)
{
Format(directionString, sizeof(directionString), "\xe2\x86\x96");
}
// Add to text
if (g_bVipPlayers[client][1] || g_bVipPlayers[client][0])
{
Format(textToPrint, sizeof(textToPrint), "%s\n", directionString);
}
else
{
Format(textToPrint, sizeof(textToPrint), directionString);
}
}
// Client get Distance?
if (g_bVipPlayers[client][1])
{
// Distance to meters
dist = near * 0.01905;
// Distance to feet
if (GetConVarInt(g_hUnit) == 1)
{
dist = dist * 3.2808399;
}
// Add to text
if (g_bVipPlayers[client][0])
{
Format(textToPrint, sizeof(textToPrint), "%s(%i %s)\n", textToPrint, RoundFloat(dist), (RoundFloat(dist) == 1 ? unitStringOne : unitString));
}
else
{
Format(textToPrint, sizeof(textToPrint), "%s(%i %s)", textToPrint, RoundFloat(dist), (RoundFloat(dist) == 1 ? unitStringOne : unitString));
}
}
// Add name
if (g_bVipPlayers[client][0])
{
Format(textToPrint, sizeof(textToPrint), "%s%N", textToPrint, nearest);
}
// Print text
PrintHintText(client, textToPrint);
}
}
}
}
return Plugin_Continue;
}