Reverted 2011-09-21T21:44:58Z!thonsew@yahoo.com and 2011-09-20T21:21:21Z!thonsew@yahoo.com.

thonsew: The lua interface is not your playground. This is not the way
I want the lua files or its C++ interface to be modified. You are
invited to talk with me on IRC.

some of the main reasons:

-Your revisions contain lots of unneccessary changes, some of which
 break existing lua or wml without a reason. Do you ever look at the
 diffs?

-Your revisions pack a lot of unrelated stuff together into one.

2011-09-21T21:44:58Z!thonsew@yahoo.com makes sense in some parts and is what you should have
done in the first place.
This commit is contained in:
Anonymissimus 2011-09-22 17:09:50 +00:00
parent 83e2b2b999
commit febb9da2ca
10 changed files with 467 additions and 994 deletions

View File

@ -4,7 +4,7 @@
-- from the engine between 1.8 and 1.10. -- from the engine between 1.8 and 1.10.
function wesnoth.get_side(i) function wesnoth.get_side(i)
return wesnoth.sides[tonumber(i)] return wesnoth.sides[i]
end end
function wesnoth.get_side_count() function wesnoth.get_side_count()

View File

@ -1,21 +1,7 @@
--! #textdomain wesnoth --! #textdomain wesnoth
--! Create the t_tokens that helper will use for fast access to vconfig objects
--! Allows the use of cfg[tk] which is faster than cfg["tk"] which is semantically the same as cfg.tk
--for example local id_token = wesnoth.create_t_token("id")
local helper = {} local helper = {}
--! Checks if a value equals another value or the string of the value equals the string og the other value
function helper.check_equal(a, b)
return (a == b) or (tostring(a) == tostring(b))
end
--! Checks if a value equals another value or the string of the value equals the string og the other value
function helper.check_not_equal(a, b)
return not ((a == b) or (tostring(a) == tostring(b)))
end
--! Returns an iterator over all the sides matching a given filter that can be used in a for-in loop. --! Returns an iterator over all the sides matching a given filter that can be used in a for-in loop.
function helper.get_sides(cfg) function helper.get_sides(cfg)
local function f(s) local function f(s)
@ -33,7 +19,7 @@ end
--! Interrupts the current execution and displays a chat message that looks like a WML error. --! Interrupts the current execution and displays a chat message that looks like a WML error.
function helper.wml_error(m) function helper.wml_error(m)
error("~wml:" .. tostring(m), 0) error("~wml:" .. m, 0)
end end
--! Returns an iterator over teams that can be used in a for-in loop. --! Returns an iterator over teams that can be used in a for-in loop.
@ -54,9 +40,9 @@ function helper.get_child(cfg, name, id)
-- ipairs cannot be used on a vconfig object -- ipairs cannot be used on a vconfig object
for i = 1, #cfg do for i = 1, #cfg do
local v = cfg[i] local v = cfg[i]
if helper.check_equal( v[1], name) then if v[1] == name then
local w = v[2] local w = v[2]
if not id or helper.check_equal( w.id, id) then return w, i end if not id or w.id == id then return w, i end
end end
end end
end end
@ -70,7 +56,7 @@ function helper.child_range(cfg, tag)
c = cfg[i] c = cfg[i]
if not c then return end if not c then return end
s.i = i + 1 s.i = i + 1
until helper.check_equal( c[1] , tag) until c[1] == tag
return c[2] return c[2]
end end
return f, { i = 1 } return f, { i = 1 }
@ -85,10 +71,10 @@ function helper.modify_unit(filter, vars)
variable = "LUA_modify_unit", variable = "LUA_modify_unit",
kill = true kill = true
}) })
for i = 0, tonumber(wesnoth.get_variable("LUA_modify_unit.length")) - 1 do for i = 0, wesnoth.get_variable("LUA_modify_unit.length") - 1 do
local u = "LUA_modify_unit[" .. i .. "]" local u = "LUA_modify_unit[" .. i .. "]"
for k, v in pairs(vars) do for k, v in pairs(vars) do
wesnoth.set_variable(u .. '.' .. tostring(k), v) wesnoth.set_variable(u .. '.' .. k, v)
end end
wesnoth.fire("unstore_unit", { wesnoth.fire("unstore_unit", {
variable = u, variable = u,
@ -131,8 +117,8 @@ function helper.move_unit_fake(filter, to_x, to_y)
gender = "$LUA_move_unit.gender", gender = "$LUA_move_unit.gender",
variation = "$LUA_move_unit.variation", variation = "$LUA_move_unit.variation",
side = "$LUA_move_unit.side", side = "$LUA_move_unit.side",
x = tostring(from_x) .. ',' .. tostring(to_x), x = from_x .. ',' .. to_x,
y = tostring(from_y) .. ',' .. tostring(to_y) y = from_y .. ',' .. to_y
}) })
wesnoth.fire("unstore_unit", { variable="LUA_move_unit", find_vacant=true }) wesnoth.fire("unstore_unit", { variable="LUA_move_unit", find_vacant=true })
@ -160,9 +146,9 @@ end
function variable_mt.__index(t, k) function variable_mt.__index(t, k)
local i = tonumber(k) local i = tonumber(k)
if i then if i then
k = tostring(t.__varname) .. '[' .. i .. ']' k = t.__varname .. '[' .. i .. ']'
else else
k = tostring(t.__varname) .. '.' .. tostring(k) k = t.__varname .. '.' .. k
end end
return get_variable_proxy(k) return get_variable_proxy(k)
end end
@ -170,9 +156,9 @@ end
function variable_mt.__newindex(t, k, v) function variable_mt.__newindex(t, k, v)
local i = tonumber(k) local i = tonumber(k)
if i then if i then
k = tostring(t.__varname) .. '[' .. i .. ']' k = t.__varname .. '[' .. i .. ']'
else else
k = tostring(t.__varname) .. '.' .. tostring(k) k = t.__varname .. '.' .. k
end end
set_variable_proxy(k, v) set_variable_proxy(k, v)
end end
@ -236,8 +222,8 @@ end
--! @returns a table containing all the variables (starting at index 1). --! @returns a table containing all the variables (starting at index 1).
function helper.get_variable_array(var) function helper.get_variable_array(var)
local result = {} local result = {}
for i = 1, wesnoth.get_variable(tostring(var) .. ".length") do for i = 1, wesnoth.get_variable(var .. ".length") do
result[i] = wesnoth.get_variable(string.format("%s[%d]", tostring(var), i - 1)) result[i] = wesnoth.get_variable(string.format("%s[%d]", var, i - 1))
end end
return result return result
end end
@ -246,7 +232,7 @@ end
function helper.set_variable_array(var, t) function helper.set_variable_array(var, t)
wesnoth.set_variable(var) wesnoth.set_variable(var)
for i, v in ipairs(t) do for i, v in ipairs(t) do
wesnoth.set_variable(string.format("%s[%d]", tostring(var), i - 1), v) wesnoth.set_variable(string.format("%s[%d]", var, i - 1), v)
end end
end end
@ -256,8 +242,8 @@ end
--! @returns a table containing all the variable proxies (starting at index 1). --! @returns a table containing all the variable proxies (starting at index 1).
function helper.get_variable_proxy_array(var) function helper.get_variable_proxy_array(var)
local result = {} local result = {}
for i = 1, wesnoth.get_variable(tostring(var) .. ".length") do for i = 1, wesnoth.get_variable(var .. ".length") do
result[i] = get_variable_proxy(string.format("%s[%d]", tostring(var), i - 1)) result[i] = get_variable_proxy(string.format("%s[%d]", var, i - 1))
end end
return result return result
end end

View File

@ -113,8 +113,8 @@ end
function methods:of_wml_var(name) function methods:of_wml_var(name)
local values = self.values local values = self.values
for i = 0, wesnoth.get_variable(tostring(name) .. ".length") - 1 do for i = 0, wesnoth.get_variable(name .. ".length") - 1 do
local t = wesnoth.get_variable(string.format("%s[%d]", tostring(name), i)) local t = wesnoth.get_variable(string.format("%s[%d]", name, i))
local x, y = t.x, t.y local x, y = t.x, t.y
t.x, t.y = nil, nil t.x, t.y = nil, nil
values[index(x, y)] = next(t) and t or true values[index(x, y)] = next(t) and t or true
@ -138,10 +138,10 @@ function methods:to_wml_var(name)
wesnoth.set_variable(name) wesnoth.set_variable(name)
self:stable_iter(function(x, y, v) self:stable_iter(function(x, y, v)
if type(v) == 'table' then if type(v) == 'table' then
wesnoth.set_variable(string.format("%s[%d]", tostring(name), i), v) wesnoth.set_variable(string.format("%s[%d]", name, i), v)
end end
wesnoth.set_variable(string.format("%s[%d].x", tostring(name), i), x) wesnoth.set_variable(string.format("%s[%d].x", name, i), x)
wesnoth.set_variable(string.format("%s[%d].y", tostring(name), i), y) wesnoth.set_variable(string.format("%s[%d].y", name, i), y)
i = i + 1 i = i + 1
end) end)
end end

View File

@ -8,9 +8,7 @@ if wesnoth.package["lua/helper.lua"] then return end
function wesnoth.game_events.on_load(cfg) function wesnoth.game_events.on_load(cfg)
if #cfg == 0 then return end if #cfg == 0 then return end
local t = {} local t = {}
for i = 1,#cfg do for i = 1,#cfg do t[i] = string.format("[%s]", cfg[i][1]) end
t[i] = string.format("[%s]", tostring(cfg[i][1]))
end
error(string.format("~wml:%s not supported at scenario toplevel", table.concat(t, ', ')), 0) error(string.format("~wml:%s not supported at scenario toplevel", table.concat(t, ', ')), 0)
end end
@ -41,7 +39,8 @@ end
function wml_actions.chat(cfg) function wml_actions.chat(cfg)
local side_list = wesnoth.get_sides(cfg) local side_list = wesnoth.get_sides(cfg)
local message = tostring(cfg.message or helper.wml_error "[chat] missing required message= attribute.") local message = tostring(cfg.message) or
helper.wml_error "[chat] missing required message= attribute."
local speaker = cfg.speaker local speaker = cfg.speaker
if speaker then if speaker then
@ -51,7 +50,7 @@ function wml_actions.chat(cfg)
end end
for index, side in ipairs(side_list) do for index, side in ipairs(side_list) do
if helper.check_equal( side.controller , "human") then if side.controller == "human" then
wesnoth.message(speaker, message) wesnoth.message(speaker, message)
break break
end end
@ -72,8 +71,9 @@ function wml_actions.store_gold(cfg)
end end
function wml_actions.clear_variable(cfg) function wml_actions.clear_variable(cfg)
local names = tostring(cfg.name or helper.wml_error "[clear_variable] missing required name= attribute.") local names = cfg.name or
for w in string.gmatch(tostring(names), "[^%s,][^,]*") do helper.wml_error "[clear_variable] missing required name= attribute."
for w in string.gmatch(names, "[^%s,][^,]*") do
wesnoth.set_variable(trim(w)) wesnoth.set_variable(trim(w))
end end
end end
@ -94,10 +94,10 @@ function wml_actions.store_unit_type(cfg)
helper.wml_error "[store_unit_type] missing required type= attribute." helper.wml_error "[store_unit_type] missing required type= attribute."
wesnoth.set_variable(var) wesnoth.set_variable(var)
local i = 0 local i = 0
for w in string.gmatch(tostring(types), "[^%s,][^,]*") do for w in string.gmatch(types, "[^%s,][^,]*") do
local unit_type = wesnoth.unit_types[w] or local unit_type = wesnoth.unit_types[w] or
helper.wml_error(string.format("Attempt to store nonexistent unit type '%s'.", w)) helper.wml_error(string.format("Attempt to store nonexistent unit type '%s'.", w))
wesnoth.set_variable(string.format("%s[%d]", tostring(var), i), unit_type.__cfg) wesnoth.set_variable(string.format("%s[%d]", var, i), unit_type.__cfg)
i = i + 1 i = i + 1
end end
end end
@ -123,7 +123,7 @@ end
function wml_actions.allow_recruit(cfg) function wml_actions.allow_recruit(cfg)
for index, team in ipairs(wesnoth.get_sides(nil, cfg, true)) do for index, team in ipairs(wesnoth.get_sides(nil, cfg, true)) do
local v = team.recruit local v = team.recruit
for type in string.gmatch(tostring(cfg.type), "[^%s,][^,]*") do for type in string.gmatch(cfg.type, "[^%s,][^,]*") do
table.insert(v, type) table.insert(v, type)
wesnoth.add_known_unit(type) wesnoth.add_known_unit(type)
end end
@ -135,7 +135,7 @@ function wml_actions.allow_extra_recruit(cfg)
local recruits = cfg.extra_recruit or helper.wml_error("[allow_extra_recruit] missing required extra_recruit= attribute") local recruits = cfg.extra_recruit or helper.wml_error("[allow_extra_recruit] missing required extra_recruit= attribute")
for index, unit in ipairs(wesnoth.get_units(cfg)) do for index, unit in ipairs(wesnoth.get_units(cfg)) do
local v = unit.extra_recruit local v = unit.extra_recruit
for recruit in string.gmatch(tostring(recruits), "[^%s,][^,]*") do for recruit in string.gmatch(recruits, "[^%s,][^,]*") do
table.insert(v, recruit) table.insert(v, recruit)
wesnoth.add_known_unit(recruit) wesnoth.add_known_unit(recruit)
end end
@ -146,9 +146,9 @@ end
function wml_actions.disallow_recruit(cfg) function wml_actions.disallow_recruit(cfg)
for index, team in ipairs(wesnoth.get_sides(nil, cfg, true)) do for index, team in ipairs(wesnoth.get_sides(nil, cfg, true)) do
local v = team.recruit local v = team.recruit
for w in string.gmatch(tostring(cfg.type), "[^%s,][^,]*") do for w in string.gmatch(cfg.type, "[^%s,][^,]*") do
for i, r in ipairs(v) do for i, r in ipairs(v) do
if helper.check_equal( r , w) then if r == w then
table.remove(v, i) table.remove(v, i)
break break
end end
@ -162,9 +162,9 @@ function wml_actions.disallow_extra_recruit(cfg)
local recruits = cfg.extra_recruit or helper.wml_error("[disallow_extra_recruit] missing required extra_recruit= attribute") local recruits = cfg.extra_recruit or helper.wml_error("[disallow_extra_recruit] missing required extra_recruit= attribute")
for index, unit in ipairs(wesnoth.get_units(cfg)) do for index, unit in ipairs(wesnoth.get_units(cfg)) do
local v = unit.extra_recruit local v = unit.extra_recruit
for w in string.gmatch(tostring(recruits), "[^%s,][^,]*") do for w in string.gmatch(recruits, "[^%s,][^,]*") do
for i, r in ipairs(v) do for i, r in ipairs(v) do
if helper.check_equal( r , w) then if r == w then
table.remove(v, i) table.remove(v, i)
break break
end end
@ -178,7 +178,7 @@ function wml_actions.set_recruit(cfg)
local recruit = cfg.recruit or helper.wml_error("[set_recruit] missing required recruit= attribute") local recruit = cfg.recruit or helper.wml_error("[set_recruit] missing required recruit= attribute")
for index, team in ipairs(wesnoth.get_sides(nil, cfg, true)) do for index, team in ipairs(wesnoth.get_sides(nil, cfg, true)) do
local v = {} local v = {}
for w in string.gmatch(tostring(recruit), "[^%s,][^,]*") do for w in string.gmatch(recruit, "[^%s,][^,]*") do
table.insert(v, w) table.insert(v, w)
end end
team.recruit = v team.recruit = v
@ -189,7 +189,7 @@ function wml_actions.set_extra_recruit(cfg)
local recruits = cfg.extra_recruit or helper.wml_error("[set_extra_recruit] missing required extra_recruit= attribute") local recruits = cfg.extra_recruit or helper.wml_error("[set_extra_recruit] missing required extra_recruit= attribute")
local v = {} local v = {}
for w in string.gmatch(tostring(recruits), "[^%s,][^,]*") do for w in string.gmatch(recruits, "[^%s,][^,]*") do
table.insert(v, w) table.insert(v, w)
end end
@ -201,9 +201,9 @@ end
function wml_actions.store_map_dimensions(cfg) function wml_actions.store_map_dimensions(cfg)
local var = cfg.variable or "map_size" local var = cfg.variable or "map_size"
local w, h, b = wesnoth.get_map_size() local w, h, b = wesnoth.get_map_size()
wesnoth.set_variable(tostring(var) .. ".width", w) wesnoth.set_variable(var .. ".width", w)
wesnoth.set_variable(tostring(var) .. ".height", h) wesnoth.set_variable(var .. ".height", h)
wesnoth.set_variable(tostring(var) .. ".border_size", b) wesnoth.set_variable(var .. ".border_size", b)
end end
function wml_actions.unit_worth(cfg) function wml_actions.unit_worth(cfg)
@ -213,7 +213,7 @@ function wml_actions.unit_worth(cfg)
local hp = u.hitpoints / u.max_hitpoints local hp = u.hitpoints / u.max_hitpoints
local xp = u.experience / u.max_experience local xp = u.experience / u.max_experience
local best_adv = ut.cost local best_adv = ut.cost
for w in string.gmatch(tostring(ut.__cfg.advances_to), "[^%s,][^,]*") do for w in string.gmatch(ut.__cfg.advances_to, "[^%s,][^,]*") do
local uta = wesnoth.unit_types[w] local uta = wesnoth.unit_types[w]
if uta and uta.cost > best_adv then best_adv = uta.cost end if uta and uta.cost > best_adv then best_adv = uta.cost end
end end
@ -228,26 +228,23 @@ function wml_actions.wml_action(cfg)
-- The new tag's name -- The new tag's name
local name = cfg.name or local name = cfg.name or
helper.wml_error "[wml_action] missing required name= attribute." helper.wml_error "[wml_action] missing required name= attribute."
local code = tostring(cfg.lua_function or helper.wml_error "[wml_action] missing required lua_function= attribute.") local code = cfg.lua_function or
helper.wml_error "[wml_action] missing required lua_function= attribute."
local bytecode, message = loadstring(code) local bytecode, message = loadstring(code)
if not bytecode then if not bytecode then
helper.wml_error("[wml_action] failed to compile Lua code: " .. tostring(message)) helper.wml_error("[wml_action] failed to compile Lua code: " .. message)
end end
-- The lua function that is executed when the tag is called -- The lua function that is executed when the tag is called
local lua_function = bytecode() or local lua_function = bytecode() or
helper.wml_error "[wml_action] expects a Lua code returning a function." helper.wml_error "[wml_action] expects a Lua code returning a function."
-- Store it as a t_token and a string
wml_actions[name] = lua_function wml_actions[name] = lua_function
wml_actions[tostring(name)] = lua_function
end end
function wml_actions.lua(cfg) function wml_actions.lua(cfg)
local cfg = helper.shallow_literal(cfg) local cfg = helper.shallow_literal(cfg)
local bytecode, message = loadstring(cfg.code or "")
local bytecode, message = loadstring(tostring(cfg.code or "")) if not bytecode then error("~lua:" .. message, 0) end
if not bytecode then error("~lua:" .. tostring(message), 0) end bytecode(helper.get_child(cfg, "args"))
local ww, ii = helper.get_child(cfg, "args")
bytecode(ww)
end end
function wml_actions.music(cfg) function wml_actions.music(cfg)
@ -265,33 +262,29 @@ local function handle_event_commands(cfg)
local cmd = v[1] local cmd = v[1]
local arg = v[2] local arg = v[2]
local insert_from local insert_from
if cmd == "insert_tag" then
if helper.check_equal( cmd , "insert_tag") then
cmd = arg.name cmd = arg.name
local from = arg.variable local from = arg.variable
arg = wesnoth.get_variable(from) arg = wesnoth.get_variable(from)
if type(arg) ~= "table" then if type(arg) ~= "table" then
-- Corner case: A missing variable is replaced -- Corner case: A missing variable is replaced
-- by an empty container rather than being ignored. -- by an empty container rather than being ignored.
arg = {} arg = {}
elseif string.sub(tostring(from), -1) ~= ']' then elseif string.sub(from, -1) ~= ']' then
insert_from = from insert_from = from
end end
arg = wesnoth.tovconfig(arg) arg = wesnoth.tovconfig(arg)
end end
if not string.find(cmd, "^filter") then
if not string.find(tostring(cmd), "^filter") then cmd = wml_actions[cmd] or
scmd = tostring(cmd); helper.wml_error(string.format("[%s] not supported", cmd))
cmd = wml_actions[cmd] or wml_actions[tostring(cmd)] or
helper.wml_error(string.format("[%s] not supported", tostring(cmd)))
if insert_from then if insert_from then
local j = 0 local j = 0
repeat repeat
cmd(arg) cmd(arg)
j = j + 1 j = j + 1
if j >= wesnoth.get_variable(tostring(insert_from) .. ".length") then break end if j >= wesnoth.get_variable(insert_from .. ".length") then break end
arg = wesnoth.tovconfig(wesnoth.get_variable(string.format("%s[%d]", tostring(insert_from), j))) arg = wesnoth.tovconfig(wesnoth.get_variable(string.format("%s[%d]", insert_from, j)))
until false until false
else else
cmd(arg) cmd(arg)
@ -331,8 +324,8 @@ function wml_actions.switch(cfg)
-- Execute all the [case]s where the value matches. -- Execute all the [case]s where the value matches.
for v in helper.child_range(cfg, "case") do for v in helper.child_range(cfg, "case") do
local match = false local match = false
for w in string.gmatch(tostring(v.value), "[^%s,][^,]*") do for w in string.gmatch(v.value, "[^%s,][^,]*") do
if helper.check_equal( w , value) then match = true ; break end if w == tostring(value) then match = true ; break end
end end
if match then if match then
handle_event_commands(v) handle_event_commands(v)
@ -378,11 +371,11 @@ function wml_actions.unit_overlay(cfg)
local img = cfg.image local img = cfg.image
for i,u in ipairs(wesnoth.get_units(cfg)) do for i,u in ipairs(wesnoth.get_units(cfg)) do
local ucfg = u.__cfg local ucfg = u.__cfg
for w in string.gmatch(tostring(ucfg.overlays), "[^%s,][^,]*") do for w in string.gmatch(ucfg.overlays, "[^%s,][^,]*") do
if helper.check_equal( w , img) then ucfg = nil end if w == img then ucfg = nil end
end end
if ucfg then if ucfg then
ucfg.overlays = tostring(ucfg.overlays) .. ',' .. tostring(img) ucfg.overlays = ucfg.overlays .. ',' .. img
wesnoth.put_unit(ucfg) wesnoth.put_unit(ucfg)
end end
end end
@ -393,7 +386,7 @@ function wml_actions.remove_unit_overlay(cfg)
for i,u in ipairs(wesnoth.get_units(cfg)) do for i,u in ipairs(wesnoth.get_units(cfg)) do
local ucfg = u.__cfg local ucfg = u.__cfg
local t = {} local t = {}
for w in string.gmatch(tostring(ucfg.overlays), "[^%s,][^,]*") do for w in string.gmatch(ucfg.overlays, "[^%s,][^,]*") do
if w ~= img then table.insert(t, w) end if w ~= img then table.insert(t, w) end
end end
ucfg.overlays = table.concat(t, ',') ucfg.overlays = table.concat(t, ',')
@ -407,38 +400,35 @@ function wml_actions.store_turns(cfg)
end end
function wml_actions.store_unit(cfg) function wml_actions.store_unit(cfg)
local filter = helper.get_child(cfg, "filter") or helper.wml_error "[store_unit] missing required [filter] tag" local filter = helper.get_child(cfg, "filter") or
helper.wml_error "[store_unit] missing required [filter] tag"
local kill_units = cfg.kill local kill_units = cfg.kill
local mode = cfg.mode local mode = cfg.mode
local var = cfg.variable or "unit" local var = cfg.variable or "unit"
local idx = 0 local idx = 0
if (helper.check_equal( mode , "append") )then if mode == "append" then
idx = wesnoth.get_variable(tostring(var) .. ".length") idx = wesnoth.get_variable(var .. ".length")
elseif helper.check_not_equal( mode, "replace" ) then elseif mode ~= "replace" then
wesnoth.set_variable(var) wesnoth.set_variable(var)
end end
for i,u in ipairs(wesnoth.get_units(filter)) do for i,u in ipairs(wesnoth.get_units(filter)) do
wesnoth.set_variable(string.format("%s[%d]", tostring(var), idx), u.__cfg) wesnoth.set_variable(string.format("%s[%d]", var, idx), u.__cfg)
idx = idx + 1 idx = idx + 1
if kill_units then if kill_units then wesnoth.put_unit(u.x, u.y) end
wesnoth.put_unit(u.x, u.y)
end
end end
if (not filter.x or helper.check_equal( filter.x , "recall")) and (not filter.y or helper.check_equal( filter.y , "recall")) then if (not filter.x or filter.x == "recall") and (not filter.y or filter.y == "recall") then
for i,u in ipairs(wesnoth.get_recall_units(filter)) do for i,u in ipairs(wesnoth.get_recall_units(filter)) do
local ucfg = u.__cfg local ucfg = u.__cfg
ucfg.x = "recall" ucfg.x = "recall"
ucfg.y = "recall" ucfg.y = "recall"
wesnoth.set_variable(string.format("%s[%d]", tostring(var), idx), ucfg) wesnoth.set_variable(string.format("%s[%d]", var, idx), ucfg)
idx = idx + 1 idx = idx + 1
if kill_units then wesnoth.extract_unit(u) end if kill_units then wesnoth.extract_unit(u) end
end end
end end
ssname = string.format("%s[%d]", tostring(var), idx-1)
end end
function wml_actions.sound(cfg) function wml_actions.sound(cfg)
@ -459,7 +449,7 @@ function wml_actions.store_locations(cfg)
if wesnoth.get_terrain_info(t).village then if wesnoth.get_terrain_info(t).village then
res.owner_side = wesnoth.get_village_owner(x, y) or 0 res.owner_side = wesnoth.get_village_owner(x, y) or 0
end end
wesnoth.set_variable(string.format("%s[%d]", tostring(var), i - 1), res) wesnoth.set_variable(string.format("%s[%d]", var, i - 1), res)
end end
end end
@ -471,7 +461,7 @@ function wml_actions.store_reachable_locations(cfg)
local moves = cfg.moves or "current" local moves = cfg.moves or "current"
local variable = cfg.variable or helper.wml_error "[store_reachable_locations] missing required variable= key" local variable = cfg.variable or helper.wml_error "[store_reachable_locations] missing required variable= key"
local reach_param = { viewing_side = cfg.viewing_side or 0 } local reach_param = { viewing_side = cfg.viewing_side or 0 }
if helper.check_equal( range , "vision") then if range == "vision" then
moves = "max" moves = "max"
reach_param.ignore_units = true reach_param.ignore_units = true
end end
@ -480,7 +470,7 @@ function wml_actions.store_reachable_locations(cfg)
for i,unit in ipairs(wesnoth.get_units(unit_filter)) do for i,unit in ipairs(wesnoth.get_units(unit_filter)) do
local unit_reach local unit_reach
if helper.check_equal( moves, "max" ) then if moves == "max" then
local saved_moves = unit.moves local saved_moves = unit.moves
unit.moves = unit.max_moves unit.moves = unit.max_moves
unit_reach = location_set.of_pairs(wesnoth.find_reach(unit, reach_param)) unit_reach = location_set.of_pairs(wesnoth.find_reach(unit, reach_param))
@ -489,7 +479,7 @@ function wml_actions.store_reachable_locations(cfg)
unit_reach = location_set.of_pairs(wesnoth.find_reach(unit, reach_param)) unit_reach = location_set.of_pairs(wesnoth.find_reach(unit, reach_param))
end end
if helper.check_equal( range, "vision" ) or helper.check_equal( range, "attack" ) then if range == "vision" or range == "attack" then
unit_reach:iter(function(x, y) unit_reach:iter(function(x, y)
reach:insert(x, y) reach:insert(x, y)
for u,v in helper.adjacent_tiles(x, y) do for u,v in helper.adjacent_tiles(x, y) do
@ -528,8 +518,8 @@ function wml_actions.modify_unit(cfg)
local function handle_attributes(cfg, unit_path, toplevel) local function handle_attributes(cfg, unit_path, toplevel)
for current_key, current_value in pairs(helper.shallow_parsed(cfg)) do for current_key, current_value in pairs(helper.shallow_parsed(cfg)) do
if type(current_value) ~= "table" and (not toplevel or helper.check_not_equal( current_key, "type" )) then if type(current_value) ~= "table" and (not toplevel or current_key ~= "type") then
wesnoth.set_variable(string.format("%s.%s", tostring(unit_path), tostring(current_key)), current_value) wesnoth.set_variable(string.format("%s.%s", unit_path, current_key), current_value)
end end
end end
end end
@ -542,7 +532,8 @@ function wml_actions.modify_unit(cfg)
for current_index, current_table in ipairs(cfg) do for current_index, current_table in ipairs(cfg) do
local current_tag = current_table[1] local current_tag = current_table[1]
local tag_index = children_handled[current_tag] or 0 local tag_index = children_handled[current_tag] or 0
handle_child(current_table[2], string.format("%s.%s[%u]", tostring(unit_path), tostring(current_tag), tostring(tag_index))) handle_child(current_table[2], string.format("%s.%s[%u]",
unit_path, current_tag, tag_index))
children_handled[current_tag] = tag_index + 1 children_handled[current_tag] = tag_index + 1
end end
end end
@ -550,15 +541,15 @@ function wml_actions.modify_unit(cfg)
local filter = helper.get_child(cfg, "filter") or helper.wml_error "[modify_unit] missing required [filter] tag" local filter = helper.get_child(cfg, "filter") or helper.wml_error "[modify_unit] missing required [filter] tag"
local function handle_unit(unit_num) local function handle_unit(unit_num)
local children_handled = {} local children_handled = {}
local unit_path = string.format("%s[%u]", tostring(unit_variable), unit_num) local unit_path = string.format("%s[%u]", unit_variable, unit_num)
wesnoth.set_variable("this_unit", wesnoth.get_variable(unit_path)) wesnoth.set_variable("this_unit", wesnoth.get_variable(unit_path))
handle_attributes(cfg, unit_path, true) handle_attributes(cfg, unit_path, true)
for current_index, current_table in ipairs(helper.shallow_parsed(cfg)) do for current_index, current_table in ipairs(helper.shallow_parsed(cfg)) do
local current_tag = current_table[1] local current_tag = current_table[1]
if helper.check_equal( current_tag, "filter" ) then if current_tag == "filter" then
-- nothing -- nothing
elseif helper.check_equal( current_tag, "object" ) or helper.check_equal( current_tag, "trait" ) then elseif current_tag == "object" or current_tag == "trait" then
local unit = wesnoth.get_variable(unit_path) local unit = wesnoth.get_variable(unit_path)
unit = wesnoth.create_unit(unit) unit = wesnoth.create_unit(unit)
wesnoth.add_modification(unit, current_tag, current_table[2]) wesnoth.add_modification(unit, current_tag, current_table[2])
@ -566,20 +557,21 @@ function wml_actions.modify_unit(cfg)
wesnoth.set_variable(unit_path, unit) wesnoth.set_variable(unit_path, unit)
else else
local tag_index = children_handled[current_tag] or 0 local tag_index = children_handled[current_tag] or 0
handle_child(current_table[2], string.format("%s.%s[%u]", tostring(unit_path), tostring(current_tag), tag_index)) handle_child(current_table[2], string.format("%s.%s[%u]",
unit_path, current_tag, tag_index))
children_handled[current_tag] = tag_index + 1 children_handled[current_tag] = tag_index + 1
end end
end end
if cfg.type then if cfg.type then
if helper.check_not_equal( cfg.type, "" ) then wesnoth.set_variable(tostring(unit_path) .. ".advances_to", cfg.type) end if cfg.type ~= "" then wesnoth.set_variable(unit_path .. ".advances_to", cfg.type) end
wesnoth.set_variable(tostring(unit_path) .. ".experience", wesnoth.get_variable(tostring(unit_path) .. ".max_experience")) wesnoth.set_variable(unit_path .. ".experience", wesnoth.get_variable(unit_path .. ".max_experience"))
end end
wml_actions.unstore_unit { variable = unit_path } wml_actions.unstore_unit { variable = unit_path }
end end
wml_actions.store_unit { {"filter", filter}, variable = unit_variable, kill = true } wml_actions.store_unit { {"filter", filter}, variable = unit_variable, kill = true }
local max_index = wesnoth.get_variable(tostring(unit_variable) .. ".length") - 1 local max_index = wesnoth.get_variable(unit_variable .. ".length") - 1
for current_unit = 0, max_index do for current_unit = 0, max_index do
handle_unit(current_unit) handle_unit(current_unit)
@ -591,8 +583,8 @@ end
function wml_actions.move_unit(cfg) function wml_actions.move_unit(cfg)
local coordinate_error = "invalid coordinate in [move_unit]" local coordinate_error = "invalid coordinate in [move_unit]"
local to_x = tostring(cfg.to_x or helper.wml_error(coordinate_error)) local to_x = tostring(cfg.to_x) or helper.wml_error(coordinate_error)
local to_y = tostring(cfg.to_y or helper.wml_error(coordinate_error)) local to_y = tostring(cfg.to_y) or helper.wml_error(coordinate_error)
local fire_event = cfg.fire_event local fire_event = cfg.fire_event
local check_passability = cfg.check_passability; if check_passability == nil then check_passability = true end local check_passability = cfg.check_passability; if check_passability == nil then check_passability = true end
cfg = helper.literal(cfg) cfg = helper.literal(cfg)
@ -602,7 +594,7 @@ function wml_actions.move_unit(cfg)
local pattern = "[^%s,]+" local pattern = "[^%s,]+"
for current_unit_index, current_unit in ipairs(units) do for current_unit_index, current_unit in ipairs(units) do
if not fire_event or current_unit.valid then if not fire_event or current_unit.valid then
local xs, ys = string.gmatch(tostring(to_x), pattern), string.gmatch(tostring(to_y), pattern) local xs, ys = string.gmatch(to_x, pattern), string.gmatch(to_y, pattern)
local move_string_x = current_unit.x local move_string_x = current_unit.x
local move_string_y = current_unit.y local move_string_y = current_unit.y
local pass_check = nil local pass_check = nil
@ -614,8 +606,8 @@ function wml_actions.move_unit(cfg)
y = tonumber(y) or helper.wml_error(coordinate_error) y = tonumber(y) or helper.wml_error(coordinate_error)
x, y = wesnoth.find_vacant_tile(x, y, pass_check) x, y = wesnoth.find_vacant_tile(x, y, pass_check)
if not x or not y then helper.wml_error("Could not find a suitable hex near to one of the target hexes in [move_unit].") end if not x or not y then helper.wml_error("Could not find a suitable hex near to one of the target hexes in [move_unit].") end
move_string_x = string.format("%s,%u", tostring(move_string_x), x) move_string_x = string.format("%s,%u", move_string_x, x)
move_string_y = string.format("%s,%u", tostring(move_string_y), y) move_string_y = string.format("%s,%u", move_string_y, y)
local next_x, next_y = xs(), ys() local next_x, next_y = xs(), ys()
if not next_x and not next_y then break end if not next_x and not next_y then break end
x, y = next_x, next_y x, y = next_x, next_y
@ -727,7 +719,7 @@ function wml_actions.harm_unit(cfg)
for index, unit_to_harm in ipairs(wesnoth.get_units(filter)) do for index, unit_to_harm in ipairs(wesnoth.get_units(filter)) do
if not fire_event or unit_to_harm.valid then if not fire_event or unit_to_harm.valid then
if animate then if animate then
if helper.check_not_equal( animate, "defender" ) and harmer and harmer.valid then if animate ~= "defender" and harmer and harmer.valid then
wesnoth.scroll_to_tile(harmer.x, harmer.y, true) wesnoth.scroll_to_tile(harmer.x, harmer.y, true)
wml_actions.animate_unit( { flag = "attack", hits = true, { "filter", { id = harmer.id } }, wml_actions.animate_unit( { flag = "attack", hits = true, { "filter", { id = harmer.id } },
{ "primary_attack", primary_attack }, { "primary_attack", primary_attack },
@ -753,7 +745,7 @@ function wml_actions.harm_unit(cfg)
local function set_status(name, male_string, female_string, sound) local function set_status(name, male_string, female_string, sound)
if not cfg[name] or unit_to_harm.status[name] then return end if not cfg[name] or unit_to_harm.status[name] then return end
if helper.check_equal( gender, "female" ) then if gender == "female" then
text = string.format("%s%s%s", text, tostring(female_string), "\n") text = string.format("%s%s%s", text, tostring(female_string), "\n")
else else
text = string.format("%s%s%s", text, tostring(male_string), "\n") text = string.format("%s%s%s", text, tostring(male_string), "\n")
@ -776,7 +768,7 @@ function wml_actions.harm_unit(cfg)
text = string.format("%s%s", "\t", text) text = string.format("%s%s", "\t", text)
end end
if animate and helper.check_not_equal( animate, "attacker" ) then if animate and animate ~= "attacker" then
if harmer and harmer.valid then if harmer and harmer.valid then
wml_actions.animate_unit( { flag = "defend", hits = true, { "filter", { id = unit_to_harm.id } }, wml_actions.animate_unit( { flag = "defend", hits = true, { "filter", { id = unit_to_harm.id } },
{ "primary_attack", primary_attack }, { "primary_attack", primary_attack },
@ -792,12 +784,12 @@ function wml_actions.harm_unit(cfg)
wesnoth.float_label( unit_to_harm.x, unit_to_harm.y, string.format( "<span foreground='red'>%s</span>", text ) ) wesnoth.float_label( unit_to_harm.x, unit_to_harm.y, string.format( "<span foreground='red'>%s</span>", text ) )
local function calc_xp( level ) -- to calculate the experience in case of kill local function calc_xp( level ) -- to calculate the experience in case of kill
if helper.check_equal( level, 0 ) then return 4 if level == 0 then return 4
else return level * 8 end else return level * 8 end
end end
if helper.check_not_equal( experience, false ) and harmer and harmer.valid and wesnoth.is_enemy( unit_to_harm.side, harmer.side ) then -- no XP earned for harming friendly units if experience ~= false and harmer and harmer.valid and wesnoth.is_enemy( unit_to_harm.side, harmer.side ) then -- no XP earned for harming friendly units
if helper.check_not_equal( kill, false ) and unit_to_harm.hitpoints <= 0 then if kill ~= false and unit_to_harm.hitpoints <= 0 then
harmer.experience = harmer.experience + calc_xp( unit_to_harm.__cfg.level ) harmer.experience = harmer.experience + calc_xp( unit_to_harm.__cfg.level )
else else
unit_to_harm.experience = unit_to_harm.experience + harmer.__cfg.level unit_to_harm.experience = unit_to_harm.experience + harmer.__cfg.level
@ -805,7 +797,7 @@ function wml_actions.harm_unit(cfg)
end end
end end
if helper.check_not_equal( kill, false ) and unit_to_harm.hitpoints <= 0 then if kill ~= false and unit_to_harm.hitpoints <= 0 then
local function bool( value ) -- support function for kill tag below local function bool( value ) -- support function for kill tag below
if value then return true if value then return true
else return false end else return false end
@ -818,11 +810,11 @@ function wml_actions.harm_unit(cfg)
end end
if variable then if variable then
wesnoth.set_variable(string.format("%s[%d]", tostring(variable), index - 1), { harm_amount = damage }) wesnoth.set_variable(string.format("%s[%d]", variable, index - 1), { harm_amount = damage })
end end
-- both may no longer be alive at this point, so double check -- both may no longer be alive at this point, so double check
if helper.check_not_equal( experience, false ) and harmer and unit_to_harm.valid and unit_to_harm.experience >= unit_to_harm.max_experience then 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.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 } wml_actions.unstore_unit { variable = "Lua_store_unit", find_vacant = false, advance = true }
wesnoth.set_variable ( "Lua_store_unit", nil ) wesnoth.set_variable ( "Lua_store_unit", nil )
@ -832,7 +824,7 @@ function wml_actions.harm_unit(cfg)
end end
end end
if helper.check_not_equal( experience, false ) and harmer and harmer.valid and harmer.experience >= harmer.max_experience then 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.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 } wml_actions.unstore_unit { variable = "Lua_store_unit", find_vacant = false, advance = true }
wesnoth.set_variable ( "Lua_store_unit", nil ) wesnoth.set_variable ( "Lua_store_unit", nil )
@ -890,14 +882,14 @@ function wml_actions.store_side(cfg)
gold = t.gold, gold = t.gold,
side = side_number side = side_number
} }
wesnoth.set_variable(string.format("%s[%u]", tostring(variable), index), container) wesnoth.set_variable(string.format("%s[%u]", variable, index), container)
index = index + 1 index = index + 1
end end
-- deprecated starting with 1.9.0 -- deprecated starting with 1.9.0
-- added message starting with 1.9.7 -- added message starting with 1.9.7
-- assuming that a wml author will usually be in debug mode and a player will not... -- assuming that a wml author will usually be in debug mode and a player will not...
if wesnoth.game_config.debug then if wesnoth.game_config.debug then
wesnoth.message("warning", string.format("$%s.colour is deprecated, use $%s.color (if needed)", tostring(variable), tostring(variable))) wesnoth.message("warning", string.format("$%s.colour is deprecated, use $%s.color (if needed)", variable, variable))
end end
end end
@ -925,8 +917,8 @@ function wml_actions.add_ai_behavior(cfg)
helper.wml_error("[add_ai_behavior]: invalid execution/evaluation handler(s)") helper.wml_error("[add_ai_behavior]: invalid execution/evaluation handler(s)")
end end
local id = "bca-" .. tostring(ca_counter) local id = "bca-" .. ca_counter
local path = "stage[" .. tostring(loop_id) .. "].candidate_action[" .. tostring(id) .. "]" -- bca: behavior candidate action local path = "stage[" .. loop_id .. "].candidate_action[" .. id .. "]" -- bca: behavior candidate action
ca_counter = ca_counter + 1 ca_counter = ca_counter + 1

View File

@ -21,7 +21,7 @@ local function remove_overlay(x, y, name)
if name then if name then
for i = #items,1,-1 do for i = #items,1,-1 do
local item = items[i] local item = items[i]
if helper.check_equal( item.image, name ) or helper.check_equal( item.halo, name ) then if item.image == name or item.halo == name then
table.remove(items, i) table.remove(items, i)
end end
end end
@ -47,7 +47,7 @@ function game_events.on_load(cfg)
local i = 1 local i = 1
while i <= #cfg do while i <= #cfg do
local v = cfg[i] local v = cfg[i]
if helper.check_equal( v[1], "item" ) then if v[1] == "item" then
local v2 = v[2] local v2 = v[2]
add_overlay(v2.x, v2.y, v2) add_overlay(v2.x, v2.y, v2)
table.remove(cfg, i) table.remove(cfg, i)
@ -90,7 +90,7 @@ function wml_actions.store_items(cfg)
local items = scenario_items[loc[1] * 10000 + loc[2]] local items = scenario_items[loc[1] * 10000 + loc[2]]
if not items then break end if not items then break end
for j, item in ipairs(items) do for j, item in ipairs(items) do
wesnoth.set_variable(string.format("%s[%u]", tostring(variable), index), item) wesnoth.set_variable(string.format("%s[%u]", variable, index), item)
index = index + 1 index = index + 1
end end
until true until true

View File

@ -7,7 +7,7 @@ local function color_prefix(r, g, b)
end end
local function insert_before_nl(s, t) local function insert_before_nl(s, t)
return string.gsub(tostring(s), "[^\n]*", "%0" .. tostring(t), 1) return string.gsub(tostring(s), "[^\n]*", "%0" .. t, 1)
end end
local scenario_objectives = {} local scenario_objectives = {}
@ -26,7 +26,7 @@ local old_on_load = game_events.on_load
function game_events.on_load(cfg) function game_events.on_load(cfg)
for i = #cfg,1,-1 do for i = #cfg,1,-1 do
local v = cfg[i] local v = cfg[i]
if helper.check_equal( v[1], "objectives" ) then if v[1] == "objectives" then
local v2 = v[2] local v2 = v[2]
scenario_objectives[v2.side or 0] = v2 scenario_objectives[v2.side or 0] = v2
table.remove(cfg, i) table.remove(cfg, i)
@ -47,18 +47,18 @@ local function generate_objectives(cfg)
local gold_carryover = "" local gold_carryover = ""
local notes = "" local notes = ""
local win_string = tostring(cfg.victory_string or _ "Victory:") local win_string = cfg.victory_string or _ "Victory:"
local lose_string = tostring(cfg.defeat_string or _ "Defeat:") local lose_string = cfg.defeat_string or _ "Defeat:"
local gold_carryover_string = tostring(cfg.gold_carryover_string or _ "Gold carryover:") local gold_carryover_string = cfg.gold_carryover_string or _ "Gold carryover:"
local notes_string = tostring(cfg.notes_string or _ "Notes:") local notes_string = cfg.notes_string or _ "Notes:"
local bullet = "&#8226; " local bullet = "&#8226; "
for obj in helper.child_range(cfg, "objective") do for obj in helper.child_range(cfg, "objective") do
local show_if = helper.get_child(obj, "show_if") local show_if = helper.get_child(obj, "show_if")
if not show_if or wesnoth.eval_conditional(show_if) then if not show_if or wesnoth.eval_conditional(show_if) then
local condition = tostring(obj.condition) local condition = obj.condition
local description = tostring(obj.description or "") local description = obj.description or ""
local turn_counter = "" local turn_counter = ""
if obj.show_turn_counter then if obj.show_turn_counter then
@ -74,22 +74,22 @@ local function generate_objectives(cfg)
end end
end end
if helper.check_equal( condition, "win" ) then if condition == "win" then
local caption = obj.caption local caption = obj.caption
if caption then if caption then
win_objectives = tostring(win_objectives) .. tostring(caption) .. "\n" win_objectives = win_objectives .. caption .. "\n"
end end
win_objectives = tostring(win_objectives) .. tostring(color_prefix(0, 255, 0)) .. tostring(bullet) .. tostring(description) .. tostring(turn_counter) .. "</span>" .. "\n" win_objectives = win_objectives .. color_prefix(0, 255, 0) .. bullet .. description .. turn_counter .. "</span>" .. "\n"
elseif helper.check_equal( condition, "lose" ) then elseif condition == "lose" then
local caption = obj.caption local caption = obj.caption
if caption then if caption then
lose_objectives = tostring(lose_objectives) .. tostring(caption) .. "\n" lose_objectives = lose_objectives .. caption .. "\n"
end end
lose_objectives = tostring(lose_objectives) .. tostring(color_prefix(255, 0, 0)) .. tostring(bullet) .. tostring(description) .. tostring(turn_counter) .. "</span>" .. "\n" lose_objectives = lose_objectives .. color_prefix(255, 0, 0) .. bullet .. description .. turn_counter .. "</span>" .. "\n"
else else
wesnoth.message "Unknown condition, ignoring." wesnoth.message "Unknown condition, ignoring."
end end
@ -99,28 +99,28 @@ local function generate_objectives(cfg)
for obj in helper.child_range(cfg, "gold_carryover") do for obj in helper.child_range(cfg, "gold_carryover") do
if obj.bonus ~= nil then if obj.bonus ~= nil then
if obj.bonus then if obj.bonus then
gold_carryover = tostring(color_prefix(255, 255, 192)) .. tostring(bullet) .. "<small>" .. _"Early finish bonus." .. "</small></span>\n" gold_carryover = color_prefix(255, 255, 192) .. bullet .. "<small>" .. _"Early finish bonus." .. "</small></span>\n"
else else
gold_carryover = tostring(color_prefix(255, 255, 192)) .. tostring(bullet) .. "<small>" .. _"No early finish bonus." .. "</small></span>\n" gold_carryover = color_prefix(255, 255, 192) .. bullet .. "<small>" .. _"No early finish bonus." .. "</small></span>\n"
end end
end end
if obj.carryover_percentage then if obj.carryover_percentage then
local carryover_amount_string = "" local carryover_amount_string = ""
if tonumber(tostring(obj.carryover_percentage or 0)) == 0 then if obj.carryover_percentage == 0 then
carryover_amount_string = _"No gold carried over to the next scenario." carryover_amount_string = _"No gold carried over to the next scenario."
else else
carryover_amount_string = string.format(tostring(_ "%d%% of gold carried over to the next scenario."), tostring(obj.carryover_percentage)) carryover_amount_string = string.format(tostring(_ "%d%% of gold carried over to the next scenario."), obj.carryover_percentage)
end end
gold_carryover = tostring(gold_carryover) .. tostring(color_prefix(255, 255, 192)) .. tostring(bullet) .. "<small>" .. tostring(carryover_amount_string) .. "</small></span>\n" gold_carryover = gold_carryover .. color_prefix(255, 255, 192) .. bullet .. "<small>" .. carryover_amount_string .. "</small></span>\n"
end end
end end
for note in helper.child_range(cfg, "note") do for note in helper.child_range(cfg, "note") do
if note.description then if note.description then
notes = tostring(notes) .. tostring(color_prefix(255, 255, 255)) .. tostring(bullet) .. "<small>" .. tostring(note.description) .. "</small></span>\n" notes = notes .. color_prefix(255, 255, 255) .. bullet .. "<small>" .. note.description .. "</small></span>\n"
end end
end end
@ -128,21 +128,21 @@ local function generate_objectives(cfg)
if summary then if summary then
objectives = "<big>" .. insert_before_nl(summary, "</big>") .. "\n" objectives = "<big>" .. insert_before_nl(summary, "</big>") .. "\n"
end end
if helper.check_not_equal( win_objectives, "" ) then if win_objectives ~= "" then
objectives = tostring(objectives) .. "<big>" .. tostring(win_string) .. "</big>\n" .. tostring(win_objectives) objectives = objectives .. "<big>" .. win_string .. "</big>\n" .. win_objectives
end end
if helper.check_not_equal( lose_objectives, "" ) then if lose_objectives ~= "" then
objectives = tostring(objectives) .. "<big>" .. tostring(lose_string) .. "</big>\n" .. tostring(lose_objectives) objectives = objectives .. "<big>" .. lose_string .. "</big>\n" .. lose_objectives
end end
if helper.check_not_equal( gold_carryover, "" ) then if gold_carryover ~= "" then
objectives = tostring(objectives) .. tostring(gold_carryover_string) .. "\n" .. tostring(gold_carryover) objectives = objectives .. gold_carryover_string .. "\n" .. gold_carryover
end end
if helper.check_not_equal( notes, "" ) then if notes ~= "" then
objectives = tostring(objectives) .. tostring(notes_string) .. "\n" .. tostring(notes) objectives = objectives .. notes_string .. "\n" .. notes
end end
local note = cfg.note local note = cfg.note
if note then if note then
objectives = tostring(objectives) .. tostring(note) .. "\n" objectives = objectives .. note .. "\n"
end end
return string.sub(tostring(objectives), 1, -2) return string.sub(tostring(objectives), 1, -2)
@ -150,8 +150,8 @@ end
function wml_actions.objectives(cfg) function wml_actions.objectives(cfg)
cfg = helper.parsed(cfg) cfg = helper.parsed(cfg)
local side = tonumber(tostring(cfg.side or 0)) or 0 local side = cfg.side or 0
local silent = tostring(cfg.silent) local silent = cfg.silent
-- Save the objectives in a WML variable in case they have to be regenerated later. -- Save the objectives in a WML variable in case they have to be regenerated later.
cfg.side = nil cfg.side = nil
@ -173,7 +173,7 @@ function wml_actions.objectives(cfg)
end end
function wml_actions.show_objectives(cfg) function wml_actions.show_objectives(cfg)
local side = tonumber(tostring(cfg.side or 0)) or 0 local side = cfg.side or 0
local cfg0 = scenario_objectives[0] local cfg0 = scenario_objectives[0]
if side == 0 then if side == 0 then
local objectives0 = cfg0 and generate_objectives(cfg0) local objectives0 = cfg0 and generate_objectives(cfg0)

View File

@ -90,7 +90,7 @@ void lua_ai_context::set_persistent_data(const config &cfg)
lua_rawget(L, LUA_REGISTRYINDEX); lua_rawget(L, LUA_REGISTRYINDEX);
lua_rawgeti(L, -1, num_); lua_rawgeti(L, -1, num_);
luaW_pushconfig(L, cfg, false); luaW_pushconfig(L, cfg);
lua_setfield(L, -2, "data"); lua_setfield(L, -2, "data");
lua_settop(L, top); lua_settop(L, top);
@ -388,7 +388,7 @@ static int cfun_ai_get_leader_aggression(lua_State *L)
static int cfun_ai_get_leader_goal(lua_State *L) static int cfun_ai_get_leader_goal(lua_State *L)
{ {
config goal = get_readonly_context(L).get_leader_goal(); config goal = get_readonly_context(L).get_leader_goal();
luaW_pushconfig(L, goal, false); luaW_pushconfig(L, goal);
return 1; return 1;
} }
@ -696,7 +696,7 @@ void lua_ai_action_handler::handle(config &cfg, bool configOut, lua_object_ptr l
if (!configOut) if (!configOut)
{ {
luaW_pushconfig(L, cfg, false); luaW_pushconfig(L, cfg);
luaW_pcall(L, 2, 0, true); luaW_pcall(L, 2, 0, true);
} }
else if (luaW_pcall(L, 1, 5, true)) // @note for Crab: how much nrets should we actually have here else if (luaW_pcall(L, 1, 5, true)) // @note for Crab: how much nrets should we actually have here

File diff suppressed because it is too large Load Diff

View File

@ -40,8 +40,8 @@ public:
void save_game(config &); void save_game(config &);
void load_game(); void load_game();
bool run_event(game_events::queued_event const &); bool run_event(game_events::queued_event const &);
void set_wml_action(config::t_token const &, game_events::action_handler); void set_wml_action(std::string const &, game_events::action_handler);
bool run_wml_action(config::t_token const &, vconfig const &, bool run_wml_action(std::string const &, vconfig const &,
game_events::queued_event const &); game_events::queued_event const &);
bool run_filter(char const *name, unit const &u); bool run_filter(char const *name, unit const &u);
/** Runs a plain script. */ /** Runs a plain script. */

View File

@ -25,8 +25,8 @@ class unit;
bool luaW_pcall(lua_State *L , int nArgs, int nRets, bool allow_wml_error = false); bool luaW_pcall(lua_State *L , int nArgs, int nRets, bool allow_wml_error = false);
unit *luaW_tounit(lua_State *L, int index, bool only_on_map = false); unit *luaW_tounit(lua_State *L, int index, bool only_on_map = false);
void luaW_pushconfig(lua_State *L, config const &cfg, bool return_tokens); void luaW_pushconfig(lua_State *L, config const &cfg);
bool luaW_toconfig(lua_State *L, int index, config &cfg, int tstring_meta = 0, int t_token_meta = 0); bool luaW_toconfig(lua_State *L, int index, config &cfg, int tstring_meta = 0);
bool luaW_tovconfig(lua_State *L, int index, vconfig &vcfg); bool luaW_tovconfig(lua_State *L, int index, vconfig &vcfg);
/** /**