mirror of
https://github.com/CPunch/gopenfusion.git
synced 2025-11-09 17:10:07 +00:00
started chunking
This commit is contained in:
@@ -3,43 +3,27 @@ package entity
|
||||
import (
|
||||
"log"
|
||||
"sync"
|
||||
|
||||
"github.com/CPunch/gopenfusion/config"
|
||||
)
|
||||
|
||||
type ChunkPosition struct {
|
||||
X int
|
||||
Y int
|
||||
}
|
||||
|
||||
func makeChunkPosition(x, y int) ChunkPosition {
|
||||
return ChunkPosition{
|
||||
X: x / (config.VIEW_DISTANCE / 3),
|
||||
Y: y / (config.VIEW_DISTANCE / 3),
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
entity.SetChunk(c)
|
||||
c.entities[entity] = struct{}{}
|
||||
c.Entities[entity] = struct{}{}
|
||||
}
|
||||
|
||||
func (c *Chunk) RemoveEntity(entity Entity) {
|
||||
entity.SetChunk(nil)
|
||||
delete(c.entities, entity)
|
||||
delete(c.Entities, entity)
|
||||
}
|
||||
|
||||
// send packet to all peers in this chunk and kill each peer if error
|
||||
@@ -47,7 +31,7 @@ func (c *Chunk) SendPacket(typeID uint32, pkt ...interface{}) {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
|
||||
for entity := range c.entities {
|
||||
for entity := range c.Entities {
|
||||
if entity.GetKind() != ENTITY_KIND_PLAYER {
|
||||
continue
|
||||
}
|
||||
@@ -59,6 +43,7 @@ func (c *Chunk) SendPacket(typeID uint32, pkt ...interface{}) {
|
||||
peer := plr.Peer
|
||||
|
||||
if err := peer.Send(typeID, pkt...); err != nil {
|
||||
log.Printf("Error sending packet to peer %p: %v", peer, err)
|
||||
peer.Kill()
|
||||
}
|
||||
}
|
||||
@@ -77,3 +62,20 @@ func (c *Chunk) GetAdjacentPositions() []ChunkPosition {
|
||||
{c.Position.X + 1, c.Position.Y + 1},
|
||||
}
|
||||
}
|
||||
|
||||
// https://stackoverflow.com/a/45428032 lol
|
||||
func ChunkSliceDifference(a, b []*Chunk) []*Chunk {
|
||||
m := make(map[*Chunk]struct{})
|
||||
for _, item := range b {
|
||||
m[item] = struct{}{}
|
||||
}
|
||||
|
||||
var diff []*Chunk
|
||||
for _, item := range a {
|
||||
if _, ok := m[item]; !ok {
|
||||
diff = append(diff, item)
|
||||
}
|
||||
}
|
||||
|
||||
return diff
|
||||
}
|
||||
|
||||
15
core/entity/chunkposition.go
Normal file
15
core/entity/chunkposition.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package entity
|
||||
|
||||
import "github.com/CPunch/gopenfusion/config"
|
||||
|
||||
type ChunkPosition struct {
|
||||
X int
|
||||
Y int
|
||||
}
|
||||
|
||||
func MakeChunkPosition(x, y int) ChunkPosition {
|
||||
return ChunkPosition{
|
||||
X: x / (config.VIEW_DISTANCE / 3),
|
||||
Y: y / (config.VIEW_DISTANCE / 3),
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package entity
|
||||
|
||||
import "github.com/CPunch/gopenfusion/core/protocol"
|
||||
|
||||
type EntityKind int
|
||||
|
||||
const (
|
||||
@@ -10,11 +12,14 @@ const (
|
||||
type Entity interface {
|
||||
GetKind() EntityKind
|
||||
|
||||
GetChunk() *Chunk
|
||||
GetChunk() ChunkPosition
|
||||
GetPosition() (x int, y int, z int)
|
||||
GetAngle() int
|
||||
|
||||
SetChunk(chunk *Chunk)
|
||||
SetChunk(chunk ChunkPosition)
|
||||
SetPosition(x, y, z int)
|
||||
SetAngle(angle int)
|
||||
|
||||
DisappearFromViewOf(peer *protocol.CNPeer)
|
||||
EnterIntoViewOf(peer *protocol.CNPeer)
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
|
||||
type Player struct {
|
||||
Peer *protocol.CNPeer
|
||||
CurrentChunk *Chunk
|
||||
Chunk ChunkPosition
|
||||
PlayerID int
|
||||
AccountID int
|
||||
AccountLevel int
|
||||
@@ -35,6 +35,7 @@ type Player struct {
|
||||
ActiveNanoSlotNum int
|
||||
Fatigue int
|
||||
CurrentMissionID int
|
||||
IPCState int8
|
||||
}
|
||||
|
||||
// ==================== Entity interface ====================
|
||||
@@ -43,8 +44,8 @@ func (plr *Player) GetKind() EntityKind {
|
||||
return ENTITY_KIND_PLAYER
|
||||
}
|
||||
|
||||
func (plr *Player) GetChunk() *Chunk {
|
||||
return plr.CurrentChunk
|
||||
func (plr *Player) GetChunk() ChunkPosition {
|
||||
return plr.Chunk
|
||||
}
|
||||
|
||||
func (plr *Player) GetPosition() (x int, y int, z int) {
|
||||
@@ -55,6 +56,10 @@ func (plr *Player) GetAngle() int {
|
||||
return plr.Angle
|
||||
}
|
||||
|
||||
func (plr *Player) SetChunk(chunk ChunkPosition) {
|
||||
plr.Chunk = chunk
|
||||
}
|
||||
|
||||
func (plr *Player) SetPosition(x, y, z int) {
|
||||
plr.X = x
|
||||
plr.Y = y
|
||||
@@ -65,8 +70,16 @@ func (plr *Player) SetAngle(angle int) {
|
||||
plr.Angle = angle
|
||||
}
|
||||
|
||||
func (plr *Player) SetChunk(chunk *Chunk) {
|
||||
plr.CurrentChunk = chunk
|
||||
func (plr *Player) DisappearFromViewOf(peer *protocol.CNPeer) {
|
||||
peer.Send(protocol.P_FE2CL_PC_EXIT, protocol.SP_FE2CL_PC_EXIT{
|
||||
IID: int32(plr.PlayerID),
|
||||
})
|
||||
}
|
||||
|
||||
func (plr *Player) EnterIntoViewOf(peer *protocol.CNPeer) {
|
||||
peer.Send(protocol.P_FE2CL_PC_NEW, protocol.SP_FE2CL_PC_NEW{
|
||||
PCAppearanceData: plr.GetAppearanceData(),
|
||||
})
|
||||
}
|
||||
|
||||
func (plr *Player) ToPCLoadData2CL() protocol.SPCLoadData2CL {
|
||||
@@ -96,3 +109,19 @@ func (plr *Player) ToPCLoadData2CL() protocol.SPCLoadData2CL {
|
||||
IFatigue: 50,
|
||||
}
|
||||
}
|
||||
|
||||
func (plr *Player) GetAppearanceData() protocol.SPCAppearanceData {
|
||||
return protocol.SPCAppearanceData{
|
||||
IID: int32(plr.PlayerID),
|
||||
IHP: int32(plr.HP),
|
||||
ILv: int16(plr.Level),
|
||||
IX: int32(plr.X),
|
||||
IY: int32(plr.Y),
|
||||
IZ: int32(plr.Z),
|
||||
IAngle: int32(plr.Angle),
|
||||
PCStyle: plr.PCStyle,
|
||||
IPCState: plr.IPCState,
|
||||
ItemEquip: plr.Equip,
|
||||
Nano: protocol.SNano{}, //plr.Nanos[plr.ActiveNanoSlotNum],
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user