Skip to content

Commit 164d9bd

Browse files
committed
Add caravan script
1 parent 53abecc commit 164d9bd

1 file changed

Lines changed: 106 additions & 0 deletions

File tree

caravan.lua

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
-- Adjusts properties of caravans
2+
--[====[
3+
4+
caravan
5+
=======
6+
7+
Adjusts properties of caravans on the map. See also `force` to create caravans.
8+
9+
This script has multiple subcommands. Commands listed with the argument
10+
``[IDS]`` can take multiple caravan IDs (see ``caravan list``). If no IDs are
11+
specified, then the commands apply to all caravans on the map.
12+
13+
**Subcommands:**
14+
15+
- ``list``: lists IDs and information about all caravans on the map.
16+
- ``extend [DAYS] [IDS]``: extends the time that caravans stay at the depot by
17+
the specified number of days (defaults to 7 if not specified). Also causes
18+
caravans to return to the depot if applicable.
19+
- ``happy [IDS]``: makes caravans willing to trade again (after seizing goods,
20+
annoying merchants, etc.). Also causes caravans to return to the depot if
21+
applicable.
22+
- ``leave [IDS]``: makes caravans pack up and leave immediately.
23+
24+
]====]
25+
26+
--@ module = true
27+
28+
INTERESTING_FLAGS = {
29+
casualty = 'Casualty',
30+
hardship = 'Encountered hardship',
31+
seized = 'Goods seized',
32+
offended = 'Offended'
33+
}
34+
caravans = df.global.ui.caravans
35+
36+
function caravans_from_ids(ids)
37+
if not ids or #ids == 0 then
38+
return pairs(caravans)
39+
end
40+
local i = 0
41+
return function()
42+
i = i + 1
43+
local id = tonumber(ids[i])
44+
if id then
45+
return id, caravans[id]
46+
end
47+
return nil
48+
end
49+
end
50+
51+
function bring_back(car)
52+
if car.trade_state ~= df.caravan_state.T_trade_state.AtDepot then
53+
car.trade_state = df.caravan_state.T_trade_state.Approaching
54+
end
55+
end
56+
57+
commands = {}
58+
59+
function commands.list()
60+
for id, car in pairs(caravans) do
61+
print(dfhack.df2console(('%d: %s caravan from %s'):format(
62+
id,
63+
df.creature_raw.find(df.historical_entity.find(car.entity).race).name[2], -- adjective
64+
dfhack.TranslateName(df.historical_entity.find(car.entity).name)
65+
)))
66+
print(' ' .. (df.caravan_state.T_trade_state[car.trade_state] or 'Unknown state: ' .. car.trade_state))
67+
print((' %d day(s) remaining'):format(math.floor(car.time_remaining / 120)))
68+
for flag, msg in pairs(INTERESTING_FLAGS) do
69+
if car.flags[flag] then
70+
print(' ' .. msg)
71+
end
72+
end
73+
end
74+
end
75+
76+
function commands.extend(days, ...)
77+
days = tonumber(days or 7) or qerror('invalid number of days: ' .. days)
78+
for id, car in caravans_from_ids{...} do
79+
car.time_remaining = car.time_remaining + (days * 120)
80+
bring_back(car)
81+
end
82+
end
83+
84+
function commands.happy(...)
85+
for id, car in caravans_from_ids{...} do
86+
-- all flags default to false
87+
car.flags.whole = 0
88+
bring_back(car)
89+
end
90+
end
91+
92+
function commands.leave(...)
93+
for id, car in caravans_from_ids{...} do
94+
car.trade_state = df.caravan_state.T_trade_state.Leaving
95+
end
96+
end
97+
98+
function main(...)
99+
args = {...}
100+
command = table.remove(args, 1)
101+
commands[command](table.unpack(args))
102+
end
103+
104+
if not dfhack_flags.module then
105+
main(...)
106+
end

0 commit comments

Comments
 (0)