actor_pathfind.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package mapPathfind
  2. import (
  3. "f1-game/internal/algorithm/astar6"
  4. "f1-game/internal/code"
  5. "f1-game/internal/constant"
  6. "f1-game/internal/data"
  7. "f1-game/internal/enum"
  8. nameActor "f1-game/internal/name/actor"
  9. nameRemote "f1-game/internal/name/remote"
  10. "f1-game/internal/pb"
  11. "f1-game/internal/types"
  12. mapTypes "f1-game/nodes/map/internal/types"
  13. clog "github.com/cherry-game/cherry/logger"
  14. cactor "github.com/cherry-game/cherry/net/actor"
  15. )
  16. type (
  17. // actorPathfind 寻路子Actor,计划开启多个实例轮询调用
  18. actorPathfind struct {
  19. cactor.Base
  20. barrierData mapTypes.BarrierData // 动态障碍数据
  21. astar *astar6.AStar // a星寻路
  22. }
  23. )
  24. func newActorPathfind(barrierData mapTypes.BarrierData) *actorPathfind {
  25. var (
  26. width = data.MapInfo().Width
  27. height = data.MapInfo().Height
  28. )
  29. if width < 0 || height < 0 {
  30. clog.Errorf("map width or height is invalid. width = %v, height = %v", width, height)
  31. return nil
  32. }
  33. // 加载寻路数据
  34. tileData := make([][]int32, width)
  35. for x := range width {
  36. tileData[x] = make([]int32, height)
  37. for y := range height {
  38. tileID, ok := data.PointToTileID(int32(x), int32(y))
  39. if !ok {
  40. continue
  41. }
  42. tile := data.MapData().Tiles[tileID]
  43. if tile != nil && !tile.IsBlock() {
  44. tileData[x][y] = int32(tile.BlockType)
  45. }
  46. }
  47. }
  48. actor := &actorPathfind{
  49. barrierData: barrierData.Clone(),
  50. }
  51. actor.astar = astar6.New(astar6.Config{
  52. Min: types.Point{
  53. X: 0,
  54. Y: 0,
  55. },
  56. Max: types.Point{
  57. X: width - 1,
  58. Y: height - 1,
  59. },
  60. MapData: tileData,
  61. IsWalkableFunc: actor.isWalkable,
  62. })
  63. return actor
  64. }
  65. func (p *actorPathfind) AliasID() string {
  66. return nameActor.Map_Pathfind
  67. }
  68. func (p *actorPathfind) OnInit() {
  69. p.Remote().Register(nameRemote.MapPathFind_Find, p.findPath)
  70. p.Remote().Register(nameRemote.MapPathFind_FindMulti, p.findMultiPath)
  71. p.Remote().Register(nameRemote.MapPathFind_SetBarriers, p.setBarriers)
  72. p.Remote().Register(nameRemote.MapPathFind_RemoveBarriers, p.removeBarriers)
  73. }
  74. // 计算寻路
  75. func (p *actorPathfind) findPath(req *pb.FindPathRequest) (*pb.Points, int32) {
  76. ctx := astar6.NewFindContext(req.StartX, req.StartY, req.EndX, req.EndY, req.PlayerID, req.LeagueID, req.MarchType)
  77. nodes, ok := p.astar.FindPath(ctx)
  78. if !ok {
  79. return nil, code.MapPathFindFail
  80. }
  81. path := &pb.Points{}
  82. for _, node := range nodes {
  83. path.List = append(path.List, &pb.Point{
  84. X: node.X(),
  85. Y: node.Y(),
  86. })
  87. }
  88. return path, code.OK
  89. }
  90. // 多重寻路
  91. func (p *actorPathfind) findMultiPath(req *pb.FindMultiPathRequest) (*pb.PointsList, int32) {
  92. findStrategy := enum.FindMultiPathType(req.FindType)
  93. switch findStrategy {
  94. case enum.FindMultiPath_AllSuccess:
  95. return p.findMultiPathAllSuccess(req.Requests)
  96. case enum.FindMultiPath_AnySuccess:
  97. return p.findMultiPathAnySuccess(req.Requests)
  98. case enum.FindMultiPath_SuccessOnly:
  99. return p.findMultiPathSuccessOnly(req.Requests)
  100. }
  101. return nil, code.MapPathFindFail
  102. }
  103. func (p *actorPathfind) findMultiPathAllSuccess(reqs []*pb.FindPathRequest) (*pb.PointsList, int32) {
  104. rsp := &pb.PointsList{}
  105. for _, request := range reqs {
  106. path, errCode := p.findPath(request)
  107. if code.IsFail(errCode) {
  108. return nil, errCode
  109. }
  110. rsp.List = append(rsp.List, path)
  111. }
  112. return rsp, code.OK
  113. }
  114. func (p *actorPathfind) findMultiPathAnySuccess(reqs []*pb.FindPathRequest) (*pb.PointsList, int32) {
  115. for _, request := range reqs {
  116. path, errCode := p.findPath(request)
  117. if code.IsOK(errCode) {
  118. rsp := &pb.PointsList{
  119. List: []*pb.Points{path},
  120. }
  121. return rsp, code.OK
  122. }
  123. }
  124. return nil, code.MapPathFindFail
  125. }
  126. func (p *actorPathfind) findMultiPathSuccessOnly(reqs []*pb.FindPathRequest) (*pb.PointsList, int32) {
  127. rsp := &pb.PointsList{}
  128. for _, request := range reqs {
  129. if path, errCode := p.findPath(request); code.IsOK(errCode) {
  130. rsp.List = append(rsp.List, path)
  131. }
  132. }
  133. return rsp, code.OK
  134. }
  135. // findLocalPath 在指定区域内寻路
  136. func (p *actorPathfind) doFindPath(sx, sy, ex, ey int32, playerID, leagueID int64, marchType int32) (*pb.Points, int32) {
  137. ctx := astar6.NewFindContext(sx, sy, ex, ey, playerID, leagueID, marchType)
  138. nodes, ok := p.astar.FindPath(ctx)
  139. if !ok {
  140. return nil, code.MapPathFindFail
  141. }
  142. path := &pb.Points{}
  143. for _, node := range nodes {
  144. path.List = append(path.List, &pb.Point{
  145. X: node.X(),
  146. Y: node.Y(),
  147. })
  148. }
  149. return path, code.OK
  150. }
  151. func (p *actorPathfind) isWalkable(node *astar6.Node, ctx *astar6.FindContext) bool {
  152. // 当前坐标的障碍点
  153. barrier, found := p.barrierData.Get(node.X(), node.Y())
  154. // 没有障碍可直接通过
  155. if !found {
  156. return true
  157. }
  158. // 通用 marchType 执行逻辑
  159. marchType := enum.MarchType(ctx.MarchType())
  160. switch marchType {
  161. case enum.MarchType_Unknow: // 默认情况,所有障碍点不可通行
  162. return false
  163. case enum.MarchType_Retreat: // 撤退类型可以通过所有障碍点
  164. return true
  165. default:
  166. // 如果是攻城或者掠夺是可以上敌方的城墙, TODO: 仅限终点,现在先不限制,等配置了攻城点再打开
  167. if marchType == enum.MarchType_Siege || marchType == enum.MarchType_Snatch /*&& ctx.IsEndPoint(x, y)*/ {
  168. return true
  169. }
  170. // 其他行军指令,或者非攻城指令的终点
  171. // 1. 只能通过友军的城墙
  172. if barrier.PlayerID == ctx.PlayerID() || (barrier.LeagueID != 0 && barrier.LeagueID == ctx.LeagueID()) {
  173. return true
  174. }
  175. // 2. 中立的码头是可以通行的
  176. // 码头 + 通道
  177. if barrier.ConfigID == constant.MapCastle_Dock1 || barrier.ConfigID == constant.MapCastle_Dock2 {
  178. return barrier.LeagueID == 0
  179. }
  180. }
  181. return false
  182. }
  183. func (p *actorPathfind) setBarriers(req *mapTypes.BarrierRequest) {
  184. p.barrierData.Add(req)
  185. }
  186. func (p *actorPathfind) removeBarriers(req types.Points) {
  187. for _, point := range req {
  188. p.barrierData.Remove(point.X, point.Y)
  189. }
  190. }