Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions abilities.lua
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ robot.add_ability({

return core.get_node(dirPos.frontpos).name
end,
runtime_unique = function (part, dir)
return part..dir
end,
runtime = true
})

Expand Down Expand Up @@ -278,6 +281,33 @@ robot.add_ability({
runtime = true
})

-- [[ Speak ]]
robot.add_ability({
ability = "speak",
item = function ()
if core.get_modpath('homedecor') then
return 'homedecor:speaker_driver'
end
return "default:pine_sapling"
end,
act_on = 'last',
depends_on = 'any',
description = S("Send chat messages to the owner"),
command_example = "robot.speak('Hello!')",
done_by = { head = true },
action = function (nodeinfo, part, message)
local hasability = api.any_has_ability(nodeinfo, 'speak')
if not hasability then return end

local meta = nodeinfo.meta()
local owner = meta:get_string('player_name')
core.chat_send_player(owner, "[robot] "..message)

return nil
end,
runtime = true
})

local function is_default_method(obj, key)
local method = obj[key]
return (key == 'on_place' and method == core.item_place)
Expand Down
18 changes: 8 additions & 10 deletions api/execute.lua
Original file line number Diff line number Diff line change
Expand Up @@ -219,16 +219,14 @@ local function save_memory(nodeinfo, mem)
end
end

local function runtime_ability(nodeinfo, action, part)
local ran = false
local result
local function runtime_ability(nodeinfo, ability, part)
local cached_results = {}
return function (...)
if ran then
return result
local unique = ability.runtime_unique and ability.runtime_unique(part or 'main', ...) or part or 'main'
if cached_results[unique] == nil then
cached_results[unique] = ability.action(nodeinfo, part or 'main', ...)
end
ran = true
result = action(nodeinfo, part, ...)
return result
return cached_results[unique]
end
end

Expand Down Expand Up @@ -277,10 +275,10 @@ local function run_inner(nodeinfo)
end
end
elseif ability.runtime then
commands[ability.ability] = runtime_ability(nodeinfo, ability.action)
commands[ability.ability] = runtime_ability(nodeinfo, ability)
for _,part in ipairs(parts) do
if commands[part] then
commands[part][ability.ability] = runtime_ability(nodeinfo, ability.action, part)
commands[part][ability.ability] = runtime_ability(nodeinfo, ability, part)
end
end
else
Expand Down