summaryrefslogtreecommitdiff
path: root/src/vehicle.h
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2007-08-30 21:11:12 +0000
committerrubidium <rubidium@openttd.org>2007-08-30 21:11:12 +0000
commitaba867d78dd52154bb7874a6998a3002dab57684 (patch)
treeb205fc9b323290f091d73bfe7f40c7ed002d44cc /src/vehicle.h
parent8a6cc3aa104b5f8631dcb74343dcd68ffa3308ec (diff)
downloadopenttd-aba867d78dd52154bb7874a6998a3002dab57684.tar.xz
(svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
-Codechange: do not brute force determine the first vehicle in the chain or previous vehicle, but do it by properly accounting the previous and first pointers when updating the next pointer. This gives a performance increase of about 15% when there are a lot of vehicles in the game.
Diffstat (limited to 'src/vehicle.h')
-rw-r--r--src/vehicle.h25
1 files changed, 19 insertions, 6 deletions
diff --git a/src/vehicle.h b/src/vehicle.h
index ea75423c1..1526c5c9f 100644
--- a/src/vehicle.h
+++ b/src/vehicle.h
@@ -219,7 +219,8 @@ struct Vehicle;
DECLARE_OLD_POOL(Vehicle, Vehicle, 9, 125)
struct SaveLoad;
-extern const SaveLoad *GetVehicleDescription(VehicleType vt);
+const SaveLoad *GetVehicleDescription(VehicleType vt);
+void AfterLoadVehicles();
struct Vehicle : PoolItem<Vehicle, VehicleID, &_Vehicle_pool> {
VehicleTypeByte type; ///< Type of vehicle
@@ -227,10 +228,12 @@ struct Vehicle : PoolItem<Vehicle, VehicleID, &_Vehicle_pool> {
private:
Vehicle *next; // pointer to the next vehicle in the chain
+ Vehicle *previous; // NOSAVE: pointer to the previous vehicle in the chain
+ Vehicle *first; // NOSAVE: pointer to the first vehicle in the chain
public:
friend const SaveLoad *GetVehicleDescription(VehicleType vt); // So we can use private/protected variables in the saveload code
+ friend void AfterLoadVehicles();
- Vehicle *first; // NOSAVE: pointer to the first vehicle in the chain
Vehicle *depot_list; // NOSAVE: linked list to tell what vehicles entered a depot during the last tick. Used by autoreplace
StringID string_id; // Displayed string
@@ -472,7 +475,7 @@ public:
* Set the next vehicle of this vehicle.
* @param next the next vehicle. NULL removes the next vehicle.
*/
- void SetNext(Vehicle *next) { this->next = next; }
+ void SetNext(Vehicle *next);
/**
* Get the next vehicle of this vehicle.
@@ -480,6 +483,19 @@ public:
* @return the next vehicle or NULL when there isn't a next vehicle.
*/
inline Vehicle *Next() const { return this->next; }
+
+ /**
+ * Get the previous vehicle of this vehicle.
+ * @note articulated parts are also counted as vehicles.
+ * @return the previous vehicle or NULL when there isn't a previous vehicle.
+ */
+ inline Vehicle *Previous() const { return this->previous; }
+
+ /**
+ * Get the first vehicle of this vehicle chain.
+ * @return the first vehicle of the chain.
+ */
+ inline Vehicle *First() const { return this->first; }
};
/**
@@ -556,10 +572,7 @@ typedef void *VehicleFromPosProc(Vehicle *v, void *data);
void VehicleServiceInDepot(Vehicle *v);
void VehiclePositionChanged(Vehicle *v);
-void AfterLoadVehicles();
Vehicle *GetLastVehicleInChain(Vehicle *v);
-Vehicle *GetPrevVehicleInChain(const Vehicle *v);
-Vehicle *GetFirstVehicleInChain(const Vehicle *v);
uint CountVehiclesInChain(const Vehicle *v);
bool IsEngineCountable(const Vehicle *v);
void DeleteVehicleChain(Vehicle *v);