diff --git a/src/tracerestrict.cpp b/src/tracerestrict.cpp index f6174ee648..05b8ae8c47 100644 --- a/src/tracerestrict.cpp +++ b/src/tracerestrict.cpp @@ -75,9 +75,6 @@ INSTANTIATE_POOL_METHODS(TraceRestrictSlot) TraceRestrictCounterPool _tracerestrictcounter_pool("TraceRestrictCounter"); INSTANTIATE_POOL_METHODS(TraceRestrictCounter) -std::vector TraceRestrictSlot::veh_temporarily_added; -std::vector TraceRestrictSlot::veh_temporarily_removed; - /** * TraceRestrictRefId --> TraceRestrictProgramID (Pool ID) mapping * The indirection is mainly to enable shared programs @@ -245,10 +242,13 @@ static bool TestStationCondition(StationID station, TraceRestrictItem item) */ void TraceRestrictProgram::Execute(const Train* v, const TraceRestrictProgramInput &input, TraceRestrictProgramResult& out) const { - // static to avoid needing to re-alloc/resize on each execution + /* static to avoid needing to re-alloc/resize on each execution */ static std::vector condstack; condstack.clear(); + /* Only for use with TRPISP_PBS_RES_END_ACQ_DRY and TRPAUF_PBS_RES_END_SIMULATE */ + static TraceRestrictSlotTemporaryState pbs_res_end_acq_dry_slot_temporary_state; + byte have_previous_signal = 0; TileIndex previous_signal_tile[3]; @@ -751,7 +751,7 @@ void TraceRestrictProgram::Execute(const Train* v, const TraceRestrictProgramInp if (!slot->Occupy(v->index)) out.flags |= TRPRF_PBS_RES_END_WAIT; } else if (input.permitted_slot_operations & TRPISP_PBS_RES_END_ACQ_DRY) { if (this->actions_used_flags & TRPAUF_PBS_RES_END_SIMULATE) { - if (!slot->OccupyDryRunUsingTemporaryState(v->index)) out.flags |= TRPRF_PBS_RES_END_WAIT; + if (!slot->OccupyUsingTemporaryState(v->index, &pbs_res_end_acq_dry_slot_temporary_state)) out.flags |= TRPRF_PBS_RES_END_WAIT; } else { if (!slot->OccupyDryRun(v->index)) out.flags |= TRPRF_PBS_RES_END_WAIT; } @@ -762,7 +762,7 @@ void TraceRestrictProgram::Execute(const Train* v, const TraceRestrictProgramInp if (input.permitted_slot_operations & TRPISP_PBS_RES_END_ACQUIRE) { slot->Occupy(v->index); } else if ((input.permitted_slot_operations & TRPISP_PBS_RES_END_ACQ_DRY) && (this->actions_used_flags & TRPAUF_PBS_RES_END_SIMULATE)) { - slot->OccupyDryRunUsingTemporaryState(v->index); + slot->OccupyUsingTemporaryState(v->index, &pbs_res_end_acq_dry_slot_temporary_state); } break; @@ -770,7 +770,7 @@ void TraceRestrictProgram::Execute(const Train* v, const TraceRestrictProgramInp if (input.permitted_slot_operations & TRPISP_PBS_RES_END_RELEASE) { slot->Vacate(v->index); } else if ((input.permitted_slot_operations & TRPISP_PBS_RES_END_ACQ_DRY) && (this->actions_used_flags & TRPAUF_PBS_RES_END_SIMULATE)) { - slot->VacateUsingTemporaryState(v->index); + slot->VacateUsingTemporaryState(v->index, &pbs_res_end_acq_dry_slot_temporary_state); } break; @@ -895,7 +895,7 @@ void TraceRestrictProgram::Execute(const Train* v, const TraceRestrictProgramInp } } if ((input.permitted_slot_operations & TRPISP_PBS_RES_END_ACQ_DRY) && (this->actions_used_flags & TRPAUF_PBS_RES_END_SIMULATE)) { - TraceRestrictSlot::RevertTemporaryChanges(v->index); + pbs_res_end_acq_dry_slot_temporary_state.RevertTemporaryChanges(v->index); } assert(condstack.empty()); } @@ -2558,19 +2558,20 @@ bool TraceRestrictSlot::OccupyDryRun(VehicleID id) } /** - * Dry-run adding vehicle ID to occupants if possible and not already an occupant, record any changes in the temporary state to be reverted later + * Add vehicle ID to occupants if possible and not already an occupant, record any changes in the temporary state to be reverted later * @param id Vehicle ID + * @param state Temporary state * @return whether vehicle ID is now an occupant */ -bool TraceRestrictSlot::OccupyDryRunUsingTemporaryState(VehicleID id) +bool TraceRestrictSlot::OccupyUsingTemporaryState(VehicleID id, TraceRestrictSlotTemporaryState *state) { if (this->IsOccupant(id)) return true; if (this->occupants.size() >= this->max_occupancy) return false; this->occupants.push_back(id); - if (find_index(veh_temporarily_removed, this->index) < 0) { - include(veh_temporarily_added, this->index); + if (find_index(state->veh_temporarily_removed, this->index) < 0) { + include(state->veh_temporarily_added, this->index); } return true; @@ -2591,12 +2592,13 @@ void TraceRestrictSlot::Vacate(VehicleID id) /** * Remove vehicle ID from occupants, record any changes in the temporary state to be reverted later * @param id Vehicle ID + * @param state Temporary state */ -void TraceRestrictSlot::VacateUsingTemporaryState(VehicleID id) +void TraceRestrictSlot::VacateUsingTemporaryState(VehicleID id, TraceRestrictSlotTemporaryState *state) { if (container_unordered_remove(this->occupants, id)) { - if (find_index(veh_temporarily_added, this->index) < 0) { - include(veh_temporarily_removed, this->index); + if (find_index(state->veh_temporarily_added, this->index) < 0) { + include(state->veh_temporarily_removed, this->index); } } } @@ -2688,18 +2690,18 @@ void TraceRestrictSlot::PreCleanPool() } /** Revert any temporary changes */ -void TraceRestrictSlot::RevertTemporaryChanges(VehicleID veh) +void TraceRestrictSlotTemporaryState::RevertTemporaryChanges(VehicleID veh) { - for (TraceRestrictSlotID id : veh_temporarily_added) { + for (TraceRestrictSlotID id : this->veh_temporarily_added) { TraceRestrictSlot *slot = TraceRestrictSlot::Get(id); container_unordered_remove(slot->occupants, veh); } - for (TraceRestrictSlotID id : veh_temporarily_removed) { + for (TraceRestrictSlotID id : this->veh_temporarily_removed) { TraceRestrictSlot *slot = TraceRestrictSlot::Get(id); include(slot->occupants, veh); } - veh_temporarily_added.clear(); - veh_temporarily_removed.clear(); + this->veh_temporarily_added.clear(); + this->veh_temporarily_removed.clear(); } /** Remove vehicle ID from all slot occupants */ diff --git a/src/tracerestrict.h b/src/tracerestrict.h index 8a1353ffae..9bed9a83b0 100644 --- a/src/tracerestrict.h +++ b/src/tracerestrict.h @@ -500,6 +500,13 @@ enum TraceRestrictProgramInputFlags : uint8_t { }; DECLARE_ENUM_AS_BIT_SET(TraceRestrictProgramInputFlags) +struct TraceRestrictSlotTemporaryState { + std::vector veh_temporarily_added; + std::vector veh_temporarily_removed; + + void RevertTemporaryChanges(VehicleID veh); +}; + /** * Execution input of a TraceRestrictProgram */ @@ -1144,14 +1151,10 @@ struct TraceRestrictSlot : TraceRestrictSlotPool::PoolItem<&_tracerestrictslot_p std::vector progsig_dependants; - static std::vector veh_temporarily_added; - static std::vector veh_temporarily_removed; - static void RebuildVehicleIndex(); static bool ValidateVehicleIndex(); static void ValidateSlotOccupants(std::function log); static void PreCleanPool(); - static void RevertTemporaryChanges(VehicleID veh); TraceRestrictSlot(CompanyID owner = INVALID_COMPANY, VehicleType type = VEH_TRAIN) { @@ -1174,9 +1177,9 @@ struct TraceRestrictSlot : TraceRestrictSlotPool::PoolItem<&_tracerestrictslot_p bool Occupy(VehicleID id, bool force = false); bool OccupyDryRun(VehicleID ids); - bool OccupyDryRunUsingTemporaryState(VehicleID id); + bool OccupyUsingTemporaryState(VehicleID id, TraceRestrictSlotTemporaryState *state); void Vacate(VehicleID id); - void VacateUsingTemporaryState(VehicleID id); + void VacateUsingTemporaryState(VehicleID id, TraceRestrictSlotTemporaryState *state); void Clear(); void UpdateSignals();