From d5a4c893656f61950595c057c7643f4082228d47 Mon Sep 17 00:00:00 2001 From: frosch Date: Mon, 20 Jul 2009 19:58:33 +0000 Subject: (svn r16894) -Codechange: Add [Specialised]Vehicle::Last(). --- src/depot_gui.cpp | 2 +- src/train_cmd.cpp | 10 +++++----- src/train_gui.cpp | 2 +- src/vehicle.cpp | 12 ------------ src/vehicle_base.h | 33 +++++++++++++++++++++++++++++++++ src/vehicle_func.h | 2 -- src/yapf/yapf_rail.cpp | 5 ++--- 7 files changed, 42 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/depot_gui.cpp b/src/depot_gui.cpp index a311ebd75..48a9fd8ce 100644 --- a/src/depot_gui.cpp +++ b/src/depot_gui.cpp @@ -151,7 +151,7 @@ static void TrainDepotMoveVehicle(const Vehicle *wagon, VehicleID sel, const Veh if (v == wagon) return; if (wagon == NULL) { - if (head != NULL) wagon = GetLastVehicleInChain(head); + if (head != NULL) wagon = head->Last(); } else { wagon = wagon->Previous(); if (wagon == NULL) return; diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index 19a8cd84f..a85b16f95 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -704,7 +704,7 @@ static CommandCost CmdBuildRailWagon(EngineID engine, TileIndex tile, DoCommandF if (w->tile == tile && w->IsFreeWagon() && w->engine_type == engine && !(w->vehstatus & VS_CRASHED)) { - u = GetLastVehicleInChain(w); + u = w->Last(); break; } } @@ -1829,7 +1829,7 @@ static void AdvanceWagonsBeforeSwap(Train *v) { Train *base = v; Train *first = base; // first vehicle to move - Train *last = Train::From(GetLastVehicleInChain(v)); // last vehicle to move + Train *last = v->Last(); // last vehicle to move uint length = CountVehiclesInChain(v); while (length > 2) { @@ -1878,7 +1878,7 @@ static void AdvanceWagonsAfterSwap(Train *v) Train *base = v; Train *first = base; // first vehicle to move - Train *last = Train::From(GetLastVehicleInChain(v)); // last vehicle to move + Train *last = v->Last(); // last vehicle to move uint length = CountVehiclesInChain(v); /* we have to make sure all wagons that leave a depot because of train reversing are moved coorectly @@ -2219,7 +2219,7 @@ static TrainFindDepotData FindClosestTrainDepot(Train *v, int max_distance) } break; case VPF_NPF: { // NPF - const Vehicle *last = GetLastVehicleInChain(v); + const Vehicle *last = v->Last(); Trackdir trackdir = v->GetVehicleTrackdir(); Trackdir trackdir_rev = ReverseTrackdir(last->GetVehicleTrackdir()); @@ -3225,7 +3225,7 @@ static bool CheckReverseTrain(Train *v) case VPF_NPF: { // NPF NPFFindStationOrTileData fstd; NPFFoundTargetData ftd; - Vehicle *last = GetLastVehicleInChain(v); + Vehicle *last = v->Last(); NPFFillWithOrderData(&fstd, v); diff --git a/src/train_gui.cpp b/src/train_gui.cpp index c9f967711..22efbe6d4 100644 --- a/src/train_gui.cpp +++ b/src/train_gui.cpp @@ -35,7 +35,7 @@ void CcBuildWagon(bool success, TileIndex tile, uint32 p1, uint32 p2) /* if we found a loco, */ if (found != NULL) { - found = GetLastVehicleInChain(found); + found = found->Last(); /* put the new wagon at the end of the loco. */ DoCommandP(0, _new_vehicle_id | (found->index << 16), 0, CMD_MOVE_RAIL_VEHICLE); InvalidateWindowClassesData(WC_TRAINS_LIST, 0); diff --git a/src/vehicle.cpp b/src/vehicle.cpp index a92931706..3562a2b58 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -453,18 +453,6 @@ void InitializeVehicles() ResetVehiclePosHash(); } -Vehicle *GetLastVehicleInChain(Vehicle *v) -{ - while (v->Next() != NULL) v = v->Next(); - return v; -} - -const Vehicle *GetLastVehicleInChain(const Vehicle *v) -{ - while (v->Next() != NULL) v = v->Next(); - return v; -} - uint CountVehiclesInChain(const Vehicle *v) { uint count = 0; diff --git a/src/vehicle_base.h b/src/vehicle_base.h index ff8c34000..80f7c0581 100644 --- a/src/vehicle_base.h +++ b/src/vehicle_base.h @@ -366,6 +366,27 @@ public: */ inline Vehicle *First() const { return this->first; } + /** + * Get the last vehicle of this vehicle chain. + * @return the last vehicle of the chain. + */ + inline Vehicle *Last() + { + Vehicle *v = this; + while (v->Next() != NULL) v = v->Next(); + return v; + } + + /** + * Get the last vehicle of this vehicle chain. + * @return the last vehicle of the chain. + */ + inline const Vehicle *Last() const + { + const Vehicle *v = this; + while (v->Next() != NULL) v = v->Next(); + return v; + } /** * Get the first order of the vehicles order list. @@ -537,6 +558,18 @@ struct SpecializedVehicle : public Vehicle { */ FORCEINLINE T *First() const { return (T *)this->Vehicle::First(); } + /** + * Get the last vehicle in the chain + * @return last vehicle in the chain + */ + FORCEINLINE T *Last() { return (T *)this->Vehicle::Last(); } + + /** + * Get the last vehicle in the chain + * @return last vehicle in the chain + */ + FORCEINLINE const T *Last() const { return (const T *)this->Vehicle::Last(); } + /** * Get next vehicle in the chain * @return next vehicle in the chain diff --git a/src/vehicle_func.h b/src/vehicle_func.h index 5d12ba9ca..e42b3b3b6 100644 --- a/src/vehicle_func.h +++ b/src/vehicle_func.h @@ -23,8 +23,6 @@ typedef Vehicle *VehicleFromPosProc(Vehicle *v, void *data); void VehicleServiceInDepot(Vehicle *v); -Vehicle *GetLastVehicleInChain(Vehicle *v); -const Vehicle *GetLastVehicleInChain(const Vehicle *v); uint CountVehiclesInChain(const Vehicle *v); void FindVehicleOnPos(TileIndex tile, void *data, VehicleFromPosProc *proc); void FindVehicleOnPosXY(int x, int y, void *data, VehicleFromPosProc *proc); diff --git a/src/yapf/yapf_rail.cpp b/src/yapf/yapf_rail.cpp index 13c064eb8..d5a5f51a2 100644 --- a/src/yapf/yapf_rail.cpp +++ b/src/yapf/yapf_rail.cpp @@ -537,8 +537,7 @@ Trackdir YapfChooseRailTrack(const Vehicle *v, TileIndex tile, DiagDirection ent bool YapfCheckReverseTrain(const Vehicle *vt) { const Train *v = Train::From(vt); - /* last wagon */ - const Train *last_veh = Train::From(GetLastVehicleInChain(v)); + const Train *last_veh = v->Last(); /* get trackdirs of both ends */ Trackdir td = v->GetVehicleTrackdir(); @@ -600,7 +599,7 @@ bool YapfFindNearestRailDepotTwoWay(const Vehicle *v, int max_distance, int reve *depot_tile = INVALID_TILE; *reversed = false; - const Vehicle *last_veh = GetLastVehicleInChain(v); + const Vehicle *last_veh = v->Last(); PBSTileInfo origin = FollowTrainReservation(Train::From(v)); TileIndex last_tile = last_veh->tile; -- cgit v1.2.3-70-g09d2