00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #ifndef VEHICLE_BASE_H
00013 #define VEHICLE_BASE_H
00014
00015 #include "core/smallmap_type.hpp"
00016 #include "track_type.h"
00017 #include "command_type.h"
00018 #include "order_base.h"
00019 #include "cargopacket.h"
00020 #include "texteff.hpp"
00021 #include "engine_type.h"
00022 #include "order_func.h"
00023 #include "transport_type.h"
00024 #include "group_type.h"
00025
00027 enum VehStatus {
00028 VS_HIDDEN = 0x01,
00029 VS_STOPPED = 0x02,
00030 VS_UNCLICKABLE = 0x04,
00031 VS_DEFPAL = 0x08,
00032 VS_TRAIN_SLOWING = 0x10,
00033 VS_SHADOW = 0x20,
00034 VS_AIRCRAFT_BROKEN = 0x40,
00035 VS_CRASHED = 0x80,
00036 };
00037
00039 enum VehicleFlags {
00040 VF_LOADING_FINISHED,
00041 VF_CARGO_UNLOADING,
00042 VF_BUILT_AS_PROTOTYPE,
00043 VF_TIMETABLE_STARTED,
00044 VF_AUTOFILL_TIMETABLE,
00045 VF_AUTOFILL_PRES_WAIT_TIME,
00046 VF_STOP_LOADING,
00047 VF_PATHFINDER_LOST,
00048 };
00049
00051 enum NewGRFCacheValidValues {
00052 NCVV_POSITION_CONSIST_LENGTH = 0,
00053 NCVV_POSITION_SAME_ID_LENGTH = 1,
00054 NCVV_CONSIST_CARGO_INFORMATION = 2,
00055 NCVV_COMPANY_INFORMATION = 3,
00056 NCVV_END,
00057 };
00058
00060 struct NewGRFCache {
00061
00062 uint32 position_consist_length;
00063 uint32 position_same_id_length;
00064 uint32 consist_cargo_information;
00065 uint32 company_information;
00066 uint8 cache_valid;
00067 };
00068
00070 enum VisualEffect {
00071 VE_OFFSET_START = 0,
00072 VE_OFFSET_COUNT = 4,
00073 VE_OFFSET_CENTRE = 8,
00074
00075 VE_TYPE_START = 4,
00076 VE_TYPE_COUNT = 2,
00077 VE_TYPE_DEFAULT = 0,
00078 VE_TYPE_STEAM = 1,
00079 VE_TYPE_DIESEL = 2,
00080 VE_TYPE_ELECTRIC = 3,
00081
00082 VE_DISABLE_EFFECT = 6,
00083 VE_DISABLE_WAGON_POWER = 7,
00084
00085 VE_DEFAULT = 0xFF,
00086 };
00087
00093 enum GroundVehicleSubtypeFlags {
00094 GVSF_FRONT = 0,
00095 GVSF_ARTICULATED_PART = 1,
00096 GVSF_WAGON = 2,
00097 GVSF_ENGINE = 3,
00098 GVSF_FREE_WAGON = 4,
00099 GVSF_MULTIHEADED = 5,
00100 };
00101
00103 struct VehicleCache {
00104 uint16 cached_max_speed;
00105 uint16 cached_cargo_age_period;
00106
00107 byte cached_vis_effect;
00108 };
00109
00111 typedef Pool<Vehicle, VehicleID, 512, 0xFF000> VehiclePool;
00112 extern VehiclePool _vehicle_pool;
00113
00114
00115 struct SaveLoad;
00116 struct GroundVehicleCache;
00117 extern const SaveLoad *GetVehicleDescription(VehicleType vt);
00118 struct LoadgameState;
00119 extern bool LoadOldVehicle(LoadgameState *ls, int num);
00120 extern void FixOldVehicles();
00121
00122 struct GRFFile;
00123
00125 struct Vehicle : VehiclePool::PoolItem<&_vehicle_pool>, BaseVehicle {
00126 private:
00127 Vehicle *next;
00128 Vehicle *previous;
00129 Vehicle *first;
00130
00131 Vehicle *next_shared;
00132 Vehicle *previous_shared;
00133
00134 public:
00135 friend const SaveLoad *GetVehicleDescription(VehicleType vt);
00136 friend void FixOldVehicles();
00137 friend void AfterLoadVehicles(bool part_of_load);
00138 friend bool LoadOldVehicle(LoadgameState *ls, int num);
00139
00140 char *name;
00141
00142 TileIndex tile;
00143
00149 TileIndex dest_tile;
00150
00151 Money profit_this_year;
00152 Money profit_last_year;
00153 Money value;
00154
00155 CargoPayment *cargo_payment;
00156
00157
00158 uint32 current_order_time;
00159 int32 lateness_counter;
00160 Date timetable_start;
00161
00162 Rect coord;
00163
00164 Vehicle *next_hash;
00165 Vehicle **prev_hash;
00166
00167 Vehicle *next_new_hash;
00168 Vehicle **prev_new_hash;
00169 Vehicle **old_new_hash;
00170
00171 SpriteID colourmap;
00172
00173
00174 Year build_year;
00175 Date age;
00176 Date max_age;
00177 Date date_of_last_service;
00178 Date service_interval;
00179 uint16 reliability;
00180 uint16 reliability_spd_dec;
00181 byte breakdown_ctr;
00182 byte breakdown_delay;
00183 byte breakdowns_since_last_service;
00184 byte breakdown_chance;
00185
00186 int32 x_pos;
00187 int32 y_pos;
00188 int32 z_pos;
00189 DirectionByte direction;
00190
00191 OwnerByte owner;
00192 byte spritenum;
00193
00194
00195 SpriteID cur_image;
00196 byte x_extent;
00197 byte y_extent;
00198 byte z_extent;
00199 int8 x_bb_offs;
00200 int8 y_bb_offs;
00201 int8 x_offs;
00202 int8 y_offs;
00203 EngineID engine_type;
00204
00205 TextEffectID fill_percent_te_id;
00206 UnitID unitnumber;
00207
00208 uint16 cur_speed;
00209 byte subspeed;
00210 byte acceleration;
00211 uint32 motion_counter;
00212 byte progress;
00213
00214 byte random_bits;
00215 byte waiting_triggers;
00216
00217 StationID last_station_visited;
00218 StationID last_loading_station;
00219
00220 CargoID cargo_type;
00221 byte cargo_subtype;
00222 uint16 cargo_cap;
00223 uint16 refit_cap;
00224 VehicleCargoList cargo;
00225 uint16 cargo_age_counter;
00226
00227 byte day_counter;
00228 byte tick_counter;
00229 byte running_ticks;
00230
00231 byte vehstatus;
00232 Order current_order;
00233 VehicleOrderID cur_real_order_index;
00234 VehicleOrderID cur_implicit_order_index;
00235
00236 union {
00237 OrderList *list;
00238 Order *old;
00239 } orders;
00240
00241 byte vehicle_flags;
00242
00243 uint16 load_unload_ticks;
00244 GroupID group_id;
00245 byte subtype;
00246
00247 NewGRFCache grf_cache;
00248 VehicleCache vcache;
00249
00250 Vehicle(VehicleType type = VEH_INVALID);
00251
00252 void PreDestructor();
00254 virtual ~Vehicle();
00255
00256 void BeginLoading();
00257 void CancelReservation(StationID next, Station *st);
00258 void LeaveStation();
00259
00260 GroundVehicleCache *GetGroundVehicleCache();
00261 const GroundVehicleCache *GetGroundVehicleCache() const;
00262
00263 uint16 &GetGroundVehicleFlags();
00264 const uint16 &GetGroundVehicleFlags() const;
00265
00266 void DeleteUnreachedImplicitOrders();
00267
00268 void HandleLoading(bool mode = false);
00269
00270 void GetConsistFreeCapacities(SmallMap<CargoID, uint> &capacities) const;
00271
00272 uint GetConsistTotalCapacity() const;
00273
00282 virtual void MarkDirty() {}
00283
00289 virtual void UpdateDeltaXY(Direction direction) {}
00290
00304 FORCEINLINE uint GetOldAdvanceSpeed(uint speed)
00305 {
00306 return (this->direction & 1) ? speed : speed * 3 / 4;
00307 }
00308
00321 static FORCEINLINE uint GetAdvanceSpeed(uint speed)
00322 {
00323 return speed * 3 / 4;
00324 }
00325
00333 FORCEINLINE uint GetAdvanceDistance()
00334 {
00335 return (this->direction & 1) ? 192 : 256;
00336 }
00337
00342 virtual ExpensesType GetExpenseType(bool income) const { return EXPENSES_OTHER; }
00343
00347 virtual void PlayLeaveStationSound() const {}
00348
00352 virtual bool IsPrimaryVehicle() const { return false; }
00353
00354 const Engine *GetEngine() const;
00355
00361 virtual SpriteID GetImage(Direction direction, EngineImageType image_type) const { return 0; }
00362
00363 const GRFFile *GetGRF() const;
00364 uint32 GetGRFID() const;
00365
00370 FORCEINLINE void InvalidateNewGRFCache()
00371 {
00372 this->grf_cache.cache_valid = 0;
00373 }
00374
00379 FORCEINLINE void InvalidateNewGRFCacheOfChain()
00380 {
00381 for (Vehicle *u = this; u != NULL; u = u->Next()) {
00382 u->InvalidateNewGRFCache();
00383 }
00384 }
00385
00390 FORCEINLINE bool IsGroundVehicle() const
00391 {
00392 return this->type == VEH_TRAIN || this->type == VEH_ROAD;
00393 }
00394
00399 virtual int GetDisplaySpeed() const { return 0; }
00400
00405 virtual int GetDisplayMaxSpeed() const { return 0; }
00406
00411 virtual Money GetRunningCost() const { return 0; }
00412
00417 virtual bool IsInDepot() const { return false; }
00418
00423 virtual bool IsStoppedInDepot() const { return this->IsInDepot() && (this->vehstatus & VS_STOPPED) != 0; }
00424
00429 virtual bool Tick() { return true; };
00430
00434 virtual void OnNewDay() {};
00435
00441 virtual uint Crash(bool flooded = false);
00442
00455 virtual Trackdir GetVehicleTrackdir() const { return INVALID_TRACKDIR; }
00456
00461 Money GetDisplayRunningCost() const { return (this->GetRunningCost() >> 8); }
00462
00467 Money GetDisplayProfitThisYear() const { return (this->profit_this_year >> 8); }
00468
00473 Money GetDisplayProfitLastYear() const { return (this->profit_last_year >> 8); }
00474
00475 void SetNext(Vehicle *next);
00476
00482 inline Vehicle *Next() const { return this->next; }
00483
00489 inline Vehicle *Previous() const { return this->previous; }
00490
00495 inline Vehicle *First() const { return this->first; }
00496
00501 inline Vehicle *Last()
00502 {
00503 Vehicle *v = this;
00504 while (v->Next() != NULL) v = v->Next();
00505 return v;
00506 }
00507
00512 inline const Vehicle *Last() const
00513 {
00514 const Vehicle *v = this;
00515 while (v->Next() != NULL) v = v->Next();
00516 return v;
00517 }
00518
00524 inline Vehicle *Move(int n)
00525 {
00526 Vehicle *v = this;
00527 if (n < 0) {
00528 for (int i = 0; i != n && v != NULL; i--) v = v->Previous();
00529 } else {
00530 for (int i = 0; i != n && v != NULL; i++) v = v->Next();
00531 }
00532 return v;
00533 }
00534
00539 inline Order *GetFirstOrder() const { return (this->orders.list == NULL) ? NULL : this->orders.list->GetFirstOrder(); }
00540
00541 void AddToShared(Vehicle *shared_chain);
00542 void RemoveFromShared();
00543
00548 inline Vehicle *NextShared() const { return this->next_shared; }
00549
00554 inline Vehicle *PreviousShared() const { return this->previous_shared; }
00555
00560 inline Vehicle *FirstShared() const { return (this->orders.list == NULL) ? this->First() : this->orders.list->GetFirstSharedVehicle(); }
00561
00566 inline bool IsOrderListShared() const { return this->orders.list != NULL && this->orders.list->IsShared(); }
00567
00572 inline VehicleOrderID GetNumOrders() const { return (this->orders.list == NULL) ? 0 : this->orders.list->GetNumOrders(); }
00573
00578 inline VehicleOrderID GetNumManualOrders() const { return (this->orders.list == NULL) ? 0 : this->orders.list->GetNumManualOrders(); }
00579
00584 inline StationID GetNextStoppingStation() const
00585 {
00586 return (this->orders.list == NULL) ? INVALID_STATION : this->orders.list->GetNextStoppingStation(this);
00587 }
00588
00589 void RefreshNextHopsStats();
00590
00597 inline void CopyVehicleConfigAndStatistics(const Vehicle *src)
00598 {
00599 this->unitnumber = src->unitnumber;
00600
00601 this->cur_real_order_index = src->cur_real_order_index;
00602 this->cur_implicit_order_index = src->cur_implicit_order_index;
00603 this->current_order = src->current_order;
00604 this->dest_tile = src->dest_tile;
00605
00606 this->profit_this_year = src->profit_this_year;
00607 this->profit_last_year = src->profit_last_year;
00608
00609 this->current_order_time = src->current_order_time;
00610 this->lateness_counter = src->lateness_counter;
00611 this->timetable_start = src->timetable_start;
00612
00613 if (HasBit(src->vehicle_flags, VF_TIMETABLE_STARTED)) SetBit(this->vehicle_flags, VF_TIMETABLE_STARTED);
00614 if (HasBit(src->vehicle_flags, VF_AUTOFILL_TIMETABLE)) SetBit(this->vehicle_flags, VF_AUTOFILL_TIMETABLE);
00615 if (HasBit(src->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME)) SetBit(this->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME);
00616
00617 this->service_interval = src->service_interval;
00618 }
00619
00620
00621 bool HandleBreakdown();
00622
00623 bool NeedsAutorenewing(const Company *c) const;
00624
00625 bool NeedsServicing() const;
00626 bool NeedsAutomaticServicing() const;
00627
00635 virtual TileIndex GetOrderStationLocation(StationID station) { return INVALID_TILE; }
00636
00645 virtual bool FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse) { return false; }
00646
00647 CommandCost SendToDepot(DoCommandFlag flags, DepotCommand command);
00648
00649 void UpdateVisualEffect(bool allow_power_change = true);
00650 void ShowVisualEffect() const;
00651
00652 private:
00657 void SkipToNextRealOrderIndex()
00658 {
00659 if (this->GetNumManualOrders() > 0) {
00660
00661 do {
00662 this->cur_real_order_index++;
00663 if (this->cur_real_order_index >= this->GetNumOrders()) this->cur_real_order_index = 0;
00664 } while (this->GetOrder(this->cur_real_order_index)->IsType(OT_IMPLICIT));
00665 } else {
00666 this->cur_real_order_index = 0;
00667 }
00668 }
00669
00670 public:
00676 void IncrementImplicitOrderIndex()
00677 {
00678 if (this->cur_implicit_order_index == this->cur_real_order_index) {
00679
00680 this->SkipToNextRealOrderIndex();
00681 }
00682
00683 assert(this->cur_real_order_index == 0 || this->cur_real_order_index < this->GetNumOrders());
00684
00685
00686 do {
00687 this->cur_implicit_order_index++;
00688 if (this->cur_implicit_order_index >= this->GetNumOrders()) this->cur_implicit_order_index = 0;
00689 } while (this->cur_implicit_order_index != this->cur_real_order_index && !this->GetOrder(this->cur_implicit_order_index)->IsType(OT_IMPLICIT));
00690
00691 InvalidateVehicleOrder(this, 0);
00692 }
00693
00700 void IncrementRealOrderIndex()
00701 {
00702 if (this->cur_implicit_order_index == this->cur_real_order_index) {
00703
00704 this->IncrementImplicitOrderIndex();
00705 } else {
00706
00707 this->SkipToNextRealOrderIndex();
00708 InvalidateVehicleOrder(this, 0);
00709 }
00710 }
00711
00715 void UpdateRealOrderIndex()
00716 {
00717
00718 if (this->cur_real_order_index >= this->GetNumOrders()) this->cur_real_order_index = 0;
00719
00720 if (this->GetNumManualOrders() > 0) {
00721
00722 while (this->GetOrder(this->cur_real_order_index)->IsType(OT_IMPLICIT)) {
00723 this->cur_real_order_index++;
00724 if (this->cur_real_order_index >= this->GetNumOrders()) this->cur_real_order_index = 0;
00725 }
00726 } else {
00727 this->cur_real_order_index = 0;
00728 }
00729 }
00730
00736 inline Order *GetOrder(int index) const
00737 {
00738 return (this->orders.list == NULL) ? NULL : this->orders.list->GetOrderAt(index);
00739 }
00740
00745 inline Order *GetLastOrder() const
00746 {
00747 return (this->orders.list == NULL) ? NULL : this->orders.list->GetLastOrder();
00748 }
00749
00750 bool IsEngineCountable() const;
00751 bool HasEngineType() const;
00752 bool HasDepotOrder() const;
00753 void HandlePathfindingResult(bool path_found);
00754
00759 FORCEINLINE bool IsFrontEngine() const
00760 {
00761 return this->IsGroundVehicle() && HasBit(this->subtype, GVSF_FRONT);
00762 }
00763
00768 FORCEINLINE bool IsArticulatedPart() const
00769 {
00770 return this->IsGroundVehicle() && HasBit(this->subtype, GVSF_ARTICULATED_PART);
00771 }
00772
00777 FORCEINLINE bool HasArticulatedPart() const
00778 {
00779 return this->Next() != NULL && this->Next()->IsArticulatedPart();
00780 }
00781
00787 FORCEINLINE Vehicle *GetNextArticulatedPart() const
00788 {
00789 assert(this->HasArticulatedPart());
00790 return this->Next();
00791 }
00792
00797 FORCEINLINE Vehicle *GetFirstEnginePart()
00798 {
00799 Vehicle *v = this;
00800 while (v->IsArticulatedPart()) v = v->Previous();
00801 return v;
00802 }
00803
00808 FORCEINLINE const Vehicle *GetFirstEnginePart() const
00809 {
00810 const Vehicle *v = this;
00811 while (v->IsArticulatedPart()) v = v->Previous();
00812 return v;
00813 }
00814
00819 FORCEINLINE Vehicle *GetLastEnginePart()
00820 {
00821 Vehicle *v = this;
00822 while (v->HasArticulatedPart()) v = v->GetNextArticulatedPart();
00823 return v;
00824 }
00825
00830 FORCEINLINE Vehicle *GetNextVehicle() const
00831 {
00832 const Vehicle *v = this;
00833 while (v->HasArticulatedPart()) v = v->GetNextArticulatedPart();
00834
00835
00836 return v->Next();
00837 }
00838
00843 FORCEINLINE Vehicle *GetPrevVehicle() const
00844 {
00845 Vehicle *v = this->Previous();
00846 while (v != NULL && v->IsArticulatedPart()) v = v->Previous();
00847
00848 return v;
00849 }
00850 };
00851
00857 #define FOR_ALL_VEHICLES_FROM(var, start) FOR_ALL_ITEMS_FROM(Vehicle, vehicle_index, var, start)
00858
00863 #define FOR_ALL_VEHICLES(var) FOR_ALL_VEHICLES_FROM(var, 0)
00864
00869 template <class T, VehicleType Type>
00870 struct SpecializedVehicle : public Vehicle {
00871 static const VehicleType EXPECTED_TYPE = Type;
00872
00873 typedef SpecializedVehicle<T, Type> SpecializedVehicleBase;
00874
00878 FORCEINLINE SpecializedVehicle<T, Type>() : Vehicle(Type) { }
00879
00884 FORCEINLINE T *First() const { return (T *)this->Vehicle::First(); }
00885
00890 FORCEINLINE T *Last() { return (T *)this->Vehicle::Last(); }
00891
00896 FORCEINLINE const T *Last() const { return (const T *)this->Vehicle::Last(); }
00897
00902 FORCEINLINE T *Next() const { return (T *)this->Vehicle::Next(); }
00903
00908 FORCEINLINE T *Previous() const { return (T *)this->Vehicle::Previous(); }
00909
00915 FORCEINLINE T *GetNextArticulatedPart() { return (T *)this->Vehicle::GetNextArticulatedPart(); }
00916
00922 FORCEINLINE T *GetNextArticulatedPart() const { return (T *)this->Vehicle::GetNextArticulatedPart(); }
00923
00928 FORCEINLINE T *GetFirstEnginePart() { return (T *)this->Vehicle::GetFirstEnginePart(); }
00929
00934 FORCEINLINE const T *GetFirstEnginePart() const { return (const T *)this->Vehicle::GetFirstEnginePart(); }
00935
00940 FORCEINLINE T *GetLastEnginePart() { return (T *)this->Vehicle::GetLastEnginePart(); }
00941
00946 FORCEINLINE T *GetNextVehicle() const { return (T *)this->Vehicle::GetNextVehicle(); }
00947
00952 FORCEINLINE T *GetPrevVehicle() const { return (T *)this->Vehicle::GetPrevVehicle(); }
00953
00959 static FORCEINLINE bool IsValidID(size_t index)
00960 {
00961 return Vehicle::IsValidID(index) && Vehicle::Get(index)->type == Type;
00962 }
00963
00968 static FORCEINLINE T *Get(size_t index)
00969 {
00970 return (T *)Vehicle::Get(index);
00971 }
00972
00977 static FORCEINLINE T *GetIfValid(size_t index)
00978 {
00979 return IsValidID(index) ? Get(index) : NULL;
00980 }
00981
00987 static FORCEINLINE T *From(Vehicle *v)
00988 {
00989 assert(v->type == Type);
00990 return (T *)v;
00991 }
00992
00998 static FORCEINLINE const T *From(const Vehicle *v)
00999 {
01000 assert(v->type == Type);
01001 return (const T *)v;
01002 }
01003
01009 FORCEINLINE void UpdateViewport(bool moved, bool turned)
01010 {
01011 extern void VehicleMove(Vehicle *v, bool update_viewport);
01012
01013
01014
01015 if (turned) ((T *)this)->T::UpdateDeltaXY(this->direction);
01016 SpriteID old_image = this->cur_image;
01017 this->cur_image = ((T *)this)->T::GetImage(this->direction, EIT_ON_MAP);
01018 if (moved || this->cur_image != old_image) VehicleMove(this, true);
01019 }
01020 };
01021
01027 #define FOR_ALL_VEHICLES_OF_TYPE(name, var) FOR_ALL_ITEMS_FROM(name, vehicle_index, var, 0) if (var->type == name::EXPECTED_TYPE)
01028
01032 struct DisasterVehicle : public SpecializedVehicle<DisasterVehicle, VEH_DISASTER> {
01033 SpriteID image_override;
01034 VehicleID big_ufo_destroyer_target;
01035
01037 DisasterVehicle() : SpecializedVehicleBase() {}
01039 virtual ~DisasterVehicle() {}
01040
01041 void UpdateDeltaXY(Direction direction);
01042 bool Tick();
01043 };
01044
01049 #define FOR_ALL_DISASTERVEHICLES(var) FOR_ALL_VEHICLES_OF_TYPE(DisasterVehicle, var)
01050
01052 struct FreeUnitIDGenerator {
01053 bool *cache;
01054 UnitID maxid;
01055 UnitID curid;
01056
01057 FreeUnitIDGenerator(VehicleType type, CompanyID owner);
01058 UnitID NextID();
01059
01061 ~FreeUnitIDGenerator() { free(this->cache); }
01062 };
01063
01065 static const int32 INVALID_COORD = 0x7fffffff;
01066
01067 #endif