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 *hash_viewport_next;
00165 Vehicle **hash_viewport_prev;
00166
00167 Vehicle *hash_tile_next;
00168 Vehicle **hash_tile_prev;
00169 Vehicle **hash_tile_current;
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
00197 byte spritenum;
00198 SpriteID cur_image;
00199 byte x_extent;
00200 byte y_extent;
00201 byte z_extent;
00202 int8 x_bb_offs;
00203 int8 y_bb_offs;
00204 int8 x_offs;
00205 int8 y_offs;
00206 EngineID engine_type;
00207
00208 TextEffectID fill_percent_te_id;
00209 UnitID unitnumber;
00210
00211 uint16 cur_speed;
00212 byte subspeed;
00213 byte acceleration;
00214 uint32 motion_counter;
00215 byte progress;
00216
00217 byte random_bits;
00218 byte waiting_triggers;
00219
00220 StationID last_station_visited;
00221 StationID last_loading_station;
00222
00223 CargoID cargo_type;
00224 byte cargo_subtype;
00225 uint16 cargo_cap;
00226 uint16 refit_cap;
00227 VehicleCargoList cargo;
00228 uint16 cargo_age_counter;
00229
00230 byte day_counter;
00231 byte tick_counter;
00232 byte running_ticks;
00233
00234 byte vehstatus;
00235 Order current_order;
00236 VehicleOrderID cur_real_order_index;
00237 VehicleOrderID cur_implicit_order_index;
00238
00239 union {
00240 OrderList *list;
00241 Order *old;
00242 } orders;
00243
00244 byte vehicle_flags;
00245
00246 uint16 load_unload_ticks;
00247 GroupID group_id;
00248 byte subtype;
00249
00250 NewGRFCache grf_cache;
00251 VehicleCache vcache;
00252
00253 Vehicle(VehicleType type = VEH_INVALID);
00254
00255 void PreDestructor();
00257 virtual ~Vehicle();
00258
00259 void BeginLoading();
00260 void CancelReservation(StationID next, Station *st);
00261 void LeaveStation();
00262
00263 GroundVehicleCache *GetGroundVehicleCache();
00264 const GroundVehicleCache *GetGroundVehicleCache() const;
00265
00266 uint16 &GetGroundVehicleFlags();
00267 const uint16 &GetGroundVehicleFlags() const;
00268
00269 void DeleteUnreachedImplicitOrders();
00270
00271 void HandleLoading(bool mode = false);
00272
00273 void GetConsistFreeCapacities(SmallMap<CargoID, uint> &capacities) const;
00274
00275 uint GetConsistTotalCapacity() const;
00276
00285 virtual void MarkDirty() {}
00286
00292 virtual void UpdateDeltaXY(Direction direction) {}
00293
00307 inline uint GetOldAdvanceSpeed(uint speed)
00308 {
00309 return (this->direction & 1) ? speed : speed * 3 / 4;
00310 }
00311
00324 static inline uint GetAdvanceSpeed(uint speed)
00325 {
00326 return speed * 3 / 4;
00327 }
00328
00336 inline uint GetAdvanceDistance()
00337 {
00338 return (this->direction & 1) ? 192 : 256;
00339 }
00340
00345 virtual ExpensesType GetExpenseType(bool income) const { return EXPENSES_OTHER; }
00346
00350 virtual void PlayLeaveStationSound() const {}
00351
00355 virtual bool IsPrimaryVehicle() const { return false; }
00356
00357 const Engine *GetEngine() const;
00358
00364 virtual SpriteID GetImage(Direction direction, EngineImageType image_type) const { return 0; }
00365
00366 const GRFFile *GetGRF() const;
00367 uint32 GetGRFID() const;
00368
00373 inline void InvalidateNewGRFCache()
00374 {
00375 this->grf_cache.cache_valid = 0;
00376 }
00377
00382 inline void InvalidateNewGRFCacheOfChain()
00383 {
00384 for (Vehicle *u = this; u != NULL; u = u->Next()) {
00385 u->InvalidateNewGRFCache();
00386 }
00387 }
00388
00393 inline bool IsGroundVehicle() const
00394 {
00395 return this->type == VEH_TRAIN || this->type == VEH_ROAD;
00396 }
00397
00402 virtual int GetDisplaySpeed() const { return 0; }
00403
00408 virtual int GetDisplayMaxSpeed() const { return 0; }
00409
00414 virtual Money GetRunningCost() const { return 0; }
00415
00420 virtual bool IsInDepot() const { return false; }
00421
00426 virtual bool IsStoppedInDepot() const { return this->IsInDepot() && (this->vehstatus & VS_STOPPED) != 0; }
00427
00432 virtual bool Tick() { return true; };
00433
00437 virtual void OnNewDay() {};
00438
00444 virtual uint Crash(bool flooded = false);
00445
00458 virtual Trackdir GetVehicleTrackdir() const { return INVALID_TRACKDIR; }
00459
00464 Money GetDisplayRunningCost() const { return (this->GetRunningCost() >> 8); }
00465
00470 Money GetDisplayProfitThisYear() const { return (this->profit_this_year >> 8); }
00471
00476 Money GetDisplayProfitLastYear() const { return (this->profit_last_year >> 8); }
00477
00478 void SetNext(Vehicle *next);
00479
00485 inline Vehicle *Next() const { return this->next; }
00486
00492 inline Vehicle *Previous() const { return this->previous; }
00493
00498 inline Vehicle *First() const { return this->first; }
00499
00504 inline Vehicle *Last()
00505 {
00506 Vehicle *v = this;
00507 while (v->Next() != NULL) v = v->Next();
00508 return v;
00509 }
00510
00515 inline const Vehicle *Last() const
00516 {
00517 const Vehicle *v = this;
00518 while (v->Next() != NULL) v = v->Next();
00519 return v;
00520 }
00521
00527 inline Vehicle *Move(int n)
00528 {
00529 Vehicle *v = this;
00530 if (n < 0) {
00531 for (int i = 0; i != n && v != NULL; i--) v = v->Previous();
00532 } else {
00533 for (int i = 0; i != n && v != NULL; i++) v = v->Next();
00534 }
00535 return v;
00536 }
00537
00542 inline Order *GetFirstOrder() const { return (this->orders.list == NULL) ? NULL : this->orders.list->GetFirstOrder(); }
00543
00544 void AddToShared(Vehicle *shared_chain);
00545 void RemoveFromShared();
00546
00551 inline Vehicle *NextShared() const { return this->next_shared; }
00552
00557 inline Vehicle *PreviousShared() const { return this->previous_shared; }
00558
00563 inline Vehicle *FirstShared() const { return (this->orders.list == NULL) ? this->First() : this->orders.list->GetFirstSharedVehicle(); }
00564
00569 inline bool IsOrderListShared() const { return this->orders.list != NULL && this->orders.list->IsShared(); }
00570
00575 inline VehicleOrderID GetNumOrders() const { return (this->orders.list == NULL) ? 0 : this->orders.list->GetNumOrders(); }
00576
00581 inline VehicleOrderID GetNumManualOrders() const { return (this->orders.list == NULL) ? 0 : this->orders.list->GetNumManualOrders(); }
00582
00587 inline StationID GetNextStoppingStation() const
00588 {
00589 return (this->orders.list == NULL) ? INVALID_STATION : this->orders.list->GetNextStoppingStation(this);
00590 }
00591
00592 void RefreshNextHopsStats();
00593
00600 inline void CopyVehicleConfigAndStatistics(const Vehicle *src)
00601 {
00602 this->unitnumber = src->unitnumber;
00603
00604 this->cur_real_order_index = src->cur_real_order_index;
00605 this->cur_implicit_order_index = src->cur_implicit_order_index;
00606 this->current_order = src->current_order;
00607 this->dest_tile = src->dest_tile;
00608
00609 this->profit_this_year = src->profit_this_year;
00610 this->profit_last_year = src->profit_last_year;
00611
00612 this->current_order_time = src->current_order_time;
00613 this->lateness_counter = src->lateness_counter;
00614 this->timetable_start = src->timetable_start;
00615
00616 if (HasBit(src->vehicle_flags, VF_TIMETABLE_STARTED)) SetBit(this->vehicle_flags, VF_TIMETABLE_STARTED);
00617 if (HasBit(src->vehicle_flags, VF_AUTOFILL_TIMETABLE)) SetBit(this->vehicle_flags, VF_AUTOFILL_TIMETABLE);
00618 if (HasBit(src->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME)) SetBit(this->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME);
00619
00620 this->service_interval = src->service_interval;
00621 }
00622
00623
00624 bool HandleBreakdown();
00625
00626 bool NeedsAutorenewing(const Company *c, bool use_renew_setting = true) const;
00627
00628 bool NeedsServicing() const;
00629 bool NeedsAutomaticServicing() const;
00630
00638 virtual TileIndex GetOrderStationLocation(StationID station) { return INVALID_TILE; }
00639
00648 virtual bool FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse) { return false; }
00649
00650 CommandCost SendToDepot(DoCommandFlag flags, DepotCommand command);
00651
00652 void UpdateVisualEffect(bool allow_power_change = true);
00653 void ShowVisualEffect() const;
00654
00655 private:
00660 void SkipToNextRealOrderIndex()
00661 {
00662 if (this->GetNumManualOrders() > 0) {
00663
00664 do {
00665 this->cur_real_order_index++;
00666 if (this->cur_real_order_index >= this->GetNumOrders()) this->cur_real_order_index = 0;
00667 } while (this->GetOrder(this->cur_real_order_index)->IsType(OT_IMPLICIT));
00668 } else {
00669 this->cur_real_order_index = 0;
00670 }
00671 }
00672
00673 public:
00679 void IncrementImplicitOrderIndex()
00680 {
00681 if (this->cur_implicit_order_index == this->cur_real_order_index) {
00682
00683 this->SkipToNextRealOrderIndex();
00684 }
00685
00686 assert(this->cur_real_order_index == 0 || this->cur_real_order_index < this->GetNumOrders());
00687
00688
00689 do {
00690 this->cur_implicit_order_index++;
00691 if (this->cur_implicit_order_index >= this->GetNumOrders()) this->cur_implicit_order_index = 0;
00692 } while (this->cur_implicit_order_index != this->cur_real_order_index && !this->GetOrder(this->cur_implicit_order_index)->IsType(OT_IMPLICIT));
00693
00694 InvalidateVehicleOrder(this, 0);
00695 }
00696
00703 void IncrementRealOrderIndex()
00704 {
00705 if (this->cur_implicit_order_index == this->cur_real_order_index) {
00706
00707 this->IncrementImplicitOrderIndex();
00708 } else {
00709
00710 this->SkipToNextRealOrderIndex();
00711 InvalidateVehicleOrder(this, 0);
00712 }
00713 }
00714
00718 void UpdateRealOrderIndex()
00719 {
00720
00721 if (this->cur_real_order_index >= this->GetNumOrders()) this->cur_real_order_index = 0;
00722
00723 if (this->GetNumManualOrders() > 0) {
00724
00725 while (this->GetOrder(this->cur_real_order_index)->IsType(OT_IMPLICIT)) {
00726 this->cur_real_order_index++;
00727 if (this->cur_real_order_index >= this->GetNumOrders()) this->cur_real_order_index = 0;
00728 }
00729 } else {
00730 this->cur_real_order_index = 0;
00731 }
00732 }
00733
00739 inline Order *GetOrder(int index) const
00740 {
00741 return (this->orders.list == NULL) ? NULL : this->orders.list->GetOrderAt(index);
00742 }
00743
00748 inline Order *GetLastOrder() const
00749 {
00750 return (this->orders.list == NULL) ? NULL : this->orders.list->GetLastOrder();
00751 }
00752
00753 bool IsEngineCountable() const;
00754 bool HasEngineType() const;
00755 bool HasDepotOrder() const;
00756 void HandlePathfindingResult(bool path_found);
00757
00762 inline bool IsFrontEngine() const
00763 {
00764 return this->IsGroundVehicle() && HasBit(this->subtype, GVSF_FRONT);
00765 }
00766
00771 inline bool IsArticulatedPart() const
00772 {
00773 return this->IsGroundVehicle() && HasBit(this->subtype, GVSF_ARTICULATED_PART);
00774 }
00775
00780 inline bool HasArticulatedPart() const
00781 {
00782 return this->Next() != NULL && this->Next()->IsArticulatedPart();
00783 }
00784
00790 inline Vehicle *GetNextArticulatedPart() const
00791 {
00792 assert(this->HasArticulatedPart());
00793 return this->Next();
00794 }
00795
00800 inline Vehicle *GetFirstEnginePart()
00801 {
00802 Vehicle *v = this;
00803 while (v->IsArticulatedPart()) v = v->Previous();
00804 return v;
00805 }
00806
00811 inline const Vehicle *GetFirstEnginePart() const
00812 {
00813 const Vehicle *v = this;
00814 while (v->IsArticulatedPart()) v = v->Previous();
00815 return v;
00816 }
00817
00822 inline Vehicle *GetLastEnginePart()
00823 {
00824 Vehicle *v = this;
00825 while (v->HasArticulatedPart()) v = v->GetNextArticulatedPart();
00826 return v;
00827 }
00828
00833 inline Vehicle *GetNextVehicle() const
00834 {
00835 const Vehicle *v = this;
00836 while (v->HasArticulatedPart()) v = v->GetNextArticulatedPart();
00837
00838
00839 return v->Next();
00840 }
00841
00846 inline Vehicle *GetPrevVehicle() const
00847 {
00848 Vehicle *v = this->Previous();
00849 while (v != NULL && v->IsArticulatedPart()) v = v->Previous();
00850
00851 return v;
00852 }
00853 };
00854
00860 #define FOR_ALL_VEHICLES_FROM(var, start) FOR_ALL_ITEMS_FROM(Vehicle, vehicle_index, var, start)
00861
00866 #define FOR_ALL_VEHICLES(var) FOR_ALL_VEHICLES_FROM(var, 0)
00867
00872 template <class T, VehicleType Type>
00873 struct SpecializedVehicle : public Vehicle {
00874 static const VehicleType EXPECTED_TYPE = Type;
00875
00876 typedef SpecializedVehicle<T, Type> SpecializedVehicleBase;
00877
00881 inline SpecializedVehicle<T, Type>() : Vehicle(Type) { }
00882
00887 inline T *First() const { return (T *)this->Vehicle::First(); }
00888
00893 inline T *Last() { return (T *)this->Vehicle::Last(); }
00894
00899 inline const T *Last() const { return (const T *)this->Vehicle::Last(); }
00900
00905 inline T *Next() const { return (T *)this->Vehicle::Next(); }
00906
00911 inline T *Previous() const { return (T *)this->Vehicle::Previous(); }
00912
00918 inline T *GetNextArticulatedPart() { return (T *)this->Vehicle::GetNextArticulatedPart(); }
00919
00925 inline T *GetNextArticulatedPart() const { return (T *)this->Vehicle::GetNextArticulatedPart(); }
00926
00931 inline T *GetFirstEnginePart() { return (T *)this->Vehicle::GetFirstEnginePart(); }
00932
00937 inline const T *GetFirstEnginePart() const { return (const T *)this->Vehicle::GetFirstEnginePart(); }
00938
00943 inline T *GetLastEnginePart() { return (T *)this->Vehicle::GetLastEnginePart(); }
00944
00949 inline T *GetNextVehicle() const { return (T *)this->Vehicle::GetNextVehicle(); }
00950
00955 inline T *GetPrevVehicle() const { return (T *)this->Vehicle::GetPrevVehicle(); }
00956
00962 static inline bool IsValidID(size_t index)
00963 {
00964 return Vehicle::IsValidID(index) && Vehicle::Get(index)->type == Type;
00965 }
00966
00971 static inline T *Get(size_t index)
00972 {
00973 return (T *)Vehicle::Get(index);
00974 }
00975
00980 static inline T *GetIfValid(size_t index)
00981 {
00982 return IsValidID(index) ? Get(index) : NULL;
00983 }
00984
00990 static inline T *From(Vehicle *v)
00991 {
00992 assert(v->type == Type);
00993 return (T *)v;
00994 }
00995
01001 static inline const T *From(const Vehicle *v)
01002 {
01003 assert(v->type == Type);
01004 return (const T *)v;
01005 }
01006
01012 inline void UpdateViewport(bool force_update, bool update_delta)
01013 {
01014 extern void VehicleUpdateViewport(Vehicle *v, bool dirty);
01015
01016
01017
01018 if (update_delta) ((T *)this)->T::UpdateDeltaXY(this->direction);
01019 SpriteID old_image = this->cur_image;
01020 this->cur_image = ((T *)this)->T::GetImage(this->direction, EIT_ON_MAP);
01021 if (force_update || this->cur_image != old_image) VehicleUpdateViewport(this, true);
01022 }
01023 };
01024
01030 #define FOR_ALL_VEHICLES_OF_TYPE(name, var) FOR_ALL_ITEMS_FROM(name, vehicle_index, var, 0) if (var->type == name::EXPECTED_TYPE)
01031
01035 struct DisasterVehicle FINAL : public SpecializedVehicle<DisasterVehicle, VEH_DISASTER> {
01036 SpriteID image_override;
01037 VehicleID big_ufo_destroyer_target;
01038
01040 DisasterVehicle() : SpecializedVehicleBase() {}
01042 virtual ~DisasterVehicle() {}
01043
01044 void UpdateDeltaXY(Direction direction);
01045 bool Tick();
01046 };
01047
01052 #define FOR_ALL_DISASTERVEHICLES(var) FOR_ALL_VEHICLES_OF_TYPE(DisasterVehicle, var)
01053
01055 struct FreeUnitIDGenerator {
01056 bool *cache;
01057 UnitID maxid;
01058 UnitID curid;
01059
01060 FreeUnitIDGenerator(VehicleType type, CompanyID owner);
01061 UnitID NextID();
01062
01064 ~FreeUnitIDGenerator() { free(this->cache); }
01065 };
01066
01068 static const int32 INVALID_COORD = 0x7fffffff;
01069
01070 #endif