-
Notifications
You must be signed in to change notification settings - Fork 5
/
schedule.lua
337 lines (267 loc) · 8.45 KB
/
schedule.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
--luacheck: in=
function schedule_get_overview()
local squads = {}
for i,squad in ipairs(find_fortress_squads()) do
local alerts = {}
for j,a in ipairs(squad.schedule) do
local months = {}
for m,sched in ipairs(a) do
if #sched.orders > 0 then
if #sched.orders > 1 then
local last = nil
local same = true
for i,sched_order in ipairs(sched.orders) do
local order = sched_order.order
local otype = order:getType()
if last and otype ~= last then
same = false
end
last = otype
end
table.insert(months, same and last or -2)
else
--todo: can be >1 ??
local sched_order = sched.orders[0]
local order = sched_order.order
local otype = order:getType()
table.insert(months, otype)
end
else
table.insert(months, -1)
end
end
table.insert(alerts, months)
end
table.insert(squads, { squadname(squad), squad.id, alerts })
end
local alerts = {}
for i,alert in ipairs(df.global.ui.alerts.list) do
table.insert(alerts, { alertname(alert), alert.id })
end
return { squads, alerts }
end
--luacheck: in=number,number
function schedule_get_months(squadid, alertid)
local alertidx = alert_id2index(alertid)
if alertidx == -1 then
return
end
local squad = df.squad.find(squadid)
local months = {}
for m,sched in ipairs(squad.schedule[alertidx]) do
if #sched.orders > 0 then
if #sched.orders > 1 then
local last = nil
local same = true
for i,sched_order in ipairs(sched.orders) do
local order = sched_order.order
local otype = order:getType()
if last and otype ~= last then
same = false
end
last = otype
end
table.insert(months, { same and last or -2, sched.sleep_mode, sched.uniform_mode, -1 })
else
local sched_order = sched.orders[0]
local order = sched_order.order
local otype = order:getType()
table.insert(months, { otype, sched.sleep_mode, sched.uniform_mode, sched_order.min_count })
end
else
table.insert(months, { -1, sched.sleep_mode, sched.uniform_mode, -1 })
end
end
local cur_month = math.floor(df.global.cur_year_tick / 33600)
return { months, cur_month }
end
--todo: order objects we're deleting may be references somewhere else, so maybe it's better to do this using the UI
--luacheck: in=number,number,number
function schedule_month_duplicate_to_all(squadid, alertid, month)
local alertidx = alert_id2index(alertid)
if alertidx == -1 then
return
end
local squad = df.squad.find(squadid)
local sched = squad.schedule[alertidx][month]
for m,sched2 in ipairs(squad.schedule[alertidx]) do
if m ~= month then
sched2.name = sched.name
sched2.sleep_mode = sched.sleep_mode
sched2.uniform_mode = sched.uniform_mode
for i,o in ipairs(sched2.orders) do
o.order:delete()
o:delete()
end
sched2.orders:resize(0)
for i,sched_order in ipairs(sched.orders) do
local sched_order2 = df.squad_schedule_order:new()
sched_order2.order = sched_order.order:clone()
sched_order2.min_count = sched_order.min_count
--xxx: sched_order2.positions:insert(j,pos) crashes for some reason
sched_order2.positions:resize(#sched_order.positions)
for j,pos in ipairs(sched_order.positions) do
sched_order2.positions[j] = pos
end
sched2.orders:insert(i, sched_order2)
end
--todo: order_assignments ?
end
end
end
--luacheck: in=number,number,number,number,number
function schedule_set_options(squadid, alertid, month, sleep_mode, uniform_mode)
local alertidx = alert_id2index(alertid)
if alertidx == -1 then
return
end
local squad = df.squad.find(squadid)
local sched = squad.schedule[alertidx][month]
sched.sleep_mode = sleep_mode
sched.uniform_mode = uniform_mode
end
--luacheck: in=number,number,number
function schedule_get_orders(squadid, alertid, month)
local alertidx = alert_id2index(alertid)
if alertidx == -1 then
return
end
local squad = df.squad.find(squadid)
local sched = squad.schedule[alertidx][month]
local orders = {}
for i,sched_order in ipairs(sched.orders) do
local order = sched_order.order
local otype = order:getType()
local title = utils.call_with_string(order, 'getDescription')
table.insert(orders, { title, otype, sched_order.min_count })
end
return { orders, sched.sleep_mode, sched.uniform_mode }
end
--luacheck: in=number,number,number,number
function schedule_order_get(squadid, alertid, month, orderidx)
local alertidx = alert_id2index(alertid)
if alertidx == -1 then
return
end
local squad = df.squad.find(squadid)
local sched = squad.schedule[alertidx][month]
local sched_order = sched.orders[orderidx]
local order = sched_order.order
local otype = order:getType()
local title = utils.call_with_string(order, 'getDescription')
return { title, otype, sched_order.min_count }
end
--luacheck: in=number,number,number,number,number
function schedule_order_set_mincount(squadid, alertid, month, orderidx, min_count)
local alertidx = alert_id2index(alertid)
if alertidx == -1 then
return
end
local squad = df.squad.find(squadid)
local sched = squad.schedule[alertidx][month]
local sched_order = sched.orders[orderidx]
sched_order.min_count = min_count
end
--luacheck: in=number,number,number,number
function schedule_order_cancel(squadid, alertid, month, orderidx)
local alertidx = alert_id2index(alertid)
if alertidx == -1 then
return
end
local squad = df.squad.find(squadid)
local sched = squad.schedule[alertidx][month]
local sched_order = sched.orders[orderidx]
sched.orders:erase(orderidx)
sched_order.order:delete()
sched_order:delete()
end
--todo: the code is here temporarily becuase of a bug in the app which expects a table at index 2
--luacheck: in=number
function schedule_order_get_choices(type)
if type == df.squad_order_type.DEFEND_BURROWS then
--return burrows_get_list()
local ret = {}
for i,v in ipairs(df.global.ui.burrows.list) do
table.insert(ret, { burrowname(v), v.id, {} })
end
return ret
elseif type == df.squad_order_type.MOVE then
--return waypoints_get_points()
local ret = {}
for i,v in ipairs(df.global.ui.waypoints.points) do
table.insert(ret, { pointname(v), v.id, {} })
end
return ret
elseif type == df.squad_order_type.PATROL_ROUTE then
return routes_get_list(true)
end
end
--luacheck: in=number,number,number,number,number[]
function schedule_order_add(squadid, alertid, month, type, targets)
local alertidx = alert_id2index(alertid)
if alertidx == -1 then
return
end
if type ~= df.squad_order_type.TRAIN and (not targets or #targets == 0) then
return
end
local squad = df.squad.find(squadid)
local sched = squad.schedule[alertidx][month]
local order = nil
if type == df.squad_order_type.TRAIN then
local train = df.squad_order_trainst:new()
order = train
train.unk_v40_1 = -1
train.unk_v40_2 = -1
train.unk_v40_3 = 0
elseif type == df.squad_order_type.DEFEND_BURROWS then
local defend = df.squad_order_defend_burrowsst:new()
order = defend
defend.unk_v40_1 = -1
defend.unk_v40_2 = -1
defend.unk_v40_3 = 0
for i,v in ipairs(targets) do
defend.burrows:insert(#defend.burrows, v)
end
elseif type == df.squad_order_type.PATROL_ROUTE then
local patrol = df.squad_order_patrol_routest:new()
order = patrol
patrol.unk_v40_1 = -1
patrol.unk_v40_2 = -1
patrol.unk_v40_3 = 0
patrol.route_id = targets[1]
elseif type == df.squad_order_type.MOVE then
local pt = waypoint_find_by_id(targets[1])
if not pt then
return
end
local move = df.squad_order_movest:new()
order = move
move.unk_v40_1 = -1
move.unk_v40_2 = -1
move.unk_v40_3 = 0
move.point_id = pt.id
move.pos.x = pt.pos.x
move.pos.y = pt.pos.y
move.pos.z = pt.pos.z
end
if not order then
return
end
order.year = df.global.cur_year
order.year_tick = df.global.cur_year_tick
local sched_order = df.squad_schedule_order:new()
sched_order.min_count = 10
sched_order.order = order
--xxx: sched_order.positions:insert(j,pos) crashes for some reason
sched_order.positions:resize(10)
for j,pos in ipairs(sched_order.positions) do
sched_order.positions[j] = false
end
sched.orders:insert(#sched.orders, sched_order)
return true
end
--print(pcall(function() return json:encode(schedule_get_overview2()) end))
--print(pcall(function() return json:encode(schedule_get_months(23,1)) end))
--print(pcall(function() return json:encode(schedule_get_orders(104,1,0)) end))
--print(pcall(function() return json:encode(schedule_month_duplicate_to_all(137,1,0)) end))