improve formatting in lua code.

This also fixes a bug in [harm_unit] where a second arument was passed
to wml_actions.animate_unit which was obviously meant to be part of the
first argument.

This also changes harm_unit to use wml_actions.animate_unit instead of
wesnoth.animate_unit becasue the former is better known an thus easier
to understand.
This commit is contained in:
gfgtdf 2015-12-21 20:18:41 +01:00
parent 9b7b1751fd
commit b4e5f16d74

View file

@ -20,6 +20,7 @@ local helper = wesnoth.require "lua/helper.lua"
local location_set = wesnoth.require "lua/location_set.lua"
local utils = wesnoth.require "lua/wml-utils.lua"
local wml_actions = wesnoth.wml_actions
local T = helper.set_wml_tag_metatable {}
function wml_actions.sync_variable(cfg)
local names = cfg.name or helper.wml_error "[sync_variable] missing required name= attribute."
@ -968,10 +969,15 @@ function wml_actions.harm_unit(cfg)
if animate then
if animate ~= "defender" and harmer and harmer.valid then
wesnoth.scroll_to_tile(harmer.x, harmer.y, true)
wesnoth.animate_unit({ flag = "attack", hits = true, { "filter", { id = harmer.id } },
{ "primary_attack", primary_attack },
{ "secondary_attack", secondary_attack }, with_bars = true,
{ "facing", { x = unit_to_harm.x, y = unit_to_harm.y } } })
wml_actions.animate_unit {
flag = "attack",
hits = true,
with_bars = true,
T.filter { id = harmer.id },
T.primary_attack ( primary_attack ),
T.secondary_attack ( secondary_attack ),
T.facing { x = unit_to_harm.x, y = unit_to_harm.y },
}
end
wesnoth.scroll_to_tile(unit_to_harm.x, unit_to_harm.y, true)
end
@ -1007,12 +1013,13 @@ function wml_actions.harm_unit(cfg)
return damage
end
local damage = calculate_damage( amount,
( cfg.alignment or "neutral" ),
wesnoth.get_time_of_day( { unit_to_harm.x, unit_to_harm.y, true } ).lawful_bonus,
wesnoth.unit_resistance( unit_to_harm, cfg.damage_type or "dummy" ),
resistance_multiplier
)
local damage = calculate_damage(
amount,
cfg.alignment or "neutral",
wesnoth.get_time_of_day( { unit_to_harm.x, unit_to_harm.y, true } ).lawful_bonus,
wesnoth.unit_resistance( unit_to_harm, cfg.damage_type or "dummy" ),
resistance_multiplier
)
if unit_to_harm.hitpoints <= damage then
if kill == false then damage = unit_to_harm.hitpoints - 1
@ -1058,14 +1065,24 @@ function wml_actions.harm_unit(cfg)
if animate and animate ~= "attacker" then
if harmer and harmer.valid then
wesnoth.animate_unit({ flag = "defend", hits = true, { "filter", { id = unit_to_harm.id } },
{ "primary_attack", primary_attack },
{ "secondary_attack", secondary_attack }, with_bars = true },
{ "facing", { x = harmer.x, y = harmer.y } })
wml_actions.animate_unit {
flag = "defend",
hits = true,
with_bars = true,
T.filter { id = unit_to_harm.id },
T.primary_attack ( primary_attack ),
T.secondary_attack ( secondary_attack ),
T.facing { x = harmer.x, y = harmer.y },
}
else
wesnoth.animate_unit({ flag = "defend", hits = true, { "filter", { id = unit_to_harm.id } },
{ "primary_attack", primary_attack },
{ "secondary_attack", secondary_attack }, with_bars = true })
wml_actions.animate_unit {
flag = "defend",
hits = true,
with_bars = true,
T.filter { id = unit_to_harm.id },
T.primary_attack ( primary_attack ),
T.secondary_attack ( secondary_attack ),
}
end
end
@ -1086,7 +1103,7 @@ function wml_actions.harm_unit(cfg)
end
if kill ~= false and unit_to_harm.hitpoints <= 0 then
wml_actions.kill({ id = unit_to_harm.id, animate = toboolean( animate ), fire_event = fire_event })
wml_actions.kill { id = unit_to_harm.id, animate = toboolean( animate ), fire_event = fire_event }
end
if animate then
@ -1100,23 +1117,35 @@ function wml_actions.harm_unit(cfg)
-- both may no longer be alive at this point, so double check
-- this blocks handles the harmed units advancing
if experience ~= false and harmer and unit_to_harm.valid and unit_to_harm.experience >= unit_to_harm.max_experience then
wml_actions.store_unit { { "filter", { id = unit_to_harm.id } }, variable = "Lua_store_unit", kill = true }
wml_actions.unstore_unit { variable = "Lua_store_unit",
find_vacant = false,
advance = true,
animate = toboolean( animate ),
fire_event = fire_event }
wml_actions.store_unit {
T.filter { id = unit_to_harm.id },
variable = "Lua_store_unit",
kill = true,
}
wml_actions.unstore_unit {
variable = "Lua_store_unit",
find_vacant = false,
advance = true,
animate = toboolean( animate ),
fire_event = fire_event,
}
wesnoth.set_variable ( "Lua_store_unit", nil )
end
-- this block handles the harmer advancing
if experience ~= false and harmer and harmer.valid and harmer.experience >= harmer.max_experience then
wml_actions.store_unit { { "filter", { id = harmer.id } }, variable = "Lua_store_unit", kill = true }
wml_actions.unstore_unit { variable = "Lua_store_unit",
find_vacant = false,
advance = true,
animate = toboolean( animate ),
fire_event = fire_event }
wml_actions.store_unit {
T.filter { id = harmer.id },
variable = "Lua_store_unit",
kill = true,
}
wml_actions.unstore_unit {
variable = "Lua_store_unit",
find_vacant = false,
advance = true,
animate = toboolean( animate ),
fire_event = fire_event,
}
wesnoth.set_variable ( "Lua_store_unit", nil )
end
end
@ -1164,23 +1193,23 @@ function wml_actions.store_side(cfg)
local writer = utils.vwriter.init(cfg, "side")
for t, side_number in helper.get_sides(cfg) do
local container = {
controller = t.controller,
recruit = table.concat(t.recruit, ","),
fog = t.fog,
shroud = t.shroud,
hidden = t.hidden,
income = t.total_income,
village_gold = t.village_gold,
village_support = t.village_support,
team_name = t.team_name,
user_team_name = t.user_team_name,
color = t.color,
gold = t.gold,
scroll_to_leader = t.scroll_to_leader,
flag = t.flag,
flag_icon = t.flag_icon,
side = side_number
}
controller = t.controller,
recruit = table.concat(t.recruit, ","),
fog = t.fog,
shroud = t.shroud,
hidden = t.hidden,
income = t.total_income,
village_gold = t.village_gold,
village_support = t.village_support,
team_name = t.team_name,
user_team_name = t.user_team_name,
color = t.color,
gold = t.gold,
scroll_to_leader = t.scroll_to_leader,
flag = t.flag,
flag_icon = t.flag_icon,
side = side_number
}
utils.vwriter.write(writer, container)
end
end
@ -1214,33 +1243,30 @@ function wml_actions.add_ai_behavior(cfg)
local path = "stage[" .. loop_id .. "].candidate_action[" .. id .. "]" -- bca: behavior candidate action
local conf = {
["action"] = "add",
["engine"] = "lua",
["path"] = path,
wesnoth.wml_actions.modify_ai {
action = "add",
engine = "lua",
path = path,
side = side,
{"candidate_action", {
["id"] = id,
["name"] = id,
["engine"] = "lua",
["sticky"] = sticky,
["unit_x"] = ux,
["unit_y"] = uy,
["evaluation"] = eval,
["execution"] = exec
}},
["side"] = side
T.candidate_action {
id = id,
name = id,
engine = "lua",
sticky = sticky,
unit_x = ux,
unit_y = uy,
evaluation = eval,
execution = exec
},
}
wesnoth.wml_actions.modify_ai(conf)
--wesnoth.message("Adding a behavior")
end
function wml_actions.find_path(cfg)
local filter_unit = (helper.get_child(cfg, "traveler")) or helper.wml_error("[find_path] missing required [traveler] tag")
local filter_unit = helper.get_child(cfg, "traveler") or helper.wml_error("[find_path] missing required [traveler] tag")
-- only the first unit matching
local unit = wesnoth.get_units(filter_unit)[1] or helper.wml_error("[find_path]'s filter didn't match any unit")
local filter_location = (helper.get_child(cfg, "destination")) or helper.wml_error( "[find_path] missing required [destination] tag" )
local filter_location = helper.get_child(cfg, "destination") or helper.wml_error( "[find_path] missing required [destination] tag" )
-- support for $this_unit
local this_unit = utils.start_var_scope("this_unit")