(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff

This commit is contained in:
tron 2006-02-13 21:15:00 +00:00
parent 235e72829e
commit ea73b46684
18 changed files with 405 additions and 445 deletions

View File

@ -695,7 +695,7 @@ static void IConsoleAliasExec(const IConsoleAlias* alias, byte tokencount, char*
{ {
const char *cmdptr; const char *cmdptr;
char *aliases[ICON_MAX_ALIAS_LINES], aliasstream[ICON_MAX_STREAMSIZE]; char *aliases[ICON_MAX_ALIAS_LINES], aliasstream[ICON_MAX_STREAMSIZE];
int i; uint i;
uint a_index, astream_i; uint a_index, astream_i;
memset(&aliases, 0, sizeof(aliases)); memset(&aliases, 0, sizeof(aliases));
@ -758,7 +758,7 @@ static void IConsoleAliasExec(const IConsoleAlias* alias, byte tokencount, char*
} }
} }
for (i = 0; i <= (int)a_index; i++) IConsoleCmdExec(aliases[i]); // execute each alias in turn for (i = 0; i <= a_index; i++) IConsoleCmdExec(aliases[i]); // execute each alias in turn
} }
/** /**

View File

@ -1121,10 +1121,11 @@ int32 GetTransportedGoodsIncome(uint num_pieces, uint dist, byte transit_days, b
if (transit_days > _cargoc.transit_days_2[cargo]) { if (transit_days > _cargoc.transit_days_2[cargo]) {
transit_days -= _cargoc.transit_days_2[cargo]; transit_days -= _cargoc.transit_days_2[cargo];
if (f < transit_days) if (f < transit_days) {
f = 0; f = 0;
else } else {
f -= transit_days; f -= transit_days;
}
} }
} }
if (f < 31) f = 31; if (f < 31) f = 31;
@ -1134,19 +1135,23 @@ int32 GetTransportedGoodsIncome(uint num_pieces, uint dist, byte transit_days, b
static void DeliverGoodsToIndustry(TileIndex xy, byte cargo_type, int num_pieces) static void DeliverGoodsToIndustry(TileIndex xy, byte cargo_type, int num_pieces)
{ {
Industry *ind, *best; Industry* best = NULL;
int t, u; Industry* ind;
uint u;
/* Check if there's an industry close to the station that accepts // Check if there's an industry close to the station that accepts the cargo
* the cargo */
best = NULL;
u = _patches.station_spread + 8; u = _patches.station_spread + 8;
FOR_ALL_INDUSTRIES(ind) { FOR_ALL_INDUSTRIES(ind) {
if (ind->xy != 0 && (cargo_type == ind->accepts_cargo[0] || cargo_type uint t;
== ind->accepts_cargo[1] || cargo_type == ind->accepts_cargo[2]) &&
ind->produced_cargo[0] != CT_INVALID && if (ind->xy != 0 && (
ind->produced_cargo[0] != cargo_type && cargo_type == ind->accepts_cargo[0] ||
(t = DistanceManhattan(ind->xy, xy)) < 2 * u) { cargo_type == ind->accepts_cargo[1] ||
cargo_type == ind->accepts_cargo[2]
) &&
ind->produced_cargo[0] != CT_INVALID &&
ind->produced_cargo[0] != cargo_type &&
(t = DistanceManhattan(ind->xy, xy)) < 2 * u) {
u = t; u = t;
best = ind; best = ind;
} }
@ -1595,7 +1600,7 @@ int32 CmdBuyShareInCompany(int x, int y, uint32 flags, uint32 p1, uint32 p2)
break; break;
} }
} }
InvalidateWindow(WC_COMPANY, (int)p1); InvalidateWindow(WC_COMPANY, p1);
} }
return cost; return cost;
} }
@ -1627,7 +1632,7 @@ int32 CmdSellShareInCompany(int x, int y, uint32 flags, uint32 p1, uint32 p2)
PlayerID* b = p->share_owners; PlayerID* b = p->share_owners;
while (*b != _current_player) b++; /* share owners is guaranteed to contain player */ while (*b != _current_player) b++; /* share owners is guaranteed to contain player */
*b = OWNER_SPECTATOR; *b = OWNER_SPECTATOR;
InvalidateWindow(WC_COMPANY, (int)p1); InvalidateWindow(WC_COMPANY, p1);
} }
return cost; return cost;
} }

View File

@ -392,19 +392,11 @@ static void IndustryViewWndProc(Window *w, WindowEvent *e)
break; break;
case WE_ON_EDIT_TEXT: case WE_ON_EDIT_TEXT:
if (*e->edittext.str) { if (e->edittext.str[0] != '\0') {
Industry *i; Industry* i = GetIndustry(w->window_number);
int val; int line = WP(w,vp2_d).data_1;
int line;
i = GetIndustry(w->window_number); i->production_rate[line] = clamp(atoi(e->edittext.str), 32, 2040) / 8;
line = WP(w,vp2_d).data_1;
val = atoi(e->edittext.str);
if (!IS_INT_INSIDE(val, 32, 2040)) {
if (val < 32) val = 32;
else val = 2040;
}
i->production_rate[line] = (byte)(val / 8);
UpdateIndustryProduction(i); UpdateIndustryProduction(i);
SetWindowDirty(w); SetWindowDirty(w);
} }

View File

@ -461,8 +461,8 @@ void ConvertGroundTilesIntoWaterTiles(void)
for (tile = 0; tile < MapSize(); ++tile) { for (tile = 0; tile < MapSize(); ++tile) {
if (IsTileType(tile, MP_CLEAR) && GetTileSlope(tile, &h) == 0 && h == 0) { if (IsTileType(tile, MP_CLEAR) && GetTileSlope(tile, &h) == 0 && h == 0) {
SetTileType(tile, MP_WATER); SetTileType(tile, MP_WATER);
_m[tile].m5 = 0;
SetTileOwner(tile, OWNER_WATER); SetTileOwner(tile, OWNER_WATER);
_m[tile].m5 = 0;
} }
} }
} }
@ -630,37 +630,48 @@ void GenerateLandscape(void)
uint flag; uint flag;
uint32 r; uint32 r;
if (_opt.landscape == LT_HILLY) { switch (_opt.landscape) {
for (i = ScaleByMapSize((Random() & 0x7F) + 950); i != 0; --i) case LT_HILLY:
GenerateTerrain(2, 0); for (i = ScaleByMapSize((Random() & 0x7F) + 950); i != 0; --i) {
GenerateTerrain(2, 0);
}
r = Random(); r = Random();
flag = GB(r, 0, 2) | 4; flag = GB(r, 0, 2) | 4;
for (i = ScaleByMapSize(GB(r, 16, 7) + 450); i != 0; --i) for (i = ScaleByMapSize(GB(r, 16, 7) + 450); i != 0; --i) {
GenerateTerrain(4, flag); GenerateTerrain(4, flag);
} else if (_opt.landscape == LT_DESERT) { }
for (i = ScaleByMapSize((Random()&0x7F) + 170); i != 0; --i) break;
GenerateTerrain(0, 0);
r = Random(); case LT_DESERT:
flag = GB(r, 0, 2) | 4; for (i = ScaleByMapSize((Random() & 0x7F) + 170); i != 0; --i) {
for (i = ScaleByMapSize(GB(r, 16, 8) + 1700); i != 0; --i) GenerateTerrain(0, 0);
GenerateTerrain(0, flag); }
flag ^= 2; r = Random();
flag = GB(r, 0, 2) | 4;
for (i = ScaleByMapSize(GB(r, 16, 8) + 1700); i != 0; --i) {
GenerateTerrain(0, flag);
}
for (i = ScaleByMapSize((Random() & 0x7F) + 410); i != 0; --i) flag ^= 2;
GenerateTerrain(3, flag);
} else { for (i = ScaleByMapSize((Random() & 0x7F) + 410); i != 0; --i) {
i = ScaleByMapSize((Random() & 0x7F) + (3 - _opt.diff.quantity_sea_lakes) * 256 + 100); GenerateTerrain(3, flag);
for (; i != 0; --i) }
GenerateTerrain(_opt.diff.terrain_type, 0); break;
default:
i = ScaleByMapSize((Random() & 0x7F) + (3 - _opt.diff.quantity_sea_lakes) * 256 + 100);
for (; i != 0; --i) {
GenerateTerrain(_opt.diff.terrain_type, 0);
}
break;
} }
ConvertGroundTilesIntoWaterTiles(); ConvertGroundTilesIntoWaterTiles();
if (_opt.landscape == LT_DESERT) if (_opt.landscape == LT_DESERT) CreateDesertOrRainForest();
CreateDesertOrRainForest();
} }
void OnTick_Town(void); void OnTick_Town(void);

41
npf.c
View File

@ -208,25 +208,26 @@ static void NPFMarkTile(TileIndex tile)
#ifdef NO_DEBUG_MESSAGES #ifdef NO_DEBUG_MESSAGES
return; return;
#else #else
if (_debug_npf_level >= 1) if (_debug_npf_level < 1) return;
switch (GetTileType(tile)) { switch (GetTileType(tile)) {
case MP_RAILWAY: case MP_RAILWAY:
/* DEBUG: mark visited tiles by mowing the grass under them /* DEBUG: mark visited tiles by mowing the grass under them ;-) */
* ;-) */ if (!IsTileDepotType(tile, TRANSPORT_RAIL)) {
if (!IsTileDepotType(tile, TRANSPORT_RAIL)) { SB(_m[tile].m2, 0, 4, 0);
SB(_m[tile].m2, 0, 4, 0); MarkTileDirtyByTile(tile);
MarkTileDirtyByTile(tile); }
} break;
break;
case MP_STREET: case MP_STREET:
if (!IsTileDepotType(tile, TRANSPORT_ROAD)) { if (!IsTileDepotType(tile, TRANSPORT_ROAD)) {
SB(_m[tile].m4, 4, 3, 0); SB(_m[tile].m4, 4, 3, 0);
MarkTileDirtyByTile(tile); MarkTileDirtyByTile(tile);
} }
break; break;
default:
break; default:
} break;
}
#endif #endif
} }
@ -298,7 +299,7 @@ static int32 NPFRailPathCost(AyStar* as, AyStarNode* current, OpenListNode* pare
TileIndex tile = current->tile; TileIndex tile = current->tile;
Trackdir trackdir = (Trackdir)current->direction; Trackdir trackdir = (Trackdir)current->direction;
int32 cost = 0; int32 cost = 0;
/* HACK: We create a OpenListNode manualy, so we can call EndNodeCheck */ /* HACK: We create a OpenListNode manually, so we can call EndNodeCheck */
OpenListNode new_node; OpenListNode new_node;
/* Determine base length */ /* Determine base length */

View File

@ -116,7 +116,7 @@ typedef enum TransportTypes {
* bridges. For now, you should just not change the values for road * bridges. For now, you should just not change the values for road
* and rail. * and rail.
*/ */
TRANSPORT_RAIL = 0, TRANSPORT_RAIL = 0,
TRANSPORT_ROAD = 1, TRANSPORT_ROAD = 1,
TRANSPORT_WATER, // = 2 TRANSPORT_WATER, // = 2
TRANSPORT_END, TRANSPORT_END,

View File

@ -558,12 +558,12 @@ static bool NtpVisit(NewTrackPathFinder *tpf, TileIndex tile, uint dir, uint len
uint offs = tpf->hash_tile[hash]; uint offs = tpf->hash_tile[hash];
do { do {
link = NTP_GET_LINK_PTR(tpf, offs); link = NTP_GET_LINK_PTR(tpf, offs);
if (tile == link->tile && (uint)(link->typelength & 0x3) == dir) { if (tile == link->tile && (link->typelength & 0x3U) == dir) {
if (length >= (uint)(link->typelength >> 2)) return false; if (length >= link->typelength >> 2) return false;
link->typelength = dir | (length << 2); link->typelength = dir | (length << 2);
return true; return true;
} }
} while ((offs=link->next) != 0xFFFF); } while ((offs = link->next) != 0xFFFF);
} }
/* get here if we need to add a new link to link, /* get here if we need to add a new link to link,
@ -611,9 +611,9 @@ static bool NtpCheck(NewTrackPathFinder *tpf, TileIndex tile, uint dir, uint len
offs = tpf->hash_tile[hash]; offs = tpf->hash_tile[hash];
for (;;) { for (;;) {
link = NTP_GET_LINK_PTR(tpf, offs); link = NTP_GET_LINK_PTR(tpf, offs);
if (tile == link->tile && (uint)(link->typelength & 0x3) == dir) { if (tile == link->tile && (link->typelength & 0x3U) == dir) {
assert( (uint)(link->typelength >> 2) <= length); assert(link->typelength >> 2 <= length);
return length == (uint)(link->typelength >> 2); return length == link->typelength >> 2;
} }
offs = link->next; offs = link->next;
assert(offs != 0xffff); assert(offs != 0xffff);

View File

@ -1577,33 +1577,34 @@ typedef struct SetSignalsData {
static bool SetSignalsEnumProc(TileIndex tile, SetSignalsData *ssd, int track, uint length, byte *state) static bool SetSignalsEnumProc(TileIndex tile, SetSignalsData *ssd, int track, uint length, byte *state)
{ {
if (!IsTileType(tile, MP_RAILWAY)) return false;
// the tile has signals? // the tile has signals?
if (IsTileType(tile, MP_RAILWAY)) { if (HasSignalOnTrack(tile, TrackdirToTrack(track))) {
if (HasSignalOnTrack(tile, TrackdirToTrack(track))) { if (HasSignalOnTrackdir(tile, ReverseTrackdir(track))) {
if (HasSignalOnTrackdir(tile, ReverseTrackdir(track))) { // yes, add the signal to the list of signals
// yes, add the signal to the list of signals if (ssd->cur != NUM_SSD_ENTRY) {
if (ssd->cur != NUM_SSD_ENTRY) { ssd->tile[ssd->cur] = tile; // remember the tile index
ssd->tile[ssd->cur] = tile; // remember the tile index ssd->bit[ssd->cur] = track; // and the controlling bit number
ssd->bit[ssd->cur] = track; // and the controlling bit number ssd->cur++;
ssd->cur++;
}
// remember if this block has a presignal.
ssd->has_presignal |= (_m[tile].m4&1);
} }
if (HasSignalOnTrackdir(tile, track) && (_m[tile].m4 & 2)) { // remember if this block has a presignal.
// this is an exit signal that points out from the segment ssd->has_presignal |= (_m[tile].m4&1);
ssd->presignal_exits++;
if (GetSignalState(tile, track) != SIGNAL_STATE_RED)
ssd->presignal_exits_free++;
}
return true;
} else if (IsTileDepotType(tile, TRANSPORT_RAIL)) {
return true; // don't look further if the tile is a depot
} }
if (HasSignalOnTrackdir(tile, track) && _m[tile].m4 & 2) {
// this is an exit signal that points out from the segment
ssd->presignal_exits++;
if (GetSignalState(tile, track) != SIGNAL_STATE_RED)
ssd->presignal_exits_free++;
}
return true;
} else if (IsTileDepotType(tile, TRANSPORT_RAIL)) {
return true; // don't look further if the tile is a depot
} }
return false; return false;
} }
@ -1806,7 +1807,7 @@ bool UpdateSignalsOnSegment(TileIndex tile, byte direction)
direction = ssd.next_dir[ssd.cur_stack]; direction = ssd.next_dir[ssd.cur_stack];
} }
return (bool)result; return result != 0;
} }
void SetSignalsOnBothDir(TileIndex tile, byte track) void SetSignalsOnBothDir(TileIndex tile, byte track)
@ -1836,7 +1837,7 @@ static uint GetSlopeZ_Track(const TileInfo* ti)
// inclined foundation // inclined foundation
th = _inclined_tileh[f - 15]; th = _inclined_tileh[f - 15];
} }
} else if ((ti->map5 & 0xC0) == 0xC0) { } else if ((ti->map5 & RAIL_TILE_TYPE_MASK) == RAIL_TYPE_DEPOT_WAYPOINT) {
// depot or waypoint // depot or waypoint
return z + 8; return z + 8;
} }

View File

@ -69,16 +69,13 @@ static bool CheckAllowRemoveRoad(TileIndex tile, uint br, bool *edge_road)
uint n; uint n;
*edge_road = true; *edge_road = true;
if (_game_mode == GM_EDITOR) if (_game_mode == GM_EDITOR) return true;
return true;
blocks = GetRoadBitsByTile(tile); blocks = GetRoadBitsByTile(tile);
if (blocks == 0) if (blocks == 0) return true;
return true;
// Only do the special processing for actual players. // Only do the special processing for actual players.
if (_current_player >= MAX_PLAYERS) if (_current_player >= MAX_PLAYERS) return true;
return true;
// A railway crossing has the road owner in the map3_lo byte. // A railway crossing has the road owner in the map3_lo byte.
if (IsTileType(tile, MP_STREET) && IsLevelCrossing(tile)) { if (IsTileType(tile, MP_STREET) && IsLevelCrossing(tile)) {
@ -92,8 +89,7 @@ static bool CheckAllowRemoveRoad(TileIndex tile, uint br, bool *edge_road)
return owner == OWNER_NONE || CheckOwnership(owner); return owner == OWNER_NONE || CheckOwnership(owner);
} }
if (_cheats.magic_bulldozer.value) if (_cheats.magic_bulldozer.value) return true;
return true;
// Get a bitmask of which neighbouring roads has a tile // Get a bitmask of which neighbouring roads has a tile
n = 0; n = 0;
@ -108,8 +104,7 @@ static bool CheckAllowRemoveRoad(TileIndex tile, uint br, bool *edge_road)
Town *t; Town *t;
*edge_road = false; *edge_road = false;
// you can remove all kind of roads with extra dynamite // you can remove all kind of roads with extra dynamite
if (_patches.extra_dynamite) if (_patches.extra_dynamite) return true;
return true;
t = ClosestTownFromTile(tile, _patches.dist_local_authority); t = ClosestTownFromTile(tile, _patches.dist_local_authority);
@ -164,14 +159,15 @@ int32 CmdRemoveRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2)
if (owner == OWNER_TOWN && _game_mode != GM_EDITOR) { if (owner == OWNER_TOWN && _game_mode != GM_EDITOR) {
if (IsTileType(tile, MP_TUNNELBRIDGE)) { // index of town is not saved for bridge (no space) if (IsTileType(tile, MP_TUNNELBRIDGE)) { // index of town is not saved for bridge (no space)
t = ClosestTownFromTile(tile, _patches.dist_local_authority); t = ClosestTownFromTile(tile, _patches.dist_local_authority);
} else } else {
t = GetTown(_m[tile].m2); t = GetTown(_m[tile].m2);
} else }
} else {
t = NULL; t = NULL;
}
// allow deleting road under bridge // allow deleting road under bridge
if (ti.type != MP_TUNNELBRIDGE && !EnsureNoVehicle(tile)) if (ti.type != MP_TUNNELBRIDGE && !EnsureNoVehicle(tile)) return CMD_ERROR;
return CMD_ERROR;
{ {
bool b; bool b;
@ -336,7 +332,7 @@ static uint32 CheckRoadSlope(int tileh, byte *pieces, byte existing)
} }
// partly leveled up tile, only if there's no road on that tile // partly leveled up tile, only if there's no road on that tile
if ( !existing && (tileh == 1 || tileh == 2 || tileh == 4 || tileh == 8) ) { if (!existing && (tileh == 1 || tileh == 2 || tileh == 4 || tileh == 8)) {
// force full pieces. // force full pieces.
*pieces |= (*pieces & 0xC) >> 2; *pieces |= (*pieces & 0xC) >> 2;
*pieces |= (*pieces & 0x3) << 2; *pieces |= (*pieces & 0x3) << 2;
@ -383,11 +379,13 @@ int32 CmdBuildRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2)
} else if (ti.type == MP_RAILWAY) { } else if (ti.type == MP_RAILWAY) {
byte m5; byte m5;
if (IsSteepTileh(ti.tileh)) // very steep tile if (IsSteepTileh(ti.tileh)) { // very steep tile
return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION); return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION);
}
if (!_valid_tileh_slopes_road[2][ti.tileh]) // prevent certain slopes if (!_valid_tileh_slopes_road[2][ti.tileh]) { // prevent certain slopes
return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION); return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION);
}
if (ti.map5 == 2) { if (ti.map5 == 2) {
if (pieces & 5) goto do_clear; if (pieces & 5) goto do_clear;
@ -395,8 +393,9 @@ int32 CmdBuildRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2)
} else if (ti.map5 == 1) { } else if (ti.map5 == 1) {
if (pieces & 10) goto do_clear; if (pieces & 10) goto do_clear;
m5 = 0x18; m5 = 0x18;
} else } else {
goto do_clear; goto do_clear;
}
if (flags & DC_EXEC) { if (flags & DC_EXEC) {
ModifyTile(tile, ModifyTile(tile,
@ -410,26 +409,25 @@ int32 CmdBuildRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2)
} }
return _price.build_road * 2; return _price.build_road * 2;
} else if (ti.type == MP_TUNNELBRIDGE) { } else if (ti.type == MP_TUNNELBRIDGE) {
/* check for flat land */ /* check for flat land */
if (IsSteepTileh(ti.tileh)) // very steep tile if (IsSteepTileh(ti.tileh)) { // very steep tile
return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION); return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION);
}
/* is this middle part of a bridge? */ /* is this middle part of a bridge? */
if ((ti.map5 & 0xC0) != 0xC0) if ((ti.map5 & 0xC0) != 0xC0) goto do_clear;
goto do_clear;
/* only allow roads pertendicular to bridge */ /* only allow roads pertendicular to bridge */
if (((pieces & 5U) != 0) == ((ti.map5 & 0x01U) != 0)) if (((pieces & 5U) != 0) == ((ti.map5 & 0x01U) != 0)) goto do_clear;
goto do_clear;
/* check if clear land under bridge */ /* check if clear land under bridge */
if ((ti.map5 & 0xF8) == 0xE8) /* road under bridge */ if ((ti.map5 & 0xF8) == 0xE8) { /* road under bridge */
return_cmd_error(STR_1007_ALREADY_BUILT); return_cmd_error(STR_1007_ALREADY_BUILT);
else if ((ti.map5 & 0xE0) == 0xE0) /* other transport route under bridge */ } else if ((ti.map5 & 0xE0) == 0xE0) { /* other transport route under bridge */
return_cmd_error(STR_1008_MUST_REMOVE_RAILROAD_TRACK); return_cmd_error(STR_1008_MUST_REMOVE_RAILROAD_TRACK);
else if ((ti.map5 & 0xF8) == 0xC8) /* water under bridge */ } else if ((ti.map5 & 0xF8) == 0xC8) { /* water under bridge */
return_cmd_error(STR_3807_CAN_T_BUILD_ON_WATER); return_cmd_error(STR_3807_CAN_T_BUILD_ON_WATER);
}
/* all checked, can build road now! */ /* all checked, can build road now! */
cost = _price.build_road * 2; cost = _price.build_road * 2;
@ -587,7 +585,7 @@ int32 CmdRemoveLongRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2)
TileIndex t = start_tile; TileIndex t = start_tile;
start_tile = end_tile; start_tile = end_tile;
end_tile = t; end_tile = t;
p2 ^= IS_INT_INSIDE(p2&3, 1, 3) ? 3 : 0; p2 ^= IS_INT_INSIDE(p2 & 3, 1, 3) ? 3 : 0;
} }
cost = 0; cost = 0;
@ -735,17 +733,23 @@ uint GetRoadFoundation(uint tileh, uint bits)
{ {
int i; int i;
// normal level sloped building // normal level sloped building
if ((~_valid_tileh_slopes_road[1][tileh] & bits) == 0) if ((~_valid_tileh_slopes_road[1][tileh] & bits) == 0) return tileh;
return tileh;
// inclined sloped building // inclined sloped building
if ( ((i=0, tileh == 1) || (i+=2, tileh == 2) || (i+=2, tileh == 4) || (i+=2, tileh == 8)) && if ((
((bits == (ROAD_SW | ROAD_NE)) || (i++, bits == (ROAD_NW | ROAD_SE)))) (i = 0, tileh == 1) ||
(i += 2, tileh == 2) ||
(i += 2, tileh == 4) ||
(i += 2, tileh == 8)
) && (
( bits == (ROAD_SW | ROAD_NE)) ||
(i++, bits == (ROAD_NW | ROAD_SE))
)) {
return i + 15; return i + 15;
}
// rail crossing // rail crossing
if ((bits & 0x10) && _valid_tileh_slopes_road[2][tileh]) if ((bits & 0x10) && _valid_tileh_slopes_road[2][tileh]) return tileh;
return tileh;
return 0; return 0;
} }
@ -808,14 +812,12 @@ static void DrawRoadBits(TileInfo *ti, byte road, byte ground_type, bool snow, b
} }
// Draw extra details. // Draw extra details.
drts = _road_display_table[ground_type][road]; for (drts = _road_display_table[ground_type][road]; drts->image != 0; drts++) {
while ((image = drts->image) != 0) {
int x = ti->x | drts->subcoord_x; int x = ti->x | drts->subcoord_x;
int y = ti->y | drts->subcoord_y; int y = ti->y | drts->subcoord_y;
byte z = ti->z; byte z = ti->z;
if (ti->tileh != 0) z = GetSlopeZ(x, y); if (ti->tileh != 0) z = GetSlopeZ(x, y);
AddSortableSpriteToDraw(image, x, y, 2, 2, 0x10, z); AddSortableSpriteToDraw(drts->image, x, y, 2, 2, 0x10, z);
drts++;
} }
} }
@ -980,22 +982,23 @@ static void TileLoop_Road(TileIndex tile)
Town *t; Town *t;
int grp; int grp;
if (_opt.landscape == LT_HILLY) { switch (_opt.landscape) {
// Fix snow style if the road is above the snowline case LT_HILLY:
if ((_m[tile].m4 & 0x80) != ((GetTileZ(tile) > _opt.snow_line) ? 0x80 : 0x00)) { if ((_m[tile].m4 & 0x80) != (GetTileZ(tile) > _opt.snow_line ? 0x80 : 0x00)) {
_m[tile].m4 ^= 0x80; _m[tile].m4 ^= 0x80;
MarkTileDirtyByTile(tile); MarkTileDirtyByTile(tile);
} }
} else if (_opt.landscape == LT_DESERT) { break;
// Fix desert style
if (GetMapExtraBits(tile) == 1 && !(_m[tile].m4 & 0x80)) { case LT_DESERT:
_m[tile].m4 |= 0x80; if (GetMapExtraBits(tile) == 1 && !(_m[tile].m4 & 0x80)) {
MarkTileDirtyByTile(tile); _m[tile].m4 |= 0x80;
} MarkTileDirtyByTile(tile);
}
break;
} }
if (_m[tile].m5 & 0xE0) if (_m[tile].m5 & 0xE0) return;
return;
if (GB(_m[tile].m4, 4, 3) < 6) { if (GB(_m[tile].m4, 4, 3) < 6) {
t = ClosestTownFromTile(tile, (uint)-1); t = ClosestTownFromTile(tile, (uint)-1);
@ -1007,8 +1010,8 @@ static void TileLoop_Road(TileIndex tile)
// Show an animation to indicate road work // Show an animation to indicate road work
if (t->road_build_months != 0 && if (t->road_build_months != 0 &&
!(DistanceManhattan(t->xy, tile) >= 8 && grp == 0) && !(DistanceManhattan(t->xy, tile) >= 8 && grp == 0) &&
(_m[tile].m5==5 || _m[tile].m5==10)) { (_m[tile].m5 == 5 || _m[tile].m5 == 10)) {
if (GetTileSlope(tile, NULL) == 0 && EnsureNoVehicle(tile) && CHANCE16(1,20)) { if (GetTileSlope(tile, NULL) == 0 && EnsureNoVehicle(tile) && CHANCE16(1, 20)) {
_m[tile].m4 |= (GB(_m[tile].m4, 4, 3) <= 2 ? 7 : 6) << 4; _m[tile].m4 |= (GB(_m[tile].m4, 4, 3) <= 2 ? 7 : 6) << 4;
SndPlayTileFx(SND_21_JACKHAMMER, tile); SndPlayTileFx(SND_21_JACKHAMMER, tile);
@ -1027,8 +1030,7 @@ static void TileLoop_Road(TileIndex tile)
const byte *p = (_opt.landscape == LT_CANDY) ? _town_road_types_2[grp] : _town_road_types[grp]; const byte *p = (_opt.landscape == LT_CANDY) ? _town_road_types_2[grp] : _town_road_types[grp];
byte b = GB(_m[tile].m4, 4, 3); byte b = GB(_m[tile].m4, 4, 3);
if (b == p[0]) if (b == p[0]) return;
return;
if (b == p[1]) { if (b == p[1]) {
b = p[0]; b = p[0];

View File

@ -82,7 +82,7 @@ void DrawRoadVehEngine(int x, int y, EngineID engine, uint32 image_ormod)
if (is_custom_sprite(spritenum)) { if (is_custom_sprite(spritenum)) {
int sprite = GetCustomVehicleIcon(engine, 6); int sprite = GetCustomVehicleIcon(engine, 6);
if (sprite) { if (sprite != 0) {
DrawSprite(sprite | image_ormod, x, y); DrawSprite(sprite | image_ormod, x, y);
return; return;
} }
@ -263,8 +263,7 @@ int32 CmdSellRoadVeh(int x, int y, uint32 flags, uint32 p1, uint32 p2)
DeleteWindowById(WC_VEHICLE_VIEW, v->index); DeleteWindowById(WC_VEHICLE_VIEW, v->index);
ClearSlot(v, v->u.road.slot); ClearSlot(v, v->u.road.slot);
DeleteVehicle(v); DeleteVehicle(v);
if (IsLocalPlayer()) if (IsLocalPlayer()) InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Road);
InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Road); // updates the replace Road window
} }
return -(int32)v->value; return -(int32)v->value;
@ -310,10 +309,11 @@ static Depot *FindClosestRoadDepot(Vehicle *v)
Trackdir trackdir = GetVehicleTrackdir(v); Trackdir trackdir = GetVehicleTrackdir(v);
ftd = NPFRouteToDepotBreadthFirst(v->tile, trackdir, TRANSPORT_ROAD, v->owner, INVALID_RAILTYPE); ftd = NPFRouteToDepotBreadthFirst(v->tile, trackdir, TRANSPORT_ROAD, v->owner, INVALID_RAILTYPE);
if (ftd.best_bird_dist == 0) if (ftd.best_bird_dist == 0) {
return GetDepotByTile(ftd.node.tile); /* Target found */ return GetDepotByTile(ftd.node.tile); /* Target found */
else } else {
return NULL; /* Target not found */ return NULL; /* Target not found */
}
/* We do not search in two directions here, why should we? We can't reverse right now can we? */ /* We do not search in two directions here, why should we? We can't reverse right now can we? */
} else { } else {
RoadFindDepotData rfdd; RoadFindDepotData rfdd;
@ -324,8 +324,7 @@ static Depot *FindClosestRoadDepot(Vehicle *v)
for (i = 0; i != 4; i++) for (i = 0; i != 4; i++)
FollowTrack(tile, 0x2000 | TRANSPORT_ROAD, i, (TPFEnumProc*)EnumRoadSignalFindDepot, NULL, &rfdd); FollowTrack(tile, 0x2000 | TRANSPORT_ROAD, i, (TPFEnumProc*)EnumRoadSignalFindDepot, NULL, &rfdd);
if (rfdd.best_length == (uint)-1) if (rfdd.best_length == (uint)-1) return NULL;
return NULL;
return GetDepotByTile(rfdd.tile); return GetDepotByTile(rfdd.tile);
} }
@ -455,8 +454,7 @@ static void RoadVehDelete(Vehicle *v)
RebuildVehicleLists(); RebuildVehicleLists();
InvalidateWindow(WC_COMPANY, v->owner); InvalidateWindow(WC_COMPANY, v->owner);
if (IsTileType(v->tile, MP_STATION)) if (IsTileType(v->tile, MP_STATION)) ClearCrashedStation(v);
ClearCrashedStation(v);
BeginVehicleMove(v); BeginVehicleMove(v);
EndVehicleMove(v); EndVehicleMove(v);
@ -471,7 +469,9 @@ static byte SetRoadVehPosition(Vehicle *v, int x, int y)
// need this hint so it returns the right z coordinate on bridges. // need this hint so it returns the right z coordinate on bridges.
_get_z_hint = v->z_pos; _get_z_hint = v->z_pos;
new_z = GetSlopeZ(v->x_pos=x, v->y_pos=y); v->x_pos = x;
v->y_pos = y;
new_z = GetSlopeZ(x, y);
_get_z_hint = 0; _get_z_hint = 0;
old_z = v->z_pos; old_z = v->z_pos;
@ -486,7 +486,7 @@ static void RoadVehSetRandomDirection(Vehicle *v)
{ {
static const int8 _turn_prob[4] = { -1, 0, 0, 1 }; static const int8 _turn_prob[4] = { -1, 0, 0, 1 };
uint32 r = Random(); uint32 r = Random();
v->direction = (v->direction+_turn_prob[r&3])&7; v->direction = (v->direction + _turn_prob[r & 3]) & 7;
BeginVehicleMove(v); BeginVehicleMove(v);
UpdateRoadVehDeltaXY(v); UpdateRoadVehDeltaXY(v);
v->cur_image = GetRoadVehImage(v, v->direction); v->cur_image = GetRoadVehImage(v, v->direction);
@ -499,8 +499,7 @@ static void RoadVehIsCrashed(Vehicle *v)
if (v->u.road.crashed_ctr == 2) { if (v->u.road.crashed_ctr == 2) {
CreateEffectVehicleRel(v, 4, 4, 8, EV_EXPLOSION_LARGE); CreateEffectVehicleRel(v, 4, 4, 8, EV_EXPLOSION_LARGE);
} else if (v->u.road.crashed_ctr <= 45) { } else if (v->u.road.crashed_ctr <= 45) {
if ((v->tick_counter & 7) == 0) if ((v->tick_counter & 7) == 0) RoadVehSetRandomDirection(v);
RoadVehSetRandomDirection(v);
} else if (v->u.road.crashed_ctr >= 2220) { } else if (v->u.road.crashed_ctr >= 2220) {
RoadVehDelete(v); RoadVehDelete(v);
} }
@ -684,7 +683,7 @@ static void HandleRoadVehLoading(Vehicle *v)
InvalidateVehicleOrder(v); InvalidateVehicleOrder(v);
} }
static void StartRoadVehSound(Vehicle *v) static void StartRoadVehSound(const Vehicle* v)
{ {
SoundFx s = RoadVehInfo(v->engine_type)->sfx; SoundFx s = RoadVehInfo(v->engine_type)->sfx;
if (s == SND_19_BUS_START_PULL_AWAY && (v->tick_counter & 3) == 0) if (s == SND_19_BUS_START_PULL_AWAY && (v->tick_counter & 3) == 0)
@ -693,8 +692,9 @@ static void StartRoadVehSound(Vehicle *v)
} }
typedef struct RoadVehFindData { typedef struct RoadVehFindData {
int x,y; int x;
Vehicle *veh; int y;
const Vehicle* veh;
byte dir; byte dir;
} RoadVehFindData; } RoadVehFindData;
@ -727,8 +727,7 @@ static Vehicle *RoadVehFindCloseTo(Vehicle *v, int x, int y, byte dir)
RoadVehFindData rvf; RoadVehFindData rvf;
Vehicle *u; Vehicle *u;
if (v->u.road.reverse_ctr != 0) if (v->u.road.reverse_ctr != 0) return NULL;
return NULL;
rvf.x = x; rvf.x = x;
rvf.y = y; rvf.y = y;
@ -823,23 +822,22 @@ static byte RoadVehGetNewDirection(Vehicle *v, int x, int y)
x = x - v->x_pos + 1; x = x - v->x_pos + 1;
y = y - v->y_pos + 1; y = y - v->y_pos + 1;
if ((uint)x > 2 || (uint)y > 2) if ((uint)x > 2 || (uint)y > 2) return v->direction;
return v->direction; return _roadveh_new_dir[y * 4 + x];
return _roadveh_new_dir[y*4+x];
} }
static byte RoadVehGetSlidingDirection(Vehicle *v, int x, int y) static byte RoadVehGetSlidingDirection(Vehicle *v, int x, int y)
{ {
byte b = RoadVehGetNewDirection(v,x,y); byte b = RoadVehGetNewDirection(v, x, y);
byte d = v->direction; byte d = v->direction;
if (b == d) return d; if (b == d) return d;
d = (d+1)&7; d = (d + 1) & 7;
if (b==d) return d; if (b == d) return d;
d = (d-2)&7; d = (d - 2) & 7;
if (b==d) return d; if (b == d) return d;
if (b==((d-1)&7)) return d; if (b == ((d - 1) & 7)) return d;
if (b==((d-2)&7)) return d; if (b == ((d - 2) & 7)) return d;
return (d+2)&7; return (d + 2) & 7;
} }
typedef struct OvertakeData { typedef struct OvertakeData {
@ -850,9 +848,9 @@ typedef struct OvertakeData {
static void *EnumFindVehToOvertake(Vehicle *v, OvertakeData *od) static void *EnumFindVehToOvertake(Vehicle *v, OvertakeData *od)
{ {
if (v->tile != od->tile || v->type != VEH_Road || v == od->u || v == od->v) return
return NULL; v->tile == od->tile && v->type == VEH_Road && v == od->u && v == od->v ?
return v; v : NULL;
} }
static bool FindRoadVehToOvertake(OvertakeData *od) static bool FindRoadVehToOvertake(OvertakeData *od)
@ -911,15 +909,13 @@ static void RoadVehCheckOvertake(Vehicle *v, Vehicle *u)
static void RoadZPosAffectSpeed(Vehicle *v, byte old_z) static void RoadZPosAffectSpeed(Vehicle *v, byte old_z)
{ {
if (old_z == v->z_pos) if (old_z == v->z_pos) return;
return;
if (old_z < v->z_pos) { if (old_z < v->z_pos) {
v->cur_speed = v->cur_speed * 232 >> 8; v->cur_speed = v->cur_speed * 232 / 256; // slow down by ~10%
} else { } else {
uint16 spd = v->cur_speed + 2; uint16 spd = v->cur_speed + 2;
if (spd <= v->max_speed) if (spd <= v->max_speed) v->cur_speed = spd;
v->cur_speed = spd;
} }
} }
@ -935,7 +931,7 @@ static int PickRandomBit(uint bits)
num = RandomRange(num); num = RandomRange(num);
for (i = 0; !((bits & 1) && ((int)--num) < 0); bits >>= 1, i++); for (i = 0; !(bits & 1) || (int)--num >= 0; bits >>= 1, i++) {}
return i; return i;
} }
@ -973,8 +969,7 @@ static int RoadFindPathToDest(Vehicle *v, TileIndex tile, int enterdir)
byte m5; byte m5;
{ {
uint32 r; uint32 r = GetTileTrackStatus(tile, TRANSPORT_ROAD);
r = GetTileTrackStatus(tile, TRANSPORT_ROAD);
signal = GB(r, 16, 16); signal = GB(r, 16, 16);
bitmask = GB(r, 0, 16); bitmask = GB(r, 0, 16);
} }
@ -1100,8 +1095,7 @@ do_it:;
found_best_track:; found_best_track:;
if (HASBIT(signal, best_track)) if (HASBIT(signal, best_track)) return -1;
return -1;
return best_track; return best_track;
} }
@ -1147,8 +1141,7 @@ static void RoadVehController(Vehicle *v)
// decrease counters // decrease counters
v->tick_counter++; v->tick_counter++;
if (v->u.road.reverse_ctr != 0) if (v->u.road.reverse_ctr != 0) v->u.road.reverse_ctr--;
v->u.road.reverse_ctr--;
// handle crashed // handle crashed
if (v->u.road.crashed_ctr != 0) { if (v->u.road.crashed_ctr != 0) {
@ -1277,8 +1270,8 @@ again:
x = TileX(tile) * 16 + rdp[0].x; x = TileX(tile) * 16 + rdp[0].x;
y = TileY(tile) * 16 + rdp[0].y; y = TileY(tile) * 16 + rdp[0].y;
if (RoadVehFindCloseTo(v, x, y, newdir=RoadVehGetSlidingDirection(v, x, y))) newdir = RoadVehGetSlidingDirection(v, x, y);
return; if (RoadVehFindCloseTo(v, x, y, newdir)) return;
r = VehicleEnterTile(v, tile, x, y); r = VehicleEnterTile(v, tile, x, y);
if (r & 8) { if (r & 8) {
@ -1336,8 +1329,8 @@ again:
x = TileX(v->tile) * 16 + rdp[1].x; x = TileX(v->tile) * 16 + rdp[1].x;
y = TileY(v->tile) * 16 + rdp[1].y; y = TileY(v->tile) * 16 + rdp[1].y;
if (RoadVehFindCloseTo(v, x, y, newdir=RoadVehGetSlidingDirection(v, x, y))) newdir = RoadVehGetSlidingDirection(v, x, y);
return; if (RoadVehFindCloseTo(v, x, y, newdir)) return;
r = VehicleEnterTile(v, v->tile, x, y); r = VehicleEnterTile(v, v->tile, x, y);
if (r & 8) { if (r & 8) {
@ -1365,8 +1358,7 @@ again:
new_dir = RoadVehGetSlidingDirection(v, x, y); new_dir = RoadVehGetSlidingDirection(v, x, y);
if (!IS_BYTE_INSIDE(v->u.road.state, 0x20, 0x30) && (u=RoadVehFindCloseTo(v, x, y, new_dir)) != NULL) { if (!IS_BYTE_INSIDE(v->u.road.state, 0x20, 0x30) && (u=RoadVehFindCloseTo(v, x, y, new_dir)) != NULL) {
if (v->u.road.overtaking == 0) if (v->u.road.overtaking == 0) RoadVehCheckOvertake(v, u);
RoadVehCheckOvertake(v, u);
return; return;
} }
@ -1449,9 +1441,7 @@ again:
return; return;
} }
if ((r & 4) == 0) { if ((r & 4) == 0) v->u.road.frame++;
v->u.road.frame++;
}
v->cur_image = GetRoadVehImage(v, v->direction); v->cur_image = GetRoadVehImage(v, v->direction);
UpdateRoadVehDeltaXY(v); UpdateRoadVehDeltaXY(v);
@ -1489,7 +1479,8 @@ void RoadVehEnterDepot(Vehicle *v)
STR_9016_ROAD_VEHICLE_IS_WAITING, STR_9016_ROAD_VEHICLE_IS_WAITING,
NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0),
v->index, v->index,
0); 0
);
} }
} }
} }
@ -1500,10 +1491,8 @@ void RoadVehEnterDepot(Vehicle *v)
static void AgeRoadVehCargo(Vehicle *v) static void AgeRoadVehCargo(Vehicle *v)
{ {
if (_age_cargo_skip_counter != 0) if (_age_cargo_skip_counter != 0) return;
return; if (v->cargo_days != 255) v->cargo_days++;
if (v->cargo_days != 255)
v->cargo_days++;
} }
void RoadVeh_Tick(Vehicle *v) void RoadVeh_Tick(Vehicle *v)
@ -1516,17 +1505,10 @@ static void CheckIfRoadVehNeedsService(Vehicle *v)
{ {
Depot *depot; Depot *depot;
if (_patches.servint_roadveh == 0) if (_patches.servint_roadveh == 0) return;
return; if (!VehicleNeedsService(v)) return;
if (v->vehstatus & VS_STOPPED) return;
if (!VehicleNeedsService(v)) if (_patches.gotodepot && VehicleHasDepotOrders(v)) return;
return;
if (v->vehstatus & VS_STOPPED)
return;
if (_patches.gotodepot && VehicleHasDepotOrders(v))
return;
// Don't interfere with a depot visit scheduled by the user, or a // Don't interfere with a depot visit scheduled by the user, or a
// depot visit by the order list. // depot visit by the order list.
@ -1534,9 +1516,8 @@ static void CheckIfRoadVehNeedsService(Vehicle *v)
(v->current_order.flags & (OF_HALT_IN_DEPOT | OF_PART_OF_ORDERS)) != 0) (v->current_order.flags & (OF_HALT_IN_DEPOT | OF_PART_OF_ORDERS)) != 0)
return; return;
//If we already got a slot at a stop, use that FIRST, and go to a depot later // If we already got a slot at a stop, use that FIRST, and go to a depot later
if (v->u.road.slot != NULL) if (v->u.road.slot != NULL) return;
return;
depot = FindClosestRoadDepot(v); depot = FindClosestRoadDepot(v);
@ -1567,11 +1548,8 @@ void OnNewDay_RoadVeh(Vehicle *v)
int32 cost; int32 cost;
Station *st; Station *st;
if ((++v->day_counter & 7) == 0) if ((++v->day_counter & 7) == 0) DecreaseVehicleValue(v);
DecreaseVehicleValue(v); if (v->u.road.blocked_ctr == 0) CheckVehicleBreakdown(v);
if (v->u.road.blocked_ctr == 0)
CheckVehicleBreakdown(v);
AgeVehicle(v); AgeVehicle(v);
CheckIfRoadVehNeedsService(v); CheckIfRoadVehNeedsService(v);
@ -1653,8 +1631,7 @@ void OnNewDay_RoadVeh(Vehicle *v)
} }
} }
if (v->vehstatus & VS_STOPPED) if (v->vehstatus & VS_STOPPED) return;
return;
cost = RoadVehInfo(v->engine_type)->running_cost * _price.roadveh_running / 364; cost = RoadVehInfo(v->engine_type)->running_cost * _price.roadveh_running / 364;

View File

@ -63,7 +63,7 @@ static void GenerateDesertArea(TileIndex end, TileIndex start)
_generating_world = false; _generating_world = false;
} }
/** Scenario editor command that generates desert areas */ /** Scenario editor command that generates rocky areas */
static void GenerateRockyArea(TileIndex end, TileIndex start) static void GenerateRockyArea(TileIndex end, TileIndex start)
{ {
int size_x, size_y; int size_x, size_y;

3
tile.c
View File

@ -46,8 +46,7 @@ uint GetTileSlope(TileIndex tile, uint *h)
if ((d -= min) != 0) r += (--d << 4) + 2; if ((d -= min) != 0) r += (--d << 4) + 2;
if ((b -= min) != 0) r += (--b << 4) + 1; if ((b -= min) != 0) r += (--b << 4) + 1;
if (h != NULL) if (h != NULL) *h = min * 8;
*h = min * 8;
return r; return r;
} }

View File

@ -570,7 +570,7 @@ static void GrowTownInTile(TileIndex *tile_ptr, uint mask, int block, Town *t1)
int a,b,rcmd; int a,b,rcmd;
TileIndex tmptile; TileIndex tmptile;
TileInfo ti; TileInfo ti;
int i; uint i;
int j; int j;
TileIndex tile = *tile_ptr; TileIndex tile = *tile_ptr;

View File

@ -305,8 +305,7 @@ static int GetTrainAcceleration(Vehicle *v, bool mode)
num++; num++;
drag_coeff += 3; drag_coeff += 3;
if (u->u.rail.track == 0x80) if (u->u.rail.track == 0x80) max_speed = min(max_speed, 61);
max_speed = min(61, max_speed);
if (HASBIT(u->u.rail.flags, VRF_GOINGUP)) { if (HASBIT(u->u.rail.flags, VRF_GOINGUP)) {
incl += u->u.rail.cached_veh_weight * 60; //3% slope, quite a bit actually incl += u->u.rail.cached_veh_weight * 60; //3% slope, quite a bit actually
@ -322,8 +321,9 @@ static int GetTrainAcceleration(Vehicle *v, bool mode)
resistance += 60 * num; resistance += 60 * num;
resistance += friction * mass * speed / 1000; resistance += friction * mass * speed / 1000;
resistance += (area * drag_coeff * speed * speed) / 10000; resistance += (area * drag_coeff * speed * speed) / 10000;
} else } else {
resistance = (area * (drag_coeff / 2) * speed * speed) / 10000; resistance = (area * (drag_coeff / 2) * speed * speed) / 10000;
}
resistance += incl; resistance += incl;
resistance *= 4; //[N] resistance *= 4; //[N]
@ -452,13 +452,11 @@ static void AddArticulatedParts(const RailVehicleInfo *rvi, Vehicle **vl)
bool flip_image; bool flip_image;
uint i; uint i;
if (!HASBIT(rvi->callbackmask, CBM_ARTIC_ENGINE)) if (!HASBIT(rvi->callbackmask, CBM_ARTIC_ENGINE)) return;
return;
for (i = 1; i < 10; i++) { for (i = 1; i < 10; i++) {
callback = GetCallBackResult(CBID_TRAIN_ARTIC_ENGINE + (i << 8), v->engine_type, NULL); callback = GetCallBackResult(CBID_TRAIN_ARTIC_ENGINE + (i << 8), v->engine_type, NULL);
if (callback == CALLBACK_FAILED || callback == 0xFF) if (callback == CALLBACK_FAILED || callback == 0xFF) return;
return;
u->next = vl[i]; u->next = vl[i];
u = u->next; u = u->next;
@ -632,7 +630,7 @@ static const byte _railveh_score[] = {
static int32 EstimateTrainCost(const RailVehicleInfo* rvi) static int32 EstimateTrainCost(const RailVehicleInfo* rvi)
{ {
return (rvi->base_cost * (_price.build_railvehicle >> 3)) >> 5; return rvi->base_cost * (_price.build_railvehicle >> 3) >> 5;
} }
static void AddRearEngineToMultiheadedTrain(Vehicle* v, Vehicle* u, bool building) static void AddRearEngineToMultiheadedTrain(Vehicle* v, Vehicle* u, bool building)
@ -858,8 +856,7 @@ static Vehicle *FindGoodVehiclePos(const Vehicle *src)
TileIndex tile = src->tile; TileIndex tile = src->tile;
FOR_ALL_VEHICLES(dst) { FOR_ALL_VEHICLES(dst) {
if (dst->type == VEH_Train && IsFreeWagon(dst) && if (dst->type == VEH_Train && IsFreeWagon(dst) && dst->tile == tile) {
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;
@ -908,7 +905,6 @@ static void NormaliseTrainConsist(Vehicle *v)
if (u == v->u.rail.other_multiheaded_part) continue; if (u == v->u.rail.other_multiheaded_part) continue;
AddWagonToConsist(v->u.rail.other_multiheaded_part, u); AddWagonToConsist(v->u.rail.other_multiheaded_part, u);
} }
} }
@ -988,9 +984,7 @@ int32 CmdMoveRailVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2)
engine = NULL; engine = NULL;
} }
if (u == dst) { if (u == dst) {
if (engine != NULL) { if (engine != NULL) dst = engine->u.rail.other_multiheaded_part;
dst = engine->u.rail.other_multiheaded_part;
}
break; break;
} }
@ -1052,8 +1046,7 @@ int32 CmdMoveRailVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2)
if (unit_num > _patches.max_trains) if (unit_num > _patches.max_trains)
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME); return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
if (flags & DC_EXEC) if (flags & DC_EXEC) src->unitnumber = unit_num;
src->unitnumber = unit_num;
} }
@ -1365,11 +1358,10 @@ int32 CmdSellRailWagon(int x, int y, uint32 flags, uint32 p1, uint32 p2)
} }
/* 3. If it is still a valid train after selling, update its acceleration and cached values */ /* 3. If it is still a valid train after selling, update its acceleration and cached values */
if ((flags & DC_EXEC) && first != NULL) { if (flags & DC_EXEC && first != NULL) {
NormaliseTrainConsist(first); NormaliseTrainConsist(first);
TrainConsistChanged(first); TrainConsistChanged(first);
if (IsFrontEngine(first)) if (IsFrontEngine(first)) UpdateTrainAcceleration(first);
UpdateTrainAcceleration(first);
InvalidateWindow(WC_VEHICLE_DETAILS, first->index); InvalidateWindow(WC_VEHICLE_DETAILS, first->index);
InvalidateWindow(WC_VEHICLE_REFIT, first->index); InvalidateWindow(WC_VEHICLE_REFIT, first->index);
} }
@ -1415,7 +1407,7 @@ static 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 != !spd) if (_patches.vehicle_speed || (old == 0) != (spd == 0))
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
} }
} }
@ -1702,16 +1694,23 @@ int32 CmdRefitRailVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2)
if (amount == CALLBACK_FAILED) { // callback failed or not used, use default if (amount == CALLBACK_FAILED) { // callback failed or not used, use default
CargoID old_cid = rvi->cargo_type; CargoID old_cid = rvi->cargo_type;
/* normally, the capacity depends on the cargo type, a rail vehicle /* normally, the capacity depends on the cargo type, a rail vehicle can
* can carry twice as much mail/goods as normal cargo, * carry twice as much mail/goods as normal cargo, and four times as
* and four times as much passengers */ * many passengers
*/
amount = rvi->capacity; amount = rvi->capacity;
(old_cid == CT_PASSENGERS) || switch (old_cid) {
(amount <<= 1, old_cid == CT_MAIL || old_cid == CT_GOODS) || case CT_PASSENGERS: break;
(amount <<= 1, true); case CT_MAIL:
(new_cid == CT_PASSENGERS) || case CT_GOODS: amount *= 2; break;
(amount >>= 1, new_cid == CT_MAIL || new_cid == CT_GOODS) || default: amount *= 4; break;
(amount >>= 1, true); }
switch (new_cid) {
case CT_PASSENGERS: break;
case CT_MAIL:
case CT_GOODS: amount /= 2; break;
default: amount /= 4; break;
}
}; };
if (amount != 0) { if (amount != 0) {
@ -1726,7 +1725,7 @@ int32 CmdRefitRailVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2)
} }
} }
} }
} while ( (v=v->next) != NULL ); } while ((v = v->next) != NULL);
_returned_refit_capacity = num; _returned_refit_capacity = num;
@ -1747,7 +1746,7 @@ typedef struct TrainFindDepotData {
static bool NtpCallbFindDepot(TileIndex tile, TrainFindDepotData *tfdd, int track, uint length) static bool NtpCallbFindDepot(TileIndex tile, TrainFindDepotData *tfdd, int track, uint length)
{ {
if (IsTileType(tile, MP_RAILWAY) && IsTileOwner(tile, tfdd->owner)) { if (IsTileType(tile, MP_RAILWAY) && IsTileOwner(tile, tfdd->owner)) {
if ((_m[tile].m5 & ~0x3) == 0xC0) { if ((_m[tile].m5 & 0xFC) == 0xC0) {
tfdd->best_length = length; tfdd->best_length = length;
tfdd->tile = tile; tfdd->tile = tile;
return true; return true;
@ -1795,8 +1794,7 @@ static TrainFindDepotData FindClosestTrainDepot(Vehicle *v)
* work for now :-) We can possibly change this when the old pathfinder * work for now :-) We can possibly change this when the old pathfinder
* is removed. */ * is removed. */
tfdd.best_length = ftd.best_path_dist / NPF_TILE_LENGTH; tfdd.best_length = ftd.best_path_dist / NPF_TILE_LENGTH;
if (NPFGetFlag(&ftd.node, NPF_FLAG_REVERSE)) if (NPFGetFlag(&ftd.node, NPF_FLAG_REVERSE)) tfdd.reverse = true;
tfdd.reverse = true;
} }
} else { } else {
// search in the forward direction first. // search in the forward direction first.
@ -1954,13 +1952,8 @@ static void TrainPlayLeaveStationSound(const Vehicle* v)
SndPlayVehicleFx(sfx[RailVehInfo(engtype)->engclass], v); SndPlayVehicleFx(sfx[RailVehInfo(engtype)->engclass], v);
break; break;
case RAILTYPE_MONO: case RAILTYPE_MONO: SndPlayVehicleFx(SND_47_MAGLEV_2, v); break;
SndPlayVehicleFx(SND_47_MAGLEV_2, v); case RAILTYPE_MAGLEV: SndPlayVehicleFx(SND_41_MAGLEV, v); break;
break;
case RAILTYPE_MAGLEV:
SndPlayVehicleFx(SND_41_MAGLEV, v);
break;
} }
} }
@ -2113,11 +2106,10 @@ static byte ChooseTrainTrack(Vehicle *v, TileIndex tile, int enterdir, TrackdirB
static float f; static float f;
#endif #endif
assert( (trackdirbits & ~0x3F) == 0); assert((trackdirbits & ~0x3F) == 0);
/* quick return in case only one possible track is available */ /* quick return in case only one possible track is available */
if (KILL_FIRST_BIT(trackdirbits) == 0) if (KILL_FIRST_BIT(trackdirbits) == 0) return FIND_FIRST_BIT(trackdirbits);
return FIND_FIRST_BIT(trackdirbits);
if (_patches.new_pathfinding_all) { /* Use a new pathfinding for everything */ if (_patches.new_pathfinding_all) { /* Use a new pathfinding for everything */
NPFFindStationOrTileData fstd; NPFFindStationOrTileData fstd;
@ -2214,10 +2206,11 @@ static bool CheckReverseTrain(Vehicle *v)
/* We didn't find anything, just keep on going straight ahead */ /* We didn't find anything, just keep on going straight ahead */
reverse_best = false; reverse_best = false;
} else { } else {
if (NPFGetFlag(&ftd.node, NPF_FLAG_REVERSE)) if (NPFGetFlag(&ftd.node, NPF_FLAG_REVERSE)) {
reverse_best = true; reverse_best = true;
else } else {
reverse_best = false; reverse_best = false;
}
} }
} else { } else {
for (;;) { for (;;) {
@ -2260,8 +2253,7 @@ good:;
best_track_dist = fd.best_track_dist; best_track_dist = fd.best_track_dist;
reverse_best = reverse; reverse_best = reverse;
bad:; bad:;
if (reverse != 0) if (reverse != 0) break;
break;
reverse = 2; reverse = 2;
} }
} }
@ -2455,7 +2447,8 @@ static void TrainEnterStation(Vehicle *v, StationID station)
STR_8801_CITIZENS_CELEBRATE_FIRST, STR_8801_CITIZENS_CELEBRATE_FIRST,
flags, flags,
v->index, v->index,
0); 0
);
} }
// Did we reach the final destination? // Did we reach the final destination?
@ -2678,8 +2671,7 @@ static void SetVehicleCrashed(Vehicle *v)
{ {
Vehicle *u; Vehicle *u;
if (v->u.rail.crash_anim_pos != 0) if (v->u.rail.crash_anim_pos != 0) return;
return;
v->u.rail.crash_anim_pos++; v->u.rail.crash_anim_pos++;
@ -2765,8 +2757,7 @@ static void *CheckVehicleAtSignal(Vehicle *v, void *data)
v->tile == vasd->tile) { v->tile == vasd->tile) {
byte diff = (v->direction - vasd->direction + 2) & 7; byte diff = (v->direction - vasd->direction + 2) & 7;
if (diff == 2 || (v->cur_speed <= 5 && diff <= 4)) if (diff == 2 || (v->cur_speed <= 5 && diff <= 4)) return v;
return v;
} }
return NULL; return NULL;
} }
@ -2796,8 +2787,7 @@ static void TrainController(Vehicle *v)
} else { } else {
/* is not inside depot */ /* is not inside depot */
if (!TrainCheckIfLineEnds(v)) if (!TrainCheckIfLineEnds(v)) return;
return;
r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y); r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
if (r & 0x8) { if (r & 0x8) {
@ -2831,14 +2821,15 @@ static void TrainController(Vehicle *v)
/* Combine the from & to directions. /* Combine the from & to directions.
* Now, the lower byte contains the track status, and the byte at bit 16 contains * Now, the lower byte contains the track status, and the byte at bit 16 contains
* the signal status. */ * the signal status. */
tracks = ts|(ts >> 8); tracks = ts | (ts >> 8);
bits = tracks & 0xFF; bits = tracks & 0xFF;
if (_patches.new_pathfinding_all && _patches.forbid_90_deg && prev == NULL) if (_patches.new_pathfinding_all && _patches.forbid_90_deg && prev == NULL) {
/* We allow wagons to make 90 deg turns, because forbid_90_deg /* We allow wagons to make 90 deg turns, because forbid_90_deg
* can be switched on halfway a turn */ * can be switched on halfway a turn */
bits &= ~TrackCrossesTracks(FIND_FIRST_BIT(v->u.rail.track)); bits &= ~TrackCrossesTracks(FIND_FIRST_BIT(v->u.rail.track));
}
if ( bits == 0) { if (bits == 0) {
//debug("%x == 0", bits); //debug("%x == 0", bits);
goto invalid_rail; goto invalid_rail;
} }
@ -2878,7 +2869,7 @@ static void TrainController(Vehicle *v)
/* Call the landscape function and tell it that the vehicle entered the tile */ /* Call the landscape function and tell it that the vehicle entered the tile */
r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y); r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
if (r&0x8){ if (r & 0x8) {
//debug("%x & 0x8", r); //debug("%x & 0x8", r);
goto invalid_rail; goto invalid_rail;
} }
@ -2891,17 +2882,14 @@ static void TrainController(Vehicle *v)
assert(v->u.rail.track); assert(v->u.rail.track);
} }
if (IsFrontEngine(v)) if (IsFrontEngine(v)) TrainMovedChangeSignals(gp.new_tile, enterdir);
TrainMovedChangeSignals(gp.new_tile, enterdir);
/* 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, (enterdir) ^ 2); TrainMovedChangeSignals(gp.old_tile, (enterdir) ^ 2);
if (prev == NULL) { if (prev == NULL) AffectSpeedByDirChange(v, chosen_dir);
AffectSpeedByDirChange(v, chosen_dir);
}
v->direction = chosen_dir; v->direction = chosen_dir;
} }
@ -2951,9 +2939,8 @@ red_light: {
if (!(_m[gp.new_tile].m3 & SignalAgainstTrackdir(i))) { if (!(_m[gp.new_tile].m3 & SignalAgainstTrackdir(i))) {
v->cur_speed = 0; v->cur_speed = 0;
v->subspeed = 0; v->subspeed = 0;
v->progress = 255-100; v->progress = 255 - 100;
if (++v->load_unload_time_rem < _patches.wait_oneway_signal * 20) if (++v->load_unload_time_rem < _patches.wait_oneway_signal * 20) return;
return;
} else if (_m[gp.new_tile].m3 & SignalAlongTrackdir(i)){ } else if (_m[gp.new_tile].m3 & SignalAlongTrackdir(i)){
v->cur_speed = 0; v->cur_speed = 0;
v->subspeed = 0; v->subspeed = 0;
@ -2965,8 +2952,7 @@ red_light: {
vasd.direction = dir ^ 4; vasd.direction = dir ^ 4;
/* check if a train is waiting on the other side */ /* check if a train is waiting on the other side */
if (VehicleFromPos(o_tile, &vasd, CheckVehicleAtSignal) == NULL) if (VehicleFromPos(o_tile, &vasd, CheckVehicleAtSignal) == NULL) return;
return;
} }
} }
} }
@ -3016,8 +3002,7 @@ static void DeleteLastWagon(Vehicle *v)
if (v->u.rail.track == 0x40) { // inside a tunnel if (v->u.rail.track == 0x40) { // inside a tunnel
TileIndex endtile = CheckTunnelBusy(v->tile, NULL); TileIndex endtile = CheckTunnelBusy(v->tile, NULL);
if (endtile == INVALID_TILE) // tunnel is busy (error returned) if (endtile == INVALID_TILE) return; // tunnel is busy (error returned)
return;
switch (v->direction) { switch (v->direction) {
case 1: case 1:
@ -3040,7 +3025,7 @@ static void DeleteLastWagon(Vehicle *v)
static void ChangeTrainDirRandomly(Vehicle *v) static void ChangeTrainDirRandomly(Vehicle *v)
{ {
static int8 _random_dir_change[4] = { -1, 0, 0, 1}; static const int8 _random_dir_change[4] = { -1, 0, 0, 1 };
do { do {
//I need to buffer the train direction //I need to buffer the train direction
@ -3052,12 +3037,12 @@ static void ChangeTrainDirRandomly(Vehicle *v)
v->cur_image = GetTrainImage(v, v->direction); v->cur_image = GetTrainImage(v, v->direction);
AfterSetTrainPos(v, false); AfterSetTrainPos(v, false);
} }
} while ( (v=v->next) != NULL); } while ((v = v->next) != NULL);
} }
static void HandleCrashedTrain(Vehicle *v) static void HandleCrashedTrain(Vehicle *v)
{ {
int state = ++v->u.rail.crash_anim_pos, index; int state = ++v->u.rail.crash_anim_pos;
uint32 r; uint32 r;
Vehicle *u; Vehicle *u;
@ -3066,7 +3051,7 @@ static void HandleCrashedTrain(Vehicle *v)
} }
if (state <= 200 && CHANCE16R(1, 7, r)) { if (state <= 200 && CHANCE16R(1, 7, r)) {
index = (r * 10 >> 16); int index = (r * 10 >> 16);
u = v; u = v;
do { do {
@ -3080,12 +3065,10 @@ static void HandleCrashedTrain(Vehicle *v)
EV_EXPLOSION_SMALL); EV_EXPLOSION_SMALL);
break; break;
} }
} while ( (u=u->next) != NULL); } while ((u = u->next) != NULL);
} }
if (state <= 240 && !(v->tick_counter&3)) { if (state <= 240 && !(v->tick_counter & 3)) ChangeTrainDirRandomly(v);
ChangeTrainDirRandomly(v);
}
if (state >= 4440 && !(v->tick_counter&0x1F)) { if (state >= 4440 && !(v->tick_counter&0x1F)) {
DeleteLastWagon(v); DeleteLastWagon(v);
@ -3174,27 +3157,13 @@ static bool TrainCheckIfLineEnds(Vehicle *v)
y = v->y_pos & 0xF; y = v->y_pos & 0xF;
switch (v->direction) { switch (v->direction) {
case 0: case 0: x = ~x + ~y + 24; break;
x = (~x) + (~y) + 24; case 7: x = y; /* FALLTHROUGH */
break; case 1: x = ~x + 16; break;
case 7: case 2: x = ~x + y + 8; break;
x = y; case 3: x = y; break;
/* fall through */ case 4: x = x + y - 8; break;
case 1: case 6: x = ~y + x + 8; break;
x = (~x) + 16;
break;
case 2:
x = (~x) + y + 8;
break;
case 3:
x = y;
break;
case 4:
x = x + y - 8;
break;
case 6:
x = (~y) + x + 8;
break;
} }
if (GB(ts, 0, 16) != 0) { if (GB(ts, 0, 16) != 0) {
@ -3227,7 +3196,7 @@ static bool TrainCheckIfLineEnds(Vehicle *v)
// slow down // slow down
v->vehstatus |= VS_TRAIN_SLOWING; v->vehstatus |= VS_TRAIN_SLOWING;
break_speed = _breakdown_speeds[x & 0xF]; break_speed = _breakdown_speeds[x & 0xF];
if (!(v->direction&1)) break_speed >>= 1; if (!(v->direction & 1)) break_speed >>= 1;
if (break_speed < v->cur_speed) v->cur_speed = break_speed; if (break_speed < v->cur_speed) v->cur_speed = break_speed;
return true; return true;
@ -3243,8 +3212,7 @@ static void TrainLocoHandler(Vehicle *v, bool mode)
return; return;
} }
if (v->u.rail.force_proceed != 0) if (v->u.rail.force_proceed != 0) v->u.rail.force_proceed--;
v->u.rail.force_proceed--;
/* train is broken down? */ /* train is broken down? */
if (v->breakdown_ctr != 0) { if (v->breakdown_ctr != 0) {
@ -3260,9 +3228,7 @@ static void TrainLocoHandler(Vehicle *v, bool mode)
} }
/* exit if train is stopped */ /* exit if train is stopped */
if (v->vehstatus & VS_STOPPED && v->cur_speed == 0) if (v->vehstatus & VS_STOPPED && v->cur_speed == 0) return;
return;
if (ProcessTrainOrder(v)) { if (ProcessTrainOrder(v)) {
v->load_unload_time_rem = 0; v->load_unload_time_rem = 0;
@ -3274,19 +3240,16 @@ static void TrainLocoHandler(Vehicle *v, bool mode)
HandleTrainLoading(v, mode); HandleTrainLoading(v, mode);
if (v->current_order.type == OT_LOADING) if (v->current_order.type == OT_LOADING) return;
return;
if (CheckTrainStayInDepot(v)) if (CheckTrainStayInDepot(v)) return;
return;
if (!mode) HandleLocomotiveSmokeCloud(v); if (!mode) HandleLocomotiveSmokeCloud(v);
j = UpdateTrainSpeed(v); j = UpdateTrainSpeed(v);
if (j == 0) { if (j == 0) {
// if the vehicle has speed 0, update the last_speed field. // if the vehicle has speed 0, update the last_speed field.
if (v->cur_speed != 0) if (v->cur_speed != 0) return;
return;
} else { } else {
TrainCheckIfLineEnds(v); TrainCheckIfLineEnds(v);
@ -3409,8 +3372,9 @@ static void CheckIfTrainNeedsService(Vehicle *v)
if (v->current_order.type == OT_GOTO_DEPOT && if (v->current_order.type == OT_GOTO_DEPOT &&
v->current_order.station != depot->index && v->current_order.station != depot->index &&
!CHANCE16(3,16)) !CHANCE16(3, 16)) {
return; return;
}
v->current_order.type = OT_GOTO_DEPOT; v->current_order.type = OT_GOTO_DEPOT;
v->current_order.flags = OF_NON_STOP; v->current_order.flags = OF_NON_STOP;
@ -3436,8 +3400,7 @@ void OnNewDay_Train(Vehicle *v)
{ {
TileIndex tile; TileIndex tile;
if ((++v->day_counter & 7) == 0) if ((++v->day_counter & 7) == 0) DecreaseVehicleValue(v);
DecreaseVehicleValue(v);
if (IsFrontEngine(v)) { if (IsFrontEngine(v)) {
CheckVehicleBreakdown(v); CheckVehicleBreakdown(v);
@ -3582,46 +3545,46 @@ void ConvertOldMultiheadToNew(void)
if (HASBIT(v->subtype, 7) && ((v->subtype & ~0x80) == 0 || (v->subtype & ~0x80) == 4)) { if (HASBIT(v->subtype, 7) && ((v->subtype & ~0x80) == 0 || (v->subtype & ~0x80) == 4)) {
Vehicle *u = v; Vehicle *u = v;
BEGIN_ENUM_WAGONS(u) BEGIN_ENUM_WAGONS(u) {
const RailVehicleInfo *rvi = RailVehInfo(u->engine_type); const RailVehicleInfo *rvi = RailVehInfo(u->engine_type);
CLRBIT(u->subtype, 7);
switch (u->subtype) { CLRBIT(u->subtype, 7);
case 0: /* TS_Front_Engine */ switch (u->subtype) {
if (rvi->flags & RVI_MULTIHEAD) { case 0: /* TS_Front_Engine */
SetMultiheaded(u); if (rvi->flags & RVI_MULTIHEAD) SetMultiheaded(u);
} SetFrontEngine(u);
SetFrontEngine(u); SetTrainEngine(u);
SetTrainEngine(u);
break;
case 1: /* TS_Artic_Part */
u->subtype = 0;
SetArticulatedPart(u);
break;
case 2: /* TS_Not_First */
u->subtype = 0;
if (rvi->flags & RVI_WAGON) {
// normal wagon
SetTrainWagon(u);
break; break;
}
case 1: /* TS_Artic_Part */
u->subtype = 0;
SetArticulatedPart(u);
break;
case 2: /* TS_Not_First */
u->subtype = 0;
if (rvi->flags & RVI_WAGON) {
// normal wagon
SetTrainWagon(u);
break;
}
if (rvi->flags & RVI_MULTIHEAD && rvi->image_index == u->spritenum - 1) { if (rvi->flags & RVI_MULTIHEAD && rvi->image_index == u->spritenum - 1) {
// rear end of a multiheaded engine // rear end of a multiheaded engine
SetMultiheaded(u); SetMultiheaded(u);
break; break;
} }
if (rvi->flags & RVI_MULTIHEAD) { if (rvi->flags & RVI_MULTIHEAD) SetMultiheaded(u);
SetMultiheaded(u);
}
SetTrainEngine(u); SetTrainEngine(u);
break; break;
case 4: /* TS_Free_Car */
u->subtype = 0; case 4: /* TS_Free_Car */
SetTrainWagon(u); u->subtype = 0;
SetFreeWagon(u); SetTrainWagon(u);
break; SetFreeWagon(u);
default: NOT_REACHED(); break; break;
} default: NOT_REACHED(); break;
END_ENUM_WAGONS(u) }
} END_ENUM_WAGONS(u)
} }
} }
} }

View File

@ -538,7 +538,7 @@ void OnTick_Trees(void)
uint32 r; uint32 r;
TileIndex tile; TileIndex tile;
ClearGround ct; ClearGround ct;
int tree; TreeType tree;
/* place a tree at a random rainforest spot */ /* place a tree at a random rainforest spot */
if (_opt.landscape == LT_DESERT && if (_opt.landscape == LT_DESERT &&

View File

@ -471,22 +471,22 @@ static int32 DoBuildTunnel(int x, int y, int x2, int y2, uint32 flags, uint exc_
/* check if valid, and make sure that (x,y) is smaller than (x2,y2) */ /* check if valid, and make sure that (x,y) is smaller than (x2,y2) */
direction = 0; direction = 0;
if (x == x2) { if (x == x2) {
if (y == y2) if (y == y2) return_cmd_error(STR_5008_CANNOT_START_AND_END_ON);
return_cmd_error(STR_5008_CANNOT_START_AND_END_ON);
direction++; direction++;
if (y > y2) { if (y > y2) {
intswap(y,y2); intswap(y,y2);
intswap(x,x2); intswap(x,x2);
exc_tile|=2; exc_tile |= 2;
} }
} else if (y == y2) { } else if (y == y2) {
if (x > x2) { if (x > x2) {
intswap(y,y2); intswap(y,y2);
intswap(x,x2); intswap(x,x2);
exc_tile|=2; exc_tile |= 2;
} }
} else } else {
return_cmd_error(STR_500A_START_AND_END_MUST_BE_IN); return_cmd_error(STR_500A_START_AND_END_MUST_BE_IN);
}
cost = 0; cost = 0;
@ -829,7 +829,6 @@ clear_it:;
SetSignalsOnBothDir(tile, direction); SetSignalsOnBothDir(tile, direction);
SetSignalsOnBothDir(endtile, direction); SetSignalsOnBothDir(endtile, direction);
} }
if (direction) { if (direction) {
@ -845,11 +844,9 @@ static int32 ClearTile_TunnelBridge(TileIndex tile, byte flags)
if ((m5 & 0xF0) == 0) { if ((m5 & 0xF0) == 0) {
if (flags & DC_AUTO) return_cmd_error(STR_5006_MUST_DEMOLISH_TUNNEL_FIRST); if (flags & DC_AUTO) return_cmd_error(STR_5006_MUST_DEMOLISH_TUNNEL_FIRST);
return DoClearTunnel(tile, flags); return DoClearTunnel(tile, flags);
} else if (m5 & 0x80) { } else if (m5 & 0x80) {
if (flags & DC_AUTO) return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST); if (flags & DC_AUTO) return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST);
return DoClearBridge(tile, flags); return DoClearBridge(tile, flags);
} }
@ -1023,13 +1020,20 @@ uint GetBridgeFoundation(uint tileh, byte direction)
{ {
int i; int i;
// normal level sloped building (7, 11, 13, 14) // normal level sloped building (7, 11, 13, 14)
if (BRIDGE_FULL_LEVELED_FOUNDATION & (1 << tileh)) if (BRIDGE_FULL_LEVELED_FOUNDATION & (1 << tileh)) return tileh;
return tileh;
// inclined sloped building // inclined sloped building
if ( ((i=0, tileh == 1) || (i+=2, tileh == 2) || (i+=2, tileh == 4) || (i+=2, tileh == 8)) && if ((
( direction == 0 || (i++, direction == 1)) ) (i = 0, tileh == 1) ||
(i += 2, tileh == 2) ||
(i += 2, tileh == 4) ||
(i += 2, tileh == 8)
) && (
direction == 0 ||
(i++, direction == 1)
)) {
return i + 15; return i + 15;
}
return 0; return 0;
} }
@ -1132,10 +1136,11 @@ static void DrawTile_TunnelBridge(TileInfo *ti)
// draw land under bridge // draw land under bridge
if (ice) image += 2; if (ice) image += 2;
if (image != 1 || ti->tileh == 0) if (image != 1 || ti->tileh == 0) {
DrawGroundSprite(_bridge_land_below[image] + _tileh_to_sprite[ti->tileh]); DrawGroundSprite(_bridge_land_below[image] + _tileh_to_sprite[ti->tileh]);
else } else {
DrawGroundSprite(_water_shore_sprites[ti->tileh]); DrawGroundSprite(_water_shore_sprites[ti->tileh]);
}
// draw canal water? // draw canal water?
if (ti->map5 & 8 && ti->z != 0) DrawCanalWater(ti->tile); if (ti->map5 & 8 && ti->z != 0) DrawCanalWater(ti->tile);
@ -1185,7 +1190,7 @@ static void DrawTile_TunnelBridge(TileInfo *ti)
if (_display_opt & DO_TRANS_BUILDINGS) MAKE_TRANSPARENT(image); if (_display_opt & DO_TRANS_BUILDINGS) MAKE_TRANSPARENT(image);
// draw roof, the component of the bridge which is logically between the vehicle and the camera // draw roof, the component of the bridge which is logically between the vehicle and the camera
if (ti->map5&1) { if (ti->map5 & 1) {
x += 12; x += 12;
if (image & SPRITE_MASK) AddSortableSpriteToDraw(image, x,y, 1, 16, 0x28, z); if (image & SPRITE_MASK) AddSortableSpriteToDraw(image, x,y, 1, 16, 0x28, z);
} else { } else {
@ -1193,10 +1198,10 @@ static void DrawTile_TunnelBridge(TileInfo *ti)
if (image & SPRITE_MASK) AddSortableSpriteToDraw(image, x,y, 16, 1, 0x28, z); if (image & SPRITE_MASK) AddSortableSpriteToDraw(image, x,y, 16, 1, 0x28, z);
} }
if (ti->z + 5 == z ) { if (ti->z + 5 == z) {
// draw poles below for small bridges // draw poles below for small bridges
image = b[2]; image = b[2];
if (image) { if (image != 0) {
if (_display_opt & DO_TRANS_BUILDINGS) MAKE_TRANSPARENT(image); if (_display_opt & DO_TRANS_BUILDINGS) MAKE_TRANSPARENT(image);
DrawGroundSpriteAt(image, x, y, z); DrawGroundSpriteAt(image, x, y, z);
} }
@ -1235,10 +1240,10 @@ static uint GetSlopeZ_TunnelBridge(const TileInfo* ti)
return z + 8; return z + 8;
} else if (!(ti->map5 & 0x20)) { // northern / southern ending } else if (!(ti->map5 & 0x20)) { // northern / southern ending
// ramp // ramp
return (z + (x>>1) + 1); return z + (x >> 1) + 1;
} else { } else {
// ramp in opposite dir // ramp in opposite dir
return (z + ((x^0xF)>>1)); return z + ((x ^ 0xF) >> 1);
} }
// bridge middle part // bridge middle part
@ -1351,23 +1356,27 @@ static void AnimateTile_TunnelBridge(TileIndex tile)
static void TileLoop_TunnelBridge(TileIndex tile) static void TileLoop_TunnelBridge(TileIndex tile)
{ {
if (_opt.landscape == LT_HILLY) { switch (_opt.landscape) {
if (GetTileZ(tile) > _opt.snow_line) { case LT_HILLY:
if (!(_m[tile].m4 & 0x80)) { if (GetTileZ(tile) > _opt.snow_line) {
if (!(_m[tile].m4 & 0x80)) {
_m[tile].m4 |= 0x80;
MarkTileDirtyByTile(tile);
}
} else {
if (_m[tile].m4 & 0x80) {
_m[tile].m4 &= ~0x80;
MarkTileDirtyByTile(tile);
}
}
break;
case LT_DESERT:
if (GetMapExtraBits(tile) == 1 && !(_m[tile].m4 & 0x80)) {
_m[tile].m4 |= 0x80; _m[tile].m4 |= 0x80;
MarkTileDirtyByTile(tile); MarkTileDirtyByTile(tile);
} }
} else { break;
if (_m[tile].m4 & 0x80) {
_m[tile].m4 &= ~0x80;
MarkTileDirtyByTile(tile);
}
}
} else if (_opt.landscape == LT_DESERT) {
if (GetMapExtraBits(tile) == 1 && !(_m[tile].m4&0x80)) {
_m[tile].m4 |= 0x80;
MarkTileDirtyByTile(tile);
}
} }
// if it's a bridge with water below, call tileloop_water on it. // if it's a bridge with water below, call tileloop_water on it.
@ -1537,14 +1546,14 @@ static uint32 VehicleEnter_TunnelBridge(Vehicle *v, TileIndex tile, int x, int y
if (v->type == VEH_Road || (v->type == VEH_Train && IsFrontEngine(v))) { if (v->type == VEH_Road || (v->type == VEH_Train && IsFrontEngine(v))) {
uint h; uint h;
if (GetTileSlope(tile, &h) != 0) // Compensate for possible foundation
h += 8; // Compensate for possible foundation if (GetTileSlope(tile, &h) != 0) h += 8;
if (!(_m[tile].m5 & 0x40) || // start/end tile of bridge if (!(_m[tile].m5 & 0x40) || // start/end tile of bridge
myabs(h - v->z_pos) > 2) { // high above the ground -> on the bridge myabs(h - v->z_pos) > 2) { // high above the ground -> on the bridge
/* modify speed of vehicle */ /* modify speed of vehicle */
uint16 spd = _bridge[GetBridgeType(tile)].speed; uint16 spd = _bridge[GetBridgeType(tile)].speed;
if (v->type == VEH_Road) spd *= 2; if (v->type == VEH_Road) spd *= 2;
if (spd < v->cur_speed) v->cur_speed = spd; if (v->cur_speed > spd) v->cur_speed = spd;
} }
} }
} }

View File

@ -51,7 +51,7 @@ static int32 DestroyCompanyHQ(TileIndex tile, uint32 flags)
DoClearSquare(p->location_of_house + TileDiffXY(1, 0)); DoClearSquare(p->location_of_house + TileDiffXY(1, 0));
DoClearSquare(p->location_of_house + TileDiffXY(1, 1)); DoClearSquare(p->location_of_house + TileDiffXY(1, 1));
p->location_of_house = 0; // reset HQ position p->location_of_house = 0; // reset HQ position
InvalidateWindow(WC_COMPANY, (int)p->index); InvalidateWindow(WC_COMPANY, p->index);
} }
// cost of relocating company is 1% of company value // cost of relocating company is 1% of company value
@ -91,7 +91,7 @@ int32 CmdBuildCompanyHQ(int x, int y, uint32 flags, uint32 p1, uint32 p2)
ModifyTile(tile + TileDiffXY(1, 0), MP_SETTYPE(MP_UNMOVABLE) | MP_MAPOWNER_CURRENT | MP_MAP5, 0x82); ModifyTile(tile + TileDiffXY(1, 0), MP_SETTYPE(MP_UNMOVABLE) | MP_MAPOWNER_CURRENT | MP_MAP5, 0x82);
ModifyTile(tile + TileDiffXY(1, 1), MP_SETTYPE(MP_UNMOVABLE) | MP_MAPOWNER_CURRENT | MP_MAP5, 0x83); ModifyTile(tile + TileDiffXY(1, 1), MP_SETTYPE(MP_UNMOVABLE) | MP_MAPOWNER_CURRENT | MP_MAP5, 0x83);
UpdatePlayerHouse(p, score); UpdatePlayerHouse(p, score);
InvalidateWindow(WC_COMPANY, (int)p->index); InvalidateWindow(WC_COMPANY, p->index);
} }
return cost; return cost;
@ -346,8 +346,8 @@ void GenerateUnmovables(void)
if (IsTileType(tile, MP_CLEAR) && GetTileSlope(tile, &h) == 0 && h >= 32) { if (IsTileType(tile, MP_CLEAR) && GetTileSlope(tile, &h) == 0 && h >= 32) {
if (!checkRadioTowerNearby(tile)) continue; if (!checkRadioTowerNearby(tile)) continue;
SetTileType(tile, MP_UNMOVABLE); SetTileType(tile, MP_UNMOVABLE);
_m[tile].m5 = 0;
SetTileOwner(tile, OWNER_NONE); SetTileOwner(tile, OWNER_NONE);
_m[tile].m5 = 0;
if (--j == 0) break; if (--j == 0) break;
} }
} while (--i); } while (--i);
@ -375,8 +375,8 @@ restart:
assert(tile == TILE_MASK(tile)); assert(tile == TILE_MASK(tile));
SetTileType(tile, MP_UNMOVABLE); SetTileType(tile, MP_UNMOVABLE);
_m[tile].m5 = 1;
SetTileOwner(tile, OWNER_NONE); SetTileOwner(tile, OWNER_NONE);
_m[tile].m5 = 1;
} while (--i); } while (--i);
} }

View File

@ -274,7 +274,6 @@ int32 CmdBuildCanal(int x, int y, uint32 flags, uint32 p1, uint32 p2)
static int32 ClearTile_Water(TileIndex tile, byte flags) static int32 ClearTile_Water(TileIndex tile, byte flags)
{ {
byte m5 = _m[tile].m5; byte m5 = _m[tile].m5;
uint slope;
if (m5 <= 1) { // water and shore if (m5 <= 1) { // water and shore
// Allow building on water? It's ok to build on shores. // Allow building on water? It's ok to build on shores.
@ -285,25 +284,26 @@ static int32 ClearTile_Water(TileIndex tile, byte flags)
if (!EnsureNoVehicle(tile)) return CMD_ERROR; if (!EnsureNoVehicle(tile)) return CMD_ERROR;
// Make sure it's not an edge tile. // Make sure it's not an edge tile.
if (!(IS_INT_INSIDE(TileX(tile), 1, MapMaxX() - 1) && if (!IS_INT_INSIDE(TileX(tile), 1, MapMaxX() - 1) ||
IS_INT_INSIDE(TileY(tile), 1, MapMaxY() - 1))) !IS_INT_INSIDE(TileY(tile), 1, MapMaxY() - 1)) {
return_cmd_error(STR_0002_TOO_CLOSE_TO_EDGE_OF_MAP); return_cmd_error(STR_0002_TOO_CLOSE_TO_EDGE_OF_MAP);
}
if (m5 == 0) { if (m5 == 0) {
if (flags & DC_EXEC) DoClearSquare(tile); if (flags & DC_EXEC) DoClearSquare(tile);
return _price.clear_water; return _price.clear_water;
} else if (m5 == 1) { } else if (m5 == 1) {
slope = GetTileSlope(tile,NULL); uint slope = GetTileSlope(tile,NULL);
if (flags & DC_EXEC) DoClearSquare(tile);
if (slope == 8 || slope == 4 || slope == 2 || slope == 1) { if (slope == 8 || slope == 4 || slope == 2 || slope == 1) {
if (flags & DC_EXEC)
DoClearSquare(tile);
return _price.clear_water; return _price.clear_water;
} else {
return _price.purchase_land;
} }
if (flags & DC_EXEC) } else {
DoClearSquare(tile);
return _price.purchase_land;
} else
return CMD_ERROR; return CMD_ERROR;
}
} else if ((m5 & 0x10) == 0x10) { } else if ((m5 & 0x10) == 0x10) {
// shiplift // shiplift