src/vehicle.h
branchnoai
changeset 9701 d1ac22c62f64
parent 9694 e72987579514
child 9703 d2a6acdbd665
equal deleted inserted replaced
9700:e442ce398e83 9701:d1ac22c62f64
   216 };
   216 };
   217 
   217 
   218 struct Vehicle;
   218 struct Vehicle;
   219 DECLARE_OLD_POOL(Vehicle, Vehicle, 9, 125)
   219 DECLARE_OLD_POOL(Vehicle, Vehicle, 9, 125)
   220 
   220 
       
   221 struct SaveLoad;
       
   222 const SaveLoad *GetVehicleDescription(VehicleType vt);
       
   223 void AfterLoadVehicles();
       
   224 
   221 struct Vehicle : PoolItem<Vehicle, VehicleID, &_Vehicle_pool> {
   225 struct Vehicle : PoolItem<Vehicle, VehicleID, &_Vehicle_pool> {
   222 	VehicleTypeByte type;    ///< Type of vehicle
   226 	VehicleTypeByte type;    ///< Type of vehicle
   223 	byte subtype;            // subtype (Filled with values from EffectVehicles/TrainSubTypes/AircraftSubTypes)
   227 	byte subtype;            // subtype (Filled with values from EffectVehicles/TrainSubTypes/AircraftSubTypes)
   224 
   228 
   225 	Vehicle *next;           // next
   229 private:
       
   230 	Vehicle *next;           // pointer to the next vehicle in the chain
       
   231 	Vehicle *previous;       // NOSAVE: pointer to the previous vehicle in the chain
   226 	Vehicle *first;          // NOSAVE: pointer to the first vehicle in the chain
   232 	Vehicle *first;          // NOSAVE: pointer to the first vehicle in the chain
   227 	Vehicle *depot_list;     //NOSAVE: linked list to tell what vehicles entered a depot during the last tick. Used by autoreplace
   233 public:
       
   234 	friend const SaveLoad *GetVehicleDescription(VehicleType vt); // So we can use private/protected variables in the saveload code
       
   235 	friend void AfterLoadVehicles();
       
   236 
       
   237 	Vehicle *depot_list;     // NOSAVE: linked list to tell what vehicles entered a depot during the last tick. Used by autoreplace
   228 
   238 
   229 	StringID string_id;      // Displayed string
   239 	StringID string_id;      // Displayed string
   230 
   240 
   231 	UnitID unitnumber;       // unit number, for display purposes only
   241 	UnitID unitnumber;       // unit number, for display purposes only
   232 	PlayerByte owner;        // which player owns the vehicle?
   242 	PlayerByte owner;        // which player owns the vehicle?
   345 	static bool AllocateList(Vehicle **vl, int num);
   355 	static bool AllocateList(Vehicle **vl, int num);
   346 
   356 
   347 	/** Create a new vehicle */
   357 	/** Create a new vehicle */
   348 	Vehicle();
   358 	Vehicle();
   349 
   359 
       
   360 	/** Destroy all stuff that (still) needs the virtual functions to work properly */
       
   361 	void PreDestructor();
   350 	/** We want to 'destruct' the right class. */
   362 	/** We want to 'destruct' the right class. */
   351 	virtual ~Vehicle();
   363 	virtual ~Vehicle();
   352 
       
   353 	void QuickFree();
       
   354 
   364 
   355 	void BeginLoading();
   365 	void BeginLoading();
   356 	void LeaveStation();
   366 	void LeaveStation();
   357 
   367 
   358 	/**
   368 	/**
   413 	 * @return the sprite for the given vehicle in the given direction
   423 	 * @return the sprite for the given vehicle in the given direction
   414 	 */
   424 	 */
   415 	virtual int GetImage(Direction direction) const { return 0; }
   425 	virtual int GetImage(Direction direction) const { return 0; }
   416 
   426 
   417 	/**
   427 	/**
       
   428 	 * Gets the speed in mph that can be sent into SetDParam for string processing.
       
   429 	 * @return the vehicle's speed
       
   430 	 */
       
   431 	virtual int GetDisplaySpeed() const { return 0; }
       
   432 
       
   433 	/**
       
   434 	 * Gets the maximum speed in mph that can be sent into SetDParam for string processing.
       
   435 	 * @return the vehicle's maximum speed
       
   436 	 */
       
   437 	virtual int GetDisplayMaxSpeed() const { return 0; }
       
   438 
       
   439 	/**
       
   440 	 * Gets the running cost of a vehicle
       
   441 	 * @return the vehicle's running cost
       
   442 	 */
       
   443 	virtual Money GetRunningCost() const { return 0; }
       
   444 
       
   445 	/**
       
   446 	 * Check whether the vehicle is in the depot.
       
   447 	 * @return true if and only if the vehicle is in the depot.
       
   448 	 */
       
   449 	virtual bool IsInDepot() const { return false; }
       
   450 
       
   451 	/**
       
   452 	 * Check whether the vehicle is in the depot *and* stopped.
       
   453 	 * @return true if and only if the vehicle is in the depot and stopped.
       
   454 	 */
       
   455 	virtual bool IsStoppedInDepot() const { return this->IsInDepot() && (this->vehstatus & VS_STOPPED) != 0; }
       
   456 
       
   457 	/**
   418 	 * Calls the tick handler of the vehicle
   458 	 * Calls the tick handler of the vehicle
   419 	 */
   459 	 */
   420 	virtual void Tick() {};
   460 	virtual void Tick() {};
   421 
   461 
   422 	bool IsValid() const { return this->type != VEH_INVALID; }
   462 	/**
       
   463 	 * Gets the running cost of a vehicle  that can be sent into SetDParam for string processing.
       
   464 	 * @return the vehicle's running cost
       
   465 	 */
       
   466 	Money GetDisplayRunningCost() const { return (this->GetRunningCost() >> 8); }
       
   467 
       
   468 	/**
       
   469 	 * Is this vehicle a valid vehicle?
       
   470 	 * @return true if and only if the vehicle is valid.
       
   471 	 */
       
   472 	inline bool IsValid() const { return this->type != VEH_INVALID; }
       
   473 
       
   474 	/**
       
   475 	 * Set the next vehicle of this vehicle.
       
   476 	 * @param next the next vehicle. NULL removes the next vehicle.
       
   477 	 */
       
   478 	void SetNext(Vehicle *next);
       
   479 
       
   480 	/**
       
   481 	 * Get the next vehicle of this vehicle.
       
   482 	 * @note articulated parts are also counted as vehicles.
       
   483 	 * @return the next vehicle or NULL when there isn't a next vehicle.
       
   484 	 */
       
   485 	inline Vehicle *Next() const { return this->next; }
       
   486 
       
   487 	/**
       
   488 	 * Get the previous vehicle of this vehicle.
       
   489 	 * @note articulated parts are also counted as vehicles.
       
   490 	 * @return the previous vehicle or NULL when there isn't a previous vehicle.
       
   491 	 */
       
   492 	inline Vehicle *Previous() const { return this->previous; }
       
   493 
       
   494 	/**
       
   495 	 * Get the first vehicle of this vehicle chain.
       
   496 	 * @return the first vehicle of the chain.
       
   497 	 */
       
   498 	inline Vehicle *First() const { return this->first; }
   423 };
   499 };
   424 
   500 
   425 /**
   501 /**
   426  * This class 'wraps' Vehicle; you do not actually instantiate this class.
   502  * This class 'wraps' Vehicle; you do not actually instantiate this class.
   427  * You create a Vehicle using AllocateVehicle, so it is added to the pool
   503  * You create a Vehicle using AllocateVehicle, so it is added to the pool
   494 
   570 
   495 typedef void *VehicleFromPosProc(Vehicle *v, void *data);
   571 typedef void *VehicleFromPosProc(Vehicle *v, void *data);
   496 
   572 
   497 void VehicleServiceInDepot(Vehicle *v);
   573 void VehicleServiceInDepot(Vehicle *v);
   498 void VehiclePositionChanged(Vehicle *v);
   574 void VehiclePositionChanged(Vehicle *v);
   499 void AfterLoadVehicles();
       
   500 Vehicle *GetLastVehicleInChain(Vehicle *v);
   575 Vehicle *GetLastVehicleInChain(Vehicle *v);
   501 Vehicle *GetPrevVehicleInChain(const Vehicle *v);
       
   502 Vehicle *GetFirstVehicleInChain(const Vehicle *v);
       
   503 uint CountVehiclesInChain(const Vehicle *v);
   576 uint CountVehiclesInChain(const Vehicle *v);
   504 bool IsEngineCountable(const Vehicle *v);
   577 bool IsEngineCountable(const Vehicle *v);
   505 void DeleteVehicleChain(Vehicle *v);
   578 void DeleteVehicleChain(Vehicle *v);
   506 void *VehicleFromPos(TileIndex tile, void *data, VehicleFromPosProc *proc);
   579 void *VehicleFromPos(TileIndex tile, void *data, VehicleFromPosProc *proc);
   507 void *VehicleFromPosXY(int x, int y, void *data, VehicleFromPosProc *proc);
   580 void *VehicleFromPosXY(int x, int y, void *data, VehicleFromPosProc *proc);
   541 void VehicleEnteredDepotThisTick(Vehicle *v);
   614 void VehicleEnteredDepotThisTick(Vehicle *v);
   542 
   615 
   543 void BeginVehicleMove(Vehicle *v);
   616 void BeginVehicleMove(Vehicle *v);
   544 void EndVehicleMove(Vehicle *v);
   617 void EndVehicleMove(Vehicle *v);
   545 
   618 
   546 void ShowAircraftViewWindow(const Vehicle* v);
       
   547 
       
   548 UnitID GetFreeUnitNumber(VehicleType type);
   619 UnitID GetFreeUnitNumber(VehicleType type);
   549 
   620 
   550 void TrainConsistChanged(Vehicle *v);
   621 void TrainConsistChanged(Vehicle *v);
   551 void TrainPowerChanged(Vehicle *v);
   622 void TrainPowerChanged(Vehicle *v);
   552 Money GetTrainRunningCost(const Vehicle *v);
   623 Money GetTrainRunningCost(const Vehicle *v);
   553 
   624 
   554 int CheckTrainStoppedInDepot(const Vehicle *v);
       
   555 
       
   556 bool VehicleNeedsService(const Vehicle *v);
   625 bool VehicleNeedsService(const Vehicle *v);
   557 
   626 
   558 uint GenerateVehicleSortList(const Vehicle*** sort_list, uint16 *length_of_array, VehicleType type, PlayerID owner, uint32 index, uint16 window_type);
   627 uint GenerateVehicleSortList(const Vehicle*** sort_list, uint16 *length_of_array, VehicleType type, PlayerID owner, uint32 index, uint16 window_type);
   559 void BuildDepotVehicleList(VehicleType type, TileIndex tile, Vehicle ***engine_list, uint16 *engine_list_length, uint16 *engine_count, Vehicle ***wagon_list, uint16 *wagon_list_length, uint16 *wagon_count);
   628 void BuildDepotVehicleList(VehicleType type, TileIndex tile, Vehicle ***engine_list, uint16 *engine_list_length, uint16 *engine_count, Vehicle ***wagon_list, uint16 *wagon_list_length, uint16 *wagon_count);
   560 CommandCost SendAllVehiclesToDepot(VehicleType type, uint32 flags, bool service, PlayerID owner, uint16 vlw_flag, uint32 id);
   629 CommandCost SendAllVehiclesToDepot(VehicleType type, uint32 flags, bool service, PlayerID owner, uint16 vlw_flag, uint32 id);
   561 bool IsVehicleInDepot(const Vehicle *v);
       
   562 void VehicleEnterDepot(Vehicle *v);
   630 void VehicleEnterDepot(Vehicle *v);
   563 
   631 
   564 void InvalidateAutoreplaceWindow(EngineID e, GroupID id_g);
   632 void InvalidateAutoreplaceWindow(EngineID e, GroupID id_g);
   565 
   633 
   566 CommandCost MaybeReplaceVehicle(Vehicle *v, bool check, bool display_costs);
   634 CommandCost MaybeReplaceVehicle(Vehicle *v, bool check, bool display_costs);
   567 bool CanBuildVehicleInfrastructure(VehicleType type);
   635 bool CanBuildVehicleInfrastructure(VehicleType type);
       
   636 
       
   637 void CcCloneVehicle(bool success, TileIndex tile, uint32 p1, uint32 p2);
   568 
   638 
   569 /* Flags to add to p2 for goto depot commands */
   639 /* Flags to add to p2 for goto depot commands */
   570 /* Note: bits 8-10 are used for VLW flags */
   640 /* Note: bits 8-10 are used for VLW flags */
   571 enum {
   641 enum {
   572 	DEPOT_SERVICE       = (1 << 0), // The vehicle will leave the depot right after arrival (serivce only)
   642 	DEPOT_SERVICE       = (1 << 0), // The vehicle will leave the depot right after arrival (serivce only)
   597 /* returns true if staying in the same tile */
   667 /* returns true if staying in the same tile */
   598 GetNewVehiclePosResult GetNewVehiclePos(const Vehicle *v);
   668 GetNewVehiclePosResult GetNewVehiclePos(const Vehicle *v);
   599 Direction GetDirectionTowards(const Vehicle *v, int x, int y);
   669 Direction GetDirectionTowards(const Vehicle *v, int x, int y);
   600 
   670 
   601 #define BEGIN_ENUM_WAGONS(v) do {
   671 #define BEGIN_ENUM_WAGONS(v) do {
   602 #define END_ENUM_WAGONS(v) } while ((v = v->next) != NULL);
   672 #define END_ENUM_WAGONS(v) } while ((v = v->Next()) != NULL);
   603 
   673 
   604 static inline VehicleID GetMaxVehicleIndex()
   674 static inline VehicleID GetMaxVehicleIndex()
   605 {
   675 {
   606 	/* TODO - This isn't the real content of the function, but
   676 	/* TODO - This isn't the real content of the function, but
   607 	 *  with the new pool-system this will be replaced with one that
   677 	 *  with the new pool-system this will be replaced with one that