From 17e9cf4e36b63fcf5802253d242f5a4029b9ec64 Mon Sep 17 00:00:00 2001 From: Celtic Minstrel Date: Fri, 19 Feb 2021 01:08:14 -0500 Subject: [PATCH] Add a utility function to extract a location from the front of a variadic parameter pack The purpose of this is to make it easy for functions implemented in Lua to handle locations in the same way as functions implemented in C++. Usage example: Imagine a function foo with the following signature: foo(bar : string, home : location, mode : string, target : location, moo : boolean) -> boolean With the new read_location function it could be implemented as follows: function foo(...) -- First argument goes in bar local bar = ... -- Read location starting at the second argument local home, n = wesnoth.mP.read_location(select(2, ...)) -- note: n will be 0 if a location wasn't found at that position -- This could be an error, or it could be handled as an optional parameter -- Next argument after that goes in mode local mode = select(n + 2, ...) -- Then read a location into target local target, m = wesnoth.map.read_location(select(n + 2, ...)) -- Finally, read a parameter into moo local moo = select(m + n + 2, ...) -- Do stuff with all these parameters return true end With that code, all the following invocations of foo work: foo('a', 'b', true) -- both optional locations omitted foo('a', 1, 2, 'q', 5, 6, false) -- locations given as separate integer parameters foo('a', 'm', {1, 7}, true) -- first location omitted, second given as 2-element array foo('a', some_unit, 'z', {x = 5, y = 10}, false) -- a unit also functions as a location foo('a', 7, 12, 'q', my_leader, true) -- mixing different forms also works --- data/lua/core/map.lua | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/data/lua/core/map.lua b/data/lua/core/map.lua index 20c952245486..6f23a9bb53aa 100644 --- a/data/lua/core/map.lua +++ b/data/lua/core/map.lua @@ -47,4 +47,21 @@ if wesnoth.kernel_type() == "Game Lua Kernel" then return code end end + + function wesnoth.map.read_location(...) + local x, y = ... + if y == nil then + if type(x.x) == 'number' and type(x.y) == 'number' then + x, y = x.x, x.y + elseif type(x[1]) == 'number' and type(x[2]) == 'number' then + x, y = table.unpack(x) + else + return nil, 0 + end + return {x = x, y = y}, 1 + elseif type(x) == 'number' or type(y) == 'number' then + return {x = x, y = y}, 2 + end + return nil, 0 + end end \ No newline at end of file