Change station cargo history storage format

Use uint16 to avoid truncation issues
Don't reserve memory for unused cargoes
Store history as ring buffer
Update history graph immediately on storage date
Show total waiting cargo
This commit is contained in:
Jonathan G Rennison
2021-06-29 18:09:49 +01:00
parent f901da344e
commit 5698507d0b
7 changed files with 57 additions and 38 deletions

View File

@@ -409,16 +409,26 @@ void Station::GetTileArea(TileArea *ta, StationType type) const
*/
void Station::UpdateCargoHistory()
{
const CargoSpec* cs;
FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs) {
auto amount = this->goods[cs->Index()].cargo.AvailableCount();
std::rotate(std::begin(this->station_cargo_history) + cs->Index() * MAX_STATION_CARGO_HISTORY_DAYS,
std::begin(this->station_cargo_history) + cs->Index() * MAX_STATION_CARGO_HISTORY_DAYS + 1,
std::begin(this->station_cargo_history) + (cs->Index() + 1) * MAX_STATION_CARGO_HISTORY_DAYS);
this->station_cargo_history[(cs->Index() + 1) * MAX_STATION_CARGO_HISTORY_DAYS - 1] = std::clamp(amount / STATION_CARGO_HISTORY_FACTOR, (uint)0, (uint)UINT8_MAX);
uint storage_offset = 0;
bool update_window = false;
for (const CargoSpec *cs : CargoSpec::Iterate()) {
uint amount = this->goods[cs->Index()].cargo.TotalCount();
if (!HasBit(this->station_cargo_history_cargoes, cs->Index())) {
if (amount == 0) {
/* No cargo present, and no history stored for this cargo, no work to do */
continue;
} else {
if (this->station_cargo_history_cargoes == 0) update_window = true;
SetBit(this->station_cargo_history_cargoes, cs->Index());
this->station_cargo_history.emplace(this->station_cargo_history.begin() + storage_offset);
}
}
this->station_cargo_history[storage_offset][this->station_cargo_history_offset] = static_cast<uint16>(std::clamp<uint>(amount, (uint)0, (uint)UINT16_MAX));
storage_offset++;
}
this->station_cargo_history_offset++;
if (this->station_cargo_history_offset == MAX_STATION_CARGO_HISTORY_DAYS) this->station_cargo_history_offset = 0;
if (update_window) InvalidateWindowData(WC_STATION_VIEW, this->index, -1);
}
/**
@@ -3788,7 +3798,7 @@ bool GetNewGrfRating(const Station *st, const CargoSpec *cs, const GoodsEntry *g
{
*new_grf_rating = 0;
bool is_using_newgrf_rating = false;
/* Perform custom station rating. If it succeeds the speed, days in transit and
* waiting cargo ratings must not be executed. */
@@ -3822,7 +3832,7 @@ int GetSpeedRating(const GoodsEntry *ge)
int GetWaitTimeRating(const CargoSpec *cs, const GoodsEntry *ge)
{
int rating = 0;
uint wait_time = ge->time_since_pickup;
if (_settings_game.station.cargo_class_rating_wait_time) {
@@ -3895,7 +3905,7 @@ int GetTargetRating(const Station *st, const CargoSpec *cs, const GoodsEntry *ge
} else if (HasBit(cs->callback_mask, CBM_CARGO_STATION_RATING_CALC)) {
int new_grf_rating;
if (GetNewGrfRating(st, cs, ge, &new_grf_rating)) {
skip = true;
rating += new_grf_rating;
@@ -4265,6 +4275,7 @@ void StationDailyLoop()
for (Station *st : Station::Iterate()) {
st->UpdateCargoHistory();
}
InvalidateWindowClassesData(WC_STATION_CARGO);
}
}