2014-07-03 18:32:15 -06:00
|
|
|
-- scripts/repeat.lua
|
2014-06-26 23:40:36 -06:00
|
|
|
-- repeatedly calls a lua script, eg "repeat -time 1 months -command cleanowned"; to disable "repeat -cancel cleanowned"
|
|
|
|
-- repeat -help for details
|
2014-06-26 06:36:57 -06:00
|
|
|
-- author expwnent
|
|
|
|
-- vaguely based on a script by Putnam
|
2015-10-23 22:10:15 -06:00
|
|
|
--[[=begin
|
|
|
|
|
|
|
|
repeat
|
|
|
|
======
|
|
|
|
Repeatedly calls a lua script at the specified interval.
|
|
|
|
|
|
|
|
This allows neat background changes to the function of the game, especially when
|
|
|
|
invoked from an init file. For detailed usage instructions, use ``repeat -help``.
|
|
|
|
|
|
|
|
Usage examples::
|
|
|
|
|
|
|
|
repeat -name jim -time delay -timeUnits units -printResult true -command [ printArgs 3 1 2 ]
|
|
|
|
repeat -time 1 -timeUnits months -command [ multicmd cleanowned scattered x; clean all ] -name clean
|
|
|
|
|
|
|
|
The first example is abstract; the second will regularly remove all contaminants
|
|
|
|
and worn items from the game.
|
|
|
|
|
|
|
|
``-name`` sets the name for the purposes of cancelling and making sure you don't schedule the
|
|
|
|
same repeating event twice. If not specified, it's set to the first argument after ``-command``.
|
|
|
|
``-time delay -timeUnits units``; delay is some positive integer, and units is some valid time
|
|
|
|
unit for ``dfhack.timeout(delay,timeUnits,function)``. ``-command [ ... ]`` specifies the
|
|
|
|
command to be run.
|
|
|
|
|
|
|
|
=end]]
|
2014-06-26 06:36:57 -06:00
|
|
|
|
2014-07-03 18:32:15 -06:00
|
|
|
local repeatUtil = require 'repeat-util'
|
|
|
|
local utils = require 'utils'
|
2014-06-26 06:36:57 -06:00
|
|
|
|
2014-07-03 18:32:15 -06:00
|
|
|
validArgs = validArgs or utils.invert({
|
|
|
|
'help',
|
|
|
|
'cancel',
|
|
|
|
'name',
|
|
|
|
'time',
|
|
|
|
'timeUnits',
|
|
|
|
'command'
|
|
|
|
})
|
|
|
|
|
|
|
|
local args = utils.processArgs({...}, validArgs)
|
|
|
|
|
|
|
|
if args.help then
|
2014-06-26 06:36:57 -06:00
|
|
|
print([[repeat.lua
|
|
|
|
repeat -help
|
|
|
|
print this help message
|
|
|
|
repeat -cancel bob
|
|
|
|
cancels the repetition with the name bob
|
2014-10-04 19:52:28 -06:00
|
|
|
repeat -name jim -time delay -timeUnits units -printResult true -command [ printArgs 3 1 2 ]
|
2014-06-26 06:36:57 -06:00
|
|
|
-name sets the name for the purposes of cancelling and making sure you don't schedule the same repeating event twice
|
|
|
|
if not specified, it's set to the first argument after -command
|
2014-07-03 18:32:15 -06:00
|
|
|
-time delay -timeUnits units
|
2014-06-26 06:36:57 -06:00
|
|
|
delay is some positive integer
|
2014-07-03 18:32:15 -06:00
|
|
|
units is some valid time unit for dfhack.timeout(delay,timeUnits,function)
|
2014-10-04 19:52:28 -06:00
|
|
|
-command [ ... ]
|
2014-06-26 06:36:57 -06:00
|
|
|
specify the command to be run
|
|
|
|
]])
|
2014-07-03 18:32:15 -06:00
|
|
|
return
|
2014-06-26 06:36:57 -06:00
|
|
|
end
|
|
|
|
|
2014-07-03 18:32:15 -06:00
|
|
|
if args.cancel then
|
|
|
|
repeatUtil.cancel(args.cancel)
|
|
|
|
if args.name then
|
|
|
|
repeatUtil.cancel(args.name)
|
2014-06-26 06:36:57 -06:00
|
|
|
end
|
2014-07-03 18:32:15 -06:00
|
|
|
return
|
2014-06-26 06:36:57 -06:00
|
|
|
end
|
|
|
|
|
2014-07-03 18:32:15 -06:00
|
|
|
args.time = tonumber(args.time)
|
|
|
|
if not args.name then
|
|
|
|
args.name = args.command[1]
|
2014-06-27 04:23:04 -06:00
|
|
|
end
|
|
|
|
|
2014-07-03 18:32:15 -06:00
|
|
|
if not args.timeUnits then
|
|
|
|
args.timeUnits = 'ticks'
|
|
|
|
end
|
|
|
|
|
|
|
|
local callCommand = function()
|
|
|
|
dfhack.run_command(table.unpack(args.command))
|
2014-06-27 04:23:04 -06:00
|
|
|
end
|
|
|
|
|
2014-07-03 18:32:15 -06:00
|
|
|
repeatUtil.scheduleEvery(args.name,args.time,args.timeUnits,callCommand)
|
2014-06-26 06:36:57 -06:00
|
|
|
|