From 1ce89305832b78412f828f37d6bd923d011947cc Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Sat, 27 Jan 2024 16:38:27 +0000 Subject: [PATCH] Use DateTicksScaled for Vehicle::last_loading_tick --- src/saveload/afterload.cpp | 10 +++++++--- src/saveload/vehicle_sl.cpp | 2 +- src/sl/extended_ver_sl.cpp | 2 +- src/sl/vehicle_sl.cpp | 2 +- src/table/newgrf_debug_data.h | 2 +- src/vehicle.cpp | 13 +++++++------ src/vehicle_base.h | 2 +- 7 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp index 23c5c6fe6a..ad337f1932 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -3907,11 +3907,15 @@ bool AfterLoadGame() /* Use current order time to approximate last loading time */ if (IsSavegameVersionBefore(SLV_LAST_LOADING_TICK) && SlXvIsFeatureMissing(XSLFI_LAST_LOADING_TICK)) { for (Vehicle *v : Vehicle::Iterate()) { - v->last_loading_tick = std::max(_scaled_tick_counter, static_cast(v->current_order_time)) - v->current_order_time; + v->last_loading_tick = _scaled_date_ticks - v->current_order_time; } - } else if (SlXvIsFeaturePresent(XSLFI_LAST_LOADING_TICK, 1, 1)) { + } else if (SlXvIsFeatureMissing(XSLFI_LAST_LOADING_TICK, 3)) { + const DateTicksScaledDelta delta = _scaled_date_ticks.base() - (int64_t)_scaled_tick_counter; for (Vehicle *v : Vehicle::Iterate()) { - v->last_loading_tick *= _settings_game.economy.day_length_factor; + if (v->last_loading_tick != 0) { + if (SlXvIsFeaturePresent(XSLFI_LAST_LOADING_TICK, 1, 1)) v->last_loading_tick = v->last_loading_tick.base() * _settings_game.economy.day_length_factor; + v->last_loading_tick += delta; + } } } diff --git a/src/saveload/vehicle_sl.cpp b/src/saveload/vehicle_sl.cpp index c9befd80ed..45a2fa5cf6 100644 --- a/src/saveload/vehicle_sl.cpp +++ b/src/saveload/vehicle_sl.cpp @@ -178,7 +178,7 @@ public: SLE_CONDVAR(Vehicle, current_order_time, SLE_UINT32, SLV_67, SLV_TIMETABLE_TICKS_TYPE), SLE_CONDVAR(Vehicle, current_order_time, SLE_FILE_I32 | SLE_VAR_U32, SLV_TIMETABLE_TICKS_TYPE, SL_MAX_VERSION), - SLE_CONDVAR(Vehicle, last_loading_tick, SLE_UINT64, SLV_LAST_LOADING_TICK, SL_MAX_VERSION), + SLE_CONDVAR(Vehicle, last_loading_tick, SLE_FILE_U64 | SLE_VAR_I64, SLV_LAST_LOADING_TICK, SL_MAX_VERSION), SLE_CONDVAR(Vehicle, lateness_counter, SLE_INT32, SLV_67, SL_MAX_VERSION), }; #if defined(_MSC_VER) && (_MSC_VER == 1915 || _MSC_VER == 1916) diff --git a/src/sl/extended_ver_sl.cpp b/src/sl/extended_ver_sl.cpp index 44aa3639c1..3eae70844a 100644 --- a/src/sl/extended_ver_sl.cpp +++ b/src/sl/extended_ver_sl.cpp @@ -197,7 +197,7 @@ const SlxiSubChunkInfo _sl_xv_sub_chunk_infos[] = { { XSLFI_SCRIPT_INT64, XSCF_NULL, 1, 1, "script_int64", nullptr, nullptr, nullptr }, { XSLFI_U64_TICK_COUNTER, XSCF_NULL, 1, 1, "u64_tick_counter", nullptr, nullptr, nullptr }, { XSLFI_LINKGRAPH_TRAVEL_TIME, XSCF_NULL, 1, 1, "linkgraph_travel_time", nullptr, nullptr, nullptr }, - { XSLFI_LAST_LOADING_TICK, XSCF_NULL, 2, 2, "last_loading_tick", nullptr, nullptr, nullptr }, + { XSLFI_LAST_LOADING_TICK, XSCF_NULL, 3, 3, "last_loading_tick", nullptr, nullptr, nullptr }, { XSLFI_SCRIPT_LEAGUE_TABLES, XSCF_NULL, 1, 1, "script_league_tables", nullptr, nullptr, "LEAE,LEAT" }, { XSLFI_VELOCITY_NAUTICAL, XSCF_IGNORABLE_ALL, 1, 1, "velocity_nautical", nullptr, nullptr, nullptr }, { XSLFI_CONSISTENT_PARTIAL_Z, XSCF_NULL, 1, 1, "consistent_partial_z", nullptr, nullptr, nullptr }, diff --git a/src/sl/vehicle_sl.cpp b/src/sl/vehicle_sl.cpp index 424c620d33..486cdf4fa7 100644 --- a/src/sl/vehicle_sl.cpp +++ b/src/sl/vehicle_sl.cpp @@ -832,7 +832,7 @@ SaveLoadTable GetVehicleDescription(VehicleType vt) SLE_CONDVAR(Vehicle, current_order_time, SLE_UINT32, SLV_67, SL_MAX_VERSION), SLE_CONDVAR_X(Vehicle, current_loading_time, SLE_UINT32, SL_MIN_VERSION, SL_MAX_VERSION, SlXvFeatureTest(XSLFTO_AND, XSLFI_AUTO_TIMETABLE)), SLE_CONDVAR_X(Vehicle, current_loading_time, SLE_UINT32, SL_MIN_VERSION, SL_MAX_VERSION, SlXvFeatureTest(XSLFTO_AND, XSLFI_JOKERPP, SL_JOKER_1_23)), - SLE_CONDVAR_X(Vehicle, last_loading_tick, SLE_UINT64, SL_MIN_VERSION, SL_MAX_VERSION, SlXvFeatureTest(XSLFTO_AND, XSLFI_LAST_LOADING_TICK)), + SLE_CONDVAR_X(Vehicle, last_loading_tick, SLE_INT64, SL_MIN_VERSION, SL_MAX_VERSION, SlXvFeatureTest(XSLFTO_AND, XSLFI_LAST_LOADING_TICK)), SLE_CONDNULL_X(4, SL_MIN_VERSION, SL_MAX_VERSION, SlXvFeatureTest(XSLFTO_AND, XSLFI_SPRINGPP)), SLE_CONDVAR(Vehicle, lateness_counter, SLE_INT32, SLV_67, SL_MAX_VERSION), diff --git a/src/table/newgrf_debug_data.h b/src/table/newgrf_debug_data.h index ee29db8abf..825632656f 100644 --- a/src/table/newgrf_debug_data.h +++ b/src/table/newgrf_debug_data.h @@ -263,7 +263,7 @@ class NIHVehicle : public NIHelper { seprintf(buffer, lastof(buffer), " V Last loading station: %u, %s", v->last_loading_station, BaseStation::Get(v->last_loading_station)->GetCachedName()); output.print(buffer); seprintf(buffer, lastof(buffer), " V Last loading tick: " OTTD_PRINTF64 " (" OTTD_PRINTF64 ", " OTTD_PRINTF64 " mins ago)", - v->last_loading_tick, _scaled_tick_counter - v->last_loading_tick, (_scaled_tick_counter - v->last_loading_tick) / _settings_time.ticks_per_minute); + v->last_loading_tick.base(), (_scaled_date_ticks - v->last_loading_tick).base(), (_scaled_date_ticks - v->last_loading_tick).base() / _settings_time.ticks_per_minute); output.print(buffer); } if (v->IsGroundVehicle()) { diff --git a/src/vehicle.cpp b/src/vehicle.cpp index bfaa47e79f..ad4e152adf 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -3239,7 +3239,7 @@ static void VehicleIncreaseStats(const Vehicle *front) { for (const Vehicle *v = front; v != nullptr; v = v->Next()) { StationID last_loading_station = HasBit(front->vehicle_flags, VF_LAST_LOAD_ST_SEP) ? v->last_loading_station : front->last_loading_station; - uint64_t loading_tick = HasBit(front->vehicle_flags, VF_LAST_LOAD_ST_SEP) ? v->last_loading_tick : front->last_loading_tick; + DateTicksScaled loading_tick = HasBit(front->vehicle_flags, VF_LAST_LOAD_ST_SEP) ? v->last_loading_tick : front->last_loading_tick; if (v->refit_cap > 0 && last_loading_station != INVALID_STATION && last_loading_station != front->last_station_visited && @@ -3254,7 +3254,7 @@ static void VehicleIncreaseStats(const Vehicle *front) EdgeUpdateMode restricted_mode = EUM_INCREASE; if (v->type == VEH_AIRCRAFT) restricted_mode |= EUM_AIRCRAFT; IncreaseStats(Station::Get(last_loading_station), v->cargo_type, front->last_station_visited, v->refit_cap, - std::min(v->refit_cap, v->cargo.StoredCount()), _scaled_tick_counter - loading_tick, restricted_mode); + std::min(v->refit_cap, v->cargo.StoredCount()), (_scaled_date_ticks - loading_tick).AsTicksT(), restricted_mode); } } } @@ -3474,7 +3474,7 @@ void Vehicle::LeaveStation() /* if the vehicle could load here or could stop with cargo loaded set the last loading station */ this->last_loading_station = this->last_station_visited; - this->last_loading_tick = _scaled_tick_counter; + this->last_loading_tick = _scaled_date_ticks; ClrBit(this->vehicle_flags, VF_LAST_LOAD_ST_SEP); } else if (cargoes_can_leave_with_cargo == 0) { /* can leave with no cargoes */ @@ -3488,14 +3488,14 @@ void Vehicle::LeaveStation() /* NB: this is saved here as we overwrite it on the first iteration of the loop below */ StationID head_last_loading_station = this->last_loading_station; - uint64_t head_last_loading_tick = this->last_loading_tick; + DateTicksScaled head_last_loading_tick = this->last_loading_tick; for (Vehicle *u = this; u != nullptr; u = u->Next()) { StationID last_loading_station = HasBit(this->vehicle_flags, VF_LAST_LOAD_ST_SEP) ? u->last_loading_station : head_last_loading_station; - uint64_t last_loading_tick = HasBit(this->vehicle_flags, VF_LAST_LOAD_ST_SEP) ? u->last_loading_tick : head_last_loading_tick; + DateTicksScaled last_loading_tick = HasBit(this->vehicle_flags, VF_LAST_LOAD_ST_SEP) ? u->last_loading_tick : head_last_loading_tick; if (u->cargo_type < NUM_CARGO && HasBit(cargoes_can_load_unload, u->cargo_type)) { if (HasBit(cargoes_can_leave_with_cargo, u->cargo_type)) { u->last_loading_station = this->last_station_visited; - u->last_loading_tick = _scaled_tick_counter; + u->last_loading_tick = _scaled_date_ticks; } else { u->last_loading_station = INVALID_STATION; } @@ -4692,6 +4692,7 @@ void AdjustVehicleScaledTickBase(DateTicksScaledDelta delta) { for (Vehicle *v : Vehicle::Iterate()) { if (v->timetable_start != 0) v->timetable_start += delta; + if (v->last_loading_tick != 0) v->last_loading_tick += delta; } for (OrderList *order_list : OrderList::Iterate()) { diff --git a/src/vehicle_base.h b/src/vehicle_base.h index 7eaa673835..6b86bb2f33 100644 --- a/src/vehicle_base.h +++ b/src/vehicle_base.h @@ -380,7 +380,7 @@ public: StationID last_station_visited; ///< The last station we stopped at. StationID last_loading_station; ///< Last station the vehicle has stopped at and could possibly leave from with any cargo loaded. (See VF_LAST_LOAD_ST_SEP). - uint64_t last_loading_tick; ///< Last time (relative to _scaled_tick_counter) the vehicle has stopped at a station and could possibly leave with any cargo loaded. (See VF_LAST_LOAD_ST_SEP). + DateTicksScaled last_loading_tick; ///< Last tick (_scaled_date_ticks) the vehicle has stopped at a station and could possibly leave with any cargo loaded. (See VF_LAST_LOAD_ST_SEP). VehicleCargoList cargo; ///< The cargo this vehicle is carrying uint16_t cargo_cap; ///< total capacity