forked from DFHack/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeteriorate.lua
More file actions
392 lines (338 loc) · 10.6 KB
/
deteriorate.lua
File metadata and controls
392 lines (338 loc) · 10.6 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
-- Cause selected item types to quickly rot away
--@module = true
--@enable = true
local argparse = require('argparse')
local utils = require('utils')
--------------------
-- state
local GLOBAL_KEY = 'deteriorate'
local categories = {
'clothes',
'food',
'corpses',
'usable-parts',
'unusable-parts',
}
local aliases = {
parts={'usable-parts', 'unusable-parts'},
all=categories,
}
local function get_default_state()
local default_state = {
enabled=false,
categories={},
}
for _,category in ipairs(categories) do
local default_enabled = category == 'corpses' or category == 'unusable-parts'
default_state.categories[category] = {
enabled=default_enabled,
frequency=1,
last_cycle_tick=0,
}
end
return default_state
end
state = state or get_default_state()
function isEnabled()
return state.enabled
end
local function persist_state()
dfhack.persistent.saveSiteData(GLOBAL_KEY, state)
end
-----------------------
-- deterioration logic
local function get_clothes_vectors()
return {
df.global.world.items.other.GLOVES,
df.global.world.items.other.ARMOR,
df.global.world.items.other.SHOES,
df.global.world.items.other.PANTS,
df.global.world.items.other.HELM,
}
end
local function get_food_vectors()
return {
df.global.world.items.other.FISH,
df.global.world.items.other.FISH_RAW,
df.global.world.items.other.EGG,
df.global.world.items.other.CHEESE,
df.global.world.items.other.PLANT,
df.global.world.items.other.PLANT_GROWTH,
df.global.world.items.other.FOOD,
df.global.world.items.other.MEAT,
df.global.world.items.other.LIQUID_MISC,
}
end
local function get_corpse_vectors()
return {
df.global.world.items.other.CORPSE,
df.global.world.items.other.REMAINS,
}
end
local function get_parts_vectors()
return {
df.global.world.items.other.CORPSEPIECE,
}
end
local function is_valid_clothing(item)
-- includes discarded owned clothes
return item.subtype.armorlevel == 0 and item.flags.on_ground
and item.wear > 0
end
local function is_valid_food(item)
if not df.item_liquid_miscst:is_instance(item) then
return true
end
local mi = dfhack.matinfo.decode(item)
return mi:getToken():endswith(':MILK')
end
-- TODO: is just checking in_building sufficient, or do we need to validate
-- that the building it is in is a coffin?
local function is_entombed(item)
return item.flags.in_building
end
local function is_valid_corpse(item)
return not is_entombed(item)
end
local usable_types = {
'plant',
'silk',
'leather',
'bone',
'shell',
'wood',
'soap',
'tooth',
'horn',
'pearl',
'skull',
'hair_wool',
'yarn',
}
local function is_usable_corpse_piece(item)
if item.flags.dead_dwarf or item.corpse_flags.unbutchered then
return false
end
for _,flag in ipairs(usable_types) do
if item.corpse_flags[flag] then return true end
end
return false
end
local function is_valid_usable_corpse_piece(item)
return not is_entombed(item) and is_usable_corpse_piece(item)
end
local function is_valid_unusable_corpse_piece(item)
return not is_entombed(item) and not is_usable_corpse_piece(item)
end
-- different algorithm for clothes so they rot away when they become tattered
local function increment_clothes_wear(item)
item.wear_timer = math.ceil(item.wear_timer * (item.wear + 0.5))
return item.wear > 2
end
local function increment_wear(threshold, item)
item.wear_timer = item.wear_timer + 1
if item.wear_timer > threshold then
item.wear_timer = 0
item.wear = item.wear + 1
end
return item.wear > 3
end
local function deteriorate_items(now, get_item_vectors_fn, is_valid_fn, increment_wear_fn)
local items_to_remove = {}
for _,v in ipairs(get_item_vectors_fn()) do
for _,item in ipairs(v) do
if is_valid_fn(item) and (now or increment_wear_fn(item)) and not item.flags.garbage_collect then
table.insert(items_to_remove, item)
end
end
end
for _,item in ipairs(items_to_remove) do
print(('deteriorate: %s crumbles away to dust'):format(dfhack.items.getReadableDescription(item)))
dfhack.items.remove(item)
end
return #items_to_remove
end
local function mk_deteriorate_fn(get_item_vectors_fn, is_valid_fn, increment_wear_fn)
return function(now)
return deteriorate_items(now, get_item_vectors_fn, is_valid_fn, increment_wear_fn)
end
end
local category_fns = {
clothes=mk_deteriorate_fn(get_clothes_vectors, is_valid_clothing, increment_clothes_wear),
food=mk_deteriorate_fn(get_food_vectors, is_valid_food, curry(increment_wear, 24)),
corpses=mk_deteriorate_fn(get_corpse_vectors, is_valid_corpse, curry(increment_wear, 24)),
['usable-parts']=mk_deteriorate_fn(get_parts_vectors, is_valid_usable_corpse_piece, curry(increment_wear, 24)),
['unusable-parts']=mk_deteriorate_fn(get_parts_vectors, is_valid_unusable_corpse_piece, curry(increment_wear, 24)),
}
----------------------------
-- cycle and timer logic
local TICKS_PER_DAY = 1200
local TICKS_PER_MONTH = 28 * TICKS_PER_DAY
local TICKS_PER_YEAR = 12 * TICKS_PER_MONTH
local function get_normalized_tick()
return dfhack.world.ReadCurrentTick() + TICKS_PER_YEAR * dfhack.world.ReadCurrentYear()
end
timeout_ids = timeout_ids or {}
local function event_loop(category)
local category_data = state.categories[category]
if not state.enabled or not category_data.enabled then return end
local current_tick = get_normalized_tick()
local ticks_per_cycle = math.max(1, math.floor(TICKS_PER_DAY * category_data.frequency))
local timeout_ticks = ticks_per_cycle
if current_tick - category_data.last_cycle_tick < ticks_per_cycle then
timeout_ticks = category_data.last_cycle_tick - current_tick + ticks_per_cycle
else
category_fns[category](false)
category_data.last_cycle_tick = current_tick
persist_state()
end
timeout_ids[category] = dfhack.timeout(timeout_ticks, 'ticks', curry(event_loop, category))
end
-- launches timer. first cycle will be after the configured frequency
local function start_category(category, category_data, current_tick)
category_data = category_data or state.categories[category]
category_data.last_cycle_tick = current_tick or get_normalized_tick()
event_loop(category)
end
local function stop_category(category)
local timeout_id = timeout_ids[category]
if timeout_id then
dfhack.timeout_active(timeout_id, nil) -- cancel callback
timeout_ids[category] = nil
end
end
local function do_enable()
if state.enabled then return end
state.enabled = true
local current_tick = get_normalized_tick()
for _,category in ipairs(categories) do
local category_data = state.categories[category]
if category_data.enabled then
start_category(category, category_data, current_tick)
end
end
end
local function do_disable()
if not state.enabled then return end
state.enabled = false
for _,category in ipairs(categories) do
stop_category(category)
end
end
dfhack.onStateChange[GLOBAL_KEY] = function(sc)
if sc == SC_MAP_UNLOADED then
do_disable()
return
end
if sc ~= SC_MAP_LOADED or not dfhack.world.isFortressMode() then
return
end
state = get_default_state()
utils.assign(state, dfhack.persistent.getSiteData(GLOBAL_KEY, state))
for _,category in ipairs(categories) do
event_loop(category)
end
end
---------------------
-- CLI
if dfhack_flags.module then
return
end
if dfhack_flags.enable then
if dfhack_flags.enable_state then
do_enable()
else
do_disable()
end
end
local function parse_categories(arg)
local list = {}
for _,v in ipairs(argparse.stringList(arg)) do
if aliases[v] then
for _,alias in ipairs(aliases[v]) do
table.insert(list, alias)
end
elseif category_fns[v] then
table.insert(list, v)
else
qerror(('unrecognized category: "%s"'):format(v))
end
end
if #list == 0 then
qerror('no categories specified')
end
return list
end
local function status()
local running_str = state.enabled and 'Running' or 'Would run'
print(('deteriorate is %s'):format(state.enabled and 'enabled' or 'disabled'))
print()
for _,category in ipairs(categories) do
local status_str = 'Stopped'
local category_data = state.categories[category]
if category_data.enabled then
status_str = ('%s every %s day%s') :format(running_str,
category_data.frequency, category_data.frequency == 1 and '' or 's')
end
print(('%18s: %s'):format(category, status_str))
end
end
local help = false
local positionals = argparse.processArgsGetopt({...}, {
{'h', 'help', handler=function() help = true end},
})
local command = table.remove(positionals, 1)
if command == 'help' or help then
print(dfhack.script_help())
return
end
if not command or command == 'status' then
status()
elseif command == 'enable' then
local cats = parse_categories(positionals[1])
for _,v in ipairs(cats) do
if state.categories[v].enabled then
goto continue
end
state.categories[v].enabled = true
if state.enabled then
start_category(v)
end
::continue::
end
elseif command == 'disable' then
local cats = parse_categories(positionals[1])
for _,v in ipairs(cats) do
if not state.categories[v].enabled then
goto continue
end
state.categories[v].enabled = false
if state.enabled then
stop_category(v)
end
::continue::
end
elseif command == 'frequency' or command == 'freq' then
local freq = tonumber(positionals[1])
if not freq or freq <= 0 then
qerror('frequency must be greater than 0')
end
local cats = parse_categories(positionals[2])
for _,v in ipairs(cats) do
state.categories[v].frequency = freq
if state.enabled then
stop_category(v)
start_category(v)
end
end
elseif command == 'now' then
local cats = parse_categories(positionals[1])
local count = 0
for _,v in ipairs(cats) do
count = count + category_fns[v](true)
end
print(('Deteriorated %d item%s'):format(count, count == 1 and '' or 's'))
else
qerror('unrecognized command: "' .. command .. '"')
end
persist_state()