Skip to content

Commit 3606888

Browse files
committed
Add copybuilding script
1 parent f71c156 commit 3606888

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ that repo.
1313
Template for new versions:
1414

1515
## New Tools
16+
- `copybuilding`: Accompanying script for ``buildingplan.copybuilding`` that allows copying buildings/constructions/stockpiles under the cursor or from history. Intended to be used with keybindings.
1617

1718
## New Features
1819

copybuilding.lua

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
--@ module = true
2+
3+
local help = [====[
4+
5+
copybuilding
6+
============
7+
8+
Copy a building and start building placement mode for the same building type.
9+
10+
Usage:
11+
12+
copybuilding
13+
Copy building under cursor
14+
copybuilding 1..9
15+
Copy the last n building you copied
16+
17+
Examples:
18+
19+
copybuilding
20+
Copy the building under the cursor and start placing the same type
21+
copybuilding 1
22+
Re-copy the last building type you copied
23+
24+
Note:
25+
26+
- Calling this script is intended to be done via keybindings. e.g. `keybinding add Alt-1 "copybuilding 1"`
27+
- History is not persisted between sessions
28+
29+
]====]
30+
31+
local argparse = require('argparse')
32+
33+
local copybuilding = require('plugins.buildingplan.copybuilding')
34+
35+
local function print_help()
36+
print(help)
37+
end
38+
39+
local function process_args(opts, args)
40+
if args[1] == 'help' then
41+
opts.help = true
42+
return {}
43+
end
44+
45+
local positionals = argparse.processArgsGetopt(args, {
46+
{'h', 'help', handler=function() opts.help = true end},
47+
})
48+
49+
return positionals
50+
end
51+
52+
local function main(...)
53+
local args, opts = {...}, {}
54+
local positionals = process_args(opts, args)
55+
56+
if opts.help then
57+
print_help()
58+
return
59+
end
60+
61+
local history_index = tonumber(positionals[1])
62+
63+
if history_index then
64+
if history_index < 1 or history_index > copybuilding.MAX_HISTORY then
65+
qerror('History index must be 1-' .. copybuilding.MAX_HISTORY)
66+
end
67+
68+
local success, message = copybuilding.copy_building_from_history(history_index)
69+
if message then
70+
if success then
71+
print('Copied ' .. message)
72+
else
73+
qerror(message)
74+
end
75+
end
76+
else
77+
local success, message = copybuilding.copy_building_at_cursor()
78+
if message then
79+
if success then
80+
print('Copied ' .. message .. ' and starting construction mode...')
81+
else
82+
qerror(message)
83+
end
84+
end
85+
end
86+
end
87+
88+
if dfhack_flags.module then
89+
return
90+
end
91+
92+
main(...)

0 commit comments

Comments
 (0)