Add __tostring metamethods to textdomains, races, unit types, weapons, and sides
This commit is contained in:
parent
d53ac4b496
commit
61d0da9c84
7 changed files with 91 additions and 5 deletions
|
@ -54,6 +54,7 @@
|
|||
* Moved to new sides module: wesnoth.get_sides, wesnoth.is_enemy, wesnoth.match_side, wesnoth.get_starting_location, wesnoth.set_side_id, all AI-related functions
|
||||
* Functions that previously only took a side index as the first parameter now also accept the side proxy userdata.
|
||||
* The wesnoth.sides module acts like a metatable for the side userdata.
|
||||
* Added `__tostring` functions to a number of Wesnoth userdata types.
|
||||
### WML engine
|
||||
* Support upkeep in StandardUnitFilter
|
||||
* [effect]apply_to=variation now supports heal_full
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
#include "scripting/lua_common.hpp"
|
||||
|
||||
#include "config.hpp"
|
||||
#include "scripting/push_check.hpp"
|
||||
#include "scripting/lua_unit.hpp"
|
||||
#include "tstring.hpp" // for t_string
|
||||
#include "variable.hpp" // for vconfig
|
||||
|
@ -75,6 +76,15 @@ static int impl_gettext(lua_State *L)
|
|||
return 1;
|
||||
}
|
||||
|
||||
static int impl_gettext_tostr(lua_State* L)
|
||||
{
|
||||
char* d = static_cast<char*>(lua_touserdata(L, 1));
|
||||
using namespace std::literals;
|
||||
std::string str = "textdomain: "s + d;
|
||||
lua_push(L, str);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an interface for gettext
|
||||
* - Arg 1: string containing the domain.
|
||||
|
@ -403,6 +413,7 @@ std::string register_gettext_metatable(lua_State *L)
|
|||
|
||||
static luaL_Reg const callbacks[] {
|
||||
{ "__call", &impl_gettext},
|
||||
{ "__tostring", &impl_gettext_tostr},
|
||||
{ nullptr, nullptr }
|
||||
};
|
||||
luaL_setfuncs(L, callbacks, 0);
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
#include "units/race.hpp"
|
||||
#include "scripting/lua_common.hpp"
|
||||
#include "scripting/push_check.hpp"
|
||||
#include "units/types.hpp"
|
||||
|
||||
#include <string>
|
||||
|
@ -41,12 +42,8 @@ static const char * Gen = "name generator";
|
|||
*/
|
||||
static int impl_race_get(lua_State* L)
|
||||
{
|
||||
const unit_race& race = luaW_checkrace(L, 1);
|
||||
char const* m = luaL_checkstring(L, 2);
|
||||
lua_pushstring(L, "id");
|
||||
lua_rawget(L, 1);
|
||||
const unit_race* raceptr = unit_types.find_race(lua_tostring(L, -1));
|
||||
if(!raceptr) return luaL_argerror(L, 1, "unknown race");
|
||||
const unit_race& race = *raceptr;
|
||||
|
||||
return_tstring_attrib("description", race.description());
|
||||
return_tstring_attrib("name", race.name());
|
||||
|
@ -89,6 +86,18 @@ static int impl_race_get(lua_State* L)
|
|||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Turns a lua proxy race to string. (__tostring metamethod)
|
||||
*/
|
||||
static int impl_race_tostring(lua_State* L)
|
||||
{
|
||||
const unit_race& race = luaW_checkrace(L, 1);
|
||||
std::ostringstream str;
|
||||
str << "race: <" << race.id() << '>';
|
||||
lua_push(L, str.str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
namespace lua_race {
|
||||
|
||||
std::string register_metatable(lua_State * L)
|
||||
|
@ -97,6 +106,7 @@ namespace lua_race {
|
|||
|
||||
static luaL_Reg const callbacks[] {
|
||||
{ "__index", &impl_race_get},
|
||||
{ "__tostring", &impl_race_tostring},
|
||||
{ nullptr, nullptr }
|
||||
};
|
||||
luaL_setfuncs(L, callbacks, 0);
|
||||
|
@ -128,3 +138,15 @@ void luaW_pushracetable(lua_State *L)
|
|||
lua_setfield(L, -2, race.first.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
const unit_race& luaW_checkrace(lua_State* L, int idx)
|
||||
{
|
||||
lua_pushstring(L, "id");
|
||||
lua_rawget(L, idx);
|
||||
const unit_race* raceptr = unit_types.find_race(lua_tostring(L, -1));
|
||||
if(!raceptr) {
|
||||
luaL_argerror(L, idx, "unknown race");
|
||||
throw "UNREACHABLE";
|
||||
}
|
||||
return *raceptr;
|
||||
}
|
||||
|
|
|
@ -30,3 +30,4 @@ namespace lua_race {
|
|||
// Create a lua reference to the race.
|
||||
void luaW_pushrace(lua_State *, const unit_race &);
|
||||
void luaW_pushracetable(lua_State *);
|
||||
const unit_race& luaW_checkrace(lua_State*, int);
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
#include "scripting/lua_team.hpp"
|
||||
|
||||
#include "scripting/lua_common.hpp"
|
||||
#include "scripting/push_check.hpp"
|
||||
#include "scripting/game_lua_kernel.hpp"
|
||||
#include "team.hpp"
|
||||
#include "resources.hpp" // for gameboard
|
||||
|
@ -126,6 +127,24 @@ static int impl_side_get(lua_State *L)
|
|||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Turns a lua proxy side to string. (__tostring metamethod)
|
||||
*/
|
||||
static int impl_side_tostring(lua_State* L)
|
||||
{
|
||||
const team& team = luaW_checkteam(L, 1);
|
||||
std::ostringstream str;
|
||||
|
||||
str << "side: <" << team.side();
|
||||
if(!team.side_name().empty()) {
|
||||
str << " " << team.side_name();
|
||||
}
|
||||
str << '>';
|
||||
|
||||
lua_push(L, str.str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets some data on a side (__newindex metamethod).
|
||||
* - Arg 1: full userdata containing the team.
|
||||
|
@ -287,6 +306,7 @@ namespace lua_team {
|
|||
{ "__index", &impl_side_get},
|
||||
{ "__newindex", &impl_side_set},
|
||||
{ "__eq", &impl_side_equal},
|
||||
{ "__tostring", &impl_side_tostring},
|
||||
{ nullptr, nullptr }
|
||||
};
|
||||
luaL_setfuncs(L, callbacks, 0);
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
#include "scripting/lua_common.hpp"
|
||||
#include "scripting/lua_unit.hpp"
|
||||
#include "scripting/lua_unit_type.hpp"
|
||||
#include "scripting/push_check.hpp"
|
||||
#include "units/unit.hpp"
|
||||
#include "units/attack_type.hpp"
|
||||
#include "utils/const_clone.hpp"
|
||||
|
@ -314,6 +315,18 @@ static int impl_unit_attack_equal(lua_State* L)
|
|||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Turns a lua proxy attack to string. (__tostring metamethod)
|
||||
*/
|
||||
static int impl_unit_attack_tostring(lua_State* L)
|
||||
{
|
||||
const_attack_ptr atk = luaW_checkweapon_ref(L, 1).cattack;
|
||||
std::ostringstream str;
|
||||
str << "weapon: <" << atk->id() << '>';
|
||||
lua_push(L, str.str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int impl_unit_attack_match(lua_State* L)
|
||||
{
|
||||
const_attack_ptr atk = luaW_toweapon(L, 1);
|
||||
|
@ -367,6 +380,8 @@ namespace lua_units {
|
|||
lua_setfield(L, -2, "__newindex");
|
||||
lua_pushcfunction(L, impl_unit_attack_equal);
|
||||
lua_setfield(L, -2, "__eq");
|
||||
lua_pushcfunction(L, impl_unit_attack_tostring);
|
||||
lua_setfield(L, -2, "__tostring");
|
||||
lua_pushcfunction(L, impl_unit_attack_collect);
|
||||
lua_setfield(L, -2, "__gc");
|
||||
lua_pushstring(L, uattackKey);
|
||||
|
|
|
@ -186,6 +186,20 @@ static int impl_unit_type_pairs(lua_State* L) {
|
|||
return 3;
|
||||
}
|
||||
|
||||
/**
|
||||
* Turns a lua proxy unit type to string. (__tostring metamethod)
|
||||
*/
|
||||
static int impl_unit_type_tostring(lua_State* L)
|
||||
{
|
||||
const unit_type& ut = luaW_checkunittype(L, 1);
|
||||
std::ostringstream str;
|
||||
|
||||
str << "unit type: <" << ut.id() << '>';
|
||||
|
||||
lua_push(L, str.str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
namespace lua_unit_type {
|
||||
std::string register_metatable(lua_State * L)
|
||||
{
|
||||
|
@ -193,6 +207,8 @@ namespace lua_unit_type {
|
|||
|
||||
lua_pushcfunction(L, impl_unit_type_get);
|
||||
lua_setfield(L, -2, "__index");
|
||||
lua_pushcfunction(L, impl_unit_type_tostring);
|
||||
lua_setfield(L, -2, "__tostring");
|
||||
lua_pushcfunction(L, impl_unit_type_equal);
|
||||
lua_setfield(L, -2, "__eq");
|
||||
lua_pushstring(L, UnitType);
|
||||
|
|
Loading…
Add table
Reference in a new issue