forked from Squamousness/Dfhack-Testing-Bin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilding.lua
More file actions
1893 lines (1754 loc) · 83.3 KB
/
building.lua
File metadata and controls
1893 lines (1754 loc) · 83.3 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
-- building.lua
-- Converts NPC site building materials on map load:
-- hardwood logs -> earthenware bricks
-- non-ceramic stone -> concrete
--
-- DFHack tiletype reference: https://github.com/DFHack/dfhack/blob/master/library/TileTypes.cpp
local CONCRETE_TOKEN = "INORGANIC:CONCRETE"
local EARTHENWARE_TOKEN = "INORGANIC:CERAMIC_EARTHENWARE"
local STONEWARE_TOKEN = "INORGANIC:CERAMIC_STONEWARE"
local GLASS_CLEAR_TOKEN = "GLASS_CLEAR"
local IRON_TOKEN = "INORGANIC:IRON"
local HARDWOOD_LOG_TOKEN = "PLANT:HARDWOOD_TREE:WOOD"
local HARDWOOD_LOG_TOKEN_LEGACY = "PLANT_MAT:HARDWOOD_TREE:WOOD"
local PLYWOOD_LOG_TOKEN = "PLANT:PLYWOOD_TREE:WOOD"
local PLYWOOD_LOG_TOKEN_LEGACY = "PLANT_MAT:PLYWOOD_TREE:WOOD"
local HAMLET_HARDWOOD_CONVERT_PERCENT = 30
local INITIAL_FORCE_DELAY_TICKS = 100
local CONCRETE_MAT_TYPE
local CONCRETE_MAT_INDEX
local EARTHENWARE_MAT_TYPE
local EARTHENWARE_MAT_INDEX
local STONEWARE_MAT_TYPE
local STONEWARE_MAT_INDEX
local GLASS_CLEAR_MAT_TYPE
local GLASS_CLEAR_MAT_INDEX
local IRON_MAT_TYPE
local IRON_MAT_INDEX
local HARDWOOD_MAT_TYPE
local HARDWOOD_MAT_INDEX
local PLYWOOD_MAT_TYPE
local PLYWOOD_MAT_INDEX
-- Maps LRUD connection suffix → ConstructedWall<suffix> tiletype index.
-- Used to preserve connection direction when rewriting stone wall tiles.
local constructed_wall_by_suffix = (function()
local t = {}
local constr_mat = df.tiletype_material.CONSTRUCTION
for i = 0, 65535 do
local name = df.tiletype[i]
local attrs = df.tiletype.attrs[i]
if name and attrs and attrs.material == constr_mat then
local suffix = tostring(name):match("^ConstructedWall([LRUD]*)$")
if suffix ~= nil then
t[suffix] = i
end
end
end
return t
end)()
-- Maps LRUD connection suffix → ConstructedFortification<suffix> tiletype index.
local constructed_fortification_by_suffix = (function()
local t = {}
local constr_mat = df.tiletype_material.CONSTRUCTION
for i = 0, 65535 do
local name = df.tiletype[i]
local attrs = df.tiletype.attrs[i]
if name and attrs and attrs.material == constr_mat then
local suffix = tostring(name):match("^ConstructedFortification([LRUD]*)$")
if suffix ~= nil then
t[suffix] = i
end
end
end
return t
end)()
-- ── STONE CONSTRUCTION TILE RULES ─────────────────────────────────────────────
-- Add/remove entries here to control which worldgen stone tiles get converted.
-- pattern : Lua pattern matched against the tiletype name
-- inside_only : true = skip tile if designation.outside is true (natural surface)
-- target_fn : function(name) → target tiletype index (nil = add record, skip rewrite)
-- ──────────────────────────────────────────────────────────────────────────────
local STONE_CONSTR_RULES -- defined after constructs (moved there)
local CONSTRUCTED_RAMP_TT = (function()
local constr_mat = df.tiletype_material.CONSTRUCTION
for i = 0, 65535 do
local name = df.tiletype[i]
local attrs = df.tiletype.attrs[i]
if name and attrs and attrs.material == constr_mat and tostring(name):find("^ConstructedRamp") then
return i
end
end
return nil
end)()
local CONSTRUCTED_FLOOR_TT = (function()
local constr_mat = df.tiletype_material.CONSTRUCTION
for i = 0, 65535 do
local name = df.tiletype[i]
local attrs = df.tiletype.attrs[i]
if name and attrs and attrs.material == constr_mat and tostring(name) == "ConstructedFloor" then
return i
end
end
return nil
end)()
local CONSTRUCTED_PILLAR_TT = (function()
local constr_mat = df.tiletype_material.CONSTRUCTION
for i = 0, 65535 do
local name = df.tiletype[i]
local attrs = df.tiletype.attrs[i]
if name and attrs and attrs.material == constr_mat and tostring(name) == "ConstructedPillar" then
return i
end
end
return nil
end)()
local CONSTRUCTED_WALL_TT = (function()
local constr_mat = df.tiletype_material.CONSTRUCTION
-- Prefer the canonical connected constructed wall token if present.
for i = 0, 65535 do
local name = df.tiletype[i]
local attrs = df.tiletype.attrs[i]
if name and attrs and attrs.material == constr_mat and tostring(name) == "ConstructedWallLRUD" then
return i
end
end
-- Fallback to any ConstructedWall* token in this raws/tileset.
for i = 0, 65535 do
local name = df.tiletype[i]
local attrs = df.tiletype.attrs[i]
if name and attrs and attrs.material == constr_mat and tostring(name):find("^ConstructedWall") then
return i
end
end
-- Final fallback: classic wall-shape construction.
local wall_shape = df.tiletype_shape_basic.Wall
for i = 0, 1000 do
local attrs = df.tiletype.attrs[i]
if attrs and attrs.shape == wall_shape and attrs.material == constr_mat then
return i
end
end
return nil
end)()
STONE_CONSTR_RULES = { -- assigned here; cache built immediately after
{ pattern = "WallSmooth", inside_only = false, target_fn = function(name)
local suffix = name:match("([LRUD]*)$") or ""
return constructed_wall_by_suffix[suffix] or CONSTRUCTED_WALL_TT
end },
{ pattern = "Fortif", inside_only = false, target_fn = function(name)
local suffix = name:match("([LRUD]*)$") or ""
return constructed_fortification_by_suffix[suffix] or constructed_fortification_by_suffix[""] or CONSTRUCTED_WALL_TT
end },
{ pattern = "Pillar", inside_only = true, target_fn = function(_)
return CONSTRUCTED_PILLAR_TT or CONSTRUCTED_WALL_TT
end },
{ pattern = "Ramp", inside_only = true, target_fn = function(_)
return CONSTRUCTED_RAMP_TT
end },
}
-- Precomputed per-tiletype lookup built from STONE_CONSTR_RULES.
-- Avoids all string operations in the Pass 2 hot path: one table lookup per tile.
-- Value: {target=tiletype_index_or_nil, inside_only=bool}, or nil if not a candidate.
local stone_constr_tt_cache = (function()
local t = {}
local constr_mat = df.tiletype_material.CONSTRUCTION
for tt = 0, 65535 do
local name = tostring(df.tiletype[tt] or "")
local attrs = df.tiletype.attrs[tt]
if attrs and attrs.material ~= constr_mat then
local sp = name:find("^Stone") or name:find("^Lava") or
name:find("^Mineral") or name:find("^Feature")
if sp then
for _, rule in ipairs(STONE_CONSTR_RULES) do
if name:find(rule.pattern) then
t[tt] = {target = rule.target_fn(name), inside_only = rule.inside_only}
break
end
end
end
end
end
return t
end)()
local S = rawget(_G, "__building_state")
if not S then
S = {
debug = false,
last_map_key = nil,
watcher_enabled = false,
watcher_gen = 0,
watcher_interval = 5,
init_force_gen = 0,
processed_blocks = {}, -- block keys already scanned by Pass 2
last_site_id = nil, -- site id when processed_blocks was last reset
floor_scan_gen = 0,
floor_scan_interval = 150, -- ticks between nearby floor scans
keep_bounds = {}, -- cached keep bounding boxes for periodic courtyard scan
}
_G.__building_state = S
end
local function log(msg)
print("[building] " .. msg)
end
local function dlog(msg)
if S.debug then
log("DIAG: " .. msg)
end
end
local function add_count(tbl, key)
if not key then key = "<nil>" end
tbl[key] = (tbl[key] or 0) + 1
end
local function log_top_counts(prefix, tbl, limit)
local rows = {}
for k, v in pairs(tbl) do
rows[#rows + 1] = {k = k, v = v}
end
table.sort(rows, function(a, b)
if a.v == b.v then
return tostring(a.k) < tostring(b.k)
end
return a.v > b.v
end)
local n = math.min(limit or 10, #rows)
if n == 0 then
dlog(prefix .. ": none")
return
end
for i = 1, n do
dlog(("%s [%d] %s = %d"):format(prefix, i, tostring(rows[i].k), rows[i].v))
end
end
local function hash_percent(site_id, a, b, c)
local mod = 2147483647
local h = 1234567
h = (h * 1103515245 + (site_id or 0) + 12345) % mod
h = (h * 1103515245 + (a or 0) + 12345) % mod
h = (h * 1103515245 + (b or 0) + 12345) % mod
h = (h * 1103515245 + (c or 0) + 12345) % mod
return h % 100
end
local function get_current_site()
if dfhack.world and dfhack.world.getCurrentSite then
return dfhack.world.getCurrentSite()
end
if df.global.plotinfo and df.global.plotinfo.site_id and df.global.plotinfo.site_id >= 0 then
return df.world_site.find(df.global.plotinfo.site_id)
end
return nil
end
local HARSH_BIOMES = {
[df.biome_type.DESERT_BADLAND] = true,
[df.biome_type.DESERT_ROCK] = true,
[df.biome_type.DESERT_SAND] = true,
[df.biome_type.TUNDRA] = true,
}
local function get_map_biome_id()
if not dfhack.isMapLoaded() then return nil end
local wm = df.global.world.map
local xmax, ymax = dfhack.maps.getTileSize()
local cx = math.floor(xmax / 2)
local cy = math.floor(ymax / 2)
local ground_z = (wm.z_count - 2) - df.global.world.worldgen.worldgen_parms.levels_above_ground
local rgnX, rgnY
for z = ground_z, 0, -1 do
rgnX, rgnY = dfhack.maps.getTileBiomeRgn(cx, cy, z)
if rgnX then break end
end
if not rgnX then return nil end
return dfhack.maps.getBiomeType(rgnX, rgnY)
end
local function get_site_policy()
local site = get_current_site()
local site_id = site and site.id or -1
local site_type = site and df.world_site_type[site.type] or "NONE"
local site_type_l = tostring(site_type):lower()
local is_hamlet = (site_type_l:find("hamlet") ~= nil) or (site_type_l:find("hillock") ~= nil)
local biome_id = get_map_biome_id()
local is_harsh_biome = biome_id ~= nil and HARSH_BIOMES[biome_id] == true
return {
site = site,
site_id = site_id,
site_type = tostring(site_type),
is_hamlet = is_hamlet,
hardwood_percent = is_hamlet and HAMLET_HARDWOOD_CONVERT_PERCENT or 100,
is_harsh_biome = is_harsh_biome,
}
end
local function should_convert_hardwood_id(site_id, id_a, id_b, id_c, percent)
if percent >= 100 then return true end
return hash_percent(site_id, id_a, id_b, id_c) < percent
end
local function find_materials()
local concrete = dfhack.matinfo.find(CONCRETE_TOKEN)
if not concrete then
log(CONCRETE_TOKEN .. " material not found in raws")
return false
end
local earthenware = dfhack.matinfo.find(EARTHENWARE_TOKEN)
if not earthenware then
log(EARTHENWARE_TOKEN .. " material not found in raws")
return false
end
local stoneware = dfhack.matinfo.find(STONEWARE_TOKEN)
if not stoneware then
log(STONEWARE_TOKEN .. " material not found in raws")
return false
end
local hardwood = dfhack.matinfo.find(HARDWOOD_LOG_TOKEN)
or dfhack.matinfo.find(HARDWOOD_LOG_TOKEN_LEGACY)
if not hardwood then
log("WARNING: hardwood material not found; natural floors will not be converted")
end
CONCRETE_MAT_TYPE = concrete.type
CONCRETE_MAT_INDEX = concrete.index
EARTHENWARE_MAT_TYPE = earthenware.type
EARTHENWARE_MAT_INDEX = earthenware.index
STONEWARE_MAT_TYPE = stoneware.type
STONEWARE_MAT_INDEX = stoneware.index
if hardwood then
HARDWOOD_MAT_TYPE = hardwood.type
HARDWOOD_MAT_INDEX = hardwood.index
end
local glass = dfhack.matinfo.find(GLASS_CLEAR_TOKEN)
if not glass then
log("WARNING: " .. GLASS_CLEAR_TOKEN .. " not found; glass block walls will not be placed")
else
GLASS_CLEAR_MAT_TYPE = glass.type
GLASS_CLEAR_MAT_INDEX = glass.index
end
local iron = dfhack.matinfo.find(IRON_TOKEN)
if not iron then
log("WARNING: " .. IRON_TOKEN .. " not found; iron fortifications will not be placed")
else
IRON_MAT_TYPE = iron.type
IRON_MAT_INDEX = iron.index
end
local plywood = dfhack.matinfo.find(PLYWOOD_LOG_TOKEN)
or dfhack.matinfo.find(PLYWOOD_LOG_TOKEN_LEGACY)
if not plywood then
log("WARNING: plywood material not found; plywood buildings will not receive windows")
else
PLYWOOD_MAT_TYPE = plywood.type
PLYWOOD_MAT_INDEX = plywood.index
end
return true
end
local function map_key()
if not dfhack.isMapLoaded() then return nil end
local wm = df.global.world.map
local cons = df.global.world.event.constructions
return ("%d,%d,%d,%d,%d,%d"):format(
wm.region_x, wm.region_y, wm.x_count, wm.y_count, #wm.map_blocks, #cons)
end
local function block_key(x, y, z)
local bx = x - (x % 16)
local by = y - (y % 16)
return ("%d,%d,%d"):format(bx, by, z)
end
local function get_block_and_local(block_cache, x, y, z)
local bk = block_key(x, y, z)
local block = block_cache[bk]
if block == nil then
block = dfhack.maps.getTileBlock(x, y, z)
block_cache[bk] = block or false
elseif block == false then
block = nil
end
if not block then return nil end
return block, x % 16, y % 16
end
local function is_path_tile(tt)
local name = df.tiletype[tt]
return name ~= nil and name:find("Path") ~= nil
end
local function is_natural_floor_or_ramp(tt)
local attrs = df.tiletype.attrs[tt]
if not attrs then return false end
return attrs.shape == df.tiletype_shape_basic.Floor or attrs.shape == df.tiletype_shape_basic.Ramp
end
local function likely_worldgen_road_construction(c, block_cache)
local block, lx, ly = get_block_and_local(block_cache, c.pos.x, c.pos.y, c.pos.z)
if not block then return false end
if not block.designation[lx][ly].outside then
return false
end
local tt = block.tiletype[lx][ly]
-- Building conversion should only exclude true path road tiles.
return is_path_tile(tt)
end
local function decode_token(mat_type, mat_index)
local info = dfhack.matinfo.decode(mat_type, mat_index)
if not info then return nil, nil end
return info:getToken(), info
end
local function is_stone_for_concrete(info)
if not info or not info.material or not info.material.flags then
return false
end
local f = info.material.flags
return f.IS_STONE and not f.IS_CERAMIC
end
local function is_builtin_inorganic(token, info)
return info and info.mode == "builtin" and token == "INORGANIC"
end
local function key_xyz(x, y, z)
return ("%d,%d,%d"):format(x, y, z)
end
local function is_building_tile(block, lx, ly)
return block.occupancy[lx][ly].building ~= 0
end
local function set_construction_material(c, mat_type, mat_index)
if c.mat_type == mat_type and c.mat_index == mat_index then
return false
end
c.mat_type = mat_type
c.mat_index = mat_index
return true
end
local function diag_walls(radius)
if not dfhack.isMapLoaded() then
log("No map loaded.")
return
end
local adv = dfhack.world.getAdventurer()
if not adv or not adv.pos then
log("No active adventurer found for diag scan.")
return
end
local r = tonumber(radius) or 20
local cx, cy, cz = adv.pos.x, adv.pos.y, adv.pos.z
local constructions = df.global.world.event.constructions
local existing = {}
for i = 0, #constructions - 1 do
local c = constructions[i]
existing[key_xyz(c.pos.x, c.pos.y, c.pos.z)] = c
end
local walls_seen = 0
local walls_with_construction = 0
local by_name = {}
local by_name_with_construction = {}
for y = cy - r, cy + r do
for x = cx - r, cx + r do
local block = dfhack.maps.getTileBlock(x, y, cz)
if block then
local lx, ly = x % 16, y % 16
local tt = block.tiletype[lx][ly]
local attrs = df.tiletype.attrs[tt]
if attrs and (attrs.shape == df.tiletype_shape_basic.Wall or attrs.shape == df.tiletype_shape_basic.Fortification) then
walls_seen = walls_seen + 1
local name = tostring(df.tiletype[tt] or ("tt_" .. tostring(tt)))
add_count(by_name, name)
if existing[key_xyz(x, y, cz)] then
walls_with_construction = walls_with_construction + 1
add_count(by_name_with_construction, name)
end
end
end
end
end
log(("Diag walls around adventurer r=%d: walls_seen=%d walls_with_construction=%d"):format(
r, walls_seen, walls_with_construction))
if S.debug then
log_top_counts("diag wall tiletype", by_name, 20)
log_top_counts("diag wall tiletype with construction", by_name_with_construction, 20)
end
end
local KEEP_COURTYARD_EXPAND = 10 -- tiles to expand keep bounds beyond the TowerTop bounding box
local KEEP_Z_RANGE = 4 -- z-levels above and below seed z to scan for outdoor ground tiles
-- Materials treated as paveable natural ground around keeps:
-- grass (all variants), soil/dirt, bare stone, lava stone, mineral stone.
local KEEP_PAVE_MATS = (function()
local t = {}
for _, name in ipairs({
"GRASS", "GRASS_LIGHT", "GRASS_DARK", "GRASS_DRY", "GRASS_DEAD",
"SOIL", "STONE", "LAVA_STONE", "MINERAL",
}) do
local ok, v = pcall(function() return df.tiletype_material[name] end)
if ok and v ~= nil then t[v] = true end
end
return t
end)()
local function convert_map_constructions(policy, force)
local wm = df.global.world.map
local constructions = df.global.world.event.constructions
local block_cache = {}
local road_cache = {}
local mat_cache = {}
local changed_hardwood = 0
local skipped_hardwood = 0
local changed_stone = 0
local already_target = 0
local skipped_roads = 0
local considered = 0
local token_counts = {}
local mode_counts = {}
local unresolved_counts = {}
local matpair_counts = {}
local hardwood_keys = {}
local existing = {}
local fallback_wall_changed = 0
local fallback_wall_inserted = 0
local fallback_wall_candidates = 0
local natural_floor_inserted = 0
local glass_walls_placed = 0
local iron_fortifications_placed = 0
local mud_paved = 0
local function get_matinfo(mat_type, mat_index)
local mk = ("%d:%d"):format(mat_type, mat_index)
local cached = mat_cache[mk]
if cached ~= nil then
if cached == false then return nil, nil end
return cached.token, cached.info
end
local token, info = decode_token(mat_type, mat_index)
if not token or not info then
mat_cache[mk] = false
return nil, nil
end
mat_cache[mk] = {token = token, info = info}
return token, info
end
local function is_road(c)
local k = key_xyz(c.pos.x, c.pos.y, c.pos.z)
local v = road_cache[k]
if v ~= nil then return v end
v = likely_worldgen_road_construction(c, block_cache)
road_cache[k] = v
return v
end
local function is_loaded_construction(c)
return dfhack.maps.getTileBlock(c.pos) ~= nil
end
-- Build connected hardwood groups so hamlet policy applies per-house, not per-tile.
if policy.is_hamlet then
local hardwood_lookup = {}
for i = 0, #constructions - 1 do
local c = constructions[i]
if is_loaded_construction(c) then
if not is_road(c) then
local token = get_matinfo(c.mat_type, c.mat_index)
if token == HARDWOOD_LOG_TOKEN or token == HARDWOOD_LOG_TOKEN_LEGACY then
local k = key_xyz(c.pos.x, c.pos.y, c.pos.z)
hardwood_lookup[k] = c
end
end
end
end
local seen = {}
for root_k, root_c in pairs(hardwood_lookup) do
if not seen[root_k] then
local stack = {root_c}
local component = {}
seen[root_k] = true
while #stack > 0 do
local cur = stack[#stack]
stack[#stack] = nil
local ck = key_xyz(cur.pos.x, cur.pos.y, cur.pos.z)
component[#component + 1] = cur
local nbrs = {
{cur.pos.x + 1, cur.pos.y, cur.pos.z},
{cur.pos.x - 1, cur.pos.y, cur.pos.z},
{cur.pos.x, cur.pos.y + 1, cur.pos.z},
{cur.pos.x, cur.pos.y - 1, cur.pos.z},
{cur.pos.x, cur.pos.y, cur.pos.z + 1},
{cur.pos.x, cur.pos.y, cur.pos.z - 1},
}
for _, n in ipairs(nbrs) do
local nk = key_xyz(n[1], n[2], n[3])
local nc = hardwood_lookup[nk]
if nc and not seen[nk] then
seen[nk] = true
stack[#stack + 1] = nc
end
end
end
local rep = component[1]
local do_convert = should_convert_hardwood_id(
policy.site_id, rep.pos.x, rep.pos.y, rep.pos.z, policy.hardwood_percent)
for _, cc in ipairs(component) do
hardwood_keys[key_xyz(cc.pos.x, cc.pos.y, cc.pos.z)] = do_convert
end
end
end
end
-- Single pass: classify + convert non-road constructions.
for i = 0, #constructions - 1 do
local c = constructions[i]
existing[key_xyz(c.pos.x, c.pos.y, c.pos.z)] = c
if is_loaded_construction(c) then
if is_road(c) then
skipped_roads = skipped_roads + 1
else
considered = considered + 1
local token, info = get_matinfo(c.mat_type, c.mat_index)
if token then
add_count(token_counts, token)
else
add_count(unresolved_counts, ("%d:%d"):format(c.mat_type, c.mat_index))
end
add_count(matpair_counts, ("%d:%d"):format(c.mat_type, c.mat_index))
if info then
add_count(mode_counts, info.mode or "<nil>")
else
add_count(mode_counts, "<unresolved>")
end
if token == HARDWOOD_LOG_TOKEN or token == HARDWOOD_LOG_TOKEN_LEGACY then
local allow = true
if policy.is_hamlet then
local hk = key_xyz(c.pos.x, c.pos.y, c.pos.z)
allow = hardwood_keys[hk] == true
end
if allow then
-- Classify by tile shape: floor→stoneware/hardwood, else→earthenware wall.
local block2, lx2, ly2 = get_block_and_local(block_cache, c.pos.x, c.pos.y, c.pos.z)
local shape2 = nil
if block2 then
local attrs2 = df.tiletype.attrs[block2.tiletype[lx2][ly2]]
if attrs2 then shape2 = attrs2.shape end
end
if shape2 == df.tiletype_shape_basic.Floor then
-- Check tile above: open sky above = roof, solid/walled above = interior floor.
local is_roof = false
local block_a, lx_a, ly_a = get_block_and_local(block_cache, c.pos.x, c.pos.y, c.pos.z + 1)
if block_a then
local tt_a = block_a.tiletype[lx_a][ly_a]
local attrs_a = df.tiletype.attrs[tt_a]
if attrs_a and attrs_a.shape == df.tiletype_shape_basic.Open
and block_a.designation[lx_a][ly_a].outside then
is_roof = true
end
else
-- No block loaded above: assume open sky (roof).
is_roof = true
end
if is_roof then
-- Roof → stoneware tile.
if set_construction_material(c, STONEWARE_MAT_TYPE, STONEWARE_MAT_INDEX) then
c.item_type = df.item_type.BLOCKS
changed_hardwood = changed_hardwood + 1
else
already_target = already_target + 1
end
else
-- Ground floor → keep hardwood material, fix item_type.
if c.item_type ~= df.item_type.BLOCKS then
c.item_type = df.item_type.BLOCKS
changed_hardwood = changed_hardwood + 1
else
already_target = already_target + 1
end
end
else
-- Wall, ramp, stair, etc. → earthenware (concrete in desert/tundra biomes).
local wall_mat_type = policy.is_harsh_biome and CONCRETE_MAT_TYPE or EARTHENWARE_MAT_TYPE
local wall_mat_index = policy.is_harsh_biome and CONCRETE_MAT_INDEX or EARTHENWARE_MAT_INDEX
if set_construction_material(c, wall_mat_type, wall_mat_index) then
c.item_type = df.item_type.BLOCKS
changed_hardwood = changed_hardwood + 1
else
already_target = already_target + 1
end
end
else
skipped_hardwood = skipped_hardwood + 1
end
elseif token == PLYWOOD_LOG_TOKEN or token == PLYWOOD_LOG_TOKEN_LEGACY then
-- Plywood buildings: same shape-based logic as hardwood.
-- Roof floor → stoneware, interior floor → keep plywood, walls → earthenware.
local block2, lx2, ly2 = get_block_and_local(block_cache, c.pos.x, c.pos.y, c.pos.z)
local shape2 = nil
if block2 then
local attrs2 = df.tiletype.attrs[block2.tiletype[lx2][ly2]]
if attrs2 then shape2 = attrs2.shape end
end
if shape2 == df.tiletype_shape_basic.Floor then
local is_roof = false
local block_a, lx_a, ly_a = get_block_and_local(block_cache, c.pos.x, c.pos.y, c.pos.z + 1)
if block_a then
local tt_a = block_a.tiletype[lx_a][ly_a]
local attrs_a = df.tiletype.attrs[tt_a]
if attrs_a and attrs_a.shape == df.tiletype_shape_basic.Open
and block_a.designation[lx_a][ly_a].outside then
is_roof = true
end
else
is_roof = true
end
if is_roof then
-- Roof → stoneware tile.
if set_construction_material(c, STONEWARE_MAT_TYPE, STONEWARE_MAT_INDEX) then
c.item_type = df.item_type.BLOCKS
changed_hardwood = changed_hardwood + 1
else
already_target = already_target + 1
end
else
-- Interior floor → keep plywood, fix item_type.
if c.item_type ~= df.item_type.BLOCKS then
c.item_type = df.item_type.BLOCKS
changed_hardwood = changed_hardwood + 1
else
already_target = already_target + 1
end
end
else
-- Wall, ramp, stair, etc. → earthenware (concrete in desert/tundra biomes).
local wall_mat_type = policy.is_harsh_biome and CONCRETE_MAT_TYPE or EARTHENWARE_MAT_TYPE
local wall_mat_index = policy.is_harsh_biome and CONCRETE_MAT_INDEX or EARTHENWARE_MAT_INDEX
if set_construction_material(c, wall_mat_type, wall_mat_index) then
c.item_type = df.item_type.BLOCKS
changed_hardwood = changed_hardwood + 1
else
already_target = already_target + 1
end
end
elseif is_stone_for_concrete(info) or is_builtin_inorganic(token, info)
or c.item_type == df.item_type.BOULDER then
if set_construction_material(c, CONCRETE_MAT_TYPE, CONCRETE_MAT_INDEX) then
changed_stone = changed_stone + 1
else
already_target = already_target + 1
end
elseif not token then
-- unresolvable material: if the tile is a construction-type wall, assume stone → concrete
local block2, lx2, ly2 = get_block_and_local(block_cache, c.pos.x, c.pos.y, c.pos.z)
if block2 then
local tt2 = block2.tiletype[lx2][ly2]
local attrs2 = df.tiletype.attrs[tt2]
if attrs2 and attrs2.material == df.tiletype_material.CONSTRUCTION then
local shape2 = attrs2.shape
if shape2 == df.tiletype_shape_basic.Wall or shape2 == df.tiletype_shape_basic.Fortification then
if set_construction_material(c, CONCRETE_MAT_TYPE, CONCRETE_MAT_INDEX) then
changed_stone = changed_stone + 1
else
already_target = already_target + 1
end
end
end
end
end
end
end
end
-- Pass 2: natural stone wall tiles without construction records.
-- Uses stone_constr_tt_cache (precomputed from STONE_CONSTR_RULES) for O(1) per-tile lookup.
-- Reset block cache on site change or explicit force.
if force or policy.site_id ~= S.last_site_id then
S.processed_blocks = {}
S.last_site_id = policy.site_id
end
for _, block in ipairs(wm.map_blocks) do
local bk = ("%d,%d,%d"):format(block.map_pos.x, block.map_pos.y, block.map_pos.z)
if not S.processed_blocks[bk] then
S.processed_blocks[bk] = true
for lx = 0, 15 do
for ly = 0, 15 do
local tt = block.tiletype[lx][ly]
local cached = stone_constr_tt_cache[tt]
local target_tt = false
if cached and (not cached.inside_only or not block.designation[lx][ly].outside) then
target_tt = cached.target -- nil = record only, number = rewrite tiletype
end
if target_tt ~= false then
local x = block.map_pos.x + lx
local y = block.map_pos.y + ly
local z = block.map_pos.z
fallback_wall_candidates = fallback_wall_candidates + 1
local k = key_xyz(x, y, z)
if not existing[k] then
local c2 = df.construction:new()
c2.pos.x = x; c2.pos.y = y; c2.pos.z = z
c2.mat_type = CONCRETE_MAT_TYPE
c2.mat_index = CONCRETE_MAT_INDEX
c2.original_tile = tt
if dfhack.constructions.insert(c2) then
fallback_wall_inserted = fallback_wall_inserted + 1
existing[k] = c2
end
if target_tt then
block.tiletype[lx][ly] = target_tt
end
elseif target_tt then
-- Record exists but tiletype not yet rewritten.
block.tiletype[lx][ly] = target_tt
end
elseif HARDWOOD_MAT_TYPE then
-- Natural floor tiles inside surface buildings → hardwood wood floor.
local attrs_f = df.tiletype.attrs[tt]
if attrs_f and attrs_f.shape == df.tiletype_shape_basic.Floor then
local mat_f = attrs_f.material
if mat_f ~= df.tiletype_material.CONSTRUCTION
and mat_f ~= df.tiletype_material.GRASS
and mat_f ~= df.tiletype_material.PLANT then
local desig_f = block.designation[lx][ly]
if not desig_f.outside and not desig_f.subterranean then
local x_f = block.map_pos.x + lx
local y_f = block.map_pos.y + ly
local z_f = block.map_pos.z
local k_f = key_xyz(x_f, y_f, z_f)
if not existing[k_f] then
local cf = df.construction:new()
cf.pos.x = x_f; cf.pos.y = y_f; cf.pos.z = z_f
cf.mat_type = HARDWOOD_MAT_TYPE
cf.mat_index = HARDWOOD_MAT_INDEX
cf.original_tile = tt
cf.item_type = df.item_type.BLOCKS
if dfhack.constructions.insert(cf) then
natural_floor_inserted = natural_floor_inserted + 1
existing[k_f] = cf
if CONSTRUCTED_FLOOR_TT then
block.tiletype[lx][ly] = CONSTRUCTED_FLOOR_TT
end
end
end
end
end
end
end
end
end
end -- if not S.processed_blocks[bk]
end
-- Pass 3: Glass fortifications in earthenware buildings; iron fortifications in concrete buildings.
-- Runs every map_key change. Already-processed components are detected by adjacency to
-- existing glass/iron-fortification tiles, so they are skipped without a separate cache.
local function is_wall_name(name)
return name:find("Wall") ~= nil or name:find("Fortif") ~= nil or name:find("Pillar") ~= nil
end
local function is_perimeter_wall(x, y, z)
for _, d in ipairs({{1,0},{-1,0},{0,1},{0,-1}}) do
local nb, nlx, nly = get_block_and_local(block_cache, x+d[1], y+d[2], z)
if not nb then return true end
local tt_name = tostring(df.tiletype[nb.tiletype[nlx][nly]] or "")
if not is_wall_name(tt_name) then return true end
end
return false
end
local function build_wall_components(mat_type, mat_index)
local wall_pos = {}
for k, c in pairs(existing) do
if c.mat_type == mat_type and c.mat_index == mat_index then
local b, lx, ly = get_block_and_local(block_cache, c.pos.x, c.pos.y, c.pos.z)
if b then
local tt_name = tostring(df.tiletype[b.tiletype[lx][ly]] or "")
if tt_name:find("^ConstructedWall") then
wall_pos[k] = c
end
end
end
end
local comps, visited = {}, {}
for k, c in pairs(wall_pos) do
if not visited[k] then
local comp, stack = {}, {c}
visited[k] = true
while #stack > 0 do
local cur = stack[#stack]; stack[#stack] = nil
comp[#comp+1] = cur
for _, d in ipairs({{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}}) do
local nk = key_xyz(cur.pos.x+d[1], cur.pos.y+d[2], cur.pos.z+d[3])
if wall_pos[nk] and not visited[nk] then
visited[nk] = true
stack[#stack+1] = wall_pos[nk]
end
end
end
comps[#comps+1] = comp
end
end
return comps
end
-- Returns true if any tile in comp has a 6-directional neighbor in key_set.
local function comp_has_neighbor_in(comp, key_set)
for _, c in ipairs(comp) do
for _, d in ipairs({{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}}) do
if key_set[key_xyz(c.pos.x+d[1], c.pos.y+d[2], c.pos.z+d[3])] then
return true
end
end
end
return false
end
local function is_door_construction(c)
if c.item_type == df.item_type.DOOR then
return true
end
local b, lx, ly = get_block_and_local(block_cache, c.pos.x, c.pos.y, c.pos.z)
if not b then return false end
local tn = tostring(df.tiletype[b.tiletype[lx][ly]] or "")
return tn:find("Door") ~= nil
end
-- Bridges are typically outside and doorless; require at least one
-- building-like signal before placing iron fortifications.
local function comp_looks_like_building(comp)
for _, c in ipairs(comp) do
for _, d in ipairs({{1,0,0},{-1,0,0},{0,1,0},{0,-1,0}}) do
local nx, ny, nz = c.pos.x + d[1], c.pos.y + d[2], c.pos.z + d[3]
local b, lx, ly = get_block_and_local(block_cache, nx, ny, nz)
if b and not b.designation[lx][ly].outside then
return true
end
local nadj = dfhack.buildings.findAtTile(nx, ny, nz)
if nadj and nadj:getType() == df.building_type.Door then
return true
end
end
end
return false
end
-- Some bridge constructions are represented with tile names containing
-- "bridge". Skip these in the iron-fortification window pass.
local function comp_is_bridge_named(comp)
for _, c in ipairs(comp) do
local b, lx, ly = get_block_and_local(block_cache, c.pos.x, c.pos.y, c.pos.z)
if b then
local tn = tostring(df.tiletype[b.tiletype[lx][ly]] or ""):lower()
if tn:find("bridge") then
return true
end
end
end
return false
end
-- ── PASS 3: GEOMETRY-BASED WINDOW PLACEMENT ────────────────────────────────
-- True if wall at (x,y,z) faces at least one outside-designated non-wall tile.
local function is_outer_wall(x, y, z)
for _, d in ipairs({{1,0},{-1,0},{0,1},{0,-1}}) do
local nb, nlx, nly = get_block_and_local(block_cache, x+d[1], y+d[2], z)
if not nb then return true end
if not is_wall_name(tostring(df.tiletype[nb.tiletype[nlx][nly]] or "")) then
if nb.designation[nlx][nly].outside then return true end
end
end
return false
end
-- True if tile below (x,y,z) is not Open (i.e. at ground level).