From 398c7e5f9d9ff8c786cac8fe0134ae2e4596dbab Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Thu, 21 Sep 2023 08:38:46 +0100 Subject: [PATCH] Codechange: Use new function to get a bitmask of empty cargo types. --- src/newgrf_roadstop.cpp | 11 ++--------- src/newgrf_station.cpp | 11 ++--------- src/station_cmd.cpp | 15 +++++++++++++++ src/station_func.h | 1 + 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/newgrf_roadstop.cpp b/src/newgrf_roadstop.cpp index 983bc0d06e..1055ea84bb 100644 --- a/src/newgrf_roadstop.cpp +++ b/src/newgrf_roadstop.cpp @@ -385,15 +385,8 @@ void TriggerRoadStopRandomisation(Station *st, TileIndex tile, RoadStopRandomTri uint32_t whole_reseed = 0; - CargoTypes empty_mask = 0; - if (trigger == RSRT_CARGO_TAKEN) { - /* Create a bitmask of completely empty cargo types to be matched */ - for (CargoID i = 0; i < NUM_CARGO; i++) { - if (st->goods[i].cargo.TotalCount() == 0) { - SetBit(empty_mask, i); - } - } - } + /* Bitmask of completely empty cargo types to be matched. */ + CargoTypes empty_mask = (trigger == RSRT_CARGO_TAKEN) ? GetEmptyMask(st) : 0; uint32_t used_triggers = 0; auto process_tile = [&](TileIndex cur_tile) { diff --git a/src/newgrf_station.cpp b/src/newgrf_station.cpp index 16106dc1c3..5fb5ceb752 100644 --- a/src/newgrf_station.cpp +++ b/src/newgrf_station.cpp @@ -962,15 +962,8 @@ void TriggerStationRandomisation(Station *st, TileIndex trigger_tile, StationRan uint32_t whole_reseed = 0; ETileArea area = ETileArea(st, trigger_tile, tas[trigger]); - CargoTypes empty_mask = 0; - if (trigger == SRT_CARGO_TAKEN) { - /* Create a bitmask of completely empty cargo types to be matched */ - for (CargoID i = 0; i < NUM_CARGO; i++) { - if (st->goods[i].cargo.TotalCount() == 0) { - SetBit(empty_mask, i); - } - } - } + /* Bitmask of completely empty cargo types to be matched. */ + CargoTypes empty_mask = (trigger == SRT_CARGO_TAKEN) ? GetEmptyMask(st) : 0; /* Store triggers now for var 5F */ SetBit(st->waiting_triggers, trigger); diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp index feb783bd74..12c30a33b8 100644 --- a/src/station_cmd.cpp +++ b/src/station_cmd.cpp @@ -502,6 +502,21 @@ CargoTypes GetAcceptanceMask(const Station *st) return mask; } +/** + * Get a mask of the cargo types that are empty at the station. + * @param st Station to query + * @return the empty mask + */ +CargoTypes GetEmptyMask(const Station *st) +{ + CargoTypes mask = 0; + + for (CargoID i = 0; i < NUM_CARGO; i++) { + if (st->goods[i].cargo.TotalCount() == 0) SetBit(mask, i); + } + return mask; +} + /** * Items contains the two cargo names that are to be accepted or rejected. * msg is the string id of the message to display. diff --git a/src/station_func.h b/src/station_func.h index ad3b60b7ae..8e72a945f7 100644 --- a/src/station_func.h +++ b/src/station_func.h @@ -31,6 +31,7 @@ CargoArray GetAcceptanceAroundTiles(TileIndex tile, int w, int h, int rad, Cargo void UpdateStationAcceptance(Station *st, bool show_msg); CargoTypes GetAcceptanceMask(const Station *st); +CargoTypes GetEmptyMask(const Station *st); const DrawTileSprites *GetStationTileLayout(StationType st, byte gfx); void StationPickerDrawSprite(int x, int y, StationType st, RailType railtype, RoadType roadtype, int image);