Skip to content

Commit 4903176

Browse files
committed
common: add Player.IsPlanting attribute (#132)
1 parent 1bcd368 commit 4903176

File tree

7 files changed

+53
-16
lines changed

7 files changed

+53
-16
lines changed

common/player.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type Player struct {
4040
IsConnected bool
4141
IsDucking bool
4242
IsDefusing bool
43+
IsPlanting bool
4344
HasDefuseKit bool
4445
HasHelmet bool
4546
}

datatables.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,27 @@ func (p *Parser) bindEntities() {
7272
func (p *Parser) bindBomb() {
7373
bomb := &p.gameState.bomb
7474

75-
// Track bomb when it is not held by a player
76-
scDroppedC4 := p.stParser.ServerClasses().FindByName("CC4")
77-
scDroppedC4.OnEntityCreated(func(bomb *st.Entity) {
78-
bomb.OnPositionUpdate(func(pos r3.Vector) {
75+
// Track bomb when it is dropped on the ground or being held by a player
76+
scC4 := p.stParser.ServerClasses().FindByName("CC4")
77+
scC4.OnEntityCreated(func(bombEntity *st.Entity) {
78+
bombEntity.OnPositionUpdate(func(pos r3.Vector) {
7979
// Bomb only has a position when not held by a player
80-
p.gameState.bomb.Carrier = nil
80+
bomb.Carrier = nil
8181

82-
p.gameState.bomb.LastOnGroundPosition = pos
82+
bomb.LastOnGroundPosition = pos
83+
})
84+
85+
bombEntity.FindPropertyI("m_hOwner").OnUpdate(func(val st.PropertyValue) {
86+
bomb.Carrier = p.gameState.Participants().FindByHandle(val.IntVal)
87+
})
88+
89+
bombEntity.FindPropertyI("m_bStartedArming").OnUpdate(func(val st.PropertyValue) {
90+
if val.IntVal != 0 {
91+
p.gameState.currentPlanter = bomb.Carrier
92+
} else if p.gameState.currentPlanter != nil {
93+
p.gameState.currentPlanter.IsPlanting = false
94+
p.eventDispatcher.Dispatch(events.BombPlantAborted{Player: p.gameState.currentPlanter})
95+
}
8396
})
8497
})
8598

@@ -91,14 +104,6 @@ func (p *Parser) bindBomb() {
91104

92105
bomb.LastOnGroundPosition = bombEntity.Position()
93106
})
94-
95-
// Track bomb when it is being held by a player
96-
scPlayerC4 := p.stParser.ServerClasses().FindByName("CC4")
97-
scPlayerC4.OnEntityCreated(func(bombEntity *st.Entity) {
98-
bombEntity.FindPropertyI("m_hOwner").OnUpdate(func(val st.PropertyValue) {
99-
bomb.Carrier = p.gameState.Participants().FindByHandle(val.IntVal)
100-
})
101-
})
102107
}
103108

104109
func (p *Parser) bindTeamStates() {

demoinfocs_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,23 @@ func TestDemoInfoCs(t *testing.T) {
9898
}
9999
})
100100

101+
// bomb planting checks
102+
p.RegisterEventHandler(func(begin events.BombPlantBegin) {
103+
if !begin.Player.IsPlanting {
104+
t.Error("Player started planting but IsPlanting is false")
105+
}
106+
})
107+
p.RegisterEventHandler(func(abort events.BombPlantAborted) {
108+
if abort.Player.IsPlanting {
109+
t.Error("Player aborted planting but IsPlanting is true")
110+
}
111+
})
112+
p.RegisterEventHandler(func(planted events.BombPlanted) {
113+
if planted.Player.IsPlanting {
114+
t.Error("Player finished planting but IsPlanting is true")
115+
}
116+
})
117+
101118
// Check some things at match start
102119
p.RegisterEventHandler(func(events.MatchStart) {
103120
participants := gs.Participants()

events/events.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,13 @@ type BombPlantBegin struct {
286286
BombEvent
287287
}
288288

289+
// BombPlantAbort signals the abortion of a plant.
290+
type BombPlantAborted struct {
291+
Player *common.Player
292+
}
293+
294+
func (BombPlantAborted) implementsBombEventIf() {}
295+
289296
// BombPlanted signals that the bomb has been planted.
290297
type BombPlanted struct {
291298
BombEvent

game_events.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,11 +433,17 @@ func (geh gameEventHandler) playerTeam(data map[string]*msg.CSVCMsg_GameEventKey
433433
}
434434

435435
func (geh gameEventHandler) bombBeginPlant(data map[string]*msg.CSVCMsg_GameEventKeyT) {
436-
geh.dispatch(events.BombPlantBegin{BombEvent: geh.bombEvent(data)})
436+
event := events.BombPlantBegin{BombEvent: geh.bombEvent(data)}
437+
event.Player.IsPlanting = true
438+
geh.parser.gameState.currentPlanter = event.Player
439+
geh.dispatch(event)
437440
}
438441

439442
func (geh gameEventHandler) bombPlanted(data map[string]*msg.CSVCMsg_GameEventKeyT) {
440-
geh.dispatch(events.BombPlanted{BombEvent: geh.bombEvent(data)})
443+
event := events.BombPlanted{BombEvent: geh.bombEvent(data)}
444+
event.Player.IsPlanting = false
445+
geh.parser.gameState.currentPlanter = nil
446+
geh.dispatch(event)
441447
}
442448

443449
func (geh gameEventHandler) bombDefused(data map[string]*msg.CSVCMsg_GameEventKeyT) {

game_state.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type GameState struct {
2626
isMatchStarted bool
2727
lastFlasher *common.Player // Last player whose flash exploded, used to find the attacker for player_blind events
2828
currentDefuser *common.Player // Player currently defusing the bomb, if any
29+
currentPlanter *common.Player // Player currently planting the bomb, if any
2930
}
3031

3132
type ingameTickNumber int

test/default.golden

49 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)