Skip to content

Commit

Permalink
Properly port [heal_unit] to Lua
Browse files Browse the repository at this point in the history
  • Loading branch information
CelticMinstrel committed Aug 6, 2016
1 parent bc4efcc commit b24e56b
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
2 changes: 2 additions & 0 deletions changelog
Expand Up @@ -24,6 +24,8 @@ Version 1.13.5+dev:
* New [remove_object] tag which removes applied [object]s from matched units;
it can filter on the entire [object] WML. The most efficient use is to
remove all objects with a specific duration value.
* New key ability_type in standard unit filters, which matches if the unit has
any ability of the specified type (tag name).
* Lua API:
* Added new functions wesnoth.fire_event_by_id and fire_event_by_name. The old
function wesnoth.fire_event is now an alias for wesnoth.fire_event_by_name
Expand Down
62 changes: 61 additions & 1 deletion data/lua/wml-tags.lua
Expand Up @@ -631,7 +631,67 @@ function wml_actions.unpetrify(cfg)
end

function wml_actions.heal_unit(cfg)
wesnoth.heal_unit(cfg)
local healers = helper.get_child("filter_second")
if healers then
healers = wesnoth.get_units{
ability_type = "heals",
T["and"](healers)
}
else
healers = {}
end

local who = helper.get_child("filter")
if who then
who = wesnoth.get_units(who)
else
who = wesnoth.get_units{
x = wesnoth.current.event_context.x1,
y = wesnoth.current.event_context.y1
}
end

local heal_full = cfg.amount == "full"
local moves_full = cfg.moves == "full"
local heal_amount_set = false
for i,u in ipairs(who) do
local heal_amount = u.max_hitpoints - u.hitpoints
if heal_full then
u.hitpoints = u.max_hitpoints
else
heal_amount = math.min(math.max(1, cfg.amount), heal_amount)
u.hitpoints = u.hitpoints + heal_amount
end

if moves_full then
u.moves = u.max_moves
else
u.moves = math.min(u.max_moves, u.moves + (cfg.moves or 0))
end

if cfg.restore_attacks then
u.attacks_left = u.max_attacks
end

if cfg.restore_statuses then
u.status.poisoned = false
u.status.petrified = false
u.status.slowed = false
u.status.unhealable = false
end

if ~heal_amount_set then
heal_amount_set = true
wesnoth.set_variable("heal_amount", heal_amount)
end

if cfg.animate then
wesnoth.animate_unit{
T.filter(healers),
flag = "healing"
}
end
end
end

function wml_actions.transform_unit(cfg)
Expand Down
1 change: 1 addition & 0 deletions src/scripting/game_lua_kernel.cpp
Expand Up @@ -2366,6 +2366,7 @@ int game_lua_kernel::intf_find_cost_map(lua_State *L)
int game_lua_kernel::intf_heal_unit(lua_State *L)
{
vconfig cfg(luaW_checkvconfig(L, 1));
WRN_LUA << "wesnoth.heal_unit is deprecated\n";

const game_events::queued_event &event_info = get_event_info();

Expand Down
13 changes: 13 additions & 0 deletions src/units/filter.cpp
Expand Up @@ -359,6 +359,19 @@ bool basic_unit_filter_impl::internal_matches_filter(const unit & u, const map_l
if (!match) return false;
}

if (!vcfg["ability_type"].empty())
{
bool match = false;

for (const std::string& ability : utils::split(vcfg["ability_type"])) {
if (u.has_ability_type(ability)) {
match = true;
break;
}
}
if (!match) return false;
}

if (!vcfg["race"].empty()) {
std::vector<std::string> races = utils::split(vcfg["race"]);
if (std::find(races.begin(), races.end(), u.race()->id()) == races.end()) {
Expand Down

0 comments on commit b24e56b

Please sign in to comment.