diff --git a/src/industry.h b/src/industry.h index b3879b1534..b6d0ee6e44 100644 --- a/src/industry.h +++ b/src/industry.h @@ -131,6 +131,28 @@ struct Industry : IndustryPool::PoolItem<&_industry_pool> { return pos - this->accepts_cargo; } + /** Test if this industry accepts any cargo. + * @return true iff the industry accepts any cargo. + */ + bool IsCargoAccepted() const { return std::any_of(std::begin(this->accepts_cargo), std::end(this->accepts_cargo), [](const auto &cargo) { return IsValidCargoID(cargo); }); } + + /** Test if this industry produces any cargo. + * @return true iff the industry produces any cargo. + */ + bool IsCargoProduced() const { return std::any_of(std::begin(this->produced_cargo), std::end(this->produced_cargo), [](const auto &cargo) { return IsValidCargoID(cargo); }); } + + /** Test if this industry accepts a specific cargo. + * @param cargo Cargo type to test. + * @return true iff the industry accepts the given cargo type. + */ + bool IsCargoAccepted(CargoID cargo) const { return std::any_of(std::begin(this->accepts_cargo), std::end(this->accepts_cargo), [&cargo](const auto &cid) { return cid == cargo; }); } + + /** Test if this industry produces a specific cargo. + * @param cargo Cargo type to test. + * @return true iff the industry produces the given cargo types. + */ + bool IsCargoProduced(CargoID cargo) const { return std::any_of(std::begin(this->produced_cargo), std::end(this->produced_cargo), [&cargo](const auto &cid) { return cid == cargo; }); } + /** * Get the industry of the given tile * @param tile the tile to get the industry from diff --git a/src/industry_cmd.cpp b/src/industry_cmd.cpp index ed8873acab..ac44a52765 100644 --- a/src/industry_cmd.cpp +++ b/src/industry_cmd.cpp @@ -460,16 +460,8 @@ static void AddAcceptedCargo_Industry(TileIndex tile, CargoArray &acceptance, Ca /* Maybe set 'always accepted' bit (if it's not set already) */ if (HasBit(*always_accepted, a)) continue; - bool accepts = false; - for (uint cargo_index = 0; cargo_index < lengthof(ind->accepts_cargo); cargo_index++) { - /* Test whether the industry itself accepts the cargo type */ - if (ind->accepts_cargo[cargo_index] == a) { - accepts = true; - break; - } - } - - if (accepts) continue; + /* Test whether the industry itself accepts the cargo type */ + if (ind->IsCargoAccepted(a)) continue; /* If the industry itself doesn't accept this cargo, set 'always accepted' bit */ SetBit(*always_accepted, a); @@ -2625,20 +2617,10 @@ static void CanCargoServiceIndustry(CargoID cargo, Industry *ind, bool *c_accept if (!IsValidCargoID(cargo)) return; /* Check for acceptance of cargo */ - for (byte j = 0; j < lengthof(ind->accepts_cargo); j++) { - if (cargo == ind->accepts_cargo[j] && !IndustryTemporarilyRefusesCargo(ind, cargo)) { - *c_accepts = true; - break; - } - } + if (ind->IsCargoAccepted(cargo) && !IndustryTemporarilyRefusesCargo(ind, cargo)) *c_accepts = true; /* Check for produced cargo */ - for (byte j = 0; j < lengthof(ind->produced_cargo); j++) { - if (cargo == ind->produced_cargo[j]) { - *c_produces = true; - break; - } - } + if (ind->IsCargoProduced(cargo)) *c_produces = true; } /** diff --git a/src/industry_gui.cpp b/src/industry_gui.cpp index 98ce724ad5..2b883afce8 100644 --- a/src/industry_gui.cpp +++ b/src/industry_gui.cpp @@ -1243,14 +1243,11 @@ static bool CDECL CargoFilter(const Industry * const *industry, const std::pair< break; case CF_NONE: - accepted_cargo_matches = std::all_of(std::begin((*industry)->accepts_cargo), std::end((*industry)->accepts_cargo), [](CargoID cargo) { - return !IsValidCargoID(cargo); - }); + accepted_cargo_matches = !(*industry)->IsCargoAccepted(); break; default: - const auto &ac = (*industry)->accepts_cargo; - accepted_cargo_matches = std::find(std::begin(ac), std::end(ac), accepted_cargo) != std::end(ac); + accepted_cargo_matches = (*industry)->IsCargoAccepted(accepted_cargo); break; } @@ -1262,14 +1259,11 @@ static bool CDECL CargoFilter(const Industry * const *industry, const std::pair< break; case CF_NONE: - produced_cargo_matches = std::all_of(std::begin((*industry)->produced_cargo), std::end((*industry)->produced_cargo), [](CargoID cargo) { - return !IsValidCargoID(cargo); - }); + produced_cargo_matches = !(*industry)->IsCargoProduced(); break; default: - const auto &pc = (*industry)->produced_cargo; - produced_cargo_matches = std::find(std::begin(pc), std::end(pc), produced_cargo) != std::end(pc); + produced_cargo_matches = (*industry)->IsCargoProduced(produced_cargo); break; } diff --git a/src/newgrf_industries.cpp b/src/newgrf_industries.cpp index fe0afcdf66..688011bac6 100644 --- a/src/newgrf_industries.cpp +++ b/src/newgrf_industries.cpp @@ -680,7 +680,7 @@ void IndustryProductionCallback(Industry *ind, int reason) */ bool IndustryTemporarilyRefusesCargo(Industry *ind, CargoID cargo_type) { - assert(std::find(ind->accepts_cargo, endof(ind->accepts_cargo), cargo_type) != endof(ind->accepts_cargo)); + assert(ind->IsCargoAccepted(cargo_type)); const IndustrySpec *indspec = GetIndustrySpec(ind->type); if (HasBit(indspec->callback_mask, CBM_IND_REFUSE_CARGO)) { diff --git a/src/script/api/script_industry.cpp b/src/script/api/script_industry.cpp index 8b9dfb332c..bc750839f0 100644 --- a/src/script/api/script_industry.cpp +++ b/src/script/api/script_industry.cpp @@ -67,14 +67,10 @@ Industry *i = ::Industry::Get(industry_id); - for (byte j = 0; j < lengthof(i->accepts_cargo); j++) { - if (i->accepts_cargo[j] == cargo_id) { - if (IndustryTemporarilyRefusesCargo(i, cargo_id)) return CAS_TEMP_REFUSED; - return CAS_ACCEPTED; - } - } + if (!i->IsCargoAccepted(cargo_id)) return CAS_NOT_ACCEPTED; + if (IndustryTemporarilyRefusesCargo(i, cargo_id)) return CAS_TEMP_REFUSED; - return CAS_NOT_ACCEPTED; + return CAS_ACCEPTED; } /* static */ SQInteger ScriptIndustry::GetStockpiledCargo(IndustryID industry_id, CargoID cargo_id) diff --git a/src/script/api/script_industrylist.cpp b/src/script/api/script_industrylist.cpp index de28f418d4..0cc1cde454 100644 --- a/src/script/api/script_industrylist.cpp +++ b/src/script/api/script_industrylist.cpp @@ -23,17 +23,13 @@ ScriptIndustryList::ScriptIndustryList() ScriptIndustryList_CargoAccepting::ScriptIndustryList_CargoAccepting(CargoID cargo_id) { for (const Industry *i : Industry::Iterate()) { - for (byte j = 0; j < lengthof(i->accepts_cargo); j++) { - if (i->accepts_cargo[j] == cargo_id) this->AddItem(i->index); - } + if (i->IsCargoAccepted(cargo_id)) this->AddItem(i->index); } } ScriptIndustryList_CargoProducing::ScriptIndustryList_CargoProducing(CargoID cargo_id) { for (const Industry *i : Industry::Iterate()) { - for (byte j = 0; j < lengthof(i->produced_cargo); j++) { - if (i->produced_cargo[j] == cargo_id) this->AddItem(i->index); - } + if (i->IsCargoProduced(cargo_id)) this->AddItem(i->index); } } diff --git a/src/script/api/script_tilelist.cpp b/src/script/api/script_tilelist.cpp index ac0b306739..8dd830efb5 100644 --- a/src/script/api/script_tilelist.cpp +++ b/src/script/api/script_tilelist.cpp @@ -83,13 +83,7 @@ ScriptTileList_IndustryAccepting::ScriptTileList_IndustryAccepting(IndustryID in if (i->neutral_station != nullptr && !_settings_game.station.serve_neutral_industries) return; /* Check if this industry accepts anything */ - { - bool cargo_accepts = false; - for (byte j = 0; j < lengthof(i->accepts_cargo); j++) { - if (::IsValidCargoID(i->accepts_cargo[j])) cargo_accepts = true; - } - if (!cargo_accepts) return; - } + if (!i->IsCargoAccepted()) return; if (!_settings_game.station.modified_catchment) radius = CA_UNMODIFIED; @@ -123,11 +117,7 @@ ScriptTileList_IndustryProducing::ScriptTileList_IndustryProducing(IndustryID in if (i->neutral_station != nullptr && !_settings_game.station.serve_neutral_industries) return; /* Check if this industry produces anything */ - bool cargo_produces = false; - for (byte j = 0; j < lengthof(i->produced_cargo); j++) { - if (::IsValidCargoID(i->produced_cargo[j])) cargo_produces = true; - } - if (!cargo_produces) return; + if (!i->IsCargoProduced()) return; if (!_settings_game.station.modified_catchment) radius = CA_UNMODIFIED; diff --git a/src/station.cpp b/src/station.cpp index 6fc7b51b69..590abc976e 100644 --- a/src/station.cpp +++ b/src/station.cpp @@ -405,11 +405,7 @@ void Station::AddIndustryToDeliver(Industry *ind, TileIndex tile) } /* Include only industries that can accept cargo */ - uint cargo_index; - for (cargo_index = 0; cargo_index < lengthof(ind->accepts_cargo); cargo_index++) { - if (IsValidCargoID(ind->accepts_cargo[cargo_index])) break; - } - if (cargo_index >= lengthof(ind->accepts_cargo)) return; + if (!ind->IsCargoAccepted()) return; this->industries_near.insert(IndustryListEntry{distance, ind}); } diff --git a/src/subsidy.cpp b/src/subsidy.cpp index 14d1829d58..634194b88e 100644 --- a/src/subsidy.cpp +++ b/src/subsidy.cpp @@ -452,8 +452,7 @@ bool FindSubsidyCargoDestination(CargoID cid, SourceType src_type, SourceID src) if (dst_ind == nullptr) return false; /* The industry must accept the cargo */ - bool valid = std::find(dst_ind->accepts_cargo, endof(dst_ind->accepts_cargo), cid) != endof(dst_ind->accepts_cargo); - if (!valid) return false; + if (!dst_ind->IsCargoAccepted(cid)) return false; dst = dst_ind->index; break;