| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- import heapq
- from collections import defaultdict
- from queue import PriorityQueue
- from game.level import level
- def r_block(x, y):
- return level.current_level.block[1][x + 1][y] > 0
- def u_block(x, y):
- return level.current_level.block[0][x][y] > 0
- def l_block(x, y):
- return level.current_level.block[1][x][y] > 0
- def d_block(x, y):
- return level.current_level.block[0][x][y + 1] > 0
- def bomb_exist(x, y):
- return len(level.current_level.get_bomb_instance(x, y)) > 0
- def can_reach(x, y):
- # 检查当前位置的右、上、左、下是否可达
- return not r_block(x, y) and not bomb_exist(x + 1, y), \
- not u_block(x, y) and not bomb_exist(x, y - 1), \
- not l_block(x, y) and not bomb_exist(x - 1, y), \
- not d_block(x, y) and not bomb_exist(x, y + 1)
- def can_reach_r(x, y):
- return not r_block(x, y) and not bomb_exist(x + 1, y)
- def can_reach_u(x, y):
- return not u_block(x, y) and not bomb_exist(x, y - 1)
- def can_reach_l(x, y):
- return not l_block(x, y) and not bomb_exist(x - 1, y)
- def can_reach_d(x, y):
- return not d_block(x, y) and not bomb_exist(x, y + 1)
- def neighbors(current, npc_grid, limit1, limit2, is_ghost):
- points = []
- x, y = current # 当前坐标
- x1, y1 = limit1 # 区域左上角坐标
- x2, y2 = limit2 # 区域右下角坐标
- # 优化为通用的可达判断
- can_r, can_u, can_l, can_d = can_reach(x, y)
- if is_ghost:
- return [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]
- # 使用缓存的can_reach结果来减少重复的检查
- if heuristic(current, npc_grid) == 1 and bomb_exist(*npc_grid):
- if can_r and x + 1 <= x2:
- points.append((x + 1, y))
- if can_u and y - 1 >= y1:
- points.append((x, y - 1))
- if can_l and x - 1 >= x1:
- points.append((x - 1, y))
- if can_d and y + 1 <= y2:
- points.append((x, y + 1))
- else:
- if can_r and x + 1 <= x2:
- points.append((x + 1, y))
- if can_u and y - 1 >= y1:
- points.append((x, y - 1))
- if can_l and x - 1 >= x1:
- points.append((x - 1, y))
- if can_d and y + 1 <= y2:
- points.append((x, y + 1))
- return points
- # PVE优先级:下>左>上>右
- def neighbors_old(current, npc_grid, limit1, limit2, is_ghost):
- # 计算四个邻位是否可达
- points = []
- x, y = current # 当前坐标
- x1, y1 = limit1 # 区域左上角坐标
- x2, y2 = limit2 # 区域右下角坐标
- if is_ghost:
- return [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]
- if heuristic(current, npc_grid) == 1 and bomb_exist(*npc_grid):
- if not r_block(x, y) and x + 1 <= x2:
- points.append((x + 1, y))
- if not u_block(x, y) and y - 1 >= y1:
- points.append((x, y - 1))
- if not l_block(x, y) and x - 1 >= x1:
- points.append((x - 1, y))
- if not d_block(x, y) and y + 1 <= y2:
- points.append((x, y + 1))
- else:
- if can_reach_r(x, y) and x + 1 <= x2:
- points.append((x + 1, y))
- if can_reach_u(x, y) and y - 1 >= y1:
- points.append((x, y - 1))
- if can_reach_l(x, y) and x - 1 >= x1:
- points.append((x - 1, y))
- if can_reach_d(x, y) and y + 1 <= y2:
- points.append((x, y + 1))
- return points
- # 原版优先级:右>下>上>左
- def heuristic(next, npc_grid):
- x1, y1 = next
- x2, y2 = npc_grid
- # return abs(x1 - x2) + abs(y1 - y2) # 曼哈顿距离
- return (x1 - x2)**2 + (y1 - y2)**2 # 原版欧几里得
- def reverse(came_from, hero_grid, npc_grid):
- go_to = dict()
- while hero_grid != npc_grid:
- if hero_grid not in came_from.keys():
- break
- go_to[came_from[hero_grid]] = hero_grid
- hero_grid = came_from[hero_grid]
- return go_to
- def cal_path_new(npc_grid, hero_grid, limit1, limit2, is_ghost=False):
- frontier = []
- heapq.heappush(frontier, (0, npc_grid)) # 用heapq的heappush替代PriorityQueue的put
- came_from = defaultdict(lambda: None) # 用defaultdict来简化came_from的访问
- cost_so_far = defaultdict(lambda: float('inf')) # 初始化cost_so_far的默认值为无限大
- came_from[npc_grid] = None
- cost_so_far[npc_grid] = 0
- while frontier:
- current_cost, current = heapq.heappop(frontier) # 用heapq的heappop替代PriorityQueue的get
- if current == hero_grid:
- break
- for _next in neighbors(current, npc_grid, limit1, limit2, is_ghost):
- new_cost = current_cost + 1
- if new_cost < cost_so_far[_next]:
- cost_so_far[_next] = new_cost
- priority = new_cost + heuristic(_next, hero_grid)
- heapq.heappush(frontier, (priority, _next)) # 用heapq的heappush来更新队列
- came_from[_next] = current
- return reverse(came_from, hero_grid, npc_grid)
- def cal_path(npc_grid, hero_grid, limit1, limit2, is_ghost=False, path_cache=None):
- if path_cache is None:
- path_cache = {} # 初始化缓存表,若未传入则创建一个新的缓存表
- # limit1 区域左上角
- # limit2 区域右下角
- frontier = PriorityQueue()
- frontier.put((0, npc_grid))
- came_from = dict()
- cost_so_far = dict()
- came_from[npc_grid] = None
- cost_so_far[npc_grid] = 0
- while not frontier.empty():
- current = frontier.get()[1]
- if current == hero_grid:
- break
- for _next in neighbors(current, npc_grid, limit1, limit2, is_ghost):
- new_cost = cost_so_far[current] + 1
- if _next not in cost_so_far or new_cost < cost_so_far[_next]:
- cost_so_far[_next] = new_cost
- priority = new_cost + heuristic(_next, hero_grid)
- frontier.put((priority, _next))
- came_from[_next] = current
- if _next in path_cache.keys(): # 如果已经缓存了路径,直接拼接缓存路径
- cached_path = path_cache[_next]
- if cached_path:
- combined_path = reverse(came_from, _next, npc_grid)
- combined_path.update(cached_path)
- if not combined_path:
- return {}
- return combined_path
- else:
- return {}
- return reverse(came_from, hero_grid, npc_grid)
|