Mapper Menu Add-Ons Script

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

Mapper Menu Add-Ons Script

Post by Jor'Mox »

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: 1034
Joined: Mon Mar 04, 2013 3:40 pm
Location: Deepest Wiltshire, UK
Discord: SlySven#2703

Re: Mapper Menu Add-Ons Script

Post by SlySven »

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: 1153
Joined: Wed Apr 03, 2013 2:19 am

Re: Mapper Menu Add-Ons Script

Post by Jor'Mox »

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 »

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

Eraene
Posts: 42
Joined: Wed Nov 27, 2013 10:18 pm

Re: Mapper Menu Add-Ons Script

Post by Eraene »

Is there an updated version of Teleport Here that replicates proper movement to a room, rather than just using centerview() ?

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

Re: Mapper Menu Add-Ons Script

Post by Jor'Mox »

You can just double click on the room to move there normally, it is a base part of how the mapper works. No need for any of this in order to do that.

Eraene
Posts: 42
Joined: Wed Nov 27, 2013 10:18 pm

Re: Mapper Menu Add-Ons Script

Post by Eraene »

You are misunderstanding me. I need the mapper's position to move, not myself. Using centerview does not properly move the mapper's position, as the next time you move makes the mapper think you are moving from the previous position, not the new centerview position.

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

Re: Mapper Menu Add-Ons Script

Post by SlySven »

Do you want a Lua API to be able to position the centre of the map at a specific position (will set the (int) T2DMap::mOx, (int) T2DMap::mOy and (int) T2DMap::mOz variables directly - and/or the corresponding ones in the 3D map) or is it sufficient to be able to replicate the behaviour of the 6 panning buttons on the mapper display?

It ought to be possible to code either - but the former is harder as it needs to account for which ever map is currently being shown. I have thought about a related function in the past that would return the current position (if there is one) as well as which map (2 or 3) is on display - then the latter controls could be used to adjust the position. Though a proposal such as this might need to take care that such automation of the display can be turned off should a broken script cause the map display to become unusable...

Eraene
Posts: 42
Joined: Wed Nov 27, 2013 10:18 pm

Re: Mapper Menu Add-Ons Script

Post by Eraene »

I... have no idea what you just said, and I don't think it's anywhere close to what I want to do. But I'm just going to let this go, as I've already written a github issue on it which is hopefully more clear than my post apparently was.

Post Reply