Codechange: Add IsCargoAccepted/Produced() helpers.

pull/564/head
Peter Nelson 1 year ago committed by PeterN
parent 633f19419d
commit 09408e8e46

@ -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

@ -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;
}
/**

@ -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;
}

@ -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)) {

@ -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)

@ -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);
}
}

@ -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;

@ -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});
}

@ -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;

Loading…
Cancel
Save