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()) {
88 for(
const auto& it : teleports->get_sources()) {
89 const double tmp_srch = heuristic(
c, it);
90 if (tmp_srch <
srch) {
srch = tmp_srch; }
94 double new_h =
srch + dsth + 1.0;
111 comp(
const std::vector<node>&
n) :
nodes_(
n) { }
112 bool operator()(
int a,
int b)
const {
121 indexer(std::size_t
w) :
w_(
w) { }
131 const std::size_t width,
const std::size_t height,
134 assert(
src.valid(width, height,
border));
135 assert(
dst.valid(width, height,
border));
141 if (calc.
cost(
dst, 0) >= stop_at) {
142 LOG_PF <<
"aborted A* search because Start or Dest is invalid";
150 if (search_counter - bad_search_counter <= 1u)
154 if (teleports && !teleports->
empty()) {
156 const double tmp_dsth = heuristic(it,
dst);
157 if (tmp_dsth < dsth) { dsth = tmp_dsth; }
161 static std::vector<node>
nodes;
162 nodes.resize(width * height);
164 indexer
index(width);
165 comp node_comp(
nodes);
169 dst_node.g = stop_at + 1;
175 while (!pq.empty()) {
176 node&
n =
nodes[pq.front()];
178 n.in = search_counter;
180 std::pop_heap(pq.begin(), pq.end(), node_comp);
183 if (
n.t >= dst_node.g)
break;
185 std::vector<map_location> locs(6);
188 if (teleports && !teleports->
empty()) {
190 locs.insert(locs.end(), allowed_teleports.begin(), allowed_teleports.end());
193 for(
auto i = locs.rbegin();
i != locs.rend(); ++
i) {
197 if (
loc ==
n.curr)
continue;
201 if(next.in - search_counter <= 1u) {
209 if (
n.g + 1 >= thresh)
continue;
211 if (cost >= thresh)
continue;
213 bool in_list = next.in == search_counter + 1;
215 next = node(cost,
loc,
n.curr,
dst,
true, teleports,
srch, dsth);
218 std::push_heap(pq.begin(),
std::find(pq.begin(), pq.end(),
static_cast<int>(
index(
loc))) + 1, node_comp);
221 std::push_heap(pq.begin(), pq.end(), node_comp);
227 if (dst_node.g <= stop_at) {
228 DBG_PF <<
"found solution; calculating it...";
229 route.
move_cost =
static_cast<int>(dst_node.g);
234 std::reverse(route.
steps.begin(), route.
steps.end());
236 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.
const std::unordered_set< map_location > & get_targets() const
Returns the locations that are an exit of the tunnel.
const std::unordered_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(std::string_view str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
auto * find(Container &container, const Value &value)
Convenience wrapper for using find on a container without needing to comare to end()
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