1
0
Fork 0

(svn r12605) -Cleanup: variable scope and coding style in train*

release/0.7
smatz 2008-04-07 12:36:50 +00:00
parent f46ae97d8e
commit ac389e16d2
3 changed files with 93 additions and 94 deletions

View File

@ -10,12 +10,10 @@
#include "vehicle_base.h" #include "vehicle_base.h"
/* /** enum to handle train subtypes
* enum to handle train subtypes
* Do not access it directly unless you have to. Use the access functions below * Do not access it directly unless you have to. Use the access functions below
* This is an enum to tell what bit to access as it is a bitmask * This is an enum to tell what bit to access as it is a bitmask
*/ */
enum TrainSubtype { enum TrainSubtype {
TS_FRONT = 0, ///< Leading engine of a train TS_FRONT = 0, ///< Leading engine of a train
TS_ARTICULATED_PART = 1, ///< Articulated part of an engine TS_ARTICULATED_PART = 1, ///< Articulated part of an engine

View File

@ -182,7 +182,7 @@ static void TrainCargoChanged(Vehicle* v)
*/ */
void TrainConsistChanged(Vehicle *v) void TrainConsistChanged(Vehicle *v)
{ {
uint16 max_speed = 0xFFFF; uint16 max_speed = UINT16_MAX;
assert(v->type == VEH_TRAIN); assert(v->type == VEH_TRAIN);
assert(IsFrontEngine(v) || IsFreeWagon(v)); assert(IsFrontEngine(v) || IsFreeWagon(v));
@ -305,7 +305,7 @@ enum AccelType {
/** new acceleration*/ /** new acceleration*/
static int GetTrainAcceleration(Vehicle *v, bool mode) static int GetTrainAcceleration(Vehicle *v, bool mode)
{ {
static const int absolute_max_speed = 2000; static const int absolute_max_speed = UINT16_MAX;
int max_speed = absolute_max_speed; int max_speed = absolute_max_speed;
int speed = v->cur_speed * 10 / 16; // km-ish/h -> mp/h int speed = v->cur_speed * 10 / 16; // km-ish/h -> mp/h
int curvecount[2] = {0, 0}; int curvecount[2] = {0, 0};
@ -571,7 +571,7 @@ static CommandCost CmdBuildRailWagon(EngineID engine, TileIndex tile, uint32 fla
v->u.rail.track = TRACK_BIT_DEPOT; v->u.rail.track = TRACK_BIT_DEPOT;
v->vehstatus = VS_HIDDEN | VS_DEFPAL; v->vehstatus = VS_HIDDEN | VS_DEFPAL;
v->subtype = 0; // v->subtype = 0;
SetTrainWagon(v); SetTrainWagon(v);
if (u != NULL) { if (u != NULL) {
@ -582,7 +582,7 @@ static CommandCost CmdBuildRailWagon(EngineID engine, TileIndex tile, uint32 fla
} }
v->cargo_type = rvi->cargo_type; v->cargo_type = rvi->cargo_type;
v->cargo_subtype = 0; // v->cargo_subtype = 0;
v->cargo_cap = rvi->capacity; v->cargo_cap = rvi->capacity;
v->value = value.GetCost(); v->value = value.GetCost();
// v->day_counter = 0; // v->day_counter = 0;
@ -646,7 +646,7 @@ static void AddRearEngineToMultiheadedTrain(Vehicle* v, Vehicle* u, bool buildin
u->z_pos = v->z_pos; u->z_pos = v->z_pos;
u->u.rail.track = TRACK_BIT_DEPOT; u->u.rail.track = TRACK_BIT_DEPOT;
u->vehstatus = v->vehstatus & ~VS_STOPPED; u->vehstatus = v->vehstatus & ~VS_STOPPED;
u->subtype = 0; // u->subtype = 0;
SetMultiheaded(u); SetMultiheaded(u);
u->spritenum = v->spritenum + 1; u->spritenum = v->spritenum + 1;
u->cargo_type = v->cargo_type; u->cargo_type = v->cargo_type;
@ -701,8 +701,9 @@ CommandCost CmdBuildRailVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32
Vehicle **vl = (Vehicle**)alloca(sizeof(*vl) * (num_vehicles + 1)); Vehicle **vl = (Vehicle**)alloca(sizeof(*vl) * (num_vehicles + 1));
memset(vl, 0, sizeof(*vl) * (num_vehicles + 1)); memset(vl, 0, sizeof(*vl) * (num_vehicles + 1));
if (!Vehicle::AllocateList(vl, num_vehicles)) if (!Vehicle::AllocateList(vl, num_vehicles)) {
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME); return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
}
Vehicle *v = vl[0]; Vehicle *v = vl[0];
@ -723,17 +724,17 @@ CommandCost CmdBuildRailVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32
v->x_pos = x; v->x_pos = x;
v->y_pos = y; v->y_pos = y;
v->z_pos = GetSlopeZ(x, y); v->z_pos = GetSlopeZ(x, y);
v->running_ticks = 0; // v->running_ticks = 0;
v->u.rail.track = TRACK_BIT_DEPOT; v->u.rail.track = TRACK_BIT_DEPOT;
v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL; v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
v->spritenum = rvi->image_index; v->spritenum = rvi->image_index;
v->cargo_type = rvi->cargo_type; v->cargo_type = rvi->cargo_type;
v->cargo_subtype = 0; // v->cargo_subtype = 0;
v->cargo_cap = rvi->capacity; v->cargo_cap = rvi->capacity;
v->max_speed = rvi->max_speed; v->max_speed = rvi->max_speed;
v->value = value.GetCost(); v->value = value.GetCost();
v->last_station_visited = INVALID_STATION; v->last_station_visited = INVALID_STATION;
v->dest_tile = 0; // v->dest_tile = 0;
v->engine_type = p1; v->engine_type = p1;
@ -752,12 +753,12 @@ CommandCost CmdBuildRailVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32
v->cur_image = 0xAC2; v->cur_image = 0xAC2;
v->random_bits = VehicleRandomBits(); v->random_bits = VehicleRandomBits();
v->vehicle_flags = 0; // v->vehicle_flags = 0;
if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) SetBit(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE); if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) SetBit(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE);
v->group_id = DEFAULT_GROUP; v->group_id = DEFAULT_GROUP;
v->subtype = 0; // v->subtype = 0;
SetFrontEngine(v); SetFrontEngine(v);
SetTrainEngine(v); SetTrainEngine(v);
@ -786,8 +787,9 @@ CommandCost CmdBuildRailVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile); InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
RebuildVehicleLists(); RebuildVehicleLists();
InvalidateWindow(WC_COMPANY, v->owner); InvalidateWindow(WC_COMPANY, v->owner);
if (IsLocalPlayer()) if (IsLocalPlayer()) {
InvalidateAutoreplaceWindow(v->engine_type, v->group_id); // updates the replace Train window InvalidateAutoreplaceWindow(v->engine_type, v->group_id); // updates the replace Train window
}
GetPlayer(_current_player)->num_engines[p1]++; GetPlayer(_current_player)->num_engines[p1]++;
} }
@ -1170,12 +1172,12 @@ CommandCost CmdMoveRailVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p
assert(src->orders == NULL); assert(src->orders == NULL);
src->num_orders = 0; src->num_orders = 0;
// Decrease the engines number of the src engine_type /* Decrease the engines number of the src engine_type */
if (!IsDefaultGroupID(src->group_id) && IsValidGroupID(src->group_id)) { if (!IsDefaultGroupID(src->group_id) && IsValidGroupID(src->group_id)) {
GetGroup(src->group_id)->num_engines[src->engine_type]--; GetGroup(src->group_id)->num_engines[src->engine_type]--;
} }
// If we move an engine to a new line affect it to the DEFAULT_GROUP /* If we move an engine to a new line affect it to the DEFAULT_GROUP */
src->group_id = DEFAULT_GROUP; src->group_id = DEFAULT_GROUP;
} }
} else { } else {
@ -1425,10 +1427,10 @@ CommandCost CmdSellRailWagon(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
* Totally braindead cause building a new engine adds all loco-less * Totally braindead cause building a new engine adds all loco-less
* engines to its train anyways */ * engines to its train anyways */
if (p2 == 2 && HasBit(ori_subtype, TS_FRONT)) { if (p2 == 2 && HasBit(ori_subtype, TS_FRONT)) {
Vehicle *tmp; for (v = first; v != NULL;) {
for (v = first; v != NULL; v = tmp) { Vehicle *tmp = GetNextVehicle(v);
tmp = GetNextVehicle(v);
DoCommand(v->tile, v->index | INVALID_VEHICLE << 16, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); DoCommand(v->tile, v->index | INVALID_VEHICLE << 16, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
v = tmp;
} }
} }
} }
@ -1437,8 +1439,7 @@ CommandCost CmdSellRailWagon(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
/* Start deleting every vehicle after the selected one /* Start deleting every vehicle after the selected one
* If we encounter a matching rear-engine to a front-engine * If we encounter a matching rear-engine to a front-engine
* earlier in the chain (before deletion), leave it alone */ * earlier in the chain (before deletion), leave it alone */
Vehicle *tmp; for (Vehicle *tmp; v != NULL; v = tmp) {
for (; v != NULL; v = tmp) {
tmp = GetNextVehicle(v); tmp = GetNextVehicle(v);
if (IsMultiheaded(v)) { if (IsMultiheaded(v)) {
@ -1526,10 +1527,11 @@ static inline void SetLastSpeed(Vehicle* v, int spd)
int old = v->u.rail.last_speed; int old = v->u.rail.last_speed;
if (spd != old) { if (spd != old) {
v->u.rail.last_speed = spd; v->u.rail.last_speed = spd;
if (_patches.vehicle_speed || (old == 0) != (spd == 0)) if (_patches.vehicle_speed || (old == 0) != (spd == 0)) {
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH); InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
} }
} }
}
static void SwapTrainFlags(byte *swap_flag1, byte *swap_flag2) static void SwapTrainFlags(byte *swap_flag1, byte *swap_flag2)
{ {
@ -1793,8 +1795,7 @@ static void ReverseTrainDirection(Vehicle *v)
TileIndex crossing = TrainApproachingCrossingTile(v); TileIndex crossing = TrainApproachingCrossingTile(v);
/* count number of vehicles */ /* count number of vehicles */
int r = 0; ///< number of vehicles - 1 int r = CountVehiclesInChain(v) - 1; // number of vehicles - 1
for (const Vehicle *u = v; (u = u->Next()) != NULL;) { r++; }
AdvanceWagonsBeforeSwap(v); AdvanceWagonsBeforeSwap(v);
@ -1843,7 +1844,7 @@ CommandCost CmdReverseTrainDirection(TileIndex tile, uint32 flags, uint32 p1, ui
if (v->type != VEH_TRAIN || !CheckOwnership(v->owner)) return CMD_ERROR; if (v->type != VEH_TRAIN || !CheckOwnership(v->owner)) return CMD_ERROR;
if (p2) { if (p2 != 0) {
/* turn a single unit around */ /* turn a single unit around */
if (IsMultiheaded(v) || HasBit(EngInfo(v->engine_type)->callbackmask, CBM_VEHICLE_ARTIC_ENGINE)) { if (IsMultiheaded(v) || HasBit(EngInfo(v->engine_type)->callbackmask, CBM_VEHICLE_ARTIC_ENGINE)) {
@ -2048,7 +2049,7 @@ static TrainFindDepotData FindClosestTrainDepot(Vehicle *v, int max_distance)
} break; } break;
case VPF_NPF: { /* NPF */ case VPF_NPF: { /* NPF */
Vehicle* last = GetLastVehicleInChain(v); const Vehicle *last = GetLastVehicleInChain(v);
Trackdir trackdir = GetVehicleTrackdir(v); Trackdir trackdir = GetVehicleTrackdir(v);
Trackdir trackdir_rev = ReverseTrackdir(GetVehicleTrackdir(last)); Trackdir trackdir_rev = ReverseTrackdir(GetVehicleTrackdir(last));
@ -2168,8 +2169,9 @@ static void HandleLocomotiveSmokeCloud(const Vehicle* v)
{ {
bool sound = false; bool sound = false;
if (v->vehstatus & VS_TRAIN_SLOWING || v->load_unload_time_rem != 0 || v->cur_speed < 2) if (v->vehstatus & VS_TRAIN_SLOWING || v->load_unload_time_rem != 0 || v->cur_speed < 2) {
return; return;
}
const Vehicle *u = v; const Vehicle *u = v;
@ -2231,6 +2233,9 @@ static void HandleLocomotiveSmokeCloud(const Vehicle* v)
sound = true; sound = true;
} }
break; break;
default:
break;
} }
} while ((v = v->Next()) != NULL); } while ((v = v->Next()) != NULL);
@ -2301,10 +2306,10 @@ static bool CheckTrainStayInDepot(Vehicle *v)
return false; return false;
} }
/* Check for station tiles */ /** Check for station tiles */
struct TrainTrackFollowerData { struct TrainTrackFollowerData {
TileIndex dest_coords; TileIndex dest_coords;
StationID station_index; // station index we're heading for StationID station_index; ///< station index we're heading for
uint best_bird_dist; uint best_bird_dist;
uint best_track_dist; uint best_track_dist;
TrackdirByte best_track; TrackdirByte best_track;
@ -2489,8 +2494,9 @@ static bool CheckReverseTrain(Vehicle *v)
{ {
if (_opt.diff.line_reverse_mode != 0 || if (_opt.diff.line_reverse_mode != 0 ||
v->u.rail.track == TRACK_BIT_DEPOT || v->u.rail.track == TRACK_BIT_WORMHOLE || v->u.rail.track == TRACK_BIT_DEPOT || v->u.rail.track == TRACK_BIT_WORMHOLE ||
!(v->direction & 1)) !(v->direction & 1)) {
return false; return false;
}
TrainTrackFollowerData fd; TrainTrackFollowerData fd;
FillWithStationData(&fd, v); FillWithStationData(&fd, v);
@ -2500,9 +2506,9 @@ static bool CheckReverseTrain(Vehicle *v)
assert(v->u.rail.track); assert(v->u.rail.track);
switch (_patches.pathfinder_for_trains) { switch (_patches.pathfinder_for_trains) {
case VPF_YAPF: { /* YAPF */ case VPF_YAPF: /* YAPF */
reverse_best = YapfCheckReverseTrain(v); reverse_best = YapfCheckReverseTrain(v);
} break; break;
case VPF_NPF: { /* NPF */ case VPF_NPF: { /* NPF */
NPFFindStationOrTileData fstd; NPFFindStationOrTileData fstd;
@ -2829,8 +2835,8 @@ static uint CountPassengersInTrain(const Vehicle* v)
} }
struct TrainCollideChecker { struct TrainCollideChecker {
Vehicle *v; Vehicle *v; ///< vehicle we are testing for collision
uint num; uint num; ///< number of dead if train collided
}; };
static void *FindTrainCollideEnum(Vehicle *v, void *data) static void *FindTrainCollideEnum(Vehicle *v, void *data)
@ -3192,8 +3198,8 @@ static void ChangeTrainDirRandomly(Vehicle *v)
v->UpdateDeltaXY(v->direction); v->UpdateDeltaXY(v->direction);
v->cur_image = v->GetImage(v->direction); v->cur_image = v->GetImage(v->direction);
/* Refrain from updating the z position of the vehicle when on /* Refrain from updating the z position of the vehicle when on
a bridge, because AfterSetTrainPos will put the vehicle under * a bridge, because AfterSetTrainPos will put the vehicle under
the bridge in that case */ * the bridge in that case */
if (v->u.rail.track != TRACK_BIT_WORMHOLE) AfterSetTrainPos(v, false); if (v->u.rail.track != TRACK_BIT_WORMHOLE) AfterSetTrainPos(v, false);
} }
} while ((v = v->Next()) != NULL); } while ((v = v->Next()) != NULL);
@ -3533,10 +3539,10 @@ void Train::Tick()
} }
} }
#define MAX_ACCEPTABLE_DEPOT_DIST 16
static void CheckIfTrainNeedsService(Vehicle *v) static void CheckIfTrainNeedsService(Vehicle *v)
{ {
static const uint MAX_ACCEPTABLE_DEPOT_DIST = 16;
if (_patches.servint_trains == 0 || !VehicleNeedsService(v)) return; if (_patches.servint_trains == 0 || !VehicleNeedsService(v)) return;
if (v->IsInDepot()) { if (v->IsInDepot()) {
VehicleServiceInDepot(v); VehicleServiceInDepot(v);

View File

@ -24,12 +24,11 @@
void CcBuildWagon(bool success, TileIndex tile, uint32 p1, uint32 p2) void CcBuildWagon(bool success, TileIndex tile, uint32 p1, uint32 p2)
{ {
Vehicle *v, *found;
if (!success) return; if (!success) return;
/* find a locomotive in the depot. */ /* find a locomotive in the depot. */
found = NULL; Vehicle *found = NULL;
Vehicle *v;
FOR_ALL_VEHICLES(v) { FOR_ALL_VEHICLES(v) {
if (v->type == VEH_TRAIN && IsFrontEngine(v) && if (v->type == VEH_TRAIN && IsFrontEngine(v) &&
v->tile == tile && v->tile == tile &&
@ -50,11 +49,9 @@ void CcBuildWagon(bool success, TileIndex tile, uint32 p1, uint32 p2)
void CcBuildLoco(bool success, TileIndex tile, uint32 p1, uint32 p2) void CcBuildLoco(bool success, TileIndex tile, uint32 p1, uint32 p2)
{ {
const Vehicle *v;
if (!success) return; if (!success) return;
v = GetVehicle(_new_vehicle_id); const Vehicle *v = GetVehicle(_new_vehicle_id);
if (tile == _backup_orders_tile) { if (tile == _backup_orders_tile) {
_backup_orders_tile = 0; _backup_orders_tile = 0;
RestoreVehicleOrders(v); RestoreVehicleOrders(v);
@ -197,8 +194,6 @@ void DrawTrainDetails(const Vehicle *v, int x, int y, int vscroll_pos, uint16 vs
for (;;) { for (;;) {
if (--vscroll_pos < 0 && vscroll_pos >= -vscroll_cap) { if (--vscroll_pos < 0 && vscroll_pos >= -vscroll_cap) {
int dx = 0; int dx = 0;
int px;
int py;
u = v; u = v;
do { do {
@ -208,8 +203,8 @@ void DrawTrainDetails(const Vehicle *v, int x, int y, int vscroll_pos, uint16 vs
u = u->Next(); u = u->Next();
} while (u != NULL && IsArticulatedPart(u) && u->cargo_cap == 0); } while (u != NULL && IsArticulatedPart(u) && u->cargo_cap == 0);
px = x + WagonLengthToPixels(dx) + 2; int px = x + WagonLengthToPixels(dx) + 2;
py = y + 2; int py = y + 2;
switch (det_tab) { switch (det_tab) {
default: NOT_REACHED(); default: NOT_REACHED();
case 0: TrainDetailsCargoTab( v, px, py); break; case 0: TrainDetailsCargoTab( v, px, py); break;