Module:CoordinatesConverter

local p = {} p.coordinates = "0" p.glyphs = "0"

function p.coordinatesToGlyphs(frame) if (string.len(frame.args[1]) ~= 19) then return "0" end

local X_Z_POS_SHIFT = 2049 local X_Z_NEG_SHIFT = 2047 local Y_POS_SHIFT = 129 local Y_NEG_SHIFT = 127

local x_coords = tonumber(mw.ustring.sub(frame.args[1], 1, 4), 16) local y_coords = tonumber(mw.ustring.sub(frame.args[1], 6, 9), 16) local z_coords = tonumber(mw.ustring.sub(frame.args[1], 11, 14), 16) local system_idx = tonumber(mw.ustring.sub(frame.args[1], 16, 19), 16)

local portal_x = 0 local portal_y = 0 local portal_z = 0 if(x_coords < X_Z_NEG_SHIFT) then portal_x = x_coords + X_Z_POS_SHIFT else portal_x = x_coords - X_Z_NEG_SHIFT end if(z_coords < X_Z_NEG_SHIFT) then portal_z = z_coords + X_Z_POS_SHIFT else portal_z = z_coords - X_Z_NEG_SHIFT end if(y_coords < Y_NEG_SHIFT) then portal_y = y_coords + Y_POS_SHIFT else portal_y = y_coords - Y_NEG_SHIFT end

local glyphs = {} glyphs[1] = '1' glyphs[2] = string.format('%03X', system_idx) glyphs[3] = string.format('%02X', portal_y) glyphs[4] = string.format('%03X', portal_z) glyphs[5] = string.format('%03X', portal_x) return table.concat(glyphs, "") end

function p.glyphsToCoordinates(frame) if (string.len(frame.args[1]) ~= 12) then return "0" end

local X_Z_POS_SHIFT = 2049 local X_Z_NEG_SHIFT = 2047 local Y_POS_SHIFT = 129 local Y_NEG_SHIFT = 127

local x_glyphs = tonumber(mw.ustring.sub(frame.args[1], 10, 12), 16) local y_glyphs = tonumber(mw.ustring.sub(frame.args[1], 5, 6), 16) local z_glyphs = tonumber(mw.ustring.sub(frame.args[1], 7, 9), 16) local system_idx = tonumber(mw.ustring.sub(frame.args[1], 2, 4), 16)

local coords_x = 0 local coords_y = 0 local coords_z = 0 if(x_glyphs >= X_Z_POS_SHIFT) then coords_x = x_glyphs - X_Z_POS_SHIFT else coords_x = x_glyphs + X_Z_NEG_SHIFT end if(z_glyphs >= X_Z_POS_SHIFT) then coords_z = z_glyphs - X_Z_POS_SHIFT else coords_z = z_glyphs + X_Z_NEG_SHIFT end if(y_glyphs >= Y_POS_SHIFT) then coords_y = y_glyphs - Y_POS_SHIFT else coords_y = y_glyphs + Y_NEG_SHIFT end

local coordinates = {} coordinates[1] = string.format('%04X', coords_x) coordinates[2] = string.format('%04X', coords_y) coordinates[3] = string.format('%04X', coords_z) coordinates[4] = string.format('%04X', system_idx) return table.concat(coordinates, ":") end

return p