aStar.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import heapq
  2. from collections import defaultdict
  3. from queue import PriorityQueue
  4. from game.level import level
  5. def r_block(x, y):
  6. return level.current_level.block[1][x + 1][y] > 0
  7. def u_block(x, y):
  8. return level.current_level.block[0][x][y] > 0
  9. def l_block(x, y):
  10. return level.current_level.block[1][x][y] > 0
  11. def d_block(x, y):
  12. return level.current_level.block[0][x][y + 1] > 0
  13. def bomb_exist(x, y):
  14. return len(level.current_level.get_bomb_instance(x, y)) > 0
  15. def can_reach(x, y):
  16. # 检查当前位置的右、上、左、下是否可达
  17. return not r_block(x, y) and not bomb_exist(x + 1, y), \
  18. not u_block(x, y) and not bomb_exist(x, y - 1), \
  19. not l_block(x, y) and not bomb_exist(x - 1, y), \
  20. not d_block(x, y) and not bomb_exist(x, y + 1)
  21. def can_reach_r(x, y):
  22. return not r_block(x, y) and not bomb_exist(x + 1, y)
  23. def can_reach_u(x, y):
  24. return not u_block(x, y) and not bomb_exist(x, y - 1)
  25. def can_reach_l(x, y):
  26. return not l_block(x, y) and not bomb_exist(x - 1, y)
  27. def can_reach_d(x, y):
  28. return not d_block(x, y) and not bomb_exist(x, y + 1)
  29. def neighbors(current, npc_grid, limit1, limit2, is_ghost):
  30. points = []
  31. x, y = current # 当前坐标
  32. x1, y1 = limit1 # 区域左上角坐标
  33. x2, y2 = limit2 # 区域右下角坐标
  34. # 优化为通用的可达判断
  35. can_r, can_u, can_l, can_d = can_reach(x, y)
  36. if is_ghost:
  37. return [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]
  38. # 使用缓存的can_reach结果来减少重复的检查
  39. if heuristic(current, npc_grid) == 1 and bomb_exist(*npc_grid):
  40. if can_r and x + 1 <= x2:
  41. points.append((x + 1, y))
  42. if can_u and y - 1 >= y1:
  43. points.append((x, y - 1))
  44. if can_l and x - 1 >= x1:
  45. points.append((x - 1, y))
  46. if can_d and y + 1 <= y2:
  47. points.append((x, y + 1))
  48. else:
  49. if can_r and x + 1 <= x2:
  50. points.append((x + 1, y))
  51. if can_u and y - 1 >= y1:
  52. points.append((x, y - 1))
  53. if can_l and x - 1 >= x1:
  54. points.append((x - 1, y))
  55. if can_d and y + 1 <= y2:
  56. points.append((x, y + 1))
  57. return points
  58. # PVE优先级:下>左>上>右
  59. def neighbors_old(current, npc_grid, limit1, limit2, is_ghost):
  60. # 计算四个邻位是否可达
  61. points = []
  62. x, y = current # 当前坐标
  63. x1, y1 = limit1 # 区域左上角坐标
  64. x2, y2 = limit2 # 区域右下角坐标
  65. if is_ghost:
  66. return [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]
  67. if heuristic(current, npc_grid) == 1 and bomb_exist(*npc_grid):
  68. if not r_block(x, y) and x + 1 <= x2:
  69. points.append((x + 1, y))
  70. if not u_block(x, y) and y - 1 >= y1:
  71. points.append((x, y - 1))
  72. if not l_block(x, y) and x - 1 >= x1:
  73. points.append((x - 1, y))
  74. if not d_block(x, y) and y + 1 <= y2:
  75. points.append((x, y + 1))
  76. else:
  77. if can_reach_r(x, y) and x + 1 <= x2:
  78. points.append((x + 1, y))
  79. if can_reach_u(x, y) and y - 1 >= y1:
  80. points.append((x, y - 1))
  81. if can_reach_l(x, y) and x - 1 >= x1:
  82. points.append((x - 1, y))
  83. if can_reach_d(x, y) and y + 1 <= y2:
  84. points.append((x, y + 1))
  85. return points
  86. # 原版优先级:右>下>上>左
  87. def heuristic(next, npc_grid):
  88. x1, y1 = next
  89. x2, y2 = npc_grid
  90. # return abs(x1 - x2) + abs(y1 - y2) # 曼哈顿距离
  91. return (x1 - x2)**2 + (y1 - y2)**2 # 原版欧几里得
  92. def reverse(came_from, hero_grid, npc_grid):
  93. go_to = dict()
  94. while hero_grid != npc_grid:
  95. if hero_grid not in came_from.keys():
  96. break
  97. go_to[came_from[hero_grid]] = hero_grid
  98. hero_grid = came_from[hero_grid]
  99. return go_to
  100. def cal_path_new(npc_grid, hero_grid, limit1, limit2, is_ghost=False):
  101. frontier = []
  102. heapq.heappush(frontier, (0, npc_grid)) # 用heapq的heappush替代PriorityQueue的put
  103. came_from = defaultdict(lambda: None) # 用defaultdict来简化came_from的访问
  104. cost_so_far = defaultdict(lambda: float('inf')) # 初始化cost_so_far的默认值为无限大
  105. came_from[npc_grid] = None
  106. cost_so_far[npc_grid] = 0
  107. while frontier:
  108. current_cost, current = heapq.heappop(frontier) # 用heapq的heappop替代PriorityQueue的get
  109. if current == hero_grid:
  110. break
  111. for _next in neighbors(current, npc_grid, limit1, limit2, is_ghost):
  112. new_cost = current_cost + 1
  113. if new_cost < cost_so_far[_next]:
  114. cost_so_far[_next] = new_cost
  115. priority = new_cost + heuristic(_next, hero_grid)
  116. heapq.heappush(frontier, (priority, _next)) # 用heapq的heappush来更新队列
  117. came_from[_next] = current
  118. return reverse(came_from, hero_grid, npc_grid)
  119. def cal_path(npc_grid, hero_grid, limit1, limit2, is_ghost=False, path_cache=None):
  120. if path_cache is None:
  121. path_cache = {} # 初始化缓存表,若未传入则创建一个新的缓存表
  122. # limit1 区域左上角
  123. # limit2 区域右下角
  124. frontier = PriorityQueue()
  125. frontier.put((0, npc_grid))
  126. came_from = dict()
  127. cost_so_far = dict()
  128. came_from[npc_grid] = None
  129. cost_so_far[npc_grid] = 0
  130. while not frontier.empty():
  131. current = frontier.get()[1]
  132. if current == hero_grid:
  133. break
  134. for _next in neighbors(current, npc_grid, limit1, limit2, is_ghost):
  135. new_cost = cost_so_far[current] + 1
  136. if _next not in cost_so_far or new_cost < cost_so_far[_next]:
  137. cost_so_far[_next] = new_cost
  138. priority = new_cost + heuristic(_next, hero_grid)
  139. frontier.put((priority, _next))
  140. came_from[_next] = current
  141. if _next in path_cache.keys(): # 如果已经缓存了路径,直接拼接缓存路径
  142. cached_path = path_cache[_next]
  143. if cached_path:
  144. combined_path = reverse(came_from, _next, npc_grid)
  145. combined_path.update(cached_path)
  146. if not combined_path:
  147. return {}
  148. return combined_path
  149. else:
  150. return {}
  151. return reverse(came_from, hero_grid, npc_grid)