24 #define LOG_PF LOG_STREAM(info, log_engine)
25 #define DBG_PF LOG_STREAM(debug, log_engine)
26 #define ERR_PF LOG_STREAM(err, log_engine)
39 double xdiff = (
src.x -
dst.x) * 0.75;
41 double ydiff = (
src.y -
dst.y) + ((
src.x & 1) - (
dst.x & 1)) * 0.5;
58 const unsigned bad_search_counter = 0;
60 static unsigned search_counter = bad_search_counter;
78 ,
in(bad_search_counter)
84 if (teleports && !teleports->empty()) {
86 double new_srch = 1.0;
87 auto sources = teleports->get_sources();
89 std::set<map_location>::const_iterator it = sources.begin();
90 for(; it != sources.end(); ++it) {
91 const double tmp_srch = heuristic(
c, *it);
92 if (tmp_srch < new_srch) { new_srch = tmp_srch; }
95 double new_dsth = 1.0;
96 auto targets = teleports->get_targets();
98 for(it = targets.begin(); it != targets.end(); ++it) {
99 const double tmp_dsth = heuristic(*it,
dst);
100 if (tmp_dsth < new_dsth) { new_dsth = tmp_dsth; }
103 double new_h = new_srch + new_dsth + 1.0;
120 comp(
const std::vector<node>&
n) :
nodes_(
n) { }
121 bool operator()(
int a,
int b)
const {
130 indexer(std::size_t
w) :
w_(
w) { }
132 return loc.
y *
w_ + loc.
x;
140 const std::size_t width,
const std::size_t height,
143 assert(
src.valid(width, height,
border));
144 assert(
dst.valid(width, height,
border));
150 if (calc.
cost(
dst, 0) >= stop_at) {
151 LOG_PF <<
"aborted A* search because Start or Dest is invalid";
159 if (search_counter - bad_search_counter <= 1u)
162 static std::vector<node>
nodes;
163 nodes.resize(width * height);
165 indexer
index(width);
166 comp node_comp(
nodes);
174 while (!pq.empty()) {
175 node&
n =
nodes[pq.front()];
177 n.in = search_counter;
179 std::pop_heap(pq.begin(), pq.end(), node_comp);
184 std::vector<map_location> locs(6);
187 if (teleports && !teleports->
empty()) {
189 locs.insert(locs.end(), allowed_teleports.begin(), allowed_teleports.end());
192 for(
auto i = locs.rbegin();
i != locs.rend(); ++
i) {
196 if (loc ==
n.curr)
continue;
199 double thresh = (next.in - search_counter <= 1u) ? next.g : stop_at + 1;
201 if (
n.g + 1 >= thresh)
continue;
202 double cost =
n.g + calc.
cost(loc,
n.g);
203 if (cost >= thresh)
continue;
205 bool in_list = next.in == search_counter + 1;
207 next = node(cost, loc,
n.curr,
dst,
true, teleports);
210 std::push_heap(pq.begin(), std::find(pq.begin(), pq.end(),
static_cast<int>(
index(loc))) + 1, node_comp);
212 pq.push_back(
index(loc));
213 std::push_heap(pq.begin(), pq.end(), node_comp);
220 DBG_PF <<
"found solution; calculating it...";
226 std::reverse(route.
steps.begin(), route.
steps.end());
228 LOG_PF <<
"aborted a* search ";
static lg::log_domain log_engine("engine")
const std::vector< node > & nodes_
unsigned in
If equal to search_counter, the node is off the list.
std::set< map_location > get_adjacents(map_location loc) const
@ border
The border of the map.
const std::vector< node > & nodes
static bool operator<(const placing_info &a, const placing_info &b)
void get_adjacent_tiles(const map_location &a, map_location *res)
Function which, given a location, will place all adjacent locations in res.
std::size_t distance_between(const map_location &a, const map_location &b)
Function which gives the number of hexes between two tiles (i.e.
Standard logging facilities (interface).
plain_route a_star_search(const map_location &src, const map_location &dst, double stop_at, const cost_calculator &calc, const std::size_t width, const std::size_t height, const teleport_map *teleports, bool border)
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
This module contains various pathfinding functions and utilities.
rect dst
Location on the final composed sheet.
rect src
Non-transparent portion of the surface to compose.
Encapsulates the map of the game.
static const map_location & null_location()
static double getNoPathValue()
virtual double cost(const map_location &loc, const double so_far) const =0
Structure which holds a single route between one location and another.
std::vector< map_location > steps
int move_cost
Movement cost for reaching the end of the route.
static map_location::DIRECTION n
static map_location::DIRECTION s