Skip to content

Commit e8d84eb

Browse files
authored
Merge pull request #1617 from Alistair-Afton/combine-dye-profile
combine: differentiate dyes by dye_profile instead of excluding them
2 parents 7ff4f01 + 9df2394 commit e8d84eb

3 files changed

Lines changed: 157 additions & 6 deletions

File tree

‎changelog.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Template for new versions:
3838
- `gui/mod-manager`: warn when loading a preset that doesn't include vanilla mods that were added to the game after the preset was saved
3939

4040
## Fixes
41+
- `combine`: dyes are combined again, but only when their ``dye_profile`` matches, so mixed dyes no longer revert to a component dye
4142
- `bodyswap`: fix "invalid argument count" when the target unit has no nemesis record
4243
- `caravan`: fix doubled "total value of items marked for trade" after toggling filter options, and keep item marks when switching between filter views in the ``Bring goods to depot`` overlay
4344
- `fix/loyaltycascade`: guard against citizens that are not historical figures and emit a warning.

‎combine.lua‎

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
-- Combine items in stockpiles into stacks.
2+
--@module = true
3+
14
local argparse = require('argparse')
25
local utils = require('utils')
36

@@ -183,8 +186,6 @@ local function stack_type_new(type_vals)
183186
end
184187

185188
local function isDye(item)
186-
-- Dyes should not be combined as this will cause bugs when mixing them together
187-
if item:getType() ~= df.item_type.POWDER_MISC then return false end
188189
-- pcall guards items/materials that can't be decoded or lack the flag
189190
local ok, is_dye = pcall(function()
190191
local mat = dfhack.matinfo.decode(item.mat_type, item.mat_index)
@@ -193,10 +194,30 @@ local function isDye(item)
193194
return ok and is_dye or false
194195
end
195196

196-
local function stacks_add_item(stockpile, stacks, stack_type, item, container)
197-
-- add an item to the matching comp_items table; based on comp_key.
198-
local comp_key = ''
197+
-- produce a fingerprint of an item's dye_profile, which distinguishes mixed
198+
-- dyes (e.g. a blend of two dyes) from their components; they all share the
199+
-- same mat_type/mat_index
200+
local function dye_profile_key(item)
201+
local profile = item.dye_profile
202+
-- merchant dyes can lose their profile contents to a vanilla bug (the
203+
-- embedded dye_profile is never absent, but has color_index -1); give
204+
-- each such dye its own key so they never combine
205+
if (not profile or profile.color_index == -1) and isDye(item) then
206+
return 'unprofiled+' .. item.id
207+
end
208+
if not profile then
209+
return ''
210+
end
211+
local parts = {profile.color_index}
212+
for _,v in ipairs(profile.dye_material) do parts[#parts+1] = v end
213+
for _,v in ipairs(profile.dye_matg) do parts[#parts+1] = v end
214+
for _,v in ipairs(profile.degree) do parts[#parts+1] = v end
215+
for _,v in ipairs(profile.target_index) do parts[#parts+1] = v end
216+
return table.concat(parts, '+')
217+
end
199218

219+
local function make_comp_key(stack_type, item)
220+
local comp_key
200221
if typesThatUseCreatures[df.item_type[stack_type.type_id]] then
201222
if not typesThatUseMaterial[df.item_type[stack_type.type_id]] then
202223
comp_key = ('%s+%s+%s'):format(stack_type.type_id, item.race, item.caste)
@@ -212,6 +233,15 @@ local function stacks_add_item(stockpile, stacks, stack_type, item, container)
212233
else
213234
comp_key = ('%s+%s+%s'):format(stack_type.type_id, item.mat_type, item.mat_index)
214235
end
236+
if stack_type.type_id == df.item_type.POWDER_MISC then
237+
comp_key = ('%s+%s'):format(comp_key, dye_profile_key(item))
238+
end
239+
return comp_key
240+
end
241+
242+
local function stacks_add_item(stockpile, stacks, stack_type, item, container)
243+
-- add an item to the matching comp_items table; based on comp_key.
244+
local comp_key = make_comp_key(stack_type, item)
215245

216246
if not stack_type.comp_items[comp_key] then
217247
stack_type.comp_items[comp_key] = comp_item_new(comp_key, stack_type)
@@ -447,7 +477,7 @@ local function stacks_add_items(stockpile, stacks, items, container, ind)
447477
local stack_type = stacks.stack_types[type_id]
448478

449479
-- item type in list of included types?
450-
if stack_type and not item:isSand() and not item:isPlaster() and not isDye(item) and isValidPart(item) then
480+
if stack_type and not item:isSand() and not item:isPlaster() and isValidPart(item) then
451481
if not isRestrictedItem(item) and item.stack_size <= stack_type.max_stack_qty then
452482

453483
stacks_add_item(stockpile, stacks, stack_type, item, container)
@@ -862,6 +892,13 @@ local function main()
862892

863893
end
864894

895+
if dfhack.internal.IN_TEST then
896+
unit_test_hooks = {
897+
make_comp_key=make_comp_key,
898+
dye_profile_key=dye_profile_key,
899+
}
900+
end
901+
865902
if not dfhack_flags.module then
866903
main()
867904
end

‎test/combine.lua‎

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
config.target = 'combine'
2+
3+
local combine = reqscript('combine')
4+
local p = combine.unit_test_hooks
5+
6+
local POWDER_MISC = df.item_type.POWDER_MISC
7+
local DRINK = df.item_type.DRINK
8+
9+
local function mock_item(item_type, fields)
10+
local item = {
11+
getType=function() return item_type end,
12+
isCrafted=function() return false end,
13+
}
14+
for k,v in pairs(fields) do item[k] = v end
15+
return item
16+
end
17+
18+
local function mock_dye(color_index, materials)
19+
return mock_item(POWDER_MISC, {
20+
mat_type=0, mat_index=0,
21+
dye_profile={
22+
color_index=color_index,
23+
dye_material=materials,
24+
dye_matg={},
25+
degree={},
26+
target_index={},
27+
},
28+
})
29+
end
30+
31+
function test.dye_same_profile_same_key()
32+
local stack_type = {type_id=POWDER_MISC}
33+
local dye_a = mock_dye(5, {10})
34+
local dye_b = mock_dye(5, {10})
35+
expect.eq(p.make_comp_key(stack_type, dye_a),
36+
p.make_comp_key(stack_type, dye_b))
37+
end
38+
39+
function test.dye_different_color_different_key()
40+
local stack_type = {type_id=POWDER_MISC}
41+
local dye_a = mock_dye(5, {10})
42+
local dye_b = mock_dye(7, {10})
43+
expect.ne(p.make_comp_key(stack_type, dye_a),
44+
p.make_comp_key(stack_type, dye_b))
45+
end
46+
47+
function test.dye_mix_different_key_than_component()
48+
-- a mixed dye shares mat_type/mat_index with its components but has a
49+
-- different dye_profile; it must not be merged into a component stack
50+
local stack_type = {type_id=POWDER_MISC}
51+
local dye_a = mock_dye(5, {10})
52+
local dye_mix = mock_dye(5, {10, 20})
53+
expect.ne(p.make_comp_key(stack_type, dye_a),
54+
p.make_comp_key(stack_type, dye_mix))
55+
end
56+
57+
function test.powder_without_profile()
58+
-- non-dye powders have an empty/unset profile and still merge as before
59+
local stack_type = {type_id=POWDER_MISC}
60+
local p1 = mock_item(POWDER_MISC, {mat_type=1, mat_index=2})
61+
local p2 = mock_item(POWDER_MISC, {mat_type=1, mat_index=2})
62+
expect.eq(p.make_comp_key(stack_type, p1),
63+
p.make_comp_key(stack_type, p2))
64+
end
65+
66+
local function find_dye_mat()
67+
for _, plant in ipairs(df.global.world.raws.plants.all) do
68+
local mtype = plant.material_defs.type.mill
69+
local midx = plant.material_defs.idx.mill
70+
if mtype ~= -1 then
71+
local matinfo = dfhack.matinfo.decode(mtype, midx)
72+
if matinfo and matinfo.material.flags.IS_DYE then
73+
return mtype, midx
74+
end
75+
end
76+
end
77+
end
78+
79+
function test.unprofiled_dye_never_combines()
80+
-- merchant dyes can lose their dye_profile to a vanilla bug; such items
81+
-- must not be merged, not even with each other. on real items the
82+
-- embedded dye_profile is never absent but has color_index -1
83+
local mtype, midx = find_dye_mat()
84+
if not mtype then return end
85+
local empty_profile = {color_index=-1, dye_material={}, dye_matg={},
86+
degree={}, target_index={}}
87+
local stack_type = {type_id=POWDER_MISC}
88+
local d1 = mock_item(POWDER_MISC, {mat_type=mtype, mat_index=midx, id=101,
89+
dye_profile=empty_profile})
90+
local d2 = mock_item(POWDER_MISC, {mat_type=mtype, mat_index=midx, id=102,
91+
dye_profile=empty_profile})
92+
local d3 = mock_item(POWDER_MISC, {mat_type=mtype, mat_index=midx, id=103})
93+
local d4 = mock_item(POWDER_MISC, {mat_type=mtype, mat_index=midx, id=104,
94+
dye_profile={color_index=1, dye_material={1}, dye_matg={}, degree={},
95+
target_index={}}})
96+
expect.ne(p.make_comp_key(stack_type, d1),
97+
p.make_comp_key(stack_type, d2))
98+
expect.ne(p.make_comp_key(stack_type, d1),
99+
p.make_comp_key(stack_type, d3))
100+
expect.ne(p.make_comp_key(stack_type, d1),
101+
p.make_comp_key(stack_type, d4))
102+
end
103+
104+
function test.dye_key_ignores_other_types()
105+
local stack_type = {type_id=DRINK}
106+
local d1 = mock_item(DRINK, {mat_type=1, mat_index=2})
107+
local d2 = mock_item(DRINK, {mat_type=1, mat_index=2,
108+
dye_profile={color_index=9, dye_material={1},
109+
dye_matg={}, degree={},
110+
target_index={}}})
111+
expect.eq(p.make_comp_key(stack_type, d1),
112+
p.make_comp_key(stack_type, d2))
113+
end

0 commit comments

Comments
 (0)