@@ -507,15 +507,54 @@ let try_create_priority_shipment ?(force=false) v =
507
507
end
508
508
| _ -> None
509
509
510
+ let clear_player_and_train_priority_shipments v player_list =
511
+ List. iter (fun i ->
512
+ let player = v.players.(i) in
513
+ let trains = Trainmap. clear_priority_shipment player.trains in
514
+ let player = [% up {player with trains; priority= None }] in
515
+ v.players.(i) < - player
516
+ ) player_list
517
+
510
518
let try_cancel_priority_shipments ?(force =false ) v =
511
519
(* Try to cancel and create corresponding messages *)
512
- match Player. cancel_priority_shipment ~force v.players ~cycle: v.cycle ~year: v.year v.region with
520
+ let try_cancel_priority_shipment_all players ~cycle ~year region =
521
+ (* Cancel priority and let us know which players' were canceled *)
522
+ let cancel_players =
523
+ Array. foldi (fun acc i player ->
524
+ if Player. has_priority player &&
525
+ (Player. check_cancel_priority_shipment player ~cycle ~year region || force)
526
+ then i::acc else acc) []
527
+ players
528
+ in
529
+ clear_player_and_train_priority_shipments v cancel_players;
530
+ cancel_players
531
+ in
532
+ match try_cancel_priority_shipment_all v.players ~cycle: v.cycle ~year: v.year v.region with
513
533
| _ ::_ as players ->
514
- let station_map = Station_map. clear_priority_shipment_for_all v.stations ~players in
515
- [% upf v.stations < - station_map ];
534
+ let stations = Station_map. clear_priority_shipment_for_all v.stations ~players in
535
+ [% upf v.stations < - stations ];
516
536
List. map (fun i -> PriorityShipmentCanceled {player= i}) players
517
537
| _ -> []
518
538
539
+ let check_priority_delivery v =
540
+ (* Check if a priority shipment has been delivered *)
541
+ let check_priority_delivery_all players stations ~cycle ~year region =
542
+ (* Check for priority delivery completion *)
543
+ let deliver_players =
544
+ Array. foldi (fun acc i player ->
545
+ if Player. has_priority player && Player. check_priority_delivery player stations
546
+ then i::acc else acc) []
547
+ players
548
+ in
549
+ clear_player_and_train_priority_shipments v deliver_players;
550
+ deliver_players
551
+ in
552
+ match check_priority_delivery_all v.players v.stations ~cycle: v.cycle ~year: v.year v.region with
553
+ | _ ::_ as players ->
554
+ let stations = Station_map. clear_priority_shipment_for_all v.stations ~players in
555
+ [% upf v.stations < - stations];
556
+ List. map (fun i -> PriorityShipmentDelivered {player= i}) players
557
+ | _ -> []
519
558
520
559
(* * Most time-based work happens here **)
521
560
let handle_cycle v =
@@ -568,7 +607,10 @@ let handle_cycle v =
568
607
else ui_msgs
569
608
in
570
609
(* Cancel any expired priority shipments *)
571
- let ui_msgs = (try_cancel_priority_shipments v) @ ui_msgs in
610
+ let cancel_ui_msgs = try_cancel_priority_shipments v in
611
+ let delivered_priority_ui_msgs = check_priority_delivery v in
612
+
613
+ let ui_msgs = cancel_ui_msgs @ ui_msgs in
572
614
573
615
(* adjust time *)
574
616
v.time < - v.time + 1 ;
0 commit comments