Difference between revisions of "Area 51"

From Mudlet
Jump to navigation Jump to search
Line 369: Line 369:
  
 
registerAnonymousEventHandler("onMouseCopyExample", "onMouseCopyExample")
 
registerAnonymousEventHandler("onMouseCopyExample", "onMouseCopyExample")
 +
</syntaxhighlight>
 +
 +
==closestColor==
 +
; closestColor(coloR[,G,B])
 +
 +
:Returns the closest color from the color_table to the one provided for use in cecho and associated functions.
 +
 +
;Parameters
 +
* ''coloR:''
 +
: Either the string representation of a color from decho or hecho (IE "#ff0077" or "<255,0,128>"), a table of color values (IE {255,0,18}), or the Red component of RGB
 +
* ''G:''
 +
: The Green component of RGB coordinates. Only needed if first parameter sent as the Red component only
 +
* ''B:''
 +
: The Blue component of RGB coordinates. Only needed if first parameter sent as the Red component only
 +
 +
;Returns
 +
* The closest color name from the color_table to the one supplied on success
 +
* nil + error if it cannot parse the parameters into a color.
 +
 +
{{MudletVersion|4.13}}
 +
 +
;Example
 +
<syntaxhighlight lang="lua">
 +
local colorName = closestColor(255, 0, 0) -- "ansi_light_red"
 +
local colorName = closestColor({127, 255, 127}) -- "ansi_120"
 +
local colorName = closestColor("<127, 127, 127>") -- "ansi_008"
 +
local colorName = closestColor("#a020e6") -- "purple"
 +
local colorName = closestColor("blue") -- "blue" (you wouldn't necessarily do this on purpose, but during automated color conversions this might occur)
 
</syntaxhighlight>
 
</syntaxhighlight>
  

Revision as of 14:30, 6 September 2021

This page is for the development of documentation for Lua API functions that are currently being worked on. Ideally the entries here can be created in the same format as will be eventually used in Lua Functions and its sub-sites.

Please use the Area_51/Template to add new entries in the sections below.

The following headings reflect those present in the main Wiki area of the Lua API functions. It is suggested that new entries are added so as to maintain a sorted alphabetical order under the appropriate heading.


Basic Essential Functions

These functions are generic functions used in normal scripting. These deal with mainly everyday things, like sending stuff and echoing to the screen.

Database Functions

A collection of functions for helping deal with the database.

Date/Time Functions

A collection of functions for handling date & time.

File System Functions

A collection of functions for interacting with the file system.

Mapper Functions

A collection of functions that manipulate the mapper and its related features.

mapSymbolFontInfo

mapSymbolFontInfo()
See also: setupMapSymbolFont()
returns
  • either a table of information about the configuration of the font used for symbols in the (2D) map, the elements are:
  • fontName - a string of the family name of the font specified
  • onlyUseThisFont - a boolean indicating whether glyphs from just the fontName font are to be used or if there is not a glyph for the required grapheme (character) then a glyph from the most suitable different font will be substituted instead. Should this be true and the specified font does not have the required glyph then the replacement character (typically something like ) could be used instead. Note that this may not affect the use of Color Emoji glyphs that are automatically used in some OSes but that behavior does vary across the range of operating systems that Mudlet can be run on.
  • scalingFactor - a floating point number between 0.50 and 2.00 which modifies the size of the symbols somewhat though the extremes are likely to be unsatisfactory because some of the particular symbols may be too small (and be less visible at smaller zoom levels) or too large (and be clipped by the edges of the room rectangle or circle).
  • or nil and an error message on failure.
As the symbol font details are stored in the (binary) map file rather than the profile then this function will not work until a map is loaded (or initialized, by activating a map window).

Note Note: pending, not yet available.

pauseSpeedwalk

pauseSpeedwalk()
Pauses a speedwalk started using speedwalk()
See also: speedwalk(), stopSpeedwalk(), resumeSpeedwalk()

Note Note: pending, not yet available.

resumeSpeedwalk

resumeSpeedwalk()
Continues a speedwalk paused using pauseSpeedwalk()
See also: speedwalk(), pauseSpeedwalk(), stopSpeedwalk()

Note Note: pending, not yet available.

setupMapSymbolFont

setupMapSymbolFont(fontName[, onlyUseThisFont[, scalingFactor]])
configures the font used for symbols in the (2D) map.
See also: mapSymbolFontInfo()
Parameters
  • fontName one of:
  • - a string that is the family name of the font to use;
  • - the empty string "" to reset to the default {which is "Bitstream Vera Sans Mono"};
  • - a Lua nil as a placeholder to not change this parameter but still allow a following one to be modified.
  • onlyUseThisFont (optional) one of:
  • - a Lua boolean true to require Mudlet to use graphemes (character) only from the selected font. Should a requested grapheme not be included in the selected font then the font replacement character (�) might be used instead; note that under some circumstances it is possible that the OS (or Mudlet) provided color Emoji Font may still be used but that cannot be guaranteed across all OS platforms that Mudlet might be run on;
  • - a Lua boolean false to allow Mudlet to get a different glyph for a particular grapheme from the most suitable other font found in the system should there not be a glyph for it in the requested font. This is the default unless previously changed by this function or by the corresponding checkbox in the Profile Preferences dialogue for the profile concerned;
  • - a Lua nil as a placeholder to not change this parameter but still allow the following one to be modified.
  • scalingFactor (optional): a floating point value in the range 0.5 to 2.0 (default 1.0) that can be used to tweak the rectangular space that each different room symbol is scaled to fit inside; this might be useful should the range of characters used to make the room symbols be consistently under- or over-sized.
Returns
  • true on success
  • nil and an error message on failure. As the symbol font details are stored in the (binary) map file rather than the profile then this function will not work until a map is loaded (or initialised, by activating a map window).

Note Note: pending, not yet available.

stopSpeedwalk

stopSpeedwalk()
Stops a speedwalk started using speedwalk()
See also: pauseSpeedwalk(), resumeSpeedwalk(), speedwalk()

Note Note: pending, not yet available.

Miscellaneous Functions

Miscellaneous functions.

getCharacterName

getCharacterName()
Returns the name entered into the "Character name" field on the Connection Preferences form. Can be used to find out the name that might need to be handled specially in scripts or anything that needs to be personalized to the player. If there is nothing set in that entry will return an empty string.

Introduced along with four other functions to enable game server log-in to be scripted with the simultaneous movement of that functionality from the Mudlet application core code to a predefined doLogin() function that may be replaced for more sophisticated requirements.

See also: sendCharacterName(), sendCharacterPassword(), sendCustomLoginText(), getCustomLoginTextId().

Note Note: Not available yet

Example
lua send("cast 'glamor' " .. getCharacterName())

You get a warm feeling passing from your core to the tips of your hands, feet and other body parts.
A small twittering bird settles on your shoulder and starts to look adoringly at you.
A light brown faun gambles around you and then nuzzles your hand.
A tawny long-haired cat saunters over and start to rub itself against your ankles.
A small twittering bird settles on your shoulder and starts to look adoringly at you.
A light brown faun gambles around you and then nuzzles your hand.
A small twittering bird settles on your shoulder and starts to look adoringly at you.
A mangy dog trots up to you and proceeds to mark the bottom of your leggings.


getCustomLoginTextId

getCustomLoginTextId()

Returns the Id number of the custom login text setting from the profile's preferences. Returns 0 if the option is disabled or a number greater than that for the item in the table; note it is possible if using an old saved profile in the future that the number might be higher than expected. As a design policy decision it is not permitted for a script to change the setting, this function is intended to allow a script or package to check that the setting is what it expects.

Introduced along with four other functions to enable game server log-in to be scripted with the simultaneous movement of that functionality from the Mudlet application core code to a predefined doLogin() function, a replacement for which is shown below.

See also: getCharacterName(), sendCharacterName(), sendCustomLoginText(), sendPassword().

Note Note: Not available yet

Only one custom login text has been defined initially:

Predefined custom login texts
Id Custom text Introduced in Mudlet version
1 "connect {character name} {password}" TBD

The addition of further texts would be subject to negotiation with the Mudlet Makers.

Example
-- A replacement for the default function placed into LuaGlobal.lua to reproduce the previous behavior of the Mudlet application:
function doLogin()
  if getCustomLoginTextId() ~= 1 then
    -- We need this particular option but it is not permitted for a script to change the setting, it can only check what it is
    echo("\nUnable to login - please select the 'connect {character name} {password}` custom login option in the profile preferences.\n")
  else
    tempTime(2.0, [[sendCustomLoginText()]], 1)
  end
end


sendCharacterName

sendCharacterName()

Sends the name entered into the "Character name" field on the Connection Preferences form directly to the game server. Returns true unless there is nothing set in that entry in which case a nil and an error message will be returned instead.

Introduced along with four other functions to enable game server log-in to be scripted with the simultaneous movement of that functionality from the Mudlet application core code to a predefined doLogin() function that may be replaced for more sophisticated requirements.

See also: getCharacterName(), sendCharacterPassword(), sendCustomLoginText(), getCustomLoginTextId().

Note Note: Not available yet


sendCharacterPassword

sendCharacterPassword()

Sends the password entered into the "Password" field on the Connection Preferences form directly to the game server. Returns true unless there is nothing set in that entry or it is too long after (or before) a connection was successfully made in which case a nil and an error message will be returned instead.

Introduced along with four other functions to enable game server log-in to be scripted with the simultaneous movement of that functionality from the Mudlet application core code to a predefined doLogin() function, reproduced below, that may be replaced for more sophisticated requirements.

See also: getCharacterName(), sendCustomLoginText(), getCustomLoginTextId(), sendCharacterName().

Note Note: Not available yet

Example
-- The default function placed into LuaGlobal.lua to reproduce the previous behavior of the Mudlet application:
function doLogin()
  if getCharacterName() ~= "" then
    tempTime(2.0, [[sendCharacterName()]], 1)
    tempTime(3.0, [[sendCharacterPassword()]], 1)
  end
end


sendCustomLoginText

sendCustomLoginText()

Sends the custom login text (which does NOT depend on the user's choice of GUI language) selected in the preferences for this profile. The {password} (and {character name} if present) fields will be replaced with the values entered into the "Password" and "Character name" fields on the Connection Preferences form and then sent directly to the game server. Returns true unless there is nothing set in either of those entries (though only if required for the character name) or it is too long after (or before) a connection was successfully made or if the custom login feature is disabled, in which case a nil and an error message will be returned instead.

Introduced along with four other functions to enable game server log-in to be scripted with the simultaneous movement of that functionality from the Mudlet application core code to a predefined doLogin() function, a replacement for which is shown below.

See also: getCharacterName(), sendCharacterName(), sendPassword(), getCustomLoginTextId().

Note Note: Not available yet

Only one custom login text has been defined initially:

Predefined custom login texts
Id Custom text Introduced in Mudlet version
1 "connect {character name} {password}" TBD

The addition of further texts would be subject to negotiation with the Mudlet Makers.

Example
-- A replacement for the default function placed into LuaGlobal.lua to reproduce the previous behavior of the Mudlet application:
function doLogin()
  if getCustomLoginTextId() ~= 1 then
    -- We need this particular option but it is not permitted for a script to change the setting, it can only check what it is
    echo("\nUnable to login - please select the 'connect {character name} {password}` custom login option in the profile preferences.\n")
  else
    tempTime(2.0, [[sendCustomLoginText()]], 1)
  end
end

Scripting Object Functions

A collection of functions that manipulate Mudlets scripting objects - triggers, aliases, and so forth.

getButtonState

getButtonState()
This function can be used within checkbox button scripts (2-state buttons) to determine the current state of the checkbox.
Returns 2 if button is checked, and 1 if it's not.
From Mudlet 4.13.0 it will also accept a numerical ID or string name as an argument and can be used anywhere in a Mudlet item's script and it returns the checked state of the item identified provided it is a check-able button as a boolean (or a nil and an error message otherwise).
See also (from Mudlet 4.13.0): setButtonState().
Example within the script for a check-able button
local checked = getButtonState()
if checked == 1 then
    hideExits()
else
    showExits()
end
Example (from Mudlet 4.13.0) anywhere in a Lua script in a profile
local checked, errMsg = getButtonState("Sleep")
if checked then
    shouldBeMounted = shouldBeMounted or false
    sendAll("wake", "stand")
    if shouldBeMounted then
        send("mount horse")
    end
else
    -- Global used to record if we were on a horse before our nap:
    shouldBeMounted = mounted or false
    if shouldBeMounted then
        send("dismount")
    end
    sendAll("sit", "sleep")
end

setButtonState

setButtonState(button ID/name, checked)
Sets the state of a check-able ("push-down") type button from any Mudlet item's script - but does not cause the script or one of the commands associated with that button to be run/sent.
Parameters
  • button ID/name:
The name of the button as a string or a unique ID (positive) integer number {for a name only one matching one will be affected - it will be the same one that the matching getButtonState() reports upon}.
  • checked:
boolean value that indicated whether the state required is down (true) or up (false).
Returns
A boolean value indicating true if the visible state was actually changed, i.e. had any effect. This function will return a nil and a suitable error message if the identifying name or ID was not found or was not a check-able (push-down) button item (i.e. was a non-checkable button or a menu or a toolbar instead).
Example
-- inside, for example, an initialization script for a GUI package:
setButtonState("Sleep", false)
setButtonState("Sit", false)
-- these are going to be used as "radio" buttons where setting one
-- of them will unset all the others, they will each need something
-- similar in their own scripts to unset all the others in the set
-- and also something to prevent them from being unset by clicking
-- on themselves:
setButtonState("Wimpy", true)
setButtonState("Defensive", false)
setButtonState("Normal", false)
setButtonState("Brave", false)
if character.type == "Warrior" then
    -- Only one type has this mode - and it can only be reset by
    -- something dying (though that could be us!)
    setButtonState("Beserk!!!", false)
end
Mudlet VersionAvailable in Mudlet4.13+

Networking Functions

A collection of functions for managing networking.

String Functions

These functions are used to manipulate strings.

Table Functions

These functions are used to manipulate tables. Through them you can add to tables, remove values, check if a value is present in the table, check the size of a table, and more.

Text to Speech Functions

These functions are used to create sound from written words. Check out our Text-To-Speech Manual for more detail on how this all works together.

UI Functions

These functions are used to construct custom user GUIs. They deal mainly with miniconsole/label/gauge creation and manipulation as well as displaying or formatting information on the screen.

addMouseEvent

addMouseEvent(uniqueName, eventName, [displayName, tooltipText])
Registers a new context menu option when right-clicked on a console window. Will return True if the event was added successfully, otherwise a warning will be thrown. If the displayName isn't set, uniqueName will be used for the menu label. eventName should be a Mudlet event that will handle the click data that includes event, unique name, window name, selection upper left column and row and selection bottom right column and row in that order. This is intended to be used with available cursor functions for any selection or processing needs.
Mudlet VersionAvailable in Mudlet4.13+
See also
getMouseEvents(), moveCursor()
Parameters
  • uniqueName:
A unique identifier for the mouse event.
  • eventName:
Name of the Mudlet event that will handle the data.
  • displayName:
(optional) Label text for the mouse context menu. If not set, the label defaults to uniqueName.
  • tooltipText:
(optional) Tooltip text when mouse is hovered on the option. If not set, no tooltip will be visible.
Returns
  • Returns True If the mouse event was added successfully.
Example

An example showing implementing a hecho-friendly copy option:

addMouseEvent("hecho copy", "onMouseCopyExample")

function rgbToHex(r,g,b)
    local rgb = (r * 0x10000) + (g * 0x100) + b
    return string.format("#%x", rgb)
end

function onMouseCopyExample(event, menu, window, startCol, startRow, endCol, endRow)
  -- Check whether there's an actual selection
  if startCol == endCol and startRow == endRow then return end
  local parsed = ""
  local lastColor = nil
  -- Loop through each symbol within the range
  for l = startRow, endRow do
    local cStart = l == startRow and startCol or 0
    moveCursor(window, cStart, l)
    local cEnd = l == endRow and endCol or #getCurrentLine() - 1
    for c = cStart, cEnd do
      selectSection(window, c, 1)
      local symbol = getSelection(window) or ""
      -- Convert the foreground color to a hex format, suitable for hecho
      local color = rgbToHex(getFgColor(window))
      -- Don't repeat the color if previous one was the same
      if color == lastColor then
        parsed = parsed .. symbol
      else
        lastColor = color
        parsed = parsed .. color .. symbol
      end
    end
    if l ~= endRow then parsed = parsed .. "\n" end
  end
  setClipboardText(parsed)
end

registerAnonymousEventHandler("onMouseCopyExample", "onMouseCopyExample")

closestColor

closestColor(coloR[,G,B])
Returns the closest color from the color_table to the one provided for use in cecho and associated functions.
Parameters
  • coloR:
Either the string representation of a color from decho or hecho (IE "#ff0077" or "<255,0,128>"), a table of color values (IE {255,0,18}), or the Red component of RGB
  • G:
The Green component of RGB coordinates. Only needed if first parameter sent as the Red component only
  • B:
The Blue component of RGB coordinates. Only needed if first parameter sent as the Red component only
Returns
  • The closest color name from the color_table to the one supplied on success
  • nil + error if it cannot parse the parameters into a color.
Mudlet VersionAvailable in Mudlet4.13+
Example
local colorName = closestColor(255, 0, 0) -- "ansi_light_red"
local colorName = closestColor({127, 255, 127}) -- "ansi_120"
local colorName = closestColor("<127, 127, 127>") -- "ansi_008"
local colorName = closestColor("#a020e6") -- "purple"
local colorName = closestColor("blue") -- "blue" (you wouldn't necessarily do this on purpose, but during automated color conversions this might occur)

removeMouseEvent

removeMouseEvent(uniqueName)
Removes an existing mouse event. Returns True If the event exists and was removed successfully, throws a warning if the event doesn't exist.
Mudlet VersionAvailable in Mudlet4.13+
See also
getMouseEvents(), addMouseEvent()
Parameters
  • uniqueName:
A unique identifier that the mouse event was registered under.
Returns
  • Returns True If the mouse event was removed successfully.

getMouseEvents

events = getMouseEvents()
Returns a table of registered mouse events, including any of the additional arguments they may have.
Mudlet VersionAvailable in Mudlet4.13+
See also
addMouseEvent(), removeMouseEvent()
Returns
  • Returns a table with mouse event uniqueName as indexes, containing all the creation arguments as the sub-table members.