Merge pull request #514 from CelticMinstrel/loop-macros

Some fixes to the looping tags
This commit is contained in:
CelticMinstrel 2015-10-01 22:01:49 -04:00
commit 5698ea218e
3 changed files with 24 additions and 28 deletions

View file

@ -211,24 +211,16 @@
# wmlindent: start ignoring
#define FOREACH ARRAY_VALUE VAR
# Macro to begin a WML clause that iterates over an array.
{VARIABLE {VAR} 0}
[while]
[variable]
name={VAR}
less_than=${ARRAY_VALUE}.length
[/variable]
[for]
array={ARRAY_VALUE}
variable={VAR}
[do]
#enddef
#define NEXT VAR
# Macro to end a WML clause that iterates over an array.
[set_variable]
name={VAR}
add=1
[/set_variable]
[/do]
[/while]
{CLEAR_VARIABLE {VAR}}
[/for]
#enddef
# wmlindent: stop ignoring
@ -242,22 +234,13 @@
#! {REPEAT 5 (
#! {QUAKE "rumble.ogg"}
#! )}
{VARIABLE REPEAT_i 0}
[while]
[variable]
name=REPEAT_i
less_than={NUMBER}
[/variable]
[repeat]
times={NUMBER}
[do]
{BODY_WML}
{VARIABLE_OP REPEAT_i add 1}
[/do]
[/while]
{CLEAR_VARIABLE REPEAT_i}
[/repeat]
#enddef
#define LOOKUP_INDEX FROM_ARRAY_NAME WHERE_KEY_NAME WHERE_VALUE SAVE_AS_NAME

View file

@ -352,7 +352,7 @@ wesnoth.wml_actions["for"] = function(cfg)
first = 0
last = wesnoth.get_variable(cfg.array .. ".length") - 1
step = 1
if cfg.reverse == "yes" then
if cfg.reverse then
first, last = last, first
step = -1
end
@ -361,10 +361,23 @@ wesnoth.wml_actions["for"] = function(cfg)
last = cfg["end"] or first
step = cfg.step or ((last - first) / math.abs(last - first))
end
if ((last - first) / math.abs(last - first)) ~= (step / math.abs(step)) then
-- Sanity check: If they specify something like start,end,step=1,4,-1
-- then we interpret it as start,end,step=4,1,-1
-- (The step takes precedence since it's optional.)
last, first = first, last
end
local i_var = cfg.variable or "i"
local save_i = utils.start_var_scope(i_var)
wesnoth.set_variable(i_var, first)
while wesnoth.get_variable(i_var) <= last do
local function loop_condition()
if first < last then
return wesnoth.get_variable(i_var) <= last
else
return wesnoth.get_variable(i_var) >= last
end
end
while loop_condition() do
for do_child in helper.child_range( cfg, "do" ) do
local action = utils.handle_event_commands(do_child, "loop")
if action == "break" then
@ -377,7 +390,7 @@ wesnoth.wml_actions["for"] = function(cfg)
goto exit
end
end
wesnoth.set_variable(i_var, wesnoth.get_variable(i_var) + 1)
wesnoth.set_variable(i_var, wesnoth.get_variable(i_var) + step)
end
::exit::
utils.end_var_scope(i_var, save_i)

View file

@ -19,7 +19,7 @@ function wml_actions.object(cfg)
if filter then
unit = wesnoth.get_units(filter)[1]
else
unit = wesnoth.get_unit(context.x, context.y)
unit = wesnoth.get_unit(context.x1, context.y1)
end
-- If a unit matches the filter, proceed