Bone Overlord.gif The Roost of the Graveborn Quest Spoiler!
Domine Graveborn: todos os bosses e mecânicas ilustradas!
Saiba mais ➔
Winter Tree.png Winter Update 2025
Acompanhe tudo sobre o Winter Update 2025!
Saiba mais ➔
Stag.gif The Order of the Stag Quest Spoiler!
Conheça Isle of Ada: sua quest, missões secundárias e todos os bosses!
Saiba mais ➔

MediaWiki:Outfitter.js: mudanças entre as edições

De Tibia Wiki - A Enciclopédia do Tibia
Ir para navegação Ir para pesquisar
Master Player
Master Player (discussão | contribs) (Adicionando suporte aos addons invertidos)
 
(23 revisões intermediárias pelo mesmo usuário não estão sendo mostradas)
Linha 1: Linha 1:
/* Elementos */
if (typeof mw !== 'undefined') {
const $carregando_outfiter = $('#carregando_outfiter')
  mw.loader.load('/index.php?title=MediaWiki:OutfitterDados.js&action=raw&ctype=text/javascript')
const $carregando = $('.carregando')
  mw.loader.load('/index.php?title=MediaWiki:UPNG.js&action=raw&ctype=text/javascript')
const $tela = $('#tela')
  mw.loader.load('/index.php?title=MediaWiki:omggif.js&action=raw&ctype=text/javascript')
const $tela_cor = $('#tela_cor')
const $montagem = $('#montagem')
const $img_letras = $('#img_letras')
const $outfit_nome = $('.outfit_nome')
const $montaria_nome = $('.montaria_nome')
const $form_outfits = $('#form_outfits')
const $form_montarias = $('#form_montarias')
const $form_outros = $('#form_outros')
const $link = $('.link')
const $sexo = $('#sexo')
const $addon_1 = $('#addon_1')
const $addon_2 = $('#addon_2')
const $aa = $('#aa')
const $animar = $('#animar')
const $quadros = $('#quadros')
const $vida = $('#vida')
const $nome_char = $('.nome_char')
const $quadros_area = $('.quadros')
const $quadro = $('.quadro')
const $tick = $('#tick')


let loop
  if ($('.outfitter').length > 0) {
const c = $tela[0]
    mw.loader.load('/index.php?title=MediaWiki:Outfitter.css&action=raw&ctype=text/css', 'text/css')
const ctx = c.getContext('2d')
    $.ajax({ url: '/index.php?title=MediaWiki:Outfitter.html&action=raw', success: ready })
const ccor = $tela_cor[0]
  } else { ready() }
const cctx = ccor.getContext('2d')
}
const cm = $montagem[0]
const mctx = cm.getContext('2d')
const img_letras = $img_letras[0]
const letras = {" ": [0, 0, 4], A: [1, 0, 9], B: [2, 0, 8], C: [3, 0, 8], D: [4, 0, 9], E: [5, 0, 8], F: [6, 0, 8], G: [7, 0, 9], H: [8, 0, 9], I: [9, 0, 6], J: [10, 0, 7], K: [11, 0, 8], L: [12, 0, 8], M: [13, 0, 10], N: [14, 0, 9], O: [15, 0, 9], P: [16, 0, 8], Q: [17, 0, 9], R: [18, 0, 9], S: [19, 0, 8], T: [20, 0, 10], U: [21, 0, 9], V: [22, 0, 8], W: [23, 0, 10], X: [24, 0, 8], Y: [25, 0, 8], Z: [26, 0, 8], "'": [0, 1, 4], a: [1, 1, 8], b: [2, 1, 8], c: [3, 1, 7], d: [4, 1, 8], e: [5, 1, 8], f: [6, 1, 7], g: [7, 1, 8], h: [8, 1, 8], i: [9, 1, 4], j: [10, 1, 6], k: [11, 1, 8], l: [12, 1, 4], m: [13, 1, 10], n: [14, 1, 8], o: [15, 1, 8], p: [16, 1, 8], q: [17, 1, 8], r: [18, 1, 7], s: [19, 1, 7], t: [20, 1, 7], u: [21, 1, 8], v: [22, 1, 8], w: [23, 1, 10], x: [24, 1, 8], y: [25, 1, 8], z: [26, 1, 7], "0": [0, 2, 9], "1": [1, 2, 8], "2": [2, 2, 8], "3": [3, 2, 9], "4": [4, 2, 8], "5": [5, 2, 8], "6": [6, 2, 9], "7": [7, 2, 9], "8": [8, 2, 8], "9": [9, 2, 8], "-": [10, 2, 9], ",": [11, 2, 4]};
const cores = [[255, 255, 255], [255, 212, 191], [255, 233, 191], [255, 255, 191], [233, 255, 191], [212, 255, 191], [191, 255, 191], [191, 255, 212], [191, 255, 233], [191, 255, 255], [191, 233, 255], [191, 212, 255], [191, 191, 255], [212, 191, 255], [233, 191, 255], [255, 191, 255], [255, 191, 233], [255, 191, 212], [255, 191, 191], [218, 218, 218], [191, 159, 143], [191, 175, 143], [191, 191, 143], [175, 191, 143], [159, 191, 143], [143, 191, 143], [143, 191, 159], [143, 191, 175], [143, 191, 191], [143, 175, 191], [143, 159, 191], [143, 143, 191], [159, 143, 191], [175, 143, 191], [191, 143, 191], [191, 143, 175], [191, 143, 159], [191, 143, 143], [182, 182, 181], [191, 127, 95], [191, 159, 95], [191, 191, 95], [159, 191, 95], [127, 191, 95], [95, 191, 95], [95, 191, 127], [95, 191, 159], [95, 191, 191], [95, 159, 191], [95, 127, 191], [95, 95, 191], [127, 95, 191], [159, 95, 191], [191, 95, 191], [191, 95, 159], [191, 95, 127], [191, 95, 95], [145, 145, 144], [191, 106, 63], [191, 148, 63], [191, 191, 63], [148, 191, 63], [106, 191, 63], [63, 191, 63], [63, 191, 106], [63, 191, 148], [63, 191, 191], [63, 148, 191], [63, 106, 191], [63, 63, 191], [106, 63, 191], [148, 63, 191], [191, 63, 191], [191, 63, 148], [191, 63, 106], [191, 63, 63], [109, 109, 109], [255, 85, 0], [255, 170, 0], [255, 255, 0], [170, 255, 0], [84, 255, 0], [0, 255, 0], [0, 255, 84], [0, 255, 170], [0, 255, 255], [0, 169, 255], [0, 85, 255], [0, 0, 255], [85, 0, 255], [169, 0, 255], [254, 0, 255], [255, 0, 170], [255, 0, 85], [255, 0, 0], [72, 72, 68], [191, 63, 0], [191, 127, 0], [191, 191, 0], [127, 191, 0], [63, 191, 0], [0, 191, 0], [0, 191, 63], [0, 191, 127], [0, 191, 191], [0, 127, 191], [0, 63, 191], [0, 0, 191], [63, 0, 191], [127, 0, 191], [191, 0, 191], [191, 0, 127], [191, 0, 63], [191, 0, 0], [36, 36, 36], [127, 42, 0], [127, 85, 0], [127, 127, 0], [85, 127, 0], [42, 127, 0], [0, 127, 0], [0, 127, 42], [0, 127, 85], [0, 127, 127], [0, 84, 127], [0, 42, 127], [0, 0, 127], [42, 0, 127], [84, 0, 127], [127, 0, 127], [127, 0, 85], [127, 0, 42], [127, 0, 0]];


function sortFn (a, b) {
function ready(res) {
   if (a.type !== b.type) return (a.type === 'outfit' ? -1 : 1)
   if (typeof dados !== 'undefined' && typeof UPNG !== 'undefined' && typeof GifWriter !== 'undefined' ) {
  if (isNaN(a.name) && isNaN(b.name)) return a.name.localeCompare(b.name)
    if (res) { $('.outfitter').html(res.slice(5, -6)) }
  if (!isNaN(a.name) && !isNaN(b.name)) return a.id - b.id
    const outputs = []
   if (!isNaN(a.name)) return 1
    $('.outfitter_output').each(function() {
  return -1
      if ($(this).is('img')) { outputs.push($(this)) }
      else { outputs.push($(document.createElement('img')).attr('data-src', $(this).attr('data-src')).appendTo($(this))) }
    })
    outfitter(outputs)
  }
   else setTimeout(() => ready(res), 300)
}
}


const outfits = dados.filter(o => o.type !== 'mount').sort(sortFn)
function outfitter(outputs) {
const montarias = [{ id: 0, name: 'Nenhuma', idle: [0], move: [0] }].concat(dados.filter(o => o.type === 'mount').sort(sortFn))
  /* Elementos */
  const com_tela = $('.outfitter').length > 0
  const $form_outfits = $('#form_outfits')
  const $form_montarias = $('#form_montarias')
  const $form_outros = $('#form_outros')
  const $addon_1 = $('#addon_1')
  const $addon_2 = $('#addon_2')
  const $aa = $('#aa')
  const $animar = $('#animar')
  const $quadros = $('#quadros')
  const $vida = $('#vida')
  const $nome_char = $('.nome_char')
  const $tick = $('#tick')


const opc = {
  const ctx = document.createElement('canvas').getContext('2d')
   outfit: outfits.find(d => d.id === 128),
   const cctx = document.createElement('canvas').getContext('2d')
   montaria: montarias.find(d => d.id === 0),
   const mctx = document.createElement('canvas').getContext('2d')
   sexo: 'Male',
   const gctx = document.createElement('canvas').getContext('2d')
  lado: 2,
 
   addon1: false,
   const img_outfit = $(document.createElement('img'))[0]
   addon2: false,
   const img_mount = $(document.createElement('img'))[0]
   h: 78,
   const img_letras = $(document.createElement('img'))[0]
  p: 69,
  s: 58,
  d: 76,
  aa: false,
  animar: false,
  quadros: false,
  hp: false,
  nome: '',
  largura: 64,
  largura_nome: 0,
}


let imgs_carregando = -1
  let imgs_carregando = -1
const img_out = $(document.createElement('img'))[0]
  img_outfit.onload = carregado
const img_mount = $(document.createElement('img'))[0]
  img_mount.onload = carregado
img_out.onload = function () { carregado() }
  img_letras.onload = carregado
img_mount.onload = function () { carregado() }


/* Funções */
  const outfits = [{ id: -1, name: 'Nenhum', idle: [0], move: [0], type: 'other', sits: true, colors: false }].concat(dados.filter(o => o.type !== 'mount').sort(sortFn))
  const montarias = [{ id: 0, name: 'Nenhuma', idle: [0], move: [0], type: 'mount', sits: false, colors: false }].concat(dados.filter(o => o.type === 'mount').sort(sortFn))
  function sortFn (a, b) {
    if (a.type !== b.type) return (a.type === 'outfit' ? -1 : 1)
    if (isNaN(a.name) && isNaN(b.name)) return a.name.localeCompare(b.name)
    if (!isNaN(a.name) && !isNaN(b.name)) return a.id - b.id
    if (!isNaN(a.name)) return 1
    return -1
  }


function atualizar () {
   let $output
  clearInterval(loop)
   const opc = { }
   $carregando.show()
 
   ctx.clearRect(0, 0, 128, 128)
   /* Init */
  excecoes()
   preencherNomeSetas()
  buscarArquivo()
  gerarlink()
}


function carregado () {
  if (com_tela) {
  imgs_carregando--
    if (typeof mw !== 'undefined' && mw.user?.getGroups) { mw.user.getGroups().then((user_groups) => $('#botao_up').toggle(user_groups.includes('bureaucrat') || user_groups.includes('sysop'))) }
  if (imgs_carregando === 0) {
     preencherListas()
    $carregando.hide()
     tabelaCores()
    dimensionar()
     artworks()
     montar()
     imprimir()
     quadros()
   }
   }
}
  processar()
 
  /* Funções */


function excecoes () {
  function processar() {
  if (opc.outfit.addons === 0) {
    if (outputs.length > 0) {
    opc.addon1 = false
      $output = outputs.pop()
    opc.addon2 = false
      obterParametros()
    $addon_1.prop('checked', false)
      atualizar()
    $addon_2.prop('checked', false)
    }
   }
   }
   if (opc.outfit.move.length === 0) {
 
     opc.animar = false
   function atualizar () {
     $animar.prop('checked', false)
    validarExcecoes()
    if (com_tela) {
      sincronizarTela()
      gerarLink()
     }
     $output.attr('src', '/wiki/Especial:Redirecionar/file/Outfiter_Carregando.gif')
    buscarArquivo()
   }
   }
   if (opc.outfit.idle.length === 0) {
 
    opc.animar = true
   function carregado () {
    $animar.prop('checked', true)
    imgs_carregando--
    if (imgs_carregando <= 0) {
      dimensionar()
      montar()
      imprimir()
      processar()
    }
   }
   }
   if (!opc.outfit.sits) {
 
    opc.montaria = montarias[0]
   function validarExcecoes () {
     $('input[value=' + opc.montaria.id + ']').prop('checked', true)
    if (opc.outfit.addons === 0) {
      opc.addon1 = false
      opc.addon2 = false
    }
    if (opc.outfit.move.length === 0) {
      opc.animar = false
    }
    if (opc.outfit.idle.length === 0) {
      opc.animar = true
    }
    if (!opc.outfit.sits) {
      opc.montaria = montarias[0]
     }
    if (opc.outfit.id === 9999) {
      opc.lado = 0
 
    }
   }
   }
  $addon_1.prop('disabled', opc.outfit.addons === 0)
  $addon_2.prop('disabled', opc.outfit.addons === 0)
  $animar.prop('disabled', opc.outfit.idle.length === 0 || opc.outfit.move.length === 0)
  $('input[name="montaria"]').not('[id="m0"]').parent().toggle(opc.outfit.sits)
  $('#tabela_cores_container').toggle(opc.outfit.colors)
}


function preencherNomeSetas () {
  function sincronizarTela () {
  $outfit_nome.text(opc.outfit.name)
    $('.outfit_nome').text(opc.outfit.name)
  $montaria_nome.text(opc.montaria.name)
    $('.montaria_nome').text(opc.montaria.name)
 
    $('input[value="' + opc.outfit.id + '"]').prop('checked', true)
    $('input[value="' + opc.montaria.id + '"]').prop('checked', true)


  $('input[value="' + opc.outfit.id + '"]').parent()[0].scrollIntoView()
    $nome_char.val(opc.nome)
  $('input[value="' + opc.montaria.id + '"]').parent()[0].scrollIntoView()
    $addon_1.prop('checked', opc.addon1)
}
    $addon_2.prop('checked', opc.addon2)
    $animar.prop('checked', opc.animar)
    $vida.prop('checked', opc.vida)
    $aa.prop('checked', opc.aa)
    $quadros.prop('checked', opc.quadros)


function buscarArquivo () {
    $addon_1.prop('disabled', opc.outfit.addons === 0)
  imgs_carregando = opc.montaria.id ? 2 : 1
    $addon_2.prop('disabled', opc.outfit.addons === 0)
  if (opc.montaria.id) {
     $animar.prop('disabled', opc.outfit.idle.length === 0 || opc.outfit.move.length === 0)
     img_mount.setAttribute('src', assetsBaseURL + 'Outfitter_' + opc.montaria.id + '.png')
    $('input[name="montaria"]').not('[id="m0"]').parent().toggle(opc.outfit.sits)
    $('#tabela_cores_container').toggle(opc.outfit.colors || opc.montaria.colors)
   }
   }
  // if (semGenero.indexOf(outfits[opc.outfit]) != -1) {
  //  opc.sexo = "Nenhum";
  //    $('.botao_sexo').hide();
  // } else if (opc.sexo == "Nenhum") {
  //  opc.sexo = "Male";
  // $('.botao_sexo').show();
  // }


   img_out.setAttribute('src', assetsBaseURL + 'Outfitter_' + opc.outfit.id + '.png')
   function buscarArquivo () {
}
    imgs_carregando = 1 + (opc.montaria.id > 0 ? 1 : 0) + (opc.outfit.id > 0 ? 1 : 0)
    if (imgs_carregando) {
      if (opc.montaria.id > 0) {
        img_mount.setAttribute('src', assetsBaseURL + 'Outfitter_' + opc.montaria.id + '.png')
      }
      if (opc.outfit.id > 0) {
        img_outfit.setAttribute('src', opc.outfit.id !== 9999 ? assetsBaseURL + 'Outfitter_' + opc.outfit.id + '.png' : gctx.canvas.toDataURL())
      }
      img_letras.setAttribute('src', assetsBaseURL + 'Outfitter_Letras.png')
    } else carregado()
  }


function mdc (a, b) { return a ? mdc(b % a, a) : b }
  function mdc (a, b) { return a ? mdc(b % a, a) : b }


function normalize (array, sum) {
  function normalize (array, sum) {
  if (array.length === 1 && array[0] === 0) return [0]
    if (array.length === 1 && array[0] === 0) return [0]
  let array_sum = array.reduce((s, a) => s + a, 0)
    let array_sum = array.reduce((s, a) => s + a, 0)
  return array.map(i => Math.round(i / array_sum * sum))
    return array.map(i => Math.round(i / array_sum * sum))
}
  }


function round (array, value) {
  function round (array, value) {
  return array.map(i => Math.round(Math.round(i / value) * value))
    return array.map(i => Math.round(Math.round(i / value) * value))
}
  }


function sync_frames (out_frames, mount_frames) {
  function sync_frames (out_frames, mount_frames) {
  if (out_frames.length <= 1 && mount_frames.length <= 1) return { frames: 1, tick: 0 }
    if (out_frames.length <= 1 && mount_frames.length <= 1) return { frames: 1, tick: 0 }


  let out_sum = out_frames.reduce((s, a) => s + a, 0)
    let out_sum = out_frames.reduce((s, a) => s + a, 0)
  let mount_sum = mount_frames.reduce((s, a) => s + a, 0)
    let mount_sum = mount_frames.reduce((s, a) => s + a, 0)
  const tick = out_frames.concat(mount_frames).filter(t => t !== 0).reduce(mdc)
    const tick = out_frames.concat(mount_frames).filter(t => t !== 0).reduce(mdc)
  return { frames: Math.max(out_sum / tick, mount_sum / tick), tick }
    return { frames: Math.max(out_sum / tick, mount_sum / tick), tick }
}
  }


function montar () {
  function montar () {
  const quadros_outfit = [opc.outfit.idle, normalize(opc.outfit.move, 800)]
    const quadros_outfit = [opc.outfit.idle, normalize(opc.outfit.move, 800)]
  const quadros_mount = [round(opc.montaria.idle, 100), normalize(opc.montaria.move, 800)]
    const quadros_mount = [round(opc.montaria.idle, 100), normalize(opc.montaria.move, 800)]
  let add = (opc.outfit.sits ? 2 : 1) * (1 + opc.outfit.addons)
    let add = (opc.outfit.sits ? 2 : 1) * (1 + opc.outfit.addons)
  let offset = (opc.montaria.id !== 0 ? 1 + opc.outfit.addons : 0)
    let offset = (opc.montaria.id !== 0 ? 1 + opc.outfit.addons : 0)
  opc.box = { min_x: 63, min_y: 63, max_x: 0, max_y: 0 }
    opc.box = { min_x: 32, min_y: 32, max_x: 32, max_y: 32 }
  opc.sync = [sync_frames(quadros_outfit[0], quadros_mount[0]), sync_frames(quadros_outfit[1], quadros_mount[1])]
    opc.sync = [sync_frames(quadros_outfit[0], quadros_mount[0]), sync_frames(quadros_outfit[1], quadros_mount[1])]
  mctx.clearRect(0, 0, 2560, 128)
    const mCanvasWidth = Math.max(opc.sync[0].frames, opc.sync[1].frames) * opc.sw
  for (let m = 0; m <= 1; m++) {
    $(mctx.canvas).attr('width', mCanvasWidth) .attr('height', 128)
    for (let j = 0, jm = 0, jo = 0, mount_timer = 0, out_timer = 0; j < opc.sync[m].frames; j++) {
    mctx.clearRect(0, 0, mCanvasWidth, 128)
      if (opc.montaria.id !== 0) {
    for (let m = 0; m <= 1; m++) {
        mctx.drawImage(img_mount, opc.lado * 64, 64 * (jm + m * quadros_mount[0].length), 64, 64, 64 * j, m * 64, 64, 64)
      for (let j = 0, jm = 0, jo = 0, mount_timer = 0, out_timer = 0; j < opc.sync[m].frames; j++) {
        mount_timer += opc.sync[m].tick
        if (opc.montaria.id !== 0) {
        if (mount_timer >= quadros_mount[m][jm]) {
          mctx.drawImage(img_mount, opc.lado * (opc.montaria.colors ? 2 : 1) * opc.sw, opc.sh * (jm + m * quadros_mount[0].length), opc.sw, opc.sh, opc.sw * j, m * opc.sh, opc.sw, opc.sh)
          mount_timer -= quadros_mount[m][jm]
          if (opc.montaria.colors) {
          jm++
            cctx.globalCompositeOperation = 'copy'
          if (jm >= quadros_mount[m].length) jm = 0
            cctx.drawImage(img_mount, opc.lado * (opc.montaria.colors ? 2 : 1) * opc.sw + opc.sw, opc.sh * (jm + m * quadros_mount[0].length), opc.sw, opc.sh, 0, 0, opc.sw, opc.sh)
            colorir(j, m)
          }
          mount_timer += opc.sync[m].tick
          if (mount_timer >= quadros_mount[m][jm]) {
            mount_timer -= quadros_mount[m][jm]
            jm++
            if (jm >= quadros_mount[m].length) jm = 0
          }
         }
         }
        if (opc.outfit.id > 0) {
          const yo = (jo + m * quadros_outfit[0].length) * add + offset
          out_timer += opc.sync[m].tick
          if (out_timer >= quadros_outfit[m][jo]) {
            out_timer -= quadros_outfit[m][jo]
            jo++
            if (jo >= quadros_outfit[m].length) jo = 0
          }
          mctx.drawImage(img_outfit, opc.lado * (opc.outfit.colors ? 2 : 1) * opc.sw, opc.sh * yo, opc.sw, opc.sh, opc.sw * j, m * opc.sh, opc.sw, opc.sh)
          if (opc.outfit.colors) {
            cctx.globalCompositeOperation = 'copy'
            cctx.drawImage(img_outfit, opc.lado * (opc.outfit.colors ? 2 : 1) * opc.sw + opc.sw, opc.sh * yo, opc.sw, opc.sh, 0, 0, opc.sw, opc.sh)
            colorir(j, m)
          }
          const reverse = opc.outfit.reverse && opc.outfit.reverse[opc.lado]
          if (!reverse) {
            if (opc.addon1) addon(opc.lado, 1, 0, yo, m, j)
            if (opc.addon2) addon(opc.lado, 2, 0, yo, m, j)
          } else {
            if (opc.addon2) addon(opc.lado, 2, 0, yo, m, j)
            if (opc.addon1) addon(opc.lado, 1, 0, yo, m, j)
          }
        }
        if ((opc.animar && m === 1) || ( !opc.animar && m === 0)) medir(j, m)
       }
       }
      const yo = (jo + m * quadros_outfit[0].length) * add + offset
    }
       out_timer += opc.sync[m].tick
  }
       if (out_timer >= quadros_outfit[m][jo]) {
 
         out_timer -= quadros_outfit[m][jo]
  function colorir (j, m) {
         jo++
    const imgData = mctx.getImageData(opc.sw * j, opc.sh * m, opc.sw, opc.sh)
        if (jo >= quadros_outfit[m].length) jo = 0
    let corData = cctx.getImageData(0, 0, opc.sw, opc.sh)
    for (let i = 0; i < corData.data.length; i += 4) {
      const r = corData.data[i]
      const g = corData.data[i + 1]
      const b = corData.data[i + 2]
       if ((r >= 240) && (g >= 240) && (b <= 15)) {
        corData.data[i] = cores[opc.h][0]
        corData.data[i + 1] = cores[opc.h][1]
        corData.data[i + 2] = cores[opc.h][2]
       } else if ((r >= 240) && (g <= 15) && (b <= 15)) {
        corData.data[i] = cores[opc.p][0]
        corData.data[i + 1] = cores[opc.p][1]
        corData.data[i + 2] = cores[opc.p][2]
      } else if ((r <= 15) && (g >= 240) && (b <= 15)) {
         corData.data[i] = cores[opc.s][0]
         corData.data[i + 1] = cores[opc.s][1]
        corData.data[i + 2] = cores[opc.s][2]
      } else if ((r <= 15) && (g <= 15) && (b >= 240)) {
        corData.data[i] = cores[opc.d][0]
        corData.data[i + 1] = cores[opc.d][1]
        corData.data[i + 2] = cores[opc.d][2]
       }
       }
      mctx.drawImage(img_out, opc.lado * (opc.outfit.colors ? 128 : 64), 64 * yo, 64, 64, 64 * j, m * 64, 64, 64)
    }
       if (opc.outfit.colors) {
    for (let i = 0; i < imgData.data.length; i += 4) {
         cctx.globalCompositeOperation = 'copy'
       if (corData.data[i + 3] !== 0) {
         cctx.drawImage(img_out, opc.lado * (opc.outfit.colors ? 128 : 64) + 64, 64 * yo, 64, 64, 0, 0, 64, 64)
         imgData.data[i] = imgData.data[i] * corData.data[i] / 255
        colorir(j, m, true)
         imgData.data[i + 1] = imgData.data[i + 1] * corData.data[i + 1] / 255
        imgData.data[i + 2] = imgData.data[i + 2] * corData.data[i + 2] / 255
       }
       }
      if (opc.addon1) addon(opc.lado, 1, 0, yo, m, j)
      if (opc.addon2) addon(opc.lado, 2, 0, yo, m, j)
      medir(j, m)
     }
     }
    mctx.putImageData(imgData, opc.sw * j, opc.sh * m, 0, 0, opc.sw, opc.sh)
   }
   }
}


function colorir (j, m) {
  function addon (lado, n, x, y, m, j) {
  const imgData = mctx.getImageData(64 * j, 64 * m, 64, 64)
     mctx.globalCompositeOperation = 'source-over'
  let corData = cctx.getImageData(0, 0, 64, 64)
     mctx.drawImage(img_outfit, lado * (opc.outfit.colors ? 2 : 1) * opc.sw, opc.sh * y + opc.sh * n, opc.sw, opc.sh, opc.sw * j, opc.sh * m, opc.sw, opc.sh)
  for (let i = 0; i < corData.data.length; i += 4) {
     cctx.drawImage(img_outfit, lado * (opc.outfit.colors ? 2 : 1) * opc.sw + opc.sw, opc.sh * y + opc.sh * n, opc.sw, opc.sh, 0, 0, opc.sw, opc.sh)
     const r = corData.data[i]
    if (opc.outfit.colors) colorir(j, m)
    const g = corData.data[i + 1]
     const b = corData.data[i + 2]
    if ((r >= 240) && (g >= 240) && (b <= 15)) {
      corData.data[i] = cores[opc.h][0]
      corData.data[i + 1] = cores[opc.h][1]
      corData.data[i + 2] = cores[opc.h][2]
    } else if ((r >= 240) && (g <= 15) && (b <= 15)) {
      corData.data[i] = cores[opc.p][0]
      corData.data[i + 1] = cores[opc.p][1]
      corData.data[i + 2] = cores[opc.p][2]
     } else if ((r <= 15) && (g >= 240) && (b <= 15)) {
      corData.data[i] = cores[opc.s][0]
      corData.data[i + 1] = cores[opc.s][1]
      corData.data[i + 2] = cores[opc.s][2]
    } else if ((r <= 15) && (g <= 15) && (b >= 240)) {
      corData.data[i] = cores[opc.d][0]
      corData.data[i + 1] = cores[opc.d][1]
      corData.data[i + 2] = cores[opc.d][2]
    }
   }
   }
   for (let i = 0; i < imgData.data.length; i += 4) {
 
     if (corData.data[i + 3] !== 0) {
   function dimensionar () {
       imgData.data[i] = imgData.data[i] * corData.data[i] / 255
    opc.largura = Math.max(64, opc.sw)
      imgData.data[i + 1] = imgData.data[i + 1] * corData.data[i + 1] / 255
     if (opc.nome !== '') {
       imgData.data[i + 2] = imgData.data[i + 2] * corData.data[i + 2] / 255
       opc.largura_nome = 0
      for (let x = 0; x < opc.nome.length; x++) {
        const letra = letras[opc.nome.charCodeAt(x)]
        opc.largura_nome += letra ? letra[0] - (letra[1] || 0) - (letra[2] || 0) : 3
      }
       while (opc.largura_nome + (opc.largura_nome % 2 ? 4 : 3) > opc.largura) {
        opc.largura += 8
      }
     }
     }
    $(ctx.canvas).attr('width', opc.largura)
   }
   }
  mctx.putImageData(imgData, 64 * j, 64 * m, 0, 0, 64, 64)
}


function addon (lado, n, x, y, m, j) {
  function medir (j, m) {
  mctx.globalCompositeOperation = 'source-over'
    const imgData = mctx.getImageData(opc.sw * j, opc.sh * m, opc.sw, opc.sh)
  mctx.drawImage(img_out, lado * 128, 64 * y + 64 * n, 64, 64, 64 * j, 64 * m, 64, 64)
    for (let i = 0; i < imgData.data.length; i += 4) {
  cctx.drawImage(img_out, lado * 128 + 64, 64 * y + 64 * n, 64, 64, 0, 0, 64, 64)
      if (imgData.data[i + 3] !== 0) {
  colorir(j, m)
        const x = (i / 4) % opc.sw
}
        const y = Math.floor((i / 4) / opc.sw)
 
        if (x < opc.box.min_x) opc.box.min_x = x
function dimensionar () {
        if (x > opc.box.max_x) opc.box.max_x = x
  opc.largura = 64
        if (y < opc.box.min_y) opc.box.min_y = y
  if (opc.nome !== '') {
        if (y > opc.box.max_y) opc.box.max_y = y
    opc.largura_nome = 3
      }
    for (let x = 0; x < opc.nome.length; x++) {
      const letra = letras[opc.nome[x]]
      opc.largura_nome += letra ? letra[2] - 1 : 3
    }
    while (opc.largura_nome > opc.largura) {
      opc.largura += 8
     }
     }
    opc.box.w = opc.box.max_x - opc.box.min_x + 1
    opc.box.h = opc.box.max_y - opc.box.min_y + 1
    opc.box.cx = Math.floor((opc.largura - opc.box.w) / 2)
    const fullH = Math.min(opc.altura, opc.box.h + (opc.nome ? 14 : (opc.hp ? 8 : 0)))
    opc.box.cy = Math.ceil((opc.altura - fullH) / 2) + fullH - opc.box.h
   }
   }
  $tela.attr('width', opc.largura * 2)
  $tela.attr('width', opc.largura * 2)
}


function medir (j, m) {
  function desenharNomeHP (ctx) {
  const imgData = mctx.getImageData(64 * j, 64 * m, 64, 64)
    ctx.globalCompositeOperation = 'source-over'
  for (let i = 0; i < imgData.data.length; i += 4) {
    let j = opc.box.cy - 6 - (opc.hp ? 4 : 0)
     if (imgData.data[i + 3] !== 0) {
    if (j < 9) j = 9
       const x = (i / 4) % 64
     if (opc.hp) {
       const y = Math.floor((i / 4) / 64)
       ctx.fillStyle = '#000000'
      if (x < opc.box.min_x) opc.box.min_x = x
      ctx.fillRect((opc.largura - 28) / 2, j + 5, 28, 4)
       if (x > opc.box.max_x) opc.box.max_x = x
       ctx.fillStyle = '#00B800'
      if (y < opc.box.min_y) opc.box.min_y = y
      ctx.fillRect((opc.largura - 26) / 2, j + 6, 26, 2)
      if (y > opc.box.max_y) opc.box.max_y = y
    }
    if (opc.nome) {
      let w = Math.round((opc.largura - opc.largura_nome) / 2)
       for (let x = 0; x < opc.nome.length; x++) {
        const letra = opc.nome.charCodeAt(x)
        if (letra !== undefined) {
          ctx.drawImage(img_letras, 16 * ((letra - 32) % 16), 16 * Math.floor((letra - 32) / 16), letras[letra][0], 16, w - (letras[letra][2] || 0), j - 10, letras[letra][0], 16)
          w += letras[letra][0] - (letras[letra][1] || 0) - (letras[letra][2] || 0)
        } else {
          w += 3
        }
      }
     }
     }
   }
   }
  opc.box.w = opc.box.max_x - opc.box.min_x + 1
  opc.box.h = opc.box.max_y - opc.box.min_y + 1
  opc.box.cx = Math.floor((opc.largura - opc.box.w) / 2)
  const fullH = Math.min(64, opc.box.h + (opc.nome ? 14 : (opc.hp ? 8 : 0)), 64)
  opc.box.cy = Math.ceil((64 - fullH) / 2) + fullH - opc.box.h
}


function imprimir () {
  function imprimir () {
  ctx.globalCompositeOperation = 'destination-over'
    const m = opc.animar ? 1 : 0
  ctx.imageSmoothingEnabled = opc.aa
  ctx.mozImageSmoothingEnabled = opc.aa
  ctx.oImageSmoothingEnabled = opc.aa
  ctx.webkitImageSmoothingEnabled = opc.aa
  ctx.scale(2, 2)
  animar(opc.animar ? 1 : 0, 0)
}
 
function animar (m, f) {
  ctx.clearRect(0, 0, 128, 128)
  ctx.drawImage(cm, 64 * f + opc.box.min_x, m * 64 + opc.box.min_y, opc.box.w, opc.box.h, opc.box.cx, opc.box.cy, opc.box.w, opc.box.h)
  desenharNomeHP(ctx)
  if (f < opc.sync[m].frames - 1) f += 1
  else f = 0
  loop = setTimeout(() => animar(m, f), opc.sync[m].tick)
}


function desenharNomeHP (ctx) {
    if (com_tela) {
  ctx.globalCompositeOperation = 'source-over'
      const $quadro = $('.quadro')
  let j = opc.box.cy - 2 - (opc.hp ? 2 : 0)
      const $quadros_area = $('.quadros')
  if (j < 11) j = 11
      if (opc.quadros) {
  if (opc.hp) {
        $quadros_area.show()
    ctx.fillStyle = '#000000'
        $quadro.hide()
    ctx.fillRect((opc.largura - 27) / 2, j + 2, 27, 4)
        $quadro.parent().hide()
    ctx.fillStyle = '#00B800'
        $tick.toggle(opc.sync[m].tick > 0)
    ctx.fillRect((opc.largura - 25) / 2, j + 3, 25, 2)
        $tick.text('Animar com intervalo de ' + opc.sync[m].tick + 'ms')
  }
  if (opc.nome) {
    let w = Math.round((opc.largura - opc.largura_nome) / 2)
    for (let x = 0; x < opc.nome.length; x++) {
      const letra = letras[opc.nome[x]]
      if (letra !== undefined) {
        ctx.drawImage(img_letras, 16 * letra[0], 16 * letra[1], letra[2], 12, w, j - 10, letra[2], 12)
        w += letra[2] - 1
       } else {
       } else {
         w += 3
         $quadros_area.hide()
       }
       }
     }
     }
  }
}


function quadros () {
     const buf = []
  if (opc.quadros) {
     let k = 0
     const m = opc.animar ? 1 : 0
     let transparentPixel = 0
     $quadros_area.show()
 
     $quadro.hide()
    $quadro.parent().hide()
    $tick.toggle(opc.sync[m].tick > 0)
    $tick.text('Animar com intervalo de ' + opc.sync[m].tick + 'ms')
     for (let c = 0; c < opc.sync[m].frames; c++) {
     for (let c = 0; c < opc.sync[m].frames; c++) {
       const q = $('#q' + c)
      ctx.clearRect(0, 0, opc.largura, opc.altura)
      const qtx = q[0].getContext('2d')
      ctx.drawImage(mctx.canvas, opc.sw * c + opc.box.min_x, m * opc.sh + opc.box.min_y, opc.box.w, opc.box.h, opc.box.cx, opc.box.cy, opc.box.w, opc.box.h)
      q.parent().show()
      desenharNomeHP(ctx)
      q.show()
 
      q.attr('width', opc.largura)
       const frame = ctx.getImageData(0, 0, opc.largura, opc.altura).data
      qtx.clearRect(0, 0, q[0].width, q[0].height)
      for (let p = 0; p < opc.largura * opc.altura * 4; p += 4) {
      qtx.drawImage(cm, 64 * c + opc.box.min_x, m * 64 + opc.box.min_y, opc.box.w, opc.box.h, opc.box.cx, opc.box.cy, opc.box.w, opc.box.h)
        buf[k++] = frame[p + 3] ? frame[p] : 0xFF
       desenharNomeHP(qtx)
        buf[k++] = frame[p + 3] ? frame[p + 1] : 0x00
        buf[k++] = frame[p + 3] ? frame[p + 2] : 0xFF
        buf[k++] = 0xFF
        if (!transparentPixel && !frame[p + 3]) transparentPixel = ~~(p / 4)
      }
 
      if (opc.quadros && com_tela) {
        const q = $('#q' + c)
        const qtx = q[0].getContext('2d')
        q.parent().show()
        q.show()
        q.attr('width', opc.largura)
        qtx.drawImage(ctx.canvas, 0, 0, opc.largura, opc.altura, 0, 0, opc.largura, opc.altura)
       }
     }
     }
  } else {
    $quadros_area.hide()
  }
}


function gerarlink () {
    const quantize = UPNG.quantize(buf, 256)
  let link = 'https://www.tibiawiki.com.br/wiki/Outfiter?'
    const palette = quantize.plte.map(p => ((p.est.rgba & 0x0000ff) << 16) + (p.est.rgba & 0x00ff00) + ((p.est.rgba & 0xff0000) >> 16))
  if (opc.outfit.id !== 128) { link += 'o=' + opc.outfit.id + '&' }
    if (palette.length >= 256) { console.warn('GIF palette reached 256 colors. Potential loss of colors.') }
  if (opc.montaria.id !== 0) { link += 'm=' + opc.montaria.id + '&' }
    while ((palette.length & palette.length - 1) !== 0) { palette.push(0) }
  if (opc.lado !== 2) { link += 'l=' + opc.lado + '&' }
    const transparent = quantize.inds[transparentPixel]
  if (opc.sexo === 'Female') { link += 'f&' }
    palette[transparent] = 0
  if (opc.addon1) { link += '1&' }
 
  if (opc.addon2) { link += '2&' }
    const gf = new GifWriter(buf, opc.largura, opc.altura, { loop: 0 })
  if (opc.h !== 78) { link += 'h=' + opc.h + '&' }
    for (let f = 0; f < opc.sync[m].frames; f++) {
  if (opc.p !== 69) { link += 'p=' + opc.p + '&' }
      gf.addFrame(0, 0, opc.largura, opc.altura, quantize.inds.slice(f * opc.largura * opc.altura, (f + 1) * opc.largura * opc.altura), { palette, transparent, delay: opc.sync[m].tick / 10, disposal: 2 })
  if (opc.s !== 58) { link += 's=' + opc.s + '&' }
    }
  if (opc.d !== 76) { link += 'd=' + opc.d + '&' }
  if (opc.animar) { link += 'an&' }
  if (opc.quadros) { link += 'q&' }
  if (opc.aa) { link += 'aa&' }
  if (opc.hp) { link += 'hp&' }
  if (opc.nome) { link += 'n=' + encodeURI(opc.nome) + '&' }
  $link.val(link.slice(0, -1))
}


function obterParametros () {
    $output.attr('src', 'data:image/gif;base64,' + btoa(String.fromCharCode.apply(null, buf.slice(0, gf.end())))).toggle(true)
  const params = new URLSearchParams(window.location.href)
    $output.css('image-rendering', opc.aa ? 'revert' : 'pixelated')
  if (params.has('o')) { opc.outfit = outfits.find(o => o.id.toString() === params.get('o')) }
  if (params.has('m')) { opc.montaria = montarias.find(m => m.id.toString() === params.get('m')) }
  if (params.has('f')) {
    opc.sexo = 'Female'
    $sexo.toggleClass('sexo_m')
    $sexo.toggleClass('sexo_f')
   }
   }
   if (params.has('1')) {
 
     opc.addon1 = true
   function salvarGif () {
     $addon_1.prop('checked', true)
    const templink = document.createElement('a')
     templink.download = (opc.nome || (opc.outfit.id > 0 ? opc.outfit.name : null) || (opc.montaria.id > 0 ? opc.montaria.name : null) || 'outfit') + '.gif'
     templink.href = $output.attr('src')
    templink.click()
   }
   }
   if (params.has('2')) {
 
    opc.addon2 = true
   function uploadGif () {
    $addon_2.prop('checked', true)
    const input = document.createElement('input');
    input.type = 'file';
    input.onchange = e => {
      const file = e.target.files[0];
      if (file && file.type === 'image/gif') {
        const reader = new FileReader()
        reader.readAsArrayBuffer(file);
        reader.onload = readerEvent => {
          const content = readerEvent.target.result
          const gf = new GifReader(new Uint8Array(content))
          $(gctx.canvas).attr('width', gf.width).attr('height', gf.numFrames() * gf.height)
          const delays = []
          for (let f = 0; f < gf.numFrames(); f++) {
            const frameInfo = gf.frameInfo(f)
            const imgData = gctx.getImageData(0, f * gf.height, gf.width, gf.height)
            gf.decodeAndBlitFrameRGBA(f, imgData.data)
            gctx.putImageData(imgData, 0, f * gf.height, 0, 0, frameInfo.width, frameInfo.height)
            gctx.drawImage(gctx.canvas, 0, f * gf.height, gf.width, gf.height, 0, (f + 1) * gf.height, gf.width, gf.height)
            if (frameInfo.disposal === 2) {
              gctx.clearRect(frameInfo.x,  frameInfo.y + (f + 1) * gf.height, frameInfo.width - frameInfo.x, frameInfo.height - frameInfo.y)
            }
            delays.push(frameInfo.delay * 10)
            img_outfit.src = gctx.canvas.toDataURL()
          }
          opc.outfit = {
            'id': 9999,
            'name': 'Objeto',
            'idle': delays,
            'move': [],
            'addons': 0,
            'colors': false,
            'sits': false,
            'reverse': null,
            'type': 'other',
          }
          opc.sw = gf.width
          opc.sh = gf.height
          atualizar()
        }
      } else {
        alert('Arquivo inválido. Somente .gif são aceitos atualmente!')
      }
    }
    input.click();
   }
   }
   if (params.has('l')) { opc.montaria = params.get('l') }
 
  if (params.has('h')) { opc.montaria = params.get('h') }
   function gerarLink () {
  if (params.has('p')) { opc.montaria = params.get('p') }
    let link = 'https://www.tibiawiki.com.br/wiki/Outfitter?&'
  if (params.has('s')) { opc.montaria = params.get('s') }
    if (opc.outfit.id !== 128) { link += 'o=' + opc.outfit.id + '&' }
  if (params.has('d')) { opc.montaria = params.get('d') }
    if (opc.montaria.id !== 0) { link += 'm=' + opc.montaria.id + '&' }
  if (params.has('aa')) {
    if (opc.lado !== 2) { link += 'l=' + opc.lado + '&' }
     opc.aa = true
    if (opc.addon1) { link += 'a1&' }
     $aa.prop('checked', true)
    if (opc.addon2) { link += 'a2&' }
    if (opc.h !== 78) { link += 'h=' + opc.h + '&' }
    if (opc.p !== 69) { link += 'p=' + opc.p + '&' }
    if (opc.s !== 58) { link += 's=' + opc.s + '&' }
    if (opc.d !== 76) { link += 'd=' + opc.d + '&' }
    if (opc.animar) { link += 'an&' }
    if (opc.quadros) { link += 'q&' }
    if (opc.aa) { link += 'aa&' }
    if (opc.hp) { link += 'hp&' }
     if (opc.nome) { link += 'n=' + encodeURI(opc.nome) + '&' }
     $('.link').val(link.slice(0, -1))
    $('.link').parent().toggle(opc.outfit.id != 9999)
   }
   }
   if (params.has('an')) {
 
     opc.animar = true
   function resetParametros () {
     $animar.prop('checked', true)
    opc.outfit = outfits.find(d => d.id === 128)
    opc.montaria = montarias.find(d => d.id === 0)
    opc.lado = 2
    opc.addon1 = false
    opc.addon2 = false
    opc.h = 78
    opc.p = 69
    opc.s = 58
    opc.d = 76
    opc.aa = false
     opc.animar = false
     opc.quadros = false
    opc.hp = false
    opc.nome = ''
    opc.sw = 64
    opc.sh = 64
    opc.largura = 64
    opc.altura = 64
    opc.largura_nome = 0
   }
   }
   if (params.has('q')) {
 
     opc.quadros = true
   function obterParametros () {
     $quadros.prop('checked', true)
    resetParametros()
    const params = new URLSearchParams($output.attr('data-src') || window.location.href)
    if (params.has('o')) { opc.outfit = outfits.find(o => o.id.toString() === params.get('o')) }
    if (params.has('m')) { opc.montaria = montarias.find(m => m.id.toString() === params.get('m')) }
    if (params.has('a1')) { opc.addon1 = true }
    if (params.has('a2')) { opc.addon2 = true }
    if (params.has('l')) { opc.l = params.get('l') }
    if (params.has('h')) { opc.h = params.get('h') }
    if (params.has('p')) { opc.p = params.get('p') }
    if (params.has('s')) { opc.s = params.get('s') }
    if (params.has('d')) { opc.d = params.get('d') }
    if (params.has('aa')) { opc.aa = true }
     if (params.has('an')) { opc.animar = true }
    if (params.has('q')) { opc.quadros = true }
     if (params.has('hp')) { opc.hp = true }
    if (params.has('n')) { opc.nome = decodeURI(params.get('n')) }
   }
   }
   if (params.has('hp')) {
 
     opc.hp = true
   function preencherListas () {
     $vida.prop('checked', true)
    $form_outfits.empty()
    let dif = true
    for (const outfit of outfits.filter(o => o.type === 'outfit')) {
      $form_outfits.append('<div class="janela_opcao_out' + (dif ? '_dif' : '') + '"><input id="o' + outfit.id + '" type="radio" name="outfit" value="' + outfit.id + '"><label for="o' + outfit.id + '">' + outfit.name + '</label></div>')
      dif = !dif
     }
    $form_outros.empty()
    dif = true
     for (const outfit of outfits.filter(o => o.type === 'other')) {
      $form_outros.append('<div class="janela_opcao_out' + (dif ? '_dif' : '') + '"><input id="o' + outfit.id + '" type="radio" name="outfit" value="' + outfit.id + '"><label for="o' + outfit.id + '">' + outfit.name + '</label></div>')
      dif = !dif
    }
    $form_montarias.empty()
    dif = true
    for (const montaria of montarias) {
      $form_montarias.append('<div class="janela_opcao_mount' + (dif ? '_dif' : '') + '"><input id="m' + montaria.id + '" type="radio" name="montaria" value="' + montaria.id + '"><label for="m' + montaria.id + '">' + montaria.name + '</label></div>')
      dif = !dif
    }
   }
   }
  if (params.has('n')) {
    opc.nome = decodeURI(params.get('n'))
    $nome_char.val(opc.nome)
  }
}


function janelas () {
   /* Eventos */
   $form_outfits.empty()
  let dif = true
  for (const outfit of outfits.filter(o => o.type === 'outfit')) {
    $form_outfits.append('<div class="janela_opcao_out' + (dif ? '_dif' : '') + '"><input id="o' + outfit.id + '" type="radio" name="outfit" value="' + outfit.id + '"><label for="o' + outfit.id + '">' + outfit.name + '</label></div>')
    dif = !dif
  }
  $form_outros.empty()
  dif = true
  for (const outfit of outfits.filter(o => o.type === 'other')) {
    $form_outros.append('<div class="janela_opcao_out' + (dif ? '_dif' : '') + '"><input id="o' + outfit.id + '" type="radio" name="outfit" value="' + outfit.id + '"><label for="o' + outfit.id + '">' + outfit.name + '</label></div>')
    dif = !dif
  }
  $form_montarias.empty()
  dif = true
  for (const montaria of montarias) {
    $form_montarias.append('<div class="janela_opcao_mount' + (dif ? '_dif' : '') + '"><input id="m' + montaria.id + '" type="radio" name="montaria" value="' + montaria.id + '"><label for="m' + montaria.id + '">' + montaria.name + '</label></div>')
    dif = !dif
  }
  $('input#o' + opc.outfit.id).prop('checked', true)
  $('input#m' + opc.montaria.id).prop('checked', true)
}


/* Eventos */
  if (!com_tela) return


$(document).ready(function () {
  $form_outfits.on('change', function () {
  $carregando_outfiter.hide()
    $form_outros.find('input[name=outfit]:checked').prop('checked', false)
  tabela_cor()
    opc.outfit = outfits.find(o => o.id.toString() === $('input[name=outfit]:checked').val())
  artworks()
    atualizar()
  obterParametros()
  })
  janelas()
  atualizar()
})


$form_outfits.on('change', function () {
  $form_outros.on('change', function () {
  $form_outros.find('input[name=outfit]:checked').prop('checked', false)
    $form_outfits.find('input[name=outfit]:checked').prop('checked', false)
  opc.outfit = outfits.find(o => o.id.toString() === $('input[name=outfit]:checked').val())
    opc.outfit = outfits.find(o => o.id.toString() === $('input[name=outfit]:checked').val())
  atualizar()
    atualizar()
})
  })


$form_outros.on('change', function () {
  $form_montarias.on('change', function () {
  $form_outfits.find('input[name=outfit]:checked').prop('checked', false)
    opc.montaria = montarias.find(m => m.id.toString() === $('input[name=montaria]:checked').val())
  opc.outfit = outfits.find(o => o.id.toString() === $('input[name=outfit]:checked').val())
    atualizar()
  atualizar()
  })
})


$form_montarias.on('change', function () {
  $('#botao_gif').on('click', function () {
  opc.montaria = montarias.find(m => m.id.toString() === $('input[name=montaria]:checked').val())
    salvarGif()
   atualizar()
   })
})


$('#out_e').on('click', function () {
  $('#botao_up').on('click', function () {
  const idx = outfits.indexOf(opc.outfit)
    uploadGif()
  if (idx !== 0) opc.outfit = outfits.at(idx - 1)
   })
  else opc.outfit = outfits[outfits.length - 1]
  $('input[value="' + opc.outfit.id + '"]').prop('checked', true)
   atualizar()
})


$('#out_d').on('click', function () {
  $('#out_e').on('click', function () {
  const idx = outfits.indexOf(opc.outfit)
    const idx = outfits.indexOf(opc.outfit)
  if (idx !== outfits.length - 1) opc.outfit = outfits.at(idx + 1)
    if (idx !== 0) opc.outfit = outfits.at(idx - 1)
  else opc.outfit = outfits[0]
    else opc.outfit = outfits[outfits.length - 1]
  $('input[value="' + opc.outfit.id + '"]').prop('checked', true)
    $('input[value="' + opc.outfit.id + '"]').prop('checked', true)
  atualizar()
    atualizar()
})
  })


$('#mount_e').on('click', function () {
  $('#out_d').on('click', function () {
  const idx = montarias.indexOf(opc.montaria)
    const idx = outfits.indexOf(opc.outfit)
  if (idx !== 0) opc.montaria = montarias.at(idx - 1)
    if (idx !== outfits.length - 1) opc.outfit = outfits.at(idx + 1)
  else opc.montaria = montarias[montarias.length - 1]
    else opc.outfit = outfits[0]
  $('input[value="' + opc.montaria.id + '"]').prop('checked', true)
    $('input[value="' + opc.outfit.id + '"]').prop('checked', true)
  atualizar()
    atualizar()
})
  })


$('#mount_d').on('click', function () {
  $('#mount_e').on('click', function () {
  const idx = montarias.indexOf(opc.montaria)
    const idx = montarias.indexOf(opc.montaria)
  if (idx !== montarias.length - 1) opc.montaria = montarias.at(idx + 1)
    if (idx !== 0) opc.montaria = montarias.at(idx - 1)
  else opc.montaria = montarias[0]
    else opc.montaria = montarias[montarias.length - 1]
  $('input[value="' + opc.montaria.id + '"]').prop('checked', true)
    $('input[value="' + opc.montaria.id + '"]').prop('checked', true)
  atualizar()
    atualizar()
})
  })


$('.botao_sexo').on('click', function () {
  $('#mount_d').on('click', function () {
  // $sexo.toggleClass("sexo_m");
    const idx = montarias.indexOf(opc.montaria)
  // $sexo.toggleClass("sexo_f");
    if (idx !== montarias.length - 1) opc.montaria = montarias.at(idx + 1)
  // if (opc.sexo == "Male") {
    else opc.montaria = montarias[0]
  //  opc.sexo = "Female";
    $('input[value="' + opc.montaria.id + '"]').prop('checked', true)
  // } else {
    atualizar()
  //  opc.sexo = "Male";
  })
  // }
  // atualizar();
})


$('.seta_cima').on('click', function () {
  $('.seta_cima').on('click', function () {
  opc.lado = 0
    opc.lado = 0
  atualizar()
    atualizar()
})
  })


$('.seta_direita').on('click', function () {
  $('.seta_direita').on('click', function () {
  opc.lado = 1
    opc.lado = 1
  atualizar()
    atualizar()
})
  })


$('.seta_baixo').on('click', function () {
  $('.seta_baixo').on('click', function () {
  opc.lado = 2
    opc.lado = 2
  atualizar()
    atualizar()
})
  })


$('.seta_esquerda').on('click', function () {
  $('.seta_esquerda').on('click', function () {
  opc.lado = 3
    opc.lado = 3
  atualizar()
    atualizar()
})
  })


$addon_1.on('click', function () {
  $addon_1.on('click', function () {
  opc.addon1 = !opc.addon1
    opc.addon1 = !opc.addon1
  atualizar()
    atualizar()
})
  })


$addon_2.on('click', function () {
  $addon_2.on('click', function () {
  opc.addon2 = !opc.addon2
    opc.addon2 = !opc.addon2
  atualizar()
    atualizar()
})
  })


$aa.on('click', function () {
  $aa.on('click', function () {
  opc.aa = $aa.prop('checked')
    opc.aa = $aa.prop('checked')
  atualizar()
    atualizar()
})
  })


$animar.on('click', function () {
  $animar.on('click', function () {
  opc.animar = $animar.prop('checked')
    opc.animar = $animar.prop('checked')
  atualizar()
    atualizar()
})
  })


$quadros.on('click', function () {
  $quadros.on('click', function () {
  opc.quadros = $quadros.prop('checked')
    opc.quadros = $quadros.prop('checked')
  atualizar()
    atualizar()
})
  })


$vida.on('click', function () {
  $vida.on('click', function () {
  opc.hp = $vida.prop('checked')
    opc.hp = $vida.prop('checked')
  atualizar()
    atualizar()
})
  })


$nome_char.on('keyup', function () {
  $nome_char.on('keyup', function () {
  opc.nome = $('.nome_char').val()
    opc.nome = $('.nome_char').val()
  atualizar()
    atualizar()
})
  })


/* Seleção de Cores */
  /* Seleção de Cores */


function tabela_cor () {
  function tabelaCores () {
  let cont = 0
    let cont = 0
  const $quadrado_cor = $('.tabela_cores').children('tbody').children('tr').children('td')
    const $quadrado_cor = $('.tabela_cores').children('tbody').children('tr').children('td')


  $quadrado_cor.each(function () {
    $quadrado_cor.each(function () {
    $(this).attr('id', 'c' + cont)
      $(this).attr('id', 'c' + cont)
    $(this).css('background', 'rgb(' + cores[cont][0] + ', ' + cores[cont][1] + ', ' + cores[cont][2] + ')')
      $(this).css('background', 'rgb(' + cores[cont][0] + ', ' + cores[cont][1] + ', ' + cores[cont][2] + ')')
    cont++
      cont++
  })
    })


  $quadrado_cor.on('click', function () {
    $quadrado_cor.on('click', function () {
    $quadrado_cor.removeClass('cor_selecionada')
      $quadrado_cor.removeClass('cor_selecionada')
    $(this).addClass('cor_selecionada')
      $(this).addClass('cor_selecionada')
    opc[$('.selecionado').attr('id')[0]] = parseInt($(this).attr('id').substring(1))
      opc[$('.selecionado').attr('id')[0]] = parseInt($(this).attr('id').substring(1))
    atualizar()
      atualizar()
  })
    })


  $('.seletores').on('click', function () {
    $('.seletores').on('click', function () {
    $('.selecionado').removeClass('selecionado')
      $('.selecionado').removeClass('selecionado')
    $(this).addClass('selecionado')
      $(this).addClass('selecionado')
    $quadrado_cor.removeClass('cor_selecionada')
      $quadrado_cor.removeClass('cor_selecionada')
    $('#c' + opc[$(this).attr('id')[0]]).addClass('cor_selecionada')
      $('#c' + opc[$(this).attr('id')[0]]).addClass('cor_selecionada')
  })
    })


  $('#head').click()
    $('#head').click()
}
  }


/* Artworks */
  /* Artworks */


function artworks () {
  function artworks () {
  const $artwork_e = $('#artwork_e')
    const $artwork_e = $('#artwork_e')
  const $artwork_d = $('#artwork_d')
    const $artwork_d = $('#artwork_d')
  const artwork_A = [['BlackStag', 75, 0, 230], ['ConqueringLion', 85, 7, 280], ['Scorpion', 80, 0, 250], ['RiftRunner', 95, 0, 290]][RandomInt(0, 3)]
    const artwork_A = [['BlackStag', 75, 0, 230], ['ConqueringLion', 85, 7, 280], ['Scorpion', 80, 0, 250], ['RiftRunner', 95, 0, 290]][RandomInt(0, 3)]
  const artwork_B = [['ArmoredWarhorse', 70, 0, 240], ['LadyBug', 70, -5, 240], ['Walker', 70, -5, 230]][RandomInt(0, 2)]
    const artwork_B = [['ArmoredWarhorse', 70, 0, 240], ['LadyBug', 70, -5, 240], ['Walker', 70, -5, 230]][RandomInt(0, 2)]
  const artwork_C = [['Dragonling', 85, 0, 300], ['Dragonling2', 90, 0, 300], ['Waccoon', 105, -15, 300]][RandomInt(0, 2)]
    const artwork_C = [['Dragonling', 85, 0, 300], ['Dragonling2', 90, 0, 300], ['Waccoon', 105, -15, 300]][RandomInt(0, 2)]
  const artwork_D = [['BatCat', 105, -20, 300], ['CreepingHand', 80, -5, 250], ['PanzerBird', 72, 0, 230], ['MantaRay', 105, -15, 300]][RandomInt(0, 3)]
    const artwork_D = [['BatCat', 105, -20, 300], ['CreepingHand', 80, -5, 250], ['PanzerBird', 72, 0, 230], ['MantaRay', 105, -15, 300]][RandomInt(0, 3)]
  $artwork_e.append('<img class="artwork" id="artwork_A" alt="' + artwork_A[0] + '" style="width: ' + artwork_A[1] + '%; max-width: ' + artwork_A[3] + 'px;  top: ' + artwork_A[2] + 'px;" src="' + assetsBaseURL + 'Outfiter_Artwork_' + artwork_A[0] + '.png">')
    $artwork_e.append('<img class="artwork" id="artwork_A" alt="' + artwork_A[0] + '" style="width: ' + artwork_A[1] + '%; max-width: ' + artwork_A[3] + 'px;  top: ' + artwork_A[2] + 'px;" src="' + assetsBaseURL + 'Outfiter_Artwork_' + artwork_A[0] + '.png">')
  $artwork_d.append('<img class="artwork" id="artwork_B" alt="' + artwork_B[0] + '" style="width: ' + artwork_B[1] + '%; max-width: ' + artwork_B[3] + 'px;  top: ' + artwork_B[2] + 'px;" src="' + assetsBaseURL + 'Outfiter_Artwork_' + artwork_B[0] + '.png">')
    $artwork_d.append('<img class="artwork" id="artwork_B" alt="' + artwork_B[0] + '" style="width: ' + artwork_B[1] + '%; max-width: ' + artwork_B[3] + 'px;  top: ' + artwork_B[2] + 'px;" src="' + assetsBaseURL + 'Outfiter_Artwork_' + artwork_B[0] + '.png">')
  $artwork_e.append('<img class="artwork" id="artwork_C" alt="' + artwork_C[0] + '" style="width: ' + artwork_C[1] + '%; max-width: ' + artwork_C[3] + 'px; right: ' + artwork_C[2] + 'px;" src="' + assetsBaseURL + 'Outfiter_Artwork_' + artwork_C[0] + '.png">')
    $artwork_e.append('<img class="artwork" id="artwork_C" alt="' + artwork_C[0] + '" style="width: ' + artwork_C[1] + '%; max-width: ' + artwork_C[3] + 'px; right: ' + artwork_C[2] + 'px;" src="' + assetsBaseURL + 'Outfiter_Artwork_' + artwork_C[0] + '.png">')
  $artwork_d.append('<img class="artwork" id="artwork_D" alt="' + artwork_D[0] + '" style="width: ' + artwork_D[1] + '%; max-width: ' + artwork_D[3] + 'px;  left: ' + artwork_D[2] + 'px;" src="' + assetsBaseURL + 'Outfiter_Artwork_' + artwork_D[0] + '.png">')
    $artwork_d.append('<img class="artwork" id="artwork_D" alt="' + artwork_D[0] + '" style="width: ' + artwork_D[1] + '%; max-width: ' + artwork_D[3] + 'px;  left: ' + artwork_D[2] + 'px;" src="' + assetsBaseURL + 'Outfiter_Artwork_' + artwork_D[0] + '.png">')


  function RandomInt (min, max) {
    function RandomInt (min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min
      return Math.floor(Math.random() * (max - min + 1)) + min
    }
   }
   }
}
}

Edição atual tal como às 15h23min de 12 de março de 2024

if (typeof mw !== 'undefined') {
  mw.loader.load('/index.php?title=MediaWiki:OutfitterDados.js&action=raw&ctype=text/javascript')
  mw.loader.load('/index.php?title=MediaWiki:UPNG.js&action=raw&ctype=text/javascript')
  mw.loader.load('/index.php?title=MediaWiki:omggif.js&action=raw&ctype=text/javascript')

  if ($('.outfitter').length > 0) {
    mw.loader.load('/index.php?title=MediaWiki:Outfitter.css&action=raw&ctype=text/css', 'text/css')
    $.ajax({ url: '/index.php?title=MediaWiki:Outfitter.html&action=raw', success: ready })
  } else { ready() }
}

function ready(res) {
  if (typeof dados !== 'undefined' && typeof UPNG !== 'undefined' && typeof GifWriter !== 'undefined' ) {
    if (res) { $('.outfitter').html(res.slice(5, -6)) }
    const outputs = []
    $('.outfitter_output').each(function() {
      if ($(this).is('img')) { outputs.push($(this)) }
      else { outputs.push($(document.createElement('img')).attr('data-src', $(this).attr('data-src')).appendTo($(this))) }
    })
    outfitter(outputs)
  }
  else setTimeout(() => ready(res), 300)
}

function outfitter(outputs) {
  /* Elementos */
  const com_tela = $('.outfitter').length > 0
  const $form_outfits = $('#form_outfits')
  const $form_montarias = $('#form_montarias')
  const $form_outros = $('#form_outros')
  const $addon_1 = $('#addon_1')
  const $addon_2 = $('#addon_2')
  const $aa = $('#aa')
  const $animar = $('#animar')
  const $quadros = $('#quadros')
  const $vida = $('#vida')
  const $nome_char = $('.nome_char')
  const $tick = $('#tick')

  const ctx = document.createElement('canvas').getContext('2d')
  const cctx = document.createElement('canvas').getContext('2d')
  const mctx = document.createElement('canvas').getContext('2d')
  const gctx = document.createElement('canvas').getContext('2d')

  const img_outfit = $(document.createElement('img'))[0]
  const img_mount = $(document.createElement('img'))[0]
  const img_letras = $(document.createElement('img'))[0]

  let imgs_carregando = -1
  img_outfit.onload = carregado
  img_mount.onload = carregado
  img_letras.onload = carregado

  const outfits = [{ id: -1, name: 'Nenhum', idle: [0], move: [0], type: 'other', sits: true, colors: false }].concat(dados.filter(o => o.type !== 'mount').sort(sortFn))
  const montarias = [{ id: 0, name: 'Nenhuma', idle: [0], move: [0], type: 'mount', sits: false, colors: false }].concat(dados.filter(o => o.type === 'mount').sort(sortFn))
  function sortFn (a, b) {
    if (a.type !== b.type) return (a.type === 'outfit' ? -1 : 1)
    if (isNaN(a.name) && isNaN(b.name)) return a.name.localeCompare(b.name)
    if (!isNaN(a.name) && !isNaN(b.name)) return a.id - b.id
    if (!isNaN(a.name)) return 1
    return -1
  }

  let $output
  const opc = { }

  /* Init */

  if (com_tela) {
    if (typeof mw !== 'undefined' && mw.user?.getGroups) { mw.user.getGroups().then((user_groups) => $('#botao_up').toggle(user_groups.includes('bureaucrat') || user_groups.includes('sysop'))) }
    preencherListas()
    tabelaCores()
    artworks()
  }
  processar()

  /* Funções */

  function processar() {
    if (outputs.length > 0) {
      $output = outputs.pop()
      obterParametros()
      atualizar()
    }
  }

  function atualizar () {
    validarExcecoes()
    if (com_tela) {
      sincronizarTela()
      gerarLink()
    }
    $output.attr('src', '/wiki/Especial:Redirecionar/file/Outfiter_Carregando.gif')
    buscarArquivo()
  }

  function carregado () {
    imgs_carregando--
    if (imgs_carregando <= 0) {
      dimensionar()
      montar()
      imprimir()
      processar()
    }
  }

  function validarExcecoes () {
    if (opc.outfit.addons === 0) {
      opc.addon1 = false
      opc.addon2 = false
    }
    if (opc.outfit.move.length === 0) {
      opc.animar = false
    }
    if (opc.outfit.idle.length === 0) {
      opc.animar = true
    }
    if (!opc.outfit.sits) {
      opc.montaria = montarias[0]
    }
    if (opc.outfit.id === 9999) {
      opc.lado = 0

    }
  }

  function sincronizarTela () {
    $('.outfit_nome').text(opc.outfit.name)
    $('.montaria_nome').text(opc.montaria.name)

    $('input[value="' + opc.outfit.id + '"]').prop('checked', true)
    $('input[value="' + opc.montaria.id + '"]').prop('checked', true)

    $nome_char.val(opc.nome)
    $addon_1.prop('checked', opc.addon1)
    $addon_2.prop('checked', opc.addon2)
    $animar.prop('checked', opc.animar)
    $vida.prop('checked', opc.vida)
    $aa.prop('checked', opc.aa)
    $quadros.prop('checked', opc.quadros)

    $addon_1.prop('disabled', opc.outfit.addons === 0)
    $addon_2.prop('disabled', opc.outfit.addons === 0)
    $animar.prop('disabled', opc.outfit.idle.length === 0 || opc.outfit.move.length === 0)
    $('input[name="montaria"]').not('[id="m0"]').parent().toggle(opc.outfit.sits)
    $('#tabela_cores_container').toggle(opc.outfit.colors || opc.montaria.colors)
  }

  function buscarArquivo () {
    imgs_carregando = 1 + (opc.montaria.id > 0 ? 1 : 0) + (opc.outfit.id > 0 ? 1 : 0)
    if (imgs_carregando) {
      if (opc.montaria.id > 0) {
        img_mount.setAttribute('src', assetsBaseURL + 'Outfitter_' + opc.montaria.id + '.png')
      }
      if (opc.outfit.id > 0) {
        img_outfit.setAttribute('src', opc.outfit.id !== 9999 ? assetsBaseURL + 'Outfitter_' + opc.outfit.id + '.png' : gctx.canvas.toDataURL())
      }
      img_letras.setAttribute('src', assetsBaseURL + 'Outfitter_Letras.png')
    } else carregado()
  }

  function mdc (a, b) { return a ? mdc(b % a, a) : b }

  function normalize (array, sum) {
    if (array.length === 1 && array[0] === 0) return [0]
    let array_sum = array.reduce((s, a) => s + a, 0)
    return array.map(i => Math.round(i / array_sum * sum))
  }

  function round (array, value) {
    return array.map(i => Math.round(Math.round(i / value) * value))
  }

  function sync_frames (out_frames, mount_frames) {
    if (out_frames.length <= 1 && mount_frames.length <= 1) return { frames: 1, tick: 0 }

    let out_sum = out_frames.reduce((s, a) => s + a, 0)
    let mount_sum = mount_frames.reduce((s, a) => s + a, 0)
    const tick = out_frames.concat(mount_frames).filter(t => t !== 0).reduce(mdc)
    return { frames: Math.max(out_sum / tick, mount_sum / tick), tick }
  }

  function montar () {
    const quadros_outfit = [opc.outfit.idle, normalize(opc.outfit.move, 800)]
    const quadros_mount = [round(opc.montaria.idle, 100), normalize(opc.montaria.move, 800)]
    let add = (opc.outfit.sits ? 2 : 1) * (1 + opc.outfit.addons)
    let offset = (opc.montaria.id !== 0 ? 1 + opc.outfit.addons : 0)
    opc.box = { min_x: 32, min_y: 32, max_x: 32, max_y: 32 }
    opc.sync = [sync_frames(quadros_outfit[0], quadros_mount[0]), sync_frames(quadros_outfit[1], quadros_mount[1])]
    const mCanvasWidth = Math.max(opc.sync[0].frames, opc.sync[1].frames) * opc.sw
    $(mctx.canvas).attr('width', mCanvasWidth) .attr('height', 128)
    mctx.clearRect(0, 0, mCanvasWidth, 128)
    for (let m = 0; m <= 1; m++) {
      for (let j = 0, jm = 0, jo = 0, mount_timer = 0, out_timer = 0; j < opc.sync[m].frames; j++) {
        if (opc.montaria.id !== 0) {
          mctx.drawImage(img_mount, opc.lado * (opc.montaria.colors ? 2 : 1) * opc.sw, opc.sh * (jm + m * quadros_mount[0].length), opc.sw, opc.sh, opc.sw * j, m * opc.sh, opc.sw, opc.sh)
          if (opc.montaria.colors) {
            cctx.globalCompositeOperation = 'copy'
            cctx.drawImage(img_mount, opc.lado * (opc.montaria.colors ? 2 : 1) * opc.sw + opc.sw, opc.sh * (jm + m * quadros_mount[0].length), opc.sw, opc.sh, 0, 0, opc.sw, opc.sh)
            colorir(j, m)
          }
          mount_timer += opc.sync[m].tick
          if (mount_timer >= quadros_mount[m][jm]) {
            mount_timer -= quadros_mount[m][jm]
            jm++
            if (jm >= quadros_mount[m].length) jm = 0
          }
        }
        if (opc.outfit.id > 0) {
          const yo = (jo + m * quadros_outfit[0].length) * add + offset
          out_timer += opc.sync[m].tick
          if (out_timer >= quadros_outfit[m][jo]) {
            out_timer -= quadros_outfit[m][jo]
            jo++
            if (jo >= quadros_outfit[m].length) jo = 0
          }
          mctx.drawImage(img_outfit, opc.lado * (opc.outfit.colors ? 2 : 1) * opc.sw, opc.sh * yo, opc.sw, opc.sh, opc.sw * j, m * opc.sh, opc.sw, opc.sh)
          if (opc.outfit.colors) {
            cctx.globalCompositeOperation = 'copy'
            cctx.drawImage(img_outfit, opc.lado * (opc.outfit.colors ? 2 : 1) * opc.sw + opc.sw, opc.sh * yo, opc.sw, opc.sh, 0, 0, opc.sw, opc.sh)
            colorir(j, m)
          }
          const reverse = opc.outfit.reverse && opc.outfit.reverse[opc.lado]
          if (!reverse) {
            if (opc.addon1) addon(opc.lado, 1, 0, yo, m, j)
            if (opc.addon2) addon(opc.lado, 2, 0, yo, m, j)
          } else {
            if (opc.addon2) addon(opc.lado, 2, 0, yo, m, j)
            if (opc.addon1) addon(opc.lado, 1, 0, yo, m, j)
          }
        }
        if ((opc.animar && m === 1) || ( !opc.animar && m === 0)) medir(j, m)
      }
    }
  }

  function colorir (j, m) {
    const imgData = mctx.getImageData(opc.sw * j, opc.sh * m, opc.sw, opc.sh)
    let corData = cctx.getImageData(0, 0, opc.sw, opc.sh)
    for (let i = 0; i < corData.data.length; i += 4) {
      const r = corData.data[i]
      const g = corData.data[i + 1]
      const b = corData.data[i + 2]
      if ((r >= 240) && (g >= 240) && (b <= 15)) {
        corData.data[i] = cores[opc.h][0]
        corData.data[i + 1] = cores[opc.h][1]
        corData.data[i + 2] = cores[opc.h][2]
      } else if ((r >= 240) && (g <= 15) && (b <= 15)) {
        corData.data[i] = cores[opc.p][0]
        corData.data[i + 1] = cores[opc.p][1]
        corData.data[i + 2] = cores[opc.p][2]
      } else if ((r <= 15) && (g >= 240) && (b <= 15)) {
        corData.data[i] = cores[opc.s][0]
        corData.data[i + 1] = cores[opc.s][1]
        corData.data[i + 2] = cores[opc.s][2]
      } else if ((r <= 15) && (g <= 15) && (b >= 240)) {
        corData.data[i] = cores[opc.d][0]
        corData.data[i + 1] = cores[opc.d][1]
        corData.data[i + 2] = cores[opc.d][2]
      }
    }
    for (let i = 0; i < imgData.data.length; i += 4) {
      if (corData.data[i + 3] !== 0) {
        imgData.data[i] = imgData.data[i] * corData.data[i] / 255
        imgData.data[i + 1] = imgData.data[i + 1] * corData.data[i + 1] / 255
        imgData.data[i + 2] = imgData.data[i + 2] * corData.data[i + 2] / 255
      }
    }
    mctx.putImageData(imgData, opc.sw * j, opc.sh * m, 0, 0, opc.sw, opc.sh)
  }

  function addon (lado, n, x, y, m, j) {
    mctx.globalCompositeOperation = 'source-over'
    mctx.drawImage(img_outfit, lado * (opc.outfit.colors ? 2 : 1) * opc.sw, opc.sh * y + opc.sh * n, opc.sw, opc.sh, opc.sw * j, opc.sh * m, opc.sw, opc.sh)
    cctx.drawImage(img_outfit, lado * (opc.outfit.colors ? 2 : 1) * opc.sw + opc.sw, opc.sh * y + opc.sh * n, opc.sw, opc.sh, 0, 0, opc.sw, opc.sh)
    if (opc.outfit.colors) colorir(j, m)
  }

  function dimensionar () {
    opc.largura = Math.max(64, opc.sw)
    if (opc.nome !== '') {
      opc.largura_nome = 0
      for (let x = 0; x < opc.nome.length; x++) {
        const letra = letras[opc.nome.charCodeAt(x)]
        opc.largura_nome += letra ? letra[0] - (letra[1] || 0) - (letra[2] || 0) : 3
      }
      while (opc.largura_nome + (opc.largura_nome % 2 ? 4 : 3) > opc.largura) {
        opc.largura += 8
      }
    }
    $(ctx.canvas).attr('width', opc.largura)
  }

  function medir (j, m) {
    const imgData = mctx.getImageData(opc.sw * j, opc.sh * m, opc.sw, opc.sh)
    for (let i = 0; i < imgData.data.length; i += 4) {
      if (imgData.data[i + 3] !== 0) {
        const x = (i / 4) % opc.sw
        const y = Math.floor((i / 4) / opc.sw)
        if (x < opc.box.min_x) opc.box.min_x = x
        if (x > opc.box.max_x) opc.box.max_x = x
        if (y < opc.box.min_y) opc.box.min_y = y
        if (y > opc.box.max_y) opc.box.max_y = y
      }
    }
    opc.box.w = opc.box.max_x - opc.box.min_x + 1
    opc.box.h = opc.box.max_y - opc.box.min_y + 1
    opc.box.cx = Math.floor((opc.largura - opc.box.w) / 2)
    const fullH = Math.min(opc.altura, opc.box.h + (opc.nome ? 14 : (opc.hp ? 8 : 0)))
    opc.box.cy = Math.ceil((opc.altura - fullH) / 2) + fullH - opc.box.h
  }

  function desenharNomeHP (ctx) {
    ctx.globalCompositeOperation = 'source-over'
    let j = opc.box.cy - 6 - (opc.hp ? 4 : 0)
    if (j < 9) j = 9
    if (opc.hp) {
      ctx.fillStyle = '#000000'
      ctx.fillRect((opc.largura - 28) / 2, j + 5, 28, 4)
      ctx.fillStyle = '#00B800'
      ctx.fillRect((opc.largura - 26) / 2, j + 6, 26, 2)
    }
    if (opc.nome) {
      let w = Math.round((opc.largura - opc.largura_nome) / 2)
      for (let x = 0; x < opc.nome.length; x++) {
        const letra = opc.nome.charCodeAt(x)
        if (letra !== undefined) {
          ctx.drawImage(img_letras, 16 * ((letra - 32) % 16), 16 * Math.floor((letra - 32) / 16), letras[letra][0], 16, w - (letras[letra][2] || 0), j - 10, letras[letra][0], 16)
          w += letras[letra][0] - (letras[letra][1] || 0) - (letras[letra][2] || 0)
        } else {
          w += 3
        }
      }
    }
  }

  function imprimir () {
    const m = opc.animar ? 1 : 0

    if (com_tela) {
      const $quadro = $('.quadro')
      const $quadros_area = $('.quadros')
      if (opc.quadros) {
        $quadros_area.show()
        $quadro.hide()
        $quadro.parent().hide()
        $tick.toggle(opc.sync[m].tick > 0)
        $tick.text('Animar com intervalo de ' + opc.sync[m].tick + 'ms')
      } else {
        $quadros_area.hide()
      }
    }

    const buf = []
    let k = 0
    let transparentPixel = 0

    for (let c = 0; c < opc.sync[m].frames; c++) {
      ctx.clearRect(0, 0, opc.largura, opc.altura)
      ctx.drawImage(mctx.canvas, opc.sw * c + opc.box.min_x, m * opc.sh + opc.box.min_y, opc.box.w, opc.box.h, opc.box.cx, opc.box.cy, opc.box.w, opc.box.h)
      desenharNomeHP(ctx)

      const frame = ctx.getImageData(0, 0, opc.largura, opc.altura).data
      for (let p = 0; p < opc.largura * opc.altura * 4; p += 4) {
        buf[k++] = frame[p + 3] ? frame[p] : 0xFF
        buf[k++] = frame[p + 3] ? frame[p + 1] : 0x00
        buf[k++] = frame[p + 3] ? frame[p + 2] : 0xFF
        buf[k++] = 0xFF
        if (!transparentPixel && !frame[p + 3]) transparentPixel = ~~(p / 4)
      }

      if (opc.quadros && com_tela) {
        const q = $('#q' + c)
        const qtx = q[0].getContext('2d')
        q.parent().show()
        q.show()
        q.attr('width', opc.largura)
        qtx.drawImage(ctx.canvas, 0, 0, opc.largura, opc.altura, 0, 0, opc.largura, opc.altura)
      }
    }

    const quantize = UPNG.quantize(buf, 256)
    const palette = quantize.plte.map(p => ((p.est.rgba & 0x0000ff) << 16) + (p.est.rgba & 0x00ff00) + ((p.est.rgba & 0xff0000) >> 16))
    if (palette.length >= 256) { console.warn('GIF palette reached 256 colors. Potential loss of colors.') }
    while ((palette.length & palette.length - 1) !== 0) { palette.push(0) }
    const transparent = quantize.inds[transparentPixel]
    palette[transparent] = 0

    const gf = new GifWriter(buf, opc.largura, opc.altura, { loop: 0 })
    for (let f = 0; f < opc.sync[m].frames; f++) {
      gf.addFrame(0, 0, opc.largura, opc.altura, quantize.inds.slice(f * opc.largura * opc.altura, (f + 1) * opc.largura * opc.altura), { palette, transparent, delay: opc.sync[m].tick / 10, disposal: 2 })
    }

    $output.attr('src', 'data:image/gif;base64,' + btoa(String.fromCharCode.apply(null, buf.slice(0, gf.end())))).toggle(true)
    $output.css('image-rendering', opc.aa ? 'revert' : 'pixelated')
  }

  function salvarGif () {
    const templink = document.createElement('a')
    templink.download = (opc.nome || (opc.outfit.id > 0 ? opc.outfit.name : null) || (opc.montaria.id > 0 ? opc.montaria.name : null) || 'outfit') + '.gif'
    templink.href = $output.attr('src')
    templink.click()
  }

  function uploadGif () {
    const input = document.createElement('input');
    input.type = 'file';
    input.onchange = e => {
      const file = e.target.files[0];
      if (file && file.type === 'image/gif') {
        const reader = new FileReader()
        reader.readAsArrayBuffer(file);
        reader.onload = readerEvent => {
          const content = readerEvent.target.result
          const gf = new GifReader(new Uint8Array(content))
          $(gctx.canvas).attr('width', gf.width).attr('height', gf.numFrames() * gf.height)
          const delays = []
          for (let f = 0; f < gf.numFrames(); f++) {
            const frameInfo = gf.frameInfo(f)
            const imgData = gctx.getImageData(0, f * gf.height, gf.width, gf.height)
            gf.decodeAndBlitFrameRGBA(f, imgData.data)
            gctx.putImageData(imgData, 0, f * gf.height, 0, 0, frameInfo.width, frameInfo.height)
            gctx.drawImage(gctx.canvas, 0, f * gf.height, gf.width, gf.height, 0, (f + 1) * gf.height, gf.width, gf.height)
            if (frameInfo.disposal === 2) {
              gctx.clearRect(frameInfo.x,  frameInfo.y + (f + 1) * gf.height, frameInfo.width - frameInfo.x, frameInfo.height - frameInfo.y)
            }
            delays.push(frameInfo.delay * 10)
            img_outfit.src = gctx.canvas.toDataURL()
          }
          opc.outfit = {
            'id': 9999,
            'name': 'Objeto',
            'idle': delays,
            'move': [],
            'addons': 0,
            'colors': false,
            'sits': false,
            'reverse': null,
            'type': 'other',
          }
          opc.sw = gf.width
          opc.sh = gf.height
          atualizar()
        }
      } else {
        alert('Arquivo inválido. Somente .gif são aceitos atualmente!')
      }
    }
    input.click();
  }

  function gerarLink () {
    let link = 'https://www.tibiawiki.com.br/wiki/Outfitter?&'
    if (opc.outfit.id !== 128) { link += 'o=' + opc.outfit.id + '&' }
    if (opc.montaria.id !== 0) { link += 'm=' + opc.montaria.id + '&' }
    if (opc.lado !== 2) { link += 'l=' + opc.lado + '&' }
    if (opc.addon1) { link += 'a1&' }
    if (opc.addon2) { link += 'a2&' }
    if (opc.h !== 78) { link += 'h=' + opc.h + '&' }
    if (opc.p !== 69) { link += 'p=' + opc.p + '&' }
    if (opc.s !== 58) { link += 's=' + opc.s + '&' }
    if (opc.d !== 76) { link += 'd=' + opc.d + '&' }
    if (opc.animar) { link += 'an&' }
    if (opc.quadros) { link += 'q&' }
    if (opc.aa) { link += 'aa&' }
    if (opc.hp) { link += 'hp&' }
    if (opc.nome) { link += 'n=' + encodeURI(opc.nome) + '&' }
    $('.link').val(link.slice(0, -1))
    $('.link').parent().toggle(opc.outfit.id != 9999)
  }

  function resetParametros () {
    opc.outfit = outfits.find(d => d.id === 128)
    opc.montaria = montarias.find(d => d.id === 0)
    opc.lado = 2
    opc.addon1 = false
    opc.addon2 = false
    opc.h = 78
    opc.p = 69
    opc.s = 58
    opc.d = 76
    opc.aa = false
    opc.animar = false
    opc.quadros = false
    opc.hp = false
    opc.nome = ''
    opc.sw = 64
    opc.sh = 64
    opc.largura = 64
    opc.altura = 64
    opc.largura_nome = 0
  }

  function obterParametros () {
    resetParametros()
    const params = new URLSearchParams($output.attr('data-src') || window.location.href)
    if (params.has('o')) { opc.outfit = outfits.find(o => o.id.toString() === params.get('o')) }
    if (params.has('m')) { opc.montaria = montarias.find(m => m.id.toString() === params.get('m')) }
    if (params.has('a1')) { opc.addon1 = true }
    if (params.has('a2')) { opc.addon2 = true }
    if (params.has('l')) { opc.l = params.get('l') }
    if (params.has('h')) { opc.h = params.get('h') }
    if (params.has('p')) { opc.p = params.get('p') }
    if (params.has('s')) { opc.s = params.get('s') }
    if (params.has('d')) { opc.d = params.get('d') }
    if (params.has('aa')) { opc.aa = true }
    if (params.has('an')) { opc.animar = true }
    if (params.has('q')) { opc.quadros = true }
    if (params.has('hp')) { opc.hp = true }
    if (params.has('n')) { opc.nome = decodeURI(params.get('n')) }
  }

  function preencherListas () {
    $form_outfits.empty()
    let dif = true
    for (const outfit of outfits.filter(o => o.type === 'outfit')) {
      $form_outfits.append('<div class="janela_opcao_out' + (dif ? '_dif' : '') + '"><input id="o' + outfit.id + '" type="radio" name="outfit" value="' + outfit.id + '"><label for="o' + outfit.id + '">' + outfit.name + '</label></div>')
      dif = !dif
    }
    $form_outros.empty()
    dif = true
    for (const outfit of outfits.filter(o => o.type === 'other')) {
      $form_outros.append('<div class="janela_opcao_out' + (dif ? '_dif' : '') + '"><input id="o' + outfit.id + '" type="radio" name="outfit" value="' + outfit.id + '"><label for="o' + outfit.id + '">' + outfit.name + '</label></div>')
      dif = !dif
    }
    $form_montarias.empty()
    dif = true
    for (const montaria of montarias) {
      $form_montarias.append('<div class="janela_opcao_mount' + (dif ? '_dif' : '') + '"><input id="m' + montaria.id + '" type="radio" name="montaria" value="' + montaria.id + '"><label for="m' + montaria.id + '">' + montaria.name + '</label></div>')
      dif = !dif
    }
  }

  /* Eventos */

  if (!com_tela) return

  $form_outfits.on('change', function () {
    $form_outros.find('input[name=outfit]:checked').prop('checked', false)
    opc.outfit = outfits.find(o => o.id.toString() === $('input[name=outfit]:checked').val())
    atualizar()
  })

  $form_outros.on('change', function () {
    $form_outfits.find('input[name=outfit]:checked').prop('checked', false)
    opc.outfit = outfits.find(o => o.id.toString() === $('input[name=outfit]:checked').val())
    atualizar()
  })

  $form_montarias.on('change', function () {
    opc.montaria = montarias.find(m => m.id.toString() === $('input[name=montaria]:checked').val())
    atualizar()
  })

  $('#botao_gif').on('click', function () {
    salvarGif()
  })

  $('#botao_up').on('click', function () {
    uploadGif()
  })

  $('#out_e').on('click', function () {
    const idx = outfits.indexOf(opc.outfit)
    if (idx !== 0) opc.outfit = outfits.at(idx - 1)
    else opc.outfit = outfits[outfits.length - 1]
    $('input[value="' + opc.outfit.id + '"]').prop('checked', true)
    atualizar()
  })

  $('#out_d').on('click', function () {
    const idx = outfits.indexOf(opc.outfit)
    if (idx !== outfits.length - 1) opc.outfit = outfits.at(idx + 1)
    else opc.outfit = outfits[0]
    $('input[value="' + opc.outfit.id + '"]').prop('checked', true)
    atualizar()
  })

  $('#mount_e').on('click', function () {
    const idx = montarias.indexOf(opc.montaria)
    if (idx !== 0) opc.montaria = montarias.at(idx - 1)
    else opc.montaria = montarias[montarias.length - 1]
    $('input[value="' + opc.montaria.id + '"]').prop('checked', true)
    atualizar()
  })

  $('#mount_d').on('click', function () {
    const idx = montarias.indexOf(opc.montaria)
    if (idx !== montarias.length - 1) opc.montaria = montarias.at(idx + 1)
    else opc.montaria = montarias[0]
    $('input[value="' + opc.montaria.id + '"]').prop('checked', true)
    atualizar()
  })

  $('.seta_cima').on('click', function () {
    opc.lado = 0
    atualizar()
  })

  $('.seta_direita').on('click', function () {
    opc.lado = 1
    atualizar()
  })

  $('.seta_baixo').on('click', function () {
    opc.lado = 2
    atualizar()
  })

  $('.seta_esquerda').on('click', function () {
    opc.lado = 3
    atualizar()
  })

  $addon_1.on('click', function () {
    opc.addon1 = !opc.addon1
    atualizar()
  })

  $addon_2.on('click', function () {
    opc.addon2 = !opc.addon2
    atualizar()
  })

  $aa.on('click', function () {
    opc.aa = $aa.prop('checked')
    atualizar()
  })

  $animar.on('click', function () {
    opc.animar = $animar.prop('checked')
    atualizar()
  })

  $quadros.on('click', function () {
    opc.quadros = $quadros.prop('checked')
    atualizar()
  })

  $vida.on('click', function () {
    opc.hp = $vida.prop('checked')
    atualizar()
  })

  $nome_char.on('keyup', function () {
    opc.nome = $('.nome_char').val()
    atualizar()
  })

  /* Seleção de Cores */

  function tabelaCores () {
    let cont = 0
    const $quadrado_cor = $('.tabela_cores').children('tbody').children('tr').children('td')

    $quadrado_cor.each(function () {
      $(this).attr('id', 'c' + cont)
      $(this).css('background', 'rgb(' + cores[cont][0] + ', ' + cores[cont][1] + ', ' + cores[cont][2] + ')')
      cont++
    })

    $quadrado_cor.on('click', function () {
      $quadrado_cor.removeClass('cor_selecionada')
      $(this).addClass('cor_selecionada')
      opc[$('.selecionado').attr('id')[0]] = parseInt($(this).attr('id').substring(1))
      atualizar()
    })

    $('.seletores').on('click', function () {
      $('.selecionado').removeClass('selecionado')
      $(this).addClass('selecionado')
      $quadrado_cor.removeClass('cor_selecionada')
      $('#c' + opc[$(this).attr('id')[0]]).addClass('cor_selecionada')
    })

    $('#head').click()
  }

  /* Artworks */

  function artworks () {
    const $artwork_e = $('#artwork_e')
    const $artwork_d = $('#artwork_d')
    const artwork_A = [['BlackStag', 75, 0, 230], ['ConqueringLion', 85, 7, 280], ['Scorpion', 80, 0, 250], ['RiftRunner', 95, 0, 290]][RandomInt(0, 3)]
    const artwork_B = [['ArmoredWarhorse', 70, 0, 240], ['LadyBug', 70, -5, 240], ['Walker', 70, -5, 230]][RandomInt(0, 2)]
    const artwork_C = [['Dragonling', 85, 0, 300], ['Dragonling2', 90, 0, 300], ['Waccoon', 105, -15, 300]][RandomInt(0, 2)]
    const artwork_D = [['BatCat', 105, -20, 300], ['CreepingHand', 80, -5, 250], ['PanzerBird', 72, 0, 230], ['MantaRay', 105, -15, 300]][RandomInt(0, 3)]
    $artwork_e.append('<img class="artwork" id="artwork_A" alt="' + artwork_A[0] + '" style="width: ' + artwork_A[1] + '%; max-width: ' + artwork_A[3] + 'px;   top: ' + artwork_A[2] + 'px;" src="' + assetsBaseURL + 'Outfiter_Artwork_' + artwork_A[0] + '.png">')
    $artwork_d.append('<img class="artwork" id="artwork_B" alt="' + artwork_B[0] + '" style="width: ' + artwork_B[1] + '%; max-width: ' + artwork_B[3] + 'px;   top: ' + artwork_B[2] + 'px;" src="' + assetsBaseURL + 'Outfiter_Artwork_' + artwork_B[0] + '.png">')
    $artwork_e.append('<img class="artwork" id="artwork_C" alt="' + artwork_C[0] + '" style="width: ' + artwork_C[1] + '%; max-width: ' + artwork_C[3] + 'px; right: ' + artwork_C[2] + 'px;" src="' + assetsBaseURL + 'Outfiter_Artwork_' + artwork_C[0] + '.png">')
    $artwork_d.append('<img class="artwork" id="artwork_D" alt="' + artwork_D[0] + '" style="width: ' + artwork_D[1] + '%; max-width: ' + artwork_D[3] + 'px;  left: ' + artwork_D[2] + 'px;" src="' + assetsBaseURL + 'Outfiter_Artwork_' + artwork_D[0] + '.png">')

    function RandomInt (min, max) {
      return Math.floor(Math.random() * (max - min + 1)) + min
    }
  }
}