diff --git a/src/rail_cmd.cpp b/src/rail_cmd.cpp index 85895a6722..24a5a4b792 100644 --- a/src/rail_cmd.cpp +++ b/src/rail_cmd.cpp @@ -485,37 +485,38 @@ CommandCost CmdBuildSingleRail(TileIndex tile, DoCommandFlag flags, uint32 p1, u RoadTypes roadtypes = GetRoadTypes(tile); RoadBits road = GetRoadBits(tile, ROADTYPE_ROAD); RoadBits tram = GetRoadBits(tile, ROADTYPE_TRAM); - if ((track == TRACK_X && (road | tram) == ROAD_Y) || - (track == TRACK_Y && (road | tram) == ROAD_X)) { - switch (roadtypes) { - default: break; - case ROADTYPES_TRAM: - /* Tram crossings must always have road. */ - if (flags & DC_EXEC) { - SetRoadOwner(tile, ROADTYPE_ROAD, _current_company); - Company *c = Company::GetIfValid(_current_company); - if (c != NULL) { - /* A full diagonal tile has two road bits. */ - c->infrastructure.road[ROADTYPE_ROAD] += 2; - DirtyCompanyInfrastructureWindows(c->index); - } - } - roadtypes |= ROADTYPES_ROAD; - cost.AddCost(2 * _price[PR_BUILD_ROAD]); - break; - - case ROADTYPES_ALL: - if (road != tram) return CMD_ERROR; - break; + if ((track == TRACK_X && ((road | tram) & ROAD_X) == 0) || + (track == TRACK_Y && ((road | tram) & ROAD_Y) == 0)) { + Owner road_owner = GetRoadOwner(tile, ROADTYPE_ROAD); + Owner tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM); + /* Disallow breaking end-of-line of someone else + * so trams can still reverse on this tile. */ + if (Company::IsValidID(tram_owner) && HasExactlyOneBit(tram)) { + CommandCost ret = CheckOwnership(tram_owner); + if (ret.Failed()) return ret; } + /* Crossings must always have a road... */ + uint num_new_road_pieces = 2 - CountBits(road); + if (road == ROAD_NONE) road_owner = _current_company; + roadtypes |= ROADTYPES_ROAD; + /* ...but tram is not required. */ + uint num_new_tram_pieces = (tram != ROAD_NONE) ? 2 - CountBits(tram) : 0; - road |= tram; + cost.AddCost((num_new_road_pieces + num_new_tram_pieces) * _price[PR_BUILD_ROAD]); if (flags & DC_EXEC) { - MakeRoadCrossing(tile, GetRoadOwner(tile, ROADTYPE_ROAD), GetRoadOwner(tile, ROADTYPE_TRAM), _current_company, (track == TRACK_X ? AXIS_Y : AXIS_X), railtype, roadtypes, GetTownIndex(tile)); + MakeRoadCrossing(tile, road_owner, tram_owner, _current_company, (track == TRACK_X ? AXIS_Y : AXIS_X), railtype, roadtypes, GetTownIndex(tile)); UpdateLevelCrossing(tile, false); Company::Get(_current_company)->infrastructure.rail[railtype] += LEVELCROSSING_TRACKBIT_FACTOR; DirtyCompanyInfrastructureWindows(_current_company); + if (num_new_road_pieces > 0 && Company::IsValidID(road_owner)) { + Company::Get(road_owner)->infrastructure.road[ROADTYPE_ROAD] += num_new_road_pieces; + DirtyCompanyInfrastructureWindows(road_owner); + } + if (num_new_tram_pieces > 0 && Company::IsValidID(tram_owner)) { + Company::Get(tram_owner)->infrastructure.road[ROADTYPE_TRAM] += num_new_tram_pieces; + DirtyCompanyInfrastructureWindows(tram_owner); + } } break; } diff --git a/src/road_cmd.cpp b/src/road_cmd.cpp index 449a8016bb..aa445eb91a 100644 --- a/src/road_cmd.cpp +++ b/src/road_cmd.cpp @@ -237,10 +237,10 @@ static CommandCost RemoveRoad(TileIndex tile, DoCommandFlag flags, RoadBits piec SetRoadTypes(tile, GetRoadTypes(tile) & ~RoadTypeToRoadTypes(rt)); /* If the owner of the bridge sells all its road, also move the ownership - * to the owner of the other roadtype. */ + * to the owner of the other roadtype, unless the bridge owner is a town. */ RoadType other_rt = (rt == ROADTYPE_ROAD) ? ROADTYPE_TRAM : ROADTYPE_ROAD; Owner other_owner = GetRoadOwner(tile, other_rt); - if (other_owner != GetTileOwner(tile)) { + if (!IsTileOwner(tile, other_owner) && !IsTileOwner(tile, OWNER_TOWN)) { SetTileOwner(tile, other_owner); SetTileOwner(other_end, other_owner); } @@ -384,7 +384,10 @@ static CommandCost RemoveRoad(TileIndex tile, DoCommandFlag flags, RoadBits piec /* Update rail count for level crossings. The plain track should still be accounted * for, so only subtract the difference to the level crossing cost. */ c = Company::GetIfValid(GetTileOwner(tile)); - if (c != NULL) c->infrastructure.rail[GetRailType(tile)] -= LEVELCROSSING_TRACKBIT_FACTOR - 1; + if (c != NULL) { + c->infrastructure.rail[GetRailType(tile)] -= LEVELCROSSING_TRACKBIT_FACTOR - 1; + DirtyCompanyInfrastructureWindows(c->index); + } } else { SetRoadTypes(tile, rts); } @@ -638,7 +641,10 @@ CommandCost CmdBuildRoad(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 /* Update rail count for level crossings. The plain track is already * counted, so only add the difference to the level crossing cost. */ c = Company::GetIfValid(GetTileOwner(tile)); - if (c != NULL) c->infrastructure.rail[GetRailType(tile)] += LEVELCROSSING_TRACKBIT_FACTOR - 1; + if (c != NULL) { + c->infrastructure.rail[GetRailType(tile)] += LEVELCROSSING_TRACKBIT_FACTOR - 1; + DirtyCompanyInfrastructureWindows(c->index); + } /* Always add road to the roadtypes (can't draw without it) */ bool reserved = HasBit(GetRailReservationTrackBits(tile), railtrack); diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp index 6a644b08eb..c2cb6bea08 100644 --- a/src/station_cmd.cpp +++ b/src/station_cmd.cpp @@ -1992,6 +1992,7 @@ static CommandCost RemoveRoadStop(TileIndex tile, DoCommandFlag flags) } } Company::Get(st->owner)->infrastructure.station--; + DirtyCompanyInfrastructureWindows(st->owner); if (IsDriveThroughStopTile(tile)) { /* Clears the tile for us */ diff --git a/src/tunnelbridge_cmd.cpp b/src/tunnelbridge_cmd.cpp index 574f07ae46..5f2534b58c 100644 --- a/src/tunnelbridge_cmd.cpp +++ b/src/tunnelbridge_cmd.cpp @@ -487,7 +487,7 @@ CommandCost CmdBuildBridge(TileIndex end_tile, DoCommandFlag flags, uint32 p1, u if (flags & DC_EXEC) { DiagDirection dir = AxisToDiagDir(direction); - Company *c = Company::GetIfValid(owner); + Company *c = Company::GetIfValid(company); switch (transport_type) { case TRANSPORT_RAIL: /* Add to company infrastructure count if required. */ @@ -510,13 +510,11 @@ CommandCost CmdBuildBridge(TileIndex end_tile, DoCommandFlag flags, uint32 p1, u RoadType new_rt; FOR_EACH_SET_ROADTYPE(new_rt, roadtypes ^ prev_roadtypes) { /* A full diagonal road tile has two road bits. */ - Company::Get(owner)->infrastructure.road[new_rt] += (bridge_len + 2) * 2 * TUNNELBRIDGE_TRACKBIT_FACTOR; + c->infrastructure.road[new_rt] += (bridge_len + 2) * 2 * TUNNELBRIDGE_TRACKBIT_FACTOR; } } - Owner owner_road = owner; - Owner owner_tram = owner; - if (HasBit(prev_roadtypes, ROADTYPE_ROAD)) owner_road = GetRoadOwner(tile_start, ROADTYPE_ROAD); - if (HasBit(prev_roadtypes, ROADTYPE_TRAM)) owner_tram = GetRoadOwner(tile_start, ROADTYPE_TRAM); + Owner owner_road = HasBit(prev_roadtypes, ROADTYPE_ROAD) ? GetRoadOwner(tile_start, ROADTYPE_ROAD) : company; + Owner owner_tram = HasBit(prev_roadtypes, ROADTYPE_TRAM) ? GetRoadOwner(tile_start, ROADTYPE_TRAM) : company; MakeRoadBridgeRamp(tile_start, owner, owner_road, owner_tram, bridge_type, dir, roadtypes); MakeRoadBridgeRamp(tile_end, owner, owner_road, owner_tram, bridge_type, ReverseDiagDir(dir), roadtypes); break; @@ -534,7 +532,7 @@ CommandCost CmdBuildBridge(TileIndex end_tile, DoCommandFlag flags, uint32 p1, u /* Mark all tiles dirty */ MarkBridgeDirty(tile_start, tile_end, AxisToDiagDir(direction), z_start); - DirtyCompanyInfrastructureWindows(owner); + DirtyCompanyInfrastructureWindows(company); } if ((flags & DC_EXEC) && transport_type == TRANSPORT_RAIL) {