# vehicle_base.h

Go to the documentation of this file.

    00001 /* $Id: vehicle_base.h 15434 2009-02-09 21:20:05Z rubidium $ */
    00002
    00005 #ifndef VEHICLE_BASE_H
    00006 #define VEHICLE_BASE_H
    00007
    00008 #include "vehicle_type.h"
    00009 #include "track_type.h"
    00010 #include "rail_type.h"
    00011 #include "road_type.h"
    00012 #include "cargo_type.h"
    00013 #include "direction_type.h"
    00014 #include "gfx_type.h"
    00015 #include "command_type.h"
    00016 #include "date_type.h"
    00017 #include "company_base.h"
    00018 #include "company_type.h"
    00019 #include "oldpool.h"
    00020 #include "order_base.h"
    00021 #include "cargopacket.h"
    00022 #include "texteff.hpp"
    00023 #include "group_type.h"
    00024 #include "engine_type.h"
    00025 #include "order_func.h"
    00026 #include "transport_type.h"
    00027
    00029 enum RoadVehicleStates {
    00030   /*
    00031    * Lower 4 bits are used for vehicle track direction. (Trackdirs)
    00032    * When in a road stop (bit 5 or bit 6 set) these bits give the
    00033    * track direction of the entry to the road stop.
    00034    * As the entry direction will always be a diagonal
    00035    * direction (X_NE, Y_SE, X_SW or Y_NW) only bits 0 and 3
    00036    * are needed to hold this direction. Bit 1 is then used to show
    00037    * that the vehicle is using the second road stop bay.
    00038    * Bit 2 is then used for drive-through stops to show the vehicle
    00039    * is stopping at this road stop.
    00040    */
    00041
    00042   /* Numeric values */
    00043   RVSB_IN_DEPOT                = 0xFE,
    00044   RVSB_WORMHOLE                = 0xFF,
    00045
    00046   /* Bit numbers */
    00047   RVS_USING_SECOND_BAY         =    1,
    00048   RVS_IS_STOPPING              =    2,
    00049   RVS_DRIVE_SIDE               =    4,
    00050   RVS_IN_ROAD_STOP             =    5,
    00051   RVS_IN_DT_ROAD_STOP          =    6,
    00052
    00053   /* Bit sets of the above specified bits */
    00054   RVSB_IN_ROAD_STOP            = 1 << RVS_IN_ROAD_STOP,
    00055   RVSB_IN_ROAD_STOP_END        = RVSB_IN_ROAD_STOP + TRACKDIR_END,
    00056   RVSB_IN_DT_ROAD_STOP         = 1 << RVS_IN_DT_ROAD_STOP,
    00057   RVSB_IN_DT_ROAD_STOP_END     = RVSB_IN_DT_ROAD_STOP + TRACKDIR_END,
    00058
    00059   RVSB_TRACKDIR_MASK           = 0x0F,
    00060   RVSB_ROAD_STOP_TRACKDIR_MASK = 0x09
    00061 };
    00062
    00063 enum VehStatus {
    00064   VS_HIDDEN          = 0x01,
    00065   VS_STOPPED         = 0x02,
    00066   VS_UNCLICKABLE     = 0x04,
    00067   VS_DEFPAL          = 0x08,
    00068   VS_TRAIN_SLOWING   = 0x10,
    00069   VS_SHADOW          = 0x20,
    00070   VS_AIRCRAFT_BROKEN = 0x40,
    00071   VS_CRASHED         = 0x80,
    00072 };
    00073
    00074 enum VehicleFlags {
    00075   VF_LOADING_FINISHED,
    00076   VF_CARGO_UNLOADING,
    00077   VF_BUILT_AS_PROTOTYPE,
    00078   VF_TIMETABLE_STARTED,
    00079   VF_AUTOFILL_TIMETABLE,
    00080   VF_AUTOFILL_PRES_WAIT_TIME,
    00081 };
    00082
    00083 struct VehicleRail {
    00084   /* Link between the two ends of a multiheaded engine */
    00085   Vehicle *other_multiheaded_part;
    00086
    00087   /* Cached wagon override spritegroup */
    00088   const struct SpriteGroup *cached_override;
    00089
    00090   uint16 last_speed; // NOSAVE: only used in UI
    00091   uint16 crash_anim_pos;
    00092
    00093   /* cached values, recalculated on load and each time a vehicle is added to/removed from the consist. */
    00094   uint32 cached_power;
    00095   uint16 cached_max_speed;
    00096   uint16 cached_total_length;
    00097   uint8 cached_veh_length;
    00098   bool cached_tilt;
    00099
    00100   /* cached values, recalculated when the cargo on a train changes (in addition to the conditions above) */
    00101   uint32 cached_weight;
    00102   uint32 cached_veh_weight;
    00103   uint32 cached_max_te;
    00104
    00112   byte cached_vis_effect;
    00113   byte user_def_data;
    00114
    00115   /* NOSAVE: for wagon override - id of the first engine in train
    00116    * 0xffff == not in train */
    00117   EngineID first_engine;
    00118
    00119   uint16 flags;
    00120   TrackBitsByte track;
    00121   byte force_proceed;
    00122   RailTypeByte railtype;
    00123   RailTypes compatible_railtypes;
    00124 };
    00125
    00126 enum VehicleRailFlags {
    00127   VRF_REVERSING         = 0,
    00128
    00129   /* used to calculate if train is going up or down */
    00130   VRF_GOINGUP           = 1,
    00131   VRF_GOINGDOWN         = 2,
    00132
    00133   /* used to store if a wagon is powered or not */
    00134   VRF_POWEREDWAGON      = 3,
    00135
    00136   /* used to reverse the visible direction of the vehicle */
    00137   VRF_REVERSE_DIRECTION = 4,
    00138
    00139   /* used to mark train as lost because PF can't find the route */
    00140   VRF_NO_PATH_TO_DESTINATION = 5,
    00141
    00142   /* used to mark that electric train engine is allowed to run on normal rail */
    00143   VRF_EL_ENGINE_ALLOWED_NORMAL_RAIL = 6,
    00144
    00145   /* used for vehicle var 0xFE bit 8 (toggled each time the train is reversed, accurate for first vehicle only) */
    00146   VRF_TOGGLE_REVERSE = 7,
    00147
    00148   /* used to mark a train that can't get a path reservation */
    00149   VRF_TRAIN_STUCK    = 8,
    00150 };
    00151
    00152 struct VehicleAir {
    00153   uint16 crashed_counter;
    00154   uint16 cached_max_speed;
    00155   byte pos;
    00156   byte previous_pos;
    00157   StationID targetairport;
    00158   byte state;
    00159 };
    00160
    00161 struct VehicleRoad {
    00162   byte state;
    00163   byte frame;
    00164   uint16 blocked_ctr;
    00165   byte overtaking;
    00166   byte overtaking_ctr;
    00167   uint16 crashed_ctr;
    00168   byte reverse_ctr;
    00169   struct RoadStop *slot;
    00170   byte slot_age;
    00171   EngineID first_engine;
    00172   byte cached_veh_length;
    00173
    00174   RoadType roadtype;
    00175   RoadTypes compatible_roadtypes;
    00176 };
    00177
    00178 struct VehicleEffect {
    00179   uint16 animation_state;
    00180   byte animation_substate;
    00181 };
    00182
    00183 struct VehicleDisaster {
    00184   uint16 image_override;
    00185   VehicleID big_ufo_destroyer_target;
    00186 };
    00187
    00188 struct VehicleShip {
    00189   TrackBitsByte state;
    00190 };
    00191
    00192 DECLARE_OLD_POOL(Vehicle, Vehicle, 9, 125)
    00193
    00194 /* Some declarations of functions, so we can make them friendly */
    00195 struct SaveLoad;
    00196 extern const SaveLoad *GetVehicleDescription(VehicleType vt);
    00197 struct LoadgameState;
    00198 extern bool LoadOldVehicle(LoadgameState *ls, int num);
    00199
    00200 struct Vehicle : PoolItem<Vehicle, VehicleID, &_Vehicle_pool>, BaseVehicle {
    00201 private:
    00202   Vehicle *next;
    00203   Vehicle *previous;
    00204   Vehicle *first;
    00205
    00206   Vehicle *next_shared;
    00207   Vehicle *previous_shared;
    00208 public:
    00209   friend const SaveLoad *GetVehicleDescription(VehicleType vt);
    00210   friend void AfterLoadVehicles(bool part_of_load);
    00211   friend bool LoadOldVehicle(LoadgameState *ls, int num);
    00212
    00213   char *name;
    00214
    00215   TileIndex tile;
    00216
    00222   TileIndex dest_tile;
    00223
    00224   Money profit_this_year;
    00225   Money profit_last_year;
    00226   Money value;
    00227
    00228   /* Used for timetabling. */
    00229   uint32 current_order_time;
    00230   int32 lateness_counter;
    00231
    00232   /* Boundaries for the current position in the world and a next hash link.
    00233    * NOSAVE: All of those can be updated with VehiclePositionChanged() */
    00234   int32 left_coord;
    00235   int32 top_coord;
    00236   int32 right_coord;
    00237   int32 bottom_coord;
    00238   Vehicle *next_hash;
    00239   Vehicle *next_new_hash;
    00240   Vehicle **old_new_hash;
    00241
    00242   SpriteID colourmap; // NOSAVE: cached colour mapping
    00243
    00244   /* Related to age and service time */
    00245   Year build_year;
    00246   Date age;     // Age in days
    00247   Date max_age; // Maximum age
    00248   Date date_of_last_service;
    00249   Date service_interval;
    00250   uint16 reliability;
    00251   uint16 reliability_spd_dec;
    00252   byte breakdown_ctr;
    00253   byte breakdown_delay;
    00254   byte breakdowns_since_last_service;
    00255   byte breakdown_chance;
    00256
    00257   int32 x_pos;             // coordinates
    00258   int32 y_pos;
    00259   byte z_pos;
    00260   DirectionByte direction; // facing
    00261
    00262   OwnerByte owner;         // which company owns the vehicle?
    00263   byte spritenum;          // currently displayed sprite index
    00264                            // 0xfd == custom sprite, 0xfe == custom second head sprite
    00265                            // 0xff == reserved for another custom sprite
    00266   uint16 cur_image;        // sprite number for this vehicle
    00267   byte x_extent;           // x-extent of vehicle bounding box
    00268   byte y_extent;           // y-extent of vehicle bounding box
    00269   byte z_extent;           // z-extent of vehicle bounding box
    00270   int8 x_offs;             // x offset for vehicle sprite
    00271   int8 y_offs;             // y offset for vehicle sprite
    00272   EngineID engine_type;
    00273
    00274   TextEffectID fill_percent_te_id; // a text-effect id to a loading indicator object
    00275   UnitID unitnumber;       // unit number, for display purposes only
    00276
    00277   uint16 max_speed;
    00278   uint16 cur_speed;
    00279   byte subspeed;
    00280   byte acceleration;
    00281   uint32 motion_counter;
    00282   byte progress;
    00283
    00284   /* for randomized variational spritegroups
    00285    * bitmask used to resolve them; parts of it get reseeded when triggers
    00286    * of corresponding spritegroups get matched */
    00287   byte random_bits;
    00288   byte waiting_triggers;
    00289
    00290   StationID last_station_visited;
    00291
    00292   CargoID cargo_type;
    00293   byte cargo_subtype;
    00294   uint16 cargo_cap;
    00295   CargoList cargo;
    00296
    00297   byte day_counter;
    00298   byte tick_counter;
    00299   byte running_ticks;
    00300
    00301   byte vehstatus;
    00302   Order current_order;
    00303   VehicleOrderID cur_order_index;
    00304
    00305   union {
    00306     OrderList *list;
    00307     Order     *old;
    00308   } orders;
    00309
    00310   byte vehicle_flags;
    00311   uint16 load_unload_time_rem;
    00312
    00313   GroupID group_id;
    00314
    00315   byte subtype;
    00316
    00317   union {
    00318     VehicleRail rail;
    00319     VehicleAir air;
    00320     VehicleRoad road;
    00321     VehicleEffect effect;
    00322     VehicleDisaster disaster;
    00323     VehicleShip ship;
    00324   } u;
    00325
    00326
    00333   static bool AllocateList(Vehicle **vl, int num);
    00334
    00336   Vehicle();
    00337
    00339   void PreDestructor();
    00341   virtual ~Vehicle();
    00342
    00343   void BeginLoading();
    00344   void LeaveStation();
    00345
    00351   void HandleLoading(bool mode = false);
    00352
    00357   virtual const char *GetTypeString() const { return "base vehicle"; }
    00358
    00367   virtual void MarkDirty() {}
    00368
    00374   virtual void UpdateDeltaXY(Direction direction) {}
    00375
    00380   virtual ExpensesType GetExpenseType(bool income) const { return EXPENSES_OTHER; }
    00381
    00385   virtual void PlayLeaveStationSound() const {}
    00386
    00390   virtual bool IsPrimaryVehicle() const { return false; }
    00391
    00397   virtual SpriteID GetImage(Direction direction) const { return 0; }
    00398
    00403   virtual int GetDisplaySpeed() const { return 0; }
    00404
    00409   virtual int GetDisplayMaxSpeed() const { return 0; }
    00410
    00415   virtual Money GetRunningCost() const { return 0; }
    00416
    00421   virtual bool IsInDepot() const { return false; }
    00422
    00427   virtual bool IsStoppedInDepot() const { return this->IsInDepot() && (this->vehstatus & VS_STOPPED) != 0; }
    00428
    00432   virtual void Tick() {};
    00433
    00437   virtual void OnNewDay() {};
    00438
    00443   Money GetDisplayRunningCost() const { return (this->GetRunningCost() >> 8); }
    00444
    00449   Money GetDisplayProfitThisYear() const { return (this->profit_this_year >> 8); }
    00450
    00455   Money GetDisplayProfitLastYear() const { return (this->profit_last_year >> 8); }
    00456
    00461   void SetNext(Vehicle *next);
    00462
    00468   inline Vehicle *Next() const { return this->next; }
    00469
    00475   inline Vehicle *Previous() const { return this->previous; }
    00476
    00481   inline Vehicle *First() const { return this->first; }
    00482
    00483
    00488   inline Order *GetFirstOrder() const { return (this->orders.list == NULL) ? NULL : this->orders.list->GetFirstOrder(); }
    00489
    00495   void AddToShared(Vehicle *shared_chain);
    00496
    00500   void RemoveFromShared();
    00501
    00506   inline Vehicle *NextShared() const { return this->next_shared; }
    00507
    00512   inline Vehicle *PreviousShared() const { return this->previous_shared; }
    00513
    00518   inline Vehicle *FirstShared() const { return (this->orders.list == NULL) ? this->First() : this->orders.list->GetFirstSharedVehicle(); }
    00519
    00524   inline bool IsOrderListShared() const { return this->orders.list != NULL && this->orders.list->IsShared(); }
    00525
    00530   inline VehicleOrderID GetNumOrders() const { return (this->orders.list == NULL) ? 0 : this->orders.list->GetNumOrders(); }
    00531
    00538   inline void CopyVehicleConfigAndStatistics(const Vehicle *src)
    00539   {
    00540     this->unitnumber = src->unitnumber;
    00541
    00542     this->cur_order_index = src->cur_order_index;
    00543     this->current_order = src->current_order;
    00544     this->dest_tile  = src->dest_tile;
    00545
    00546     this->profit_this_year = src->profit_this_year;
    00547     this->profit_last_year = src->profit_last_year;
    00548
    00549     this->current_order_time = src->current_order_time;
    00550     this->lateness_counter = src->lateness_counter;
    00551
    00552     this->service_interval = src->service_interval;
    00553   }
    00554
    00555   bool NeedsAutorenewing(const Company *c) const;
    00556
    00563   bool NeedsServicing() const;
    00564
    00570   bool NeedsAutomaticServicing() const;
    00571
    00579   virtual TileIndex GetOrderStationLocation(StationID station) { return INVALID_TILE; }
    00580
    00589   virtual bool FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse) { return false; }
    00590
    00597   CommandCost SendToDepot(DoCommandFlag flags, DepotCommand command);
    00598 };
    00599
    00608 struct DisasterVehicle : public Vehicle {
    00610   DisasterVehicle() { this->type = VEH_DISASTER; }
    00611
    00613   virtual ~DisasterVehicle() {}
    00614
    00615   const char *GetTypeString() const { return "disaster vehicle"; }
    00616   void UpdateDeltaXY(Direction direction);
    00617   void Tick();
    00618 };
    00619
    00628 struct InvalidVehicle : public Vehicle {
    00630   InvalidVehicle() { this->type = VEH_INVALID; }
    00631
    00633   virtual ~InvalidVehicle() {}
    00634
    00635   const char *GetTypeString() const { return "invalid vehicle"; }
    00636   void Tick() {}
    00637 };
    00638
    00639 static inline VehicleID GetMaxVehicleIndex()
    00640 {
    00641   /* TODO - This isn't the real content of the function, but
    00642    *  with the new pool-system this will be replaced with one that
    00643    *  _really_ returns the highest index. Now it just returns
    00644    *  the next safe value we are sure about everything is below.
    00645    */
    00646   return GetVehiclePoolSize() - 1;
    00647 }
    00648
    00649 static inline uint GetNumVehicles()
    00650 {
    00651   return GetVehiclePoolSize();
    00652 }
    00653
    00654 #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())
    00655 #define FOR_ALL_VEHICLES(v) FOR_ALL_VEHICLES_FROM(v, 0)
    00656
    00662 static inline bool IsValidVehicleID(uint index)
    00663 {
    00664   return index < GetVehiclePoolSize() && GetVehicle(index)->IsValid();
    00665 }
    00666
    00667
    00669 struct FreeUnitIDGenerator {
    00670   bool *cache;
    00671   UnitID maxid;
    00672   UnitID curid;
    00673
    00680   FreeUnitIDGenerator(VehicleType type, CompanyID owner);
    00681
    00683   UnitID NextID();
    00684
    00686   ~FreeUnitIDGenerator() { free(this->cache); }
    00687 };
    00688
    00689 /* Returns order 'index' of a vehicle or NULL when it doesn't exists */
    00690 static inline Order *GetVehicleOrder(const Vehicle *v, int index) { return (v->orders.list == NULL) ? NULL : v->orders.list->GetOrderAt(index); }
    00691
    00697 static inline Order *GetLastVehicleOrder(const Vehicle *v) { return (v->orders.list == NULL) ? NULL : v->orders.list->GetLastOrder(); }
    00698
    00710 Trackdir GetVehicleTrackdir(const Vehicle *v);
    00711
    00712 void CheckVehicle32Day(Vehicle *v);
    00713
    00714 static const int32 INVALID_COORD = 0x7fffffff;
    00715
    00716 #endif /* VEHICLE_BASE_H */