god forgive me for this commit

This commit is contained in:
2023-06-25 01:51:21 -05:00
parent dfc00bcb52
commit f0b9bc6ed6
13 changed files with 327 additions and 96 deletions

79
core/entity/chunk.go Normal file
View File

@@ -0,0 +1,79 @@
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{}
lock sync.Mutex
}
func NewChunk(position ChunkPosition) *Chunk {
return &Chunk{
Position: position,
entities: make(map[Entity]struct{}),
}
}
func (c *Chunk) AddEntity(entity Entity) {
entity.SetChunk(c)
c.entities[entity] = struct{}{}
}
func (c *Chunk) RemoveEntity(entity Entity) {
entity.SetChunk(nil)
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.lock.Lock()
defer c.lock.Unlock()
for entity := range c.entities {
if entity.GetKind() != ENTITY_KIND_PLAYER {
continue
}
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 {
peer.Kill()
}
}
}
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},
}
}

20
core/entity/entity.go Normal file
View File

@@ -0,0 +1,20 @@
package entity
type EntityKind int
const (
ENTITY_KIND_PLAYER EntityKind = iota
ENTITY_KIND_NPC
)
type Entity interface {
GetKind() EntityKind
GetChunk() *Chunk
GetPosition() (x int, y int, z int)
GetAngle() int
SetChunk(chunk *Chunk)
SetPosition(x, y, z int)
SetAngle(angle int)
}

98
core/entity/player.go Normal file
View File

@@ -0,0 +1,98 @@
package entity
import (
"github.com/CPunch/gopenfusion/config"
"github.com/CPunch/gopenfusion/core/protocol"
)
type Player struct {
Peer *protocol.CNPeer
CurrentChunk *Chunk
PlayerID int
AccountID int
AccountLevel int
Slot int
PCStyle protocol.SPCStyle
PCStyle2 protocol.SPCStyle2
EquippedNanos [3]int
Nanos [config.NANO_COUNT]protocol.SNano
Equip [config.AEQUIP_COUNT]protocol.SItemBase
Inven [config.AINVEN_COUNT]protocol.SItemBase
Bank [config.ABANK_COUNT]protocol.SItemBase
SkywayLocationFlag []byte
FirstUseFlag []byte
Quests []byte
HP int
Level int
Taros int
FusionMatter int
Mentor int
X, Y, Z int
Angle int
BatteryN int
BatteryW int
WarpLocationFlag int
ActiveNanoSlotNum int
Fatigue int
CurrentMissionID int
}
// ==================== Entity interface ====================
func (plr *Player) GetKind() EntityKind {
return ENTITY_KIND_PLAYER
}
func (plr *Player) GetChunk() *Chunk {
return plr.CurrentChunk
}
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) 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) SetChunk(chunk *Chunk) {
plr.CurrentChunk = chunk
}
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,
}
}