changeset 17909:4d6b4b6d6069 draft

(svn r22713) -Feature: [NewGRF] Per vehicle custom cargo ageing period.
author michi_cc <michi_cc@openttd.org>
date Wed, 03 Aug 2011 20:55:08 +0000
parents cd2cebd6c3b3
children 71fa31ee7ee9
files src/aircraft_cmd.cpp src/engine.cpp src/engine_type.h src/newgrf.cpp src/newgrf_properties.h src/roadveh_cmd.cpp src/saveload/misc_sl.cpp src/saveload/oldloader_sl.cpp src/saveload/saveload.cpp src/saveload/vehicle_sl.cpp src/ship_cmd.cpp src/table/engines.h src/train_cmd.cpp src/vehicle.cpp src/vehicle_base.h src/vehicle_cmd.cpp src/vehicle_func.h
diffstat 17 files changed, 74 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/src/aircraft_cmd.cpp
+++ b/src/aircraft_cmd.cpp
@@ -549,6 +549,11 @@
 		/* 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);
 }
 
 
--- a/src/engine.cpp
+++ b/src/engine.cpp
@@ -98,6 +98,8 @@
 			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;
 	}
 
--- a/src/engine_type.h
+++ b/src/engine_type.h
@@ -134,6 +134,7 @@
 	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.
 };
 
 /**
--- a/src/newgrf.cpp
+++ b/src/newgrf.cpp
@@ -966,6 +966,10 @@
 				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 @@
 				}
 				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 @@
 				}
 				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 @@
 				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;
--- a/src/newgrf_properties.h
+++ b/src/newgrf_properties.h
@@ -26,6 +26,7 @@
 	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 @@
 	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 */
--- a/src/roadveh_cmd.cpp
+++ b/src/roadveh_cmd.cpp
@@ -201,6 +201,9 @@
 
 		/* 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);
--- a/src/saveload/misc_sl.cpp
+++ b/src/saveload/misc_sl.cpp
@@ -60,6 +60,7 @@
 	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 @@
 	    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 @@
 	    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
--- a/src/saveload/oldloader_sl.cpp
+++ b/src/saveload/oldloader_sl.cpp
@@ -1573,6 +1573,7 @@
 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 ),
--- 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
 
--- a/src/saveload/vehicle_sl.cpp
+++ b/src/saveload/vehicle_sl.cpp
@@ -237,6 +237,8 @@
 	}
 }
 
+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 @@
 				}
 			}
 		}
+
+		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 @@
 		     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),
--- a/src/ship_cmd.cpp
+++ b/src/ship_cmd.cpp
@@ -182,6 +182,9 @@
 	/* 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();
 }
 
--- 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
--- a/src/train_cmd.cpp
+++ b/src/train_cmd.cpp
@@ -233,6 +233,7 @@
 		}
 
 		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;
--- 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 @@
 	this->fill_percent_te_id = INVALID_TE_ID;
 	this->first              = this;
 	this->colourmap          = PAL_NONE;
+	this->cargo_age_counter  = 1;
 }
 
 /**
@@ -586,8 +586,6 @@
 
 void InitializeVehicles()
 {
-	_age_cargo_skip_counter = 1;
-
 	_vehicles_to_autoreplace.Reset();
 	ResetVehiclePosHash();
 }
@@ -801,8 +799,6 @@
 {
 	_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 @@
 			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;
--- a/src/vehicle_base.h
+++ b/src/vehicle_base.h
@@ -100,7 +100,8 @@
 
 /** 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 @@
 	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
--- 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 @@
 				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();
--- a/src/vehicle_func.h
+++ b/src/vehicle_func.h
@@ -161,7 +161,6 @@
 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);