Advertisement
Guest User

Untitled

a guest
Jan 16th, 2012
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 24.93 KB | None | 0 0
  1. mmp.game = false
  2.  
  3. function mmp.echo(what)
  4.   moveCursorEnd("main") if getCurrentLine() ~= "" then echo"\n" end
  5.   decho("<112,229,0>(<73,149,0>mapper<112,229,0>): <255,255,255>")
  6.   cecho(what)
  7.   echo("\n")
  8. end
  9.  
  10. function mmp.echon(what)
  11.   moveCursorEnd("main") if getCurrentLine() ~= "" then echo"\n" end
  12.   decho("<112,229,0>(<73,149,0>mapper<112,229,0>): <255,255,255>")
  13.   cecho(what)
  14. end
  15.  
  16. function mmp.deleteLineP()
  17.   deleteLine()
  18.   tempLineTrigger(1,1,[[
  19.     if isPrompt() then deleteLine() end
  20.   ]])
  21. end
  22.  
  23. function mmp.pause(what)
  24.   assert(what == nil or what == "on" or what == "off", "mmp.pause wants 'on', 'off' or nothing as an argument")
  25.  
  26.   if what == "on" or (what == nil and not mmp.paused) then
  27.     mmp.paused = true
  28.   elseif  what == "off" or (what == nil and mmp.paused) then
  29.     mmp.paused = false
  30.   end
  31.  
  32.   mmp.echo("Speedwalking " .. (mmp.paused and "paused" or "unpaused") .. ".")
  33.   if not mmp.paused then mmp.move() end
  34. end
  35.  
  36. function mmp.getnums(roomname)
  37.   local t = mmp.searchRoom(roomname)
  38.  
  39.   if not next(t) then
  40.     return nil end
  41.  
  42.   local result = {}
  43.  
  44.   if not tonumber(select(2, next(t))) then
  45.     for roomid,_ in pairs(t) do
  46.       if roomid ~= 0 then result[#result+1] = tonumber(roomid) end
  47.     end
  48.   else
  49.     for _,roomid in pairs(t) do
  50.       if roomid ~= 0 then result[#result+1] = tonumber(roomid) end
  51.     end
  52.   end
  53.  
  54.   return result
  55. end
  56.  
  57. -- for a given room name, we'll echo all the vnums
  58. function mmp.echonums(roomname, exact)
  59.   local t = mmp.searchRoom(roomname)
  60.  
  61.   if not next(t) then
  62.     echo "?" return nil end
  63.  
  64.   -- transform the kv table into a table of tables for cleaner code.
  65.     -- + perhaps Mudlet in future will give this us anyway, sorted by relevancy
  66.   local dt = {}
  67.   if not tonumber(select(2, next(t))) then
  68.     for roomid,room in pairs(t) do
  69.         if not exact then
  70.             dt[#dt+1] = {name = room, id = roomid}
  71.         else
  72.             if room:lower() == roomname:lower() then
  73.                 dt[#dt+1] = {name = room, id = roomid}
  74.             end
  75.         end
  76.     end
  77.   else
  78.     for room,roomid in pairs(t) do
  79.         if not exact then
  80.             dt[#dt+1] = {name = room, id = roomid}
  81.         else
  82.             if room:lower() == roomname:lower() then
  83.                 dt[#dt+1] = {name = room, id = roomid}
  84.             end
  85.         end
  86.     end
  87.   end
  88.  
  89.   -- we can have nothing if we asked for exact match
  90.   if not dt[1] then echo "?" return end
  91.  
  92.   -- display first three ids. Can't really nicely table.concat them.
  93.   echoLink(dt[1].id, 'mmp.gotoRoom('..dt[1].id..')', string.format("Go to %s (%s)", dt[1].id, dt[1].name))
  94.   if not dt[2] then return end
  95.  
  96.   echo", "
  97.   echoLink(dt[2].id, 'mmp.gotoRoom('..dt[2].id..')', string.format("Go to %s (%s)", dt[2].id, dt[2].name))
  98.   if not dt[3] then return end
  99.  
  100.   echo", "
  101.   echoLink(dt[3].id, 'mmp.gotoRoom('..dt[3].id..')', string.format("Go to %s (%s)", dt[3].id, dt[3].name))
  102.   if not dt[4] then return end
  103.  
  104.   echo", ..."
  105. end
  106.  
  107. function mmp.roomEcho(query)
  108.   local result = mmp.searchRoom(query)
  109.  
  110.   if not tonumber(select(2, next(result))) then
  111.     for roomid, roomname in pairs(result) do roomid = tonumber(roomid)
  112.       cecho("<DarkSlateGrey> (<blue>")
  113.       echoLink(roomid, 'mmp.gotoRoom('..roomid..')', string.format("Go to %s (%s)", roomid, tostring(roomname)))
  114.       cecho("<DarkSlateGrey>)")
  115.     end
  116.  
  117.   else
  118.     for roomname, roomid in pairs(result) do roomid = tonumber(roomid)
  119.       cecho("<DarkSlateGrey> (<blue>")
  120.       echoLink(roomid, 'mmp.gotoRoom('..roomid..')', string.format("Go to %s (%s)", roomid, tostring(roomname)))
  121.       cecho("<DarkSlateGrey>)")
  122.     end
  123.   end
  124. end
  125.  
  126.  
  127. -- searchRoom with a cache!
  128. local cache = {}
  129. setmetatable(cache, {__mode = "kv"}) -- weak keys/values = it'll periodically get cleaned up by gc
  130.  
  131. function mmp.searchRoom(what)
  132.   local result = cache[what]
  133.   if not result then
  134.     result = searchRoom(what)
  135.     local realResult = {}
  136.     for key, value in pairs(result) do
  137.         -- both ways, because searchRoom can return either id-room name or the reverse
  138.         if type(key) == "string" then
  139.           realResult[key:ends(" (road)") and key:sub(1, -8) or key] = value
  140.         else
  141.           realResult[key] = value:ends(" (road)") and value:sub(1, -8) or value
  142.         end
  143.     end
  144.     cache[what] = realResult
  145.     result = realResult
  146.   end
  147.   return result
  148. end
  149.  
  150. local getpathcache = {}
  151. setmetatable(getpathcache, {__mode = "kv"}) -- weak keys/values = it'll periodically get cleaned up by gc
  152.  
  153. function mmp.searchRoomExact(what)
  154.   local roomTable = mmp.searchRoom(what)
  155.   local realResult = {}
  156.   for key, value in pairs (roomTable) do
  157.     if (key == what or value == what) then
  158.       if (type(key) == "string") then
  159.         realResult[key:ends(" (road)") and key:sub(1, -8) or key] = value
  160.       else
  161.         realResult[key] = value:ends(" (road)") and value:sub(1, -8) or value
  162.       end
  163.     end
  164.   end
  165.   if (table.is_empty(realResult)) then
  166.     return roomTable
  167.   else
  168.     return realResult
  169.   end
  170. end
  171.  
  172. function mmp.roomWhoFind(query)
  173.   if query:ends('.') then query = query:sub(1,-2) end
  174.   local result = mmp.searchRoomExact(query)
  175.  
  176.   if type(result) == "string" or not next(result) then
  177.     cecho("<CadetBlue>  You have no recollection of any room with that name.") return end
  178.  
  179.   if not tonumber(select(2, next(result))) then -- old style
  180.     for roomid, roomname in pairs(result) do roomid = tonumber(roomid)
  181.       cecho(string.format("  <LightSlateGray>%s<DarkSlateGrey> (<blue>",
  182.         tostring(roomname)))
  183.       echoLink(roomid, 'mmp.gotoRoom('..roomid..')', string.format("Go to %s (%s)", roomid, tostring(roomname)))
  184.       cecho(string.format("<DarkSlateGrey>) <white>%s<DarkSlateGrey>.\n", tostring(mmp.areatabler[getRoomArea(roomid)])))
  185.     end
  186.  
  187.   else -- new style
  188.     for roomname, roomid in pairs(result) do roomid = tonumber(roomid)
  189.       cecho(string.format("  <LightSlateGray>%s<DarkSlateGrey> (<blue>",
  190.         tostring(roomname)))
  191.       echoLink(roomid, 'mmp.gotoRoom('..roomid..')', string.format("Go to %s (%s)", roomid, tostring(roomname)))
  192.       cecho(string.format("<DarkSlateGrey>) <white>%s<DarkSlateGrey>.\n", tostring(mmp.areatabler[getRoomArea(roomid)])))
  193.     end
  194.   end
  195. end
  196.  
  197. -- DOES NOT ACCOUNT FOR CHANGING THE MAP YET (within a profile load), because we don't know when it happens
  198. function mmp.getPath(from, to)
  199.   assert(tonumber(from) and tonumber(to), "mmp.getPath: both from and to have to be room IDs")
  200.  
  201.   local key = string.format("%s_%s", from, to)
  202.   local resulttbl = getpathcache[key]
  203.  
  204.   -- not in cache?
  205.   if not resulttbl then
  206.     local boolean = getPath(from, to)
  207.     -- save it into the cache & send away
  208.     getpathcache[key] = {boolean, speedWalkDir, speedWalkPath}
  209.  
  210.     return boolean
  211.   end
  212.  
  213.   -- or if it is, retrieve & send away
  214.   speedWalkDir = getpathcache[key][2]
  215.   speedWalkPath = getpathcache[key][3]
  216.   return getpathcache[key][1]
  217. end
  218.  
  219. function mmp.roomFind(query)
  220.   if query:ends('.') then query = query:sub(1,-2) end
  221.   local result = mmp.searchRoom(query)
  222.  
  223.   if type(result) == "string" or not next(result) then
  224.     cecho("<grey>You have no recollection of any room with that name.") return end
  225.  
  226.   cecho("<DarkSlateGrey>You know the following relevant rooms::\n")
  227.  
  228. currentRoom = mmp.currentroom
  229.   if not tonumber(select(2, next(result))) then -- old style
  230.     for roomid, roomname in pairs(result) do roomid = tonumber(roomid)
  231.       cecho(string.format("  <LightSlateGray>%s<DarkSlateGrey> (<blue>",
  232.         tostring(roomname)))
  233.       echoLink(roomid, 'mmp.gotoRoom('..roomid..')', string.format("Go to %s (%s)", roomid, tostring(roomname)))
  234.       cecho(string.format("<DarkSlateGrey>) in the <LightSlateGray>%s<DarkSlateGrey>.", tostring(mmp.areatabler[getRoomArea(roomid)])))      
  235.       _G.roomid = roomid
  236.       echoLink(" > Show path\n", [[echoPath(currentRoom, roomid)]], "Display directions" .. roomid, true)
  237.      end
  238.   else -- new style
  239.     for roomname, roomid in pairs(result) do roomid = tonumber(roomid)
  240.       cecho(string.format("  <LightSlateGray>%s<DarkSlateGrey> (<blue>",
  241.         tostring(roomname)))
  242.       echoLink(roomid, 'mmp.gotoRoom('..roomid..')', string.format("Go to %s (%s)", roomid, tostring(roomname)))
  243.       cecho(string.format("<DarkSlateGrey>) in the <LightSlateGray>%s<DarkSlateGrey>.\n", tostring(mmp.areatabler[getRoomArea(roomid)])))
  244.       _G.roomid = roomid
  245.       echoLink(" > Show path\n", [[echoPath(currentRoom, roomid)]], "Display directions", true)  
  246.     end
  247.   end
  248.  
  249.   cecho(string.format("  <DarkSlateGrey>%d rooms found.\n", table.size(result)))
  250. end
  251.  
  252. function mmp.findAreaID(areaname, exact)
  253.   local areaname = areaname:lower()
  254.   local list = getAreaTable()
  255.  
  256.   -- iterate over the list of areas, matching them with substring match.
  257.   -- if we get match a single area, then return it's ID, otherwise return
  258.   -- 'false' and a message that there are than one are matches
  259.   local returnid, fullareaname, multipleareas = nil, nil, {}
  260.   for area, id in pairs(list) do
  261.     if (not exact and area:lower():find(areaname, 1, true)) or (exact and areaname == area:lower()) then
  262.       returnid = id; fullareaname = area; multipleareas[#multipleareas+1] = area
  263.     end
  264.   end
  265.  
  266.   if #multipleareas == 1 then
  267.     return returnid, fullareaname
  268.   else
  269.     return nil, nil, multipleareas
  270.   end
  271. end
  272.  
  273. function mmp.echoRoomList(areaname, exact)
  274.   local id, msg, multiples = mmp.findAreaID(areaname, exact)
  275.   if id then
  276.     local roomlist, endresult = getAreaRooms(id), {}
  277.  
  278.     -- obtain a room list for each of the room IDs we got
  279.     for _, id in pairs(roomlist) do
  280.       endresult[id] = getRoomName(id)
  281.     end
  282.  
  283.   -- sort room IDs so we can display them in order
  284.     table.sort(roomlist)
  285.  
  286.     -- now display something half-decent looking
  287.     cecho(string.format(
  288.       "<DarkSlateGrey>List of all rooms in <grey>%s<DarkSlateGrey> (<grey>%d<DarkSlateGrey>):\n", msg, table.size(endresult)))
  289.     for _, roomid in ipairs(roomlist) do
  290.     local roomname = endresult[roomid]
  291.       fg("blue") echoLink(string.format("%6s", roomid), 'mmp.gotoRoom('..roomid..')', string.format("Go to %s (%s)", roomid, tostring(roomname)), true)
  292.        cecho(string.format("<DarkSlateGrey>: <LightSlateGray>%s<DarkSlateGrey>.\n", roomname))
  293.     end
  294.   elseif not id and #multiples > 0 then
  295.     mmp.echo("For which area would you want to list rooms for?")
  296.  
  297.   fg("DimGrey")
  298.     for _, areaname in ipairs(multiples) do
  299.       echo"  "; setUnderline(true) echoLink(areaname, 'mmp.echoRoomList("'..areaname..'", true)', "Click to view the room list for "..areaname, true) setUnderline(false) echo"\n"
  300.     end
  301.   resetFormat()
  302.   else
  303.     mmp.echo(string.format("Don't know of any area named '%s'.", areaname))
  304.   end
  305. end
  306.  
  307. function mmp.echoAreaList()
  308.   local list = getAreaTable()
  309.   local ids, rlist = {}, {}
  310.   for area, id in pairs(list) do
  311.     if id ~= 0 then ids[#ids+1] = id; rlist[id] = area end end
  312.   table.sort(ids)
  313.  
  314.   cecho(string.format(
  315.       "<DarkSlateGrey>List of all areas we know of (click to view room list):\n"))
  316.   for _, id in pairs(ids) do
  317.     cecho(string.format("<blue>%s%d ", string.rep(" ", (6-#tostring(id))), id))
  318.     fg("DarkSlateGrey") echoLink(rlist[id], 'mmp.echoRoomList("'..rlist[id]..'", true)', "View the room list for "..rlist[id], true) echo("\n")
  319.   end
  320. end
  321.  
  322. function mmp.clearLabels(areaid)
  323.   assert(type(areaid) == "number", "mmp.clearLabels: areaid must be a number")
  324.   local t = getMapLabels(areaid)
  325.   if type(t) ~= "table" then return end
  326.  
  327.   for labelid, _ in pairs(t) do deleteMapLabel(areaid, labelid) end
  328.   mmp.echo(string.format("Cleared all labels in '%s'.", mmp.areatabler[areaid]))
  329. end
  330.  
  331. function mmp.roomLook(input)
  332.  
  333.   -- we can do a report with a number
  334.   local function handle_number(num)
  335.     -- compile all available data
  336.     if not mmp.roomexists(num) then mmp.echo(num.." doesn't seem to exist.") return end
  337.     local s, areanum = pcall(getRoomArea,num)
  338.     if not s then mmp.echo(areanum); return; end
  339.  
  340.     local exits = getRoomExits(num)
  341.     local name = getRoomName(num)
  342.     local islocked = roomLocked(num)
  343.     local weight = (getRoomWeight(num) and getRoomWeight(num) or "?") -- getRoomWeight is buggy in one of the versions, is actually linked to setRoomWeight and thus returns nil
  344.     local coords = {getRoomCoordinates(num)}
  345.     local specexits = getSpecialExits(num)
  346.     local env = getRoomEnv(num)
  347.     local envname = (mmp.envidsr and mmp.envidsr[env]) or "?"
  348.  
  349.     -- generate a report
  350.     mmp.echo(string.format("Room: %s #: %d area: %s (%d)",
  351.       name, num, tostring(mmp.areatabler[areanum]), areanum))
  352.  
  353.     mmp.echo(string.format("Coordinates: x:%d, y:%d, z:%d, locked: %s, weight: %s", coords[1], coords[2], coords[3], (islocked and "yep" or "nope"), tostring(weight)))
  354.  
  355.     mmp.echo(string.format("Environment: %s (%d)", tostring(envname), env))
  356.  
  357.     mmp.echo(string.format("Exits (%d):", table.size(exits)))
  358.     for exit, leadsto in pairs(exits) do
  359.       echo(string.format("  %s -> %s (%d)%s\n", exit, getRoomName(leadsto), leadsto,
  360.         ((getRoomArea(leadsto) or "?") == areanum and ""
  361.         or " (in "..(mmp.areatabler[getRoomArea(leadsto)] or "?")..")")))
  362.     end
  363.  
  364.     -- display special exits if we got any
  365.     if next(specexits) then
  366.       mmp.echo(string.format("Special exits (%d):",table.size(specexits)))
  367.       for leadsto, command in pairs(specexits) do
  368.         echo(string.format("  %s -> %s (%d)\n", command, getRoomName(leadsto), leadsto))
  369.       end
  370.     end
  371.  
  372.     -- actions we can do. This will be a short menu of sorts for actions
  373.     mmp.echo("Stuff you can do:")
  374.     echo"  " echoLink("Clear all labels in this area", 'mmp.clearLabels('..areanum..')', '') echo"\n"
  375.   end
  376.  
  377.   -- see if we can do anything with the name
  378.   local function handle_name(name)
  379.     local result = mmp.searchRoom(name)
  380.  
  381.     if type(result) == "string" then
  382.       cecho("<grey>You have no recollection of any room with that name.") return end
  383.  
  384.     -- if we got one result, then act on it
  385.     if table.size(result) == 1 then
  386.       if type(next(result)) == "number" then
  387.         handle_number(next(result))
  388.       else
  389.         handle_number(select(2, next(result)))
  390.       end
  391.       return
  392.     end
  393.  
  394.     -- if not, then ask the user to clarify which one would they want
  395.     mmp.echo("Which room specifically would you like to look up?")
  396.     if not select(2, next(result)) or not tonumber(select(2, next(result))) then
  397.       for roomid, roomname in pairs(result) do roomid = tonumber(roomid)
  398.         cecho(string.format("  <LightSlateGray>%s<DarkSlateGrey> (<blue>",
  399.           tostring(roomname)))
  400.         echoLink(roomid, 'mmp.roomLook('..roomid..')', string.format("View room details for %s (%s)", roomid, tostring(roomname)))
  401.         cecho(string.format("<DarkSlateGrey>) in the <LightSlateGray>%s<DarkSlateGrey>.\n", tostring(mmp.areatabler[getRoomArea(roomid)])))
  402.       end
  403.  
  404.     else
  405.       for roomname, roomid in pairs(result) do roomid = tonumber(roomid)
  406.         cecho(string.format("  <LightSlateGray>%s<DarkSlateGrey> (<blue>",
  407.           tostring(roomname)))
  408.         echoLink(roomid, 'mmp.roomLook('..roomid..')', string.format("View room details for %s (%s)", roomid, tostring(roomname)))
  409.         cecho(string.format("<DarkSlateGrey>) in the <LightSlateGray>%s<DarkSlateGrey>.\n", tostring(mmp.areatabler[getRoomArea(roomid)])))
  410.       end
  411.     end
  412.   end
  413.  
  414.   if not input then
  415.     if not mmp.roomexists(mmp.currentroom) then
  416.       mmp.echo(mmp.currentroom.. " doesn't seem to be mapped yet.")
  417.     else input = mmp.currentroom end
  418.   end
  419.  
  420.   if tonumber(input) then
  421.     handle_number(tonumber(input))
  422.   else
  423.     handle_name(input)
  424.   end
  425.  
  426.   mmp.echo(string.format("version %s.", tostring(mmp.version)))
  427. end
  428.  
  429. function mmp.roomexists(num)
  430.   if not num then return false end
  431.   if roomExists then return roomExists(num) end
  432.  
  433.   local s,m = pcall(getRoomArea, tonumber(num))
  434.   return (s and true or false)
  435. end
  436.  
  437. -- translates n to north and so forth
  438. function mmp.anytolong(exit)
  439.   local t = {
  440.     n = "north",
  441.     e = "east",
  442.     s = "south",
  443.     w = "west",
  444.     ne = "northeast",
  445.     se = "southeast",
  446.     sw = "southwest",
  447.     nw = "northwest",
  448.     u = "up",
  449.     d = "down",
  450.     i = "in",
  451.     o = "out"
  452.   }
  453.  
  454.   for s, l in pairs(t) do t[l] = l end
  455.  
  456.   return t[exit]
  457. end
  458.  
  459. function mmp.anytoshort(exit)
  460.   local t = {
  461.     n = "north",
  462.     e = "east",
  463.     s = "south",
  464.     w = "west",
  465.     ne = "northeast",
  466.     se = "southeast",
  467.     sw = "southwest",
  468.     nw = "northwest",
  469.     u = "up",
  470.     d = "down",
  471.     ["in"] = "in",
  472.     out = "out"
  473.   }
  474.   local rt = {}
  475.   for s,l in pairs(t) do
  476.     rt[l] = s; rt[s] = s
  477.   end
  478.  
  479.   return rt[exit]
  480. end
  481.  
  482.  
  483. function mmp.ranytolong(exit)
  484.   local t = {
  485.     n = "south",
  486.     north = "south",
  487.     e = "west",
  488.     east = "west",
  489.     s = "north",
  490.     south = "north",
  491.     w = "east",
  492.     west = "east",
  493.     ne = "southwest",
  494.     northeast = "southwest",
  495.     se = "northwest",
  496.     southeast = "northwest",
  497.     sw = "northeast",
  498.     southwest = "northeast",
  499.     nw = "southeast",
  500.     northwest = "southeast",
  501.     u = "down",
  502.     up = "down",
  503.     d = "up",
  504.     down = "up",
  505.     i = "out",
  506.     ["in"] = "out",
  507.     o = "in",
  508.     out = "in"
  509.   }
  510.  
  511.   return t[exit]
  512. end
  513.  
  514. -- returns nil or the room number relative to this one
  515. function mmp.relativeroom(from, dir)
  516.   if not mmp.roomexists(from) then return end
  517.  
  518.   local exits = getRoomExits(tonumber(from))
  519.   return exits[mmp.anytolong(dir)]
  520. end
  521.  
  522. function mmp.deleteArea(name, exact)
  523.   local id, fname, ma = mmp.findAreaID(name, exact)
  524.  
  525.   if id then
  526.     deleteArea(id)
  527.     mmp.echo(string.format("Deleted %s (%d).", fname, id))
  528.     centerview(mmp.currentroom)
  529.   elseif next(ma) then
  530.     mmp.echo("Which one of these specifically would you like to delete?")
  531.  
  532.     fg("DimGrey")
  533.     for _,name in ipairs(ma) do
  534.     echo "  " setUnderline(true) echoLink(name, [[mmp.deleteArea("]]..name..[[", true)]], "Delete "..name, true) setUnderline(false) echo"\n"
  535.     end
  536.     resetFormat()
  537.     else
  538.       mmp.echo("Don't know of that area.")
  539.   end
  540.  
  541.   raiseEvent("mmp areas changed")
  542. end
  543.  
  544. function mmp.renameArea(name, exact)
  545.   if not (mmp.currentroom or getRoomArea(mmp.currentroom)) then
  546.     mmp.echo("Don't know what area are we in at the moment, to rename it.")
  547.   else
  548.   setAreaName(getRoomArea(mmp.currentroom), name)
  549.   mmp.echo(string.format("Renamed %s to %s (%d).", mmp.areatabler[getRoomArea(mmp.currentroom)], name, getRoomArea(mmp.currentroom)))
  550.   centerview(mmp.currentroom)
  551.   end
  552.  
  553.   raiseEvent("mmp areas changed")
  554. end
  555.  
  556. function mmp.roomArea(otherroom, name, exact)
  557.     local id,fname,ma
  558.     if tonumber(name) then id = tonumber(name); fname = mmp.areatabler[id]
  559.     else id, fname, ma = mmp.findAreaID(name, exact) end
  560.  
  561.     if otherroom ~= "" and not mmp.roomexists(otherroom) then
  562.       mmp.echo("Room id "..otherroom.." doesn't seem to exist.")
  563.       return
  564.     elseif otherroom == "" and not mmp.roomexists(mmp.currentroom) then
  565.       mmp.echo("Don't know where we are at the moment.")
  566.       return
  567.     end
  568.  
  569.    otherroom = otherroom ~= "" and otherroom or mmp.currentroom
  570.  
  571.   if id then
  572.     setRoomArea(otherroom, id)
  573.     mmp.echo(string.format("Moved %s to %s (%d).", (getRoomName(otherroom) ~= "" and getRoomName(otherroom) or "''"), fname, id))
  574.     centerview(otherroom)
  575.   elseif next(ma) then
  576.     mmp.echo("Into which area exactly would you like to move the room?")
  577.  
  578.     fg("DimGrey")
  579.     for _,name in ipairs(ma) do
  580.     echo "  " setUnderline(true) echoLink(name, [[mmp.roomArea('', "]]..name..[[", true)]], "Move the room to "..name, true) setUnderline(false) echo"\n"
  581.     end
  582.     resetFormat()
  583.     else
  584.       mmp.echo("Don't know of that area.")
  585.   end
  586. end
  587.  
  588. function mmp.getAreaBorders(areaid)
  589.  
  590.     local roomlist, endresult = getAreaRooms(areaid), {}
  591.     -- sometimes getAreaRooms can give us no result :(
  592.     if not roomlist then mmp.echo("Sorry, seems we can't go there - getAreaRooms("..areaid..") didn't give us any results (Mudlet problem - redownloading the map might help fix it)") return end
  593.  
  594.     -- obtain a room list for each of the room IDs we got
  595.     for _, id in pairs(roomlist) do
  596.       local exits = getRoomExits(id)
  597.       for _, to in pairs(exits) do
  598.         if not table.contains(roomlist, to) then
  599.           endresult[id] = getRoomName(id)
  600.         end
  601.       end
  602.     end
  603.  
  604.  
  605.     return endresult
  606.  
  607. end
  608.  
  609. function mmp.locateAndEcho(room, person)
  610.     local t = mmp.searchRoom(room)
  611.  
  612.     echo "  ("
  613.     mmp.echonums(room, true)
  614.     echo ")"
  615.  
  616.     if not (t[room] or table.contains(t, room)) then return end
  617.  
  618.     echo"\n"
  619.     if table.size(t) == 1 then
  620.       local k,v = next(t)
  621.       cecho("<red>From your knowledge, that room is in <orange_red>"..(mmp.areatabler[getRoomArea(type(k) == "number" and k or v)] or "?").."<red>.")
  622.     else
  623.       local k,v = next(t)
  624.     local areas = {}
  625.       if type(k) == "number" then
  626.         for k, _ in pairs(t) do
  627.           areas[mmp.areatabler[getRoomArea(k)] or "?"] = true
  628.         end
  629.       else
  630.         for _, k in pairs(t) do
  631.           areas[mmp.areatabler[getRoomArea(k)] or "?"] = true
  632.         end
  633.       end
  634.  
  635.       local flattened_areas = {}
  636.       for k, _ in pairs(areas) do
  637.         if k ~= "" then flattened_areas[#flattened_areas+1] = k end
  638.       end
  639.  
  640.       cecho("<red>From your knowledge, that room might be in <orange_red>"..table.concat(flattened_areas, ", or ").."<red>.")
  641.     end
  642.  
  643.    if person then
  644.       mmp.pdb[person] = room
  645.       mmp.pdb_lastupdate[person] = true
  646.       raiseEvent("mmapper updated pdb")
  647.    end
  648. end
  649.  
  650. function mmp.viewArea (where, exact)
  651.   if not where or not type(where)=="string" then
  652.     mmp.echo("Which area would you like to view?") return end
  653.  
  654.  
  655.   local areaid, msg, multiples = mmp.findAreaID(where, exact)
  656.   if areaid then
  657.     -- center on the first room ID, which typically is the start of an area
  658.     centerview(getAreaRooms(areaid)[1])
  659.  
  660.   elseif not areaid and #multiples > 0 then
  661.     mmp.echo("Which area would you like to view exactly?")
  662.     fg("DimGrey")
  663.     for _, areaname in ipairs(multiples) do
  664.       echo"  "; setUnderline(true) echoLink(areaname, 'mmp.viewArea("'..areaname..'", true)', "Click to view "..areaname, true) setUnderline(false) echo"\n"
  665.     end
  666.     resetFormat()
  667.     return
  668.   else
  669.     mmp.echo(string.format("Don't know of any area named '%s'.", where))
  670.     return
  671.   end
  672. end
  673.  
  674. -- room label the room I'm in
  675. -- room label 342 this is a label in room 342
  676. -- room label green this is a green label where I'm at
  677. -- room label green black this is a green to black label where I'm at
  678. -- room label 34 green black this is a green to black label at room 34
  679. -- how it works: split input string into tokens by space, then determine
  680. -- what to do by checking first few tokens, and finally call the local
  681. -- function with the proper arguments
  682. function mmp.roomLabel(input)
  683.   if not createMapLabel then
  684.     mmp.echo("Your Mudlet doesn't support createMapLabel() yet - please update to 2.0-test3 or better.") return
  685.   end
  686.  
  687.   local tk = input:split(" ")
  688.   local room, fg, bg, message = mmp.currentroom, "yellow", "red", "Some room label"
  689.  
  690.   -- input always have to be something, so tk[1] at least always exists
  691.   if tonumber(tk[1]) then
  692.     room = tonumber(table.remove(tk, 1)) -- remove the number, so we're left with the colors or msg
  693.   end
  694.  
  695.   -- next: is this a foreground color?
  696.   if tk[1] and color_table[tk[1]] then
  697.     fg = table.remove(tk, 1)
  698.   end
  699.  
  700.   -- next: is this a backround color?
  701.   if tk[1] and color_table[tk[1]] then
  702.     bg = table.remove(tk, 1)
  703.   end
  704.  
  705.   -- the rest would be our message
  706.   if tk[1] then
  707.     message = table.concat(tk, " ")
  708.   end
  709.  
  710.   -- if we haven't provided a room ID and we don't know where we are yet, we can't make a label
  711.   if not room then
  712.     mmp.echo("We don't know where we are to make a label here.") return
  713.   end
  714.  
  715.   local x,y = getRoomCoordinates(room)
  716.   local f1,f2,f3 = unpack(color_table[fg])
  717.   local b1,b2,b3 = unpack(color_table[bg])
  718.  
  719.   -- finally: do it :)
  720.  
  721.   local lid = createMapLabel(getRoomArea(room), message, x, y, f1,f2,f3, b1,b2,b3)
  722.   mmp.echo(string.format("Created new label #%d '%s' in %s.", lid, message, getRoomAreaName(getRoomArea(room))))
  723. end
  724.  
  725. function mmp.areaLabels (where, exact)
  726.   if not getMapLabels then
  727.     mmp.echo("Your Mudlet doesn't support getMapLabels() yet - please update to 2.0-test3 or better.") return
  728.   end
  729.  
  730.   if (not where or not type(where)=="string") and not mmp.currentroom then
  731.     mmp.echo("For which area would you like to view labels?") return end
  732.  
  733.   if not where then
  734.   exact = true
  735.     where = getRoomAreaName(getRoomArea(mmp.currentroom))
  736.   end
  737.  
  738.   local areaid, msg, multiples = mmp.findAreaID(where, exact)
  739.   if areaid then
  740.     local t = getMapLabels(areaid)
  741.     if type(t) ~= "table" or not next(t) then
  742.       mmp.echo(string.format("'%s' doesn't seem to have any labels.", getRoomAreaName(areaid)))
  743.       return
  744.     end
  745.  
  746.     mmp.echo(string.format("Area labels for '%s'", getRoomAreaName(areaid)))
  747.     for labelid, labeltext in pairs(t) do
  748.       fg("DimGrey")
  749.       echo(string.format("  %d) %s (", labelid, labeltext))
  750.       fg"orange_red" setUnderline(true) echoLink('-', string.format('deleteMapLabel(%d, %d)', areaid, labelid), "Delete label #"..labelid.." from "..getRoomAreaName(areaid))
  751.       setUnderline(false) echo")\n"
  752.     end
  753.     resetFormat()
  754.  
  755.   elseif not areaid and #multiples > 0 then
  756.     mmp.echo("Which area would you like to view exactly?")
  757.     fg("DimGrey")
  758.     for _, areaname in ipairs(multiples) do
  759.       echo"  "; setUnderline(true) echoLink(areaname, 'mmp.areaLabels("'..areaname..'", true)', "Click to view labels in "..areaname, true) setUnderline(false) echo"\n"
  760.     end
  761.     resetFormat()
  762.     return
  763.   else
  764.     mmp.echo(string.format("Don't know of any area named '%s'.", where))
  765.     return
  766.   end
  767. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement