Skip to content

Commit 7025d64

Browse files
committed
Continue refactor
1 parent 838afbf commit 7025d64

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

src/rails_lib/backend/backend_low.ml

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ module Train_update = struct
229229
delivery to station, print revenue
230230
*)
231231

232-
let _enter_station (v:t) idx (train: rw Train.t) station loc =
232+
let _enter_station (v:t) idx (train: rw Train.t) stations loc =
233+
let station = Station_map.get_exn loc stations in
233234
(* TODO: income handling *)
234235
let last_station, priority_stop, stop, train, _income, ui_msgs =
235236
if Station.is_proper_station station then (
@@ -242,9 +243,9 @@ module Train_update = struct
242243
train.last_station, train.priority_stop, train.stop, train, 0, []
243244
)
244245
in
245-
{train with last_station; priority_stop; stop}, ui_msgs
246+
{train with last_station; priority_stop; stop}, stations, ui_msgs
246247

247-
let _exit_station ~idx ~cycle (v:t) (train: rw Train.t) (track:Track.t) ((x, y) as loc) =
248+
let _exit_station ~idx ~cycle (v:t) (train: rw Train.t) stations (track:Track.t) ((x, y) as loc) =
248249
let compute_dir_to_dest graph =
249250
(* This is expensive *)
250251
let dest = Train.get_dest train in
@@ -258,7 +259,7 @@ module Train_update = struct
258259
dir
259260
in
260261
let dir = compute_dir_to_dest v.graph in
261-
let station = Station_map.get_exn loc v.stations in
262+
let station = Station_map.get_exn loc stations in
262263
(* Second check of this. No matter *)
263264
let can_go, cancel_override = Station.can_train_go station dir in
264265
if can_go then (
@@ -277,9 +278,9 @@ module Train_update = struct
277278
state=Train.Traveling {speed=0; target_speed=4; traveling_past_station=true; block}
278279
}
279280
in
280-
_update_train_target_speed v train track ~idx ~cycle ~x ~y ~dir
281+
_update_train_target_speed v train track ~idx ~cycle ~x ~y ~dir, stations
281282
) else (
282-
{train with state=Train.StoppedAtSignal dir}
283+
{train with state=Train.StoppedAtSignal dir}, stations
283284
)
284285

285286
let _handle_train_mid_tile ~idx ~cycle (v:t) (train:rw Train.t) stations ((x, y) as loc) =
@@ -297,24 +298,24 @@ module Train_update = struct
297298
match track.kind with
298299
| Station _ ->
299300
(* TODO: remove override Proceed after one train *)
300-
let train, ui_msgs = match train.state with
301+
let train, stations, ui_msgs = match train.state with
301302
(* This is only when we've already processed the train *)
302303
| Traveling s when s.traveling_past_station -> default_ret
303304

304305
(* This is before possibly entering the station *)
305306
| Traveling s ->
306307
Block_map.block_decr_train s.block v.blocks;
307-
let train, ui_msgs =
308+
let train, stations, ui_msgs =
308309
if train.hold_at_next_station then (
309310
{train with state = HoldingAtStation}, stations, []
310311
) else (
311-
let station = Station_map.get_exn loc stations in
312-
_enter_station v idx train station loc
312+
_enter_station v idx train stations loc
313313
)
314314
in
315315
if Train.is_traveling train then
316316
(* No stopping at this station *)
317-
_exit_station ~idx ~cycle v train track loc, ui_msgs
317+
let train, stations = _exit_station ~idx ~cycle v train stations track loc in
318+
train, stations, ui_msgs
318319
else
319320
(* Some kind of stop. Exit later *)
320321
train, stations, ui_msgs
@@ -327,22 +328,21 @@ module Train_update = struct
327328
| LoadingAtStation _ ->
328329
(* Done loading/unloading. Check if we can exit the station *)
329330
let wait_at_stop, _ = Train.get_next_stop train in
330-
let train = match wait_at_stop with
331+
let train, stations = match wait_at_stop with
331332
| `Wait -> {train with state=Train.WaitingForFullLoad}
332-
| _ -> _exit_station ~idx ~cycle v train track loc
333+
| _ -> _exit_station ~idx ~cycle v train stations track loc
333334
in
334-
default_ret
335+
train, stations, []
335336

336337
| WaitingForFullLoad when Train.is_full train ->
337338
(* Done waiting for full load *)
338-
_exit_station ~idx ~cycle v train track loc, []
339+
let train, stations = _exit_station ~idx ~cycle v train stations track loc in
340+
train, stations, []
339341

340342
| WaitingForFullLoad ->
341343
(* If we're not full, we need to see if we can offload more from the station *)
342-
let wait_time, cars =
343-
let station = Station_map.get_exn loc v.stations in
344-
let station_supply = Station.get_supply_exn station in
345-
Train_station.train_pickup_and_empty_station train.cars loc v.cycle station_supply
344+
let wait_time, cars, stations =
345+
Train_station.train_pickup_and_empty_station train.cars loc ~cycle:v.cycle stations
346346
in
347347
if wait_time > 0 then
348348
(* We found stuff to load *)
@@ -357,16 +357,16 @@ module Train_update = struct
357357

358358
| HoldingAtStation ->
359359
(* Hold happens before we enter the station *)
360-
let station = Station_map.get_exn loc stations in
361-
_enter_station v idx train station loc
360+
_enter_station v idx train stations loc
362361

363362
| StoppedAtSignal dir ->
364363
(* This happens after we've already 'exited' *)
365364
let station = Station_map.get_exn loc v.stations in
366365
(* Check here as well to avoid expensive computation *)
367366
let can_go, _ = Station.can_train_go station dir in
368367
if can_go then
369-
_exit_station ~idx ~cycle v train track loc, []
368+
let train, stations = _exit_station ~idx ~cycle v train stations track loc in
369+
train, stations, []
370370
else
371371
default_ret
372372
in

src/rails_lib/backend/train_station.ml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@ let dump_unused_cars_to_station cars (stop:T.stop) station_supply =
4949
work_done, expense, train_cars
5050

5151

52-
let train_pickup_and_empty_station cars source cycle station_supply =
52+
let train_pickup_and_empty_station cars source ~cycle stations =
5353
(* Go over the train and find goods to fill it up with.
5454
Returns time for pickup and new cars
5555
*)
56+
let station = Station_map.get_exn source stations in
57+
let station_supply = Station.get_supply_exn station in
5658
let total_pickup, pickup_amounts =
5759
List.fold_map (fun total car ->
5860
let car_amount, good = T.Car.get_amount car, T.Car.get_good car in
@@ -66,7 +68,7 @@ let train_pickup_and_empty_station cars source cycle station_supply =
6668
in
6769
(* Shortcut if we can pick up nothing *)
6870
match total_pickup with
69-
| 0 -> 0, cars
71+
| 0 -> 0, cars, stations
7072
| _ ->
7173
let cars =
7274
List.map2 (fun (car:T.Car.t) add_amount ->
@@ -97,7 +99,7 @@ let train_pickup_and_empty_station cars source cycle station_supply =
9799
cars
98100
pickup_amounts
99101
in
100-
time_pickup, cars
102+
time_pickup, cars, stations
101103

102104
(* Check whether a train stops at a particular size station *)
103105
let train_class_stops_at station_info train =

0 commit comments

Comments
 (0)