forked from SilvDev/Various_Scripts_Collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
l4d_weapon_spawn.sp
1908 lines (1611 loc) · 53.9 KB
/
l4d_weapon_spawn.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
/*
* Weapon Spawn
* Copyright (C) 2023 Silvers
*
* 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
* (at your option) 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 <https://www.gnu.org/licenses/>.
*/
#define PLUGIN_VERSION "1.14"
/*======================================================================================
Plugin Info:
* Name : [L4D & L4D2] Weapon Spawn
* Author : SilverShot
* Descrp : Spawns a single weapon fixed in position, these can be temporary or saved for auto-spawning.
* Link : https://forums.alliedmods.net/showthread.php?t=222934
* Plugins : https://sourcemod.net/plugins.php?exact=exact&sortby=title&search=1&author=Silvers
========================================================================================
Change Log:
1.14 (25-May-2023)
- Fixed the M60, Grenade Launcher and Chainsaw not following the count cvar limit. Thanks to "gamer_kanelita" for reporting.
1.13 (20-Sep-2022)
- Fixed incorrect model list. Thanks to "HarryPotter" for reporting.
1.12 (10-Aug-2022)
- Fixed the plugin attempting to back up the data file even after converting to version 2 format. Thanks to "HarryPotter" for reporting.
1.11 (04-Jun-2022)
- Fixed not updating the full data config if an index was missing. Now throws errors to warn about missing indexes.
- Plugin will auto backs up the previous data config before updating it.
1.10 (04-Jun-2022)
- L4D2: Plugin now automatically converts old /data/ configs to use the new index values. Previous version was spawning the wrong types.
- Thanks to "KoMiKoZa" for reporting.
1.9 (26-May-2022)
- Changed the menu order of items to group similar types together.
- Menu now displays the last page that was selected instead of returning to the first page.
1.8 (23-Apr-2022)
- Changes to allow "CSS" weapons to spawn multiple copies with the "l4d_weapon_spawn_count" cvar. Thanks to "vikingo12" for reporting.
1.7 (15-Feb-2021)
- Added new command "sm_weapon_spawn_mov" for direct console control of position. Thanks to "eyeonus" for scripting.
- Added new command "sm_weapon_spawn_rot" for direct console control of rotation. Thanks to "eyeonus" for scripting.
- Fixed invalid convar handles in L4D1. Thanks to "CryWolf" for reporting.
1.6 (10-May-2020)
- Blocked glow command and convars from L4D1 which does not support glows.
- Extra checks to prevent "IsAllowedGameMode" throwing errors.
- Various changes to tidy up code.
- Various optimizations and fixes.
1.5 (01-Apr-2020)
- Fixed "IsAllowedGameMode" from throwing errors when the "_tog" cvar was changed before MapStart.
1.4 (05-May-2018)
- Converted plugin source to the latest syntax utilizing methodmaps. Requires SourceMod 1.8 or newer.
- Changed cvar "l4d_weapon_spawn_modes_tog" now supports L4D1.
1.3.2 (20-May-2017)
- Added cvar "l4d_weapon_spawn_glow" to set the glow color.
1.3.1 (20-Nov-2015)
- Fixed the Sniper Scout not spawning when "l4d_weapon_spawn_count" cvar was set to 0.
1.3 (19-Nov-2015)
- Fixed Auto Shotgun ammo in L4D1 not filling.
- Fixed some guns not spawning when "l4d_weapon_spawn_count" cvar was set to 0.
1.2 (29-Mar-2015)
- Fixed the plugin not loading in L4D1 due to an Invalid Convar Handle.
1.1 (18-Aug-2013)
- Added cvar "l4d_weapon_spawn_count" to set how many times a spawner gives items/weapons before disappearing.
- Added cvar "l4d_weapon_spawn_randomise" to randomise the spawns based on a chance out of 100.
1.0 (09-Aug-2013)
- Initial release.
========================================================================================
Thanks:
This plugin was made using source code from the following plugins.
If I have used your code and not credited you, please let me know.
* "Zuko & McFlurry" for "[L4D2] Weapon/Zombie Spawner" - Modified SetTeleportEndPoint function.
https://forums.alliedmods.net/showthread.php?t=109659
* Thanks to "Boikinov" for "[L4D] Left FORT Dead builder" - RotateYaw function to rotate ground flares
https://forums.alliedmods.net/showthread.php?t=93716
======================================================================================*/
#pragma semicolon 1
#pragma newdecls required
#include <sourcemod>
#include <sdktools>
#define CVAR_FLAGS FCVAR_NOTIFY
#define CHAT_TAG "\x04[\x05Weapon Spawn\x04] \x01"
#define CONFIG_SPAWNS "data/l4d_spawn_weapon.cfg"
#define MAX_SPAWNS 32
#define MAX_WEAPONS 10
#define MAX_WEAPONS2 29
ConVar g_hCvarAllow, g_hCvarCount, g_hCvarGlow, g_hCvarGlowCol, g_hCvarMPGameMode, g_hCvarModes, g_hCvarModesOff, g_hCvarModesTog, g_hCvarRandom, g_hCvarRandomise;
int g_iCvarCount, g_iCvarGlow, g_iCvarGlowCol, g_iCvarRandom, g_iCvarRandomise, g_iPlayerSpawn, g_iRoundStart, g_iSave[MAXPLAYERS+1], g_iSpawnCount, g_iSpawns[MAX_SPAWNS][3];
bool g_bCvarAllow, g_bMapStarted, g_bLeft4Dead2, g_bLoaded;
Menu g_hMenuAng, g_hMenuList, g_hMenuPos;
ConVar g_hAmmoAutoShot, g_hAmmoChainsaw, g_hAmmoGL, g_hAmmoHunting, g_hAmmoM60, g_hAmmoRifle, g_hAmmoShotgun, g_hAmmoSmg, g_hAmmoSniper;
int g_iAmmoAutoShot, g_iAmmoChainsaw, g_iAmmoGL, g_iAmmoHunting, g_iAmmoM60, g_iAmmoRifle, g_iAmmoShotgun, g_iAmmoSmg, g_iAmmoSniper;
static char g_sWeaponNames[MAX_WEAPONS][] =
{
"Rifle",
"Auto Shotgun",
"Hunting Rifle",
"SMG",
"Pump Shotgun",
"Pistol",
"Molotov",
"Pipe Bomb",
"First Aid Kit",
"Pain Pills"
};
static char g_sWeapons[MAX_WEAPONS][] =
{
"weapon_rifle",
"weapon_autoshotgun",
"weapon_hunting_rifle",
"weapon_smg",
"weapon_pumpshotgun",
"weapon_pistol",
"weapon_molotov",
"weapon_pipe_bomb",
"weapon_first_aid_kit",
"weapon_pain_pills"
};
static char g_sWeaponModels[MAX_WEAPONS][] =
{
"models/w_models/weapons/w_rifle_m16a2.mdl",
"models/w_models/weapons/w_autoshot_m4super.mdl",
"models/w_models/weapons/w_sniper_mini14.mdl",
"models/w_models/weapons/w_smg_uzi.mdl",
"models/w_models/weapons/w_shotgun.mdl",
"models/w_models/weapons/w_pistol_1911.mdl",
"models/w_models/weapons/w_eq_molotov.mdl",
"models/w_models/weapons/w_eq_pipebomb.mdl",
"models/w_models/weapons/w_eq_medkit.mdl",
"models/w_models/weapons/w_eq_painpills.mdl"
};
static char g_sWeaponNames2[MAX_WEAPONS2][] =
{
"Pistol",
"Pistol Magnum",
"Rifle",
"AK47",
"SG552",
"Rifle Desert",
"Auto Shotgun",
"Shotgun Spas",
"Pump Shotgun",
"Shotgun Chrome",
"SMG",
"SMG Silenced",
"SMG MP5",
"Hunting Rifle",
"Sniper AWP",
"Sniper Military",
"Sniper Scout",
"M60",
"Grenade Launcher",
"Chainsaw",
"Molotov",
"Pipe Bomb",
"VomitJar",
"Pain Pills",
"Adrenaline",
"First Aid Kit",
"Defibrillator",
"Upgradepack Explosive",
"Upgradepack Incendiary"
};
static char g_sWeapons2[MAX_WEAPONS2][] =
{
"weapon_pistol",
"weapon_pistol_magnum",
"weapon_rifle",
"weapon_rifle_ak47",
"weapon_rifle_sg552",
"weapon_rifle_desert",
"weapon_autoshotgun",
"weapon_shotgun_spas",
"weapon_pumpshotgun",
"weapon_shotgun_chrome",
"weapon_smg",
"weapon_smg_silenced",
"weapon_smg_mp5",
"weapon_hunting_rifle",
"weapon_sniper_awp",
"weapon_sniper_military",
"weapon_sniper_scout",
"weapon_rifle_m60",
"weapon_grenade_launcher",
"weapon_chainsaw",
"weapon_molotov",
"weapon_pipe_bomb",
"weapon_vomitjar",
"weapon_pain_pills",
"weapon_adrenaline",
"weapon_first_aid_kit",
"weapon_defibrillator",
"weapon_upgradepack_explosive",
"weapon_upgradepack_incendiary"
};
static char g_sWeaponModels2[MAX_WEAPONS2][] =
{
"models/w_models/weapons/w_pistol_b.mdl",
"models/w_models/weapons/w_desert_eagle.mdl",
"models/w_models/weapons/w_rifle_m16a2.mdl",
"models/w_models/weapons/w_rifle_ak47.mdl",
"models/w_models/weapons/w_rifle_sg552.mdl",
"models/w_models/weapons/w_desert_rifle.mdl",
"models/w_models/weapons/w_autoshot_m4super.mdl",
"models/w_models/weapons/w_shotgun_spas.mdl",
"models/w_models/weapons/w_shotgun.mdl",
"models/w_models/weapons/w_pumpshotgun_a.mdl",
"models/w_models/weapons/w_smg_uzi.mdl",
"models/w_models/weapons/w_smg_a.mdl",
"models/w_models/weapons/w_smg_mp5.mdl",
"models/w_models/weapons/w_sniper_mini14.mdl",
"models/w_models/weapons/w_sniper_awp.mdl",
"models/w_models/weapons/w_sniper_military.mdl",
"models/w_models/weapons/w_sniper_scout.mdl",
"models/w_models/weapons/w_m60.mdl",
"models/w_models/weapons/w_grenade_launcher.mdl",
"models/weapons/melee/w_chainsaw.mdl",
"models/w_models/weapons/w_eq_molotov.mdl",
"models/w_models/weapons/w_eq_pipebomb.mdl",
"models/w_models/weapons/w_eq_bile_flask.mdl",
"models/w_models/weapons/w_eq_painpills.mdl",
"models/w_models/weapons/w_eq_adrenaline.mdl",
"models/w_models/weapons/w_eq_medkit.mdl",
"models/w_models/weapons/w_eq_defibrillator.mdl",
"models/w_models/weapons/w_eq_explosive_ammopack.mdl",
"models/w_models/weapons/w_eq_incendiary_ammopack.mdl"
};
// ====================================================================================================
// PLUGIN INFO / START / END
// ====================================================================================================
public Plugin myinfo =
{
name = "[L4D & L4D2] Weapon Spawn",
author = "SilverShot",
description = "Spawns a weapon in a weapon crate/locker, these can be temporary or saved for auto-spawning.",
version = PLUGIN_VERSION,
url = "https://forums.alliedmods.net/showthread.php?t=222934"
}
public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max)
{
EngineVersion test = GetEngineVersion();
if( test == Engine_Left4Dead ) g_bLeft4Dead2 = false;
else if( test == Engine_Left4Dead2 ) g_bLeft4Dead2 = true;
else
{
strcopy(error, err_max, "Plugin only supports Left 4 Dead 1 & 2.");
return APLRes_SilentFailure;
}
return APLRes_Success;
}
public void OnPluginStart()
{
// Cvars
g_hCvarAllow = CreateConVar( "l4d_weapon_spawn_allow", "1", "0=Plugin off, 1=Plugin on.", CVAR_FLAGS );
if( g_bLeft4Dead2 )
{
g_hCvarGlow = CreateConVar( "l4d_weapon_spawn_glow", "100", "0=Off. Any other value is the range at which the glow will turn on.", CVAR_FLAGS );
g_hCvarGlowCol = CreateConVar( "l4d_weapon_spawn_glow_color", "0 255 0", "0=Default glow color. Three values between 0-255 separated by spaces. RGB Color255 - Red Green Blue.", CVAR_FLAGS );
}
g_hCvarModes = CreateConVar( "l4d_weapon_spawn_modes", "", "Turn on the plugin in these game modes, separate by commas (no spaces). (Empty = all).", CVAR_FLAGS );
g_hCvarModesOff = CreateConVar( "l4d_weapon_spawn_modes_off", "", "Turn off the plugin in these game modes, separate by commas (no spaces). (Empty = none).", CVAR_FLAGS );
g_hCvarModesTog = CreateConVar( "l4d_weapon_spawn_modes_tog", "0", "Turn on the plugin in these game modes. 0=All, 1=Coop, 2=Survival, 4=Versus, 8=Scavenge. Add numbers together.", CVAR_FLAGS );
g_hCvarCount = CreateConVar( "l4d_weapon_spawn_count", "1", "0=Infinite. How many items/weapons to give from 1 spawner.", CVAR_FLAGS );
g_hCvarRandom = CreateConVar( "l4d_weapon_spawn_random", "-1", "-1=All, 0=None. Otherwise randomly select this many weapons to spawn from the maps config.", CVAR_FLAGS );
g_hCvarRandomise = CreateConVar( "l4d_weapon_spawn_randomise", "25", "0=Off. Chance out of 100 to randomise the type of item/weapon regardless of what it's set to.", CVAR_FLAGS );
CreateConVar( "l4d_weapon_spawn_version", PLUGIN_VERSION, "Weapon Spawn plugin version.", FCVAR_NOTIFY|FCVAR_DONTRECORD);
AutoExecConfig(true, "l4d_weapon_spawn");
g_hCvarMPGameMode = FindConVar("mp_gamemode");
g_hCvarMPGameMode.AddChangeHook(ConVarChanged_Allow);
g_hCvarAllow.AddChangeHook(ConVarChanged_Allow);
g_hCvarModes.AddChangeHook(ConVarChanged_Allow);
g_hCvarModesOff.AddChangeHook(ConVarChanged_Allow);
g_hCvarModesTog.AddChangeHook(ConVarChanged_Allow);
g_hCvarCount.AddChangeHook(ConVarChanged_Cvars);
g_hCvarRandom.AddChangeHook(ConVarChanged_Cvars);
g_hCvarRandomise.AddChangeHook(ConVarChanged_Cvars);
if( g_bLeft4Dead2 )
{
g_hCvarGlow.AddChangeHook(ConVarChanged_Glow);
g_hCvarGlowCol.AddChangeHook(ConVarChanged_Glow);
}
g_hAmmoRifle = FindConVar("ammo_assaultrifle_max");
g_hAmmoSmg = FindConVar("ammo_smg_max");
g_hAmmoHunting = FindConVar("ammo_huntingrifle_max");
g_hAmmoRifle.AddChangeHook(ConVarChanged_Cvars);
g_hAmmoSmg.AddChangeHook(ConVarChanged_Cvars);
g_hAmmoHunting.AddChangeHook(ConVarChanged_Cvars);
if( g_bLeft4Dead2 )
{
g_hAmmoShotgun = FindConVar("ammo_shotgun_max");
g_hAmmoGL = FindConVar("ammo_grenadelauncher_max");
g_hAmmoChainsaw = FindConVar("ammo_chainsaw_max");
g_hAmmoAutoShot = FindConVar("ammo_autoshotgun_max");
g_hAmmoM60 = FindConVar("ammo_m60_max");
g_hAmmoSniper = FindConVar("ammo_sniperrifle_max");
g_hAmmoGL.AddChangeHook(ConVarChanged_Cvars);
g_hAmmoChainsaw.AddChangeHook(ConVarChanged_Cvars);
g_hAmmoAutoShot.AddChangeHook(ConVarChanged_Cvars);
g_hAmmoM60.AddChangeHook(ConVarChanged_Cvars);
g_hAmmoSniper.AddChangeHook(ConVarChanged_Cvars);
} else {
g_hAmmoShotgun = FindConVar("ammo_buckshot_max");
}
g_hAmmoShotgun.AddChangeHook(ConVarChanged_Cvars);
// Commands
RegAdminCmd("sm_weapon_spawn", CmdSpawnerTemp, ADMFLAG_ROOT, "Opens a menu of weapons/items to spawn. Spawns a temporary weapon at your crosshair.");
RegAdminCmd("sm_weapon_spawn_save", CmdSpawnerSave, ADMFLAG_ROOT, "Opens a menu of weapons/items to spawn. Spawns a weapon at your crosshair and saves to config.");
RegAdminCmd("sm_weapon_spawn_del", CmdSpawnerDel, ADMFLAG_ROOT, "Removes the weapon you are pointing at and deletes from the config if saved.");
RegAdminCmd("sm_weapon_spawn_clear", CmdSpawnerClear, ADMFLAG_ROOT, "Removes all weapons spawned by this plugin from the current map.");
RegAdminCmd("sm_weapon_spawn_wipe", CmdSpawnerWipe, ADMFLAG_ROOT, "Removes all weapons spawned by this plugin from the current map and deletes them from the config.");
if( g_bLeft4Dead2 )
RegAdminCmd("sm_weapon_spawn_glow", CmdSpawnerGlow, ADMFLAG_ROOT, "Toggle to enable glow on all weapons to see where they are placed.");
RegAdminCmd("sm_weapon_spawn_list", CmdSpawnerList, ADMFLAG_ROOT, "Display a list weapon positions and the total number of.");
RegAdminCmd("sm_weapon_spawn_tele", CmdSpawnerTele, ADMFLAG_ROOT, "Teleport to a weapon (Usage: sm_weapon_spawn_tele <index: 1 to MAX_SPAWNS (32)>).");
RegAdminCmd("sm_weapon_spawn_ang", CmdSpawnerAng, ADMFLAG_ROOT, "Displays a menu to adjust the weapon angles your crosshair is over.");
RegAdminCmd("sm_weapon_spawn_rot", CmdSpawnerRot, ADMFLAG_ROOT, "Rotate weapon. Usage: sm_weapon_spawn_rot {x|y|z|} {degree}");
RegAdminCmd("sm_weapon_spawn_pos", CmdSpawnerPos, ADMFLAG_ROOT, "Displays a menu to adjust the weapon origin your crosshair is over.");
RegAdminCmd("sm_weapon_spawn_mov", CmdSpawnerMov, ADMFLAG_ROOT, "Move weapon. Usage: sm_weapon_spawn_mov {x|y|z} {distance}");
// Menu
g_hMenuList = new Menu(ListMenuHandler);
int max = MAX_WEAPONS;
if( g_bLeft4Dead2 ) max = MAX_WEAPONS2;
for( int i = 0; i < max; i++ )
{
g_hMenuList.AddItem("", g_bLeft4Dead2 ? g_sWeaponNames2[i] : g_sWeaponNames[i]);
}
g_hMenuList.SetTitle("Spawn Weapon");
g_hMenuList.ExitBackButton = true;
// Config version
if( g_bLeft4Dead2 )
{
int iMod, iNum, iIndex;
bool process;
char sKey[128];
char sPath[PLATFORM_MAX_PATH];
BuildPath(Path_SM, sPath, sizeof(sPath), CONFIG_SPAWNS);
if( FileExists(sPath) )
{
// Load config
KeyValues hFile = new KeyValues("spawns");
if( hFile.ImportFromFile(sPath) )
{
// Version check
if( hFile.GetNum("version", 1) != 2 )
{
char sNew[PLATFORM_MAX_PATH];
BuildPath(Path_SM, sNew, sizeof(sNew), "%s.backup", CONFIG_SPAWNS);
RenameFile(sNew, sPath);
int iNew[] = { 2, 6, 13, 10, 8, 0, 20, 21, 25, 23, 9, 5, 18, 17, 3, 4, 7, 11, 12, 14, 15, 16, 19, 1, 22, 26, 27, 28, 24 };
hFile.GotoFirstSubKey(false);
process = true;
while( process )
{
// hFile.GetSectionName(sKey, sizeof(sKey));
// PrintToServer("Section: %s", sKey);
iNum = hFile.GetNum("num", 0);
iIndex = 0;
while( iIndex < iNum )
{
iIndex++;
IntToString(iIndex, sKey, sizeof(sKey));
if( hFile.JumpToKey(sKey) )
{
iMod = hFile.GetNum("mod", -1);
if( iMod != -1 )
{
// PrintToServer("New: %s (%d > %d)", sKey, iMod, iNew[iMod]);
iMod = iNew[iMod];
hFile.SetNum("mod", iMod);
}
hFile.GoBack();
} else {
hFile.GetSectionName(sKey, sizeof(sKey));
LogError("Update warning: missing index detected: \"%d\" from \"%s\" in \"%s\". Suggest manually fixing, this could break some functionality.", iIndex, sKey, CONFIG_SPAWNS);
}
}
if( !hFile.GotoNextKey(false) )
{
process = false;
}
}
// Save cfg
hFile.GoBack();
hFile.SetNum("version", 2);
hFile.Rewind();
hFile.ExportToFile(sPath);
}
}
delete hFile;
}
}
}
public void OnPluginEnd()
{
ResetPlugin();
}
public void OnMapStart()
{
g_bMapStarted = true;
int max = MAX_WEAPONS;
if( g_bLeft4Dead2 ) max = MAX_WEAPONS2;
for( int i = 0; i < max; i++ )
{
PrecacheModel(g_bLeft4Dead2 ? g_sWeaponModels2[i] : g_sWeaponModels[i], true);
}
}
public void OnMapEnd()
{
g_bMapStarted = false;
ResetPlugin(false);
}
// ====================================================================================================
// CVARS
// ====================================================================================================
public void OnConfigsExecuted()
{
IsAllowed();
}
void ConVarChanged_Allow(Handle convar, const char[] oldValue, const char[] newValue)
{
IsAllowed();
}
void ConVarChanged_Cvars(Handle convar, const char[] oldValue, const char[] newValue)
{
GetCvars();
}
void ConVarChanged_Glow(Handle convar, const char[] oldValue, const char[] newValue)
{
g_iCvarGlow = g_hCvarGlow.IntValue;
g_iCvarGlowCol = GetColor(g_hCvarGlowCol);
VendorGlow(g_iCvarGlow);
}
int GetColor(ConVar hCvar)
{
char sTemp[12];
hCvar.GetString(sTemp, sizeof(sTemp));
if( sTemp[0] == 0 )
return 0;
char sColors[3][4];
int color = ExplodeString(sTemp, " ", sColors, sizeof(sColors), sizeof(sColors[]));
if( color != 3 )
return 0;
color = StringToInt(sColors[0]);
color += 256 * StringToInt(sColors[1]);
color += 65536 * StringToInt(sColors[2]);
return color;
}
void GetCvars()
{
g_iCvarCount = g_hCvarCount.IntValue;
g_iCvarRandom = g_hCvarRandom.IntValue;
g_iCvarRandomise = g_hCvarRandomise.IntValue;
g_iAmmoRifle = g_hAmmoRifle.IntValue;
g_iAmmoShotgun = g_hAmmoShotgun.IntValue;
g_iAmmoSmg = g_hAmmoSmg.IntValue;
g_iAmmoHunting = g_hAmmoHunting.IntValue;
if( g_bLeft4Dead2 )
{
g_iAmmoGL = g_hAmmoGL.IntValue;
g_iAmmoChainsaw = g_hAmmoChainsaw.IntValue;
g_iAmmoAutoShot = g_hAmmoAutoShot.IntValue;
g_iAmmoM60 = g_hAmmoM60.IntValue;
g_iAmmoSniper = g_hAmmoSniper.IntValue;
}
}
void IsAllowed()
{
bool bCvarAllow = g_hCvarAllow.BoolValue;
bool bAllowMode = IsAllowedGameMode();
GetCvars();
if( g_bCvarAllow == false && bCvarAllow == true && bAllowMode == true )
{
g_bCvarAllow = true;
LoadSpawns();
if( g_bLeft4Dead2 )
HookEvent("player_use", Event_PlayerUse);
HookEvent("player_spawn", Event_PlayerSpawn, EventHookMode_PostNoCopy);
HookEvent("round_start", Event_RoundStart, EventHookMode_PostNoCopy);
HookEvent("round_end", Event_RoundEnd, EventHookMode_PostNoCopy);
}
else if( g_bCvarAllow == true && (bCvarAllow == false || bAllowMode == false) )
{
g_bCvarAllow = false;
ResetPlugin();
if( g_bLeft4Dead2 )
UnhookEvent("player_use", Event_PlayerUse);
UnhookEvent("player_spawn", Event_PlayerSpawn, EventHookMode_PostNoCopy);
UnhookEvent("round_start", Event_RoundStart, EventHookMode_PostNoCopy);
UnhookEvent("round_end", Event_RoundEnd, EventHookMode_PostNoCopy);
}
}
int g_iCurrentMode;
bool IsAllowedGameMode()
{
if( g_hCvarMPGameMode == null )
return false;
int iCvarModesTog = g_hCvarModesTog.IntValue;
if( iCvarModesTog != 0 )
{
if( g_bMapStarted == false )
return false;
g_iCurrentMode = 0;
int entity = CreateEntityByName("info_gamemode");
if( IsValidEntity(entity) )
{
DispatchSpawn(entity);
HookSingleEntityOutput(entity, "OnCoop", OnGamemode, true);
HookSingleEntityOutput(entity, "OnSurvival", OnGamemode, true);
HookSingleEntityOutput(entity, "OnVersus", OnGamemode, true);
HookSingleEntityOutput(entity, "OnScavenge", OnGamemode, true);
ActivateEntity(entity);
AcceptEntityInput(entity, "PostSpawnActivate");
if( IsValidEntity(entity) ) // Because sometimes "PostSpawnActivate" seems to kill the ent.
RemoveEdict(entity); // Because multiple plugins creating at once, avoid too many duplicate ents in the same frame
}
if( g_iCurrentMode == 0 )
return false;
if( !(iCvarModesTog & g_iCurrentMode) )
return false;
}
char sGameModes[64], sGameMode[64];
g_hCvarMPGameMode.GetString(sGameMode, sizeof(sGameMode));
Format(sGameMode, sizeof(sGameMode), ",%s,", sGameMode);
g_hCvarModes.GetString(sGameModes, sizeof(sGameModes));
if( sGameModes[0] )
{
Format(sGameModes, sizeof(sGameModes), ",%s,", sGameModes);
if( StrContains(sGameModes, sGameMode, false) == -1 )
return false;
}
g_hCvarModesOff.GetString(sGameModes, sizeof(sGameModes));
if( sGameModes[0] )
{
Format(sGameModes, sizeof(sGameModes), ",%s,", sGameModes);
if( StrContains(sGameModes, sGameMode, false) != -1 )
return false;
}
return true;
}
void OnGamemode(const char[] output, int caller, int activator, float delay)
{
if( strcmp(output, "OnCoop") == 0 )
g_iCurrentMode = 1;
else if( strcmp(output, "OnSurvival") == 0 )
g_iCurrentMode = 2;
else if( strcmp(output, "OnVersus") == 0 )
g_iCurrentMode = 4;
else if( strcmp(output, "OnScavenge") == 0 )
g_iCurrentMode = 8;
}
// ====================================================================================================
// EVENTS
// ====================================================================================================
// Re-create M60, Grenade Launcher and Chainsaw on pickup when set to inifinite/increased spawn count
void Event_PlayerUse(Event event, const char[] name, bool dontBroadcast)
{
int entity = event.GetInt("targetid");
if( entity > MaxClients && IsValidEntity(entity) )
{
static char classname[32];
GetEntityClassname(entity, classname, sizeof(classname));
int type;
if( strncmp(classname, "weapon_rifle_m60", 16) == 0 ) type = 16;
else if( strncmp(classname, "weapon_grenade_launcher", 23) == 0 ) type = 18;
else if( strncmp(classname, "weapon_chainsaw", 15) == 0 ) type = 19;
if( type )
{
entity = EntIndexToEntRef(entity);
for( int i = 0; i < MAX_SPAWNS; i++ )
{
if( g_iSpawns[i][0] == entity )
{
g_iSpawns[i][2]--;
if( g_iSpawns[i][2] > 0 )
{
float vAng[3], vPos[3];
GetEntPropVector(entity, Prop_Data, "m_vecOrigin", vPos);
GetEntPropVector(entity, Prop_Data, "m_angRotation", vAng);
int count = g_iSpawnCount; // Lazy hack
g_iSpawnCount = 0;
CreateSpawn(vPos, vAng, g_iSpawns[i][1], type, false, g_iSpawns[i][2]);
g_iSpawnCount = count;
}
break;
}
}
}
}
}
void Event_RoundEnd(Event event, const char[] name, bool dontBroadcast)
{
ResetPlugin(false);
}
void Event_RoundStart(Event event, const char[] name, bool dontBroadcast)
{
if( g_iPlayerSpawn == 1 && g_iRoundStart == 0 )
CreateTimer(1.0, TimerStart, _, TIMER_FLAG_NO_MAPCHANGE);
g_iRoundStart = 1;
}
void Event_PlayerSpawn(Event event, const char[] name, bool dontBroadcast)
{
if( g_iPlayerSpawn == 0 && g_iRoundStart == 1 )
CreateTimer(1.0, TimerStart, _, TIMER_FLAG_NO_MAPCHANGE);
g_iPlayerSpawn = 1;
}
Action TimerStart(Handle timer)
{
ResetPlugin();
LoadSpawns();
return Plugin_Continue;
}
// ====================================================================================================
// LOAD SPAWNS
// ====================================================================================================
void LoadSpawns()
{
if( g_bLoaded || g_iCvarRandom == 0 ) return;
g_bLoaded = true;
char sPath[PLATFORM_MAX_PATH];
BuildPath(Path_SM, sPath, sizeof(sPath), CONFIG_SPAWNS);
if( !FileExists(sPath) )
return;
// Load config
KeyValues hFile = new KeyValues("spawns");
if( !hFile.ImportFromFile(sPath) )
{
delete hFile;
return;
}
// Check for current map in the config
char sMap[64];
GetCurrentMap(sMap, sizeof(sMap));
if( !hFile.JumpToKey(sMap) )
{
delete hFile;
return;
}
// Retrieve how many weapons to display
int iCount = hFile.GetNum("num", 0);
if( iCount == 0 )
{
delete hFile;
return;
}
// Spawn only a select few weapons?
int iIndexes[MAX_SPAWNS+1];
if( iCount > MAX_SPAWNS )
iCount = MAX_SPAWNS;
// Spawn saved weapons or create random
int iRandom = g_iCvarRandom;
if( iRandom == -1 || iRandom > iCount)
iRandom = iCount;
if( iRandom != -1 )
{
for( int i = 1; i <= iCount; i++ )
iIndexes[i-1] = i;
SortIntegers(iIndexes, iCount, Sort_Random);
iCount = iRandom;
}
// Get the weapon origins and spawn
char sTemp[4];
float vPos[3], vAng[3];
int index, iMod;
for( int i = 1; i <= iCount; i++ )
{
if( iRandom != -1 ) index = iIndexes[i-1];
else index = i;
IntToString(index, sTemp, sizeof(sTemp));
if( hFile.JumpToKey(sTemp) )
{
hFile.GetVector("ang", vAng);
hFile.GetVector("pos", vPos);
iMod = hFile.GetNum("mod");
if( vPos[0] == 0.0 && vPos[1] == 0.0 && vPos[2] == 0.0 ) // Should never happen...
LogError("Error: 0,0,0 origin. Iteration=%d. Index=%d. Random=%d. Count=%d.", i, index, iRandom, iCount);
else
CreateSpawn(vPos, vAng, index, iMod, true);
hFile.GoBack();
} else {
LogError("Error: missing index detected: \"%d\" from \"%s\" in \"%s\"", index, sMap, CONFIG_SPAWNS);
}
}
delete hFile;
}
// ====================================================================================================
// CREATE SPAWN
// ====================================================================================================
void CreateSpawn(const float vOrigin[3], const float vAngles[3], int index = 0, int model = 0, int autospawn = false, int respawn_count = -1)
{
if( g_iSpawnCount >= MAX_SPAWNS )
return;
int iSpawnIndex = -1;
for( int i = 0; i < MAX_SPAWNS; i++ )
{
if( !IsValidEntRef(g_iSpawns[i][0]) )
{
iSpawnIndex = i;
break;
}
}
if( iSpawnIndex == -1 )
return;
if( autospawn && g_iCvarRandomise && GetRandomInt(0, 100) <= g_iCvarRandomise )
{
if( g_bLeft4Dead2 )
{
model = GetRandomInt(0, MAX_WEAPONS2-1);
// if( model == 15 || model == 18 ) model = GetRandomInt(0, 14);
// else if( model == 19 || model == 21 ) model = GetRandomInt(22, 28);
} else {
model = GetRandomInt(0, MAX_WEAPONS-1);
}
}
char classname[64];
strcopy(classname, sizeof(classname), g_bLeft4Dead2 ? g_sWeapons2[model] : g_sWeapons[model]);
int iCount = g_iCvarCount;
if( iCount != 1 )
{
StrCat(classname, sizeof(classname), "_spawn");
}
int entity_weapon = -1;
entity_weapon = CreateEntityByName(classname);
if( entity_weapon == -1 )
ThrowError("Failed to create entity '%s'", classname);
DispatchKeyValue(entity_weapon, "solid", "6");
DispatchKeyValue(entity_weapon, "model", g_bLeft4Dead2 ? g_sWeaponModels2[model] : g_sWeaponModels[model]);
DispatchKeyValue(entity_weapon, "rendermode", "3");
DispatchKeyValue(entity_weapon, "disableshadows", "1");
if( iCount <= 0 ) // Infinite
{
DispatchKeyValue(entity_weapon, "spawnflags", "8");
DispatchKeyValue(entity_weapon, "count", "9999");
}
else if( iCount != 1 )
{
char sCount[5];
IntToString(iCount, sCount, sizeof(sCount));
DispatchKeyValue(entity_weapon, "count", sCount);
}
float vAng[3], vPos[3];
vPos = vOrigin;
vAng = vAngles;
if( model == (g_bLeft4Dead2 ? 25 : 8) ) // First aid
{
vAng[0] += 90.0;
vPos[2] += 1.0;
}
else if( g_bLeft4Dead2 && model == 24 ) // Adrenaline
{
vAng[1] -= 90.0;
vAng[2] -= 90.0;
vPos[2] += 1.0;
}
else if( g_bLeft4Dead2 && (model == 26 || model == 27 || model == 28 )) // Defib + Upgrades
{
vAng[1] -= 90.0;
vAng[2] += 90.0;
}
else if( g_bLeft4Dead2 && model == 19 ) // Chainsaw
{
vPos[2] += 3.0;
}
TeleportEntity(entity_weapon, vPos, vAng, NULL_VECTOR);
DispatchSpawn(entity_weapon);
if( iCount == 1 )
{
int ammo;
if( g_bLeft4Dead2 )
{
switch( model )
{
case 10, 11, 12: ammo = g_iAmmoSmg;
case 2, 3, 4, 5: ammo = g_iAmmoRifle;
case 8, 9: ammo = g_iAmmoShotgun;
case 6, 7: ammo = g_iAmmoAutoShot;
case 19: ammo = g_iAmmoChainsaw;
case 17: ammo = g_iAmmoM60;
case 18: ammo = g_iAmmoGL;
case 13, 14, 15, 16: ammo = g_iAmmoSniper;
}
} else {
switch( model )
{
case 0: ammo = g_iAmmoRifle;
case 1: ammo = g_iAmmoAutoShot;
case 2: ammo = g_iAmmoHunting;
case 3: ammo = g_iAmmoSmg;
case 4: ammo = g_iAmmoShotgun;
}
}
if( !g_bLeft4Dead2 && model == 1 ) ammo = g_iAmmoShotgun;
SetEntProp(entity_weapon, Prop_Send, "m_iExtraPrimaryAmmo", ammo, 4);
}
SetEntityMoveType(entity_weapon, MOVETYPE_NONE);
// Save M60, Grenade Launcher and Chainsaw spawn counts
if( g_bLeft4Dead2 && iCount != 1 && (model == 17 || model == 18 || model == 19) )
{
g_iSpawns[iSpawnIndex][2] = respawn_count != -1 ? respawn_count : iCount;
}
g_iSpawns[iSpawnIndex][0] = EntIndexToEntRef(entity_weapon);
g_iSpawns[iSpawnIndex][1] = index;
g_iSpawnCount++;
}
// ====================================================================================================
// COMMANDS
// ====================================================================================================
// sm_weapon_spawn
// ====================================================================================================
int ListMenuHandler(Menu menu, MenuAction action, int client, int index)
{
if( action == MenuAction_Select )
{
if( g_iSave[client] == 0 )
{
CmdSpawnerTempMenu(client, index);
} else {
CmdSpawnerSaveMenu(client, index);
}
g_hMenuList.DisplayAt(client, g_hMenuList.Selection, MENU_TIME_FOREVER);
}
return 0;
}
Action CmdSpawnerTemp(int client, int args)
{
if( !client )
{
ReplyToCommand(client, "[Weapon Spawn] Command can only be used %s", IsDedicatedServer() ? "in game on a dedicated server." : "in chat on a Listen server.");
return Plugin_Handled;
}
else if( g_iSpawnCount >= MAX_SPAWNS )
{
PrintToChat(client, "%sError: Cannot add anymore weapons. Used: (\x05%d/%d\x01).", CHAT_TAG, g_iSpawnCount, MAX_SPAWNS);
return Plugin_Handled;
}
g_iSave[client] = 0;