-
Notifications
You must be signed in to change notification settings - Fork 5
/
items.lua
380 lines (300 loc) · 10.9 KB
/
items.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
function item_is_metal(item)
local matinfo = dfhack.matinfo.decode(item:getActualMaterial(), item:getActualMaterialIndex())
return matinfo and (matinfo:getCraftClass() == df.craft_material_class.Metal)
end
-- from plugins/uicommon.h
function item_can_melt(item)
local f = item.flags
if f.in_job or f.hostile or f.on_fire or f.rotten or f.trader or f.construction or f.artifact or f.in_building or f.garbage_collect then
return false
end
local t = item:getType()
if t == df.item_type.BOX or t == df.item_type.BAR then
return false
end
if not item_is_metal(item) then
return false
end
--todo: more checks here ?
return true
end
function item_is_fort_owned(item)
if item.flags.spider_web then
return false
end
if item.flags.trader then
return false
end
if item.flags.in_inventory then
local holder = dfhack.items.getHolderUnit(item)
if holder and not unit_iscitizen(holder) then
return false
end
end
return true
end
function item_is_in_stockpile(item, sp)
local cont = dfhack.items.getContainer(item)
if cont then
return item_is_in_stockpile(cont, sp)
end
if item.pos.z ~= sp.z or item.pos.x < sp.x1 or item.pos.x >= sp.x2 or item.pos.y < sp.y1 or item.pos.y >= sp.y2 then
return false
end
local e = (item.pos.x - sp.x1) + (item.pos.y - sp.y1) * sp.room.width
return (sp.room.extents[e] == 1)
end
--todo: output for stockpiles should be improved before used in app (currently containers and items inside are mixed)
--todo: make this use bldid too
--luacheck: in=number
function building_get_contained_items(bldid)
local ws = screen_main()
if ws._type ~= df.viewscreen_dwarfmodest then
error(errmsg_wrongscreen(ws))
end
if df.global.ui.main.mode ~= 17 or df.global.world.selected_building == nil then
error('no selected building')
end
local ret = {}
local bld = df.global.world.selected_building
if bld._type == df.building_stockpilest then
for i,item in ipairs(df.global.world.items.other.IN_PLAY) do
--todo: check that the item is owned by the civ
if item_is_in_stockpile(item,bld) then
local title = itemname(item, 0, true)
table.insert(ret, { title, item.id, item.flags.whole, item_can_melt(item) })
end
end
else
for i,citem in ipairs(bld.contained_items) do --hint:df.building_actual
local item = citem.item
local title = itemname(item, 0, true)
table.insert(ret, { title, item.id, item.flags.whole, item_can_melt(item) })
end
end
return ret
end
--luacheck: in=number,number,bool
function item_action(itemid, action, value)
local item = df.item:is_instance(itemid) and itemid or df.item.find(itemid) --as:df.item
if not item then
error('no item '..tostring(itemid))
end
value = istrue(value)
if action == df.item_flags.forbid or action == df.item_flags.dump or action == df.item_flags.melt or action == df.item_flags.hidden then
if action == df.item_flags.melt then
if not item_can_melt(item) then
return false
end
if value and not item.flags.melt then
utils.insert_sorted(df.global.world.items.other.ANY_MELT_DESIGNATED, item, 'id')
elseif not value and item.flags.melt then
utils.erase_sorted(df.global.world.items.other.ANY_MELT_DESIGNATED, item, 'id')
end
--todo: any other additional action to update world state?
end
item.flags[action] = value
-- Melt and dump are mutually exclusive
if value then
if action == df.item_flags.dump and item.flags.melt then
item_action(item, item.flags.melt, false)
elseif action == df.item_flags.melt and item.flags.dump then
item_action(item, item.flags.dump, false)
end
end
end
return true
end
item_spatter_sizes = {
{ 0, 'spatter' },
{ 25, 'smear' },
{ 50, 'covering' },
}
--luacheck: in=number
function item_query(itemid)
local item = df.item.find(itemid) --as:df.item_actual
if not item then
error('no item '..tostring(itemid))
end
local dispname,realname = itemname(item, 0, true)
local value = dfhack.items.getValue(item)
if not item.flags.weight_computed then
item:calculateWeight()
end
local itemscnt = 0
local unitscnt = 0
for i,v in ipairs(item.general_refs) do
if v._type == df.general_ref_contains_itemst then
itemscnt = itemscnt + 1
elseif v._type == df.general_ref_contains_unitst then
unitscnt = unitscnt + 1
end
end
--todo: game shows "water covering" BUT "coating of <name>'s elf blood" for the same spatter size
local contaminants = {}
if item.contaminants then
for i,v in ipairs(item.contaminants) do
local mi = dfhack.matinfo.decode(v.mat_type, v.mat_index)
if mi then
local spattersize = ''
for k,w in ripairs(item_spatter_sizes) do
if v.size >= w[1] then
spattersize = ' ' .. w[2]
break
end
end
local creatureprefix = mi.figure and (hfname(mi.figure) .. ' ') or ''
local matprefix = #mi.material.prefix > 0 and (mi.material.prefix .. ' ') or ''
local title = creatureprefix .. matprefix .. mi.material.state_name[v.mat_state] .. spattersize
--todo: any other colors?
--local c = df.global.world.raws.language.colors[mi.material.state_color[v.mat_state]]
local color = 2 + 8 --c.color + c.bold*8
table.insert(contaminants, { title, color })
end
end
end
return { realname or dispname, item.id, item.flags.whole, item_can_melt(item), realname and dispname or mp.NIL, value, item.weight, itemscnt, unitscnt, contaminants }
end
--luacheck: in=number
function item_get_description(itemid)
local item = df.item.find(itemid)
if not item then
error('no item '..tostring(itemid))
end
local itemws = df.viewscreen_itemst:new()
itemws.item = item
gui.simulateInput(itemws, K'ITEM_DESCRIPTION')
df.delete(itemws)
local ws = dfhack.gui.getCurViewscreen() --as:df.viewscreen_textviewerst
local text = ''
for i,v in ipairs(ws.src_text) do
if #v.value > 0 then
text = text .. dfhack.df2utf(v.value:gsub('%[R]', '[P]')) .. ' '
end
end
--xxx: item description-specific
text = text:gsub('%s*%[B]%s*$', '')
text = text:gsub('%s*%[P]%s*$', '')
text = fixspaces(text)
ws.breakdown_level = df.interface_breakdown_types.STOPSCREEN
return { text }
end
--luacheck: in=number
function item_get_contained_items(itemid)
local item = df.item.find(itemid)
if not item then
error('no item '..tostring(itemid))
end
local ret = {}
for i,v in ipairs(item.general_refs) do
if v._type == df.general_ref_contains_itemst then
local v = v --as:df.general_ref_contains_itemst
local item = df.item.find(v.item_id)
if item then
local title = itemname(item, 0, true)
table.insert(ret, { title, item.id, item.flags.whole, item_can_melt(item) })
end
end
end
return ret
end
--luacheck: in=number
function item_get_contained_units(itemid)
local item = df.item.find(itemid)
if not item then
error('no item '..tostring(itemid))
end
local ret = {}
for i,v in ipairs(item.general_refs) do
if v._type == df.general_ref_contains_unitst then
local v = v --as:df.general_ref_contains_unitst
local unit = df.unit.find(v.unit_id)
if unit then
local title = unit_fulltitle(unit)
table.insert(ret, { title, unit.id })
end
end
end
return ret
end
--luacheck: in=
function artifacts_list()
local ret = {}
--todo: what is the correct way to get artifacts?
for i,item in ipairs(df.global.world.items.other.ANY_ARTIFACT) do
local artname,realname = itemname(item, 0, true)
if realname then
table.insert(ret, { realname, item.id, item.flags.whole, false, artname })
end
end
return ret
end
mat_category_names = { --as:string[]
[df.entity_material_category.None] = 'any material',
[df.entity_material_category.Leather] = 'leather',
[df.entity_material_category.Cloth] = 'cloth',
[df.entity_material_category.Wood] = 'wood',
[df.entity_material_category.Stone] = 'stone',
[df.entity_material_category.Ammo2] = 'metal',
[df.entity_material_category.Armor] = 'metal',
[df.entity_material_category.Gem] = 'gem',
[df.entity_material_category.Bone] = 'bone',
[df.entity_material_category.Shell] = 'shell',
[df.entity_material_category.Pearl] = 'pearl',
[df.entity_material_category.Ivory] = 'tooth',
[df.entity_material_category.Horn] = 'horn',
[df.entity_material_category.PlantFiber] = 'plant fiber',
[df.entity_material_category.Silk] = 'silk',
[df.entity_material_category.Wool] = 'yarn',
}
--todo: implement this without reaction_product_itemst?
function generic_item_name(type, subtype, mat_class, mat_type, mat_index, single)
--local q = df.item:new()
local q = df.reaction_product_itemst:new()
q.item_type = type
q.item_subtype = subtype
--todo: use this in appropriate places
if single then
q.count = 1
end
if mat_class == -1 then
q.mat_type = mat_type
q.mat_index = mat_index
end
local title = utils.call_with_string(q, 'getDescription')
q:delete()
title = title:sub(1, title:find(' %(')-1)
title = dfhack.df2utf(title)
if mat_class ~= -1 then
local n = mat_category_names[mat_class]
if n then
title = capitalize(n) .. ' ' .. title:utf8lower()
end
end
return title
end
--luacheck: in=number
function item_zoom(itemid)
local item = df.item.find(itemid)
if not item then
return false
end
local x,y,z = dfhack.items.getPosition(item)
if x ~= -30000 then
recenter_view(x, y, z)
local ws = dfhack.gui.getCurViewscreen()
gui.simulateInput(ws, K'D_LOOK')
df.global.cursor.x = x
df.global.cursor.y = y
if z > 0 then
df.global.cursor.z = z - 1
gui.simulateInput(ws, K'CURSOR_UP_Z')
else
df.global.cursor.z = z + 1
gui.simulateInput(ws, K'CURSOR_DOWN_Z')
end
return true
end
return false
end
--print(pcall(function() return json:encode(building_get_contained_items()) end))