find_context.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package astar6
  2. // FindContext 搜索上下文(每次搜索独立的状态)
  3. // 优化内存布局:将小字段放在一起,减少padding
  4. type FindContext struct {
  5. // 用户传递的原始参数
  6. sx, sy int32 // 起点坐标
  7. ex, ey int32 // 终点坐标
  8. playerID int64 // 当前搜索的玩家ID
  9. leagueID int64 // 当前搜索的联盟ID
  10. marchType int32 // 当前搜索类型,定义路径搜索的行为:默认行为(所有障碍点不可走)|友军障碍可走|忽略终点动态障碍|忽略所有动态障碍
  11. start *Node // 当前搜索的起点(用于视觉惩罚计算)
  12. end *Node // 当前搜索的终点(用于视觉惩罚计算)
  13. minY int32 // 起点和终点的最小y值
  14. openHeap nodeHeap // 优先队列
  15. openSet map[*Node]*heapItem // 快速查询节点对应的heapItem(索引堆优化)
  16. closeSet map[*Node]struct{} // 关闭列表
  17. parentMap map[*Node]*Node // 父节点映射
  18. gMap map[*Node]int32 // g值映射
  19. }
  20. func DefaultFindContext(sx, sy, ex, ey int32) *FindContext {
  21. ctx := acquireContext()
  22. ctx.sx = sx
  23. ctx.sy = sy
  24. ctx.ex = ex
  25. ctx.ey = ey
  26. ctx.minY = min(sy, ey)
  27. return ctx
  28. }
  29. func NewFindContext(sx, sy, ex, ey int32, playerID, leagueID int64, marchType int32) *FindContext {
  30. ctx := DefaultFindContext(sx, sy, ex, ey)
  31. ctx.playerID = playerID
  32. ctx.leagueID = leagueID
  33. ctx.marchType = marchType
  34. return ctx
  35. }
  36. func (p *FindContext) PlayerID() int64 {
  37. return p.playerID
  38. }
  39. func (p *FindContext) LeagueID() int64 {
  40. return p.leagueID
  41. }
  42. func (p *FindContext) MarchType() int32 {
  43. return p.marchType
  44. }
  45. func (p *FindContext) IsEndPoint(x, y int32) bool {
  46. return x == p.ex && y == p.ey
  47. }