-
Notifications
You must be signed in to change notification settings - Fork 509
Expand file tree
/
Copy pathtiletypes.lua
More file actions
118 lines (105 loc) · 3.71 KB
/
Copy pathtiletypes.lua
File metadata and controls
118 lines (105 loc) · 3.71 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
config.mode = 'fortress'
config.target = 'tiletypes'
local function set_cursor(pos)
df.global.cursor.x = pos.x
df.global.cursor.y = pos.y
df.global.cursor.z = pos.z
end
local function reset_cursor()
df.global.cursor.x = -30000
df.global.cursor.y = -30000
df.global.cursor.z = -30000
end
-- options is a list of "<option> <value>" paint spec strings; each is emitted
-- as its own paint command since a paint command consumes a single
-- option/value pair
local function paint_at(pos, options)
set_cursor(pos)
local parts = {'tiletypes-command point', 'paint any'}
for _, opt in ipairs(options) do
table.insert(parts, 'paint ' .. opt)
end
table.insert(parts, 'run')
return dfhack.run_command_silent(table.concat(parts, ' ; '))
end
local function tiletype_spec(tt)
local attrs = df.tiletype.attrs[tt]
return {
'shape ' .. (df.tiletype_shape[attrs.shape] or 'ANY'),
'material ' .. (df.tiletype_material[attrs.material] or 'ANY'),
'special ' .. (df.tiletype_special[attrs.special] or 'ANY'),
'variant ' .. (df.tiletype_variant[attrs.variant] or 'ANY'),
}
end
local WALL_SPEC = {'shape WALL', 'material SOIL', 'special NORMAL'}
local function find_plant(want_tree)
for _, plant in ipairs(df.global.world.plants.all) do
if want_tree == (plant.tree_info ~= nil) and
dfhack.maps.getTileType(plant.pos) then
return plant
end
end
end
local function check_plant_survival(pos, expect_removed)
local plant = dfhack.maps.getPlantAtTile(pos)
if expect_removed then
expect.nil_(plant)
else
expect.ne(nil, plant)
end
end
function test.painting_away_tile_removes_plant()
local plant = find_plant(false)
if not plant then qerror('no shrub or sapling found on the map') end
local pos = xyz2pos(plant.pos.x, plant.pos.y, plant.pos.z)
local orig_tt = dfhack.maps.getTileType(pos)
dfhack.with_finalize(function()
paint_at(pos, tiletype_spec(orig_tt))
reset_cursor()
end, function()
check_plant_survival(pos, false)
local _, status = paint_at(pos, WALL_SPEC)
expect.eq(CR_OK, status)
check_plant_survival(pos, true)
end)
end
function test.painting_compatible_tile_keeps_plant()
local plant = find_plant(false)
if not plant then qerror('no shrub or sapling found on the map') end
local pos = xyz2pos(plant.pos.x, plant.pos.y, plant.pos.z)
dfhack.with_finalize(function()
reset_cursor()
end, function()
local orig_tt = dfhack.maps.getTileType(pos)
local _, status = paint_at(pos, tiletype_spec(orig_tt))
expect.eq(CR_OK, status)
check_plant_survival(pos, false)
end)
end
function test.painting_trunk_removes_tree()
local plant = find_plant(true)
if not plant then qerror('no grown tree found on the map') end
local pos = xyz2pos(plant.pos.x, plant.pos.y, plant.pos.z)
local orig_tt = dfhack.maps.getTileType(pos)
dfhack.with_finalize(function()
paint_at(pos, tiletype_spec(orig_tt))
reset_cursor()
end, function()
check_plant_survival(pos, false)
local _, status = paint_at(pos, WALL_SPEC)
expect.eq(CR_OK, status)
check_plant_survival(pos, true)
end)
end
function test.tiletypes_needs_console()
local _, status = dfhack.run_command_silent('tiletypes')
expect.eq(CR_NEEDS_CONSOLE, status)
end
function test.command_help_runs()
local _, status = dfhack.run_command_silent('tiletypes-command help')
expect.eq(CR_OK, status)
end
function test.here_point_bad_option_is_failure()
local _, status = dfhack.run_command_silent('tiletypes-here-point --bogus')
expect.eq(CR_FAILURE, status)
end