mirror of https://github.com/OpenTTD/OpenTTD
Codechange: Split vehicle aging into its own calendar day proc
parent
82ce84e5f9
commit
9d8a55cef7
|
@ -107,6 +107,7 @@ struct Aircraft FINAL : public SpecializedVehicle<Aircraft, VEH_AIRCRAFT> {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Tick() override;
|
bool Tick() override;
|
||||||
|
void OnNewCalendarDay() override;
|
||||||
void OnNewEconomyDay() override;
|
void OnNewEconomyDay() override;
|
||||||
uint Crash(bool flooded = false) override;
|
uint Crash(bool flooded = false) override;
|
||||||
TileIndex GetOrderStationLocation(StationID station) override;
|
TileIndex GetOrderStationLocation(StationID station) override;
|
||||||
|
|
|
@ -440,6 +440,14 @@ Money Aircraft::GetRunningCost() const
|
||||||
return GetPrice(PR_RUNNING_AIRCRAFT, cost_factor, e->GetGRF());
|
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()
|
void Aircraft::OnNewEconomyDay()
|
||||||
{
|
{
|
||||||
if (!this->IsNormalAircraft()) return;
|
if (!this->IsNormalAircraft()) return;
|
||||||
|
@ -449,7 +457,6 @@ void Aircraft::OnNewEconomyDay()
|
||||||
CheckOrders(this);
|
CheckOrders(this);
|
||||||
|
|
||||||
CheckVehicleBreakdown(this);
|
CheckVehicleBreakdown(this);
|
||||||
AgeVehicle(this);
|
|
||||||
CheckIfAircraftNeedsService(this);
|
CheckIfAircraftNeedsService(this);
|
||||||
|
|
||||||
if (this->running_ticks == 0) return;
|
if (this->running_ticks == 0) return;
|
||||||
|
|
|
@ -1415,6 +1415,7 @@ void StateGameLoop()
|
||||||
TimerManager<TimerGameEconomy>::Elapsed(1);
|
TimerManager<TimerGameEconomy>::Elapsed(1);
|
||||||
TimerManager<TimerGameTick>::Elapsed(1);
|
TimerManager<TimerGameTick>::Elapsed(1);
|
||||||
RunTileLoop();
|
RunTileLoop();
|
||||||
|
RunVehicleCalendarDayProc();
|
||||||
CallVehicleTicks();
|
CallVehicleTicks();
|
||||||
CallLandscapeTick();
|
CallLandscapeTick();
|
||||||
BasePersistentStorageArray::SwitchMode(PSM_LEAVE_GAMELOOP);
|
BasePersistentStorageArray::SwitchMode(PSM_LEAVE_GAMELOOP);
|
||||||
|
|
|
@ -134,6 +134,7 @@ struct RoadVehicle FINAL : public GroundVehicle<RoadVehicle, VEH_ROAD> {
|
||||||
int GetDisplayImageWidth(Point *offset = nullptr) const;
|
int GetDisplayImageWidth(Point *offset = nullptr) const;
|
||||||
bool IsInDepot() const override { return this->state == RVSB_IN_DEPOT; }
|
bool IsInDepot() const override { return this->state == RVSB_IN_DEPOT; }
|
||||||
bool Tick() override;
|
bool Tick() override;
|
||||||
|
void OnNewCalendarDay() override;
|
||||||
void OnNewEconomyDay() override;
|
void OnNewEconomyDay() override;
|
||||||
uint Crash(bool flooded = false) override;
|
uint Crash(bool flooded = false) override;
|
||||||
Trackdir GetVehicleTrackdir() const override;
|
Trackdir GetVehicleTrackdir() const override;
|
||||||
|
|
|
@ -1706,10 +1706,16 @@ static void CheckIfRoadVehNeedsService(RoadVehicle *v)
|
||||||
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
|
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()
|
void RoadVehicle::OnNewEconomyDay()
|
||||||
{
|
{
|
||||||
AgeVehicle(this);
|
|
||||||
|
|
||||||
if (!this->IsFrontEngine()) return;
|
if (!this->IsFrontEngine()) return;
|
||||||
|
|
||||||
if ((++this->day_counter & 7) == 0) DecreaseVehicleValue(this);
|
if ((++this->day_counter & 7) == 0) DecreaseVehicleValue(this);
|
||||||
|
|
|
@ -45,6 +45,7 @@ struct Ship FINAL : public SpecializedVehicle<Ship, VEH_SHIP> {
|
||||||
Money GetRunningCost() const override;
|
Money GetRunningCost() const override;
|
||||||
bool IsInDepot() const override { return this->state == TRACK_BIT_DEPOT; }
|
bool IsInDepot() const override { return this->state == TRACK_BIT_DEPOT; }
|
||||||
bool Tick() override;
|
bool Tick() override;
|
||||||
|
void OnNewCalendarDay() override;
|
||||||
void OnNewEconomyDay() override;
|
void OnNewEconomyDay() override;
|
||||||
Trackdir GetVehicleTrackdir() const override;
|
Trackdir GetVehicleTrackdir() const override;
|
||||||
TileIndex GetOrderStationLocation(StationID station) override;
|
TileIndex GetOrderStationLocation(StationID station) override;
|
||||||
|
|
|
@ -223,6 +223,13 @@ Money Ship::GetRunningCost() const
|
||||||
return GetPrice(PR_RUNNING_SHIP, cost_factor, e->GetGRF());
|
return GetPrice(PR_RUNNING_SHIP, cost_factor, e->GetGRF());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Calendar day handler. */
|
||||||
|
void Ship::OnNewCalendarDay()
|
||||||
|
{
|
||||||
|
AgeVehicle(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Economy day handler. */
|
||||||
void Ship::OnNewEconomyDay()
|
void Ship::OnNewEconomyDay()
|
||||||
{
|
{
|
||||||
if ((++this->day_counter & 7) == 0) {
|
if ((++this->day_counter & 7) == 0) {
|
||||||
|
@ -230,7 +237,6 @@ void Ship::OnNewEconomyDay()
|
||||||
}
|
}
|
||||||
|
|
||||||
CheckVehicleBreakdown(this);
|
CheckVehicleBreakdown(this);
|
||||||
AgeVehicle(this);
|
|
||||||
CheckIfShipNeedsService(this);
|
CheckIfShipNeedsService(this);
|
||||||
|
|
||||||
CheckOrders(this);
|
CheckOrders(this);
|
||||||
|
|
|
@ -123,6 +123,7 @@ struct Train FINAL : public GroundVehicle<Train, VEH_TRAIN> {
|
||||||
int GetDisplayImageWidth(Point *offset = nullptr) const;
|
int GetDisplayImageWidth(Point *offset = nullptr) const;
|
||||||
bool IsInDepot() const override { return this->track == TRACK_BIT_DEPOT; }
|
bool IsInDepot() const override { return this->track == TRACK_BIT_DEPOT; }
|
||||||
bool Tick() override;
|
bool Tick() override;
|
||||||
|
void OnNewCalendarDay() override;
|
||||||
void OnNewEconomyDay() override;
|
void OnNewEconomyDay() override;
|
||||||
uint Crash(bool flooded = false) override;
|
uint Crash(bool flooded = false) override;
|
||||||
Trackdir GetVehicleTrackdir() const override;
|
Trackdir GetVehicleTrackdir() const override;
|
||||||
|
|
|
@ -4164,11 +4164,15 @@ static void CheckIfTrainNeedsService(Train *v)
|
||||||
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Update day counters of the train vehicle. */
|
/** Calendar day handler. */
|
||||||
void Train::OnNewEconomyDay()
|
void Train::OnNewCalendarDay()
|
||||||
{
|
{
|
||||||
AgeVehicle(this);
|
AgeVehicle(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Economy day handler. */
|
||||||
|
void Train::OnNewEconomyDay()
|
||||||
|
{
|
||||||
if ((++this->day_counter & 7) == 0) DecreaseVehicleValue(this);
|
if ((++this->day_counter & 7) == 0) DecreaseVehicleValue(this);
|
||||||
|
|
||||||
if (this->IsFrontEngine()) {
|
if (this->IsFrontEngine()) {
|
||||||
|
|
|
@ -916,6 +916,21 @@ void VehicleEnteredDepotThisTick(Vehicle *v)
|
||||||
v->vehstatus |= VS_STOPPED;
|
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.
|
* 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",
|
* Each tick, it processes vehicles with "index % DAY_TICKS == TimerGameEconomy::date_fract",
|
||||||
|
|
|
@ -567,7 +567,12 @@ public:
|
||||||
virtual bool Tick() { return true; };
|
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() {};
|
virtual void OnNewEconomyDay() {};
|
||||||
|
|
||||||
|
|
|
@ -62,6 +62,7 @@ CommandCost TunnelBridgeIsFree(TileIndex tile, TileIndex endtile, const Vehicle
|
||||||
void DecreaseVehicleValue(Vehicle *v);
|
void DecreaseVehicleValue(Vehicle *v);
|
||||||
void CheckVehicleBreakdown(Vehicle *v);
|
void CheckVehicleBreakdown(Vehicle *v);
|
||||||
void AgeVehicle(Vehicle *v);
|
void AgeVehicle(Vehicle *v);
|
||||||
|
void RunVehicleCalendarDayProc();
|
||||||
void VehicleEnteredDepotThisTick(Vehicle *v);
|
void VehicleEnteredDepotThisTick(Vehicle *v);
|
||||||
|
|
||||||
UnitID GetFreeUnitNumber(VehicleType type);
|
UnitID GetFreeUnitNumber(VehicleType type);
|
||||||
|
|
Loading…
Reference in New Issue