Minecraft Wiki
Advertisement

Este módulo implementa {{Linha de quebramento}}

Dependências

[ver | editar | histórico | purgar]A documentação acima é transcluída de Módulo:Breaking row/doc.
local p = {}

local breakingTimeHeader;
local function getBreakingTimeHeader( f )
    if breakingTimeHeader == nil then
        breakingTimeHeader = 'Tempo  de [[Quebrar|quebra]]' .. f:preprocess( '<ref group="nota" name="breakingtimenote">Os tempos são para ferramentas sem encantamento em segundos.</ref>' )
    end
    return breakingTimeHeader
end

p.row = function( f )
	lang = mw.getContentLanguage()
	local args = require( [[Módulo:ProcessArgs]] ).norm()
	
	local getDplVar = function( var )
		local val = f:callParserFunction( '#dplvar', 'breaking ' .. var )
		if val == '' then
			val = false
		end
		return val
	end
	local dplVars = {}
	local setDplVar = function( var, val )
		table.insert( dplVars, 'breaking ' .. var )
		table.insert( dplVars, val or '1' )
	end
		
	local rows = {}
	local tableParts = {}
	local categories = {}
	
	local horizontal
	if args.horizontal or args[1]:match( ';' ) then
		horizontal = true
	end
	local showFerramenta = true
	local showTesoura = true
	local showEspada = true
	local header, sortable, simple
	if horizontal or not getDplVar( 'header' ) then
		if args.hideferramenta or horizontal and ( not args[2] or args[2]:lower() == 'qualquer' or args[2]:lower() == 'nenhum' ) then
			showFerramenta = false
			setDplVar( 'hideferramenta' )
		end
		if args.hidetesoura or horizontal and not args.tesoura then
			showTesoura = false
			setDplVar( 'hidetesoura' )
		end
		if args.hideespada or horizontal and not args.espada then
			showEspada = false
			setDplVar( 'hideespada' )
		end
		
		sortable = not horizontal and args.sort
		if sortable then
			setDplVar( 'sortable' )
		end
		simple = args.simple
		if simple and not horizontal then
			setDplVar( 'simple' )
		end
		
		local tableClasses = { 'wikitable' }
		if sortable then
			table.insert( tableClasses, 'sortable' )
		end
		table.insert( rows, ' {| class="' .. table.concat( tableClasses, ' ' ) .. '" style="text-align:center"' )
		
		local sortType = ''
		if sortable then
			sortType = 'data-sort-type="number"'
		end
		local rowspan = ''
		if not horizontal then
			rowspan = 'rowspan="2" '
		end
		header = {
			'! ' .. rowspan .. ' | Bloco'
		}
		
		if not simple then
			table.insert( header, '! ' .. rowspan .. sortType .. ' | Dureza' )
			if showFerramenta then
				table.insert( header, '! ' .. rowspan .. ' | Ferramenta' )
			end
		end
		
		local ferramentaColumns = {}
		if showFerramenta then
			ferramentaColumns = { 'Madeira', 'Pedra', 'Ferro', 'Diamante', 'Ouro' }
		end
		table.insert(
			ferramentaColumns, 1,
			'<abbr title="Inclui segurar itens não-ferramenta ou o tipo errado de ferramenta">Mão</abbr>'
		)
		if not simple then
			if showTesoura then
				table.insert( ferramentaColumns, 'Tesoura' )
			end
			if showEspada then
				table.insert( ferramentaColumns, 'Espada' )
			end
		end
		
		if not horizontal then
			table.insert( header, '! colspan="' .. #ferramentaColumns .. '" |' .. getBreakingTimeHeader( f ) )
			table.insert( header, '|-' )
		end
		
		for _, ferramenta in ipairs( ferramentaColumns ) do
			table.insert( header, '! ' .. sortType .. ' | ' .. ferramenta )
		end
		
		if not horizontal then
			header = table.concat( header, '\n' )
			setDplVar( 'header', header )
		end
		table.insert( tableParts, header )
	else
		showFerramenta = not getDplVar( 'hideferramenta' )
		showTesoura = not getDplVar( 'hidetesoura' )
		showEspada = not getDplVar( 'hideespada' )
		sortable = getDplVar( 'sortable' )
		simple = getDplVar( 'simple' )
	end
	
	local sprite = require( [[Módulo:Sprite]] )
	local dureza = require( [[Módulo:Block value]] ).value
	
	local fillCells = function( cellsTable, text, num )
		for i = 1, num do
			table.insert( cellsTable, text )
		end
	end
	local materialGrade = {
		Qualquer = 0,
		Madeira = 1,
		Ouro = 1,
		Pedra = 2,
		Ferro = 3,
		Diamante = 4,
		Nenhum = 5
	}
	local materialSpeed = {
		Nenhum = 1,
		Qualquer = 1,
		Madeira = 2,
		Pedra = 4,
		Ferro = 6,
		Diamante = 8,
		Ouro = 12
	}
	local insertBlock = function( blockArgs )
		local cells = {}
		local blocks = mw.text.split( blockArgs[1], '%s*,%s*' )
		local durezaVal = tonumber( dureza{ blocks[1], type = 'dureza' } )
		if not durezaVal then
			durezaVal = '?'
		end
		local unbreakable
		if durezaVal == -1 or blockArgs.liquid then
			unbreakable = true
		end
		
		local blockSprites = {}
		local links = mw.text.split( blockArgs.link or '', '%s*,%s*' )
		local ids = mw.text.split( blockArgs.sprite or '', '%s*,%s*' )
		local items = mw.text.split( blockArgs.item or '', '%s*,%s*' )
		for i, block in ipairs( blocks ) do
			local link
			if not links[i] and links[1] ~= '' then
				link = links[1]
			elseif links[i] ~= '' then
				link = links[i]
			end
			local id
			if not ids[i] and ids[1] ~= '' then
				id = ids[1]
			elseif ids[i] ~= '' then
				id = ids[i]
			end
			local blockText
			if args.textTrim then
				blockText = block:gsub( '^' .. args.textTrim .. ' ', '' )
			else
				blockText = block
			end
			local blockSpriteArgs = {
				data = 'BlockSprite',
				block,
				text = blockText,
				link = link,
				id = id
			}
			if items[i] == '1' or not items[i] and items[1] == '1' then
				blockSpriteArgs.data = 'ItemSprite'
			end
			
			local image, spriteCat = sprite.link( blockSpriteArgs )
			table.insert( blockSprites, image )
			table.insert( categories, spriteCat )
		end
		table.insert( cells,
			'! style="text-align:left" | ' .. table.concat( blockSprites, '<br>' ) .. ( blockArgs.note or '' )
		)
		
		local ferramenta = mw.text.trim( simple and 'Ferramenta' or blockArgs[2] or 'Qualquer' ):gsub( '^%l', string.upper )
		local material = mw.text.trim( simple and blockArgs[2] or blockArgs[3] or 'Qualquer' ):gsub( '^%l', string.upper )
		if ferramenta == 'Nenhum' then
			material = ferramenta
		end
		if not simple then
			local durezaText = durezaVal
			if durezaVal == -1 then
				durezaText = ( sortable and 'data-sort-value="999" | ' or '' ) .. '∞'
			else
				if durezaVal == '?' then
					durezaText = durezaVal
				else
					durezaText = lang:formatNum( durezaVal )
				end
			end
			table.insert( cells, '|' .. durezaText )
			
			if showFerramenta then
				local ferramentaCell = '—'
				if ferramenta ~= 'Qualquer' and ferramenta ~= 'Nenhum' then
					if material == 'Madeira' then
						material = 'Madeira'
					end
					local ferramentaName = ( material == 'Qualquer' and material .. ' ' or '' ) .. ferramenta .. ( material ~= 'Qualquer' and ' de ' .. mw.ustring.lower(material) or '' )
					local image, spriteCat = sprite.sprite{
						data = 'ItemSprite',
						ferramenta .. ( material ~= 'Qualquer' and ' de ' .. mw.ustring.lower(material) or ' de madeira' ),
						title = ferramentaName,
						link = ferramenta
					}
					ferramentaCell = ( sortable and 'data-sort-value="' .. ferramentaName .. '" |' or '' ) .. image
					table.insert( categories, spriteCat )
				end
				table.insert( cells, '|' .. ferramentaCell )
			end
		end
		
		local choices = {}
		local getChoice = function( choice, text )
			if not choices[choice] then
				choices[choice] = f:expandTemplate{ title = 'Tabela de Escolha', args = { choice, '' } }
			end
			return choices[choice] .. text
		end
		
		if durezaVal == '?' then
			fillCells( cells, '|?', 6 )
		else
			local processTime = function( num )
				if num < 0.05 then		-- Blocos têm um tempo mínimo de ruptura de um tick do jogo (0,05 segundos)
					num = 0.05
				else					-- E eles devem ser quebrados em múltiplos de 1 tick do jogo (0.05 segundos)
					num = math.ceil( num * 20 ) / 20
				end
				return num
			end
			
			if unbreakable then
				table.insert( cells, '| ' .. ( sortable and 'data-sort-value="999" ' or '' ) .. getChoice( 'não', '∞' ) )
				if showFerramenta then
					fillCells( cells, '|—', 5 )
				end
			else
				local drop = 'sim'
				if blockArgs.drop == '0' then
					drop = 'parcial'
				end
				local requiredLevel = unbreakable and 999 or materialGrade[material]
				local insertMaterialCell = function( material )
					local willDrop = drop
					if materialGrade[material] < requiredLevel then
						willDrop = 'não'
					end
					local breakTime = processTime( durezaVal / materialSpeed[material] * ( willDrop == 'não' and 5 or 1.5 ) )
					table.insert( cells, '|' .. getChoice( willDrop, lang:formatNum( breakTime ) ) )
				end
				
				if not showFerramenta or ferramenta == 'Qualquer' or ferramenta == 'Nenhum' then
					insertMaterialCell( 'Qualquer' )
					if showFerramenta then
						fillCells( cells, '|—', 5 )
					end
				else
					for _, material in ipairs{ 'Qualquer', 'Madeira', 'Pedra', 'Ferro', 'Diamante', 'Ouro' } do
						insertMaterialCell( material )
					end
				end
			end
			
			if not simple and ( showTesoura or showEspada ) then
				local ferramentas = {}
				if showTesoura then
					table.insert( ferramentas, 'Tesoura' )
				end
				if showEspada then
					table.insert( ferramentas, 'Espada' )
				end
				if durezaVal == '?' then
					fillCells( cells, '|?', #ferramentas )
				else
					local ferramentaSpeed = {
						Tesoura = 1,
						Espada = 1.5
					}
					if blocks[1] == 'Lã' then
						ferramentaSpeed.Tesoura = 5
					elseif blocks[1] == 'Folhas' then
						ferramentaSpeed.Tesoura = 15
					elseif blocks[1] == 'Teia' then
						ferramentaSpeed.Espada = 15
						ferramentaSpeed.Tesoura = 15
					end
					
					for _, ferramenta in ipairs( ferramentas ) do
						local ferramentaDrop = blockArgs[mw.ustring.lower( ferramenta )]
						if not ferramentaDrop then
							table.insert( cells, '|—' )
						else
							local willDrop = 'sim'
							if ferramentaDrop == '0' then
								willDrop = 'parcial'
							end
							
							local breakTime = processTime( durezaVal / ferramentaSpeed[ferramenta] * 1.5 )
							table.insert( cells, '|' .. getChoice( willDrop, lang:formatNum( breakTime ) ) )
						end
					end
				end
			end
		end
		
		if not horizontal then
			cells = table.concat( cells, '\n' )
		end
		table.insert( tableParts, cells )
	end
	
	if horizontal then
		local blocksArgs = {}
		for _, arg in ipairs{ 1, 'nota', 'sprite', 'link', 'item', 'drop', 2, 3, 'tesoura', 'espada' } do
			if args[arg] then
				local col = 0
				for colVal in mw.text.gsplit( args[arg], '%s*;%s*' ) do
					col = col + 1
					if colVal ~= '' then
						if not blocksArgs[col] then
							blocksArgs[col] = {}
						end
						
						blocksArgs[col][arg] = colVal
					end
				end
			end
		end
		for _, block in ipairs( blocksArgs ) do
			insertBlock( block )
		end
		
		local columns = #tableParts
		for row = 1, #tableParts[1] do
			local cells = {}
			for col = 1, columns do
				table.insert( cells, tableParts[col][row] )
			end
			table.insert( rows, table.concat( cells, '\n' ) )
		end
		
		-- Insira cabeçalho de tempo de ruptura após a linha de blocos quando simples, ou depois da ferramenta ou linha de dureza não quando
		table.insert( rows, simple and 3 or showFerramenta and 5 or 4, '! colspan="' .. columns + 1 .. '" |' .. getBreakingTimeHeader( f ) )
	else
		insertBlock( args )
		for _, row in ipairs( tableParts ) do
			table.insert( rows, row )
		end
	end
	table.insert( rows, '' )
	
	local note = ''
	if args.foot or horizontal then
		note = f:preprocess( '<references group="nota"/>' )
		
		if args.foot == '2' then
			table.insert( rows, header or getDplVar( 'header' ) )
		end
		table.insert( rows, '|}' )
		
		if not horizontal then
			f:callParserFunction( '#dplvar:set',
				'breaking header', '',
				'breaking hideferramenta', '',
				'breaking hidetesoura', '',
				'breaking hideespada', '',
				'breaking simple', '',
				'breaking sortable', ''
			)
		end
	elseif #dplVars > 0 then
		f:callParserFunction( '#dplvar:set', dplVars )
	end
	
	return table.concat( rows, '\n|-\n' ) .. note .. table.concat( categories )
end
return p
Advertisement