Skip to content

Commit

Permalink
Add a utility function to extract a location from the front of a vari…
Browse files Browse the repository at this point in the history
…adic 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
  • Loading branch information
CelticMinstrel committed Feb 20, 2021
1 parent 998a0ec commit f4b39a4
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions data/lua/core.lua
Expand Up @@ -681,6 +681,23 @@ 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
else
--[========[Backwards compatibility for wml.tovconfig]========]
local fake_vconfig_mt = {
Expand Down

0 comments on commit f4b39a4

Please sign in to comment.