forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaquifer.lua
More file actions
154 lines (126 loc) · 4.47 KB
/
aquifer.lua
File metadata and controls
154 lines (126 loc) · 4.47 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
local _ENV = mkmodule('plugins.aquifer')
local argparse = require('argparse')
local guidm = require('gui.dwarfmode')
local utils = require('utils')
local actions = utils.invert{'list', 'add', 'drain', 'convert'}
local aq_types = utils.invert{'light', 'heavy'}
local function parse_positionals(positionals)
local action, aq_type, pos1, pos2
local idx = 1
if actions[positionals[idx]] then
action = positionals[idx]
idx = idx + 1
else
action = 'list'
end
if aq_types[positionals[idx]] then
aq_type = positionals[idx]
idx = idx + 1
end
if positionals[idx] then
pos1 = argparse.coords(positionals[idx])
idx = idx + 1
end
if positionals[idx] then
pos2 = argparse.coords(positionals[idx])
idx = idx + 1
end
return action, aq_type, pos1, pos2
end
local function get_bounds(opts, def_all)
local map = df.global.world.map
local curz = df.global.window_z
local origin = xyz2pos(0, 0, 0)
local max = xyz2pos(map.x_count-1, map.y_count-1, map.z_count-1)
if opts.all then
return origin, max
elseif opts.zdown then
return origin, xyz2pos(max.x, max.y, curz)
elseif opts.zup then
return xyz2pos(origin.x, origin.y, curz), max
elseif opts.curz then
return xyz2pos(origin.x, origin.y, curz), xyz2pos(max.x, max.y, curz)
end
if def_all then
return origin, max
end
qerror('Please specify a target range')
end
function parse_commandline(args)
local opts = {
all=false,
zdown=false,
zup=false,
curz=false,
help=false,
skip_top=0,
levels=nil,
leaky=false,
quiet=false,
}
local positionals = argparse.processArgsGetopt(args, {
{'a', 'all', handler=function() opts.all = true end},
{'d', 'zdown', handler=function() opts.zdown = true end},
{'h', 'help', handler=function() opts.help = true end},
{'l', 'levels', hasArg=true, handler=function(optarg) opts.levels = argparse.positiveInt(optarg, 'levels') end},
{nil, 'leaky', handler=function() opts.leaky = true end},
{'q', 'quiet', handler=function() opts.quiet = true end},
{'s', 'skip-top', hasArg=true, handler=function(optarg) opts.skip_top = argparse.nonnegativeInt(optarg, 'skip-top') end},
{'u', 'zup', handler=function() opts.zup = true end},
{'z', 'cur-zlevel', handler=function() opts.curz = true end},
})
if help or positionals[1] == 'help' then
print(dfhack.script_help())
return false
end
local action, aq_type, pos1, pos2 = parse_positionals(positionals)
if pos1 and not pos2 then
local cursor = guidm.getCursorPos()
if cursor then
pos2 = cursor
else
pos2 = pos1
end
opts.all, opts.zdown, opts.zup, opts.curz = false, false, false, false
end
if not pos1 then
pos1, pos2 = get_bounds(opts, action == 'list')
end
if not pos1 then
qerror('must specify a target range (--all, --zdown, --zup, --cur-zlevel, or coordinates)')
end
pos1.x, pos2.x = math.min(pos1.x, pos2.x), math.max(pos1.x, pos2.x)
pos1.y, pos2.y = math.min(pos1.y, pos2.y), math.max(pos1.y, pos2.y)
pos1.z, pos2.z = math.min(pos1.z, pos2.z), math.max(pos1.z, pos2.z)
if not opts.levels then
opts.levels = pos2.z - pos1.z + 1
elseif opts.zdown then
pos1.z = math.max(pos1.z, pos2.z - opts.levels + 1)
opts.levels = pos2.z - pos1.z + 1
elseif opts.zup then
pos2.z = math.min(pos2.z, pos1.z + opts.levels - 1)
opts.levels = pos2.z - pos1.z + 1
end
if action == 'list' then
aquifer_list(pos1, pos2, opts.levels, opts.leaky)
return true
end
if action ~= 'drain' and not aq_type then
qerror('must specify an aquifer type (heavy or light)')
end
local modified = 0
if action == 'drain' then
modified = aquifer_drain(aq_type or 'all', pos1, pos2, opts.skip_top, opts.levels, opts.leaky)
elseif action == 'convert' then
modified = aquifer_convert(aq_type, pos1, pos2, opts.skip_top, opts.levels, opts.leaky)
elseif action == 'add' then
modified = aquifer_add(aq_type, pos1, pos2, opts.skip_top, opts.levels, opts.leaky)
else
qerror(('invalid action: %s'):format(action))
end
if not opts.quiet then
print(('%d tile(s) modified'):format(modified))
end
return true
end
return _ENV