Lua: Flip functions moved to the units module

This means that they are now added to the units module by default in C++, and only duplicated to the Wesnoth module in Lua.

Some additional functions were moved:
- wesnoth.create_unit -> wesnoth.units.create
- wesnoth.get_units -> wesnoth.units.find
- wesnoth.get_unit -> wesnoth.units.get

Deprecated wesnoth.get_recall_units in favour of wesnoth.get_units, which has gained the ability to match units on the recall list if x="recall" or y="recall" appears in the filter at toplevel.

The wesnoth.units module now acts like a metatable for unit userdata, meaning that any functions (or attributes) added to the module will be visible through any unit.
This commit is contained in:
Celtic Minstrel 2019-11-14 23:38:15 -05:00
parent 3772e633fa
commit 4dc986f028
4 changed files with 63 additions and 78 deletions

View file

@ -43,6 +43,9 @@
* New read-write keys in unit userdata: ellipse, halo, description, renamable
* New functions for working with WML: wml.merge, wml.diff, wml.patch
* wesnoth.wml_matches_filter renamed to wml.matches_filter (the old name still works)
* Moved to units module: wesnoth.create_unit, wesnoth.get_unit, wesnoth.get_units, wesnoth.get_recall_units
* The wesnoth.units module now acts like a metatable for unit userdata.
* wesnoth.units.find (formerly wesnoth.get_units) now supports x="recall",y="recall"
### WML engine
* Support upkeep in StandardUnitFilter
* [effect]apply_to=variation now supports heal_full

View file

@ -462,7 +462,7 @@ if wesnoth.kernel_type() == "Game Lua Kernel" then
wesnoth.interface = {
delay = wesnoth.delay,
float_label = wesnoth.float_label,
select_unit = wesnoth.select_unit,
select_unit = wesnoth.units.select,
highlight_hex = wesnoth.highlight_hex,
deselect_hex = wesnoth.deselect_hex,
get_selected_hex = wesnoth.get_selected_tile,
@ -507,25 +507,6 @@ if wesnoth.kernel_type() == "Game Lua Kernel" then
end
--[========[Units module]========]
-- TODO: Eventually this could actually be the units metatable, allowing people to add new methods to it.
wesnoth.units = {}
wesnoth.units.matches = wesnoth.match_unit
wesnoth.units.to_recall = wesnoth.put_recall_unit
wesnoth.units.to_map = wesnoth.put_unit
wesnoth.units.erase = wesnoth.erase_unit
wesnoth.units.clone = wesnoth.copy_unit
wesnoth.units.extract = wesnoth.extract_unit
wesnoth.units.advance = wesnoth.advance_unit
wesnoth.units.add_modification = wesnoth.add_modification
wesnoth.units.remove_modifications = wesnoth.remove_modifications
wesnoth.units.resistance = wesnoth.unit_resistance
wesnoth.units.defense = wesnoth.unit_defense
wesnoth.units.movement = wesnoth.unit_movement_cost
wesnoth.units.vision = wesnoth.unit_vision_cost
wesnoth.units.jamming = wesnoth.unit_jamming_cost
wesnoth.units.ability = wesnoth.unit_ability
wesnoth.units.transform = wesnoth.transform_unit
wesnoth.units.select = wesnoth.select_unit
--! Modifies all the units satisfying the given @a filter.
--! @param vars key/value pairs that need changing.
@ -541,6 +522,21 @@ if wesnoth.kernel_type() == "Game Lua Kernel" then
end
end
end
--! This function is deprecated, do not call directly.
function wesnoth.get_recall_units(filter)
filter = filter or {}
if getmetatable(filter) == 'wml object' then
filter = filter.__literal
filter.x = 'recall'
filter.y = 'recall'
filter = wml.tovconfig(filter)
else
filter.x = 'recall'
filter.y = 'recall'
end
return wesnoth.units.find(filter)
end
end
--[========[GUI2 Dialog Manipulations]========]
@ -597,7 +593,6 @@ if wesnoth.kernel_type() == "Game Lua Kernel" then
-- Interface module
wesnoth.delay = wesnoth.deprecate_api('wesnoth.delay', 'wesnoth.interface.delay', 1, nil, wesnoth.interface.delay)
wesnoth.float_label = wesnoth.deprecate_api('wesnoth.float_label', 'wesnoth.interface.float_label', 1, nil, wesnoth.interface.float_label)
wesnoth.select_unit = wesnoth.deprecate_api('wesnoth.select_unit', 'wesnoth.interface.select_unit', 1, nil, wesnoth.interface.select_unit)
wesnoth.highlight_hex = wesnoth.deprecate_api('wesnoth.highlight_hex', 'wesnoth.interface.highlight_hex', 1, nil, wesnoth.interface.highlight_hex)
wesnoth.deselect_hex = wesnoth.deprecate_api('wesnoth.deselect_hex', 'wesnoth.interface.deselect_hex', 1, nil, wesnoth.interface.deselect_hex)
wesnoth.get_selected_tile = wesnoth.deprecate_api('wesnoth.get_selected_tile', 'wesnoth.interface.get_selected_hex', 1, nil, wesnoth.interface.get_selected_hex)
@ -629,6 +624,10 @@ if wesnoth.kernel_type() == "Game Lua Kernel" then
wesnoth.unit_ability = wesnoth.deprecate_api('wesnoth.unit_ability', 'wesnoth.units.ability', 1, nil, wesnoth.units.ability)
wesnoth.transform_unit = wesnoth.deprecate_api('wesnoth.transform_unit', 'wesnoth.units.transform', 1, nil, wesnoth.units.transform)
wesnoth.select_unit = wesnoth.deprecate_api('wesnoth.select_unit', 'wesnoth.units.select', 1, nil, wesnoth.units.select)
wesnoth.create_unit = wesnoth.deprecate_api('wesnoth.create_unit', 'wesnoth.units.create', 1, nil, wesnoth.units.create)
wesnoth.get_unit = wesnoth.deprecate_api('wesnoth.get_unit', 'wesnoth.units.get', 1, nil, wesnoth.units.get)
wesnoth.get_units = wesnoth.deprecate_api('wesnoth.get_units', 'wesnoth.units.find', 1, nil, wesnoth.units.find)
wesnoth.get_recall_units = wesnoth.deprecate_api('wesnoth.get_units', 'wesnoth.units.find with x="recall", y="recall"', 1, nil, wesnoth.get_recall_units)
end
wesnoth.tovconfig = wesnoth.deprecate_api('wesnoth.tovconfig', 'wml.tovconfig', 1, nil, wml.tovconfig)
wesnoth.debug = wesnoth.deprecate_api('wesnoth.debug', 'wml.tostring', 1, nil, wml.tostring)

View file

@ -559,6 +559,11 @@ int game_lua_kernel::intf_get_displayed_unit(lua_State *L)
int game_lua_kernel::intf_get_units(lua_State *L)
{
vconfig filter = luaW_checkvconfig(L, 1, true);
if(filter["x"] == "recall" || filter["y"] == "recall") {
lua_pop(L, 1);
return intf_get_recall_units(L);
}
unit_filter filt(filter);
std::vector<const unit*> units;
@ -4149,11 +4154,7 @@ game_lua_kernel::game_lua_kernel(game_state & gs, play_controller & pc, reports
// Put some callback functions in the scripting environment.
static luaL_Reg const callbacks[] {
{ "add_known_unit", &intf_add_known_unit },
{ "add_modification", &intf_add_modification },
{ "advance_unit", &intf_advance_unit },
{ "copy_unit", &intf_copy_unit },
{ "create_animator", &dispatch<&game_lua_kernel::intf_create_animator> },
{ "create_unit", &intf_create_unit },
{ "debug_ai", &intf_debug_ai },
{ "eval_conditional", &intf_eval_conditional },
{ "get_era", &intf_get_era },
@ -4161,15 +4162,8 @@ game_lua_kernel::game_lua_kernel(game_state & gs, play_controller & pc, reports
{ "get_viewing_side", &intf_get_viewing_side },
{ "invoke_synced_command", &intf_invoke_synced_command },
{ "modify_ai", &intf_modify_ai_old },
{ "remove_modifications", &intf_remove_modifications },
{ "set_music", &intf_set_music },
{ "sound_volume", &intf_sound_volume },
{ "transform_unit", &intf_transform_unit },
{ "unit_defense", &intf_unit_defense },
{ "unit_movement_cost", &intf_unit_movement_cost },
{ "unit_vision_cost", &intf_unit_vision_cost },
{ "unit_jamming_cost", &intf_unit_jamming_cost },
{ "unit_resistance", &intf_unit_resistance },
{ "unsynced", &intf_do_unsynced },
{ "add_event_handler", &dispatch<&game_lua_kernel::intf_add_event > },
{ "add_fog", &dispatch2<&game_lua_kernel::intf_toggle_fog, false > },
@ -4186,8 +4180,6 @@ game_lua_kernel::game_lua_kernel(game_state & gs, play_controller & pc, reports
{ "delay", &dispatch<&game_lua_kernel::intf_delay > },
{ "end_turn", &dispatch<&game_lua_kernel::intf_end_turn > },
{ "end_level", &dispatch<&game_lua_kernel::intf_end_level > },
{ "erase_unit", &dispatch<&game_lua_kernel::intf_erase_unit > },
{ "extract_unit", &dispatch<&game_lua_kernel::intf_extract_unit > },
{ "find_cost_map", &dispatch<&game_lua_kernel::intf_find_cost_map > },
{ "find_path", &dispatch<&game_lua_kernel::intf_find_path > },
{ "find_reach", &dispatch<&game_lua_kernel::intf_find_reach > },
@ -4201,7 +4193,6 @@ game_lua_kernel::game_lua_kernel(game_state & gs, play_controller & pc, reports
{ "get_locations", &dispatch<&game_lua_kernel::intf_get_locations > },
{ "get_map_size", &dispatch<&game_lua_kernel::intf_get_map_size > },
{ "get_mouseover_tile", &dispatch<&game_lua_kernel::intf_get_mouseover_tile > },
{ "get_recall_units", &dispatch<&game_lua_kernel::intf_get_recall_units > },
{ "get_selected_tile", &dispatch<&game_lua_kernel::intf_get_selected_tile > },
{ "get_sides", &dispatch<&game_lua_kernel::intf_get_sides > },
{ "get_sound_source", &dispatch<&game_lua_kernel::intf_get_sound_source > },
@ -4209,8 +4200,6 @@ game_lua_kernel::game_lua_kernel(game_state & gs, play_controller & pc, reports
{ "get_terrain", &dispatch<&game_lua_kernel::intf_get_terrain > },
{ "get_terrain_info", &dispatch<&game_lua_kernel::intf_get_terrain_info > },
{ "get_time_of_day", &dispatch<&game_lua_kernel::intf_get_time_of_day > },
{ "get_unit", &dispatch<&game_lua_kernel::intf_get_unit > },
{ "get_units", &dispatch<&game_lua_kernel::intf_get_units > },
{ "get_variable", &dispatch<&game_lua_kernel::intf_get_variable > },
{ "get_side_variable", &dispatch<&game_lua_kernel::intf_get_side_variable > },
{ "get_villages", &dispatch<&game_lua_kernel::intf_get_villages > },
@ -4224,13 +4213,10 @@ game_lua_kernel::game_lua_kernel(game_state & gs, play_controller & pc, reports
{ "log", &dispatch<&game_lua_kernel::intf_log > },
{ "match_location", &dispatch<&game_lua_kernel::intf_match_location > },
{ "match_side", &dispatch<&game_lua_kernel::intf_match_side > },
{ "match_unit", &dispatch<&game_lua_kernel::intf_match_unit > },
{ "message", &dispatch<&game_lua_kernel::intf_message > },
{ "open_help", &dispatch<&game_lua_kernel::intf_open_help > },
{ "play_sound", &dispatch<&game_lua_kernel::intf_play_sound > },
{ "print", &dispatch<&game_lua_kernel::intf_print > },
{ "put_recall_unit", &dispatch<&game_lua_kernel::intf_put_recall_unit > },
{ "put_unit", &dispatch<&game_lua_kernel::intf_put_unit > },
{ "redraw", &dispatch<&game_lua_kernel::intf_redraw > },
{ "remove_event_handler", &dispatch<&game_lua_kernel::intf_remove_event > },
{ "remove_fog", &dispatch2<&game_lua_kernel::intf_toggle_fog, true > },
@ -4243,7 +4229,6 @@ game_lua_kernel::game_lua_kernel(game_state & gs, play_controller & pc, reports
{ "select_hex", &dispatch<&game_lua_kernel::intf_select_hex > },
{ "set_time_of_day", &dispatch<&game_lua_kernel::intf_set_time_of_day > },
{ "deselect_hex", &dispatch<&game_lua_kernel::intf_deselect_hex > },
{ "select_unit", &dispatch<&game_lua_kernel::intf_select_unit > },
{ "skip_messages", &dispatch<&game_lua_kernel::intf_skip_messages > },
{ "is_fogged", &dispatch2<&game_lua_kernel::intf_get_fog_or_shroud, true > },
{ "is_shrouded", &dispatch2<&game_lua_kernel::intf_get_fog_or_shroud, false > },
@ -4264,7 +4249,6 @@ game_lua_kernel::game_lua_kernel(game_state & gs, play_controller & pc, reports
{ "terrain_mask", &dispatch<&game_lua_kernel::intf_terrain_mask > },
{ "zoom", &dispatch<&game_lua_kernel::intf_zoom > },
{ "teleport", &dispatch<&game_lua_kernel::intf_teleport > },
{ "unit_ability", &dispatch<&game_lua_kernel::intf_unit_ability > },
{ "view_locked", &dispatch<&game_lua_kernel::intf_view_locked > },
{ "place_shroud", &dispatch2<&game_lua_kernel::intf_shroud_op, true > },
{ "remove_shroud", &dispatch2<&game_lua_kernel::intf_shroud_op, false > },
@ -4330,6 +4314,40 @@ game_lua_kernel::game_lua_kernel(game_state & gs, play_controller & pc, reports
lua_setmetatable(L, -2);
lua_setfield(L, -2, "current");
lua_pop(L, 1);
// Create the units module
cmd_log_ << "Adding units module...\n";
static luaL_Reg const unit_callbacks[] {
{"advance", &intf_advance_unit},
{"clone", &intf_copy_unit},
{"erase", &dispatch<&game_lua_kernel::intf_erase_unit>},
{"extract", &dispatch<&game_lua_kernel::intf_extract_unit>},
{"matches", &dispatch<&game_lua_kernel::intf_match_unit>},
{"select", &dispatch<&game_lua_kernel::intf_select_unit>},
{"to_map", &dispatch<&game_lua_kernel::intf_put_unit>},
{"to_recall", &dispatch<&game_lua_kernel::intf_put_recall_unit>},
{"transform", &intf_transform_unit},
{"ability", &dispatch<&game_lua_kernel::intf_unit_ability>},
{"defense", &intf_unit_defense},
{"jamming", &intf_unit_jamming_cost},
{"movement", &intf_unit_movement_cost},
{"resistance", intf_unit_resistance},
{"vision", &intf_unit_vision_cost},
{"add_modification", &intf_add_modification},
{"remove_modifications", &intf_remove_modifications},
// Static functions
{"create", &intf_create_unit},
{"find", &dispatch<&game_lua_kernel::intf_get_units>},
{"get", &dispatch<&game_lua_kernel::intf_get_unit>},
{ nullptr, nullptr }
};
lua_getglobal(L, "wesnoth");
lua_newtable(L);
luaL_setfuncs(L, unit_callbacks, 0);
lua_setfield(L, -2, "units");
lua_pop(L, 1);
// Create the playlist table with its metatable
cmd_log_ << lua_audio::register_table(L);

View file

@ -404,7 +404,7 @@ static int impl_unit_get(lua_State *L)
: u.big_profile());
return_cfg_attrib("__cfg", u.write(cfg); u.get_location().write(cfg));
if(luaW_getmetafield(L, 1, m)) {
if(luaW_getglobal(L, "wesnoth", "units", m)) {
return 1;
}
return 0;
@ -662,41 +662,6 @@ namespace lua_units {
lua_setfield(L, -2, "__newindex");
lua_pushstring(L, "unit");
lua_setfield(L, -2, "__metatable");
// Unit methods
luaW_getglobal(L, "wesnoth", "match_unit");
lua_setfield(L, -2, "matches");
luaW_getglobal(L, "wesnoth", "put_recall_unit");
lua_setfield(L, -2, "to_recall");
luaW_getglobal(L, "wesnoth", "put_unit");
lua_setfield(L, -2, "to_map");
luaW_getglobal(L, "wesnoth", "erase_unit");
lua_setfield(L, -2, "erase");
luaW_getglobal(L, "wesnoth", "copy_unit");
lua_setfield(L, -2, "clone");
luaW_getglobal(L, "wesnoth", "extract_unit");
lua_setfield(L, -2, "extract");
luaW_getglobal(L, "wesnoth", "advance_unit");
lua_setfield(L, -2, "advance");
luaW_getglobal(L, "wesnoth", "add_modification");
lua_setfield(L, -2, "add_modification");
luaW_getglobal(L, "wesnoth", "unit_resistance");
lua_setfield(L, -2, "resistance");
luaW_getglobal(L, "wesnoth", "remove_modifications");
lua_setfield(L, -2, "remove_modifications");
luaW_getglobal(L, "wesnoth", "unit_defense");
lua_setfield(L, -2, "defense");
luaW_getglobal(L, "wesnoth", "unit_movement_cost");
lua_setfield(L, -2, "movement");
luaW_getglobal(L, "wesnoth", "unit_vision_cost");
lua_setfield(L, -2, "vision");
luaW_getglobal(L, "wesnoth", "unit_jamming_cost");
lua_setfield(L, -2, "jamming");
luaW_getglobal(L, "wesnoth", "unit_ability");
lua_setfield(L, -2, "ability");
luaW_getglobal(L, "wesnoth", "transform_unit");
lua_setfield(L, -2, "transform");
luaW_getglobal(L, "wesnoth", "select_unit");
lua_setfield(L, -2, "select");
// Create the unit status metatable.
cmd_out << "Adding unit status metatable...\n";