move feedung event to lua

this has 2 advantages:
1) It decreased savefile size since the event is no longer sotred in
each savefile.
2) The ABILITY_FEEDING no longer assumes its used from within
[abilities]
This commit is contained in:
gfgtdf 2016-05-20 14:25:16 +02:00
parent eaa7dac9c2
commit 2ba7ad5c43
3 changed files with 60 additions and 81 deletions

View file

@ -325,7 +325,6 @@ Enemy units cannot see this unit while it is in deep water, except if they have
[/hides]
#enddef
# wmllint: unbalanced-on
#define ABILITY_FEEDING
# Canned definition of the Feeding ability to be included in an
# [abilities] clause. Note: this is deliberately unbalanced WML,
@ -337,87 +336,7 @@ Enemy units cannot see this unit while it is in deep water, except if they have
female_name= _ "female^feeding"
description=_ "This unit gains 1 hitpoint added to its maximum whenever it kills a unit, except units that are immune to plague."
[/dummy]
# wmlxgettext: [abilities]
[/abilities]
[event]
id=ability_feeding_die
name=die
first_time_only=no
[filter]
[not]
status=unplagueable
[/not]
[/filter]
[filter_second]
ability=feeding
[/filter_second]
[unstore_unit]
variable=second_unit
{COLOR_HEAL}
text= _ "+1 max HP"
find_vacant=no
[/unstore_unit]
[object]
silent=yes
duration=forever
feeding=yes
[filter]
x,y=$x2,$y2
# Add the object only once, so the unit is not bloated by an object per kill
[not]
[filter_wml]
[modifications]
[object]
feeding=yes
[/object]
[/modifications]
[/filter_wml]
[/not]
[/filter]
[effect]
apply_to=hitpoints
increase_total=1
increase=1
[/effect]
# If the unit already had the object, update its and the unit's properties
[else]
{FOREACH second_unit.modifications.object i}
[if]
[variable]
name=second_unit.modifications.object[$i].feeding
boolean_equals=yes
[/variable]
[then]
{VARIABLE_OP second_unit.modifications.object[$i].effect.increase_total add 1}
{VARIABLE_OP second_unit.hitpoints add 1}
{VARIABLE_OP second_unit.max_hitpoints add 1}
# Not needed, so might as well remove these
{CLEAR_VARIABLE second_unit.modifications.object[$i].filter,second_unit.modifications.object[$i].else}
[unstore_unit]
variable=second_unit
find_vacant=no
[/unstore_unit]
[/then]
[/if]
{NEXT i}
[/else]
[/object]
[/event]
[+abilities]
# wmlxgettext: [/abilities]
#enddef
# wmllint: unbalanced-off
#weapons specials

59
data/lua/feeding.lua Normal file
View file

@ -0,0 +1,59 @@
local helper = wesnoth.require("lua/helper.lua")
local _ = wesnoth.textdomain 'wesnoth-help'
local T = helper.set_wml_tag_metatable {}
-- Code taken form 2p_dark_forecast.lua
-- TODO: maybe put it in a place where everyone can use it?
local event_handlers = {}
local old_on_event = wesnoth.game_events.on_event or function(eventname) end
wesnoth.game_events.on_event = function(eventname)
for k,v in pairs(event_handlers[eventname] or {}) do
v()
end
old_on_event(eventname)
end
local function on_event(eventname, handler)
eventname = string.gsub(eventname, " ", "_")
event_handlers[eventname] = event_handlers[eventname] or {}
table.insert(event_handlers[eventname], handler)
end
-- end code taken from dark forecast.
-- The feeding event code
on_event("die", function()
local ec = wesnoth.current.event_context
local u_killer = wesnoth.get_unit(ec.x2, ec.y2)
local u_victim = wesnoth.get_unit(ec.x1, ec.y1)
if not u_killer or not u_killer:matches { ability = "feeding" } then
return
end
if not u_victim or u_victim:matches { status = "unplagueable" } then
return
end
local u_killer_cfg = u_killer.__cfg
for i,v in ipairs(helper.get_child(u_killer_cfg, "modifications"))do
if v[1] == "object" and v[2].feeding == true then
local effect = helper.get_child(v[2], "effect")
effect.increase_total = effect.increase_total + 1
u_killer_cfg.max_hitpoints = u_killer_cfg.max_hitpoints + 1
u_killer_cfg.hitpoints = u_killer_cfg.hitpoints + 1
wesnoth.create_unit(u_killer_cfg):to_map()
wesnoth.float_label(ec.x2, ec.y2, _ "+1 max HP", "0,255,0")
return
end
end
-- reaching this point means that the unit didn't have the feedng object yet.
u_killer:add_modification("object", {
feeding = true,
T.effect {
apply_to = "hitpoints",
increase_total = 1,
},
})
u_killer.hitpoints = u_killer.hitpoints + 1
wesnoth.float_label(ec.x2, ec.y2, _ "+1 max HP", "0,255,0")
end)

View file

@ -11,6 +11,7 @@ function wesnoth.game_events.on_save()
return {}
end
wesnoth.require "lua/feeding.lua"
wesnoth.require "lua/wml-flow.lua"
wesnoth.require "lua/wml/objectives.lua"
wesnoth.require "lua/wml/items.lua"