(svn r12569) -Cleanup: variable scope and coding style in road*.cpp
authorsmatz
Fri, 04 Apr 2008 18:41:16 +0000
changeset 9317 191b8b10e545
parent 9316 788721007cfd
child 9318 4db5b6462073
(svn r12569) -Cleanup: variable scope and coding style in road*.cpp
src/road_cmd.cpp
src/road_gui.cpp
src/road_internal.h
--- a/src/road_cmd.cpp	Fri Apr 04 17:49:42 2008 +0000
+++ b/src/road_cmd.cpp	Fri Apr 04 18:41:16 2008 +0000
@@ -342,7 +342,7 @@
  * @param other The other existent RoadBits
  * @return The costs for these RoadBits on this slope
  */
-static CommandCost CheckRoadSlope(Slope tileh, RoadBits* pieces, RoadBits existing, RoadBits other)
+static CommandCost CheckRoadSlope(Slope tileh, RoadBits *pieces, RoadBits existing, RoadBits other)
 {
 	/* Remove already build pieces */
 	CLRBITS(*pieces, existing);
@@ -418,7 +418,7 @@
 CommandCost CmdBuildRoad(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 {
 	CommandCost cost(EXPENSES_CONSTRUCTION);
-	CommandCost ret;
+
 	RoadBits existing = ROAD_NONE;
 	RoadBits other_bits = ROAD_NONE;
 
@@ -484,8 +484,6 @@
 			break;
 
 		case MP_RAILWAY: {
-			Axis roaddir;
-
 			if (IsSteepSlope(tileh)) {
 				return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION);
 			}
@@ -496,6 +494,8 @@
 			}
 
 			if (GetRailTileType(tile) != RAIL_TILE_NORMAL) goto do_clear;
+
+			Axis roaddir;
 			switch (GetTrackBits(tile)) {
 				case TRACK_BIT_X:
 					if (pieces & ROAD_X) goto do_clear;
@@ -534,16 +534,17 @@
 			if (GetVehicleTunnelBridge(tile, GetOtherTunnelBridgeEnd(tile)) != NULL) return CMD_ERROR;
 			break;
 
-		default:
+		default: {
 do_clear:;
-			ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
+			CommandCost ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
 			if (CmdFailed(ret)) return ret;
 			cost.AddCost(ret);
+		} break;
 	}
 
 	if (other_bits != pieces) {
 		/* Check the foundation/slopes when adding road/tram bits */
-		ret = CheckRoadSlope(tileh, &pieces, existing, other_bits);
+		CommandCost ret = CheckRoadSlope(tileh, &pieces, existing, other_bits);
 		/* Return an error if we need to build a foundation (ret != 0) but the
 		 * current patch-setting is turned off (or stupid AI@work) */
 		if (CmdFailed(ret) || (ret.GetCost() != 0 && !_patches.build_on_slopes)) {
@@ -643,8 +644,7 @@
  */
 CommandCost CmdBuildLongRoad(TileIndex end_tile, uint32 flags, uint32 p1, uint32 p2)
 {
-	TileIndex start_tile, tile;
-	CommandCost ret, cost(EXPENSES_CONSTRUCTION);
+	CommandCost cost(EXPENSES_CONSTRUCTION);
 	bool had_bridge = false;
 	bool had_tunnel = false;
 	bool had_success = false;
@@ -654,7 +654,7 @@
 
 	if (p1 >= MapSize()) return CMD_ERROR;
 
-	start_tile = p1;
+	TileIndex start_tile = p1;
 	RoadType rt = (RoadType)GB(p2, 3, 2);
 	if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR;
 
@@ -678,7 +678,7 @@
 	/* No disallowed direction bits have to be toggled */
 	if (!HasBit(p2, 5)) drd = DRD_NONE;
 
-	tile = start_tile;
+	TileIndex tile = start_tile;
 	/* Start tile is the small number. */
 	for (;;) {
 		RoadBits bits = HasBit(p2, 2) ? ROAD_Y : ROAD_X;
@@ -686,7 +686,7 @@
 		if (tile == end_tile && !HasBit(p2, 1)) bits &= ROAD_NW | ROAD_NE;
 		if (tile == start_tile && HasBit(p2, 0)) bits &= ROAD_SE | ROAD_SW;
 
-		ret = DoCommand(tile, drd << 6 | rt << 4 | bits, 0, flags, CMD_BUILD_ROAD);
+		CommandCost ret = DoCommand(tile, drd << 6 | rt << 4 | bits, 0, flags, CMD_BUILD_ROAD);
 		if (CmdFailed(ret)) {
 			if (_error_message != STR_1007_ALREADY_BUILT) return CMD_ERROR;
 		} else {
@@ -729,13 +729,11 @@
  */
 CommandCost CmdRemoveLongRoad(TileIndex end_tile, uint32 flags, uint32 p1, uint32 p2)
 {
-	TileIndex start_tile, tile;
-	CommandCost ret, cost(EXPENSES_CONSTRUCTION);
-	Money money;
+	CommandCost cost(EXPENSES_CONSTRUCTION);
 
 	if (p1 >= MapSize()) return CMD_ERROR;
 
-	start_tile = p1;
+	TileIndex start_tile = p1;
 	RoadType rt = (RoadType)GB(p2, 3, 2);
 	if (!IsValidRoadType(rt)) return CMD_ERROR;
 
@@ -751,8 +749,8 @@
 		p2 ^= IsInsideMM(p2 & 3, 1, 3) ? 3 : 0;
 	}
 
-	money = GetAvailableMoneyForCommand();
-	tile = start_tile;
+	Money money = GetAvailableMoneyForCommand();
+	TileIndex tile = start_tile;
 	/* Start tile is the small number. */
 	for (;;) {
 		RoadBits bits = HasBit(p2, 2) ? ROAD_Y : ROAD_X;
@@ -762,7 +760,7 @@
 
 		/* try to remove the halves. */
 		if (bits != 0) {
-			ret = RemoveRoad(tile, flags & ~DC_EXEC, bits, rt, true);
+			CommandCost ret = RemoveRoad(tile, flags & ~DC_EXEC, bits, rt, true);
 			if (CmdSucceeded(ret)) {
 				if (flags & DC_EXEC) {
 					money -= ret.GetCost();
@@ -796,15 +794,12 @@
  */
 CommandCost CmdBuildRoadDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 {
-	CommandCost cost;
-	Slope tileh;
-
 	DiagDirection dir = Extract<DiagDirection, 0>(p1);
 	RoadType rt = (RoadType)GB(p1, 2, 2);
 
 	if (!IsValidRoadType(rt) || !ValParamRoadType(rt)) return CMD_ERROR;
 
-	tileh = GetTileSlope(tile, NULL);
+	Slope tileh = GetTileSlope(tile, NULL);
 	if (tileh != SLOPE_FLAT && (
 				!_patches.build_on_slopes ||
 				IsSteepSlope(tileh) ||
@@ -813,7 +808,7 @@
 		return_cmd_error(STR_0007_FLAT_LAND_REQUIRED);
 	}
 
-	cost = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
+	CommandCost cost = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
 	if (CmdFailed(cost)) return CMD_ERROR;
 
 	if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST);
@@ -834,8 +829,7 @@
 
 static CommandCost RemoveRoadDepot(TileIndex tile, uint32 flags)
 {
-	if (!CheckTileOwnership(tile) && _current_player != OWNER_WATER)
-		return CMD_ERROR;
+	if (!CheckTileOwnership(tile) && _current_player != OWNER_WATER) return CMD_ERROR;
 
 	if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
 
@@ -968,7 +962,7 @@
  * @param ti   information about the tile (slopes, height etc)
  * @param tram the roadbits for the tram
  */
-void DrawTramCatenary(TileInfo *ti, RoadBits tram)
+void DrawTramCatenary(const TileInfo *ti, RoadBits tram)
 {
 	/* Do not draw catenary if it is invisible */
 	if (IsInvisibilitySet(TO_CATENARY)) return;
@@ -1003,7 +997,7 @@
  * @param dy  the offset from the top of the BB of the tile
  * @param h   the height of the sprite to draw
  */
-static void DrawRoadDetail(SpriteID img, TileInfo *ti, int dx, int dy, int h)
+static void DrawRoadDetail(SpriteID img, const TileInfo *ti, int dx, int dy, int h)
 {
 	int x = ti->x | dx;
 	int y = ti->y | dy;
@@ -1016,15 +1010,13 @@
  * Draw ground sprite and road pieces
  * @param ti TileInfo
  */
-static void DrawRoadBits(TileInfo* ti)
+static void DrawRoadBits(TileInfo *ti)
 {
 	RoadBits road = GetRoadBits(ti->tile, ROADTYPE_ROAD);
 	RoadBits tram = GetRoadBits(ti->tile, ROADTYPE_TRAM);
 
-	const DrawRoadTileStruct *drts;
 	SpriteID image = 0;
 	SpriteID pal = PAL_NONE;
-	Roadside roadside;
 
 	if (ti->tileh != SLOPE_FLAT) {
 		DrawFoundation(ti, GetRoadFoundation(ti->tileh, road | tram));
@@ -1036,7 +1028,7 @@
 
 	if (image == 0) image = _road_tile_sprites_1[road != ROAD_NONE ? road : tram];
 
-	roadside = GetRoadside(ti->tile);
+	Roadside roadside = GetRoadside(ti->tile);
 
 	if (AlwaysDrawUnpavedRoads(ti->tile, roadside)) {
 		image += 19;
@@ -1096,7 +1088,7 @@
 	if (CountBits(road) < 2) return;
 
 	/* Draw extra details. */
-	for (drts = _road_display_table[roadside][road | tram]; drts->image != 0; drts++) {
+	for (const DrawRoadTileStruct *drts = _road_display_table[roadside][road | tram]; drts->image != 0; drts++) {
 		DrawRoadDetail(drts->image, ti, drts->subcoord_x, drts->subcoord_y, 0x10);
 	}
 }
@@ -1109,17 +1101,16 @@
 			break;
 
 		case ROAD_TILE_CROSSING: {
-			SpriteID image;
-			SpriteID pal = PAL_NONE;
-			Roadside roadside = GetRoadside(ti->tile);
-
 			if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
 
-			image = GetRailTypeInfo(GetRailType(ti->tile))->base_sprites.crossing;
+			SpriteID image = GetRailTypeInfo(GetRailType(ti->tile))->base_sprites.crossing;
+			SpriteID pal = PAL_NONE;
 
 			if (GetCrossingRoadAxis(ti->tile) == AXIS_X) image++;
 			if (IsCrossingBarred(ti->tile)) image += 2;
 
+			Roadside roadside = GetRoadside(ti->tile);
+
 			if (AlwaysDrawUnpavedRoads(ti->tile, roadside)) {
 				image += 8;
 			} else {
@@ -1141,14 +1132,11 @@
 
 		default:
 		case ROAD_TILE_DEPOT: {
-			const DrawTileSprites* dts;
-			const DrawTileSeqStruct* dtss;
-			SpriteID palette;
-
 			if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
 
-			palette = PLAYER_SPRITE_COLOR(GetTileOwner(ti->tile));
+			SpriteID palette = PLAYER_SPRITE_COLOR(GetTileOwner(ti->tile));
 
+			const DrawTileSprites *dts;
 			if (HasTileRoadType(ti->tile, ROADTYPE_TRAM)) {
 				dts =  &_tram_depot[GetRoadDepotDirection(ti->tile)];
 			} else {
@@ -1160,7 +1148,7 @@
 			/* End now if buildings are invisible */
 			if (IsInvisibilitySet(TO_BUILDINGS)) break;
 
-			for (dtss = dts->seq; dtss->image.sprite != 0; dtss++) {
+			for (const DrawTileSeqStruct *dtss = dts->seq; dtss->image.sprite != 0; dtss++) {
 				SpriteID image = dtss->image.sprite;
 				SpriteID pal;
 
@@ -1187,15 +1175,14 @@
 void DrawRoadDepotSprite(int x, int y, DiagDirection dir, RoadType rt)
 {
 	SpriteID palette = PLAYER_SPRITE_COLOR(_local_player);
-	const DrawTileSprites* dts = (rt == ROADTYPE_TRAM) ? &_tram_depot[dir] : &_road_depot[dir];
-	const DrawTileSeqStruct* dtss;
+	const DrawTileSprites *dts = (rt == ROADTYPE_TRAM) ? &_tram_depot[dir] : &_road_depot[dir];
 
 	x += 33;
 	y += 17;
 
 	DrawSprite(dts->ground.sprite, PAL_NONE, x, y);
 
-	for (dtss = dts->seq; dtss->image.sprite != 0; dtss++) {
+	for (const DrawTileSeqStruct *dtss = dts->seq; dtss->image.sprite != 0; dtss++) {
 		Point pt = RemapCoords(dtss->delta_x, dtss->delta_y, dtss->delta_z);
 		SpriteID image = dtss->image.sprite;
 
@@ -1275,7 +1262,7 @@
 
 	if (IsRoadDepot(tile)) return;
 
-	const Town* t = ClosestTownFromTile(tile, (uint)-1);
+	const Town *t = ClosestTownFromTile(tile, (uint)-1);
 	if (!HasRoadWorks(tile)) {
 		HouseZonesBits grp = HZB_TOWN_EDGE;
 
--- a/src/road_gui.cpp	Fri Apr 04 17:49:42 2008 +0000
+++ b/src/road_gui.cpp	Fri Apr 04 18:41:16 2008 +0000
@@ -175,7 +175,7 @@
 static void BuildRoadOutsideStation(TileIndex tile, DiagDirection direction)
 {
 	tile += TileOffsByDiagDir(direction);
-	// if there is a roadpiece just outside of the station entrance, build a connecting route
+	/* if there is a roadpiece just outside of the station entrance, build a connecting route */
 	if (IsNormalRoadTile(tile)) {
 		if (GetRoadBits(tile, _cur_roadtype) != ROAD_NONE) {
 			DoCommandP(tile, _cur_roadtype << 4 | DiagDirToRoadBits(ReverseDiagDir(direction)), 0, NULL, CMD_BUILD_ROAD);
@@ -409,7 +409,7 @@
  * @param w The toolbar window
  * @param clicked_widget The widget which the player clicked just now
  */
-static void UpdateOptionWidgetStatus(Window *w, int clicked_widget)
+static void UpdateOptionWidgetStatus(Window *w, RoadToolbarWidgets clicked_widget)
 {
 	/* The remove and the one way button state is driven
 	 * by the other buttons so they don't act on themselfs.
@@ -419,15 +419,18 @@
 			w->RaiseWidget(RTW_ONE_WAY);
 			w->InvalidateWidget(RTW_ONE_WAY);
 			break;
+
 		case RTW_ONE_WAY:
 			w->RaiseWidget(RTW_REMOVE);
 			w->InvalidateWidget(RTW_REMOVE);
 			break;
+
 		case RTW_BUS_STATION:
 		case RTW_TRUCK_STATION:
 			w->DisableWidget(RTW_ONE_WAY);
 			w->SetWidgetDisabledState(RTW_REMOVE, !w->IsWidgetLowered(clicked_widget));
 			break;
+
 		case RTW_ROAD_X:
 		case RTW_ROAD_Y:
 		case RTW_AUTOROAD:
@@ -436,6 +439,7 @@
 				RTW_ONE_WAY,
 				WIDGET_LIST_END);
 			break;
+
 		default:
 			/* When any other buttons than road/station, raise and
 			 * disable the removal button */
@@ -454,157 +458,162 @@
 static void BuildRoadToolbWndProc(Window *w, WindowEvent *e)
 {
 	switch (e->event) {
-	case WE_CREATE:
-		w->SetWidgetsDisabledState(true,
-			RTW_REMOVE,
-			RTW_ONE_WAY,
-			WIDGET_LIST_END);
-		break;
+		case WE_CREATE:
+			w->SetWidgetsDisabledState(true,
+				RTW_REMOVE,
+				RTW_ONE_WAY,
+				WIDGET_LIST_END);
+			break;
 
-	case WE_PAINT:
-		w->SetWidgetsDisabledState(!CanBuildVehicleInfrastructure(VEH_ROAD),
-			RTW_DEPOT,
-			RTW_BUS_STATION,
-			RTW_TRUCK_STATION,
-			WIDGET_LIST_END);
-		DrawWindowWidgets(w);
-		break;
+		case WE_PAINT:
+			w->SetWidgetsDisabledState(!CanBuildVehicleInfrastructure(VEH_ROAD),
+				RTW_DEPOT,
+				RTW_BUS_STATION,
+				RTW_TRUCK_STATION,
+				WIDGET_LIST_END);
+			DrawWindowWidgets(w);
+			break;
 
-	case WE_CLICK:
-		if (e->we.click.widget >= RTW_ROAD_X) {
-			_remove_button_clicked = false;
-			_one_way_button_clicked = false;
-			_build_road_button_proc[e->we.click.widget - RTW_ROAD_X](w);
-		}
-		UpdateOptionWidgetStatus(w, e->we.click.widget);
-		if (_ctrl_pressed) RoadToolbar_CtrlChanged(w);
-		break;
-
-	case WE_KEYPRESS:
-		for (uint8 i = 0; i != lengthof(_road_keycodes); i++) {
-			if (e->we.keypress.keycode == _road_keycodes[i]) {
-				e->we.keypress.cont = false;
+		case WE_CLICK:
+			if (e->we.click.widget >= RTW_ROAD_X) {
 				_remove_button_clicked = false;
 				_one_way_button_clicked = false;
-				_build_road_button_proc[i](w);
-				UpdateOptionWidgetStatus(w, i + RTW_ROAD_X);
-				if (_ctrl_pressed) RoadToolbar_CtrlChanged(w);
-				break;
+				_build_road_button_proc[e->we.click.widget - RTW_ROAD_X](w);
 			}
-		}
-		MarkTileDirty(_thd.pos.x, _thd.pos.y); // redraw tile selection
-		break;
-
-	case WE_PLACE_OBJ:
-		_remove_button_clicked = w->IsWidgetLowered(RTW_REMOVE);
-		_one_way_button_clicked = w->IsWidgetLowered(RTW_ONE_WAY);
-		_place_proc(e->we.place.tile);
-		break;
-
-	case WE_ABORT_PLACE_OBJ:
-		w->RaiseButtons();
-		w->SetWidgetsDisabledState(true,
-			RTW_REMOVE,
-			RTW_ONE_WAY,
-			WIDGET_LIST_END);
-		w->InvalidateWidget(RTW_REMOVE);
-		w->InvalidateWidget(RTW_ONE_WAY);
-
-		w = FindWindowById(WC_BUS_STATION, 0);
-		if (w != NULL) WP(w, def_d).close = true;
-		w = FindWindowById(WC_TRUCK_STATION, 0);
-		if (w != NULL) WP(w, def_d).close = true;
-		w = FindWindowById(WC_BUILD_DEPOT, 0);
-		if (w != NULL) WP(w, def_d).close = true;
-		break;
+			UpdateOptionWidgetStatus(w, (RoadToolbarWidgets)e->we.click.widget);
+			if (_ctrl_pressed) RoadToolbar_CtrlChanged(w);
+			break;
 
-	case WE_PLACE_DRAG:
-		/* Here we update the end tile flags
-		 * of the road placement actions.
-		 * At first we reset the end halfroad
-		 * bits and if needed we set them again. */
-		switch (e->we.place.select_proc) {
-			case DDSP_PLACE_ROAD_X_DIR:
-				_place_road_flag &= ~RF_END_HALFROAD_X;
-				if (e->we.place.pt.x & 8) _place_road_flag |= RF_END_HALFROAD_X;
-				break;
-
-			case DDSP_PLACE_ROAD_Y_DIR:
-				_place_road_flag &= ~RF_END_HALFROAD_Y;
-				if (e->we.place.pt.y & 8) _place_road_flag |= RF_END_HALFROAD_Y;
-				break;
-
-			case DDSP_PLACE_AUTOROAD:
-				_place_road_flag &= ~(RF_END_HALFROAD_Y | RF_END_HALFROAD_X);
-				if (e->we.place.pt.y & 8) _place_road_flag |= RF_END_HALFROAD_Y;
-				if (e->we.place.pt.x & 8) _place_road_flag |= RF_END_HALFROAD_X;
+		case WE_KEYPRESS:
+			for (uint i = 0; i != lengthof(_road_keycodes); i++) {
+				if (e->we.keypress.keycode == _road_keycodes[i]) {
+					e->we.keypress.cont = false;
+					_remove_button_clicked = false;
+					_one_way_button_clicked = false;
+					_build_road_button_proc[i](w);
+					UpdateOptionWidgetStatus(w, (RoadToolbarWidgets)(i + RTW_ROAD_X));
+					if (_ctrl_pressed) RoadToolbar_CtrlChanged(w);
+					break;
+				}
+			}
+			MarkTileDirty(_thd.pos.x, _thd.pos.y); // redraw tile selection
+			break;
 
-				/* For autoroad we need to update the
-				 * direction of the road */
-				if (_thd.size.x > _thd.size.y || (_thd.size.x == _thd.size.y &&
-						( (_tile_fract_coords.x < _tile_fract_coords.y && (_tile_fract_coords.x + _tile_fract_coords.y) < 16) ||
-						  (_tile_fract_coords.x > _tile_fract_coords.y && (_tile_fract_coords.x + _tile_fract_coords.y) > 16) ))) {
-					/* Set dir = X */
-					_place_road_flag &= ~RF_DIR_Y;
-				} else {
-					/* Set dir = Y */
-					_place_road_flag |= RF_DIR_Y;
-				}
+		case WE_PLACE_OBJ:
+			_remove_button_clicked = w->IsWidgetLowered(RTW_REMOVE);
+			_one_way_button_clicked = w->IsWidgetLowered(RTW_ONE_WAY);
+			_place_proc(e->we.place.tile);
+			break;
 
-				break;
-		}
-
-		VpSelectTilesWithMethod(e->we.place.pt.x, e->we.place.pt.y, e->we.place.select_method);
-		return;
+		case WE_ABORT_PLACE_OBJ:
+			w->RaiseButtons();
+			w->SetWidgetsDisabledState(true,
+				RTW_REMOVE,
+				RTW_ONE_WAY,
+				WIDGET_LIST_END);
+			w->InvalidateWidget(RTW_REMOVE);
+			w->InvalidateWidget(RTW_ONE_WAY);
 
-	case WE_PLACE_MOUSEUP:
-		if (e->we.place.pt.x != -1) {
-			TileIndex start_tile = e->we.place.starttile;
-			TileIndex end_tile = e->we.place.tile;
+			w = FindWindowById(WC_BUS_STATION, 0);
+			if (w != NULL) WP(w, def_d).close = true;
+			w = FindWindowById(WC_TRUCK_STATION, 0);
+			if (w != NULL) WP(w, def_d).close = true;
+			w = FindWindowById(WC_BUILD_DEPOT, 0);
+			if (w != NULL) WP(w, def_d).close = true;
+			break;
 
+		case WE_PLACE_DRAG:
+			/* Here we update the end tile flags
+			* of the road placement actions.
+			* At first we reset the end halfroad
+			* bits and if needed we set them again. */
 			switch (e->we.place.select_proc) {
-				case DDSP_BUILD_BRIDGE:
-					ResetObjectToPlace();
-					ShowBuildBridgeWindow(start_tile, end_tile, TRANSPORT_ROAD, RoadTypeToRoadTypes(_cur_roadtype));
+				case DDSP_PLACE_ROAD_X_DIR:
+					_place_road_flag &= ~RF_END_HALFROAD_X;
+					if (e->we.place.pt.x & 8) _place_road_flag |= RF_END_HALFROAD_X;
 					break;
 
-				case DDSP_DEMOLISH_AREA:
-					DoCommandP(end_tile, start_tile, 0, CcPlaySound10, CMD_CLEAR_AREA | CMD_MSG(STR_00B5_CAN_T_CLEAR_THIS_AREA));
+				case DDSP_PLACE_ROAD_Y_DIR:
+					_place_road_flag &= ~RF_END_HALFROAD_Y;
+					if (e->we.place.pt.y & 8) _place_road_flag |= RF_END_HALFROAD_Y;
 					break;
 
-				case DDSP_PLACE_ROAD_X_DIR:
-				case DDSP_PLACE_ROAD_Y_DIR:
 				case DDSP_PLACE_AUTOROAD:
-					/* Flag description:
-					 * Use the first three bits (0x07) if dir == Y
-					 * else use the last 2 bits (X dir has
-					 * not the 3rd bit set) */
-					_place_road_flag = (RoadFlags)((_place_road_flag & RF_DIR_Y) ? (_place_road_flag & 0x07) : (_place_road_flag >> 3));
+					_place_road_flag &= ~(RF_END_HALFROAD_Y | RF_END_HALFROAD_X);
+					if (e->we.place.pt.y & 8) _place_road_flag |= RF_END_HALFROAD_Y;
+					if (e->we.place.pt.x & 8) _place_road_flag |= RF_END_HALFROAD_X;
 
-					DoCommandP(end_tile, start_tile, _place_road_flag | (_cur_roadtype << 3) | (_one_way_button_clicked << 5), CcPlaySound1D,
-						(_ctrl_pressed || _remove_button_clicked) ?
-						CMD_REMOVE_LONG_ROAD | CMD_NO_WATER | CMD_MSG(_road_type_infos[_cur_roadtype].err_remove_road) :
-						CMD_BUILD_LONG_ROAD | CMD_NO_WATER | CMD_MSG(_road_type_infos[_cur_roadtype].err_build_road));
+					/* For autoroad we need to update the
+					* direction of the road */
+					if (_thd.size.x > _thd.size.y || (_thd.size.x == _thd.size.y &&
+							( (_tile_fract_coords.x < _tile_fract_coords.y && (_tile_fract_coords.x + _tile_fract_coords.y) < 16) ||
+							(_tile_fract_coords.x > _tile_fract_coords.y && (_tile_fract_coords.x + _tile_fract_coords.y) > 16) ))) {
+						/* Set dir = X */
+						_place_road_flag &= ~RF_DIR_Y;
+					} else {
+						/* Set dir = Y */
+						_place_road_flag |= RF_DIR_Y;
+					}
+
+					break;
+
+				default:
 					break;
 			}
-		}
-		break;
-
-	case WE_PLACE_PRESIZE: {
-		TileIndex tile = e->we.place.tile;
 
-		DoCommand(tile, 0x200 | RoadTypeToRoadTypes(_cur_roadtype), 0, DC_AUTO, CMD_BUILD_TUNNEL);
-		VpSetPresizeRange(tile, _build_tunnel_endtile == 0 ? tile : _build_tunnel_endtile);
-		break;
-	}
+			VpSelectTilesWithMethod(e->we.place.pt.x, e->we.place.pt.y, e->we.place.select_method);
+			return;
 
-	case WE_DESTROY:
-		if (_patches.link_terraform_toolbar) DeleteWindowById(WC_SCEN_LAND_GEN, 0);
-		break;
+		case WE_PLACE_MOUSEUP:
+			if (e->we.place.pt.x != -1) {
+				TileIndex start_tile = e->we.place.starttile;
+				TileIndex end_tile = e->we.place.tile;
 
-	case WE_CTRL_CHANGED:
-		if (RoadToolbar_CtrlChanged(w)) e->we.ctrl.cont = false;
-		break;
+				switch (e->we.place.select_proc) {
+					case DDSP_BUILD_BRIDGE:
+						ResetObjectToPlace();
+						ShowBuildBridgeWindow(start_tile, end_tile, TRANSPORT_ROAD, RoadTypeToRoadTypes(_cur_roadtype));
+						break;
+
+					case DDSP_DEMOLISH_AREA:
+						DoCommandP(end_tile, start_tile, 0, CcPlaySound10, CMD_CLEAR_AREA | CMD_MSG(STR_00B5_CAN_T_CLEAR_THIS_AREA));
+						break;
+
+					case DDSP_PLACE_ROAD_X_DIR:
+					case DDSP_PLACE_ROAD_Y_DIR:
+					case DDSP_PLACE_AUTOROAD:
+						/* Flag description:
+						* Use the first three bits (0x07) if dir == Y
+						* else use the last 2 bits (X dir has
+						* not the 3rd bit set) */
+						_place_road_flag = (RoadFlags)((_place_road_flag & RF_DIR_Y) ? (_place_road_flag & 0x07) : (_place_road_flag >> 3));
+
+						DoCommandP(end_tile, start_tile, _place_road_flag | (_cur_roadtype << 3) | (_one_way_button_clicked << 5), CcPlaySound1D,
+							(_ctrl_pressed || _remove_button_clicked) ?
+							CMD_REMOVE_LONG_ROAD | CMD_NO_WATER | CMD_MSG(_road_type_infos[_cur_roadtype].err_remove_road) :
+							CMD_BUILD_LONG_ROAD | CMD_NO_WATER | CMD_MSG(_road_type_infos[_cur_roadtype].err_build_road));
+						break;
+				}
+			}
+			break;
+
+		case WE_PLACE_PRESIZE: {
+			TileIndex tile = e->we.place.tile;
+
+			DoCommand(tile, 0x200 | RoadTypeToRoadTypes(_cur_roadtype), 0, DC_AUTO, CMD_BUILD_TUNNEL);
+			VpSetPresizeRange(tile, _build_tunnel_endtile == 0 ? tile : _build_tunnel_endtile);
+		} break;
+
+		case WE_DESTROY:
+			if (_patches.link_terraform_toolbar) DeleteWindowById(WC_SCEN_LAND_GEN, 0);
+			break;
+
+		case WE_CTRL_CHANGED:
+			if (RoadToolbar_CtrlChanged(w)) e->we.ctrl.cont = false;
+			break;
+
+		default:
+			break;
 	}
 }
 
@@ -724,39 +733,47 @@
 static void BuildRoadDepotWndProc(Window *w, WindowEvent *e)
 {
 	switch (e->event) {
-	case WE_CREATE: w->LowerWidget(_road_depot_orientation + BRDW_DEPOT_NE); break;
-
-	case WE_PAINT:
-		DrawWindowWidgets(w);
-
-		DrawRoadDepotSprite(70, 17, DIAGDIR_NE, _cur_roadtype);
-		DrawRoadDepotSprite(70, 69, DIAGDIR_SE, _cur_roadtype);
-		DrawRoadDepotSprite( 2, 69, DIAGDIR_SW, _cur_roadtype);
-		DrawRoadDepotSprite( 2, 17, DIAGDIR_NW, _cur_roadtype);
-		break;
+		case WE_CREATE:
+			w->LowerWidget(_road_depot_orientation + BRDW_DEPOT_NE);
+			break;
 
-	case WE_CLICK:
-		switch (e->we.click.widget) {
-			case BRDW_DEPOT_NW:
-			case BRDW_DEPOT_NE:
-			case BRDW_DEPOT_SW:
-			case BRDW_DEPOT_SE:
-				w->RaiseWidget(_road_depot_orientation + BRDW_DEPOT_NE);
-				_road_depot_orientation = (DiagDirection)(e->we.click.widget - BRDW_DEPOT_NE);
-				w->LowerWidget(_road_depot_orientation + BRDW_DEPOT_NE);
-				SndPlayFx(SND_15_BEEP);
-				SetWindowDirty(w);
-				break;
-		}
-		break;
+		case WE_PAINT:
+			DrawWindowWidgets(w);
 
-	case WE_MOUSELOOP:
-		if (WP(w, def_d).close) DeleteWindow(w);
-		break;
+			DrawRoadDepotSprite(70, 17, DIAGDIR_NE, _cur_roadtype);
+			DrawRoadDepotSprite(70, 69, DIAGDIR_SE, _cur_roadtype);
+			DrawRoadDepotSprite( 2, 69, DIAGDIR_SW, _cur_roadtype);
+			DrawRoadDepotSprite( 2, 17, DIAGDIR_NW, _cur_roadtype);
+			break;
 
-	case WE_DESTROY:
-		if (!WP(w, def_d).close) ResetObjectToPlace();
-		break;
+		case WE_CLICK:
+			switch (e->we.click.widget) {
+				case BRDW_DEPOT_NW:
+				case BRDW_DEPOT_NE:
+				case BRDW_DEPOT_SW:
+				case BRDW_DEPOT_SE:
+					w->RaiseWidget(_road_depot_orientation + BRDW_DEPOT_NE);
+					_road_depot_orientation = (DiagDirection)(e->we.click.widget - BRDW_DEPOT_NE);
+					w->LowerWidget(_road_depot_orientation + BRDW_DEPOT_NE);
+					SndPlayFx(SND_15_BEEP);
+					SetWindowDirty(w);
+					break;
+
+				default:
+					break;
+			}
+			break;
+
+		case WE_MOUSELOOP:
+			if (WP(w, def_d).close) DeleteWindow(w);
+			break;
+
+		case WE_DESTROY:
+			if (!WP(w, def_d).close) ResetObjectToPlace();
+			break;
+
+		default:
+			break;
 	}
 }
 
@@ -824,93 +841,99 @@
 static void RoadStationPickerWndProc(Window *w, WindowEvent *e)
 {
 	switch (e->event) {
-	case WE_CREATE:
-		/* Trams don't have non-drivethrough stations */
-		if (_cur_roadtype == ROADTYPE_TRAM && _road_station_picker_orientation < DIAGDIR_END) {
-			_road_station_picker_orientation = DIAGDIR_END;
-		}
-		w->SetWidgetsDisabledState(_cur_roadtype == ROADTYPE_TRAM,
-			BRSW_STATION_NE,
-			BRSW_STATION_SE,
-			BRSW_STATION_SW,
-			BRSW_STATION_NW,
-			WIDGET_LIST_END);
-
-		w->LowerWidget(_road_station_picker_orientation + BRSW_STATION_NE);
-		w->LowerWidget(_station_show_coverage + BRSW_LT_OFF);
-		break;
-
-	case WE_PAINT: {
-		if (WP(w, def_d).close) return;
-
-		DrawWindowWidgets(w);
-
-		if (_station_show_coverage) {
-			int rad = _patches.modified_catchment ? CA_TRUCK /* = CA_BUS */ : CA_UNMODIFIED;
-			SetTileSelectBigSize(-rad, -rad, 2 * rad, 2 * rad);
-		} else {
-			SetTileSelectSize(1, 1);
-		}
-
-		StationType st = (w->window_class == WC_BUS_STATION) ? STATION_BUS : STATION_TRUCK;
-
-		StationPickerDrawSprite(103, 35, st, INVALID_RAILTYPE, ROADTYPE_ROAD, 0);
-		StationPickerDrawSprite(103, 85, st, INVALID_RAILTYPE, ROADTYPE_ROAD, 1);
-		StationPickerDrawSprite( 35, 85, st, INVALID_RAILTYPE, ROADTYPE_ROAD, 2);
-		StationPickerDrawSprite( 35, 35, st, INVALID_RAILTYPE, ROADTYPE_ROAD, 3);
-
-		StationPickerDrawSprite(171, 35, st, INVALID_RAILTYPE, _cur_roadtype, 4);
-		StationPickerDrawSprite(171, 85, st, INVALID_RAILTYPE, _cur_roadtype, 5);
+		case WE_CREATE:
+			/* Trams don't have non-drivethrough stations */
+			if (_cur_roadtype == ROADTYPE_TRAM && _road_station_picker_orientation < DIAGDIR_END) {
+				_road_station_picker_orientation = DIAGDIR_END;
+			}
+			w->SetWidgetsDisabledState(_cur_roadtype == ROADTYPE_TRAM,
+				BRSW_STATION_NE,
+				BRSW_STATION_SE,
+				BRSW_STATION_SW,
+				BRSW_STATION_NW,
+				WIDGET_LIST_END);
 
-		int text_end = DrawStationCoverageAreaText(2, 146,
-			(w->window_class == WC_BUS_STATION) ? SCT_PASSENGERS_ONLY : SCT_NON_PASSENGERS_ONLY,
-			3) + 4;
-		if (text_end > w->widget[BRSW_BACKGROUND].bottom) {
-			SetWindowDirty(w);
-			ResizeWindowForWidget(w, BRSW_BACKGROUND, 0, text_end - w->widget[BRSW_BACKGROUND].bottom);
-			SetWindowDirty(w);
-		}
-
-	} break;
+			w->LowerWidget(_road_station_picker_orientation + BRSW_STATION_NE);
+			w->LowerWidget(_station_show_coverage + BRSW_LT_OFF);
+			break;
 
-	case WE_CLICK: {
-		switch (e->we.click.widget) {
-			case BRSW_STATION_NE:
-			case BRSW_STATION_SE:
-			case BRSW_STATION_SW:
-			case BRSW_STATION_NW:
-			case BRSW_STATION_X:
-			case BRSW_STATION_Y:
-				w->RaiseWidget(_road_station_picker_orientation + BRSW_STATION_NE);
-				_road_station_picker_orientation = (DiagDirection)(e->we.click.widget - BRSW_STATION_NE);
-				w->LowerWidget(_road_station_picker_orientation + BRSW_STATION_NE);
-				SndPlayFx(SND_15_BEEP);
+		case WE_PAINT: {
+			if (WP(w, def_d).close) return;
+
+			DrawWindowWidgets(w);
+
+			if (_station_show_coverage) {
+				int rad = _patches.modified_catchment ? CA_TRUCK /* = CA_BUS */ : CA_UNMODIFIED;
+				SetTileSelectBigSize(-rad, -rad, 2 * rad, 2 * rad);
+			} else {
+				SetTileSelectSize(1, 1);
+			}
+
+			StationType st = (w->window_class == WC_BUS_STATION) ? STATION_BUS : STATION_TRUCK;
+
+			StationPickerDrawSprite(103, 35, st, INVALID_RAILTYPE, ROADTYPE_ROAD, 0);
+			StationPickerDrawSprite(103, 85, st, INVALID_RAILTYPE, ROADTYPE_ROAD, 1);
+			StationPickerDrawSprite( 35, 85, st, INVALID_RAILTYPE, ROADTYPE_ROAD, 2);
+			StationPickerDrawSprite( 35, 35, st, INVALID_RAILTYPE, ROADTYPE_ROAD, 3);
+
+			StationPickerDrawSprite(171, 35, st, INVALID_RAILTYPE, _cur_roadtype, 4);
+			StationPickerDrawSprite(171, 85, st, INVALID_RAILTYPE, _cur_roadtype, 5);
+
+			int text_end = DrawStationCoverageAreaText(2, 146,
+				(w->window_class == WC_BUS_STATION) ? SCT_PASSENGERS_ONLY : SCT_NON_PASSENGERS_ONLY,
+				3) + 4;
+			if (text_end > w->widget[BRSW_BACKGROUND].bottom) {
 				SetWindowDirty(w);
-				break;
-
-			case BRSW_LT_OFF:
-			case BRSW_LT_ON:
-				w->RaiseWidget(_station_show_coverage + BRSW_LT_OFF);
-				_station_show_coverage = (e->we.click.widget != BRSW_LT_OFF);
-				w->LowerWidget(_station_show_coverage + BRSW_LT_OFF);
-				SndPlayFx(SND_15_BEEP);
+				ResizeWindowForWidget(w, BRSW_BACKGROUND, 0, text_end - w->widget[BRSW_BACKGROUND].bottom);
 				SetWindowDirty(w);
-				break;
-		}
-	} break;
+			}
 
-	case WE_MOUSELOOP: {
-		if (WP(w, def_d).close) {
-			DeleteWindow(w);
-			return;
-		}
+		} break;
 
-		CheckRedrawStationCoverage(w);
-	} break;
+		case WE_CLICK: {
+			switch (e->we.click.widget) {
+				case BRSW_STATION_NE:
+				case BRSW_STATION_SE:
+				case BRSW_STATION_SW:
+				case BRSW_STATION_NW:
+				case BRSW_STATION_X:
+				case BRSW_STATION_Y:
+					w->RaiseWidget(_road_station_picker_orientation + BRSW_STATION_NE);
+					_road_station_picker_orientation = (DiagDirection)(e->we.click.widget - BRSW_STATION_NE);
+					w->LowerWidget(_road_station_picker_orientation + BRSW_STATION_NE);
+					SndPlayFx(SND_15_BEEP);
+					SetWindowDirty(w);
+					break;
 
-	case WE_DESTROY:
-		if (!WP(w, def_d).close) ResetObjectToPlace();
-		break;
+				case BRSW_LT_OFF:
+				case BRSW_LT_ON:
+					w->RaiseWidget(_station_show_coverage + BRSW_LT_OFF);
+					_station_show_coverage = (e->we.click.widget != BRSW_LT_OFF);
+					w->LowerWidget(_station_show_coverage + BRSW_LT_OFF);
+					SndPlayFx(SND_15_BEEP);
+					SetWindowDirty(w);
+					break;
+
+				default:
+					break;
+			}
+		} break;
+
+		case WE_MOUSELOOP: {
+			if (WP(w, def_d).close) {
+				DeleteWindow(w);
+				return;
+			}
+
+			CheckRedrawStationCoverage(w);
+		} break;
+
+		case WE_DESTROY:
+			if (!WP(w, def_d).close) ResetObjectToPlace();
+			break;
+
+		default:
+			break;
 	}
 }
 
--- a/src/road_internal.h	Fri Apr 04 17:49:42 2008 +0000
+++ b/src/road_internal.h	Fri Apr 04 18:41:16 2008 +0000
@@ -31,6 +31,6 @@
  * @param ti   information about the tile (position, slope)
  * @param tram the roadbits to draw the catenary for
  */
-void DrawTramCatenary(TileInfo *ti, RoadBits tram);
+void DrawTramCatenary(const TileInfo *ti, RoadBits tram);
 
 #endif /* ROAD_INTERNAL_H */