Skip to content

Commit 1c41d30

Browse files
committed
Fix Player.Weapons (#31)
1 parent d5761a6 commit 1c41d30

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

common/structs.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ type Player struct {
5151
RoundStartEquipmentValue int
5252
ActiveWeaponID int
5353
RawWeapons map[int]*Equipment
54-
Weapons []*Equipment
5554
AmmoLeft [32]int
5655
Entity *st.Entity
5756
AdditionalPlayerInformation *AdditionalPlayerInformation
@@ -77,6 +76,15 @@ func (p *Player) ActiveWeapon() *Equipment {
7776
return p.RawWeapons[p.ActiveWeaponID]
7877
}
7978

79+
// Weapons returns all weapons in the player's possession
80+
func (p *Player) Weapons() []*Equipment {
81+
res := make([]*Equipment, 0, len(p.RawWeapons))
82+
for _, w := range p.RawWeapons {
83+
res = append(res, w)
84+
}
85+
return res
86+
}
87+
8088
// AdditionalPlayerInformation contains mostly scoreboard information.
8189
type AdditionalPlayerInformation struct {
8290
Kills int

datatables.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ func (p *Parser) bindNewPlayer(playerEntity *st.Entity) {
263263
if entityID != indexMask {
264264
if cache[i2] != 0 {
265265
// Player already has a weapon in this slot.
266-
pl.RawWeapons[cache[i2]] = nil
266+
delete(pl.RawWeapons, cache[i2])
267267
}
268268
cache[i2] = entityID
269269

@@ -275,9 +275,10 @@ func (p *Parser) bindNewPlayer(playerEntity *st.Entity) {
275275
if cache[i2] != 0 && pl.RawWeapons[cache[i2]] != nil {
276276
pl.RawWeapons[cache[i2]].Owner = nil
277277
}
278-
pl.RawWeapons[cache[i2]] = nil
278+
delete(pl.RawWeapons, cache[i2])
279+
280+
cache[i2] = 0
279281
}
280-
cache[i2] = 0
281282
})
282283
}
283284

game_events.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ func (p *Parser) handleGameEvent(ge *msg.CSVCMsg_GameEvent) {
437437
func getAttackingWeapon(wep *common.Equipment, attacker *common.Player) *common.Equipment {
438438
class := wep.Class()
439439
isSpecialWeapon := class == common.EqClassGrenade || (class == common.EqClassEquipment && wep.Weapon != common.EqKnife)
440-
if !isSpecialWeapon && attacker != nil && len(attacker.Weapons) > 0 {
440+
if !isSpecialWeapon && attacker != nil && len(attacker.RawWeapons) > 0 {
441441
return attacker.ActiveWeapon()
442442
} else {
443443
return wep

0 commit comments

Comments
 (0)