2023-06-25 06:51:21 +00:00
|
|
|
package shard
|
|
|
|
|
2023-06-25 08:33:17 +00:00
|
|
|
import (
|
2023-08-23 23:16:24 +00:00
|
|
|
"github.com/CPunch/gopenfusion/internal/entity"
|
2023-06-25 08:33:17 +00:00
|
|
|
)
|
2023-06-25 06:51:21 +00:00
|
|
|
|
2023-06-27 05:11:00 +00:00
|
|
|
func (server *ShardServer) addEntity(e entity.Entity) {
|
2023-11-21 07:36:23 +00:00
|
|
|
pos := e.GetChunkPos()
|
|
|
|
server.addEntityToChunks(e, server.getViewableChunks(pos))
|
2023-06-27 05:11:00 +00:00
|
|
|
server.getChunk(pos).AddEntity(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (server *ShardServer) removeEntity(e entity.Entity) {
|
2023-06-28 02:49:11 +00:00
|
|
|
// TODO: chunk cleanup
|
2023-11-21 07:36:23 +00:00
|
|
|
pos := e.GetChunkPos()
|
|
|
|
server.removeEntityFromChunks(e, server.getViewableChunks(pos))
|
2023-06-27 05:11:00 +00:00
|
|
|
server.getChunk(pos).RemoveEntity(e)
|
|
|
|
}
|
|
|
|
|
2023-06-25 06:51:21 +00:00
|
|
|
func (server *ShardServer) getChunk(pos entity.ChunkPosition) *entity.Chunk {
|
|
|
|
chunk, ok := server.chunks[pos]
|
|
|
|
if !ok {
|
|
|
|
chunk = entity.NewChunk(pos)
|
|
|
|
server.chunks[pos] = chunk
|
|
|
|
}
|
|
|
|
|
|
|
|
return chunk
|
|
|
|
}
|
|
|
|
|
2023-06-25 08:33:17 +00:00
|
|
|
func (server *ShardServer) getViewableChunks(pos entity.ChunkPosition) []*entity.Chunk {
|
2023-06-25 06:51:21 +00:00
|
|
|
chunks := make([]*entity.Chunk, 0, 9)
|
2023-06-25 08:33:17 +00:00
|
|
|
for _, pos := range server.getChunk(pos).GetAdjacentPositions() {
|
2023-06-25 06:51:21 +00:00
|
|
|
chunks = append(chunks, server.getChunk(pos))
|
|
|
|
}
|
|
|
|
|
|
|
|
return chunks
|
|
|
|
}
|
2023-06-25 08:33:17 +00:00
|
|
|
|
2023-06-25 09:27:42 +00:00
|
|
|
// sends a packet to all peers in the given chunks, excluding the given peer
|
|
|
|
func (server *ShardServer) sendOthersPacket(plr *entity.Player, typeID uint32, pkt ...interface{}) error {
|
|
|
|
chunks := server.getViewableChunks(plr.Chunk)
|
|
|
|
for _, chunk := range chunks {
|
|
|
|
chunk.SendPacketExclude(plr, typeID, pkt...)
|
2023-06-25 08:33:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-28 03:05:08 +00:00
|
|
|
// sends a packet to all peers in the given chunks
|
|
|
|
func (server *ShardServer) sendAllPacket(plr *entity.Player, typeID uint32, pkt ...interface{}) error {
|
|
|
|
chunks := server.getViewableChunks(plr.Chunk)
|
|
|
|
for _, chunk := range chunks {
|
|
|
|
chunk.SendPacket(typeID, pkt...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-21 07:36:23 +00:00
|
|
|
func (server *ShardServer) removeEntityFromChunks(this entity.Entity, chunks []*entity.Chunk) {
|
2023-06-25 08:33:17 +00:00
|
|
|
for _, chunk := range chunks {
|
2023-11-21 07:49:57 +00:00
|
|
|
chunk.ForEachEntity(func(e entity.Entity) bool {
|
2023-06-25 09:27:42 +00:00
|
|
|
if e == this {
|
2023-11-21 07:49:57 +00:00
|
|
|
return false
|
2023-06-25 09:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// notify other players we're leaving
|
2023-06-25 08:33:17 +00:00
|
|
|
if e.GetKind() == entity.ENTITY_KIND_PLAYER {
|
|
|
|
otherPlr := e.(*entity.Player)
|
|
|
|
this.DisappearFromViewOf(otherPlr.Peer)
|
|
|
|
}
|
|
|
|
|
2023-06-25 09:27:42 +00:00
|
|
|
// notify us they're leaving
|
2023-06-25 08:33:17 +00:00
|
|
|
if this.GetKind() == entity.ENTITY_KIND_PLAYER {
|
|
|
|
thisPlr := this.(*entity.Player)
|
|
|
|
e.DisappearFromViewOf(thisPlr.Peer)
|
|
|
|
}
|
2023-11-21 07:49:57 +00:00
|
|
|
|
|
|
|
return false
|
|
|
|
})
|
2023-06-25 08:33:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-21 07:36:23 +00:00
|
|
|
func (server *ShardServer) addEntityToChunks(this entity.Entity, chunks []*entity.Chunk) {
|
2023-06-25 08:33:17 +00:00
|
|
|
for _, chunk := range chunks {
|
2023-11-21 07:49:57 +00:00
|
|
|
chunk.ForEachEntity(func(e entity.Entity) bool {
|
2023-06-25 09:27:42 +00:00
|
|
|
if e == this {
|
2023-11-21 07:49:57 +00:00
|
|
|
return false
|
2023-06-25 09:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// notify other players we're entering
|
2023-06-25 08:33:17 +00:00
|
|
|
if e.GetKind() == entity.ENTITY_KIND_PLAYER {
|
|
|
|
otherPlr := e.(*entity.Player)
|
|
|
|
this.EnterIntoViewOf(otherPlr.Peer)
|
|
|
|
}
|
|
|
|
|
2023-06-25 09:27:42 +00:00
|
|
|
// notify us they're entering
|
2023-06-25 08:33:17 +00:00
|
|
|
if this.GetKind() == entity.ENTITY_KIND_PLAYER {
|
|
|
|
thisPlr := this.(*entity.Player)
|
|
|
|
e.EnterIntoViewOf(thisPlr.Peer)
|
|
|
|
}
|
2023-11-21 07:49:57 +00:00
|
|
|
|
|
|
|
return false
|
|
|
|
})
|
2023-06-25 08:33:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (server *ShardServer) updateEntityChunk(e entity.Entity, from entity.ChunkPosition, to entity.ChunkPosition) {
|
2023-06-25 09:27:42 +00:00
|
|
|
// no change needed
|
|
|
|
if from == to {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-25 08:33:17 +00:00
|
|
|
oldViewables := server.getViewableChunks(from)
|
|
|
|
newViewables := server.getViewableChunks(to)
|
|
|
|
|
|
|
|
// compute differences
|
|
|
|
toExit := entity.ChunkSliceDifference(oldViewables, newViewables)
|
|
|
|
toEnter := entity.ChunkSliceDifference(newViewables, oldViewables)
|
|
|
|
|
|
|
|
// update chunks
|
2023-11-21 07:36:23 +00:00
|
|
|
server.removeEntityFromChunks(e, toExit)
|
|
|
|
server.addEntityToChunks(e, toEnter)
|
2023-06-25 08:33:17 +00:00
|
|
|
server.getChunk(from).RemoveEntity(e)
|
|
|
|
server.getChunk(to).AddEntity(e)
|
2023-11-21 07:36:23 +00:00
|
|
|
e.SetChunkPos(to)
|
2023-06-25 08:33:17 +00:00
|
|
|
}
|