diff --git a/src/aircraft_cmd.cpp b/src/aircraft_cmd.cpp index ae80ae7f9c..ce969a5337 100644 --- a/src/aircraft_cmd.cpp +++ b/src/aircraft_cmd.cpp @@ -549,6 +549,11 @@ void UpdateAircraftCache(Aircraft *v) /* Use the default max speed of the vehicle. */ v->vcache.cached_max_speed = AircraftVehInfo(v->engine_type)->max_speed; } + + /* Update cargo aging period. */ + v->vcache.cached_cargo_age_period = GetVehicleProperty(v, PROP_AIRCRAFT_CARGO_AGE_PERIOD, EngInfo(v->engine_type)->cargo_age_period); + Aircraft *u = v->Next(); // Shadow for mail + u->vcache.cached_cargo_age_period = GetVehicleProperty(u, PROP_AIRCRAFT_CARGO_AGE_PERIOD, EngInfo(u->engine_type)->cargo_age_period); } diff --git a/src/engine.cpp b/src/engine.cpp index 42454e4012..05bbc91136 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -98,6 +98,8 @@ Engine::Engine(VehicleType type, EngineID base) case VEH_SHIP: this->u.ship.visual_effect = VE_DEFAULT; break; default: break; // The aircraft, disasters and especially visual effects have no NewGRF configured visual effects } + /* Set cargo aging period to the default value. */ + this->info.cargo_age_period = CARGO_AGING_TICKS; return; } diff --git a/src/engine_type.h b/src/engine_type.h index 6f93bfbb6f..2bd7fae479 100644 --- a/src/engine_type.h +++ b/src/engine_type.h @@ -134,6 +134,7 @@ struct EngineInfo { byte callback_mask; ///< Bitmask of vehicle callbacks that have to be called int8 retire_early; ///< Number of years early to retire vehicle StringID string_id; ///< Default name of engine + uint16 cargo_age_period; ///< Number of ticks before carried cargo is aged. }; /** diff --git a/src/newgrf.cpp b/src/newgrf.cpp index 86d2da2a6a..514d6dfdf6 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -966,6 +966,10 @@ static ChangeInfoResult RailVehicleChangeInfo(uint engine, int numinfo, int prop ei->base_intro = buf->ReadDWord(); break; + case PROP_TRAIN_CARGO_AGE_PERIOD: // 0x2B Cargo aging period + ei->cargo_age_period = buf->ReadWord(); + break; + default: ret = CommonVehicleChangeInfo(ei, prop, buf); break; @@ -1108,6 +1112,10 @@ static ChangeInfoResult RoadVehicleChangeInfo(uint engine, int numinfo, int prop } break; + case PROP_ROADVEH_CARGO_AGE_PERIOD: // 0x22 Cargo aging period + ei->cargo_age_period = buf->ReadWord(); + break; + default: ret = CommonVehicleChangeInfo(ei, prop, buf); break; @@ -1238,6 +1246,10 @@ static ChangeInfoResult ShipVehicleChangeInfo(uint engine, int numinfo, int prop } break; + case PROP_SHIP_CARGO_AGE_PERIOD: // 0x1D Cargo aging period + ei->cargo_age_period = buf->ReadWord(); + break; + default: ret = CommonVehicleChangeInfo(ei, prop, buf); break; @@ -1352,6 +1364,10 @@ static ChangeInfoResult AircraftVehicleChangeInfo(uint engine, int numinfo, int AlterVehicleListOrder(e->index, buf->ReadExtendedByte()); break; + case PROP_AIRCRAFT_CARGO_AGE_PERIOD: // 0x1C Cargo aging period + ei->cargo_age_period = buf->ReadWord(); + break; + default: ret = CommonVehicleChangeInfo(ei, prop, buf); break; diff --git a/src/newgrf_properties.h b/src/newgrf_properties.h index 900220f949..1f18940e0a 100644 --- a/src/newgrf_properties.h +++ b/src/newgrf_properties.h @@ -26,6 +26,7 @@ enum PropertyID { PROP_TRAIN_COST_FACTOR = 0x17, ///< Purchase cost (if dualheaded: sum of both vehicles) PROP_TRAIN_TRACTIVE_EFFORT = 0x1F, ///< Tractive effort coefficient in 1/256 PROP_TRAIN_USER_DATA = 0x25, ///< User defined data for vehicle variable 0x42 + PROP_TRAIN_CARGO_AGE_PERIOD = 0x2B, ///< Number of ticks before carried cargo is aged PROP_ROADVEH_RUNNING_COST_FACTOR = 0x09, ///< Yearly runningcost PROP_ROADVEH_CARGO_CAPACITY = 0x0F, ///< Capacity @@ -34,17 +35,20 @@ enum PropertyID { PROP_ROADVEH_WEIGHT = 0x14, ///< Weight in 1/4 t PROP_ROADVEH_SPEED = 0x15, ///< Max. speed: 1 unit = 1/0.8 mph = 2 km-ish/h PROP_ROADVEH_TRACTIVE_EFFORT = 0x18, ///< Tractive effort coefficient in 1/256 + PROP_ROADVEH_CARGO_AGE_PERIOD = 0x22, ///< Number of ticks before carried cargo is aged PROP_SHIP_COST_FACTOR = 0x0A, ///< Purchase cost PROP_SHIP_SPEED = 0x0B, ///< Max. speed: 1 unit = 1/3.2 mph = 0.5 km-ish/h PROP_SHIP_CARGO_CAPACITY = 0x0D, ///< Capacity PROP_SHIP_RUNNING_COST_FACTOR = 0x0F, ///< Yearly runningcost + PROP_SHIP_CARGO_AGE_PERIOD = 0x1D, ///< Number of ticks before carried cargo is aged PROP_AIRCRAFT_COST_FACTOR = 0x0B, ///< Purchase cost PROP_AIRCRAFT_SPEED = 0x0C, ///< Max. speed: 1 unit = 8 mph = 12.8 km-ish/h PROP_AIRCRAFT_RUNNING_COST_FACTOR = 0x0E, ///< Yearly runningcost PROP_AIRCRAFT_PASSENGER_CAPACITY = 0x0F, ///< Passenger Capacity PROP_AIRCRAFT_MAIL_CAPACITY = 0x11, ///< Mail Capacity + PROP_AIRCRAFT_CARGO_AGE_PERIOD = 0x1C, ///< Number of ticks before carried cargo is aged }; #endif /* NEWGRF_PROPERTIES_H */ diff --git a/src/roadveh_cmd.cpp b/src/roadveh_cmd.cpp index 8a77e9d891..672c6e426f 100644 --- a/src/roadveh_cmd.cpp +++ b/src/roadveh_cmd.cpp @@ -201,6 +201,9 @@ void RoadVehUpdateCache(RoadVehicle *v) /* Invalidate the vehicle colour map */ u->colourmap = PAL_NONE; + + /* Update cargo aging period. */ + u->vcache.cached_cargo_age_period = GetVehicleProperty(u, PROP_ROADVEH_CARGO_AGE_PERIOD, EngInfo(u->engine_type)->cargo_age_period); } uint max_speed = GetVehicleProperty(v, PROP_ROADVEH_SPEED, 0); diff --git a/src/saveload/misc_sl.cpp b/src/saveload/misc_sl.cpp index 2f3827ce9f..ccfc7a2d36 100644 --- a/src/saveload/misc_sl.cpp +++ b/src/saveload/misc_sl.cpp @@ -60,6 +60,7 @@ void ResetViewportAfterLoadGame() MarkWholeScreenDirty(); } +byte _age_cargo_skip_counter; ///< Skip aging of cargo? Used before savegame version 162. static const SaveLoadGlobVarList _date_desc[] = { SLEG_CONDVAR(_date, SLE_FILE_U16 | SLE_VAR_I32, 0, 30), @@ -67,7 +68,7 @@ static const SaveLoadGlobVarList _date_desc[] = { SLEG_VAR(_date_fract, SLE_UINT16), SLEG_VAR(_tick_counter, SLE_UINT16), SLE_CONDNULL(2, 0, 156), // _vehicle_id_ctr_day - SLEG_VAR(_age_cargo_skip_counter, SLE_UINT8), + SLEG_CONDVAR(_age_cargo_skip_counter, SLE_UINT8, 0, 161), SLE_CONDNULL(1, 0, 45), SLEG_CONDVAR(_cur_tileloop_tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5), SLEG_CONDVAR(_cur_tileloop_tile, SLE_UINT32, 6, SL_MAX_VERSION), @@ -92,7 +93,7 @@ static const SaveLoadGlobVarList _date_check_desc[] = { SLE_NULL(2), // _date_fract SLE_NULL(2), // _tick_counter SLE_CONDNULL(2, 0, 156), // _vehicle_id_ctr_day - SLE_NULL(1), // _age_cargo_skip_counter + SLE_CONDNULL(1, 0, 161), // _age_cargo_skip_counter SLE_CONDNULL(1, 0, 45), SLE_CONDNULL(2, 0, 5), // _cur_tileloop_tile SLE_CONDNULL(4, 6, SL_MAX_VERSION), // _cur_tileloop_tile diff --git a/src/saveload/oldloader_sl.cpp b/src/saveload/oldloader_sl.cpp index 7f1722e0f0..d4ff3758ab 100644 --- a/src/saveload/oldloader_sl.cpp +++ b/src/saveload/oldloader_sl.cpp @@ -1573,6 +1573,7 @@ static bool LoadTTDPatchExtraChunks(LoadgameState *ls, int num) extern TileIndex _cur_tileloop_tile; extern uint16 _disaster_delay; extern byte _trees_tick_ctr; +extern byte _age_cargo_skip_counter; // From misc_sl.cpp static const OldChunks main_chunk[] = { OCL_ASSERT( OC_TTD, 0 ), OCL_ASSERT( OC_TTO, 0 ), diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp index 486d1d8e5b..1627765335 100644 --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -226,8 +226,9 @@ * 159 21962 * 160 21974 * 161 22567 + * 162 22713 */ -extern const uint16 SAVEGAME_VERSION = 161; ///< Current savegame version of OpenTTD. +extern const uint16 SAVEGAME_VERSION = 162; ///< Current savegame version of OpenTTD. SavegameType _savegame_type; ///< type of savegame we are loading diff --git a/src/saveload/vehicle_sl.cpp b/src/saveload/vehicle_sl.cpp index b9b2d53ce9..ff738c7e24 100644 --- a/src/saveload/vehicle_sl.cpp +++ b/src/saveload/vehicle_sl.cpp @@ -237,6 +237,8 @@ static void CheckValidVehicles() } } +extern byte _age_cargo_skip_counter; // From misc_sl.cpp + /** Called after load to update coordinates */ void AfterLoadVehicles(bool part_of_load) { @@ -342,6 +344,13 @@ void AfterLoadVehicles(bool part_of_load) } } } + + if (IsSavegameVersionBefore(162)) { + /* Set the vehicle-local cargo age counter from the old global counter. */ + FOR_ALL_VEHICLES(v) { + v->cargo_age_counter = _age_cargo_skip_counter; + } + } } CheckValidVehicles(); @@ -499,6 +508,7 @@ const SaveLoad *GetVehicleDescription(VehicleType vt) SLE_VAR(Vehicle, cargo_cap, SLE_UINT16), SLEG_CONDVAR( _cargo_count, SLE_UINT16, 0, 67), SLE_CONDLST(Vehicle, cargo.packets, REF_CARGO_PACKET, 68, SL_MAX_VERSION), + SLE_CONDVAR(Vehicle, cargo_age_counter, SLE_UINT16, 162, SL_MAX_VERSION), SLE_VAR(Vehicle, day_counter, SLE_UINT8), SLE_VAR(Vehicle, tick_counter, SLE_UINT8), diff --git a/src/ship_cmd.cpp b/src/ship_cmd.cpp index 29959c931e..222d48994e 100644 --- a/src/ship_cmd.cpp +++ b/src/ship_cmd.cpp @@ -182,6 +182,9 @@ void Ship::UpdateCache() /* speed_frac == 0 means no reduction while 0xFF means reduction to 1/256. */ this->vcache.cached_max_speed = GetVehicleProperty(this, PROP_SHIP_SPEED, svi->max_speed) * (256 - speed_frac) / 256; + /* Update cargo aging period. */ + this->vcache.cached_cargo_age_period = GetVehicleProperty(this, PROP_SHIP_CARGO_AGE_PERIOD, EngInfo(this->engine_type)->cargo_age_period); + this->UpdateVisualEffect(); } diff --git a/src/table/engines.h b/src/table/engines.h index 5be9914759..77574fd7a6 100644 --- a/src/table/engines.h +++ b/src/table/engines.h @@ -26,7 +26,7 @@ * @param f Bitmask of the climates * @note the 5 between b and f is the load amount */ -#define MT(a, b, c, d, e, f) { DAYS_TILL_ORIGINAL_BASE_YEAR + a, c, d, b, 5, f, e, 0, 8, 1 << EF_RAIL_FLIPS, 0, 0, STR_EMPTY } +#define MT(a, b, c, d, e, f) { DAYS_TILL_ORIGINAL_BASE_YEAR + a, c, d, b, 5, f, e, 0, 8, 1 << EF_RAIL_FLIPS, 0, 0, STR_EMPTY, CARGO_AGING_TICKS } /** * Writes the properties of a train carriage into the EngineInfo struct. @@ -39,7 +39,7 @@ * @see MT * @note the 5 between b and f is the load amount */ -#define MW(a, b, c, d, e, f) { DAYS_TILL_ORIGINAL_BASE_YEAR + a, c, d, b, 5, f, e, 0, 8, 1 << EF_RAIL_FLIPS, 0, 0, STR_EMPTY } +#define MW(a, b, c, d, e, f) { DAYS_TILL_ORIGINAL_BASE_YEAR + a, c, d, b, 5, f, e, 0, 8, 1 << EF_RAIL_FLIPS, 0, 0, STR_EMPTY, CARGO_AGING_TICKS } /** * Writes the properties of a road vehicle into the EngineInfo struct. @@ -52,7 +52,7 @@ * @param f Bitmask of the climates * @note the 5 between b and f is the load amount */ -#define MR(a, b, c, d, e, f) { DAYS_TILL_ORIGINAL_BASE_YEAR + a, c, d, b, 5, f, e, 0, 8, 0, 0, 0, STR_EMPTY } +#define MR(a, b, c, d, e, f) { DAYS_TILL_ORIGINAL_BASE_YEAR + a, c, d, b, 5, f, e, 0, 8, 0, 0, 0, STR_EMPTY, CARGO_AGING_TICKS } /** * Writes the properties of a ship into the EngineInfo struct. @@ -64,7 +64,7 @@ * @param f Bitmask of the climates * @note the 10 between b and f is the load amount */ -#define MS(a, b, c, d, e, f) { DAYS_TILL_ORIGINAL_BASE_YEAR + a, c, d, b, 10, f, e, 0, 8, 0, 0, 0, STR_EMPTY } +#define MS(a, b, c, d, e, f) { DAYS_TILL_ORIGINAL_BASE_YEAR + a, c, d, b, 10, f, e, 0, 8, 0, 0, 0, STR_EMPTY, CARGO_AGING_TICKS } /** * Writes the properties of an aeroplane into the EngineInfo struct. @@ -75,7 +75,7 @@ * @param e Bitmask of the climates * @note the 20 between b and e is the load amount */ -#define MA(a, b, c, d, e) { DAYS_TILL_ORIGINAL_BASE_YEAR + a, c, d, b, 20, e, CT_INVALID, 0, 8, 0, 0, 0, STR_EMPTY } +#define MA(a, b, c, d, e) { DAYS_TILL_ORIGINAL_BASE_YEAR + a, c, d, b, 20, e, CT_INVALID, 0, 8, 0, 0, 0, STR_EMPTY, CARGO_AGING_TICKS } /* Climates * T = Temperate diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index 8db6490b9c..149aa25da7 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -233,6 +233,7 @@ void Train::ConsistChanged(bool same_length) } u->cargo_cap = GetVehicleCapacity(u); + u->vcache.cached_cargo_age_period = GetVehicleProperty(u, PROP_TRAIN_CARGO_AGE_PERIOD, e_u->info.cargo_age_period); /* check the vehicle length (callback) */ uint16 veh_len = CALLBACK_FAILED; diff --git a/src/vehicle.cpp b/src/vehicle.cpp index ccbb960689..ab1b4df2f6 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -60,7 +60,6 @@ VehicleID _new_vehicle_id; uint16 _returned_refit_capacity; ///< Stores the capacity after a refit operation. uint16 _returned_mail_refit_capacity; ///< Stores the mail capacity after a refit operation (Aircraft only). -byte _age_cargo_skip_counter; ///< Skip aging of cargo? /** The pool with all our precious vehicles. */ @@ -245,6 +244,7 @@ Vehicle::Vehicle(VehicleType type) this->fill_percent_te_id = INVALID_TE_ID; this->first = this; this->colourmap = PAL_NONE; + this->cargo_age_counter = 1; } /** @@ -586,8 +586,6 @@ static AutoreplaceMap _vehicles_to_autoreplace; void InitializeVehicles() { - _age_cargo_skip_counter = 1; - _vehicles_to_autoreplace.Reset(); ResetVehiclePosHash(); } @@ -801,8 +799,6 @@ void CallVehicleTicks() { _vehicles_to_autoreplace.Clear(); - _age_cargo_skip_counter = (_age_cargo_skip_counter == 0) ? CARGO_AGING_TICKS - 1 : (_age_cargo_skip_counter - 1); - RunVehicleDayProc(); Station *st; @@ -825,7 +821,13 @@ void CallVehicleTicks() case VEH_ROAD: case VEH_AIRCRAFT: case VEH_SHIP: - if (_age_cargo_skip_counter == 0) v->cargo.AgeCargo(); + if (v->vcache.cached_cargo_age_period != 0) { + v->cargo_age_counter = min(v->cargo_age_counter, v->vcache.cached_cargo_age_period); + if (--v->cargo_age_counter == 0) { + v->cargo.AgeCargo(); + v->cargo_age_counter = v->vcache.cached_cargo_age_period; + } + } if (v->type == VEH_TRAIN && Train::From(v)->IsWagon()) continue; if (v->type == VEH_AIRCRAFT && v->subtype != AIR_HELICOPTER) continue; diff --git a/src/vehicle_base.h b/src/vehicle_base.h index 6781b472d2..547344c275 100644 --- a/src/vehicle_base.h +++ b/src/vehicle_base.h @@ -100,7 +100,8 @@ enum GroundVehicleSubtypeFlags { /** Cached often queried values common to all vehicles. */ struct VehicleCache { - uint16 cached_max_speed; ///< Maximum speed of the consist (minimum of the max speed of all vehicles in the consist). + uint16 cached_max_speed; ///< Maximum speed of the consist (minimum of the max speed of all vehicles in the consist). + uint16 cached_cargo_age_period; ///< Number of ticks before carried cargo is aged. byte cached_vis_effect; ///< Visual effect to show (see #VisualEffect) }; @@ -213,6 +214,7 @@ public: byte cargo_subtype; ///< Used for livery refits (NewGRF variations) uint16 cargo_cap; ///< total capacity VehicleCargoList cargo; ///< The cargo this vehicle is carrying + uint16 cargo_age_counter; ///< Ticks till cargo is aged next. byte day_counter; ///< Increased by one for each day byte tick_counter; ///< Increased by one for each tick diff --git a/src/vehicle_cmd.cpp b/src/vehicle_cmd.cpp index ec341163e6..fdb9fd66cc 100644 --- a/src/vehicle_cmd.cpp +++ b/src/vehicle_cmd.cpp @@ -30,6 +30,7 @@ #include "autoreplace_gui.h" #include "company_base.h" #include "order_backup.h" +#include "ship.h" #include "table/strings.h" @@ -373,9 +374,15 @@ CommandCost CmdRefitVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint break; case VEH_SHIP: + v->InvalidateNewGRFCacheOfChain(); + v->colourmap = PAL_NONE; // invalidate vehicle colour map + Ship::From(v)->UpdateCache(); + break; + case VEH_AIRCRAFT: v->InvalidateNewGRFCacheOfChain(); v->colourmap = PAL_NONE; // invalidate vehicle colour map + UpdateAircraftCache(Aircraft::From(v)); break; default: NOT_REACHED(); diff --git a/src/vehicle_func.h b/src/vehicle_func.h index ebdb5f3435..166c0b9cfd 100644 --- a/src/vehicle_func.h +++ b/src/vehicle_func.h @@ -161,7 +161,6 @@ CommandCost EnsureNoTrainOnTrackBits(TileIndex tile, TrackBits track_bits); extern VehicleID _new_vehicle_id; extern uint16 _returned_refit_capacity; extern uint16 _returned_mail_refit_capacity; -extern byte _age_cargo_skip_counter; bool CanVehicleUseStation(EngineID engine_type, const struct Station *st); bool CanVehicleUseStation(const Vehicle *v, const struct Station *st);