55 "strings"
66
77 "github.com/golang/geo/r3"
8+ "github.com/markus-wa/go-unassert"
89
910 "github.com/markus-wa/demoinfocs-golang/common"
1011 "github.com/markus-wa/demoinfocs-golang/events"
@@ -24,36 +25,40 @@ const (
2425
2526func (p * Parser ) mapEquipment () {
2627 for _ , sc := range p .stParser .ServerClasses () {
28+ switch sc .Name () {
29+ case "CC4" :
30+ p .equipmentMapping [sc ] = common .EqBomb
31+ continue
32+
33+ case "CWeaponNOVA" :
34+ fallthrough
35+ case "CWeaponSawedoff" :
36+ fallthrough
37+ case "CWeaponXM1014" :
38+ p .equipmentMapping [sc ] = common .MapEquipment (strings .ToLower (sc .Name ()[7 :]))
39+ continue
40+ }
41+
2742 baseClasses := sc .BaseClasses ()
2843 for _ , bc := range baseClasses {
2944 if bc .Name () == "CBaseGrenade" { // Grenades projectiles, i.e. thrown by player
3045 p .equipmentMapping [sc ] = common .MapEquipment (strings .ToLower (sc .DataTableName ()[3 :]))
46+ break
3147 }
32- }
3348
34- if len (baseClasses ) > 6 && baseClasses [6 ].Name () == "CWeaponCSBase" {
35- if len (baseClasses ) > 7 {
36- switch baseClasses [7 ].Name () {
37- case "CWeaponCSBaseGun" :
38- // Most guns
39- p .equipmentMapping [sc ] = common .MapEquipment (strings .ToLower (sc .DataTableName ()[9 :]))
40- case "CBaseCSGrenade" :
41- // Nades
42- p .equipmentMapping [sc ] = common .MapEquipment (strings .ToLower (sc .DataTableName ()[3 :]))
43- }
44- } else if sc .Name () == "CKnife" || (len (baseClasses ) > 6 && baseClasses [6 ].Name () == "CKnife" ) {
49+ if bc .Name () == "CKnife" {
4550 p .equipmentMapping [sc ] = common .EqKnife
46- } else {
47- switch sc . Name () {
48- case "CC4" :
49- p . equipmentMapping [ sc ] = common . EqBomb
50- case "CWeaponNOVA" :
51- fallthrough
52- case "CWeaponSawedoff" :
53- fallthrough
54- case "CWeaponXM1014" :
55- p .equipmentMapping [sc ] = common .MapEquipment (strings .ToLower (sc .Name ()[7 :]))
56- }
51+ break
52+ }
53+
54+ if bc . Name () == "CWeaponCSBaseGun" { // most guns
55+ p . equipmentMapping [ sc ] = common . MapEquipment ( strings . ToLower ( sc . DataTableName ()[ 9 :]))
56+ break
57+ }
58+
59+ if bc . Name () == "CBaseCSGrenade" { // nades
60+ p .equipmentMapping [sc ] = common .MapEquipment (strings .ToLower (sc .DataTableName ()[3 :]))
61+ break
5762 }
5863 }
5964 }
@@ -375,7 +380,17 @@ func (p *Parser) bindGrenadeProjectiles(entity *st.Entity) {
375380 proj .EntityID = entityID
376381 p .gameState .grenadeProjectiles [entityID ] = proj
377382
383+ var wep common.EquipmentElement
378384 entity .OnCreateFinished (func () {
385+ // copy the weapon so it doesn't get overwritten by a new entity in Parser.weapons
386+ wepCopy := * (getPlayerWeapon (proj .Thrower , wep ))
387+ proj .WeaponInstance = & wepCopy
388+
389+ unassert .NotNilf (proj .WeaponInstance , "couldn't find grenade instance for player" )
390+ if proj .WeaponInstance != nil {
391+ unassert .NotNilf (proj .WeaponInstance .Owner , "getPlayerWeapon() returned weapon instance with Owner=nil" )
392+ }
393+
379394 p .gameEventHandler .addThrownGrenade (proj .Thrower , proj .WeaponInstance )
380395
381396 p .eventDispatcher .Dispatch (events.GrenadeProjectileThrow {
@@ -389,9 +404,7 @@ func (p *Parser) bindGrenadeProjectiles(entity *st.Entity) {
389404
390405 entity .FindPropertyI ("m_nModelIndex" ).OnUpdate (func (val st.PropertyValue ) {
391406 proj .Weapon = p .grenadeModelIndices [val .IntVal ]
392-
393- equipment := common .NewEquipment (p .grenadeModelIndices [val .IntVal ])
394- proj .WeaponInstance = & equipment
407+ wep = p .grenadeModelIndices [val .IntVal ]
395408 })
396409
397410 // @micvbang: not quite sure what the difference between Thrower and Owner is.
0 commit comments