Refactor [move_unit_fake]

This commit is contained in:
Alexander van Gessel 2010-05-12 01:05:26 +01:00
parent 8920c6ce2b
commit 89dbba9a0b

View File

@ -48,6 +48,7 @@
#include <boost/scoped_ptr.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/ptr_container/ptr_vector.hpp>
#include <algorithm>
#include <iomanip>
#include <iostream>
@ -1021,16 +1022,11 @@ WML_HANDLER_FUNCTION(store_turns, /*event_info*/, cfg)
resources::state_of_game->get_variable(var_name) = lexical_cast_default<std::string>(turns);
}
// Moving a 'unit' - i.e. a dummy unit
// that is just moving for the visual effect
WML_HANDLER_FUNCTION(move_unit_fake, /*event_info*/, cfg)
{
gamemap *game_map = resources::game_map;
namespace {
std::auto_ptr<unit> create_fake_unit(const vconfig& cfg)
{
std::string type = cfg["type"];
std::string side = cfg["side"];
std::string x = cfg["x"];
std::string y = cfg["y"];
std::string variation = cfg["variation"];
size_t side_num = lexical_cast_default<int>(side,1)-1;
@ -1038,17 +1034,21 @@ WML_HANDLER_FUNCTION(move_unit_fake, /*event_info*/, cfg)
unit_race::GENDER gender = string_gender(cfg["gender"]);
const unit_type *ut = unit_types.find(type);
if (!ut) return;
unit dummy_unit(ut, side_num + 1, false, gender);
if (!ut) return std::auto_ptr<unit>();
std::auto_ptr<unit> fake_unit(new unit(ut, side_num + 1, false, gender));
if(!variation.empty()) {
config mod;
config &effect = mod.add_child("effect");
effect["apply_to"] = "variation";
effect["name"] = variation;
dummy_unit.add_modification("variation",mod);
const std::vector<std::string> xvals = utils::split(x);
const std::vector<std::string> yvals = utils::split(y);
fake_unit->add_modification("variation",mod);
}
return fake_unit;
}
std::vector<map_location> fake_unit_path(const unit& fake_unit, const std::vector<std::string>& xvals, const std::vector<std::string>& yvals)
{
gamemap *game_map = resources::game_map;
std::vector<map_location> path;
map_location src;
map_location dst;
@ -1063,8 +1063,8 @@ WML_HANDLER_FUNCTION(move_unit_fake, /*event_info*/, cfg)
path.push_back(src);
continue;
}
pathfind::shortest_path_calculator calc(dummy_unit,
(*resources::teams)[side_num],
pathfind::shortest_path_calculator calc(fake_unit,
(*resources::teams)[fake_unit.side()-1],
*resources::units,
*resources::teams,
*game_map);
@ -1081,7 +1081,7 @@ WML_HANDLER_FUNCTION(move_unit_fake, /*event_info*/, cfg)
if (route.steps.size() == 0) {
WRN_NG << "Could not find move_unit_fake route from " << src << " to " << dst << ": ignoring complexities\n";
pathfind::emergency_path_calculator calc(dummy_unit, *game_map);
pathfind::emergency_path_calculator calc(fake_unit, *game_map);
route = pathfind::a_star_search(src, dst, 10000, &calc,
game_map->w(), game_map->h());
@ -1090,7 +1090,7 @@ WML_HANDLER_FUNCTION(move_unit_fake, /*event_info*/, cfg)
// over locations which are unreachable to it (infinite movement
// costs). This really cannot fail.
WRN_NG << "Could not find move_unit_fake route from " << src << " to " << dst << ": ignoring terrain\n";
pathfind::dummy_path_calculator calc(dummy_unit, *game_map);
pathfind::dummy_path_calculator calc(fake_unit, *game_map);
route = a_star_search(src, dst, 10000, &calc, game_map->w(), game_map->h());
assert(route.steps.size() > 0);
}
@ -1103,7 +1103,27 @@ WML_HANDLER_FUNCTION(move_unit_fake, /*event_info*/, cfg)
src = dst;
}
if (!path.empty()) unit_display::move_unit(path, dummy_unit, *resources::teams);
return path;
}
}
// Moving a 'unit' - i.e. a dummy unit
// that is just moving for the visual effect
WML_HANDLER_FUNCTION(move_unit_fake, /*event_info*/, cfg)
{
std::auto_ptr<unit> dummy_unit = create_fake_unit(cfg);
if(!dummy_unit.get())
return;
const std::string x = cfg["x"];
const std::string y = cfg["y"];
const std::vector<std::string> xvals = utils::split(x);
const std::vector<std::string> yvals = utils::split(y);
const std::vector<map_location>& path = fake_unit_path(*dummy_unit, xvals, yvals);
if (!path.empty())
unit_display::move_unit(path, *dummy_unit, *resources::teams);
}
// Helper function(s) for [set_variable]