changeset 18309:5c0ddcab3a11 draft

(svn r23145) -Change: [NewGRF v8] Determine the 'first' refittable cargo of vehicles using the cargo ordering from the cargo translation table.
author frosch <frosch@openttd.org>
date Tue, 08 Nov 2011 17:25:51 +0000
parents 0e8f567d1cb0
children 1bba5a7e2c9d
files src/newgrf.cpp
diffstat 1 files changed, 24 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/newgrf.cpp
+++ b/src/newgrf.cpp
@@ -7971,6 +7971,7 @@
 		EngineID engine = e->index;
 		EngineInfo *ei = &e->info;
 		bool only_defaultcargo; ///< Set if the vehicle shall carry only the default cargo
+		const uint8 *cargo_map_for_first_refittable = NULL;
 
 		/* Did the newgrf specify any refitting? If not, use defaults. */
 		if (_gted[engine].refitmask_valid) {
@@ -7982,9 +7983,13 @@
 			 * Note: After applying the translations, the vehicle may end up carrying no defined cargo. It becomes unavailable in that case. */
 			only_defaultcargo = (ei->refit_mask == 0 && _gted[engine].cargo_allowed == 0);
 
+			const GRFFile *file = _gted[engine].refitmask_grf;
+			if (file == NULL) file = e->GetGRF();
+			if (file != NULL && file->grf_version >= 8 && file->cargo_max != 0) {
+				cargo_map_for_first_refittable = file->cargo_map;
+			}
+
 			if (ei->refit_mask != 0) {
-				const GRFFile *file = _gted[engine].refitmask_grf;
-				if (file == NULL) file = e->GetGRF();
 				if (file != NULL && file->cargo_max != 0) {
 					/* Apply cargo translation table to the refit mask */
 					uint num_cargo = min(32, file->cargo_max);
@@ -8045,7 +8050,23 @@
 
 		/* Check if this engine's cargo type is valid. If not, set to the first refittable
 		 * cargo type. Finally disable the vehicle, if there is still no cargo. */
-		if (ei->cargo_type == CT_INVALID && ei->refit_mask != 0) ei->cargo_type = (CargoID)FindFirstBit(ei->refit_mask);
+		if (ei->cargo_type == CT_INVALID && ei->refit_mask != 0) {
+			if (cargo_map_for_first_refittable == NULL) {
+				/* Use first refittable cargo slot */
+				ei->cargo_type = (CargoID)FindFirstBit(ei->refit_mask);
+			} else {
+				/* Use first refittable cargo from cargo translation table */
+				byte best_local_slot = 0xFF;
+				CargoID cargo_type;
+				FOR_EACH_SET_CARGO_ID(cargo_type, ei->refit_mask) {
+					byte local_slot = cargo_map_for_first_refittable[cargo_type];
+					if (local_slot < best_local_slot) {
+						best_local_slot = local_slot;
+						ei->cargo_type = cargo_type;
+					}
+				}
+			}
+		}
 		if (ei->cargo_type == CT_INVALID) ei->climates = 0;
 
 		/* Clear refit_mask for not refittable ships */