Skip to content

Commit d3043fb

Browse files
GameState: expose weapons via Weapons() func (#195)
1 parent dfd0d94 commit d3043fb

File tree

13 files changed

+64
-32
lines changed

13 files changed

+64
-32
lines changed

common/equipment.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,8 @@ func (e Equipment) AmmoReserve2() int {
333333
// NewEquipment creates a new Equipment and sets the UniqueID.
334334
//
335335
// Intended for internal use only.
336-
func NewEquipment(wep EquipmentElement) Equipment {
337-
return Equipment{Weapon: wep, uniqueID: rand.Int63()}
336+
func NewEquipment(wep EquipmentElement) *Equipment {
337+
return &Equipment{Weapon: wep, uniqueID: rand.Int63()}
338338
}
339339

340340
var equipmentToAlternative = map[EquipmentElement]EquipmentElement{

common/player_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ func TestPlayerActiveWeapon(t *testing.T) {
1515
ak47 := NewEquipment(EqAK47)
1616

1717
pl := newPlayer(0)
18-
pl.RawWeapons[1] = &knife
19-
pl.RawWeapons[2] = &glock
20-
pl.RawWeapons[3] = &ak47
18+
pl.RawWeapons[1] = knife
19+
pl.RawWeapons[2] = glock
20+
pl.RawWeapons[3] = ak47
2121
pl.ActiveWeaponID = 3
2222

23-
assert.Equal(t, &ak47, pl.ActiveWeapon(), "Should have AK-47 equipped")
23+
assert.Equal(t, ak47, pl.ActiveWeapon(), "Should have AK-47 equipped")
2424
}
2525

2626
func TestPlayerWeapons(t *testing.T) {
@@ -29,11 +29,11 @@ func TestPlayerWeapons(t *testing.T) {
2929
ak47 := NewEquipment(EqAK47)
3030

3131
pl := newPlayer(0)
32-
pl.RawWeapons[1] = &knife
33-
pl.RawWeapons[2] = &glock
34-
pl.RawWeapons[3] = &ak47
32+
pl.RawWeapons[1] = knife
33+
pl.RawWeapons[2] = glock
34+
pl.RawWeapons[3] = ak47
3535

36-
expected := []*Equipment{&knife, &glock, &ak47}
36+
expected := []*Equipment{knife, glock, ak47}
3737
assert.ElementsMatch(t, expected, pl.Weapons(), "Should have expected weapons")
3838
}
3939

datatables.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,15 @@ func (p *Parser) bindNewPlayer(playerEntity st.IEntity) {
302302
}
303303
cache[i2] = entityID
304304

305-
wep := &p.weapons[entityID]
305+
wep := p.gameState.weapons[entityID]
306+
307+
if wep == nil {
308+
// Something is clearly wrong here
309+
// But since we had an empty Equipment instance here before the change
310+
// from array with default elements to map, let's create a new instance.
311+
wep = new(common.Equipment)
312+
p.gameState.weapons[entityID] = wep
313+
}
306314

307315
// Clear previous owner
308316
if wep.Owner != nil {
@@ -470,13 +478,22 @@ func (p *Parser) nadeProjectileDestroyed(proj *common.GrenadeProjectile) {
470478

471479
func (p *Parser) bindWeapon(entity *st.Entity, wepType common.EquipmentElement) {
472480
entityID := entity.ID()
473-
currentOwner := p.weapons[entityID].Owner
474-
p.weapons[entityID] = common.NewEquipment(wepType)
475-
eq := &p.weapons[entityID]
481+
482+
var currentOwner *common.Player
483+
if wep, ok := p.gameState.weapons[entityID]; ok {
484+
currentOwner = wep.Owner
485+
}
486+
487+
p.gameState.weapons[entityID] = common.NewEquipment(wepType)
488+
eq := p.gameState.weapons[entityID]
476489
eq.Owner = currentOwner
477490
eq.EntityID = entityID
478491
eq.AmmoInMagazine = -1
479492

493+
entity.OnDestroy(func() {
494+
delete(p.gameState.weapons, entityID)
495+
})
496+
480497
entity.FindPropertyI("m_iClip1").OnUpdate(func(val st.PropertyValue) {
481498
eq.AmmoInMagazine = val.IntVal - 1
482499

examples/mocking/mocking_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func kill(wep common.EquipmentElement) events.Kill {
3232
eq := common.NewEquipment(wep)
3333
return events.Kill{
3434
Killer: new(common.Player),
35-
Weapon: &eq,
35+
Weapon: eq,
3636
Victim: new(common.Player),
3737
}
3838
}

fake/game_state.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ func (gs *GameState) Infernos() map[int]*common.Inferno {
5050
return gs.Called().Get(0).(map[int]*common.Inferno)
5151
}
5252

53+
// Weapons is a mock-implementation of IGameState.Weapons().
54+
func (gs *GameState) Weapons() map[int]*common.Equipment {
55+
return gs.Called().Get(0).(map[int]*common.Equipment)
56+
}
57+
5358
// Entities is a mock-implementation of IGameState.Entities().
5459
func (gs *GameState) Entities() map[int]*st.Entity {
5560
return gs.Called().Get(0).(map[int]*st.Entity)

fake/parser_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func kill(wepType common.EquipmentElement) events.Kill {
5353
wep := common.NewEquipment(wepType)
5454
return events.Kill{
5555
Killer: new(common.Player),
56-
Weapon: &wep,
56+
Weapon: wep,
5757
Victim: new(common.Player),
5858
}
5959
}

game_events.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,8 @@ func getPlayerWeapon(player *common.Player, wepType common.EquipmentElement) *co
694694
}
695695

696696
wep := common.NewEquipment(wepType)
697-
return &wep
697+
698+
return wep
698699
}
699700

700701
func mapGameEventData(d *msg.CSVCMsg_GameEventListDescriptorT, e *msg.CSVCMsg_GameEvent) map[string]*msg.CSVCMsg_GameEventKeyT {

game_events_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func TestAddThrownGrenade_NilPlayer(t *testing.T) {
9393

9494
assert.Empty(t, p.gameState.thrownGrenades)
9595

96-
p.gameEventHandler.addThrownGrenade(nil, &he)
96+
p.gameEventHandler.addThrownGrenade(nil, he)
9797

9898
assert.Empty(t, p.gameState.thrownGrenades)
9999
}
@@ -105,11 +105,11 @@ func TestAddThrownGrenade(t *testing.T) {
105105

106106
assert.Empty(t, p.gameState.thrownGrenades)
107107

108-
p.gameEventHandler.addThrownGrenade(pl, &he)
108+
p.gameEventHandler.addThrownGrenade(pl, he)
109109

110110
assert.NotEmpty(t, p.gameState.thrownGrenades)
111111
assert.NotEmpty(t, p.gameState.thrownGrenades[pl])
112-
assert.Equal(t, p.gameState.thrownGrenades[pl][0], &he)
112+
assert.Equal(t, p.gameState.thrownGrenades[pl][0], he)
113113
}
114114

115115
func TestGetThrownGrenade_NilPlayer(t *testing.T) {
@@ -137,11 +137,11 @@ func TestGetThrownGrenade_Found(t *testing.T) {
137137
pl := &common.Player{}
138138
he := common.NewEquipment(common.EqHE)
139139

140-
p.gameEventHandler.addThrownGrenade(pl, &he)
140+
p.gameEventHandler.addThrownGrenade(pl, he)
141141
wep := p.gameEventHandler.getThrownGrenade(pl, he.Weapon)
142142

143143
assert.Equal(t, wep.Weapon, he.Weapon)
144-
assert.Equal(t, wep, &he)
144+
assert.Equal(t, wep, he)
145145
}
146146

147147
func TestDeleteThrownGrenade_NilPlayer(t *testing.T) {
@@ -159,7 +159,7 @@ func TestDeleteThrownGrenade_NotFound(t *testing.T) {
159159

160160
assert.Empty(t, p.gameState.thrownGrenades)
161161

162-
p.gameEventHandler.addThrownGrenade(pl, &he)
162+
p.gameEventHandler.addThrownGrenade(pl, he)
163163

164164
assert.NotEmpty(t, p.gameState.thrownGrenades[pl])
165165

@@ -175,7 +175,7 @@ func TestDeleteThrownGrenade_Found(t *testing.T) {
175175

176176
assert.Empty(t, p.gameState.thrownGrenades)
177177

178-
p.gameEventHandler.addThrownGrenade(pl, &he)
178+
p.gameEventHandler.addThrownGrenade(pl, he)
179179

180180
assert.NotEmpty(t, p.gameState.thrownGrenades[pl])
181181

@@ -207,10 +207,10 @@ func TestGetEquipmentInstance_Grenade_Thrown(t *testing.T) {
207207
pl := &common.Player{}
208208
he := common.NewEquipment(common.EqHE)
209209

210-
p.gameEventHandler.addThrownGrenade(pl, &he)
210+
p.gameEventHandler.addThrownGrenade(pl, he)
211211
wep := p.gameEventHandler.getEquipmentInstance(pl, he.Weapon)
212212

213-
assert.Equal(t, &he, wep)
213+
assert.Equal(t, he, wep)
214214
}
215215

216216
func TestGetCommunityId(t *testing.T) {

game_state.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type GameState struct {
1717
playersByEntityID map[int]*common.Player // Maps entity-IDs to players
1818
grenadeProjectiles map[int]*common.GrenadeProjectile // Maps entity-IDs to active nade-projectiles. That's grenades that have been thrown, but have not yet detonated.
1919
infernos map[int]*common.Inferno // Maps entity-IDs to active infernos.
20+
weapons map[int]*common.Equipment // Maps entity IDs to weapons. Used to remember what a weapon is (p250 / cz etc.)
2021
entities map[int]*st.Entity // Maps entity IDs to entities
2122
conVars map[string]string
2223
bomb common.Bomb
@@ -99,6 +100,11 @@ func (gs GameState) Infernos() map[int]*common.Inferno {
99100
return gs.infernos
100101
}
101102

103+
// Weapons returns a map from entity-IDs to all weapons currently in the game.
104+
func (gs GameState) Weapons() map[int]*common.Equipment {
105+
return gs.weapons
106+
}
107+
102108
// Entities returns all currently existing entities.
103109
// (Almost?) everything in the game is an entity, such as weapons, players, fire etc.
104110
func (gs GameState) Entities() map[int]*st.Entity {
@@ -143,6 +149,7 @@ func newGameState() *GameState {
143149
playersByUserID: make(map[int]*common.Player),
144150
grenadeProjectiles: make(map[int]*common.GrenadeProjectile),
145151
infernos: make(map[int]*common.Inferno),
152+
weapons: make(map[int]*common.Equipment),
146153
entities: make(map[int]*st.Entity),
147154
conVars: make(map[string]string),
148155
thrownGrenades: make(map[*common.Player][]*common.Equipment),
@@ -223,7 +230,7 @@ func (ptcp Participants) All() []*common.Player {
223230
// Connected returns all currently connected players & spectators.
224231
// The returned slice is a snapshot and is not updated on changes.
225232
func (ptcp Participants) Connected() []*common.Player {
226-
res, original := ptcp.initalizeSliceFromByUserID()
233+
res, original := ptcp.initializeSliceFromByUserID()
227234
for _, p := range original {
228235
res = append(res, p)
229236
}
@@ -234,7 +241,7 @@ func (ptcp Participants) Connected() []*common.Player {
234241
// Playing returns all players that aren't spectating or unassigned.
235242
// The returned slice is a snapshot and is not updated on changes.
236243
func (ptcp Participants) Playing() []*common.Player {
237-
res, original := ptcp.initalizeSliceFromByUserID()
244+
res, original := ptcp.initializeSliceFromByUserID()
238245
for _, p := range original {
239246
if p.Team != common.TeamSpectators && p.Team != common.TeamUnassigned {
240247
res = append(res, p)
@@ -247,7 +254,7 @@ func (ptcp Participants) Playing() []*common.Player {
247254
// TeamMembers returns all players belonging to the requested team at this time.
248255
// The returned slice is a snapshot and is not updated on changes.
249256
func (ptcp Participants) TeamMembers(team common.Team) []*common.Player {
250-
res, original := ptcp.initalizeSliceFromByUserID()
257+
res, original := ptcp.initializeSliceFromByUserID()
251258
for _, p := range original {
252259
if p.Team == team {
253260
res = append(res, p)
@@ -281,7 +288,7 @@ func (ptcp Participants) FindByHandle(handle int) *common.Player {
281288
return player
282289
}
283290

284-
func (ptcp Participants) initalizeSliceFromByUserID() ([]*common.Player, map[int]*common.Player) {
291+
func (ptcp Participants) initializeSliceFromByUserID() ([]*common.Player, map[int]*common.Player) {
285292
byUserID := ptcp.ByUserID()
286293
return make([]*common.Player, 0, len(byUserID)), byUserID
287294
}

game_state_interface.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ type IGameState interface {
3737
GrenadeProjectiles() map[int]*common.GrenadeProjectile
3838
// Infernos returns a map from entity-IDs to all currently burning infernos (fires from incendiaries and Molotovs).
3939
Infernos() map[int]*common.Inferno
40+
// Weapons returns a map from entity-IDs to all weapons currently in the game.
41+
Weapons() map[int]*common.Equipment
4042
// Entities returns all currently existing entities.
4143
// (Almost?) everything in the game is an entity, such as weapons, players, fire etc.
4244
Entities() map[int]*st.Entity

0 commit comments

Comments
 (0)