Module:Breaking row

local p = {} p.row = function( f ) local args = f	if f == mw.getCurrentFrame then args = f:getParent.args end local getDplVar = function( var ) local val = f:callParserFunction( '#dplvar', 'breaking ' .. var ) if val == '' then val = false end return val end local row = {} local header, sortable, simple if not getDplVar( 'header' ) then sortable = args.sort if sortable then f:callParserFunction( '#dplvar:set', 'breaking sortable', '1' ) end simple = args.simple if simple then f:callParserFunction( '#dplvar:set', 'breaking simple', '1' ) end local tableClasses = { 'wikitable' } if sortable then table.insert( tableClasses, 'sortable' ) end table.insert( row, '{| class="' .. table.concat( tableClasses, ' ' ) .. '" style="text-align:center"' ) local sortType = '' if sortable then sortType = 'data-sort-type="number"' end header = { '! rowspan="2" | Block' } 		if not simple then table.insert( header, '! rowspan="2" ' .. sortType .. ' | Hardness' ) table.insert( header, '! rowspan="2" | Tool' ) end table.insert( header, '! colspan="' .. ( simple and 6 or 8 ) .. '" | Breaking time (seconds)' ) table.insert( header, '|-' ) local toolColumns = { 'Hand ', 'Wooden', 'Stone', 'Iron', 'Diamond', 'Golden' }		if not simple then table.insert( toolColumns, 'Shears' ) table.insert( toolColumns, 'Sword' ) end for _, tool in ipairs( toolColumns ) do table.insert( header, '! ' .. sortType .. ' | ' .. tool ) end header = table.concat( header, '\n' ) table.insert( row, header ) table.insert( row, '|-' ) f:callParserFunction( '#dplvar:set', 'breaking header', header ) else sortable = getDplVar( 'sortable' ) simple = getDplVar( 'simple' ) end local sprite = require( Module:Sprite ) local hardness = require( Module:Hardness ).hardness local block = mw.text.trim( args[1] ) local hardnessVal = tonumber( hardness{ block } ) if type( hardnessVal ) == 'string' then hardnessVal = '?' end local unbreakable if hardnessVal == -1 or args.liquid then unbreakable = true end local blockSpriteArgs = { settings = 'BlockSprite', block, link = args.link, id = args.sprite }	if args.item then blockSpriteArgs.settings = 'ItemSprite' end table.insert( row, '! style="text-align:left" | ' .. sprite.link( blockSpriteArgs ) .. ( args.note or '' ) ) local tool = mw.text.trim( simple and 'Tool' or args[2] or 'Any' ):gsub( '^%l', string.upper ) local material = mw.text.trim( args[3] or simple and args[2] or 'Any' ):gsub( '^%l', string.upper ) if tool == 'None' then material = tool end if not simple then local hardnessText = hardnessVal if hardnessVal == -1 then hardnessText = ( sortable and 'data-sort-value="999" | ' or '' ) .. '∞'		end table.insert( row, '|' .. hardnessText ) local toolCell = '—' if tool ~= 'Any' and tool ~= 'None' then if material == 'Wood' then material = 'Wooden' end local toolName = ( material ~= 'Any' and material .. ' ' or '' ) .. tool toolCell = ( sortable and 'data-sort-value="' .. toolName .. '" |' or '' ) .. sprite.sprite{ settings = 'ItemSprite', ( material == 'Any' and 'Wooden ' or '' ) .. toolName, title = toolName, link = tool }		end table.insert( row, '|' .. toolCell ) end local choices = {} local getChoice = function( choice, text ) if not choices[choice] then choices[choice] = f:expandTemplate{ title = 'Table Choice', args = { choice, '' } } end return choices[choice] .. text end if hardnessVal == '?' then table.insert( row, '|?||?||?||?||?||?' ) else local round = function( num, idp ) idp = idp or 0 local mult = 10 ^ ( idp < 0 and 0 or idp ) return math.floor( num * mult + 0.5 ) / mult end if unbreakable then table.insert( row, '| ' .. ( sortable and 'data-sort-value="999" ' or '' ) .. getChoice( 'no', '∞' ) ) table.insert( row, '|—||—||—||—||—' ) else local materialGrade = { Any = 0, Wooden = 1, Wood = 1, Golden = 1, Stone = 2, Iron = 3, Diamond = 4, None = 5 }			local materialSpeed = { None = 1, Any = 1, Wooden = 2, Wood = 2, Stone = 4, Iron = 6, Diamond = 8, Golden = 12 }			local drop = 'yes' if args.drop == '0' then drop = 'partial' end local requiredLevel = unbreakable and 999 or materialGrade[material] local insertMaterialCell = function( material ) local willDrop = drop if materialGrade[material] < requiredLevel then willDrop = 'no' end local breakTime = hardnessVal / materialSpeed[material] * ( willDrop == 'no' and 5 or 1.5 ) local precision = 5 - #tostring( round( breakTime ) ) breakTime = round( breakTime, precision ) table.insert( row, '|' .. getChoice( willDrop, breakTime ) ) end if tool == 'Any' or tool == 'None' then insertMaterialCell( 'Any' ) table.insert( row, '|—||—||—||—||—' ) else for _, material in ipairs{ 'Any', 'Wooden', 'Stone', 'Iron', 'Diamond', 'Golden' } do					insertMaterialCell( material ) end end end if not simple then if hardnessVal == '?' then table.insert( row, '|?||?' ) else local toolSpeed = { Shears = 15, Sword = 1.5 }				if block == 'Wool' then toolSpeed.shears = 5 elseif block == 'Cobweb' then toolSpeed.sword = 15 end for _, tool in ipairs{ 'Shears', 'Sword' } do					local toolDrop = args[mw.ustring.lower( tool )] if not toolDrop then table.insert( row, '|—' ) else local willDrop = 'yes' if toolDrop == '0' then willDrop = 'partial' end local breakTime = hardnessVal / toolSpeed[tool] * 1.5 local precision = 5 - #tostring( round( breakTime ) ) breakTime = round( breakTime, precision ) table.insert( row, '|' .. getChoice( willDrop, breakTime ) ) end end end end end table.insert( row, '|-' ) if args.foot then if args.foot == '2' then table.insert( row, header or getDplVar( 'header' ) ) end table.insert( row, '|}' ) f:callParserFunction( '#dplvar:set',			'breaking header', ,			'breaking simple', ,			'breaking sortable', ''		) end return table.concat( row, '\n' ) end return p