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:
b = _m[tile].m5;
if ((b & 0xF0) == 0) {
} else if (IsLevelCrossing(tile)) {
b = (b&8)?5:10;
} else if ((b & 0xF0) == 0x20) {
return (~b & 3) == i;
} else {
return false;
switch (b & 0xF0) {
case 0: break; // normal road
case 1: b = (b & 8 ? 5 : 10); break; // level crossing
case 2: return (~b & 3) == i; // depot
default: return false;
}
break;
@ -177,16 +175,17 @@ int32 CmdRemoveRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2)
if (!b) return CMD_ERROR;
}
if (ti.type == MP_TUNNELBRIDGE) {
if (!EnsureNoVehicleZ(tile, TilePixelHeight(tile)))
return CMD_ERROR;
switch (ti.type) {
case MP_TUNNELBRIDGE:
if (!EnsureNoVehicleZ(tile, TilePixelHeight(tile))) return CMD_ERROR;
if ((ti.map5 & 0xE9) == 0xE8) {
if (pieces & 10) goto return_error;
} else if ((ti.map5 & 0xE9) == 0xE9) {
if (pieces & 5) goto return_error;
} else
} else {
goto return_error;
}
cost = _price.remove_road * 2;
@ -197,13 +196,14 @@ int32 CmdRemoveRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2)
MarkTileDirtyByTile(tile);
}
return cost;
} else if (ti.type == MP_STREET) {
case MP_STREET:
// check if you're allowed to remove the street owned by a town
// removal allowance depends on difficulty setting
if (!CheckforTownRating(flags, t, ROAD_REMOVE)) return CMD_ERROR;
// XXX - change cascading ifs to switch when doing rewrite
if ((ti.map5 & 0xF0) == 0) { // normal road
switch (GB(ti.map5, 4, 4)) {
case 0: { // normal road
byte c = pieces, t2;
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;
} else if ((ti.map5 & 0xE0) == 0) { // railroad crossing
}
case 1: { // level crossing
byte c;
if (!(ti.map5 & 8)) {
@ -255,10 +257,13 @@ int32 CmdRemoveRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2)
);
}
return cost;
} else
goto return_error;
}
} else {
default: // depot
goto return_error;
}
default:
return_error:;
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
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 ((pieces & (byte)ti.map5) == pieces)
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);
goto do_clear;
}
} else if (ti.type == MP_RAILWAY) {
break;
case MP_RAILWAY: {
byte m5;
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;
} else if (ti.type == MP_TUNNELBRIDGE) {
}
case MP_TUNNELBRIDGE:
/* check for flat land */
if (IsSteepTileh(ti.tileh)) { // very steep tile
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;
} else {
default:
do_clear:;
if (CmdFailed(DoCommandByTile(tile, 0, 0, flags & ~DC_EXEC, CMD_LANDSCAPE_CLEAR)))
return CMD_ERROR;
@ -680,32 +691,40 @@ static int32 RemoveRoadDepot(TileIndex tile, uint32 flags)
static int32 ClearTile_Road(TileIndex tile, byte flags)
{
int32 ret;
byte m5 = _m[tile].m5;
if ( (m5 & 0xF0) == 0) {
switch (GB(_m[tile].m5, 4, 4)) {
case 0: { // normal road
byte b = m5 & 0xF;
if (! ((1 << b) & (M(1)|M(2)|M(4)|M(8))) ) {
if ((!(flags & DC_AI_BUILDING) || !IsTileOwner(tile, OWNER_TOWN)) && flags & DC_AUTO)
if (!((1 << b) & (M(1)|M(2)|M(4)|M(8))) &&
(!(flags & DC_AI_BUILDING) || !IsTileOwner(tile, OWNER_TOWN)) &&
flags & DC_AUTO) {
return_cmd_error(STR_1801_MUST_REMOVE_ROAD_FIRST);
}
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 (flags & DC_EXEC) {
DoCommandByTile(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
}
return ret;
} else {
if (flags & DC_AUTO)
}
default: // depot
if (flags & DC_AUTO) {
return_cmd_error(STR_2004_BUILDING_MUST_BE_DEMOLISHED);
}
return RemoveRoadDepot(tile, flags);
}
}
@ -825,9 +844,12 @@ static void DrawTile_Road(TileInfo *ti)
PalSpriteID image;
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);
} else if ( (ti->map5 & 0xE0) == 0) { // railroad crossing
break;
case 1: { // level crossing
int f = GetRoadFoundation(ti->tileh, ti->map5 & 0xF);
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 ( (ti->map5 & 4) != 0)
image += 2;
if ((ti->map5 & 4) != 0) image += 2;
if ( _m[ti->tile].m4 & 0x80) {
image += 8;
@ -847,17 +868,19 @@ static void DrawTile_Road(TileInfo *ti)
}
DrawGroundSprite(image);
} else {
break;
}
default: { // depot
uint32 ormod;
PlayerID player;
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?
player = GetTileOwner(ti->tile);
if (player < MAX_PLAYERS)
ormod = PLAYER_SPRITE_COLOR(player);
if (player < MAX_PLAYERS) ormod = PLAYER_SPRITE_COLOR(player);
drss = _road_display_datas[ti->map5 & 0xF];
@ -866,13 +889,14 @@ static void DrawTile_Road(TileInfo *ti)
for (; drss->image != 0; drss++) {
uint32 image = drss->image;
if (image & PALETTE_MODIFIER_COLOR)
image |= ormod;
if (_display_opt & DO_TRANS_BUILDINGS) // show transparent depots
MAKE_TRANSPARENT(image);
if (image & PALETTE_MODIFIER_COLOR) image |= ormod;
if (_display_opt & DO_TRANS_BUILDINGS) MAKE_TRANSPARENT(image);
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
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);
if (f != 0) {
if (f < 15) {
@ -918,9 +944,13 @@ static uint GetSlopeZ_Road(const TileInfo* ti)
// inclined foundation
th = _inclined_tileh[f - 15];
}
} else if ((ti->map5 & 0xF0) == 0x20) {
// depot
break;
}
case 2: // depot
return z + 8;
default: break;
}
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
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);
if (f != 0) {
if (f < 15) {
@ -941,9 +973,13 @@ static uint GetSlopeTileh_Road(const TileInfo *ti)
// inclined foundation
return _inclined_tileh[f - 15];
}
} else if ((ti->map5 & 0xF0) == 0x20) {
// depot
break;
}
case 2: // depot
return 0;
default: break;
}
}
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)
{
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) {
/* train crossing a road */
SndPlayVehicleFx(SND_0E_LEVEL_CROSSING, v);
SB(_m[tile].m5, 2, 1, 1);
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 (_roadveh_enter_depot_unk0[GB(_m[tile].m5, 0, 2)] == v->u.road.state) {
RoadVehEnterDepot(v);
return 4;
}
}
break;
default: break;
}
return 0;
}
@ -1164,15 +1206,21 @@ static void ChangeTileOwner_Road(TileIndex tile, PlayerID old_player, PlayerID n
if (new_player != OWNER_SPECTATOR) {
SetTileOwner(tile, new_player);
} else {
if (GB(_m[tile].m5, 4, 4) == 0) {
switch (GB(_m[tile].m5, 4, 4)) {
case 0: // normal road
SetTileOwner(tile, OWNER_NONE);
} else if (IsLevelCrossing(tile)) {
break;
case 1: // level crossing
_m[tile].m5 = (_m[tile].m5&8) ? 0x5 : 0xA;
SetTileOwner(tile, _m[tile].m3);
_m[tile].m3 = 0;
_m[tile].m4 &= 0x80;
} else {
break;
default: // depot
DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
break;
}
}
}