/
misc-sm.inc
5496 lines (4411 loc) · 145 KB
/
misc-sm.inc
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* _____ _ __ __ _
* / ____| | | | \/ (_)
* | (___ ___ _ _ _ __ ___ ___ _ __ ___ ___ __| |______| \ / |_ ___ ___
* \___ \ / _ \| | | | '__/ __/ _ \ '_ ` _ \ / _ \ / _` |______| |\/| | / __|/ __|
* ____) | (_) | |_| | | | (_| __/ | | | | | (_) | (_| | | | | | \__ \ (__
* |_____/ \___/ \__,_|_| \___\___|_| |_| |_|\___/ \__,_| |_| |_|_|___/\___|
*
* This include contains a ton of useful stocks and functions you can use.
* I made this include considering I feel like most of this stuff should be built into Sourcemod anyways.
* Author: Keith Warren (Drixevel)
* https://github.com/Drixevel
*
* NOTE: The best way to use this include is to copy and paste into your projects manually.
* Credits: Pelipoika, SM9();, TheXeon
**/
//Examples on how to update bitwise operators:
//Apply Bit: bitbuffer |= BIT_1;
//Apply Multi Bit: bitbuffer |= (BIT_1 | BIT_2);
//Remove Bit: bitbuffer &= ~BIT_2;
//Remove Multi Bit: bitbuffer &= ~(BIT_1 | BIT_2);
//
//Example on how to update damage code to add falloff:
//Damage = MaxDmg - (Range - StartFalloff) / (EndFalloff - StartFalloff) * (MaxDmg - MinDmg)
#if defined _misc_sm_included
#endinput
#endif
#define _misc_sm_included
#tryinclude <sdktools>
#tryinclude <sdkhooks>
#tryinclude <regex>
#tryinclude <clientprefs>
#undef REQUIRE_EXTENSIONS
#tryinclude <steamworks>
#define REQUIRE_EXTENSIONS
#define IS_CONSOLE 0
#define IS_SERVER 0
#define INVALID_INDEX -1
#define INVALID_ENT_INDEX -1
#define INVALID_ARRAY_INDEX -1
#define MAX_ENTITY_LIMIT 4096
#define MAX_QUERY_LENGTH 8192
#define MAX_BUTTONS 25
#define MAX_IP_LENGTH 64
#define MAX_MAP_NAME_LENGTH 64
#define MAX_FLAGS_LENGTH 21
#define MAX_STEAMID_LENGTH 64
#define MAX_ENTITY_CLASSNAME_LENGTH 32
#define MAX_PARTICLE_NAME_LENGTH 32
#define MAX_COMMAND_NAME_LENGTH 255
#define EF_BONEMERGE (1 << 0)
#define EF_BONEMERGE_FASTCULL (1 << 7)
#define EF_PARENT_ANIMATES (1 << 9)
#define EF_NOSHADOW 16
#define EF_NOINTERP 8
#define FFADE_IN 0x0001 // Just here so we don't pass 0 into the function
#define FFADE_OUT 0x0002 // Fade out (not in)
#define FFADE_MODULATE 0x0004 // Modulate (don't blend)
#define FFADE_STAYOUT 0x0008 // ignores the duration, stays faded out until new ScreenFade message received
#define FFADE_PURGE 0x0010 // Purges all other fades, replacing them with this one
#define SHAKE_START 0 // Starts the screen shake for all players within the radius.
#define SHAKE_STOP 1 // Stops the screen shake for all players within the radius.
#define SHAKE_AMPLITUDE 2 // Modifies the amplitude of an active screen shake for all players within the radius.
#define SHAKE_FREQUENCY 3 // Modifies the frequency of an active screen shake for all players within the radius.
#define SHAKE_START_RUMBLEONLY 4 // Starts a shake effect that only rumbles the controller, no screen effect.
#define SHAKE_START_NORUMBLE 5 // Starts a shake that does NOT rumble the controller.
//Hex Colors ( Should be used as characters and not strings, ala %c or {1:c} )
#define WHITE 0x01
#define DARKRED 0x02
#define PURPLE 0x03
#define GREEN 0x04
#define MOSSGREEN 0x05
#define LIMEGREEN 0x06
#define RED 0x07
#define GRAY 0x08
#define YELLOW 0x09
#define DARKGREY 0x0A
#define BLUE 0x0B
#define DARKBLUE 0x0C
#define LIGHTBLUE 0x0D
#define PINK 0x0E
#define LIGHTRED 0x0F
/*
* Checks whether a client is valid or not.
*
* client Client index.
*
* return True if valid, false otherwise.
*/
stock bool IsValidClient(int client)
{
return client > 0 && client <= MaxClients && IsClientConnected(client) && IsClientInGame(client) && !IsClientSourceTV(client);
}
/*
* Stops the timer and sets it to null.
*
* return N/A
*/
stock bool StopTimer(Handle& timer)
{
if (timer != null)
{
KillTimer(timer);
timer = null;
return true;
}
return false;
}
/*
* Kicks a client based on the specified SteamID on the server if they exist.
*
* steamid Steamid to check for based on the authtype.
* authtype Authtype to check for.
* reason Reason to give the client being kicked.
* ... Format arguments for the reaso.
*
* return True if found and kicked, false otherwise.
*/
stock bool KickClientBySteamID(const char[] steamid, AuthIdType authtype = AuthId_Engine, const char[] reason, any ...)
{
if (strlen(steamid) == 0)
return false;
char sBuffer[256];
VFormat(sBuffer, sizeof(sBuffer), reason, 4);
char sSteamID[32];
for (int i = 1; i <= MaxClients; i++)
{
if (!IsClientInGame(i) || IsFakeClient(i) || !GetClientAuthId(i, authtype, sSteamID, sizeof(sSteamID)) || !StrEqual(steamid, sSteamID))
continue;
KickClient(i, sBuffer);
return true;
}
return false;
}
/*
* Retrieves a client with a specific steamid on the server if they exist.
*
* steamid Steamid to check for based on the authtype.
* authtype Authtype to check for.
*
* return Client index if found, -1 if not found.
*/
stock int GetClientBySteamID(const char[] steamid, AuthIdType authtype = AuthId_Engine)
{
if (strlen(steamid) == 0)
return -1;
char sSteamID[64];
for (int i = 1; i <= MaxClients; i++)
{
if (!IsClientInGame(i) || IsFakeClient(i) || !GetClientAuthId(i, authtype, sSteamID, sizeof(sSteamID)) || !StrEqual(steamid, sSteamID))
continue;
return i;
}
return -1;
}
/*
* Retrieves a client with a specific accountid on the server if they exist.
* AccountIDs are one of the most consistent ways of tracking specific player accounts.
*
* accountid AccountID to search for.
*
* return Client index if found, -1 if not found.
*/
stock int GetClientByAccountID(int accountid)
{
for (int i = 1; i <= MaxClients; i++)
{
if (!IsClientInGame(i) || IsFakeClient(i) || GetSteamAccountID(i) != accountid)
continue;
return i;
}
return -1;
}
/*
* Retrieves a client with a specific ip address on the server if they exist.
*
* ip IP address to search for.
*
* return Client index if found, -1 if not found.
*/
stock int GetClientByIP(const char[] ip)
{
char sBuffer[16];
for (int i = 1; i <= MaxClients; i++)
{
if (!IsClientInGame(i) || IsFakeClient(i))
continue;
GetClientIP(i, sBuffer, sizeof(sBuffer));
if (StrEqual(ip, sBuffer))
return i;
}
return -1;
}
/*
* Checks the players steamid based on the auth type.
*
* client Client index.
* steamid SteamID to check.
* auth Auth type to check.
*
* return True if they have the steamid, false otherwise.
*/
stock bool HasSteamID(int client, const char[] steamid, AuthIdType auth = AuthId_Steam2)
{
if (!IsClientAuthorized(client) || IsFakeClient(client))
return false;
char buffer[64];
if (!GetClientAuthId(client, auth, buffer, sizeof(buffer)))
return false;
return StrEqual(buffer, steamid, false);
}
/*
* Checks whether or not the client is console or not. (Same as IsClientConsole)
* You can easily just add checks to make sure client is more than 0 if you use this for error checking.
* I use this sometimes for things like replies to console specifically.
*
* client Client index.
*
* return True if client is console, false otherwise.
*/
stock bool IsClientServer(int client)
{
return client == 0;
}
/*
* Converts a string to a boolean.
* Fixes the ugly workaround it's actually doing.
*
* str String to convert.
*
* return Boolean version.
*/
stock bool StringToBool(const char[] str)
{
return view_as<bool>(StringToInt(str));
}
/*
* Converts a string to a 3D vector.
* Fixes the ugly workaround it's actually doing.
*
* explode String to convert to 3D vectors.
* buffer Buffer to store values in.
* defaultvector Default values if the string is malformed.
*
* return True if converted successfully, false otherwise.
*/
stock bool StringToVector(const char[] explode, float buffer[3], float defaultvector[3] = {0.0, 0.0, 0.0})
{
if (strlen(explode) == 0)
{
buffer[0] = defaultvector[0]; buffer[1] = defaultvector[1]; buffer[2] = defaultvector[2];
return false;
}
char sPart[3][32];
int iReturned = ExplodeString(explode, StrContains(explode, ", ") != -1 ? ", " : " ", sPart, 3, 32);
if (iReturned != 3)
{
buffer[0] = defaultvector[0]; buffer[1] = defaultvector[1]; buffer[2] = defaultvector[2];
return false;
}
buffer[0] = StringToFloat(sPart[0]); buffer[1] = StringToFloat(sPart[1]); buffer[2] = StringToFloat(sPart[2]);
return true;
}
/*
* Converts a string to a 2D vector.
* Fixes the ugly workaround it's actually doing.
*
* explode String to convert to 3D vectors.
* buffer Buffer to store values in.
* defaultvector Default values if the string is malformed.
*
* return True if converted successfully, false otherwise.
*/
stock bool StringToVector2D(const char[] explode, float buffer[2], float defaultvector[2] = {0.0, 0.0})
{
if (strlen(explode) == 0)
{
buffer[0] = defaultvector[0]; buffer[1] = defaultvector[1];
return false;
}
char sPart[2][32];
int iReturned = ExplodeString(explode, StrContains(explode, ", ") != -1 ? ", " : " ", sPart, 2, 32);
if (iReturned != 2)
{
buffer[0] = defaultvector[0]; buffer[1] = defaultvector[1];
return false;
}
buffer[0] = StringToFloat(sPart[0]); buffer[1] = StringToFloat(sPart[1]);
return true;
}
/*
* Converts a string to a color vector.
* Fixes the ugly workaround it's actually doing.
*
* explode String to convert a color vector.
* buffer Buffer to store values in.
* defaultvector Default values if the string is malformed.
*
* return True if converted successfully, false otherwise.
*/
stock bool StringToColor(const char[] explode, int buffer[4], int defaultvalues[4] = {255, 255, 255, 255})
{
if (strlen(explode) == 0)
{
buffer[0] = defaultvalues[0]; buffer[1] = defaultvalues[1]; buffer[2] = defaultvalues[2]; buffer[3] = defaultvalues[3];
return false;
}
char sPart[4][32];
int iReturned = ExplodeString(explode, StrContains(explode, ",") != -1 ? ", " : " ", sPart, 4, 32);
if (iReturned != 4)
{
buffer[0] = defaultvalues[0]; buffer[1] = defaultvalues[1]; buffer[2] = defaultvalues[2]; buffer[3] = defaultvalues[3];
return false;
}
buffer[0] = StringToInt(sPart[0]); buffer[1] = StringToInt(sPart[1]); buffer[2] = StringToInt(sPart[2]); buffer[3] = StringToInt(sPart[3]);
return true;
}
/*
* Same as array.PushString/PushArrayString but with a formatting parameter.
*
* array Array handle to push.
* format Format buffer to use.
* ... Parameters to use.
*
* return Index used in the array, -1 otherwise.
*/
stock int PushArrayStringFormat(ArrayList array, char[] format, any ...)
{
if (array == null)
return -1;
char sBuffer[1024];
VFormat(sBuffer, sizeof(sBuffer), format, 3);
return array.PushString(sBuffer);
}
/*
* Clears the ArrayStack handles data.
* Apparently there isn't a native one.
*
* stack ArrayStack handle to push.
*
* return True if successfully emptied, false otherwise.
*/
stock bool ClearStack(ArrayStack stack)
{
if (stack == null)
return false;
while (!stack.Empty)
stack.Pop();
return true;
}
/*
* Prints a specific file on the server to console.
* Mostly used to debug error logs but it can be used for anything.
*
* client Client index.
* path Path to the file.
*
* return True if file is found and shown, false otherwise.
*/
stock bool PrintFileToConsole(int client, const char[] path)
{
if (strlen(path) == 0)
return false;
PrintToConsole(client, "------------------------------------------------------\n - %s", path);
Handle fil = OpenFile(path, "r");
if (fil == null)
{
PrintToConsole(client, "FILE NOT FOUND\n------------------------------------------------------");
return false;
}
char sLine[128];
while (!IsEndOfFile(fil) && ReadFileLine(fil, sLine, sizeof(sLine)))
{
TrimString(sLine);
PrintToConsole(client, ">%s", sLine);
}
delete fil;
//Makes 100% sure to put the ending line under the console outputs.
CreateTimer(0.2, __Timer_DelayEndBuffer, GetClientUserId(client), TIMER_FLAG_NO_MAPCHANGE);
return true;
}
public Action __Timer_DelayEndBuffer(Handle timer, any userid)
{
int client;
if ((client = GetClientOfUserId(userid)) > 0)
PrintToConsole(client, "------------------------------------------------------");
}
/*
* Checks if a client has specific flags or not.
* This is mostly deprecated though, might as well use CheckCommandAccess with ReadFlagString instead.
* CheckCommandAccess(client, "", ReadFlagString(flags), true);
*
* client Client index.
* flags Flags to check for.
*
* return True if client has flags, false otherwise.
*/
//Forgot
//Lost but not forgotten.
stock bool CheckAdminFlagsByString(int client, const char[] flags)
{
if (client == 0 || IsFakeClient(client) || strlen(flags) == 0)
return false;
AdminId admin = GetUserAdmin(client);
if (admin != INVALID_ADMIN_ID)
{
int count; int found; int flags = ReadFlagString(flags);
for (int i = 0; i <= 20; i++)
{
if (flags & (1 << i))
{
count++;
if (GetAdminFlag(admin, view_as<AdminFlag>(i)))
found++;
}
}
if (count == found)
return true;
}
return false;
}
/*
* Returns a random boolean between true or false.
*
* return True or false.
*/
stock bool GetRandomBool()
{
return view_as<bool>(GetRandomInt(0, 1));
}
/*
* Sends an input to the entity if it's valid.
* Also checks if the world is trying to be killed due to faulty checks.
*
* dest Destination entity.
* input Input to send.
* activator Activator sending the input.
* caller Caller sending the input.
* outputid Outputid to send the input.
*
* return True if successful, false otherwise.
*/
stock bool AcceptEntityInputSafe(int dest, const char[] input, int activator = -1, int caller = -1, int outputid = 0)
{
if (!IsValidEntity(dest) || dest < 1 && StrEqual(input, "kill", false))
return false;
return AcceptEntityInput(dest, input, activator, caller, outputid);
}
/*
* Same as AddFileToDownloadsTable except as a format buffer.
*
* format Format buffer.
* ... Parameters to use.
*
* return True if successful, false otherwise.
*/
stock bool AddFileToDownloadsTableEx(const char[] format, any ...)
{
if (strlen(format) == 0)
return false;
char sBuffer[PLATFORM_MAX_PATH];
VFormat(sBuffer, sizeof(sBuffer), format, 2);
AddFileToDownloadsTable(sBuffer);
return true;
}
/*
* Format seconds into a formatted string to display neatly.
* This function can largely be edited to fit whatever needs, it's hard to make a solid stock for every necessity.
*
* seconds Seconds to convert.
* buffer Buffer to store the results.
* maxlength Length of the buffer.
* format Format string to reference to reference and save in the buffer.
* precision Whether to use precise milliseconds alongside the normal display of seconds. (Useful for precise timers)
*
* return N/A
*/
stock void FormatSeconds(float seconds, char[] buffer, int maxlength, const char[] format, bool precision = false)
{
int t = RoundToFloor(seconds);
int day; char sDay[32];
if (t >= 86400)
{
day = RoundToFloor(t / 86400.0);
t %= 86400;
Format(sDay, sizeof(sDay), "%02d", day);
}
int hour; char sHour[32];
if (t >= 3600)
{
hour = RoundToFloor(t / 3600.0);
t %= 3600;
Format(sHour, sizeof(sHour), "%02d", hour);
}
int mins; char sMinute[32];
if (t >= 60)
{
mins = RoundToFloor(t / 60.0);
t %= 60;
Format(sMinute, sizeof(sMinute), "%02d", mins);
}
char sSeconds[32];
switch (precision)
{
case true: Format(sSeconds, sizeof(sSeconds), "%05.2f", float(t) + seconds - RoundToFloor(seconds));
case false: Format(sSeconds, sizeof(sSeconds), "%02d", t);
}
strcopy(buffer, maxlength, format);
ReplaceString(buffer, maxlength, "%D", strlen(sDay) > 0 ? sDay : "00");
ReplaceString(buffer, maxlength, "%H", strlen(sHour) > 0 ? sHour : "00");
ReplaceString(buffer, maxlength, "%M", strlen(sMinute) > 0 ? sMinute : "00");
ReplaceString(buffer, maxlength, "%S", strlen(sSeconds) > 0 ? sSeconds : "00");
}
/*
* Clears all indexes from an array that have the specified value.
*
* array Array handle.
* value Value to clear.
*
* return N/A
*/
stock void ClearValueFromArray(ArrayList array, any value)
{
if (array == null || array.FindValue(value) == -1)
return;
int index;
while ((index = array.FindValue(value)) != -1)
array.Erase(index);
}
/*
* Clears an array of all indexes regardless if handles exist or not.
* Clearing the array normally won't kill the handles saved as well.
*
* array Array handle.
*
* return True if cleared, false otherwise.
*/
stock bool ClearArraySafe(ArrayList array)
{
if (array == null)
return false;
for (int i = 0; i < array.Length; i++)
{
Handle hndl = array.Get(i);
delete hndl;
}
array.Clear();
return true;
}
/*
* Clears a stringmap of all indexes regardless if handles exist or not.
* Clearing the stringmap normally won't kill the handles saved as well.
*
* map StringMap handle.
*
* return True if cleared, false otherwise.
*/
stock bool ClearTrieSafe(StringMap map)
{
if (map == null)
return false;
StringMapSnapshot snapshot = map.Snapshot();
int size;
for (int i = 0; i < snapshot.Length; i++)
{
size = snapshot.KeyBufferSize(i);
char[] sBuffer = new char[size];
snapshot.GetKey(i, sBuffer, size);
Handle hLocal;
map.GetValue(sBuffer, hLocal);
delete hLocal;
map.Remove(sBuffer);
}
delete snapshot;
return true;
}
stock void SteamWorks_SetHTTPRequestGetOrPostParameterInt(Handle hHandle, const char[] sName, int value)
{
char sValue[256];
IntToString(value, sValue, sizeof(sValue));
SteamWorks_SetHTTPRequestGetOrPostParameter(hHandle, sName, sValue);
}
stock float CalculateFloat(int value1, int value2, float fMultiplier = 0.0)
{
float fValue = float(value1) / float(value2);
if (value2 < 1)
fValue = float(value1);
if (value1 < 1)
fValue = 0.0;
if (fMultiplier > 0.0)
fValue *= fMultiplier;
return fValue;
}
stock void LogDebug(char[] name, const char[] format, any ...)
{
char sLog[4096];
VFormat(sLog, sizeof(sLog), format, 3);
char sDate[32];
FormatTime(sDate, sizeof(sDate), "%Y-%m-%d", GetTime());
if (strlen(name) == 0)
strcopy(name, PLATFORM_MAX_PATH, "Debug");
char sPath[PLATFORM_MAX_PATH]; char sPathFinal[PLATFORM_MAX_PATH];
Format(sPath, sizeof(sPath), "logs/%s.%s.log", name, sDate);
BuildPath(Path_SM, sPathFinal, sizeof(sPathFinal), sPath);
LogToFileEx(sPathFinal, "%s", sLog);
}
stock bool IsStringNumeric(const char[] str)
{
int x = 0;
int dotsFound = 0;
int numbersFound = 0;
if (str[x] == '+' || str[x] == '-')
x++;
while (str[x] != '\0')
{
if (IsCharNumeric(str[x]))
numbersFound++;
else if (str[x] == '.')
{
dotsFound++;
if (dotsFound > 1)
return false;
}
else
return false;
x++;
}
return numbersFound > 0;
}
stock void StripCharactersPre(char[] buffer, int size, int position)
{
strcopy(buffer, size, buffer[position]);
}
stock void StripCharactersPost(char[] buffer, int position)
{
buffer[position] = '\0';
}
stock bool GetStringMinMax(const char[] buffer, any& min, any& max, const char[] splitter = "/", bool clamp = true)
{
char sPart[2][12];
if (ExplodeString(buffer, splitter, sPart, 2, 12) < 2)
return false;
min = StrContains(sPart[0], ".") != -1 ? StringToFloat(sPart[0]) : StringToInt(sPart[0]);
max = StrContains(sPart[1], ".") != -1 ? StringToFloat(sPart[1]) : StringToInt(sPart[1]);
if (clamp)
{
if (min > max)
min = max;
if (max < min)
max = min;
}
return true;
}
stock bool IsPlayerIndex(int index)
{
return index > 0 && index <= MaxClients;
}
stock bool IsEntityIndex(int index)
{
return index > MaxClients;
}
stock bool IsEntityWorld(int index)
{
return index == 0;
}
stock int GetRandomString(char[] buffer, int size, int length = 32, const char[] chrs = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234556789")
{
int random; int len;
size--;
if (chrs[0] != '\0')
len = strlen(chrs) - 1;
int n = 0;
while (n < length && n < size)
{
if (chrs[0] == '\0')
{
random = GetRandomInt(33, 126);
buffer[n] = random;
}
else
{
random = GetRandomInt(0, len);
buffer[n] = chrs[random];
}
n++;
}
buffer[length] = '\0';
}
stock void FillArrayToValue(any[] array, int size, any value, int start = 0)
{
if (start < 0)
start = 0;
for (int i = start; i < size; i++)
array[i] = value;
}
stock void CopyArrayToArray(const any[] array, any[] newArray, int size)
{
for (int i = 0; i < size; i++)
newArray[i] = array[i];
}
/**
* Gets the origin of where the client is looking such as geometry, displacements and entities.
*
* client Client index.
* pOrigin Origin results to pass through.
* filter_players Filter out players from the traceray.
* distance Maximum distance allowed in the traceray.
*
* return True if results are returned, false otherwise.
**/
stock bool GetClientLookOrigin(int client, float pOrigin[3], bool filter_players = true, float distance = 35.0)
{
if (client == 0 || client > MaxClients || !IsClientInGame(client))
return false;
float vOrigin[3];
GetClientEyePosition(client,vOrigin);
float vAngles[3];
GetClientEyeAngles(client, vAngles);
Handle trace = TR_TraceRayFilterEx(vOrigin, vAngles, MASK_SHOT, RayType_Infinite, filter_players ? TraceEntityFilterPlayer : TraceEntityFilterNone, client);
bool bReturn = TR_DidHit(trace);
if (bReturn)
{
float vStart[3];
TR_GetEndPosition(vStart, trace);
float vBuffer[3];
GetAngleVectors(vAngles, vBuffer, NULL_VECTOR, NULL_VECTOR);
pOrigin[0] = vStart[0] + (vBuffer[0] * -distance);
pOrigin[1] = vStart[1] + (vBuffer[1] * -distance);
pOrigin[2] = vStart[2] + (vBuffer[2] * -distance);
}
delete trace;
return bReturn;
}
stock bool IsPlayerStuck(int client)
{
float vecMin[3];
GetEntPropVector(client, Prop_Send, "m_vecMins", vecMin);
float vecMax[3];
GetEntPropVector(client, Prop_Send, "m_vecMaxs", vecMax);
float vecOrigin[3];
GetEntPropVector(client, Prop_Send, "m_vecOrigin", vecOrigin);
TR_TraceHullFilter(vecOrigin, vecOrigin, vecMin, vecMax, MASK_SOLID, TraceEntityFilterNone, client);
return (TR_DidHit());
}
public bool TraceEntityFilterPlayer(int entity, int contentsMask, any data)
{
return entity > MaxClients || !entity;
}
public bool TraceEntityFilterNone(int entity, int contentsMask, any data)
{
return entity != data;
}
stock bool IsEntityInSightRange(int client, int entity, float angle = 90.0, float distance = 0.0, bool heightcheck = true, bool negativeangle = false)
{
if (angle > 360.0 || angle < 0.0)
angle = 180.0;
float anglevector[3];
GetClientEyeAngles(client, anglevector);
anglevector[0] = anglevector[2] = 0.0;
GetAngleVectors(anglevector, anglevector, NULL_VECTOR, NULL_VECTOR);
NormalizeVector(anglevector, anglevector);
if (negativeangle)
NegateVector(anglevector);
float clientpos[3];
GetClientAbsOrigin(client, clientpos);
float targetpos[3];
if (HasEntProp(entity, Prop_Data, "m_vecAbsOrigin"))
GetEntPropVector(entity, Prop_Data, "m_vecAbsOrigin", targetpos);
else
GetClientAbsOrigin(entity, targetpos);
float resultdistance;
if (heightcheck && distance > 0)
resultdistance = GetVectorDistance(clientpos, targetpos);
clientpos[2] = targetpos[2] = 0.0;
float targetvector[3];
MakeVectorFromPoints(clientpos, targetpos, targetvector);
NormalizeVector(targetvector, targetvector);
float resultangle = RadToDeg(ArcCosine(GetVectorDotProduct(targetvector, anglevector)));
if (resultangle <= angle / 2)
{
if(distance > 0)
{
if(!heightcheck)
resultdistance = GetVectorDistance(clientpos, targetpos);
if(distance >= resultdistance)
return true;
else
return false;
}
else
return true;
}
else
return false;
}
stock int AttachParticle(int entity, const char[] name, float time = 0.0, const char[] attach = "", float angles[3] = {0.0, 0.0, 0.0}, float offsets[3] = {0.0, 0.0, 0.0})
{
float origin[3];
GetEntPropVector(entity, Prop_Send, "m_vecOrigin", origin);
origin[0] += offsets[0];
origin[1] += offsets[1];
origin[2] += offsets[2];
int particle = CreateEntityByName("info_particle_system");
if (IsValidEntity(particle))
{
DispatchKeyValueVector(particle, "origin", origin);
DispatchKeyValueVector(particle, "angles", angles);
DispatchKeyValue(particle, "effect_name", name);
DispatchSpawn(particle);
ActivateEntity(particle);
AcceptEntityInput(particle, "Start");
if (time > 0.0)
{
char output[64];
Format(output, sizeof(output), "OnUser1 !self:kill::%.1f:1", time);
SetVariantString(output);
AcceptEntityInput(particle, "AddOutput");
AcceptEntityInput(particle, "FireUser1");
}
SetVariantString("!activator");
AcceptEntityInput(particle, "SetParent", entity, particle, 0);
if (strlen(attach) > 0)
{
SetVariantString(attach);
AcceptEntityInput(particle, "SetParentAttachmentMaintainOffset", particle, particle, 0);
}
}
return particle;
}
stock int CreateParticle(const char[] name, float origin[3], float time = 0.0, float angles[3] = {0.0, 0.0, 0.0}, float offsets[3] = {0.0, 0.0, 0.0})
{