forked from DFHack/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepeat.lua
90 lines (73 loc) · 2.22 KB
/
repeat.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
-- repeatedly call a lua script
-- eg "repeat -time 1 months -command cleanowned"; to disable "repeat -cancel cleanowned"
-- author expwnent
-- vaguely based on a script by Putnam
local usage = [====[
repeat
======
Repeatedly calls a lua script at the specified interval. This can be used from
init files. Note that any time units other than ``frames`` are unsupported when
a world is not loaded (see ``dfhack.timeout()``).
Usage examples::
repeat -name jim -time delay -timeUnits units -command [ printArgs 3 1 2 ]
repeat -time 1 -timeUnits months -command [ multicmd cleanowned scattered x; clean all ] -name clean
repeat -list
The first example is abstract; the second will regularly remove all contaminants
and worn items from the game.
Arguments:
``-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`` (default "ticks"). Units can be
in simulation-time "frames" (raw FPS) or "ticks" (only while
unpaused), while "days", "months", and "years" are by in-world time.
``-command [ ... ]``
``...`` specifies the command to be run
``-cancel NAME``
cancels the repetition with the name NAME
``-list``
prints names of scheduled commands
]====]
local repeatUtil = require 'repeat-util'
local utils = require 'utils'
local validArgs = utils.invert({
'help',
'cancel',
'name',
'time',
'timeUnits',
'command',
'list'
})
local args = utils.processArgs({...}, validArgs)
if args.help then
print(usage)
return
end
if args.cancel then
repeatUtil.cancel(args.cancel)
if args.name then
repeatUtil.cancel(args.name)
end
elseif args.command then
local time = tonumber(args.time)
if not args.name then
args.name = args.command[1]
end
if not args.timeUnits then
args.timeUnits = 'ticks'
end
local callCommand = function()
dfhack.run_command(table.unpack(args.command))
end
repeatUtil.scheduleEvery(args.name,time,args.timeUnits,callCommand)
end
if args.list then
for k in pairs(repeatUtil.repeating) do
print(k)
end
return
end