Codechange: Use iterators and/or range-for on cargo related loops.

This commit is contained in:
Peter Nelson
2023-10-18 20:49:01 +01:00
committed by Peter Nelson
parent 2a88e0fab3
commit 9602de474d
16 changed files with 79 additions and 89 deletions

View File

@@ -1690,9 +1690,9 @@ bool AfterLoadGame()
if (IsSavegameVersionBefore(SLV_74)) {
for (Station *st : Station::Iterate()) {
for (CargoID c = 0; c < NUM_CARGO; c++) {
st->goods[c].last_speed = 0;
if (st->goods[c].cargo.AvailableCount() != 0) SetBit(st->goods[c].status, GoodsEntry::GES_RATING);
for (GoodsEntry &ge : st->goods) {
ge.last_speed = 0;
if (ge.cargo.AvailableCount() != 0) SetBit(ge.status, GoodsEntry::GES_RATING);
}
}
}

View File

@@ -43,10 +43,8 @@
* information is lost. In that case we set it to the position of this
* station */
for (Station *st : Station::Iterate()) {
for (CargoID c = 0; c < NUM_CARGO; c++) {
GoodsEntry *ge = &st->goods[c];
const StationCargoPacketMap *packets = ge->cargo.Packets();
for (GoodsEntry &ge : st->goods) {
const StationCargoPacketMap *packets = ge.cargo.Packets();
for (StationCargoList::ConstIterator it(packets->begin()); it != packets->end(); it++) {
CargoPacket *cp = *it;
cp->source_xy = Station::IsValidID(cp->first_station) ? Station::Get(cp->first_station)->xy : st->xy;
@@ -69,7 +67,7 @@
for (Vehicle *v : Vehicle::Iterate()) v->cargo.InvalidateCache();
for (Station *st : Station::Iterate()) {
for (CargoID c = 0; c < NUM_CARGO; c++) st->goods[c].cargo.InvalidateCache();
for (GoodsEntry &ge : st->goods) ge.cargo.InvalidateCache();
}
}
@@ -81,9 +79,8 @@
if (IsSavegameVersionBefore(SLV_CARGO_TRAVELLED)) {
/* Update the cargo-traveled in stations as if they arrived from the source tile. */
for (Station *st : Station::Iterate()) {
for (size_t i = 0; i < NUM_CARGO; i++) {
GoodsEntry *ge = &st->goods[i];
for (auto it = ge->cargo.Packets()->begin(); it != ge->cargo.Packets()->end(); it++) {
for (GoodsEntry &ge : st->goods) {
for (auto it = ge.cargo.Packets()->begin(); it != ge.cargo.Packets()->end(); ++it) {
for (CargoPacket *cp : it->second) {
if (cp->source_xy != INVALID_TILE && cp->source_xy != st->xy) {
cp->travelled.x = TileX(cp->source_xy) - TileX(st->xy);

View File

@@ -412,8 +412,8 @@ public:
SlSetStructListLength(NUM_CARGO);
for (CargoID i = 0; i < NUM_CARGO; i++) {
SlObject(&st->goods[i], this->GetDescription());
for (GoodsEntry &ge : st->goods) {
SlObject(&ge, this->GetDescription());
}
}
@@ -429,15 +429,15 @@ public:
std::copy(std::begin(_old_st_persistent_storage.storage), std::end(_old_st_persistent_storage.storage), std::begin(st->airport.psa->storage));
}
size_t num_cargo = this->GetNumCargo();
for (size_t i = 0; i < num_cargo; i++) {
GoodsEntry *ge = &st->goods[i];
SlObject(ge, this->GetLoadDescription());
auto end = std::next(std::begin(st->goods), std::min(this->GetNumCargo(), std::size(st->goods)));
for (auto it = std::begin(st->goods); it != end; ++it) {
GoodsEntry &ge = *it;
SlObject(&ge, this->GetLoadDescription());
if (IsSavegameVersionBefore(SLV_183)) {
SwapPackets(ge);
SwapPackets(&ge);
}
if (IsSavegameVersionBefore(SLV_68)) {
SB(ge->status, GoodsEntry::GES_ACCEPTANCE, 1, HasBit(_waiting_acceptance, 15));
SB(ge.status, GoodsEntry::GES_ACCEPTANCE, 1, HasBit(_waiting_acceptance, 15));
if (GB(_waiting_acceptance, 0, 12) != 0) {
/* In old versions, enroute_from used 0xFF as INVALID_STATION */
StationID source = (IsSavegameVersionBefore(SLV_7) && _cargo_source == 0xFF) ? INVALID_STATION : _cargo_source;
@@ -450,8 +450,8 @@ public:
/* Don't construct the packet with station here, because that'll fail with old savegames */
CargoPacket *cp = new CargoPacket(GB(_waiting_acceptance, 0, 12), _cargo_periods, source, _cargo_source_xy, _cargo_feeder_share);
ge->cargo.Append(cp, INVALID_STATION);
SB(ge->status, GoodsEntry::GES_RATING, 1, 1);
ge.cargo.Append(cp, INVALID_STATION);
SB(ge.status, GoodsEntry::GES_RATING, 1, 1);
}
}
}
@@ -461,15 +461,16 @@ public:
{
Station *st = Station::From(bst);
uint num_cargo = IsSavegameVersionBefore(SLV_55) ? 12 : IsSavegameVersionBefore(SLV_EXTEND_CARGOTYPES) ? 32 : NUM_CARGO;
for (CargoID i = 0; i < num_cargo; i++) {
GoodsEntry *ge = &st->goods[i];
size_t num_cargo = IsSavegameVersionBefore(SLV_55) ? 12 : IsSavegameVersionBefore(SLV_EXTEND_CARGOTYPES) ? 32 : NUM_CARGO;
auto end = std::next(std::begin(st->goods), std::min(num_cargo, std::size(st->goods)));
for (auto it = std::begin(st->goods); it != end; ++it) {
GoodsEntry &ge = *it;
if (IsSavegameVersionBefore(SLV_183)) {
SwapPackets(ge); // We have to swap back again to be in the format pre-183 expects.
SlObject(ge, this->GetDescription());
SwapPackets(ge);
SwapPackets(&ge); // We have to swap back again to be in the format pre-183 expects.
SlObject(&ge, this->GetDescription());
SwapPackets(&ge);
} else {
SlObject(ge, this->GetDescription());
SlObject(&ge, this->GetDescription());
}
}
}

View File

@@ -139,9 +139,9 @@ public:
void Save(Town *t) const override
{
SlSetStructListLength(NUM_CARGO);
for (CargoID i = 0; i < NUM_CARGO; i++) {
SlObject(&t->supplied[i], this->GetDescription());
SlSetStructListLength(std::size(t->supplied));
for (auto &supplied : t->supplied) {
SlObject(&supplied, this->GetDescription());
}
}
@@ -166,9 +166,9 @@ public:
void Save(Town *t) const override
{
SlSetStructListLength(NUM_TE);
for (size_t i = TE_BEGIN; i < TE_END; i++) {
SlObject(&t->received[i], this->GetDescription());
SlSetStructListLength(std::size(t->received));
for (auto &received : t->received) {
SlObject(&received, this->GetDescription());
}
}