mirror of
https://github.com/FlourishingWorld/hk4e.git
synced 2026-02-04 15:32:26 +08:00
寻路服务器
This commit is contained in:
70
pathfinding/handle/handle.go
Normal file
70
pathfinding/handle/handle.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package handle
|
||||
|
||||
import (
|
||||
"hk4e/common/mq"
|
||||
"hk4e/pathfinding/world"
|
||||
"hk4e/pkg/logger"
|
||||
"hk4e/protocol/cmd"
|
||||
|
||||
pb "google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
type Handle struct {
|
||||
worldStatic *world.WorldStatic
|
||||
messageQueue *mq.MessageQueue
|
||||
}
|
||||
|
||||
func NewHandle(messageQueue *mq.MessageQueue) (r *Handle) {
|
||||
r = new(Handle)
|
||||
r.worldStatic = world.NewWorldStatic()
|
||||
r.worldStatic.InitTerrain()
|
||||
r.messageQueue = messageQueue
|
||||
go r.run()
|
||||
return r
|
||||
}
|
||||
|
||||
func (h *Handle) run() {
|
||||
for i := 0; i < 4; i++ {
|
||||
go func() {
|
||||
for {
|
||||
netMsg := <-h.messageQueue.GetNetMsg()
|
||||
if netMsg.MsgType != mq.MsgTypeGame {
|
||||
continue
|
||||
}
|
||||
if netMsg.EventId != mq.NormalMsg {
|
||||
continue
|
||||
}
|
||||
gameMsg := netMsg.GameMsg
|
||||
switch gameMsg.CmdId {
|
||||
case cmd.QueryPathReq:
|
||||
h.QueryPath(gameMsg.UserId, gameMsg.PayloadMessage)
|
||||
case cmd.ObstacleModifyNotify:
|
||||
h.ObstacleModifyNotify(gameMsg.UserId, gameMsg.PayloadMessage)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
// SendMsg 发送消息给客户端
|
||||
func (h *Handle) SendMsg(cmdId uint16, userId uint32, payloadMsg pb.Message) {
|
||||
if userId < 100000000 || payloadMsg == nil {
|
||||
return
|
||||
}
|
||||
gameMsg := new(mq.GameMsg)
|
||||
gameMsg.UserId = userId
|
||||
gameMsg.CmdId = cmdId
|
||||
gameMsg.ClientSeq = 0
|
||||
// 在这里直接序列化成二进制数据 防止发送的消息内包含各种游戏数据指针 而造成并发读写的问题
|
||||
payloadMessageData, err := pb.Marshal(payloadMsg)
|
||||
if err != nil {
|
||||
logger.Error("parse payload msg to bin error: %v", err)
|
||||
return
|
||||
}
|
||||
gameMsg.PayloadMessageData = payloadMessageData
|
||||
h.messageQueue.SendToGate("1", &mq.NetMsg{
|
||||
MsgType: mq.MsgTypeGame,
|
||||
EventId: mq.NormalMsg,
|
||||
GameMsg: gameMsg,
|
||||
})
|
||||
}
|
||||
74
pathfinding/handle/query_path.go
Normal file
74
pathfinding/handle/query_path.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package handle
|
||||
|
||||
import (
|
||||
"hk4e/pathfinding/pfalg"
|
||||
"hk4e/pkg/logger"
|
||||
"hk4e/protocol/cmd"
|
||||
"hk4e/protocol/proto"
|
||||
|
||||
pb "google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
func (h *Handle) ConvPbVecToMeshVec(pbVec *proto.Vector) pfalg.MeshVector {
|
||||
return pfalg.MeshVector{
|
||||
X: int16(pbVec.X),
|
||||
Y: int16(pbVec.Y),
|
||||
Z: int16(pbVec.Z),
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handle) ConvMeshVecToPbVec(meshVec pfalg.MeshVector) *proto.Vector {
|
||||
return &proto.Vector{
|
||||
X: float32(meshVec.X),
|
||||
Y: float32(meshVec.Y),
|
||||
Z: float32(meshVec.Z),
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handle) ConvPbVecListToMeshVecList(pbVecList []*proto.Vector) []pfalg.MeshVector {
|
||||
ret := make([]pfalg.MeshVector, 0)
|
||||
for _, pbVec := range pbVecList {
|
||||
ret = append(ret, h.ConvPbVecToMeshVec(pbVec))
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func (h *Handle) ConvMeshVecListToPbVecList(meshVecList []pfalg.MeshVector) []*proto.Vector {
|
||||
ret := make([]*proto.Vector, 0)
|
||||
for _, meshVec := range meshVecList {
|
||||
ret = append(ret, h.ConvMeshVecToPbVec(meshVec))
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func (h *Handle) QueryPath(userId uint32, payloadMsg pb.Message) {
|
||||
req := payloadMsg.(*proto.QueryPathReq)
|
||||
logger.Debug("query path req: %v, uid: %v", req, userId)
|
||||
var ok = false
|
||||
var path []pfalg.MeshVector = nil
|
||||
for _, destinationPos := range req.DestinationPos {
|
||||
ok, path = h.worldStatic.Pathfinding(h.ConvPbVecToMeshVec(req.SourcePos), h.ConvPbVecToMeshVec(destinationPos))
|
||||
if ok {
|
||||
break
|
||||
}
|
||||
}
|
||||
if !ok {
|
||||
queryPathRsp := &proto.QueryPathRsp{
|
||||
QueryId: req.QueryId,
|
||||
QueryStatus: proto.QueryPathRsp_PATH_STATUS_TYPE_FAIL,
|
||||
}
|
||||
h.SendMsg(cmd.QueryPathRsp, userId, queryPathRsp)
|
||||
return
|
||||
}
|
||||
queryPathRsp := &proto.QueryPathRsp{
|
||||
QueryId: req.QueryId,
|
||||
QueryStatus: proto.QueryPathRsp_PATH_STATUS_TYPE_SUCC,
|
||||
Corners: h.ConvMeshVecListToPbVecList(path),
|
||||
}
|
||||
h.SendMsg(cmd.QueryPathRsp, userId, queryPathRsp)
|
||||
}
|
||||
|
||||
func (h *Handle) ObstacleModifyNotify(userId uint32, payloadMsg pb.Message) {
|
||||
req := payloadMsg.(*proto.ObstacleModifyNotify)
|
||||
logger.Debug("obstacle modify req: %v, uid: %v", req, userId)
|
||||
}
|
||||
Reference in New Issue
Block a user