summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/pbs.cpp2
-rw-r--r--src/roadveh.h1
-rw-r--r--src/roadveh_cmd.cpp27
-rw-r--r--src/saveload/oldloader_sl.cpp4
-rw-r--r--src/saveload/vehicle_sl.cpp2
-rw-r--r--src/ship.h5
-rw-r--r--src/ship_cmd.cpp39
-rw-r--r--src/station_cmd.cpp16
-rw-r--r--src/train.h1
-rw-r--r--src/train_cmd.cpp41
-rw-r--r--src/tunnelbridge_cmd.cpp7
-rw-r--r--src/vehicle.cpp47
-rw-r--r--src/vehicle_base.h32
-rw-r--r--src/yapf/yapf_rail.cpp6
-rw-r--r--src/yapf/yapf_road.cpp4
-rw-r--r--src/yapf/yapf_ship.cpp2
16 files changed, 126 insertions, 110 deletions
diff --git a/src/pbs.cpp b/src/pbs.cpp
index a738da913..213d1c051 100644
--- a/src/pbs.cpp
+++ b/src/pbs.cpp
@@ -256,7 +256,7 @@ PBSTileInfo FollowTrainReservation(const Vehicle *v, bool *train_on_res)
assert(v->type == VEH_TRAIN);
TileIndex tile = v->tile;
- Trackdir trackdir = GetVehicleTrackdir(v);
+ Trackdir trackdir = v->GetVehicleTrackdir();
if (IsRailDepotTile(tile) && !GetRailDepotReservation(tile)) return PBSTileInfo(tile, trackdir, false);
diff --git a/src/roadveh.h b/src/roadveh.h
index 2cfee9043..2e42972ff 100644
--- a/src/roadveh.h
+++ b/src/roadveh.h
@@ -101,6 +101,7 @@ struct RoadVehicle : public Vehicle {
bool IsStoppedInDepot() const;
bool Tick();
void OnNewDay();
+ Trackdir GetVehicleTrackdir() const;
TileIndex GetOrderStationLocation(StationID station);
bool FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse);
};
diff --git a/src/roadveh_cmd.cpp b/src/roadveh_cmd.cpp
index a84f473bf..1a87bcb9c 100644
--- a/src/roadveh_cmd.cpp
+++ b/src/roadveh_cmd.cpp
@@ -364,7 +364,7 @@ static const Depot *FindClosestRoadDepot(const Vehicle *v)
case VPF_NPF: { // NPF
/* See where we are now */
- Trackdir trackdir = GetVehicleTrackdir(v);
+ Trackdir trackdir = v->GetVehicleTrackdir();
NPFFoundTargetData ftd = NPFRouteToDepotBreadthFirstTwoWay(v->tile, trackdir, false, v->tile, ReverseTrackdir(trackdir), false, TRANSPORT_ROAD, v->u.road.compatible_roadtypes, v->owner, INVALID_RAILTYPES, 0);
@@ -1174,7 +1174,7 @@ static uint RoadFindPathToStop(const Vehicle *v, TileIndex tile)
}
/* use NPF */
- Trackdir trackdir = GetVehicleTrackdir(v);
+ Trackdir trackdir = v->GetVehicleTrackdir();
assert(trackdir != INVALID_TRACKDIR);
NPFFindStationOrTileData fstd;
@@ -1950,6 +1950,29 @@ void RoadVehicle::OnNewDay()
InvalidateWindowClasses(WC_ROADVEH_LIST);
}
+Trackdir RoadVehicle::GetVehicleTrackdir() const
+{
+ if (this->vehstatus & VS_CRASHED) return INVALID_TRACKDIR;
+
+ if (this->IsInDepot()) {
+ /* We'll assume the road vehicle is facing outwards */
+ return DiagDirToDiagTrackdir(GetRoadDepotDirection(this->tile));
+ }
+
+ if (IsStandardRoadStopTile(this->tile)) {
+ /* We'll assume the road vehicle is facing outwards */
+ return DiagDirToDiagTrackdir(GetRoadStopDir(this->tile)); // Road vehicle in a station
+ }
+
+ /* Drive through road stops / wormholes (tunnels) */
+ if (this->u.road.state > RVSB_TRACKDIR_MASK) return DiagDirToDiagTrackdir(DirToDiagDir(this->direction));
+
+ /* If vehicle's state is a valid track direction (vehicle is not turning around) return it,
+ * otherwise transform it into a valid track direction */
+ return (Trackdir)((IsReversingRoadTrackdir((Trackdir)this->u.road.state)) ? (this->u.road.state - 6) : this->u.road.state);
+}
+
+
/** Refit a road vehicle to the specified cargo type
* @param tile unused
* @param flags operation to perform
diff --git a/src/saveload/oldloader_sl.cpp b/src/saveload/oldloader_sl.cpp
index 5e6202401..d692be42b 100644
--- a/src/saveload/oldloader_sl.cpp
+++ b/src/saveload/oldloader_sl.cpp
@@ -1103,7 +1103,7 @@ static const OldChunks vehicle_road_chunk[] = {
};
static const OldChunks vehicle_ship_chunk[] = {
- OCL_SVAR( OC_UINT8, VehicleShip, state ),
+ OCL_SVAR( OC_UINT8, Ship, state ),
OCL_NULL( 9 ), ///< Junk
@@ -1158,7 +1158,7 @@ static bool LoadOldVehicleUnion(LoadgameState *ls, int num)
default: NOT_REACHED();
case VEH_TRAIN : res = LoadChunk(ls, &v->u.rail, vehicle_train_chunk); break;
case VEH_ROAD : res = LoadChunk(ls, &v->u.road, vehicle_road_chunk); break;
- case VEH_SHIP : res = LoadChunk(ls, &v->u.ship, vehicle_ship_chunk); break;
+ case VEH_SHIP : res = LoadChunk(ls, v, vehicle_ship_chunk); break;
case VEH_AIRCRAFT: res = LoadChunk(ls, &v->u.air, vehicle_air_chunk); break;
case VEH_EFFECT : res = LoadChunk(ls, &v->u.effect, vehicle_effect_chunk); break;
case VEH_DISASTER: res = LoadChunk(ls, &v->u.disaster, vehicle_disaster_chunk); break;
diff --git a/src/saveload/vehicle_sl.cpp b/src/saveload/vehicle_sl.cpp
index a44bb6960..c749a27e3 100644
--- a/src/saveload/vehicle_sl.cpp
+++ b/src/saveload/vehicle_sl.cpp
@@ -560,7 +560,7 @@ const SaveLoad *GetVehicleDescription(VehicleType vt)
static const SaveLoad _ship_desc[] = {
SLE_WRITEBYTE(Vehicle, type, VEH_SHIP),
SLE_VEH_INCLUDEX(),
- SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleShip, state), SLE_UINT8),
+ SLE_VAR(Ship, state, SLE_UINT8),
/* reserve extra space in savegame here. (currently 16 bytes) */
SLE_CONDNULL(16, 2, SL_MAX_VERSION),
diff --git a/src/ship.h b/src/ship.h
index 95df31e54..f6306a8d4 100644
--- a/src/ship.h
+++ b/src/ship.h
@@ -23,6 +23,8 @@ void GetShipSpriteSize(EngineID engine, uint &width, uint &height);
* As side-effect the vehicle type is set correctly.
*/
struct Ship: public Vehicle {
+ TrackBitsByte state;
+
/** Initializes the Vehicle to a ship */
Ship() { this->type = VEH_SHIP; }
@@ -39,9 +41,10 @@ struct Ship: public Vehicle {
int GetDisplaySpeed() const { return this->cur_speed / 2; }
int GetDisplayMaxSpeed() const { return this->max_speed / 2; }
Money GetRunningCost() const;
- bool IsInDepot() const { return this->u.ship.state == TRACK_BIT_DEPOT; }
+ bool IsInDepot() const { return this->state == TRACK_BIT_DEPOT; }
bool Tick();
void OnNewDay();
+ Trackdir GetVehicleTrackdir() const;
TileIndex GetOrderStationLocation(StationID station);
bool FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse);
};
diff --git a/src/ship_cmd.cpp b/src/ship_cmd.cpp
index d39bcdcbe..fb0e2e07b 100644
--- a/src/ship_cmd.cpp
+++ b/src/ship_cmd.cpp
@@ -97,7 +97,7 @@ SpriteID Ship::GetImage(Direction direction) const
static const Depot *FindClosestShipDepot(const Vehicle *v)
{
if (_settings_game.pf.pathfinder_for_ships == VPF_NPF) { // NPF is used
- Trackdir trackdir = GetVehicleTrackdir(v);
+ Trackdir trackdir = v->GetVehicleTrackdir();
NPFFoundTargetData ftd = NPFRouteToDepotTrialError(v->tile, trackdir, false, TRANSPORT_WATER, 0, v->owner, INVALID_RAILTYPES);
if (ftd.best_bird_dist == 0) return GetDepotByTile(ftd.node.tile); // Found target
@@ -178,6 +178,23 @@ void Ship::OnNewDay()
InvalidateWindowClasses(WC_SHIPS_LIST);
}
+Trackdir Ship::GetVehicleTrackdir() const
+{
+ if (this->vehstatus & VS_CRASHED) return INVALID_TRACKDIR;
+
+ if (this->IsInDepot()) {
+ /* We'll assume the ship is facing outwards */
+ return DiagDirToDiagTrackdir(GetShipDepotDirection(this->tile));
+ }
+
+ if (this->state == TRACK_BIT_WORMHOLE) {
+ /* ship on aqueduct, so just use his direction and assume a diagonal track */
+ return DiagDirToDiagTrackdir(DirToDiagDir(this->direction));
+ }
+
+ return TrackDirectionToTrackdir(FindFirstTrack(this->state), this->direction);
+}
+
static void HandleBrokenShip(Vehicle *v)
{
if (v->breakdown_ctr != 1) {
@@ -276,7 +293,7 @@ static const TileIndexDiffC _ship_leave_depot_offs[] = {
{ 0, -1}
};
-static void CheckShipLeaveDepot(Vehicle *v)
+static void CheckShipLeaveDepot(Ship *v)
{
if (!v->IsInDepot()) return;
@@ -293,7 +310,7 @@ static void CheckShipLeaveDepot(Vehicle *v)
return;
}
- v->u.ship.state = AxisToTrackBits(axis);
+ v->state = AxisToTrackBits(axis);
v->vehstatus &= ~VS_HIDDEN;
v->cur_speed = 0;
@@ -447,7 +464,7 @@ static inline NPFFoundTargetData PerfNPFRouteToStationOrTile(TileIndex tile, Tra
/** returns the track to choose on the next tile, or -1 when it's better to
* reverse. The tile given is the tile we are about to enter, enterdir is the
* direction in which we are entering the tile */
-static Track ChooseShipTrack(Vehicle *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks)
+static Track ChooseShipTrack(Ship *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks)
{
assert(IsValidDiagDirection(enterdir));
@@ -459,7 +476,7 @@ static Track ChooseShipTrack(Vehicle *v, TileIndex tile, DiagDirection enterdir,
case VPF_NPF: { // NPF
NPFFindStationOrTileData fstd;
- Trackdir trackdir = GetVehicleTrackdir(v);
+ Trackdir trackdir = v->GetVehicleTrackdir();
assert(trackdir != INVALID_TRACKDIR); // Check that we are not in a depot
NPFFillWithOrderData(&fstd, v);
@@ -479,7 +496,7 @@ static Track ChooseShipTrack(Vehicle *v, TileIndex tile, DiagDirection enterdir,
Track track;
/* Let's find out how far it would be if we would reverse first */
- TrackBits b = GetTileShipTrackStatus(tile2) & _ship_sometracks[ReverseDiagDir(enterdir)] & v->u.ship.state;
+ TrackBits b = GetTileShipTrackStatus(tile2) & _ship_sometracks[ReverseDiagDir(enterdir)] & v->state;
uint distr = UINT_MAX; // distance if we reversed
if (b != 0) {
@@ -558,7 +575,7 @@ static const byte _ship_subcoord[4][6][3] = {
}
};
-static void ShipController(Vehicle *v)
+static void ShipController(Ship *v)
{
uint32 r;
const byte *b;
@@ -589,7 +606,7 @@ static void ShipController(Vehicle *v)
if (!ShipAccelerate(v)) return;
GetNewVehiclePosResult gp = GetNewVehiclePos(v);
- if (v->u.ship.state != TRACK_BIT_WORMHOLE) {
+ if (v->state != TRACK_BIT_WORMHOLE) {
/* Not on a bridge */
if (gp.old_tile == gp.new_tile) {
/* Staying in tile */
@@ -669,7 +686,7 @@ static void ShipController(Vehicle *v)
if (!HasBit(r, VETS_ENTERED_WORMHOLE)) {
v->tile = gp.new_tile;
- v->u.ship.state = TrackToTrackBits(track);
+ v->state = TrackToTrackBits(track);
}
v->direction = (Direction)b[2];
@@ -754,7 +771,7 @@ CommandCost CmdBuildShip(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32
const ShipVehicleInfo *svi = ShipVehInfo(p1);
- Vehicle *v = new Ship();
+ Ship *v = new Ship();
v->unitnumber = unit_num;
v->owner = _current_company;
@@ -786,7 +803,7 @@ CommandCost CmdBuildShip(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32
_new_vehicle_id = v->index;
v->name = NULL;
- v->u.ship.state = TRACK_BIT_DEPOT;
+ v->state = TRACK_BIT_DEPOT;
v->service_interval = _settings_game.vehicle.servint_ships;
v->date_of_last_service = _date;
diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp
index fe2422c32..14718a45d 100644
--- a/src/station_cmd.cpp
+++ b/src/station_cmd.cpp
@@ -1027,9 +1027,9 @@ CommandCost CmdBuildRailroadStation(TileIndex tile_org, DoCommandFlag flags, uin
if (v != NULL) {
FreeTrainTrackReservation(v);
*affected_vehicles.Append() = v;
- if (IsRailwayStationTile(v->tile)) SetRailwayStationPlatformReservation(v->tile, TrackdirToExitdir(GetVehicleTrackdir(v)), false);
+ if (IsRailwayStationTile(v->tile)) SetRailwayStationPlatformReservation(v->tile, TrackdirToExitdir(v->GetVehicleTrackdir()), false);
for (; v->Next() != NULL; v = v->Next()) ;
- if (IsRailwayStationTile(v->tile)) SetRailwayStationPlatformReservation(v->tile, TrackdirToExitdir(ReverseTrackdir(GetVehicleTrackdir(v))), false);
+ if (IsRailwayStationTile(v->tile)) SetRailwayStationPlatformReservation(v->tile, TrackdirToExitdir(ReverseTrackdir(v->GetVehicleTrackdir())), false);
}
}
@@ -1064,10 +1064,10 @@ CommandCost CmdBuildRailroadStation(TileIndex tile_org, DoCommandFlag flags, uin
for (uint i = 0; i < affected_vehicles.Length(); ++i) {
/* Restore reservations of trains. */
Vehicle *v = affected_vehicles[i];
- if (IsRailwayStationTile(v->tile)) SetRailwayStationPlatformReservation(v->tile, TrackdirToExitdir(GetVehicleTrackdir(v)), true);
+ if (IsRailwayStationTile(v->tile)) SetRailwayStationPlatformReservation(v->tile, TrackdirToExitdir(v->GetVehicleTrackdir()), true);
TryPathReserve(v, true, true);
for (; v->Next() != NULL; v = v->Next()) ;
- if (IsRailwayStationTile(v->tile)) SetRailwayStationPlatformReservation(v->tile, TrackdirToExitdir(ReverseTrackdir(GetVehicleTrackdir(v))), true);
+ if (IsRailwayStationTile(v->tile)) SetRailwayStationPlatformReservation(v->tile, TrackdirToExitdir(ReverseTrackdir(v->GetVehicleTrackdir())), true);
}
st->MarkTilesDirty(false);
@@ -1203,10 +1203,10 @@ CommandCost CmdRemoveFromRailroadStation(TileIndex tile, DoCommandFlag flags, ui
if (v != NULL) {
/* Free train reservation. */
FreeTrainTrackReservation(v);
- if (IsRailwayStationTile(v->tile)) SetRailwayStationPlatformReservation(v->tile, TrackdirToExitdir(GetVehicleTrackdir(v)), false);
+ if (IsRailwayStationTile(v->tile)) SetRailwayStationPlatformReservation(v->tile, TrackdirToExitdir(v->GetVehicleTrackdir()), false);
Vehicle *temp = v;
for (; temp->Next() != NULL; temp = temp->Next()) ;
- if (IsRailwayStationTile(temp->tile)) SetRailwayStationPlatformReservation(temp->tile, TrackdirToExitdir(ReverseTrackdir(GetVehicleTrackdir(temp))), false);
+ if (IsRailwayStationTile(temp->tile)) SetRailwayStationPlatformReservation(temp->tile, TrackdirToExitdir(ReverseTrackdir(temp->GetVehicleTrackdir())), false);
}
}
@@ -1226,10 +1226,10 @@ CommandCost CmdRemoveFromRailroadStation(TileIndex tile, DoCommandFlag flags, ui
if (v != NULL) {
/* Restore station reservation. */
- if (IsRailwayStationTile(v->tile)) SetRailwayStationPlatformReservation(v->tile, TrackdirToExitdir(GetVehicleTrackdir(v)), true);
+ if (IsRailwayStationTile(v->tile)) SetRailwayStationPlatformReservation(v->tile, TrackdirToExitdir(v->GetVehicleTrackdir()), true);
TryPathReserve(v, true, true);
for (; v->Next() != NULL; v = v->Next()) ;
- if (IsRailwayStationTile(v->tile)) SetRailwayStationPlatformReservation(v->tile, TrackdirToExitdir(ReverseTrackdir(GetVehicleTrackdir(v))), true);
+ if (IsRailwayStationTile(v->tile)) SetRailwayStationPlatformReservation(v->tile, TrackdirToExitdir(ReverseTrackdir(v->GetVehicleTrackdir())), true);
}
/* if we deleted the whole station, delete the train facility. */
diff --git a/src/train.h b/src/train.h
index 4816a251b..4fa431d90 100644
--- a/src/train.h
+++ b/src/train.h
@@ -331,6 +331,7 @@ struct Train : public Vehicle {
bool IsStoppedInDepot() const { return CheckTrainStoppedInDepot(this) >= 0; }
bool Tick();
void OnNewDay();
+ Trackdir GetVehicleTrackdir() const;
TileIndex GetOrderStationLocation(StationID station);
bool FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse);
};
diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp
index a2e731c65..61c2e547d 100644
--- a/src/train_cmd.cpp
+++ b/src/train_cmd.cpp
@@ -1920,10 +1920,10 @@ static void ReverseTrainDirection(Vehicle *v)
/* If we are currently on a tile with conventional signals, we can't treat the
* current tile as a safe tile or we would enter a PBS block without a reservation. */
bool first_tile_okay = !(IsTileType(v->tile, MP_RAILWAY) &&
- HasSignalOnTrackdir(v->tile, GetVehicleTrackdir(v)) &&
+ HasSignalOnTrackdir(v->tile, v->GetVehicleTrackdir()) &&
!IsPbsSignal(GetSignalType(v->tile, FindFirstTrack(v->u.rail.track))));
- if (IsRailwayStationTile(v->tile)) SetRailwayStationPlatformReservation(v->tile, TrackdirToExitdir(GetVehicleTrackdir(v)), true);
+ if (IsRailwayStationTile(v->tile)) SetRailwayStationPlatformReservation(v->tile, TrackdirToExitdir(v->GetVehicleTrackdir()), true);
if (TryPathReserve(v, false, first_tile_okay)) {
/* Do a look-ahead now in case our current tile was already a safe tile. */
CheckNextTrainTile(v);
@@ -2169,8 +2169,8 @@ static TrainFindDepotData FindClosestTrainDepot(Vehicle *v, int max_distance)
case VPF_NPF: { // NPF
const Vehicle *last = GetLastVehicleInChain(v);
- Trackdir trackdir = GetVehicleTrackdir(v);
- Trackdir trackdir_rev = ReverseTrackdir(GetVehicleTrackdir(last));
+ Trackdir trackdir = v->GetVehicleTrackdir();
+ Trackdir trackdir_rev = ReverseTrackdir(last->GetVehicleTrackdir());
assert(trackdir != INVALID_TRACKDIR);
NPFFoundTargetData ftd = NPFRouteToDepotBreadthFirstTwoWay(v->tile, trackdir, false, last->tile, trackdir_rev, false, TRANSPORT_RAIL, 0, v->owner, v->u.rail.compatible_railtypes, NPF_INFINITE_PENALTY);
@@ -2353,7 +2353,7 @@ static void CheckNextTrainTile(Vehicle *v)
/* Exit if the current order doesn't have a destination, but the train has orders. */
if ((v->current_order.IsType(OT_NOTHING) || v->current_order.IsType(OT_LEAVESTATION) || v->current_order.IsType(OT_LOADING)) && v->GetNumOrders() > 0) return;
- Trackdir td = GetVehicleTrackdir(v);
+ Trackdir td = v->GetVehicleTrackdir();
/* On a tile with a red non-pbs signal, don't look ahead. */
if (IsTileType(v->tile, MP_RAILWAY) && HasSignalOnTrackdir(v->tile, td) &&
@@ -2493,7 +2493,7 @@ void FreeTrainTrackReservation(const Vehicle *v, TileIndex origin, Trackdir orig
assert(IsFrontEngine(v));
TileIndex tile = origin != INVALID_TILE ? origin : v->tile;
- Trackdir td = orig_td != INVALID_TRACKDIR ? orig_td : GetVehicleTrackdir(v);
+ Trackdir td = orig_td != INVALID_TRACKDIR ? orig_td : v->GetVehicleTrackdir();
bool free_tile = tile != v->tile || !(IsRailwayStationTile(v->tile) || IsTileType(v->tile, MP_TUNNELBRIDGE));
StationID station_id = IsRailwayStationTile(v->tile) ? GetStationIndex(v->tile) : INVALID_STATION;
@@ -3013,7 +3013,7 @@ static Track ChooseTrainTrack(Vehicle *v, TileIndex tile, DiagDirection enterdir
if (TryReserveSafeTrack(v, origin.tile, origin.trackdir, false)) {
TrackBits res = GetReservedTrackbits(tile) & DiagdirReachesTracks(enterdir);
best_track = FindFirstTrack(res);
- TryReserveRailTrack(v->tile, TrackdirToTrack(GetVehicleTrackdir(v)));
+ TryReserveRailTrack(v->tile, TrackdirToTrack(v->GetVehicleTrackdir()));
if (got_reservation != NULL) *got_reservation = true;
if (changed_signal) MarkTileDirtyByTile(tile);
} else {
@@ -3060,7 +3060,7 @@ static Track ChooseTrainTrack(Vehicle *v, TileIndex tile, DiagDirection enterdir
break;
}
- TryReserveRailTrack(v->tile, TrackdirToTrack(GetVehicleTrackdir(v)));
+ TryReserveRailTrack(v->tile, TrackdirToTrack(v->GetVehicleTrackdir()));
if (changed_signal) MarkTileDirtyByTile(tile);
@@ -3179,8 +3179,8 @@ static bool CheckReverseTrain(Vehicle *v)
NPFFillWithOrderData(&fstd, v);
- Trackdir trackdir = GetVehicleTrackdir(v);
- Trackdir trackdir_rev = ReverseTrackdir(GetVehicleTrackdir(last));
+ Trackdir trackdir = v->GetVehicleTrackdir();
+ Trackdir trackdir_rev = ReverseTrackdir(last->GetVehicleTrackdir());
assert(trackdir != INVALID_TRACKDIR);
assert(trackdir_rev != INVALID_TRACKDIR);
@@ -3471,7 +3471,7 @@ static void SetVehicleCrashed(Vehicle *v)
* and any railway station paltform reservation. */
FreeTrainTrackReservation(v);
for (const Vehicle *u = v; u != NULL; u = u->Next()) {
- ClearPathReservation(u, u->tile, GetVehicleTrackdir(u));
+ ClearPathReservation(u, u->tile, u->GetVehicleTrackdir());
if (IsTileType(u->tile, MP_TUNNELBRIDGE)) {
/* ClearPathReservation will not free the wormhole exit
* if the train has just entered the wormhole. */
@@ -3796,7 +3796,7 @@ static void TrainController(Vehicle *v, Vehicle *nomove)
}
/* Clear any track reservation when the last vehicle leaves the tile */
- if (v->Next() == NULL) ClearPathReservation(v, v->tile, GetVehicleTrackdir(v));
+ if (v->Next() == NULL) ClearPathReservation(v, v->tile, v->GetVehicleTrackdir());
v->tile = gp.new_tile;
@@ -4520,6 +4520,23 @@ void Train::OnNewDay()
}
}
+Trackdir Train::GetVehicleTrackdir() const
+{
+ if (this->vehstatus & VS_CRASHED) return INVALID_TRACKDIR;
+
+ if (this->u.rail.track == TRACK_BIT_DEPOT) {
+ /* We'll assume the train is facing outwards */
+ return DiagDirToDiagTrackdir(GetRailDepotDirection(this->tile)); // Train in depot
+ }
+
+ if (this->u.rail.track == TRACK_BIT_WORMHOLE) {
+ /* train in tunnel or on bridge, so just use his direction and assume a diagonal track */
+ return DiagDirToDiagTrackdir(DirToDiagDir(this->direction));
+ }
+
+ return TrackDirectionToTrackdir(FindFirstTrack(this->u.rail.track), this->direction);
+}
+
void InitializeTrains()
{
_age_cargo_skip_counter = 1;
diff --git a/src/tunnelbridge_cmd.cpp b/src/tunnelbridge_cmd.cpp
index 489e5a8ed..26f5b6635 100644
--- a/src/tunnelbridge_cmd.cpp
+++ b/src/tunnelbridge_cmd.cpp
@@ -15,6 +15,7 @@
#include "town.h"
#include "variables.h"
#include "train.h"
+#include "ship.h"
#include "water_map.h"
#include "yapf/yapf.h"
#include "newgrf_sound.h"
@@ -1470,7 +1471,7 @@ static VehicleEnterTileStatus VehicleEnter_TunnelBridge(Vehicle *v, TileIndex ti
break;
case VEH_SHIP:
- v->u.ship.state = TRACK_BIT_WORMHOLE;
+ static_cast<Ship*>(v)->state = TRACK_BIT_WORMHOLE;
break;
default: NOT_REACHED();
@@ -1495,8 +1496,8 @@ static VehicleEnterTileStatus VehicleEnter_TunnelBridge(Vehicle *v, TileIndex ti
break;
case VEH_SHIP:
- if (v->u.ship.state == TRACK_BIT_WORMHOLE) {
- v->u.ship.state = (DiagDirToAxis(dir) == AXIS_X ? TRACK_BIT_X : TRACK_BIT_Y);
+ if (static_cast<Ship*>(v)->state == TRACK_BIT_WORMHOLE) {
+ static_cast<Ship*>(v)->state = (DiagDirToAxis(dir) == AXIS_X ? TRACK_BIT_X : TRACK_BIT_Y);
return VETSB_ENTERED_WORMHOLE;
}
break;
diff --git a/src/vehicle.cpp b/src/vehicle.cpp
index 961e9ee4b..90a9406d5 100644
--- a/src/vehicle.cpp
+++ b/src/vehicle.cpp
@@ -992,7 +992,7 @@ void VehicleEnterDepot(Vehicle *v)
case VEH_SHIP:
InvalidateWindowClasses(WC_SHIPS_LIST);
- v->u.ship.state = TRACK_BIT_DEPOT;
+ static_cast<Ship*>(v)->state = TRACK_BIT_DEPOT;
RecalcShipStuff(v);
break;
@@ -1173,49 +1173,6 @@ Direction GetDirectionTowards(const Vehicle *v, int x, int y)
return ChangeDir(dir, dirdiff > DIRDIFF_REVERSE ? DIRDIFF_45LEFT : DIRDIFF_45RIGHT);
}
-Trackdir GetVehicleTrackdir(const Vehicle *v)
-{
- if (v->vehstatus & VS_CRASHED) return INVALID_TRACKDIR;
-
- switch (v->type) {
- case VEH_TRAIN:
- if (v->u.rail.track == TRACK_BIT_DEPOT) // We'll assume the train is facing outwards
- return DiagDirToDiagTrackdir(GetRailDepotDirection(v->tile)); // Train in depot
-
- if (v->u.rail.track == TRACK_BIT_WORMHOLE) // train in tunnel or on bridge, so just use his direction and assume a diagonal track
- return DiagDirToDiagTrackdir(DirToDiagDir(v->direction));
-
- return TrackDirectionToTrackdir(FindFirstTrack(v->u.rail.track), v->direction);
-
- case VEH_SHIP:
- if (v->IsInDepot())
- /* We'll assume the ship is facing outwards */
- return DiagDirToDiagTrackdir(GetShipDepotDirection(v->tile));
-
- if (v->u.ship.state == TRACK_BIT_WORMHOLE) // ship on aqueduct, so just use his direction and assume a diagonal track
- return DiagDirToDiagTrackdir(DirToDiagDir(v->direction));
-
- return TrackDirectionToTrackdir(FindFirstTrack(v->u.ship.state), v->direction);
-
- case VEH_ROAD:
- if (v->IsInDepot()) // We'll assume the road vehicle is facing outwards
- return DiagDirToDiagTrackdir(GetRoadDepotDirection(v->tile));
-
- if (IsStandardRoadStopTile(v->tile)) // We'll assume the road vehicle is facing outwards
- return DiagDirToDiagTrackdir(GetRoadStopDir(v->tile)); // Road vehicle in a station
-
- /* Drive through road stops / wormholes (tunnels) */
- if (v->u.road.state > RVSB_TRACKDIR_MASK) return DiagDirToDiagTrackdir(DirToDiagDir(v->direction));
-
- /* If vehicle's state is a valid track direction (vehicle is not turning around) return it,
- * otherwise transform it into a valid track direction */
- return (Trackdir)((IsReversingRoadTrackdir((Trackdir)v->u.road.state)) ? (v->u.road.state - 6) : v->u.road.state);
-
- /* case VEH_AIRCRAFT: case VEH_EFFECT: case VEH_DISASTER: */
- default: return INVALID_TRACKDIR;
- }
-}
-
/**
* Call the tile callback function for a vehicle entering a tile
* @param v Vehicle entering the tile
@@ -1529,7 +1486,7 @@ void Vehicle::LeaveStation()
/* Try to reserve a path when leaving the station as we
* might not be marked as wanting a reservation, e.g.
* when an overlength train gets turned around in a station. */
- if (UpdateSignalsOnSegment(this->tile, TrackdirToExitdir(GetVehicleTrackdir(this)), this->owner) == SIGSEG_PBS || _settings_game.pf.reserve_paths) {
+ if (UpdateSignalsOnSegment(this->tile, TrackdirToExitdir(this->GetVehicleTrackdir()), this->owner) == SIGSEG_PBS || _settings_game.pf.reserve_paths) {
TryPathReserve(this, true, true);
}
}
diff --git a/src/vehicle_base.h b/src/vehicle_base.h
index 6c3d86757..91b5b69b8 100644
--- a/src/vehicle_base.h
+++ b/src/vehicle_base.h
@@ -185,10 +185,6 @@ struct VehicleDisaster {
VehicleID big_ufo_destroyer_target;
};
-struct VehicleShip {
- TrackBitsByte state;
-};
-
typedef Pool<Vehicle, VehicleID, 512, 64000> VehiclePool;
extern VehiclePool _vehicle_pool;
@@ -322,7 +318,6 @@ public:
VehicleRoad road;
VehicleEffect effect;
VehicleDisaster disaster;
- VehicleShip ship;
} u;
/* cached oftenly queried NewGRF values */
@@ -438,6 +433,20 @@ public:
virtual void OnNewDay() {};
/**
+ * Returns the Trackdir on which the vehicle is currently located.
+ * Works for trains and ships.
+ * Currently works only sortof for road vehicles, since they have a fuzzy
+ * concept of being "on" a trackdir. Dunno really what it returns for a road
+ * vehicle that is halfway a tile, never really understood that part. For road
+ * vehicles that are at the beginning or end of the tile, should just return
+ * the diagonal trackdir on which they are driving. I _think_.
+ * For other vehicles types, or vehicles with no clear trackdir (such as those
+ * in depots), returns 0xFF.
+ * @return the trackdir of the vehicle
+ */
+ virtual Trackdir GetVehicleTrackdir() const { return INVALID_TRACKDIR; }
+
+ /**
* Gets the running cost of a vehicle that can be sent into SetDParam for string processing.
* @return the vehicle's running cost
*/
@@ -663,19 +672,6 @@ static inline Order *GetVehicleOrder(const Vehicle *v, int index) { return (v->o
*/
static inline Order *GetLastVehicleOrder(const Vehicle *v) { return (v->orders.list == NULL) ? NULL : v->orders.list->GetLastOrder(); }
-/**
- * Returns the Trackdir on which the vehicle is currently located.
- * Works for trains and ships.
- * Currently works only sortof for road vehicles, since they have a fuzzy
- * concept of being "on" a trackdir. Dunno really what it returns for a road
- * vehicle that is halfway a tile, never really understood that part. For road
- * vehicles that are at the beginning or end of the tile, should just return
- * the diagonal trackdir on which they are driving. I _think_.
- * For other vehicles types, or vehicles with no clear trackdir (such as those
- * in depots), returns 0xFF.
- */
-Trackdir GetVehicleTrackdir(const Vehicle *v);
-
void CheckVehicle32Day(Vehicle *v);
static const int32 INVALID_COORD = 0x7fffffff;
diff --git a/src/yapf/yapf_rail.cpp b/src/yapf/yapf_rail.cpp
index 09f672605..8d530bf52 100644
--- a/src/yapf/yapf_rail.cpp
+++ b/src/yapf/yapf_rail.cpp
@@ -540,8 +540,8 @@ bool YapfCheckReverseTrain(const Vehicle *v)
const Vehicle *last_veh = GetLastVehicleInChain(v);
/* get trackdirs of both ends */
- Trackdir td = GetVehicleTrackdir(v);
- Trackdir td_rev = ReverseTrackdir(GetVehicleTrackdir(last_veh));
+ Trackdir td = v->GetVehicleTrackdir();
+ Trackdir td_rev = ReverseTrackdir(last_veh->GetVehicleTrackdir());
/* tiles where front and back are */
TileIndex tile = v->tile;
@@ -603,7 +603,7 @@ bool YapfFindNearestRailDepotTwoWay(const Vehicle *v, int max_distance, int reve
PBSTileInfo origin = FollowTrainReservation(v);
TileIndex last_tile = last_veh->tile;
- Trackdir td_rev = ReverseTrackdir(GetVehicleTrackdir(last_veh));
+ Trackdir td_rev = ReverseTrackdir(last_veh->GetVehicleTrackdir());
typedef bool (*PfnFindNearestDepotTwoWay)(const Vehicle*, TileIndex, Trackdir, TileIndex, Trackdir, int, int, TileIndex*, bool*);
PfnFindNearestDepotTwoWay pfnFindNearestDepotTwoWay = &CYapfAnyDepotRail1::stFindNearestDepotTwoWay;
diff --git a/src/yapf/yapf_road.cpp b/src/yapf/yapf_road.cpp
index fbb7c6fa3..facfec14a 100644
--- a/src/yapf/yapf_road.cpp
+++ b/src/yapf/yapf_road.cpp
@@ -372,7 +372,7 @@ public:
{
/* set origin (tile, trackdir) */
TileIndex src_tile = v->tile;
- Trackdir src_td = GetVehicleTrackdir(v);
+ Trackdir src_td = v->GetVehicleTrackdir();
if ((TrackStatusToTrackdirBits(GetTileTrackStatus(src_tile, TRANSPORT_ROAD, v->u.road.compatible_roadtypes)) & TrackdirToTrackdirBits(src_td)) == 0) {
/* sometimes the roadveh is not on the road (it resides on non-existing track)
* how should we handle that situation? */
@@ -469,7 +469,7 @@ uint YapfRoadVehDistanceToTile(const Vehicle *v, TileIndex tile)
Depot *YapfFindNearestRoadDepot(const Vehicle *v)
{
TileIndex tile = v->tile;
- Trackdir trackdir = GetVehicleTrackdir(v);
+ Trackdir trackdir = v->GetVehicleTrackdir();
if ((TrackStatusToTrackdirBits(GetTileTrackStatus(tile, TRANSPORT_ROAD, v->u.road.compatible_roadtypes)) & TrackdirToTrackdirBits(trackdir)) == 0) {
return NULL;
}
diff --git a/src/yapf/yapf_ship.cpp b/src/yapf/yapf_ship.cpp
index 36b0b0076..0d3975833 100644
--- a/src/yapf/yapf_ship.cpp
+++ b/src/yapf/yapf_ship.cpp
@@ -54,7 +54,7 @@ public:
/* move back to the old tile/trackdir (where ship is coming from) */
TileIndex src_tile = TILE_ADD(tile, TileOffsByDiagDir(ReverseDiagDir(enterdir)));
- Trackdir trackdir = GetVehicleTrackdir(v);
+ Trackdir trackdir = v->GetVehicleTrackdir();
assert(IsValidTrackdir(trackdir));
/* convert origin trackdir to TrackdirBits */