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="FN" name="breakingtimenote">Os tempos são para ferramentas sem encantamento em segundos. Para mais informações, veja [[Quebrar#Velocidade|Quebrar § Velocidade]].</ref>' )
    end
    return breakingTimeHeader
end

function p.row( f )
	lang = mw.getContentLanguage() 
	local args = require( [[Módulo:ProcessArgs]] ).norm()
	local sprite = require( [[Módulo:Sprite]] )
	
	local function getDplVar( var )
		local val = f:callParserFunction( '#dplvar', 'breaking ' .. var )
		if val == '' then
			val = false
		end
		return val
	end
	local dplVars = {}
	local function setDplVar( 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' )
			breakingTimeHeader = 'Tempo  de [[Quebrar|quebra]]'
		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', 'Netherita', 'Ouro' }
		end
		table.insert(
			ferramentaColumns, 1, 'Padrão')
		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
		
		local ferramentaSprites = {
			Madeira = { 'BlockSprite', 'tábuas' },
			Pedra = { 'BlockSprite', 'pedregulho' },
			Ferro = { 'ItemSprite', 'barra-de-ferro' },
			Diamante = { 'ItemSprite', 'diamante' },
			Netherita = { 'ItemSprite', 'barra-de-netherita' },
			Ouro = { 'ItemSprite', 'barra-de-ouro' },
			Tesoura = { 'ItemSprite', 'tesoura' },
			Espada = { 'ItemSprite', 'espada-de-madeira' }
		}
		for _, ferramenta in ipairs( ferramentaColumns ) do
			if ferramentaSprites[ferramenta] then
				local image, spriteCat = sprite.sprite{
					data = ferramentaSprites[ferramenta][1],
					ferramentaSprites[ferramenta][2],
					text = ferramenta
				}
				table.insert( header, '! style="text-align:left" ' .. sortType .. ' | ' .. image )
				table.insert( categories, spriteCat )
			else
				table.insert( header, '! ' .. sortType .. ' | ' .. ferramenta )
			end
		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 dureza = require( [[Módulo:Block value]] ).value
	
	local function fillCells( 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,
		Netherita = 5,
		Nenhum = 6
	}
	local materialSpeed = {
		Nenhum = 1,
		Qualquer = 1,
		Madeira = 2,
		Pedra = 4,
		Ferro = 6,
		Diamante = 8,
		Netherita = 9,
		Ouro = 12
	}
	local numberMaterials = 6
	
	local function insertBlock( 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 = '?'
			local title = mw.title.getCurrentTitle()
			if title.namespace == 0 and not title.isSubpage then
				table.insert(categories, '[[Categoria:Dureza ausente]]')
			end
		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 .. ' ', '' ):gsub("^%l", string.upper)
			elseif args.textTrimSuffix then
				blockText = block:gsub( args.textTrimSuffix .. '$', '' )
			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 isMaterialSpecified = (material ~= 'Qualquer') and (material ~= 'Nenhum')
					local ferramentaName =  ferramenta .. ( isMaterialSpecified and ' de ' .. mw.ustring.lower(material) or '' )
					local fullFerramentaName =  ferramentaName .. ( (not isMaterialSpecified) and ' de madeira' or '' )
					local image, spriteCat = sprite.sprite{
						data = 'ItemSprite',
						fullFerramentaName,
						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 function getChoice( choice, text )
			if not choices[choice] then
				choices[choice] = f:expandTemplate{ title = 'Tabela de Escolha', args = { choice, '' } }
			end
			return choices[choice] .. text
		end
		
		local function processTime( num )
			-- o número passado foi multiplicado por 100
			if num <= 5 then		-- Os blocos têm um tempo mínimo de quebra de 1 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 / 5 ) / 20
			end
			return num
		end
		
		if durezaVal == '?' then
			fillCells( cells, '|?', numberMaterials + 1 )
		else
			if unbreakable then
				table.insert( cells, '| style=" text-align: center " ' .. ( sortable and 'data-sort-value="999" ' or '' ) .. getChoice( 'não', '∞' ) )
				if showFerramenta then
					fillCells( cells, '|—', numberMaterials )
				end
			else
				local drop = 'sim'
				local forceDrop = false
				if blockArgs.drop == '0' then
					drop = 'parcial'
				elseif blockArgs.drop == '1' then
					forceDrop = 'sim'
				end
				local requiredLevel = unbreakable and 999 or materialGrade[material]
				local function insertMaterialCell( material )
					local shouldDrop = drop
					if materialGrade[material] < requiredLevel then
						shouldDrop = 'não'
					end
					-- evitar a perda de precisão do número flutuante, multiplique por 100 e divida na função processTime
					local breakTime = processTime( durezaVal * ( shouldDrop == 'não' and 500 or 150) / materialSpeed[material] )
					table.insert( cells, '| style=" text-align: center " ' .. getChoice( forceDrop or shouldDrop, lang:formatNum( breakTime ) ) )
				end
				
				if not showFerramenta or ferramenta == 'Qualquer' or ferramenta == 'Nenhum' then
					insertMaterialCell( 'Qualquer' )
					if showFerramenta then
						fillCells( cells, '|—', numberMaterials )
					end
				else
					for _, material in ipairs{ 'Qualquer', 'Madeira', 'Pedra', 'Ferro', 'Diamante', 'Netherita', 'Ouro' } do
						insertMaterialCell( material )
					end
				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
				elseif blocks[1] =='Bambu' then
					ferramentaSpeed.Espada = 5000
				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
						-- evitar a perda de precisão do número flutuante, multiplique por 100 e divida na função processTime
						local breakTime = processTime( durezaVal * 150 / ferramentaSpeed[ferramenta] )
						table.insert( cells, '| style=" text-align: center "  ' .. getChoice( willDrop, lang:formatNum( breakTime ) ) )
					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
		
		-- Insire 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="FN"/>' )
		
		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