From d8a1be48cd60c690235de175e9a044b95f92ea28 Mon Sep 17 00:00:00 2001 From: glx Date: Tue, 17 Dec 2019 03:37:43 +0100 Subject: Codechange: Replace vehicle related FOR_ALL with range-based for loops --- src/aircraft.h | 5 -- src/aircraft_cmd.cpp | 3 +- src/company_cmd.cpp | 3 +- src/date.cpp | 4 +- src/disaster_vehicle.cpp | 24 ++++----- src/disaster_vehicle.h | 6 --- src/economy.cpp | 12 ++--- src/effectvehicle_base.h | 6 --- src/elrail.cpp | 5 +- src/engine.cpp | 9 ++-- src/group_cmd.cpp | 17 ++---- src/industry_cmd.cpp | 3 +- src/network/network_server.cpp | 7 +-- src/openttd.cpp | 5 +- src/order_cmd.cpp | 4 +- src/road.cpp | 3 +- src/road_cmd.cpp | 6 +-- src/roadveh.h | 2 - src/saveload/afterload.cpp | 98 ++++++++++++----------------------- src/saveload/cargopacket_sl.cpp | 9 ++-- src/saveload/oldloader_sl.cpp | 7 +-- src/saveload/station_sl.cpp | 3 +- src/saveload/vehicle_sl.cpp | 57 ++++++++------------ src/saveload/waypoint_sl.cpp | 3 +- src/script/api/script_group.cpp | 6 +-- src/script/api/script_vehiclelist.cpp | 15 ++---- src/settings.cpp | 27 ++++------ src/ship.h | 6 --- src/smallmap_gui.cpp | 3 +- src/station.cpp | 6 +-- src/station_cmd.cpp | 12 ++--- src/toolbar_gui.cpp | 3 +- src/train.h | 2 - src/train_cmd.cpp | 12 ++--- src/train_gui.cpp | 3 +- src/vehicle.cpp | 27 ++++------ src/vehicle_base.h | 27 +++------- src/vehicle_cmd.cpp | 4 +- src/vehiclelist.cpp | 18 +++---- 39 files changed, 155 insertions(+), 317 deletions(-) (limited to 'src') diff --git a/src/aircraft.h b/src/aircraft.h index 4acf0da3d..d201743a6 100644 --- a/src/aircraft.h +++ b/src/aircraft.h @@ -136,11 +136,6 @@ struct Aircraft FINAL : public SpecializedVehicle { } }; -/** - * Macro for iterating over all aircraft. - */ -#define FOR_ALL_AIRCRAFT(var) FOR_ALL_VEHICLES_OF_TYPE(Aircraft, var) - void GetRotorImage(const Aircraft *v, EngineImageType image_type, VehicleSpriteSeq *result); Station *GetTargetAirportIfValid(const Aircraft *v); diff --git a/src/aircraft_cmd.cpp b/src/aircraft_cmd.cpp index 262834344..e13658d6f 100644 --- a/src/aircraft_cmd.cpp +++ b/src/aircraft_cmd.cpp @@ -2113,8 +2113,7 @@ void UpdateAirplanesOnNewStation(const Station *st) const AirportFTAClass *ap = st->airport.GetFTA(); Direction rotation = st->airport.tile == INVALID_TILE ? DIR_N : st->airport.rotation; - Aircraft *v; - FOR_ALL_AIRCRAFT(v) { + for (Aircraft *v : Aircraft::Iterate()) { if (!v->IsNormalAircraft() || v->targetairport != st->index) continue; assert(v->state == FLYING); diff --git a/src/company_cmd.cpp b/src/company_cmd.cpp index 4fa8eced2..6308c1447 100644 --- a/src/company_cmd.cpp +++ b/src/company_cmd.cpp @@ -1025,8 +1025,7 @@ CommandCost CmdSetCompanyColour(TileIndex tile, DoCommandFlag flags, uint32 p1, InvalidateWindowData(WC_SMALLMAP, 0, 1); /* Company colour data is indirectly cached. */ - Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { if (v->owner == _current_company) v->InvalidateNewGRFCache(); } diff --git a/src/date.cpp b/src/date.cpp index 778688720..c45396efe 100644 --- a/src/date.cpp +++ b/src/date.cpp @@ -202,14 +202,12 @@ static void OnNewYear() ShowEndGameChart(); /* check if we reached the maximum year, decrement dates by a year */ } else if (_cur_year == MAX_YEAR + 1) { - Vehicle *v; int days_this_year; _cur_year--; days_this_year = IsLeapYear(_cur_year) ? DAYS_IN_LEAP_YEAR : DAYS_IN_YEAR; _date -= days_this_year; - FOR_ALL_VEHICLES(v) v->date_of_last_service -= days_this_year; - + for (Vehicle *v : Vehicle::Iterate()) v->date_of_last_service -= days_this_year; for (LinkGraph *lg : LinkGraph::Iterate()) lg->ShiftDates(-days_this_year); /* Because the _date wraps here, and text-messages expire by game-days, we have to clean out diff --git a/src/disaster_vehicle.cpp b/src/disaster_vehicle.cpp index a76b9a3cf..1244f99ab 100644 --- a/src/disaster_vehicle.cpp +++ b/src/disaster_vehicle.cpp @@ -331,8 +331,7 @@ static bool DisasterTick_Ufo(DisasterVehicle *v) v->current_order.SetDestination(1); uint n = 0; // Total number of targetable road vehicles. - RoadVehicle *u; - FOR_ALL_ROADVEHICLES(u) { + for (const RoadVehicle *u : RoadVehicle::Iterate()) { if (u->IsFrontEngine()) n++; } @@ -343,14 +342,16 @@ static bool DisasterTick_Ufo(DisasterVehicle *v) } n = RandomRange(n); // Choose one of them. - FOR_ALL_ROADVEHICLES(u) { + for (const RoadVehicle *u : RoadVehicle::Iterate()) { /* Find (n+1)-th road vehicle. */ - if (u->IsFrontEngine() && (n-- == 0)) break; + if (u->IsFrontEngine() && (n-- == 0)) { + /* Target it. */ + v->dest_tile = u->index; + v->age = 0; + break; + } } - /* Target it. */ - v->dest_tile = u->index; - v->age = 0; return true; } else { /* Target a vehicle */ @@ -540,8 +541,7 @@ static bool DisasterTick_Big_Ufo(DisasterVehicle *v) v->current_order.SetDestination(2); - Vehicle *target; - FOR_ALL_VEHICLES(target) { + for (Vehicle *target : Vehicle::Iterate()) { if (target->IsGroundVehicle()) { if (Delta(target->x_pos, v->x_pos) + Delta(target->y_pos, v->y_pos) <= 12 * (int)TILE_SIZE) { target->breakdown_ctr = 5; @@ -937,8 +937,7 @@ void StartupDisasters() */ void ReleaseDisastersTargetingIndustry(IndustryID i) { - DisasterVehicle *v; - FOR_ALL_DISASTERVEHICLES(v) { + for (DisasterVehicle *v : DisasterVehicle::Iterate()) { /* primary disaster vehicles that have chosen target */ if (v->subtype == ST_AIRPLANE || v->subtype == ST_HELICOPTER) { /* if it has chosen target, and it is this industry (yes, dest_tile is IndustryID here), set order to "leaving map peacefully" */ @@ -953,8 +952,7 @@ void ReleaseDisastersTargetingIndustry(IndustryID i) */ void ReleaseDisastersTargetingVehicle(VehicleID vehicle) { - DisasterVehicle *v; - FOR_ALL_DISASTERVEHICLES(v) { + for (DisasterVehicle *v : DisasterVehicle::Iterate()) { /* primary disaster vehicles that have chosen target */ if (v->subtype == ST_SMALL_UFO) { if (v->current_order.GetDestination() != 0 && v->dest_tile == vehicle) { diff --git a/src/disaster_vehicle.h b/src/disaster_vehicle.h index 11d579ed7..dff97a69e 100644 --- a/src/disaster_vehicle.h +++ b/src/disaster_vehicle.h @@ -51,10 +51,4 @@ struct DisasterVehicle FINAL : public SpecializedVehicleowner != owner) continue; if (v->type == VEH_TRAIN || @@ -156,12 +155,11 @@ int UpdateCompanyRatingAndValue(Company *c, bool update) /* Count vehicles */ { - Vehicle *v; Money min_profit = 0; bool min_profit_first = true; uint num = 0; - FOR_ALL_VEHICLES(v) { + for (const Vehicle *v : Vehicle::Iterate()) { if (v->owner != owner) continue; if (IsCompanyBuildableVehicleType(v->type) && v->IsPrimaryVehicle()) { if (v->profit_last_year > 0) num++; // For the vehicle score only count profitable vehicles @@ -390,8 +388,7 @@ void ChangeOwnershipOfCompanyItems(Owner old_owner, Owner new_owner) } { - Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { if (v->owner == old_owner && IsCompanyBuildableVehicleType(v->type)) { if (new_owner == INVALID_OWNER) { if (v->Previous() == nullptr) delete v; @@ -435,8 +432,7 @@ void ChangeOwnershipOfCompanyItems(Owner old_owner, Owner new_owner) old_company->settings.vehicle.servint_ispercent = new_company->settings.vehicle.servint_ispercent; } - Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { if (v->owner == old_owner && IsCompanyBuildableVehicleType(v->type)) { assert(new_owner != INVALID_OWNER); diff --git a/src/effectvehicle_base.h b/src/effectvehicle_base.h index 5356d8b4f..73a49046b 100644 --- a/src/effectvehicle_base.h +++ b/src/effectvehicle_base.h @@ -35,10 +35,4 @@ struct EffectVehicle FINAL : public SpecializedVehiclerailtype == RAILTYPE_ELECTRIC) { /* this railroad vehicle is now compatible only with elrail, * so add there also normal rail compatibility */ @@ -623,7 +622,7 @@ bool SettingsDisableElrail(int32 p1) } /* Fix the total power and acceleration for trains */ - FOR_ALL_TRAINS(t) { + for (Train *t : Train::Iterate()) { /* power and acceleration is cached only for front engines */ if (t->IsFrontEngine()) { t->ConsistChanged(CCF_TRACK); diff --git a/src/engine.cpp b/src/engine.cpp index d0333b8f4..66e68fd3c 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -526,8 +526,7 @@ EngineID EngineOverrideManager::GetID(VehicleType type, uint16 grf_local_id, uin */ bool EngineOverrideManager::ResetToCurrentNewGRFConfig() { - const Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (const Vehicle *v : Vehicle::Iterate()) { if (IsCompanyBuildableVehicleType(v)) return false; } @@ -765,8 +764,7 @@ static CompanyID GetPreviewCompany(Engine *e) c->old_economy[0].performance_history > best_hist) { /* Check whether the company uses similar vehicles */ - Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (const Vehicle *v : Vehicle::Iterate()) { if (v->owner != c->index || v->type != e->type) continue; if (!v->GetEngine()->CanCarryCargo() || !HasBit(cargomask, v->cargo_type)) continue; @@ -900,7 +898,6 @@ CommandCost CmdWantEnginePreview(TileIndex tile, DoCommandFlag flags, uint32 p1, */ static void NewVehicleAvailable(Engine *e) { - Vehicle *v; EngineID index = e->index; /* In case the company didn't build the vehicle during the intro period, @@ -914,7 +911,7 @@ static void NewVehicleAvailable(Engine *e) /* We assume the user did NOT build it.. prove me wrong ;) */ c->block_preview = 20; - FOR_ALL_VEHICLES(v) { + for (const Vehicle *v : Vehicle::Iterate()) { if (v->type == VEH_TRAIN || v->type == VEH_ROAD || v->type == VEH_SHIP || (v->type == VEH_AIRCRAFT && Aircraft::From(v)->IsNormalAircraft())) { if (v->owner == c->index && v->engine_type == index) { diff --git a/src/group_cmd.cpp b/src/group_cmd.cpp index a13cd5f38..497d74d68 100644 --- a/src/group_cmd.cpp +++ b/src/group_cmd.cpp @@ -113,8 +113,7 @@ void GroupStatistics::Clear() g->statistics.Clear(); } - const Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (const Vehicle *v : Vehicle::Iterate()) { if (!v->IsEngineCountable()) continue; GroupStatistics::CountEngine(v, 1); @@ -193,8 +192,7 @@ void GroupStatistics::Clear() g->statistics.ClearProfits(); } - const Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (const Vehicle *v : Vehicle::Iterate()) { if (v->IsPrimaryVehicle() && v->age > VEHICLE_PROFIT_MIN_AGE) GroupStatistics::VehicleReachedProfitAge(v); } } @@ -267,8 +265,7 @@ const Livery *GetParentLivery(const Group *g) void PropagateChildLivery(const Group *g) { /* Company colour data is indirectly cached. */ - Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { if (v->group_id == g->index && (!v->IsGroundVehicle() || v->IsFrontEngine())) { for (Vehicle *u = v; u != nullptr; u = u->Next()) { u->colourmap = PAL_NONE; @@ -573,11 +570,9 @@ CommandCost CmdAddSharedVehicleGroup(TileIndex tile, DoCommandFlag flags, uint32 if (!Group::IsValidID(id_g) || !IsCompanyBuildableVehicleType(type)) return CMD_ERROR; if (flags & DC_EXEC) { - Vehicle *v; - /* Find the first front engine which belong to the group id_g * then add all shared vehicles of this front engine to the group id_g */ - FOR_ALL_VEHICLES(v) { + for (const Vehicle *v : Vehicle::Iterate()) { if (v->type == type && v->IsPrimaryVehicle()) { if (v->group_id != id_g) continue; @@ -613,10 +608,8 @@ CommandCost CmdRemoveAllVehiclesGroup(TileIndex tile, DoCommandFlag flags, uint3 if (g == nullptr || g->owner != _current_company) return CMD_ERROR; if (flags & DC_EXEC) { - Vehicle *v; - /* Find each Vehicle that belongs to the group old_g and add it to the default group */ - FOR_ALL_VEHICLES(v) { + for (const Vehicle *v : Vehicle::Iterate()) { if (v->IsPrimaryVehicle()) { if (v->group_id != old_g) continue; diff --git a/src/industry_cmd.cpp b/src/industry_cmd.cpp index 3bbd22bd0..c6602b087 100644 --- a/src/industry_cmd.cpp +++ b/src/industry_cmd.cpp @@ -2502,9 +2502,8 @@ static int WhoCanServiceIndustry(Industry *ind) { if (ind->stations_near.size() == 0) return 0; // No stations found at all => nobody services - const Vehicle *v; int result = 0; - FOR_ALL_VEHICLES(v) { + for (const Vehicle *v : Vehicle::Iterate()) { /* Is it worthwhile to try this vehicle? */ if (v->owner != _local_company && result != 0) continue; diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp index 42a5ab0fc..f9c9e91f2 100644 --- a/src/network/network_server.cpp +++ b/src/network/network_server.cpp @@ -1534,12 +1534,10 @@ void NetworkSocketHandler::SendCompanyInformation(Packet *p, const Company *c, c */ void NetworkPopulateCompanyStats(NetworkCompanyStats *stats) { - const Vehicle *v; - memset(stats, 0, sizeof(*stats) * MAX_COMPANIES); /* Go through all vehicles and count the type of vehicles */ - FOR_ALL_VEHICLES(v) { + for (const Vehicle *v : Vehicle::Iterate()) { if (!Company::IsValidID(v->owner) || !v->IsPrimaryVehicle()) continue; byte type = 0; switch (v->type) { @@ -1623,8 +1621,7 @@ static void NetworkAutoCleanCompanies() if (_settings_client.network.autoclean_novehicles != 0) { memset(vehicles_in_company, 0, sizeof(vehicles_in_company)); - const Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (const Vehicle *v : Vehicle::Iterate()) { if (!Company::IsValidID(v->owner) || !v->IsPrimaryVehicle()) continue; vehicles_in_company[v->owner]++; } diff --git a/src/openttd.cpp b/src/openttd.cpp index cf79419cc..63d5bf756 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -1230,8 +1230,7 @@ static void CheckCaches() rs->GetEntry(DIAGDIR_NW)->CheckIntegrity(rs); } - Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { extern void FillNewGRFVehicleCache(const Vehicle *v); if (v != v->First() || v->vehstatus & VS_CRASHED || !v->IsPrimaryVehicle()) continue; @@ -1306,7 +1305,7 @@ static void CheckCaches() } /* Check whether the caches are still valid */ - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { byte buff[sizeof(VehicleCargoList)]; memcpy(buff, &v->cargo, sizeof(VehicleCargoList)); v->cargo.InvalidateCache(); diff --git a/src/order_cmd.cpp b/src/order_cmd.cpp index fd2015c21..67092bc77 100644 --- a/src/order_cmd.cpp +++ b/src/order_cmd.cpp @@ -1800,14 +1800,12 @@ void CheckOrders(const Vehicle *v) */ void RemoveOrderFromAllVehicles(OrderType type, DestinationID destination, bool hangar) { - Vehicle *v; - /* Aircraft have StationIDs for depot orders and never use DepotIDs * This fact is handled specially below */ /* Go through all vehicles */ - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { Order *order; order = &v->current_order; diff --git a/src/road.cpp b/src/road.cpp index aa9935ad4..486dc0044 100644 --- a/src/road.cpp +++ b/src/road.cpp @@ -324,8 +324,7 @@ bool CanBuildRoadTypeInfrastructure(RoadType roadtype, CompanyID company) } /* We should be able to build infrastructure when we have the actual vehicle type */ - const Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (const Vehicle *v : Vehicle::Iterate()) { if (v->type == VEH_ROAD && (company == OWNER_DEITY || v->owner == company) && HasBit(roadtypes, RoadVehicle::From(v)->roadtype) && HasPowerOnRoad(RoadVehicle::From(v)->roadtype, roadtype)) return true; } diff --git a/src/road_cmd.cpp b/src/road_cmd.cpp index 7a0e9aa91..8126a06d3 100644 --- a/src/road_cmd.cpp +++ b/src/road_cmd.cpp @@ -182,8 +182,7 @@ RoadType AllocateRoadType(RoadTypeLabel label, RoadTramType rtt) */ bool RoadVehiclesAreBuilt() { - const RoadVehicle *rv; - FOR_ALL_ROADVEHICLES(rv) return true; + for (const RoadVehicle *rv : RoadVehicle::Iterate()) return true; return false; } @@ -456,8 +455,7 @@ static CommandCost RemoveRoad(TileIndex tile, DoCommandFlag flags, RoadBits piec if (HasRoadWorks(tile)) { /* flooding tile with road works, don't forget to remove the effect vehicle too */ assert(_current_company == OWNER_WATER); - EffectVehicle *v; - FOR_ALL_EFFECTVEHICLES(v) { + for (EffectVehicle *v : EffectVehicle::Iterate()) { if (TileVirtXY(v->x_pos, v->y_pos) == tile) { delete v; } diff --git a/src/roadveh.h b/src/roadveh.h index cb2627273..028af9dec 100644 --- a/src/roadveh.h +++ b/src/roadveh.h @@ -319,6 +319,4 @@ protected: // These functions should not be called outside acceleration code. } }; -#define FOR_ALL_ROADVEHICLES(var) FOR_ALL_VEHICLES_OF_TYPE(RoadVehicle, var) - #endif /* ROADVEH_H */ diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp index 39ee5b845..8df01bc58 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -278,8 +278,7 @@ static void InitializeWindowsAndCaches() (*it)->tile = t->xy; } } - RoadVehicle *rv; - FOR_ALL_ROADVEHICLES(rv) { + for (RoadVehicle *rv : RoadVehicle::Iterate()) { if (rv->IsFrontEngine()) { rv->CargoChanged(); } @@ -436,8 +435,8 @@ static void FixOwnerOfRailTrack(TileIndex t) assert(!Company::IsValidID(GetTileOwner(t)) && (IsLevelCrossingTile(t) || IsPlainRailTile(t))); /* remove leftover rail piece from crossing (from very old savegames) */ - Train *v = nullptr, *w; - FOR_ALL_TRAINS(w) { + Train *v = nullptr; + for (Train *w : Train::Iterate()) { if (w->tile == t) { v = w; break; @@ -1175,8 +1174,6 @@ bool AfterLoadGame() } if (IsSavegameVersionBefore(SLV_42)) { - Vehicle *v; - for (TileIndex t = 0; t < map_size; t++) { if (MayHaveBridgeAbove(t)) ClearBridgeMiddle(t); if (IsBridgeTile(t)) { @@ -1230,7 +1227,7 @@ bool AfterLoadGame() } } - FOR_ALL_VEHICLES(v) { + for (Vehicle* v : Vehicle::Iterate()) { if (!v->IsGroundVehicle()) continue; if (IsBridgeTile(v->tile)) { DiagDirection dir = GetTunnelBridgeDirection(v->tile); @@ -1289,8 +1286,7 @@ bool AfterLoadGame() if (IsSavegameVersionBefore(SLV_24)) { RailType min_rail = RAILTYPE_ELECTRIC; - Train *v; - FOR_ALL_TRAINS(v) { + for (Train *v : Train::Iterate()) { RailType rt = RailVehInfo(v->engine_type)->railtype; v->railtype = rt; @@ -1327,7 +1323,7 @@ bool AfterLoadGame() } } - FOR_ALL_TRAINS(v) { + for (Train *v : Train::Iterate()) { if (v->IsFrontEngine() || v->IsFreeWagon()) v->ConsistChanged(CCF_TRACK); } @@ -1384,8 +1380,7 @@ bool AfterLoadGame() } if (IsSavegameVersionBefore(SLV_25)) { - RoadVehicle *rv; - FOR_ALL_ROADVEHICLES(rv) { + for (RoadVehicle *rv : RoadVehicle::Iterate()) { rv->vehstatus &= ~0x40; } } @@ -1412,8 +1407,6 @@ bool AfterLoadGame() /* Time starts at 0 instead of 1920. * Account for this in older games by adding an offset */ if (IsSavegameVersionBefore(SLV_31)) { - Vehicle *v; - _date += DAYS_TILL_ORIGINAL_BASE_YEAR; _cur_year += ORIGINAL_BASE_YEAR; @@ -1423,7 +1416,7 @@ bool AfterLoadGame() for (Company *c : Company::Iterate()) c->inaugurated_year += ORIGINAL_BASE_YEAR; for (Industry *i : Industry::Iterate()) i->last_prod_year += ORIGINAL_BASE_YEAR; - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { v->date_of_last_service += DAYS_TILL_ORIGINAL_BASE_YEAR; v->build_year += ORIGINAL_BASE_YEAR; } @@ -1451,13 +1444,11 @@ bool AfterLoadGame() /* Setting no refit flags to all orders in savegames from before refit in orders were added */ if (IsSavegameVersionBefore(SLV_36)) { - Vehicle *v; - for (Order *order : Order::Iterate()) { order->SetRefit(CT_NO_REFIT); } - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { v->current_order.SetRefit(CT_NO_REFIT); } } @@ -1536,13 +1527,12 @@ bool AfterLoadGame() } if (IsSavegameVersionBefore(SLV_45)) { - Vehicle *v; /* Originally just the fact that some cargo had been paid for was * stored to stop people cheating and cashing in several times. This * wasn't enough though as it was cleared when the vehicle started * loading again, even if it didn't actually load anything, so now the * amount that has been paid is stored. */ - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { ClrBit(v->vehicle_flags, 2); } } @@ -1556,9 +1546,8 @@ bool AfterLoadGame() } if (IsSavegameVersionBefore(SLV_50)) { - Aircraft *v; /* Aircraft units changed from 8 mph to 1 km-ish/h */ - FOR_ALL_AIRCRAFT(v) { + for (Aircraft *v : Aircraft::Iterate()) { if (v->subtype <= AIR_AIRCRAFT) { const AircraftVehicleInfo *avi = AircraftVehInfo(v->engine_type); v->cur_speed *= 128; @@ -1592,9 +1581,8 @@ bool AfterLoadGame() } if (IsSavegameVersionBefore(SLV_57)) { - Vehicle *v; /* Added a FIFO queue of vehicles loading at stations */ - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { if ((v->type != VEH_TRAIN || Train::From(v)->IsFrontEngine()) && // for all locs !(v->vehstatus & (VS_STOPPED | VS_CRASHED)) && // not stopped or crashed v->current_order.IsType(OT_LOADING)) { // loading @@ -1645,8 +1633,7 @@ bool AfterLoadGame() if (IsSavegameVersionBefore(SLV_69)) { /* In some old savegames a bit was cleared when it should not be cleared */ - RoadVehicle *rv; - FOR_ALL_ROADVEHICLES(rv) { + for (RoadVehicle *rv : RoadVehicle::Iterate()) { if (rv->state == 250 || rv->state == 251) { SetBit(rv->state, 2); } @@ -1725,8 +1712,7 @@ bool AfterLoadGame() /* Rework of orders. */ for (Order *order : Order::Iterate()) order->ConvertFromOldSavegame(); - Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { if (v->orders.list != nullptr && v->orders.list->GetFirstOrder() != nullptr && v->orders.list->GetFirstOrder()->IsType(OT_NOTHING)) { v->orders.list->FreeChain(); v->orders.list = nullptr; @@ -1747,8 +1733,7 @@ bool AfterLoadGame() } } - Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { if ((v->current_order.GetUnloadType() & (OUFB_UNLOAD | OUFB_TRANSFER)) == (OUFB_UNLOAD | OUFB_TRANSFER)) { v->current_order.SetUnloadType(OUFB_TRANSFER); v->current_order.SetLoadType(OLFB_NO_LOAD); @@ -1897,8 +1882,7 @@ bool AfterLoadGame() if (IsSavegameVersionBefore(SLV_88)) { /* Profits are now with 8 bit fract */ - Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { v->profit_this_year <<= 8; v->profit_last_year <<= 8; v->running_ticks = 0; @@ -1919,8 +1903,7 @@ bool AfterLoadGame() GroupStatistics::UpdateAfterLoad(); // Ensure statistics pool is initialised before trying to delete vehicles /* Remove all trams from savegames without tram support. * There would be trams without tram track under causing crashes sooner or later. */ - RoadVehicle *v; - FOR_ALL_ROADVEHICLES(v) { + for (RoadVehicle *v : RoadVehicle::Iterate()) { if (v->First() == v && HasBit(EngInfo(v->engine_type)->misc_flags, EF_ROAD_TRAM)) { ShowErrorMessage(STR_WARNING_LOADGAME_REMOVED_TRAMS, INVALID_STRING_ID, WL_CRITICAL); delete v; @@ -1991,8 +1974,7 @@ bool AfterLoadGame() /* Reserve all tracks trains are currently on. */ if (IsSavegameVersionBefore(SLV_101)) { - const Train *t; - FOR_ALL_TRAINS(t) { + for (const Train *t : Train::Iterate()) { if (t->First() == t) t->ReserveTrackUnderConsist(); } } @@ -2022,8 +2004,7 @@ bool AfterLoadGame() } if (IsSavegameVersionBefore(SLV_104)) { - Aircraft *a; - FOR_ALL_AIRCRAFT(a) { + for (Aircraft *a : Aircraft::Iterate()) { /* Set engine_type of shadow and rotor */ if (!a->IsNormalAircraft()) { a->engine_type = a->First()->engine_type; @@ -2172,8 +2153,7 @@ bool AfterLoadGame() if (IsSavegameVersionBefore(SLV_121)) { /* Delete small ufos heading for non-existing vehicles */ - Vehicle *v; - FOR_ALL_DISASTERVEHICLES(v) { + for (Vehicle *v : DisasterVehicle::Iterate()) { if (v->subtype == 2 /* ST_SMALL_UFO */ && v->current_order.GetDestination() != 0) { const Vehicle *u = Vehicle::GetIfValid(v->dest_tile); if (u == nullptr || u->type != VEH_ROAD || !RoadVehicle::From(u)->IsFrontEngine()) { @@ -2335,8 +2315,7 @@ bool AfterLoadGame() /* The behaviour of force_proceed has been changed. Now * it counts signals instead of some random time out. */ if (IsSavegameVersionBefore(SLV_131)) { - Train *t; - FOR_ALL_TRAINS(t) { + for (Train *t : Train::Iterate()) { if (t->force_proceed != TFP_NONE) { t->force_proceed = TFP_STUCK; } @@ -2366,13 +2345,11 @@ bool AfterLoadGame() /* Wait counter and load/unload ticks got split. */ if (IsSavegameVersionBefore(SLV_136)) { - Aircraft *a; - FOR_ALL_AIRCRAFT(a) { + for (Aircraft *a : Aircraft::Iterate()) { a->turn_counter = a->current_order.IsType(OT_LOADING) ? 0 : a->load_unload_ticks; } - Train *t; - FOR_ALL_TRAINS(t) { + for (Train *t : Train::Iterate()) { t->wait_counter = t->current_order.IsType(OT_LOADING) ? 0 : t->load_unload_ticks; } } @@ -2447,8 +2424,7 @@ bool AfterLoadGame() * For old savegames with such aircraft we just throw them in the air and * treat the aircraft like they were flying already. */ if (IsSavegameVersionBefore(SLV_146)) { - Aircraft *v; - FOR_ALL_AIRCRAFT(v) { + for (Aircraft *v : Aircraft::Iterate()) { if (!v->IsNormalAircraft()) continue; Station *st = GetTargetAirportIfValid(v); if (st == nullptr && v->state != FLYING) { @@ -2532,8 +2508,7 @@ bool AfterLoadGame() /* The moment vehicles go from hidden to visible changed. This means * that vehicles don't always get visible anymore causing things to * get messed up just after loading the savegame. This fixes that. */ - Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { /* Not all vehicle types can be inside a tunnel. Furthermore, * testing IsTunnelTile() for invalid tiles causes a crash. */ if (!v->IsGroundVehicle()) continue; @@ -2596,8 +2571,7 @@ bool AfterLoadGame() } if (IsSavegameVersionBefore(SLV_153)) { - RoadVehicle *rv; - FOR_ALL_ROADVEHICLES(rv) { + for (RoadVehicle *rv : RoadVehicle::Iterate()) { if (rv->state == RVSB_IN_DEPOT || rv->state == RVSB_WORMHOLE) continue; bool loading = rv->current_order.IsType(OT_LOADING) || rv->current_order.IsType(OT_LEAVESTATION); @@ -2612,8 +2586,7 @@ bool AfterLoadGame() if (IsSavegameVersionBefore(SLV_156)) { /* The train's pathfinder lost flag got moved. */ - Train *t; - FOR_ALL_TRAINS(t) { + for (Train *t : Train::Iterate()) { if (!HasBit(t->flags, 5)) continue; ClrBit(t->flags, 5); @@ -2628,8 +2601,7 @@ bool AfterLoadGame() } if (IsSavegameVersionBefore(SLV_158)) { - Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { switch (v->type) { case VEH_TRAIN: { Train *t = Train::From(v); @@ -2713,7 +2685,7 @@ bool AfterLoadGame() } /* Fill Vehicle::cur_real_order_index */ - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { if (!v->IsPrimaryVehicle()) continue; /* Older versions are less strict with indices being in range and fix them on the fly */ @@ -2733,8 +2705,7 @@ bool AfterLoadGame() * will keep reversing disabled, otherwise it'll be turned on. */ _settings_game.pf.reverse_at_signals = IsSavegameVersionBefore(SLV_100) || (_settings_game.pf.wait_oneway_signal != 255 && _settings_game.pf.wait_twoway_signal != 255 && _settings_game.pf.wait_for_pbs_path != 255); - Train *t; - FOR_ALL_TRAINS(t) { + for (Train *t : Train::Iterate()) { _settings_game.vehicle.max_train_length = max(_settings_game.vehicle.max_train_length, CeilDiv(t->gcache.cached_total_length, TILE_SIZE)); } } @@ -2899,9 +2870,8 @@ bool AfterLoadGame() } if (IsSavegameVersionBefore(SLV_182)) { - Aircraft *v; /* Aircraft acceleration variable was bonkers */ - FOR_ALL_AIRCRAFT(v) { + for (Aircraft *v : Aircraft::Iterate()) { if (v->subtype <= AIR_AIRCRAFT) { const AircraftVehicleInfo *avi = AircraftVehInfo(v->engine_type); v->acceleration = avi->acceleration; @@ -2945,10 +2915,9 @@ bool AfterLoadGame() * Now they have the same length, but that means that trailing articulated parts will * take longer to go through the curve than the parts in front which already left the courve. * So, make articulated parts catch up. */ - RoadVehicle *v; bool roadside = _settings_game.vehicle.road_side == 1; std::vector skip_frames; - FOR_ALL_ROADVEHICLES(v) { + for (RoadVehicle *v : RoadVehicle::Iterate()) { if (!v->IsFrontEngine()) continue; skip_frames.clear(); TileIndex prev_tile = v->tile; @@ -3062,8 +3031,7 @@ bool AfterLoadGame() if (IsSavegameVersionBefore(SLV_SHIPS_STOP_IN_LOCKS)) { /* Move ships from lock slope to upper or lower position. */ - Ship *s; - FOR_ALL_SHIPS(s) { + for (Ship *s : Ship::Iterate()) { /* Suitable tile? */ if (!IsTileType(s->tile, MP_WATER) || !IsLock(s->tile) || GetLockPart(s->tile) != LOCK_PART_MIDDLE) continue; diff --git a/src/saveload/cargopacket_sl.cpp b/src/saveload/cargopacket_sl.cpp index bb44c663f..e9476fd4b 100644 --- a/src/saveload/cargopacket_sl.cpp +++ b/src/saveload/cargopacket_sl.cpp @@ -21,14 +21,13 @@ /* static */ void CargoPacket::AfterLoad() { if (IsSavegameVersionBefore(SLV_44)) { - Vehicle *v; /* If we remove a station while cargo from it is still en route, payment calculation will assume * 0, 0 to be the source of the cargo, resulting in very high payments usually. v->source_xy * stores the coordinates, preserving them even if the station is removed. However, if a game is loaded * where this situation exists, the cargo-source information is lost. in this case, we set the source * to the current tile of the vehicle to prevent excessive profits */ - FOR_ALL_VEHICLES(v) { + for (const Vehicle *v : Vehicle::Iterate()) { const CargoPacketList *packets = v->cargo.Packets(); for (VehicleCargoList::ConstIterator it(packets->begin()); it != packets->end(); it++) { CargoPacket *cp = *it; @@ -67,8 +66,7 @@ /* Only since version 68 we have cargo packets. Savegames from before used * 'new CargoPacket' + cargolist.Append so their caches are already * correct and do not need rebuilding. */ - Vehicle *v; - FOR_ALL_VEHICLES(v) v->cargo.InvalidateCache(); + 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(); @@ -76,8 +74,7 @@ } if (IsSavegameVersionBefore(SLV_181)) { - Vehicle *v; - FOR_ALL_VEHICLES(v) v->cargo.KeepAll(); + for (Vehicle *v : Vehicle::Iterate()) v->cargo.KeepAll(); } } diff --git a/src/saveload/oldloader_sl.cpp b/src/saveload/oldloader_sl.cpp index d2befa84d..830d300b6 100644 --- a/src/saveload/oldloader_sl.cpp +++ b/src/saveload/oldloader_sl.cpp @@ -172,9 +172,7 @@ static StringID *_old_vehicle_names; */ void FixOldVehicles() { - Vehicle *v; - - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { if ((size_t)v->next == 0xFFFF) { v->next = nullptr; } else { @@ -384,8 +382,7 @@ static bool FixTTOEngines() 233, 234, 235, 236, 237, 238, 253 }; - Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { if (v->engine_type >= lengthof(tto_to_ttd)) return false; v->engine_type = tto_to_ttd[v->engine_type]; } diff --git a/src/saveload/station_sl.cpp b/src/saveload/station_sl.cpp index 08fc07157..2df8d8167 100644 --- a/src/saveload/station_sl.cpp +++ b/src/saveload/station_sl.cpp @@ -47,8 +47,7 @@ void MoveBuoysToWaypoints() for (Order *o = ol->GetFirstOrder(); o != nullptr; o = o->next) UpdateWaypointOrder(o); } - Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { VehicleType vt = v->type; if (vt != VEH_SHIP && vt != VEH_TRAIN) continue; diff --git a/src/saveload/vehicle_sl.cpp b/src/saveload/vehicle_sl.cpp index 74a01616b..3569507c7 100644 --- a/src/saveload/vehicle_sl.cpp +++ b/src/saveload/vehicle_sl.cpp @@ -31,13 +31,11 @@ */ void ConnectMultiheadedTrains() { - Train *v; - - FOR_ALL_TRAINS(v) { + for (Train *v : Train::Iterate()) { v->other_multiheaded_part = nullptr; } - FOR_ALL_TRAINS(v) { + for (Train *v : Train::Iterate()) { if (v->IsFrontEngine() || v->IsFreeWagon()) { /* Two ways to associate multiheaded parts to each other: * sequential-matching: Trains shall be arranged to look like <..>..<..>..<..>.. @@ -111,10 +109,9 @@ void ConnectMultiheadedTrains() */ void ConvertOldMultiheadToNew() { - Train *t; - FOR_ALL_TRAINS(t) SetBit(t->subtype, 7); // indicates that it's the old format and needs to be converted in the next loop + for (Train *t : Train::Iterate()) SetBit(t->subtype, 7); // indicates that it's the old format and needs to be converted in the next loop - FOR_ALL_TRAINS(t) { + for (Train *t : Train::Iterate()) { if (HasBit(t->subtype, 7) && ((t->subtype & ~0x80) == 0 || (t->subtype & ~0x80) == 4)) { for (Train *u = t; u != nullptr; u = u->Next()) { const RailVehicleInfo *rvi = RailVehInfo(u->engine_type); @@ -169,8 +166,7 @@ void UpdateOldAircraft() st->airport.flags = 0; // reset airport } - Aircraft *a; - FOR_ALL_AIRCRAFT(a) { + for (Aircraft *a : Aircraft::Iterate()) { /* airplane has another vehicle with subtype 4 (shadow), helicopter also has 3 (rotor) * skip those */ if (a->IsNormalAircraft()) { @@ -220,8 +216,7 @@ static void CheckValidVehicles() for (const Engine *e : Engine::IterateType(VEH_SHIP)) { first_engine[VEH_SHIP] = e->index; break; } for (const Engine *e : Engine::IterateType(VEH_AIRCRAFT)) { first_engine[VEH_AIRCRAFT] = e->index; break; } - Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { /* Test if engine types match */ switch (v->type) { case VEH_TRAIN: @@ -244,9 +239,7 @@ extern byte _age_cargo_skip_counter; // From misc_sl.cpp /** Called after load to update coordinates */ void AfterLoadVehicles(bool part_of_load) { - Vehicle *v; - - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { /* Reinstate the previous pointer */ if (v->Next() != nullptr) v->Next()->previous = v; if (v->NextShared() != nullptr) v->NextShared()->previous_shared = v; @@ -267,7 +260,7 @@ void AfterLoadVehicles(bool part_of_load) */ std::map mapping; - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { if (v->orders.old != nullptr) { if (IsSavegameVersionBefore(SLV_105)) { // Pre-105 didn't save an OrderList if (mapping[v->orders.old] == nullptr) { @@ -294,7 +287,7 @@ void AfterLoadVehicles(bool part_of_load) } } - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { /* Fill the first pointers */ if (v->Previous() == nullptr) { for (Vehicle *u = v; u != nullptr; u = u->Next()) { @@ -306,7 +299,7 @@ void AfterLoadVehicles(bool part_of_load) if (part_of_load) { if (IsSavegameVersionBefore(SLV_105)) { /* Before 105 there was no order for shared orders, thus it messed up horribly */ - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { if (v->First() != v || v->orders.list != nullptr || v->previous_shared != nullptr || v->next_shared == nullptr) continue; /* As above, allocating OrderList here is safe. */ @@ -320,8 +313,7 @@ void AfterLoadVehicles(bool part_of_load) if (IsSavegameVersionBefore(SLV_157)) { /* The road vehicle subtype was converted to a flag. */ - RoadVehicle *rv; - FOR_ALL_ROADVEHICLES(rv) { + for (RoadVehicle *rv : RoadVehicle::Iterate()) { if (rv->subtype == 0) { /* The road vehicle is at the front. */ rv->SetFrontEngine(); @@ -337,7 +329,7 @@ void AfterLoadVehicles(bool part_of_load) if (IsSavegameVersionBefore(SLV_160)) { /* In some old savegames there might be some "crap" stored. */ - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { if (!v->IsPrimaryVehicle() && v->type != VEH_DISASTER) { v->current_order.Free(); v->unitnumber = 0; @@ -347,14 +339,14 @@ void AfterLoadVehicles(bool part_of_load) if (IsSavegameVersionBefore(SLV_162)) { /* Set the vehicle-local cargo age counter from the old global counter. */ - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { v->cargo_age_counter = _age_cargo_skip_counter; } } if (IsSavegameVersionBefore(SLV_180)) { /* Set service interval flags */ - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { if (!v->IsPrimaryVehicle()) continue; const Company *c = Company::Get(v->owner); @@ -367,13 +359,11 @@ void AfterLoadVehicles(bool part_of_load) if (IsSavegameVersionBefore(SLV_SHIP_ROTATION)) { /* Ship rotation added */ - Ship *s; - FOR_ALL_SHIPS(s) { + for (Ship *s : Ship::Iterate()) { s->rotation = s->direction; } } else { - Ship *s; - FOR_ALL_SHIPS(s) { + for (Ship *s : Ship::Iterate()) { if (s->rotation == s->direction) continue; /* In case we are rotating on gameload, set the rotation position to * the current position, otherwise the applied workaround offset would @@ -387,7 +377,7 @@ void AfterLoadVehicles(bool part_of_load) CheckValidVehicles(); - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { assert(v->first != nullptr); v->trip_occupancy = CalcPercentVehicleFilled(v, nullptr); @@ -432,7 +422,7 @@ void AfterLoadVehicles(bool part_of_load) /* Stop non-front engines */ if (part_of_load && IsSavegameVersionBefore(SLV_112)) { - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { if (v->type == VEH_TRAIN) { Train *t = Train::From(v); if (!t->IsFrontEngine()) { @@ -450,7 +440,7 @@ void AfterLoadVehicles(bool part_of_load) } } - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { switch (v->type) { case VEH_ROAD: case VEH_TRAIN: @@ -494,8 +484,7 @@ void FixupTrainLengths() { /* Vehicle center was moved from 4 units behind the front to half the length * behind the front. Move vehicles so they end up on the same spot. */ - Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { if (v->type == VEH_TRAIN && v->IsPrimaryVehicle()) { /* The vehicle center is now more to the front depending on vehicle length, * so we need to move all vehicles forward to cover the difference to the @@ -892,9 +881,8 @@ const SaveLoad *GetVehicleDescription(VehicleType vt) /** Will be called when the vehicles need to be saved. */ static void Save_VEHS() { - Vehicle *v; /* Write the vehicles */ - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { SlSetArrayIndex(v->index); SlObject(v, GetVehicleDescription(v->type)); } @@ -951,8 +939,7 @@ void Load_VEHS() static void Ptrs_VEHS() { - Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { SlObject(v, GetVehicleDescription(v->type)); } } diff --git a/src/saveload/waypoint_sl.cpp b/src/saveload/waypoint_sl.cpp index ddb11082e..c0570f81d 100644 --- a/src/saveload/waypoint_sl.cpp +++ b/src/saveload/waypoint_sl.cpp @@ -136,8 +136,7 @@ void MoveWaypointsToBaseStations() for (Order *o = ol->GetFirstOrder(); o != nullptr; o = o->next) UpdateWaypointOrder(o); } - Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { if (v->type != VEH_TRAIN) continue; UpdateWaypointOrder(&v->current_order); diff --git a/src/script/api/script_group.cpp b/src/script/api/script_group.cpp index 67aa1ac35..7f8081586 100644 --- a/src/script/api/script_group.cpp +++ b/src/script/api/script_group.cpp @@ -154,8 +154,7 @@ Money profit = 0; - const Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (const Vehicle *v : Vehicle::Iterate()) { if (v->group_id != group_id) continue; if (!v->IsPrimaryVehicle()) continue; @@ -179,8 +178,7 @@ uint32 occupancy = 0; uint32 vehicle_count = 0; - const Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (const Vehicle *v : Vehicle::Iterate()) { if (v->group_id != group_id) continue; if (!v->IsPrimaryVehicle()) continue; diff --git a/src/script/api/script_vehiclelist.cpp b/src/script/api/script_vehiclelist.cpp index 727306231..58c03ff9a 100644 --- a/src/script/api/script_vehiclelist.cpp +++ b/src/script/api/script_vehiclelist.cpp @@ -20,8 +20,7 @@ ScriptVehicleList::ScriptVehicleList() { - const Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (const Vehicle *v : Vehicle::Iterate()) { if ((v->owner == ScriptObject::GetCompany() || ScriptObject::GetCompany() == OWNER_DEITY) && (v->IsPrimaryVehicle() || (v->type == VEH_TRAIN && ::Train::From(v)->IsFreeWagon()))) this->AddItem(v->index); } } @@ -30,8 +29,7 @@ ScriptVehicleList_Station::ScriptVehicleList_Station(StationID station_id) { if (!ScriptBaseStation::IsValidBaseStation(station_id)) return; - const Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (const Vehicle *v : Vehicle::Iterate()) { if ((v->owner == ScriptObject::GetCompany() || ScriptObject::GetCompany() == OWNER_DEITY) && v->IsPrimaryVehicle()) { const Order *order; @@ -81,8 +79,7 @@ ScriptVehicleList_Depot::ScriptVehicleList_Depot(TileIndex tile) return; } - const Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (const Vehicle *v : Vehicle::Iterate()) { if ((v->owner == ScriptObject::GetCompany() || ScriptObject::GetCompany() == OWNER_DEITY) && v->IsPrimaryVehicle() && v->type == type) { const Order *order; @@ -109,8 +106,7 @@ ScriptVehicleList_Group::ScriptVehicleList_Group(GroupID group_id) { if (!ScriptGroup::IsValidGroup((ScriptGroup::GroupID)group_id)) return; - const Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (const Vehicle *v : Vehicle::Iterate()) { if (v->owner == ScriptObject::GetCompany() && v->IsPrimaryVehicle()) { if (v->group_id == group_id) this->AddItem(v->index); } @@ -121,8 +117,7 @@ ScriptVehicleList_DefaultGroup::ScriptVehicleList_DefaultGroup(ScriptVehicle::Ve { if (vehicle_type < ScriptVehicle::VT_RAIL || vehicle_type > ScriptVehicle::VT_AIR) return; - const Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (const Vehicle *v : Vehicle::Iterate()) { if (v->owner == ScriptObject::GetCompany() && v->IsPrimaryVehicle()) { if (v->type == (::VehicleType)vehicle_type && v->group_id == ScriptGroup::GROUP_DEFAULT) this->AddItem(v->index); } diff --git a/src/settings.cpp b/src/settings.cpp index 98c273876..0a7ac89ac 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -888,8 +888,7 @@ static bool DeleteSelectStationWindow(int32 p1) static bool UpdateConsists(int32 p1) { - Train *t; - FOR_ALL_TRAINS(t) { + for (Train *t : Train::Iterate()) { /* Update the consist of all trains so the maximum speed is set correctly. */ if (t->IsFrontEngine() || t->IsFreeWagon()) t->ConsistChanged(CCF_TRACK); } @@ -924,8 +923,7 @@ static bool CheckInterval(int32 p1) if (update_vehicles) { const Company *c = Company::Get(_current_company); - Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { if (v->owner == _current_company && v->IsPrimaryVehicle() && !v->ServiceIntervalIsCustom()) { v->SetServiceInterval(CompanyServiceInterval(c, v->type)); v->SetServiceIntervalIsPercent(p1 != 0); @@ -955,8 +953,7 @@ static bool UpdateInterval(VehicleType type, int32 p1) if (interval != p1) return false; if (update_vehicles) { - Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { if (v->owner == _current_company && v->type == type && v->IsPrimaryVehicle() && !v->ServiceIntervalIsCustom()) { v->SetServiceInterval(p1); } @@ -990,8 +987,7 @@ static bool UpdateIntervalAircraft(int32 p1) static bool TrainAccelerationModelChanged(int32 p1) { - Train *t; - FOR_ALL_TRAINS(t) { + for (Train *t : Train::Iterate()) { if (t->IsFrontEngine()) { t->tcache.cached_max_curve_speed = t->GetCurveSpeedLimit(); t->UpdateAcceleration(); @@ -1013,8 +1009,7 @@ static bool TrainAccelerationModelChanged(int32 p1) */ static bool TrainSlopeSteepnessChanged(int32 p1) { - Train *t; - FOR_ALL_TRAINS(t) { + for (Train *t : Train::Iterate()) { if (t->IsFrontEngine()) t->CargoChanged(); } @@ -1029,8 +1024,7 @@ static bool TrainSlopeSteepnessChanged(int32 p1) static bool RoadVehAccelerationModelChanged(int32 p1) { if (_settings_game.vehicle.roadveh_acceleration_model != AM_ORIGINAL) { - RoadVehicle *rv; - FOR_ALL_ROADVEHICLES(rv) { + for (RoadVehicle *rv : RoadVehicle::Iterate()) { if (rv->IsFrontEngine()) { rv->CargoChanged(); } @@ -1052,8 +1046,7 @@ static bool RoadVehAccelerationModelChanged(int32 p1) */ static bool RoadVehSlopeSteepnessChanged(int32 p1) { - RoadVehicle *rv; - FOR_ALL_ROADVEHICLES(rv) { + for (RoadVehicle *rv : RoadVehicle::Iterate()) { if (rv->IsFrontEngine()) rv->CargoChanged(); } @@ -1225,8 +1218,7 @@ static bool CheckFreeformEdges(int32 p1) { if (_game_mode == GM_MENU) return true; if (p1 != 0) { - Ship *s; - FOR_ALL_SHIPS(s) { + for (Ship *s : Ship::Iterate()) { /* Check if there is a ship on the northern border. */ if (TileX(s->tile) == 0 || TileY(s->tile) == 0) { ShowErrorMessage(STR_CONFIG_SETTING_EDGES_NOT_EMPTY, INVALID_STRING_ID, WL_ERROR); @@ -1334,8 +1326,7 @@ static bool MaxVehiclesChanged(int32 p1) static bool InvalidateShipPathCache(int32 p1) { - Ship *s; - FOR_ALL_SHIPS(s) { + for (Ship *s : Ship::Iterate()) { s->path.clear(); } return true; diff --git a/src/ship.h b/src/ship.h index 282b94360..f3e56c39f 100644 --- a/src/ship.h +++ b/src/ship.h @@ -57,10 +57,4 @@ struct Ship FINAL : public SpecializedVehicle { bool IsShipDestinationTile(TileIndex tile, StationID station); -/** - * Iterate over all ships. - * @param var The variable used for iteration. - */ -#define FOR_ALL_SHIPS(var) FOR_ALL_VEHICLES_OF_TYPE(Ship, var) - #endif /* SHIP_H */ diff --git a/src/smallmap_gui.cpp b/src/smallmap_gui.cpp index d76c4652f..d8816265d 100644 --- a/src/smallmap_gui.cpp +++ b/src/smallmap_gui.cpp @@ -876,8 +876,7 @@ void SmallMapWindow::DrawSmallMapColumn(void *dst, uint xc, uint yc, int pitch, */ void SmallMapWindow::DrawVehicles(const DrawPixelInfo *dpi, Blitter *blitter) const { - const Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (const Vehicle *v : Vehicle::Iterate()) { if (v->type == VEH_EFFECT) continue; if (v->vehstatus & (VS_HIDDEN | VS_UNCLICKABLE)) continue; diff --git a/src/station.cpp b/src/station.cpp index 01311ece3..3e00bdb14 100644 --- a/src/station.cpp +++ b/src/station.cpp @@ -97,8 +97,7 @@ Station::~Station() this->loading_vehicles.front()->LeaveStation(); } - Aircraft *a; - FOR_ALL_AIRCRAFT(a) { + for (Aircraft *a : Aircraft::Iterate()) { if (!a->IsNormalAircraft()) continue; if (a->targetairport == this->index) a->targetairport = INVALID_STATION; } @@ -122,8 +121,7 @@ Station::~Station() } } - Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { /* Forget about this station if this station is removed */ if (v->last_station_visited == this->index) { v->last_station_visited = INVALID_STATION; diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp index 4b2acb2db..86b3aedae 100644 --- a/src/station_cmd.cpp +++ b/src/station_cmd.cpp @@ -2033,8 +2033,7 @@ static CommandCost RemoveRoadStop(TileIndex tile, DoCommandFlag flags) delete cur_stop; /* Make sure no vehicle is going to the old roadstop */ - RoadVehicle *v; - FOR_ALL_ROADVEHICLES(v) { + for (RoadVehicle *v : RoadVehicle::Iterate()) { if (v->First() == v && v->current_order.IsType(OT_GOTO_STATION) && v->dest_tile == tile) { v->SetDestTile(v->GetOrderStationLocation(st->index)); @@ -2380,8 +2379,7 @@ static CommandCost RemoveAirport(TileIndex tile, DoCommandFlag flags) CommandCost cost(EXPENSES_CONSTRUCTION); - const Aircraft *a; - FOR_ALL_AIRCRAFT(a) { + for (const Aircraft *a : Aircraft::Iterate()) { if (!a->IsNormalAircraft()) continue; if (a->targetairport == st->index && a->state != FLYING) { return_cmd_error(STR_ERROR_AIRCRAFT_IN_THE_WAY); @@ -2480,8 +2478,7 @@ CommandCost CmdOpenCloseAirport(TileIndex tile, DoCommandFlag flags, uint32 p1, */ bool HasStationInUse(StationID station, bool include_company, CompanyID company) { - const Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (const Vehicle *v : Vehicle::Iterate()) { if ((v->owner == company) == include_company) { const Order *order; FOR_VEHICLE_ORDERS(v, order) { @@ -2721,8 +2718,7 @@ static CommandCost RemoveDock(TileIndex tile, DoCommandFlag flags) * will be selected and in case of no appropriate order it will just * wander around the world. */ if (!(st->facilities & FACIL_DOCK)) { - Ship *s; - FOR_ALL_SHIPS(s) { + for (Ship *s : Ship::Iterate()) { if (s->current_order.IsType(OT_LOADING) && s->current_order.GetDestination() == st->index) { s->LeaveStation(); } diff --git a/src/toolbar_gui.cpp b/src/toolbar_gui.cpp index 6a227b698..9a58ff437 100644 --- a/src/toolbar_gui.cpp +++ b/src/toolbar_gui.cpp @@ -757,10 +757,9 @@ static CallBackFunction MenuClickIndustry(int index) static void ToolbarVehicleClick(Window *w, VehicleType veh) { - const Vehicle *v; int dis = ~0; - FOR_ALL_VEHICLES(v) { + for (const Vehicle *v : Vehicle::Iterate()) { if (v->type == veh && v->IsPrimaryVehicle()) ClrBit(dis, v->owner); } PopupMainCompanyToolbMenu(w, WID_TN_VEHICLE_START + veh, dis); diff --git a/src/train.h b/src/train.h index 895021d91..af638c283 100644 --- a/src/train.h +++ b/src/train.h @@ -333,6 +333,4 @@ protected: // These functions should not be called outside acceleration code. } }; -#define FOR_ALL_TRAINS(var) FOR_ALL_VEHICLES_OF_TYPE(Train, var) - #endif /* TRAIN_H */ diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index ae769374a..5d02764b5 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -71,10 +71,9 @@ byte FreightWagonMult(CargoID cargo) /** Checks if lengths of all rail vehicles are valid. If not, shows an error message. */ void CheckTrainsLengths() { - const Train *v; bool first = true; - FOR_ALL_TRAINS(v) { + for (const Train *v : Train::Iterate()) { if (v->First() == v && !(v->vehstatus & VS_CRASHED)) { for (const Train *u = v, *w = v->Next(); w != nullptr; u = w, w = w->Next()) { if (u->track != TRACK_BIT_DEPOT) { @@ -641,8 +640,7 @@ static CommandCost CmdBuildRailWagon(TileIndex tile, DoCommandFlag flags, const CheckConsistencyOfArticulatedVehicle(v); /* Try to connect the vehicle to one of free chains of wagons. */ - Train *w; - FOR_ALL_TRAINS(w) { + for (Train *w : Train::Iterate()) { if (w->tile == tile && ///< Same depot w->IsFreeWagon() && ///< A free wagon chain w->engine_type == e->index && ///< Same type @@ -660,8 +658,7 @@ static CommandCost CmdBuildRailWagon(TileIndex tile, DoCommandFlag flags, const /** Move all free vehicles in the depot to the train */ static void NormalizeTrainVehInDepot(const Train *u) { - const Train *v; - FOR_ALL_TRAINS(v) { + for (const Train *v : Train::Iterate()) { if (v->IsFreeWagon() && v->tile == u->tile && v->track == TRACK_BIT_DEPOT) { if (DoCommand(0, v->index | 1 << 20, u->index, DC_EXEC, @@ -796,8 +793,7 @@ static Train *FindGoodVehiclePos(const Train *src) EngineID eng = src->engine_type; TileIndex tile = src->tile; - Train *dst; - FOR_ALL_TRAINS(dst) { + for (Train *dst : Train::Iterate()) { if (dst->IsFreeWagon() && dst->tile == tile && !(dst->vehstatus & VS_CRASHED)) { /* check so all vehicles in the line have the same engine. */ Train *t = dst; diff --git a/src/train_gui.cpp b/src/train_gui.cpp index 6da185464..476c8f9cd 100644 --- a/src/train_gui.cpp +++ b/src/train_gui.cpp @@ -33,8 +33,7 @@ void CcBuildWagon(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p /* find a locomotive in the depot. */ const Vehicle *found = nullptr; - const Train *t; - FOR_ALL_TRAINS(t) { + for (const Train *t : Train::Iterate()) { if (t->IsFrontEngine() && t->tile == tile && t->IsStoppedInDepot()) { if (found != nullptr) return; // must be exactly one. found = t; diff --git a/src/vehicle.cpp b/src/vehicle.cpp index 57e00418a..806bf4795 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -670,16 +670,14 @@ static void UpdateVehicleViewportHash(Vehicle *v, int x, int y) void ResetVehicleHash() { - Vehicle *v; - FOR_ALL_VEHICLES(v) { v->hash_tile_current = nullptr; } + for (Vehicle *v : Vehicle::Iterate()) { v->hash_tile_current = nullptr; } memset(_vehicle_viewport_hash, 0, sizeof(_vehicle_viewport_hash)); memset(_vehicle_tile_hash, 0, sizeof(_vehicle_tile_hash)); } void ResetVehicleColourMap() { - Vehicle *v; - FOR_ALL_VEHICLES(v) { v->colourmap = PAL_NONE; } + for (Vehicle *v : Vehicle::Iterate()) { v->colourmap = PAL_NONE; } } /** @@ -954,8 +952,8 @@ void CallVehicleTicks() PerformanceAccumulator::Reset(PFE_GL_SHIPS); PerformanceAccumulator::Reset(PFE_GL_AIRCRAFT); - Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { + size_t vehicle_index = v->index; /* Vehicle could be deleted in this tick */ if (!v->Tick()) { assert(Vehicle::Get(vehicle_index) == nullptr); @@ -1026,7 +1024,7 @@ void CallVehicleTicks() Backup cur_company(_current_company, FILE_LINE); for (auto &it : _vehicles_to_autoreplace) { - v = it.first; + Vehicle *v = it.first; /* Autoreplace needs the current company set as the vehicle owner */ cur_company.Change(v->owner); @@ -1166,7 +1164,7 @@ void ViewportAddVehicles(DrawPixelInfo *dpi) */ Vehicle *CheckClickOnVehicle(const ViewPort *vp, int x, int y) { - Vehicle *found = nullptr, *v; + Vehicle *found = nullptr; uint dist, best_dist = UINT_MAX; if ((uint)(x -= vp->left) >= (uint)vp->width || (uint)(y -= vp->top) >= (uint)vp->height) return nullptr; @@ -1174,7 +1172,7 @@ Vehicle *CheckClickOnVehicle(const ViewPort *vp, int x, int y) x = ScaleByZoom(x, vp->zoom) + vp->virtual_left; y = ScaleByZoom(y, vp->zoom) + vp->virtual_top; - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { if ((v->vehstatus & (VS_HIDDEN | VS_UNCLICKABLE)) == 0 && x >= v->coord.left && x <= v->coord.right && y >= v->coord.top && y <= v->coord.bottom) { @@ -1688,8 +1686,7 @@ VehicleEnterTileStatus VehicleEnterTile(Vehicle *v, TileIndex tile, int x, int y FreeUnitIDGenerator::FreeUnitIDGenerator(VehicleType type, CompanyID owner) : cache(nullptr), maxid(0), curid(0) { /* Find maximum */ - const Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (const Vehicle *v : Vehicle::Iterate()) { if (v->type == type && v->owner == owner) { this->maxid = max(this->maxid, v->unitnumber); } @@ -1703,7 +1700,7 @@ FreeUnitIDGenerator::FreeUnitIDGenerator(VehicleType type, CompanyID owner) : ca this->cache = CallocT(this->maxid + 2); /* Fill the cache */ - FOR_ALL_VEHICLES(v) { + for (const Vehicle *v : Vehicle::Iterate()) { if (v->type == type && v->owner == owner) { this->cache[v->unitnumber] = true; } @@ -1787,8 +1784,7 @@ bool CanBuildVehicleInfrastructure(VehicleType type, byte subtype) } /* We should be able to build infrastructure when we have the actual vehicle type */ - const Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (const Vehicle *v : Vehicle::Iterate()) { if (type == VEH_ROAD && GetRoadTramType(RoadVehicle::From(v)->roadtype) != (RoadTramType)subtype) continue; if (v->owner == _local_company && v->type == type) return true; } @@ -2745,8 +2741,7 @@ void Vehicle::RemoveFromShared() void VehiclesYearlyLoop() { - Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (Vehicle *v : Vehicle::Iterate()) { if (v->IsPrimaryVehicle()) { /* show warning if vehicle is not generating enough income last 2 years (corresponds to a red icon in the vehicle list) */ Money profit = v->GetDisplayProfitThisYear(); diff --git a/src/vehicle_base.h b/src/vehicle_base.h index 4c2aa891a..bc72c6bbf 100644 --- a/src/vehicle_base.h +++ b/src/vehicle_base.h @@ -971,19 +971,6 @@ public: } }; -/** - * Iterate over all vehicles from a given point. - * @param var The variable used to iterate over. - * @param start The vehicle to start the iteration at. - */ -#define FOR_ALL_VEHICLES_FROM(var, start) FOR_ALL_ITEMS_FROM(Vehicle, vehicle_index, var, start) - -/** - * Iterate over all vehicles. - * @param var The variable used to iterate over. - */ -#define FOR_ALL_VEHICLES(var) FOR_ALL_VEHICLES_FROM(var, 0) - /** * Class defining several overloaded accessors so we don't * have to cast vehicle types that often @@ -1146,14 +1133,14 @@ struct SpecializedVehicle : public Vehicle { this->Vehicle::UpdateViewport(true); } } -}; -/** - * Iterate over all vehicles of a particular type. - * @param name The type of vehicle to iterate over. - * @param var The variable used to iterate over. - */ -#define FOR_ALL_VEHICLES_OF_TYPE(name, var) FOR_ALL_ITEMS_FROM(name, vehicle_index, var, 0) if (var->type == name::EXPECTED_TYPE) + /** + * Returns an iterable ensemble of all valid vehicles of type T + * @param from index of the first vehicle to consider + * @return an iterable ensemble of all valid vehicles of type T + */ + static Pool::IterateWrapper Iterate(size_t from = 0) { return Pool::IterateWrapper(from); } +}; /** Generates sequence of free UnitID numbers */ struct FreeUnitIDGenerator { diff --git a/src/vehicle_cmd.cpp b/src/vehicle_cmd.cpp index f935bc5df..5ba31945f 100644 --- a/src/vehicle_cmd.cpp +++ b/src/vehicle_cmd.cpp @@ -747,9 +747,7 @@ CommandCost CmdDepotMassAutoReplace(TileIndex tile, DoCommandFlag flags, uint32 */ static bool IsUniqueVehicleName(const char *name) { - const Vehicle *v; - - FOR_ALL_VEHICLES(v) { + for (const Vehicle *v : Vehicle::Iterate()) { if (v->name != nullptr && strcmp(v->name, name) == 0) return false; } diff --git a/src/vehiclelist.cpp b/src/vehiclelist.cpp index 0beccdd40..de37e3aba 100644 --- a/src/vehiclelist.cpp +++ b/src/vehiclelist.cpp @@ -71,8 +71,7 @@ void BuildDepotVehicleList(VehicleType type, TileIndex tile, VehicleList *engine engines->clear(); if (wagons != nullptr && wagons != engines) wagons->clear(); - const Vehicle *v; - FOR_ALL_VEHICLES(v) { + for (const Vehicle *v : Vehicle::Iterate()) { /* General tests for all vehicle types */ if (v->type != type) continue; if (v->tile != tile) continue; @@ -115,11 +114,9 @@ bool GenerateVehicleSortList(VehicleList *list, const VehicleListIdentifier &vli { list->clear(); - const Vehicle *v; - switch (vli.type) { case VL_STATION_LIST: - FOR_ALL_VEHICLES(v) { + for (const Vehicle *v : Vehicle::Iterate()) { if (v->type == vli.vtype && v->IsPrimaryVehicle()) { const Order *order; @@ -134,19 +131,20 @@ bool GenerateVehicleSortList(VehicleList *list, const VehicleListIdentifier &vli } break; - case VL_SHARED_ORDERS: + case VL_SHARED_ORDERS: { /* Add all vehicles from this vehicle's shared order list */ - v = Vehicle::GetIfValid(vli.index); + const Vehicle *v = Vehicle::GetIfValid(vli.index); if (v == nullptr || v->type != vli.vtype || !v->IsPrimaryVehicle()) return false; for (; v != nullptr; v = v->NextShared()) { list->push_back(v); } break; + } case VL_GROUP_LIST: if (vli.index != ALL_GROUP) { - FOR_ALL_VEHICLES(v) { + for (const Vehicle *v : Vehicle::Iterate()) { if (v->type == vli.vtype && v->IsPrimaryVehicle() && v->owner == vli.company && GroupIsInGroup(v->group_id, vli.index)) { list->push_back(v); @@ -157,7 +155,7 @@ bool GenerateVehicleSortList(VehicleList *list, const VehicleListIdentifier &vli FALLTHROUGH; case VL_STANDARD: - FOR_ALL_VEHICLES(v) { + for (const Vehicle *v : Vehicle::Iterate()) { if (v->type == vli.vtype && v->owner == vli.company && v->IsPrimaryVehicle()) { list->push_back(v); } @@ -165,7 +163,7 @@ bool GenerateVehicleSortList(VehicleList *list, const VehicleListIdentifier &vli break; case VL_DEPOT_LIST: - FOR_ALL_VEHICLES(v) { + for (const Vehicle *v : Vehicle::Iterate()) { if (v->type == vli.vtype && v->IsPrimaryVehicle()) { const Order *order; -- cgit v1.2.3-54-g00ecf