mirror of
https://github.com/CPunch/gopenfusion.git
synced 2025-10-11 03:40:06 +00:00
moved internal/entity to shard/entity
This commit is contained in:
@@ -4,8 +4,8 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/CPunch/gopenfusion/cnpeer"
|
||||
"github.com/CPunch/gopenfusion/internal/entity"
|
||||
"github.com/CPunch/gopenfusion/internal/protocol"
|
||||
"github.com/CPunch/gopenfusion/shard/entity"
|
||||
)
|
||||
|
||||
func (server *ShardServer) freeChat(peer *cnpeer.CNPeer, pkt protocol.Packet) error {
|
||||
|
101
shard/entity/chunk.go
Normal file
101
shard/entity/chunk.go
Normal file
@@ -0,0 +1,101 @@
|
||||
package entity
|
||||
|
||||
import (
|
||||
"log"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type Chunk struct {
|
||||
Position ChunkPosition
|
||||
entities map[Entity]struct{}
|
||||
lock sync.Mutex
|
||||
}
|
||||
|
||||
func NewChunk(position ChunkPosition) *Chunk {
|
||||
return &Chunk{
|
||||
Position: position,
|
||||
entities: make(map[Entity]struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Chunk) AddEntity(entity Entity) {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
|
||||
c.entities[entity] = struct{}{}
|
||||
}
|
||||
|
||||
func (c *Chunk) RemoveEntity(entity 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
|
||||
func (c *Chunk) SendPacket(typeID uint32, pkt ...interface{}) {
|
||||
c.SendPacketExclude(nil, typeID, pkt...)
|
||||
}
|
||||
|
||||
// calls f for each entity in this chunk, if f returns true, stop iterating
|
||||
// f can safely add/remove entities from the chunk
|
||||
func (c *Chunk) ForEachEntity(f func(entity Entity) bool) {
|
||||
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 {
|
||||
return false
|
||||
}
|
||||
|
||||
plr, ok := entity.(*Player)
|
||||
if !ok {
|
||||
log.Panic("Chunk.SendPacket: entity kind was player, but is not a *Player")
|
||||
}
|
||||
peer := plr.Peer
|
||||
|
||||
if err := peer.Send(typeID, pkt...); err != nil {
|
||||
log.Printf("Error sending packet to peer %p: %v", peer, err)
|
||||
peer.Kill()
|
||||
}
|
||||
|
||||
return false
|
||||
})
|
||||
}
|
||||
|
||||
func (c *Chunk) GetAdjacentPositions() []ChunkPosition {
|
||||
return []ChunkPosition{
|
||||
{c.Position.X - 1, c.Position.Y - 1},
|
||||
{c.Position.X - 1, c.Position.Y},
|
||||
{c.Position.X - 1, c.Position.Y + 1},
|
||||
{c.Position.X, c.Position.Y - 1},
|
||||
{c.Position.X, c.Position.Y},
|
||||
{c.Position.X, c.Position.Y + 1},
|
||||
{c.Position.X + 1, c.Position.Y - 1},
|
||||
{c.Position.X + 1, c.Position.Y},
|
||||
{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
shard/entity/chunkposition.go
Normal file
15
shard/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),
|
||||
}
|
||||
}
|
25
shard/entity/entity.go
Normal file
25
shard/entity/entity.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package entity
|
||||
|
||||
import "github.com/CPunch/gopenfusion/cnpeer"
|
||||
|
||||
type EntityKind int
|
||||
|
||||
const (
|
||||
ENTITY_KIND_PLAYER EntityKind = iota
|
||||
ENTITY_KIND_NPC
|
||||
)
|
||||
|
||||
type Entity interface {
|
||||
GetKind() EntityKind
|
||||
|
||||
GetChunkPos() ChunkPosition
|
||||
GetPosition() (x int, y int, z int)
|
||||
GetAngle() int
|
||||
|
||||
SetChunkPos(chunk ChunkPosition)
|
||||
SetPosition(x, y, z int)
|
||||
SetAngle(angle int)
|
||||
|
||||
DisappearFromViewOf(peer *cnpeer.CNPeer)
|
||||
EnterIntoViewOf(peer *cnpeer.CNPeer)
|
||||
}
|
36
shard/entity/entity_test.go
Normal file
36
shard/entity/entity_test.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package entity_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/CPunch/gopenfusion/shard/entity"
|
||||
"github.com/matryer/is"
|
||||
)
|
||||
|
||||
func TestChunkSliceDifference(t *testing.T) {
|
||||
is := is.New(t)
|
||||
chunks := []*entity.Chunk{
|
||||
entity.NewChunk(entity.MakeChunkPosition(0, 0)),
|
||||
entity.NewChunk(entity.MakeChunkPosition(0, 1)),
|
||||
entity.NewChunk(entity.MakeChunkPosition(1, 0)),
|
||||
entity.NewChunk(entity.MakeChunkPosition(1, 1)),
|
||||
}
|
||||
|
||||
c1 := []*entity.Chunk{
|
||||
chunks[0],
|
||||
chunks[1],
|
||||
chunks[2],
|
||||
chunks[3],
|
||||
}
|
||||
|
||||
c2 := []*entity.Chunk{
|
||||
chunks[0],
|
||||
chunks[1],
|
||||
chunks[2],
|
||||
}
|
||||
|
||||
diff := entity.ChunkSliceDifference(c1, c2)
|
||||
|
||||
is.True(len(diff) == 1) // should be 1 chunk in difference
|
||||
is.True(diff[0] == chunks[3]) // should be chunks[3] in difference
|
||||
}
|
88
shard/entity/npc.go
Normal file
88
shard/entity/npc.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package entity
|
||||
|
||||
import (
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/CPunch/gopenfusion/cnpeer"
|
||||
"github.com/CPunch/gopenfusion/internal/protocol"
|
||||
)
|
||||
|
||||
type NPC struct {
|
||||
ID int
|
||||
X int `json:"iX"`
|
||||
Y int `json:"iY"`
|
||||
Z int `json:"iZ"`
|
||||
Angle int `json:"iAngle"`
|
||||
NPCType int `json:"iNPCType"`
|
||||
Chunk ChunkPosition
|
||||
}
|
||||
|
||||
var nextNPCID = &atomic.Int32{}
|
||||
|
||||
func NewNPC(X, Y, Z, Angle int, npcType int) *NPC {
|
||||
return &NPC{
|
||||
ID: int(nextNPCID.Add(1)),
|
||||
X: X,
|
||||
Y: Y,
|
||||
Z: Z,
|
||||
Angle: Angle,
|
||||
NPCType: npcType,
|
||||
Chunk: MakeChunkPosition(X, Y),
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== Entity interface ====================
|
||||
|
||||
func (npc *NPC) GetKind() EntityKind {
|
||||
return ENTITY_KIND_NPC
|
||||
}
|
||||
|
||||
func (npc *NPC) GetChunkPos() ChunkPosition {
|
||||
return npc.Chunk
|
||||
}
|
||||
|
||||
func (npc *NPC) GetPosition() (x int, y int, z int) {
|
||||
return npc.X, npc.Y, npc.Z
|
||||
}
|
||||
|
||||
func (npc *NPC) GetAngle() int {
|
||||
return npc.Angle
|
||||
}
|
||||
|
||||
func (npc *NPC) SetChunkPos(chunk ChunkPosition) {
|
||||
npc.Chunk = chunk
|
||||
}
|
||||
|
||||
func (npc *NPC) SetPosition(x, y, z int) {
|
||||
npc.X = x
|
||||
npc.Y = y
|
||||
npc.Z = z
|
||||
}
|
||||
|
||||
func (npc *NPC) SetAngle(angle int) {
|
||||
npc.Angle = angle
|
||||
}
|
||||
|
||||
func (npc *NPC) DisappearFromViewOf(peer *cnpeer.CNPeer) {
|
||||
peer.Send(protocol.P_FE2CL_NPC_EXIT, protocol.SP_FE2CL_NPC_EXIT{
|
||||
INPC_ID: int32(npc.ID),
|
||||
})
|
||||
}
|
||||
|
||||
func (npc *NPC) EnterIntoViewOf(peer *cnpeer.CNPeer) {
|
||||
peer.Send(protocol.P_FE2CL_NPC_NEW, protocol.SP_FE2CL_NPC_NEW{
|
||||
NPCAppearanceData: npc.GetAppearanceData(),
|
||||
})
|
||||
}
|
||||
|
||||
func (npc *NPC) GetAppearanceData() protocol.SNPCAppearanceData {
|
||||
return protocol.SNPCAppearanceData{
|
||||
INPC_ID: int32(npc.ID),
|
||||
INPCType: int32(npc.NPCType),
|
||||
IHP: 100,
|
||||
IX: int32(npc.X),
|
||||
IY: int32(npc.Y),
|
||||
IZ: int32(npc.Z),
|
||||
IAngle: int32(npc.Angle),
|
||||
}
|
||||
}
|
109
shard/entity/player.go
Normal file
109
shard/entity/player.go
Normal file
@@ -0,0 +1,109 @@
|
||||
package entity
|
||||
|
||||
import (
|
||||
"github.com/CPunch/gopenfusion/cnpeer"
|
||||
"github.com/CPunch/gopenfusion/internal/db"
|
||||
"github.com/CPunch/gopenfusion/internal/protocol"
|
||||
)
|
||||
|
||||
type Player struct {
|
||||
db.Player
|
||||
Peer *cnpeer.CNPeer
|
||||
Chunk ChunkPosition
|
||||
}
|
||||
|
||||
func NewPlayer(peer *cnpeer.CNPeer, player *db.Player) *Player {
|
||||
return &Player{
|
||||
Player: *player,
|
||||
Peer: peer,
|
||||
Chunk: MakeChunkPosition(player.X, player.Y),
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== Entity interface ====================
|
||||
|
||||
func (plr *Player) GetKind() EntityKind {
|
||||
return ENTITY_KIND_PLAYER
|
||||
}
|
||||
|
||||
func (plr *Player) GetChunkPos() ChunkPosition {
|
||||
return plr.Chunk
|
||||
}
|
||||
|
||||
func (plr *Player) GetPosition() (x int, y int, z int) {
|
||||
return plr.X, plr.Y, plr.Z
|
||||
}
|
||||
|
||||
func (plr *Player) GetAngle() int {
|
||||
return plr.Angle
|
||||
}
|
||||
|
||||
func (plr *Player) SetChunkPos(chunk ChunkPosition) {
|
||||
plr.Chunk = chunk
|
||||
}
|
||||
|
||||
func (plr *Player) SetPosition(x, y, z int) {
|
||||
plr.X = x
|
||||
plr.Y = y
|
||||
plr.Z = z
|
||||
}
|
||||
|
||||
func (plr *Player) SetAngle(angle int) {
|
||||
plr.Angle = angle
|
||||
}
|
||||
|
||||
func (plr *Player) DisappearFromViewOf(peer *cnpeer.CNPeer) {
|
||||
peer.Send(protocol.P_FE2CL_PC_EXIT, protocol.SP_FE2CL_PC_EXIT{
|
||||
IID: int32(plr.PlayerID),
|
||||
})
|
||||
}
|
||||
|
||||
func (plr *Player) EnterIntoViewOf(peer *cnpeer.CNPeer) {
|
||||
peer.Send(protocol.P_FE2CL_PC_NEW, protocol.SP_FE2CL_PC_NEW{
|
||||
PCAppearanceData: plr.GetAppearanceData(),
|
||||
})
|
||||
}
|
||||
|
||||
func (plr *Player) ToPCLoadData2CL() protocol.SPCLoadData2CL {
|
||||
return protocol.SPCLoadData2CL{
|
||||
IUserLevel: int16(plr.AccountLevel),
|
||||
PCStyle: plr.PCStyle,
|
||||
PCStyle2: plr.PCStyle2,
|
||||
IMentor: int16(plr.Mentor),
|
||||
IMentorCount: 1,
|
||||
IHP: int32(plr.HP),
|
||||
IBatteryW: int32(plr.BatteryW),
|
||||
IBatteryN: int32(plr.BatteryN),
|
||||
ICandy: int32(plr.Taros),
|
||||
IFusionMatter: int32(plr.FusionMatter),
|
||||
ISpecialState: 0,
|
||||
IMapNum: 0,
|
||||
IX: int32(plr.X),
|
||||
IY: int32(plr.Y),
|
||||
IZ: int32(plr.Z),
|
||||
IAngle: int32(plr.Angle),
|
||||
AEquip: plr.Equip,
|
||||
AInven: plr.Inven,
|
||||
ANanoSlots: [3]int16{int16(plr.EquippedNanos[0]), int16(plr.EquippedNanos[1]), int16(plr.EquippedNanos[2])},
|
||||
IActiveNanoSlotNum: int16(plr.ActiveNanoSlotNum),
|
||||
IWarpLocationFlag: int32(plr.WarpLocationFlag),
|
||||
IBuddyWarpTime: 60,
|
||||
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: plr.Nanos[plr.ActiveNanoSlotNum],
|
||||
}
|
||||
}
|
@@ -1,7 +1,7 @@
|
||||
package shard
|
||||
|
||||
import (
|
||||
"github.com/CPunch/gopenfusion/internal/entity"
|
||||
"github.com/CPunch/gopenfusion/shard/entity"
|
||||
)
|
||||
|
||||
func (server *ShardServer) addEntity(e entity.Entity) {
|
||||
|
@@ -5,9 +5,9 @@ import (
|
||||
"log"
|
||||
|
||||
"github.com/CPunch/gopenfusion/cnpeer"
|
||||
"github.com/CPunch/gopenfusion/internal/entity"
|
||||
"github.com/CPunch/gopenfusion/internal/protocol"
|
||||
"github.com/CPunch/gopenfusion/internal/redis"
|
||||
"github.com/CPunch/gopenfusion/shard/entity"
|
||||
"github.com/CPunch/gopenfusion/util"
|
||||
)
|
||||
|
||||
|
@@ -4,8 +4,8 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/CPunch/gopenfusion/cnpeer"
|
||||
"github.com/CPunch/gopenfusion/internal/entity"
|
||||
"github.com/CPunch/gopenfusion/internal/protocol"
|
||||
"github.com/CPunch/gopenfusion/shard/entity"
|
||||
"github.com/CPunch/gopenfusion/util"
|
||||
)
|
||||
|
||||
|
@@ -6,7 +6,7 @@ import (
|
||||
"os"
|
||||
|
||||
"github.com/CPunch/gopenfusion/config"
|
||||
"github.com/CPunch/gopenfusion/internal/entity"
|
||||
"github.com/CPunch/gopenfusion/shard/entity"
|
||||
)
|
||||
|
||||
type NPCData struct {
|
||||
|
@@ -6,10 +6,10 @@ import (
|
||||
"github.com/CPunch/gopenfusion/cnpeer"
|
||||
"github.com/CPunch/gopenfusion/config"
|
||||
"github.com/CPunch/gopenfusion/internal/db"
|
||||
"github.com/CPunch/gopenfusion/internal/entity"
|
||||
"github.com/CPunch/gopenfusion/internal/protocol"
|
||||
"github.com/CPunch/gopenfusion/internal/redis"
|
||||
"github.com/CPunch/gopenfusion/internal/service"
|
||||
"github.com/CPunch/gopenfusion/shard/entity"
|
||||
)
|
||||
|
||||
type PacketHandler func(peer *cnpeer.CNPeer, pkt protocol.Packet) error
|
||||
|
Reference in New Issue
Block a user