(svn r8858) -Codechange: Replace magic number test with class method for determining if a cargo is valid/active.
authorpeter1138
Fri, 23 Feb 2007 09:56:20 +0000
changeset 6448 a749c78812a7
parent 6447 3b71e57fd22b
child 6449 e520244dc71e
(svn r8858) -Codechange: Replace magic number test with class method for determining if a cargo is valid/active.
src/cargotype.cpp
src/cargotype.h
src/newgrf_engine.cpp
src/newgrf_station.cpp
src/station_gui.cpp
--- a/src/cargotype.cpp	Fri Feb 23 08:37:33 2007 +0000
+++ b/src/cargotype.cpp	Fri Feb 23 09:56:20 2007 +0000
@@ -67,3 +67,9 @@
 	assert(_cargo_bitnum_map[bitnum] != CT_INVALID);
 	return _cargo_bitnum_map[bitnum];
 }
+
+
+bool CargoSpec::IsValid() const
+{
+	return bitnum != INVALID_CARGO;
+}
--- a/src/cargotype.h	Fri Feb 23 08:37:33 2007 +0000
+++ b/src/cargotype.h	Fri Feb 23 09:56:20 2007 +0000
@@ -31,6 +31,8 @@
 	SpriteID sprite;
 
 	uint16 classes;
+
+	bool IsValid() const;
 } CargoSpec;
 
 
--- a/src/newgrf_engine.cpp	Fri Feb 23 08:37:33 2007 +0000
+++ b/src/newgrf_engine.cpp	Fri Feb 23 09:56:20 2007 +0000
@@ -820,8 +820,10 @@
 	if (v == NULL) {
 		cargo = GC_PURCHASE;
 	} else {
-		cargo = GetCargo(v->cargo_type)->bitnum;
-		assert(cargo != GC_INVALID);
+		const CargoSpec *cs = GetCargo(v->cargo_type);
+		assert(cs->IsValid());
+
+		cargo = cs->bitnum;
 
 		if (v->type == VEH_Train) {
 			group = GetWagonOverrideSpriteSet(engine, cargo, v->u.rail.first_engine);
--- a/src/newgrf_station.cpp	Fri Feb 23 08:37:33 2007 +0000
+++ b/src/newgrf_station.cpp	Fri Feb 23 09:56:20 2007 +0000
@@ -515,7 +515,7 @@
 		/* Pick the first cargo that we have waiting */
 		for (CargoID cargo = 0; cargo < NUM_CARGO; cargo++) {
 			const CargoSpec *cs = GetCargo(cargo);
-			if (cs->bitnum != 0xFF && object->u.station.statspec->spritegroup[cs->bitnum] != NULL &&
+			if (cs->IsValid() && object->u.station.statspec->spritegroup[cs->bitnum] != NULL &&
 					GB(object->u.station.st->goods[cargo].waiting_acceptance, 0, 12) != 0) {
 				ctype = cs->bitnum;
 				break;
--- a/src/station_gui.cpp	Fri Feb 23 08:37:33 2007 +0000
+++ b/src/station_gui.cpp	Fri Feb 23 09:56:20 2007 +0000
@@ -59,7 +59,7 @@
 static void StationsWndShowStationRating(int x, int y, CargoID type, uint amount, byte rating)
 {
 	const CargoSpec *cs = GetCargo(type);
-	if (cs->bitnum == 0xFF) return;
+	if (!cs->IsValid()) return;
 
 	int colour = cs->rating_colour;
 	uint w = (minu(amount, 576) + 5) / 36;
@@ -328,7 +328,7 @@
 				cg_ofst = IsWindowWidgetLowered(w, i + STATIONLIST_WIDGET_CARGOSTART) ? 2 : 1;
 
 				const CargoSpec *cs = GetCargo(i);
-				if (cs->bitnum != 0xFF) {
+				if (cs->IsValid()) {
 					GfxFillRect(x + cg_ofst, y + cg_ofst, x + cg_ofst + 10 , y + cg_ofst + 7, cs->rating_colour);
 					DrawStringCentered(x + 6 + cg_ofst, y + cg_ofst, cs->abbrev, 0x10);
 				}