diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp index 71feb7bcb0..7320194e04 100644 --- a/src/station_cmd.cpp +++ b/src/station_cmd.cpp @@ -1601,7 +1601,7 @@ CommandCost CmdBuildRailStation(TileIndex tile_org, DoCommandFlag flags, uint32 byte old_specindex = HasStationTileRail(tile) ? GetCustomStationSpecIndex(tile) : 0; MakeRailStation(tile, st->owner, st->index, axis, layout & ~1, rt); /* Free the spec if we overbuild something */ - DeallocateSpecFromStation(st, old_specindex); + if (old_specindex != specindex) DeallocateSpecFromStation(st, old_specindex); SetCustomStationSpecIndex(tile, specindex); SetStationTileRandomBits(tile, GB(Random(), 0, 4)); @@ -2033,7 +2033,7 @@ static RoadStop **FindRoadStopSpot(bool truck_station, Station *st) } } -CommandCost RemoveRoadStop(TileIndex tile, DoCommandFlag flags); +CommandCost RemoveRoadStop(TileIndex tile, DoCommandFlag flags, int replacement_spec_index = -1); /** * Find a nearby station that joins this road stop. @@ -2165,7 +2165,7 @@ CommandCost CmdBuildRoadStop(TileIndex tile, DoCommandFlag flags, uint32 p1, uin DisallowedRoadDirections drd = IsNormalRoadTile(cur_tile) ? GetDisallowedRoadDirections(cur_tile) : DRD_NONE; if (IsTileType(cur_tile, MP_STATION) && IsAnyRoadStop(cur_tile)) { - RemoveRoadStop(cur_tile, flags); + RemoveRoadStop(cur_tile, flags, specindex); } if (roadstopspec != nullptr) { @@ -2251,7 +2251,7 @@ static Vehicle *ClearRoadStopStatusEnum(Vehicle *v, void *) return nullptr; } -CommandCost RemoveRoadWaypointStop(TileIndex tile, DoCommandFlag flags) +CommandCost RemoveRoadWaypointStop(TileIndex tile, DoCommandFlag flags, int replacement_spec_index) { Waypoint *wp = Waypoint::GetByTile(tile); @@ -2287,18 +2287,20 @@ CommandCost RemoveRoadWaypointStop(TileIndex tile, DoCommandFlag flags) wp->rect.AfterRemoveTile(wp, tile); wp->RemoveRoadStopTileData(tile); - DeallocateRoadStopSpecFromStation(wp, specindex); + if ((int)specindex != replacement_spec_index) DeallocateRoadStopSpecFromStation(wp, specindex); - MakeRoadWaypointStationAreaSmaller(wp, wp->road_waypoint_area); + if (replacement_spec_index < 0) { + MakeRoadWaypointStationAreaSmaller(wp, wp->road_waypoint_area); - UpdateStationSignCoord(wp); + UpdateStationSignCoord(wp); - /* if we deleted the whole waypoint, delete the road facility. */ - if (wp->road_waypoint_area.tile == INVALID_TILE) { - wp->facilities &= ~(FACIL_BUS_STOP | FACIL_TRUCK_STOP); - SetWindowWidgetDirty(WC_STATION_VIEW, wp->index, WID_SV_ROADVEHS); - wp->UpdateVirtCoord(); - DeleteStationIfEmpty(wp); + /* if we deleted the whole waypoint, delete the road facility. */ + if (wp->road_waypoint_area.tile == INVALID_TILE) { + wp->facilities &= ~(FACIL_BUS_STOP | FACIL_TRUCK_STOP); + SetWindowWidgetDirty(WC_STATION_VIEW, wp->index, WID_SV_ROADVEHS); + wp->UpdateVirtCoord(); + DeleteStationIfEmpty(wp); + } } NotifyRoadLayoutChanged(false); @@ -2311,12 +2313,13 @@ CommandCost RemoveRoadWaypointStop(TileIndex tile, DoCommandFlag flags) * Remove a bus station/truck stop * @param tile TileIndex been queried * @param flags operation to perform + * @param replacement_spec_index replacement spec index to avoid deallocating, if < 0, tile is not being replaced * @return cost or failure of operation */ -CommandCost RemoveRoadStop(TileIndex tile, DoCommandFlag flags) +CommandCost RemoveRoadStop(TileIndex tile, DoCommandFlag flags, int replacement_spec_index) { if (IsRoadWaypoint(tile)) { - return RemoveRoadWaypointStop(tile, flags); + return RemoveRoadWaypointStop(tile, flags, replacement_spec_index); } Station *st = Station::GetByTile(tile); @@ -2399,10 +2402,10 @@ CommandCost RemoveRoadStop(TileIndex tile, DoCommandFlag flags) st->rect.AfterRemoveTile(st, tile); - st->AfterStationTileSetChange(false, is_truck ? STATION_TRUCK: STATION_BUS); + if (replacement_spec_index < 0) st->AfterStationTileSetChange(false, is_truck ? STATION_TRUCK: STATION_BUS); st->RemoveRoadStopTileData(tile); - DeallocateRoadStopSpecFromStation(st, specindex); + if ((int)specindex != replacement_spec_index) DeallocateRoadStopSpecFromStation(st, specindex); /* Update the tile area of the truck/bus stop */ if (is_truck) { diff --git a/src/waypoint_cmd.cpp b/src/waypoint_cmd.cpp index baf7e989e3..a54daa3aa8 100644 --- a/src/waypoint_cmd.cpp +++ b/src/waypoint_cmd.cpp @@ -315,11 +315,11 @@ CommandCost CmdBuildRailWaypoint(TileIndex start_tile, DoCommandFlag flags, uint HasBit(GetRailReservationTrackBits(tile), AxisToTrack(axis)) : HasStationReservation(tile); MakeRailWaypoint(tile, wp->owner, wp->index, axis, layout_ptr[i], GetRailType(tile)); + if (old_specindex != map_spec_index) DeallocateSpecFromStation(wp, old_specindex); SetCustomStationSpecIndex(tile, map_spec_index); SetRailStationReservation(tile, reserved); MarkTileDirtyByTile(tile, VMDF_NOT_MAP_MODE); - DeallocateSpecFromStation(wp, old_specindex); YapfNotifyTrackLayoutChange(tile, AxisToTrack(axis)); } DirtyCompanyInfrastructureWindows(wp->owner); @@ -446,9 +446,9 @@ CommandCost CmdBuildRoadWaypoint(TileIndex start_tile, DoCommandFlag flags, uint Owner tram_owner = tram_rt != INVALID_ROADTYPE ? GetRoadOwner(cur_tile, RTT_TRAM) : _current_company; DisallowedRoadDirections drd = IsNormalRoadTile(cur_tile) ? GetDisallowedRoadDirections(cur_tile) : DRD_NONE; - extern CommandCost RemoveRoadStop(TileIndex tile, DoCommandFlag flags); + extern CommandCost RemoveRoadStop(TileIndex tile, DoCommandFlag flags, int replacement_spec_index); if (IsTileType(cur_tile, MP_STATION) && IsAnyRoadStop(cur_tile)) { - RemoveRoadStop(cur_tile, flags); + RemoveRoadStop(cur_tile, flags, map_spec_index); } wp->road_waypoint_area.Add(cur_tile);