Skip to content

Commit

Permalink
Expand 13cacd1 by allowing unit.loc = {x, y}
Browse files Browse the repository at this point in the history
  • Loading branch information
Vultraz committed Feb 4, 2018
1 parent 419568d commit 883b4f1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
3 changes: 2 additions & 1 deletion changelog
Expand Up @@ -36,7 +36,8 @@ 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.
* Allow moving on-map units by setting unit.x an unit.y fields, or with

This comment has been minimized.

Copy link
@stevecotton

stevecotton Feb 4, 2018

Contributor

misspelling of "and"

This comment has been minimized.

Copy link
@Vultraz

Vultraz Feb 4, 2018

Author Member

Ah, thanks.

unit.loc = {x, y}
* Multiplayer:
* Dark Forecast: Fixed broken faction and leader selection.
* Rename the Khalifate to Dunefolk. This includes renaming all the faction's
Expand Down
21 changes: 18 additions & 3 deletions src/scripting/lua_unit.cpp
Expand Up @@ -440,9 +440,10 @@ static int impl_unit_set(lua_State *L)
} else {
const bool is_key_x = strcmp(m, "x") == 0;
const bool is_key_y = strcmp(m, "y") == 0;
const bool is_loc_key = strcmp(m, "loc") == 0;

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

if(!gb) {
Expand All @@ -453,9 +454,23 @@ static int impl_unit_set(lua_State *L)
map_location dst = src;

if(is_key_x) {
dst.set_wml_x(static_cast<int>(luaL_checkinteger(L, 3)));
dst.set_wml_x(luaL_checkinteger(L, 3));
} else if(is_key_y) {
dst.set_wml_y(luaL_checkinteger(L, 3));
} else if(lua_istable(L, 3)) { // is_loc_key
const auto& v = lua_check<std::vector<int>>(L, 3);

if(v.size() != 2) {
std::string err_msg = "both x, y coordinates not provided when assigning unit.loc";
return luaL_argerror(L, 2, err_msg.c_str());
}

dst.set_wml_x(v[0]);
dst.set_wml_y(v[1]);
} else {
dst.set_wml_y(static_cast<int>(luaL_checkinteger(L, 3)));
// This should only happen if trying to assign unit.loc with a non-table value.
std::string err_msg = "unknown value type for unit.loc, expected table";
return luaL_argerror(L, 2, err_msg.c_str());
}

// TODO: could probably be relegated to a helper function.
Expand Down

0 comments on commit 883b4f1

Please sign in to comment.