-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtilecheck.lua
More file actions
390 lines (348 loc) · 14.8 KB
/
tilecheck.lua
File metadata and controls
390 lines (348 loc) · 14.8 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
-- tilecheck.lua - diagnostics for roads and tiletypes
local args = {...}
local function print_cursor_tile_info()
if not dfhack.isMapLoaded() then
print("[tilecheck] No map loaded.")
return
end
local p = df.global.cursor
if not p or p.x == -30000 then
print("[tilecheck] Put the in-game cursor on a tile first, then run: tilecheck cursor")
return
end
local block = dfhack.maps.getTileBlock(p)
if not block then
print(("[tilecheck] No map block at cursor (%d,%d,%d)"):format(p.x, p.y, p.z))
return
end
local lx, ly = p.x % 16, p.y % 16
local tt = block.tiletype[lx][ly]
local attrs = df.tiletype.attrs[tt]
local name = df.tiletype[tt] or "nil"
local shape = attrs and df.tiletype_shape_basic[attrs.shape] or "?"
local mat = attrs and df.tiletype_material[attrs.material] or "?"
local occ = block.occupancy[lx][ly]
local des = block.designation[lx][ly]
print(("[tilecheck] cursor=(%d,%d,%d) tiletype=%d name=%s shape=%s mat=%s outside=%s building=%s"):format(
p.x, p.y, p.z, tt, tostring(name), tostring(shape), tostring(mat),
tostring(des and des.outside or false), tostring(occ and occ.building or false)))
end
local function scan_nearby_walls(radius)
if not dfhack.isMapLoaded() then
print("[tilecheck] No map loaded.")
return
end
local p = df.global.cursor
if not p or p.x == -30000 then
print("[tilecheck] Put the in-game cursor near the area you want to scan, then run: tilecheck near")
return
end
local r = tonumber(radius) or 20
local floor = df.tiletype_shape_basic.Floor
local wall = df.tiletype_shape_basic.Wall
local constr = df.tiletype_material.CONSTRUCTION
local seen = {}
local total = 0
print(("[tilecheck] scanning radius=%d around (%d,%d,%d)"):format(r, p.x, p.y, p.z))
for y = p.y - r, p.y + r do
for x = p.x - r, p.x + r do
local pos = {x = x, y = y, z = p.z}
local block = dfhack.maps.getTileBlock(pos)
if block then
local lx, ly = x % 16, y % 16
local tt = block.tiletype[lx][ly]
local a = df.tiletype.attrs[tt]
if a then
local is_wall = (a.shape == wall)
local is_constr = (a.material == constr)
if is_wall or is_constr then
total = total + 1
if not seen[tt] then
seen[tt] = true
local shape = df.tiletype_shape_basic[a.shape] or "?"
local mat = df.tiletype_material[a.material] or "?"
local name = df.tiletype[tt] or "nil"
local occ = block.occupancy[lx][ly]
local des = block.designation[lx][ly]
print((" tt=%d name=%s shape=%s mat=%s outside=%s building=%s"):format(
tt, tostring(name), tostring(shape), tostring(mat),
tostring(des and des.outside or false), tostring(occ and occ.building or false)))
end
elseif a.shape == floor and a.material == constr then
-- Also useful reference: constructed floors nearby
if not seen[tt] then
seen[tt] = true
local shape = df.tiletype_shape_basic[a.shape] or "?"
local mat = df.tiletype_material[a.material] or "?"
local name = df.tiletype[tt] or "nil"
print((" tt=%d name=%s shape=%s mat=%s"):format(
tt, tostring(name), tostring(shape), tostring(mat)))
end
end
end
end
end
end
print(("[tilecheck] scan complete; matching tiles visited=%d unique tiletypes listed above"):format(total))
end
local function scan_at_position(cx, cy, cz, radius, tag)
local r = tonumber(radius) or 20
local floor = df.tiletype_shape_basic.Floor
local wall = df.tiletype_shape_basic.Wall
local constr = df.tiletype_material.CONSTRUCTION
local seen = {}
local total = 0
print(("[tilecheck] %s radius=%d around (%d,%d,%d)"):format(tag, r, cx, cy, cz))
for y = cy - r, cy + r do
for x = cx - r, cx + r do
local pos = {x = x, y = y, z = cz}
local block = dfhack.maps.getTileBlock(pos)
if block then
local lx, ly = x % 16, y % 16
local tt = block.tiletype[lx][ly]
local a = df.tiletype.attrs[tt]
if a then
local is_wall = (a.shape == wall)
local is_constr = (a.material == constr)
if is_wall or is_constr or (a.shape == floor and a.material == constr) then
total = total + 1
if not seen[tt] then
seen[tt] = true
local shape = df.tiletype_shape_basic[a.shape] or "?"
local mat = df.tiletype_material[a.material] or "?"
local name = df.tiletype[tt] or "nil"
local occ = block.occupancy[lx][ly]
local des = block.designation[lx][ly]
print((" tt=%d name=%s shape=%s mat=%s outside=%s building=%s"):format(
tt, tostring(name), tostring(shape), tostring(mat),
tostring(des and des.outside or false), tostring(occ and occ.building or false)))
end
end
end
end
end
end
print(("[tilecheck] %s complete; matching tiles visited=%d"):format(tag, total))
end
local function scan_adventurer_near(radius)
if not dfhack.isMapLoaded() then
print("[tilecheck] No map loaded.")
return
end
local adv = dfhack.world.getAdventurer()
if not adv or not adv.pos then
print("[tilecheck] No active adventurer found.")
return
end
scan_at_position(adv.pos.x, adv.pos.y, adv.pos.z, radius, "advnear")
end
local function build_surface_z_map(map_blocks)
local OPEN_SPACE = 32
local surface_z = {}
for _, block in ipairs(map_blocks) do
local bz = block.map_pos.z
for lx = 0, 15 do
for ly = 0, 15 do
local tt = block.tiletype[lx][ly]
if tt ~= OPEN_SPACE then
local x = block.map_pos.x + lx
local y = block.map_pos.y + ly
local k = ("%d,%d"):format(x, y)
local cur = surface_z[k]
if not cur or bz > cur then
surface_z[k] = bz
end
end
end
end
end
return surface_z
end
local function scan_map_center_near(radius)
if not dfhack.isMapLoaded() then
print("[tilecheck] No map loaded.")
return
end
local r = tonumber(radius) or 20
local wm = df.global.world.map
local cx = math.floor(wm.x_count / 2)
local cy = math.floor(wm.y_count / 2)
local surface_z = build_surface_z_map(wm.map_blocks)
local floor = df.tiletype_shape_basic.Floor
local wall = df.tiletype_shape_basic.Wall
local constr = df.tiletype_material.CONSTRUCTION
local seen = {}
local total = 0
print(("[tilecheck] mapnear radius=%d center=(%d,%d)"):format(r, cx, cy))
for y = cy - r, cy + r do
for x = cx - r, cx + r do
local z = surface_z[("%d,%d"):format(x, y)]
if z then
local pos = {x = x, y = y, z = z}
local block = dfhack.maps.getTileBlock(pos)
if block then
local lx, ly = x % 16, y % 16
local tt = block.tiletype[lx][ly]
local a = df.tiletype.attrs[tt]
if a then
local is_wall = (a.shape == wall)
local is_constr = (a.material == constr)
if is_wall or is_constr or (a.shape == floor and a.material == constr) then
total = total + 1
if not seen[tt] then
seen[tt] = true
local shape = df.tiletype_shape_basic[a.shape] or "?"
local mat = df.tiletype_material[a.material] or "?"
local name = df.tiletype[tt] or "nil"
local occ = block.occupancy[lx][ly]
local des = block.designation[lx][ly]
print((" tt=%d name=%s shape=%s mat=%s outside=%s building=%s z=%d"):format(
tt, tostring(name), tostring(shape), tostring(mat),
tostring(des and des.outside or false), tostring(occ and occ.building or false), z))
end
end
end
end
end
end
end
print(("[tilecheck] mapnear complete; matching tiles visited=%d"):format(total))
end
local function scan_wall_enum()
local wall = df.tiletype_shape_basic.Wall
local constr = df.tiletype_material.CONSTRUCTION
local count = 0
print("[tilecheck] wallenum: scanning tiletype attrs for CONSTRUCTION+Wall")
for i = 0, 65535 do
local a = df.tiletype.attrs[i]
if a and a.shape == wall and a.material == constr then
local name = df.tiletype[i] or "nil"
print((" tt=%d name=%s shape=%s mat=%s"):format(
i, tostring(name), tostring(df.tiletype_shape_basic[a.shape]), tostring(df.tiletype_material[a.material])))
count = count + 1
end
end
print(("[tilecheck] wallenum complete; matches=%d"):format(count))
end
local function scan_wall_names()
local count = 0
print("[tilecheck] wallnames: listing tiletypes named ConstructedWall*")
for i = 0, 65535 do
local name = df.tiletype[i]
if name and tostring(name):find("^ConstructedWall") then
local a = df.tiletype.attrs[i]
local shape = a and df.tiletype_shape_basic[a.shape] or "?"
local mat = a and df.tiletype_material[a.material] or "?"
print((" tt=%d name=%s shape=%s mat=%s"):format(i, tostring(name), tostring(shape), tostring(mat)))
count = count + 1
end
end
print(("[tilecheck] wallnames complete; matches=%d"):format(count))
end
if args[1] == "cursor" then
print_cursor_tile_info()
return
end
if args[1] == "near" then
scan_nearby_walls(args[2])
return
end
if args[1] == "advnear" then
scan_adventurer_near(args[2])
return
end
if args[1] == "mapnear" then
scan_map_center_near(args[2])
return
end
if args[1] == "wallenum" then
scan_wall_enum()
return
end
if args[1] == "wallnames" then
scan_wall_names()
return
end
local cons = df.global.world.world_data.constructions
local wm = df.global.world.map
local rx, ry = wm.region_x, wm.region_y
local ok_t0, t0 = pcall(function() return df.world_construction_type[0] end)
print("world_construction_type[0]:", ok_t0 and t0 or "ERROR")
print(("region_x=%d region_y=%d"):format(rx, ry))
-- Diagnostic: map block coordinate system and map size
local b0 = df.global.world.map.map_blocks[0]
print(("map_blocks[0].map_pos = (%d, %d, %d)"):format(b0.map_pos.x, b0.map_pos.y, b0.map_pos.z))
print(("x_count=%d y_count=%d"):format(wm.x_count, wm.y_count))
local wx1 = math.floor(rx / 16)
local wx2 = math.floor((rx + 47) / 16)
local wy1 = math.floor(ry / 16)
local wy2 = math.floor((ry + 47) / 16)
print(("Loaded world tiles: x[%d-%d] y[%d-%d]"):format(wx1, wx2, wy1, wy2))
-- Diagnostic: sample tiletypes at the first few Formula-A road positions
local sample_coords = {}
local function maybe_add_sample(lx, ly)
if lx >= 0 and ly >= 0 and #sample_coords < 8 then
sample_coords[#sample_coords + 1] = {lx, ly}
end
end
print("--- Tiletype samples at Formula-A road positions ---")
-- (filled below after we scan constructions; printed at end)
for ci = 0, #cons.list - 1 do
local c = cons.list[ci]
if c:getType() == 0 then -- Road
local sp = c.square_pos
for i = 0, #sp.x - 1 do
local sx, sy = sp.x[i], sp.y[i]
if sx >= wx1 and sx <= wx2 and sy >= wy1 and sy <= wy2 then
local lx_wt = (sx * 16 - rx) * 3
local ly_wt = (sy * 16 - ry) * 3
print(("Road cons[%d] sq[%d] world(%d,%d) wt_local(%d,%d)"):format(
ci, i, sx, sy, lx_wt, ly_wt))
pcall(function()
local sq = c.square_obj[i]
local ex, ey = sq.embark_x, sq.embark_y
for j = 0, #ex - 1 do
local exj, eyj = ex[j], ey[j]
-- hypothesis A: embark tile offset within world tile (0-15)
local lx_A = lx_wt + exj * 3
local ly_A = ly_wt + eyj * 3
-- hypothesis B: global sub-tile coord
local lx_B = (exj - rx) * 3
local ly_B = (eyj - ry) * 3
print((" [%d] ex=%d ey=%d | A:local(%d,%d) | B:local(%d,%d)"):format(
j, exj, eyj, lx_A, ly_A, lx_B, ly_B))
maybe_add_sample(lx_A, ly_A)
end
end)
end
end
end
end
-- Print tiletype at each sampled road position.
-- Find the highest z-level that is NOT OpenSpace (32) = the actual surface ground.
local OPEN_SPACE = 32
for _, coord in ipairs(sample_coords) do
local sx, sy = coord[1], coord[2]
local bx, by = math.floor(sx / 16), math.floor(sy / 16)
local lx, ly = sx % 16, sy % 16
local best_block, best_z = nil, -999
for _, bl in ipairs(df.global.world.map.map_blocks) do
if bl.map_pos.x == bx * 16 and bl.map_pos.y == by * 16 then
local tt = bl.tiletype[lx][ly]
if tt ~= OPEN_SPACE and bl.map_pos.z > best_z then
best_block = bl
best_z = bl.map_pos.z
end
end
end
if best_block then
local tt = best_block.tiletype[lx][ly]
local name = df.tiletype[tt] or "nil"
local shape = df.tiletype.attrs[tt] and df.tiletype_shape_basic[df.tiletype.attrs[tt].shape] or "?"
print((" sample (%d,%d) z=%d tiletype=%d [%s] shape=%s"):format(
sx, sy, best_z, tt, name, tostring(shape)))
else
print((" sample (%d,%d) -> no non-OpenSpace block found at block_xy=(%d,%d)"):format(
sx, sy, bx * 16, by * 16))
end
end