1
0
Fork 0

(svn r16894) -Codechange: Add [Specialised]Vehicle::Last().

release/1.0
frosch 2009-07-20 19:58:33 +00:00
parent e3752f3ef8
commit d5a4c89365
7 changed files with 42 additions and 24 deletions

View File

@ -151,7 +151,7 @@ static void TrainDepotMoveVehicle(const Vehicle *wagon, VehicleID sel, const Veh
if (v == wagon) return; if (v == wagon) return;
if (wagon == NULL) { if (wagon == NULL) {
if (head != NULL) wagon = GetLastVehicleInChain(head); if (head != NULL) wagon = head->Last();
} else { } else {
wagon = wagon->Previous(); wagon = wagon->Previous();
if (wagon == NULL) return; if (wagon == NULL) return;

View File

@ -704,7 +704,7 @@ static CommandCost CmdBuildRailWagon(EngineID engine, TileIndex tile, DoCommandF
if (w->tile == tile && w->IsFreeWagon() && if (w->tile == tile && w->IsFreeWagon() &&
w->engine_type == engine && w->engine_type == engine &&
!(w->vehstatus & VS_CRASHED)) { !(w->vehstatus & VS_CRASHED)) {
u = GetLastVehicleInChain(w); u = w->Last();
break; break;
} }
} }
@ -1829,7 +1829,7 @@ static void AdvanceWagonsBeforeSwap(Train *v)
{ {
Train *base = v; Train *base = v;
Train *first = base; // first vehicle to move 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); uint length = CountVehiclesInChain(v);
while (length > 2) { while (length > 2) {
@ -1878,7 +1878,7 @@ static void AdvanceWagonsAfterSwap(Train *v)
Train *base = v; Train *base = v;
Train *first = base; // first vehicle to move 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); uint length = CountVehiclesInChain(v);
/* we have to make sure all wagons that leave a depot because of train reversing are moved coorectly /* 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; } break;
case VPF_NPF: { // NPF case VPF_NPF: { // NPF
const Vehicle *last = GetLastVehicleInChain(v); const Vehicle *last = v->Last();
Trackdir trackdir = v->GetVehicleTrackdir(); Trackdir trackdir = v->GetVehicleTrackdir();
Trackdir trackdir_rev = ReverseTrackdir(last->GetVehicleTrackdir()); Trackdir trackdir_rev = ReverseTrackdir(last->GetVehicleTrackdir());
@ -3225,7 +3225,7 @@ static bool CheckReverseTrain(Train *v)
case VPF_NPF: { // NPF case VPF_NPF: { // NPF
NPFFindStationOrTileData fstd; NPFFindStationOrTileData fstd;
NPFFoundTargetData ftd; NPFFoundTargetData ftd;
Vehicle *last = GetLastVehicleInChain(v); Vehicle *last = v->Last();
NPFFillWithOrderData(&fstd, v); NPFFillWithOrderData(&fstd, v);

View File

@ -35,7 +35,7 @@ void CcBuildWagon(bool success, TileIndex tile, uint32 p1, uint32 p2)
/* if we found a loco, */ /* if we found a loco, */
if (found != NULL) { if (found != NULL) {
found = GetLastVehicleInChain(found); found = found->Last();
/* put the new wagon at the end of the loco. */ /* put the new wagon at the end of the loco. */
DoCommandP(0, _new_vehicle_id | (found->index << 16), 0, CMD_MOVE_RAIL_VEHICLE); DoCommandP(0, _new_vehicle_id | (found->index << 16), 0, CMD_MOVE_RAIL_VEHICLE);
InvalidateWindowClassesData(WC_TRAINS_LIST, 0); InvalidateWindowClassesData(WC_TRAINS_LIST, 0);

View File

@ -453,18 +453,6 @@ void InitializeVehicles()
ResetVehiclePosHash(); 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 CountVehiclesInChain(const Vehicle *v)
{ {
uint count = 0; uint count = 0;

View File

@ -366,6 +366,27 @@ public:
*/ */
inline Vehicle *First() const { return this->first; } 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. * 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(); } 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 * Get next vehicle in the chain
* @return next vehicle in the chain * @return next vehicle in the chain

View File

@ -23,8 +23,6 @@
typedef Vehicle *VehicleFromPosProc(Vehicle *v, void *data); typedef Vehicle *VehicleFromPosProc(Vehicle *v, void *data);
void VehicleServiceInDepot(Vehicle *v); void VehicleServiceInDepot(Vehicle *v);
Vehicle *GetLastVehicleInChain(Vehicle *v);
const Vehicle *GetLastVehicleInChain(const Vehicle *v);
uint CountVehiclesInChain(const Vehicle *v); uint CountVehiclesInChain(const Vehicle *v);
void FindVehicleOnPos(TileIndex tile, void *data, VehicleFromPosProc *proc); void FindVehicleOnPos(TileIndex tile, void *data, VehicleFromPosProc *proc);
void FindVehicleOnPosXY(int x, int y, void *data, VehicleFromPosProc *proc); void FindVehicleOnPosXY(int x, int y, void *data, VehicleFromPosProc *proc);

View File

@ -537,8 +537,7 @@ Trackdir YapfChooseRailTrack(const Vehicle *v, TileIndex tile, DiagDirection ent
bool YapfCheckReverseTrain(const Vehicle *vt) bool YapfCheckReverseTrain(const Vehicle *vt)
{ {
const Train *v = Train::From(vt); const Train *v = Train::From(vt);
/* last wagon */ const Train *last_veh = v->Last();
const Train *last_veh = Train::From(GetLastVehicleInChain(v));
/* get trackdirs of both ends */ /* get trackdirs of both ends */
Trackdir td = v->GetVehicleTrackdir(); Trackdir td = v->GetVehicleTrackdir();
@ -600,7 +599,7 @@ bool YapfFindNearestRailDepotTwoWay(const Vehicle *v, int max_distance, int reve
*depot_tile = INVALID_TILE; *depot_tile = INVALID_TILE;
*reversed = false; *reversed = false;
const Vehicle *last_veh = GetLastVehicleInChain(v); const Vehicle *last_veh = v->Last();
PBSTileInfo origin = FollowTrainReservation(Train::From(v)); PBSTileInfo origin = FollowTrainReservation(Train::From(v));
TileIndex last_tile = last_veh->tile; TileIndex last_tile = last_veh->tile;