1
0
Fork 0

(svn r1976) Cleanups - mostly indentation and fiddling with loops

release/0.4.5
tron 2005-03-09 19:09:04 +00:00
parent 750a265531
commit e6e3e69da4
3 changed files with 130 additions and 135 deletions

View File

@ -343,10 +343,10 @@ static bool GenerateStationName(Station *st, uint tile, int flag)
/* check mine? */ /* check mine? */
if (HASBIT(free_names, M(STR_SV_STNAME_MINES))) { if (HASBIT(free_names, M(STR_SV_STNAME_MINES))) {
if (CountMapSquareAround(tile, MP_INDUSTRY, 0, 6) >= 2 || if (CountMapSquareAround(tile, MP_INDUSTRY, 0, 6) >= 2 ||
CountMapSquareAround(tile, MP_INDUSTRY, 0x64, 0x73) >= 2 || CountMapSquareAround(tile, MP_INDUSTRY, 0x64, 0x73) >= 2 ||
CountMapSquareAround(tile, MP_INDUSTRY, 0x2F, 0x33) >= 2 || CountMapSquareAround(tile, MP_INDUSTRY, 0x2F, 0x33) >= 2 ||
CountMapSquareAround(tile, MP_INDUSTRY, 0x48, 0x58) >= 2 || CountMapSquareAround(tile, MP_INDUSTRY, 0x48, 0x58) >= 2 ||
CountMapSquareAround(tile, MP_INDUSTRY, 0x5B, 0x63) >= 2) { CountMapSquareAround(tile, MP_INDUSTRY, 0x5B, 0x63) >= 2) {
found = M(STR_SV_STNAME_MINES); found = M(STR_SV_STNAME_MINES);
goto done; goto done;
} }
@ -362,18 +362,20 @@ static bool GenerateStationName(Station *st, uint tile, int flag)
} }
/* Check lakeside */ /* Check lakeside */
if (HASBIT(free_names, M(STR_SV_STNAME_LAKESIDE)) if (HASBIT(free_names, M(STR_SV_STNAME_LAKESIDE)) &&
&& DistanceFromEdge(tile) < 20 DistanceFromEdge(tile) < 20 &&
&& CountMapSquareAround(tile, MP_WATER, 0, 0) >= 5) { CountMapSquareAround(tile, MP_WATER, 0, 0) >= 5) {
found = M(STR_SV_STNAME_LAKESIDE); found = M(STR_SV_STNAME_LAKESIDE);
goto done; goto done;
} }
/* Check woods */ /* Check woods */
if (HASBIT(free_names, M(STR_SV_STNAME_WOODS)) && if (HASBIT(free_names, M(STR_SV_STNAME_WOODS)) && (
(CountMapSquareAround(tile, MP_TREES, 0, 255) >= 8 || CountMapSquareAround(tile, MP_TREES, 0, 255) >= 8 ||
CountMapSquareAround(tile, MP_INDUSTRY, 0x10, 0x11) >= 2)) { CountMapSquareAround(tile, MP_INDUSTRY, 0x10, 0x11) >= 2)
found = (_opt.landscape==LT_DESERT) ? M(STR_SV_STNAME_FOREST) : M(STR_SV_STNAME_WOODS); ) {
found = _opt.landscape == LT_DESERT ?
M(STR_SV_STNAME_FOREST) : M(STR_SV_STNAME_WOODS);
goto done; goto done;
} }
@ -889,11 +891,12 @@ static inline byte *CreateMulti(byte *layout, int n, byte b)
// stolen from TTDPatch // stolen from TTDPatch
static void GetStationLayout(byte *layout, int numtracks, int plat_len, struct StationSpec *spec) static void GetStationLayout(byte *layout, int numtracks, int plat_len, struct StationSpec *spec)
{ {
if (spec != NULL && spec->lengths >= plat_len && spec->platforms[plat_len - 1] >= numtracks if (spec != NULL && spec->lengths >= plat_len &&
&& spec->layouts[plat_len - 1][numtracks - 1]) { spec->platforms[plat_len - 1] >= numtracks &&
spec->layouts[plat_len - 1][numtracks - 1]) {
/* Custom layout defined, follow it. */ /* Custom layout defined, follow it. */
memcpy(layout, spec->layouts[plat_len - 1][numtracks - 1], memcpy(layout, spec->layouts[plat_len - 1][numtracks - 1],
plat_len * numtracks); plat_len * numtracks);
return; return;
} }
@ -1181,8 +1184,8 @@ void SetCustomStation(byte local_stid, struct StationSpec *spec)
int i; int i;
for (i = 0; i <= _statspec_highest_id[sclass]; i++) { for (i = 0; i <= _statspec_highest_id[sclass]; i++) {
if (_station_spec[sclass][i].grfid == spec->grfid if (_station_spec[sclass][i].grfid == spec->grfid &&
&& _station_spec[sclass][i].localidx == local_stid + 1) { _station_spec[sclass][i].localidx == local_stid + 1) {
stid = i; stid = i;
/* FIXME: Release original SpriteGroup to /* FIXME: Release original SpriteGroup to
* prevent leaks. But first we need to * prevent leaks. But first we need to
@ -1251,8 +1254,7 @@ ResolveStationSpriteGroup(struct SpriteGroup *spritegroup, struct Station *stat)
/* TODO: Town structure. */ /* TODO: Town structure. */
} else /* VSG_SELF */ { } else /* VSG_SELF */ {
if (dsg->variable == 0x40 if (dsg->variable == 0x40 || dsg->variable == 0x41) {
|| dsg->variable == 0x41) {
/* FIXME: This is ad hoc only /* FIXME: This is ad hoc only
* for waypoints. */ * for waypoints. */
value = 0x01010000; value = 0x01010000;
@ -1313,7 +1315,7 @@ uint32 GetCustomStationRelocation(struct StationSpec *spec, struct Station *stat
} }
error("Custom station 0x%08x::0x%02x has no sprites associated.", error("Custom station 0x%08x::0x%02x has no sprites associated.",
spec->grfid, spec->localidx); spec->grfid, spec->localidx);
/* This is what gets subscribed of dtss->image in newgrf.c, /* This is what gets subscribed of dtss->image in newgrf.c,
* so it's probably kinda "default offset". Try to use it as * so it's probably kinda "default offset". Try to use it as
* emergency measure. */ * emergency measure. */
@ -3104,8 +3106,7 @@ static void Load_STNS(void)
} }
} }
/* This is to ensure all pointers are within the limits of /* This is to ensure all pointers are within the limits of _stations_size */
_stations_size */
if (_station_tick_ctr > GetStationPoolSize()) if (_station_tick_ctr > GetStationPoolSize())
_station_tick_ctr = 0; _station_tick_ctr = 0;
} }

View File

@ -64,13 +64,15 @@ static bool TrainShouldStop(Vehicle *v, TileIndex tile)
assert(IsTileType(v->tile, MP_STATION)); assert(IsTileType(v->tile, MP_STATION));
//When does a train drive through a station //When does a train drive through a station
//first we deal with the "new nonstop handling" //first we deal with the "new nonstop handling"
if ( _patches.new_nonstop && o->flags & OF_NON_STOP && _map2[tile] == o->station ) if (_patches.new_nonstop && o->flags & OF_NON_STOP &&
_map2[tile] == o->station )
return false; return false;
if (v->last_station_visited == _map2[tile]) if (v->last_station_visited == _map2[tile])
return false; return false;
if ( _map2[tile] != o->station && (o->flags & OF_NON_STOP || _patches.new_nonstop)) if (_map2[tile] != o->station &&
(o->flags & OF_NON_STOP || _patches.new_nonstop))
return false; return false;
return true; return true;
@ -122,24 +124,22 @@ static int GetTrainAcceleration(Vehicle *v, bool mode)
} }
//if we have a 90 degree turn, fix the speed limit to 60 //if we have a 90 degree turn, fix the speed limit to 60
if ( _curve_neighbours90[dir][0] == ndir || _curve_neighbours90[dir][1] == ndir) { if (_curve_neighbours90[dir][0] == ndir ||
_curve_neighbours90[dir][1] == ndir) {
max_speed = 61; max_speed = 61;
} }
} }
for(i = 0; i < numcurve; i++) { for (i = 0; i < numcurve; i++) sum += dist[i];
sum += dist[i];
}
free(dist); free(dist);
dist = NULL; dist = NULL;
if (numcurve > 0) { if (numcurve > 0) sum /= numcurve;
sum /= numcurve;
}
if ((curvecount[0] != 0 || curvecount[1] != 0) && (max_speed > 88)) { if ((curvecount[0] != 0 || curvecount[1] != 0) && max_speed > 88) {
int total = curvecount[0] + curvecount[1]; int total = curvecount[0] + curvecount[1];
if (curvecount[0] == 1 && curvecount[1] == 1) { if (curvecount[0] == 1 && curvecount[1] == 1) {
max_speed = 0xFFFF; max_speed = 0xFFFF;
} else if (total > 1) { } else if (total > 1) {
@ -221,53 +221,55 @@ static int GetTrainAcceleration(Vehicle *v, bool mode)
switch (v->u.rail.railtype) { switch (v->u.rail.railtype) {
case 0: case 0:
case 1: case 1:
{
force = power / speed; //[N] force = power / speed; //[N]
force *= 22; force *= 22;
force /= 10; force /= 10;
} break; break;
case 2: case 2:
force = power / 25; force = power / 25;
break; break;
} }
} else } else {
//"kickoff" acceleration //"kickoff" acceleration
force = resistance * 10; force = resistance * 10;
}
if (force <= 0) force = 10000; if (force <= 0) force = 10000;
if (v->u.rail.railtype != 2) if (v->u.rail.railtype != 2) force = min(force, mass * 10 * 200);
force = min(force, mass * 10 * 200);
if (mode == AM_ACCEL) { if (mode == AM_ACCEL) {
return (force - resistance) / (mass * 4); return (force - resistance) / (mass * 4);
} else { } else {
return min((-force - resistance) /(mass * 4), (10000 / (mass * 4))); return min((-force - resistance) / (mass * 4), 10000 / (mass * 4));
} }
} }
void UpdateTrainAcceleration(Vehicle *v) void UpdateTrainAcceleration(Vehicle *v)
{ {
uint acc, power=0, max_speed=5000, weight=0; uint power = 0;
uint max_speed = 5000;
uint weight = 0;
Vehicle *u = v; Vehicle *u = v;
assert(v->subtype == TS_Front_Engine); assert(v->subtype == TS_Front_Engine);
// compute stuff like max speed, power, and weight. // compute stuff like max speed, power, and weight.
do { for (; u != NULL; u = u->next) {
const RailVehicleInfo *rvi = RailVehInfo(u->engine_type); const RailVehicleInfo *rvi = RailVehInfo(u->engine_type);
// power is sum of the power for all engines // power is sum of the power for all engines
power += rvi->power; power += rvi->power;
// limit the max speed to the speed of the slowest vehicle. // limit the max speed to the speed of the slowest vehicle.
if (rvi->max_speed && rvi->max_speed <= max_speed) max_speed = rvi->max_speed; if (rvi->max_speed != 0 && rvi->max_speed <= max_speed)
max_speed = rvi->max_speed;
// weight is the sum of the weight of the wagon and the weight of the cargo. // weight is the sum of the weight of the wagon and the weight of the cargo.
weight += rvi->weight; weight += rvi->weight;
weight += (_cargoc.weights[u->cargo_type] * u->cargo_count) >> 4; weight += (_cargoc.weights[u->cargo_type] * u->cargo_count) / 16;
}
} while ( (u=u->next) != NULL);
// these are shown in the UI // these are shown in the UI
v->u.rail.cached_weight = weight; v->u.rail.cached_weight = weight;
@ -276,13 +278,7 @@ void UpdateTrainAcceleration(Vehicle *v)
assert(weight != 0); assert(weight != 0);
// compute acceleration v->acceleration = clamp(power / weight * 4, 1, 255);
acc = power / weight * 4;
if (acc >= 255) acc=255;
if (acc == 0) acc++;
v->acceleration = (byte)acc;
} }
int GetTrainImage(Vehicle *v, byte direction) int GetTrainImage(Vehicle *v, byte direction)
@ -292,7 +288,7 @@ int GetTrainImage(Vehicle *v, byte direction)
if (is_custom_sprite(img)) { if (is_custom_sprite(img)) {
base = GetCustomVehicleSprite(v, direction + 4 * is_custom_secondhead_sprite(img)); base = GetCustomVehicleSprite(v, direction + 4 * is_custom_secondhead_sprite(img));
if (base) return base; if (base != 0) return base;
img = _engine_original_sprites[v->engine_type]; img = _engine_original_sprites[v->engine_type];
} }
@ -312,22 +308,24 @@ void DrawTrainEngine(int x, int y, int engine, uint32 image_ormod)
if (is_custom_sprite(img)) { if (is_custom_sprite(img)) {
image = GetCustomVehicleIcon(engine, 6); image = GetCustomVehicleIcon(engine, 6);
if (!image) img = _engine_original_sprites[engine]; if (image == 0) img = _engine_original_sprites[engine];
} }
if (!image) { if (image == 0) {
image = (6 & _engine_sprite_and[img]) + _engine_sprite_base[img]; image = (6 & _engine_sprite_and[img]) + _engine_sprite_base[img];
} }
if (rvi->flags & RVI_MULTIHEAD) { if (rvi->flags & RVI_MULTIHEAD) {
DrawSprite(image | image_ormod, x-14, y); DrawSprite(image | image_ormod, x - 14, y);
x += 15; x += 15;
image = 0; image = 0;
if (is_custom_sprite(img)) { if (is_custom_sprite(img)) {
image = GetCustomVehicleIcon(engine, 2); image = GetCustomVehicleIcon(engine, 2);
if (!image) img = _engine_original_sprites[engine]; if (image == 0) img = _engine_original_sprites[engine];
} }
if (!image) { if (image == 0) {
image = ((6 + _engine_sprite_add[img+1]) & _engine_sprite_and[img+1]) + _engine_sprite_base[img+1]; image =
((6 + _engine_sprite_add[img + 1]) & _engine_sprite_and[img + 1]) +
_engine_sprite_base[img + 1];
} }
} }
DrawSprite(image | image_ormod, x, y); DrawSprite(image | image_ormod, x, y);
@ -337,7 +335,7 @@ void DrawTrainEngineInfo(int engine, int x, int y, int maxw)
{ {
const RailVehicleInfo *rvi = RailVehInfo(engine); const RailVehicleInfo *rvi = RailVehInfo(engine);
int cap; int cap;
uint multihead = ((rvi->flags & RVI_MULTIHEAD) ? 1 : 0); uint multihead = (rvi->flags & RVI_MULTIHEAD) ? 1 : 0;
SetDParam(0, ((_price.build_railvehicle >> 3) * rvi->base_cost) >> 5); SetDParam(0, ((_price.build_railvehicle >> 3) * rvi->base_cost) >> 5);
SetDParam(2, rvi->max_speed * 10 >> 4); SetDParam(2, rvi->max_speed * 10 >> 4);
@ -416,7 +414,7 @@ static int32 CmdBuildRailWagon(uint engine, uint tile, uint32 flags)
u->next = v; u->next = v;
v->subtype = TS_Not_First; v->subtype = TS_Not_First;
v->u.rail.first_engine = u->u.rail.first_engine; v->u.rail.first_engine = u->u.rail.first_engine;
if (v->u.rail.first_engine == 0xffff && u->subtype == TS_Front_Engine ) if (v->u.rail.first_engine == 0xffff && u->subtype == TS_Front_Engine)
v->u.rail.first_engine = u->engine_type; v->u.rail.first_engine = u->engine_type;
} else { } else {
v->u.rail.first_engine = 0xffff; v->u.rail.first_engine = 0xffff;
@ -449,11 +447,13 @@ static int32 CmdBuildRailWagon(uint engine, uint tile, uint32 flags)
static void NormalizeTrainVehInDepot(Vehicle *u) static void NormalizeTrainVehInDepot(Vehicle *u)
{ {
Vehicle *v; Vehicle *v;
FOR_ALL_VEHICLES(v) { FOR_ALL_VEHICLES(v) {
if (v->type == VEH_Train && v->subtype == TS_Free_Car && if (v->type == VEH_Train && v->subtype == TS_Free_Car &&
v->tile == u->tile && v->tile == u->tile &&
v->u.rail.track == 0x80) { v->u.rail.track == 0x80) {
if (DoCommandByTile(0,v->index | (u->index<<16), 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE) == CMD_ERROR) if (DoCommandByTile(0, v->index | (u->index << 16), 1, DC_EXEC,
CMD_MOVE_RAIL_VEHICLE) == CMD_ERROR)
break; break;
} }
} }
@ -521,10 +521,8 @@ void AddRearEngineToMultiheadedTrain(Vehicle *v, Vehicle *u, bool building)
if (building) v->next = u; if (building) v->next = u;
u->engine_type = v->engine_type; u->engine_type = v->engine_type;
u->build_year = v->build_year; u->build_year = v->build_year;
if (building) if (building) v->value >>= 1;
v->value = u->value = v->value >> 1; u->value = v->value;
else
u->value = v->value;
u->type = VEH_Train; u->type = VEH_Train;
u->cur_image = 0xAC2; u->cur_image = 0xAC2;
VehiclePositionChanged(u); VehiclePositionChanged(u);
@ -634,30 +632,30 @@ int32 CmdBuildRailVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2)
static bool IsTunnelTile(TileIndex tile) static bool IsTunnelTile(TileIndex tile)
{ {
return IsTileType(tile, MP_TUNNELBRIDGE) && return IsTileType(tile, MP_TUNNELBRIDGE) && (_map5[tile] & 0x80) == 0;
(_map5[tile]&0x80) == 0;
} }
int CheckTrainStoppedInDepot(Vehicle *v) int CheckTrainStoppedInDepot(const Vehicle *v)
{ {
int count; int count;
TileIndex tile = v->tile; TileIndex tile = v->tile;
/* check if stopped in a depot */ /* check if stopped in a depot */
if (!IsTileDepotType(tile, TRANSPORT_RAIL) || v->cur_speed != 0) { if (!IsTileDepotType(tile, TRANSPORT_RAIL) || v->cur_speed != 0) {
errmsg:
_error_message = STR_881A_TRAINS_CAN_ONLY_BE_ALTERED; _error_message = STR_881A_TRAINS_CAN_ONLY_BE_ALTERED;
return -1; return -1;
} }
count = 0; count = 0;
do { for (; v != NULL; v = v->next) {
count++; count++;
if (v->u.rail.track != 0x80 || v->tile != (TileIndex)tile || if (v->u.rail.track != 0x80 || v->tile != tile ||
(v->subtype == TS_Front_Engine && !(v->vehstatus&VS_STOPPED))) (v->subtype == TS_Front_Engine && !(v->vehstatus & VS_STOPPED))) {
goto errmsg; _error_message = STR_881A_TRAINS_CAN_ONLY_BE_ALTERED;
} while ( (v=v->next) != NULL); return -1;
}
}
return count; return count;
} }
@ -670,30 +668,36 @@ static Vehicle *UnlinkWagon(Vehicle *v, Vehicle *first)
v->u.rail.first_engine = 0xffff; v->u.rail.first_engine = 0xffff;
if (v == first) { if (v == first) {
Vehicle *u; Vehicle *u;
if ((v=v->next) == NULL) return NULL;
for (u=v; u; u=u->next) u->u.rail.first_engine = v->engine_type; v = v->next;
if (v == NULL) return NULL;
for (u = v; u != NULL; u = u->next) u->u.rail.first_engine = v->engine_type;
v->subtype = TS_Free_Car; v->subtype = TS_Free_Car;
return v; return v;
} else { } else {
Vehicle *u; Vehicle *u;
for(u=first; u->next!=v; u=u->next) {}
for (u = first; u->next != v; u = u->next) {}
u->next = v->next; u->next = v->next;
return first; return first;
} }
} }
static Vehicle *FindGoodVehiclePos(Vehicle *src) static Vehicle *FindGoodVehiclePos(const Vehicle *src)
{ {
Vehicle *dst; Vehicle *dst;
uint16 eng = src->engine_type; uint16 eng = src->engine_type;
TileIndex tile = src->tile; TileIndex tile = src->tile;
FOR_ALL_VEHICLES(dst) { FOR_ALL_VEHICLES(dst) {
if (dst->type == VEH_Train && dst->subtype == TS_Free_Car && dst->tile==tile) { if (dst->type == VEH_Train && dst->subtype == TS_Free_Car &&
dst->tile == tile) {
// check so all vehicles in the line have the same engine. // check so all vehicles in the line have the same engine.
Vehicle *v = dst; Vehicle *v = dst;
while (v->engine_type == eng) { while (v->engine_type == eng) {
if ((v = v->next) == NULL) return dst; v = v->next;
if (v == NULL) return dst;
} }
} }
} }
@ -701,11 +705,11 @@ static Vehicle *FindGoodVehiclePos(Vehicle *src)
return NULL; return NULL;
} }
/* p1 & 0xffff= source vehicle index /* p1 & 0xffff = source vehicle index
p1 & 0xffff0000 = what wagon to put the wagon AFTER, 0xffff0000 to make a new line * p1 & 0xffff0000 = what wagon to put the wagon AFTER,
p2 & 1 = move all vehicles following the vehicle.. * 0xffff0000 to make a new line
* p2 & 1 = move all vehicles following the vehicle..
*/ */
int32 CmdMoveRailVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2) int32 CmdMoveRailVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2)
{ {
Vehicle *src, *dst, *src_head, *dst_head; Vehicle *src, *dst, *src_head, *dst_head;
@ -717,15 +721,15 @@ int32 CmdMoveRailVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2)
if (src->type != VEH_Train) return CMD_ERROR; if (src->type != VEH_Train) return CMD_ERROR;
is_loco = !(RailVehInfo(src->engine_type)->flags & RVI_WAGON) is_loco = !(RailVehInfo(src->engine_type)->flags & RVI_WAGON) &&
&& is_firsthead_sprite(src->spritenum); is_firsthead_sprite(src->spritenum);
// if nothing is selected as destination, try and find a matching vehicle to drag to. // if nothing is selected as destination, try and find a matching vehicle to drag to.
if (((int32)p1 >> 16) == -1) { if (((int32)p1 >> 16) == -1) {
dst = NULL; dst = NULL;
if (!is_loco) dst = FindGoodVehiclePos(src); if (!is_loco) dst = FindGoodVehiclePos(src);
} else { } else {
dst = GetVehicle(((int32)p1 >> 16)); dst = GetVehicle((int32)p1 >> 16);
} }
// don't move the same vehicle.. // don't move the same vehicle..
@ -752,14 +756,17 @@ int32 CmdMoveRailVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2)
if (num < 0) if (num < 0)
return CMD_ERROR; return CMD_ERROR;
if (num > (_patches.mammoth_trains ? 100 : 9) && dst_head->subtype == TS_Front_Engine ) if (num > (_patches.mammoth_trains ? 100 : 9) &&
dst_head->subtype == TS_Front_Engine )
return_cmd_error(STR_8819_TRAIN_TOO_LONG); return_cmd_error(STR_8819_TRAIN_TOO_LONG);
// if it's a multiheaded vehicle we're dragging to, drag to the vehicle before.. // if it's a multiheaded vehicle we're dragging to, drag to the vehicle before..
while (is_custom_secondhead_sprite(dst->spritenum) while (is_custom_secondhead_sprite(dst->spritenum) || (
|| (!is_custom_sprite(dst->spritenum) && _engine_sprite_add[dst->spritenum] != 0)) { !is_custom_sprite(dst->spritenum) &&
_engine_sprite_add[dst->spritenum] != 0)
) {
Vehicle *v = GetPrevVehicleInChain(dst); Vehicle *v = GetPrevVehicleInChain(dst);
if (!v || src == v) break; if (v == NULL || src == v) break;
dst = v; dst = v;
} }
@ -827,10 +834,10 @@ int32 CmdMoveRailVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2)
// link in the wagon(s) in the chain. // link in the wagon(s) in the chain.
{ {
Vehicle *v = src; Vehicle *v;
while (v->next != NULL) {
for (v = src; v->next != NULL; v = v->next) {
v->next->u.rail.first_engine = v->u.rail.first_engine; v->next->u.rail.first_engine = v->u.rail.first_engine;
v = v->next;
} }
v->next = dst->next; v->next = dst->next;
} }
@ -910,7 +917,8 @@ int32 CmdSellRailWagon(int x, int y, uint32 flags, uint32 p1, uint32 p2)
// 4) the first and the last vehicle of the chain are not identical // 4) the first and the last vehicle of the chain are not identical
// 5) and of "engine" type (i.e. not a carriage) // 5) and of "engine" type (i.e. not a carriage)
// then let the last vehicle live // then let the last vehicle live
if ( (p2 == 1) && (v != last) && ( last->engine_type == first->engine_type ) && (last != first) && (first->subtype == TS_Front_Engine) ) if (p2 == 1 && v != last && last->engine_type == first->engine_type &&
last != first && first->subtype == TS_Front_Engine)
last = GetPrevVehicleInChain(last); last = GetPrevVehicleInChain(last);
else else
last = NULL; last = NULL;
@ -919,7 +927,8 @@ int32 CmdSellRailWagon(int x, int y, uint32 flags, uint32 p1, uint32 p2)
// sell last part of multiheaded? // sell last part of multiheaded?
last = GetLastVehicleInChain(v); last = GetLastVehicleInChain(v);
// Check if the end-part is the same engine and check if it is the rear-end // Check if the end-part is the same engine and check if it is the rear-end
if (last->engine_type != first->engine_type || is_firsthead_sprite(last->spritenum)) if (last->engine_type != first->engine_type ||
is_firsthead_sprite(last->spritenum))
last = NULL; last = NULL;
} else { } else {
last = NULL; last = NULL;
@ -932,69 +941,54 @@ int32 CmdSellRailWagon(int x, int y, uint32 flags, uint32 p1, uint32 p2)
if (flags & DC_EXEC) { if (flags & DC_EXEC) {
// always redraw the depot. maybe redraw train list Vehicle *tmp;
InvalidateWindow(WC_VEHICLE_DEPOT, first->tile); InvalidateWindow(WC_VEHICLE_DEPOT, first->tile);
if (first->subtype == TS_Front_Engine) { if (first->subtype == TS_Front_Engine) RebuildVehicleLists();
RebuildVehicleLists();
}
// when selling an attached locomotive. we need to delete its window. // when selling an attached locomotive. we need to delete its window.
if (v->subtype == TS_Front_Engine) { if (v->subtype == TS_Front_Engine) {
DeleteWindowById(WC_VEHICLE_VIEW, v->index); DeleteWindowById(WC_VEHICLE_VIEW, v->index);
// rearrange all vehicles that follow to separate lines. // rearrange all vehicles that follow to separate lines.
if (p2 == 2) { if (p2 == 2) {
Vehicle *u,*tmp; const Vehicle* u;
u = v->next; const Vehicle* tmp;
while (u != last) {
tmp = u; for (u = v->next; u != last; u = tmp) {
u = u->next; tmp = u->next;
DoCommandByTile(tmp->tile, tmp->index | ((-1)<<16), 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); DoCommandByTile(u->tile, u->index | INVALID_VEHICLE << 16, 0, DC_EXEC,
CMD_MOVE_RAIL_VEHICLE);
} }
} }
} }
// delete the vehicles // delete the vehicles
cost = 0; cost = 0;
for(;;) { for (; v != last && p2 == 1; v = tmp) {
Vehicle *tmp; assert (first != NULL);
assert(first);
first = UnlinkWagon(v, first); first = UnlinkWagon(v, first);
cost -= v->value; cost -= v->value;
tmp = v; tmp = v->next;
DeleteVehicle(tmp); DeleteVehicle(v);
if ( v == last ) {
last = NULL;
break;
}
if ( (v=v->next) == last || p2 != 1) break;
} }
if (v != NULL) {
// delete last vehicle of multiheaded train? first = UnlinkWagon(v, first);
if (last) { cost -= v->value;
first = UnlinkWagon(last, first); DeleteVehicle(v);
cost -= last->value;
DeleteVehicle(last);
} }
// an attached train changed? // an attached train changed?
if (first && first->subtype == TS_Front_Engine) { if (first != NULL && first->subtype == TS_Front_Engine) {
UpdateTrainAcceleration(first); UpdateTrainAcceleration(first);
InvalidateWindow(WC_VEHICLE_DETAILS, first->index); InvalidateWindow(WC_VEHICLE_DETAILS, first->index);
} }
} else { } else {
cost = 0; cost = 0;
for(;;) { for (; v != last && p2 == 1; v = v->next) cost -= v->value;
cost -= v->value; if (v != NULL) cost -= v->value;
if ( v == last ) {
last = NULL;
break;
}
if ( (v=v->next) == last || p2 != 1) break;
}
if (last) cost -= last->value;
} }
InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Train); // updates the replace Train window InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Train);
return cost; return cost;
} }

View File

@ -325,7 +325,7 @@ uint GetWaypointByTile(uint tile);
void UpdateTrainAcceleration(Vehicle *v); void UpdateTrainAcceleration(Vehicle *v);
int32 GetTrainRunningCost(Vehicle *v); int32 GetTrainRunningCost(Vehicle *v);
int CheckTrainStoppedInDepot(Vehicle *v); int CheckTrainStoppedInDepot(const Vehicle *v);
bool VehicleNeedsService(const Vehicle *v); bool VehicleNeedsService(const Vehicle *v);