Module

ServerMap

From Dogcraft Wiki

Revision as of 20:50, 29 October 2024 by Xpmodder (talk | contribs) (finished (for now, I think)!)

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()

    -- 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\">\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
	
	-- 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
	
	-- 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 scale number, the current tile is the very center tile that will have the marker in it and needs this extra css class
			if i == scale then
				if j == scale 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/1/x" .. (math.floor(xCoord / 500) + (j - scale)) .. "/z" .. (math.floor(zCoord / 500) + (i - scale)) .. ".png\n"
			
			-- if we are on the center tile, we need to add the marker
			if i == scale then
				if j == scale then
					
					-- calculate the marker position within the tile
					local xMod = xCoord % 500
					local left = 0
				
					if xMod < 0 then
						left = (500 + xMod) / 5.01
					else
						left = xMod / 5.01
					end
					
					local zMod = zCoord % 500
					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 29 October 2024, at 20:50. (21 days ago)