Module:Map

From Outward Wiki
Jump to navigation Jump to search
Template-info.svg Documentation

Module invoked by the Map template.


local p = {}

local mapImages = {
	chersonese = 'NewChersoneseMap.png',
	cierzo = 'NewCierzoMap.png',
	enmerkarforest = 'NewEnmerkar_ForestMap.png',
	berg = 'NewBergMap.png',
	hallowedmarsh = 'NewHallowed_MarshMap.png',
	monsoon = 'NewMonsoonMap.png',
	abrassar = 'NewAbrassarMap.png',
	levant = 'NewLevantMap.png',
	antiqueplateau = 'NewAntique_PlateauMap.png',
	harmattan = 'NewHarmattanMap.png',
	caldera = 'NewCalderaMap.png',
	cierzodestroyed = 'NewCierzo (Destroyed)Map.png'
}

function p.main(frame)
	args = require('Module:ProcessArgs').merge(true)

	local width = args.width or '400px'
	local height = args.height or '400px'
	local center = args.center or args.centre or '50,50'
	local zoom = args.zoom or '2'
	local minzoom = args.minzoom or '1'
	
	local mapImage = mapImages[args.map]
	if not mapImage then 
		return 'Unrecognized map image. See [[Template:Map]] for accepted values.' 
	end
	
    local rects = ''
    if (args.points) then
    	local allsplit = split(args.points, ';')
    	for i,arg in ipairs(allsplit) do
			local split = split(arg, ',')
			rects = rects .. p.addSimplePoint(split[1], split[2], split[3], split[4], split[5])
		end
	end
	
    if (args.rectangles) then
    	local allsplit = split(args.rectangles, ';')
    	for i,arg in ipairs(allsplit) do
			local split = split(arg, ',')
			rects = rects .. p.addRectangle(split[1], split[2], split[3], split[4], split[5], split[6], split[7])
		end
	end
	
	local out = [[{{#display_map:
| zoom = ]] .. zoom .. [[

| center = ]] .. center .. [[

| image layers=]] .. mapImage .. [[

| minzoom = ]] .. minzoom .. [[
| width = ]] .. width .. [[
| height = ]] .. height .. [[

| service=leaflet
| maxzoom = 4
| fullscreen = yes
| scrollzoom = no
| static = no
| rectangles=
]]..rects..'}}'
	
	return frame:preprocess(out)
end

function p.addSimplePoint(y, x, title, description, color)
	if not y or not x then return '' end
	if tonumber(y) < 0 or tonumber(x) < 0 then return '' end
	local out = (y - 0.5) .. ',' .. (x - 0.5) .. ':' .. (y + 0.5) .. ',' .. (x + 0.5)
	color = color or '#00fffb'
	out = out .. '~' .. (title or '') .. '~' .. (description or '') .. '~' .. color .. '~0.9~2~' .. color .. [[~0.2;
]]
	return out
end

function p.addRectangle(yA, xA, yB, xB, title, description, color)
	if not yA or not xA then return '' end
	if tonumber(yA) < 0 or tonumber(xA) < 0 or tonumber(yB) < 0 or tonumber(xB) < 0 then return '' end
	local out = yA .. ',' .. xA .. ':' .. yB .. ',' .. xB
	color = color or '#00fffb'
	out = out .. '~' .. (title or '') .. '~' .. (description or '') .. '~' .. color .. '~0.9~2~' .. color .. [[~0.2;
]]
	return out
end

----------------------------------------------------------------
--------------------------- HELPERS ----------------------------

function find(tbl, val)
    for k, v in pairs(tbl) do
        if k == val then return v end
    end
    return nil
end

function notempty(string)
    return string ~= nil and string ~= ''
end

function hasmatch(table1, table2)
    for _,v in ipairs(table1) do
        if contains(table2, v) then
            return true
        end
    end
    return false
end

function contains(table, value)
    for _,v in ipairs(table) do
        if v == value then
            return true
        end
    end
    return false
end

function split(inputstr, sep)
    if sep == nil then
        sep = "%s"
    end
    local t={}
    if notempty(inputstr) then
        for str in string.gmatch(inputstr .. ',', "([^"..sep.."]+)") do
            table.insert(t, str)
        end
    end
    return t
end

return p