-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathuniforms.lua
318 lines (253 loc) · 8.22 KB
/
uniforms.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
function uniform_id2index(id)
for i,v in ipairs(df.global.ui.main.fortress_entity.uniforms) do
if v.id == id then
return i
end
end
return -1
end
function uniform_find_by_id(id)
for i,v in ipairs(df.global.ui.main.fortress_entity.uniforms) do
if v.id == id then
return v
end
end
return nil
end
--luacheck: in=
function uniforms_get_list()
local ret = {}
for i,v in ipairs(df.global.ui.main.fortress_entity.uniforms) do
table.insert(ret, { uniformname(v), v.id })
end
return ret
end
--luacheck: in=
function uniforms_add()
execute_with_military_screen(function(ws)
gui.simulateInput(ws, K'D_MILITARY_UNIFORMS')
gui.simulateInput(ws, K'D_MILITARY_ADD_UNIFORM')
end)
end
--luacheck: in=number
function uniform_delete(id)
local idx = uniform_id2index(id)
if idx == -1 then
return
end
execute_with_military_screen(function(ws)
gui.simulateInput(ws, K'D_MILITARY_UNIFORMS')
ws.layer_objects[0].cursor = idx --hint:df.layer_object_listst
gui.simulateInput(ws, K'D_MILITARY_DELETE_UNIFORM')
end)
return true
end
--luacheck: in=number
function uniform_get_info(id)
local uniform = uniform_find_by_id(id)
if not uniform then
error('no uniform '..tostring(id))
end
local items = {}
--todo: maybe do this via UI to be sure the order is the same as in UI (for deletion)
for i,infos in ipairs(uniform.uniform_item_info) do
for j,info in ipairs(infos) do
local type = uniform.uniform_item_types[i][j]
local subtype = uniform.uniform_item_subtypes[i][j]
local mat_class = info.material_class
local mattype = info.mattype
local matindex = info.matindex
local title
if info.indiv_choice.whole ~= 0 then
if info.indiv_choice.any then
title = 'Weapon of choice'
elseif info.indiv_choice.melee then
title = 'Melee weapon of choice'
elseif info.indiv_choice.ranged then
title = 'Ranged weapon of choice'
end
if mat_class ~= -1 then
local n = mat_category_names[mat_class]
if n then
title = capitalize(n) .. ' ' .. title:lower()
end
elseif mattype ~= -1 or matindex ~= -1 then
local mi = dfhack.matinfo.decode(mattype, matindex)
if mi then
title = capitalize(mi.material.state_name.Solid) .. ' ' .. title:lower()
end
end
else
title = generic_item_name(type, subtype, mat_class, mattype, matindex)
end
table.insert(items, { title, #items, i })
end
end
return { uniformname(uniform), uniform.id, uniform.name, items, uniform.flags.whole }
end
--luacheck: in=number,string
function uniform_set_name(id, name)
local uniform = uniform_find_by_id(id)
if not uniform then
error('no uniform '..tostring(id))
end
uniform.name = dfhack.utf2df(name)
return true
end
--luacheck: in=number,number
function uniform_set_flags(id, flags)
local uniform = uniform_find_by_id(id)
if not uniform then
error('no uniform '..tostring(id))
end
uniform.flags.whole = flags
return true
end
uniform_additem_keys = {
K'D_MILITARY_ADD_ARMOR',
K'D_MILITARY_ADD_PANTS',
K'D_MILITARY_ADD_HELM',
K'D_MILITARY_ADD_GLOVES',
K'D_MILITARY_ADD_BOOTS',
K'D_MILITARY_ADD_SHIELD',
K'D_MILITARY_ADD_WEAPON',
}
--luacheck: in=number
function uniform_get_additem(cat)
return execute_with_military_screen(function(ws)
gui.simulateInput(ws, K'D_MILITARY_UNIFORMS')
gui.simulateInput(ws, uniform_additem_keys[cat+1])
local items = {}
for i,v in ipairs(ws.equip.add_item.type) do
local type = v
local subtype = ws.equip.add_item.subtype[i]
local indiv_choice = ws.equip.add_item.indiv_choice[i]
local title
if indiv_choice.whole ~= 0 then
if indiv_choice.any then
title = 'Weapon of choice'
elseif indiv_choice.melee then
title = 'Melee weapon of choice'
elseif indiv_choice.ranged then
title = 'Ranged weapon of choice'
end
else
title = generic_item_name(type, subtype, -1, -1, -1)
end
table.insert(items, { title, i, ws.equip.add_item.foreign[i] })
end
return { items }
end)
end
--luacheck: in=number,number,number
function uniform_item_add(uniformid, cat, itemspec)
return execute_with_military_screen(function(ws)
gui.simulateInput(ws, K'D_MILITARY_UNIFORMS')
list_select_item_by_id(ws, 0, ws.equip.uniforms, uniformid)
gui.simulateInput(ws, uniform_additem_keys[cat+1])
ws.layer_objects[0].active = false
ws.layer_objects[2].active = true
if itemspec < 0 or itemspec >= #ws.equip.add_item.type then
return
end
ws.layer_objects[2].cursor = itemspec --hint:df.layer_object_listst
gui.simulateInput(ws, K'SELECT')
return true
end)
end
--luacheck: in=number,number
function uniform_item_get_matchoices(uniformid, itemidx)
return execute_with_military_screen(function(ws)
gui.simulateInput(ws, K'D_MILITARY_UNIFORMS')
list_select_item_by_id(ws, 0, ws.equip.uniforms, uniformid)
ws.layer_objects[0].active = false
ws.layer_objects[1].active = true
ws.layer_objects[1].cursor = itemidx --hint:df.layer_object_listst
gui.simulateInput(ws, K'D_MILITARY_ADD_MATERIAL')
local mats = {}
for i,v in ipairs(ws.equip.material.specific.mat_type) do
if v == -1 then
local matclass = ws.equip.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.equip.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 { mats }
end)
end
--luacheck: in=number,number,number[]
function uniform_item_set_material(uniformid, itemidx, matspec)
return execute_with_military_screen(function(ws)
gui.simulateInput(ws, K'D_MILITARY_UNIFORMS')
list_select_item_by_id(ws, 0, ws.equip.uniforms, uniformid)
local info = ws.equip.uniform.info[itemidx]
info.material_class = matspec[1]
if info.material_class == -1 then
info.mattype = matspec[2]
info.matindex = matspec[3]
else
info.mattype = -1
info.matindex = -1
end
return true
end)
end
--luacheck: in=number,number
function uniform_item_get_colorchoices(uniformid, itemidx)
end
--luacheck: in=number,number,number
function uniform_item_set_color(uniformid, itemidx, color)
end
--luacheck: in=number,number
function uniform_item_delete(uniformid, itemidx)
return execute_with_military_screen(function(ws)
gui.simulateInput(ws, K'D_MILITARY_UNIFORMS')
list_select_item_by_id(ws, 0, ws.equip.uniforms, uniformid)
if itemidx < 0 or itemidx >= #ws.equip.uniform.info then
return
end
ws.layer_objects[0].active = false
ws.layer_objects[1].active = true
ws.layer_objects[1].cursor = itemidx --hint:df.layer_object_listst
gui.simulateInput(ws, K'SELECT')
return true
end)
end
--luacheck: in=number,number,number
function uniform_apply(squadid, unitid, uniid)
local uniidx = uniform_id2index(uniid)
if not uniidx then
return
end
--xxx: temporary until this is fixed in publicly available app version
if unitid == 0 then
unitid = -1
end
return execute_with_military_screen(function(ws)
gui.simulateInput(ws, K'D_MILITARY_EQUIP')
gui.simulateInput(ws, K'D_MILITARY_EQUIP_UNIFORM')
list_select_item_by_id(ws, 0, ws.equip.squads, squadid)
ws.layer_objects[0].active = false
if unitid ~= -1 then
ws.layer_objects[1].active = true
list_select_item_by_id(ws, 1, ws.equip.units, unitid)
ws.layer_objects[1].active = false
end
ws.layer_objects[2].active = true
ws.layer_objects[2].cursor = uniidx --hint:df.layer_object_listst
gui.simulateInput(ws, unitid == -1 and K'SEC_SELECT' or K'SELECT')
return ret
end)
end
--print(pcall(function() return json:encode(uniform_item_get_matchoices(1,6)) end))