-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
nmrih-backpack2.sp
2105 lines (1694 loc) · 47 KB
/
nmrih-backpack2.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
#include <sdktools>
#include <sdkhooks>
#include <clientprefs>
#include <autoexecconfig>
#include <vscript_proxy>
#pragma semicolon 1
#pragma newdecls required
// FIXME: Item trace hulls are not clipped
// FIXME: Zombies can drop weapons at 0 0 0 when they receive a backpack
// TODO: Change backpack glow color when obj items are added to them
// TODO: Re-add speed penalty
#define PLUGIN_PREFIX "[Backpack2] "
#define INVALID_USER_ID 0
#define TEMPLATE_RANDOM -1
#define WEAPON_NOT_CARRIED 0
#define MAXENTITIES 2048
#define EF_ITEM_BLINK 0x100
#define MAX_BP_USE_DISTANCE 90.0
#define MAX_TARGETNAME 50
#define MAXPLAYERS_NMRIH 9
#define PROP_PHYS_DEBRIS 4
#define PROP_PHYS_USE_OUTPUT 256
#define GLOWTYPE_GLOW 1
#define GLOWTYPE_BLINK 2
#define MASK_ITEM 0
#define INVALID_ITEM 0
#define INVALID_AMMO -1
enum
{
COL_LEFT,
COL_MIDDLE,
COL_RIGHT,
COL_MAX
}
enum
{
CAT_WEAPON,
CAT_AMMO
}
public Plugin myinfo = {
name = "[NMRiH] Backpack2",
author = "Dysphie & Ryan",
description = "Portable inventory boxes",
version = "2.0.8",
url = "github.com/dysphie/nmrih-backpack2"
};
bool optimize;
bool fixUpBoards;
ConVar cvAmmoWeight;
int ammoWeight;
ConVar cvItemGlow;
ConVar cvOptimize;
ConVar cvHints;
ConVar cvGlowDist;
ConVar cvBlink;
ConVar cvNpcBackpackChance;
ConVar cvBackpackCount;
ConVar cvAmmoMultiplier;
ConVar cvBackpackColorize;
ConVar cvRightLootMin;
ConVar cvRightLootMax;
ConVar cvAmmoLootMinPct;
ConVar cvAmmoLootMaxPct;
ConVar cvMiddleLootMin;
ConVar cvMiddleLootMax;
ConVar cvLeftLootMin;
ConVar cvLeftLootMax;
ConVar inv_maxcarry = null;
Cookie hintCookie;
ArrayList templates = null;
ArrayList backpacks = null;
StringMap itemLookup = null;
ArrayList itemRegistry = null;
bool wasLookingAtBackpack[MAXPLAYERS_NMRIH+1] = {false, ...};
float nextHintTime[MAXPLAYERS_NMRIH+1] = {-1.0, ...};
bool wearingBackpack[MAXENTITIES+1] = {false, ...};
bool isDroppedBackpack[MAXENTITIES+1] = {false, ...};
float stopThinkTime[MAXENTITIES+1] = {-1.0, ...};
bool used[MAXENTITIES+1] = { false, ...};
int MaxEntities = 0;
int numDroppedBackpacks = 0;
enum struct BackpackSound
{
char path[PLATFORM_MAX_PATH];
int layers;
}
enum struct Item
{
char alias[64];
int id;
int category;
int columns; // Columns this item can go in
bool spawnAsLoot;
int ammoID;
int capacity;
}
enum
{
SoundAdd,
SoundDrop,
SoundWear,
SoundOpen,
SoundMAX
}
enum struct BackpackTemplate
{
int itemLimit[COL_MAX];
char attachMdl[PLATFORM_MAX_PATH];
char droppedMdl[PLATFORM_MAX_PATH];
ArrayList sounds[SoundMAX];
int layers[SoundMAX];
void Init()
{
for (int i; i < SoundMAX; i++) {
this.sounds[i] = new ArrayList(sizeof(BackpackSound));
}
}
void Delete()
{
for (int i; i < SoundMAX; i++) {
delete this.sounds[i];
}
}
}
enum struct StoredItem
{
int id;
int ammoCount;
char name[MAX_TARGETNAME];
}
enum struct Backpack
{
int templateID; // Template index, used for sounds, colors, models, etc.
int propRef; // Entity reference to the backpack's object in the world
int wearerRef; // Entity reference to the backpack wearer
ArrayList items[COL_MAX];
int atInterface[MAXPLAYERS_NMRIH+1]; // True if player index is likely browsing the backpack
// Must verify with in-game check
int color[3]; // Unique color to distinguish from other backpacks (tint and glow)
void Init(int templateID)
{
StoredItem blank;
if (templateID == TEMPLATE_RANDOM)
{
this.templateID = GetRandomInt(0, templates.Length - 1);
} else {
this.templateID = templateID;
}
BackpackTemplate template;
templates.GetArray(this.templateID, template);
for (int i = 0; i < COL_MAX; i++)
{
this.items[i] = new ArrayList(sizeof(StoredItem));
for (int j; j < template.itemLimit[i]; j++)
{
this.items[i].PushArray(blank);
}
}
this.propRef = INVALID_ENT_REFERENCE;
this.wearerRef = INVALID_ENT_REFERENCE;
GetRandomRGB(this.color);
}
void ColorizeProp(int prop)
{
if (cvBackpackColorize.BoolValue)
{
SetEntityRenderMode(prop, RENDER_TRANSCOLOR);
SetEntityRenderColor(prop, this.color[0], this.color[1], this.color[2], 255);
}
}
void ShuffleContents()
{
for (int i = 0; i < COL_MAX; i++) {
this.items[i].Sort(Sort_Random, Sort_Integer);
}
}
void HighlightEntity(int entity)
{
if (cvItemGlow.BoolValue && cvGlowDist.IntValue != -1)
{
DispatchKeyValue(entity, "glowable", "1");
DispatchKeyValue(entity, "glowblip", "0");
DispatchKeyValueFloat(entity, "glowdistance", cvGlowDist.FloatValue);
DispatchKeyValue(entity, "glowcolor", "0 255 0"); // same as item pickup
RequestFrame(Frame_GlowEntity, EntIndexToEntRef(entity));
}
if (cvBlink.BoolValue) {
AddEntityEffects(entity, EF_ITEM_BLINK);
}
}
bool Attach(int wearer, bool suppressSound = false)
{
if (this.wearerRef != INVALID_ENT_REFERENCE) {
return false;
}
this.EndUseForAll();
int attached = CreateEntityByName("prop_dynamic_ornament");
if (attached == -1) {
return false;
}
BackpackTemplate template;
templates.GetArray(this.templateID, template);
DispatchKeyValue(attached, "model", template.attachMdl);
DispatchKeyValue(attached, "disableshadows", "1");
if (!DispatchSpawn(attached))
{
RemoveEntity(attached);
return false;
}
// Remove physics prop
int dropped = EntRefToEntIndex(this.propRef);
if (dropped != -1)
{
isDroppedBackpack[dropped] = false;
RemoveEntity(dropped);
numDroppedBackpacks--;
}
SetVariantString("!activator");
AcceptEntityInput(attached, "SetAttached", wearer, wearer);
this.ColorizeProp(attached);
this.wearerRef = EntIndexToEntRef(wearer);
this.propRef = EntIndexToEntRef(attached);
wearingBackpack[wearer] = true;
if (!suppressSound) {
this.PlaySound(SoundWear);
}
return true;
}
bool Drop()
{
int wearer = EntRefToEntIndex(this.wearerRef);
if (wearer == -1) {
return true;
}
int dropped = CreateEntityByName("prop_physics_override");
if (dropped == -1) {
return false;
}
BackpackTemplate template;
templates.GetArray(this.templateID, template);
DispatchKeyValue(dropped, "model", template.droppedMdl);
DispatchKeyValue(dropped, "spawnflags", "260"); // Debris (4) + Generate output on +use (256)
if (!DispatchSpawn(dropped) || !DropFromEntity(wearer, dropped))
{
RemoveEntity(dropped);
return false;
}
// Drop was successful past this point
numDroppedBackpacks++;
this.ColorizeProp(dropped);
this.HighlightEntity(dropped);
SDKHook(dropped, SDKHook_Use, OnBackpackPropUse);
SDKHook(dropped, SDKHook_OnTakeDamage, OnBackpackPropDamage);
if (IsValidEntity(this.propRef)) {
RemoveEntity(this.propRef);
}
this.propRef = EntIndexToEntRef(dropped);
isDroppedBackpack[dropped] = true;
wearingBackpack[wearer] = false;
this.wearerRef = INVALID_ENT_REFERENCE;
this.PlaySound(SoundDrop);
if (IsValidPlayer(wearer) && ClientWantsHints(wearer))
{
SendBackpackHint(wearer, "");
nextHintTime[wearer] = GetTickedTime() + 2.0;
}
return true;
}
void PlaySound(int soundType)
{
BackpackTemplate template;
templates.GetArray(this.templateID, template);
int rnd = GetRandomInt(0, template.sounds[soundType].Length - 1);
BackpackSound snd;
template.sounds[soundType].GetArray(rnd, snd);
for (int i; i < snd.layers; i++) {
EmitSoundToAll(snd.path, this.propRef);
}
}
bool Use(int client)
{
if (IsValidEntity(this.wearerRef)) {
return false;
}
int dropped = EntRefToEntIndex(this.propRef);
if (dropped == -1 || !CanReachBackpack(client, dropped)) {
return false;
}
FreezePlayer(client);
this.PlaySound(SoundOpen);
if (ClientWantsHints(client))
{
SendBackpackHint(client, "");
nextHintTime[client] = GetTickedTime() + 999999.9;
}
Handle msg = StartMessageOne("ItemBoxOpen", client, USERMSG_RELIABLE|USERMSG_BLOCKHOOKS);
BfWrite bf = UserMessageToBfWrite(msg);
bf.WriteShort(dropped);
// Describe every cell in the inventory box
int expected[] = { 8, 4, 8 };
for (int k = 0; k < COL_MAX; k++)
{
int i = 0;
int max = this.items[k].Length;
for (; i < max; i++)
{
int id = this.items[k].Get(i, StoredItem::id);
// HACKHACK: Replace boards with the barricade hammer in the user msg
// as clients are unable to render board entries in 1.12.1
// https://github.com/nmrih/source-game/issues/1256
if (fixUpBoards && id == 61) {
id = 23;
}
bf.WriteShort(id);
}
// Our columns might be smaller than the game expects due
// configurable backpack limits, make sure to fill the rest
for (; i < expected[k]; i++) {
bf.WriteShort(INVALID_ITEM);
}
}
EndMessage();
this.atInterface[client] = true;
return true;
}
void EndUseForAll()
{
for (int i = 1; i <= MaxClients; i++) {
if (this.atInterface[i] && IsClientInGame(i)) {
this.EndUse(i);
}
}
}
void EndUse(int client)
{
// HACK: Re enable hints
nextHintTime[client] = GetTickedTime();
wasLookingAtBackpack[client] = false;
UnfreezePlayer(client);
this.atInterface[client] = false;
UserMsg_EndUse(client);
}
bool AddWeapon(int ammoCount, Item reg, bool suppressSound = false, const char[] name = "")
{
for (int i; i < COL_MAX; i++)
{
if (reg.columns & (1 << i)
&& this.AddWeaponToColumn(i, ammoCount, reg, suppressSound, name))
{
return true;
}
}
return false;
}
bool AddWeaponToColumn(int col, int ammoCount, Item reg, bool suppressSound = false, const char[] name = "")
{
ArrayList arr = this.items[col];
int maxItems = arr.Length;
for (int i = 0; i < maxItems; i++)
{
StoredItem stored;
arr.GetArray(i, stored);
if (stored.id == INVALID_ITEM)
{
stored.id = reg.id;
stored.ammoCount = ammoCount;
if (name[0]) {
strcopy(stored.name, sizeof(stored.name), name);
}
if (!suppressSound) {
this.PlaySound(SoundAdd);
}
arr.SetArray(i, stored);
return true;
}
}
return false;
}
int AddAmmo(int ammoCount, Item reg, bool suppressSound = false, bool allowStacking = true)
{
int leftover = ammoCount;
int i = 0;
while (leftover > 0 && i < COL_MAX)
{
if (reg.columns & (1 << i))
{
leftover = this.AddAmmoToColumn(i, leftover, reg, suppressSound);
}
i++;
}
return leftover;
}
int AddAmmoToColumn(int col, int ammoCount, Item reg, bool suppressSound = false, bool allowStacking = true)
{
ArrayList arr = this.items[col];
int fullCapacity = RoundToNearest(reg.capacity * cvAmmoMultiplier.FloatValue);
if (fullCapacity <= 0) {
fullCapacity = cellmax;
}
int leftover = this.AddAmmoRecursively(arr, reg.id, ammoCount, fullCapacity, allowStacking);
if (!suppressSound && leftover < ammoCount) {
this.PlaySound(SoundAdd);
}
return leftover;
}
void AddRandomLoot(int count, int column)
{
// Get candidate weapons
ArrayList loot = new ArrayList(sizeof(Item));
GetLootForColumn(column, loot);
int numLoot = loot.Length;
if (numLoot <= 0)
{
delete loot;
return;
}
// For each available slot, get a random weapon and ammo count
for (int i; i < count; i++)
{
int rnd = GetRandomInt(0, numLoot - 1);
Item reg;
loot.GetArray(rnd, reg);
if (reg.category == CAT_WEAPON)
{
int weapon = CreateEntityByName(reg.alias);
DispatchSpawn(weapon);
int rndClip = 1;
int maxClip = GetMaxClip1(weapon);
if (maxClip != -1)
{
int min = RoundToNearest(maxClip * cvAmmoLootMinPct.FloatValue / 100);
int max = RoundToNearest(maxClip * cvAmmoLootMaxPct.FloatValue / 100);
rndClip = GetRandomInt(min, max);
}
if (this.AddWeaponToColumn(column, rndClip, reg, true))
{
RemoveEntity(weapon);
}
}
else if (reg.category == CAT_AMMO)
{
int minAmmo = reg.capacity * cvAmmoLootMinPct.IntValue / 100;
if (minAmmo < 1) {
minAmmo = 1;
}
int maxAmmo = reg.capacity * cvAmmoLootMaxPct.IntValue / 100;
int rndClip = GetRandomInt(minAmmo, maxAmmo);
this.AddAmmoToColumn(column, rndClip, reg, true, false);
}
}
delete loot;
}
int AddAmmoRecursively(ArrayList arr, int itemID, int curAmmo, int maxAmmo, bool allowStacking = true)
{
int bestSlot = -1;
int bestIntake = -1;
for (int i = 0; i < arr.Length; i++)
{
StoredItem stored;
arr.GetArray(i, stored);
if (stored.id == itemID && allowStacking)
{
// We choose this slot if our best slot is undefined or empty
// or it can hold more ammo than the best slot
if (bestSlot == -1 || arr.Get(bestSlot, StoredItem::id) == INVALID_ITEM)
{
int intake = maxAmmo - stored.ammoCount;
if (intake && intake > bestIntake)
{
bestSlot = i;
bestIntake = intake;
}
}
}
else if (stored.id == INVALID_ITEM && bestSlot == -1)
{
// We choose this slot if our best slot is undefined
// else we prioritize partially full slots
bestSlot = i;
bestIntake = maxAmmo;
}
}
if (bestSlot != -1)
{
if (bestIntake > curAmmo)
bestIntake = curAmmo;
StoredItem addTo;
arr.GetArray(bestSlot, addTo);
curAmmo -= bestIntake;
addTo.ammoCount += bestIntake;
addTo.id = itemID;
arr.SetArray(bestSlot, addTo);
// Don't take more ammo than we have
if (curAmmo < 0)
{
curAmmo = 0;
}
else if (curAmmo > 0)
{
this.AddAmmoRecursively(arr, itemID, curAmmo, maxAmmo);
}
}
// No more slots, return remainder
return curAmmo;
}
void TakeItemUpdate(int index, int category)
{
for (int i = 1; i <= MaxClients; i++)
{
if (this.atInterface[i] && IsClientInGame(i))
{
Handle msg = StartMessageOne("ItemBoxItemTaken", i, USERMSG_BLOCKHOOKS);
BfWrite bf = UserMessageToBfWrite(msg);
bf.WriteShort(index);
bf.WriteShort(category);
bf.WriteShort(EntRefToEntIndex(this.propRef));
EndMessage();
}
}
}
bool TakeItem(int index, int column, int client)
{
if (!CanReachBackpack(client, this.propRef))
{
this.EndUse(client);
return false;
}
StoredItem stored;
this.items[column].GetArray(index, stored);
if (stored.id == INVALID_ITEM)
{
this.EndUse(client);
return false;
}
Item reg;
if (!GetItemByID(stored.id, reg)) {
return false;
}
if (reg.category == CAT_WEAPON)
{
if (ClientOwnsWeapon(client, reg.alias))
{
PrintCenterText(client, "%t", "Already Own This Type");
return false;
}
int weapon = CreateEntityByName(reg.alias);
if (weapon == -1) {
return false;
}
DispatchSpawn(weapon);
int leftoverWeight = inv_maxcarry.IntValue - GetCarriedWeight(client);
if (leftoverWeight < GetWeaponWeight(weapon))
{
RemoveEntity(weapon);
PrintCenterText(client, "%t", "No Inventory Space");
return false;
}
SetEntProp(weapon, Prop_Send, "m_iClip1", stored.ammoCount);
SetEntityTargetname(weapon, stored.name);
float pos[3];
GetClientEyePosition(client, pos);
TeleportEntity(weapon, pos, NULL_VECTOR, NULL_VECTOR);
if (0 < client <= MaxClients && IsClientInGame(client))
{
AcceptEntityInput(weapon, "Use", client, client);
}
stored.id = INVALID_ITEM;
stored.ammoCount = 0;
this.items[column].SetArray(index, stored);
this.TakeItemUpdate(index, column);
}
else if (reg.category == CAT_AMMO)
{
if (reg.ammoID == INVALID_AMMO) {
return false;
}
int leftoverWeight = inv_maxcarry.IntValue - GetCarriedWeight(client);
if (leftoverWeight <= 0)
{
PrintCenterText(client, "%t", "No Inventory Space");
return false;
}
int canTake;
if (ammoWeight <= 0)
{
canTake = stored.ammoCount;
}
else
{
canTake = leftoverWeight / ammoWeight;
if (canTake > stored.ammoCount)
canTake = stored.ammoCount;
}
int curAmmo = GetEntProp(client, Prop_Send, "m_iAmmo", _, reg.ammoID);
// Give all but last ammo directly, this bypasses ammo capacity limits
SetEntProp(client, Prop_Send, "m_iAmmo", curAmmo + canTake - 1, _, reg.ammoID);
// Now give the last piece of ammo, which will trigger a pickup
GivePlayerAmmo(client, 1, reg.ammoID);
stored.ammoCount -= canTake;
if (stored.ammoCount <= 0)
{
stored.id = INVALID_ITEM;
this.TakeItemUpdate(index, column);
}
this.items[column].SetArray(index, stored);
}
return true;
}
bool IsWorn()
{
return IsValidEntity(this.wearerRef);
}
void Delete()
{
this.EndUseForAll();
int wearer = EntRefToEntIndex(this.wearerRef);
if (wearer == -1) {
numDroppedBackpacks--;
} else {
wearingBackpack[wearer] = false;
}
int dropped = EntRefToEntIndex(this.propRef);
if (dropped != -1)
{
isDroppedBackpack[dropped] = false;
RemoveEntity(dropped);
}
for (int i = 0; i < COL_MAX; i++) {
delete this.items[i];
}
}
}
void UserMsg_EndUse(int client)
{
StartMessageOne("ItemBoxClose", client, USERMSG_RELIABLE|USERMSG_BLOCKHOOKS);
EndMessage();
}
bool IsBackpackableNPC(const char[] classname)
{
return (StrEqual(classname, "npc_nmrih_shamblerzombie") ||
StrEqual(classname, "npc_nmrih_runnerzombie"));
}
void GetRandomRGB(int rgb[3])
{
rgb[0] = GetRandomInt(0, 255);
rgb[1] = GetRandomInt(0, 255);
rgb[2] = GetRandomInt(0, 255);
}
public void OnClientPutInServer(int client)
{
SDKHook(client, SDKHook_WeaponDropPost, OnWeaponDropped);
SDKHook(client, SDKHook_WeaponSwitch, OnWeaponSwitch);
}
public void OnPluginStart()
{
LoadTranslations("backpack2.phrases");
LoadTranslations("common.phrases");
ConVar cvGameVersion = FindConVar("nmrih_version");
if (cvGameVersion)
{
char gameVersion[11];
cvGameVersion.GetString(gameVersion, sizeof(gameVersion));
if (StrEqual(gameVersion, "1.12.0") || StrEqual(gameVersion, "1.12.1"))
{
fixUpBoards = true;
}
}
hintCookie = new Cookie("backpack2_hints", "Toggles Backpack2 screen hints", CookieAccess_Protected);
HookEvent("player_spawn", Event_PlayerSpawn);
HookEvent("player_death", OnPlayerDeath);
HookEvent("player_extracted", OnPlayerExtracted);
HookEvent("nmrih_reset_map", Event_MapReset);
HookEvent("game_restarting", OnGameRestarting, EventHookMode_PostNoCopy);
HookEvent("npc_killed", OnNPCKilled);
RegConsoleCmd("dropbackpack", Cmd_DropBackpack);
MaxEntities = GetMaxEntities();
if (MaxEntities > MAXENTITIES)
{
SetFailState("Entity limit greater than expected. " ...
"Change '#define MAXENTITIES %d' to '#define MAXENTITIES %d' and recompile the plugin",
MAXENTITIES, MaxEntities);
}
cvItemGlow = FindConVar("sv_item_glow");
// Add support for future 1.12.2 custom ammo weights
// If we are still on 1.12.1 or lower, just use the default 5
cvAmmoWeight = FindConVar("inv_ammoweight");
if (cvAmmoWeight)
{
ammoWeight = cvAmmoWeight.IntValue;
cvAmmoWeight.AddChangeHook(OnAmmoWeightCvarChange);
}
else
{
ammoWeight = 5;
}
AutoExecConfig_SetFile("plugin.backpack2");
cvRightLootMin = AutoExecConfig_CreateConVar("sm_backpack_loot_ammo_min", "0",
"Minimum ammo boxes to place in backpacks spawned as loot", _, true, 0.0, true, 8.0);
cvRightLootMax = AutoExecConfig_CreateConVar("sm_backpack_loot_ammo_max", "4",
"Maximum ammo boxes to place in backpacks spawned as loot", _, true, 0.0, true, 8.0);
cvAmmoLootMinPct = AutoExecConfig_CreateConVar("sm_backpack_loot_ammo_min_pct", "40",
"Minimum fill percentage for ammo boxes spawned as backpack loot", _, true, 1.0);
cvAmmoLootMaxPct = AutoExecConfig_CreateConVar("sm_backpack_loot_ammo_max_pct", "100",
"Maximum fill percentage for ammo boxes spawned as backpack loot", _, true, 1.0);
cvMiddleLootMin = AutoExecConfig_CreateConVar("sm_backpack_loot_gear_min", "0",
"Minimum gear items to place in backpacks spawned as loot", _, true, 0.0, true, 4.0);
cvMiddleLootMax = AutoExecConfig_CreateConVar("sm_backpack_loot_gear_max", "1",
"Maximum gear items to place in backpacks spawned as loot", _, true, 0.0, true, 4.0);
cvLeftLootMin = AutoExecConfig_CreateConVar("sm_backpack_loot_weapon_min", "0",
"Minimum weapons to place in backpacks spawned as loot", _, true, 0.0, true, 8.0);
cvLeftLootMax = AutoExecConfig_CreateConVar("sm_backpack_loot_weapon_max", "2",
"Maximum weapons to place in backpacks spawned as loot", _, true, 0.0, true, 8.0);
cvHints = AutoExecConfig_CreateConVar("sm_backpack_show_hints", "1",
"Whether to show screen hints on how to use backpacks");
cvAmmoMultiplier = AutoExecConfig_CreateConVar("sm_backpack_ammo_stack_limit", "4",
"Number of ammo pickups that can be stored per ammo slot. 0 means infinite.");
cvBackpackCount = AutoExecConfig_CreateConVar("sm_backpack_count", "1",
"Number of backpacks to create at round start. Won't create more backpacks than there are players.");
cvBackpackColorize = AutoExecConfig_CreateConVar("sm_backpack_colorize", "1",
"Randomly colorize backpacks to help distinguish them.");
cvGlowDist = AutoExecConfig_CreateConVar("sm_backpack_glow_distance", "90", "Glow backbacks in this range of the player");
cvBlink = AutoExecConfig_CreateConVar("sm_backpack_blink", "1", "Whether dropped backpacks pulse their brightness");
cvNpcBackpackChance = AutoExecConfig_CreateConVar("sm_backpack_zombie_spawn_chance", "0.005",
"Chance for a zombie to spawn with a backpack. Set to zero or negative to disable");
cvOptimize = AutoExecConfig_CreateConVar("sm_backpack_enable_optimizations", "1",
"Don't trace dropped items if perceived backpack count is zero. Disable for debugging only");
cvOptimize.AddChangeHook(CvarChangeOptimize);
AutoExecConfig_ExecuteFile();
inv_maxcarry = FindConVar("inv_maxcarry");
backpacks = new ArrayList(sizeof(Backpack));
templates = new ArrayList(sizeof(BackpackTemplate));
itemRegistry = new ArrayList(sizeof(Item));
itemLookup = new StringMap();
ParseConfig();
AddCommandListener(Cmd_TakeItems, "takeitems");
AddCommandListener(Cmd_CloseBox, "closeitembox");
RegAdminCmd("sm_bp", Cmd_Backpack, ADMFLAG_CHEATS);
RegAdminCmd("sm_backpack", Cmd_Backpack, ADMFLAG_CHEATS);
hintCookie.SetPrefabMenu(CookieMenu_OnOff_Int, "Backpack2 Hints");
for (int i = 1; i <= MaxClients; i++)
if (IsClientInGame(i))
OnClientPutInServer(i);
}
void OnAmmoWeightCvarChange(ConVar convar, const char[] oldValue, const char[] newValue)
{
ammoWeight = StringToInt(newValue);
}
void CvarChangeOptimize(ConVar convar, const char[] oldValue, const char[] newValue)
{
optimize = newValue[0] != '0';
}
public Action Cmd_DropBackpack(int client, int args)
{
if (IsValidPlayer(client)) {
ClientDropBackpack(client);
}
return Plugin_Handled;
}
void Frame_GlowEntity(int entRef)
{
int entity = EntRefToEntIndex(entRef);
if (entity != -1) {
AcceptEntityInput(entity, "enableglow");
}
}
public void OnMapEnd()
{
DeleteAllBackpacks();
}
public void OnPluginEnd()
{
DeleteAllBackpacks();
}
public Action Cmd_Backpack(int client, int args)
{
if (args < 1)
{
ReplyToCommand(client, PLUGIN_PREFIX ... "Usage: sm_backpack <#userid|name>");
return Plugin_Handled;
}
char cmdTarget[512];
GetCmdArg(1, cmdTarget, sizeof(cmdTarget));
int target = FindTarget(client, cmdTarget);
if (!IsValidPlayer(target)) {
return Plugin_Handled;
}
if (wearingBackpack[target])
{
ReplyToCommand(client, PLUGIN_PREFIX ... "%t", "Give Backpack Already Owns", target);
return Plugin_Handled;
}
Backpack bp;
bp.Init(TEMPLATE_RANDOM);
bp.Attach(target);
backpacks.PushArray(bp);
ReplyToCommand(client, PLUGIN_PREFIX ... "%t", "Give Backpack Success", target);