Eu sinceramente acho injustiça chamar um pacote com duas funções de biblioteca, mas...
Isso foi uma ideia do Oneshot. Serve para rotacionar tabelas (por exemplo, se você quiser rotacionar uma área de uma magia). Eu sinceramente não vejo muita utilidade em open tibia, mas ele jura que serve para alguma coisa e me disse para postar, então aqui está:
matrix.lua
-- Copyright (c) 2013 Skyen Hasus -- -- Permission is hereby granted, free of charge, to any person obtaining a copy -- of this software and associated documentation files (the "Software"), to deal -- in the Software without restriction, including without limitation the rights -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -- copies of the Software, and to permit persons to whom the Software is -- furnished to do so, subject to the following conditions: -- -- The above copyright notice and this permission notice shall be included in -- all copies or substantial portions of the Software. module("matrix", package.seeall) _G.DEGREES_0 = 0 _G.DEGREES_90 = 90 _G.DEGREES_180 = 180 _G.DEGREES_270 = 270 _G.DIRECTION_VERTICAL = 0 _G.DIRECTION_HORIZONTAL = 1 local function rotate_90(matrix) local ret = {} for y in ipairs(matrix) do local w = #matrix[y] for x, v in ipairs(matrix[y]) do if not ret[w-x+1] then ret[w-x+1] = {} end ret[w-x+1][y] = v end end return ret end local function rotate_180(matrix) local ret = {} local h = #matrix for y in ipairs(matrix) do local w = #matrix[y] for x, v in ipairs(matrix[y]) do if not ret[h-y+1] then ret[h-y+1] = {} end ret[h-y+1][w-x+1] = v end end return ret end local function rotate_270(matrix) local ret = {} local h = #matrix for y in ipairs(matrix) do for x, v in ipairs(matrix[y]) do if not ret[x] then ret[x] = {} end ret[x][h-y+1] = v end end return ret end local function mirror_v(matrix) local ret = {} local h = #matrix for y in ipairs(matrix) do for x, v in ipairs(matrix[y]) do if not ret[h-y+1] then ret[h-y+1] = {} end ret[h-y+1][x] = v end end return ret end local function mirror_h(matrix) local ret = {} for y in ipairs(matrix) do local w = #matrix[y] for x, v in ipairs(matrix[y]) do if not ret[y] then ret[y] = {} end ret[y][w-x+1] = v end end return ret end function rotate(matrix, degrees) degrees = degrees % 360 if degrees == DEGREES_0 then return matrix elseif degrees == DEGREES_90 then return rotate_90(matrix) elseif degrees == DEGREES_180 then return rotate_180(matrix) elseif degrees == DEGREES_270 then return rotate_270(matrix) end error("Invalid degree value to function 'rotate'.", 2) return false end function mirror(matrix, direction) if direction == DIRECTION_VERTICAL then return mirror_v(matrix) elseif direction == DIRECTION_HORIZONTAL then return mirror_h(matrix) end error("Invalid direction to function 'mirror'.", 2) return false end
Exemplo de uso:
require("matrix") local array = { {1, 1, 2}, {0, 1, 0}, {0, 1, 0}, {3, 1, 4}, } local array_vertical = matrix.mirror(array, DIRECTION_VERTICAL) local array_horizontal = matrix.mirror(array, DIRECTION_HORIZONTAL) local array_90 = matrix.rotate(array, DEGREES_90) local array_180 = matrix.rotate(array, DEGREES_180) local array_270 = matrix.rotate(array, DEGREES_270) local array_vertical = matrix.mirror(array, 0) local array_horizontal = matrix.mirror(array, 1) local array_90 = matrix.rotate(array, 90) local array_180 = matrix.rotate(array, 180) local array_270 = matrix.rotate(array, 270)
Funções e parâmetros:
(Apesar de conter sete funções, apenas duas delas são públicas e você pode usar.)
matrix.rotate(matrix, degrees)
Rotaciona uma matriz, onde matrix é a matriz a ser rotacionada e degrees é o ângulo da rotação. Apenas aceita ângulos "quadrados" (múltiplos de 90): -180, -90, 0, 90, 180, 270, 360, 450, e por ai vai... Você pode usar DEGREES_0, DEGREES_90, DEGREES_180 e DEGREES_270 também. A função retorna uma nova tabela, com a rotação aplicada.
matrix.mirror(matrix, direction)
Espelha uma matriz, onde matrix é a matriz a ser espelhada e direction é o sentido do espelhamento. Apenas aceita os valores DIRECTION_VERTICAL (0, vertical) e DIRECTION_HORIZONTAL (1, horizontal). A função retorna uma nova tabela, com o espelhamento aplicado.
Você precisa dar o nome do arquivo de matrix.lua, por ser um módulo. Se for usar fora de open tibia, você precisa dar require("matrix") para importar o módulo. Se for usar no open tibia, basta jogar o matrix.lua na pasta lib.