summaryrefslogtreecommitdiff
path: root/src/train_cmd.cpp
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2008-09-07 11:23:10 +0000
committerrubidium <rubidium@openttd.org>2008-09-07 11:23:10 +0000
commit9474db5cb6c4b9db2f7b68d1dda1a523305282f1 (patch)
tree94706b006a8848169482c99bfbb9ca6f9c734954 /src/train_cmd.cpp
parentf4ee4fd5aed4861fe65757d2c61ce9510c4a6216 (diff)
downloadopenttd-9474db5cb6c4b9db2f7b68d1dda1a523305282f1.tar.xz
(svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
-Fix: desync in PBS reservation following, vehicle flooding and road vehicle overtake/follow code.
Diffstat (limited to 'src/train_cmd.cpp')
-rw-r--r--src/train_cmd.cpp30
1 files changed, 14 insertions, 16 deletions
diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp
index 42e1d02ff..328c39e88 100644
--- a/src/train_cmd.cpp
+++ b/src/train_cmd.cpp
@@ -1694,24 +1694,22 @@ static Vehicle *TrainApproachingCrossingEnum(Vehicle *v, void *data)
/**
* Finds a vehicle approaching rail-road crossing
* @param tile tile to test
- * @return pointer to vehicle approaching the crossing
+ * @return true if a vehicle is approaching the crossing
* @pre tile is a rail-road crossing
*/
-static Vehicle *TrainApproachingCrossing(TileIndex tile)
+static bool TrainApproachingCrossing(TileIndex tile)
{
assert(IsLevelCrossingTile(tile));
DiagDirection dir = AxisToDiagDir(GetCrossingRailAxis(tile));
TileIndex tile_from = tile + TileOffsByDiagDir(dir);
- Vehicle *v = VehicleFromPos(tile_from, &tile, &TrainApproachingCrossingEnum);
-
- if (v != NULL) return v;
+ if (HasVehicleOnPos(tile_from, &tile, &TrainApproachingCrossingEnum)) return true;
dir = ReverseDiagDir(dir);
tile_from = tile + TileOffsByDiagDir(dir);
- return VehicleFromPos(tile_from, &tile, &TrainApproachingCrossingEnum);
+ return HasVehicleOnPos(tile_from, &tile, &TrainApproachingCrossingEnum);
}
@@ -1726,7 +1724,7 @@ void UpdateLevelCrossing(TileIndex tile, bool sound)
assert(IsLevelCrossingTile(tile));
/* train on crossing || train approaching crossing || reserved */
- bool new_state = VehicleFromPos(tile, NULL, &TrainOnTileEnum) != NULL || TrainApproachingCrossing(tile) || GetCrossingReservation(tile);
+ bool new_state = HasVehicleOnPos(tile, NULL, &TrainOnTileEnum) || TrainApproachingCrossing(tile) || GetCrossingReservation(tile);
if (new_state != IsCrossingBarred(tile)) {
if (new_state && sound) {
@@ -3028,7 +3026,7 @@ bool TryPathReserve(Vehicle *v, bool mark_as_stuck, bool first_tile_okay)
}
}
- Vehicle *other_train = NULL;
+ bool other_train = false;
PBSTileInfo origin = FollowTrainReservation(v, &other_train);
/* If we have a reserved path and the path ends at a safe tile, we are finished already. */
if (origin.okay && (v->tile != origin.tile || first_tile_okay)) {
@@ -3041,7 +3039,7 @@ bool TryPathReserve(Vehicle *v, bool mark_as_stuck, bool first_tile_okay)
* This can only happen when tracks and signals are changed. A crash
* is probably imminent, don't do any further reservation because
* it might cause stale reservations. */
- if (other_train != NULL && v->tile != origin.tile) {
+ if (other_train && v->tile != origin.tile) {
if (mark_as_stuck) MarkTrainAsStuck(v);
return false;
}
@@ -3518,10 +3516,10 @@ static void CheckTrainCollision(Vehicle *v)
/* find colliding vehicles */
if (v->u.rail.track == TRACK_BIT_WORMHOLE) {
- VehicleFromPos(v->tile, &tcc, FindTrainCollideEnum);
- VehicleFromPos(GetOtherTunnelBridgeEnd(v->tile), &tcc, FindTrainCollideEnum);
+ FindVehicleOnPos(v->tile, &tcc, FindTrainCollideEnum);
+ FindVehicleOnPos(GetOtherTunnelBridgeEnd(v->tile), &tcc, FindTrainCollideEnum);
} else {
- VehicleFromPosXY(v->x_pos, v->y_pos, &tcc, FindTrainCollideEnum);
+ FindVehicleOnPosXY(v->x_pos, v->y_pos, &tcc, FindTrainCollideEnum);
}
/* any dead -> no crash */
@@ -3652,7 +3650,7 @@ static void TrainController(Vehicle *v, Vehicle *nomove, bool update_image)
exitdir = ReverseDiagDir(exitdir);
/* check if a train is waiting on the other side */
- if (VehicleFromPos(o_tile, &exitdir, &CheckVehicleAtSignal) == NULL) return;
+ if (!HasVehicleOnPos(o_tile, &exitdir, &CheckVehicleAtSignal)) return;
}
}
@@ -3819,9 +3817,9 @@ reverse_train_direction:
}
/** Collect trackbits of all crashed train vehicles on a tile
- * @param v Vehicle passed from VehicleFromPos()
+ * @param v Vehicle passed from Find/HasVehicleOnPos()
* @param data trackdirbits for the result
- * @return NULL to not abort VehicleFromPos()
+ * @return NULL to iterate over all vehicles on the tile.
*/
static Vehicle *CollectTrackbitsFromCrashedVehiclesEnum(Vehicle *v, void *data)
{
@@ -3894,7 +3892,7 @@ static void DeleteLastWagon(Vehicle *v)
/* If there are still crashed vehicles on the tile, give the track reservation to them */
TrackBits remaining_trackbits = TRACK_BIT_NONE;
- VehicleFromPos(tile, &remaining_trackbits, CollectTrackbitsFromCrashedVehiclesEnum);
+ FindVehicleOnPos(tile, &remaining_trackbits, CollectTrackbitsFromCrashedVehiclesEnum);
/* It is important that these two are the first in the loop, as reservation cannot deal with every trackbit combination */
assert(TRACK_BEGIN == TRACK_X && TRACK_Y == TRACK_BEGIN + 1);