@@ -59,6 +59,9 @@ module Train_update = struct
59
59
let handle_stop station_info =
60
60
let had_maintenance = Station. can_maintain station || train.had_maintenance in
61
61
(* TODO: deal with priority shipment *)
62
+ let holds_priority_shipment =
63
+ Train. holds_priority_shipment station train || Station. holds_priority_shipment station in
64
+
62
65
(* TODO: deal with dist_shipped_cargo *)
63
66
let _dist_shipped_cargo =
64
67
let dist = Utils. classic_dist loc train.last_station in
@@ -259,7 +262,7 @@ module Train_update = struct
259
262
(* Second check of this. No matter *)
260
263
let can_go, cancel_override = Station. can_train_go station dir in
261
264
if can_go then (
262
- (* Mutably canel override if needed. Stringing the station along
265
+ (* Mutably cancel override if needed. Stringing the station along
263
266
right now would be painful *)
264
267
begin match cancel_override with
265
268
| `Cancel_override -> Station. cancel_override_mut station dir
@@ -279,7 +282,7 @@ module Train_update = struct
279
282
{train with state= Train. StoppedAtSignal dir}
280
283
)
281
284
282
- let _handle_train_mid_tile ~idx ~cycle (v :t ) (train :rw Train.t ) ((x , y ) as loc ) =
285
+ let _handle_train_mid_tile ~idx ~cycle (v :t ) (train :rw Train.t ) stations ((x , y ) as loc ) =
283
286
(* All major computation happens mid-tile *)
284
287
(* Log.debug (fun f -> f "_update_train_mid_tile"); *)
285
288
(* TODO: check for colocated trains (accidents/stop a train) *)
@@ -290,22 +293,22 @@ module Train_update = struct
290
293
3. Prevent from leaving via manual signal hold
291
294
*)
292
295
let track = Trackmap. get_exn v.track ~x ~y in
296
+ let default_ret = train, stations, [] in
293
297
match track.kind with
294
298
| Station _ ->
295
299
(* TODO: remove override Proceed after one train *)
296
300
let train, ui_msgs = match train.state with
297
301
(* This is only when we've already processed the train *)
298
- | Traveling s when s.traveling_past_station ->
299
- train, []
302
+ | Traveling s when s.traveling_past_station -> default_ret
300
303
301
304
(* This is before possibly entering the station *)
302
305
| Traveling s ->
303
306
Block_map. block_decr_train s.block v.blocks;
304
307
let train, ui_msgs =
305
308
if train.hold_at_next_station then (
306
- {train with state = HoldingAtStation }, []
309
+ {train with state = HoldingAtStation }, stations, []
307
310
) else (
308
- let station = Station_map. get_exn loc v. stations in
311
+ let station = Station_map. get_exn loc stations in
309
312
_enter_station v idx train station loc
310
313
)
311
314
in
@@ -314,12 +317,12 @@ module Train_update = struct
314
317
_exit_station ~idx ~cycle v train track loc, ui_msgs
315
318
else
316
319
(* Some kind of stop. Exit later *)
317
- train, ui_msgs
320
+ train, stations, ui_msgs
318
321
319
322
(* Loading/unloading goods at the station *)
320
323
| LoadingAtStation s when s.wait_time > 0 ->
321
324
s.wait_time < - s.wait_time - 1 ;
322
- train, []
325
+ default_ret
323
326
324
327
| LoadingAtStation _ ->
325
328
(* Done loading/unloading. Check if we can exit the station *)
@@ -328,7 +331,7 @@ module Train_update = struct
328
331
| `Wait -> {train with state= Train. WaitingForFullLoad }
329
332
| _ -> _exit_station ~idx ~cycle v train track loc
330
333
in
331
- train, []
334
+ default_ret
332
335
333
336
| WaitingForFullLoad when Train. is_full train ->
334
337
(* Done waiting for full load *)
@@ -343,18 +346,18 @@ module Train_update = struct
343
346
in
344
347
if wait_time > 0 then
345
348
(* We found stuff to load *)
346
- {train with state = LoadingAtStation {wait_time}; cars}, []
349
+ {train with state = LoadingAtStation {wait_time}; cars}, stations, []
347
350
else
348
351
(* Keep waiting for more goods to show up *)
349
- [% up {train with cars}], []
352
+ [% up {train with cars}], stations, []
350
353
351
354
| HoldingAtStation when train.hold_at_next_station ->
352
355
(* Don't enter station yet *)
353
- train, []
356
+ default_ret
354
357
355
358
| HoldingAtStation ->
356
359
(* Hold happens before we enter the station *)
357
- let station = Station_map. get_exn loc v. stations in
360
+ let station = Station_map. get_exn loc stations in
358
361
_enter_station v idx train station loc
359
362
360
363
| StoppedAtSignal dir ->
@@ -365,10 +368,10 @@ module Train_update = struct
365
368
if can_go then
366
369
_exit_station ~idx ~cycle v train track loc, []
367
370
else
368
- train, []
371
+ default_ret
369
372
in
370
373
Log. debug (fun f -> f " Train at station: %s" (Train. show_state train.state));
371
- train, ui_msgs
374
+ train, stations, ui_msgs
372
375
373
376
(* --- Below this trains cannot stop so must be traveling --- *)
374
377
@@ -380,24 +383,24 @@ module Train_update = struct
380
383
~ixn: loc ~cur_dir: train.dir ~dest
381
384
|> Option. get_exn_or " Cannot find route for train"
382
385
in
383
- _update_train_target_speed v train track ~idx ~cycle ~x ~y ~dir , []
386
+ _update_train_target_speed v train track ~idx ~cycle ~x ~y ~dir , stations, []
384
387
385
388
| _ ->
386
389
(* All other track and non-decision ixns *)
387
390
let dir =
388
391
Dir.Set. find_nearest train.dir track.dirs
389
392
|> Option. get_exn_or " Cannot find track for train"
390
393
in
391
- _update_train_target_speed v train track ~idx ~cycle ~x ~y ~dir , []
394
+ _update_train_target_speed v train track ~idx ~cycle ~x ~y ~dir , stations, []
392
395
393
- let update_train v idx (train :rw Train.t ) ~cycle ~cycle_check ~cycle_bit ~region_div =
396
+ let _update_train v idx (train :rw Train.t ) stations ~cycle ~cycle_check ~cycle_bit ~region_div =
394
397
(* This is the regular update function for trains. Important stuff happens midtile *)
395
398
(* let priority = (Goods.freight_to_enum train.freight) * 3 - (Train.train_type_to_enum train.typ) + 2 in *)
396
399
begin match train.state with
397
400
| Traveling travel_state ->
398
401
let train = Train. update_speed train ~cycle ~cycle_check ~cycle_bit in
399
402
(* TODO: fiscal period update stuff *)
400
- let rec train_update_loop train speed_bound ui_msg_acc =
403
+ let rec train_update_loop train speed_bound stations ui_msg_acc =
401
404
let speed = Train. get_speed train in
402
405
if speed_bound > = speed then train, ui_msg_acc
403
406
else (
@@ -415,7 +418,7 @@ let update_train v idx (train:rw Train.t) ~cycle ~cycle_check ~cycle_bit ~region
415
418
|> min Train. update_array_length
416
419
in
417
420
(* Log.debug (fun f -> f "Update val %d, cycle_bit %d" update_val cycle_bit); *)
418
- let train, ui_msgs =
421
+ let train, stations, ui_msgs =
419
422
if (Train. update_cycle_array.(update_val) land cycle_bit) <> 0 then begin
420
423
(* Log.debug (fun f -> f "Pass test. Update val %d, cycle_bit %d" update_val cycle_bit); *)
421
424
let is_mid_tile =
@@ -427,40 +430,40 @@ let update_train v idx (train:rw Train.t) ~cycle ~cycle_check ~cycle_bit ~region
427
430
(* Make sure we don't double-process mid-tiles *)
428
431
match is_mid_tile, travel_state.traveling_past_station with
429
432
| true , false ->
430
- _handle_train_mid_tile ~idx ~cycle v train loc
433
+ _handle_train_mid_tile ~idx ~cycle v train stations loc
431
434
| false , true ->
432
435
travel_state.traveling_past_station < - false ;
433
- Train. advance train, []
436
+ Train. advance train, stations, []
434
437
| _ ->
435
- Train. advance train, []
438
+ Train. advance train, stations, []
436
439
end else
437
- train, []
440
+ train, stations, []
438
441
in
439
- train_update_loop train (speed_bound + 12 ) (ui_msgs @ ui_msg_acc)
442
+ train_update_loop train (speed_bound + 12 ) stations (ui_msgs @ ui_msg_acc)
440
443
)
441
444
in
442
- train_update_loop train 0 []
445
+ train_update_loop train 0 stations []
443
446
444
447
| _ -> (* Other train states or time is up *)
445
448
let loc = train.x / C. tile_w, train.y / C. tile_h in
446
- _handle_train_mid_tile ~idx ~cycle v train loc
449
+ _handle_train_mid_tile ~idx ~cycle v train stations loc
447
450
end
448
451
449
-
450
452
(* Run every cycle, updating every train's position and speed *)
451
453
let _update_all_trains (v :t ) ~player =
452
454
(* Log.debug (fun f -> f "update_all_trains"); *)
453
455
let cycle_check, region_div = if Region. is_us v.region then 16 , 1 else 8 , 2 in
454
456
let cycle_bit = 1 lsl (v.cycle mod 12 ) in
455
457
let cycle = v.cycle in
456
458
(* TODO: We update the high priority trains before the low priority *)
457
- Trainmap. fold_mapi_in_place (fun idx ui_msg_acc train ->
458
- let train, ui_msgs =
459
- update_train v idx train ~cycle ~cycle_check ~cycle_bit ~region_div
459
+ Trainmap. fold_mapi_in_place (fun idx ( stations , ui_msg_acc ) train ->
460
+ let train, stations, ui_msgs =
461
+ _update_train v idx train stations ~cycle ~cycle_check ~cycle_bit ~region_div
460
462
in
461
- ui_msgs @ ui_msg_acc, train)
463
+ (stations, ui_msgs @ ui_msg_acc), train
464
+ )
462
465
v.players.(player).trains
463
- ~init: []
466
+ ~init: (v.stations, [] )
464
467
465
468
end
466
469
0 commit comments