Fix #14631, Fix 1cb0cbcb6c: Waypoint customs spec not allocated properly on initial construction. (#14633)
Split AllocateSpecToStation/RoadStop into Allocate and Assign functions, allowing command tests to occur separately.
This commit is contained in:
@@ -692,16 +692,18 @@ CommandCost PerformStationTileSlopeCheck(TileIndex north_tile, TileIndex cur_til
|
||||
|
||||
/**
|
||||
* Allocate a StationSpec to a Station. This is called once per build operation.
|
||||
* @param statspec StationSpec to allocate.
|
||||
* @param spec StationSpec to allocate.
|
||||
* @param st Station to allocate it to.
|
||||
* @param exec Whether to actually allocate the spec.
|
||||
* @return Index within the Station's station spec list, or std::nullopt if the allocation failed.
|
||||
*/
|
||||
std::optional<uint8_t> AllocateSpecToStation(const StationSpec *statspec, BaseStation *st, bool exec)
|
||||
std::optional<uint8_t> AllocateSpecToStation(const StationSpec *spec, BaseStation *st)
|
||||
{
|
||||
uint i;
|
||||
|
||||
if (statspec == nullptr || st == nullptr) return 0;
|
||||
if (spec == nullptr) return 0;
|
||||
|
||||
/* If station doesn't exist yet then the first slot is available. */
|
||||
if (st == nullptr) return 1;
|
||||
|
||||
for (i = 1; i < st->speclist.size() && i < NUM_STATIONSSPECS_PER_STATION; i++) {
|
||||
if (st->speclist[i].spec == nullptr && st->speclist[i].grfid == 0) break;
|
||||
@@ -714,24 +716,32 @@ std::optional<uint8_t> AllocateSpecToStation(const StationSpec *statspec, BaseSt
|
||||
* but it's fairly unlikely that one reaches the limit anyways.
|
||||
*/
|
||||
for (i = 1; i < st->speclist.size() && i < NUM_STATIONSSPECS_PER_STATION; i++) {
|
||||
if (st->speclist[i].spec == statspec) return i;
|
||||
if (st->speclist[i].spec == spec) return i;
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
if (exec) {
|
||||
if (i >= st->speclist.size()) st->speclist.resize(i + 1);
|
||||
st->speclist[i].spec = statspec;
|
||||
st->speclist[i].grfid = statspec->grf_prop.grfid;
|
||||
st->speclist[i].localidx = statspec->grf_prop.local_id;
|
||||
|
||||
StationUpdateCachedTriggers(st);
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign a previously allocated StationSpec specindex to a Station.
|
||||
* @param spec StationSpec to assign..
|
||||
* @param st Station to allocate it to.
|
||||
* @param specindex Spec index of allocation.
|
||||
*/
|
||||
void AssignSpecToStation(const StationSpec *spec, BaseStation *st, uint8_t specindex)
|
||||
{
|
||||
if (specindex == 0) return;
|
||||
if (specindex >= st->speclist.size()) st->speclist.resize(specindex + 1);
|
||||
|
||||
st->speclist[specindex].spec = spec;
|
||||
st->speclist[specindex].grfid = spec->grf_prop.grfid;
|
||||
st->speclist[specindex].localidx = spec->grf_prop.local_id;
|
||||
|
||||
StationUpdateCachedTriggers(st);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deallocate a StationSpec from a Station. Called when removing a single station tile.
|
||||
|
||||
Reference in New Issue
Block a user