Skip to content

Commit 838afbf

Browse files
committed
Start refactor to pass station around train loop
1 parent a0f0211 commit 838afbf

File tree

1 file changed

+36
-33
lines changed

1 file changed

+36
-33
lines changed

src/rails_lib/backend/backend_low.ml

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ module Train_update = struct
5959
let handle_stop station_info =
6060
let had_maintenance = Station.can_maintain station || train.had_maintenance in
6161
(* TODO: deal with priority shipment *)
62+
let holds_priority_shipment =
63+
Train.holds_priority_shipment station train || Station.holds_priority_shipment station in
64+
6265
(* TODO: deal with dist_shipped_cargo *)
6366
let _dist_shipped_cargo =
6467
let dist = Utils.classic_dist loc train.last_station in
@@ -259,7 +262,7 @@ module Train_update = struct
259262
(* Second check of this. No matter *)
260263
let can_go, cancel_override = Station.can_train_go station dir in
261264
if can_go then (
262-
(* Mutably canel override if needed. Stringing the station along
265+
(* Mutably cancel override if needed. Stringing the station along
263266
right now would be painful *)
264267
begin match cancel_override with
265268
| `Cancel_override -> Station.cancel_override_mut station dir
@@ -279,7 +282,7 @@ module Train_update = struct
279282
{train with state=Train.StoppedAtSignal dir}
280283
)
281284

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) =
283286
(* All major computation happens mid-tile *)
284287
(* Log.debug (fun f -> f "_update_train_mid_tile"); *)
285288
(* TODO: check for colocated trains (accidents/stop a train) *)
@@ -290,22 +293,22 @@ module Train_update = struct
290293
3. Prevent from leaving via manual signal hold
291294
*)
292295
let track = Trackmap.get_exn v.track ~x ~y in
296+
let default_ret = train, stations, [] in
293297
match track.kind with
294298
| Station _ ->
295299
(* TODO: remove override Proceed after one train *)
296300
let train, ui_msgs = match train.state with
297301
(* 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
300303

301304
(* This is before possibly entering the station *)
302305
| Traveling s ->
303306
Block_map.block_decr_train s.block v.blocks;
304307
let train, ui_msgs =
305308
if train.hold_at_next_station then (
306-
{train with state = HoldingAtStation}, []
309+
{train with state = HoldingAtStation}, stations, []
307310
) else (
308-
let station = Station_map.get_exn loc v.stations in
311+
let station = Station_map.get_exn loc stations in
309312
_enter_station v idx train station loc
310313
)
311314
in
@@ -314,12 +317,12 @@ module Train_update = struct
314317
_exit_station ~idx ~cycle v train track loc, ui_msgs
315318
else
316319
(* Some kind of stop. Exit later *)
317-
train, ui_msgs
320+
train, stations, ui_msgs
318321

319322
(* Loading/unloading goods at the station *)
320323
| LoadingAtStation s when s.wait_time > 0 ->
321324
s.wait_time <- s.wait_time - 1;
322-
train, []
325+
default_ret
323326

324327
| LoadingAtStation _ ->
325328
(* Done loading/unloading. Check if we can exit the station *)
@@ -328,7 +331,7 @@ module Train_update = struct
328331
| `Wait -> {train with state=Train.WaitingForFullLoad}
329332
| _ -> _exit_station ~idx ~cycle v train track loc
330333
in
331-
train, []
334+
default_ret
332335

333336
| WaitingForFullLoad when Train.is_full train ->
334337
(* Done waiting for full load *)
@@ -343,18 +346,18 @@ module Train_update = struct
343346
in
344347
if wait_time > 0 then
345348
(* We found stuff to load *)
346-
{train with state = LoadingAtStation {wait_time}; cars}, []
349+
{train with state = LoadingAtStation {wait_time}; cars}, stations, []
347350
else
348351
(* Keep waiting for more goods to show up *)
349-
[%up {train with cars}], []
352+
[%up {train with cars}], stations, []
350353

351354
| HoldingAtStation when train.hold_at_next_station ->
352355
(* Don't enter station yet *)
353-
train, []
356+
default_ret
354357

355358
| HoldingAtStation ->
356359
(* 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
358361
_enter_station v idx train station loc
359362

360363
| StoppedAtSignal dir ->
@@ -365,10 +368,10 @@ module Train_update = struct
365368
if can_go then
366369
_exit_station ~idx ~cycle v train track loc, []
367370
else
368-
train, []
371+
default_ret
369372
in
370373
Log.debug (fun f -> f "Train at station: %s" (Train.show_state train.state));
371-
train, ui_msgs
374+
train, stations, ui_msgs
372375

373376
(* --- Below this trains cannot stop so must be traveling --- *)
374377

@@ -380,24 +383,24 @@ module Train_update = struct
380383
~ixn:loc ~cur_dir:train.dir ~dest
381384
|> Option.get_exn_or "Cannot find route for train"
382385
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, []
384387

385388
| _ ->
386389
(* All other track and non-decision ixns *)
387390
let dir =
388391
Dir.Set.find_nearest train.dir track.dirs
389392
|> Option.get_exn_or "Cannot find track for train"
390393
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, []
392395

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 =
394397
(* This is the regular update function for trains. Important stuff happens midtile *)
395398
(* let priority = (Goods.freight_to_enum train.freight) * 3 - (Train.train_type_to_enum train.typ) + 2 in *)
396399
begin match train.state with
397400
| Traveling travel_state ->
398401
let train = Train.update_speed train ~cycle ~cycle_check ~cycle_bit in
399402
(* 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 =
401404
let speed = Train.get_speed train in
402405
if speed_bound >= speed then train, ui_msg_acc
403406
else (
@@ -415,7 +418,7 @@ let update_train v idx (train:rw Train.t) ~cycle ~cycle_check ~cycle_bit ~region
415418
|> min Train.update_array_length
416419
in
417420
(* 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 =
419422
if (Train.update_cycle_array.(update_val) land cycle_bit) <> 0 then begin
420423
(* Log.debug (fun f -> f "Pass test. Update val %d, cycle_bit %d" update_val cycle_bit); *)
421424
let is_mid_tile =
@@ -427,40 +430,40 @@ let update_train v idx (train:rw Train.t) ~cycle ~cycle_check ~cycle_bit ~region
427430
(* Make sure we don't double-process mid-tiles *)
428431
match is_mid_tile, travel_state.traveling_past_station with
429432
| true, false ->
430-
_handle_train_mid_tile ~idx ~cycle v train loc
433+
_handle_train_mid_tile ~idx ~cycle v train stations loc
431434
| false, true ->
432435
travel_state.traveling_past_station <- false;
433-
Train.advance train, []
436+
Train.advance train, stations, []
434437
| _ ->
435-
Train.advance train, []
438+
Train.advance train, stations, []
436439
end else
437-
train, []
440+
train, stations, []
438441
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)
440443
)
441444
in
442-
train_update_loop train 0 []
445+
train_update_loop train 0 stations []
443446

444447
| _ -> (* Other train states or time is up *)
445448
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
447450
end
448451

449-
450452
(* Run every cycle, updating every train's position and speed *)
451453
let _update_all_trains (v:t) ~player =
452454
(* Log.debug (fun f -> f "update_all_trains"); *)
453455
let cycle_check, region_div = if Region.is_us v.region then 16, 1 else 8, 2 in
454456
let cycle_bit = 1 lsl (v.cycle mod 12) in
455457
let cycle = v.cycle in
456458
(* 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
460462
in
461-
ui_msgs @ ui_msg_acc, train)
463+
(stations, ui_msgs @ ui_msg_acc), train
464+
)
462465
v.players.(player).trains
463-
~init:[]
466+
~init:(v.stations, [])
464467

465468
end
466469

0 commit comments

Comments
 (0)