Change: Reorganise industry accept/produce arrays. (#10853)

Use a array of struct for each cargo instead of an array for each statistic.
This makes iterating for acceptance and production much simpler.
pct_transported is now calculated when needed.
This commit is contained in:
PeterN
2023-05-25 21:25:46 +01:00
committed by GitHub
parent db3b086a52
commit 584faaf064
16 changed files with 527 additions and 355 deletions

View File

@@ -29,10 +29,9 @@ ScriptCargoList_IndustryAccepting::ScriptCargoList_IndustryAccepting(IndustryID
if (!ScriptIndustry::IsValidIndustry(industry_id)) return;
Industry *ind = ::Industry::Get(industry_id);
for (uint i = 0; i < lengthof(ind->accepts_cargo); i++) {
CargoID cargo_id = ind->accepts_cargo[i];
if (::IsValidCargoID(cargo_id)) {
this->AddItem(cargo_id);
for (const auto &a : ind->accepted) {
if (::IsValidCargoID(a.cargo)) {
this->AddItem(a.cargo);
}
}
}
@@ -42,10 +41,9 @@ ScriptCargoList_IndustryProducing::ScriptCargoList_IndustryProducing(IndustryID
if (!ScriptIndustry::IsValidIndustry(industry_id)) return;
Industry *ind = ::Industry::Get(industry_id);
for (uint i = 0; i < lengthof(ind->produced_cargo); i++) {
CargoID cargo_id = ind->produced_cargo[i];
if (::IsValidCargoID(cargo_id)) {
this->AddItem(cargo_id);
for (const auto &p : ind->produced) {
if (::IsValidCargoID(p.cargo)) {
this->AddItem(p.cargo);
}
}
}

View File

@@ -80,10 +80,10 @@
Industry *i = ::Industry::Get(industry_id);
int j = i->GetCargoAcceptedIndex(cargo_id);
if (j < 0) return -1;
auto it = i->GetCargoAccepted(cargo_id);
if (it == std::end(i->accepted)) return -1;
return i->incoming_cargo_waiting[j];
return it->waiting;
}
/* static */ SQInteger ScriptIndustry::GetLastMonthProduction(IndustryID industry_id, CargoID cargo_id)
@@ -91,12 +91,12 @@
if (!IsValidIndustry(industry_id)) return -1;
if (!ScriptCargo::IsValidCargo(cargo_id)) return -1;
const Industry *i = ::Industry::Get(industry_id);
Industry *i = ::Industry::Get(industry_id);
int j = i->GetCargoProducedIndex(cargo_id);
if (j < 0) return -1;
auto it = i->GetCargoProduced(cargo_id);
if (it == std::end(i->produced)) return -1;
return i->last_month_production[j];
return it->history[LAST_MONTH].production;
}
/* static */ SQInteger ScriptIndustry::GetLastMonthTransported(IndustryID industry_id, CargoID cargo_id)
@@ -104,12 +104,12 @@
if (!IsValidIndustry(industry_id)) return -1;
if (!ScriptCargo::IsValidCargo(cargo_id)) return -1;
const Industry *i = ::Industry::Get(industry_id);
Industry *i = ::Industry::Get(industry_id);
int j = i->GetCargoProducedIndex(cargo_id);
if (j < 0) return -1;
auto it = i->GetCargoProduced(cargo_id);
if (it == std::end(i->produced)) return -1;
return i->last_month_transported[j];
return it->history[LAST_MONTH].transported;
}
/* static */ SQInteger ScriptIndustry::GetLastMonthTransportedPercentage(IndustryID industry_id, CargoID cargo_id)
@@ -117,12 +117,12 @@
if (!IsValidIndustry(industry_id)) return -1;
if (!ScriptCargo::IsValidCargo(cargo_id)) return -1;
const Industry *i = ::Industry::Get(industry_id);
Industry *i = ::Industry::Get(industry_id);
int j = i->GetCargoProducedIndex(cargo_id);
if (j < 0) return -1;
auto it = i->GetCargoProduced(cargo_id);
if (it == std::end(i->produced)) return -1;
return ::ToPercent8(i->last_month_pct_transported[j]);
return ::ToPercent8(it->history[LAST_MONTH].PctTransported());
}
/* static */ TileIndex ScriptIndustry::GetLocation(IndustryID industry_id)
@@ -225,11 +225,12 @@
if (i == nullptr) return ScriptDate::DATE_INVALID;
if (!::IsValidCargoID(cargo_type)) {
return (ScriptDate::Date)std::accumulate(std::begin(i->last_cargo_accepted_at), std::end(i->last_cargo_accepted_at), 0, [](TimerGameCalendar::Date a, TimerGameCalendar::Date b) { return std::max(a, b); });
auto it = std::max_element(std::begin(i->accepted), std::end(i->accepted), [](const auto &a, const auto &b) { return a.last_accepted < b.last_accepted; });
return (ScriptDate::Date)it->last_accepted;
} else {
int index = i->GetCargoAcceptedIndex(cargo_type);
if (index < 0) return ScriptDate::DATE_INVALID;
return (ScriptDate::Date)i->last_cargo_accepted_at[index];
auto it = i->GetCargoAccepted(cargo_type);
if (it == std::end(i->accepted)) return ScriptDate::DATE_INVALID;
return (ScriptDate::Date)it->last_accepted;
}
}

View File

@@ -95,13 +95,7 @@ ScriptTileList_IndustryAccepting::ScriptTileList_IndustryAccepting(IndustryID in
/* Only add the tile if it accepts the cargo (sometimes just 1 tile of an
* industry triggers the acceptance). */
CargoArray acceptance = ::GetAcceptanceAroundTiles(cur_tile, 1, 1, radius);
{
bool cargo_accepts = false;
for (byte j = 0; j < lengthof(i->accepts_cargo); j++) {
if (::IsValidCargoID(i->accepts_cargo[j]) && acceptance[i->accepts_cargo[j]] != 0) cargo_accepts = true;
}
if (!cargo_accepts) continue;
}
if (std::none_of(std::begin(i->accepted), std::end(i->accepted), [&acceptance](const auto &a) { return ::IsValidCargoID(a.cargo) && acceptance[a.cargo] != 0; })) continue;
this->AddTile(cur_tile);
}