-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexport-map.lua
More file actions
286 lines (255 loc) · 9.38 KB
/
export-map.lua
File metadata and controls
286 lines (255 loc) · 9.38 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
-- Export diggable parts of map elevations to text files
--[====[
export-diggable-areas
=============
Export map elevations to text files, with diggable areas marked as
'1', and other areas marked as '0'.
]====]
local tm = require('tile-material')
local utils = require('utils')
local args = {...}
local mode = nil
local spoilers = false
local debug = false
if args[1] == 'spoilers' then
spoilers = true
end
if args[2] == 'debug' then
debug = true
end
local function dump(o)
-- https://stackoverflow.com/a/27028488
if type(o) == 'table' then
local s = '{ '
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. '['..k..'] = ' .. dump(v) .. ', '
end
return s .. '} '
else
return tostring(o)
end
end
-- from quickfort
local hard_natural_materials = utils.invert({
df.tiletype_material.STONE,
df.tiletype_material.FEATURE,
df.tiletype_material.LAVA_STONE,
df.tiletype_material.MINERAL,
df.tiletype_material.FROZEN_LIQUID,
})
local function is_boulder(tileattrs)
return tileattrs.shape == df.tiletype_shape.BOULDER
end
local function is_tree(tileattrs)
-- from quickfort
return tileattrs.material == df.tiletype_material.TREE
end
local function is_wall(tileattrs)
-- from quickfort
return tileattrs.shape == df.tiletype_shape.WALL
end
local function is_diggable_wall(tileattrs)
if is_wall(tileattrs) then
if not is_tree(tileattrs) then
return true
end
end
return false
end
local function is_hard(tileattrs)
-- from quickfort
return hard_natural_materials[tileattrs.material]
end
local function is_visible(x, y, z)
return dfhack.maps.isTileVisible(x, y, z)
end
local function is_magma(tileflags)
return tileflags.liquid_type and (tileflags.flow_size > 0)
end
local function is_water(tileattrs)
return (tileattrs.material == df.tiletype_material.RIVER) or (tileattrs.material == df.tiletype_material.BROOK) or (tileattrs.material == df.tiletype_material.POOL) or (tileattrs.material == df.tiletype_material.FROZEN_LIQUID)
end
local function is_grass(tileattrs)
return (tileattrs.material == df.tiletype_material.GRASS_LIGHT) or (tileattrs.material == df.tiletype_material.GRASS_DARK) or (tileattrs.material == df.tiletype_material.GRASS_DRY) or (tileattrs.material == df.tiletype_material.GRASS_DEAD)
end
local function is_plant(tileattrs)
return (tileattrs.material == df.tiletype_material.PLANT)
end
local function is_soil_floor_or_ramp(tileattrs)
return ((tileattrs.shape == df.tiletype_shape.FLOOR) or (tileattrs.shape == df.tiletype_shape.RAMP)) and (tileattrs.material == df.tiletype_material.SOIL)
end
local function is_stone_floor_or_ramp(tileattrs)
return ((tileattrs.shape == df.tiletype_shape.FLOOR) or (tileattrs.shape == df.tiletype_shape.RAMP)) and (tileattrs.material == df.tiletype_material.STONE)
end
local function is_pebbles(tileattrs)
return (tileattrs.shape == df.tiletype_shape.PEBBLES)
end
local function z_to_elevation(z)
return z+df.global.world.map.region_z-100
end
local function elevation_to_z(elevation)
return elevation-(df.global.world.map.region_z-100)
end
local function classify_tile(x, y, z, spoilers)
-- print(x, y, z, dfhack.maps.getTileType(x, y, z))
-- if z == 3 then
-- print('At (x, y, z)=(' .. x .. ',' .. y .. ',' .. z .. '), getTileType(x, y, z) = ' .. dfhack.maps.getTileType(x, y, z))
-- end
local tileattrs = nil
if dfhack.maps.getTileType(x, y, z) == nil then
return '!'
else
tileattrs = df.tiletype.attrs[dfhack.maps.getTileType(x, y, z)]
tileflags = dfhack.maps.getTileFlags(x, y, z)
end
if not is_visible(x, y, z) and not spoilers then
return '?'
elseif is_magma(tileflags) then
return 'M'
elseif is_water(tileattrs) then
return '~'
elseif is_grass(tileattrs) then
return 'g'
elseif is_plant(tileattrs) then
return 'p'
elseif (is_diggable_wall(tileattrs) and not is_hard(tileattrs)) or is_soil_floor_or_ramp(tileattrs) then
return 's'
elseif is_tree(tileattrs) then
return 'T'
elseif is_boulder(tileattrs) then
return 'B'
elseif (is_diggable_wall(tileattrs) and is_hard(tileattrs)) or is_stone_floor_or_ramp(tileattrs) or is_pebbles(tileattrs) then
return 'r'
else
return ' '
end
end
local function export_one_z_level(folder, z, vis_check, spoilers)
local spoilers_str = 'false'
local vis_check_str = 'false'
if spoilers == true then
spoilers_str = 'true'
end
if vis_check == true then
local vis_check_str = 'true'
end
-- print ('spoilers = ' .. spoilers_str .. ', vis_check = ' .. vis_check_str)
if spoilers or vis_check then
print('Exporting z-level ' .. z .. ' (elevation '.. z_to_elevation(z) .. ')')
local xmax, ymax, _ = dfhack.maps.getTileSize()
local elevation = z_to_elevation(z)
local fortress_name = dfhack.translation.translateName(df.global.world.world_data.active_site[0].name)
local filename = string.format("%s/%s-%+04d.txt", folder, dfhack.capitalizeStringWords(dfhack.toSearchNormalized(fortress_name)), elevation)
local f = assert(io.open(filename, 'w'))
for y=0, ymax-1 do
local row_string = ''
for x=0, xmax-1 do
local classification = classify_tile(x, y, z, spoilers)
row_string = row_string .. classification
end
f:write(row_string..'\n')
end
f:close()
end
end
local function find_ground_layers()
xmax, ymax, zmax = dfhack.maps.getTileSize()
-- Keep track of elevations with diggable tiles
local ground_layers = {}
for z=zmax-1, 0, -1 do
-- Go through each elevation looking for diggable wall tiles
ground_layers[z] = false
for y=0, ymax-1 do
if ground_layers[z] == false then
for x=0, xmax-1 do
-- j, i since (y, x) matches (row, col)
local tiletype = dfhack.maps.getTileType(x, y, z)
if tiletype ~= nil then
-- status, err = pcall(function() tileattrs = df.tiletype.attrs[dfhack.maps.getTileType(x, y, z)] end)
-- if status then
tileattrs = df.tiletype.attrs[dfhack.maps.getTileType(x, y, z)]
if is_diggable_wall(tileattrs) then
if ground_layers[z] == false then
ground_layers[z] = true
break
end
end
-- else
-- print('At (x,y,z)=('..x..','..y..','..z..'), tiletype was nil')
end
end
else
break
end
end
if debug then
if ground_layers[z] then
print('Checked z-level '..z..', (elevation '..z_to_elevation(z)..'), ground_layers[z]=true')
else
print('Checked z-level '..z..', (elevation '..z_to_elevation(z)..'), ground_layers[z]=false')
end
end
end
return ground_layers
end
local function find_visible_layers()
xmax, ymax, zmax = dfhack.maps.getTileSize()
-- Keep track of elevations with visible tiles
local visible_layers = {}
for z=zmax-1, 0, -1 do
-- Go through each elevation looking for vislble tiles
visible_layers[z] = false
for y=0, ymax-1 do
if visible_layers[z] == false then
for x=0, xmax-1 do
if is_visible(x, y, z) then
visible_layers[z] = true
break
end
end
else
break
end
end
if debug then
if visible_layers[z] then
print('Checked z-level '..z..', (elevation '..z_to_elevation(z)..'), visible_layers[z]=true')
else
print('Checked z-level '..z..', (elevation '..z_to_elevation(z)..'), visible_layers[z]=false')
end
end
end
return visible_layers
end
local function export_map_elevations()
_, _, zmax = dfhack.maps.getTileSize()
if debug then
print('find_ground_layers()')
end
local ground_layers = find_ground_layers()
-- if debug then
-- print('ground_layers = ')
-- print(dump(ground_layers))
-- end
if debug then
print('find_visible_layers()')
end
local visible_layers = find_visible_layers()
if spoilers then
print('Finding elevations with diggable areas (including fully-subterranean levels):')
else
print('Finding elevations with both visible and diggable areas:')
end
local df_path = dfhack.getDFPath()
local fortress_name = dfhack.translation.translateName(df.global.world.world_data.active_site[0].name)
local export_path = "map-exports/" .. dfhack.capitalizeStringWords(dfhack.toSearchNormalized(fortress_name))
dfhack.filesystem.mkdir_recursive(export_path)
for z=zmax-1, 0, -1 do
if ground_layers[z] or ground_layers[z-1] then
export_one_z_level(export_path, z, visible_layers[z], spoilers)
end
end
print("Map export files can be found in Dwarf Fortress folder under "..export_path)
end
export_map_elevations(...)