astar.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. package astar0
  2. import (
  3. "fmt"
  4. "sort"
  5. clog "github.com/cherry-game/cherry/logger"
  6. )
  7. var (
  8. straightCost int32 = 10 // 直线的移动成本
  9. diagonalCost int32 = 14 // 对角线的移动成本
  10. )
  11. type (
  12. Config struct {
  13. Width int32 // 地图宽(x)
  14. Height int32 // 地图高(y)
  15. MapData [][]int32 // 地图数据
  16. DirectionFunc func(node *Node) [][]int32 // 获取各方向节点函数
  17. IsWalkableFunc func(x, y, value int32) bool // 节点是否可走计算函数
  18. HeuristicFunc func(node, end *Node) int32 // 启发算法函数
  19. IsDebug bool // 是否为调试模式
  20. }
  21. AStar struct {
  22. config Config // 配置
  23. nodes [][]*Node // 预初始化的节点对象 x,y
  24. start *Node // 开始节点
  25. end *Node // 结束节点
  26. openList []*Node // 开放列表
  27. closeList []*Node // 关闭列表
  28. }
  29. )
  30. func New(config Config) *AStar {
  31. instance := &AStar{
  32. config: config,
  33. }
  34. instance.init()
  35. return instance
  36. }
  37. func (p *AStar) init() {
  38. p.nodes = make([][]*Node, p.config.Width)
  39. for x := int32(0); x < p.config.Width; x++ {
  40. p.nodes[x] = make([]*Node, p.config.Height)
  41. for y := int32(0); y < p.config.Height; y++ {
  42. p.nodes[x][y] = newNode(x, y, 0)
  43. }
  44. }
  45. for y := int32(0); y < int32(len(p.config.MapData)); y++ {
  46. for x := int32(0); x < int32(len(p.config.MapData)); x++ {
  47. if x > p.config.Width || y > p.config.Height {
  48. continue
  49. }
  50. p.nodes[x][y].value = p.config.MapData[y][x]
  51. }
  52. }
  53. p.config.MapData = nil
  54. if p.config.DirectionFunc == nil {
  55. p.config.DirectionFunc = Direction8Func
  56. }
  57. if p.config.IsWalkableFunc == nil {
  58. p.config.IsWalkableFunc = IsWalkableFunc()
  59. }
  60. if p.config.HeuristicFunc == nil {
  61. p.config.HeuristicFunc = ManhattanFunc
  62. }
  63. }
  64. func (p *AStar) SetNodeValue(x, y, value int32) {
  65. if x < 0 || y < 0 {
  66. return
  67. }
  68. if x > p.config.Width-1 || y > p.config.Height-1 {
  69. return
  70. }
  71. p.nodes[x][y].value = value
  72. }
  73. func (p *AStar) SetStraightCost(value int32) {
  74. if value > 0 {
  75. straightCost = value
  76. }
  77. }
  78. func (p *AStar) SetDiagonalCost(value int32) {
  79. if value > 0 {
  80. diagonalCost = value
  81. }
  82. }
  83. func (p *AStar) calcG(start, end *Node) int32 {
  84. // 判断移动方式是水平(或垂直)、对角,计算成本
  85. if start.x == end.x || start.y == end.y {
  86. return straightCost
  87. }
  88. return diagonalCost
  89. }
  90. func (p *AStar) calcH(start, end *Node) int32 {
  91. return p.config.HeuristicFunc(start, end)
  92. }
  93. func (p *AStar) FindPath(sx, sy, ex, ey int32) ([]*Node, error) {
  94. end, err := p.findPath(sx, sy, ex, ey)
  95. if err != nil {
  96. return nil, err
  97. }
  98. pathList := BuildPathing(end)
  99. p.Reset()
  100. return pathList, nil
  101. }
  102. func (p *AStar) CheckNodes() {
  103. for x, n1 := range p.nodes {
  104. for y := range n1 {
  105. node := p.nodes[x][y]
  106. if node.status > 0 {
  107. fmt.Println("status:", node)
  108. }
  109. if node.parent != nil {
  110. fmt.Println("parent:", node, node.parent)
  111. }
  112. }
  113. }
  114. }
  115. func (p *AStar) findPath(sx, sy, ex, ey int32) (*Node, error) {
  116. if !p.isWalkable(sx, sy) {
  117. return nil, ErrStartNotWalkable
  118. }
  119. if !p.isWalkable(ex, ey) {
  120. return nil, ErrEndNotWalkable
  121. }
  122. p.start = p.nodes[sx][sy]
  123. p.end = p.nodes[ex][ey]
  124. p.addOpenNode(p.start)
  125. for len(p.openList) > 0 {
  126. openNode := p.openList[0]
  127. p.openList = p.openList[1:]
  128. if p.isEnd(openNode) {
  129. return openNode, nil
  130. }
  131. nodes := p.getNodes(openNode)
  132. if p.config.IsDebug {
  133. clog.Infof("%v -> %v", openNode, nodes)
  134. }
  135. for _, node := range nodes {
  136. g := openNode.g + p.calcG(openNode, node)
  137. if !node.IsOpen() || g < node.g {
  138. node.g = g
  139. node.h = p.calcH(node, p.end)
  140. node.f = node.g + node.h
  141. node.parent = openNode
  142. if !node.IsOpen() {
  143. p.addOpenNode(node)
  144. }
  145. }
  146. }
  147. p.addCloseNode(openNode)
  148. sort.Slice(p.openList, func(i, j int) bool {
  149. return p.openList[i].f < p.openList[j].f
  150. })
  151. }
  152. return nil, ErrFindPathIsNil
  153. }
  154. func (p *AStar) Reset() {
  155. for _, node := range p.openList {
  156. node.Clean()
  157. }
  158. for _, node := range p.closeList {
  159. node.Clean()
  160. }
  161. p.start.Clean()
  162. p.end.Clean()
  163. p.start = nil
  164. p.end = nil
  165. p.openList = nil
  166. p.closeList = nil
  167. }
  168. func (p *AStar) addOpenNode(node *Node) {
  169. node.status = statusOpen
  170. p.openList = append(p.openList, node)
  171. }
  172. func (p *AStar) addCloseNode(node *Node) {
  173. node.status = statusClose
  174. p.closeList = append(p.closeList, node)
  175. }
  176. func (p *AStar) getNodes(node *Node) []*Node {
  177. var (
  178. list []*Node
  179. directionList = p.config.DirectionFunc(node)
  180. )
  181. for _, v := range directionList {
  182. x, y := node.x+v[0], node.y+v[1]
  183. if !p.isWalkable(x, y) {
  184. continue
  185. }
  186. tmpNode := p.nodes[x][y]
  187. if tmpNode.IsClose() {
  188. continue
  189. }
  190. list = append(list, tmpNode)
  191. }
  192. return list
  193. }
  194. func (p *AStar) CheckPoint(x, y int32) bool {
  195. if x < 0 || y < 0 {
  196. return false
  197. }
  198. if x > p.config.Width-1 || y > p.config.Height-1 {
  199. return false
  200. }
  201. return true
  202. }
  203. func (p *AStar) isWalkable(x, y int32) bool {
  204. if !p.CheckPoint(x, y) {
  205. return false
  206. }
  207. return p.config.IsWalkableFunc(x, y, p.nodes[x][y].value)
  208. }
  209. func (p *AStar) isEnd(node *Node) bool {
  210. return node.x == p.end.x && node.y == p.end.y
  211. }
  212. func (p *AStar) Print(list []*Node) {
  213. if len(list) < 1 {
  214. return
  215. }
  216. var (
  217. fromNode = list[0]
  218. toNode = list[len(list)-1]
  219. minX, minY, maxX, maxY = GetRectanglePoint(list)
  220. )
  221. mapText := "\n"
  222. mapText += fmt.Sprintf(" Rectangle: [(%d,%d) -> (%d,%d)] \n", minX, minY, maxX, maxY)
  223. mapText += fmt.Sprintf(" Pathing: %v\n", list)
  224. mapText += " Pattern: start = ◉, point = ◦, end = ●\n"
  225. mapText += "Coordinate: row = x, col = y\n\n"
  226. for y := minY; y <= maxY; y++ {
  227. for x := minX; x <= maxX; x++ {
  228. if InList(list, x, y) {
  229. if x == fromNode.x && y == fromNode.y {
  230. mapText += "◉ "
  231. } else if x == toNode.x && y == toNode.y {
  232. mapText += "● "
  233. } else {
  234. mapText += "◦ "
  235. }
  236. } else {
  237. mapText += fmt.Sprintf("%d ", p.nodes[x][y].value)
  238. }
  239. }
  240. mapText += "\n"
  241. }
  242. clog.Debug(mapText)
  243. }