Fix spec allocation issues when overbuilding a road stop with the same type

This commit is contained in:
Jonathan G Rennison
2022-02-24 18:19:00 +00:00
parent 6b03575dcd
commit 5109083564
2 changed files with 23 additions and 20 deletions

View File

@@ -1601,7 +1601,7 @@ CommandCost CmdBuildRailStation(TileIndex tile_org, DoCommandFlag flags, uint32
byte old_specindex = HasStationTileRail(tile) ? GetCustomStationSpecIndex(tile) : 0; byte old_specindex = HasStationTileRail(tile) ? GetCustomStationSpecIndex(tile) : 0;
MakeRailStation(tile, st->owner, st->index, axis, layout & ~1, rt); MakeRailStation(tile, st->owner, st->index, axis, layout & ~1, rt);
/* Free the spec if we overbuild something */ /* Free the spec if we overbuild something */
DeallocateSpecFromStation(st, old_specindex); if (old_specindex != specindex) DeallocateSpecFromStation(st, old_specindex);
SetCustomStationSpecIndex(tile, specindex); SetCustomStationSpecIndex(tile, specindex);
SetStationTileRandomBits(tile, GB(Random(), 0, 4)); 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. * 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; DisallowedRoadDirections drd = IsNormalRoadTile(cur_tile) ? GetDisallowedRoadDirections(cur_tile) : DRD_NONE;
if (IsTileType(cur_tile, MP_STATION) && IsAnyRoadStop(cur_tile)) { if (IsTileType(cur_tile, MP_STATION) && IsAnyRoadStop(cur_tile)) {
RemoveRoadStop(cur_tile, flags); RemoveRoadStop(cur_tile, flags, specindex);
} }
if (roadstopspec != nullptr) { if (roadstopspec != nullptr) {
@@ -2251,7 +2251,7 @@ static Vehicle *ClearRoadStopStatusEnum(Vehicle *v, void *)
return nullptr; return nullptr;
} }
CommandCost RemoveRoadWaypointStop(TileIndex tile, DoCommandFlag flags) CommandCost RemoveRoadWaypointStop(TileIndex tile, DoCommandFlag flags, int replacement_spec_index)
{ {
Waypoint *wp = Waypoint::GetByTile(tile); Waypoint *wp = Waypoint::GetByTile(tile);
@@ -2287,8 +2287,9 @@ CommandCost RemoveRoadWaypointStop(TileIndex tile, DoCommandFlag flags)
wp->rect.AfterRemoveTile(wp, tile); wp->rect.AfterRemoveTile(wp, tile);
wp->RemoveRoadStopTileData(tile); wp->RemoveRoadStopTileData(tile);
DeallocateRoadStopSpecFromStation(wp, specindex); if ((int)specindex != replacement_spec_index) DeallocateRoadStopSpecFromStation(wp, specindex);
if (replacement_spec_index < 0) {
MakeRoadWaypointStationAreaSmaller(wp, wp->road_waypoint_area); MakeRoadWaypointStationAreaSmaller(wp, wp->road_waypoint_area);
UpdateStationSignCoord(wp); UpdateStationSignCoord(wp);
@@ -2300,6 +2301,7 @@ CommandCost RemoveRoadWaypointStop(TileIndex tile, DoCommandFlag flags)
wp->UpdateVirtCoord(); wp->UpdateVirtCoord();
DeleteStationIfEmpty(wp); DeleteStationIfEmpty(wp);
} }
}
NotifyRoadLayoutChanged(false); NotifyRoadLayoutChanged(false);
} }
@@ -2311,12 +2313,13 @@ CommandCost RemoveRoadWaypointStop(TileIndex tile, DoCommandFlag flags)
* Remove a bus station/truck stop * Remove a bus station/truck stop
* @param tile TileIndex been queried * @param tile TileIndex been queried
* @param flags operation to perform * @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 * @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)) { if (IsRoadWaypoint(tile)) {
return RemoveRoadWaypointStop(tile, flags); return RemoveRoadWaypointStop(tile, flags, replacement_spec_index);
} }
Station *st = Station::GetByTile(tile); Station *st = Station::GetByTile(tile);
@@ -2399,10 +2402,10 @@ CommandCost RemoveRoadStop(TileIndex tile, DoCommandFlag flags)
st->rect.AfterRemoveTile(st, tile); 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); st->RemoveRoadStopTileData(tile);
DeallocateRoadStopSpecFromStation(st, specindex); if ((int)specindex != replacement_spec_index) DeallocateRoadStopSpecFromStation(st, specindex);
/* Update the tile area of the truck/bus stop */ /* Update the tile area of the truck/bus stop */
if (is_truck) { if (is_truck) {

View File

@@ -315,11 +315,11 @@ CommandCost CmdBuildRailWaypoint(TileIndex start_tile, DoCommandFlag flags, uint
HasBit(GetRailReservationTrackBits(tile), AxisToTrack(axis)) : HasBit(GetRailReservationTrackBits(tile), AxisToTrack(axis)) :
HasStationReservation(tile); HasStationReservation(tile);
MakeRailWaypoint(tile, wp->owner, wp->index, axis, layout_ptr[i], GetRailType(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); SetCustomStationSpecIndex(tile, map_spec_index);
SetRailStationReservation(tile, reserved); SetRailStationReservation(tile, reserved);
MarkTileDirtyByTile(tile, VMDF_NOT_MAP_MODE); MarkTileDirtyByTile(tile, VMDF_NOT_MAP_MODE);
DeallocateSpecFromStation(wp, old_specindex);
YapfNotifyTrackLayoutChange(tile, AxisToTrack(axis)); YapfNotifyTrackLayoutChange(tile, AxisToTrack(axis));
} }
DirtyCompanyInfrastructureWindows(wp->owner); 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; Owner tram_owner = tram_rt != INVALID_ROADTYPE ? GetRoadOwner(cur_tile, RTT_TRAM) : _current_company;
DisallowedRoadDirections drd = IsNormalRoadTile(cur_tile) ? GetDisallowedRoadDirections(cur_tile) : DRD_NONE; 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)) { 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); wp->road_waypoint_area.Add(cur_tile);