Skip to content

Commit f5f80e7

Browse files
authored
Merge pull request #599 from markus-wa/smoke-uniqueid
fix: SmokeStart wrong UniqueID2()
2 parents d4feac5 + 9c2b44a commit f5f80e7

File tree

3 files changed

+48
-29
lines changed

3 files changed

+48
-29
lines changed

pkg/demoinfocs/game_events.go

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ func (geh gameEventHandler) clearGrenadeProjectiles() {
324324
}
325325

326326
// Thrown grenades could not be deleted at the end of the round (if they are thrown at the very end, they never get destroyed)
327-
geh.gameState().thrownGrenades = make(map[*common.Player][]*common.Equipment)
327+
geh.gameState().thrownGrenades = make(map[*common.Player]map[common.EquipmentType][]*common.Equipment)
328328
geh.gameState().flyingFlashbangs = make([]*FlyingFlashbang, 0)
329329
}
330330

@@ -1021,7 +1021,11 @@ func (geh gameEventHandler) addThrownGrenade(p *common.Player, wep *common.Equip
10211021
}
10221022

10231023
gameState := geh.gameState()
1024-
gameState.thrownGrenades[p] = append(gameState.thrownGrenades[p], wep)
1024+
if gameState.thrownGrenades[p] == nil {
1025+
gameState.thrownGrenades[p] = make(map[common.EquipmentType][]*common.Equipment)
1026+
}
1027+
1028+
gameState.thrownGrenades[p][wep.Type] = append(gameState.thrownGrenades[p][wep.Type], wep)
10251029
}
10261030

10271031
func (geh gameEventHandler) getThrownGrenade(p *common.Player, wepType common.EquipmentType) *common.Equipment {
@@ -1030,14 +1034,32 @@ func (geh gameEventHandler) getThrownGrenade(p *common.Player, wepType common.Eq
10301034
return nil
10311035
}
10321036

1033-
// Get the first weapon we found for this player with this weapon type
1034-
for _, thrownGrenade := range geh.gameState().thrownGrenades[p] {
1035-
if isSameEquipmentElement(thrownGrenade.Type, wepType) {
1036-
return thrownGrenade
1037+
playerGrenades := geh.gameState().thrownGrenades[p]
1038+
grenades := playerGrenades[wepType]
1039+
1040+
if len(grenades) == 0 {
1041+
// Molotovs/incendiaries may be reported as the opposite type in game-events. (i.e. incendiary reported as molotov and vice versa)
1042+
switch wepType { //nolint:exhaustive
1043+
case common.EqIncendiary:
1044+
grenades = playerGrenades[common.EqMolotov]
1045+
case common.EqMolotov:
1046+
grenades = playerGrenades[common.EqIncendiary]
1047+
}
1048+
}
1049+
1050+
if len(grenades) == 0 {
1051+
// The player might be controlling a bot, in such case the thrown grenade is stored in the bot's state.
1052+
bot := p.ControlledBot()
1053+
if bot != nil && bot.SteamID64 != p.SteamID64 {
1054+
return geh.getThrownGrenade(bot, wepType)
10371055
}
10381056
}
10391057

1040-
return nil
1058+
if len(grenades) == 0 {
1059+
return nil
1060+
}
1061+
1062+
return grenades[len(grenades)-1]
10411063
}
10421064

10431065
func (geh gameEventHandler) deleteThrownGrenade(p *common.Player, wepType common.EquipmentType) {
@@ -1046,16 +1068,20 @@ func (geh gameEventHandler) deleteThrownGrenade(p *common.Player, wepType common
10461068
return
10471069
}
10481070

1049-
gameState := geh.gameState()
1071+
playerGrenades := geh.gameState().thrownGrenades[p]
1072+
if len(playerGrenades) == 0 {
1073+
return
1074+
}
10501075

1051-
// Delete the first weapon we found with this weapon type
1052-
for i, weapon := range gameState.thrownGrenades[p] {
1053-
// If same weapon type
1054-
// OR if it's an EqIncendiary we must check for EqMolotov too because of geh.infernoExpire() handling ?
1055-
if isSameEquipmentElement(wepType, weapon.Type) {
1056-
gameState.thrownGrenades[p] = append(gameState.thrownGrenades[p][:i], gameState.thrownGrenades[p][i+1:]...)
1057-
return
1058-
}
1076+
grenades := playerGrenades[wepType]
1077+
if len(grenades) == 0 {
1078+
return
1079+
}
1080+
1081+
// Delete the first grenade thrown by the player and this grenade type.
1082+
playerGrenades[wepType] = grenades[:len(grenades)-1]
1083+
if len(playerGrenades[wepType]) == 0 {
1084+
delete(playerGrenades, wepType)
10591085
}
10601086
}
10611087

@@ -1090,13 +1116,6 @@ func (geh gameEventHandler) getEquipmentInstance(player *common.Player, wepType
10901116
return getPlayerWeapon(player, wepType)
10911117
}
10921118

1093-
// checks if two EquipmentElements are the same, considering that incendiary and molotov should be treated as identical
1094-
func isSameEquipmentElement(a common.EquipmentType, b common.EquipmentType) bool {
1095-
return a == b ||
1096-
(a == common.EqIncendiary && b == common.EqMolotov) ||
1097-
(b == common.EqIncendiary && a == common.EqMolotov)
1098-
}
1099-
11001119
// Returns the players instance of the weapon if applicable or a new instance otherwise.
11011120
func getPlayerWeapon(player *common.Player, wepType common.EquipmentType) *common.Equipment {
11021121
if player != nil {

pkg/demoinfocs/game_events_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func TestAddThrownGrenade(t *testing.T) {
115115

116116
assert.NotEmpty(t, p.gameState.thrownGrenades)
117117
assert.NotEmpty(t, p.gameState.thrownGrenades[pl])
118-
assert.Equal(t, p.gameState.thrownGrenades[pl][0], he)
118+
assert.Equal(t, p.gameState.thrownGrenades[pl][common.EqHE][0], he)
119119
}
120120

121121
func TestGetThrownGrenade_NilPlayer(t *testing.T) {

pkg/demoinfocs/game_state.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ type gameState struct {
3737
isFreezetime bool
3838
isMatchStarted bool
3939
overtimeCount int
40-
lastFlash lastFlash // Information about the last flash that exploded, used to find the attacker and projectile for player_blind events
41-
currentDefuser *common.Player // Player currently defusing the bomb, if any
42-
currentPlanter *common.Player // Player currently planting the bomb, if any
43-
thrownGrenades map[*common.Player][]*common.Equipment // Information about every player's thrown grenades (from the moment they are thrown to the moment their effect is ended)
40+
lastFlash lastFlash // Information about the last flash that exploded, used to find the attacker and projectile for player_blind events
41+
currentDefuser *common.Player // Player currently defusing the bomb, if any
42+
currentPlanter *common.Player // Player currently planting the bomb, if any
43+
thrownGrenades map[*common.Player]map[common.EquipmentType][]*common.Equipment // Information about every player's thrown grenades (from the moment they are thrown to the moment their effect is ended)
4444
rules gameRules
4545
demoInfo demoInfoProvider
4646
lastRoundStartEvent *events.RoundStart // Used to dispatch this event after a possible MatchStartedChanged event
@@ -249,7 +249,7 @@ func newGameState(demoInfo demoInfoProvider) *gameState {
249249
weapons: make(map[int]*common.Equipment),
250250
hostages: make(map[int]*common.Hostage),
251251
entities: make(map[int]st.Entity),
252-
thrownGrenades: make(map[*common.Player][]*common.Equipment),
252+
thrownGrenades: make(map[*common.Player]map[common.EquipmentType][]*common.Equipment),
253253
flyingFlashbangs: make([]*FlyingFlashbang, 0),
254254
lastFlash: lastFlash{
255255
projectileByPlayer: make(map[*common.Player]*common.GrenadeProjectile),

0 commit comments

Comments
 (0)