Module

ServerMap

From Dogcraft Wiki

Revision as of 16:04, 31 October 2024 by Xpmodder (talk | contribs) (should now use lower res images for large scale maps, reducing total number of images)

Documentation for this module may be created at Module:ServerMap/doc

-- Copyright (c) 2024, XPModder, Dogcraft.net and contributors
-- 
-- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
-- to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
-- and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
-- 
-- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-- 
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-- 
-- End license text.

-- A module creating a map of the server by utilising images from the Bluemap server plugin

local p = {}
local scale = 0
local xCoord = 0
local zCoord = 0
local world = "sheltie"
local overlay = ""


function generate()

	-- the number of map tiles is double the scale number + 1. As we start the loops with 0, we only need to set the variable to be double the scale number
	local maxWidthHeight = scale * 2

	local tileSize = 1
	local tilePixels = 500
	local center = scale

    -- output variable will hold the entire output of this module
	-- first we add the outer map container and map grid container divs
	local output = "<div id=\"outer-map-container\">\n"
	output = output .. "<div class=\"map-grid-container\""
	
	if scale >= 7 then
		
		maxWidthHeight = math.floor((scale + 0.5) / 2.5) - 1
		
		if maxWidthHeight % 2 > 0 then
			maxWidthHeight = maxWidthHeight + 1
		end
		
		tileSize = 2
		tilePixels = 2500
		
		if (scale + 0.5) % 2.5 > 0 then
			
			maxWidthHeight = maxWidthHeight + 2
	
			local width = (maxWidthHeight * 2.5) / (scale + 0.5)
			
			local topLeft = (width / 2) - 0.5
			
			output = output .. " style=\"position: relative; width: " .. (width * 100) .. "%; top: -" .. (topLeft * 100) .. "%; left: -" .. (topLeft * 100) .. "%;\""
			
		end
		
		center = maxWidthHeight / 2
		
	end
	
	output = output .. ">\n"
	
	-- now add the link that is visible when hovering over the map
	output = output .. "[https://dogcraft.net/map#" .. world .. ":" .. xCoord .. ":0:" .. zCoord .. ":250:0:0:0:0:perspective Click for full map]\n"
	
	-- if a overlay was given, add it now
	if overlay ~= "" then
		output = output .. "<div class=\"map-overlay-container\">\n" .. overlay .. "\n</div>\n"
	end
	
	-- loop over all required map tiles. i is the row iterator, j is the column iterator
	for i = 0, maxWidthHeight do
		for j = 0, maxWidthHeight do
		
			-- create the grid child div for the current tile
			output = output .. "<div class=\"map-grid-child"
			
			-- if both row and column are equal to the center number, the current tile is the very center tile that will have the marker in it and needs this extra css class
			if i == center then
				if j == center then
					output = output .. " inner-map-container"
				end
			end
			
			-- if we are on the first tile of the last row, add the class to round the bottom left corner and if we are on the last tile of the last row, add the class to round the bottom right corner
			if i == maxWidthHeight then
				if j == 0 then
					output = output .. " map-grid-bottom-left"
				end
				if j == maxWidthHeight then
					output = output .. " map-grid-bottom-right"
				end
			end
			
			-- add the grid-column and grid-row css parameters according to our position (we need to add 1 to both as css doesnt like a row or column of 0)
			output = output .. "\" style=\"grid-row: " .. (i + 1) .. "; grid-column: " .. (j + 1) .. ";\">"
			
			-- here we add the actual image tile of the map
			output = output .. "https://map.dogcraft.net/maps/" .. world .. "/tiles/" .. tileSize .. "/x" .. (math.floor(xCoord / tilePixels) + (j - center)) .. "/z" .. (math.floor(zCoord / tilePixels) + (i - center)) .. ".png\n"
			
			-- if we are on the center tile, we need to add the marker
			if i == center then
				if j == center then
					
					-- calculate the marker position within the tile
					local xMod = xCoord % tilePixels
					local left = 0
				
					if xMod < 0 then
						left = (500 + xMod) / 5.01
					else
						left = xMod / 5.01
					end
					
					local zMod = zCoord % tilePixels
					local top = 0
					
					if zMod < 0 then
						top = (500 + zMod) / 5.01
					else
						top = zMod / 5.01
					end
					
					-- and add the marker at that position. The css calc here is used to correct for the size of the actual marker icon
					output = output .. "<div id=\"marker-wrapper\" style=\"left: calc(" .. left .. "% - 30px); top: calc(" .. top .. "% - 70px);\">\n"
					output = output .. "<i class=\"fa-solid fa-location-dot\"></i>\n"
					output = output .. "</div>\n"
					
				end
			end
			
			-- close the current grid-child div
			output = output .. "</div>\n"
			
		end
	end
	
	-- close the map-grid-container and outer-map-container divs
	output = output .. "</div>\n"
	output = output .. "</div>\n"
	
	-- and finally return the output
	return output
	
end


-- The main function called on #invoke via the template
function p.Map(frame)
    local args = {}
    -- get the arguments
    if frame == mw.getCurrentFrame() then
        args = frame:getParent().args
    else
        args = frame.args
    end

    -- validate the arguments and populate the variables with good values
    local scaleParam = args.scale
    if scaleParam == nil then
    	scaleParam = 0
    end
	scale = tonumber(scaleParam)
	
	local x = args.xCoord
	if x == nil then
		x = 0
	end
	xCoord = tonumber(x)
	
	local z = args.zCoord
	if z == nil then
		z = 0
	end
	zCoord = tonumber(z)
	
	local worldParam = args.world
	if worldParam == nil then
		worldParam = "sheltie"
	end
	world = tostring(worldParam)
	world = mw.text.trim(world)
	world = mw.ustring.lower(world)
	
	local overlayParam = args.overlay
	if overlayParam == nil then
		overlayParam = ""
	end
	overlay = tostring(overlayParam)
	
    -- finally generate the map
    return generate()
end

return p
This page was last modified on 31 October 2024, at 16:04. (21 days ago)