-
-
Notifications
You must be signed in to change notification settings - Fork 203
/
weapons.cpp
2837 lines (2382 loc) · 69.6 KB
/
weapons.cpp
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 "precompiled.h"
short g_sModelIndexLaser; // holds the index for the laser beam
short g_sModelIndexLaserDot; // holds the index for the laser beam dot
short g_sModelIndexFireball; // holds the index for the fireball
short g_sModelIndexSmoke; // holds the index for the smoke cloud
short g_sModelIndexWExplosion; // holds the index for the underwater explosion
short g_sModelIndexBubbles; // holds the index for the bubbles model
short g_sModelIndexBloodDrop; // holds the sprite index for the initial blood
short g_sModelIndexBloodSpray; // holds the sprite index for splattered blood
short g_sModelIndexSmokePuff;
short g_sModelIndexFireball2;
short g_sModelIndexFireball3;
short g_sModelIndexFireball4;
short g_sModelIndexRadio;
short int g_sModelIndexCTGhost;
short int g_sModelIndexTGhost;
short int g_sModelIndexC4Glow;
int giAmmoIndex;
MULTIDAMAGE gMultiDamage;
// Pass in a name and this function will tell
// you the maximum amount of that type of ammunition that a player can carry.
int MaxAmmoCarry(const char *szName)
{
for (int i = 0; i < MAX_WEAPONS; i++)
{
ItemInfo *info = &CBasePlayerItem::m_ItemInfoArray[i];
if (info->pszAmmo1 && !Q_stricmp(szName, info->pszAmmo1))
{
return info->iMaxAmmo1;
}
if (info->pszAmmo2 && !Q_stricmp(szName, info->pszAmmo2))
{
return info->iMaxAmmo2;
}
}
ALERT(at_console, "MaxAmmoCarry() doesn't recognize '%s'!\n", szName);
return -1;
}
int MaxAmmoCarry(WeaponIdType weaponId)
{
return CBasePlayerItem::m_ItemInfoArray[weaponId].iMaxAmmo1;
}
// All automatic weapons are represented here
float GetBaseAccuracy(WeaponIdType id)
{
switch (id)
{
case WEAPON_M4A1:
case WEAPON_AK47:
case WEAPON_AUG:
case WEAPON_SG552:
case WEAPON_FAMAS:
case WEAPON_GALIL:
case WEAPON_M249:
case WEAPON_P90:
case WEAPON_TMP:
return 0.2f;
case WEAPON_MAC10:
return 0.15f;
case WEAPON_UMP45:
case WEAPON_MP5N:
return 0.0f;
}
return 0.0f;
}
LINK_HOOK_VOID_CHAIN2(ClearMultiDamage)
// Resets the global multi damage accumulator
void EXT_FUNC __API_HOOK(ClearMultiDamage)()
{
gMultiDamage.hEntity = nullptr;
gMultiDamage.amount = 0;
gMultiDamage.type = 0;
}
LINK_HOOK_VOID_CHAIN(ApplyMultiDamage, (entvars_t *pevInflictor, entvars_t *pevAttacker), pevInflictor, pevAttacker)
// Inflicts contents of global multi damage register on gMultiDamage.pEntity
void EXT_FUNC __API_HOOK(ApplyMultiDamage)(entvars_t *pevInflictor, entvars_t *pevAttacker)
{
EntityHandle<CBaseEntity> hEnt = gMultiDamage.hEntity;
if (!hEnt)
return;
hEnt->TakeDamage(pevInflictor, pevAttacker, gMultiDamage.amount, gMultiDamage.type);
// check again, the entity may be removed after taking damage
if (hEnt)
hEnt->ResetDmgPenetrationLevel();
}
LINK_HOOK_VOID_CHAIN(AddMultiDamage, (entvars_t *pevInflictor, CBaseEntity *pEntity, float flDamage, int bitsDamageType), pevInflictor, pEntity, flDamage, bitsDamageType)
void EXT_FUNC __API_HOOK(AddMultiDamage)(entvars_t *pevInflictor, CBaseEntity *pEntity, float flDamage, int bitsDamageType)
{
if (!pEntity)
return;
gMultiDamage.type |= bitsDamageType;
if (pEntity != gMultiDamage.hEntity)
{
#ifdef REGAMEDLL_FIXES
if (gMultiDamage.hEntity) // avoid api calls with null default pEntity
#endif
{
// UNDONE: wrong attacker!
ApplyMultiDamage(pevInflictor, pevInflictor);
}
gMultiDamage.hEntity = pEntity;
gMultiDamage.amount = 0;
}
gMultiDamage.amount += flDamage;
}
void SpawnBlood(Vector vecSpot, int bloodColor, float flDamage)
{
UTIL_BloodDrips(vecSpot, bloodColor, int(flDamage));
}
NOXREF int DamageDecal(CBaseEntity *pEntity, int bitsDamageType)
{
if (pEntity)
{
return pEntity->DamageDecal(bitsDamageType);
}
return RANDOM_LONG(DECAL_GUNSHOT4, DECAL_GUNSHOT5);
}
void DecalGunshot(TraceResult *pTrace, int iBulletType, bool ClientOnly, entvars_t *pShooter, bool bHitMetal)
{
;
}
// EjectBrass - tosses a brass shell from passed origin at passed velocity
void EjectBrass(const Vector &vecOrigin, const Vector &vecLeft, const Vector &vecVelocity, float rotation, int model, int soundtype, int entityIndex)
{
bool useNewBehavior = AreRunningCZero();
MESSAGE_BEGIN(MSG_PVS, gmsgBrass, vecOrigin);
if (!useNewBehavior)
{
// noxref
WRITE_BYTE(TE_MODEL);
}
WRITE_COORD(vecOrigin.x); // origin
WRITE_COORD(vecOrigin.y);
WRITE_COORD(vecOrigin.z);
if (!useNewBehavior)
{
// noxref
// it parses the client side, but does not use it
WRITE_COORD(vecLeft.x);
WRITE_COORD(vecLeft.y);
WRITE_COORD(vecLeft.z);
}
WRITE_COORD(vecVelocity.x); // velocity
WRITE_COORD(vecVelocity.y);
WRITE_COORD(vecVelocity.z);
WRITE_ANGLE(rotation);
WRITE_SHORT(model);
WRITE_BYTE(soundtype);
if (!useNewBehavior)
{
// noxref
WRITE_BYTE(25);// 2.5 seconds
}
WRITE_BYTE(entityIndex);
MESSAGE_END();
}
NOXREF void EjectBrass2(const Vector &vecOrigin, const Vector &vecVelocity, float rotation, int model, int soundtype, entvars_t *pev)
{
MESSAGE_BEGIN(MSG_ONE, SVC_TEMPENTITY, nullptr, pev);
WRITE_BYTE(TE_MODEL);
WRITE_COORD(vecOrigin.x);
WRITE_COORD(vecOrigin.y);
WRITE_COORD(vecOrigin.z);
WRITE_COORD(vecVelocity.x);
WRITE_COORD(vecVelocity.y);
WRITE_COORD(vecVelocity.z);
WRITE_ANGLE(rotation);
WRITE_SHORT(model);
WRITE_BYTE(0);
WRITE_BYTE(5);// 0.5 seconds
MESSAGE_END();
}
#ifdef REGAMEDLL_ADD
struct {
AmmoType type;
const char *name;
} ammoIndex[] =
{
{ AMMO_338MAGNUM, "338Magnum" },
{ AMMO_762NATO, "762Nato" },
{ AMMO_556NATOBOX, "556NatoBox" },
{ AMMO_556NATO, "556Nato" },
{ AMMO_BUCKSHOT, "buckshot" },
{ AMMO_45ACP, "45acp" },
{ AMMO_57MM, "57mm" },
{ AMMO_50AE, "50AE" },
{ AMMO_357SIG, "357SIG" },
{ AMMO_9MM, "9mm" },
{ AMMO_FLASHBANG, "Flashbang" },
{ AMMO_HEGRENADE, "HEGrenade" },
{ AMMO_SMOKEGRENADE, "SmokeGrenade" },
{ AMMO_C4, "C4" },
};
#endif
// Precaches the ammo and queues the ammo info for sending to clients
int AddAmmoNameToAmmoRegistry(const char *szAmmoname)
{
// string validation
if (!szAmmoname || !szAmmoname[0])
{
return -1;
}
// make sure it's not already in the registry
for (int i = 1; i < MAX_AMMO_SLOTS; i++)
{
if (!CBasePlayerItem::m_AmmoInfoArray[i].pszName)
continue;
if (!Q_stricmp(CBasePlayerItem::m_AmmoInfoArray[i].pszName, szAmmoname))
{
// ammo already in registry, just quite
return i;
}
}
giAmmoIndex++;
DbgAssert(giAmmoIndex < MAX_AMMO_SLOTS);
if (giAmmoIndex >= MAX_AMMO_SLOTS)
giAmmoIndex = 1;
#ifdef REGAMEDLL_ADD
for (auto& ammo : ammoIndex)
{
if (Q_stricmp(ammo.name, szAmmoname))
continue;
if (ammo.type != giAmmoIndex) {
CONSOLE_ECHO("Warning: ammo '%s' index mismatch; expected %i, real %i\n", szAmmoname, ammo.type, giAmmoIndex);
}
break;
}
#endif
CBasePlayerItem::m_AmmoInfoArray[giAmmoIndex].pszName = szAmmoname;
// Yes, this info is redundant
CBasePlayerItem::m_AmmoInfoArray[giAmmoIndex].iId = giAmmoIndex;
return giAmmoIndex;
}
// Precaches the weapon and queues the weapon info for sending to clients
void UTIL_PrecacheOtherWeapon(const char *szClassname)
{
edict_t *pEdict = CREATE_NAMED_ENTITY(MAKE_STRING(szClassname));
if (FNullEnt(pEdict))
{
ALERT(at_console, "NULL Ent in UTIL_PrecacheOtherWeapon classname `%s`\n", szClassname);
return;
}
CBasePlayerItem *pItem = GET_PRIVATE<CBasePlayerItem>(pEdict);
if (pItem)
{
ItemInfo info;
Q_memset(&info, 0, sizeof(info));
pItem->Precache();
if (pItem->GetItemInfo(&info))
{
CBasePlayerItem::m_ItemInfoArray[info.iId] = info;
AddAmmoNameToAmmoRegistry(info.pszAmmo1);
AddAmmoNameToAmmoRegistry(info.pszAmmo2);
}
}
REMOVE_ENTITY(pEdict);
}
// Called by worldspawn
void WeaponsPrecache()
{
Q_memset(CBasePlayerItem::m_ItemInfoArray, 0, sizeof(CBasePlayerItem::m_ItemInfoArray));
Q_memset(CBasePlayerItem::m_AmmoInfoArray, 0, sizeof(CBasePlayerItem::m_AmmoInfoArray));
giAmmoIndex = 0;
// custom items...
// common world objects
UTIL_PrecacheOther("item_suit");
UTIL_PrecacheOther("item_battery");
UTIL_PrecacheOther("item_antidote");
UTIL_PrecacheOther("item_security");
UTIL_PrecacheOther("item_longjump");
UTIL_PrecacheOther("item_kevlar");
UTIL_PrecacheOther("item_assaultsuit");
UTIL_PrecacheOther("item_thighpack");
// awp magnum
UTIL_PrecacheOtherWeapon("weapon_awp");
UTIL_PrecacheOther("ammo_338magnum");
UTIL_PrecacheOtherWeapon("weapon_g3sg1");
UTIL_PrecacheOtherWeapon("weapon_ak47");
UTIL_PrecacheOtherWeapon("weapon_scout");
UTIL_PrecacheOther("ammo_762nato");
// m249
UTIL_PrecacheOtherWeapon("weapon_m249");
UTIL_PrecacheOther("ammo_556natobox");
UTIL_PrecacheOtherWeapon("weapon_m4a1");
UTIL_PrecacheOtherWeapon("weapon_sg552");
UTIL_PrecacheOtherWeapon("weapon_aug");
UTIL_PrecacheOtherWeapon("weapon_sg550");
UTIL_PrecacheOther("ammo_556nato");
// shotgun
UTIL_PrecacheOtherWeapon("weapon_m3");
UTIL_PrecacheOtherWeapon("weapon_xm1014");
UTIL_PrecacheOther("ammo_buckshot");
UTIL_PrecacheOtherWeapon("weapon_usp");
UTIL_PrecacheOtherWeapon("weapon_mac10");
UTIL_PrecacheOtherWeapon("weapon_ump45");
UTIL_PrecacheOther("ammo_45acp");
UTIL_PrecacheOtherWeapon("weapon_fiveseven");
UTIL_PrecacheOtherWeapon("weapon_p90");
UTIL_PrecacheOther("ammo_57mm");
// deagle
UTIL_PrecacheOtherWeapon("weapon_deagle");
UTIL_PrecacheOther("ammo_50ae");
// p228
UTIL_PrecacheOtherWeapon("weapon_p228");
UTIL_PrecacheOther("ammo_357sig");
// knife
UTIL_PrecacheOtherWeapon("weapon_knife");
UTIL_PrecacheOtherWeapon("weapon_glock18");
UTIL_PrecacheOtherWeapon("weapon_mp5navy");
UTIL_PrecacheOtherWeapon("weapon_tmp");
UTIL_PrecacheOtherWeapon("weapon_elite");
UTIL_PrecacheOther("ammo_9mm");
UTIL_PrecacheOtherWeapon("weapon_flashbang");
UTIL_PrecacheOtherWeapon("weapon_hegrenade");
UTIL_PrecacheOtherWeapon("weapon_smokegrenade");
UTIL_PrecacheOtherWeapon("weapon_c4");
UTIL_PrecacheOtherWeapon("weapon_galil");
UTIL_PrecacheOtherWeapon("weapon_famas");
if (g_pGameRules->IsDeathmatch())
{
// container for dropped deathmatch weapons
UTIL_PrecacheOther("weaponbox");
}
g_sModelIndexFireball = PRECACHE_MODEL("sprites/zerogxplode.spr"); // fireball
g_sModelIndexWExplosion = PRECACHE_MODEL("sprites/WXplo1.spr"); // underwater fireball
g_sModelIndexSmoke = PRECACHE_MODEL("sprites/steam1.spr"); // smoke
g_sModelIndexBubbles = PRECACHE_MODEL("sprites/bubble.spr"); // bubbles
g_sModelIndexBloodSpray = PRECACHE_MODEL("sprites/bloodspray.spr"); // initial blood
g_sModelIndexBloodDrop = PRECACHE_MODEL("sprites/blood.spr"); // splattered blood
g_sModelIndexSmokePuff = PRECACHE_MODEL("sprites/smokepuff.spr");
g_sModelIndexFireball2 = PRECACHE_MODEL("sprites/eexplo.spr");
g_sModelIndexFireball3 = PRECACHE_MODEL("sprites/fexplo.spr");
g_sModelIndexFireball4 = PRECACHE_MODEL("sprites/fexplo1.spr");
g_sModelIndexRadio = PRECACHE_MODEL("sprites/radio.spr");
g_sModelIndexCTGhost = PRECACHE_MODEL("sprites/b-tele1.spr");
g_sModelIndexTGhost = PRECACHE_MODEL("sprites/c-tele1.spr");
g_sModelIndexC4Glow = PRECACHE_MODEL("sprites/ledglow.spr");
g_sModelIndexLaser = PRECACHE_MODEL("sprites/laserbeam.spr");
g_sModelIndexLaserDot = PRECACHE_MODEL("sprites/laserdot.spr");
// used by explosions
PRECACHE_MODEL("models/grenade.mdl");
PRECACHE_MODEL("sprites/explode1.spr");
PRECACHE_SOUND("weapons/debris1.wav"); // explosion aftermaths
PRECACHE_SOUND("weapons/debris2.wav"); // explosion aftermaths
PRECACHE_SOUND("weapons/debris3.wav"); // explosion aftermaths
PRECACHE_SOUND("weapons/grenade_hit1.wav"); // grenade
PRECACHE_SOUND("weapons/grenade_hit2.wav"); // grenade
PRECACHE_SOUND("weapons/grenade_hit3.wav"); // grenade
PRECACHE_SOUND("weapons/bullet_hit1.wav"); // hit by bullet
PRECACHE_SOUND("weapons/bullet_hit2.wav"); // hit by bullet
PRECACHE_SOUND("items/weapondrop1.wav"); // weapon falls to the ground
PRECACHE_SOUND("weapons/generic_reload.wav");
}
ItemInfo CBasePlayerItem::m_ItemInfoArray[MAX_WEAPONS];
AmmoInfo CBasePlayerItem::m_AmmoInfoArray[MAX_AMMO_SLOTS];
TYPEDESCRIPTION CBasePlayerItem::m_SaveData[] =
{
DEFINE_FIELD(CBasePlayerItem, m_pPlayer, FIELD_CLASSPTR),
DEFINE_FIELD(CBasePlayerItem, m_pNext, FIELD_CLASSPTR),
DEFINE_FIELD(CBasePlayerItem, m_iId, FIELD_INTEGER),
};
IMPLEMENT_SAVERESTORE(CBasePlayerItem, CBaseAnimating)
TYPEDESCRIPTION CBasePlayerWeapon::m_SaveData[] =
{
DEFINE_FIELD(CBasePlayerWeapon, m_flNextPrimaryAttack, FIELD_TIME),
DEFINE_FIELD(CBasePlayerWeapon, m_flNextSecondaryAttack, FIELD_TIME),
DEFINE_FIELD(CBasePlayerWeapon, m_flTimeWeaponIdle, FIELD_TIME),
DEFINE_FIELD(CBasePlayerWeapon, m_iPrimaryAmmoType, FIELD_INTEGER),
DEFINE_FIELD(CBasePlayerWeapon, m_iSecondaryAmmoType, FIELD_INTEGER),
DEFINE_FIELD(CBasePlayerWeapon, m_iClip, FIELD_INTEGER),
DEFINE_FIELD(CBasePlayerWeapon, m_iDefaultAmmo, FIELD_INTEGER),
};
IMPLEMENT_SAVERESTORE(CBasePlayerWeapon, CBasePlayerItem)
void CBasePlayerItem::SetObjectCollisionBox()
{
pev->absmin = pev->origin + Vector(-24, -24, 0);
pev->absmax = pev->origin + Vector(24, 24, 16);
}
// Sets up movetype, size, solidtype for a new weapon.
void CBasePlayerItem::FallInit()
{
pev->movetype = MOVETYPE_TOSS;
pev->solid = SOLID_BBOX;
UTIL_SetOrigin(pev, pev->origin);
// pointsize until it lands on the ground.
UTIL_SetSize(pev, Vector(0, 0, 0), Vector(0, 0, 0));
SetTouch(&CBasePlayerItem::DefaultTouch);
SetThink(&CBasePlayerItem::FallThink);
pev->nextthink = gpGlobals->time + 0.1f;
}
// FallThink - Items that have just spawned run this think
// to catch them when they hit the ground. Once we're sure
// that the object is grounded, we change its solid type
// to trigger and set it in a large box that helps the
// player get it.
void CBasePlayerItem::FallThink()
{
pev->nextthink = gpGlobals->time + 0.1f;
if (pev->flags & FL_ONGROUND)
{
// clatter if we have an owner (i.e., dropped by someone)
// don't clatter if the gun is waiting to respawn (if it's waiting, it is invisible!)
if (!FNullEnt(pev->owner))
{
EMIT_SOUND_DYN(ENT(pev), CHAN_VOICE, "items/weapondrop1.wav", VOL_NORM, ATTN_NORM, 0, RANDOM_LONG(0, 29) + 95);
}
// lie flat
pev->angles.x = 0.0f;
pev->angles.z = 0.0f;
Materialize();
}
}
// Materialize - make a CBasePlayerItem visible and tangible
void CBasePlayerItem::Materialize()
{
if (pev->effects & EF_NODRAW)
{
// changing from invisible state to visible.
if (g_pGameRules->IsMultiplayer())
{
EMIT_SOUND_DYN(ENT(pev), CHAN_WEAPON, "items/suitchargeok1.wav", VOL_NORM, ATTN_NORM, 0, 150);
}
pev->effects &= ~EF_NODRAW;
pev->effects |= EF_MUZZLEFLASH;
}
pev->solid = SOLID_TRIGGER;
// link into world.
UTIL_SetOrigin(pev, pev->origin);
SetTouch(&CBasePlayerItem::DefaultTouch);
if (g_pGameRules->IsMultiplayer())
{
if (!CanDrop())
{
SetTouch(nullptr);
}
SetThink(&CBaseEntity::SUB_Remove);
pev->nextthink = gpGlobals->time + 1.0f;
}
else
{
SetThink(nullptr);
}
}
// AttemptToMaterialize - the item is trying to rematerialize,
// should it do so now or wait longer?
void CBasePlayerItem::AttemptToMaterialize()
{
float time = g_pGameRules->FlWeaponTryRespawn(this);
if (time == 0)
{
Materialize();
return;
}
pev->nextthink = gpGlobals->time + time;
}
// CheckRespawn - a player is taking this weapon, should
// it respawn?
void CBasePlayerItem::CheckRespawn()
{
switch (g_pGameRules->WeaponShouldRespawn(this))
{
case GR_WEAPON_RESPAWN_YES:
return;
case GR_WEAPON_RESPAWN_NO:
return;
}
}
// Respawn - this item is already in the world, but it is
// invisible and intangible. Make it visible and tangible.
CBaseEntity *CBasePlayerItem::Respawn()
{
// make a copy of this weapon that is invisible and inaccessible to players (no touch function). The weapon spawn/respawn code
// will decide when to make the weapon visible and touchable.
CBaseEntity *pNewWeapon = CBaseEntity::Create(pev->classname, g_pGameRules->VecWeaponRespawnSpot(this), pev->angles, pev->owner);
if (pNewWeapon)
{
// invisible for now
pNewWeapon->pev->effects |= EF_NODRAW;
// no touch
pNewWeapon->SetTouch(nullptr);
pNewWeapon->SetThink(&CBasePlayerItem::AttemptToMaterialize);
DROP_TO_FLOOR(ENT(pev));
// not a typo! We want to know when the weapon the player just picked up should respawn! This new entity we created is the replacement,
// but when it should respawn is based on conditions belonging to the weapon that was taken.
pNewWeapon->pev->nextthink = g_pGameRules->FlWeaponRespawnTime(this);
}
else
{
ALERT(at_console, "Respawn failed to create %s!\n", STRING(pev->classname));
}
return pNewWeapon;
}
// whats going on here is that if the player drops this weapon, they shouldn't take it back themselves
// for a little while. But if they throw it at someone else, the other player should get it immediately.
void CBasePlayerItem::DefaultTouch(CBaseEntity *pOther)
{
// if it's not a player, ignore
if (!pOther->IsPlayer())
{
return;
}
CBasePlayer *pPlayer = static_cast<CBasePlayer *>(pOther);
if (pPlayer->m_bIsVIP
&& m_iId != WEAPON_USP
&& m_iId != WEAPON_GLOCK18
&& m_iId != WEAPON_P228
&& m_iId != WEAPON_DEAGLE
&& m_iId != WEAPON_KNIFE)
{
return;
}
// can I have this?
if (!g_pGameRules->CanHavePlayerItem(pPlayer, this))
{
return;
}
if (pOther->AddPlayerItem(this))
{
AttachToPlayer(pPlayer);
#ifndef REGAMEDLL_FIXES
SetThink(nullptr);
#endif
EMIT_SOUND(ENT(pPlayer->pev), CHAN_ITEM, "items/gunpickup2.wav", VOL_NORM, ATTN_NORM);
}
SUB_UseTargets(pOther, USE_TOGGLE, 0);
}
void CBasePlayerWeapon::SetPlayerShieldAnim()
{
if (!m_pPlayer->HasShield())
return;
if (m_iWeaponState & WPNSTATE_SHIELD_DRAWN)
{
Q_strcpy(m_pPlayer->m_szAnimExtention, "shield");
}
else
{
Q_strcpy(m_pPlayer->m_szAnimExtention, "shieldgun");
}
}
void CBasePlayerWeapon::ResetPlayerShieldAnim()
{
if (m_pPlayer->HasShield())
{
if (m_iWeaponState & WPNSTATE_SHIELD_DRAWN)
{
Q_strcpy(m_pPlayer->m_szAnimExtention, "shieldgun");
}
}
}
void CBasePlayerWeapon::EjectBrassLate()
{
int soundType;
Vector vecUp, vecRight, vecShellVelocity;
UTIL_MakeVectors(m_pPlayer->pev->v_angle + m_pPlayer->pev->punchangle);
vecUp = RANDOM_FLOAT(100, 150) * gpGlobals->v_up;
vecRight = RANDOM_FLOAT(50, 70) * gpGlobals->v_right;
vecShellVelocity = (m_pPlayer->pev->velocity + vecRight + vecUp) + gpGlobals->v_forward * 25;
soundType = (m_iId == WEAPON_XM1014 || m_iId == WEAPON_M3) ? 2 : 1;
EjectBrass(pev->origin + m_pPlayer->pev->view_ofs + gpGlobals->v_up * -9 + gpGlobals->v_forward * 16, gpGlobals->v_right * -9,
vecShellVelocity, pev->angles.y, m_iShellId, soundType, m_pPlayer->entindex());
}
bool CBasePlayerWeapon::ShieldSecondaryFire(int iUpAnim, int iDownAnim)
{
if (!m_pPlayer->HasShield())
return false;
if (m_iWeaponState & WPNSTATE_SHIELD_DRAWN)
{
m_iWeaponState &= ~WPNSTATE_SHIELD_DRAWN;
SendWeaponAnim(iDownAnim, UseDecrement() != FALSE);
Q_strcpy(m_pPlayer->m_szAnimExtention, "shieldgun");
m_fMaxSpeed = 250.0f;
m_pPlayer->m_bShieldDrawn = false;
}
else
{
m_iWeaponState |= WPNSTATE_SHIELD_DRAWN;
SendWeaponAnim(iUpAnim, UseDecrement() != FALSE);
Q_strcpy(m_pPlayer->m_szAnimExtention, "shielded");
m_fMaxSpeed = 180.0f;
m_pPlayer->m_bShieldDrawn = true;
}
m_pPlayer->UpdateShieldCrosshair((m_iWeaponState & WPNSTATE_SHIELD_DRAWN) != WPNSTATE_SHIELD_DRAWN);
m_pPlayer->ResetMaxSpeed();
m_flNextSecondaryAttack = 0.4f;
m_flNextPrimaryAttack = 0.4f;
m_flTimeWeaponIdle = 0.6f;
return true;
}
LINK_HOOK_CLASS_VOID_CHAIN(CBasePlayerWeapon, KickBack, (float up_base, float lateral_base, float up_modifier, float lateral_modifier, float up_max, float lateral_max, int direction_change), up_base, lateral_base, up_modifier, lateral_modifier, up_max, lateral_max, direction_change)
void EXT_FUNC CBasePlayerWeapon::__API_HOOK(KickBack)(float up_base, float lateral_base, float up_modifier, float lateral_modifier, float up_max, float lateral_max, int direction_change)
{
real_t flKickUp;
float flKickLateral;
if (m_iShotsFired == 1)
{
flKickUp = up_base;
flKickLateral = lateral_base;
}
else
{
flKickUp = m_iShotsFired * up_modifier + up_base;
flKickLateral = m_iShotsFired * lateral_modifier + lateral_base;
}
m_pPlayer->pev->punchangle.x -= flKickUp;
if (m_pPlayer->pev->punchangle.x < -up_max)
{
m_pPlayer->pev->punchangle.x = -up_max;
}
if (m_iDirection == 1)
{
m_pPlayer->pev->punchangle.y += flKickLateral;
if (m_pPlayer->pev->punchangle.y > lateral_max)
m_pPlayer->pev->punchangle.y = lateral_max;
}
else
{
m_pPlayer->pev->punchangle.y -= flKickLateral;
if (m_pPlayer->pev->punchangle.y < -lateral_max)
m_pPlayer->pev->punchangle.y = -lateral_max;
}
if (!RANDOM_LONG(0, direction_change))
{
m_iDirection = !m_iDirection;
}
}
void CBasePlayerWeapon::FireRemaining(int &shotsFired, float &shootTime, BOOL bIsGlock)
{
float nexttime = 0.1f;
if (--m_iClip < 0)
{
m_iClip = 0;
shotsFired = 3;
shootTime = 0;
return;
}
UTIL_MakeVectors(m_pPlayer->pev->v_angle + m_pPlayer->pev->punchangle);
Vector vecSrc = m_pPlayer->GetGunPosition();
Vector vecDir;
int flag;
#ifdef CLIENT_WEAPONS
flag = FEV_NOTHOST;
#else
flag = 0;
#endif
#ifdef REGAMEDLL_API
float flBaseDamage = CSPlayerWeapon()->m_flBaseDamage;
#else
float flBaseDamage = bIsGlock ? GLOCK18_DAMAGE : FAMAS_DAMAGE;
#endif
if (bIsGlock)
{
vecDir = m_pPlayer->FireBullets3(vecSrc, gpGlobals->v_forward, 0.05, 8192, 1, BULLET_PLAYER_9MM, flBaseDamage, 0.9, m_pPlayer->pev, true, m_pPlayer->random_seed);
#ifndef REGAMEDLL_FIXES
--m_pPlayer->ammo_9mm;
#endif
PLAYBACK_EVENT_FULL(flag, m_pPlayer->edict(), m_usFireGlock18, 0, (float *)&g_vecZero, (float *)&g_vecZero, vecDir.x, vecDir.y,
int(m_pPlayer->pev->punchangle.x * 10000), int(m_pPlayer->pev->punchangle.y * 10000), m_iClip == 0, FALSE);
}
else
{
vecDir = m_pPlayer->FireBullets3(vecSrc, gpGlobals->v_forward, m_fBurstSpread, 8192, 2, BULLET_PLAYER_556MM, flBaseDamage, 0.96, m_pPlayer->pev, false, m_pPlayer->random_seed);
#ifndef REGAMEDLL_FIXES
--m_pPlayer->ammo_556nato;
#endif
#ifdef REGAMEDLL_ADD
// HACKHACK: client-side weapon prediction fix
if (!(iFlags() & ITEM_FLAG_NOFIREUNDERWATER) && m_pPlayer->pev->waterlevel == 3)
flag = 0;
#endif
PLAYBACK_EVENT_FULL(flag, m_pPlayer->edict(), m_usFireFamas, 0, (float *)&g_vecZero, (float *)&g_vecZero, vecDir.x, vecDir.y,
int(m_pPlayer->pev->punchangle.x * 10000000), int(m_pPlayer->pev->punchangle.y * 10000000), FALSE, FALSE);
}
m_pPlayer->pev->effects |= EF_MUZZLEFLASH;
m_pPlayer->SetAnimation(PLAYER_ATTACK1);
if (++shotsFired != 3)
{
shootTime = gpGlobals->time + nexttime;
}
else
shootTime = 0;
}
BOOL CanAttack(float attack_time, float curtime, BOOL isPredicted)
{
#ifdef CLIENT_WEAPONS
if (!isPredicted)
#else
if (1)
#endif
{
return (attack_time <= curtime) ? TRUE : FALSE;
}
else
{
return (attack_time <= 0.0f) ? TRUE : FALSE;
}
}
bool CBasePlayerWeapon::HasSecondaryAttack()
{
#ifdef REGAMEDLL_API
if (CSPlayerWeapon()->m_iStateSecondaryAttack != WEAPON_SECONDARY_ATTACK_NONE)
{
switch (CSPlayerWeapon()->m_iStateSecondaryAttack)
{
case WEAPON_SECONDARY_ATTACK_SET:
return true;
case WEAPON_SECONDARY_ATTACK_BLOCK:
return false;
default:
break;
}
}
#endif
if (m_pPlayer && m_pPlayer->HasShield())
{
return true;
}
switch (m_iId)
{
case WEAPON_AK47:
case WEAPON_XM1014:
case WEAPON_MAC10:
case WEAPON_ELITE:
case WEAPON_FIVESEVEN:
case WEAPON_MP5N:
#ifdef BUILD_LATEST_FIXES
case WEAPON_UMP45:
#endif
case WEAPON_M249:
case WEAPON_M3:
case WEAPON_TMP:
case WEAPON_DEAGLE:
case WEAPON_P228:
case WEAPON_P90:
case WEAPON_C4:
case WEAPON_GALIL:
return false;
default:
break;
}
return true;
}
void CBasePlayerWeapon::HandleInfiniteAmmo()
{
int nInfiniteAmmo = 0;
#ifdef REGAMEDLL_API
nInfiniteAmmo = m_pPlayer->CSPlayer()->m_iWeaponInfiniteAmmo;
#endif
if (!nInfiniteAmmo)
nInfiniteAmmo = static_cast<int>(infiniteAmmo.value);
if (nInfiniteAmmo == WPNMODE_INFINITE_CLIP && !IsGrenadeWeapon(m_iId))
{
m_iClip = iMaxClip();
}
else if ((nInfiniteAmmo == WPNMODE_INFINITE_BPAMMO
#ifdef REGAMEDLL_API
&&
((m_pPlayer->CSPlayer()->m_iWeaponInfiniteIds & (1 << m_iId)) || (m_pPlayer->CSPlayer()->m_iWeaponInfiniteIds <= 0 && !IsGrenadeWeapon(m_iId)))
#endif
)
|| (IsGrenadeWeapon(m_iId) && infiniteGrenades.value == 1.0f))
{
if (pszAmmo1())
{
m_pPlayer->m_rgAmmo[PrimaryAmmoIndex()] = iMaxAmmo1();
}
if (pszAmmo2())
{
m_pPlayer->m_rgAmmo[SecondaryAmmoIndex()] = iMaxAmmo2();
}
}
}
LINK_HOOK_CLASS_VOID_CHAIN2(CBasePlayerWeapon, ItemPostFrame)
void EXT_FUNC CBasePlayerWeapon::__API_HOOK(ItemPostFrame)()
{
int usableButtons = m_pPlayer->pev->button;
if (!HasSecondaryAttack())
{
usableButtons &= ~IN_ATTACK2;
}
if (m_flGlock18Shoot != 0)
{
FireRemaining(m_iGlock18ShotsFired, m_flGlock18Shoot, TRUE);
}
else if (gpGlobals->time > m_flFamasShoot && m_flFamasShoot != 0)
{
FireRemaining(m_iFamasShotsFired, m_flFamasShoot, FALSE);
}
// Return zoom level back to previous zoom level before we fired a shot.
// This is used only for the AWP and Scout
if (m_flNextPrimaryAttack <= UTIL_WeaponTimeBase())
{
if (m_pPlayer->m_bResumeZoom)
{
m_pPlayer->m_iFOV = m_pPlayer->m_iLastZoom;
m_pPlayer->pev->fov = m_pPlayer->m_iFOV;
if (m_pPlayer->m_iFOV == m_pPlayer->m_iLastZoom)
{
// return the fade level in zoom.
m_pPlayer->m_bResumeZoom = false;
}
}
}
if (m_pPlayer->m_flEjectBrass != 0 && m_pPlayer->m_flEjectBrass <= gpGlobals->time)
{
m_pPlayer->m_flEjectBrass = 0;
EjectBrassLate();
}
if (!(m_pPlayer->pev->button & IN_ATTACK))
{
m_flLastFireTime = 0;
}
if (m_pPlayer->HasShield())
{
if (m_fInReload && (m_pPlayer->pev->button & IN_ATTACK2))
{
SecondaryAttack();
m_pPlayer->pev->button &= ~IN_ATTACK2;
m_fInReload = FALSE;
m_pPlayer->m_flNextAttack = UTIL_WeaponTimeBase();
}
}
if (m_fInReload && m_pPlayer->m_flNextAttack <= UTIL_WeaponTimeBase())
{
// complete the reload.
int j = Q_min(iMaxClip() - m_iClip, m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType]);
// Add them to the clip
m_iClip += j;
#ifdef REGAMEDLL_ADD
// Do not remove bpammo of the player,
// if cvar allows to refill bpammo on during reloading the weapons
if (refill_bpammo_weapons.value < 3.0f)
#endif
{
m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType] -= j;
}
m_pPlayer->TabulateAmmo();
m_fInReload = FALSE;