-- <pre>
-- IMPORTANT: This module should be considered UNSTABLE and not be used widely
-- until further testing/bugfixing
		Used to create a static map made up from tiles.
		This is a metamodule for use in other LUA modules, and should not be
		invoked. If you're looking for an invokable module, you probably want
		Module:CoordinateMap instead.
-- Usage -----------------------------------------------------------------------
		.create(left, top, width, height, mapid)
			left - leftmost X position the map should show
			top - topmost Y position the map should show
			width - width the map should be
			height - height the map should be
			mapdata - table of map data containing the following values:
				tilesx = total number of tiles horizontally
				tilesy = total number of tiles vertically
				tilewidth = width of each tile
				tileheight = height of each tile
				blanktiles = table of tiles which are solid color e.g. sea tiles
					e.g. {12,134,155}
				blankcolor = color of the blank tiles
				(See Module:CoordinateMap/data for examples)
		Returns a TileMap object with the following functions:
			:getTileCoords(x, y)
				x - x coord in pixels
				y - y coord in pixels
			Returns the local col,row of a map tile based on the pixel values
			:getTileId(x, y)
				x - x coordy (col) of tile
				y - y coordy (row) of tile
			Returns the id of the tile based on local x,y coords
				id - id of the tile
			Returns true if the tile is blank or out of bounds, false otherwise
		TileMap object also has the following properties:
			data = data loaded from Module:TileMap/data based on mapid
			width = width of map
			height = height of map
			xoffset = map's local X offset
			yoffset = map's local Y offset
			left = leftmost relevant tile column
			top = topmost relevant tile row
			right = rightmost relevant tile column
			bottom = bottommost relevant tile row
			container = map container
			map = inner map container
		Calling tostring() on a TileMap object will return the TileMap
		container's HTML.
			local TileMap = require('Module:TileMap')
			local tmap = TileMap.create(
				200, 400, --left, top
				500, 500, --width, height
				mapdata -- var containing map data
			tmap.container:addClass('tright') -- align right
			return tmap -- return a 500x500 map of keldagrim starting at 200,400
			(See Module:CoordinateMap for a real example of this module in use)
-- Private functions -----------------------------------------------------------
-- Convert S -> Spx
local function px(s)
	return string.format('%spx', tostring(s))
-- Public TileMap class --------------------------------------------------------
local TileMap = {}
TileMap.__index = TileMap
-- Constructor
function TileMap.create(left, top, width, height, mapdata)
	local self = setmetatable({}, TileMap) = mapdata
	self.width = width
	self.height = height
	self.xoffset = -(left %
	self.yoffset = -(top %
	self.left, = self:getTileCoords(left, top)
	self.right, self.bottom =
		self:getTileCoords(left + self.width, top + self.height)
	self.container = mw.html.create('div')
			['position'] = 'relative',
			['overflow'] = 'hidden',
			['background-color'] = '#000',
			['width'] = px(self.width),
			['height'] = px(self.height)
		}) = self.container:tag('div')
			['position'] = 'absolute',
			['width'] = px((self.right - self.left + 1) *,
			['left'] = px(self.xoffset),
			['top'] = px(self.yoffset)
	-- Main loop
	for row =, self.bottom do -- Loop through relevant rows
		for col = self.left, self.right do -- Loop through relevant columns
			if self:isTileBlank(col, row) then -- Tile is out of bounds/blank'div')
						['display'] = 'inline-block',
						['vertical-align'] = 'middle',
						['background-color'] = or 'black',
						['width'] = px(,
						['height'] = px(
			else -- Tile is in bounds and not blank, add it
					'[[File:%s %d.png|%dx%dpx|link=|alt=]]',, self:getTileId(col, row),,
	return self
function TileMap:__tostring()
	return tostring(self.container)
-- Get tile coords from pixel coords
function TileMap:getTileCoords(x, y)
	return math.floor(x /,
		math.floor(y /
-- Get tile number from coords
function TileMap:getTileId(col, row)
	return (row * + col
-- Is a tile blank?
function TileMap:isTileBlank(col, row)
	if row < 0 or col < 0 or
		row >= or col >=
		return true
	local id = self:getTileId(col, row)
	for _, v in ipairs( do
		if v == id then return true end
	return false
return TileMap

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.