Difference between revisions of "Manual:Mapper Functions"
m (moved Template:Mapper Functions to Manual:Mapper Functions: Moving to proper Namespace) |
|
(No difference)
|
Revision as of 18:23, 18 January 2012
Mapper Functions
These are functions that are to be used with the Mudlet Mapper. The mapper is designed to be MUD-generic - it only provides the display and pathway calculations, to be used in Lua scripts that are tailored to the MUD you're playing. For a collection of pre-made scripts and general mapper talk, visit the mapper section of the forums.
To register a script as a mapping one with Mudlet (so Mudlet knows the profile has one and won't bother the user when they open the map), please do this in your script: <lua> mudlet = mudlet or {}; mudlet.mapper_script = true </lua>
addAreaName
areaID = addAreaName(areaName)
Adds a new area name and returns the new area ID for the new name. If the name already exists, -1 is returned.
- See also: deleteArea
<lua> local newID = addAreaName("My house")
if newID == -1 then echo("That area name is already taken :(\n") else echo("Created new area with the ID of "..newid..".\n") end </lua>
addRoom
addRoom(roomID)
Creates a new room with the given ID, returns true if the room was successfully created.
- See also: createRoomID
<lua> local newroomid = createRoomID() addRoom(newroomid) </lua>
addSpecialExit
addSpecialExit(roomIDFrom, roomIDTo, command)
Creates a one-way from one room to another, that will use the given command for going through them.
- See also: clearSpecialExits
<lua> -- sample alias pattern: ^spe (\d+) (.*?)$ -- mmp.currentroom is your current room ID in this example addSpecialExit(mmp.currentroom,tonumber(matches[2]), matches[3]) echo("\n SPECIAL EXIT ADDED TO ROOMID:"..matches[2]..", Command:"..matches[3]) centerview(mmp.currentroom) </lua>
centerview
centerview (roomID)
Centers the map view onto the given room ID. The map must be open to see this take effect. This function can also be used to see the map of an area if you know the number of a room there and the area and room are mapped.
clearRoomUserData
clearRoomUserData(roomID)
Clears all user data from a given room.
- See also: setRoomUserData
<lua> clearRoomUserData(341) </lua>
clearSpecialExits
clearSpecialExits(roomID)
Removes all special exits from a room.
- See also: addSpecialExit
<lua> clearSpecialExits(1337)
if #getSpecialExits(1337) == 0 then -- clearSpecialExits will neve fail on a valid room ID, this is an example
echo("All special exits successfully cleared from 1337.\n")
end </lua>
createMapLabel
labelID = createMapLabel(areaID, text, posx, posy, fgRed, fgGreen, fgBlue, bgRed, bgGreen, bgBlue)
Creates a visual label on the map for all z-levels at given coordinates, with the given background and foreground colors. It returns a label ID that you can use later for deleting it.
The coordinates 0,0 are in the middle of the map, and are in sync with the room coordinates - so using the x,y values of getRoomCoordinates will place the label near that room.
- See also: deleteMapLabel
<lua> local labelid = createMapLabel( 50, "my map label", 0, 0, 255,0,0,23,0,0) </lua>
createMapper
createMapper(x, y, width, height)
Creates a miniconsole window for mapper to render in, the with the given dimensions. You can only create one at a time at the moment.
<lua> createMapper(0,0,300,300) -- creates a 300x300 mapper top-right of Mudlet setBorderLeft(305) -- adds a border so text doesn't underlap the mapper display </lua>
createRoomID
usableId = createRoomID()
Returns the lowest possible room ID you can use for creating a new room. If there are gaps in room IDs your map uses it, this function will go through the gaps first before creating higher IDs.
- See also: addRoom
deleteArea
deleteArea(areaID)
Deletes the given area, permanently. This will also delete all rooms in it!
- See also: addAreaName
<lua> deleteArea(23) </lua>
deleteMapLabel
deleteMapLabel(areaID, labelID)
Deletes a map label from a specfic area.
- See also: createMapLabel
<lua> deleteMapLabel(50, 1) </lua>
deleteRoom
deleteRoom(roomID)
Deletes an individual room, and unlinks all exits leading to and from it.
<lua> deleteRoom(335) </lua>
getAreaRooms
getAreaRooms(area id)
Returns an indexed table with all rooms IDs for a given area ID (room IDs are values), or nil if no such area exists.
Note that on Mudlet versions prior to the 2.0 final release, this function would raise an error.
<lua> -- using the sample findAreaID() function defined in the getAreaTable() example, -- we'll define a function that echo's us a nice list of all rooms in an area with their ID function echoRoomList(areaname)
local id, msg = findAreaID(areaname) if id then local roomlist, endresult = getAreaRooms(id), {} -- obtain a room list for each of the room IDs we got for _, id in ipairs(roomlist) do endresult[id] = getRoomName(id) end -- now display something half-decent looking cecho(string.format( "List of all rooms in %s (%d):\n", msg, table.size(endresult)))
for roomid, roomname in pairs(endresult) do cecho(string.format( "%6s: %s\n", roomid, roomname)) end elseif not id and msg then echo("ID not found; " .. msg) else echo("No areas matched the query.") end
end </lua>
getAreaTable
getAreaTable()
Returns a key(area name)-value(area id) table with all known areas and their IDs. There is an area with the name of and an ID of 0 included in it, you should ignore that.
<lua> -- example function that returns the area ID for a given area
function findAreaID(areaname)
local list = getAreaTable()
-- iterate over the list of areas, matching them with substring match. -- if we get match a single area, then return it's ID, otherwise return -- 'false' and a message that there are than one are matches local returnid, fullareaname for area, id in pairs(list) do if area:find(areaname, 1, true) then if returnid then return false, "more than one area matches" end returnid = id; fullareaname = area end end return returnid, fullareaname
end
-- sample use: local id, msg = findAreaID("blahblah") if id then
echo("Found a matching ID: " .. id")
elseif not id and msg then
echo("ID not found; " .. msg)
else
echo("No areas matched the query.")
end </lua>
getCustomEnvColorTable
envcolors = getCustomEnvColorTable()
Returns a table with customized environments, where the key is the environment ID and the value is a indexed table of rgb values.
<lua> {
envid1 = {r,g,b}, envid2 = {r,g,b}
} </lua>
getMapLabels
arealabels = getMapLabels(areaID)
Returns an indexed table (that starts indexing from 0) of all of the labels in the area, plus their label text. You can use the label id to deleteMapLabel it.
<lua> display(getMapLabels(43)) table {
0: 1: 'Svorai's grove'
}
deleteMapLabel(43, 0) display(getMapLabels(43)) table {
1: 'Svorai's grove'
} </lua>
getPath
getPath(roomID from, roomID to)
Returns a boolean true/false if a path between two room IDs is possible. If it is, the global `speedWalkPath` table is set to all of the directions that have to be taken to get there, and the global `speedWalkDir` table is set to all of the roomIDs you'll encounter on the way.
<lua> -- check if we can go to a room - if yes, go to it if getPath(34,155) then
gotoRoom(155)
else
echo("\nCan't go there!")
end </lua>
getRoomArea
areaID = getRoomArea(roomID)
Returns the area ID of a given room ID. To get the area name, you can check the area ID against the data given by getAreaTable() function, or use the getRoomAreaName function.
Note that if the room ID does not exist, this function will raise an error
<lua> display("Area ID of room #100 is: "..getRoomArea(100))
display("Area name for room #100 is: "..getRoomAreaName(getRoomArea(100))) </lua>
getRoomAreaName
areaname = getRoomAreaName(areaID)
Returns the area name for a given area id.
<lua> echo(string.format("room id #455 is in %s.", getRoomAreaName(getRoomArea(455)))) </lua>
getRoomCoordinates
x,y,z = getRoomCoordinates(room ID)
Returns the room coordinates of the given room ID.
<lua> local x,y,z = getRoomCoordinates(roomID) echo("Room Coordinates for "..roomID..":") echo("\n X:"..x) echo("\n Y:"..y) echo("\n Z:"..z) </lua>
getRoomEnv
envID = getRoomEnv(roomID)
Returns the environment ID of a room. The mapper does not store environment names, so you'd need to keep track of which ID is what name yourself.
<lua> funtion checkID(id)
echo(strinf.format("The env ID of room #%d is %d.\n", id, getRoomEnv(id)))
end </lua>
getRoomExits
getRoomExits (roomID) Returns the currently known non-special exits for a room in an key-index form: exit = exitroomid, ie:
<lua> table {
'northwest': 80 'east': 78
} </lua>
getRoomIDbyHash
roomID = getRoomIDbyHash(hash)
Returns a room ID that is associated with a given hash in the mapper. This is primarily for MUDs that make use of hashes instead of room IDs (like Avalon.de MUD). -1 is returned if no room ID matches the hash.
<lua> -- example taken from http://forums.mudlet.org/viewtopic.php?f=13&t=2177 _id1 = getRoomIDbyHash( "5dfe55b0c8d769e865fd85ba63127fbc" ); if _id1 == -1 then
_id1 = createRoomID() setRoomIDbyHash( _id1, "5dfe55b0c8d769e865fd85ba63127fbc" ) addRoom( _id ) setRoomCoordinates( _id1, 0, 0, -1 )
end </lua>
getRoomName
roomName = getRoomName(roomID)
Returns the room name for a given room id.
<lua> echo(string.format("The name of the room id #455 is %s.", getRoomname(455)) </lua>
getRooms
rooms = getRooms()
Returns the list of all rooms in the map in an area in roomid - room name format.
<lua> -- simple, raw viewer for rooms in an area display(getRooms()) </lua>
getRoomsByPosition
getRoomsByPosition(areaID, x,y,z)
Returns an indexed table of all rooms at the given coordinates in the given area, or an empty one if there are none. This function can be useful for checking if a room exists at certain coordinates, or whenever you have rooms overlapping.
<lua> -- sample script to determine a room nearby, given a relative direction from the current room local otherroom if matches[2] == "" then
local w = matches[3] local ox, oy, oz, x,y,z = getRoomCoordinates(mmp.currentroom) local has = table.contains if has({"west", "left", "w", "l"}, w) then x = (x or ox) - 1; y = (y or oy); z = (z or oz) elseif has({"east", "right", "e", "r"}, w) then x = (x or ox) + 1; y = (y or oy); z = (z or oz) elseif has({"north", "top", "n", "t"}, w) then x = (x or ox); y = (y or oy) + 1; z = (z or oz) elseif has({"south", "bottom", "s", "b"}, w) then x = (x or ox); y = (y or oy) - 1; z = (z or oz) elseif has({"northwest", "topleft", "nw", "tl"}, w) then x = (x or ox) - 1; y = (y or oy) + 1; z = (z or oz) elseif has({"northeast", "topright", "ne", "tr"}, w) then x = (x or ox) + 1; y = (y or oy) + 1; z = (z or oz) elseif has({"southeast", "bottomright", "se", "br"}, w) then x = (x or ox) + 1; y = (y or oy) - 1; z = (z or oz) elseif has({"southwest", "bottomleft", "sw", "bl"}, w) then x = (x or ox) - 1; y = (y or oy) - 1; z = (z or oz) elseif has({"up", "u"}, w) then x = (x or ox); y = (y or oy); z = (z or oz) + 1 elseif has({"down", "d"}, w) then x = (x or ox); y = (y or oy); z = (z or oz) - 1 end
local carea = getRoomArea(mmp.currentroom) if not carea then mmp.echo("Don't know what area are we in.") return end
otherroom = select(2, next(getRoomsByPosition(carea,x,y,z)))
if not otherroom then mmp.echo("There isn't a room to the "..w.." that I see - try with an exact room id.") return else mmp.echo("The room "..w.." of us has an ID of "..otherroom) end
</lua>
getRoomUserData
data = getRoomUserData(roomID, key (as a string))
Returns the user data stored at a given room with a given key, or "" if none is stored. Use setRoomUserData for setting the user data.
<lua> display(getRoomUserData(341, "visitcount")) </lua>
getRoomWeight
room weight = getRoomWeight(roomID)
Returns the weight of a room. By default, all new rooms have a weight of 1.
- See also: setRoomWeight
<lua> display("Original weight of room 541: "..getRoomWeight(541) setRoomWeight(541, 3) display("New weight of room 541: "..getRoomWeight(541) </lua>
getSpecialExits
exits = getSpecialExits(roomID)
Returns a roomid - command table of all special exits in the room. If there are no special exits in the room, the table returned will be empty.
<lua> getSpecialExits(1337)
-- results in: --[[ table {
12106: 'faiglom nexus'
} ]] </lua>
getSpecialExitsSwap
exits = getSpecialExitsSwap(roomID)
Very similar to getSpecialExits, but returns the rooms in the command - roomid style.
gotoRoom
gotoRoom (roomID) Speedwalks you to the given room from your current room if it is able and knows the way. You must turn the map on for this to work, else it will return "(mapper): Don't know how to get there from here :(".
hasExitLock
status = hasExitLock(roomID, direction)
Returns true or false depending on whenever a given exit leading out from a room is locked. direction right now is a number that corresponds to the direction:
exitmap = {
n = 1,
north = 1,
ne = 2,
northeast = 2,
nw = 3,
northwest = 3,
e = 4,
east = 4,
w = 5,
west = 5,
s = 6,
south = 6,
se = 7,
southeast = 7,
sw = 8,
southwest = 8,
u = 9,
up = 9,
d = 10,
down = 10,
["in"] = 11,
out = 12
}
- Examples
<lua> -- check if the east exit of room 1201 is locked display(hasExitLock(1201, 4)) </lua>
- See also: lockExit
highlightRoom
highlightRoom( id, r1,g1,b1,r2,g2,b2, radius, alpha1, alpha2)
Highlights a room with the given color, which will override it's environment color. If you use two different colors, then there'll be a shading from the center going outwards that changes into the other color. highlightRadius is the radius for the highlight circle - if you don't want rooms beside each other to over lap, use 1 as the radius. alphaColor1 and alphaColor2 are transparency values from 0 (completely transparent) to 255 (not transparent at all).
- See also: unHighlightRoom
Available since Mudlet 2.0 final release
<lua> -- color room #351 red to blue local r,g,b = unpack(color_table.red) local br,bg,bb = unpack(color_table.blue) highlightRoom(351, r,g,b,br,bg,bb, 1, 255, 255) </lua>
lockExit
lockExit(roomID, direction, lock = true/false)
Locks a given exit from a room (which means that unless all exits in the incoming room are locked, it'll still be accessible). Direction at the moment is only set as a number, and here's a listing of them:
exitmap = {
n = 1,
north = 1,
ne = 2,
northeast = 2,
nw = 3,
northwest = 3,
e = 4,
east = 4,
w = 5,
west = 5,
s = 6,
south = 6,
se = 7,
southeast = 7,
sw = 8,
southwest = 8,
u = 9,
up = 9,
d = 10,
down = 10,
["in"] = 11,
out = 12
}
- Examples
<lua> -- lock the east exit of room 1201 so we never path through it lockExit(1201, 4, true) </lua>
- See also: hasExitLock
lockRoom
lockRoom (roomID, lock = true/false)
Locks a given room id from future speed walks (thus the mapper will never path through that room).
- See also: roomLocked
<lua> lockRoom(1, true) -- locks a room if from being walked through when speedwalking. lockRoom(1, false) -- unlocks the room, adding it back to possible rooms that can be walked through. </lua>
roomExists
roomExists(roomID)
Returns a boolean true/false depending if the room with that ID exists (is created) or not.
roomLocked
locked = roomLocked(roomID)
Returns true or false whenever a given room is locked.
- See also: lockRoom
<lua> echo(string.format("Is room #4545 locked? %s.", roomLocked(4545) and "Yep" or "Nope")) </lua>
saveMap
saveMap(location)
Saves the map to a given location, and returns true on success. The location folder needs to be already created for save to work.
<lua> local savedok = saveMap(getMudletHomeDir().."/my fancy map.dat") if not savedok then
echo("Couldn't save :(\n")
else
echo("Saved fine!\n")
end </lua>
searchRoom
searchRoom (room name)
Searches for rooms that match (by case-insensitive, substring match) the given room name. It returns a key-value table in form of roomid = roomname, like so:
<lua> display(searchRoom("master"))
--[[ would result in: table {
17463: 'in the branches of the Master Ravenwood' 3652: 'master bedroom' 10361: 'Hall of Cultural Masterpieces' 6324: 'Exhibit of the Techniques of the Master Artisans' 5340: 'office of the Guildmaster' (...) 2004: 'office of the guildmaster' 14457: 'the Master Gear' 1337: 'before the Master Ravenwood Tree'
} ]]</lua>
If no rooms are found, then an empty table is returned.
setAreaName
setAreaName(areaID, newName)
Renames an existing area to the new name.
<lua> setAreaName(2, "My city") </lua>
setCustomEnvColor
setCustomEnvColor(environmentID, r,g,b,a)
Creates, or overrides an already created custom color definition for a given environment ID. Note that this will not work on the default environment colors - those are adjustable by the user in the preferences. You can however create your own environment and use a custom color definition for it.
<lua> setRoomEnv(571, 200) -- change the room's environment ID to something arbitrary, like 200 local r,g,b = unpack(color_table.blue) setCustomEnvColor(200, r,g,b, 255) -- set the color of environmentID 200 to blue </lua>
setExit
setExit(from roomID, to roomID, direction)
Creates a one-way exit from one room to another using a standard direction - which can be either one of n, ne, nw, e, w, s, se, sw, u, d, in, out, or a number which represents a direction.
Returns false if the exit creation didn't work.
<lua> -- alias pattern: ^exit (\d+) (\w+)$
if setExit(mmp.currentroom, tonumber(matches[2]),matches[3]) then echo("\nExit set to room:"..matches[2]..", Direction:"..string.upper(matches[3])) centerview(mmp.currentroom) else mmp.echo("Failed to set the exit.") end </lua>
This function can also delete exits from a room if you use it like so: setExit(from roomID, -1, direction)
Which will delete an outgoing exit in the specified direction from a room.
<lua> -- locate the room on the other end, so we can unlink it from there as well if necessary local otherroom if getRoomExits(getRoomExits(mmp.currentroom)[dir])[mmp.ranytolong(dir)] then
otherroom = getRoomExits(mmp.currentroom)[dir]
end
if setExit(mmp.currentroom, -1, dir) then
if otherroom then if setExit(otherroom, -1, mmp.ranytolong(dir)) then mmp.echo(string.format("Deleted the %s exit from %s (%d).", dir, getRoomName(mmp.currentroom), mmp.currentroom)) else mmp.echo("Couldn't delete the incoming exit.") end else mmp.echo(string.format("Deleted the one-way %s exit from %s (%d).", dir, getRoomName(mmp.currentroom), mmp.currentroom)) end
else
mmp.echo("Couldn't delete the outgoing exit.")
end </lua>
You can use these numbers for setting the directions as well: <lua> exitmap = {
n = 1, north = 1, ne = 2, northeast = 2, nw = 3, northwest = 3, e = 4, east = 4, w = 5, west = 5, s = 6, south = 6, se = 7, southeast = 7, sw = 8, southwest = 8, u = 9, up = 9, d = 10, down = 10, ["in"] = 11, out = 12
}</lua>
setGridMode
setGridMode(area, true/false)
Enables grid/wilderness view mode for an area - this will cause the rooms to lose their visible exit connections, like you'd see on compressed ASCII maps, both in 2D and 3D view mode.
<lua> setGridMode(55,true) -- set area with ID 55 to be in grid mode </lua>
setRoomArea
setRoomArea(roomID, newAreaID)
Assigns the given room to a new area. This will have the room be visually moved into the area as well.
setRoomChar
setRoomChar(roomID, character)
Designed for an area in grid mode, this will set a single character to be on a room. You can use "_" to clear it.
<lua> setRoomChar(431, "#")
setRoomChar(123. "$") </lua>
setRoomCoordinates
setRoomCoordinates(roomID, x, y, z)
Sets the given room ID to be at the following coordinates visually on the map, where z is the up/down level.
0,0,0 is the center of the map.
<lua> -- alias pattern: ^set rc (-?\d+) (-?\d+) (-?\d+)$ local x,y,z = getRoomCoordinates(previousRoomID) local dir = matches[2]
if dir == "n" then
y = y+1
elseif dir == "ne" then
y = y+1 x = x+1
elseif dir == "e" then
x = x+1
elseif dir == "se" then
y = y-1 x = x+1
elseif dir == "s" then
y = y-1
elseif dir == "sw" then
y = y-1 x = x-1
elseif dir == "w" then
x = x-1
elseif dir == "nw" then
y = y+1 x = x-1
elseif dir == "u" or dir == "up" then
z = z+1
elseif dir == "down" then
z = z-1
end setRoomCoordinates(roomID,x,y,z) centerview(roomID) </lua>
You can make them relative as well:
<lua> -- alias pattern: ^src (\w+)$
local x,y,z = getRoomCoordinates(previousRoomID) local dir = matches[2]
if dir == "n" then
y = y+1
elseif dir == "ne" then
y = y+1 x = x+1
elseif dir == "e" then
x = x+1
elseif dir == "se" then
y = y-1 x = x+1
elseif dir == "s" then
y = y-1
elseif dir == "sw" then
y = y-1 x = x-1
elseif dir == "w" then
x = x-1
elseif dir == "nw" then
y = y+1 x = x-1
elseif dir == "u" or dir == "up" then
z = z+1
elseif dir == "down" then
z = z-1
end setRoomCoordinates(roomID,x,y,z) centerview(roomID) </lua>
setRoomEnv
setRoomEnv(roomID, newEnvID)
Sets the given room to a new environment ID. You don't have to use any functions to create it - can just set it right away.
<lua> setRoomEnv(551, 34) -- set room with the ID of 551 to the environment ID 34 </lua>
setRoomIDbyHash
setRoomIDbyHash(roomID, hash)
Sets the hash to be associated with the given roomID. See also getRoomIDbyHash().
setRoomName
setRoomName(roomID, newName)
Renames an existing room to a new name.
<lua> setRoomName(534, "That evil room I shouldn't visit again.") lockRoom(534, true) -- and lock it just to be safe </lua>
setRoomUserData
setRoomUserData(roomID, key (as a string), value (as a string))
Stores information about a room under a given key. Similar to Lua's key-value tables, except only strings may be used here. One advantage of using userdata is that it's stored within the map file itself - so sharing the map with someone else will pass on the user data. You can have as many keys as you'd like.
Returns true if successfully set.
- See also: clearRoomUserData
<lua> -- can use it to store room descriptions... setRoomUserData(341, "description", "This is a plain-looking room.")
-- or whenever it's outdoors or not... setRoomUserData(341, "ourdoors", "true")
-- how how many times we visited that room local visited = getRoomUserData(341, "visitcount") visited = (tonumber(visited) or 0) + 1 setRoomUserData(341, "visitcount", tostring(visited))
-- can even store tables in it, using the built-in yajl.to_string function setRoomUserData(341, "some table", yajl.to_string({name = "bub", age = 23})) display("The denizens name is: "..yajl.to_value(getRoomUserData(341, "some table")).name) </lua>
setRoomWeight
setRoomWeight(roomID, weight)
Sets a weight to the given roomID. By default, all rooms have a weight of 0 - the higher the weight is, the more likely the room is to be avoided for pathfinding. For example, if travelling across water rooms takes more time than land ones - then you'd want to assign a weight to all water rooms, so they'd be avoided if there are possible land pathways.
To completely avoid a room, make use of lockRoom().
- See also: getRoomWeight
<lua> setRoomWeight(1532, 3) -- avoid using this room if possible, but don't completely ignore it </lua>
unHighlightRoom
unHighlightRoom(roomID)
Unhighlights a room if it was previously highlighted and restores the rooms original environment color.
- See also: highlightRoom
Available since Mudlet 2.0 final release
<lua> unHighlightRoom(4534) </lua>