Difference between revisions of "Module:Grid"

From Industrial-Craft-Wiki
Jump to navigation Jump to search
m (There is no way the 5 is ever being used.)
(It worked, so I'm refining it a bit.)
 
(39 intermediate revisions by 2 users not shown)
Line 1: Line 1:
--<syntaxhighlight lang="lua">
local p = {}
local p = {}
-- Individual cell
-- Individual cell
Line 6: Line 7:
   
   
     local iLink = nil
     local iLink = nil
    local iFill = nil --[[for fluid_cell implementation]]
     local iNum= math.floor( string.match(item, ',%s*(%d+)' ) or 0 )
     local iNum= math.floor( string.match(item, ',%s*(%d+)' ) or 0 )
     local iName = string.sub(string.match(item, '.+,') or item..' ', 1, -2)
     local iName = string.sub(string.match(item, '.+,') or item..' ', 1, -2)
     local tooltip = string.match(item, '%[.+%]')
     local tooltip = string.match(item, '%[.+%]')
     if tooltip ~= nil then
     if tooltip ~= nil then
     iName = string.sub(iName, string.len(tooltip)+1, -(string.len(string.match(item, ',%s*(%d+)'))))
     iName = string.sub(iName, string.len(tooltip)+1)
    tooltip = string.sub(tooltip,2,-2)
     end
     end
     local hasPercentage = string.match(item, "[%%]")
     local hasPercentage = string.match(item, "[%%]")
   
   
local aliases = mw.loadData('Module:Grid/Aliases')
if tooltip == nil then
if aliases[iName] then
tooltip = aliases[iName]
end
else
if aliases[tooltip] then
tooltip = aliases[tooltip]
end
end
     if(iName:find("~")) then
     if(iName:find("~")) then
         iLink = string.match(iName, '~([%a%s-]+)')
         iLink = string.match(iName, '~(.+)')
         iName = string.match(iName, '([%a%s-]+)~')
        iName = string.match(iName, '(.+)~')
    end
 
    if(iName:find("%^")) then
        local fhandler = mw.loadData('Module:Grid/FluidHandler')
        iFill = string.match(iName, '%^(.+)')
         iName = string.match(iName, '(.+)%^')
        if fhandler[iFill] then
          iFill = fhandler[iFill]  --[[rename]]
        else  --[[invalid fluid, so remove it]]
          iFill = nil
        end
        if fhandler[iName] then
          iName = fhandler[iName]  --[[rename]]
        else  --[[invalid container, so remove fluid]]
          iFill = nil
        end
     end
     end
   
   
Line 25: Line 54:
   
   
     if(iLink ~= nil) then paras["link"]=iLink end
     if(iLink ~= nil) then paras["link"]=iLink end
    if(iFill ~= nil) then paras["fill"]=iFill end
     if(iNum > 1 or hasPercentage) then paras["number"]=iNum end
     if(iNum > 1 or hasPercentage) then paras["number"]=iNum end
     if(hasPercentage) then paras["percent"]="x" end
     if(hasPercentage) then paras["percent"]="x" end
     if(tooltip ~= nil) then paras["tooltip"]=string.sub(tooltip,2,-2) end
     if(tooltip ~= nil) then paras["tooltip"]=tooltip end
      
      
     return paras
     return paras
Line 63: Line 93:
end
end


 
--[[
function p.liquid(f, item)  
function p.liquid(f, item)  
   
   
Line 76: Line 106:
     return text
     return text
end
end
--]]
 
function p.tank(f)
local args = f
if f == mw.getCurrentFrame() then
args = f:getParent().args
end
local link = args.link or ''
local tank = args.tank or ''
local left = args.left or ''
local width = args.width or '24'
local top = tonumber(args.top) or 92
local height = tonumber(args.height) or 94
local max = tonumber(args.max) or 10000
local fill = tonumber(args.fill) or 0
if fill > max then
top = top - height
up = height
down = height * 2
else
top = top - ((height/max)*fill)
up = (height/max)*fill
down = height + ((height/max)*fill)
end
 
local div = mw.html.create('div')
div
:addClass('tooltip')
:css('left', left .. 'px')
:css('top', tostring(top) .. 'px')
:css('position', 'absolute')
:css('width', width .. 'px')
:css('height', tostring(height) .. 'px')
:css('clip', 'rect(' .. tostring(up) .. 'px,' .. tostring(width) ..'px,' .. tostring(down) .. 'px,0px)')
:attr('title', tank)
:wikitext('[[File:Tank ' .. tank .. '.png|' .. tostring(width) .. 'px|link=' .. link .. '|alt=]]')
return tostring( div )
end
 
 
function p.gc ( f )
function p.gc ( f )


Line 121: Line 189:
end
end


--[=[
function p.furnace( f )
function p.carpenter( f )
   
   
     local args = f
     local args = f
Line 131: Line 198:
   
   
     local gridArgs = {  
     local gridArgs = {  
         Input=p.cell(f, (args.Input or args.Top)),
        Liquid=p.liquid(f, args.Liquid),
         Fuel=p.cell(f, (args.Fuel or args.Bottom)),
        A1=p.cell(f, args.A1),
         A2=p.cell(f, args.A2),
        A3=p.cell(f, args.A3),
        B1=p.cell(f, args.B1),
        B2=p.cell(f, args.B2),
         B3=p.cell(f, args.B3),
        C1=p.cell(f, args.C1),
        C2=p.cell(f, args.C2),
        C3=p.cell(f, args.C3),
         Output=p.cell(f, args.Output),
         Output=p.cell(f, args.Output),
        Break=args.Break
     }
     }
   
   
   return f:expandTemplate{ title='CraftingCarpenter', args= gridArgs}
   return f:expandTemplate{ title='Crafting/Furnace', args= gridArgs}
   
   
end
end
   
   
function p.centrifuge( f )
 
function p.turningNumber(number)
    local args = f
if number == '1' then return 22
elseif number == '2' then return 44
elseif number == '3' then return 66
elseif number == '4' then return 88
else return 110
end
end
   
   
    if f == mw.getCurrentFrame() then
function p.turningBarrel(number,t)
    args = f:getParent().args
if t == nil then
if number == '1' then return 38
elseif number == '2' then return 46
elseif number == '3' then return 52
elseif number == '4' then return 58
else return 64
end
else
if number == '1' then return 26
elseif number == '2' then return 20
elseif number == '3' then return 14
elseif number == '4' then return 6
else return 0
end
end
end
    local gridArgs = {
        Input=p.cell(f, args.Input),
        Output1=p.cell(f, args.Output1),
        Output2=p.cell(f, args.Output2),
        Output3=p.cell(f, args.Output3)
    }
  return f:expandTemplate{ title='CraftingCentrifuge', args= gridArgs}
end
end
--]=]
function p.furnace( f )
   
   
    local args = f
function p.turning(f)
local args = f
    if f == mw.getCurrentFrame() then
if f == mw.getCurrentFrame() then
args = f:getParent().args
args = f:getParent().args
end
end
local gridArgs = {Output=p.cell(f, args.Output)}
    local gridArgs = {  
if args.float or args.Float then gridArgs.float = args.float or args.Float end
        Input=p.cell(f, (args.Input or args.Top)),
if args.nobreak then gridArgs.nobreak = args.nobreak end
        Fuel=p.cell(f, (args.Fuel or args.Bottom)),
        Output=p.cell(f, args.Output),
if args.Output == nil then return f:expandTemplate{title='Grid/Turning Table/Return', args=gridArgs} else gridArgs.Output = args.Output end
        Break=args.Break
 
    }
local valid = {Drill='Iron', Handle='Iron', Axle='Iron', Wood='Wooden', Wooden='Wooden'}
if valid[args.Output] == nil then
  return f:expandTemplate{ title='Crafting/Furnace', args= gridArgs}
return f:expandTemplate{title='Grid/Turning Table/Return', args=gridArgs}
else
gridArgs.Type = valid[args.Output]
end
 
if gridArgs.Type == 'Wooden' then
bounce = 3
else
bounce = 5
end
gridArgs.top = 70
if args.Layout ~= nil and args.Layout:find(',') then
local layout = mw.text.split(args.Layout,"%g*,%g*")
for i = 1, #layout do
if tonumber(layout[i]) > bounce then
layout[i] = tostring(bounce)
elseif tonumber(layout[i]) < 1 then
layout[i] = "1"
end
if tonumber(layout[i]) == 5 then
gridArgs.top = 71
end
end
gridArgs.n1 = p.turningNumber(layout[1])
gridArgs.b1 = p.turningBarrel(layout[1])
gridArgs.b1t = p.turningBarrel(layout[1],1)
gridArgs.n2 = p.turningNumber(layout[2])
gridArgs.b2 = p.turningBarrel(layout[2])
gridArgs.b2t = p.turningBarrel(layout[2],1)
gridArgs.n3 = p.turningNumber(layout[3] or bounce)
gridArgs.b3 = p.turningBarrel(layout[3] or bounce)
gridArgs.b3t = p.turningBarrel((layout[3] or bounce),1)
gridArgs.n4 = p.turningNumber(layout[4] or bounce)
gridArgs.b4 = p.turningBarrel(layout[4] or bounce)
gridArgs.b4t = p.turningBarrel((layout[4] or bounce),1)
gridArgs.n5 = p.turningNumber(layout[5] or bounce)
gridArgs.b5 = p.turningBarrel(layout[5] or bounce)
gridArgs.b5t = p.turningBarrel((layout[5] or bounce),1)
else
barrelbounce = p.turningBarrel(bounce)
barreltbounce = p.turningBarrel(bounce,1)
bounce = p.turningNumber(bounce)
gridArgs.n1 = bounce
gridArgs.b1 = barrelbounce
gridArgs.b1t = barreltbounce
gridArgs.n2 = bounce
gridArgs.b2 = barrelbounce
gridArgs.b2t = barreltbounce
gridArgs.n3 = bounce
gridArgs.b3 = barrelbounce
gridArgs.b3t = barreltbounce
gridArgs.n4 = bounce
gridArgs.b4 = barrelbounce
gridArgs.b4t = barreltbounce
gridArgs.n5 = bounce
gridArgs.b5 = barrelbounce
gridArgs.b5t = barreltbounce
end
return f:expandTemplate{title='Grid/Turning Table/Return', args=gridArgs}
end
end
   
   
return p
return p
--</syntaxhighlight>

Latest revision as of 13:43, 13 December 2016

Template-info.png Documentation

This module is a WIP (work in progress), but is still being called by {{Grid/Crafting Table}}, so any changes should be tested to avoid inconvenience.

Related Links[edit]

The above documentation is transcluded from Module:Grid/doc. (edit | history)


--<syntaxhighlight lang="lua">
local p = {}
-- Individual cell
function p.grid(item)
 
    if (item == nil) then return { } end
 
    local iLink = nil
    local iFill = nil --[[for fluid_cell implementation]]
    local iNum= math.floor( string.match(item, ',%s*(%d+)' ) or 0 )
    local iName = string.sub(string.match(item, '.+,') or item..' ', 1, -2)
    local tooltip = string.match(item, '%[.+%]')
    if tooltip ~= nil then
    	iName = string.sub(iName, string.len(tooltip)+1)
    	tooltip = string.sub(tooltip,2,-2)
    end
    local hasPercentage = string.match(item, "[%%]")
 
	local aliases = mw.loadData('Module:Grid/Aliases')
	if tooltip == nil then
		if aliases[iName] then
			tooltip = aliases[iName]
		end
	else
		if aliases[tooltip] then
			tooltip = aliases[tooltip]
		end
	end

    if(iName:find("~")) then
        iLink = string.match(iName, '~(.+)')
        iName = string.match(iName, '(.+)~')
    end

    if(iName:find("%^")) then
        local fhandler = mw.loadData('Module:Grid/FluidHandler')
        iFill = string.match(iName, '%^(.+)')
        iName = string.match(iName, '(.+)%^')
        if fhandler[iFill] then
          iFill = fhandler[iFill]  --[[rename]]
        else  --[[invalid fluid, so remove it]]
          iFill = nil
        end
        if fhandler[iName] then
          iName = fhandler[iName]  --[[rename]]
        else  --[[invalid container, so remove fluid]]
          iFill = nil
        end
    end
 
    paras = { }
 
    paras["name"] = iName
 
    if(iLink ~= nil) then paras["link"]=iLink end
    if(iFill ~= nil) then paras["fill"]=iFill end
    if(iNum > 1 or hasPercentage) then paras["number"]=iNum end
    if(hasPercentage) then paras["percent"]="x" end
    if(tooltip ~= nil) then paras["tooltip"]=tooltip end
    
    return paras
 
end


-- Gets a cell or animated cell
function p.cell(f, item) 
 
    if (item == nil) then return '' end
 
    if (item:find(';')) then
        local text = '<div class=\"animated-grids\">'
 
 
        for frame in mw.text.gsplit( item, '%s*;%s*' ) do
 
            text = text .. '<span class=\"animated-grid\">'
            if frame == 'Blank' or frame == 'blank' then
               text = text .. ''
            else
               text = text .. f:expandTemplate{ title='Grid', args = p.grid(frame) }
            end
            text = text .. '</span>'
 
        end
 
        text = text .. '</div>'
 
        return text
    end
 
    return f:expandTemplate{ title='Grid', args = p.grid(item) }
end

--[[
function p.liquid(f, item) 
 
    if (item == nil) then return '' end
 
    local text = ''
 
    for i=1,3,1 do
        text = text .. f:expandTemplate{ title='Grid', args = p.grid(item) }
    end
 
    return text
end
--]]

function p.tank(f)
	local args = f
	if f == mw.getCurrentFrame() then
		args = f:getParent().args
	end
	local link = args.link or ''
	local tank = args.tank or ''
	local left = args.left or ''
	local width = args.width or '24'
	local top = tonumber(args.top) or 92
	local height = tonumber(args.height) or 94
	local max = tonumber(args.max) or 10000
	local fill = tonumber(args.fill) or 0
	if fill > max then
		top = top - height
		up = height
		down = height * 2
	else
		top = top - ((height/max)*fill)
		up = (height/max)*fill
		down = height + ((height/max)*fill)
	end

	local div = mw.html.create('div')
	div
		:addClass('tooltip')
		:css('left', left .. 'px')
		:css('top', tostring(top) .. 'px')
		:css('position', 'absolute')
		:css('width', width .. 'px')
		:css('height', tostring(height) .. 'px')
		:css('clip', 'rect(' .. tostring(up) .. 'px,' .. tostring(width) ..'px,' .. tostring(down) .. 'px,0px)')
		:attr('title', tank)
		:wikitext('[[File:Tank ' .. tank .. '.png|' .. tostring(width) .. 'px|link=' .. link .. '|alt=]]')
	return tostring( div )
end


function p.gc ( f )

   local args = f

   if f == mw.getCurrentFrame() then
      args = f:getParent().args
   end

   return p.cell(f, (args[1] or args.name))
end


function p.craftingTable( f )
 
    local args = f
 
	if f == mw.getCurrentFrame() then
		args = f:getParent().args
	end
 
    local gridArgs = { 
 
        A1=p.cell(f, args.A1),
        A2=p.cell(f, args.A2),
        A3=p.cell(f, args.A3),
 
        B1=p.cell(f, args.B1),
        B2=p.cell(f, args.B2),
        B3=p.cell(f, args.B3),
 
        C1=p.cell(f, args.C1),
        C2=p.cell(f, args.C2),
        C3=p.cell(f, args.C3),
 
        Output=p.cell(f, args.Output),
 
        Break=args.Break
    }

   return f:expandTemplate{ title='Crafting', args= gridArgs}
 
end

function p.furnace( f )
 
    local args = f
 
    if f == mw.getCurrentFrame() then
		args = f:getParent().args
	end
 
    local gridArgs = { 
        Input=p.cell(f, (args.Input or args.Top)),
        Fuel=p.cell(f, (args.Fuel or args.Bottom)),
        Output=p.cell(f, args.Output),
        Break=args.Break
    }
 
   return f:expandTemplate{ title='Crafting/Furnace', args= gridArgs}
 
end
 

function p.turningNumber(number)
	if number == '1' then return 22
	elseif number == '2' then return 44
	elseif number == '3' then return 66
	elseif number == '4' then return 88
	else return 110
	end
end
 
function p.turningBarrel(number,t)
	if t == nil then
		if number == '1' then return 38
		elseif number == '2' then return 46
		elseif number == '3' then return 52
		elseif number == '4' then return 58
		else return 64
		end
	else
		if number == '1' then return 26
		elseif number == '2' then return 20
		elseif number == '3' then return 14
		elseif number == '4' then return 6
		else return 0
		end
	end
end
 
function p.turning(f)
	local args = f
	if f == mw.getCurrentFrame() then
		args = f:getParent().args
	end
	local gridArgs = {Output=p.cell(f, args.Output)}
	if args.float or args.Float then gridArgs.float = args.float or args.Float end
	if args.nobreak then gridArgs.nobreak = args.nobreak end
	
	if args.Output == nil then return f:expandTemplate{title='Grid/Turning Table/Return', args=gridArgs} else gridArgs.Output = args.Output end

	local valid = {Drill='Iron', Handle='Iron', Axle='Iron', Wood='Wooden', Wooden='Wooden'}
	if valid[args.Output] == nil then
		return f:expandTemplate{title='Grid/Turning Table/Return', args=gridArgs}
	else
		gridArgs.Type = valid[args.Output]
	end

	if gridArgs.Type == 'Wooden' then
		bounce = 3
	else
		bounce = 5
	end
	gridArgs.top = 70
	if args.Layout ~= nil and args.Layout:find(',') then
		local layout = mw.text.split(args.Layout,"%g*,%g*")
		for i = 1, #layout do
			if tonumber(layout[i]) > bounce then
				layout[i] = tostring(bounce)
			elseif tonumber(layout[i]) < 1 then
				layout[i] = "1"
			end
			if tonumber(layout[i]) == 5 then
				gridArgs.top = 71
			end
		end
		gridArgs.n1 = p.turningNumber(layout[1])
		gridArgs.b1 = p.turningBarrel(layout[1])
		gridArgs.b1t = p.turningBarrel(layout[1],1)
		gridArgs.n2 = p.turningNumber(layout[2])
		gridArgs.b2 = p.turningBarrel(layout[2])
		gridArgs.b2t = p.turningBarrel(layout[2],1)
		gridArgs.n3 = p.turningNumber(layout[3] or bounce)
		gridArgs.b3 = p.turningBarrel(layout[3] or bounce)
		gridArgs.b3t = p.turningBarrel((layout[3] or bounce),1)
		gridArgs.n4 = p.turningNumber(layout[4] or bounce)
		gridArgs.b4 = p.turningBarrel(layout[4] or bounce)
		gridArgs.b4t = p.turningBarrel((layout[4] or bounce),1)
		gridArgs.n5 = p.turningNumber(layout[5] or bounce)
		gridArgs.b5 = p.turningBarrel(layout[5] or bounce)
		gridArgs.b5t = p.turningBarrel((layout[5] or bounce),1)
	else
		barrelbounce = p.turningBarrel(bounce)
		barreltbounce = p.turningBarrel(bounce,1)
		bounce = p.turningNumber(bounce)
		gridArgs.n1 = bounce
		gridArgs.b1 = barrelbounce
		gridArgs.b1t = barreltbounce
		gridArgs.n2 = bounce
		gridArgs.b2 = barrelbounce
		gridArgs.b2t = barreltbounce
		gridArgs.n3 = bounce
		gridArgs.b3 = barrelbounce
		gridArgs.b3t = barreltbounce
		gridArgs.n4 = bounce
		gridArgs.b4 = barrelbounce
		gridArgs.b4t = barreltbounce
		gridArgs.n5 = bounce
		gridArgs.b5 = barrelbounce
		gridArgs.b5t = barreltbounce
	end
	
	return f:expandTemplate{title='Grid/Turning Table/Return', args=gridArgs}
end
 
return p
--</syntaxhighlight>