Skip to content

Commit

Permalink
Allow moving on-map units by setting unit.x an unit.y fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
Vultraz committed Feb 3, 2018
1 parent fe23166 commit 13cacd1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog
Expand Up @@ -36,6 +36,7 @@ Version 1.13.10+dev:
* New wesnoth.unit_types[].advances_to getter.
* New wesnoth.unit_types[].advances_from getter.
* unit.id is now a modifiable field for off-map (Lua-only) units.
* Allow moving on-map units by setting unit.x an unit.y fields.
* Multiplayer:
* Dark Forecast: Fixed broken faction and leader selection.
* Rename the Khalifate to Dunefolk.
Expand Down
36 changes: 36 additions & 0 deletions src/scripting/lua_unit.cpp
Expand Up @@ -427,11 +427,47 @@ static int impl_unit_set(lua_State *L)
}
return 0;
}

if(!lu->on_map()) {
map_location loc = u.get_location();
modify_int_attrib("x", loc.set_wml_x(value); u.set_location(loc));
modify_int_attrib("y", loc.set_wml_y(value); u.set_location(loc));
modify_string_attrib("id", u.set_id(value));
} else {
const bool is_key_x = strcmp(m, "x") == 0;
const bool is_key_y = strcmp(m, "y") == 0;

// Handle moving an on-map unit
if(is_key_x || is_key_y) {
game_board* gb = resources::gameboard;

if(!gb) {
return 0;
}

map_location src = u.get_location();
map_location dst = src;

if(is_key_x) {
dst.set_wml_x(static_cast<int>(luaL_checknumber(L, 3)));

This comment has been minimized.

Copy link
@jyrkive

jyrkive Feb 3, 2018

Member

Prefer luaL_checkinteger() instead.

} else {
dst.set_wml_y(static_cast<int>(luaL_checknumber(L, 3)));
}

// TODO: could probably be relegated to a helper function.
if(src != dst) {
unit_map::iterator u = gb->units().end();
bool success = false;

std::tie(u, success) = gb->units().move(src, dst);

if(success) {
u->anim_comp().set_standing();
}
}

return 0;
}
}

std::string err_msg = "unknown modifiable property of unit: ";
Expand Down

0 comments on commit 13cacd1

Please sign in to comment.