Modify lua function find_cost_map
- Add optin to use current moves instead of max_moves. - Output a quadrupple - Optional table for a standart location filter
This commit is contained in:
parent
cb6b0798df
commit
828c4bfb6b
3 changed files with 49 additions and 34 deletions
|
@ -873,7 +873,7 @@ full_cost_map::full_cost_map(bool force_ignore_zoc,
|
|||
* Adds a units cost map to cost_map (increments the elements in cost_map)
|
||||
* @param u a real existing unit on the map
|
||||
*/
|
||||
void full_cost_map::add_unit(const unit& u)
|
||||
void full_cost_map::add_unit(const unit& u, bool use_max_moves)
|
||||
{
|
||||
std::vector<team> const &teams = *resources::teams;
|
||||
if (u.side() < 1 || u.side() > int(teams.size())) {
|
||||
|
@ -884,7 +884,8 @@ void full_cost_map::add_unit(const unit& u)
|
|||
paths::dest_vect dummy = paths::dest_vect();
|
||||
|
||||
find_routes(u.get_location(), u.movement_type().get_movement(),
|
||||
u.get_state(unit::STATE_SLOWED), u.total_movement(),
|
||||
u.get_state(unit::STATE_SLOWED),
|
||||
(use_max_moves) ? u.total_movement() : u.movement_left(),
|
||||
u.total_movement(), 99, dummy, NULL,
|
||||
allow_teleport_ ? &u : NULL,
|
||||
ignore_units_ ? NULL : &teams[u.side()-1],
|
||||
|
|
|
@ -276,7 +276,7 @@ struct full_cost_map
|
|||
bool allow_teleport, const team &viewing_team,
|
||||
bool see_all=true, bool ignore_units=true);
|
||||
|
||||
void add_unit(const unit& u);
|
||||
void add_unit(const unit& u, bool use_max_moves=true);
|
||||
void add_unit(const map_location& origin, const unit_type* const unit_type, int side);
|
||||
int get_cost_at(int x, int y) const;
|
||||
std::pair<int, int> get_pair_at(int x, int y) const;
|
||||
|
|
|
@ -2312,6 +2312,7 @@ static int intf_find_reach(lua_State *L)
|
|||
* - Args 1,2: source location. (Or Arg 1: unit. Or Arg 1: table containing a filter)
|
||||
* - Arg 3: optional array of tables with 4 elements (coordinates + side + unit type string)
|
||||
* - Arg 4: optional table (optional fields: ignore_units, ignore_teleport, viewing_side, debug).
|
||||
* - Arg 5: optional table: standart location filter.
|
||||
* - Ret 1: array of triples (coordinates + array of tuples(summed cost + reach counter)).
|
||||
*/
|
||||
static int intf_find_cost_map(lua_State *L)
|
||||
|
@ -2395,7 +2396,7 @@ static int intf_find_cost_map(lua_State *L)
|
|||
|
||||
std::vector<team> &teams = *resources::teams;
|
||||
int viewing_side = 0;
|
||||
bool ignore_units = true, see_all = true, ignore_teleport = false, debug = false;
|
||||
bool ignore_units = true, see_all = true, ignore_teleport = false, debug = false, use_max_moves = false;
|
||||
|
||||
if (lua_istable(L, arg)) // 4. arg - options
|
||||
{
|
||||
|
@ -2433,10 +2434,30 @@ static int intf_find_cost_map(lua_State *L)
|
|||
{
|
||||
debug = lua_toboolean(L, -1);
|
||||
}
|
||||
|
||||
lua_pop(L, 1);
|
||||
|
||||
lua_pushstring(L, "use_max_moves");
|
||||
lua_rawget(L, arg);
|
||||
if (!lua_isnil(L, -1))
|
||||
{
|
||||
use_max_moves = lua_toboolean(L, -1);
|
||||
}
|
||||
lua_pop(L, 1);
|
||||
++arg;
|
||||
}
|
||||
|
||||
// 5. arg - location filter
|
||||
filter = vconfig::unconstructed_vconfig();
|
||||
std::set<map_location> location_set;
|
||||
luaW_tovconfig(L, arg, filter);
|
||||
if (filter.null())
|
||||
{
|
||||
filter = vconfig(config(), true);
|
||||
}
|
||||
const terrain_filter t_filter(filter, *resources::units);
|
||||
t_filter.get_locations(location_set, true);
|
||||
++arg;
|
||||
|
||||
// build cost_map
|
||||
team &viewing_team = teams[(viewing_side ? viewing_side : 1) - 1];
|
||||
pathfind::full_cost_map cost_map(
|
||||
|
@ -2444,7 +2465,7 @@ static int intf_find_cost_map(lua_State *L)
|
|||
|
||||
BOOST_FOREACH(const unit* const u, real_units)
|
||||
{
|
||||
cost_map.add_unit(*u);
|
||||
cost_map.add_unit(*u, use_max_moves);
|
||||
}
|
||||
BOOST_FOREACH(const unit_type_vector::value_type& fu, fake_units)
|
||||
{
|
||||
|
@ -2452,47 +2473,40 @@ static int intf_find_cost_map(lua_State *L)
|
|||
cost_map.add_unit(fu.get<0>(), ut, fu.get<1>());
|
||||
}
|
||||
|
||||
const gamemap* map = resources::game_map;
|
||||
|
||||
if (debug)
|
||||
{
|
||||
resources::screen->labels().clear_all();
|
||||
for (int x = 0; x < map->w(); ++x)
|
||||
BOOST_FOREACH(const map_location& loc, location_set)
|
||||
{
|
||||
for (int y = 0; y < map->h(); ++y)
|
||||
{
|
||||
std::stringstream s;
|
||||
s << cost_map.get_pair_at(x, y).first;
|
||||
s << " / ";
|
||||
s << cost_map.get_pair_at(x, y).second;
|
||||
resources::screen->labels().set_label(map_location(x, y), s.str());
|
||||
}
|
||||
std::stringstream s;
|
||||
s << cost_map.get_pair_at(loc.x, loc.y).first;
|
||||
s << " / ";
|
||||
s << cost_map.get_pair_at(loc.x, loc.y).second;
|
||||
resources::screen->labels().set_label(loc, s.str());
|
||||
}
|
||||
}
|
||||
|
||||
// create return value
|
||||
lua_createtable(L, map->w() * map->h(), 0);
|
||||
for (int x = 0; x < map->w(); ++x)
|
||||
lua_createtable(L, location_set.size(), 0);
|
||||
int counter = 1;
|
||||
BOOST_FOREACH(const map_location& loc, location_set)
|
||||
{
|
||||
for (int y = 0; y < map->h(); ++y)
|
||||
{
|
||||
lua_createtable(L, 4, 0);
|
||||
lua_createtable(L, 4, 0);
|
||||
|
||||
lua_pushinteger(L, x + 1);
|
||||
lua_rawseti(L, -2, 1);
|
||||
lua_pushinteger(L, loc.x + 1);
|
||||
lua_rawseti(L, -2, 1);
|
||||
|
||||
lua_pushinteger(L, y + 1);
|
||||
lua_rawseti(L, -2, 2);
|
||||
lua_pushinteger(L, loc.y + 1);
|
||||
lua_rawseti(L, -2, 2);
|
||||
|
||||
lua_createtable(L, 2, 0);
|
||||
lua_pushinteger(L, cost_map.get_pair_at(x, y).first);
|
||||
lua_rawseti(L, -2, 1);
|
||||
lua_pushinteger(L, cost_map.get_pair_at(x, y).second);
|
||||
lua_rawseti(L, -2, 2);
|
||||
lua_pushinteger(L, cost_map.get_pair_at(loc.x, loc.y).first);
|
||||
lua_rawseti(L, -2, 3);
|
||||
|
||||
lua_rawseti(L, -2, 3);
|
||||
lua_rawseti(L, -2, x * map->h() + y + 1);
|
||||
}
|
||||
lua_pushinteger(L, cost_map.get_pair_at(loc.x, loc.y).second);
|
||||
lua_rawseti(L, -2, 4);
|
||||
|
||||
lua_rawseti(L, -2, counter);
|
||||
++counter;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue