Changeset - r27429:1d7757745f42
[Not reviewed]
master
0 9 0
Peter Nelson - 18 months ago 2023-05-23 23:52:44
peter1138@openttd.org
Codechange: Add IsCargoAccepted/Produced() helpers.
9 files changed with 40 insertions and 65 deletions:
0 comments (0 inline, 0 general)
src/industry.h
Show inline comments
 
@@ -131,6 +131,28 @@ struct Industry : IndustryPool::PoolItem
 
		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
src/industry_cmd.cpp
Show inline comments
 
@@ -460,16 +460,8 @@ static void AddAcceptedCargo_Industry(Ti
 
		/* 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(Carg
 
	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;
 
}
 

	
 
/**
src/industry_gui.cpp
Show inline comments
 
@@ -1243,14 +1243,11 @@ static bool CDECL CargoFilter(const Indu
 
			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 Indu
 
			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;
 
	}
 

	
src/newgrf_industries.cpp
Show inline comments
 
@@ -680,7 +680,7 @@ void IndustryProductionCallback(Industry
 
 */
 
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)) {
src/script/api/script_industry.cpp
Show inline comments
 
@@ -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)
src/script/api/script_industrylist.cpp
Show inline comments
 
@@ -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);
 
	}
 
}
src/script/api/script_tilelist.cpp
Show inline comments
 
@@ -83,13 +83,7 @@ ScriptTileList_IndustryAccepting::Script
 
	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::Script
 
	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;
 

	
src/station.cpp
Show inline comments
 
@@ -405,11 +405,7 @@ void Station::AddIndustryToDeliver(Indus
 
	}
 

	
 
	/* 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});
 
}
src/subsidy.cpp
Show inline comments
 
@@ -452,8 +452,7 @@ bool FindSubsidyCargoDestination(CargoID
 
			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;
0 comments (0 inline, 0 general)