changeset 6460:cebf3bbc9227 draft

(svn r9620) -Codechange: apply cargo translation table to newstation variables 0x60..0x65
author peter1138 <peter1138@openttd.org>
date Fri, 13 Apr 2007 19:32:18 +0000
parents bd75cf7f2505
children ad8742a2d46d
files src/cargotype.cpp src/cargotype.h src/newgrf_cargo.cpp src/newgrf_cargo.h src/newgrf_station.cpp
diffstat 5 files changed, 54 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/cargotype.cpp
+++ b/src/cargotype.cpp
@@ -71,3 +71,21 @@
 	/* No matching label was found, so it is invalid */
 	return CT_INVALID;
 }
+
+
+/** Find the CargoID of a 'bitnum' value.
+ * @param bitnum 'bitnum' to find.
+ * @return First CargoID with the given bitnum, or CT_INVALID if not found.
+ */
+CargoID GetCargoIDByBitnum(uint8 bitnum)
+{
+	if (bitnum == INVALID_CARGO) return CT_INVALID;
+
+	for (CargoID c = 0; c < lengthof(_cargo); c++) {
+		if (_cargo[c].bitnum == bitnum) return c;
+	}
+
+	/* No matching label was found, so it is invalid */
+	return CT_INVALID;
+}
+
--- a/src/cargotype.h
+++ b/src/cargotype.h
@@ -58,6 +58,7 @@
 const CargoSpec *GetCargo(CargoID c);
 /* Get the cargo ID with the cargo label */
 CargoID GetCargoIDByLabel(CargoLabel cl);
+CargoID GetCargoIDByBitnum(uint8 bitnum);
 
 static inline bool IsCargoInClass(CargoID c, uint16 cc)
 {
--- a/src/newgrf_cargo.cpp
+++ b/src/newgrf_cargo.cpp
@@ -94,3 +94,17 @@
 
 	return group->g.callback.result;
 }
+
+
+CargoID GetCargoTranslation(uint8 cargo, const GRFFile *grffile)
+{
+	/* Pre-version 7 uses the 'climate dependent' ID, i.e. cargo is the cargo ID */
+	if (grffile->grf_version < 7) return HASBIT(_cargo_mask, cargo) ? cargo : (CargoID) CT_INVALID;
+
+	/* If the GRF contains a translation table (and the cargo is in bounds)
+	 * then get the cargo ID for the label */
+	if (cargo < grffile->cargo_max) return GetCargoIDByLabel(grffile->cargo_list[cargo]);
+
+	/* Else the cargo value is a 'climate independent' 'bitnum' */
+	return GetCargoIDByBitnum(cargo);
+}
--- a/src/newgrf_cargo.h
+++ b/src/newgrf_cargo.h
@@ -21,9 +21,12 @@
 static const CargoID CT_PURCHASE     = NUM_CARGO + 1;
 static const CargoID CT_DEFAULT_NA   = NUM_CARGO + 2;
 
+/* Forward declarations of structs used */
 struct CargoSpec;
+struct GRFFile;
 
 SpriteID GetCustomCargoSprite(const CargoSpec *cs);
 uint16 GetCargoCallback(uint16 callback, uint32 param1, uint32 param2, const CargoSpec *cs);
+CargoID GetCargoTranslation(uint8 cargo, const GRFFile *grffile);
 
 #endif /* NEWGRF_CARGO_H */
--- a/src/newgrf_station.cpp
+++ b/src/newgrf_station.cpp
@@ -409,12 +409,7 @@
 		case 0x49: return GetPlatformInfoHelper(tile, false, true, false);
 
 		/* Variables which use the parameter */
-		case 0x60: return GB(st->goods[parameter].waiting_acceptance, 0, 12);
-		case 0x61: return st->goods[parameter].days_since_pickup;
-		case 0x62: return st->goods[parameter].rating;
-		case 0x63: return st->goods[parameter].enroute_time;
-		case 0x64: return st->goods[parameter].last_speed | (st->goods[parameter].last_age << 8);
-		case 0x65: return GB(st->goods[parameter].waiting_acceptance, 12, 4);
+		/* Variables 0x60 to 0x65 are handled separately below */
 
 		/* General station properties */
 		case 0x82: return 50;
@@ -430,6 +425,23 @@
 		case 0xFA: return max(st->build_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0);
 	}
 
+	/* Handle cargo variables with parameter, 0x60 to 0x65 */
+	if (variable >= 0x60 && variable <= 0x65) {
+		CargoID c = GetCargoTranslation(parameter, object->u.station.statspec->grffile);
+
+		if (c == CT_INVALID) return 0;
+		const GoodsEntry *ge = &st->goods[c];
+
+		switch (variable) {
+			case 0x60: return GB(ge->waiting_acceptance, 0, 12);
+			case 0x61: return ge->days_since_pickup;
+			case 0x62: return ge->rating;
+			case 0x63: return ge->enroute_time;
+			case 0x64: return ge->last_speed | (ge->last_age << 8);
+			case 0x65: return GB(ge->waiting_acceptance, 12, 4);
+		}
+	}
+
 	/* Handle cargo variables (deprecated) */
 	if (variable >= 0x8C && variable <= 0xEC) {
 		const GoodsEntry *g = &st->goods[GB(variable - 0x8C, 3, 4)];