Skip to content

Commit

Permalink
Align formula and Lua views of unit types
Browse files Browse the repository at this point in the history
- New key in both - race
  • Loading branch information
CelticMinstrel committed Mar 30, 2016
1 parent d7c5827 commit 64892b8
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
10 changes: 7 additions & 3 deletions changelog
Expand Up @@ -127,6 +127,10 @@ Version 1.13.4+dev:
* abilities - list of the IDs of all abilities
* Additional fields in table returned by wesnoth.get_terrain_info:
* icon, editor_image, light
* Additional fields in unit type proxu:
* race, id, alignment
* attacks, which returns the same thing as unit.attacks
* abilities, same as unit.abilities
* LuaAI:
* The table returned by check_*() now has a "result" field which
gives a description of the action's result; similar to "status"
Expand Down Expand Up @@ -165,9 +169,9 @@ Version 1.13.4+dev:
total_movement -> max_moves
movement_left -> moves
states -> status
* Nearly All side, weapon, and terrain attributes available to Lua code
are now also exposed to WFL. The exceptions are mainly translatable
strings.
* Nearly all unit type, side, weapon, and terrain attributes available
to Lua code are now also exposed to WFL. The exceptions are mainly
translatable strings.
* The 'special' attribute of weapons was renamed to 'specials', and it now
contains the special IDs rather than their translateable names.
* New syntax features:
Expand Down
21 changes: 18 additions & 3 deletions src/formula/callable_objects.cpp
Expand Up @@ -376,6 +376,8 @@ variant unit_type_callable::get_value(const std::string& key) const
return variant(u_.type_name());
} else if(key == "alignment") {
return variant(lexical_cast<std::string>(u_.alignment()));
} else if(key == "race") {
return variant(u_.race_id());
} else if(key == "abilities") {
std::vector<std::string> abilities = u_.get_ability_list();
std::vector<variant> res;
Expand All @@ -388,20 +390,28 @@ variant unit_type_callable::get_value(const std::string& key) const
res.push_back( variant(*it) );
}
return variant( &res );
} else if(key == "traits") {
std::vector<variant> res;

FOREACH(const AUTO& config , u_.possible_traits())
{
res.push_back(variant(config["id"].str()));
}
return variant(&res);
} else if(key == "attacks") {
std::vector<attack_type> att = u_.attacks();
std::vector<variant> res;

for( std::vector<attack_type>::iterator i = att.begin(); i != att.end(); ++i)
res.push_back(variant(new attack_type_callable(*i)));
return variant(&res);
} else if(key == "hitpoints") {
} else if(key == "hitpoints" || key == "max_hitpoints") {
return variant(u_.hitpoints());
} else if(key == "experience") {
} else if(key == "experience" || key == "max_experience") {
return variant(u_.experience_needed(true));
} else if(key == "level") {
return variant(u_.level());
} else if(key == "total_movement") {
} else if(key == "total_movement" || key == "max_moves" || key == "moves") {
return variant(u_.movement());
} else if(key == "unpoisonable") {
return variant(u_.musthave_status("unpoisonable"));
Expand All @@ -411,6 +421,8 @@ variant unit_type_callable::get_value(const std::string& key) const
return variant(u_.musthave_status("unplagueable"));
} else if(key == "cost") {
return variant(u_.cost());
} else if(key == "recall_cost") {
return variant(u_.recall_cost());
} else if(key == "usage") {
return variant(u_.usage());
} else {
Expand All @@ -423,15 +435,18 @@ void unit_type_callable::get_inputs(std::vector<game_logic::formula_input>* inpu
using game_logic::FORMULA_READ_ONLY;
inputs->push_back(game_logic::formula_input("id", FORMULA_READ_ONLY));
inputs->push_back(game_logic::formula_input("type", FORMULA_READ_ONLY));
inputs->push_back(game_logic::formula_input("race", FORMULA_READ_ONLY));
inputs->push_back(game_logic::formula_input("alignment", FORMULA_READ_ONLY));
inputs->push_back(game_logic::formula_input("abilities", FORMULA_READ_ONLY));
inputs->push_back(game_logic::formula_input("traits", FORMULA_READ_ONLY));
inputs->push_back(game_logic::formula_input("attacks", FORMULA_READ_ONLY));
inputs->push_back(game_logic::formula_input("hitpoints", FORMULA_READ_ONLY));
inputs->push_back(game_logic::formula_input("experience", FORMULA_READ_ONLY));
inputs->push_back(game_logic::formula_input("level", FORMULA_READ_ONLY));
inputs->push_back(game_logic::formula_input("total_movement", FORMULA_READ_ONLY));
inputs->push_back(game_logic::formula_input("undead", FORMULA_READ_ONLY));
inputs->push_back(game_logic::formula_input("cost", FORMULA_READ_ONLY));
inputs->push_back(game_logic::formula_input("recall_cost", FORMULA_READ_ONLY));
inputs->push_back(game_logic::formula_input("usage", FORMULA_READ_ONLY));
}

Expand Down
9 changes: 5 additions & 4 deletions src/scripting/game_lua_kernel.cpp
Expand Up @@ -518,8 +518,8 @@ static int impl_unit_variables_get(lua_State *L)
return luaW_pushvariable(L, v) ? 1 : 0;
}
/**
* Gets the attacks of a unit (__index metamethod).
* - Arg 1: table containing the userdata containing the unit id.
* Gets the attacks of a unit or unit type (__index metamethod).
* - Arg 1: table containing the userdata containing the unit or unit type.
* - Arg 2: index (int) or id (string) identifying a particular attack.
* - Ret 1: the unit's attacks.
*/
Expand All @@ -530,11 +530,12 @@ static int impl_unit_attacks_get(lua_State *L)
}
lua_rawgeti(L, 1, -1);
const unit* u = luaW_tounit(L, -1);
if (!u) {
const unit_type* ut = static_cast<const unit_type*>(luaL_testudata(L, -1, "unit type"));
if (!u && !ut) {
return luaL_argerror(L, 1, "unknown unit");
}
const attack_type* attack = NULL;
const std::vector<attack_type>& attacks = u->attacks();
const std::vector<attack_type>& attacks = u ? u->attacks() : ut->attacks();
if(!lua_isnumber(L,2)) {
std::string attack_id = luaL_checkstring(L, 2);
BOOST_FOREACH(const attack_type& at, attacks) {
Expand Down
18 changes: 18 additions & 0 deletions src/scripting/lua_unit_type.cpp
Expand Up @@ -15,6 +15,7 @@
#include "scripting/lua_unit_type.hpp"

#include "scripting/lua_common.hpp"
#include "scripting/push_check.hpp"
#include "units/types.hpp"

#include <boost/foreach.hpp>
Expand Down Expand Up @@ -47,6 +48,9 @@ static int impl_unit_type_get(lua_State *L)

// Find the corresponding attribute.
return_tstring_attrib("name", ut.type_name());
return_string_attrib("id", ut.id());
return_string_attrib("alignment", ut.alignment().to_string());
return_string_attrib("race", ut.race_id());
return_int_attrib("max_hitpoints", ut.hitpoints());
return_int_attrib("max_moves", ut.movement());
return_int_attrib("max_experience", ut.experience_needed());
Expand All @@ -64,6 +68,20 @@ static int impl_unit_type_get(lua_State *L)
}
return 1;
}
if (strcmp(m, "abilities") == 0) {
lua_push(L, ut.get_ability_list());
return 1;
}
if (strcmp(m, "attacks") == 0) {
lua_createtable(L, 1, 0);
lua_pushvalue(L, 1);
// hack: store the unit_type at -1 because we want positive indices to refer to the attacks.
lua_rawseti(L, -2, -1);
lua_pushlightuserdata(L, uattacksKey);
lua_rawget(L, LUA_REGISTRYINDEX);
lua_setmetatable(L, -2);
return 1;
}
return 0;
}

Expand Down

0 comments on commit 64892b8

Please sign in to comment.