forked from DFHack/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
caravan.lua
181 lines (162 loc) · 5.53 KB
/
caravan.lua
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
-- Adjusts properties of caravans and provides overlays for enhanced trading
--@ module = true
local movegoods = reqscript('internal/caravan/movegoods')
local pedestal = reqscript('internal/caravan/pedestal')
local trade = reqscript('internal/caravan/trade')
local tradeagreement = reqscript('internal/caravan/tradeagreement')
dfhack.onStateChange.caravanTradeOverlay = function(code)
if code == SC_WORLD_UNLOADED then
trade.trader_selected_state = {}
trade.broker_selected_state = {}
trade.handle_ctrl_click_on_render = false
trade.handle_shift_click_on_render = false
end
end
OVERLAY_WIDGETS = {
trade=trade.TradeOverlay,
tradebanner=trade.TradeBannerOverlay,
tradeethics=trade.TradeEthicsWarningOverlay,
tradeagreement=tradeagreement.TradeAgreementOverlay,
movegoods=movegoods.MoveGoodsOverlay,
movegoods_hider=movegoods.MoveGoodsHiderOverlay,
assigntrade=movegoods.AssignTradeOverlay,
displayitemselector=pedestal.PedestalOverlay,
}
INTERESTING_FLAGS = {
casualty = 'Casualty',
hardship = 'Encountered hardship',
seized = 'Goods seized',
offended = 'Offended'
}
local caravans = df.global.plotinfo.caravans
local function caravans_from_ids(ids)
if not ids or #ids == 0 then
return caravans
end
local c = {} --as:df.caravan_state[]
for _,id in ipairs(ids) do
id = tonumber(id)
if id then
c[id] = caravans[id]
end
end
return c
end
function bring_back(car)
if car.trade_state ~= df.caravan_state.T_trade_state.AtDepot then
car.trade_state = df.caravan_state.T_trade_state.Approaching
end
end
local commands = {}
function commands.list()
for id, car in pairs(caravans) do
print(dfhack.df2console(('%d: %s caravan from %s'):format(
id,
df.creature_raw.find(df.historical_entity.find(car.entity).race).name[2], -- adjective
dfhack.TranslateName(df.historical_entity.find(car.entity).name)
)))
print(' ' .. (df.caravan_state.T_trade_state[car.trade_state] or ('Unknown state: ' .. car.trade_state)))
print((' %d day(s) remaining'):format(math.floor(car.time_remaining / 120)))
for flag, msg in pairs(INTERESTING_FLAGS) do
if car.flags[flag] then
print(' ' .. msg)
end
end
end
end
function commands.extend(days, ...)
days = tonumber(days or 7) or qerror('invalid number of days: ' .. days) --luacheck: retype
for id, car in pairs(caravans_from_ids{...}) do
car.time_remaining = car.time_remaining + (days * 120)
bring_back(car)
end
end
function commands.happy(...)
for id, car in pairs(caravans_from_ids{...}) do
-- all flags default to false
car.flags.whole = 0
bring_back(car)
end
end
function commands.leave(...)
for id, car in pairs(caravans_from_ids{...}) do
car.trade_state = df.caravan_state.T_trade_state.Leaving
end
local still_needs_broker = false
for _,car in ipairs(caravans) do
if car.trade_state == df.caravan_state.T_trade_state.Approaching or
car.trade_state == df.caravan_state.T_trade_state.AtDepot
then
still_needs_broker = true
break
end
end
if not still_needs_broker then
for _,depot in ipairs(df.global.world.buildings.other.TRADE_DEPOT) do
depot.trade_flags.trader_requested = false
for _, job in ipairs(depot.jobs) do
if job.job_type == df.job_type.TradeAtDepot then
dfhack.job.removeJob(job)
break
end
end
end
end
end
local function isDisconnectedPackAnimal(unit)
if unit.following then
local dragger = unit.following
return (
unit.relationship_ids[ df.unit_relationship_type.Dragger ] == -1 and
dragger.relationship_ids[ df.unit_relationship_type.Draggee ] == -1
)
end
end
local function getPrintableUnitName(unit)
local visible_name = dfhack.units.getVisibleName(unit)
local profession_name = dfhack.units.getProfessionName(unit)
if visible_name.has_name then
return ('%s (%s)'):format(dfhack.TranslateName(visible_name), profession_name)
end
return profession_name -- for unnamed animals
end
local function rejoin_pack_animals()
print('Reconnecting disconnected pack animals...')
local found = false
for _, unit in ipairs(df.global.world.units.active) do
if unit.flags1.merchant and isDisconnectedPackAnimal(unit) then
local dragger = unit.following
print((' %s <-> %s'):format(
dfhack.df2console(getPrintableUnitName(unit)),
dfhack.df2console(getPrintableUnitName(dragger))
))
unit.relationship_ids[ df.unit_relationship_type.Dragger ] = dragger.id
dragger.relationship_ids[ df.unit_relationship_type.Draggee ] = unit.id
found = true
end
end
if (found) then
print('All pack animals reconnected.')
else
print('No disconnected pack animals found.')
end
end
function commands.unload()
rejoin_pack_animals()
end
function commands.help()
print(dfhack.script_help())
end
function main(args)
local command = table.remove(args, 1) or 'list'
if commands[command] then
commands[command](table.unpack(args))
else
commands.help()
print()
qerror("No such command: " .. command)
end
end
if not dfhack_flags.module then
main{...}
end