Compare commits

...

2 Commits

Author SHA1 Message Date
CPunch 459b71a109 fix: Chunks are now goroutine safe
- added Chunk.ForEachEntity()
- refactored SendPacketExclude() to use it
- Chunk.Entities is now Chunk.entities, which is private.
- Chunk.AddEntity() && RemoveEntity() now lock the chunk mutex
2023-11-21 01:49:57 -06:00
CPunch 06f4a4d33f minor chunk refactoring
- rename GetChunk -> GetChunkPos
- rename SetChunk -> SetChunkPos
2023-11-21 01:36:23 -06:00
6 changed files with 51 additions and 30 deletions

View File

@ -7,23 +7,29 @@ import (
type Chunk struct {
Position ChunkPosition
Entities map[Entity]struct{}
entities map[Entity]struct{}
lock sync.Mutex
}
func NewChunk(position ChunkPosition) *Chunk {
return &Chunk{
Position: position,
Entities: make(map[Entity]struct{}),
entities: make(map[Entity]struct{}),
}
}
func (c *Chunk) AddEntity(entity Entity) {
c.Entities[entity] = struct{}{}
c.lock.Lock()
defer c.lock.Unlock()
c.entities[entity] = struct{}{}
}
func (c *Chunk) RemoveEntity(entity Entity) {
delete(c.Entities, entity)
c.lock.Lock()
defer c.lock.Unlock()
delete(c.entities, entity)
}
// send packet to all peers in this chunk and kill each peer if error
@ -31,14 +37,23 @@ func (c *Chunk) SendPacket(typeID uint32, pkt ...interface{}) {
c.SendPacketExclude(nil, typeID, pkt...)
}
func (c *Chunk) SendPacketExclude(exclude Entity, typeID uint32, pkt ...interface{}) {
// calls f for each entity in this chunk, if f returns true, stop iterating
func (c *Chunk) ForEachEntity(f func(entity Entity) bool) {
c.lock.Lock()
defer c.lock.Unlock()
for entity := range c.Entities {
for entity := range c.entities {
if f(entity) {
break
}
}
}
func (c *Chunk) SendPacketExclude(exclude Entity, typeID uint32, pkt ...interface{}) {
c.ForEachEntity(func(entity Entity) bool {
// only send to players, and exclude the player that sent the packet
if entity.GetKind() != ENTITY_KIND_PLAYER || entity == exclude {
continue
return false
}
plr, ok := entity.(*Player)
@ -51,7 +66,9 @@ func (c *Chunk) SendPacketExclude(exclude Entity, typeID uint32, pkt ...interfac
log.Printf("Error sending packet to peer %p: %v", peer, err)
peer.Kill()
}
}
return false
})
}
func (c *Chunk) GetAdjacentPositions() []ChunkPosition {

View File

@ -12,11 +12,11 @@ const (
type Entity interface {
GetKind() EntityKind
GetChunk() ChunkPosition
GetChunkPos() ChunkPosition
GetPosition() (x int, y int, z int)
GetAngle() int
SetChunk(chunk ChunkPosition)
SetChunkPos(chunk ChunkPosition)
SetPosition(x, y, z int)
SetAngle(angle int)

View File

@ -36,7 +36,7 @@ func (npc *NPC) GetKind() EntityKind {
return ENTITY_KIND_NPC
}
func (npc *NPC) GetChunk() ChunkPosition {
func (npc *NPC) GetChunkPos() ChunkPosition {
return npc.Chunk
}
@ -48,7 +48,7 @@ func (npc *NPC) GetAngle() int {
return npc.Angle
}
func (npc *NPC) SetChunk(chunk ChunkPosition) {
func (npc *NPC) SetChunkPos(chunk ChunkPosition) {
npc.Chunk = chunk
}

View File

@ -44,7 +44,7 @@ func (plr *Player) GetKind() EntityKind {
return ENTITY_KIND_PLAYER
}
func (plr *Player) GetChunk() ChunkPosition {
func (plr *Player) GetChunkPos() ChunkPosition {
return plr.Chunk
}
@ -56,7 +56,7 @@ func (plr *Player) GetAngle() int {
return plr.Angle
}
func (plr *Player) SetChunk(chunk ChunkPosition) {
func (plr *Player) SetChunkPos(chunk ChunkPosition) {
plr.Chunk = chunk
}

View File

@ -5,15 +5,15 @@ import (
)
func (server *ShardServer) addEntity(e entity.Entity) {
pos := e.GetChunk()
server.addEntityToChunks(server.getViewableChunks(pos), e)
pos := e.GetChunkPos()
server.addEntityToChunks(e, server.getViewableChunks(pos))
server.getChunk(pos).AddEntity(e)
}
func (server *ShardServer) removeEntity(e entity.Entity) {
// TODO: chunk cleanup
pos := e.GetChunk()
server.removeEntityFromChunks(server.getViewableChunks(pos), e)
pos := e.GetChunkPos()
server.removeEntityFromChunks(e, server.getViewableChunks(pos))
server.getChunk(pos).RemoveEntity(e)
}
@ -56,11 +56,11 @@ func (server *ShardServer) sendAllPacket(plr *entity.Player, typeID uint32, pkt
return nil
}
func (server *ShardServer) removeEntityFromChunks(chunks []*entity.Chunk, this entity.Entity) {
func (server *ShardServer) removeEntityFromChunks(this entity.Entity, chunks []*entity.Chunk) {
for _, chunk := range chunks {
for e := range chunk.Entities {
chunk.ForEachEntity(func(e entity.Entity) bool {
if e == this {
continue
return false
}
// notify other players we're leaving
@ -74,15 +74,17 @@ func (server *ShardServer) removeEntityFromChunks(chunks []*entity.Chunk, this e
thisPlr := this.(*entity.Player)
e.DisappearFromViewOf(thisPlr.Peer)
}
}
return false
})
}
}
func (server *ShardServer) addEntityToChunks(chunks []*entity.Chunk, this entity.Entity) {
func (server *ShardServer) addEntityToChunks(this entity.Entity, chunks []*entity.Chunk) {
for _, chunk := range chunks {
for e := range chunk.Entities {
chunk.ForEachEntity(func(e entity.Entity) bool {
if e == this {
continue
return false
}
// notify other players we're entering
@ -96,7 +98,9 @@ func (server *ShardServer) addEntityToChunks(chunks []*entity.Chunk, this entity
thisPlr := this.(*entity.Player)
e.EnterIntoViewOf(thisPlr.Peer)
}
}
return false
})
}
}
@ -114,9 +118,9 @@ func (server *ShardServer) updateEntityChunk(e entity.Entity, from entity.ChunkP
toEnter := entity.ChunkSliceDifference(newViewables, oldViewables)
// update chunks
server.removeEntityFromChunks(toExit, e)
server.addEntityToChunks(toEnter, e)
server.removeEntityFromChunks(e, toExit)
server.addEntityToChunks(e, toEnter)
server.getChunk(from).RemoveEntity(e)
server.getChunk(to).AddEntity(e)
e.SetChunk(to)
e.SetChunkPos(to)
}

View File

@ -10,7 +10,7 @@ func (server *ShardServer) updatePlayerPosition(plr *entity.Player, X, Y, Z, Ang
plr.Y = Y
plr.Z = Z
plr.Angle = Angle
server.updateEntityChunk(plr, plr.GetChunk(), entity.MakeChunkPosition(X, Y))
server.updateEntityChunk(plr, plr.GetChunkPos(), entity.MakeChunkPosition(X, Y))
}
func (server *ShardServer) playerMove(peer *protocol.CNPeer, pkt protocol.Packet) error {