11-- Change the preferences of a unit.
22-- @ module = true
33
4- local help = [====[
5-
6- assign-preferences
7- ==================
8- A script to change the preferences of a unit.
9-
10- Preferences are classified into 12 types. The first 9 are:
11-
12- * like material;
13- * like creature;
14- * like food;
15- * hate creature;
16- * like item;
17- * like plant;
18- * like tree;
19- * like colour;
20- * like shape.
21-
22- These can be changed using this script.
23-
24- The remaining three are not currently managed by this script,
25- and are: like poetic form, like musical form, like dance form.
26-
27- To produce the correct description in the "thoughts and preferences"
28- page, you must specify the particular type of preference. For
29- each type, a description is provided in the section below.
30-
31- You will need to know the token of the object you want your dwarf to like.
32- You can find them in the wiki, otherwise in the folder "/raw/objects/" under
33- the main DF directory you will find all the raws defined in the game.
34-
35- For more information:
36- https://dwarffortresswiki.org/index.php/DF2014:Preferences
37-
38- Usage:
39-
40- ``-help``:
41- print the help page.
42-
43- ``-unit <UNIT_ID>``:
44- set the target unit ID. If not present, the
45- currently selected unit will be the target.
46-
47- ``-likematerial [ <TOKEN> <TOKEN> <...> ]``:
48- usually a type of stone, a type of metal and a type
49- of gem, plus it can also be a type of wood, a type of
50- glass, a type of leather, a type of horn, a type of
51- pearl, a type of ivory, a decoration material - coral
52- or amber, a type of bone, a type of shell, a type
53- of silk, a type of yarn, or a type of plant cloth.
54- Write the full tokens.
55- There must be a space before and after each square
56- bracket.
57-
58- ``-likecreature [ <TOKEN> <TOKEN> <...> ]``:
59- one or more creatures liked by the unit. You can
60- just list the species: the creature token will be
61- something similar to ``CREATURE:SPARROW:SKIN``,
62- so the name of the species will be ``SPARROW``. Nothing
63- will stop you to write the full token, if you want: the
64- script will just ignore the first and the last parts.
65- There must be a space before and after each square
66- bracket.
67-
68- ``-likefood [ <TOKEN> <TOKEN> <...> ]``:
69- usually a type of alcohol, plus it can be a type of
70- meat, a type of fish, a type of cheese, a type of edible
71- plant, a cookable plant/creature extract, a cookable
72- mill powder, a cookable plant seed or a cookable plant
73- leaf. Write the full tokens.
74- There must be a space before and after each square
75- bracket.
76-
77- ``-hatecreature [ <TOKEN> <TOKEN> <...> ]``:
78- works the same way as ``-likecreature``, but this time
79- it's one or more creatures that the unit detests. They
80- should be a type of ``HATEABLE`` vermin which isn't already
81- explicitly liked, but no check is performed about this.
82- Like before, you can just list the creature species.
83- There must be a space before and after each square
84- bracket.
85-
86- ``-likeitem [ <TOKEN> <TOKEN> <...> ]``:
87- a kind of weapon, a kind of ammo, a kind of piece of
88- armor, a piece of clothing (including backpacks or
89- quivers), a type of furniture (doors, floodgates, beds,
90- chairs, windows, cages, barrels, tables, coffins,
91- statues, boxes, armor stands, weapon racks, cabinets,
92- bins, hatch covers, grates, querns, millstones, traction
93- benches, or slabs), a kind of craft (figurines, amulets,
94- scepters, crowns, rings, earrings, bracelets, or large
95- gems), or a kind of miscellaneous item (catapult parts,
96- ballista parts, a type of siege ammo, a trap component,
97- coins, anvils, totems, chains, flasks, goblets,
98- buckets, animal traps, an instrument, a toy, splints,
99- crutches, or a tool). The item tokens can be found here:
100- https://dwarffortresswiki.org/index.php/DF2014:Item_token
101- If you want to specify an item subtype, look into the files
102- listed under the column "Subtype" of the wiki page (they are
103- in the "/raw/ojects/" folder), then specify the items using
104- the full tokens found in those files (see examples below).
105- There must be a space before and after each square
106- bracket.
107-
108- ``-likeplant [ <TOKEN> <TOKEN> <...> ]``:
109- works in a similar way as ``-likecreature``, this time
110- with plants. You can just List the plant species (the
111- middle part of the token).
112- There must be a space before and after each square
113- bracket.
114-
115- ``-liketree [ <TOKEN> <TOKEN> <...> ]``:
116- works exactly as ``-likeplant``. I think this
117- preference type is here for backward compatibility (?).
118- You can still use it, however. As before,
119- you can just list the tree (plant) species.
120- There must be a space before and after each square
121- bracket.
122-
123- ``-likecolor [ <TOKEN> <TOKEN> <...> ]``:
124- you can find the color tokens here:
125- https://dwarffortresswiki.org/index.php/DF2014:Color#Color_tokens
126- or inside the "descriptor_color_standard.txt" file
127- (in the "/raw/ojects/" folder). You can use the full token or
128- just the color name.
129- There must be a space before and after each square
130- bracket.
131-
132- ``-likeshape [ <TOKEN> <TOKEN> <...> ]``:
133- I couldn't find a list of shape tokens in the wiki, but you
134- can find them inside the "descriptor_shape_standard.txt"
135- file (in the "/raw/ojects/" folder). You can
136- use the full token or just the shape name.
137- There must be a space before and after each square
138- bracket.
139-
140- ``-reset``:
141- clear all preferences. If the script is called
142- with both this option and one or more preferences,
143- first all the unit preferences will be cleared
144- and then the listed preferences will be added.
145-
146- Examples:
147-
148- * "likes alabaster and willow wood"::
149-
150- assign-preferences -reset -likematerial [ INORGANIC:ALABASTER PLANT:WILLOW:WOOD ]
151-
152- * "likes sparrows for their ..."::
153-
154- assign-preferences -reset -likecreature SPARROW
155-
156- * "prefers to consume dwarven wine and olives"::
157-
158- assign-preferences -reset -likefood [ PLANT:MUSHROOM_HELMET_PLUMP:DRINK PLANT:OLIVE:FRUIT ]
159-
160- * "absolutely detests jumping spiders::
161-
162- assign-preferences -reset -hatecreature SPIDER_JUMPING
163-
164- * "likes logs and battle axes"::
165-
166- assign-preferences -reset -likeitem [ WOOD ITEM_WEAPON:ITEM_WEAPON_AXE_BATTLE ]
167-
168- * "likes straberry plants for their ..."::
169-
170- assign-preferences -reset -likeplant BERRIES_STRAW
171-
172- * "likes oaks for their ..."::
173-
174- assign-preferences -reset -liketree OAK
175-
176- * "likes the color aqua"::
177-
178- assign-preferences -reset -likecolor AQUA
179-
180- * "likes stars"::
181-
182- assign-preferences -reset -likeshape STAR
183-
184- ]====]
185-
1864local utils = require (" utils" )
1875
1886local valid_args = utils .invert ({
1897 ' help' ,
1908 ' unit' ,
9+ ' show' ,
19110 ' likematerial' ,
19211 ' likecreature' ,
19312 ' likefood' ,
@@ -207,6 +26,38 @@ local function print_yellow(text)
20726 dfhack .color (- 1 )
20827end
20928
29+ local function format_preference (pref , index )
30+ print (string.format (" Preference #%d:" , index ))
31+
32+ local pref_type = df .unit_preference .T_type [pref .type ]
33+ local description = " "
34+ if pref_type == " LikeMaterial" then
35+ description = " Likes material: " .. dfhack .matinfo .getToken (pref .mattype , pref .matindex )
36+ elseif pref_type == " LikeFood" then
37+ description = " Likes food: " .. dfhack .matinfo .getToken (pref .mattype , pref .matindex )
38+ elseif pref_type == " LikeItem" then
39+ description = " Likes item type: " .. tostring (pref .item_type )
40+ elseif pref_type == " LikePlant" then
41+ description = " Likes plant: " .. dfhack .matinfo .getToken (pref .mattype , pref .matindex )
42+ elseif pref_type == " HateCreature" then
43+ description = " Hates creature: " .. df .global .world .raws .creatures .all [pref .creature_id ].creature_id
44+ elseif pref_type == " LikeColor" then
45+ description = " Likes color: " .. df .global .world .raws .descriptors .colors [pref .color_id ].id
46+ elseif pref_type == " LikeShape" then
47+ description = " Likes shape: " .. df .global .world .raws .descriptors .shapes [pref .shape_id ].id
48+ elseif pref_type == " LikePoeticForm" then
49+ description = " Likes poetic form: " .. dfhack .translation .translateName (df .global .world .poetic_forms .all [pref .poetic_form_id ].name , true )
50+ elseif pref_type == " LikeMusicalForm" then
51+ description = " Likes musical form: " .. dfhack .translation .translateName (df .global .world .musical_forms .all [pref .musical_form_id ].name , true )
52+ elseif pref_type == " LikeDanceForm" then
53+ description = " Likes dance form: " .. dfhack .translation .translateName (df .global .world .dance_forms .all [pref .dance_form_id ].name , true )
54+ else
55+ description = " Unknown preference type: " .. tostring (pref .type )
56+ end
57+
58+ print (description )
59+ end
60+
21061-- initialise random number generator
21162local rng = dfhack .random .new ()
21263
@@ -624,12 +475,25 @@ function assign(preferences, unit, reset)
624475 end
625476end
626477
478+ -- ----------------------------------------------- SHOW PREF UTILITY ------------------------------------------------ --
479+ local function showPreferences (unit )
480+ assert (not unit or type (unit ) == " number" or df .unit :is_instance (unit ))
481+ unit = unit or dfhack .gui .getSelectedUnit (true )
482+ if not unit then
483+ qerror (" No unit found." )
484+ end
485+
486+ for i , pref in ipairs (unit .status .current_soul .preferences ) do
487+ format_preference (pref , i )
488+ end
489+ end
490+
627491-- ------------------------------------------------------ MAIN ------------------------------------------------------ --
628492local function main (...)
629493 local args = utils .processArgs ({ ... }, valid_args )
630494
631495 if args .help then
632- print (help )
496+ print (dfhack . script_help () )
633497 return
634498 end
635499
@@ -646,9 +510,15 @@ local function main(...)
646510 reset = true
647511 end
648512
513+ if args .show then
514+ showPreferences (unit )
515+ return
516+ end
517+
649518 -- parse preferences
650519 args .unit = nil -- remove from args table
651520 args .reset = nil -- remove from args table
521+ args .show = nil -- remove from args table
652522 local preferences = {}
653523 utils .assign (preferences , args )
654524
0 commit comments