aStarOriginal.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. from queue import PriorityQueue
  2. from game.level import level
  3. def r_block(x, y):
  4. return level.current_level.block[1][x + 1][y] > 0
  5. def u_block(x, y):
  6. return level.current_level.block[0][x][y] > 0
  7. def l_block(x, y):
  8. return level.current_level.block[1][x][y] > 0
  9. def d_block(x, y):
  10. return level.current_level.block[0][x][y + 1] > 0
  11. def bomb_exist(x, y):
  12. return len(level.current_level.get_bomb_instance(x, y)) > 0
  13. def can_reach_r(x, y):
  14. return not r_block(x, y) and not bomb_exist(x + 1, y)
  15. def can_reach_u(x, y):
  16. return not u_block(x, y) and not bomb_exist(x, y - 1)
  17. def can_reach_l(x, y):
  18. return not l_block(x, y) and not bomb_exist(x - 1, y)
  19. def can_reach_d(x, y):
  20. return not d_block(x, y) and not bomb_exist(x, y + 1)
  21. def neighbors(current, npc_grid, limit1, limit2, is_ghost):
  22. # 计算四个邻位是否可达
  23. points = []
  24. x, y = current # 当前坐标
  25. x1, y1 = limit1 # 区域左上角坐标
  26. x2, y2 = limit2 # 区域右下角坐标
  27. if is_ghost:
  28. return [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]
  29. if heuristic(current, npc_grid) == 1 and bomb_exist(*npc_grid):
  30. if not r_block(x, y) and x + 1 <= x2:
  31. points.append((x + 1, y))
  32. if not u_block(x, y) and y - 1 >= y1:
  33. points.append((x, y - 1))
  34. if not l_block(x, y) and x - 1 >= x1:
  35. points.append((x - 1, y))
  36. if not d_block(x, y) and y + 1 <= y2:
  37. points.append((x, y + 1))
  38. else:
  39. if can_reach_r(x, y) and x + 1 <= x2:
  40. points.append((x + 1, y))
  41. if can_reach_u(x, y) and y - 1 >= y1:
  42. points.append((x, y - 1))
  43. if can_reach_l(x, y) and x - 1 >= x1:
  44. points.append((x - 1, y))
  45. if can_reach_d(x, y) and y + 1 <= y2:
  46. points.append((x, y + 1))
  47. return points
  48. def heuristic(next, npc_grid):
  49. x1, y1 = next
  50. x2, y2 = npc_grid
  51. return (x1 - x2)**2 + (y1 - y2)**2
  52. def reverse(came_from, hero_grid, npc_grid):
  53. go_to = dict()
  54. while hero_grid != npc_grid:
  55. if hero_grid not in came_from.keys():
  56. break
  57. go_to[came_from[hero_grid]] = hero_grid
  58. hero_grid = came_from[hero_grid]
  59. return go_to
  60. def cal_path(npc_grid, hero_grid, limit1, limit2, is_ghost=False):
  61. # limit1 区域左上角
  62. # limit2 区域右下角
  63. frontier = PriorityQueue()
  64. frontier.put((0, npc_grid))
  65. came_from = dict()
  66. cost_so_far = dict()
  67. came_from[npc_grid] = None
  68. cost_so_far[npc_grid] = 0
  69. while not frontier.empty():
  70. current = frontier.get()[1]
  71. if current == hero_grid:
  72. break
  73. for next in neighbors(current, npc_grid, limit1, limit2, is_ghost):
  74. new_cost = cost_so_far[current] + 1
  75. if next not in cost_so_far or new_cost < cost_so_far[next]:
  76. cost_so_far[next] = new_cost
  77. priority = new_cost + heuristic(next, hero_grid)
  78. frontier.put((priority, next))
  79. came_from[next] = current
  80. return reverse(came_from, hero_grid, npc_grid)