forked from DFHack/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent.lua
More file actions
267 lines (251 loc) · 11.7 KB
/
event.lua
File metadata and controls
267 lines (251 loc) · 11.7 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
-- event logic for the nestboxes
--@ module = true
local utils = require("utils")
local nestboxes_common = reqscript("internal/nestboxes/common")
local print_local = nestboxes_common.print_local
local print_details = nestboxes_common.print_details
local handle_error = nestboxes_common.handle_error
---------------------------------------------------------------------------------------------------
--ITEM_CREATED event handling functions
local function copy_egg_fields(source_egg, target_egg)
print_details("start copy_egg_fields")
target_egg.incubation_counter = source_egg.incubation_counter
print_details("incubation_counter done")
target_egg.egg_flags = utils.clone(source_egg.egg_flags, true)
target_egg.hatchling_flags1 = utils.clone(source_egg.hatchling_flags1, true)
target_egg.hatchling_flags2 = utils.clone(source_egg.hatchling_flags2, true)
target_egg.hatchling_flags3 = utils.clone(source_egg.hatchling_flags3, true)
target_egg.hatchling_flags4 = utils.clone(source_egg.hatchling_flags4, true)
print_details("flags done")
target_egg.hatchling_training_level = utils.clone(source_egg.hatchling_training_level, true)
utils.assign(target_egg.hatchling_animal_population, source_egg.hatchling_animal_population)
print_details("hatchling_animal_population done")
target_egg.hatchling_mother_id = source_egg.hatchling_mother_id
print_details("hatchling_mother_id done")
target_egg.mother_hf = source_egg.mother_hf
target_egg.father_hf = source_egg.mother_hf
print_details("mother_hf father_hf done")
target_egg.mothers_caste = source_egg.mothers_caste
target_egg.fathers_caste = source_egg.fathers_caste
print_details("mothers_caste fathers_caste done")
target_egg.mothers_genes = source_egg.mothers_genes
target_egg.fathers_genes = source_egg.fathers_genes
print_details("mothers_genes fathers_genes done")
target_egg.hatchling_civ_id = source_egg.hatchling_civ_id
print_details("hatchling_civ_id done")
print_details("end copy_egg_fields")
end
---------------------------------------------------------------------------------------------------
local function resize_egg_stack(egg_stack, new_stack_size)
print_details("start resize_egg_stack")
egg_stack.stack_size = new_stack_size
--TODO check if weight or size need adjustment
print_details("end resize_egg_stack")
end
---------------------------------------------------------------------------------------------------
local function create_new_egg_stack(original_eggs, new_stack_count)
print_details("start create_new_egg_stack")
print_details("about to split create new egg stack")
print_details(("type= %s"):format(original_eggs:getType()))
print_details(("creature= %s"):format(original_eggs.race))
print_details(("caste= %s "):format(original_eggs.caste))
print_details(("stack size for new eggs = %s "):format(new_stack_count))
local created_items =
dfhack.items.createItem(
df.unit.find(original_eggs.hatchling_mother_id),
original_eggs:getType(),
-1,
original_eggs.race,
original_eggs.caste
)
print_details("created new egg stack")
local created_egg_stack = created_items[0] or created_items[1]
print_details(df.creature_raw.find(created_egg_stack.race).creature_id)
print_details("about to copy fields from orginal eggs")
copy_egg_fields(original_eggs, created_egg_stack)
print_details("about to resize new egg stack")
resize_egg_stack(created_egg_stack, new_stack_count)
print_details("about to move new stack to nestbox")
if dfhack.items.moveToBuilding(created_egg_stack, dfhack.items.getHolderBuilding(original_eggs)) then
print_details("moved new egg stack to nestbox")
else
print_local("move of separated eggs to nestbox failed")
end
print_details("end create_new_egg_stack")
end
---------------------------------------------------------------------------------------------------
local function split_egg_stack(source_egg_stack, to_be_left_in_source_stack)
print_details("start split_egg_stack")
local egg_count_in_new_stack_size = source_egg_stack.stack_size - to_be_left_in_source_stack
if egg_count_in_new_stack_size > 0 then
create_new_egg_stack(source_egg_stack, egg_count_in_new_stack_size)
resize_egg_stack(source_egg_stack, to_be_left_in_source_stack)
else
print_details("nothing to do, wrong egg_count_in_new_stack_size")
end
print_details("end split_egg_stack")
end
---------------------------------------------------------------------------------------------------
local function count_forbidden_eggs_for_race_in_claimed_nestobxes(race)
print_details(("start count_forbidden_eggs_for_race_in_claimed_nestobxes"))
local eggs_count = 0
for _, nestbox in ipairs(df.global.world.buildings.other.NEST_BOX) do
if nestbox.claimed_by ~= -1 then
print_details(("Found claimed nextbox"))
for _, nestbox_contained_item in ipairs(nestbox.contained_items) do
if nestbox_contained_item.use_mode == df.building_item_role_type.TEMP then
print_details(("Found claimed nextbox containing items"))
if df.item_type.EGG == nestbox_contained_item.item:getType() then
print_details(("Found claimed nextbox containing items that are eggs"))
if nestbox_contained_item.item.egg_flags.fertile and nestbox_contained_item.item.flags.forbid then
print_details(("Eggs are fertile and forbidden"))
if nestbox_contained_item.item.race == race then
print_details(("Eggs belong to %s"):format(race))
print_details(
("eggs_count %s + new %s"):format(
eggs_count,
nestbox_contained_item.item.stack_size
)
)
eggs_count = eggs_count + nestbox_contained_item.item.stack_size
print_details(("eggs_count after adding current nestbox %s "):format(eggs_count))
end
end
end
end
end
end
end
print_details(("end count_forbidden_eggs_for_race_in_claimed_nestobxes"))
return eggs_count
end
---------------------------------------------------------------------------------------------------
local function is_valid_animal(unit)
return unit and dfhack.units.isActive(unit) and dfhack.units.isAnimal(unit) and dfhack.units.isFortControlled(unit) and
dfhack.units.isTame(unit) and
not dfhack.units.isDead(unit)
end
---------------------------------------------------------------------------------------------------
local function count_live_animals(race, count_children, count_adults)
print_details(("start count_live_animals"))
if count_adults then
print_details(("we are counting adults for %s"):format(race))
end
if count_children then
print_details(("we are counting children and babies for %s"):format(race))
end
local count = 0
if not count_adults and not count_children then
print_details(("end 1 count_live_animals"))
return count
end
for _, unit in ipairs(df.global.world.units.active) do
if
race == unit.race and is_valid_animal(unit) and
((count_adults and dfhack.units.isAdult(unit)) or
(count_children and (dfhack.units.isChild(unit) or dfhack.units.isBaby(unit))))
then
count = count + 1
end
end
print_details(("found %s life animals"):format(count))
print_details(("end 2 count_live_animals"))
return count
end
---------------------------------------------------------------------------------------------------
function validate_eggs(eggs)
if not eggs.egg_flags.fertile then
print_details("Newly laid eggs are not fertile, do nothing")
return false
end
local should_be_nestbox = dfhack.items.getHolderBuilding(eggs)
if should_be_nestbox ~= nil then
for _, nestbox in ipairs(df.global.world.buildings.other.NEST_BOX) do
if nestbox == should_be_nestbox then
print_details("Found nestbox, continue with egg handling")
return true
end
end
print_details("Newly laid eggs are in building different than nestbox, we were to late")
return false
else
print_details("Newly laid eggs are not in building, we were to late")
return false
end
return true
end
---------------------------------------------------------------------------------------------------
function handle_eggs(eggs, race_config, split_stacks)
print_details(("start handle_eggs"))
local race = eggs.race
local max_eggs = race_config[1]
local count_children = race_config[2]
local count_adults = race_config[3]
local ignore = race_config[4]
if ignore then
print_details(("race is ignored, nothing to do here"))
return
end
print_details(("max_eggs %s "):format(max_eggs))
print_details(("count_children %s "):format(count_children))
print_details(("count_adults %s "):format(count_adults))
local current_eggs = eggs.stack_size
local total_count = current_eggs
total_count = total_count + count_forbidden_eggs_for_race_in_claimed_nestobxes(race)
if total_count - current_eggs < max_eggs then
print_details(
("Total count for %s only existing eggs is %s, about to count life animals if enabled"):format(
race,
total_count - current_eggs
)
)
total_count = total_count + count_live_animals(race, count_children, count_adults)
else
print_details(
("Total count for %s eggs only is %s greater than maximum %s, no need to count life animals"):format(
race,
total_count,
max_eggs
)
)
print_details(("end 1 handle_eggs"))
return
end
print_details(("Total count for %s eggs is %s"):format(race, total_count))
if total_count - current_eggs < max_eggs then
local egg_count_to_leave_in_source_stack = current_eggs
if split_stacks and total_count > max_eggs then
egg_count_to_leave_in_source_stack = max_eggs - total_count + current_eggs
split_egg_stack(eggs, egg_count_to_leave_in_source_stack)
end
eggs.flags.forbid = true
if eggs.flags.in_job then
local job_ref = dfhack.items.getSpecificRef(eggs, df.specific_ref_type.JOB)
if job_ref then
print_details(("About to remove job related to egg(s)"))
dfhack.job.removeJob(job_ref.data.job)
eggs.flags.in_job = false
end
end
print_local(
("Previously existing %s egg(s) is %s lower than maximum %s , forbidden %s egg(s) out of %s new"):format(
race,
total_count - current_eggs,
max_eggs,
egg_count_to_leave_in_source_stack,
current_eggs
)
)
else
print_local(
("Total count for %s egg(s) is %s over maximum %s, newly laid egg(s) %s , no action taken."):format(
race,
total_count,
max_eggs,
current_eggs
)
)
end
print_details(("end 2 handle_eggs"))
end
---------------------------------------------------------------------------------------------------