-
Notifications
You must be signed in to change notification settings - Fork 199
/
Copy pathassign-preferences.lua
530 lines (502 loc) · 19.9 KB
/
assign-preferences.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
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
-- Change the preferences of a unit.
--@ module = true
local utils = require("utils")
local valid_args = utils.invert({
'help',
'unit',
'show',
'likematerial',
'likecreature',
'likefood',
'hatecreature',
'likeitem',
'likeplant',
'liketree',
'likecolor',
'likeshape',
'reset',
})
-- ----------------------------------------------- UTILITY FUNCTIONS ------------------------------------------------ --
local function print_yellow(text)
dfhack.color(COLOR_YELLOW)
print(text)
dfhack.color(-1)
end
local function format_preference(pref, index)
print(string.format("Preference #%d:", index))
local pref_type = df.unit_preference.T_type[pref.type]
local description = ""
if pref_type == "LikeMaterial" then
description = "Likes material: " .. dfhack.matinfo.getToken(pref.mattype, pref.matindex)
elseif pref_type == "LikeFood" then
description = "Likes food: " .. dfhack.matinfo.getToken(pref.mattype, pref.matindex)
elseif pref_type == "LikeItem" then
description = "Likes item type: " .. tostring(pref.item_type)
elseif pref_type == "LikePlant" then
description = "Likes plant: " .. dfhack.matinfo.getToken(pref.mattype, pref.matindex)
elseif pref_type == "HateCreature" then
description = "Hates creature: " .. df.global.world.raws.creatures.all[pref.creature_id].creature_id
elseif pref_type == "LikeColor" then
description = "Likes color: " .. df.global.world.raws.descriptors.colors[pref.color_id].id
elseif pref_type == "LikeShape" then
description = "Likes shape: " .. df.global.world.raws.descriptors.shapes[pref.shape_id].id
elseif pref_type == "LikePoeticForm" then
description = "Likes poetic form: " .. dfhack.translation.translateName(df.global.world.poetic_forms.all[pref.poetic_form_id].name, true)
elseif pref_type == "LikeMusicalForm" then
description = "Likes musical form: " .. dfhack.translation.translateName(df.global.world.musical_forms.all[pref.musical_form_id].name, true)
elseif pref_type == "LikeDanceForm" then
description = "Likes dance form: " .. dfhack.translation.translateName(df.global.world.dance_forms.all[pref.dance_form_id].name, true)
else
description = "Unknown preference type: " .. tostring(pref.type)
end
print(description)
end
-- initialise random number generator
local rng = dfhack.random.new()
-- ----------------------------------------------- ASSIGN PREFERENCES ----------------------------------------------- --
--[[
This table stores all the information needed to instantiate a new df.unit_preference object.
The keys are the names of the different types of preferences, the values are functions that
return a table with all the fields of a df.unit_preference object and their values.
I find it easier to just overwrite every single field instead of keeping track of the default
values assigned to the field at object creation.
--]]
local preference_functions = {
-- ---------------- LIKEMATERIAL ---------------- --
LIKEMATERIAL = function(token)
local mat_info = dfhack.matinfo.find(token)
local ret = {}
if mat_info then
ret = { --luacheck:retype
type = df.unit_preference.T_type.LikeMaterial,
item_type = -1,
creature_id = -1,
color_id = -1,
shape_id = -1,
plant_id = -1,
poetic_form_id = -1,
musical_form_id = -1,
dance_form_id = -1,
item_subtype = -1,
mattype = mat_info.type,
matindex = mat_info.index,
mat_state = 0,
active = true,
prefstring_seed = rng:random()
}
else
print_yellow("WARNING: '" .. token .. "' does not seem to be a valid material token. Skipping...")
end
return ret
end,
-- ---------------- LIKECREATURE ---------------- --
LIKECREATURE = function(token)
local creature_id
local parts = token:split(":")
if #parts == 1 then
creature_id = parts[1]
else
creature_id = parts[2]
end
local index = utils.linear_index(df.global.world.raws.creatures.all, creature_id, "creature_id")
if index then
return {
type = df.unit_preference.T_type.LikeCreature,
item_type = index,
creature_id = index,
color_id = index,
shape_id = index,
plant_id = index,
poetic_form_id = index,
musical_form_id = index,
dance_form_id = index,
item_subtype = -1,
mattype = -1,
matindex = -1,
mat_state = 0,
active = true,
prefstring_seed = rng:random()
}
else
print_yellow("WARNING: '" .. token .. "' does not seem to be a valid creature token. Skipping...")
return {}
end
end,
-- ---------------- LIKEFOOD ---------------- --
LIKEFOOD = function(token)
local mat_info = dfhack.matinfo.find(token)
if not mat_info then
goto error
end
do
local item_type
local item_subtype = -1
local food_mat_index = mat_info.material.food_mat_index
-- FIXME: is fish missing? Apparently it's considered meat...
-- FIXME: maybe automatize this process instead of using a chain of if-elseif statements?
if food_mat_index.Meat > -1 then
item_type = df.item_type.MEAT
elseif food_mat_index.EdibleCheese > -1 then
item_type = df.item_type.CHEESE
elseif food_mat_index.EdiblePlant > -1 then
item_type = df.item_type.PLANT
elseif food_mat_index.AnyDrink > -1 then
item_type = df.item_type.DRINK
elseif food_mat_index.CookableLiquid > -1 then
item_type = df.item_type.LIQUID_MISC
elseif food_mat_index.CookablePowder > -1 then
item_type = df.item_type.POWDER_MISC
elseif food_mat_index.CookableSeed > -1 then
item_type = df.item_type.SEEDS
elseif food_mat_index.CookablePlantGrowth > -1 then
--[[
In case of plant growths, "mat_info" stores the item type as a specific subtype ("FLOWER", or "FRUIT",
etc.) instead of the generic "PLANT_GROWTH" item type. Also, the IDs of the different types of growths
are sometimes stored in the plural form and sometimes in the singular form, so we need to know what to
look for when we try to associate the item_type to the growth_id.
--]]
local growths = {
-- item_id = growth_id, as returned by dfhack.matinfo.find()
BULB = "BULBS",
FLOWER = "FLOWER",
LEAF = "LEAVES",
NUT = "NUT",
POD = "POD",
FRUIT = "FRUIT",
}
local item_type_str = mat_info.material.id
if growths[item_type_str] then
local growth_id = growths[item_type_str]
for _, growth in ipairs(mat_info.plant.growths) do
if growth_id == growth.id then
item_type = growth.item_type
item_subtype = growth.item_subtype
end
end
end
end
if item_type then
return {
type = df.unit_preference.T_type.LikeFood,
item_type = item_type,
creature_id = item_type,
color_id = item_type,
shape_id = item_type,
plant_id = item_type,
poetic_form_id = item_type,
musical_form_id = item_type,
dance_form_id = item_type,
item_subtype = item_subtype,
mattype = mat_info.type,
matindex = mat_info.index,
mat_state = 1,
active = true,
prefstring_seed = rng:random()
}
end
end
:: error ::
print_yellow("WARNING: '" .. token .. "' does not seem to be a valid food token. Skipping...")
return {}
end,
-- ---------------- HATECREATURE ---------------- --
HATECREATURE = function(token)
local creature_id
local parts = token:split(":")
if #parts == 1 then
creature_id = parts[1]
else
creature_id = parts[2]
end
local index = utils.linear_index(df.global.world.raws.creatures.all, creature_id, "creature_id")
if index then
return {
type = df.unit_preference.T_type.HateCreature,
item_type = index,
creature_id = index,
color_id = index,
shape_id = index,
plant_id = index,
poetic_form_id = index,
musical_form_id = index,
dance_form_id = index,
item_subtype = -1,
mattype = -1,
matindex = -1,
mat_state = 0,
active = true,
prefstring_seed = rng:random()
}
else
print_yellow("WARNING: '" .. token .. "' does not seem to be a valid creature token. Skipping...")
return {}
end
end,
-- ---------------- LIKEITEM ---------------- --
LIKEITEM = function(token)
local item_type
local item_subtype = -1
local parts = token:split(":")
if #parts == 1 then
item_type = df.item_type[parts[1]]
else
-- the token is someting like ITEM_WEAPON:ITEM_WEAPON_AXE_BATTLE
item_type = df.item_type[string.gsub(parts[1], "^ITEM_", "")]
local _, item = utils.linear_index(df.global.world.raws.itemdefs.all, parts[2], "id")
if item then
item_subtype = item.subtype
else
goto error
end
end
do
if item_type then
return {
type = df.unit_preference.T_type.LikeItem,
item_type = item_type,
creature_id = item_type,
color_id = item_type,
shape_id = item_type,
plant_id = item_type,
poetic_form_id = item_type,
musical_form_id = item_type,
dance_form_id = item_type,
item_subtype = item_subtype,
mattype = -1,
matindex = -1,
mat_state = 0,
active = true,
prefstring_seed = rng:random()
}
end
end
:: error ::
print_yellow("WARNING: '" .. token .. "' does not seem to be a valid item token. Skipping...")
return {}
end,
-- ---------------- LIKEPLANT ---------------- --
LIKEPLANT = function(token)
local plant_id
local parts = token:split(":")
if #parts == 1 then
plant_id = parts[1]
else
plant_id = parts[2]
end
local index = utils.linear_index(df.global.world.raws.plants.all, plant_id, "id")
if index then
return {
type = df.unit_preference.T_type.LikePlant,
item_type = index,
creature_id = index,
color_id = index,
shape_id = index,
plant_id = index,
poetic_form_id = index,
musical_form_id = index,
dance_form_id = index,
item_subtype = -1,
mattype = -1,
matindex = -1,
mat_state = 0,
active = true,
prefstring_seed = rng:random()
}
else
print_yellow("WARNING: '" .. token .. "' does not seem to be a valid plant token. Skipping...")
return {}
end
end,
-- ---------------- LIKETREE ---------------- --
LIKETREE = function(token)
local plant_id
local parts = token:split(":")
if #parts == 1 then
plant_id = parts[1]
else
plant_id = parts[2]
end
local index = utils.linear_index(df.global.world.raws.plants.all, plant_id, "id")
if index then
return {
type = df.unit_preference.T_type.LikeTree,
item_type = index,
creature_id = index,
color_id = index,
shape_id = index,
plant_id = index,
poetic_form_id = index,
musical_form_id = index,
dance_form_id = index,
item_subtype = -1,
mattype = -1,
matindex = -1,
mat_state = 0,
active = true,
prefstring_seed = rng:random()
}
else
print_yellow("WARNING: '" .. token .. "' does not seem to be a valid plant token. Skipping...")
return {}
end
end,
-- ---------------- LIKECOLOR ---------------- --
LIKECOLOR = function(token)
local color_name
local parts = token:split(":")
if #parts == 1 then
color_name = parts[1]
else
color_name = parts[2]
end
-- f.global.world.raws.descriptors.colors is ordered by id, we can use binary search
local _, found, index = utils.binsearch(df.global.world.raws.descriptors.colors, color_name, "id")
if found then
return {
type = df.unit_preference.T_type.LikeColor,
item_type = index,
creature_id = index,
color_id = index,
shape_id = index,
plant_id = index,
poetic_form_id = index,
musical_form_id = index,
dance_form_id = index,
item_subtype = -1,
mattype = -1,
matindex = -1,
mat_state = 0,
active = true,
prefstring_seed = rng:random()
}
else
print_yellow("WARNING: '" .. token .. "' does not seem to be a valid color token. Skipping...")
return {}
end
end,
-- ---------------- LIKESHAPE ---------------- --
LIKESHAPE = function(token)
local shape_name
local parts = token:split(":")
if #parts == 1 then
shape_name = parts[1]
else
shape_name = parts[2]
end
local index, _ = utils.linear_index(df.global.world.raws.descriptors.shapes, shape_name, "id")
if index then
return {
type = df.unit_preference.T_type.LikeShape,
item_type = index,
creature_id = index,
color_id = index,
shape_id = index,
plant_id = index,
poetic_form_id = index,
musical_form_id = index,
dance_form_id = index,
item_subtype = -1,
mattype = -1,
matindex = -1,
mat_state = 0,
active = true,
prefstring_seed = rng:random()
}
else
print_yellow("WARNING: '" .. token .. "' does not seem to be a valid shape token. Skipping...")
return {}
end
end,
}
--- Assign the given preferences to a unit, clearing all the other preferences if requested.
--- :preferences: nil, or a table. The fields have the preference type as key and an array of preference tokens
--- as value. If the token is just one, the field can be in a simple key=value format.
--- :unit: a valid unit id, a df.unit object, or nil. If nil, the currently selected unit will be targeted.
--- :reset: boolean, or nil.
function assign(preferences, unit, reset)
assert(not preferences or type(preferences) == "table")
assert(not unit or type(unit) == "number" or df.unit:is_instance(unit))
assert(not reset or type(reset) == "boolean")
local preferences = preferences or {} --as:string[][]
reset = reset or false
if type(unit) == "number" then
unit = df.unit.find(tonumber(unit)) --luacheck:retype
end
unit = unit or dfhack.gui.getSelectedUnit(true)
if not unit then
qerror("No unit found.")
end
-- clear preferences
if reset then
unit.status.current_soul.preferences = {}
end
-- assign preferences
local unit_preferences = unit.status.current_soul.preferences -- store the userdata in a variable for convenience
for preference_type, preference_tokens in pairs(preferences) do
assert(type(preference_tokens) == "table" or type(preference_tokens) == "string")
local funct = preference_functions[preference_type:upper()]
if type(preference_tokens) == "string" then --luacheck:skip
preference_tokens = {preference_tokens}
end
for _, token in ipairs(preference_tokens) do
assert(type(token) == "string")
local new_preference = df.unit_preference:new()
local values = funct(token:upper())
for field, value in pairs(values) do
if value then
new_preference[field] = value
else
print_yellow("WARNING: unable to calculate a value for field '" .. field .. "'. Skipping...")
goto next_preference
end
end
-- TODO: should we insert preferences following the order in which DF presents them?
unit_preferences:insert(#unit_preferences, new_preference)
end
:: next_preference ::
end
end
-- ----------------------------------------------- SHOW PREF UTILITY ------------------------------------------------ --
local function showPreferences(unit)
assert(not unit or type(unit) == "number" or df.unit:is_instance(unit))
unit = unit or dfhack.gui.getSelectedUnit(true)
if not unit then
qerror("No unit found.")
end
for i, pref in ipairs(unit.status.current_soul.preferences) do
format_preference(pref, i)
end
end
-- ------------------------------------------------------ MAIN ------------------------------------------------------ --
local function main(...)
local args = utils.processArgs({ ... }, valid_args)
if args.help then
print(dfhack.script_help())
return
end
local unit
if args.unit then
unit = tonumber(args.unit)
if not unit then
qerror("'" .. args.unit .. "' is not a valid unit ID.")
end
end
local reset = false
if args.reset then
reset = true
end
if args.show then
showPreferences(unit)
return
end
-- parse preferences
args.unit = nil -- remove from args table
args.reset = nil -- remove from args table
args.show = nil -- remove from args table
local preferences = {}
utils.assign(preferences, args)
assign(preferences, unit, reset)
end
if not dfhack_flags.module then
main(...)
end