aStar.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import heapq
  2. from collections import defaultdict
  3. from queue import PriorityQueue
  4. from client_local.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 (x1 - x2)**2 + (y1 - y2)**2
  91. def reverse(came_from, hero_grid, npc_grid):
  92. go_to = dict()
  93. while hero_grid != npc_grid:
  94. if hero_grid not in came_from.keys():
  95. break
  96. go_to[came_from[hero_grid]] = hero_grid
  97. hero_grid = came_from[hero_grid]
  98. return go_to
  99. def cal_path_new(npc_grid, hero_grid, limit1, limit2, is_ghost=False):
  100. frontier = []
  101. heapq.heappush(frontier, (0, npc_grid)) # 用heapq的heappush替代PriorityQueue的put
  102. came_from = defaultdict(lambda: None) # 用defaultdict来简化came_from的访问
  103. cost_so_far = defaultdict(lambda: float('inf')) # 初始化cost_so_far的默认值为无限大
  104. came_from[npc_grid] = None
  105. cost_so_far[npc_grid] = 0
  106. while frontier:
  107. current_cost, current = heapq.heappop(frontier) # 用heapq的heappop替代PriorityQueue的get
  108. if current == hero_grid:
  109. break
  110. for _next in neighbors(current, npc_grid, limit1, limit2, is_ghost):
  111. new_cost = current_cost + 1
  112. if new_cost < cost_so_far[_next]:
  113. cost_so_far[_next] = new_cost
  114. priority = new_cost + heuristic(_next, hero_grid)
  115. heapq.heappush(frontier, (priority, _next)) # 用heapq的heappush来更新队列
  116. came_from[_next] = current
  117. return reverse(came_from, hero_grid, npc_grid)
  118. def cal_path(npc_grid, hero_grid, limit1, limit2, is_ghost=False, path_cache=None):
  119. if path_cache is None:
  120. path_cache = dict() # 初始化缓存表,若未传入则创建一个新的缓存表
  121. # limit1 区域左上角
  122. # limit2 区域右下角
  123. frontier = PriorityQueue()
  124. frontier.put((0, npc_grid))
  125. came_from = dict()
  126. cost_so_far = dict()
  127. came_from[npc_grid] = None
  128. cost_so_far[npc_grid] = 0
  129. while not frontier.empty():
  130. current = frontier.get()[1]
  131. if current == hero_grid:
  132. break
  133. for _next in neighbors(current, npc_grid, limit1, limit2, is_ghost):
  134. new_cost = cost_so_far[current] + 1
  135. if _next not in cost_so_far or new_cost < cost_so_far[_next]:
  136. cost_so_far[_next] = new_cost
  137. priority = new_cost + heuristic(_next, hero_grid)
  138. frontier.put((priority, _next))
  139. came_from[_next] = current
  140. if _next in path_cache.keys(): # 如果已经缓存了路径,直接拼接缓存路径
  141. cached_path = path_cache[_next]
  142. if cached_path:
  143. combined_path = reverse(came_from, _next, npc_grid)
  144. combined_path.update(cached_path)
  145. if not combined_path:
  146. return dict()
  147. return combined_path
  148. else:
  149. return dict()
  150. return reverse(came_from, hero_grid, npc_grid)