@@ -13,7 +13,7 @@ TradeAgreementOverlay.ATTRS{
1313 default_pos = {x = 45 , y =- 6 },
1414 default_enabled = true ,
1515 viewscreens = ' dwarfmode/Diplomacy/Requests' ,
16- frame = {w = 58 , h = 7 },
16+ frame = {w = 58 , h = 5 },
1717 frame_style = gui .MEDIUM_FRAME ,
1818 frame_background = gui .CLEAR_PEN ,
1919}
@@ -31,9 +31,12 @@ local function decode_mat_list(mat)
3131 return minfo and minfo .material .material_value or 0
3232end
3333
34- local function decode_mat_weight (mat )
35- local minfo = dfhack .matinfo .decode (mat .type , mat .index )
36- return minfo and minfo .material .solid_density or 0
34+ local function decode_mat_density (mat )
35+ local ok , result = pcall (function ()
36+ local minfo = dfhack .matinfo .decode (mat .type , mat .index )
37+ return minfo and minfo .material .solid_density or 0
38+ end )
39+ return ok and result or 0
3740end
3841
3942local select_by_value_tab = {
@@ -140,7 +143,7 @@ local select_by_value_tab = {
140143}
141144select_by_value_tab .LargeCutGems = select_by_value_tab .SmallCutGems
142145
143- local select_by_weight_tab = {
146+ local select_by_density_tab = {
144147 Stone = {
145148 get_mats = function (resources ) return resources .stones end ,
146149 decode = function (id ) return dfhack .matinfo .decode (0 , id ).material .solid_density end ,
@@ -155,84 +158,84 @@ local select_by_weight_tab = {
155158 },
156159 Cheese = {
157160 get_mats = function (resources ) return transform_mat_list (resources .misc_mat .cheese ) end ,
158- decode = decode_mat_weight ,
161+ decode = decode_mat_density ,
159162 },
160163 Powders = {
161164 get_mats = function (resources ) return transform_mat_list (resources .misc_mat .powders ) end ,
162- decode = decode_mat_weight ,
165+ decode = decode_mat_density ,
163166 },
164167 Extracts = {
165168 get_mats = function (resources ) return transform_mat_list (resources .misc_mat .extracts ) end ,
166- decode = decode_mat_weight ,
169+ decode = decode_mat_density ,
167170 },
168171 Drinks = {
169172 get_mats = function (resources ) return transform_mat_list (resources .misc_mat .booze ) end ,
170- decode = decode_mat_weight ,
173+ decode = decode_mat_density ,
171174 },
172175 Meat = {
173176 get_mats = function (resources ) return transform_mat_list (resources .misc_mat .meat ) end ,
174- decode = decode_mat_weight ,
177+ decode = decode_mat_density ,
175178 },
176179 Leather = {
177180 get_mats = function (resources ) return transform_mat_list (resources .organic .leather ) end ,
178- decode = decode_mat_weight ,
181+ decode = decode_mat_density ,
179182 },
180183 Parchment = {
181184 get_mats = function (resources ) return transform_mat_list (resources .organic .parchment ) end ,
182- decode = decode_mat_weight ,
185+ decode = decode_mat_density ,
183186 },
184187 SmallCutGems = {
185188 get_mats = function (resources ) return resources .gems end ,
186189 decode = function (id ) return dfhack .matinfo .decode (0 , id ).material .solid_density end ,
187190 },
188191 CupsMugsGoblets = {
189192 get_mats = function (resources ) return transform_mat_list (resources .misc_mat .crafts ) end ,
190- decode = decode_mat_weight ,
193+ decode = decode_mat_density ,
191194 },
192195 Crafts = {
193196 get_mats = function (resources ) return transform_mat_list (resources .misc_mat .crafts ) end ,
194- decode = decode_mat_weight ,
197+ decode = decode_mat_density ,
195198 },
196199 FlasksWaterskins = {
197200 get_mats = function (resources ) return transform_mat_list (resources .misc_mat .flasks ) end ,
198- decode = decode_mat_weight ,
201+ decode = decode_mat_density ,
199202 },
200203 Quivers = {
201204 get_mats = function (resources ) return transform_mat_list (resources .misc_mat .quivers ) end ,
202- decode = decode_mat_weight ,
205+ decode = decode_mat_density ,
203206 },
204207 Backpacks = {
205208 get_mats = function (resources ) return transform_mat_list (resources .misc_mat .backpacks ) end ,
206- decode = decode_mat_weight ,
209+ decode = decode_mat_density ,
207210 },
208211 Barrels = {
209212 get_mats = function (resources ) return transform_mat_list (resources .misc_mat .barrels ) end ,
210- decode = decode_mat_weight ,
213+ decode = decode_mat_density ,
211214 },
212215 Sand = {
213216 get_mats = function (resources ) return transform_mat_list (resources .misc_mat .sand ) end ,
214- decode = decode_mat_weight ,
217+ decode = decode_mat_density ,
215218 },
216219 Glass = {
217220 get_mats = function (resources ) return transform_mat_list (resources .misc_mat .glass ) end ,
218- decode = decode_mat_weight ,
221+ decode = decode_mat_density ,
219222 },
220223 Clay = {
221224 get_mats = function (resources ) return transform_mat_list (resources .misc_mat .clay ) end ,
222- decode = decode_mat_weight ,
223- },
224- ClothPlant = { get_mats = function (resources ) return transform_mat_list (resources .organic .fiber ) end , decode = decode_mat_weight },
225- ThreadPlant = { get_mats = function (resources ) return transform_mat_list (resources .organic .fiber ) end , decode = decode_mat_weight },
226- RopesPlant = { get_mats = function (resources ) return transform_mat_list (resources .organic .fiber ) end , decode = decode_mat_weight },
227- BagsPlant = { get_mats = function (resources ) return transform_mat_list (resources .organic .fiber ) end , decode = decode_mat_weight },
228- ClothSilk = { get_mats = function (resources ) return transform_mat_list (resources .organic .silk ) end , decode = decode_mat_weight },
229- ThreadSilk = { get_mats = function (resources ) return transform_mat_list (resources .organic .silk ) end , decode = decode_mat_weight },
230- RopesSilk = { get_mats = function (resources ) return transform_mat_list (resources .organic .silk ) end , decode = decode_mat_weight },
231- BagsSilk = { get_mats = function (resources ) return transform_mat_list (resources .organic .silk ) end , decode = decode_mat_weight },
232- ClothYarn = { get_mats = function (resources ) return transform_mat_list (resources .organic .wool ) end , decode = decode_mat_weight },
233- ThreadYarn = { get_mats = function (resources ) return transform_mat_list (resources .organic .wool ) end , decode = decode_mat_weight },
234- RopesYarn = { get_mats = function (resources ) return transform_mat_list (resources .organic .wool ) end , decode = decode_mat_weight },
235- BagsYarn = { get_mats = function (resources ) return transform_mat_list (resources .organic .wool ) end , decode = decode_mat_weight },
225+ decode = decode_mat_density ,
226+ },
227+ ClothPlant = { get_mats = function (resources ) return transform_mat_list (resources .organic .fiber ) end , decode = decode_mat_density },
228+ ThreadPlant = { get_mats = function (resources ) return transform_mat_list (resources .organic .fiber ) end , decode = decode_mat_density },
229+ RopesPlant = { get_mats = function (resources ) return transform_mat_list (resources .organic .fiber ) end , decode = decode_mat_density },
230+ BagsPlant = { get_mats = function (resources ) return transform_mat_list (resources .organic .fiber ) end , decode = decode_mat_density },
231+ ClothSilk = { get_mats = function (resources ) return transform_mat_list (resources .organic .silk ) end , decode = decode_mat_density },
232+ ThreadSilk = { get_mats = function (resources ) return transform_mat_list (resources .organic .silk ) end , decode = decode_mat_density },
233+ RopesSilk = { get_mats = function (resources ) return transform_mat_list (resources .organic .silk ) end , decode = decode_mat_density },
234+ BagsSilk = { get_mats = function (resources ) return transform_mat_list (resources .organic .silk ) end , decode = decode_mat_density },
235+ ClothYarn = { get_mats = function (resources ) return transform_mat_list (resources .organic .wool ) end , decode = decode_mat_density },
236+ ThreadYarn = { get_mats = function (resources ) return transform_mat_list (resources .organic .wool ) end , decode = decode_mat_density },
237+ RopesYarn = { get_mats = function (resources ) return transform_mat_list (resources .organic .wool ) end , decode = decode_mat_density },
238+ BagsYarn = { get_mats = function (resources ) return transform_mat_list (resources .organic .wool ) end , decode = decode_mat_density },
236239 Plants = {
237240 get_mats = function (resources ) return resources .plants end ,
238241 decode = function (id ) return dfhack .matinfo .decode (df .builtin_mats .PLANT , id ).material .solid_density end ,
@@ -242,7 +245,7 @@ local select_by_weight_tab = {
242245 decode = function (id ) return dfhack .matinfo .decode (df .builtin_mats .PLANT , id ).material .solid_density end ,
243246 },
244247}
245- select_by_weight_tab .LargeCutGems = select_by_weight_tab .SmallCutGems
248+ select_by_density_tab .LargeCutGems = select_by_density_tab .SmallCutGems
246249
247250local function get_cur_tab_category ()
248251 return diplomacy .taking_requests_tablist [diplomacy .taking_requests_selected_tab ]
@@ -253,9 +256,9 @@ local function get_select_by_value_tab(category)
253256 return select_by_value_tab [df .entity_sell_category [category ]]
254257end
255258
256- local function get_select_by_weight_tab (category )
259+ local function get_select_by_density_tab (category )
257260 category = category or get_cur_tab_category ()
258- return select_by_weight_tab [df .entity_sell_category [category ]]
261+ return select_by_density_tab [df .entity_sell_category [category ]]
259262end
260263
261264local function get_cur_priority_list ()
@@ -332,7 +335,7 @@ local function save_requests()
332335 if dfhack .filesystem .isfile (CONFIG_FILE ) then
333336 data = json .decode_file (CONFIG_FILE ) or {}
334337 end
335-
338+
336339 local civ_data = data [key ] or {}
337340 for _ , cat in ipairs (diplomacy .taking_requests_tablist ) do
338341 local cat_name = df .entity_sell_category [cat ]
@@ -353,7 +356,7 @@ local function save_requests()
353356 end
354357 end
355358 end
356-
359+
357360 data [key ] = civ_data
358361 json .encode_file (data , CONFIG_FILE , {pretty = true })
359362 dfhack .gui .showAnnouncement (' Trade requests saved for ' .. key , COLOR_GREEN )
@@ -364,18 +367,18 @@ local function load_requests()
364367 dfhack .gui .showAnnouncement (' No saved trade agreements found.' , COLOR_RED )
365368 return
366369 end
367-
370+
368371 local data = json .decode_file (CONFIG_FILE )
369372 if not data then return end
370-
373+
371374 local key = get_civ_key ()
372375 local civ_data = data [key ]
373-
376+
374377 if not civ_data then
375378 dfhack .gui .showAnnouncement (' No saved requests found for ' .. key , COLOR_YELLOW )
376379 return
377380 end
378-
381+
379382 for _ , cat in ipairs (diplomacy .taking_requests_tablist ) do
380383 local cat_name = df .entity_sell_category [cat ]
381384 local priority = diplomacy .environment .dipev .sell_requests .priority [cat ]
@@ -386,7 +389,7 @@ local function load_requests()
386389 end
387390 end
388391 end
389-
392+
390393 dfhack .gui .showAnnouncement (' Trade requests loaded for ' .. key , COLOR_GREEN )
391394end
392395
@@ -425,10 +428,10 @@ function TradeAgreementOverlay:init()
425428 },
426429 widgets .HotkeyLabel {
427430 frame = {t = 2 , l = 24 , w = 23 },
428- label = ' Select by weight ' ,
431+ label = ' Select by density ' ,
429432 key = ' CUSTOM_CTRL_W' ,
430- on_activate = self :callback (' select_by_weight ' ),
431- enabled = get_select_by_weight_tab ,
433+ on_activate = self :callback (' select_by_density ' ),
434+ enabled = get_select_by_density_tab ,
432435 },
433436 }
434437end
@@ -474,19 +477,23 @@ function TradeAgreementOverlay:select_by_value()
474477 )
475478end
476479
477- function TradeAgreementOverlay :select_by_weight ()
480+ function TradeAgreementOverlay :select_by_density ()
478481 local cat = get_cur_tab_category ()
479- local cur_tab = get_select_by_weight_tab (cat )
482+ local cur_tab = get_select_by_density_tab (cat )
480483
481484 local resource_name = df .entity_sell_category [cat ]
482485 local prices , matValuesUnique = get_prices (cur_tab )
486+ if # matValuesUnique == 0 then
487+ dfhack .gui .showAnnouncement (' No density data available for ' .. resource_name :lower (), COLOR_YELLOW )
488+ return
489+ end
483490 local list = {}
484491 for index , value in ipairs (matValuesUnique ) do
485- list [index ] = (' %4d (%d type%s of %s)' ):format (
492+ list [index ] = (' %5d kg/m ' .. string.char ( 253 ) .. ' (%d type%s of %s)' ):format (
486493 value .value , value .count , value .count == 1 and ' ' or ' s' , resource_name :lower ())
487494 end
488495 dlg .showListPrompt (
489- " Select materials with solid density" , " " ,
496+ " Select materials by density (kg/m " .. string.char ( 253 ) .. " ) " , " " ,
490497 COLOR_WHITE ,
491498 list ,
492499 function (id ) select_by_value (prices , matValuesUnique [id ].value ) end
0 commit comments