Changeset - r17174:2c402e0c1de1
[Not reviewed]
master
0 2 0
terkhen - 13 years ago 2011-01-29 17:27:32
terkhen@openttd.org
(svn r21923) -Codechange: Unify articulated vehicle iteration functions.
2 files changed with 112 insertions and 70 deletions:
0 comments (0 inline, 0 general)
src/train.h
Show inline comments
 
@@ -136,76 +136,6 @@ struct Train : public GroundVehicle<Trai
 
	int GetCurrentMaxSpeed() const;
 

	
 
	/**
 
	 * Get the next part of a multi-part engine.
 
	 * Will only work on a multi-part engine (this->EngineHasArticPart() == true),
 
	 * Result is undefined for normal engine.
 
	 * @return next part of articulated engine
 
	 */
 
	FORCEINLINE Train *GetNextArticulatedPart() const
 
	{
 
		assert(this->HasArticulatedPart());
 
		return this->Next();
 
	}
 

	
 
	/**
 
	 * Get the first part of a multi-part engine.
 
	 * @return First part of the engine.
 
	 */
 
	FORCEINLINE Train *GetFirstEnginePart()
 
	{
 
		Train *v = this;
 
		while (v->IsArticulatedPart()) v = v->Previous();
 
		return v;
 
	}
 

	
 
	/**
 
	 * Get the first part of a multi-part engine.
 
	 * @return First part of the engine.
 
	 */
 
	FORCEINLINE const Train *GetFirstEnginePart() const
 
	{
 
		const Train *v = this;
 
		while (v->IsArticulatedPart()) v = v->Previous();
 
		return v;
 
	}
 

	
 
	/**
 
	 * Get the last part of a multi-part engine.
 
	 * @return Last part of the engine.
 
	 */
 
	FORCEINLINE Train *GetLastEnginePart()
 
	{
 
		Train *v = this;
 
		while (v->HasArticulatedPart()) v = v->GetNextArticulatedPart();
 
		return v;
 
	}
 

	
 
	/**
 
	 * Get the next real (non-articulated part) vehicle in the consist.
 
	 * @return Next vehicle in the consist.
 
	 */
 
	FORCEINLINE Train *GetNextVehicle() const
 
	{
 
		const Train *v = this;
 
		while (v->HasArticulatedPart()) v = v->GetNextArticulatedPart();
 

	
 
		/* v now contains the last artic part in the engine */
 
		return v->Next();
 
	}
 

	
 
	/**
 
	 * Get the previous real (non-articulated part) vehicle in the consist.
 
	 * @return Previous vehicle in the consist.
 
	 */
 
	FORCEINLINE Train *GetPrevVehicle() const
 
	{
 
		Train *v = this->Previous();
 
		while (v != NULL && v->IsArticulatedPart()) v = v->Previous();
 

	
 
		return v;
 
	}
 

	
 
	/**
 
	 * Get the next real (non-articulated part and non rear part of dualheaded engine) vehicle in the consist.
 
	 * @return Next vehicle in the consist.
 
	 */
src/vehicle_base.h
Show inline comments
 
@@ -671,6 +671,75 @@ public:
 
	{
 
		return this->Next() != NULL && this->Next()->IsArticulatedPart();
 
	}
 

	
 
	/**
 
	 * Get the next part of an articulated engine.
 
	 * @return Next part of the articulated engine.
 
	 * @pre The vehicle is an articulated engine.
 
	 */
 
	FORCEINLINE Vehicle *GetNextArticulatedPart() const
 
	{
 
		assert(this->HasArticulatedPart());
 
		return this->Next();
 
	}
 

	
 
	/**
 
	 * Get the first part of an articulated engine.
 
	 * @return First part of the engine.
 
	 */
 
	FORCEINLINE Vehicle *GetFirstEnginePart()
 
	{
 
		Vehicle *v = this;
 
		while (v->IsArticulatedPart()) v = v->Previous();
 
		return v;
 
	}
 

	
 
	/**
 
	 * Get the first part of an articulated engine.
 
	 * @return First part of the engine.
 
	 */
 
	FORCEINLINE const Vehicle *GetFirstEnginePart() const
 
	{
 
		const Vehicle *v = this;
 
		while (v->IsArticulatedPart()) v = v->Previous();
 
		return v;
 
	}
 

	
 
	/**
 
	 * Get the last part of an articulated engine.
 
	 * @return Last part of the engine.
 
	 */
 
	FORCEINLINE Vehicle *GetLastEnginePart()
 
	{
 
		Vehicle *v = this;
 
		while (v->HasArticulatedPart()) v = v->GetNextArticulatedPart();
 
		return v;
 
	}
 

	
 
	/**
 
	 * Get the next real (non-articulated part) vehicle in the consist.
 
	 * @return Next vehicle in the consist.
 
	 */
 
	FORCEINLINE Vehicle *GetNextVehicle() const
 
	{
 
		const Vehicle *v = this;
 
		while (v->HasArticulatedPart()) v = v->GetNextArticulatedPart();
 

	
 
		/* v now contains the last articulated part in the engine */
 
		return v->Next();
 
	}
 

	
 
	/**
 
	 * Get the previous real (non-articulated part) vehicle in the consist.
 
	 * @return Previous vehicle in the consist.
 
	 */
 
	FORCEINLINE Vehicle *GetPrevVehicle() const
 
	{
 
		Vehicle *v = this->Previous();
 
		while (v != NULL && v->IsArticulatedPart()) v = v->Previous();
 

	
 
		return v;
 
	}
 
};
 

	
 
#define FOR_ALL_VEHICLES_FROM(var, start) FOR_ALL_ITEMS_FROM(Vehicle, vehicle_index, var, start)
 
@@ -721,6 +790,49 @@ struct SpecializedVehicle : public Vehic
 
	 */
 
	FORCEINLINE T *Previous() const { return (T *)this->Vehicle::Previous(); }
 

	
 
	/**
 
	 * Get the next part of an articulated engine.
 
	 * @return Next part of the articulated engine.
 
	 * @pre The vehicle is an articulated engine.
 
	 */
 
	FORCEINLINE T *GetNextArticulatedPart() { return (T *)this->Vehicle::GetNextArticulatedPart(); }
 

	
 
	/**
 
	 * Get the next part of an articulated engine.
 
	 * @return Next part of the articulated engine.
 
	 * @pre The vehicle is an articulated engine.
 
	 */
 
	FORCEINLINE T *GetNextArticulatedPart() const { return (T *)this->Vehicle::GetNextArticulatedPart(); }
 

	
 
	/**
 
	 * Get the first part of an articulated engine.
 
	 * @return First part of the engine.
 
	 */
 
	FORCEINLINE T *GetFirstEnginePart() { return (T *)this->Vehicle::GetFirstEnginePart(); }
 

	
 
	/**
 
	 * Get the first part of an articulated engine.
 
	 * @return First part of the engine.
 
	 */
 
	FORCEINLINE const T *GetFirstEnginePart() const { return (const T *)this->Vehicle::GetFirstEnginePart(); }
 

	
 
	/**
 
	 * Get the last part of an articulated engine.
 
	 * @return Last part of the engine.
 
	 */
 
	FORCEINLINE T *GetLastEnginePart() { return (T *)this->Vehicle::GetLastEnginePart(); }
 

	
 
	/**
 
	 * Get the next real (non-articulated part) vehicle in the consist.
 
	 * @return Next vehicle in the consist.
 
	 */
 
	FORCEINLINE T *GetNextVehicle() const { return (T *)this->Vehicle::GetNextVehicle(); }
 

	
 
	/**
 
	 * Get the previous real (non-articulated part) vehicle in the consist.
 
	 * @return Previous vehicle in the consist.
 
	 */
 
	FORCEINLINE T *GetPrevVehicle() const { return (T *)this->Vehicle::GetPrevVehicle(); }
 

	
 
	/**
 
	 * Tests whether given index is a valid index for vehicle of this type
0 comments (0 inline, 0 general)