Skip to content

Commit 6841ec3

Browse files
committed
Add copybuilding script
1 parent f71c156 commit 6841ec3

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-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: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
-- Import the copybuilding module from buildingplan
34+
local copybuilding = require('plugins.buildingplan.copybuilding')
35+
36+
local function print_help()
37+
print(help)
38+
end
39+
40+
local function process_args(opts, args)
41+
if args[1] == 'help' then
42+
opts.help = true
43+
return {}
44+
end
45+
46+
local positionals = argparse.processArgsGetopt(args, {
47+
{'h', 'help', handler=function() opts.help = true end},
48+
})
49+
50+
return positionals
51+
end
52+
53+
local function main(...)
54+
local args, opts = {...}, {}
55+
local positionals = process_args(opts, args)
56+
57+
if opts.help then
58+
print_help()
59+
return
60+
end
61+
62+
local history_index = tonumber(positionals[1])
63+
64+
if history_index then
65+
if history_index < 1 or history_index > copybuilding.MAX_HISTORY then
66+
qerror('History index must be 1-' .. copybuilding.MAX_HISTORY)
67+
end
68+
69+
local success, message = copybuilding.copy_building_from_history(history_index)
70+
if message then
71+
if success then
72+
print('Copied ' .. message)
73+
else
74+
qerror(message)
75+
end
76+
end
77+
else
78+
local success, message = copybuilding.copy_building_at_cursor()
79+
if message then
80+
if success then
81+
print('Copied ' .. message .. ' and starting construction mode...')
82+
else
83+
qerror(message)
84+
end
85+
end
86+
end
87+
end
88+
89+
if dfhack_flags.module then
90+
return
91+
end
92+
93+
main(...)

0 commit comments

Comments
 (0)