OpenTTD
vehicle_base.h
Go to the documentation of this file.
1 /* $Id: vehicle_base.h 27822 2017-03-24 07:33:31Z peter1138 $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8  */
9 
12 #ifndef VEHICLE_BASE_H
13 #define VEHICLE_BASE_H
14 
15 #include "core/smallmap_type.hpp"
16 #include "track_type.h"
17 #include "command_type.h"
18 #include "order_base.h"
19 #include "cargopacket.h"
20 #include "texteff.hpp"
21 #include "engine_type.h"
22 #include "order_func.h"
23 #include "transport_type.h"
24 #include "group_type.h"
25 #include "base_consist.h"
26 #include "network/network.h"
27 #include <list>
28 #include <map>
29 
31 enum VehStatus {
32  VS_HIDDEN = 0x01,
33  VS_STOPPED = 0x02,
34  VS_UNCLICKABLE = 0x04,
35  VS_DEFPAL = 0x08,
37  VS_SHADOW = 0x20,
39  VS_CRASHED = 0x80,
40 };
41 
54 };
55 
64 };
65 
67 struct NewGRFCache {
68  /* Values calculated when they are requested for the first time after invalidating the NewGRF cache. */
74  uint8 cache_valid;
75 };
76 
82 
89 
93 
94  VE_DEFAULT = 0xFF,
95 };
96 
99  VESM_NONE = 0,
103 
104  VESM_END
105 };
106 
119 };
120 
122 struct VehicleCache {
125 
127 };
128 
131  PalSpriteID seq[4];
132  uint count;
133 
134  bool operator==(const VehicleSpriteSeq &other) const
135  {
136  return this->count == other.count && MemCmpT<PalSpriteID>(this->seq, other.seq, this->count) == 0;
137  }
138 
139  bool operator!=(const VehicleSpriteSeq &other) const
140  {
141  return !this->operator==(other);
142  }
143 
147  bool IsValid() const
148  {
149  return this->count != 0;
150  }
151 
155  void Clear()
156  {
157  this->count = 0;
158  }
159 
163  void Set(SpriteID sprite)
164  {
165  this->count = 1;
166  this->seq[0].sprite = sprite;
167  this->seq[0].pal = 0;
168  }
169 
174  {
175  this->count = src.count;
176  for (uint i = 0; i < src.count; ++i) {
177  this->seq[i].sprite = src.seq[i].sprite;
178  this->seq[i].pal = 0;
179  }
180  }
181 
182  void GetBounds(Rect *bounds) const;
183  void Draw(int x, int y, PaletteID default_pal, bool force_pal) const;
184 };
185 
189 
190 /* Some declarations of functions, so we can make them friendly */
191 struct SaveLoad;
192 struct GroundVehicleCache;
193 extern const SaveLoad *GetVehicleDescription(VehicleType vt);
194 struct LoadgameState;
195 extern bool LoadOldVehicle(LoadgameState *ls, int num);
196 extern void FixOldVehicles();
197 
198 struct GRFFile;
199 
203 struct RefitDesc {
205  uint16 capacity;
206  uint16 remaining;
207  RefitDesc(CargoID cargo, uint16 capacity, uint16 remaining) :
208  cargo(cargo), capacity(capacity), remaining(remaining) {}
209 };
210 
213 private:
214  typedef std::list<RefitDesc> RefitList;
215  typedef std::map<CargoID, uint> CapacitiesMap;
216 
220 
223 
224 public:
225  friend const SaveLoad *GetVehicleDescription(VehicleType vt);
226  friend void FixOldVehicles();
227  friend void AfterLoadVehicles(bool part_of_load);
228  friend bool LoadOldVehicle(LoadgameState *ls, int num);
229 
231 
238 
242 
244 
246 
249 
253 
255 
256  /* Related to age and service time */
261  uint16 reliability;
267 
268  int32 x_pos;
269  int32 y_pos;
270  int32 z_pos;
272 
274 
279  byte spritenum;
281  byte x_extent;
282  byte y_extent;
283  byte z_extent;
284  int8 x_bb_offs;
285  int8 y_bb_offs;
286  int8 x_offs;
287  int8 y_offs;
289 
290  TextEffectID fill_percent_te_id;
292 
293  uint16 cur_speed;
294  byte subspeed;
296  uint32 motion_counter;
297  byte progress;
298 
299  byte random_bits;
301 
304 
307  uint16 cargo_cap;
308  uint16 refit_cap;
312 
313  byte day_counter;
316 
317  byte vehstatus;
319 
320  union {
323  } orders;
324 
327  byte subtype;
328 
331 
333 
334  void PreDestructor();
336  virtual ~Vehicle();
337 
338  void BeginLoading();
339  void CancelReservation(StationID next, Station *st);
340  void LeaveStation();
341 
344 
345  uint16 &GetGroundVehicleFlags();
346  const uint16 &GetGroundVehicleFlags() const;
347 
349 
350  void HandleLoading(bool mode = false);
351 
352  void GetConsistFreeCapacities(SmallMap<CargoID, uint> &capacities) const;
353 
354  uint GetConsistTotalCapacity() const;
355 
364  virtual void MarkDirty() {}
365 
372 
386  inline uint GetOldAdvanceSpeed(uint speed)
387  {
388  return (this->direction & 1) ? speed : speed * 3 / 4;
389  }
390 
403  static inline uint GetAdvanceSpeed(uint speed)
404  {
405  return speed * 3 / 4;
406  }
407 
415  inline uint GetAdvanceDistance()
416  {
417  return (this->direction & 1) ? 192 : 256;
418  }
419 
424  virtual ExpensesType GetExpenseType(bool income) const { return EXPENSES_OTHER; }
425 
429  virtual void PlayLeaveStationSound() const {}
430 
434  virtual bool IsPrimaryVehicle() const { return false; }
435 
436  const Engine *GetEngine() const;
437 
443  virtual void GetImage(Direction direction, EngineImageType image_type, VehicleSpriteSeq *result) const { result->Clear(); }
444 
445  const GRFFile *GetGRF() const;
446  uint32 GetGRFID() const;
447 
452  inline void InvalidateNewGRFCache()
453  {
454  this->grf_cache.cache_valid = 0;
455  }
456 
462  {
463  for (Vehicle *u = this; u != NULL; u = u->Next()) {
464  u->InvalidateNewGRFCache();
465  }
466  }
467 
472  inline bool IsGroundVehicle() const
473  {
474  return this->type == VEH_TRAIN || this->type == VEH_ROAD;
475  }
476 
481  virtual int GetDisplaySpeed() const { return 0; }
482 
487  virtual int GetDisplayMaxSpeed() const { return 0; }
488 
493  virtual int GetCurrentMaxSpeed() const { return 0; }
494 
499  virtual Money GetRunningCost() const { return 0; }
500 
505  virtual bool IsInDepot() const { return false; }
506 
511  virtual bool IsChainInDepot() const { return this->IsInDepot(); }
512 
517  bool IsStoppedInDepot() const
518  {
519  assert(this == this->First());
520  /* Free wagons have no VS_STOPPED state */
521  if (this->IsPrimaryVehicle() && !(this->vehstatus & VS_STOPPED)) return false;
522  return this->IsChainInDepot();
523  }
524 
529  virtual bool Tick() { return true; };
530 
534  virtual void OnNewDay() {};
535 
541  virtual uint Crash(bool flooded = false);
542 
555  virtual Trackdir GetVehicleTrackdir() const { return INVALID_TRACKDIR; }
556 
561  Money GetDisplayRunningCost() const { return (this->GetRunningCost() >> 8); }
562 
567  Money GetDisplayProfitThisYear() const { return (this->profit_this_year >> 8); }
568 
573  Money GetDisplayProfitLastYear() const { return (this->profit_last_year >> 8); }
574 
575  void SetNext(Vehicle *next);
576 
582  inline Vehicle *Next() const { return this->next; }
583 
589  inline Vehicle *Previous() const { return this->previous; }
590 
595  inline Vehicle *First() const { return this->first; }
596 
601  inline Vehicle *Last()
602  {
603  Vehicle *v = this;
604  while (v->Next() != NULL) v = v->Next();
605  return v;
606  }
607 
612  inline const Vehicle *Last() const
613  {
614  const Vehicle *v = this;
615  while (v->Next() != NULL) v = v->Next();
616  return v;
617  }
618 
624  inline Vehicle *Move(int n)
625  {
626  Vehicle *v = this;
627  if (n < 0) {
628  for (int i = 0; i != n && v != NULL; i--) v = v->Previous();
629  } else {
630  for (int i = 0; i != n && v != NULL; i++) v = v->Next();
631  }
632  return v;
633  }
634 
640  inline const Vehicle *Move(int n) const
641  {
642  const Vehicle *v = this;
643  if (n < 0) {
644  for (int i = 0; i != n && v != NULL; i--) v = v->Previous();
645  } else {
646  for (int i = 0; i != n && v != NULL; i++) v = v->Next();
647  }
648  return v;
649  }
650 
655  inline Order *GetFirstOrder() const { return (this->orders.list == NULL) ? NULL : this->orders.list->GetFirstOrder(); }
656 
657  void AddToShared(Vehicle *shared_chain);
658  void RemoveFromShared();
659 
664  inline Vehicle *NextShared() const { return this->next_shared; }
665 
670  inline Vehicle *PreviousShared() const { return this->previous_shared; }
671 
676  inline Vehicle *FirstShared() const { return (this->orders.list == NULL) ? this->First() : this->orders.list->GetFirstSharedVehicle(); }
677 
682  inline bool IsOrderListShared() const { return this->orders.list != NULL && this->orders.list->IsShared(); }
683 
688  inline VehicleOrderID GetNumOrders() const { return (this->orders.list == NULL) ? 0 : this->orders.list->GetNumOrders(); }
689 
694  inline VehicleOrderID GetNumManualOrders() const { return (this->orders.list == NULL) ? 0 : this->orders.list->GetNumManualOrders(); }
695 
701  {
702  return (this->orders.list == NULL) ? INVALID_STATION : this->orders.list->GetNextStoppingStation(this);
703  }
704 
705  void ResetRefitCaps();
706 
713  inline void CopyVehicleConfigAndStatistics(const Vehicle *src)
714  {
715  this->CopyConsistPropertiesFrom(src);
716 
717  this->unitnumber = src->unitnumber;
718 
719  this->current_order = src->current_order;
720  this->dest_tile = src->dest_tile;
721 
722  this->profit_this_year = src->profit_this_year;
723  this->profit_last_year = src->profit_last_year;
724  }
725 
726 
727  bool HandleBreakdown();
728 
729  bool NeedsAutorenewing(const Company *c, bool use_renew_setting = true) const;
730 
731  bool NeedsServicing() const;
732  bool NeedsAutomaticServicing() const;
733 
741  virtual TileIndex GetOrderStationLocation(StationID station) { return INVALID_TILE; }
742 
751  virtual bool FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse) { return false; }
752 
754 
755  void UpdateVisualEffect(bool allow_power_change = true);
756  void ShowVisualEffect() const;
757 
758  void UpdatePosition();
759  void UpdateViewport(bool dirty);
761  void MarkAllViewportsDirty() const;
762 
763  inline uint16 GetServiceInterval() const { return this->service_interval; }
764 
765  inline void SetServiceInterval(uint16 interval) { this->service_interval = interval; }
766 
767  inline bool ServiceIntervalIsCustom() const { return HasBit(this->vehicle_flags, VF_SERVINT_IS_CUSTOM); }
768 
769  inline bool ServiceIntervalIsPercent() const { return HasBit(this->vehicle_flags, VF_SERVINT_IS_PERCENT); }
770 
771  inline void SetServiceIntervalIsCustom(bool on) { SB(this->vehicle_flags, VF_SERVINT_IS_CUSTOM, 1, on); }
772 
773  inline void SetServiceIntervalIsPercent(bool on) { SB(this->vehicle_flags, VF_SERVINT_IS_PERCENT, 1, on); }
774 
775 private:
781  {
782  if (this->GetNumManualOrders() > 0) {
783  /* Advance to next real order */
784  do {
785  this->cur_real_order_index++;
786  if (this->cur_real_order_index >= this->GetNumOrders()) this->cur_real_order_index = 0;
787  } while (this->GetOrder(this->cur_real_order_index)->IsType(OT_IMPLICIT));
788  } else {
789  this->cur_real_order_index = 0;
790  }
791  }
792 
793 public:
800  {
801  if (this->cur_implicit_order_index == this->cur_real_order_index) {
802  /* Increment real order index as well */
803  this->SkipToNextRealOrderIndex();
804  }
805 
806  assert(this->cur_real_order_index == 0 || this->cur_real_order_index < this->GetNumOrders());
807 
808  /* Advance to next implicit order */
809  do {
810  this->cur_implicit_order_index++;
811  if (this->cur_implicit_order_index >= this->GetNumOrders()) this->cur_implicit_order_index = 0;
812  } while (this->cur_implicit_order_index != this->cur_real_order_index && !this->GetOrder(this->cur_implicit_order_index)->IsType(OT_IMPLICIT));
813 
814  InvalidateVehicleOrder(this, 0);
815  }
816 
824  {
825  if (this->cur_implicit_order_index == this->cur_real_order_index) {
826  /* Increment both real and implicit order */
828  } else {
829  /* Increment real order only */
830  this->SkipToNextRealOrderIndex();
831  InvalidateVehicleOrder(this, 0);
832  }
833  }
834 
839  {
840  /* Make sure the index is valid */
841  if (this->cur_real_order_index >= this->GetNumOrders()) this->cur_real_order_index = 0;
842 
843  if (this->GetNumManualOrders() > 0) {
844  /* Advance to next real order */
845  while (this->GetOrder(this->cur_real_order_index)->IsType(OT_IMPLICIT)) {
846  this->cur_real_order_index++;
847  if (this->cur_real_order_index >= this->GetNumOrders()) this->cur_real_order_index = 0;
848  }
849  } else {
850  this->cur_real_order_index = 0;
851  }
852  }
853 
859  inline Order *GetOrder(int index) const
860  {
861  return (this->orders.list == NULL) ? NULL : this->orders.list->GetOrderAt(index);
862  }
863 
868  inline Order *GetLastOrder() const
869  {
870  return (this->orders.list == NULL) ? NULL : this->orders.list->GetLastOrder();
871  }
872 
873  bool IsEngineCountable() const;
874  bool HasEngineType() const;
875  bool HasDepotOrder() const;
876  void HandlePathfindingResult(bool path_found);
877 
882  inline bool IsFrontEngine() const
883  {
884  return this->IsGroundVehicle() && HasBit(this->subtype, GVSF_FRONT);
885  }
886 
891  inline bool IsArticulatedPart() const
892  {
893  return this->IsGroundVehicle() && HasBit(this->subtype, GVSF_ARTICULATED_PART);
894  }
895 
900  inline bool HasArticulatedPart() const
901  {
902  return this->Next() != NULL && this->Next()->IsArticulatedPart();
903  }
904 
911  {
912  assert(this->HasArticulatedPart());
913  return this->Next();
914  }
915 
921  {
922  Vehicle *v = this;
923  while (v->IsArticulatedPart()) v = v->Previous();
924  return v;
925  }
926 
931  inline const Vehicle *GetFirstEnginePart() const
932  {
933  const Vehicle *v = this;
934  while (v->IsArticulatedPart()) v = v->Previous();
935  return v;
936  }
937 
943  {
944  Vehicle *v = this;
945  while (v->HasArticulatedPart()) v = v->GetNextArticulatedPart();
946  return v;
947  }
948 
953  inline Vehicle *GetNextVehicle() const
954  {
955  const Vehicle *v = this;
956  while (v->HasArticulatedPart()) v = v->GetNextArticulatedPart();
957 
958  /* v now contains the last articulated part in the engine */
959  return v->Next();
960  }
961 
966  inline Vehicle *GetPrevVehicle() const
967  {
968  Vehicle *v = this->Previous();
969  while (v != NULL && v->IsArticulatedPart()) v = v->Previous();
970 
971  return v;
972  }
973 };
974 
980 #define FOR_ALL_VEHICLES_FROM(var, start) FOR_ALL_ITEMS_FROM(Vehicle, vehicle_index, var, start)
981 
986 #define FOR_ALL_VEHICLES(var) FOR_ALL_VEHICLES_FROM(var, 0)
987 
992 template <class T, VehicleType Type>
993 struct SpecializedVehicle : public Vehicle {
994  static const VehicleType EXPECTED_TYPE = Type;
995 
997 
1002  {
1003  this->sprite_seq.count = 1;
1004  }
1005 
1010  inline T *First() const { return (T *)this->Vehicle::First(); }
1011 
1016  inline T *Last() { return (T *)this->Vehicle::Last(); }
1017 
1022  inline const T *Last() const { return (const T *)this->Vehicle::Last(); }
1023 
1028  inline T *Next() const { return (T *)this->Vehicle::Next(); }
1029 
1034  inline T *Previous() const { return (T *)this->Vehicle::Previous(); }
1035 
1041  inline T *GetNextArticulatedPart() { return (T *)this->Vehicle::GetNextArticulatedPart(); }
1042 
1048  inline T *GetNextArticulatedPart() const { return (T *)this->Vehicle::GetNextArticulatedPart(); }
1049 
1054  inline T *GetFirstEnginePart() { return (T *)this->Vehicle::GetFirstEnginePart(); }
1055 
1060  inline const T *GetFirstEnginePart() const { return (const T *)this->Vehicle::GetFirstEnginePart(); }
1061 
1066  inline T *GetLastEnginePart() { return (T *)this->Vehicle::GetLastEnginePart(); }
1067 
1072  inline T *GetNextVehicle() const { return (T *)this->Vehicle::GetNextVehicle(); }
1073 
1078  inline T *GetPrevVehicle() const { return (T *)this->Vehicle::GetPrevVehicle(); }
1079 
1085  static inline bool IsValidID(size_t index)
1086  {
1087  return Vehicle::IsValidID(index) && Vehicle::Get(index)->type == Type;
1088  }
1089 
1094  static inline T *Get(size_t index)
1095  {
1096  return (T *)Vehicle::Get(index);
1097  }
1098 
1103  static inline T *GetIfValid(size_t index)
1104  {
1105  return IsValidID(index) ? Get(index) : NULL;
1106  }
1107 
1113  static inline T *From(Vehicle *v)
1114  {
1115  assert(v->type == Type);
1116  return (T *)v;
1117  }
1118 
1124  static inline const T *From(const Vehicle *v)
1125  {
1126  assert(v->type == Type);
1127  return (const T *)v;
1128  }
1129 
1135  inline void UpdateViewport(bool force_update, bool update_delta)
1136  {
1137  /* Skip updating sprites on dedicated servers without screen */
1138  if (_network_dedicated) return;
1139 
1140  /* Explicitly choose method to call to prevent vtable dereference -
1141  * it gives ~3% runtime improvements in games with many vehicles */
1142  if (update_delta) ((T *)this)->T::UpdateDeltaXY(this->direction);
1143  VehicleSpriteSeq seq;
1144  ((T *)this)->T::GetImage(this->direction, EIT_ON_MAP, &seq);
1145  if (force_update || this->sprite_seq != seq) {
1146  this->sprite_seq = seq;
1147  this->Vehicle::UpdateViewport(true);
1148  }
1149  }
1150 };
1151 
1157 #define FOR_ALL_VEHICLES_OF_TYPE(name, var) FOR_ALL_ITEMS_FROM(name, vehicle_index, var, 0) if (var->type == name::EXPECTED_TYPE)
1158 
1161  bool *cache;
1164 
1166  UnitID NextID();
1167 
1170 };
1171 
1173 static const int32 INVALID_COORD = 0x7fffffff;
1174 
1175 #endif /* VEHICLE_BASE_H */