1
0
Fork 0

(svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.

release/0.6
rubidium 2007-02-13 11:29:20 +00:00
parent 4ec7eb2201
commit d1d1d170f3
2 changed files with 31 additions and 31 deletions

View File

@ -693,12 +693,12 @@ static void ShipController(Vehicle *v)
BeginVehicleMove(v); BeginVehicleMove(v);
if (GetNewVehiclePos(v, &gp)) { if (GetNewVehiclePos(v, &gp)) {
// staying in tile /* Staying in tile */
if (IsShipInDepot(v)) { if (IsShipInDepot(v)) {
gp.x = v->x_pos; gp.x = v->x_pos;
gp.y = v->y_pos; gp.y = v->y_pos;
} else { } else {
/* isnot inside depot */ /* Not inside depot */
r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y); r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
if (HASBIT(r, VETS_CANNOT_ENTER)) goto reverse_direction; if (HASBIT(r, VETS_CANNOT_ENTER)) goto reverse_direction;
@ -757,21 +757,20 @@ static void ShipController(Vehicle *v)
} }
} else { } else {
DiagDirection diagdir; DiagDirection diagdir;
// new tile /* New tile */
if (TileX(gp.new_tile) >= MapMaxX() || TileY(gp.new_tile) >= MapMaxY()) if (TileX(gp.new_tile) >= MapMaxX() || TileY(gp.new_tile) >= MapMaxY()) {
goto reverse_direction; goto reverse_direction;
}
dir = ShipGetNewDirectionFromTiles(gp.new_tile, gp.old_tile); dir = ShipGetNewDirectionFromTiles(gp.new_tile, gp.old_tile);
assert(dir == DIR_NE || dir == DIR_SE || dir == DIR_SW || dir == DIR_NW); assert(dir == DIR_NE || dir == DIR_SE || dir == DIR_SW || dir == DIR_NW);
diagdir = DirToDiagDir(dir); diagdir = DirToDiagDir(dir);
tracks = GetAvailShipTracks(gp.new_tile, diagdir); tracks = GetAvailShipTracks(gp.new_tile, diagdir);
if (tracks == 0) if (tracks == TRACK_BIT_NONE) goto reverse_direction;
goto reverse_direction;
// Choose a direction, and continue if we find one /* Choose a direction, and continue if we find one */
track = ChooseShipTrack(v, gp.new_tile, diagdir, tracks); track = ChooseShipTrack(v, gp.new_tile, diagdir, tracks);
if (track == INVALID_TRACK) if (track == INVALID_TRACK) goto reverse_direction;
goto reverse_direction;
b = _ship_subcoord[diagdir][track]; b = _ship_subcoord[diagdir][track];

View File

@ -3013,11 +3013,11 @@ static void TrainController(Vehicle *v, bool update_image)
if (GetNewVehiclePos(v, &gp)) { if (GetNewVehiclePos(v, &gp)) {
/* Staying in the old tile */ /* Staying in the old tile */
if (v->u.rail.track == TRACK_BIT_DEPOT) { if (v->u.rail.track == TRACK_BIT_DEPOT) {
/* inside depot */ /* Inside depot */
gp.x = v->x_pos; gp.x = v->x_pos;
gp.y = v->y_pos; gp.y = v->y_pos;
} else { } else {
/* is not inside depot */ /* Not inside depot */
if (IsFrontEngine(v) && !TrainCheckIfLineEnds(v)) return; if (IsFrontEngine(v) && !TrainCheckIfLineEnds(v)) return;
@ -3043,7 +3043,7 @@ static void TrainController(Vehicle *v, bool update_image)
/* Determine what direction we're entering the new tile from */ /* Determine what direction we're entering the new tile from */
dir = GetNewVehicleDirectionByTile(gp.new_tile, gp.old_tile); dir = GetNewVehicleDirectionByTile(gp.new_tile, gp.old_tile);
enterdir = DirToDiagDir(dir); enterdir = DirToDiagDir(dir);
assert(enterdir==0 || enterdir==1 || enterdir==2 || enterdir==3); assert(IsValidDiagDirection(enterdir));
/* Get the status of the tracks in the new tile and mask /* Get the status of the tracks in the new tile and mask
* away the bits that aren't reachable. */ * away the bits that aren't reachable. */
@ -3060,17 +3060,11 @@ static void TrainController(Vehicle *v, bool update_image)
bits &= ~TrackCrossesTracks(FindFirstTrack(v->u.rail.track)); bits &= ~TrackCrossesTracks(FindFirstTrack(v->u.rail.track));
} }
if (bits == TRACK_BIT_NONE) { if (bits == TRACK_BIT_NONE) goto invalid_rail;
//debug("%x == 0", bits);
goto invalid_rail;
}
/* Check if the new tile contrains tracks that are compatible /* Check if the new tile contrains tracks that are compatible
* with the current train, if not, bail out. */ * with the current train, if not, bail out. */
if (!CheckCompatibleRail(v, gp.new_tile)) { if (!CheckCompatibleRail(v, gp.new_tile)) goto invalid_rail;
//debug("!CheckCompatibleRail(%p, %x)", v, gp.new_tile);
goto invalid_rail;
}
if (prev == NULL) { if (prev == NULL) {
/* Currently the locomotive is active. Determine which one of the /* Currently the locomotive is active. Determine which one of the
@ -3081,14 +3075,22 @@ static void TrainController(Vehicle *v, bool update_image)
/* Check if it's a red signal and that force proceed is not clicked. */ /* Check if it's a red signal and that force proceed is not clicked. */
if ((tracks >> 16) & chosen_track && v->u.rail.force_proceed == 0) goto red_light; if ((tracks >> 16) & chosen_track && v->u.rail.force_proceed == 0) goto red_light;
} else { } else {
static byte _matching_tracks[8] = {0x30, 1, 0xC, 2, 0x30, 1, 0xC, 2}; static const TrackBits _matching_tracks[8] = {
TRACK_BIT_LEFT | TRACK_BIT_RIGHT, TRACK_BIT_X,
TRACK_BIT_UPPER | TRACK_BIT_LOWER, TRACK_BIT_Y,
TRACK_BIT_LEFT | TRACK_BIT_RIGHT, TRACK_BIT_X,
TRACK_BIT_UPPER | TRACK_BIT_LOWER, TRACK_BIT_Y
};
/* The wagon is active, simply follow the prev vehicle. */ /* The wagon is active, simply follow the prev vehicle. */
chosen_track = (TrackBits)(byte)(_matching_tracks[GetDirectionToVehicle(prev, gp.x, gp.y)] & bits); chosen_track = (TrackBits)(byte)(_matching_tracks[GetDirectionToVehicle(prev, gp.x, gp.y)] & bits);
} }
/* make sure chosen track is a valid track */ /* Make sure chosen track is a valid track */
assert(chosen_track==1 || chosen_track==2 || chosen_track==4 || chosen_track==8 || chosen_track==16 || chosen_track==32); assert(
chosen_track == TRACK_BIT_X || chosen_track == TRACK_BIT_Y ||
chosen_track == TRACK_BIT_UPPER || chosen_track == TRACK_BIT_LOWER ||
chosen_track == TRACK_BIT_LEFT || chosen_track == TRACK_BIT_RIGHT);
/* Update XY to reflect the entrance to the new tile, and select the direction to use */ /* Update XY to reflect the entrance to the new tile, and select the direction to use */
{ {
@ -3126,15 +3128,14 @@ static void TrainController(Vehicle *v, bool update_image)
/* Signals can only change when the first /* Signals can only change when the first
* (above) or the last vehicle moves. */ * (above) or the last vehicle moves. */
if (v->next == NULL) if (v->next == NULL) TrainMovedChangeSignals(gp.old_tile, ReverseDiagDir(enterdir));
TrainMovedChangeSignals(gp.old_tile, ReverseDiagDir(enterdir));
if (prev == NULL) AffectSpeedByDirChange(v, chosen_dir); if (prev == NULL) AffectSpeedByDirChange(v, chosen_dir);
v->direction = chosen_dir; v->direction = chosen_dir;
} }
} else { } else {
/* in tunnel on on a bridge */ /* In tunnel or on a bridge */
GetNewVehiclePos(v, &gp); GetNewVehiclePos(v, &gp);
SetSpeedLimitOnBridge(v); SetSpeedLimitOnBridge(v);