Skip to content

Commit 690bda0

Browse files
committed
common: add new ammo functions to Equipment for grenades
see #145 - AmmoInMagazine2() returns 1 for grenades - AmmoReserve2() returns 'Owner.LeftAmmo[AmmoType] - 1' for grenades
1 parent a13ce5d commit 690bda0

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

common/equipment.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ type Equipment struct {
274274
Weapon EquipmentElement // The type of weapon which the equipment instantiates.
275275
Owner *Player // The player carrying the equipment, not necessarily the buyer.
276276
AmmoType int // TODO: Remove this? doesn't seem applicable to CS:GO
277-
AmmoInMagazine int // Amount of bullets in the weapon's magazine
277+
AmmoInMagazine int // Amount of bullets in the weapon's magazine. Deprecated, use AmmoInMagazine2() instead.
278278
AmmoReserve int // Amount of reserve bullets
279279
OriginalString string // E.g. 'models/weapons/w_rif_m4a1_s.mdl'. Used internally to differentiate alternative weapons (M4A4 / M4A1-S etc.).
280280
ZoomLevel int // How far the player has zoomed in on the weapon. 0=no zoom, 1=first level, 2=maximum zoom
@@ -295,6 +295,32 @@ func (e Equipment) UniqueID() int64 {
295295
return e.uniqueID
296296
}
297297

298+
// AmmoInMagazine2 returns the ammo left in the magazine.
299+
// Returns CWeaponCSBase.m_iClip1 for most weapons and 1 for grenades.
300+
func (e Equipment) AmmoInMagazine2() int {
301+
if e.Class() == EqClassGrenade {
302+
return 1
303+
}
304+
305+
return e.AmmoInMagazine
306+
}
307+
308+
// AmmoReserve2 returns the ammo left available for reloading.
309+
// Returns CWeaponCSBase.m_iPrimaryReserveAmmoCount for most weapons and 'Owner.AmmoLeft[AmmoType] - 1' for grenades.
310+
// Use AmmoInMagazine2() + AmmoReserve2() to quickly get the amount of grenades a player owns.
311+
func (e Equipment) AmmoReserve2() int {
312+
if e.Class() == EqClassGrenade {
313+
if e.Owner != nil {
314+
// minus one for 'InMagazine'
315+
return e.Owner.AmmoLeft[e.AmmoType] - 1
316+
}
317+
318+
return 0
319+
}
320+
321+
return e.AmmoReserve
322+
}
323+
298324
// NewEquipment creates a new Equipment and sets the UniqueID.
299325
//
300326
// Intended for internal use only.

common/equipment_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,46 @@ func TestEquipment_Class(t *testing.T) {
3535
func TestEquipment_UniqueID(t *testing.T) {
3636
assert.NotEqual(t, NewEquipment(EqAK47).UniqueID(), NewEquipment(EqAK47).UniqueID(), "UniqueIDs of different equipment instances should be different")
3737
}
38+
39+
func TestEquipment_AmmoInMagazine2_Default(t *testing.T) {
40+
wep := &Equipment{AmmoInMagazine: 1}
41+
42+
assert.Equal(t, 1, wep.AmmoInMagazine2())
43+
}
44+
45+
func TestEquipment_AmmoInMagazine2_Grenade(t *testing.T) {
46+
wep := &Equipment{
47+
Weapon: EqFlash,
48+
}
49+
50+
assert.Equal(t, 1, wep.AmmoInMagazine2())
51+
}
52+
53+
func TestEquipment_AmmoReserve2_Default(t *testing.T) {
54+
wep := &Equipment{
55+
AmmoReserve: 1,
56+
}
57+
58+
assert.Equal(t, 1, wep.AmmoReserve2())
59+
}
60+
61+
func TestEquipment_AmmoReserve2_Grenade(t *testing.T) {
62+
owner := new(Player)
63+
owner.AmmoLeft[1] = 2
64+
wep := &Equipment{
65+
Weapon: EqFlash,
66+
AmmoInMagazine: -1,
67+
Owner: owner,
68+
AmmoType: 1,
69+
}
70+
71+
assert.Equal(t, 1, wep.AmmoReserve2())
72+
}
73+
74+
func TestEquipment_AmmoReserve2_Grenade_OwnerNil(t *testing.T) {
75+
wep := &Equipment{
76+
Weapon: EqFlash,
77+
}
78+
79+
assert.Equal(t, 0, wep.AmmoReserve2())
80+
}

common/player.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"time"
55

66
"github.com/golang/geo/r3"
7+
78
st "github.com/markus-wa/demoinfocs-golang/sendtables"
89
)
910

@@ -26,7 +27,7 @@ type Player struct {
2627
RoundStartEquipmentValue int
2728
ActiveWeaponID int // Used internally to set the active weapon, see ActiveWeapon()
2829
RawWeapons map[int]*Equipment // All weapons the player is currently carrying
29-
AmmoLeft [32]int // Ammo left in the various weapons, index corresponds to key of RawWeapons
30+
AmmoLeft [32]int // Ammo left for special weapons (e.g. grenades), index corresponds Equipment.AmmoType
3031
Entity st.IEntity
3132
AdditionalPlayerInformation *AdditionalPlayerInformation // Mostly scoreboard information such as kills, deaths, etc.
3233
ViewDirectionX float32

0 commit comments

Comments
 (0)