OpenTTD
ground_vehicle.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
10 #ifndef GROUND_VEHICLE_HPP
11 #define GROUND_VEHICLE_HPP
12 
13 #include "vehicle_base.h"
14 #include "vehicle_gui.h"
15 #include "landscape.h"
16 #include "window_func.h"
17 #include "widgets/vehicle_widget.h"
18 
23 };
24 
30  /* Cached acceleration values, recalculated when the cargo on a vehicle changes (in addition to the conditions below) */
31  uint32 cached_weight;
33  uint32 cached_max_te;
35 
36  /* Cached acceleration values, recalculated on load and each time a vehicle is added to/removed from the consist. */
38  uint32 cached_power;
39  uint32 cached_air_drag;
40 
41  /* Cached NewGRF values, recalculated on load and each time a vehicle is added to/removed from the consist. */
45 
46  /* Cached UI information. */
47  uint16 last_speed;
48 };
49 
55 };
56 
78 template <class T, VehicleType Type>
79 struct GroundVehicle : public SpecializedVehicle<T, Type> {
81  uint16 gv_flags;
82 
84 
89 
90  void PowerChanged();
91  void CargoChanged();
92  int GetAcceleration() const;
93  bool IsChainInDepot() const override;
94 
100  uint Crash(bool flooded) override
101  {
102  /* Crashed vehicles aren't going up or down */
103  for (T *v = T::From(this); v != nullptr; v = v->Next()) {
104  ClrBit(v->gv_flags, GVF_GOINGUP_BIT);
105  ClrBit(v->gv_flags, GVF_GOINGDOWN_BIT);
106  }
107  return this->Vehicle::Crash(flooded);
108  }
109 
114  inline int64 GetSlopeResistance() const
115  {
116  int64 incl = 0;
117 
118  for (const T *u = T::From(this); u != nullptr; u = u->Next()) {
119  if (HasBit(u->gv_flags, GVF_GOINGUP_BIT)) {
120  incl += u->gcache.cached_slope_resistance;
121  } else if (HasBit(u->gv_flags, GVF_GOINGDOWN_BIT)) {
122  incl -= u->gcache.cached_slope_resistance;
123  }
124  }
125 
126  return incl;
127  }
128 
136  {
137  this->z_pos = GetSlopePixelZ(this->x_pos, this->y_pos);
138  ClrBit(this->gv_flags, GVF_GOINGUP_BIT);
139  ClrBit(this->gv_flags, GVF_GOINGDOWN_BIT);
140 
141  if (T::From(this)->TileMayHaveSlopedTrack()) {
142  /* To check whether the current tile is sloped, and in which
143  * direction it is sloped, we get the 'z' at the center of
144  * the tile (middle_z) and the edge of the tile (old_z),
145  * which we then can compare. */
146  int middle_z = GetSlopePixelZ((this->x_pos & ~TILE_UNIT_MASK) | (TILE_SIZE / 2), (this->y_pos & ~TILE_UNIT_MASK) | (TILE_SIZE / 2));
147 
148  if (middle_z != this->z_pos) {
149  SetBit(this->gv_flags, (middle_z > this->z_pos) ? GVF_GOINGUP_BIT : GVF_GOINGDOWN_BIT);
150  }
151  }
152  }
153 
160  inline void UpdateZPosition()
161  {
162 #if 0
163  /* The following code does this: */
164 
165  if (HasBit(this->gv_flags, GVF_GOINGUP_BIT)) {
166  switch (this->direction) {
167  case DIR_NE:
168  this->z_pos += (this->x_pos & 1); break;
169  case DIR_SW:
170  this->z_pos += (this->x_pos & 1) ^ 1; break;
171  case DIR_NW:
172  this->z_pos += (this->y_pos & 1); break;
173  case DIR_SE:
174  this->z_pos += (this->y_pos & 1) ^ 1; break;
175  default: break;
176  }
177  } else if (HasBit(this->gv_flags, GVF_GOINGDOWN_BIT)) {
178  switch (this->direction) {
179  case DIR_NE:
180  this->z_pos -= (this->x_pos & 1); break;
181  case DIR_SW:
182  this->z_pos -= (this->x_pos & 1) ^ 1; break;
183  case DIR_NW:
184  this->z_pos -= (this->y_pos & 1); break;
185  case DIR_SE:
186  this->z_pos -= (this->y_pos & 1) ^ 1; break;
187  default: break;
188  }
189  }
190 
191  /* But gcc 4.4.5 isn't able to nicely optimise it, and the resulting
192  * code is full of conditional jumps. */
193 #endif
194 
195  /* Vehicle's Z position can change only if it has GVF_GOINGUP_BIT or GVF_GOINGDOWN_BIT set.
196  * Furthermore, if this function is called once every time the vehicle's position changes,
197  * we know the Z position changes by +/-1 at certain moments - when x_pos, y_pos is odd/even,
198  * depending on orientation of the slope and vehicle's direction */
199 
200  if (HasBit(this->gv_flags, GVF_GOINGUP_BIT) || HasBit(this->gv_flags, GVF_GOINGDOWN_BIT)) {
201  if (T::From(this)->HasToUseGetSlopePixelZ()) {
202  /* In some cases, we have to use GetSlopePixelZ() */
203  this->z_pos = GetSlopePixelZ(this->x_pos, this->y_pos);
204  return;
205  }
206  /* DirToDiagDir() is a simple right shift */
207  DiagDirection dir = DirToDiagDir(this->direction);
208  /* Read variables, so the compiler knows the access doesn't trap */
209  int8 x_pos = this->x_pos;
210  int8 y_pos = this->y_pos;
211  /* DiagDirToAxis() is a simple mask */
212  int8 d = DiagDirToAxis(dir) == AXIS_X ? x_pos : y_pos;
213  /* We need only the least significant bit */
214  d &= 1;
215  /* Conditional "^ 1". Optimised to "(dir - 1) <= 1". */
216  d ^= (int8)(dir == DIAGDIR_SW || dir == DIAGDIR_SE);
217  /* Subtraction instead of addition because we are testing for GVF_GOINGUP_BIT.
218  * GVF_GOINGUP_BIT is used because it's bit 0, so simple AND can be used,
219  * without any shift */
220  this->z_pos += HasBit(this->gv_flags, GVF_GOINGUP_BIT) ? d : -d;
221  }
222 
223  assert(this->z_pos == GetSlopePixelZ(this->x_pos, this->y_pos));
224  }
225 
232  inline int UpdateInclination(bool new_tile, bool update_delta)
233  {
234  int old_z = this->z_pos;
235 
236  if (new_tile) {
237  this->UpdateZPositionAndInclination();
238  } else {
239  this->UpdateZPosition();
240  }
241 
242  this->UpdateViewport(true, update_delta);
243  return old_z;
244  }
245 
249  inline void SetFrontEngine() { SetBit(this->subtype, GVSF_FRONT); }
250 
254  inline void ClearFrontEngine() { ClrBit(this->subtype, GVSF_FRONT); }
255 
259  inline void SetArticulatedPart() { SetBit(this->subtype, GVSF_ARTICULATED_PART); }
260 
264  inline void ClearArticulatedPart() { ClrBit(this->subtype, GVSF_ARTICULATED_PART); }
265 
269  inline void SetWagon() { SetBit(this->subtype, GVSF_WAGON); }
270 
274  inline void ClearWagon() { ClrBit(this->subtype, GVSF_WAGON); }
275 
279  inline void SetEngine() { SetBit(this->subtype, GVSF_ENGINE); }
280 
284  inline void ClearEngine() { ClrBit(this->subtype, GVSF_ENGINE); }
285 
289  inline void SetFreeWagon() { SetBit(this->subtype, GVSF_FREE_WAGON); }
290 
294  inline void ClearFreeWagon() { ClrBit(this->subtype, GVSF_FREE_WAGON); }
295 
299  inline void SetMultiheaded() { SetBit(this->subtype, GVSF_MULTIHEADED); }
300 
304  inline void ClearMultiheaded() { ClrBit(this->subtype, GVSF_MULTIHEADED); }
305 
310  inline bool IsFreeWagon() const { return HasBit(this->subtype, GVSF_FREE_WAGON); }
311 
316  inline bool IsEngine() const { return HasBit(this->subtype, GVSF_ENGINE); }
317 
322  inline bool IsWagon() const { return HasBit(this->subtype, GVSF_WAGON); }
323 
328  inline bool IsMultiheaded() const { return HasBit(this->subtype, GVSF_MULTIHEADED); }
329 
334  inline bool IsRearDualheaded() const { return this->IsMultiheaded() && !this->IsEngine(); }
335 
341  inline void SetLastSpeed()
342  {
343  if (this->cur_speed != this->gcache.last_speed) {
345  this->gcache.last_speed = this->cur_speed;
346  }
347  }
348 
349 protected:
363  inline uint DoUpdateSpeed(uint accel, int min_speed, int max_speed)
364  {
365  uint spd = this->subspeed + accel;
366  this->subspeed = (byte)spd;
367 
368  /* When we are going faster than the maximum speed, reduce the speed
369  * somewhat gradually. But never lower than the maximum speed. */
370  int tempmax = max_speed;
371  if (this->cur_speed > max_speed) {
372  tempmax = max(this->cur_speed - (this->cur_speed / 10) - 1, max_speed);
373  }
374 
375  /* Enforce a maximum and minimum speed. Normally we would use something like
376  * Clamp for this, but in this case min_speed might be below the maximum speed
377  * threshold for some reason. That makes acceleration fail and assertions
378  * happen in Clamp. So make it explicit that min_speed overrules the maximum
379  * speed by explicit ordering of min and max. */
380  this->cur_speed = spd = max(min(this->cur_speed + ((int)spd >> 8), tempmax), min_speed);
381 
382  int scaled_spd = this->GetAdvanceSpeed(spd);
383 
384  scaled_spd += this->progress;
385  this->progress = 0; // set later in *Handler or *Controller
386  return scaled_spd;
387  }
388 };
389 
390 #endif /* GROUND_VEHICLE_HPP */
void ClearEngine()
Clear engine status.
AccelStatus
What is the status of our acceleration?
GroundVehicleFlags
Ground vehicle flags.
static DiagDirection DirToDiagDir(Direction dir)
Convert a Direction to a DiagDirection.
void SetFrontEngine()
Set front engine state.
virtual uint Crash(bool flooded=false)
Crash the (whole) vehicle chain.
Definition: vehicle.cpp:259
uint DoUpdateSpeed(uint accel, int min_speed, int max_speed)
Update the speed of the vehicle.
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
void ClearArticulatedPart()
Clear a vehicle from being an articulated part.
Leading engine of a consist.
Definition: vehicle_base.h:111
void UpdateZPositionAndInclination()
Updates vehicle&#39;s Z position and inclination.
Start or stop this vehicle, and show information about the current state.
bool IsMultiheaded() const
Check if the vehicle is a multiheaded engine.
Engine that can be front engine, but might be placed behind another engine (not used for road vehicle...
Definition: vehicle_base.h:114
uint32 cached_power
Total power of the consist (valid only for the first engine).
GroundVehicle()
The constructor at SpecializedVehicle must be called.
uint16 cached_axle_resistance
Resistance caused by the axles of the vehicle (valid only for the first engine).
static T max(const T a, const T b)
Returns the maximum of two values.
Definition: math_func.hpp:24
Wagon (not used for road vehicles).
Definition: vehicle_base.h:113
Cached, frequently calculated values.
Functions related to the vehicle&#39;s GUIs.
static const uint TILE_SIZE
Tile size in world coordinates.
Definition: tile_type.h:13
Southwest.
EngineID first_engine
Cached EngineID of the front vehicle. INVALID_ENGINE for the front vehicle itself.
Class defining several overloaded accessors so we don&#39;t have to cast vehicle types that often...
Definition: vehicle_base.h:979
uint32 cached_air_drag
Air drag coefficient of the vehicle (valid only for the first engine).
GroundVehicle< T, Type > GroundVehicleBase
Our type.
bool IsWagon() const
Check if a vehicle is a wagon.
Southeast.
void SetEngine()
Set engine status.
int64 GetSlopeResistance() const
Calculates the total slope resistance for this vehicle.
We want to stop.
Southeast.
void ClearFrontEngine()
Remove the front engine state.
void ClearFreeWagon()
Clear a vehicle from being a free wagon.
Vehicle view; Window numbers:
Definition: window_type.h:332
uint32 cached_slope_resistance
Resistance caused by weight when this vehicle part is at a slope.
bool IsRearDualheaded() const
Tell if we are dealing with the rear end of a multiheaded engine.
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:40
Vehicle is currently going uphill. (Cached track information for acceleration)
We want to go faster, if possible of course.
Engine is multiheaded (not used for road vehicles).
Definition: vehicle_base.h:116
uint16 last_speed
The last speed we did display, so we only have to redraw when this changes.
The X axis.
static Axis DiagDirToAxis(DiagDirection d)
Convert a DiagDirection to the axis.
uint Crash(bool flooded) override
Common code executed for crashed ground vehicles.
Vehicle is currently going downhill. (Cached track information for acceleration)
Articulated part of an engine.
Definition: vehicle_base.h:112
void UpdateZPosition()
Updates vehicle&#39;s Z position.
static T ClrBit(T &x, const uint8 y)
Clears a bit in a variable.
void SetWindowWidgetDirty(WindowClass cls, WindowNumber number, byte widget_index)
Mark a particular widget in a particular window as dirty (in need of repainting)
Definition: window.cpp:3229
Base class for all vehicles.
uint16 EngineID
Unique identification number of an engine.
Definition: engine_type.h:21
uint16 cached_total_length
Length of the whole vehicle (valid only for the first engine).
First in a wagon chain (in depot) (not used for road vehicles).
Definition: vehicle_base.h:115
Northwest.
uint16 cached_max_track_speed
Maximum consist speed (in internal units) limited by track type (valid only for the first engine)...
static const uint TILE_UNIT_MASK
For masking in/out the inner-tile world coordinate units.
Definition: tile_type.h:14
Functions related to OTTD&#39;s landscape.
Northeast.
void SetLastSpeed()
Update the GUI variant of the current speed of the vehicle.
bool IsFreeWagon() const
Check if the vehicle is a free wagon (got no engine in front of it).
uint8 cached_veh_length
Length of this vehicle in units of 1/VEHICLE_LENGTH of normal length. It is cached because this can b...
Types related to the vehicle widgets.
void ClearMultiheaded()
Clear multiheaded engine property.
void SetArticulatedPart()
Set a vehicle to be an articulated part.
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
DiagDirection
Enumeration for diagonal directions.
void SetFreeWagon()
Set a vehicle as a free wagon.
uint32 cached_max_te
Maximum tractive effort of consist (valid only for the first engine).
Window functions not directly related to making/drawing windows.
int UpdateInclination(bool new_tile, bool update_delta)
Checks if the vehicle is in a slope and sets the required flags in that case.
Base class for all vehicles that move through ground.
void SetMultiheaded()
Set a vehicle as a multiheaded engine.
void SetWagon()
Set a vehicle to be a wagon.
uint32 cached_weight
Total weight of the consist (valid only for the first engine).
Disable insertion and removal of automatic orders until the vehicle completes the real order...
bool IsEngine() const
Check if a vehicle is an engine (can be first in a consist).
GroundVehicleCache gcache
Cache of often calculated values.
void ClearWagon()
Clear wagon property.
Southwest.