Module:Dynmap: Difference between revisions
From Colonia Wiki
Created page with "local p = {} --p stands for package function p.hello( frame ) return "Hello, world!" end return p" |
No edit summary |
||
(7 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
local p = {} --p | local p = {} -- package | ||
bit32 = require( 'bit32' ) | |||
-- worldtomap | |||
p.wtms = { world = {}, world_nether = {}, world_the_end = {}}; | |||
p.wtms.world.flat = {4, 0, -2.4492935982947064e-16, -2.4492935982947064e-16, 0, -4, 0, 1, 0} | |||
p.wtms.world.t = {1.31370849898476, 0, -11.313708498984761, -5.6568542494923815, 13.856406460551018, -5.656854249492381, 5.551115123125782e-17, 0.9999999999999997, 5.551115123125782e-17 }; | |||
-- mapzoomout | |||
p.mapzoomouts = { world = {}, world_nether = {}, world_the_end = {}}; | |||
p.mapzoomouts.world.flat = 5; | |||
p.mapzoomouts.world.t = 5; | |||
function p.getWorldToMap(world, map) | |||
return p.wtms[world][map] | |||
end | |||
function p.getMapZoomOut(world, map) | |||
return p.mapzoomouts[world][map] | |||
end | |||
function p.gameCoordsToTile(coords, world, map) | |||
local wtm = p.getWorldToMap(world, map) | |||
local mapzoomout = p.getMapZoomOut(world, map) | |||
local lat = wtm[4] * coords.x + wtm[5] * coords.y + wtm[6] * coords.z; | |||
local lng = wtm[1] * coords.x + wtm[2] * coords.y + wtm[3] * coords.z; | |||
local results = {}; | |||
-- results.x = -((128 - lat) / (1 << mapzoomout)); | |||
results.x = -((128 - lat) / (bit32.lshift(1, mapzoomout))); | |||
-- results.z = lng / (1 << mapzoomout); | |||
results.z = lng / (bit32.lshift(1, mapzoomout)); | |||
results.y = coords.y; | |||
return results; | |||
end | |||
function p.zoomPrefix(level) | |||
-- repeat "z" for each zoom level | |||
local prefix = ""; | |||
for i = 1, level do | |||
prefix = prefix .. "z"; | |||
end | |||
return prefix; | |||
end | |||
function p.tileToPath(tile, zoom, world, map) | |||
local scale = bit32.lshift(1, zoom) | |||
local x = scale * tile.x; | |||
local z = scale * tile.z; | |||
print("x:" .. x .. " z:" .. z) | |||
local scaledx = bit32.rshift(math.floor(x), zoom); | |||
local scaledz = bit32.rshift(math.floor(z), zoom); | |||
print("scaledx:" .. scaledx .. " scaledz:" .. scaledz) | |||
-- /map/tiles/<world>/<map>/<scaledx>_<scaledz>/<zoom prefix>_<x>_<z>.png | |||
local path = "/map/tiles/" .. world .. "/" .. map .. "/" .. scaledx .. "_" .. scaledz .. "/" .. p.zoomPrefix(zoom) .. "_" .. x .. "_" .. z .. ".png"; | |||
return path; | |||
end | |||
function p.dynmapImageForCoords(world, map, x, z, zoom) | |||
local tile = p.gameCoordsToTile({x = x, y = 64, z = z}, world, map); | |||
local path = p.tileToPath(tile, zoom, world, map); | |||
return path; | |||
end | |||
function p.hello( frame ) | function p.hello( frame ) |
Latest revision as of 00:31, 8 March 2024
TODO documentation
Lua error at line 19: attempt to index field '?' (a nil value).
local p = {} -- package
bit32 = require( 'bit32' )
-- worldtomap
p.wtms = { world = {}, world_nether = {}, world_the_end = {}};
p.wtms.world.flat = {4, 0, -2.4492935982947064e-16, -2.4492935982947064e-16, 0, -4, 0, 1, 0}
p.wtms.world.t = {1.31370849898476, 0, -11.313708498984761, -5.6568542494923815, 13.856406460551018, -5.656854249492381, 5.551115123125782e-17, 0.9999999999999997, 5.551115123125782e-17 };
-- mapzoomout
p.mapzoomouts = { world = {}, world_nether = {}, world_the_end = {}};
p.mapzoomouts.world.flat = 5;
p.mapzoomouts.world.t = 5;
function p.getWorldToMap(world, map)
return p.wtms[world][map]
end
function p.getMapZoomOut(world, map)
return p.mapzoomouts[world][map]
end
function p.gameCoordsToTile(coords, world, map)
local wtm = p.getWorldToMap(world, map)
local mapzoomout = p.getMapZoomOut(world, map)
local lat = wtm[4] * coords.x + wtm[5] * coords.y + wtm[6] * coords.z;
local lng = wtm[1] * coords.x + wtm[2] * coords.y + wtm[3] * coords.z;
local results = {};
-- results.x = -((128 - lat) / (1 << mapzoomout));
results.x = -((128 - lat) / (bit32.lshift(1, mapzoomout)));
-- results.z = lng / (1 << mapzoomout);
results.z = lng / (bit32.lshift(1, mapzoomout));
results.y = coords.y;
return results;
end
function p.zoomPrefix(level)
-- repeat "z" for each zoom level
local prefix = "";
for i = 1, level do
prefix = prefix .. "z";
end
return prefix;
end
function p.tileToPath(tile, zoom, world, map)
local scale = bit32.lshift(1, zoom)
local x = scale * tile.x;
local z = scale * tile.z;
print("x:" .. x .. " z:" .. z)
local scaledx = bit32.rshift(math.floor(x), zoom);
local scaledz = bit32.rshift(math.floor(z), zoom);
print("scaledx:" .. scaledx .. " scaledz:" .. scaledz)
-- /map/tiles/<world>/<map>/<scaledx>_<scaledz>/<zoom prefix>_<x>_<z>.png
local path = "/map/tiles/" .. world .. "/" .. map .. "/" .. scaledx .. "_" .. scaledz .. "/" .. p.zoomPrefix(zoom) .. "_" .. x .. "_" .. z .. ".png";
return path;
end
function p.dynmapImageForCoords(world, map, x, z, zoom)
local tile = p.gameCoordsToTile({x = x, y = 64, z = z}, world, map);
local path = p.tileToPath(tile, zoom, world, map);
return path;
end
function p.hello( frame )
return "Hello, world!"
end
return p