2006-06-05 10:23:18 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2007-04-03 19:19:04 +00:00
|
|
|
/** @file src/roadveh.h Road vehicle states */
|
2007-03-28 20:41:35 +00:00
|
|
|
|
2006-09-28 18:42:35 +00:00
|
|
|
#ifndef ROADVEH_H
|
|
|
|
#define ROADVEH_H
|
|
|
|
|
2010-03-06 12:54:42 +00:00
|
|
|
#include "ground_vehicle.hpp"
|
|
|
|
#include "engine_base.h"
|
|
|
|
#include "cargotype.h"
|
2010-08-26 22:01:16 +00:00
|
|
|
#include "track_func.h"
|
|
|
|
#include "road_type.h"
|
2010-10-30 14:31:49 +00:00
|
|
|
#include "newgrf_engine.h"
|
2006-06-05 10:23:18 +00:00
|
|
|
|
2009-05-22 20:18:45 +00:00
|
|
|
struct RoadVehicle;
|
|
|
|
|
2009-05-22 20:22:20 +00:00
|
|
|
/** Road vehicle states */
|
|
|
|
enum RoadVehicleStates {
|
|
|
|
/*
|
|
|
|
* Lower 4 bits are used for vehicle track direction. (Trackdirs)
|
|
|
|
* When in a road stop (bit 5 or bit 6 set) these bits give the
|
|
|
|
* track direction of the entry to the road stop.
|
|
|
|
* As the entry direction will always be a diagonal
|
|
|
|
* direction (X_NE, Y_SE, X_SW or Y_NW) only bits 0 and 3
|
|
|
|
* are needed to hold this direction. Bit 1 is then used to show
|
|
|
|
* that the vehicle is using the second road stop bay.
|
|
|
|
* Bit 2 is then used for drive-through stops to show the vehicle
|
|
|
|
* is stopping at this road stop.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Numeric values */
|
|
|
|
RVSB_IN_DEPOT = 0xFE, ///< The vehicle is in a depot
|
|
|
|
RVSB_WORMHOLE = 0xFF, ///< The vehicle is in a tunnel and/or bridge
|
|
|
|
|
|
|
|
/* Bit numbers */
|
|
|
|
RVS_USING_SECOND_BAY = 1, ///< Only used while in a road stop
|
2010-11-20 09:09:57 +00:00
|
|
|
RVS_ENTERED_STOP = 2, ///< Only set when a vehicle has entered the stop
|
2009-05-22 20:22:20 +00:00
|
|
|
RVS_DRIVE_SIDE = 4, ///< Only used when retrieving move data
|
|
|
|
RVS_IN_ROAD_STOP = 5, ///< The vehicle is in a road stop
|
|
|
|
RVS_IN_DT_ROAD_STOP = 6, ///< The vehicle is in a drive-through road stop
|
|
|
|
|
|
|
|
/* Bit sets of the above specified bits */
|
|
|
|
RVSB_IN_ROAD_STOP = 1 << RVS_IN_ROAD_STOP, ///< The vehicle is in a road stop
|
|
|
|
RVSB_IN_ROAD_STOP_END = RVSB_IN_ROAD_STOP + TRACKDIR_END,
|
|
|
|
RVSB_IN_DT_ROAD_STOP = 1 << RVS_IN_DT_ROAD_STOP, ///< The vehicle is in a drive-through road stop
|
|
|
|
RVSB_IN_DT_ROAD_STOP_END = RVSB_IN_DT_ROAD_STOP + TRACKDIR_END,
|
|
|
|
|
2011-02-02 21:56:56 +00:00
|
|
|
RVSB_DRIVE_SIDE = 1 << RVS_DRIVE_SIDE, ///< The vehicle is at the opposite side of the road
|
|
|
|
|
2009-05-22 20:22:20 +00:00
|
|
|
RVSB_TRACKDIR_MASK = 0x0F, ///< The mask used to extract track dirs
|
2011-12-19 17:48:04 +00:00
|
|
|
RVSB_ROAD_STOP_TRACKDIR_MASK = 0x09, ///< Only bits 0 and 3 are used to encode the trackdir for road stops
|
2009-05-22 20:22:20 +00:00
|
|
|
};
|
|
|
|
|
2009-02-06 15:39:34 +00:00
|
|
|
/** State information about the Road Vehicle controller */
|
2010-05-13 09:44:44 +00:00
|
|
|
static const uint RDE_NEXT_TILE = 0x80; ///< We should enter the next tile
|
|
|
|
static const uint RDE_TURNED = 0x40; ///< We just finished turning
|
|
|
|
|
|
|
|
/* Start frames for when a vehicle enters a tile/changes its state.
|
|
|
|
* The start frame is different for vehicles that turned around or
|
|
|
|
* are leaving the depot as the do not start at the edge of the tile.
|
|
|
|
* For trams there are a few different start frames as there are two
|
|
|
|
* places where trams can turn. */
|
|
|
|
static const uint RVC_DEFAULT_START_FRAME = 0;
|
|
|
|
static const uint RVC_TURN_AROUND_START_FRAME = 1;
|
|
|
|
static const uint RVC_DEPOT_START_FRAME = 6;
|
|
|
|
static const uint RVC_START_FRAME_AFTER_LONG_TRAM = 21;
|
|
|
|
static const uint RVC_TURN_AROUND_START_FRAME_SHORT_TRAM = 16;
|
|
|
|
/* Stop frame for a vehicle in a drive-through stop */
|
|
|
|
static const uint RVC_DRIVE_THROUGH_STOP_FRAME = 11;
|
|
|
|
static const uint RVC_DEPOT_STOP_FRAME = 11;
|
2009-02-06 15:39:34 +00:00
|
|
|
|
2011-02-02 22:06:14 +00:00
|
|
|
/** The number of ticks a vehicle has for overtaking. */
|
|
|
|
static const byte RV_OVERTAKE_TIMEOUT = 35;
|
|
|
|
|
2011-11-04 15:04:29 +00:00
|
|
|
void RoadVehUpdateCache(RoadVehicle *v, bool same_length = false);
|
2008-04-11 08:14:43 +00:00
|
|
|
|
2007-04-29 21:24:08 +00:00
|
|
|
/**
|
2009-05-26 23:24:34 +00:00
|
|
|
* Buses, trucks and trams belong to this class.
|
2007-04-29 21:24:08 +00:00
|
|
|
*/
|
2011-12-18 17:17:18 +00:00
|
|
|
struct RoadVehicle FINAL : public GroundVehicle<RoadVehicle, VEH_ROAD> {
|
2009-05-22 20:22:20 +00:00
|
|
|
byte state; ///< @see RoadVehicleStates
|
|
|
|
byte frame;
|
|
|
|
uint16 blocked_ctr;
|
2011-02-02 21:56:56 +00:00
|
|
|
byte overtaking; ///< Set to #RVSB_DRIVE_SIDE when overtaking, otherwise 0.
|
|
|
|
byte overtaking_ctr; ///< The length of the current overtake attempt.
|
2011-02-05 16:20:55 +00:00
|
|
|
uint16 crashed_ctr; ///< Animation counter when the vehicle has crashed. @see RoadVehIsCrashed
|
2009-05-22 20:22:20 +00:00
|
|
|
byte reverse_ctr;
|
|
|
|
|
|
|
|
RoadType roadtype;
|
|
|
|
RoadTypes compatible_roadtypes;
|
|
|
|
|
2009-06-02 19:12:28 +00:00
|
|
|
/** We don't want GCC to zero our struct! It already is zeroed and has an index! */
|
2011-01-21 14:43:38 +00:00
|
|
|
RoadVehicle() : GroundVehicleBase() {}
|
2007-04-29 21:24:08 +00:00
|
|
|
/** We want to 'destruct' the right class. */
|
2007-08-05 17:43:04 +00:00
|
|
|
virtual ~RoadVehicle() { this->PreDestructor(); }
|
2007-04-29 21:24:08 +00:00
|
|
|
|
2010-03-06 12:54:42 +00:00
|
|
|
friend struct GroundVehicle<RoadVehicle, VEH_ROAD>; // GroundVehicle needs to use the acceleration functions defined at RoadVehicle.
|
|
|
|
|
2007-04-29 22:33:51 +00:00
|
|
|
void MarkDirty();
|
2007-05-01 16:35:14 +00:00
|
|
|
void UpdateDeltaXY(Direction direction);
|
2007-05-02 09:39:11 +00:00
|
|
|
ExpensesType GetExpenseType(bool income) const { return income ? EXPENSES_ROADVEH_INC : EXPENSES_ROADVEH_RUN; }
|
2011-01-19 18:44:13 +00:00
|
|
|
bool IsPrimaryVehicle() const { return this->IsFrontEngine(); }
|
2011-11-01 16:51:47 +00:00
|
|
|
SpriteID GetImage(Direction direction, EngineImageType image_type) const;
|
2011-01-26 17:31:44 +00:00
|
|
|
int GetDisplaySpeed() const { return this->gcache.last_speed / 2; }
|
2010-11-06 13:05:11 +00:00
|
|
|
int GetDisplayMaxSpeed() const { return this->vcache.cached_max_speed / 2; }
|
2009-11-08 13:02:05 +00:00
|
|
|
Money GetRunningCost() const;
|
2009-07-18 12:34:19 +00:00
|
|
|
int GetDisplayImageWidth(Point *offset = NULL) const;
|
2009-05-22 20:22:20 +00:00
|
|
|
bool IsInDepot() const { return this->state == RVSB_IN_DEPOT; }
|
2008-08-17 11:12:56 +00:00
|
|
|
bool IsStoppedInDepot() const;
|
2009-05-22 13:53:14 +00:00
|
|
|
bool Tick();
|
2008-02-01 22:02:14 +00:00
|
|
|
void OnNewDay();
|
2009-12-04 20:29:46 +00:00
|
|
|
uint Crash(bool flooded = false);
|
2009-05-22 18:17:20 +00:00
|
|
|
Trackdir GetVehicleTrackdir() const;
|
2008-04-05 10:55:50 +00:00
|
|
|
TileIndex GetOrderStationLocation(StationID station);
|
2008-04-11 08:14:43 +00:00
|
|
|
bool FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse);
|
2009-07-02 08:59:27 +00:00
|
|
|
|
2009-12-02 17:37:02 +00:00
|
|
|
bool IsBus() const;
|
|
|
|
|
2010-03-06 12:56:57 +00:00
|
|
|
int GetCurrentMaxSpeed() const;
|
2011-01-26 17:35:03 +00:00
|
|
|
int UpdateSpeed();
|
2010-03-06 12:56:57 +00:00
|
|
|
|
2010-03-06 12:54:42 +00:00
|
|
|
protected: // These functions should not be called outside acceleration code.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allows to know the power value that this vehicle will use.
|
|
|
|
* @return Power value from the engine in HP, or zero if the vehicle is not powered.
|
|
|
|
*/
|
|
|
|
FORCEINLINE uint16 GetPower() const
|
|
|
|
{
|
|
|
|
/* Power is not added for articulated parts */
|
|
|
|
if (!this->IsArticulatedPart()) {
|
2010-10-30 14:31:49 +00:00
|
|
|
/* Road vehicle power is in units of 10 HP. */
|
|
|
|
return 10 * GetVehicleProperty(this, PROP_ROADVEH_POWER, RoadVehInfo(this->engine_type)->power);
|
2010-03-06 12:54:42 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a value if this articulated part is powered.
|
|
|
|
* @return Zero, because road vehicles don't have powered parts.
|
|
|
|
*/
|
|
|
|
FORCEINLINE uint16 GetPoweredPartPower(const RoadVehicle *head) const
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allows to know the weight value that this vehicle will use.
|
|
|
|
* @return Weight value from the engine in tonnes.
|
|
|
|
*/
|
|
|
|
FORCEINLINE uint16 GetWeight() const
|
|
|
|
{
|
|
|
|
uint16 weight = (CargoSpec::Get(this->cargo_type)->weight * this->cargo.Count()) / 16;
|
|
|
|
|
|
|
|
/* Vehicle weight is not added for articulated parts. */
|
|
|
|
if (!this->IsArticulatedPart()) {
|
2010-10-30 14:31:49 +00:00
|
|
|
/* Road vehicle weight is in units of 1/4 t. */
|
|
|
|
weight += GetVehicleProperty(this, PROP_ROADVEH_WEIGHT, RoadVehInfo(this->engine_type)->weight) / 4;
|
2010-03-06 12:54:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return weight;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allows to know the tractive effort value that this vehicle will use.
|
|
|
|
* @return Tractive effort value from the engine.
|
|
|
|
*/
|
|
|
|
FORCEINLINE byte GetTractiveEffort() const
|
|
|
|
{
|
2010-10-30 14:31:49 +00:00
|
|
|
/* The tractive effort coefficient is in units of 1/256. */
|
|
|
|
return GetVehicleProperty(this, PROP_ROADVEH_TRACTIVE_EFFORT, RoadVehInfo(this->engine_type)->tractive_effort);
|
2010-03-06 12:54:42 +00:00
|
|
|
}
|
|
|
|
|
2010-08-12 08:39:06 +00:00
|
|
|
/**
|
2010-08-02 14:49:23 +00:00
|
|
|
* Gets the area used for calculating air drag.
|
2010-11-07 13:35:07 +00:00
|
|
|
* @return Area of the engine in m^2.
|
2010-08-02 14:49:23 +00:00
|
|
|
*/
|
|
|
|
FORCEINLINE byte GetAirDragArea() const
|
|
|
|
{
|
2010-11-07 13:35:07 +00:00
|
|
|
return 6;
|
2010-08-02 14:49:23 +00:00
|
|
|
}
|
|
|
|
|
2010-08-02 14:52:51 +00:00
|
|
|
/**
|
|
|
|
* Gets the air drag coefficient of this vehicle.
|
|
|
|
* @return Air drag value from the engine.
|
|
|
|
*/
|
|
|
|
FORCEINLINE byte GetAirDrag() const
|
|
|
|
{
|
|
|
|
return RoadVehInfo(this->engine_type)->air_drag;
|
|
|
|
}
|
|
|
|
|
2010-03-06 12:54:42 +00:00
|
|
|
/**
|
|
|
|
* Checks the current acceleration status of this vehicle.
|
|
|
|
* @return Acceleration status.
|
|
|
|
*/
|
|
|
|
FORCEINLINE AccelStatus GetAccelerationStatus() const
|
|
|
|
{
|
|
|
|
return (this->vehstatus & VS_STOPPED) ? AS_BRAKE : AS_ACCEL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the current speed of this vehicle.
|
2010-11-07 13:35:07 +00:00
|
|
|
* @return Current speed in km/h-ish.
|
2010-03-06 12:54:42 +00:00
|
|
|
*/
|
|
|
|
FORCEINLINE uint16 GetCurrentSpeed() const
|
|
|
|
{
|
2010-11-07 13:35:07 +00:00
|
|
|
return this->cur_speed / 2;
|
2010-03-06 12:54:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the rolling friction coefficient of this vehicle.
|
2010-11-07 13:35:07 +00:00
|
|
|
* @return Rolling friction coefficient in [1e-4].
|
2010-03-06 12:54:42 +00:00
|
|
|
*/
|
|
|
|
FORCEINLINE uint32 GetRollingFriction() const
|
|
|
|
{
|
2010-11-07 13:35:07 +00:00
|
|
|
/* Trams have a slightly greater friction coefficient than trains.
|
|
|
|
* The rest of road vehicles have bigger values. */
|
|
|
|
uint32 coeff = (this->roadtype == ROADTYPE_TRAM) ? 40 : 75;
|
|
|
|
/* The friction coefficient increases with speed in a way that
|
|
|
|
* it doubles at 128 km/h, triples at 256 km/h and so on. */
|
|
|
|
return coeff * (128 + this->GetCurrentSpeed()) / 128;
|
2010-03-06 12:54:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allows to know the acceleration type of a vehicle.
|
|
|
|
* @return Zero, road vehicles always use a normal acceleration method.
|
|
|
|
*/
|
|
|
|
FORCEINLINE int GetAccelerationType() const
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the slope steepness used by this vehicle.
|
|
|
|
* @return Slope steepness used by the vehicle.
|
|
|
|
*/
|
|
|
|
FORCEINLINE uint32 GetSlopeSteepness() const
|
|
|
|
{
|
2010-11-07 13:35:07 +00:00
|
|
|
return _settings_game.vehicle.roadveh_slope_steepness;
|
2010-03-06 12:54:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the maximum speed allowed by the track for this vehicle.
|
|
|
|
* @return Since roads don't limit road vehicle speed, it returns always zero.
|
|
|
|
*/
|
|
|
|
FORCEINLINE uint16 GetMaxTrackSpeed() const
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the vehicle is at a tile that can be sloped.
|
|
|
|
* @return True if the tile can be sloped.
|
|
|
|
*/
|
|
|
|
FORCEINLINE bool TileMayHaveSlopedTrack() const
|
|
|
|
{
|
2010-03-06 12:55:56 +00:00
|
|
|
TrackStatus ts = GetTileTrackStatus(this->tile, TRANSPORT_ROAD, this->compatible_roadtypes);
|
|
|
|
TrackBits trackbits = TrackStatusToTrackBits(ts);
|
|
|
|
|
|
|
|
return trackbits == TRACK_BIT_X || trackbits == TRACK_BIT_Y;
|
2010-03-06 12:54:42 +00:00
|
|
|
}
|
2011-01-21 17:35:17 +00:00
|
|
|
|
|
|
|
/**
|
2011-11-04 10:18:13 +00:00
|
|
|
* Road vehicles have to use GetSlopePixelZ() to compute their height
|
2011-01-21 17:35:17 +00:00
|
|
|
* if they are reversing because in that case, their direction
|
|
|
|
* is not parallel with the road. It is safe to return \c true
|
|
|
|
* even if it is not reversing.
|
|
|
|
* @return are we (possibly) reversing?
|
|
|
|
*/
|
2011-11-04 10:18:13 +00:00
|
|
|
FORCEINLINE bool HasToUseGetSlopePixelZ()
|
2011-01-21 17:35:17 +00:00
|
|
|
{
|
|
|
|
const RoadVehicle *rv = this->First();
|
|
|
|
|
|
|
|
/* Check if this vehicle is in the same direction as the road under.
|
|
|
|
* We already know it has either GVF_GOINGUP_BIT or GVF_GOINGDOWN_BIT set. */
|
|
|
|
|
|
|
|
if (rv->state <= RVSB_TRACKDIR_MASK && IsReversingRoadTrackdir((Trackdir)rv->state)) {
|
|
|
|
/* If the first vehicle is reversing, this vehicle may be reversing too
|
|
|
|
* (especially if this is the first, and maybe the only, vehicle).*/
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (rv != this) {
|
|
|
|
/* If any previous vehicle has different direction,
|
|
|
|
* we may be in the middle of reversing. */
|
|
|
|
if (this->direction != rv->direction) return true;
|
|
|
|
rv = rv->Next();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2007-04-29 21:24:08 +00:00
|
|
|
};
|
|
|
|
|
2009-05-26 22:10:13 +00:00
|
|
|
#define FOR_ALL_ROADVEHICLES(var) FOR_ALL_VEHICLES_OF_TYPE(RoadVehicle, var)
|
|
|
|
|
2006-09-28 18:42:35 +00:00
|
|
|
#endif /* ROADVEH_H */
|