-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdfcapture.lua
More file actions
3120 lines (2934 loc) · 145 KB
/
Copy pathdfcapture.lua
File metadata and controls
3120 lines (2934 loc) · 145 KB
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- dfcapture - multiplayer Dwarf Fortress in the browser, as a DFHack plugin
-- Copyright (C) 2026 Gabriel Rios
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Affero General Public License as published by
-- the Free Software Foundation, version 3 of the License.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU Affero General Public License for more details.
--
-- You should have received a copy of the GNU Affero General Public License
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
--
-- Runs on DFHack (Zlib); descends from DFPlex (Zlib) and webfort (ISC).
-- Full license: see LICENSE. Third-party credits: see NOTICE.
--
-- SPDX-License-Identifier: AGPL-3.0-only
-- Companion Lua module for the dfcapture plugin.
--
-- The C++ side handles HTTP + premium frame capture; the more intricate game-state
-- mutations (creating stockpiles, placing buildings with materials) go through DFHack's
-- tested high-level APIs here, which is far less error-prone than replicating the
-- raws-dependent logic in C++. Called from C++ via Lua::CallLuaModuleFunction.
local _ENV = mkmodule('plugins.dfcapture')
-- Trivial round-trip used to verify the C++ -> Lua bridge in isolation.
function ping(n)
return (n or 0) + 1
end
-- Maps the browser's preset names to DFHack's shipped stockpile library presets
-- (data/stockpiles/*.dfstock). "all" accepts everything; cat_* accept one category.
local STOCKPILE_PRESETS = {
all = 'all', everything = 'all',
food = 'cat_food', stone = 'cat_stone', wood = 'cat_wood',
furniture = 'cat_furniture', finished = 'cat_finished_goods',
bars = 'cat_bars_blocks', gems = 'cat_gems', cloth = 'cat_cloth',
leather = 'cat_leather', ammo = 'cat_ammo', armor = 'cat_armor',
weapons = 'cat_weapons', animals = 'cat_animals', corpses = 'cat_corpses',
refuse = 'cat_refuse', coins = 'cat_coins', sheets = 'cat_sheets',
}
local function get_stockpile(id)
local b = df.building.find(id)
if b and b:getType() == df.building_type.Stockpile then return b end
return nil
end
-- Change what a stockpile accepts: 'none' clears it; otherwise apply a category preset.
-- mode can be 'set' (replace), 'enable', or 'disable'. Returns (ok, err).
function stockpile_set_preset(id, preset, mode)
local b = get_stockpile(id)
if not b then return false, 'not a stockpile' end
preset = tostring(preset or 'all'):lower()
if preset == 'none' then
b.settings.flags.whole = 0
return true, ''
end
mode = tostring(mode or 'set'):lower()
if mode ~= 'set' and mode ~= 'enable' and mode ~= 'disable' then
mode = 'set'
end
local lib = STOCKPILE_PRESETS[preset] or preset
local ok, err = pcall(function()
require('plugins.stockpiles').import_settings(lib, {id = id, mode = mode})
end)
if not ok then return false, tostring(err) end
return true, ''
end
-- ===== Custom stockpile item editor (DF-style: category -> sub-groups -> per-item toggles) =====
-- A category has a group flag + one or more sub-groups (DF's middle column). Each group maps to the
-- exact stockpile_settings field that native DF and DFHack's StockpileSerializer use. Some groups are
-- raw vectors (e.g. finished_goods.mats indexed by inorganic raws), while others are fixed arrays or
-- scalar bool fields (usable/unusable, dyed/undyed). Keep this table data-driven so adding another
-- native middle-column group is a local change.
local function sp_stone_allowed(inorg)
local f = inorg.flags
local soil = f.SOIL and not f.AQUIFER
local mf = inorg.material.flags
return soil or (mf.IS_STONE and not mf.NO_STONE_STOCKPILE)
end
local function sp_is_ore(inorg)
local ok, n = pcall(function() return #inorg.metal_ore.mat_index end)
return ok and n and n > 0
end
local function sp_is_soil(inorg) return inorg.flags.SOIL and not inorg.flags.AQUIFER end
local function sp_stone_name(m)
local ok, s = pcall(function() return m.material.state_name.Solid end)
if ok and s and #s > 0 then return s end
return m.id
end
local function sp_stone_vec(b) return b.settings.stone.mats end
local function sp_inorganics() return df.global.world.raws.inorganics.all end
local function sp_any(v) return v ~= nil end
local function sp_bool(v) return v == true or v == 1 or tostring(v) == 'true' or tostring(v) == '1' end
local function sp_ensure_vec(vec, n) while #vec < n do vec:insert('#', 0) end end
local SP_QUALITIES
local function sp_title_token(s)
s = tostring(s or '')
s = s:gsub('_', ' '):gsub('(%l)(%u)', '%1 %2'):lower()
return (s:gsub('^%l', string.upper))
end
local function sp_material_name(m)
if not m then return '' end
local ok, s = pcall(function() return m.material.state_name.Solid end)
if ok and s and #s > 0 then return s end
return sp_title_token(m.id or '')
end
local function sp_itemdef_name(d)
if not d then return '' end
return tostring((d.name_plural and #d.name_plural > 0 and d.name_plural) or
(d.name and #d.name > 0 and d.name) or d.id or '')
end
local function sp_creature_name(c)
if not c then return '' end
local ok, s = pcall(function() return c.name[1] end)
if ok and s and #s > 0 then return s end
ok, s = pcall(function() return c.name[0] end)
if ok and s and #s > 0 then return s end
return sp_title_token(c.creature_id or c.id or '')
end
local function sp_color_name(c)
if not c then return '' end
return tostring((c.name and #c.name > 0 and c.name) or c.id or '')
end
local function sp_enum_list(entries)
local t = {n = 0}
for _, e in ipairs(entries) do
local idx = tonumber(e[1])
if idx and idx >= 0 then
t[idx] = {name = e[2], token = e[3] or e[2]}
if idx + 1 > t.n then t.n = idx + 1 end
end
end
return t
end
local function sp_collection_count(raws)
if type(raws) == 'table' and raws.n then return raws.n end
return #raws
end
local function sp_collection_get(raws, idx) return raws[idx] end
local function sp_entry_name(e) return e and e.name or '' end
local function sp_bool_entries(entries)
local list = {n = #entries}
for i, e in ipairs(entries) do list[i - 1] = e end
return list
end
local function sp_bool_group(key, label, entries)
local list = sp_bool_entries(entries)
return {
key = key, label = label,
raws = function() return list end,
include = sp_any,
name = sp_entry_name,
get = function(b, i) return list[i].get(b) end,
set = function(b, i, on) list[i].set(b, on) end,
}
end
local function sp_vec_group(key, label, vec, raws, include, name, fixed)
return {
key = key, label = label, vec = vec, raws = raws,
include = include or sp_any, name = name or sp_entry_name, fixed = fixed,
}
end
local function sp_inorganic_group(key, label, vec, include)
return sp_vec_group(key, label, vec, sp_inorganics, include, sp_material_name)
end
local function sp_quality_group(key, label, vec)
return sp_vec_group(key, label, vec, function() return SP_QUALITIES end, sp_any, sp_entry_name, true)
end
local function sp_color_group(vec)
return sp_vec_group('color', 'Color', vec, function() return df.global.world.raws.descriptors.colors end, sp_any, sp_color_name)
end
local function sp_organic_material_name(cat, mat_type, mat_index)
if cat == df.organic_mat_category.Fish or
cat == df.organic_mat_category.UnpreparedFish or
cat == df.organic_mat_category.Eggs then
local cr = df.global.world.raws.creatures.all[mat_type]
local caste = cr and cr.caste and cr.caste[mat_index] or nil
local ok, s = pcall(function() return caste.caste_name[0] end)
if ok and s and #s > 0 then return s end
ok, s = pcall(function() return caste.caste_name[1] end)
if ok and s and #s > 0 then return s end
return sp_creature_name(cr)
end
local ok, info = pcall(dfhack.matinfo.decode, mat_type, mat_index)
if ok and info then
local okn, name = pcall(function() return info.material.state_name.Solid end)
if okn and name and #name > 0 then return name end
okn, name = pcall(function() return info:toString() end)
if okn and name and #name > 0 then return name end
end
return ('material %s:%s'):format(tostring(mat_type), tostring(mat_index))
end
local SP_ORGANIC_RAW_CACHE = {}
local function sp_organic_raws(cat)
cat = tonumber(cat)
if not cat then return {n = 0} end
if SP_ORGANIC_RAW_CACHE[cat] then return SP_ORGANIC_RAW_CACHE[cat] end
local mt = df.global.world.raws.mat_table
local types = mt.organic_types[cat]
local indexes = mt.organic_indexes[cat]
local n = types and #types or 0
local list = {n = n}
for i = 0, n - 1 do
list[i] = {
name = sp_organic_material_name(cat, types[i], indexes[i]),
mat_type = types[i],
mat_index = indexes[i],
}
end
SP_ORGANIC_RAW_CACHE[cat] = list
return list
end
local function sp_organic_group(key, label, vec, cat)
return sp_vec_group(key, label, vec, function() return sp_organic_raws(cat) end, sp_any, sp_entry_name)
end
local function sp_metal(m)
return m and m.material and m.material.flags and m.material.flags.IS_METAL
end
local function sp_metal_or_stone(m)
return m and m.material and m.material.flags and (m.material.flags.IS_METAL or m.material.flags.IS_STONE)
end
local function sp_gem(m)
return m and m.material and m.material.flags and m.material.flags.IS_GEM
end
local function sp_finished_mat(m)
return m and m.material and m.material.flags and
(m.material.flags.IS_GEM or m.material.flags.IS_METAL or m.material.flags.IS_STONE)
end
SP_QUALITIES = sp_enum_list({
{0, 'Ordinary'}, {1, 'Well-crafted'}, {2, 'Finely-crafted'}, {3, 'Superior'},
{4, 'Exceptional'}, {5, 'Masterful'}, {6, 'Artifact'},
})
local SP_OTHER_MATS_FURNITURE = sp_enum_list({
{0, 'Wood'}, {1, 'Plant cloth'}, {2, 'Bone'}, {3, 'Tooth'}, {4, 'Horn'},
{5, 'Pearl'}, {6, 'Shell'}, {7, 'Leather'}, {8, 'Silk'}, {9, 'Amber'},
{10, 'Coral'}, {11, 'Green glass'}, {12, 'Clear glass'}, {13, 'Crystal glass'}, {14, 'Yarn'},
})
local SP_OTHER_MATS_FINISHED = sp_enum_list({
{0, 'Wood'}, {1, 'Plant cloth'}, {2, 'Bone'}, {3, 'Tooth'}, {4, 'Horn'},
{5, 'Pearl'}, {6, 'Shell'}, {7, 'Leather'}, {8, 'Silk'}, {9, 'Amber'},
{10, 'Coral'}, {11, 'Green glass'}, {12, 'Clear glass'}, {13, 'Crystal glass'}, {14, 'Yarn'}, {15, 'Wax'},
})
local SP_OTHER_MATS_WEAPON_ARMOR = sp_enum_list({
{0, 'Wood'}, {1, 'Plant cloth'}, {2, 'Bone'}, {3, 'Shell'}, {4, 'Leather'},
{5, 'Silk'}, {6, 'Green glass'}, {7, 'Clear glass'}, {8, 'Crystal glass'}, {9, 'Yarn'},
})
local SP_BAR_OTHER_MATS = sp_enum_list({
{0, 'Coal'}, {1, 'Potash'}, {2, 'Ash'}, {3, 'Pearlash'}, {4, 'Soap'},
})
local SP_BLOCK_OTHER_MATS = sp_enum_list({
{0, 'Green glass'}, {1, 'Clear glass'}, {2, 'Crystal glass'}, {3, 'Wood'},
})
local SP_AMMO_OTHER_MATS = sp_enum_list({{0, 'Wood'}, {1, 'Bone'}})
local SP_FINISHED_TYPES = sp_enum_list({
{10, 'Chains'}, {11, 'Flasks'}, {12, 'Goblets'}, {13, 'Musical instruments'}, {14, 'Toys'},
{25, 'Armor'}, {26, 'Shoes'}, {28, 'Helms'}, {29, 'Gloves'}, {36, 'Figurines'},
{37, 'Amulets'}, {38, 'Scepters'}, {40, 'Crowns'}, {41, 'Rings'}, {42, 'Earrings'},
{43, 'Bracelets'}, {44, 'Large gems'}, {59, 'Totems'}, {60, 'Pants'}, {61, 'Backpacks'},
{62, 'Quivers'}, {82, 'Splints'}, {83, 'Crutches'}, {86, 'Tools'}, {89, 'Books'},
})
local SP_FURNITURE_TYPES = sp_enum_list({
{0, 'Floodgates'}, {1, 'Hatch covers'}, {2, 'Grates'}, {3, 'Doors'}, {4, 'Catapult parts'},
{5, 'Ballista parts'}, {6, 'Trap components'}, {7, 'Beds'}, {8, 'Traction benches'}, {9, 'Windows'},
{10, 'Chairs'}, {11, 'Tables'}, {12, 'Coffins'}, {13, 'Statues'}, {14, 'Slabs'},
{15, 'Querns'}, {16, 'Millstones'}, {17, 'Armor stands'}, {18, 'Weapon racks'}, {19, 'Cabinets'},
{20, 'Anvils'}, {21, 'Buckets'}, {22, 'Bins'}, {23, 'Boxes'}, {24, 'Bags'},
{25, 'Siege ammo'}, {26, 'Barrels'}, {27, 'Ballista arrowheads'}, {28, 'Pipe sections'},
{29, 'Large pots'}, {30, 'Minecarts'}, {31, 'Wheelbarrows'}, {32, 'Other large tools'},
{33, 'Sand bags'}, {34, 'Bolt thrower parts'},
})
local SP_REFUSE_TYPES = sp_enum_list({
{23, 'Corpses'}, {46, 'Body parts'}, {47, 'Vermin remains'}, {55, 'Tanned hides'},
})
local SP_CATEGORIES = {
ammo = {
label = 'Ammo', flag = 'ammo',
groups = {
sp_vec_group('type', 'Ammo type', function(b) return b.settings.ammo.type end,
function() return df.global.world.raws.itemdefs.ammo end, sp_any, sp_itemdef_name),
sp_inorganic_group('mats', 'Metal', function(b) return b.settings.ammo.mats end, sp_metal),
sp_vec_group('other', 'Other materials', function(b) return b.settings.ammo.other_mats end,
function() return SP_AMMO_OTHER_MATS end),
sp_quality_group('core', 'Core quality', function(b) return b.settings.ammo.quality_core end),
sp_quality_group('total', 'Total quality', function(b) return b.settings.ammo.quality_total end),
},
},
animals = {
label = 'Animals', flag = 'animals',
groups = {
sp_bool_group('empty', 'Empty cages/traps', {
{name='Empty cages', get=function(b) return b.settings.animals.empty_cages end,
set=function(b, on) b.settings.animals.empty_cages = sp_bool(on) end},
{name='Empty animal traps', get=function(b) return b.settings.animals.empty_traps end,
set=function(b, on) b.settings.animals.empty_traps = sp_bool(on) end},
}),
sp_vec_group('creatures', 'Creatures', function(b) return b.settings.animals.enabled end,
function() return df.global.world.raws.creatures.all end, sp_any, sp_creature_name),
},
},
armor = {
label = 'Armor', flag = 'armor',
groups = {
sp_vec_group('body', 'Body', function(b) return b.settings.armor.body end,
function() return df.global.world.raws.itemdefs.armor end, sp_any, sp_itemdef_name),
sp_vec_group('head', 'Head', function(b) return b.settings.armor.head end,
function() return df.global.world.raws.itemdefs.helms end, sp_any, sp_itemdef_name),
sp_vec_group('feet', 'Feet', function(b) return b.settings.armor.feet end,
function() return df.global.world.raws.itemdefs.shoes end, sp_any, sp_itemdef_name),
sp_vec_group('hands', 'Hands', function(b) return b.settings.armor.hands end,
function() return df.global.world.raws.itemdefs.gloves end, sp_any, sp_itemdef_name),
sp_vec_group('legs', 'Legs', function(b) return b.settings.armor.legs end,
function() return df.global.world.raws.itemdefs.pants end, sp_any, sp_itemdef_name),
sp_vec_group('shield', 'Shield', function(b) return b.settings.armor.shield end,
function() return df.global.world.raws.itemdefs.shields end, sp_any, sp_itemdef_name),
sp_inorganic_group('mats', 'Metal', function(b) return b.settings.armor.mats end, sp_metal),
sp_vec_group('other', 'Other materials', function(b) return b.settings.armor.other_mats end,
function() return SP_OTHER_MATS_WEAPON_ARMOR end),
sp_quality_group('core', 'Core quality', function(b) return b.settings.armor.quality_core end),
sp_quality_group('total', 'Total quality', function(b) return b.settings.armor.quality_total end),
sp_color_group(function(b) return b.settings.armor.color end),
sp_bool_group('use', 'Usability', {
{name='Usable armor', get=function(b) return b.settings.armor.usable end,
set=function(b, on) b.settings.armor.usable = sp_bool(on) end},
{name='Unusable armor', get=function(b) return b.settings.armor.unusable end,
set=function(b, on) b.settings.armor.unusable = sp_bool(on) end},
}),
sp_bool_group('dye', 'Dye', {
{name='Dyed', get=function(b) return b.settings.armor.dyed end,
set=function(b, on) b.settings.armor.dyed = sp_bool(on) end},
{name='Undyed', get=function(b) return b.settings.armor.undyed end,
set=function(b, on) b.settings.armor.undyed = sp_bool(on) end},
}),
},
},
bars = {
label = 'Bars/blocks', flag = 'bars_blocks',
groups = {
sp_inorganic_group('bars_mats', 'Metal bars', function(b) return b.settings.bars_blocks.bars_mats end, sp_metal),
sp_vec_group('bars_other', 'Other bars', function(b) return b.settings.bars_blocks.bars_other_mats end,
function() return SP_BAR_OTHER_MATS end),
sp_inorganic_group('blocks_mats', 'Metal/stone blocks', function(b) return b.settings.bars_blocks.blocks_mats end, sp_metal_or_stone),
sp_vec_group('blocks_other', 'Other blocks', function(b) return b.settings.bars_blocks.blocks_other_mats end,
function() return SP_BLOCK_OTHER_MATS end),
},
},
cloth = {
label = 'Cloth', flag = 'cloth',
groups = {
sp_organic_group('thread_silk', 'Silk thread', function(b) return b.settings.cloth.thread_silk end, df.organic_mat_category.Silk),
sp_organic_group('thread_plant', 'Plant thread', function(b) return b.settings.cloth.thread_plant end, df.organic_mat_category.PlantFiber),
sp_organic_group('thread_yarn', 'Yarn thread', function(b) return b.settings.cloth.thread_yarn end, df.organic_mat_category.Yarn),
sp_organic_group('thread_metal', 'Metal thread', function(b) return b.settings.cloth.thread_metal end, df.organic_mat_category.MetalThread),
sp_organic_group('cloth_silk', 'Silk cloth', function(b) return b.settings.cloth.cloth_silk end, df.organic_mat_category.Silk),
sp_organic_group('cloth_plant', 'Plant cloth', function(b) return b.settings.cloth.cloth_plant end, df.organic_mat_category.PlantFiber),
sp_organic_group('cloth_yarn', 'Yarn cloth', function(b) return b.settings.cloth.cloth_yarn end, df.organic_mat_category.Yarn),
sp_organic_group('cloth_metal', 'Metal cloth', function(b) return b.settings.cloth.cloth_metal end, df.organic_mat_category.MetalThread),
sp_color_group(function(b) return b.settings.cloth.color end),
sp_bool_group('dye', 'Dye', {
{name='Dyed', get=function(b) return b.settings.cloth.dyed end,
set=function(b, on) b.settings.cloth.dyed = sp_bool(on) end},
{name='Undyed', get=function(b) return b.settings.cloth.undyed end,
set=function(b, on) b.settings.cloth.undyed = sp_bool(on) end},
}),
},
},
coins = {
label = 'Coins', flag = 'coins',
groups = {
sp_inorganic_group('mats', 'Material', function(b) return b.settings.coins.mats end, sp_any),
},
},
corpses = {
label = 'Corpses', flag = 'corpses',
groups = {
sp_vec_group('creatures', 'Creatures', function(b) return b.settings.corpses.corpses end,
function() return df.global.world.raws.creatures.all end, sp_any, sp_creature_name),
},
},
finished = {
label = 'Finished goods', flag = 'finished_goods',
groups = {
sp_vec_group('type', 'Type', function(b) return b.settings.finished_goods.type end,
function() return SP_FINISHED_TYPES end),
sp_inorganic_group('mats', 'Metal/stone/gem', function(b) return b.settings.finished_goods.mats end, sp_finished_mat),
sp_vec_group('other', 'Other materials', function(b) return b.settings.finished_goods.other_mats end,
function() return SP_OTHER_MATS_FINISHED end),
sp_quality_group('core', 'Core quality', function(b) return b.settings.finished_goods.quality_core end),
sp_quality_group('total', 'Total quality', function(b) return b.settings.finished_goods.quality_total end),
sp_color_group(function(b) return b.settings.finished_goods.color end),
sp_bool_group('dye', 'Dye', {
{name='Dyed', get=function(b) return b.settings.finished_goods.dyed end,
set=function(b, on) b.settings.finished_goods.dyed = sp_bool(on) end},
{name='Undyed', get=function(b) return b.settings.finished_goods.undyed end,
set=function(b, on) b.settings.finished_goods.undyed = sp_bool(on) end},
}),
},
},
food = {
label = 'Food', flag = 'food',
groups = {
sp_bool_group('prepared', 'Prepared meals', {
{name='Prepared meals', get=function(b) return b.settings.food.prepared_meals end,
set=function(b, on) b.settings.food.prepared_meals = sp_bool(on) end},
}),
sp_organic_group('meat', 'Meat', function(b) return b.settings.food.meat end, df.organic_mat_category.Meat),
sp_organic_group('fish', 'Prepared fish', function(b) return b.settings.food.fish end, df.organic_mat_category.Fish),
sp_organic_group('unprepared_fish', 'Unprepared fish', function(b) return b.settings.food.unprepared_fish end, df.organic_mat_category.UnpreparedFish),
sp_organic_group('egg', 'Eggs', function(b) return b.settings.food.egg end, df.organic_mat_category.Eggs),
sp_organic_group('plants', 'Plants', function(b) return b.settings.food.plants end, df.organic_mat_category.Plants),
sp_organic_group('drink_plant', 'Plant drinks', function(b) return b.settings.food.drink_plant end, df.organic_mat_category.PlantDrink),
sp_organic_group('drink_animal', 'Animal drinks', function(b) return b.settings.food.drink_animal end, df.organic_mat_category.CreatureDrink),
sp_organic_group('cheese_plant', 'Plant cheese', function(b) return b.settings.food.cheese_plant end, df.organic_mat_category.PlantCheese),
sp_organic_group('cheese_animal', 'Animal cheese', function(b) return b.settings.food.cheese_animal end, df.organic_mat_category.CreatureCheese),
sp_organic_group('seeds', 'Seeds', function(b) return b.settings.food.seeds end, df.organic_mat_category.Seed),
sp_organic_group('leaves', 'Leaves / growths', function(b) return b.settings.food.leaves end, df.organic_mat_category.PlantGrowth),
sp_organic_group('powder_plant', 'Plant powder', function(b) return b.settings.food.powder_plant end, df.organic_mat_category.PlantPowder),
sp_organic_group('powder_creature', 'Animal powder', function(b) return b.settings.food.powder_creature end, df.organic_mat_category.CreaturePowder),
sp_organic_group('glob', 'Glob', function(b) return b.settings.food.glob end, df.organic_mat_category.Glob),
sp_organic_group('glob_paste', 'Paste', function(b) return b.settings.food.glob_paste end, df.organic_mat_category.Paste),
sp_organic_group('glob_pressed', 'Pressed', function(b) return b.settings.food.glob_pressed end, df.organic_mat_category.Pressed),
sp_organic_group('liquid_plant', 'Plant liquid', function(b) return b.settings.food.liquid_plant end, df.organic_mat_category.PlantLiquid),
sp_organic_group('liquid_animal', 'Animal liquid', function(b) return b.settings.food.liquid_animal end, df.organic_mat_category.CreatureLiquid),
sp_organic_group('liquid_misc', 'Misc liquid', function(b) return b.settings.food.liquid_misc end, df.organic_mat_category.MiscLiquid),
},
},
furniture = {
label = 'Furniture/siege ammo', flag = 'furniture',
groups = {
sp_vec_group('type', 'Type', function(b) return b.settings.furniture.type end,
function() return SP_FURNITURE_TYPES end),
sp_inorganic_group('mats', 'Metal/stone', function(b) return b.settings.furniture.mats end, sp_metal_or_stone),
sp_vec_group('other', 'Other materials', function(b) return b.settings.furniture.other_mats end,
function() return SP_OTHER_MATS_FURNITURE end),
sp_quality_group('core', 'Core quality', function(b) return b.settings.furniture.quality_core end),
sp_quality_group('total', 'Total quality', function(b) return b.settings.furniture.quality_total end),
},
},
gems = {
label = 'Gems', flag = 'gems',
groups = {
sp_inorganic_group('rough_mats', 'Rough gems', function(b) return b.settings.gems.rough_mats end, sp_gem),
sp_inorganic_group('cut_mats', 'Cut gems', function(b) return b.settings.gems.cut_mats end, sp_gem),
},
},
leather = {
label = 'Leather', flag = 'leather',
groups = {
sp_organic_group('mats', 'Leather', function(b) return b.settings.leather.mats end, df.organic_mat_category.Leather),
sp_color_group(function(b) return b.settings.leather.color end),
sp_bool_group('dye', 'Dye', {
{name='Dyed', get=function(b) return b.settings.leather.dyed end,
set=function(b, on) b.settings.leather.dyed = sp_bool(on) end},
{name='Undyed', get=function(b) return b.settings.leather.undyed end,
set=function(b, on) b.settings.leather.undyed = sp_bool(on) end},
}),
},
},
refuse = {
label = 'Refuse', flag = 'refuse',
groups = {
sp_vec_group('type', 'Type', function(b) return b.settings.refuse.type end,
function() return SP_REFUSE_TYPES end),
sp_vec_group('corpses', 'Corpses', function(b) return b.settings.refuse.corpses end,
function() return df.global.world.raws.creatures.all end, sp_any, sp_creature_name),
sp_vec_group('body_parts', 'Body parts', function(b) return b.settings.refuse.body_parts end,
function() return df.global.world.raws.creatures.all end, sp_any, sp_creature_name),
sp_vec_group('skulls', 'Skulls', function(b) return b.settings.refuse.skulls end,
function() return df.global.world.raws.creatures.all end, sp_any, sp_creature_name),
sp_vec_group('bones', 'Bones', function(b) return b.settings.refuse.bones end,
function() return df.global.world.raws.creatures.all end, sp_any, sp_creature_name),
sp_vec_group('hair', 'Hair/wool', function(b) return b.settings.refuse.hair end,
function() return df.global.world.raws.creatures.all end, sp_any, sp_creature_name),
sp_vec_group('shells', 'Shells', function(b) return b.settings.refuse.shells end,
function() return df.global.world.raws.creatures.all end, sp_any, sp_creature_name),
sp_vec_group('teeth', 'Teeth', function(b) return b.settings.refuse.teeth end,
function() return df.global.world.raws.creatures.all end, sp_any, sp_creature_name),
sp_vec_group('horns', 'Horns', function(b) return b.settings.refuse.horns end,
function() return df.global.world.raws.creatures.all end, sp_any, sp_creature_name),
sp_bool_group('hide', 'Raw hides', {
{name='Fresh raw hide', get=function(b) return b.settings.refuse.fresh_raw_hide end,
set=function(b, on) b.settings.refuse.fresh_raw_hide = sp_bool(on) end},
{name='Rotten raw hide', get=function(b) return b.settings.refuse.rotten_raw_hide end,
set=function(b, on) b.settings.refuse.rotten_raw_hide = sp_bool(on) end},
}),
},
},
sheets = {
label = 'Sheets', flag = 'sheet',
groups = {
sp_organic_group('paper', 'Paper', function(b) return b.settings.sheet.paper end, df.organic_mat_category.Paper),
sp_organic_group('parchment', 'Parchment', function(b) return b.settings.sheet.parchment end, df.organic_mat_category.Parchment),
},
},
weapons = {
label = 'Weapons/trap comps', flag = 'weapons',
groups = {
sp_vec_group('weapon_type', 'Weapons', function(b) return b.settings.weapons.weapon_type end,
function() return df.global.world.raws.itemdefs.weapons end, sp_any, sp_itemdef_name),
sp_vec_group('trapcomp_type', 'Trap components', function(b) return b.settings.weapons.trapcomp_type end,
function() return df.global.world.raws.itemdefs.trapcomps end, sp_any, sp_itemdef_name),
sp_inorganic_group('mats', 'Metal/stone', function(b) return b.settings.weapons.mats end, sp_metal_or_stone),
sp_vec_group('other', 'Other materials', function(b) return b.settings.weapons.other_mats end,
function() return SP_OTHER_MATS_WEAPON_ARMOR end),
sp_quality_group('core', 'Core quality', function(b) return b.settings.weapons.quality_core end),
sp_quality_group('total', 'Total quality', function(b) return b.settings.weapons.quality_total end),
sp_bool_group('use', 'Usability', {
{name='Usable weapons', get=function(b) return b.settings.weapons.usable end,
set=function(b, on) b.settings.weapons.usable = sp_bool(on) end},
{name='Unusable weapons', get=function(b) return b.settings.weapons.unusable end,
set=function(b, on) b.settings.weapons.unusable = sp_bool(on) end},
}),
},
},
wood = {
label = 'Wood', flag = 'wood',
groups = {
sp_vec_group('trees', 'Trees', function(b) return b.settings.wood.mats end,
function() return df.global.world.raws.plants.all end,
function(p) return p.flags and p.flags.TREE end,
function(p) return p.name end),
},
},
stone = {
label = 'Stone', flag = 'stone',
groups = {
{ key = 'ores', label = 'Metal ores', vec = sp_stone_vec, raws = sp_inorganics,
include = function(i) return sp_stone_allowed(i) and sp_is_ore(i) end, name = sp_stone_name },
{ key = 'other', label = 'Other stone', vec = sp_stone_vec, raws = sp_inorganics,
include = function(i) return sp_stone_allowed(i) and not sp_is_ore(i) and not sp_is_soil(i) end, name = sp_stone_name },
{ key = 'soil', label = 'Soil / clay', vec = sp_stone_vec, raws = sp_inorganics,
include = function(i) return sp_is_soil(i) end, name = sp_stone_name },
},
},
}
local function sp_group_count(g) return sp_collection_count(g.raws()) end
local function sp_group_item(g, idx) return sp_collection_get(g.raws(), idx) end
local function sp_group_get(g, b, idx)
if g.get then return sp_bool(g.get(b, idx)) end
local vec = g.vec(b)
if not g.fixed then sp_ensure_vec(vec, sp_group_count(g)) end
if g.fixed or idx < #vec then return sp_bool(vec[idx]) end
return false
end
local function sp_group_set(g, b, idx, on)
if g.set then g.set(b, idx, on); return end
local vec = g.vec(b)
if not g.fixed then sp_ensure_vec(vec, sp_group_count(g)) end
vec[idx] = sp_bool(on) and 1 or 0
end
-- Resolve (category, group key) -> spec, group. Defaults to the first group if blank/unknown.
local function sp_find_group(cat, group)
local spec = SP_CATEGORIES[tostring(cat or '')]
if not spec then return nil, nil end
for _, g in ipairs(spec.groups) do
if g.key == tostring(group or '') then return spec, g end
end
return spec, spec.groups[1]
end
-- (stockpile_cat_groups + stockpile_item_list build JSON, so they're defined later after
-- json_string/json_bool are in scope.)
function stockpile_toggle_item(id, cat, group, idx, on)
local b = get_stockpile(id)
if not b then return false, 'not a stockpile' end
local spec, g = sp_find_group(cat, group)
if not g then return false, 'category not editable' end
idx = tonumber(idx)
if not idx or idx < 0 then return false, 'bad index' end
local ok, err = pcall(function()
sp_group_set(g, b, idx, on)
if sp_bool(on) then b.settings.flags[spec.flag] = true end
end)
if not ok then return false, tostring(err) end
return true, ''
end
function stockpile_toggle_all(id, cat, group, on)
local b = get_stockpile(id)
if not b then return false, 'not a stockpile' end
local spec, g = sp_find_group(cat, group)
if not g then return false, 'category not editable' end
local want = sp_bool(on)
local ok, err = pcall(function()
for i = 0, sp_group_count(g) - 1 do
local r = sp_group_item(g, i)
if r and g.include(r, i) then sp_group_set(g, b, i, want) end
end
if want then b.settings.flags[spec.flag] = true end
end)
if not ok then return false, tostring(err) end
return true, ''
end
-- Create a stockpile over the inclusive world-tile rectangle (x1,y1)-(x2,y2) on z and
-- apply a category preset. Returns (id, ''). On failure returns (-1, errmsg).
function create_stockpile(x1, y1, x2, y2, z, preset)
local lx, hx = math.min(x1, x2), math.max(x1, x2)
local ly, hy = math.min(y1, y2), math.max(y1, y2)
local ok, bld, err = pcall(dfhack.buildings.constructBuilding, {
type = df.building_type.Stockpile,
abstract = true,
pos = {x = lx, y = ly, z = z},
width = hx - lx + 1,
height = hy - ly + 1,
})
if not ok then return -1, tostring(bld) end -- bld is the error on pcall failure
if not bld then return -1, tostring(err or 'could not place stockpile') end
-- Configure which items it accepts, using DFHack's tested preset import.
local libname = STOCKPILE_PRESETS[tostring(preset or 'all'):lower()] or 'all'
pcall(function()
require('plugins.stockpiles').import_settings(libname, {id = bld.id, mode = 'enable'})
end)
return bld.id, ''
end
-- ---------------------------------------------------------------------------
-- Browser build menu + placement
-- ---------------------------------------------------------------------------
local BUILD_CATEGORIES = {
{id='furniture', label='Furniture'},
{id='workshops', label='Workshops'},
{id='furnaces', label='Furnaces'},
{id='constructions', label='Constructions'},
{id='machines', label='Machines'},
{id='traps', label='Traps'},
{id='siege', label='Siege engines'},
{id='track', label='Track'},
{id='farming', label='Farming'},
{id='trade', label='Trade'},
}
local function json_string(s)
s = tostring(s or '')
-- DF strings are CP437. A raw high byte (>=0x80, e.g. the i/o in dwarf names like "Atir"/"Onul",
-- or special material names) is NOT valid UTF-8, and a raw control char (<0x20) is illegal in a
-- JSON string -- either one makes the browser's JSON.parse() throw, which surfaces as
-- "Workshop data unavailable" even though the Lua built the response fine. Convert to UTF-8 and
-- escape every control char so the JSON is always valid regardless of item/job/worker names.
local ok, u = pcall(dfhack.df2utf, s)
if ok and type(u) == 'string' then s = u end
s = s:gsub('\\', '\\\\'):gsub('"', '\\"')
:gsub('\n', '\\n'):gsub('\r', '\\r'):gsub('\t', '\\t')
:gsub('[%z\1-\8\11\12\14-\31\127]', function(c) return string.format('\\u%04x', c:byte()) end)
return '"' .. s .. '"'
end
local function json_bool(v)
return v and 'true' or 'false'
end
-- Sub-groups (DF's middle column) for a stockpile category. Defined here (not with the other
-- stockpile fns) so json_string/json_bool above are already in scope.
function stockpile_cat_groups(cat)
local spec = SP_CATEGORIES[tostring(cat or '')]
if not spec then return '{"ok":false,"groups":[]}\n' end
local out = {}
for _, g in ipairs(spec.groups) do
out[#out + 1] = '{"key":' .. json_string(g.key) .. ',"label":' .. json_string(g.label) .. '}'
end
return '{"ok":true,"label":' .. json_string(spec.label) .. ',"groups":[' .. table.concat(out, ',') .. ']}\n'
end
-- The items of a category's sub-group with their on/off state.
function stockpile_item_list(id, cat, group)
local b = get_stockpile(id)
if not b then return '{"ok":false,"items":[]}\n' end
local spec, g = sp_find_group(cat, group)
if not g then return '{"ok":false,"error":"category not editable yet","items":[]}\n' end
local ok, res = pcall(function()
local items = {}
for i = 0, sp_group_count(g) - 1 do
local r = sp_group_item(g, i)
if r and g.include(r, i) then
local on = sp_group_get(g, b, i)
items[#items + 1] = '{"idx":' .. i .. ',"name":' .. json_string(tostring(g.name(r))) ..
',"on":' .. json_bool(on) .. '}'
end
end
return '{"ok":true,"items":[' .. table.concat(items, ',') .. ']}\n'
end)
if ok and res then return res end
return '{"ok":false,"items":[],"error":' .. json_string(tostring(res)) .. '}\n'
end
local function token_for(btype, subtype, custom)
return ('%d:%d:%d'):format(tonumber(btype) or -1, tonumber(subtype) or -1, tonumber(custom) or -1)
end
local function parse_token(token)
local t, s, c = tostring(token or ''):match('^(-?%d+):(-?%d+):(-?%d+)$')
if not t then return nil end
return tonumber(t), tonumber(s), tonumber(c)
end
local function direction_options(kind)
if kind == 'axis' then
return {
{label='E-W', value=0},
{label='N-S', value=1},
}
elseif kind == 'bridge' then
return {
{label='Retract', value=-1},
{label='West', value=0},
{label='East', value=1},
{label='North', value=2},
{label='South', value=3},
}
elseif kind == 'pump' then
return {
{label='From N', value=df.screw_pump_direction.FromNorth},
{label='From E', value=df.screw_pump_direction.FromEast},
{label='From S', value=df.screw_pump_direction.FromSouth},
{label='From W', value=df.screw_pump_direction.FromWest},
}
end
return {
{label='N', value=0},
{label='E', value=1},
{label='S', value=2},
{label='W', value=3},
}
end
local function requirements_for(filters)
-- NOTE: deliberately does NOT call into plugins.buildingplan. buildingplan.get_desc makes a
-- nested CallLuaModuleFunction that misbehaves from dfcapture's render-thread context and can
-- raise a luaL_argerror whose error-message formatting overflows the render thread's tiny
-- stack and HARD-CRASHES the game (intermittent build-menu crash). dfcapture places buildings
-- via constructBuilding directly, so we don't need buildingplan here. Use the filter's own name.
local out = {}
for _, filter in ipairs(filters or {}) do
local desc = (filter and filter.name and #tostring(filter.name) > 0)
and tostring(filter.name) or 'Material'
table.insert(out, {label=desc, quantity=(filter and filter.quantity) or 1})
end
return out
end
local function correct_size(width, height, btype, subtype, custom, direction)
local ok, used, adjusted_w, adjusted_h = pcall(
dfhack.buildings.getCorrectSize,
width or 1, height or 1, btype, subtype or -1, custom or -1, direction or 0)
if ok and used then
return adjusted_w or width or 1, adjusted_h or height or 1
end
return width or 1, height or 1
end
local function item_to_json(item)
local parts = {
'"token":' .. json_string(item.token),
'"label":' .. json_string(item.label),
'"category":' .. json_string(item.category),
'"type":' .. tostring(item.type),
'"subtype":' .. tostring(item.subtype or -1),
'"custom":' .. tostring(item.custom or -1),
'"area":' .. json_bool(item.area),
'"direction":' .. json_bool(item.direction),
'"directionKind":' .. json_string(item.direction_kind or ''),
'"hollow":' .. json_bool(item.hollow),
'"pressure":' .. json_bool(item.pressure),
'"trackStop":' .. json_bool(item.track_stop),
'"weaponCount":' .. json_bool(item.weapon_count),
'"speed":' .. json_bool(item.speed),
'"customRaw":' .. json_bool(item.custom_raw),
'"size":{"w":' .. tostring(item.size_w or 1) .. ',"h":' .. tostring(item.size_h or 1) .. '}',
'"limit":{"w":' .. tostring(item.limit_w or 1) .. ',"h":' .. tostring(item.limit_h or 1) .. '}',
}
local dir = direction_options(item.direction_kind)
local dir_parts = {}
if item.direction then
for _, opt in ipairs(dir) do
table.insert(dir_parts, '{"label":' .. json_string(opt.label) .. ',"value":' .. tostring(opt.value) .. '}')
end
end
table.insert(parts, '"directions":[' .. table.concat(dir_parts, ',') .. ']')
local req_parts = {}
for _, req in ipairs(item.requirements or {}) do
table.insert(req_parts, '{"label":' .. json_string(req.label) ..
',"quantity":' .. tostring(req.quantity or 1) .. '}')
end
table.insert(parts, '"requirements":[' .. table.concat(req_parts, ',') .. ']')
return '{' .. table.concat(parts, ',') .. '}'
end
local function category_to_json(cat, count)
return '{"id":' .. json_string(cat.id) ..
',"label":' .. json_string(cat.label) ..
',"count":' .. tostring(count or 0) .. '}'
end
local function add_build_item(items, category, label, btype, subtype, custom, opts)
if btype == nil then return end
subtype = subtype or -1
custom = custom or -1
opts = opts or {}
if btype == df.building_type.Construction and subtype == df.construction_type.TrackNSEW then
return
end
local ok_filters, filters = pcall(dfhack.buildings.getFiltersByType, {}, btype, subtype, custom)
if not ok_filters or filters == nil then
return
end
-- NOTE: do NOT call buildingplan.isPlannableBuilding here. Its nested CallLuaModuleFunction
-- from dfcapture's render-thread context can raise an error that overflows the render thread's
-- stack and HARD-CRASHES the game (intermittent build-menu crash). dfcapture places via
-- constructBuilding directly, so the plannable filter isn't needed -- getFiltersByType above
-- already establishes the building can be built.
local size_w, size_h = correct_size(1, 1, btype, subtype, custom, opts.default_direction or 0)
local item = {
category=category,
label=label,
type=btype,
subtype=subtype,
custom=custom,
token=token_for(btype, subtype, custom),
area=opts.area or false,
direction=opts.direction or false,
direction_kind=opts.direction_kind or '',
hollow=opts.hollow or false,
pressure=opts.pressure or false,
track_stop=opts.track_stop or false,
weapon_count=opts.weapon_count or false,
speed=opts.speed or false,
custom_raw=opts.custom_raw or false,
size_w=size_w,
size_h=size_h,
limit_w=opts.limit_w or size_w or 1,
limit_h=opts.limit_h or size_h or 1,
requirements=requirements_for(filters),
}
table.insert(items, item)
end
local function add_native_build_items(items)
local bt = df.building_type
local wt = df.workshop_type
local ft = df.furnace_type
local tt = df.trap_type
local st = df.siegeengine_type
local ct = df.construction_type
add_build_item(items, 'furniture', 'Chair / Throne', bt.Chair)
add_build_item(items, 'furniture', 'Bed', bt.Bed)
add_build_item(items, 'furniture', 'Table', bt.Table)
add_build_item(items, 'furniture', 'Coffin', bt.Coffin)
add_build_item(items, 'furniture', 'Door', bt.Door)
add_build_item(items, 'furniture', 'Floodgate', bt.Floodgate)
add_build_item(items, 'furniture', 'Hatch cover', bt.Hatch)
add_build_item(items, 'furniture', 'Wall grate', bt.GrateWall)
add_build_item(items, 'furniture', 'Floor grate', bt.GrateFloor)
add_build_item(items, 'furniture', 'Vertical bars', bt.BarsVertical)
add_build_item(items, 'furniture', 'Floor bars', bt.BarsFloor)
add_build_item(items, 'furniture', 'Cabinet', bt.Cabinet)
add_build_item(items, 'furniture', 'Statue', bt.Statue)
add_build_item(items, 'furniture', 'Slab', bt.Slab)
add_build_item(items, 'furniture', 'Glass window', bt.WindowGlass)
add_build_item(items, 'furniture', 'Gem window', bt.WindowGem)
add_build_item(items, 'furniture', 'Box / Chest', bt.Box)
add_build_item(items, 'furniture', 'Cage', bt.Cage)
add_build_item(items, 'furniture', 'Chain / Rope', bt.Chain)
add_build_item(items, 'furniture', 'Bookcase', bt.Bookcase)
add_build_item(items, 'furniture', 'Display furniture', bt.DisplayFurniture)
add_build_item(items, 'furniture', 'Offering place', bt.OfferingPlace)
add_build_item(items, 'furniture', 'Stationary instrument', bt.Instrument)
add_build_item(items, 'workshops', 'Carpenter', bt.Workshop, wt.Carpenters)
add_build_item(items, 'workshops', 'Farmer', bt.Workshop, wt.Farmers)
add_build_item(items, 'workshops', 'Mason / Stoneworker', bt.Workshop, wt.Masons)
add_build_item(items, 'workshops', 'Craftsdwarf', bt.Workshop, wt.Craftsdwarfs)
add_build_item(items, 'workshops', 'Jeweler', bt.Workshop, wt.Jewelers)
add_build_item(items, 'workshops', 'Metalsmith forge', bt.Workshop, wt.MetalsmithsForge)
add_build_item(items, 'workshops', 'Magma forge', bt.Workshop, wt.MagmaForge)
add_build_item(items, 'workshops', 'Bowyer', bt.Workshop, wt.Bowyers)
add_build_item(items, 'workshops', 'Mechanic', bt.Workshop, wt.Mechanics)
add_build_item(items, 'workshops', 'Siege', bt.Workshop, wt.Siege)
add_build_item(items, 'workshops', 'Butcher', bt.Workshop, wt.Butchers)
add_build_item(items, 'workshops', 'Leather works', bt.Workshop, wt.Leatherworks)
add_build_item(items, 'workshops', 'Tanner', bt.Workshop, wt.Tanners)
add_build_item(items, 'workshops', 'Clothier', bt.Workshop, wt.Clothiers)
add_build_item(items, 'workshops', 'Fishery', bt.Workshop, wt.Fishery)
add_build_item(items, 'workshops', 'Still', bt.Workshop, wt.Still)
add_build_item(items, 'workshops', 'Loom', bt.Workshop, wt.Loom)
add_build_item(items, 'workshops', 'Quern', bt.Workshop, wt.Quern)
add_build_item(items, 'workshops', 'Kennel', bt.Workshop, wt.Kennels)
add_build_item(items, 'workshops', 'Kitchen', bt.Workshop, wt.Kitchen)
add_build_item(items, 'workshops', 'Ashery', bt.Workshop, wt.Ashery)
add_build_item(items, 'workshops', 'Dyer', bt.Workshop, wt.Dyers)
add_build_item(items, 'workshops', 'Millstone', bt.Workshop, wt.Millstone)
add_build_item(items, 'furnaces', 'Wood furnace', bt.Furnace, ft.WoodFurnace)
add_build_item(items, 'furnaces', 'Smelter', bt.Furnace, ft.Smelter)
add_build_item(items, 'furnaces', 'Glass furnace', bt.Furnace, ft.GlassFurnace)
add_build_item(items, 'furnaces', 'Kiln', bt.Furnace, ft.Kiln)
add_build_item(items, 'furnaces', 'Magma smelter', bt.Furnace, ft.MagmaSmelter)
add_build_item(items, 'furnaces', 'Magma glass furnace', bt.Furnace, ft.MagmaGlassFurnace)
add_build_item(items, 'furnaces', 'Magma kiln', bt.Furnace, ft.MagmaKiln)
add_build_item(items, 'constructions', 'Wall', bt.Construction, ct.Wall, -1, {area=true, hollow=true, limit_w=31, limit_h=31})
add_build_item(items, 'constructions', 'Floor', bt.Construction, ct.Floor, -1, {area=true, hollow=true, limit_w=31, limit_h=31})
add_build_item(items, 'constructions', 'Ramp', bt.Construction, ct.Ramp, -1, {area=true, hollow=true, limit_w=31, limit_h=31})
add_build_item(items, 'constructions', 'Up stair', bt.Construction, ct.UpStair, -1, {area=true, hollow=true, limit_w=31, limit_h=31})
add_build_item(items, 'constructions', 'Down stair', bt.Construction, ct.DownStair, -1, {area=true, hollow=true, limit_w=31, limit_h=31})
add_build_item(items, 'constructions', 'Up/down stair', bt.Construction, ct.UpDownStair, -1, {area=true, hollow=true, limit_w=31, limit_h=31})
add_build_item(items, 'constructions', 'Fortification', bt.Construction, ct.Fortification, -1, {area=true, hollow=true, limit_w=31, limit_h=31})
add_build_item(items, 'constructions', 'Reinforced wall', bt.Construction, ct.ReinforcedWall, -1, {area=true, hollow=true, limit_w=31, limit_h=31})
add_build_item(items, 'machines', 'Screw pump', bt.ScrewPump, -1, -1, {direction=true, direction_kind='pump'})
add_build_item(items, 'machines', 'Gear assembly', bt.GearAssembly)
add_build_item(items, 'machines', 'Horizontal axle', bt.AxleHorizontal, -1, -1, {area=true, direction=true, direction_kind='axis', limit_w=31, limit_h=31})
add_build_item(items, 'machines', 'Vertical axle', bt.AxleVertical)
add_build_item(items, 'machines', 'Water wheel', bt.WaterWheel, -1, -1, {direction=true})
add_build_item(items, 'machines', 'Windmill', bt.Windmill)
add_build_item(items, 'machines', 'Rollers', bt.Rollers, -1, -1, {area=true, direction=true, speed=true, limit_w=31, limit_h=31})
add_build_item(items, 'machines', 'Support', bt.Support)
add_build_item(items, 'machines', 'Well', bt.Well)
add_build_item(items, 'machines', 'Bridge', bt.Bridge, -1, -1, {area=true, direction=true, direction_kind='bridge', limit_w=31, limit_h=31})
add_build_item(items, 'traps', 'Lever', bt.Trap, tt.Lever)
add_build_item(items, 'traps', 'Pressure plate', bt.Trap, tt.PressurePlate, -1, {pressure=true})
add_build_item(items, 'traps', 'Cage trap', bt.Trap, tt.CageTrap)
add_build_item(items, 'traps', 'Stone-fall trap', bt.Trap, tt.StoneFallTrap)
add_build_item(items, 'traps', 'Weapon trap', bt.Trap, tt.WeaponTrap, -1, {weapon_count=true})
add_build_item(items, 'traps', 'Track stop', bt.Trap, tt.TrackStop, -1, {direction=true, track_stop=true})
add_build_item(items, 'traps', 'Animal trap', bt.AnimalTrap)
add_build_item(items, 'traps', 'Upright spear / spike', bt.Weapon, -1, -1, {weapon_count=true})
add_build_item(items, 'siege', 'Catapult', bt.SiegeEngine, st.Catapult, -1, {direction=true})
add_build_item(items, 'siege', 'Ballista', bt.SiegeEngine, st.Ballista, -1, {direction=true})
add_build_item(items, 'siege', 'Bolt thrower', bt.SiegeEngine, st.BoltThrower, -1, {direction=true})
add_build_item(items, 'farming', 'Farm plot', bt.FarmPlot, -1, -1, {area=true, limit_w=31, limit_h=31})
add_build_item(items, 'farming', 'Nest box', bt.NestBox)
add_build_item(items, 'farming', 'Hive', bt.Hive)
add_build_item(items, 'farming', 'Archery target', bt.ArcheryTarget)
add_build_item(items, 'farming', 'Armor stand', bt.Armorstand)
add_build_item(items, 'farming', 'Weapon rack', bt.Weaponrack)
add_build_item(items, 'farming', 'Traction bench', bt.TractionBench)
add_build_item(items, 'trade', 'Trade depot', bt.TradeDepot)
add_build_item(items, 'trade', 'Dirt road', bt.RoadDirt, -1, -1, {area=true, limit_w=31, limit_h=31})
add_build_item(items, 'trade', 'Paved road', bt.RoadPaved, -1, -1, {area=true, limit_w=31, limit_h=31})
local track_names = {
{ct.TrackN, 'Track N'}, {ct.TrackS, 'Track S'}, {ct.TrackE, 'Track E'}, {ct.TrackW, 'Track W'},
{ct.TrackNS, 'Track N-S'}, {ct.TrackNE, 'Track N-E'}, {ct.TrackNW, 'Track N-W'},
{ct.TrackSE, 'Track S-E'}, {ct.TrackSW, 'Track S-W'}, {ct.TrackEW, 'Track E-W'},
{ct.TrackNSE, 'Track N-S-E'}, {ct.TrackNSW, 'Track N-S-W'},
{ct.TrackNEW, 'Track N-E-W'}, {ct.TrackSEW, 'Track S-E-W'},
{ct.TrackRampN, 'Track ramp N'}, {ct.TrackRampS, 'Track ramp S'},
{ct.TrackRampE, 'Track ramp E'}, {ct.TrackRampW, 'Track ramp W'},
{ct.TrackRampNS, 'Track ramp N-S'}, {ct.TrackRampNE, 'Track ramp N-E'},
{ct.TrackRampNW, 'Track ramp N-W'}, {ct.TrackRampSE, 'Track ramp S-E'},
{ct.TrackRampSW, 'Track ramp S-W'}, {ct.TrackRampEW, 'Track ramp E-W'},
{ct.TrackRampNSE, 'Track ramp N-S-E'}, {ct.TrackRampNSW, 'Track ramp N-S-W'},
{ct.TrackRampNEW, 'Track ramp N-E-W'}, {ct.TrackRampSEW, 'Track ramp S-E-W'},
{ct.TrackRampNSEW, 'Track ramp N-S-E-W'},
}
for _, t in ipairs(track_names) do
add_build_item(items, 'track', t[2], bt.Construction, t[1], -1, {area=true, hollow=false, limit_w=31, limit_h=31})
end
end
local function add_custom_build_items(items)
local world = df.global.world
if not world or not world.raws or not world.raws.buildings then return end
for _, def in ipairs(world.raws.buildings.workshops or {}) do
if def then
add_build_item(items, 'workshops',
(def.name and #def.name > 0) and def.name or def.code or 'Custom workshop',
df.building_type.Workshop, df.workshop_type.Custom, def.id,
{custom_raw=true})
end
end
for _, def in ipairs(world.raws.buildings.furnaces or {}) do
if def then
add_build_item(items, 'furnaces',
(def.name and #def.name > 0) and def.name or def.code or 'Custom furnace',
df.building_type.Furnace, df.furnace_type.Custom, def.id,
{custom_raw=true})
end