-
Notifications
You must be signed in to change notification settings - Fork 5
/
ammunition.lua
265 lines (211 loc) · 6.78 KB
/
ammunition.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
--luacheck: in=
function ammunition_get_list()
local ret = {}
local function process_specs(specs)
local ammolist = {}
for i,ammo in ipairs(specs) do
local f = ammo.item_filter
local title = generic_item_name(f.item_type, f.item_subtype, f.material_class, f.mattype, f.matindex)
table.insert(ammolist, { title, ammo.amount, ammo.flags.whole })
end
return ammolist
end
table.insert(ret, { 'Hunters', -1, process_specs(df.global.ui.equipment.hunter_ammunition) })
for i,squad in ipairs(find_fortress_squads()) do
local sqname = squadname(squad)
local ammolist = process_specs(squad.ammunition)
table.insert(ret, { sqname, squad.id, ammolist })
end
return ret
end
--luacheck: in=
function ammunition_get_list_short()
local ret = {}
table.insert(ret, { 'Hunters', -1, #df.global.ui.equipment.hunter_ammunition })
for i,squad in ipairs(find_fortress_squads()) do
local sqname = squadname(squad)
table.insert(ret, { sqname, squad.id, #squad.ammunition })
end
return ret
end
--luacheck: in=number
function ammunition_get_squad(id)
local function process_specs(specs)
local ammolist = {}
for i,ammo in ipairs(specs) do
local f = ammo.item_filter
local title = generic_item_name(f.item_type, f.item_subtype, f.material_class, f.mattype, f.matindex)
table.insert(ammolist, { title, ammo.amount, ammo.flags.whole })
end
return ammolist
end
if id == -1 then
return process_specs(df.global.ui.equipment.hunter_ammunition)
end
local squad = df.squad.find(id)
if not squad then
return
end
return process_specs(squad.ammunition)
end
--todo: can do this directly, without employing UI
--luacheck: in=
function ammunition_get_additem()
return execute_with_military_screen(function(ws)
gui.simulateInput(ws, K'D_MILITARY_AMMUNITION')
gui.simulateInput(ws, K'D_MILITARY_AMMUNITION_ADD_ITEM')
gui.simulateInput(ws, K'SELECT')
gui.simulateInput(ws, K'D_MILITARY_AMMUNITION_MATERIAL')
gui.simulateInput(ws, K'LEAVESCREEN')
gui.simulateInput(ws, K'D_MILITARY_AMMUNITION_REMOVE_ITEM')
local items = {}
for i,v in ipairs(ws.ammo.add_item.type) do
local type = v
local subtype = ws.ammo.add_item.subtype[i]
local title = generic_item_name(type, subtype, -1, -1, -1)
table.insert(items, { title, { type, subtype }, ws.ammo.add_item.foreign[i] })
end
--xxx: return only material classes and inorganics, skipping numerous types of wood and bone
local mats = {}
for i,v in ipairs(ws.ammo.material.specific.mat_type) do
if v == -1 then
local matclass = ws.ammo.material.generic[i]
local title = mat_category_names[matclass]
if title then
table.insert(mats, { title, { matclass, -1, -1 } })
end
elseif v == 0 then
local matindex = ws.ammo.material.specific.mat_index[i]
local mi = dfhack.matinfo.decode(v, matindex)
if mi then
local title = mi.material.state_name.Solid
table.insert(mats, { title, { -1, v, matindex } })
end
else
break
end
end
return { items, mats }
end)
end
--luacheck: in=number,number[],number[]
function ammunition_item_add(squadid, itemspec, matspec)
local specs
if squadid == -1 then
specs = df.global.ui.equipment.hunter_ammunition
else
local squad = df.squad.find(squadid)
if not squad then
return
end
specs = squad.ammunition
end
local record = df.squad_ammo_spec:new()
local f = record.item_filter
f.item_type = itemspec[1]
f.item_subtype = itemspec[2]
f.material_class = matspec[1]
if f.material_class == -1 then
f.mattype = matspec[2]
f.matindex = matspec[3]
else
f.mattype = -1
f.matindex = -1
end
record.amount = 100
record.flags.use_combat = true
record.flags.use_training = true
specs:insert(#specs, record)
df.global.ui.equipment.update.ammo = true
--todo: this could return the new item, so that app doesn't need to reload
end
--todo: maybe do this directly, without employing UI, just don't forget to set df.global.ui.equipment.update.ammo
--luacheck: in=number,number
function ammunition_item_remove(squadid, idx)
execute_with_military_screen(function(ws)
gui.simulateInput(ws, K'D_MILITARY_AMMUNITION')
for i,v in ipairs(ws.ammo.squads) do
if (not v and squadid == -1) or (v and v.id == squadid) then
if i > 0 then
if idx < 0 or idx >= #v.ammunition then
return
end
ws.layer_objects[0].cursor = i - 1 --hint:df.layer_object_listst
gui.simulateInput(ws, K'STANDARDSCROLL_DOWN')
else
if idx < 0 or idx >= #df.global.ui.equipment.hunter_ammunition then
return
end
end
ws.layer_objects[0].active = false
ws.layer_objects[1].active = true
ws.layer_objects[1].cursor = idx --hint:df.layer_object_listst
gui.simulateInput(ws, K'D_MILITARY_AMMUNITION_REMOVE_ITEM')
break
end
end
end)
end
--luacheck: in=number,number
function ammunition_get_assigned(squadid, idx)
local spec
local obj
if squadid == -1 then
spec = df.global.ui.equipment.hunter_ammunition[idx]
obj = df.global.ui.equipment
else
local squad = df.squad.find(squadid)
if not squad then
return
end
spec = squad.ammunition[idx]
obj = squad
end
local ret = {}
for i,v in ipairs(spec.assigned) do
local item = df.item.find(v)
if item then
local name = itemname(item, 3, true)
local in_use = utils.binsearch(obj.ammo_items, v) ~= nil
table.insert(ret, { name, in_use })
end
end
return ret
end
--luacheck: in=number,number,number
function ammunition_set_amount(squadid, idx, amount)
local spec
if squadid == -1 then
spec = df.global.ui.equipment.hunter_ammunition[idx]
else
local squad = df.squad.find(squadid)
if not squad then
return
end
spec = squad.ammunition[idx]
end
spec.amount = amount
df.global.ui.equipment.update.ammo = true
return true
end
--luacheck: in=number,number,number
function ammunition_set_flags(squadid, idx, flags)
local spec
if squadid == -1 then
spec = df.global.ui.equipment.hunter_ammunition[idx]
else
local squad = df.squad.find(squadid)
if not squad then
return
end
spec = squad.ammunition[idx]
end
spec.flags.whole = flags
df.global.ui.equipment.update.ammo = true
return true
end
--print(pcall(function() return json:encode(ammunition_get_additem()) end))
--print(pcall(function() return json:encode(ammunition_item_add(140, { 38,0 }, { 14,-1,-1 })) end))
--print(pcall(function() return json:encode(ammunition_item_remove(-1,1)) end))
--print(pcall(function() return json:encode(ammunition_get()) end))
--print(pcall(function() return json:encode(ammunition_get_assigned(140,2)) end))