Skip to content

Commit 416f3a0

Browse files
committed
trainmap: fixes to connect clear priority
1 parent f2d3134 commit 416f3a0

File tree

4 files changed

+32
-17
lines changed

4 files changed

+32
-17
lines changed

src/rails_lib/backend/backend_low.ml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,14 @@ let try_create_priority_shipment ?(force=false) v =
482482
| _ -> None
483483

484484
let try_cancel_priority_shipments ?(force=false) v =
485-
Player.cancel_priority ~force v.players ~cycle:v.cycle ~year:v.year v.region
486-
|> List.map (fun i -> PriorityShipmentCanceled{player=i})
485+
(* Try to cancel and create corresponding messages *)
486+
match Player.cancel_priority_shipment ~force v.players ~cycle:v.cycle ~year:v.year v.region with
487+
| _::_ as players ->
488+
let station_map = Station_map.clear_priority_shipment_for_all v.stations ~players in
489+
[%upf v.stations <- station_map];
490+
List.map (fun i -> PriorityShipmentCanceled{player=i}) players
491+
| _ -> []
492+
487493

488494
(** Most time-based work happens here **)
489495
let handle_cycle v =
@@ -529,7 +535,6 @@ let handle_cycle v =
529535
else ui_msgs
530536
in
531537
(* Cancel any expired priority shipments *)
532-
533538
let ui_msgs = (try_cancel_priority_shipments v) @ ui_msgs in
534539

535540
(* adjust time *)

src/rails_lib/backend/player.ml

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -385,23 +385,30 @@ let set_priority priority player = {player with priority}
385385

386386
let has_priority player = Option.is_some player.priority
387387

388-
let check_cancel_priority player ~cycle ~year region =
388+
let check_cancel_priority_shipment player ~cycle ~year region =
389389
(* Priority shipments are cancelled when the bonus is < 20 *)
390390
match player.priority with
391391
| None -> false
392392
| Some pr_data ->
393393
Priority_shipment.should_be_cancelled pr_data ~cycle ~year region
394394

395-
let cancel_priority ?(force=false) players ~cycle ~year region =
395+
let cancel_priority_shipment ?(force=false) players ~cycle ~year region =
396396
(* Cancel priority and let us know which players' were canceled *)
397-
Array.foldi (fun acc i player ->
398-
if has_priority player &&
399-
(check_cancel_priority player ~cycle ~year region || force) then (
400-
players.(i) <- {player with priority=None};
401-
i::acc
402-
) else acc)
403-
[]
404-
players
397+
let cancel_players =
398+
Array.foldi (fun acc i player ->
399+
if has_priority player &&
400+
(check_cancel_priority_shipment player ~cycle ~year region || force)
401+
then i::acc else acc) []
402+
players
403+
in
404+
List.iter (fun i ->
405+
let player = players.(i) in
406+
let trains = Trainmap.clear_priority_shipment player.trains in
407+
let player = [%up {player with trains; priority=None}] in
408+
players.(i) <- player
409+
)
410+
cancel_players;
411+
cancel_players
405412

406413
let update players idx f =
407414
let p = players.(idx) in

src/rails_lib/backend/trainmap.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,13 @@ let total_engine_value (v:t) =
143143
let total_car_value (v:t) =
144144
C.car_cost * fold (fun acc train -> acc + Train.get_num_of_cars train) v ~init:0
145145

146-
let clear_priority_shipment_for_all v ~players =
146+
let clear_priority_shipment v =
147147
(* Clear priority shipment holding for the given players *)
148148
let open Train in
149149
Vector.map_in_place (fun train ->
150-
if List.mem ~eq:Int.equal train.player players then
150+
if Train.holds_priority_shipment train then
151151
Train.set_priority_shipment train false
152152
else train)
153-
v.trains
154-
153+
v.trains;
154+
v
155155

src/rails_lib/backend/trainmap.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,6 @@ val get_at_loc: Utils.loc -> t -> Id.t list
3939
val total_engine_value: t -> int
4040

4141
val total_car_value: t -> int
42+
43+
val clear_priority_shipment: t -> t
44+

0 commit comments

Comments
 (0)