00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #ifndef VEHICLE_BASE_H
00013 #define VEHICLE_BASE_H
00014
00015 #include "track_type.h"
00016 #include "command_type.h"
00017 #include "order_base.h"
00018 #include "cargopacket.h"
00019 #include "texteff.hpp"
00020 #include "engine_type.h"
00021 #include "order_func.h"
00022 #include "transport_type.h"
00023 #include "group_type.h"
00024
00026 enum VehStatus {
00027 VS_HIDDEN = 0x01,
00028 VS_STOPPED = 0x02,
00029 VS_UNCLICKABLE = 0x04,
00030 VS_DEFPAL = 0x08,
00031 VS_TRAIN_SLOWING = 0x10,
00032 VS_SHADOW = 0x20,
00033 VS_AIRCRAFT_BROKEN = 0x40,
00034 VS_CRASHED = 0x80,
00035 };
00036
00038 enum VehicleFlags {
00039 VF_LOADING_FINISHED,
00040 VF_CARGO_UNLOADING,
00041 VF_BUILT_AS_PROTOTYPE,
00042 VF_TIMETABLE_STARTED,
00043 VF_AUTOFILL_TIMETABLE,
00044 VF_AUTOFILL_PRES_WAIT_TIME,
00045 VF_STOP_LOADING,
00046 VF_PATHFINDER_LOST,
00047 };
00048
00050 enum NewGRFCacheValidValues {
00051 NCVV_POSITION_CONSIST_LENGTH = 0,
00052 NCVV_POSITION_SAME_ID_LENGTH = 1,
00053 NCVV_CONSIST_CARGO_INFORMATION = 2,
00054 NCVV_COMPANY_INFORMATION = 3,
00055 NCVV_END,
00056 };
00057
00059 struct NewGRFCache {
00060
00061 uint32 position_consist_length;
00062 uint32 position_same_id_length;
00063 uint32 consist_cargo_information;
00064 uint32 company_information;
00065 uint8 cache_valid;
00066 };
00067
00069 enum VisualEffect {
00070 VE_OFFSET_START = 0,
00071 VE_OFFSET_COUNT = 4,
00072 VE_OFFSET_CENTRE = 8,
00073
00074 VE_TYPE_START = 4,
00075 VE_TYPE_COUNT = 2,
00076 VE_TYPE_DEFAULT = 0,
00077 VE_TYPE_STEAM = 1,
00078 VE_TYPE_DIESEL = 2,
00079 VE_TYPE_ELECTRIC = 3,
00080
00081 VE_DISABLE_EFFECT = 6,
00082 VE_DISABLE_WAGON_POWER = 7,
00083
00084 VE_DEFAULT = 0xFF,
00085 };
00086
00092 enum GroundVehicleSubtypeFlags {
00093 GVSF_FRONT = 0,
00094 GVSF_ARTICULATED_PART = 1,
00095 GVSF_WAGON = 2,
00096 GVSF_ENGINE = 3,
00097 GVSF_FREE_WAGON = 4,
00098 GVSF_MULTIHEADED = 5,
00099 };
00100
00102 struct VehicleCache {
00103 uint16 cached_max_speed;
00104 uint16 cached_cargo_age_period;
00105
00106 byte cached_vis_effect;
00107 };
00108
00110 typedef Pool<Vehicle, VehicleID, 512, 0xFF000> VehiclePool;
00111 extern VehiclePool _vehicle_pool;
00112
00113
00114 struct SaveLoad;
00115 struct GroundVehicleCache;
00116 extern const SaveLoad *GetVehicleDescription(VehicleType vt);
00117 struct LoadgameState;
00118 extern bool LoadOldVehicle(LoadgameState *ls, int num);
00119 extern void FixOldVehicles();
00120
00121 struct GRFFile;
00122
00124 struct Vehicle : VehiclePool::PoolItem<&_vehicle_pool>, BaseVehicle {
00125 private:
00126 Vehicle *next;
00127 Vehicle *previous;
00128 Vehicle *first;
00129
00130 Vehicle *next_shared;
00131 Vehicle *previous_shared;
00132 public:
00133 friend const SaveLoad *GetVehicleDescription(VehicleType vt);
00134 friend void FixOldVehicles();
00135 friend void AfterLoadVehicles(bool part_of_load);
00136 friend bool LoadOldVehicle(LoadgameState *ls, int num);
00137
00138 char *name;
00139
00140 TileIndex tile;
00141
00147 TileIndex dest_tile;
00148
00149 Money profit_this_year;
00150 Money profit_last_year;
00151 Money value;
00152
00153 CargoPayment *cargo_payment;
00154
00155
00156 uint32 current_order_time;
00157 int32 lateness_counter;
00158 Date timetable_start;
00159
00160 Rect coord;
00161
00162 Vehicle *next_hash;
00163 Vehicle **prev_hash;
00164
00165 Vehicle *next_new_hash;
00166 Vehicle **prev_new_hash;
00167 Vehicle **old_new_hash;
00168
00169 SpriteID colourmap;
00170
00171
00172 Year build_year;
00173 Date age;
00174 Date max_age;
00175 Date date_of_last_service;
00176 Date service_interval;
00177 uint16 reliability;
00178 uint16 reliability_spd_dec;
00179 byte breakdown_ctr;
00180 byte breakdown_delay;
00181 byte breakdowns_since_last_service;
00182 byte breakdown_chance;
00183
00184 int32 x_pos;
00185 int32 y_pos;
00186 int32 z_pos;
00187 DirectionByte direction;
00188
00189 OwnerByte owner;
00190
00195 byte spritenum;
00196 SpriteID cur_image;
00197 byte x_extent;
00198 byte y_extent;
00199 byte z_extent;
00200 int8 x_bb_offs;
00201 int8 y_bb_offs;
00202 int8 x_offs;
00203 int8 y_offs;
00204 EngineID engine_type;
00205
00206 TextEffectID fill_percent_te_id;
00207 UnitID unitnumber;
00208
00209 uint16 cur_speed;
00210 byte subspeed;
00211 byte acceleration;
00212 uint32 motion_counter;
00213 byte progress;
00214
00215 byte random_bits;
00216 byte waiting_triggers;
00217
00218 StationID last_station_visited;
00219
00220 CargoID cargo_type;
00221 byte cargo_subtype;
00222 uint16 cargo_cap;
00223 VehicleCargoList cargo;
00224 uint16 cargo_age_counter;
00225
00226 byte day_counter;
00227 byte tick_counter;
00228 byte running_ticks;
00229
00230 byte vehstatus;
00231 Order current_order;
00232 VehicleOrderID cur_real_order_index;
00233 VehicleOrderID cur_implicit_order_index;
00234
00235 union {
00236 OrderList *list;
00237 Order *old;
00238 } orders;
00239
00240 byte vehicle_flags;
00241
00242 uint16 load_unload_ticks;
00243 GroupID group_id;
00244 byte subtype;
00245
00246 NewGRFCache grf_cache;
00247 VehicleCache vcache;
00248
00249 Vehicle(VehicleType type = VEH_INVALID);
00250
00251 void PreDestructor();
00253 virtual ~Vehicle();
00254
00255 void BeginLoading();
00256 void LeaveStation();
00257
00258 GroundVehicleCache *GetGroundVehicleCache();
00259 const GroundVehicleCache *GetGroundVehicleCache() const;
00260
00261 uint16 &GetGroundVehicleFlags();
00262 const uint16 &GetGroundVehicleFlags() const;
00263
00264 void DeleteUnreachedImplicitOrders();
00265
00266 void HandleLoading(bool mode = false);
00267
00276 virtual void MarkDirty() {}
00277
00283 virtual void UpdateDeltaXY(Direction direction) {}
00284
00298 inline uint GetOldAdvanceSpeed(uint speed)
00299 {
00300 return (this->direction & 1) ? speed : speed * 3 / 4;
00301 }
00302
00315 static inline uint GetAdvanceSpeed(uint speed)
00316 {
00317 return speed * 3 / 4;
00318 }
00319
00327 inline uint GetAdvanceDistance()
00328 {
00329 return (this->direction & 1) ? 192 : 256;
00330 }
00331
00336 virtual ExpensesType GetExpenseType(bool income) const { return EXPENSES_OTHER; }
00337
00341 virtual void PlayLeaveStationSound() const {}
00342
00346 virtual bool IsPrimaryVehicle() const { return false; }
00347
00348 const Engine *GetEngine() const;
00349
00355 virtual SpriteID GetImage(Direction direction, EngineImageType image_type) const { return 0; }
00356
00357 const GRFFile *GetGRF() const;
00358 uint32 GetGRFID() const;
00359
00364 inline void InvalidateNewGRFCache()
00365 {
00366 this->grf_cache.cache_valid = 0;
00367 }
00368
00373 inline void InvalidateNewGRFCacheOfChain()
00374 {
00375 for (Vehicle *u = this; u != NULL; u = u->Next()) {
00376 u->InvalidateNewGRFCache();
00377 }
00378 }
00379
00384 inline bool IsGroundVehicle() const
00385 {
00386 return this->type == VEH_TRAIN || this->type == VEH_ROAD;
00387 }
00388
00393 virtual int GetDisplaySpeed() const { return 0; }
00394
00399 virtual int GetDisplayMaxSpeed() const { return 0; }
00400
00405 virtual Money GetRunningCost() const { return 0; }
00406
00411 virtual bool IsInDepot() const { return false; }
00412
00417 virtual bool IsStoppedInDepot() const { return this->IsInDepot() && (this->vehstatus & VS_STOPPED) != 0; }
00418
00423 virtual bool Tick() { return true; };
00424
00428 virtual void OnNewDay() {};
00429
00435 virtual uint Crash(bool flooded = false);
00436
00449 virtual Trackdir GetVehicleTrackdir() const { return INVALID_TRACKDIR; }
00450
00455 Money GetDisplayRunningCost() const { return (this->GetRunningCost() >> 8); }
00456
00461 Money GetDisplayProfitThisYear() const { return (this->profit_this_year >> 8); }
00462
00467 Money GetDisplayProfitLastYear() const { return (this->profit_last_year >> 8); }
00468
00469 void SetNext(Vehicle *next);
00470
00476 inline Vehicle *Next() const { return this->next; }
00477
00483 inline Vehicle *Previous() const { return this->previous; }
00484
00489 inline Vehicle *First() const { return this->first; }
00490
00495 inline Vehicle *Last()
00496 {
00497 Vehicle *v = this;
00498 while (v->Next() != NULL) v = v->Next();
00499 return v;
00500 }
00501
00506 inline const Vehicle *Last() const
00507 {
00508 const Vehicle *v = this;
00509 while (v->Next() != NULL) v = v->Next();
00510 return v;
00511 }
00512
00518 inline Vehicle *Move(int n)
00519 {
00520 Vehicle *v = this;
00521 if (n < 0) {
00522 for (int i = 0; i != n && v != NULL; i--) v = v->Previous();
00523 } else {
00524 for (int i = 0; i != n && v != NULL; i++) v = v->Next();
00525 }
00526 return v;
00527 }
00528
00533 inline Order *GetFirstOrder() const { return (this->orders.list == NULL) ? NULL : this->orders.list->GetFirstOrder(); }
00534
00535 void AddToShared(Vehicle *shared_chain);
00536 void RemoveFromShared();
00537
00542 inline Vehicle *NextShared() const { return this->next_shared; }
00543
00548 inline Vehicle *PreviousShared() const { return this->previous_shared; }
00549
00554 inline Vehicle *FirstShared() const { return (this->orders.list == NULL) ? this->First() : this->orders.list->GetFirstSharedVehicle(); }
00555
00560 inline bool IsOrderListShared() const { return this->orders.list != NULL && this->orders.list->IsShared(); }
00561
00566 inline VehicleOrderID GetNumOrders() const { return (this->orders.list == NULL) ? 0 : this->orders.list->GetNumOrders(); }
00567
00572 inline VehicleOrderID GetNumManualOrders() const { return (this->orders.list == NULL) ? 0 : this->orders.list->GetNumManualOrders(); }
00573
00580 inline void CopyVehicleConfigAndStatistics(const Vehicle *src)
00581 {
00582 this->unitnumber = src->unitnumber;
00583
00584 this->cur_real_order_index = src->cur_real_order_index;
00585 this->cur_implicit_order_index = src->cur_implicit_order_index;
00586 this->current_order = src->current_order;
00587 this->dest_tile = src->dest_tile;
00588
00589 this->profit_this_year = src->profit_this_year;
00590 this->profit_last_year = src->profit_last_year;
00591
00592 this->current_order_time = src->current_order_time;
00593 this->lateness_counter = src->lateness_counter;
00594 this->timetable_start = src->timetable_start;
00595
00596 if (HasBit(src->vehicle_flags, VF_TIMETABLE_STARTED)) SetBit(this->vehicle_flags, VF_TIMETABLE_STARTED);
00597 if (HasBit(src->vehicle_flags, VF_AUTOFILL_TIMETABLE)) SetBit(this->vehicle_flags, VF_AUTOFILL_TIMETABLE);
00598 if (HasBit(src->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME)) SetBit(this->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME);
00599
00600 this->service_interval = src->service_interval;
00601 }
00602
00603
00604 bool HandleBreakdown();
00605
00606 bool NeedsAutorenewing(const Company *c) const;
00607
00608 bool NeedsServicing() const;
00609 bool NeedsAutomaticServicing() const;
00610
00618 virtual TileIndex GetOrderStationLocation(StationID station) { return INVALID_TILE; }
00619
00628 virtual bool FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse) { return false; }
00629
00630 CommandCost SendToDepot(DoCommandFlag flags, DepotCommand command);
00631
00632 void UpdateVisualEffect(bool allow_power_change = true);
00633 void ShowVisualEffect() const;
00634
00635 private:
00640 void SkipToNextRealOrderIndex()
00641 {
00642 if (this->GetNumManualOrders() > 0) {
00643
00644 do {
00645 this->cur_real_order_index++;
00646 if (this->cur_real_order_index >= this->GetNumOrders()) this->cur_real_order_index = 0;
00647 } while (this->GetOrder(this->cur_real_order_index)->IsType(OT_IMPLICIT));
00648 } else {
00649 this->cur_real_order_index = 0;
00650 }
00651 }
00652
00653 public:
00659 void IncrementImplicitOrderIndex()
00660 {
00661 if (this->cur_implicit_order_index == this->cur_real_order_index) {
00662
00663 this->SkipToNextRealOrderIndex();
00664 }
00665
00666 assert(this->cur_real_order_index == 0 || this->cur_real_order_index < this->GetNumOrders());
00667
00668
00669 do {
00670 this->cur_implicit_order_index++;
00671 if (this->cur_implicit_order_index >= this->GetNumOrders()) this->cur_implicit_order_index = 0;
00672 } while (this->cur_implicit_order_index != this->cur_real_order_index && !this->GetOrder(this->cur_implicit_order_index)->IsType(OT_IMPLICIT));
00673
00674 InvalidateVehicleOrder(this, 0);
00675 }
00676
00683 void IncrementRealOrderIndex()
00684 {
00685 if (this->cur_implicit_order_index == this->cur_real_order_index) {
00686
00687 this->IncrementImplicitOrderIndex();
00688 } else {
00689
00690 this->SkipToNextRealOrderIndex();
00691 InvalidateVehicleOrder(this, 0);
00692 }
00693 }
00694
00698 void UpdateRealOrderIndex()
00699 {
00700
00701 if (this->cur_real_order_index >= this->GetNumOrders()) this->cur_real_order_index = 0;
00702
00703 if (this->GetNumManualOrders() > 0) {
00704
00705 while (this->GetOrder(this->cur_real_order_index)->IsType(OT_IMPLICIT)) {
00706 this->cur_real_order_index++;
00707 if (this->cur_real_order_index >= this->GetNumOrders()) this->cur_real_order_index = 0;
00708 }
00709 } else {
00710 this->cur_real_order_index = 0;
00711 }
00712 }
00713
00719 inline Order *GetOrder(int index) const
00720 {
00721 return (this->orders.list == NULL) ? NULL : this->orders.list->GetOrderAt(index);
00722 }
00723
00728 inline Order *GetLastOrder() const
00729 {
00730 return (this->orders.list == NULL) ? NULL : this->orders.list->GetLastOrder();
00731 }
00732
00733 bool IsEngineCountable() const;
00734 bool HasEngineType() const;
00735 bool HasDepotOrder() const;
00736 void HandlePathfindingResult(bool path_found);
00737
00742 inline bool IsFrontEngine() const
00743 {
00744 return this->IsGroundVehicle() && HasBit(this->subtype, GVSF_FRONT);
00745 }
00746
00751 inline bool IsArticulatedPart() const
00752 {
00753 return this->IsGroundVehicle() && HasBit(this->subtype, GVSF_ARTICULATED_PART);
00754 }
00755
00760 inline bool HasArticulatedPart() const
00761 {
00762 return this->Next() != NULL && this->Next()->IsArticulatedPart();
00763 }
00764
00770 inline Vehicle *GetNextArticulatedPart() const
00771 {
00772 assert(this->HasArticulatedPart());
00773 return this->Next();
00774 }
00775
00780 inline Vehicle *GetFirstEnginePart()
00781 {
00782 Vehicle *v = this;
00783 while (v->IsArticulatedPart()) v = v->Previous();
00784 return v;
00785 }
00786
00791 inline const Vehicle *GetFirstEnginePart() const
00792 {
00793 const Vehicle *v = this;
00794 while (v->IsArticulatedPart()) v = v->Previous();
00795 return v;
00796 }
00797
00802 inline Vehicle *GetLastEnginePart()
00803 {
00804 Vehicle *v = this;
00805 while (v->HasArticulatedPart()) v = v->GetNextArticulatedPart();
00806 return v;
00807 }
00808
00813 inline Vehicle *GetNextVehicle() const
00814 {
00815 const Vehicle *v = this;
00816 while (v->HasArticulatedPart()) v = v->GetNextArticulatedPart();
00817
00818
00819 return v->Next();
00820 }
00821
00826 inline Vehicle *GetPrevVehicle() const
00827 {
00828 Vehicle *v = this->Previous();
00829 while (v != NULL && v->IsArticulatedPart()) v = v->Previous();
00830
00831 return v;
00832 }
00833 };
00834
00840 #define FOR_ALL_VEHICLES_FROM(var, start) FOR_ALL_ITEMS_FROM(Vehicle, vehicle_index, var, start)
00841
00846 #define FOR_ALL_VEHICLES(var) FOR_ALL_VEHICLES_FROM(var, 0)
00847
00852 template <class T, VehicleType Type>
00853 struct SpecializedVehicle : public Vehicle {
00854 static const VehicleType EXPECTED_TYPE = Type;
00855
00856 typedef SpecializedVehicle<T, Type> SpecializedVehicleBase;
00857
00861 inline SpecializedVehicle<T, Type>() : Vehicle(Type) { }
00862
00867 inline T *First() const { return (T *)this->Vehicle::First(); }
00868
00873 inline T *Last() { return (T *)this->Vehicle::Last(); }
00874
00879 inline const T *Last() const { return (const T *)this->Vehicle::Last(); }
00880
00885 inline T *Next() const { return (T *)this->Vehicle::Next(); }
00886
00891 inline T *Previous() const { return (T *)this->Vehicle::Previous(); }
00892
00898 inline T *GetNextArticulatedPart() { return (T *)this->Vehicle::GetNextArticulatedPart(); }
00899
00905 inline T *GetNextArticulatedPart() const { return (T *)this->Vehicle::GetNextArticulatedPart(); }
00906
00911 inline T *GetFirstEnginePart() { return (T *)this->Vehicle::GetFirstEnginePart(); }
00912
00917 inline const T *GetFirstEnginePart() const { return (const T *)this->Vehicle::GetFirstEnginePart(); }
00918
00923 inline T *GetLastEnginePart() { return (T *)this->Vehicle::GetLastEnginePart(); }
00924
00929 inline T *GetNextVehicle() const { return (T *)this->Vehicle::GetNextVehicle(); }
00930
00935 inline T *GetPrevVehicle() const { return (T *)this->Vehicle::GetPrevVehicle(); }
00936
00942 static inline bool IsValidID(size_t index)
00943 {
00944 return Vehicle::IsValidID(index) && Vehicle::Get(index)->type == Type;
00945 }
00946
00951 static inline T *Get(size_t index)
00952 {
00953 return (T *)Vehicle::Get(index);
00954 }
00955
00960 static inline T *GetIfValid(size_t index)
00961 {
00962 return IsValidID(index) ? Get(index) : NULL;
00963 }
00964
00970 static inline T *From(Vehicle *v)
00971 {
00972 assert(v->type == Type);
00973 return (T *)v;
00974 }
00975
00981 static inline const T *From(const Vehicle *v)
00982 {
00983 assert(v->type == Type);
00984 return (const T *)v;
00985 }
00986
00992 inline void UpdateViewport(bool moved, bool turned)
00993 {
00994 extern void VehicleMove(Vehicle *v, bool update_viewport);
00995
00996
00997
00998 if (turned) ((T *)this)->T::UpdateDeltaXY(this->direction);
00999 SpriteID old_image = this->cur_image;
01000 this->cur_image = ((T *)this)->T::GetImage(this->direction, EIT_ON_MAP);
01001 if (moved || this->cur_image != old_image) VehicleMove(this, true);
01002 }
01003 };
01004
01010 #define FOR_ALL_VEHICLES_OF_TYPE(name, var) FOR_ALL_ITEMS_FROM(name, vehicle_index, var, 0) if (var->type == name::EXPECTED_TYPE)
01011
01015 struct DisasterVehicle FINAL : public SpecializedVehicle<DisasterVehicle, VEH_DISASTER> {
01016 SpriteID image_override;
01017 VehicleID big_ufo_destroyer_target;
01018
01020 DisasterVehicle() : SpecializedVehicleBase() {}
01022 virtual ~DisasterVehicle() {}
01023
01024 void UpdateDeltaXY(Direction direction);
01025 bool Tick();
01026 };
01027
01032 #define FOR_ALL_DISASTERVEHICLES(var) FOR_ALL_VEHICLES_OF_TYPE(DisasterVehicle, var)
01033
01035 struct FreeUnitIDGenerator {
01036 bool *cache;
01037 UnitID maxid;
01038 UnitID curid;
01039
01040 FreeUnitIDGenerator(VehicleType type, CompanyID owner);
01041 UnitID NextID();
01042
01044 ~FreeUnitIDGenerator() { free(this->cache); }
01045 };
01046
01048 static const int32 INVALID_COORD = 0x7fffffff;
01049
01050 #endif