1
0
Fork 0

(svn r3649) Turn several if-cascades into switch()es

release/0.5
tron 2006-02-21 18:07:35 +00:00
parent e3f2007da9
commit 6bf65d401b
1 changed files with 299 additions and 251 deletions

View File

@ -34,13 +34,11 @@ static bool HasTileRoadAt(TileIndex tile, int i)
case MP_STREET: case MP_STREET:
b = _m[tile].m5; b = _m[tile].m5;
if ((b & 0xF0) == 0) { switch (b & 0xF0) {
} else if (IsLevelCrossing(tile)) { case 0: break; // normal road
b = (b&8)?5:10; case 1: b = (b & 8 ? 5 : 10); break; // level crossing
} else if ((b & 0xF0) == 0x20) { case 2: return (~b & 3) == i; // depot
return (~b & 3) == i; default: return false;
} else {
return false;
} }
break; break;
@ -177,16 +175,17 @@ int32 CmdRemoveRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2)
if (!b) return CMD_ERROR; if (!b) return CMD_ERROR;
} }
if (ti.type == MP_TUNNELBRIDGE) { switch (ti.type) {
if (!EnsureNoVehicleZ(tile, TilePixelHeight(tile))) case MP_TUNNELBRIDGE:
return CMD_ERROR; if (!EnsureNoVehicleZ(tile, TilePixelHeight(tile))) return CMD_ERROR;
if ((ti.map5 & 0xE9) == 0xE8) { if ((ti.map5 & 0xE9) == 0xE8) {
if (pieces & 10) goto return_error; if (pieces & 10) goto return_error;
} else if ((ti.map5 & 0xE9) == 0xE9) { } else if ((ti.map5 & 0xE9) == 0xE9) {
if (pieces & 5) goto return_error; if (pieces & 5) goto return_error;
} else } else {
goto return_error; goto return_error;
}
cost = _price.remove_road * 2; cost = _price.remove_road * 2;
@ -197,13 +196,14 @@ int32 CmdRemoveRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2)
MarkTileDirtyByTile(tile); MarkTileDirtyByTile(tile);
} }
return cost; return cost;
} else if (ti.type == MP_STREET) {
case MP_STREET:
// check if you're allowed to remove the street owned by a town // check if you're allowed to remove the street owned by a town
// removal allowance depends on difficulty setting // removal allowance depends on difficulty setting
if (!CheckforTownRating(flags, t, ROAD_REMOVE)) return CMD_ERROR; if (!CheckforTownRating(flags, t, ROAD_REMOVE)) return CMD_ERROR;
// XXX - change cascading ifs to switch when doing rewrite switch (GB(ti.map5, 4, 4)) {
if ((ti.map5 & 0xF0) == 0) { // normal road case 0: { // normal road
byte c = pieces, t2; byte c = pieces, t2;
if (ti.tileh != 0 && (ti.map5 == 5 || ti.map5 == 10)) { if (ti.tileh != 0 && (ti.map5 == 5 || ti.map5 == 10)) {
@ -232,7 +232,9 @@ int32 CmdRemoveRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2)
} }
} }
return cost; return cost;
} else if ((ti.map5 & 0xE0) == 0) { // railroad crossing }
case 1: { // level crossing
byte c; byte c;
if (!(ti.map5 & 8)) { if (!(ti.map5 & 8)) {
@ -255,10 +257,13 @@ int32 CmdRemoveRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2)
); );
} }
return cost; return cost;
} else }
goto return_error;
} else { default: // depot
goto return_error;
}
default:
return_error:; return_error:;
return_cmd_error(INVALID_STRING_ID); return_cmd_error(INVALID_STRING_ID);
} }
@ -366,7 +371,8 @@ int32 CmdBuildRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2)
// allow building road under bridge // allow building road under bridge
if (ti.type != MP_TUNNELBRIDGE && !EnsureNoVehicle(tile)) return CMD_ERROR; if (ti.type != MP_TUNNELBRIDGE && !EnsureNoVehicle(tile)) return CMD_ERROR;
if (ti.type == MP_STREET) { switch (ti.type) {
case MP_STREET:
if (!(ti.map5 & 0xF0)) { if (!(ti.map5 & 0xF0)) {
if ((pieces & (byte)ti.map5) == pieces) if ((pieces & (byte)ti.map5) == pieces)
return_cmd_error(STR_1007_ALREADY_BUILT); return_cmd_error(STR_1007_ALREADY_BUILT);
@ -376,7 +382,9 @@ int32 CmdBuildRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2)
return_cmd_error(STR_1007_ALREADY_BUILT); return_cmd_error(STR_1007_ALREADY_BUILT);
goto do_clear; goto do_clear;
} }
} else if (ti.type == MP_RAILWAY) { break;
case MP_RAILWAY: {
byte m5; byte m5;
if (IsSteepTileh(ti.tileh)) { // very steep tile if (IsSteepTileh(ti.tileh)) { // very steep tile
@ -408,7 +416,9 @@ 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) { }
case 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);
@ -438,7 +448,8 @@ int32 CmdBuildRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2)
); );
} }
return cost; return cost;
} else {
default:
do_clear:; do_clear:;
if (CmdFailed(DoCommandByTile(tile, 0, 0, flags & ~DC_EXEC, CMD_LANDSCAPE_CLEAR))) if (CmdFailed(DoCommandByTile(tile, 0, 0, flags & ~DC_EXEC, CMD_LANDSCAPE_CLEAR)))
return CMD_ERROR; return CMD_ERROR;
@ -680,32 +691,40 @@ static int32 RemoveRoadDepot(TileIndex tile, uint32 flags)
static int32 ClearTile_Road(TileIndex tile, byte flags) static int32 ClearTile_Road(TileIndex tile, byte flags)
{ {
int32 ret;
byte m5 = _m[tile].m5; byte m5 = _m[tile].m5;
if ( (m5 & 0xF0) == 0) { switch (GB(_m[tile].m5, 4, 4)) {
case 0: { // normal road
byte b = m5 & 0xF; byte b = m5 & 0xF;
if (! ((1 << b) & (M(1)|M(2)|M(4)|M(8))) ) { if (!((1 << b) & (M(1)|M(2)|M(4)|M(8))) &&
if ((!(flags & DC_AI_BUILDING) || !IsTileOwner(tile, OWNER_TOWN)) && flags & DC_AUTO) (!(flags & DC_AI_BUILDING) || !IsTileOwner(tile, OWNER_TOWN)) &&
flags & DC_AUTO) {
return_cmd_error(STR_1801_MUST_REMOVE_ROAD_FIRST); return_cmd_error(STR_1801_MUST_REMOVE_ROAD_FIRST);
} }
return DoCommandByTile(tile, b, 0, flags, CMD_REMOVE_ROAD); return DoCommandByTile(tile, b, 0, flags, CMD_REMOVE_ROAD);
} else if ( (m5 & 0xE0) == 0) { }
if (flags & DC_AUTO)
return_cmd_error(STR_1801_MUST_REMOVE_ROAD_FIRST);
ret = DoCommandByTile(tile, (m5&8)?5:10, 0, flags, CMD_REMOVE_ROAD); case 1: { // level crossing
int32 ret;
if (flags & DC_AUTO) {
return_cmd_error(STR_1801_MUST_REMOVE_ROAD_FIRST);
}
ret = DoCommandByTile(tile, (m5 & 8 ? 5 : 10), 0, flags, CMD_REMOVE_ROAD);
if (CmdFailed(ret)) return CMD_ERROR; if (CmdFailed(ret)) return CMD_ERROR;
if (flags & DC_EXEC) { if (flags & DC_EXEC) {
DoCommandByTile(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR); DoCommandByTile(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
} }
return ret; return ret;
} else { }
if (flags & DC_AUTO)
default: // depot
if (flags & DC_AUTO) {
return_cmd_error(STR_2004_BUILDING_MUST_BE_DEMOLISHED); return_cmd_error(STR_2004_BUILDING_MUST_BE_DEMOLISHED);
}
return RemoveRoadDepot(tile, flags); return RemoveRoadDepot(tile, flags);
} }
} }
@ -825,9 +844,12 @@ static void DrawTile_Road(TileInfo *ti)
PalSpriteID image; PalSpriteID image;
uint16 m2; uint16 m2;
if ( (ti->map5 & 0xF0) == 0) { // if it is a road the upper 4 bits are 0 switch (GB(ti->map5, 4, 4)) {
case 0: // normal road
DrawRoadBits(ti, GB(ti->map5, 0, 4), GB(_m[ti->tile].m4, 4, 3), HASBIT(_m[ti->tile].m4, 7), false); DrawRoadBits(ti, GB(ti->map5, 0, 4), GB(_m[ti->tile].m4, 4, 3), HASBIT(_m[ti->tile].m4, 7), false);
} else if ( (ti->map5 & 0xE0) == 0) { // railroad crossing break;
case 1: { // level crossing
int f = GetRoadFoundation(ti->tileh, ti->map5 & 0xF); int f = GetRoadFoundation(ti->tileh, ti->map5 & 0xF);
if (f) DrawFoundation(ti, f); if (f) DrawFoundation(ti, f);
@ -835,8 +857,7 @@ static void DrawTile_Road(TileInfo *ti)
if (GB(ti->map5, 3, 1) == 0) image++; /* direction */ if (GB(ti->map5, 3, 1) == 0) image++; /* direction */
if ( (ti->map5 & 4) != 0) if ((ti->map5 & 4) != 0) image += 2;
image += 2;
if ( _m[ti->tile].m4 & 0x80) { if ( _m[ti->tile].m4 & 0x80) {
image += 8; image += 8;
@ -847,17 +868,19 @@ static void DrawTile_Road(TileInfo *ti)
} }
DrawGroundSprite(image); DrawGroundSprite(image);
} else { break;
}
default: { // depot
uint32 ormod; uint32 ormod;
PlayerID player; PlayerID player;
const DrawRoadSeqStruct* drss; const DrawRoadSeqStruct* drss;
if (ti->tileh != 0) { DrawFoundation(ti, ti->tileh); } if (ti->tileh != 0) DrawFoundation(ti, ti->tileh);
ormod = PALETTE_TO_GREY; //was this a bug/problem? ormod = PALETTE_TO_GREY; //was this a bug/problem?
player = GetTileOwner(ti->tile); player = GetTileOwner(ti->tile);
if (player < MAX_PLAYERS) if (player < MAX_PLAYERS) ormod = PLAYER_SPRITE_COLOR(player);
ormod = PLAYER_SPRITE_COLOR(player);
drss = _road_display_datas[ti->map5 & 0xF]; drss = _road_display_datas[ti->map5 & 0xF];
@ -866,13 +889,14 @@ static void DrawTile_Road(TileInfo *ti)
for (; drss->image != 0; drss++) { for (; drss->image != 0; drss++) {
uint32 image = drss->image; uint32 image = drss->image;
if (image & PALETTE_MODIFIER_COLOR) if (image & PALETTE_MODIFIER_COLOR) image |= ormod;
image |= ormod; if (_display_opt & DO_TRANS_BUILDINGS) MAKE_TRANSPARENT(image);
if (_display_opt & DO_TRANS_BUILDINGS) // show transparent depots
MAKE_TRANSPARENT(image);
AddSortableSpriteToDraw(image, ti->x | drss->subcoord_x, AddSortableSpriteToDraw(image, ti->x | drss->subcoord_x,
ti->y | drss->subcoord_y, drss->width, drss->height, 0x14, ti->z); ti->y | drss->subcoord_y, drss->width, drss->height, 0x14, ti->z
);
}
break;
} }
} }
} }
@ -908,7 +932,9 @@ static uint GetSlopeZ_Road(const TileInfo* ti)
// check if it's a foundation // check if it's a foundation
if (ti->tileh != 0) { if (ti->tileh != 0) {
if ((ti->map5 & 0xE0) == 0) { /* road or crossing */ switch (GB(ti->map5, 4, 4)) {
case 0: // normal road
case 1: { // level crossing
uint f = GetRoadFoundation(ti->tileh, ti->map5 & 0x3F); uint f = GetRoadFoundation(ti->tileh, ti->map5 & 0x3F);
if (f != 0) { if (f != 0) {
if (f < 15) { if (f < 15) {
@ -918,9 +944,13 @@ static uint GetSlopeZ_Road(const TileInfo* ti)
// inclined foundation // inclined foundation
th = _inclined_tileh[f - 15]; th = _inclined_tileh[f - 15];
} }
} else if ((ti->map5 & 0xF0) == 0x20) { break;
// depot }
case 2: // depot
return z + 8; return z + 8;
default: break;
} }
return GetPartialZ(ti->x&0xF, ti->y&0xF, th) + z; return GetPartialZ(ti->x&0xF, ti->y&0xF, th) + z;
} }
@ -931,7 +961,9 @@ static uint GetSlopeTileh_Road(const TileInfo *ti)
{ {
// check if it's a foundation // check if it's a foundation
if (ti->tileh != 0) { if (ti->tileh != 0) {
if ((ti->map5 & 0xE0) == 0) { /* road or crossing */ switch (GB(ti->map5, 4, 4)) {
case 0: // normal road
case 1: { // level crossing
uint f = GetRoadFoundation(ti->tileh, ti->map5 & 0x3F); uint f = GetRoadFoundation(ti->tileh, ti->map5 & 0x3F);
if (f != 0) { if (f != 0) {
if (f < 15) { if (f < 15) {
@ -941,9 +973,13 @@ static uint GetSlopeTileh_Road(const TileInfo *ti)
// inclined foundation // inclined foundation
return _inclined_tileh[f - 15]; return _inclined_tileh[f - 15];
} }
} else if ((ti->map5 & 0xF0) == 0x20) { break;
// depot }
case 2: // depot
return 0; return 0;
default: break;
} }
} }
return ti->tileh; return ti->tileh;
@ -1125,20 +1161,26 @@ static const byte _roadveh_enter_depot_unk0[4] = {
static uint32 VehicleEnter_Road(Vehicle *v, TileIndex tile, int x, int y) static uint32 VehicleEnter_Road(Vehicle *v, TileIndex tile, int x, int y)
{ {
if (IsLevelCrossing(tile)) { switch (GB(_m[tile].m5, 4, 4)) {
case 1: // level crossing
if (v->type == VEH_Train && GB(_m[tile].m5, 2, 1) == 0) { if (v->type == VEH_Train && GB(_m[tile].m5, 2, 1) == 0) {
/* train crossing a road */ /* train crossing a road */
SndPlayVehicleFx(SND_0E_LEVEL_CROSSING, v); SndPlayVehicleFx(SND_0E_LEVEL_CROSSING, v);
SB(_m[tile].m5, 2, 1, 1); SB(_m[tile].m5, 2, 1, 1);
MarkTileDirtyByTile(tile); MarkTileDirtyByTile(tile);
} }
} else if (GB(_m[tile].m5, 4, 4) == 2) { break;
case 2: // depot
if (v->type == VEH_Road && v->u.road.frame == 11) { if (v->type == VEH_Road && v->u.road.frame == 11) {
if (_roadveh_enter_depot_unk0[GB(_m[tile].m5, 0, 2)] == v->u.road.state) { if (_roadveh_enter_depot_unk0[GB(_m[tile].m5, 0, 2)] == v->u.road.state) {
RoadVehEnterDepot(v); RoadVehEnterDepot(v);
return 4; return 4;
} }
} }
break;
default: break;
} }
return 0; return 0;
} }
@ -1164,15 +1206,21 @@ static void ChangeTileOwner_Road(TileIndex tile, PlayerID old_player, PlayerID n
if (new_player != OWNER_SPECTATOR) { if (new_player != OWNER_SPECTATOR) {
SetTileOwner(tile, new_player); SetTileOwner(tile, new_player);
} else { } else {
if (GB(_m[tile].m5, 4, 4) == 0) { switch (GB(_m[tile].m5, 4, 4)) {
case 0: // normal road
SetTileOwner(tile, OWNER_NONE); SetTileOwner(tile, OWNER_NONE);
} else if (IsLevelCrossing(tile)) { break;
case 1: // level crossing
_m[tile].m5 = (_m[tile].m5&8) ? 0x5 : 0xA; _m[tile].m5 = (_m[tile].m5&8) ? 0x5 : 0xA;
SetTileOwner(tile, _m[tile].m3); SetTileOwner(tile, _m[tile].m3);
_m[tile].m3 = 0; _m[tile].m3 = 0;
_m[tile].m4 &= 0x80; _m[tile].m4 &= 0x80;
} else { break;
default: // depot
DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
break;
} }
} }
} }