mirror of https://github.com/OpenMW/openmw.git
Add usehandlers.lua (same approach as activationhandlers.lua)
parent
58aeb81e46
commit
ea8692a534
@ -0,0 +1,6 @@
|
|||||||
|
Interface ItemUsage
|
||||||
|
===================
|
||||||
|
|
||||||
|
.. raw:: html
|
||||||
|
:file: generated_html/scripts_omw_usehandlers.html
|
||||||
|
|
@ -0,0 +1,34 @@
|
|||||||
|
.. list-table::
|
||||||
|
:widths: 20 20 60
|
||||||
|
|
||||||
|
* - Interface
|
||||||
|
- Can be used
|
||||||
|
- Description
|
||||||
|
* - :ref:`Activation <Interface Activation>`
|
||||||
|
- by global scripts
|
||||||
|
- Allows to extend or override built-in activation mechanics.
|
||||||
|
* - :ref:`AI <Interface AI>`
|
||||||
|
- by local scripts
|
||||||
|
- Control basic AI of NPCs and creatures.
|
||||||
|
* - :ref:`Camera <Interface Camera>`
|
||||||
|
- by player scripts
|
||||||
|
- | Allows to alter behavior of the built-in camera script
|
||||||
|
| without overriding the script completely.
|
||||||
|
* - :ref:`Controls <Interface Controls>`
|
||||||
|
- by player scripts
|
||||||
|
- | Allows to alter behavior of the built-in script
|
||||||
|
| that handles player controls.
|
||||||
|
* - :ref:`ItemUsage <Interface ItemUsage>`
|
||||||
|
- by global scripts
|
||||||
|
- | Allows to extend or override built-in item usage
|
||||||
|
| mechanics.
|
||||||
|
* - :ref:`Settings <Interface Settings>`
|
||||||
|
- by player and global scripts
|
||||||
|
- Save, display and track changes of setting values.
|
||||||
|
* - :ref:`MWUI <Interface MWUI>`
|
||||||
|
- by player scripts
|
||||||
|
- Morrowind-style UI templates.
|
||||||
|
* - :ref:`UI <Interface UI>`
|
||||||
|
- by player scripts
|
||||||
|
- | High-level UI modes interface. Allows to override parts
|
||||||
|
| of the interface.
|
@ -0,0 +1,97 @@
|
|||||||
|
local types = require('openmw.types')
|
||||||
|
local world = require('openmw.world')
|
||||||
|
|
||||||
|
local handlersPerObject = {}
|
||||||
|
local handlersPerType = {}
|
||||||
|
|
||||||
|
local function useItem(obj, actor)
|
||||||
|
local handlers = handlersPerObject[obj.id]
|
||||||
|
if handlers then
|
||||||
|
for i = #handlers, 1, -1 do
|
||||||
|
if handlers[i](obj, actor) == false then
|
||||||
|
return -- skip other handlers
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
handlers = handlersPerType[obj.type]
|
||||||
|
if handlers then
|
||||||
|
for i = #handlers, 1, -1 do
|
||||||
|
if handlers[i](obj, actor) == false then
|
||||||
|
return -- skip other handlers
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
world._runStandardUseAction(obj, actor)
|
||||||
|
end
|
||||||
|
|
||||||
|
return {
|
||||||
|
interfaceName = 'ItemUsage',
|
||||||
|
---
|
||||||
|
-- Allows to extend or override built-in item usage mechanics.
|
||||||
|
-- Note: at the moment it can override item usage in inventory
|
||||||
|
-- (dragging an item on the character's model), but
|
||||||
|
--
|
||||||
|
-- * can't intercept actions performed by mwscripts;
|
||||||
|
-- * can't intercept actions performed by the AI (i.e. drinking a potion in combat);
|
||||||
|
-- * can't intercept actions performed via quick keys menu.
|
||||||
|
-- @module ItemUsage
|
||||||
|
-- @usage local I = require('openmw.interfaces')
|
||||||
|
--
|
||||||
|
-- -- Override Use action (global script).
|
||||||
|
-- -- Forbid equipping armor with weight > 5
|
||||||
|
-- I.ItemUsage.addHandlerForType(types.Armor, function(armor, actor)
|
||||||
|
-- if types.Armor.record(armor).weight > 5 then
|
||||||
|
-- return false -- disable other handlers
|
||||||
|
-- end
|
||||||
|
-- end)
|
||||||
|
--
|
||||||
|
-- -- Call Use action (any script).
|
||||||
|
-- core.sendGlobalEvent('UseItem', {object = armor, actor = player})
|
||||||
|
interface = {
|
||||||
|
--- Interface version
|
||||||
|
-- @field [parent=#ItemUsage] #number version
|
||||||
|
version = 0,
|
||||||
|
|
||||||
|
--- Add new use action handler for a specific object.
|
||||||
|
-- If `handler(object, actor)` returns false, other handlers for
|
||||||
|
-- the same object (including type handlers) will be skipped.
|
||||||
|
-- @function [parent=#ItemUsage] addHandlerForObject
|
||||||
|
-- @param openmw.core#GameObject obj The object.
|
||||||
|
-- @param #function handler The handler.
|
||||||
|
addHandlerForObject = function(obj, handler)
|
||||||
|
local handlers = handlersPerObject[obj.id]
|
||||||
|
if handlers == nil then
|
||||||
|
handlers = {}
|
||||||
|
handlersPerObject[obj.id] = handlers
|
||||||
|
end
|
||||||
|
handlers[#handlers + 1] = handler
|
||||||
|
end,
|
||||||
|
|
||||||
|
--- Add new use action handler for a type of objects.
|
||||||
|
-- If `handler(object, actor)` returns false, other handlers for
|
||||||
|
-- the same object (including type handlers) will be skipped.
|
||||||
|
-- @function [parent=#ItemUsage] addHandlerForType
|
||||||
|
-- @param #any type A type from the `openmw.types` package.
|
||||||
|
-- @param #function handler The handler.
|
||||||
|
addHandlerForType = function(type, handler)
|
||||||
|
local handlers = handlersPerType[type]
|
||||||
|
if handlers == nil then
|
||||||
|
handlers = {}
|
||||||
|
handlersPerType[type] = handlers
|
||||||
|
end
|
||||||
|
handlers[#handlers + 1] = handler
|
||||||
|
end,
|
||||||
|
},
|
||||||
|
engineHandlers = { _onUseItem = useItem },
|
||||||
|
eventHandlers = {
|
||||||
|
UseItem = function(data)
|
||||||
|
if not data.object then
|
||||||
|
error('UseItem: missing argument "object"')
|
||||||
|
end
|
||||||
|
if not data.actor or not types.Actor.objectIsInstance(data.actor) then
|
||||||
|
error('UseItem: invalid argument "actor"')
|
||||||
|
end
|
||||||
|
useItem(data.object, data.actor)
|
||||||
|
end
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue