src/vehicle.h
branchnoai
changeset 9694 e72987579514
parent 9631 8a2d1c2ceb88
child 9701 d1ac22c62f64
--- a/src/vehicle.h	Fri Aug 03 19:16:36 2007 +0000
+++ b/src/vehicle.h	Fri Aug 03 22:09:42 2007 +0000
@@ -202,26 +202,26 @@
 };
 
 struct VehicleSpecial {
-	uint16 unk0;
-	byte unk2;
+	uint16 animation_state;
+	byte animation_substate;
 };
 
 struct VehicleDisaster {
 	uint16 image_override;
-	uint16 unk2;
+	VehicleID big_ufo_destroyer_target;
 };
 
 struct VehicleShip {
 	TrackBitsByte state;
 };
 
+struct Vehicle;
+DECLARE_OLD_POOL(Vehicle, Vehicle, 9, 125)
 
-struct Vehicle {
+struct Vehicle : PoolItem<Vehicle, VehicleID, &_Vehicle_pool> {
 	VehicleTypeByte type;    ///< Type of vehicle
 	byte subtype;            // subtype (Filled with values from EffectVehicles/TrainSubTypes/AircraftSubTypes)
 
-	VehicleID index;         // NOSAVE: Index in vehicle array
-
 	Vehicle *next;           // next
 	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
@@ -335,6 +335,23 @@
 		VehicleShip ship;
 	} u;
 
+
+	/**
+	 * Allocates a lot of vehicles.
+	 * @param vl pointer to an array of vehicles to get allocated. Can be NULL if the vehicles aren't needed (makes it test only)
+	 * @param num number of vehicles to allocate room for
+	 * @return true if there is room to allocate all the vehicles
+	 */
+	static bool AllocateList(Vehicle **vl, int num);
+
+	/** Create a new vehicle */
+	Vehicle();
+
+	/** We want to 'destruct' the right class. */
+	virtual ~Vehicle();
+
+	void QuickFree();
+
 	void BeginLoading();
 	void LeaveStation();
 
@@ -346,41 +363,10 @@
 	void HandleLoading(bool mode = false);
 
 	/**
-	 * An overriden version of new, so you can use the vehicle instance
-	 * instead of a newly allocated piece of memory.
-	 * @param size the size of the variable (unused)
-	 * @param v    the vehicle to use as 'storage' backend
-	 * @return the memory that is 'allocated'
-	 */
-	void* operator new(size_t size, Vehicle *v) { return v; }
-
-	/**
-	 * 'Free' the memory allocated by the overriden new.
-	 * @param p the memory to 'free'
-	 * @param v the vehicle that was given to 'new' on creation.
-	 * @note This function isn't used (at the moment) and only added
-	 *       to please some compiler.
-	 */
-	void operator delete(void *p, Vehicle *v) {}
-
-	/**
-	 * 'Free' the memory allocated by the overriden new.
-	 * @param p the memory to 'free'
-	 * @note This function isn't used (at the moment) and only added
-	 *       as the above function was needed to please some compiler
-	 *       which made it necessary to add this to please yet
-	 *       another compiler...
-	 */
-	void operator delete(void *p) {}
-
-	/** We want to 'destruct' the right class. */
-	virtual ~Vehicle() {}
-
-	/**
 	 * Get a string 'representation' of the vehicle type.
 	 * @return the string representation.
 	 */
-	virtual const char* GetTypeString() const = 0;
+	virtual const char* GetTypeString() const { return "base vehicle"; }
 
 	/**
 	 * Marks the vehicles to be redrawn and updates cached variables
@@ -431,7 +417,9 @@
 	/**
 	 * Calls the tick handler of the vehicle
 	 */
-	virtual void Tick() = 0;
+	virtual void Tick() {};
+
+	bool IsValid() const { return this->type != VEH_INVALID; }
 };
 
 /**
@@ -507,16 +495,12 @@
 typedef void *VehicleFromPosProc(Vehicle *v, void *data);
 
 void VehicleServiceInDepot(Vehicle *v);
-Vehicle *AllocateVehicle();
-bool AllocateVehicles(Vehicle **vl, int num);
-Vehicle *ForceAllocateVehicle();
-Vehicle *ForceAllocateSpecialVehicle();
 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);
+uint CountVehiclesInChain(const Vehicle *v);
 bool IsEngineCountable(const Vehicle *v);
 void DeleteVehicleChain(Vehicle *v);
 void *VehicleFromPos(TileIndex tile, void *data, VehicleFromPosProc *proc);
@@ -592,7 +576,7 @@
 };
 
 struct GetNewVehiclePosResult {
-	int x,y;
+	int x, y;
 	TileIndex old_tile;
 	TileIndex new_tile;
 };
@@ -612,12 +596,10 @@
 
 /* returns true if staying in the same tile */
 GetNewVehiclePosResult GetNewVehiclePos(const Vehicle *v);
-Direction GetDirectionTowards(const Vehicle* v, int x, int y);
+Direction GetDirectionTowards(const Vehicle *v, int x, int y);
 
 #define BEGIN_ENUM_WAGONS(v) do {
-#define END_ENUM_WAGONS(v) } while ( (v=v->next) != NULL);
-
-DECLARE_OLD_POOL(Vehicle, Vehicle, 9, 125)
+#define END_ENUM_WAGONS(v) } while ((v = v->next) != NULL);
 
 static inline VehicleID GetMaxVehicleIndex()
 {
@@ -634,22 +616,6 @@
 	return GetVehiclePoolSize();
 }
 
-/**
- * Check if a Vehicle really exists.
- */
-static inline bool IsValidVehicle(const Vehicle *v)
-{
-	return v->type != VEH_INVALID;
-}
-
-void DestroyVehicle(Vehicle *v);
-
-static inline void DeleteVehicle(Vehicle *v)
-{
-	DestroyVehicle(v);
-	v = new (v) InvalidVehicle();
-}
-
 static inline bool IsPlayerBuildableVehicleType(VehicleType type)
 {
 	switch (type) {
@@ -668,7 +634,7 @@
 	return IsPlayerBuildableVehicleType(v->type);
 }
 
-#define FOR_ALL_VEHICLES_FROM(v, start) for (v = GetVehicle(start); v != NULL; v = (v->index + 1U < GetVehiclePoolSize()) ? GetVehicle(v->index + 1) : NULL) if (IsValidVehicle(v))
+#define FOR_ALL_VEHICLES_FROM(v, start) for (v = GetVehicle(start); v != NULL; v = (v->index + 1U < GetVehiclePoolSize()) ? GetVehicle(v->index + 1) : NULL) if (v->IsValid())
 #define FOR_ALL_VEHICLES(v) FOR_ALL_VEHICLES_FROM(v, 0)
 
 /**
@@ -678,7 +644,7 @@
  */
 static inline bool IsValidVehicleID(uint index)
 {
-	return index < GetVehiclePoolSize() && IsValidVehicle(GetVehicle(index));
+	return index < GetVehiclePoolSize() && GetVehicle(index)->IsValid();
 }
 
 /* Returns order 'index' of a vehicle or NULL when it doesn't exists */