Module:Dynmap: Difference between revisions

From Colonia Wiki
No edit summary
Add missing return
Line 71: Line 71:
     return "Hello, world!"
     return "Hello, world!"
end
end
return p

Revision as of 21:18, 6 March 2024

TODO documentation

Lua error at line 17: attempt to index field '?' (a nil value).


local p = {} -- package

-- worldtomap
local wtms = { world = {}, world_nether = {}, world_the_end = {}};

wtms.world.flat = {4, 0, -2.4492935982947064e-16, -2.4492935982947064e-16, 0, -4, 0, 1, 0}
wtms.world.t = {1.31370849898476, 0, -11.313708498984761, -5.6568542494923815, 13.856406460551018, -5.656854249492381, 5.551115123125782e-17, 0.9999999999999997, 5.551115123125782e-17 };

-- mapzoomout

local mapzoomouts = { world = {}, world_nether = {}, world_the_end = {}};

mapzoomouts.world.flat = 5;
mapzoomouts.world.t = 5;

function p.getWorldToMap(world, map)
    return wtms[world][map]
end

function p.getMapZoomOut(world, map)
    return 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