(svn r4187) Simplify the code for building/removing a piece of road a bit

This commit is contained in:
tron 2006-03-31 06:16:04 +00:00
parent 1a56b830c0
commit 7fbd940c82
1 changed files with 51 additions and 53 deletions

View File

@ -23,6 +23,18 @@
void RoadVehEnterDepot(Vehicle *v); void RoadVehEnterDepot(Vehicle *v);
static uint CountRoadBits(RoadBits r)
{
uint count = 0;
if (r & ROAD_NW) ++count;
if (r & ROAD_SW) ++count;
if (r & ROAD_SE) ++count;
if (r & ROAD_NE) ++count;
return count;
}
static bool CheckAllowRemoveRoad(TileIndex tile, RoadBits remove, bool* edge_road) static bool CheckAllowRemoveRoad(TileIndex tile, RoadBits remove, bool* edge_road)
{ {
RoadBits present; RoadBits present;
@ -85,7 +97,6 @@ int32 CmdRemoveRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2)
// cost for removing inner/edge -roads // cost for removing inner/edge -roads
static const uint16 road_remove_cost[2] = {50, 18}; static const uint16 road_remove_cost[2] = {50, 18};
int32 cost;
TileIndex tile; TileIndex tile;
PlayerID owner; PlayerID owner;
Town *t; Town *t;
@ -130,14 +141,12 @@ int32 CmdRemoveRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2)
return CMD_ERROR; return CMD_ERROR;
} }
cost = _price.remove_road * 2;
if (flags & DC_EXEC) { if (flags & DC_EXEC) {
ChangeTownRating(t, -road_remove_cost[(byte)edge_road], RATING_ROAD_MINIMUM); ChangeTownRating(t, -road_remove_cost[(byte)edge_road], RATING_ROAD_MINIMUM);
SetClearUnderBridge(tile); SetClearUnderBridge(tile);
MarkTileDirtyByTile(tile); MarkTileDirtyByTile(tile);
} }
return cost; return _price.remove_road * 2;
case MP_STREET: case MP_STREET:
if (!EnsureNoVehicle(tile)) return CMD_ERROR; if (!EnsureNoVehicle(tile)) return CMD_ERROR;
@ -161,13 +170,6 @@ int32 CmdRemoveRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2)
c &= present; c &= present;
if (c == 0) return CMD_ERROR; if (c == 0) return CMD_ERROR;
// calculate the cost
cost = 0;
if (c & ROAD_NW) cost += _price.remove_road;
if (c & ROAD_SW) cost += _price.remove_road;
if (c & ROAD_SE) cost += _price.remove_road;
if (c & ROAD_NE) cost += _price.remove_road;
if (flags & DC_EXEC) { if (flags & DC_EXEC) {
ChangeTownRating(t, -road_remove_cost[(byte)edge_road], RATING_ROAD_MINIMUM); ChangeTownRating(t, -road_remove_cost[(byte)edge_road], RATING_ROAD_MINIMUM);
@ -179,7 +181,7 @@ int32 CmdRemoveRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2)
MarkTileDirtyByTile(tile); MarkTileDirtyByTile(tile);
} }
} }
return cost; return CountRoadBits(c) * _price.remove_road;
} }
case ROAD_CROSSING: { case ROAD_CROSSING: {
@ -187,14 +189,13 @@ int32 CmdRemoveRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2)
return CMD_ERROR; return CMD_ERROR;
} }
cost = _price.remove_road * 2;
if (flags & DC_EXEC) { if (flags & DC_EXEC) {
ChangeTownRating(t, -road_remove_cost[(byte)edge_road], RATING_ROAD_MINIMUM); ChangeTownRating(t, -road_remove_cost[(byte)edge_road], RATING_ROAD_MINIMUM);
MakeRailNormal(tile, GetTileOwner(tile), GetCrossingRailBits(tile), GetRailTypeCrossing(tile)); MakeRailNormal(tile, GetTileOwner(tile), GetCrossingRailBits(tile), GetRailTypeCrossing(tile));
MarkTileDirtyByTile(tile); MarkTileDirtyByTile(tile);
} }
return cost; return _price.remove_road * 2;
} }
default: default:
@ -242,28 +243,29 @@ static const RoadBits _valid_tileh_slopes_road[][15] = {
static uint32 CheckRoadSlope(int tileh, RoadBits* pieces, RoadBits existing) static uint32 CheckRoadSlope(int tileh, RoadBits* pieces, RoadBits existing)
{ {
if (!IsSteepTileh(tileh)) { RoadBits road_bits;
RoadBits road_bits = *pieces | existing;
// no special foundation if (IsSteepTileh(tileh)) return CMD_ERROR;
if ((~_valid_tileh_slopes_road[0][tileh] & road_bits) == 0) { road_bits = *pieces | existing;
// force that all bits are set when we have slopes
if (tileh != 0) *pieces |= _valid_tileh_slopes_road[0][tileh];
return 0; // no extra cost
}
// foundation is used. Whole tile is leveled up // no special foundation
if ((~_valid_tileh_slopes_road[1][tileh] & road_bits) == 0) { if ((~_valid_tileh_slopes_road[0][tileh] & road_bits) == 0) {
return existing ? 0 : _price.terraform; // force that all bits are set when we have slopes
} if (tileh != 0) *pieces |= _valid_tileh_slopes_road[0][tileh];
return 0; // no extra cost
}
// partly leveled up tile, only if there's no road on that tile // foundation is used. Whole tile is leveled up
if (existing == 0 && (tileh == 1 || tileh == 2 || tileh == 4 || tileh == 8)) { if ((~_valid_tileh_slopes_road[1][tileh] & road_bits) == 0) {
// force full pieces. return existing ? 0 : _price.terraform;
*pieces |= (*pieces & 0xC) >> 2; }
*pieces |= (*pieces & 0x3) << 2;
return (*pieces == ROAD_X || *pieces == ROAD_Y) ? _price.terraform : CMD_ERROR; // partly leveled up tile, only if there's no road on that tile
} if (existing == 0 && (tileh == 1 || tileh == 2 || tileh == 4 || tileh == 8)) {
// force full pieces.
*pieces |= (*pieces & 0xC) >> 2;
*pieces |= (*pieces & 0x3) << 2;
if (*pieces == ROAD_X || *pieces == ROAD_Y) return _price.terraform;
} }
return CMD_ERROR; return CMD_ERROR;
} }
@ -276,7 +278,8 @@ static uint32 CheckRoadSlope(int tileh, RoadBits* pieces, RoadBits existing)
int32 CmdBuildRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2) int32 CmdBuildRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2)
{ {
TileInfo ti; TileInfo ti;
int32 cost; int32 cost = 0;
int32 ret;
RoadBits existing = 0; RoadBits existing = 0;
RoadBits pieces; RoadBits pieces;
TileIndex tile; TileIndex tile;
@ -291,9 +294,6 @@ int32 CmdBuildRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2)
FindLandscapeHeight(&ti, x, y); FindLandscapeHeight(&ti, x, y);
tile = ti.tile; tile = ti.tile;
// allow building road under bridge
if (ti.type != MP_TUNNELBRIDGE && !EnsureNoVehicle(tile)) return CMD_ERROR;
switch (ti.type) { switch (ti.type) {
case MP_STREET: case MP_STREET:
switch (GetRoadType(tile)) { switch (GetRoadType(tile)) {
@ -302,6 +302,7 @@ int32 CmdBuildRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2)
if ((existing & pieces) == pieces) { if ((existing & pieces) == pieces) {
return_cmd_error(STR_1007_ALREADY_BUILT); return_cmd_error(STR_1007_ALREADY_BUILT);
} }
if (!EnsureNoVehicle(tile)) return CMD_ERROR;
break; break;
case ROAD_CROSSING: case ROAD_CROSSING:
@ -345,6 +346,8 @@ int32 CmdBuildRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2)
default: goto do_clear; default: goto do_clear;
} }
if (!EnsureNoVehicle(tile)) return CMD_ERROR;
if (flags & DC_EXEC) { if (flags & DC_EXEC) {
MakeRoadCrossing(tile, _current_player, GetTileOwner(tile), roaddir, GetRailType(tile), p2); MakeRoadCrossing(tile, _current_player, GetTileOwner(tile), roaddir, GetRailType(tile), p2);
MarkTileDirtyByTile(tile); MarkTileDirtyByTile(tile);
@ -377,37 +380,32 @@ int32 CmdBuildRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2)
} }
} }
/* all checked, can build road now! */
cost = _price.build_road * 2;
if (flags & DC_EXEC) { if (flags & DC_EXEC) {
SetRoadUnderBridge(tile, _current_player); SetRoadUnderBridge(tile, _current_player);
MarkTileDirtyByTile(tile); MarkTileDirtyByTile(tile);
} }
return cost; return _price.build_road * 2;
default: default:
do_clear:; do_clear:;
if (CmdFailed(DoCommandByTile(tile, 0, 0, flags & ~DC_EXEC, CMD_LANDSCAPE_CLEAR))) ret = DoCommandByTile(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
return CMD_ERROR; if (CmdFailed(ret)) return ret;
cost += ret;
} }
cost = CheckRoadSlope(ti.tileh, &pieces, existing); ret = CheckRoadSlope(ti.tileh, &pieces, existing);
if (CmdFailed(cost)) return_cmd_error(STR_1800_LAND_SLOPED_IN_WRONG_DIRECTION); if (CmdFailed(ret)) return_cmd_error(STR_1800_LAND_SLOPED_IN_WRONG_DIRECTION);
if (ret != 0 && (!_patches.build_on_slopes || _is_old_ai_player)) {
if (cost && (!_patches.build_on_slopes || _is_old_ai_player))
return CMD_ERROR; return CMD_ERROR;
}
cost += ret;
if (ti.type == MP_STREET && GetRoadType(tile) == ROAD_NORMAL) { if (ti.type == MP_STREET) {
// Don't put the pieces that already exist // Don't put the pieces that already exist
pieces &= ComplementRoadBits(existing); pieces &= ComplementRoadBits(existing);
} else {
cost += DoCommandByTile(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
} }
if (pieces & ROAD_NW) cost += _price.build_road; cost += CountRoadBits(pieces) * _price.build_road;
if (pieces & ROAD_SW) cost += _price.build_road;
if (pieces & ROAD_SE) cost += _price.build_road;
if (pieces & ROAD_NE) cost += _price.build_road;
if (flags & DC_EXEC) { if (flags & DC_EXEC) {
if (ti.type == MP_STREET) { if (ti.type == MP_STREET) {