8 #include <unordered_map>
14 using std::unordered_map;
26 return *(--path.end());
37 return &maybeTarget==⌖
53 int x_offset = grid[0][0].x_coord;
54 int y_offset = grid[0][0].y_coord;
57 multimap<int,GridCell*> unvisited_nodes;
58 unordered_map<GridCell*,list<GridCell*> > current_costs;
61 list<GridCell*> origin_path = {};
64 current_costs[&origin] = origin_path;
65 unvisited_nodes.insert(make_pair(0,&origin));
67 while(unvisited_nodes.size()!=0)
72 auto current_entry = unvisited_nodes.begin();
73 our_cost = current_entry->first;
74 our_cell = current_entry->second;
76 unvisited_nodes.erase(current_entry);
79 list<GridCell*>& current_path = current_costs[our_cell];
82 if(isTarget(*our_cell))
88 list<GridCell*> adjacent_nodes;
89 int gridX_value = our_cell->
x_coord - x_offset;
90 int gridY_value = our_cell->
y_coord - y_offset;
92 adjacent_nodes.push_back(&grid[gridX_value-1][gridY_value]);
93 if(gridX_value!=grid.size()-1)
94 adjacent_nodes.push_back(&grid[gridX_value+1][gridY_value]);
96 adjacent_nodes.push_back(&grid[gridX_value][gridY_value-1]);
97 if(gridY_value!=grid[0].size()-1)
98 adjacent_nodes.push_back(&grid[gridX_value][gridY_value+1]);
106 if(!isPassable(*
x) && !isTarget(*
x))
110 list<GridCell*> x_proposed_path = current_path;
111 x_proposed_path.push_back(
x);
114 if(current_costs.count(
x))
116 list<GridCell*>& clcp = current_costs[
x];
117 if(clcp.size()-1>our_cost+1)
119 auto old_range = unvisited_nodes.equal_range(clcp.size()-1);
120 for(
auto i = old_range.first; i!=old_range.second; ++i)
123 unvisited_nodes.erase(i);
131 unvisited_nodes.insert(make_pair(our_cost+1,
x));
132 current_costs[
x]=x_proposed_path;
138 return list<GridCell*>();
static std::list< GridCell * > findShortestPathInternal(GridCell &origin, const std::function< bool(const GridCell &)> &isTarget, const std::function< bool(const GridCell &)> &isPassable, std::vector< std::vector< GridCell > > &grid)
static std::list< GridCell * > findShortestPath(GridCell &origin, const GridCell &target, std::vector< std::vector< GridCell > > &grid)
static GridCell * findNearestAlly(GridCell &origin, std::vector< std::vector< GridCell > > &grid)