@@ -7,15 +7,30 @@ local utils = require('utils')
7
7
local GLOBAL_KEY = " eggwatch"
8
8
local EVENT_FREQ = 7
9
9
local print_prefix = " eggwatch: "
10
+ local default_table = {10 , false , false }
11
+ local stringtoboolean = { [" true" ]= true , [" false" ]= false , [" 1" ] = true , [" 0" ] = false , [" Y" ] = true , [" N" ] = false }
10
12
11
- local default_table = {}
12
- default_table .DEFAULT = {10 , false , false }
13
+ function dump (o )
14
+ if type (o ) == " table" then
15
+ local s = " { "
16
+ for k , v in pairs (o ) do
17
+ if type (k ) ~= " number" then
18
+ k = ' "' .. k .. ' "'
19
+ end
20
+ s = s .. " [" .. k .. " ] = " .. dump (v ) .. " ,"
21
+ end
22
+ return s .. " } "
23
+ else
24
+ return tostring (o )
25
+ end
26
+ end
13
27
14
28
local function get_default_state ()
15
29
return {
16
30
enabled = false ,
17
31
verbose = false ,
18
- target_eggs_count_per_race = default_table
32
+ default = default_table ,
33
+ target_eggs_count_per_race = {}
19
34
}
20
35
end
21
36
32
47
local function handle_error (text )
33
48
qerror (text )
34
49
end
35
-
50
+ local function format_target_count_row (header , row )
51
+ return header .. ' : ' .. ' target count: ' .. row [1 ] .. ' ; count children: ' .. tostring (row [2 ]) .. ' ; count adults: ' .. tostring (row [3 ])
52
+ end
36
53
local function print_status ()
37
54
print_local ((" eggwatch is currently %s." ):format (state .enabled and " enabled" or " disabled" ))
55
+ print_local (format_target_count_row (' Default' , state .default ))
56
+ if state .target_eggs_count_per_race ~= nil then
57
+ for k , v in pairs (state .target_eggs_count_per_race ) do
58
+ print_local (format_target_count_row (df .global .world .raws .creatures .all [k ].creature_id , v ))
59
+ end
60
+ end
38
61
if state .verbose then
39
62
print_local (" eggwatch is in verbose mode" )
40
63
end
@@ -46,15 +69,39 @@ local function print_detalis(details)
46
69
end
47
70
end
48
71
72
+
49
73
local function persist_state ()
50
- dfhack .persistent .saveSiteData (GLOBAL_KEY , state )
74
+ local state_to_persist = {}
75
+ state_to_persist .enabled = state .enabled
76
+ state_to_persist .verbose = state .verbose
77
+ state_to_persist .default = state .default
78
+ state_to_persist .target_eggs_count_per_race = {}
79
+ if state .target_eggs_count_per_race ~= nil then
80
+ for k , v in pairs (state .target_eggs_count_per_race ) do
81
+ state_to_persist .target_eggs_count_per_race [tostring (k )]= v
82
+ end
83
+ end
84
+ dfhack .persistent .saveSiteData (GLOBAL_KEY , state_to_persist )
51
85
end
52
86
53
87
--- Load the saved state of the script
54
88
local function load_state ()
55
89
-- load persistent data
56
- state = get_default_state ()
57
- utils .assign (state , dfhack .persistent .getSiteData (GLOBAL_KEY , state ))
90
+ local persisted_data = dfhack .persistent .getSiteData (GLOBAL_KEY , persisted_data )
91
+ state = {}
92
+ if persisted_data ~= nil then
93
+ state .enabled = persisted_data .enabled
94
+ state .verbose = persisted_data .verbose
95
+ state .default = persisted_data .default
96
+ state .target_eggs_count_per_race = {}
97
+ if persisted_data .target_eggs_count_per_race ~= nil then
98
+ for k , v in pairs (persisted_data .target_eggs_count_per_race ) do
99
+ state .target_eggs_count_per_race [tonumber (k )]= v
100
+ end
101
+ end
102
+ else
103
+ state = get_default_state ()
104
+ end
58
105
end
59
106
60
107
local function update_event_listener ()
143
190
-- dfhack.items.moveToContainer(created_egg_stack, find_current_nestbox(original_eggs))
144
191
-- end
145
192
146
- local function count_forbidden_eggs_for_race_in_claimed_nestobxes (race_creature_id )
193
+ local function count_forbidden_eggs_for_race_in_claimed_nestobxes (race )
147
194
print_detalis ((" start count_forbidden_eggs_for_race_in_claimed_nestobxes" ))
148
195
local eggs_count = 0
149
196
for _ , nestbox in ipairs (df .global .world .buildings .other .NEST_BOX ) do
@@ -156,8 +203,8 @@ local function count_forbidden_eggs_for_race_in_claimed_nestobxes(race_creature_
156
203
print_detalis ((" Found claimed nextbox containing items that are eggs" ))
157
204
if nestbox_contained_item .item .egg_flags .fertile and nestbox_contained_item .item .flags .forbid then
158
205
print_detalis ((" Eggs are fertile and forbidden" ))
159
- if df . creature_raw . find ( nestbox_contained_item .item .race ). creature_id == race_creature_id then
160
- print_detalis ((" Eggs belong to %s" ):format (race_creature_id ))
206
+ if nestbox_contained_item .item .race == race then
207
+ print_detalis ((" Eggs belong to %s" ):format (race ))
161
208
print_detalis (
162
209
(" eggs_count %s + new %s" ):format (
163
210
eggs_count ,
@@ -177,16 +224,16 @@ local function count_forbidden_eggs_for_race_in_claimed_nestobxes(race_creature_
177
224
return eggs_count
178
225
end
179
226
180
- local function get_config_for_race (race_creature_id )
181
- print_detalis ((" getting config for race %s " ):format (race_creature_id ))
227
+ local function get_config_for_race (race )
228
+ print_detalis ((" getting config for race %s " ):format (race ))
182
229
for k , v in pairs (state .target_eggs_count_per_race ) do
183
- if k == race_creature_id then
230
+ if k == race then
184
231
return v
185
232
end
186
233
end
187
- state .target_eggs_count_per_race [race_creature_id ] = state .target_eggs_count_per_race . DEFAULT
234
+ state .target_eggs_count_per_race [race ] = state .default
188
235
persist_state ()
189
- return state .target_eggs_count_per_race [race_creature_id ]
236
+ return state .target_eggs_count_per_race [race ]
190
237
end
191
238
192
239
local function is_valid_animal (unit )
@@ -198,17 +245,17 @@ local function is_valid_animal(unit)
198
245
not dfhack .units .isDead (unit )
199
246
end
200
247
201
- local function count_live_animals (race_creature_id , count_children , count_adults )
202
- if count_adults then print_detalis ((' we are counting adults for %s' ):format (race_creature_id )) end
203
- if count_children then print_detalis ((' we are counting children and babies for %s' ):format (race_creature_id )) end
248
+ local function count_live_animals (race , count_children , count_adults )
249
+ if count_adults then print_detalis ((' we are counting adults for %s' ):format (race )) end
250
+ if count_children then print_detalis ((' we are counting children and babies for %s' ):format (race )) end
204
251
205
252
local count = 0
206
253
if not count_adults and not count_children then
207
254
return count
208
255
end
209
256
210
257
for _ ,unit in ipairs (df .global .world .units .active ) do
211
- if race_creature_id == df . creature_raw . find ( unit .race ). creature_id
258
+ if race == unit .race
212
259
and is_valid_animal (unit )
213
260
and ( (count_adults and dfhack .units .isAdult (unit ))
214
261
or (count_children and ( dfhack .units .isChild (unit ) or dfhack .units .isBaby (unit )))
@@ -227,8 +274,8 @@ local function handle_eggs(eggs)
227
274
return
228
275
end
229
276
230
- local race_creature_id = df . creature_raw . find ( eggs .race ). creature_id
231
- local race_config = get_config_for_race (race_creature_id )
277
+ local race = eggs .race
278
+ local race_config = get_config_for_race (race )
232
279
local max_eggs = race_config [1 ]
233
280
local count_children = race_config [2 ]
234
281
local count_adults = race_config [3 ]
@@ -240,17 +287,17 @@ local function handle_eggs(eggs)
240
287
local current_eggs = eggs .stack_size
241
288
242
289
local total_count = current_eggs
243
- total_count = total_count + count_forbidden_eggs_for_race_in_claimed_nestobxes (race_creature_id )
290
+ total_count = total_count + count_forbidden_eggs_for_race_in_claimed_nestobxes (race )
244
291
245
292
if total_count - current_eggs < max_eggs then
246
- print_detalis ((" Total count for %s only existing eggs is %s, about to count life animals if enabled" ):format (race_creature_id , total_count - current_eggs ))
247
- total_count = total_count + count_live_animals (race_creature_id , count_children , count_adults )
293
+ print_detalis ((" Total count for %s only existing eggs is %s, about to count life animals if enabled" ):format (race , total_count - current_eggs ))
294
+ total_count = total_count + count_live_animals (race , count_children , count_adults )
248
295
else
249
- print_detalis ((" Total count for %s eggs only is %s greater than maximum %s, no need to count life animals" ):format (race_creature_id , total_count , max_eggs ))
296
+ print_detalis ((" Total count for %s eggs only is %s greater than maximum %s, no need to count life animals" ):format (race , total_count , max_eggs ))
250
297
return
251
298
end
252
299
253
- print_detalis ((" Total count for %s eggs is %s" ):format (race_creature_id , total_count ))
300
+ print_detalis ((" Total count for %s eggs is %s" ):format (race , total_count ))
254
301
255
302
if total_count - current_eggs < max_eggs then
256
303
-- ###if possible split egg stack to forbid only part below max change previous condition to total_count < max_eggs
@@ -261,11 +308,11 @@ local function handle_eggs(eggs)
261
308
-- create_new_egg_stack(eggs, remaining_eggs, df.creature_raw.find(eggs.race), race_creature.caste[eggs.caste])
262
309
-- eggs.stack_size = forbid_eggs
263
310
-- eggs.flags.forbid = true
264
- -- print(('Total count for %s eggs is %s over maximum %s , forbidden %s eggs out of clutch of %s.'):format(race_creature_id , total_count, max_eggs, forbid_eggs, current_eggs))
311
+ -- print(('Total count for %s eggs is %s over maximum %s , forbidden %s eggs out of clutch of %s.'):format(race , total_count, max_eggs, forbid_eggs, current_eggs))
265
312
eggs .flags .forbid = true
266
313
print_local (
267
314
(" Previously existing %s eggs is %s lower than maximum %s , forbidden %s new eggs." ):format (
268
- race_creature_id ,
315
+ race ,
269
316
total_count - current_eggs ,
270
317
max_eggs ,
271
318
current_eggs
@@ -274,7 +321,7 @@ local function handle_eggs(eggs)
274
321
else
275
322
print_local (
276
323
(" Total count for %s eggs is %s over maximum %s, newly laid eggs %s , no action taken." ):format (
277
- race_creature_id ,
324
+ race ,
278
325
total_count ,
279
326
max_eggs ,
280
327
current_eggs
@@ -297,45 +344,34 @@ end
297
344
local function validate_creature_id (creature_id )
298
345
for i , c in ipairs (df .global .world .raws .creatures .all ) do
299
346
if c .creature_id == creature_id then
300
- return true
347
+ return i
301
348
end
302
349
end
303
- return false
350
+ return - 1
304
351
end
305
352
306
353
local function set_target (target_race , target_count , count_children , count_adult )
307
- local stringtoboolean = { [" true" ]= true , [" false" ]= false , [" 1" ] = true , [" 0" ] = false , [" Y" ] = true , [" N" ] = false }
308
354
309
355
if target_race == nil or target_race == " " then
310
356
handle_error (' must specify "DEFAULT" or valid creature_id' )
311
357
end
358
+
312
359
local target_race_upper = string.upper (target_race )
360
+
313
361
if tonumber (target_count ) == nil or tonumber (target_count ) < 0 then
314
362
handle_error (" No valid target count specified" )
315
363
end
316
- if target_race_upper == " DEFAULT" or validate_creature_id (target_race_upper ) then
317
- state .target_eggs_count_per_race [target_race_upper ] = {tonumber (target_count ), stringtoboolean [count_children ] or false , stringtoboolean [count_adult ] or false }
364
+ local race = validate_creature_id (target_race_upper )
365
+ if target_race_upper == " DEFAULT" then
366
+ state .default = {tonumber (target_count ), stringtoboolean [count_children ] or false , stringtoboolean [count_adult ] or false }
367
+ elseif race >= 0 then
368
+ print (race )
369
+ state .target_eggs_count_per_race [race ] = {tonumber (target_count ), stringtoboolean [count_children ] or false , stringtoboolean [count_adult ] or false }
318
370
else
319
371
handle_error (' must specify "DEFAULT" or valid creature_id' )
320
372
end
321
-
322
- print_local (dump (state .target_eggs_count_per_race ))
323
373
end
324
374
325
- function dump (o )
326
- if type (o ) == " table" then
327
- local s = " { "
328
- for k , v in pairs (o ) do
329
- if type (k ) ~= " number" then
330
- k = ' "' .. k .. ' "'
331
- end
332
- s = s .. " [" .. k .. " ] = " .. dump (v ) .. " ,"
333
- end
334
- return s .. " } "
335
- else
336
- return tostring (o )
337
- end
338
- end
339
375
340
376
if df .global .gamemode ~= df .game_mode .DWARF or not dfhack .isMapLoaded () then
341
377
dfhack .printerr (" eggwatch needs a loaded fortress to work" )
@@ -379,17 +415,11 @@ elseif command == "verbose" then
379
415
state .verbose = not state .verbose
380
416
print_status ()
381
417
elseif command == ' clear' then
382
- state .target_eggs_count_per_race = default_table
383
- elseif command == ' hardreset' then
384
418
state = get_default_state ()
385
419
update_event_listener ()
386
420
elseif not command or command == " status" then
387
421
print_status ()
388
- -- print_local(dump(state.enabled))
389
- -- print_local(dump(state.verbose))
390
- -- print_local(dump(state))
391
- print_local (dump (state .target_eggs_count_per_race ))
392
422
elseif (command ~= ' enable' or command ~= ' disable' ) and not dfhack_flags .enable then
393
423
handle_error ((' Command "%s" is not recognized' ):format (command ))
394
424
end
395
- persist_state ()
425
+ persist_state ()
0 commit comments