Mapper Menu Add-Ons Script

Share your scripts and packages with other Mudlet users.
Post Reply
Jor'Mox
Posts: 1099
Joined: Wed Apr 03, 2013 2:19 am

Mapper Menu Add-Ons Script

Post by Jor'Mox » Sun Sep 30, 2018 6:59 pm

After realizing that this was a thing, I made a few add-ons for the mapper menu that I thought would be useful, including one to "safe delete" which leaves exit stubs on connected rooms instead of just deleting the exit entirely, one to "shift rooms" which lets you use the keypad to move selected rooms around on the map (1 = sw, 2 = s, 3 = se, ... 9 = ne, + = d, - = u, and 5 = finished), and one to "teleport here" which basically just does centerview on that room.

Just drop it into a script and you should be good to go.
Code: [show] | [select all] lua
mapAddOns = mapAddOns or {}

local function safeDelete(ids)
    for _, id in ipairs(ids) do
        id = tonumber(id)
        local rooms = {}
        for _, room in ipairs(getAllRoomEntrances(id)) do
            for dir, exit in pairs(getRoomExits(room)) do
                if exit == id then
                    if rooms[room] then
                        if type(rooms[room]) == "table" then
                            table.insert(rooms[room],dir)
                        else
                            rooms[room] = {rooms[room], dir}
                        end
                    else
                        rooms[room] = dir
                    end
                end
            end
        end
        deleteRoom(id)
        for rid, dirs in pairs(rooms) do
            if type(dirs) == "table" then
                for _, dir in ipairs(dirs) do
                    setExitStub(rid, dir, true)
                end
            else
                setExitStub(rid, dirs, true)
            end
        end
    end
    updateMap()
end

local keys = {['1'] = 'sw', ['2'] = 's', ['3'] = 'se', ['4'] = 'w', ['5'] = 'done', ['6'] = 'e', ['7'] = 'nw',
    ['8'] = 'n', ['9'] = 'ne', ['Plus'] = 'd', ['Minus'] = 'u'}
mapAddOns.keysEnabled = false
mapAddOns.keyIds = mapAddOns.keyIds or {}
mapAddOns.roomIds = mapAddOns.roomIds or {}

local function toggleMapKeys()
    mapAddOns.keysEnabled = not mapAddOns.keysEnabled
    if mapAddOns.keysEnabled then
        for key, val in pairs(keys) do
            table.insert(mapAddOns.keyIds, tempKey(mudlet.keymodifier.Keypad, mudlet.key[key],
                string.format("raiseEvent('tempMapKey','%s')",val)))
        end
    else
        for _,id in ipairs(mapAddOns.keyIds) do
            killKey(id)
        end
        mapAddOns.keyIds = {}
    end
end

local coordmap = {
    n = {0,1,0},    ne = {1,1,0},   nw = {-1,1,0},  e = {1,0,0},    w = {-1,0,0},
    s = {0,-1,0},   se = {1,-1,0},  sw = {-1,-1,0}, u = {0,0,1},    d = {0,0,-1},
}

local function shiftRooms(dir)
    local dx,dy,dz = unpack(coordmap[dir])
    for _,id in ipairs(mapAddOns.roomIds) do
        local x,y,z = getRoomCoordinates(id)
        setRoomCoordinates(id,x+dx,y+dy,z+dz)
    end
    updateMap()
end

function mapAddOns.eventHandler(event, mapEvent, ...)
    if event == "mapAddOnEvent" then
        if mapEvent == "safeDelete" then
            safeDelete(arg)
        elseif mapEvent == "shiftRooms" then
            mapAddOns.roomIds = arg
            toggleMapKeys()
            cecho("\n<green>Please use the keypad keys to shift the selected rooms around.<reset>")
        elseif mapEvent == "teleportHere" then
            if arg.n == 1 then
                centerview(arg[1])
            else
                cecho("\n<red>Cannot teleport to multiple rooms at once.<reset>")
            end
        end
    elseif event == "tempMapKey" and mapAddOns.keysEnabled then
        if mapEvent == "done" then
            toggleMapKeys()
            mapAddOns.roomIds = {}
            cecho("\n<green>Room shifting completed.<reset>")
        else
            shiftRooms(mapEvent)
        end
    elseif event == "mapOpenEvent" then
        local events = {safeDelete = "safe delete", shiftRooms = "shift rooms", teleportHere = "teleport here"}
        local mapEvents = getMapEvents()
        for event, name in pairs(events) do
            if not mapEvents[event] then
                addMapEvent(event, "mapAddOnEvent","",name)
            end
        end
    end
end

registerAnonymousEventHandler("mapAddOnEvent","mapAddOns.eventHandler")
registerAnonymousEventHandler("tempMapKey","mapAddOns.eventHandler")
registerAnonymousEventHandler("mapOpenEvent","mapAddOns.eventHandler")
Edit: I forgot, this adds the new menu items when a mapper window is first opened. So the easiest way to get them working is to close the current profile after saving this to a script and then reopen it.

User avatar
SlySven
Posts: 919
Joined: Mon Mar 04, 2013 3:40 pm
Location: Deepest Wiltshire, UK
Discord: SlySven#2703

Re: Mapper Menu Add-Ons Script

Post by SlySven » Sat Oct 13, 2018 11:40 pm

Are you sure that the function function mapAddOns.eventHandler(event, mapEvent, ...) works as you anticipate - the conversion of the ... varadic argument (all the remaining arguments, if any, from the caller) to that arg table was only a feature of Lua 5.0 and Mudlet is built on Lua 5.1 - to access the values passed in the ... part you use select("#", ...) to get a count of the number of arguments it contains and then a numeric index between 1 and that total in another call to select - which returns a table containing the specified indexed value and all the subsequent values though the latter can easy be dropped with an assignment operation - see the select entry in the PIL 5.1 – Basic Functions entry. You can however recreate the Lua 5.0 table with

Code: Select all

local arg = select(1, ...)
before you use arg in that function...

Jor'Mox
Posts: 1099
Joined: Wed Apr 03, 2013 2:19 am

Re: Mapper Menu Add-Ons Script

Post by Jor'Mox » Sun Oct 14, 2018 1:06 am

Well, I can't speak to what should or shouldn't work with whatever version of lua, but I can say definitively that the code as written works in the most recent version of Mudlet, and that general setup has worked for every version of Mudlet I have used, since 2.1. So, yes, I'm sure.

jieiku
Posts: 33
Joined: Fri Mar 01, 2019 1:25 pm
Contact:

Re: Mapper Menu Add-Ons Script

Post by jieiku » Sun Mar 03, 2019 4:59 am

Thank you for this script, the safeDelete feature is exactly what I was missing to make mapping easier.

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests