From 4eb5841c60afdb8a12602a93e2e68ffdec19bac4 Mon Sep 17 00:00:00 2001 From: Petr Mikheev Date: Fri, 2 Apr 2021 19:42:19 +0200 Subject: [PATCH] Update OpenMW Lua documentation --- docs/source/conf.py | 2 +- docs/source/reference/lua-scripting/api.rst | 455 ++---------------- .../reference/lua-scripting/overview.rst | 446 ++++++++++++++--- files/CMakeLists.txt | 1 + files/lua_api/CMakeLists.txt | 21 + files/lua_api/README.md | 13 + files/lua_api/coroutine.doclua | 71 +++ files/lua_api/global.doclua | 269 +++++++++++ files/lua_api/math.doclua | 200 ++++++++ files/lua_api/openmw/README.md | 14 + files/lua_api/openmw/async.lua | 52 ++ files/lua_api/openmw/core.lua | 328 +++++++++++++ files/lua_api/openmw/generate_luadoc.sh | 4 + files/lua_api/openmw/nearby.lua | 36 ++ files/lua_api/openmw/query.lua | 116 +++++ files/lua_api/openmw/self.lua | 68 +++ files/lua_api/openmw/ui.lua | 15 + files/lua_api/openmw/util.lua | 150 ++++++ files/lua_api/openmw/world.lua | 34 ++ files/lua_api/string.doclua | 231 +++++++++ files/lua_api/table.doclua | 66 +++ 21 files changed, 2111 insertions(+), 481 deletions(-) create mode 100644 files/lua_api/CMakeLists.txt create mode 100644 files/lua_api/README.md create mode 100644 files/lua_api/coroutine.doclua create mode 100644 files/lua_api/global.doclua create mode 100644 files/lua_api/math.doclua create mode 100644 files/lua_api/openmw/README.md create mode 100644 files/lua_api/openmw/async.lua create mode 100644 files/lua_api/openmw/core.lua create mode 100755 files/lua_api/openmw/generate_luadoc.sh create mode 100644 files/lua_api/openmw/nearby.lua create mode 100644 files/lua_api/openmw/query.lua create mode 100644 files/lua_api/openmw/self.lua create mode 100644 files/lua_api/openmw/ui.lua create mode 100644 files/lua_api/openmw/util.lua create mode 100644 files/lua_api/openmw/world.lua create mode 100644 files/lua_api/string.doclua create mode 100644 files/lua_api/table.doclua diff --git a/docs/source/conf.py b/docs/source/conf.py index 7653b94edf..72bb17adbb 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -176,7 +176,7 @@ html_static_path = [ # Add any extra paths that contain custom files (such as robots.txt or # .htaccess) here, relative to this directory. These files are copied # directly to the root of the documentation. -#html_extra_path = [] +html_extra_path = ['generated-luadoc'] # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, # using the given strftime format. diff --git a/docs/source/reference/lua-scripting/api.rst b/docs/source/reference/lua-scripting/api.rst index a7b153c039..f98d4ef074 100644 --- a/docs/source/reference/lua-scripting/api.rst +++ b/docs/source/reference/lua-scripting/api.rst @@ -17,426 +17,69 @@ Engine handler is a function defined by a script, that can be called by the engi | onUpdate(dt) | | Called every frame if game not paused. `dt` is the time | | | | from the last update in seconds. | +----------------------------------+-------------------------------------------------------------+ -| onSave() -> data | Called when the game is saving. | +| onSave() -> data | | Called when the game is saving. May be called in inactive | +| | | state, so it shouldn't use `openmw.nearby`. | +----------------------------------+-------------------------------------------------------------+ -| onLoad(data) | Called on loading with the data previosly returned by onSave| +| onLoad(data) | | Called on loading with the data previosly returned by | +| | | onSave. During loading the object is always in inactive. | +----------------------------------+-------------------------------------------------------------+ | **Only for global scripts** | +----------------------------------+-------------------------------------------------------------+ | onNewGame() | New game is started | +----------------------------------+-------------------------------------------------------------+ -| onPlayerAdded(player) |Player added to game world. The argument is a `Game object`_.| +| onPlayerAdded(player) |Player added to game world. The argument is a `Game object`. | +----------------------------------+-------------------------------------------------------------+ | onActorActive(actor) | Actor (NPC or Creature) becomes active. | +----------------------------------+-------------------------------------------------------------+ +| **Only for local scripts** | ++----------------------------------+-------------------------------------------------------------+ +| onActive() | | Called when the object becomes active (either a player | +| | | came to this cell again, or a save was loaded). | ++----------------------------------+-------------------------------------------------------------+ +| onInactive() | | Object became inactive. Since it is inactive the handler | +| | | can not access anything nearby, but it is possible to send| +| | | an event to global scripts. | ++----------------------------------+-------------------------------------------------------------+ +| onConsume(recordId) | | Called if `recordId` (e.g. a potion) is consumed. | ++----------------------------------+-------------------------------------------------------------+ | **Only for local scripts attached to a player** | +----------------------------------+-------------------------------------------------------------+ | onKeyPress(symbol, modifiers) | | Key pressed. `Symbol` is an ASCII code, `modifiers` is | | | | a binary OR of flags of special keys (ctrl, shift, alt). | +----------------------------------+-------------------------------------------------------------+ -.. _Game object: - -Game object reference -===================== - -Game object is a universal reference to an object in the game world. Anything that has a reference number. - -**Can be used on any object:** - -+---------------------------------------------+--------------------------------------------------+ -| Function | Description | -+=============================================+==================================================+ -| object:isValid() | | Returns true if the object exists and loaded, | -| | | and false otherwise. If false, then every | -| | | access to the object will raise an error. | -+---------------------------------------------+--------------------------------------------------+ -| object:sendEvent(eventName, eventData) | Sends local event to the object. | -+---------------------------------------------+--------------------------------------------------+ -| object:isEquipped(item) | Returns true if `item` is equipped on `object`. | -+---------------------------------------------+--------------------------------------------------+ -| object:getEquipment() -> table | | Returns a table `slot` -> `object` of currently| -| | | equipped items. See `core.EQUIPMENT_SLOT`. | -| | | Returns empty table if `object` doesn't have | -| | | equipment slots. | -+---------------------------------------------+--------------------------------------------------+ -| object:setEquipment(table) | | Sets equipment. Keys in the table are equipment| -| | | slots (see `core.EQUIPMENT_SLOT`). Each value | -| | | can be either an object or `recordId`. Raises | -| | | an error if `object` doesn't have equipment | -| | | slots and `table` is not empty. Can be called | -| | | only on `self` or from a global script. | -+---------------------------------------------+--------------------------------------------------+ -| object:addScript(scriptPath) | | Adds new script to the object. | -| | | Can be called only from a global script. | -+---------------------------------------------+--------------------------------------------------+ -| object:teleport(cell, pos, [rot]) | | Moves object to given cell and position. | -| | | The effect is not immediate: the position will | -| | | be updated only in the next frame. | -| | | Can be called only from a global script. | -+---------------------------------------------+--------------------------------------------------+ - -+-----------------------+---------------------+--------------------------------------------------+ -| Field | Type | Description | -+=======================+=====================+==================================================+ -| object.position | vector3_ | Position | -+-----------------------+---------------------+--------------------------------------------------+ -| object.rotation | vector3_ | Rotation | -+-----------------------+---------------------+--------------------------------------------------+ -| object.cell | string | Cell | -+-----------------------+---------------------+--------------------------------------------------+ -| object.type | string | :ref:`Type ` of the object | -+-----------------------+---------------------+--------------------------------------------------+ -| object.count | integer | Count (makes sense if holded in a container) | -+-----------------------+---------------------+--------------------------------------------------+ -| object.recordId | string | Record ID | -+-----------------------+---------------------+--------------------------------------------------+ -| object.inventory | Inventory | Inventory of an actor or content of a container | -+-----------------------+---------------------+--------------------------------------------------+ - -**Can be used if object.type == 'Door':** - -+-----------------------+---------------------+--------------------------------------------------+ -| Field | Type | Description | -+=======================+=====================+==================================================+ -| object.isTeleport | boolean | True if it is a teleport door | -+-----------------------+---------------------+--------------------------------------------------+ -| object.destPosition | vector3_ | Destination (only if a teleport door) | -+-----------------------+---------------------+--------------------------------------------------+ -| object.destRotation | vector3_ | Destination rotation (only if a teleport door) | -+-----------------------+---------------------+--------------------------------------------------+ -| object.destCell | string | Destination cell (only if a teleport door) | -+-----------------------+---------------------+--------------------------------------------------+ - -Object type ------------ - -Type is represented as a string. Can be one of: - -- "Activator" -- "Armor" -- "Book" -- "Clothing" -- "Container" -- "Creature" -- "Door" -- "Ingredient" -- "Light" -- "Miscellaneous" -- "NPC" -- "Player" -- "Potion" -- "Static" -- "Weapon" - -ObjectList ----------- - -List of game objects. Can't be created or modified by a script. - -.. code-block:: Lua - - -- Iteration by index - for i = 1, #someList do - doSomething(someList[i]) - end - - -- Generic for (equivalent to iteration by index) - for i, item in someList:ipairs() do - doSomething(item) - end - - -- WRONG: for i, item in ipairs(someList) do - -- It doesn't work because Lua 5.1 doesn't allow to overload ipairs for userdata. - -+---------------------------------------------+--------------------------------------------------+ -| Function | Description | -+=============================================+==================================================+ -| list:ipairs() | Returns an iterator | -+---------------------------------------------+--------------------------------------------------+ -| list:select(query) -> ObjectList | Returns a filtered list | -+---------------------------------------------+--------------------------------------------------+ - -Object inventory ----------------- - -+---------------------------------------------+--------------------------------------------------+ -| Function | Description | -+=============================================+==================================================+ -| inv:countOf(recordId) -> int | The number of items with given recordId | -+---------------------------------------------+--------------------------------------------------+ -| inv:getAll(recordId) -> ObjectList_ | All contained items | -+---------------------------------------------+--------------------------------------------------+ -| inv:getPotions() -> ObjectList_ | All potions from the inventory | -+---------------------------------------------+--------------------------------------------------+ -| inv:getApparatuses() -> ObjectList_ | All apparatuses from the inventory | -+---------------------------------------------+--------------------------------------------------+ -| inv:getArmors() -> ObjectList_ | All armors from the inventory | -+---------------------------------------------+--------------------------------------------------+ -| inv:getBooks() -> ObjectList_ | All books from the inventory | -+---------------------------------------------+--------------------------------------------------+ -| inv:getClothing() -> ObjectList_ | All clothing from the inventory | -+---------------------------------------------+--------------------------------------------------+ -| inv:getIngredients() -> ObjectList_ | All ingredients from the inventory | -+---------------------------------------------+--------------------------------------------------+ -| inv:getLights() -> ObjectList_ | All lights from the inventory | -+---------------------------------------------+--------------------------------------------------+ -| inv:getLockpicks() -> ObjectList_ | All lockpicks from the inventory | -+---------------------------------------------+--------------------------------------------------+ -| inv:getMiscellaneous() -> ObjectList_ | All miscellaneous items from the inventory | -+---------------------------------------------+--------------------------------------------------+ -| inv:getProbes() -> ObjectList_ | All probes from the inventory | -+---------------------------------------------+--------------------------------------------------+ -| inv:getRepairKits() -> ObjectList_ | All repair kits from the inventory | -+---------------------------------------------+--------------------------------------------------+ -| inv:getWeapons() -> ObjectList_ | All weapon from the inventory | -+---------------------------------------------+--------------------------------------------------+ - -openmw.util -=========== - -+---------------------------------------------+--------------------------------------------------+ -| Function | Description | -+=============================================+==================================================+ -| vector2(x, y) -> vector2_ | Creates 2D vector | -+---------------------------------------------+--------------------------------------------------+ -| vector3(x, y, z) -> vector3_ | Creates 3D vector | -+---------------------------------------------+--------------------------------------------------+ -| clamp(value, from, to) -> number | Limits given value to the interval [from, to] | -+---------------------------------------------+--------------------------------------------------+ -| normalizeAngle(radians) -> number | Adds 2pi*k and puts the angle in range [-pi, pi] | -+---------------------------------------------+--------------------------------------------------+ - -vector2 -------- - -Immutable 2D vector. - -.. code-block:: Lua - - v = vector2(3, 4) - v.x, v.y -- 3.0, 4.0 - str(v) -- "(3.0, 4.0)" - v:length() -- 5.0 length - v:length2() -- 25.0 square of the length - v:normalize() -- vector2(3/5, 4/5) - v:rotate(radians) -- rotate clockwise (returns rotated vector) - v1:dot(v2) -- dot product (returns a number) - v1 * v2 -- dot product - v1 + v2 -- vector addition - v1 - v2 -- vector subtraction - v1 * x -- multiplication by a number - v1 / x -- division by a number - -vector3 -------- - -.. code-block:: Lua - - v = vector3(3, 4, 5) - v.x, v.y, v.z -- 3.0, 4.0, 5.0 - str(v) -- "(3.0, 4.0, 4.5)" - v:length() -- length - v:length2() -- square of the length - v:normalize() -- normalized vector - v1:dot(v2) -- dot product (returns a number) - v1 * v2 -- dot product (returns a number) - v1:cross(v2) -- cross product (returns a vector) - v1 ^ v2 -- cross product (returns a vector) - v1 + v2 -- vector addition - v1 - v2 -- vector subtraction - v1 * x -- multiplication by a number - v1 / x -- division by a number - -openmw.core -=========== - -+-----------------------+---------------------+---------------------------------------------------+ -| Field | Type | Description | -+=======================+=====================+===================================================+ -| OBJECT_TYPE | Readonly table | Possible :ref:`object type ` values | -+-----------------------+---------------------+---------------------------------------------------+ -| EQUIPMENT_SLOTS | Readonly table | | Contains keys that can be used in | -| | | | `object:getEquipment` and `object:setEquipment`.| -+-----------------------+---------------------+---------------------------------------------------+ - -``EQUIPMENT_SLOTS`` contains fields: "Helmet", "Cuirass", "Greaves", "LeftPauldron", "RightPauldron", -"LeftGauntlet", "RightGauntlet", "Boots", "Shirt", "Pants", "Skirt", "Robe", "LeftRing", "RightRing", -"Amulet", "Belt", "CarriedRight", "CarriedLeft", "Ammunition". - -+---------------------------------------------+--------------------------------------------------+ -| Function | Description | -+=============================================+==================================================+ -| sendGlobalEvent(eventName, eventData) | Sends an event to global scripts | -+---------------------------------------------+--------------------------------------------------+ -| getRealTime() | | The number of seconds (with floating point) | -| | | passed from 00:00 UTC 1 Januar 1970 (unix time)| -+---------------------------------------------+--------------------------------------------------+ -| getGameTimeInSeconds() | | The number of seconds in the game world, passed| -| | | from starting a new game. | -+---------------------------------------------+--------------------------------------------------+ -| getGameTimeInHours() | | Current time of the game world in hours. | -| | | Note that the number of game seconds in a game | -| | | hour is not guaranteed to be fixed. | -+---------------------------------------------+--------------------------------------------------+ - -openmw.async -============ - -Timers and coroutine utils. -All functions require the package itself as a first argument. -I.e. functions should be called with ``:`` rather than with ``.``. - -+-----------------------------------------------------+--------------------------------------------------+ -| Function | Description | -+=====================================================+==================================================+ -| async:registerTimerCallback(name, func) -> Callback | Registers a function as a timer callback | -+-----------------------------------------------------+--------------------------------------------------+ -| async:newTimerInSeconds(delay, Callback, arg) | | Calls `Callback(arg)` in `delay` seconds. | -| | | `Callback` must be registered in advance. | -+-----------------------------------------------------+--------------------------------------------------+ -| async:newTimerInHours(delay, Callback, arg) | | Calls `Callback(arg)` in `delay` game hours. | -| | | `Callback` must be registered in advance. | -+-----------------------------------------------------+--------------------------------------------------+ -| async:newUnsavableTimerInSeconds(delay, func) | | Call `func()` in `delay` seconds. The timer | -| | | will be lost if the game is saved and loaded. | -+-----------------------------------------------------+--------------------------------------------------+ -| async:newUnsavableTimerInHours(delay, func) | | Call `func()` in `delay` game hours. The timer | -| | | will be lost if the game is saved and loaded. | -+-----------------------------------------------------+--------------------------------------------------+ - -openmw.query -============ - -**TODO** - -openmw.world -============ - -Interface to the game world. Can be used only by global scripts. - -+-----------------------+---------------------+--------------------------------------------------+ -| Field | Type | Description | -+=======================+=====================+==================================================+ -| activeActors | ObjectList_ | List of currently active actors | -+-----------------------+---------------------+--------------------------------------------------+ - -+---------------------------------------------+--------------------------------------------------+ -| Function | Description | -+=============================================+==================================================+ -| selectObjects(query) -> ObjectList_ | Evaluates a query | -+---------------------------------------------+--------------------------------------------------+ - -openmw.nearby -============= - -Can be used only by local scripts. -Read-only access to the nearest area of the game world. - -+-----------------------+---------------------+--------------------------------------------------+ -| Field | Type | Description | -+=======================+=====================+==================================================+ -| activators | ObjectList_ | List of nearby activators | -+-----------------------+---------------------+--------------------------------------------------+ -| actors | ObjectList_ | List of nearby actors | -+-----------------------+---------------------+--------------------------------------------------+ -| containers | ObjectList_ | List of nearby containers | -+-----------------------+---------------------+--------------------------------------------------+ -| doors | ObjectList_ | List of nearby doors | -+-----------------------+---------------------+--------------------------------------------------+ -| items | ObjectList_ | Everything that can be picked up in the nearby | -+-----------------------+---------------------+--------------------------------------------------+ - -+---------------------------------------------+--------------------------------------------------+ -| Function | Description | -+=============================================+==================================================+ -| selectObjects(query) -> ObjectList_ | Evaluates a query | -+---------------------------------------------+--------------------------------------------------+ - -openmw.self -=========== - -Can be used only by local scripts. Full access to the object the script is attached to. -All fields and function of `Game object`_ are also available for `openmw.self`. For example: - -.. code-block:: Lua - - local self = require('openmw.self') - if self.type == 'Player' then - self:sendEvent("something", self.position) - end - -Note that `self` is not a Game Object. If you need an actual object, use `self.object`: - -.. code-block:: Lua - - if self == someObject then ... -- Incorrect, this condition is always false - core.sendGlobalEvent('something', self) -- Incorrect, will raise an error - - if self.object == someObject then ... -- Correct - core.sendGlobalEvent('something', self.object) -- Correct - - -+-----------------------+---------------------+--------------------------------------------------+ -| Field | Type | Description | -+=======================+=====================+==================================================+ -| self.object | `Game object`_ | The object the script is attached to (readonly) | -+-----------------------+---------------------+--------------------------------------------------+ -| self.controls | `Actor controls`_ | Movement controls (only for actors) | -+-----------------------+---------------------+--------------------------------------------------+ - -+---------------------------------------------+--------------------------------------------------+ -| Function | Description | -+=============================================+==================================================+ -| self:setDirectControl(bool) | Enables or disables direct movement control | -+---------------------------------------------+--------------------------------------------------+ -| self:setEquipment(table) | | Sets equipment. Keys in the table are equipment| -| | | slots (see `core.EQUIPMENT_SLOT`). Each value | -| | | can be either an object or `recordId`. Raises | -| | | an error if the object has no equipment | -| | | slots and `table` is not empty. | -+---------------------------------------------+--------------------------------------------------+ -| self:getCombatTarget() -> `Game object`_ | Returns current target or nil if not in combat | -+---------------------------------------------+--------------------------------------------------+ -| self:stopCombat() | Removes all combat packages from the actor | -+---------------------------------------------+--------------------------------------------------+ -| self:startCombat(target) | Attack `target` | -+---------------------------------------------+--------------------------------------------------+ - - -Actor controls --------------- - -Allows to control movements of an actor. Makes an effect only if `setDirectControl(true)` was called. -All fields are mutable. - -+-----------------------+---------------------+--------------------------------------------------+ -| Field | Type | Description | -+=======================+=====================+==================================================+ -| movement | float number | +1 - move forward, -1 - move backward | -+-----------------------+---------------------+--------------------------------------------------+ -| sideMovement | float number | +1 - move right, -1 - move left | -+-----------------------+---------------------+--------------------------------------------------+ -| turn | float number | turn right (radians); if negative - turn left | -+-----------------------+---------------------+--------------------------------------------------+ -| run | boolean | true - run, false - walk | -+-----------------------+---------------------+--------------------------------------------------+ -| jump | boolean | if true - initiate a jump | -+-----------------------+---------------------+--------------------------------------------------+ - -openmw.ui -========= - -Can be used only by local scripts, that are attached to a player. - -+---------------------------------------------+--------------------------------------------------+ -| Function | Description | -+=============================================+==================================================+ -| showMessage(string) | Shows given message at the bottom of the screen. | -+---------------------------------------------+--------------------------------------------------+ -openmw.camera -============= +Packages reference +================== + +API packages provide functions that can be called by scripts. I.e. it is a script-to-engine interaction. +A package can be loaded with ``require('')``. +It can not be overloaded even if there is a lua file with the same name. +The list of available packages is different for global and for local scripts. +Player scripts are local scripts that are attached to a player. + ++---------------------------------------------------------+--------------------+---------------------------------------------------------------+ +| Package | Can be used | Description | ++=========================================================+====================+===============================================================+ +|:ref:`openmw.interfaces