Changeset - r3381:6ff4243f5b1b
[Not reviewed]
master
0 1 0
tron - 19 years ago 2006-03-31 06:16:04
tron@openttd.org
(svn r4187) Simplify the code for building/removing a piece of road a bit
1 file changed with 37 insertions and 39 deletions:
road_cmd.c
37
39
0 comments (0 inline, 0 general)
road_cmd.c
Show inline comments
 
@@ -20,12 +20,24 @@
 
#include "sound.h"
 
#include "depot.h"
 

	
 
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)
 
{
 
	RoadBits present;
 
	RoadBits n;
 
	byte owner;
 
	*edge_road = true;
 
@@ -82,13 +94,12 @@ static bool CheckAllowRemoveRoad(TileInd
 
 */
 
int32 CmdRemoveRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	// cost for removing inner/edge -roads
 
	static const uint16 road_remove_cost[2] = {50, 18};
 

	
 
	int32 cost;
 
	TileIndex tile;
 
	PlayerID owner;
 
	Town *t;
 
	/* true if the roadpiece was always removeable,
 
	 * false if it was a center piece. Affects town ratings drop */
 
	bool edge_road;
 
@@ -127,20 +138,18 @@ int32 CmdRemoveRoad(int x, int y, uint32
 
					!IsTransportUnderBridge(tile) ||
 
					GetTransportTypeUnderBridge(tile) != TRANSPORT_ROAD ||
 
					(pieces & ComplementRoadBits(GetRoadBitsUnderBridge(tile))) != 0) {
 
				return CMD_ERROR;
 
			}
 

	
 
			cost = _price.remove_road * 2;
 

	
 
			if (flags & DC_EXEC) {
 
				ChangeTownRating(t, -road_remove_cost[(byte)edge_road], RATING_ROAD_MINIMUM);
 
				SetClearUnderBridge(tile);
 
				MarkTileDirtyByTile(tile);
 
			}
 
			return cost;
 
			return _price.remove_road * 2;
 

	
 
		case MP_STREET:
 
			if (!EnsureNoVehicle(tile)) return CMD_ERROR;
 

	
 
			// check if you're allowed to remove the street owned by a town
 
			// removal allowance depends on difficulty setting
 
@@ -158,46 +167,38 @@ int32 CmdRemoveRoad(int x, int y, uint32
 
					}
 

	
 
					// limit the bits to delete to the existing bits.
 
					c &= present;
 
					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) {
 
						ChangeTownRating(t, -road_remove_cost[(byte)edge_road], RATING_ROAD_MINIMUM);
 

	
 
						present ^= c;
 
						if (present == 0) {
 
							DoClearSquare(tile);
 
						} else {
 
							SetRoadBits(tile, present);
 
							MarkTileDirtyByTile(tile);
 
						}
 
					}
 
					return cost;
 
					return CountRoadBits(c) * _price.remove_road;
 
				}
 

	
 
				case ROAD_CROSSING: {
 
					if (pieces & ComplementRoadBits(GetCrossingRoadBits(tile))) {
 
						return CMD_ERROR;
 
					}
 

	
 
					cost = _price.remove_road * 2;
 
					if (flags & DC_EXEC) {
 
						ChangeTownRating(t, -road_remove_cost[(byte)edge_road], RATING_ROAD_MINIMUM);
 

	
 
						MakeRailNormal(tile, GetTileOwner(tile), GetCrossingRailBits(tile), GetRailTypeCrossing(tile));
 
						MarkTileDirtyByTile(tile);
 
					}
 
					return cost;
 
					return _price.remove_road * 2;
 
				}
 

	
 
				default:
 
				case ROAD_DEPOT:
 
					return CMD_ERROR;
 
			}
 
@@ -239,14 +240,16 @@ static const RoadBits _valid_tileh_slope
 
	},
 
};
 

	
 

	
 
static uint32 CheckRoadSlope(int tileh, RoadBits* pieces, RoadBits existing)
 
{
 
	if (!IsSteepTileh(tileh)) {
 
		RoadBits road_bits = *pieces | existing;
 
	RoadBits road_bits;
 

	
 
	if (IsSteepTileh(tileh)) return CMD_ERROR;
 
	road_bits = *pieces | existing;
 

	
 
		// no special foundation
 
		if ((~_valid_tileh_slopes_road[0][tileh] & road_bits) == 0) {
 
			// 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
 
@@ -259,27 +262,27 @@ static uint32 CheckRoadSlope(int tileh, 
 

	
 
		// 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;
 
			return (*pieces == ROAD_X || *pieces == ROAD_Y) ? _price.terraform : CMD_ERROR;
 
		}
 
		if (*pieces == ROAD_X || *pieces == ROAD_Y) return _price.terraform;
 
	}
 
	return CMD_ERROR;
 
}
 

	
 
/** Build a piece of road.
 
 * @param x,y tile coordinates for road construction
 
 * @param p1 road piece flags
 
 * @param p2 the town that is building the road (0 if not applicable)
 
 */
 
int32 CmdBuildRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	TileInfo ti;
 
	int32 cost;
 
	int32 cost = 0;
 
	int32 ret;
 
	RoadBits existing = 0;
 
	RoadBits pieces;
 
	TileIndex tile;
 

	
 
	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
 

	
 
@@ -288,23 +291,21 @@ int32 CmdBuildRoad(int x, int y, uint32 
 
	if ((p1 >> 4) || (_current_player < MAX_PLAYERS && p2 != 0) || !IsTownIndex(p2)) return CMD_ERROR;
 
	pieces = p1;
 

	
 
	FindLandscapeHeight(&ti, x, y);
 
	tile = ti.tile;
 

	
 
	// allow building road under bridge
 
	if (ti.type != MP_TUNNELBRIDGE && !EnsureNoVehicle(tile)) return CMD_ERROR;
 

	
 
	switch (ti.type) {
 
		case MP_STREET:
 
			switch (GetRoadType(tile)) {
 
				case ROAD_NORMAL:
 
					existing = GetRoadBits(tile);
 
					if ((existing & pieces) == pieces) {
 
						return_cmd_error(STR_1007_ALREADY_BUILT);
 
					}
 
					if (!EnsureNoVehicle(tile)) return CMD_ERROR;
 
					break;
 

	
 
				case ROAD_CROSSING:
 
					if (pieces != GetCrossingRoadBits(tile)) { // XXX is this correct?
 
						return_cmd_error(STR_1007_ALREADY_BUILT);
 
					}
 
@@ -342,12 +343,14 @@ int32 CmdBuildRoad(int x, int y, uint32 
 
					roaddir = AXIS_X;
 
					break;
 

	
 
				default: goto do_clear;
 
			}
 

	
 
			if (!EnsureNoVehicle(tile)) return CMD_ERROR;
 

	
 
			if (flags & DC_EXEC) {
 
				MakeRoadCrossing(tile, _current_player, GetTileOwner(tile), roaddir, GetRailType(tile), p2);
 
				MarkTileDirtyByTile(tile);
 
			}
 
			return _price.build_road * 2;
 
		}
 
@@ -374,43 +377,38 @@ int32 CmdBuildRoad(int x, int y, uint32 
 
			} else {
 
				if (IsWaterUnderBridge(tile)) {
 
					return_cmd_error(STR_3807_CAN_T_BUILD_ON_WATER);
 
				}
 
			}
 

	
 
			/* all checked, can build road now! */
 
			cost = _price.build_road * 2;
 
			if (flags & DC_EXEC) {
 
				SetRoadUnderBridge(tile, _current_player);
 
				MarkTileDirtyByTile(tile);
 
			}
 
			return cost;
 
			return _price.build_road * 2;
 

	
 
		default:
 
do_clear:;
 
			if (CmdFailed(DoCommandByTile(tile, 0, 0, flags & ~DC_EXEC, CMD_LANDSCAPE_CLEAR)))
 
				return CMD_ERROR;
 
			ret = DoCommandByTile(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
 
			if (CmdFailed(ret)) return ret;
 
			cost += ret;
 
	}
 

	
 
	cost = CheckRoadSlope(ti.tileh, &pieces, existing);
 
	if (CmdFailed(cost)) return_cmd_error(STR_1800_LAND_SLOPED_IN_WRONG_DIRECTION);
 
	ret = CheckRoadSlope(ti.tileh, &pieces, existing);
 
	if (CmdFailed(ret)) return_cmd_error(STR_1800_LAND_SLOPED_IN_WRONG_DIRECTION);
 
	if (ret != 0 && (!_patches.build_on_slopes || _is_old_ai_player)) {
 
		return CMD_ERROR;
 
	}
 
	cost += ret;
 

	
 
	if (cost && (!_patches.build_on_slopes || _is_old_ai_player))
 
		return CMD_ERROR;
 

	
 
	if (ti.type == MP_STREET && GetRoadType(tile) == ROAD_NORMAL) {
 
	if (ti.type == MP_STREET) {
 
		// Don't put the pieces that already exist
 
		pieces &= ComplementRoadBits(existing);
 
	} else {
 
		cost += DoCommandByTile(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
 
	}
 

	
 
	if (pieces & ROAD_NW) cost += _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;
 
	cost += CountRoadBits(pieces) * _price.build_road;
 

	
 
	if (flags & DC_EXEC) {
 
		if (ti.type == MP_STREET) {
 
			SetRoadBits(tile, existing | pieces);
 
		} else {
 
			MakeRoadNormal(tile, _current_player, pieces, p2);
0 comments (0 inline, 0 general)