navigation_reference.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. from queue import PriorityQueue
  2. from client_local.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. # PVE优先级:下>左>上>右
  22. def neighbors(current, npc_grid, limit1, limit2, is_ghost):
  23. # 计算四个邻位是否可达
  24. points = []
  25. x, y = current # 当前坐标
  26. x1, y1 = limit1 # 区域左上角坐标
  27. x2, y2 = limit2 # 区域右下角坐标
  28. if is_ghost:
  29. return [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]
  30. if heuristic(current, npc_grid) == 1 and bomb_exist(*npc_grid):
  31. if not r_block(x, y) and x + 1 <= x2:
  32. points.append((x + 1, y))
  33. if not u_block(x, y) and y - 1 >= y1:
  34. points.append((x, y - 1))
  35. if not l_block(x, y) and x - 1 >= x1:
  36. points.append((x - 1, y))
  37. if not d_block(x, y) and y + 1 <= y2:
  38. points.append((x, y + 1))
  39. else:
  40. if can_reach_r(x, y) and x + 1 <= x2:
  41. points.append((x + 1, y))
  42. if can_reach_u(x, y) and y - 1 >= y1:
  43. points.append((x, y - 1))
  44. if can_reach_l(x, y) and x - 1 >= x1:
  45. points.append((x - 1, y))
  46. if can_reach_d(x, y) and y + 1 <= y2:
  47. points.append((x, y + 1))
  48. return points
  49. # 原版优先级:右>下>上>左
  50. def heuristic(next, npc_grid):
  51. x1, y1 = next
  52. x2, y2 = npc_grid
  53. return (x1 - x2)**2 + (y1 - y2)**2
  54. def reverse(came_from, hero_grid, npc_grid):
  55. go_to = dict()
  56. while hero_grid != npc_grid:
  57. if hero_grid not in came_from.keys():
  58. break
  59. go_to[came_from[hero_grid]] = hero_grid
  60. hero_grid = came_from[hero_grid]
  61. return go_to
  62. def cal_path(npc_grid, hero_grid, limit1, limit2, is_ghost=False):
  63. # limit1 区域左上角
  64. # limit2 区域右下角
  65. frontier = PriorityQueue()
  66. frontier.put((0, npc_grid))
  67. came_from = dict()
  68. cost_so_far = dict()
  69. came_from[npc_grid] = None
  70. cost_so_far[npc_grid] = 0
  71. while not frontier.empty():
  72. current = frontier.get()[1]
  73. if current == hero_grid:
  74. break
  75. for next in neighbors(current, npc_grid, limit1, limit2, is_ghost):
  76. new_cost = cost_so_far[current] + 1
  77. if next not in cost_so_far or new_cost < cost_so_far[next]:
  78. cost_so_far[next] = new_cost
  79. priority = new_cost + heuristic(next, hero_grid)
  80. frontier.put((priority, next))
  81. came_from[next] = current
  82. return reverse(came_from, hero_grid, npc_grid)