Use reference instead of pointer for pathfinding cost calculator, since it cannot be null

This commit is contained in:
Celtic Minstrel 2016-08-31 20:17:59 -04:00
parent 3f7db7f878
commit f24ba1da50
15 changed files with 28 additions and 29 deletions

View file

@ -383,7 +383,7 @@ bool move_result::test_route(const unit &un)
pathfind::teleport_map allowed_teleports = pathfind::get_teleport_locations(un, my_team, true);///@todo 1.9: see_all -> false
//do an A*-search
route_ = std::shared_ptr<pathfind::plain_route>( new pathfind::plain_route(pathfind::a_star_search(un.get_location(), to_, 10000.0, &calc, resources::gameboard->map().w(), resources::gameboard->map().h(), &allowed_teleports)));
route_ = std::shared_ptr<pathfind::plain_route>( new pathfind::plain_route(pathfind::a_star_search(un.get_location(), to_, 10000.0, calc, resources::gameboard->map().w(), resources::gameboard->map().h(), &allowed_teleports)));
if (route_->steps.empty()) {
set_error(E_NO_ROUTE);
return false;

View file

@ -89,7 +89,7 @@ double goto_phase::evaluate()
const pathfind::teleport_map allowed_teleports = pathfind::get_teleport_locations(*ui, current_team());
pathfind::plain_route route;
route = pathfind::a_star_search(ui->get_location(), ui->get_goto(), 10000.0, &calc, map_.w(), map_.h(), &allowed_teleports);
route = pathfind::a_star_search(ui->get_location(), ui->get_goto(), 10000.0, calc, map_.w(), map_.h(), &allowed_teleports);
if (!route.steps.empty()){
move_ = check_move_action(ui->get_location(), route.steps.back(), true, true);
@ -301,7 +301,7 @@ double move_leader_to_goals_phase::evaluate()
}
pathfind::shortest_path_calculator calc(*leader, current_team(), resources::gameboard->teams(), resources::gameboard->map());
pathfind::plain_route route = a_star_search(leader->get_location(), dst_, 1000.0, &calc,
pathfind::plain_route route = a_star_search(leader->get_location(), dst_, 1000.0, calc,
resources::gameboard->map().w(), resources::gameboard->map().h());
if(route.steps.empty()) {
LOG_AI_TESTING_AI_DEFAULT << "route empty";
@ -420,7 +420,7 @@ double move_leader_to_keep_phase::evaluate()
const pathfind::teleport_map allowed_teleports = pathfind::get_teleport_locations(*leader, current_team());
pathfind::plain_route route;
route = pathfind::a_star_search(leader->get_location(), keep, 10000.0, &calc, resources::gameboard->map().w(), resources::gameboard->map().h(), &allowed_teleports);
route = pathfind::a_star_search(leader->get_location(), keep, 10000.0, calc, resources::gameboard->map().w(), resources::gameboard->map().h(), &allowed_teleports);
if (!route.steps.empty() || route.move_cost < shortest_distance) {
best_leader = &(*leader);
@ -456,7 +456,7 @@ double move_leader_to_keep_phase::evaluate()
ordered_locations moves_toward_keep;
pathfind::plain_route route;
route = pathfind::a_star_search(leader->get_location(), keep, 10000.0, &calc, resources::gameboard->map().w(), resources::gameboard->map().h(), &allowed_teleports);
route = pathfind::a_star_search(leader->get_location(), keep, 10000.0, calc, resources::gameboard->map().w(), resources::gameboard->map().h(), &allowed_teleports);
// find next hop
map_location next_hop = map_location::null_location();
@ -477,7 +477,7 @@ double move_leader_to_keep_phase::evaluate()
for (const pathfind::paths::step &dest : leader_paths.destinations) {
if (!units_.find(dest.curr).valid()) {
route = pathfind::a_star_search(dest.curr, next_hop, 10000.0, &calc,
route = pathfind::a_star_search(dest.curr, next_hop, 10000.0, calc,
resources::gameboard->map().w(), resources::gameboard->map().h(), &allowed_teleports);
if (route.move_cost < next_hop_cost) {
moves_toward_keep.insert(std::make_pair(route.move_cost, dest.curr));

View file

@ -338,7 +338,7 @@ std::pair<map_location,map_location> move_to_targets_phase::choose_move(std::vec
// as it can cause the AI to give up on searches and just do nothing.
const double locStopValue = 500.0;
const pathfind::teleport_map allowed_teleports = pathfind::get_teleport_locations(*u, current_team());
pathfind::plain_route real_route = a_star_search(u->get_location(), tg.loc, locStopValue, &cost_calc, map_.w(), map_.h(), &allowed_teleports);
pathfind::plain_route real_route = a_star_search(u->get_location(), tg.loc, locStopValue, cost_calc, map_.w(), map_.h(), &allowed_teleports);
if(real_route.steps.empty()) {
LOG_AI << "Can't reach target: " << locStopValue << " = " << tg.value << "/" << best_rating << "\n";
@ -405,7 +405,7 @@ std::pair<map_location,map_location> move_to_targets_phase::choose_move(std::vec
const double locStopValue = 500.0;
const pathfind::teleport_map allowed_teleports = pathfind::get_teleport_locations(*u, current_team());
pathfind::plain_route cur_route = pathfind::a_star_search(u->get_location(), best_target->loc, locStopValue, &calc, map_.w(), map_.h(), &allowed_teleports);
pathfind::plain_route cur_route = pathfind::a_star_search(u->get_location(), best_target->loc, locStopValue, calc, map_.w(), map_.h(), &allowed_teleports);
if(cur_route.steps.empty()) {
continue;
@ -638,7 +638,7 @@ void move_to_targets_phase::access_points(const move_map& srcdst, const map_loca
if (int(distance_between(loc,dst)) <= u_it->total_movement()) {
pathfind::shortest_path_calculator calc(*u_it, current_team(), resources::gameboard->teams(), map_);
const pathfind::teleport_map allowed_teleports = pathfind::get_teleport_locations(*u_it, current_team());
pathfind::plain_route rt = a_star_search(loc, dst, u_it->total_movement(), &calc, map_.w(), map_.h(), &allowed_teleports);
pathfind::plain_route rt = a_star_search(loc, dst, u_it->total_movement(), calc, map_.w(), map_.h(), &allowed_teleports);
if(rt.steps.empty() == false) {
out.push_back(loc);
}

View file

@ -252,7 +252,7 @@ pathfind::plain_route formula_ai::shortest_path_calculator(const map_location &s
destination = res;
}
pathfind::plain_route route = pathfind::a_star_search(src, destination, 1000.0, &calc,
pathfind::plain_route route = pathfind::a_star_search(src, destination, 1000.0, calc,
resources::gameboard->map().w(), resources::gameboard->map().h(), &allowed_teleports);
return route;

View file

@ -1017,7 +1017,7 @@ private:
pathfind::emergency_path_calculator em_calc(*unit_it, resources::gameboard->map());
pathfind::plain_route route = pathfind::a_star_search(src, dst, 1000.0, &em_calc, resources::gameboard->map().w(), resources::gameboard->map().h(), &allowed_teleports);
pathfind::plain_route route = pathfind::a_star_search(src, dst, 1000.0, em_calc, resources::gameboard->map().w(), resources::gameboard->map().h(), &allowed_teleports);
if( route.steps.size() < 2 ) {
return variant(&locations);

View file

@ -182,14 +182,14 @@ namespace { // Support functions
break;
}
pathfind::plain_route route = pathfind::a_star_search(src, dst, 10000, &calc,
pathfind::plain_route route = pathfind::a_star_search(src, dst, 10000, calc,
game_map->w(), game_map->h());
if (route.steps.empty()) {
WRN_NG << "Could not find move_unit_fake route from " << src << " to " << dst << ": ignoring complexities" << std::endl;
pathfind::emergency_path_calculator calc(fake_unit, *game_map);
route = pathfind::a_star_search(src, dst, 10000, &calc,
route = pathfind::a_star_search(src, dst, 10000, calc,
game_map->w(), game_map->h());
if(route.steps.empty()) {
// This would occur when trying to do a MUF of a unit
@ -197,7 +197,7 @@ namespace { // Support functions
// costs). This really cannot fail.
WRN_NG << "Could not find move_unit_fake route from " << src << " to " << dst << ": ignoring terrain" << std::endl;
pathfind::dummy_path_calculator calc(fake_unit, *game_map);
route = a_star_search(src, dst, 10000, &calc, game_map->w(), game_map->h());
route = a_star_search(src, dst, 10000, calc, game_map->w(), game_map->h());
assert(!route.steps.empty());
}
}

View file

@ -355,7 +355,7 @@ bool game_state::can_recruit_on(const map_location& leader_loc, const map_locati
// any convex castle on the map. Strictly speaking it could be
// reduced to sqrt(map.w()**2 + map.h()**2).
pathfind::plain_route rt =
pathfind::a_star_search(leader_loc, recruit_loc, map.w()+map.h(), &calc,
pathfind::a_star_search(leader_loc, recruit_loc, map.w()+map.h(), calc,
map.w(), map.h());
return !rt.steps.empty();
}

View file

@ -316,7 +316,7 @@ void cave_map_generator::cave_map_generator_job::place_passage(const passage& p)
passage_path_calculator calc(map_, params.wall_, laziness, windiness, rng_);
pathfind::plain_route rt = a_star_search(p.src, p.dst, 10000.0, &calc, params.width_, params.height_);
pathfind::plain_route rt = a_star_search(p.src, p.dst, 10000.0, calc, params.width_, params.height_);
int width = std::max<int>(1, p.cfg["width"].to_int());
int jagged = p.cfg["jagged"];

View file

@ -1039,7 +1039,7 @@ std::string default_map_generator_job::default_generate_map(
}
// Search a path out for the road
pathfind::plain_route rt = pathfind::a_star_search(src, dst, 10000.0, &calc, width, height);
pathfind::plain_route rt = pathfind::a_star_search(src, dst, 10000.0, calc, width, height);
const std::string& road_base_name = misc_labels != nullptr
? base_name_generator->generate()

View file

@ -457,7 +457,7 @@ pathfind::marked_route mouse_handler::get_route(const unit* un, map_location go_
pathfind::plain_route route;
route = pathfind::a_star_search(un->get_location(), go_to, 10000.0, &calc, board.map().w(), board.map().h(), &allowed_teleports);
route = pathfind::a_star_search(un->get_location(), go_to, 10000.0, calc, board.map().w(), board.map().h(), &allowed_teleports);
return mark_route(route);
}

View file

@ -142,22 +142,21 @@ public:
plain_route a_star_search(const map_location& src, const map_location& dst,
double stop_at, const cost_calculator *calc,
double stop_at, const cost_calculator& calc,
const size_t width, const size_t height,
const teleport_map *teleports, bool border) {
//----------------- PRE_CONDITIONS ------------------
assert(src.valid(width, height, border));
assert(dst.valid(width, height, border));
assert(calc != nullptr);
assert(stop_at <= calc->getNoPathValue());
assert(stop_at <= calc.getNoPathValue());
//---------------------------------------------------
DBG_PF << "A* search: " << src << " -> " << dst << '\n';
if (calc->cost(dst, 0) >= stop_at) {
if (calc.cost(dst, 0) >= stop_at) {
LOG_PF << "aborted A* search because Start or Dest is invalid\n";
plain_route locRoute;
locRoute.move_cost = int(calc->getNoPathValue());
locRoute.move_cost = int(calc.getNoPathValue());
return locRoute;
}
@ -209,7 +208,7 @@ plain_route a_star_search(const map_location& src, const map_location& dst,
double thresh = (next.in - search_counter <= 1u) ? next.g : stop_at + 1;
// cost() is always >= 1 (assumed and needed by the heuristic)
if (n.g + 1 >= thresh) continue;
double cost = n.g + calc->cost(locs[i], n.g);
double cost = n.g + calc.cost(locs[i], n.g);
if (cost >= thresh) continue;
bool in_list = next.in == search_counter + 1;
@ -236,7 +235,7 @@ plain_route a_star_search(const map_location& src, const map_location& dst,
std::reverse(route.steps.begin(), route.steps.end());
} else {
LOG_PF << "aborted a* search " << "\n";
route.move_cost = static_cast<int>(calc->getNoPathValue());
route.move_cost = static_cast<int>(calc.getNoPathValue());
}
return route;

View file

@ -191,7 +191,7 @@ struct marked_route
};
plain_route a_star_search(map_location const &src, map_location const &dst,
double stop_at, const cost_calculator* costCalculator,
double stop_at, const cost_calculator& costCalculator,
const size_t parWidth, const size_t parHeight,
const teleport_map* teleports = nullptr, bool border = false);

View file

@ -1494,7 +1494,7 @@ int game_lua_kernel::intf_find_path(lua_State *L)
teams(), map, ignore_units, false, see_all);
}
pathfind::plain_route res = pathfind::a_star_search(src, dst, stop_at, calc, map.w(), map.h(),
pathfind::plain_route res = pathfind::a_star_search(src, dst, stop_at, *calc, map.w(), map.h(),
&teleport_locations);
delete calc;

View file

@ -98,7 +98,7 @@ static int intf_find_path(lua_State *L)
if(lua_isboolean(L, 8)) {
border = luaW_toboolean(L, 8);
}
pathfind::plain_route res = pathfind::a_star_search(src, dst, 10000, &calc, width, height, nullptr, border);
pathfind::plain_route res = pathfind::a_star_search(src, dst, 10000, calc, width, height, nullptr, border);
int nb = res.steps.size();
lua_createtable(L, nb, 0);

View file

@ -317,7 +317,7 @@ bool move::calculate_new_route(const map_location& source_hex, const map_locatio
resources::gameboard->teams().at(team_index()),
resources::gameboard->teams(), resources::gameboard->map());
new_plain_route = pathfind::a_star_search(source_hex,
dest_hex, 10000, &path_calc, resources::gameboard->map().w(), resources::gameboard->map().h());
dest_hex, 10000, path_calc, resources::gameboard->map().w(), resources::gameboard->map().h());
if (new_plain_route.move_cost >= path_calc.getNoPathValue()) return false;
route_.reset(new pathfind::marked_route(pathfind::mark_route(new_plain_route)));
calculate_move_cost();