-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathl4d2_gifts.sp
1406 lines (1211 loc) · 40.3 KB
/
l4d2_gifts.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
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
#define PLUGIN_VERSION "1.3.6.1"
/*
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Plugin Info:
* Name : [L4D2] Gifts Drop & Spawn
* Author : Aceleraci�n
* Descrp : Drop gifts when a special infected died and win points & special weapon
* Link : https://forums.alliedmods.net/showthread.php?t=302731
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Change Log:
1.3.6.1 (19-November-2017)
- Fixed the problem that you can not collect special gifts in version 1.3.6
- Fixed the problem that the first aid kit was not spawned.
- Fixed the problem that some weapons specified by the "sm_gift" command were not spawned.
1.3.6 (17-November-2017)
- The value of the DATABASE_CONFIG definition was changed to "l4d2gifts".
Now the plugin has its own named configuration that you must add to the databases.cfg file.
- Added the automatic creation of the table "players" of the database you have configured
(CREATE TABLE IF NOT EXISTS `players`).
- The following fields were added to the table "players":
+ collected_gift: Save the total of collected gifts per player.
+ collected_gift_standard: Save the total of standard gifts collected per player.
+ collected_gift_special: Save the total of special gifts collected per player.
- Added the dimensional array "TotalGifts" to save the number of collected gifts of each type throughout the
game by the player (since he first connected to the server).
- Added array "AllGifts" to save the total of gifts collected throughout the game by the player (since he first
connected to the server).
- Added the use of OnClientPostAdminCheck forward to get from the database how many gifts of each type and in
general has the player.
- The "sm_giftcollect" alias "sm_giftc" command was updated to display the gifts collected throughout the game
(since it was first connected to the server).
- Added the dimensional array "g_sGifSWeapon" to store the weapon of special gifts generated by the administrator.
- The "sm_gift" command was updated so that administrators with the ADMFLAG_CHEATS flag can spawn the gift they want.
It can be a random gift, a standard gift and a special gift with the weapon they wish (It must be valid weapons).
- The library "l4d_weapon_stocks" was updated so that it recognizes the classname of the weapons and melee weapons
compatible with left 4 dead 2.
- The "DropGift" function was modified to return the entity index of the gift that was spawned.
- The "sm_reloadgifts" command flag was modified to ADMFLAG_CONFIG.
- Added cvar "l4d2_gifts_maxcollectMap" for the maximum number of gifts that all survivors can collect per map.
The value of 0 disables this functionality.
- Added cvar "l4d2_gifts_maxcollectRound" for the maximum number of gifts that all survivors can collect per round.
The value of 0 disables this functionality.
- Added definition MAX_SPECIALWEAPONS to set the maximum of special weapons.
- Added the "first aid kit" to the special weapons that the player wins for a special gift.
- The "NotifyGift" function was modified by adding the "gift" parameter that corresponds to the entity index of a gift.
1.3.5 (15-November-2017)
- Added definition USE_DATABASE to you want to use the database. Default is true.
1.3.4 (13-November-2017)
- The dependency on the database was removed. The plugin can work without the need for database.
- The translations were updated to show the ads for the gifts without points when there is not database.
- The points earned by gifts and the command "sm_giftpoints" is activated if the plugin is working with a database.
1.3.3 (09-November-2017)
- Added function "PrecacheModelGifts" and "PrecacheSoundGifts"
- Fixed the problem of gifts that did not spawn when reloading the configuration file with the command "sm_reloadgifts"
- Changed PrintToChat to Client_PrintToChat and PrintToChatAll to Client_PrintToChatAll (except when printing points)
- Fixed the color lightgreen of translations.
1.3.2 (05-November-2017)
- Added array for player gift points (CurrentPointsForMap & CurrentPointsForRound)
- Added dimensional arrays for the number of gifts of the player (CurrentGiftsForMap && CurrentGiftsForRound)
- Added array for the player's total gift points (CurrentGiftsTotalForMap & CurrentGiftsTotalForRound)
- Added command "sm_giftpoints" alias "sm_giftp" for players to view points for gifts collected in the current map and
current round.
- Added command "sm_giftcollect" alias "sm_giftc" for players to view the number for gifts collected in the current map and
current round specific for type the gift.
1.3.1 (04-October-2017)
- Added translations, required for the spawn gifts
- Fixed the name of weapons for print to chat when spawn gifts
1.3 (03-October-2017)
- Added a config file for the gifts "l4d2_gifts" replacing the definitions.
Here you specify the path and model type, and the type of gift whether standard or special.
- Changed cvar l4d2_gifts_pointsA for l4d2_gifts_pointsE
- Fixed the gifts with models not physic that did not show up
- Added OnPlayerRunCmd forward when a survivor presses +USE on gifts static.
- Added command "sm_reloadgifts" for reloads the settings from the config file "l4d2_gifts"
- Added cvar "l4d2_gifts_probabilityE" to the probability for gifts standard (animals and other objects)
with respect to chance of infected drop gift. (Chance of infected drop gift represents 100% of these).
- Added cvar "l4d2_gifts_probabilityS" to the probability for gift special with respect to chance of infected drop gift.
- Added definition MAX_GIFTS to set the maximum of gifts.
1.2.1 (19-July-2017)
- Added weapons to the gifts square for give to the player when he catches the gifts
1.2 (01-June-2017)
- Added cvar "l4d2_gifts_pointsA" to the points for take a gift (animals and other objects).
- Added cvar "l4d2_gifts_pointsS" to the points for take a gift square.
- Added sound for gifts animals and gifts square when these are caught
- Added the config file that should be executed after plugin load.
- Added the databases config to the points of the gifts
- Added a hook for when the console variables values is changed.
1.1 (15-March-2017)
- Added command "sm_gift" for admins to spawn gifts. Used for the developer.
- Added cvar "l4d2_gifts_enabled" to toggle to turn on or off the gifts.
- Added cvar "l4d2_gifts_giflife" to the time that the gift stay on ground in seconds.
- Added cvar "l4d2_gifts_chance" to the chance (%) of infected drop gift.
- Added TAG CHAT for PrintChatToAll to the gifts spawned
1.0 (20-January-2017)
- Initial release.
======================================================================================*/
#pragma semicolon 1
#include <sourcemod>
#include <smlib>
#include <l4d_stocks>
#pragma newdecls required
#define USE_DATABASE true // If you want to use the database.
#define USE_SIMPLECOMBAT true
#define DATABASE_CONFIG "l4d2gifts"
#define TAG_GIFT "{G}[{L}GIFTS{G}]\x01"
#define PLUGIN_FCVAR 0 //FCVAR_PLUGIN
#define MAX_GIFTS 20
#define MAX_STRING_WIDTH 64
#define MAX_TYPEGIFTS 2
#define TYPE_ESTANDAR 0
#define TYPE_SPECIAL 1
#define MAX_SPECIALWEAPONS 9
#define TEAM_SURVIVOR 2
#define TEAM_INFECTED 3
#define COLOR_CYAN "0 255 255 255"
#define COLOR_LIGHT_GREEN "144 238 144 255"
#define COLOR_PURPLE "128 0 128 255"
#define COLOR_PINK "250 88 130 255"
#define COLOR_RED "255 0 0 255"
#define COLOR_ORANGE "254 100 46 255"
#define COLOR_YELLOW "255 255 0 255"
#define AURA_CYAN "0 255 255"
#define AURA_BLUE "0 0 255"
#define AURA_GREEN "144 238 144"
#define AURA_PINK "250 88 130"
#define AURA_RED "255 0 0"
#define AURA_ORANGE "254 100 46"
#define AURA_YELLOW "255 255 0"
#define SND_REWARD1 "level/loud/climber.wav"
#define SND_REWARD2 "level/gnomeftw.wav"
// Database handle
Database db = null;
ConVar cvar_gift_enable;
ConVar cvar_gift_life;
ConVar cvar_gift_chance;
ConVar cvar_gift_EPoints;
ConVar cvar_gift_SPoints;
ConVar cvar_gift_probabilityE;
ConVar cvar_gift_probabilityS;
ConVar cvar_gift_maxcollectMap;
ConVar cvar_gift_maxcollectRound;
#if defined(USE_SIMPLECOMBAT) && USE_SIMPLECOMBAT
#include <l4d2_simple_combat>
#endif
char weapons_name[MAX_SPECIALWEAPONS][2][50] =
{
{"weapon_rifle_ak47", "rifle ak47"},
{"weapon_rifle_m60", "rifle m60"},
{"machete", "machete"},
{"knife", "knife"},
{"katana", "katana"},
{"baseball_bat","baseball bat"},
{"weapon_grenade_launcher", "grenade launcher"},
{"weapon_sniper_awp", "sniper awp"},
{"weapon_first_aid_kit", "first aid kit"}
};
int probability_weapon[MAX_SPECIALWEAPONS] = { 30, 30, 40, 40, 40, 50, 15, 5, 10};
int CurrentPointsForMap[MAXPLAYERS+1];
int CurrentPointsForRound[MAXPLAYERS+1];
int CurrentGiftsForMap[MAXPLAYERS+1][MAX_TYPEGIFTS];
int CurrentGiftsForRound[MAXPLAYERS+1][MAX_TYPEGIFTS];
int CurrentGiftsTotalForMap[MAXPLAYERS+1];
int CurrentGiftsTotalForRound[MAXPLAYERS+1];
int TotalGifts[MAXPLAYERS+1][MAX_TYPEGIFTS];
int AllGifts[MAXPLAYERS+1];
char g_sModel[MAX_GIFTS][MAX_STRING_WIDTH];
char g_sTypeModel[MAX_GIFTS][10];
char g_sTypeGift[MAX_GIFTS][10];
float g_fScale[MAX_GIFTS];
int g_GifLife[2000];
char g_sGifType[2000][10];
int g_GifEntIndex[2000];
float g_GiftMov[2000];
char g_sGifSWeapon[2000][50];
bool bDatabase;
bool bGiftEnable;
int iGiftLife;
int iGiftChance;
int iGiftEPoints;
int iGiftSPoints;
int iGiftEProbability;
int iGiftSProbability;
int iGiftMaxMap;
int iGiftMaxRound;
bool g_RoundEnd;
int gifts_collected_map;
int gifts_collected_round;
char sPath_gifts[PLATFORM_MAX_PATH];
int g_iCountGifts;
public Plugin myinfo =
{
name = "礼物",
author = "Aceleraci�n",
description = "Drop gifts when a special infected died and win points & special weapon",
version = PLUGIN_VERSION,
url = "https://forums.alliedmods.net/showthread.php?t=302731"
}
public void OnPluginStart()
{
LoadTranslations("l4d2_gifts.phrases");
CreateConVar("l4d2_gifts", PLUGIN_VERSION, "Plugin version", 0 );
cvar_gift_enable = CreateConVar("l4d2_gifts_enabled", "1", "是否开启插件", PLUGIN_FCVAR, true, 0.0, true, 1.0);
cvar_gift_life = CreateConVar("l4d2_gifts_giflife", "60", "掉落的礼物多久后消失", PLUGIN_FCVAR, true, 0.0);
cvar_gift_chance = CreateConVar("l4d2_gifts_chance", "10", "特感死亡掉落礼物的几率", PLUGIN_FCVAR, true, 1.0, true, 100.0);
cvar_gift_EPoints = CreateConVar("l4d2_gifts_pointsE", "10", "捡起普通礼物获得多少积分", PLUGIN_FCVAR, true, 1.0);
cvar_gift_SPoints = CreateConVar("l4d2_gifts_pointsS", "20", "捡起特殊礼物获得多少积分", PLUGIN_FCVAR, true, 1.0);
cvar_gift_probabilityE = CreateConVar("l4d2_gifts_probabilityE", "92", "标准礼物掉落几率", PLUGIN_FCVAR, true, 1.0, true, 100.0);
cvar_gift_probabilityS = CreateConVar("l4d2_gifts_probabilityS", "8", "特殊礼物掉落几率", PLUGIN_FCVAR, true, 1.0, true, 100.0);
cvar_gift_maxcollectMap = CreateConVar("l4d2_gifts_maxcollectMap", "0", "每张地图幸存者捡起礼物上限.0=无限", PLUGIN_FCVAR, true, 0.0);
cvar_gift_maxcollectRound = CreateConVar("l4d2_gifts_maxcollectRound", "0", "每回合幸存者捡起礼物上限.0=无限", PLUGIN_FCVAR, true, 0.0);
AutoExecConfig(true, "l4d2_gifts");
BuildPath(Path_SM, sPath_gifts, PLATFORM_MAX_PATH, "data/l4d2_gifts.cfg");
if(!FileExists(sPath_gifts))
{
SetFailState("Cannot find the file 'data/l4d2_gifts.cfg'");
}
if(!LoadConfigGifts(false))
{
SetFailState("Cannot load the file 'data/l4d2_gifts.cfg'");
}
if(g_iCountGifts == 0 )
{
SetFailState("Do not have models in 'data/l4d2_gifts.cfg'");
}
HookEvent("round_start", Event_RoundStart);
HookEvent("round_end", Event_RoundEnd);
HookEvent("player_death", Event_PlayerDeath);
// HookEvent("player_use", Event_PlayerUse);
HookConVarChange(cvar_gift_enable, Cvar_Changed1);
HookConVarChange(cvar_gift_life, Cvar_Changed2);
HookConVarChange(cvar_gift_chance, Cvar_Changed3);
HookConVarChange(cvar_gift_EPoints, Cvar_Changed4);
HookConVarChange(cvar_gift_SPoints, Cvar_Changed5);
HookConVarChange(cvar_gift_probabilityE, Cvar_Changed6);
HookConVarChange(cvar_gift_probabilityS, Cvar_Changed6);
HookConVarChange(cvar_gift_maxcollectMap, Cvar_Changed7);
HookConVarChange(cvar_gift_maxcollectRound, Cvar_Changed7);
RegConsoleCmd("sm_giftpoints", Command_GiftPoints, "View points for gifts collected");
RegConsoleCmd("sm_giftp", Command_GiftPoints, "View points for gifts collected");
RegConsoleCmd("sm_giftcollect", Command_GiftCollected, "View number of gifts collected");
RegConsoleCmd("sm_giftc", Command_GiftCollected, "View number of gifts collected");
RegAdminCmd("sm_gift", Command_Gift, ADMFLAG_CHEATS, "Spawn a gift in your position");
RegAdminCmd("sm_reloadgifts", Command_ReloadGift, ADMFLAG_CONFIG, " Reload the config file of gifts (data/l4d2_gifts.cfg)");
}
public void OnMapStart()
{
PrecacheModelGifts();
PrecacheSoundGifts();
for (int i = 1; i <= MaxClients; i++)
{
if(IsClientConnected(i) && IsClientInGame(i) && !IsFakeClient(i) && GetClientTeam(i) == TEAM_SURVIVOR)
{
CurrentPointsForMap[i] = 0;
for (int j=0; j < MAX_TYPEGIFTS; j++)
{
CurrentGiftsForMap[i][j] = 0;
}
CurrentGiftsTotalForMap[i] = 0;
}
}
gifts_collected_map = 0;
}
public void PrecacheModelGifts()
{
for( int i = 0; i < g_iCountGifts; i++ )
{
CheckPrecacheModel(g_sModel[i]);
}
}
public void PrecacheSoundGifts()
{
PrecacheSound(SND_REWARD1, true);
PrecacheSound(SND_REWARD2, true);
}
public void CheckPrecacheModel(char[] Model)
{
if (!IsModelPrecached(Model))
{
PrecacheModel(Model, false);
}
}
public void OnConfigsExecuted()
{
GetCvars();
#if USE_DATABASE
if (!ConnectDB())
{
LogError("Connecting to database failed. Read error log for further details.");
LogError("[GIFTS] Not database found. Points is disabled");
bDatabase = false;
}
else
{
bDatabase = true;
}
#else
bDatabase = false;
#endif
}
public void OnClientPostAdminCheck(int client)
{
#if USE_DATABASE
if(!bDatabase)
{
return;
}
if (!IsValidClient(client) || IsFakeClient(client))
{
return;
}
char ClientID[64];
GetClientRankAuthString(client, ClientID, sizeof(ClientID));
char query[200];
Format(query, sizeof(query), "SELECT `collected_gift`, `collected_gift_standard`, `collected_gift_special` FROM l4d2_gifts WHERE `steamid` = '%s'", ClientID);
SendSQLSelect(query, SQLSelectCallback, client);
#endif
}
public void Cvar_Changed1(ConVar convar, const char[] oldValue, const char[] newValue)
{
int value = StringToInt(newValue);
if(value == 0 || value == 1)
{
SetConVarInt(cvar_gift_enable, value, false, false);
}
else
{
SetConVarInt(cvar_gift_enable, GetConVarInt(cvar_gift_enable), false, false);
}
GetCvars();
}
public void Cvar_Changed2(ConVar convar, const char[] oldValue, const char[] newValue)
{
int value = StringToInt(newValue);
if(value > 0.0)
{
SetConVarInt(cvar_gift_life, value, false, false);
}
else
{
SetConVarInt(cvar_gift_life, GetConVarInt(cvar_gift_life), false, false);
}
GetCvars();
}
public void Cvar_Changed3(ConVar convar, const char[] oldValue, const char[] newValue)
{
int value = StringToInt(newValue);
if(value > 0 && value <= 100)
{
SetConVarInt(cvar_gift_chance, value, false, false);
}
else
{
SetConVarInt(cvar_gift_chance, GetConVarInt(cvar_gift_chance), false, false);
}
GetCvars();
}
public void Cvar_Changed4(ConVar convar, const char[] oldValue, const char[] newValue)
{
SetConVarInt(cvar_gift_EPoints, StringToInt(newValue), false, false);
GetCvars();
}
public void Cvar_Changed5(ConVar convar, const char[] oldValue, const char[] newValue)
{
SetConVarInt(cvar_gift_SPoints, StringToInt(newValue), false, false);
GetCvars();
}
public void Cvar_Changed6(ConVar convar, const char[] oldValue, const char[] newValue)
{
int value = StringToInt(newValue);
if(value > 0 && value <= 100)
{
GetCvars();
}
}
public void Cvar_Changed7(ConVar convar, const char[] oldValue, const char[] newValue)
{
int value = StringToInt(newValue);
if(value > 0)
{
GetCvars();
}
}
void GetCvars()
{
//Values of cvars
bGiftEnable = GetConVarBool(cvar_gift_enable);
iGiftLife = GetConVarInt(cvar_gift_life);
iGiftChance = GetConVarInt(cvar_gift_chance);
iGiftEPoints = GetConVarInt(cvar_gift_EPoints);
iGiftSPoints = GetConVarInt(cvar_gift_SPoints);
iGiftEProbability = GetConVarInt(cvar_gift_probabilityE);
iGiftSProbability = GetConVarInt(cvar_gift_probabilityS);
iGiftMaxMap = GetConVarInt(cvar_gift_maxcollectMap);
iGiftMaxRound = GetConVarInt(cvar_gift_maxcollectRound);
}
bool ConnectDB()
{
if (db != null)
return true;
if (SQL_CheckConfig(DATABASE_CONFIG))
{
char Error[256];
db = SQL_Connect(DATABASE_CONFIG, true, Error, sizeof(Error));
if (db == INVALID_HANDLE)
{
LogError("Failed to connect to database: %s", Error);
return false;
}
if (!CheckDatabaseValidity())
{
char query[400];
Format(query, sizeof(query), "CREATE TABLE IF NOT EXISTS `l4d2_gifts` (`steamid` VARCHAR(64) NOT NULL, `points` int(11) NOT NULL DEFAULT '0', `collected_gift` int(11) NOT NULL, `collected_gift_standard` int(11) NOT NULL, `collected_gift_special` int(11) NOT NULL, PRIMARY KEY (`steamid`)) ENGINE = MyISAM DEFAULT CHARSET = utf8 COLLATE utf8_general_ci");
if(!SQL_FastQuery(db, query))
{
if (SQL_GetError(db, Error, sizeof(Error)))
{
LogError("Query CREATE TABLE l4d2_gifts failed!: %s", Error);
}
else
{
LogError("Database is missing required table or tables.");
}
return false;
}
}
}
else
{
LogError("Databases.cfg missing '%s' entry!", DATABASE_CONFIG);
return false;
}
return true;
}
bool CheckDatabaseValidity()
{
if (!SQL_FastQuery(db, "SELECT * FROM l4d2_gifts WHERE 1 = 2"))
{
return false;
}
return true;
}
public Action Command_Gift(int client, int args)
{
if (!bGiftEnable)
return Plugin_Handled;
if(!IsValidClient(client))
return Plugin_Handled;
if(GetClientTeam(client) != 2 || IsFakeClient(client))
return Plugin_Handled;
if(args < 1)
{
DropGift(client, "random");
}
else
{
char arg1[10];
char arg2[40];
GetCmdArg(1, arg1, sizeof(arg1));
if(StrEqual(arg1, "standard", false))
{
DropGift(client, arg1);
}
else if(StrEqual(arg1, "special", false))
{
if(args < 2)
{
DropGift(client, arg1);
}
else
{
GetCmdArg(2, arg2, sizeof(arg2));
if(L4D2_IsValidWeaponName(arg2) || L4D2_IsValidMeleeWeaponName(arg2))
{
int gift = DropGift(client, arg1);
if(gift > -1)
Format(g_sGifSWeapon[gift], sizeof(g_sGifSWeapon[]), "%s", arg2);
}
else
{
ReplyToCommand(client, "[SM] Invalid weapon_name");
}
}
}
else
{
ReplyToCommand(client, "[SM] Usage: sm_gift | sm_gift standard | sm_gift special [weapon_name]");
}
}
return Plugin_Handled;
}
//==========================================
// CONSOLE COMMANDS
//==========================================
public Action Command_GiftPoints(int client, int args)
{
if (!bGiftEnable)
return Plugin_Handled;
if (!bDatabase)
{
ReplyToCommand(client, "[GIFTS] Points is disabled");
return Plugin_Handled;
}
if(!IsValidClient(client))
return Plugin_Handled;
if(GetClientTeam(client) != 2 || IsFakeClient(client))
return Plugin_Handled;
Client_PrintToChat(client, false, "%s %t", TAG_GIFT, "Your Points for gifts collected");
Client_PrintToChat(client, false, "%t", "In current map: %d", CurrentPointsForMap[client]);
Client_PrintToChat(client, false, "%t", "In current round: %d", CurrentPointsForRound[client]);
return Plugin_Handled;
}
public Action Command_GiftCollected(int client, int args)
{
if (!bGiftEnable)
return Plugin_Handled;
if(!IsValidClient(client))
return Plugin_Handled;
if(GetClientTeam(client) != 2 || IsFakeClient(client))
return Plugin_Handled;
if (bDatabase)
{
Client_PrintToChat(client, false, "%s %t", TAG_GIFT, "Number of gifts collected");
Client_PrintToChat(client, false, "{B}Standard: %t", "In current map: %d | In current round: %d | Throughout the game: %d", CurrentGiftsForMap[client][TYPE_ESTANDAR], CurrentGiftsForRound[client][TYPE_ESTANDAR], TotalGifts[client][TYPE_ESTANDAR]);
Client_PrintToChat(client, false, "{B}Special: %t", "In current map: %d | In current round: %d | Throughout the game: %d", CurrentGiftsForMap[client][TYPE_SPECIAL], CurrentGiftsForRound[client][TYPE_SPECIAL], TotalGifts[client][TYPE_SPECIAL]);
Client_PrintToChat(client, false, "{B}Total: %t", "In current map: %d | In current round: %d | Throughout the game: %d", CurrentGiftsTotalForMap[client], CurrentGiftsTotalForRound[client], AllGifts[client]);
}
else
{
Client_PrintToChat(client, false, "%s %t", TAG_GIFT, "Number of gifts collected");
Client_PrintToChat(client, false, "{B}Standard: %t", "In current map: %d | In current round: %d", CurrentGiftsForMap[client][TYPE_ESTANDAR], CurrentGiftsForRound[client][TYPE_ESTANDAR]);
Client_PrintToChat(client, false, "{B}Special: %t", "In current map: %d | In current round: %d", CurrentGiftsForMap[client][TYPE_SPECIAL], CurrentGiftsForRound[client][TYPE_SPECIAL]);
Client_PrintToChat(client, false, "{B}Total: %t", "In current map: %d | In current round: %d", CurrentGiftsTotalForMap[client], CurrentGiftsTotalForRound[client]);
}
return Plugin_Handled;
}
//==========================================
// ADMINS COMMANDS
//==========================================
public Action Command_ReloadGift(int client, int args)
{
if(!LoadConfigGifts(true))
{
LogError("Cannot load the file 'data/l4d2_gifts.cfg'");
SetConVarInt(cvar_gift_enable, 0 , false, false);
GetCvars();
}
if(g_iCountGifts == 0 )
{
LogError("���Do not have models!!!");
SetConVarInt(cvar_gift_enable, 0 , false, false);
GetCvars();
}
return Plugin_Handled;
}
public bool LoadConfigGifts(bool precache)
{
KeyValues hFile = CreateKeyValues("Gifts");
if(!FileToKeyValues(hFile, sPath_gifts) )
{
CloseHandle(hFile);
return false;
}
KvGotoFirstSubKey(hFile);
g_iCountGifts = 0;
char sTemp[MAX_STRING_WIDTH];
int i = 0;
do
{
char sNum[8];
KvGetSectionName(hFile, sNum, sizeof(sNum));
int num = StringToInt(sNum);
if(num > MAX_GIFTS || i >= MAX_GIFTS)
break;
KvGetString(hFile, "model", sTemp, MAX_STRING_WIDTH);
if(strlen(sTemp) == 0)
continue;
if(FileExists(sTemp, true))
{
strcopy(g_sModel[i], MAX_STRING_WIDTH, sTemp);
KvGetString(hFile, "type", g_sTypeModel[i], sizeof(g_sTypeModel[]), "static");
KvGetString(hFile, "gift", g_sTypeGift[i], sizeof(g_sTypeGift[]));
g_fScale[i] = KvGetFloat(hFile, "scale", 1.0);
g_iCountGifts++;
i++;
}
}
while (KvGotoNextKey(hFile));
CloseHandle(hFile);
if(precache)
{
PrecacheModelGifts();
}
return true;
}
public Action Event_RoundStart(Event event, const char[] name, bool dontBroadcast)
{
if (!bGiftEnable)
return;
g_RoundEnd = false;
gifts_collected_round = 0;
for (int i = 1; i <= MaxClients; i++)
{
if(IsClientConnected(i) && IsClientInGame(i) && !IsFakeClient(i) && GetClientTeam(i) == TEAM_SURVIVOR)
{
CurrentPointsForRound[i] = 0;
for (int j=0; j < MAX_TYPEGIFTS; j++)
{
CurrentGiftsForRound[i][j] = 0;
}
CurrentGiftsTotalForRound[i] = 0;
}
}
}
public Action Event_RoundEnd(Event event, const char[] name, bool dontBroadcast)
{
if (!bGiftEnable)
return;
g_RoundEnd = true;
gifts_collected_round = 0;
}
public Action Event_PlayerDeath(Event event, const char[] name, bool dontBroadcast)
{
if (!bGiftEnable)
return;
if (iGiftMaxRound != 0 && gifts_collected_round > iGiftMaxRound)
return;
if (iGiftMaxMap != 0 && gifts_collected_map > iGiftMaxMap)
return;
int victim = GetClientOfUserId(GetEventInt(event, "userid"));
int attacker = GetClientOfUserId(GetEventInt(event, "attacker"));
if (IsValidClient(victim) && GetClientTeam(victim) == 3 && IsValidClient(attacker) && GetClientTeam(attacker) == 2)
{
if(Infected_Admitted(victim) != -1)
{
if (GetRandomInt(1, 100) < iGiftChance)
{
DropGift(victim);
}
}
}
}
// When a Survivor presses +USE on gifts physics
/*
public Action Event_PlayerUse(Event event, const char[] name, bool dontBroadcast)
{
if (!bGiftEnable)
return;
int client = GetClientOfUserId(GetEventInt(event, "userid"));
int gift = EntRefToEntIndex(GetEventInt(event, "targetid"));
if(!IsValidClient(client) || IsFakeClient(client))
return;
if (IsValidEntity(gift))
{
char classname[30];
GetEntityClassname(gift, classname, sizeof(classname));
if(StrContains(classname, "physics") != -1)
{
if(g_GifEntIndex[gift] == EntIndexToEntRef(gift))
{
int Score;
int type;
if(StrEqual(g_sGifType[gift], "standard"))
{
//Points for Gifts Standard
Score = iGiftEPoints;
NotifyGift(client, TYPE_ESTANDAR, Score);
type = TYPE_ESTANDAR;
}
else
{
//Points for Gifts Special
Score = iGiftSPoints;
NotifyGift(client, TYPE_SPECIAL, Score, gift);
type = TYPE_SPECIAL;
}
if (bDatabase)
{
char query[600];
char ClientID[100];
char giftCollected[40];
Format(giftCollected, sizeof(giftCollected), "collected_gift_%s", g_sGifType[gift]);
GetClientRankAuthString(client, ClientID, sizeof(ClientID));
Format(query, sizeof(query), "UPDATE l4d2_gifts SET points = points + %i, collected_gift = collected_gift + 1, %s = %s + 1 WHERE steamid = '%s'", Score, giftCollected, giftCollected, ClientID);
DataPack data = CreateDataPack();
WritePackCell(data, client);
WritePackCell(data, Score);
WritePackCell(data, type);
SendSQLUpdate(query, SQLCallback, data);
}
AcceptEntityInput(gift, "kill");
gifts_collected_map += 1;
gifts_collected_round += 1;
}
}
}
}
*/
// When a Survivor presses +USE on gifts static
/*
public Action OnPlayerRunCmd(int client, int &buttons, int &impulse, float vel[3], float angles[3])
{
if (!bGiftEnable)
return Plugin_Continue;
//Check if its a valid player
if (!IsValidClient(client) || IsFakeClient(client))
{
return Plugin_Continue;
}
if (buttons & IN_USE)
{
int gift = GetClientAimTarget(client, false);
if (IsValidEntity(gift))
{
char classname[30];
float myPos[3];
float gfPos[3];
GetEntPropVector(gift, Prop_Send, "m_vecOrigin", gfPos);
if (IsPlayerAlive(client) && !IsFakeClient(client) && GetClientTeam(client) == 2)
{
GetEntPropVector(client, Prop_Send, "m_vecOrigin", myPos);
//PrintToChatAll("%f", GetVectorDistance(myPos, gfPos));
if (GetVectorDistance(myPos, gfPos) < 70.0)
{
GetEntityClassname(gift, classname, sizeof(classname));
if(StrContains(classname, "dynamic") != -1)
{
if(g_GifEntIndex[gift] == EntIndexToEntRef(gift))
{
int Score;
int type;
if(StrEqual(g_sGifType[gift], "standard"))
{
//Points for Gifts Standard
Score = iGiftEPoints;
NotifyGift(client, TYPE_ESTANDAR, Score);
type = TYPE_ESTANDAR;
}
else
{
//Points for Gifts Special
Score = iGiftSPoints;
NotifyGift(client, TYPE_SPECIAL, Score, gift);
type = TYPE_SPECIAL;
}
if (bDatabase)
{
char query[600];
char ClientID[100];
char giftCollected[40];
Format(giftCollected, sizeof(giftCollected), "collected_gift_%s", g_sGifType[gift]);
GetClientRankAuthString(client, ClientID, sizeof(ClientID));
Format(query, sizeof(query), "UPDATE l4d2_gifts SET points = points + %i, collected_gift = collected_gift + 1, %s = %s + 1 WHERE steamid = '%s'", Score, giftCollected, giftCollected, ClientID);
DataPack data = CreateDataPack();
WritePackCell(data, client);
WritePackCell(data, Score);
WritePackCell(data, type);
SendSQLUpdate(query, SQLCallback, data);
}
AcceptEntityInput(gift, "kill");
gifts_collected_map += 1;
gifts_collected_round += 1;
}
}
}
}
}
}
return Plugin_Continue;
}
*/
void SendSQLUpdate(const char[] query, SQLTCallback callback=INVALID_FUNCTION, Handle data = INVALID_HANDLE)
{
if (db == INVALID_HANDLE)
{
return;
}
if (callback == INVALID_FUNCTION)
{
callback = SQLCallback;
}
SQL_TQuery(db, callback, query, data);
}
void SendSQLSelect(const char[] query, SQLTCallback callback=INVALID_FUNCTION, int client)
{
if (db == INVALID_HANDLE)
{
return;
}
if (callback == INVALID_FUNCTION)
{
callback = SQLCallback;
}
SQL_TQuery(db, callback, query, client);
}
public void SQLSelectCallback(Handle owner, Handle hndl, const char[] error, any client)
{
if (db == INVALID_HANDLE)
{
return;
}
if (hndl == INVALID_HANDLE)
{
LogError("SQL Error: %s (Select collected gifts player. Query failed)", error);
return;
}
while (SQL_FetchRow(hndl))
{
AllGifts[client] = SQL_FetchInt(hndl, 0);
TotalGifts[client][TYPE_ESTANDAR] = SQL_FetchInt(hndl, 1);
TotalGifts[client][TYPE_SPECIAL] = SQL_FetchInt(hndl, 2);
}
}
public void SQLCallback(Handle owner, Handle hndl, const char[] error, any data)
{
if (db == INVALID_HANDLE)
{
return;
}
if (hndl == INVALID_HANDLE)
{
LogError("SQL Error: %s (Update points player. Query failed)", error);
return;
}
ResetPack(data);
int client = ReadPackCell(data);
int Score = ReadPackCell(data);
int type = ReadPackCell(data);
CloseHandle(data);
if(!IsValidClient(client))
return;
AddScore(client, Score, type);
}
void NotifyGift(int client, int type, int Score, int gift = -1)
{
float origin[3];
if(IsValidClient(client))
GetClientAbsOrigin(client, origin);
if(type == TYPE_ESTANDAR)
{
/*
if (bDatabase)
{
Client_PrintToChatAll(false, "%s %t", TAG_GIFT, "Spawn Gift Standard", client, Score);
PrintToChat(client, "\x04+%i", Score);
}
else
{
Client_PrintToChatAll(false, "%s %t", TAG_GIFT, "Spawn Gift Standard Not Points", client);
}
*/
EmitSoundToAll(SND_REWARD1, client, _, _, _, _, _, _, origin);
AddCollect(client, type);
}