This function will convert a table like this:
Into this:
(Note: you will not get the center position into the table.)
It's also possible to set center position coordinates, which would instead return a table with real positions, and not positions relative to the area maps center.
You can define the number which represents the center position on your own. Also, to use numbers above 10, I recommend using variables such as A, B, C etc... Or even starting from 1, considering it might make your area map seem clearer.
Usage:
parseAreaMap(areaMapTable[, centerVal=2])
parseAreaMap(areaMapTable[, centerPos[, centerVal=2] ])
Parameters:
areaMapTable: A multi-dimensional table consisting of values (preferably numbers).
centerPos: [OPTIONAL]: The center position (e.g. a creature or tile position)
centerVal: [OPTIONAL]: The number representing center position in the area map (default 2)
Return value: Table consisting of tables with Position objects.
Example:
Or, you might loop all areas at the same time:
The function:
Any questions, bugs, suggestions, please reply.
Lua:
local areaMap = {
{1,2,0,2,1},
{2,0,0,0,2},
{0,0,3,0,0},
{2,0,0,0,2},
{1,2,0,2,1}
}
Into this:
Lua:
{
[1] = {
{x=-2, y=-2},
{x=2, y=-2},
{x=2, y=2},
{x=-2, y=2}
},
[2] = {
{x=-2, y=-1},
{x=-2, y=-1},
.... ETC (all coordinates where "2" is, relative to center "3").
},
}
It's also possible to set center position coordinates, which would instead return a table with real positions, and not positions relative to the area maps center.
You can define the number which represents the center position on your own. Also, to use numbers above 10, I recommend using variables such as A, B, C etc... Or even starting from 1, considering it might make your area map seem clearer.
Usage:
parseAreaMap(areaMapTable[, centerVal=2])
parseAreaMap(areaMapTable[, centerPos[, centerVal=2] ])
Parameters:
areaMapTable: A multi-dimensional table consisting of values (preferably numbers).
centerPos: [OPTIONAL]: The center position (e.g. a creature or tile position)
centerVal: [OPTIONAL]: The number representing center position in the area map (default 2)
Return value: Table consisting of tables with Position objects.
Example:
Lua:
local areaMap = {
{1,2,0,2,1},
{2,0,0,0,2},
{0,0,3,0,0},
{2,0,0,0,2},
{1,2,0,2,1}
}
local positions = parseAreaMap(areaMap, player:getPosition(), 3) -- setting 3 as center
local firstPositions = positions[1]
local secondPositions = positions[2]
for _, position in ipairs(firstPositions) do
position:sendMagicEffect(CONST_ME_MAGIC_BLUE)
-- and some more code here
end
for _, position in ipairs(secondPositions) do
position:sendMagicEffect(CONST_ME_MAGIC_GREEN)
-- some other code here.....
end
Or, you might loop all areas at the same time:
Lua:
local positionEffects = {[1] = CONST_ME_MAGIC_BLUE, [2] = CONST_ME_MAGIC_GREEN}
local areaPositions = parseAreaMap(areaMap, player:getPosition(), 3)
for i, positions in ipairs(areaPositions) do
for _, position in ipairs(positions) do
position:sendMagicEffect(positionEffects[i]) -- send magic effect based on value on area map...
end
end
The function:
Lua:
function parseAreaMap(t, ...)
if type(t) ~= 'table' then
return false, error("Error: table expected for 'parseAreaMap', got " .. type(t), 2)
end
local args = {...}
local centerVal, centerPos
if type(args[1]) == 'table' then
centerPos = args[1]
centerVal = args[2]
end
centerVal = centerVal or (not centerPos and args[1]) or 2
local rawValues, values, center = {}, {}
for iy, yt in ipairs(t) do
for ix, v in ipairs(yt) do
if v ~= 0 then
if v == centerVal then
if center then
return error("Error: Duplicate center positions!", 2)
end
center = Position(-ix, -iy, 0)
if(centerPos) then
center = centerPos + center
end
-- With center pos just discovered, correct the raw values and insert into values table.
for _, raw in ipairs(rawValues) do
values[raw.value] = values[raw.value] or {}
table.insert(values[raw.value], Position({x = center.x + raw.x, y = center.y + raw.y, z = center.z}))
end
elseif center then
-- We have a center pos, place directly into values.
values[v] = values[v] or {}
table.insert(values[v], Position({x = center.x + ix, y = center.y + iy, z = center.z}))
else
-- We don't have a center pos yet. Store raw.
table.insert(rawValues, {value = v, x = ix, y = iy})
end
end
end
end
if not center then
return false, error("Error: no center position found!", 2)
end
return values
end
Any questions, bugs, suggestions, please reply.
Last edited: