Module:Multiblock

local p = {}

local ipairs, pairs, type = ipairs, pairs, type

p.multiblock = function(f) local args = f.args or f	if f == mw.getCurrentFrame then args = f:getParent.args else f = mw.getCurrentFrame end local alpha = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','-'} structure = {} local size = 0 local height = 0 for i, layer in ipairs(alpha) do if args[layer .. "A1"] ~= nil then --Good, another row structure[layer] = {} for i, letter in ipairs(alpha) do if args[layer .. letter .. 1] ~= nil then structure[layer][letter] = {} for i = 1, (tonumber(args.oversize) or 5) do if args[layer .. letter .. i] ~= nil then structure[layer][letter][i] = args[layer .. letter .. i]							local s = #structure[layer][letter] if s > size then size = s							end end end end end else if i ~= 1 then height = i-1 break --End of multiblock bits else return "Shorted" --Just give up ;)			end		end	end	return build(f, alpha, structure, height, size, args.page or mw.title.getCurrentTitle.text) end

function tableIndex(T, O) --Stupid Lua needing this as well if "table" == type(T) then for i = 1, #T do			if O == T[i] then return i			end end return -1 else error("tableIndex expects table for first argument, " .. type(T) .. " given") end end

p.alt = function(f) local args = f.args or f	if f == mw.getCurrentFrame then args = f:getParent.args else f = mw.getCurrentFrame end local alpha = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','-'} local structure = {} local height = 0 for i, layer in ipairs(alpha) do if args[layer .. "A1"] ~= nil then --Good, another row structure[layer] = {} for i, letter in ipairs(alpha) do if args[layer .. letter .. 1] ~= nil then structure[layer][letter] = {} for i = 1, (tonumber(args.oversize) or 5) do if args[layer .. letter .. i] ~= nil then structure[layer][letter][i] = args[layer .. letter .. i]						end end end end else if i ~= 1 then height = i-1 break else return "Shorted" end end end

local out = {} local size = 0 local offset = 0 for i, layer in ipairs(alpha) do		if i <= height then out[layer] = {} for i, letter in ipairs(alpha) do				if structure[layer][letter] ~= nil then local r = tableReverse(structure[layer][letter]) for i, thing in ipairs(r) do						if out[layer][alpha[i+offset]] == nil then out[layer][alpha[i+offset]] = {} end table.insert(out[layer][alpha[i+offset]], thing) local s = #out[layer][alpha[i+offset]] if (s > size) then size = s						end end offset = offset + 1 else break end end offset = 0 else break end end return build(f, alpha, out, height, size, args.page or "Multiblock") end

function tableReverse(T) --Stupid Lua even pretending tables were intended size = #T out = {} for i = size, 1, -1 do		out[size-i+1] = T[i] end return out end

function build(f, alpha, structure, height, size, p) --There's no chance of this going wrong at all. EVERYTHING. local page = " " .. f:expandTemplate{title="Cg", args={nil, ((size-1)*64)+32, (height*((32*size)+4))+15}} local option = {A={0}} --More creative. for i = 1, size-1 do --1 since account for A offset, -1 since we're a letter ahead local n = -i local done = 1 option[alpha[i+1]] = {} while n < size and done < i+2 do			table.insert(option[alpha[i+1]], n)			n = n + 2 done = done + 1 end end local offset = size + 1 for i = offset-2, 1, -1 do		option[alpha[offset]] = option[alpha[i]] offset = offset + 1 end for i, outer in pairs(structure) do		for j, inner in pairs(outer) do			for k, num in pairs(inner) do page = page .. f:expandTemplate{title="Cg", args={"cell", num, 32*((size-1)+option[j][k]), ((tableIndex(alpha, i)-1)*((32*size)+4))+(21+(16*(tableIndex(alpha, j)-1))), class="craftingGridNoBg"}} end end end

return page .. " "	end

return p