diff --git a/src/aircraft.h b/src/aircraft.h index dbf9bfc63c..0beab20dfb 100644 --- a/src/aircraft.h +++ b/src/aircraft.h @@ -107,6 +107,7 @@ struct Aircraft FINAL : public SpecializedVehicle { } bool Tick() override; + void OnNewCalendarDay() override; void OnNewEconomyDay() override; uint Crash(bool flooded = false) override; TileIndex GetOrderStationLocation(StationID station) override; diff --git a/src/aircraft_cmd.cpp b/src/aircraft_cmd.cpp index 5ce05b82b1..6002a05f20 100644 --- a/src/aircraft_cmd.cpp +++ b/src/aircraft_cmd.cpp @@ -440,6 +440,14 @@ Money Aircraft::GetRunningCost() const return GetPrice(PR_RUNNING_AIRCRAFT, cost_factor, e->GetGRF()); } +/** Calendar day handler */ +void Aircraft::OnNewCalendarDay() +{ + if (!this->IsNormalAircraft()) return; + AgeVehicle(this); +} + +/** Economy day handler */ void Aircraft::OnNewEconomyDay() { if (!this->IsNormalAircraft()) return; @@ -449,7 +457,6 @@ void Aircraft::OnNewEconomyDay() CheckOrders(this); CheckVehicleBreakdown(this); - AgeVehicle(this); CheckIfAircraftNeedsService(this); if (this->running_ticks == 0) return; diff --git a/src/openttd.cpp b/src/openttd.cpp index c8106c8792..50521ab663 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -1415,6 +1415,7 @@ void StateGameLoop() TimerManager::Elapsed(1); TimerManager::Elapsed(1); RunTileLoop(); + RunVehicleCalendarDayProc(); CallVehicleTicks(); CallLandscapeTick(); BasePersistentStorageArray::SwitchMode(PSM_LEAVE_GAMELOOP); diff --git a/src/roadveh.h b/src/roadveh.h index e4cb266919..f05e7ee3a6 100644 --- a/src/roadveh.h +++ b/src/roadveh.h @@ -134,6 +134,7 @@ struct RoadVehicle FINAL : public GroundVehicle { int GetDisplayImageWidth(Point *offset = nullptr) const; bool IsInDepot() const override { return this->state == RVSB_IN_DEPOT; } bool Tick() override; + void OnNewCalendarDay() override; void OnNewEconomyDay() override; uint Crash(bool flooded = false) override; Trackdir GetVehicleTrackdir() const override; diff --git a/src/roadveh_cmd.cpp b/src/roadveh_cmd.cpp index 0998849bce..066ea84762 100644 --- a/src/roadveh_cmd.cpp +++ b/src/roadveh_cmd.cpp @@ -1706,10 +1706,16 @@ static void CheckIfRoadVehNeedsService(RoadVehicle *v) SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP); } +/** Calandar day handler */ +void RoadVehicle::OnNewCalendarDay() +{ + if (!this->IsFrontEngine()) return; + AgeVehicle(this); +} + +/** Economy day handler. */ void RoadVehicle::OnNewEconomyDay() { - AgeVehicle(this); - if (!this->IsFrontEngine()) return; if ((++this->day_counter & 7) == 0) DecreaseVehicleValue(this); diff --git a/src/ship.h b/src/ship.h index 85e3ee64ca..955bbf6bfe 100644 --- a/src/ship.h +++ b/src/ship.h @@ -45,6 +45,7 @@ struct Ship FINAL : public SpecializedVehicle { Money GetRunningCost() const override; bool IsInDepot() const override { return this->state == TRACK_BIT_DEPOT; } bool Tick() override; + void OnNewCalendarDay() override; void OnNewEconomyDay() override; Trackdir GetVehicleTrackdir() const override; TileIndex GetOrderStationLocation(StationID station) override; diff --git a/src/ship_cmd.cpp b/src/ship_cmd.cpp index fc5299f995..5e2ff388af 100644 --- a/src/ship_cmd.cpp +++ b/src/ship_cmd.cpp @@ -223,6 +223,13 @@ Money Ship::GetRunningCost() const return GetPrice(PR_RUNNING_SHIP, cost_factor, e->GetGRF()); } +/** Calendar day handler. */ +void Ship::OnNewCalendarDay() +{ + AgeVehicle(this); +} + +/** Economy day handler. */ void Ship::OnNewEconomyDay() { if ((++this->day_counter & 7) == 0) { @@ -230,7 +237,6 @@ void Ship::OnNewEconomyDay() } CheckVehicleBreakdown(this); - AgeVehicle(this); CheckIfShipNeedsService(this); CheckOrders(this); diff --git a/src/train.h b/src/train.h index f6209d7b19..f863250e5c 100644 --- a/src/train.h +++ b/src/train.h @@ -123,6 +123,7 @@ struct Train FINAL : public GroundVehicle { int GetDisplayImageWidth(Point *offset = nullptr) const; bool IsInDepot() const override { return this->track == TRACK_BIT_DEPOT; } bool Tick() override; + void OnNewCalendarDay() override; void OnNewEconomyDay() override; uint Crash(bool flooded = false) override; Trackdir GetVehicleTrackdir() const override; diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index 73c66fadd9..5d019380d2 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -4164,11 +4164,15 @@ static void CheckIfTrainNeedsService(Train *v) SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP); } -/** Update day counters of the train vehicle. */ -void Train::OnNewEconomyDay() +/** Calendar day handler. */ +void Train::OnNewCalendarDay() { AgeVehicle(this); +} +/** Economy day handler. */ +void Train::OnNewEconomyDay() +{ if ((++this->day_counter & 7) == 0) DecreaseVehicleValue(this); if (this->IsFrontEngine()) { diff --git a/src/vehicle.cpp b/src/vehicle.cpp index 16cabda120..b2f5e37c90 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -916,6 +916,21 @@ void VehicleEnteredDepotThisTick(Vehicle *v) v->vehstatus |= VS_STOPPED; } +/** + * Age all vehicles, spreading out the action using the current TimerGameCalendar::date_fract. + */ +void RunVehicleCalendarDayProc() +{ + if (_game_mode != GM_NORMAL) return; + + /* Run the calendar day proc for every DAY_TICKS vehicle starting at TimerGameCalendar::date_fract. */ + for (size_t i = TimerGameCalendar::date_fract; i < Vehicle::GetPoolSize(); i += Ticks::DAY_TICKS) { + Vehicle *v = Vehicle::Get(i); + if (v == nullptr) continue; + v->OnNewCalendarDay(); + } +} + /** * Increases the day counter for all vehicles and calls 1-day and 32-day handlers. * Each tick, it processes vehicles with "index % DAY_TICKS == TimerGameEconomy::date_fract", diff --git a/src/vehicle_base.h b/src/vehicle_base.h index 60056bf418..313ec18346 100644 --- a/src/vehicle_base.h +++ b/src/vehicle_base.h @@ -567,7 +567,12 @@ public: virtual bool Tick() { return true; }; /** - * Calls the new economy day handler of the vehicle + * Calls the new calendar day handler of the vehicle. + */ + virtual void OnNewCalendarDay() {}; + + /** + * Calls the new economy day handler of the vehicle. */ virtual void OnNewEconomyDay() {}; diff --git a/src/vehicle_func.h b/src/vehicle_func.h index 7595c9478b..f2d67227d5 100644 --- a/src/vehicle_func.h +++ b/src/vehicle_func.h @@ -62,6 +62,7 @@ CommandCost TunnelBridgeIsFree(TileIndex tile, TileIndex endtile, const Vehicle void DecreaseVehicleValue(Vehicle *v); void CheckVehicleBreakdown(Vehicle *v); void AgeVehicle(Vehicle *v); +void RunVehicleCalendarDayProc(); void VehicleEnteredDepotThisTick(Vehicle *v); UnitID GetFreeUnitNumber(VehicleType type);