(svn r6912) - Feature: Show a list of cargo types that a vehicle is refittable to in the purchase information window. (mart3p)
authorpeter1138
Mon, 23 Oct 2006 21:39:15 +0000
changeset 4932 3567a3ec9d80
parent 4931 6e09e884257d
child 4933 106e2a14ab3c
(svn r6912) - Feature: Show a list of cargo types that a vehicle is refittable to in the purchase information window. (mart3p)
build_vehicle_gui.c
lang/english.txt
roadveh_gui.c
ship_gui.c
train_gui.c
vehicle_gui.c
vehicle_gui.h
--- a/build_vehicle_gui.c	Mon Oct 23 21:25:30 2006 +0000
+++ b/build_vehicle_gui.c	Mon Oct 23 21:39:15 2006 +0000
@@ -256,6 +256,7 @@
 
 	/* Additional text from NewGRF */
 	y += ShowAdditionalText(x, y, w, engine_number);
+	y += ShowRefitOptionsList(x, y, w, engine_number);
 }
 
 void DrawAircraftImage(const Vehicle *v, int x, int y, VehicleID selection)
--- a/lang/english.txt	Mon Oct 23 21:25:30 2006 +0000
+++ b/lang/english.txt	Mon Oct 23 21:39:15 2006 +0000
@@ -3021,6 +3021,9 @@
 STR_PURCHASE_INFO_COST_SPEED                                    :{BLACK}Cost: {GOLD}{CURRENCY}{BLACK} Speed: {GOLD}{VELOCITY}
 STR_PURCHASE_INFO_AIRCRAFT_CAPACITY                             :{BLACK}Capacity: {GOLD}{COMMA} passengers, {COMMA} bags of mail
 STR_PURCHASE_INFO_PWAGPOWER_PWAGWEIGHT                          :{BLACK}Powered Wagons: {GOLD}+{POWER}{BLACK} Weight: {GOLD}+{WEIGHT_S}
+STR_PURCHASE_INFO_REFITTABLE_TO                                 :{BLACK}Refittable to: {GOLD}
+STR_PURCHASE_INFO_ALL_TYPES                                     :All cargo types
+STR_PURCHASE_INFO_ALL_BUT                                       :All but {GOLD}
 
 ########### String for New Landscape Generator
 
--- a/roadveh_gui.c	Mon Oct 23 21:25:30 2006 +0000
+++ b/roadveh_gui.c	Mon Oct 23 21:39:15 2006 +0000
@@ -66,6 +66,7 @@
 
 	/* Additional text from NewGRF */
 	y += ShowAdditionalText(x, y, w, engine_number);
+	y += ShowRefitOptionsList(x, y, w, engine_number);
 }
 
 void DrawRoadVehImage(const Vehicle *v, int x, int y, VehicleID selection)
--- a/ship_gui.c	Mon Oct 23 21:25:30 2006 +0000
+++ b/ship_gui.c	Mon Oct 23 21:39:15 2006 +0000
@@ -66,6 +66,7 @@
 
 	/* Additional text from NewGRF */
 	y += ShowAdditionalText(x, y, w, engine_number);
+	if (svi->refittable) y += ShowRefitOptionsList(x, y, w, engine_number);
 }
 
 void DrawShipImage(const Vehicle *v, int x, int y, VehicleID selection)
--- a/train_gui.c	Mon Oct 23 21:25:30 2006 +0000
+++ b/train_gui.c	Mon Oct 23 21:39:15 2006 +0000
@@ -293,6 +293,7 @@
 
 	/* Additional text from NewGRF */
 	y += ShowAdditionalText(x, y, w, engine_number);
+	y += ShowRefitOptionsList(x, y, w, engine_number);
 }
 
 /**
@@ -337,6 +338,7 @@
 
 	/* Additional text from NewGRF */
 	y += ShowAdditionalText(x, y, w, engine_number);
+	y += ShowRefitOptionsList(x, y, w, engine_number);
 }
 
 void CcBuildWagon(bool success, TileIndex tile, uint32 p1, uint32 p2)
--- a/vehicle_gui.c	Mon Oct 23 21:25:30 2006 +0000
+++ b/vehicle_gui.c	Mon Oct 23 21:39:15 2006 +0000
@@ -482,7 +482,7 @@
 }
 
 /* Display additional text from NewGRF in the purchase information window */
-uint ShowAdditionalText(int x, int y, int w, EngineID engine)
+uint ShowAdditionalText(int x, int y, uint w, EngineID engine)
 {
 	uint16 callback = GetVehicleCallback(CBID_VEHICLE_ADDITIONAL_TEXT, 0, 0, engine, NULL);
 	if (callback == CALLBACK_FAILED) return 0;
@@ -492,6 +492,55 @@
 	return DrawStringMultiLine(x, y, STR_02BD, w);
 }
 
+/* Count the number of bits that are set in a mask */
+static uint CountBits(uint32 mask)
+{
+	uint c = 0;
+	for (; mask != 0; mask >>= 1) if (HASBIT(mask, 0)) c++;
+	return c;
+}
+
+/* Display list of cargo types of the engine, for the purchase information window */
+uint ShowRefitOptionsList(int x, int y, uint w, EngineID engine)
+{
+	/* List of cargo types of this engine */
+	uint32 cmask = EngInfo(engine)->refit_mask;
+	/* List of cargo types available in this climate */
+	uint32 lmask = _landscape_global_cargo_mask[_opt.landscape];
+	char *b = _userstring;
+
+	/* Draw nothing if the engine is not refittable */
+	if (CountBits(cmask) <= 1) return 0;
+
+	b = InlineString(b, STR_PURCHASE_INFO_REFITTABLE_TO);
+
+	if (cmask == lmask) {
+		/* Engine can be refitted to all types in this climate */
+		b = InlineString(b, STR_PURCHASE_INFO_ALL_TYPES);
+	} else {
+		CargoID cid;
+
+		/* Check if we are able to refit to more cargo types and unable to. If
+		 * so, invert the cargo types to list those that we can't refit to. */
+		if (CountBits(cmask ^ lmask) < CountBits(cmask)) {
+			cmask ^= lmask;
+			b = InlineString(b, STR_PURCHASE_INFO_ALL_BUT);
+		}
+
+		/* Add each cargo type to the list */
+		for (cid = 0; cmask != 0; cmask >>= 1, cid++) {
+			if (!HASBIT(cmask, 0)) continue;
+
+			b = InlineString(b, _cargoc.names_s[_local_cargo_id_ctype[cid]]);
+			if (cmask > 1) b = strecpy(b, ", ", lastof(_userstring));
+		}
+	}
+
+	/* Terminate and display the completed string */
+	*b = '\0';
+	return DrawStringMultiLine(x, y, STR_SPEC_USERSTRING, w);
+}
+
 
 // if the sorting criteria had the same value, sort vehicle by unitnumber
 #define VEHICLEUNITNUMBERSORTER(r, a, b) {if (r == 0) {r = a->unitnumber - b->unitnumber;}}
--- a/vehicle_gui.h	Mon Oct 23 21:25:30 2006 +0000
+++ b/vehicle_gui.h	Mon Oct 23 21:39:15 2006 +0000
@@ -50,7 +50,8 @@
 
 void ChangeVehicleViewWindow(const Vehicle *from_v, const Vehicle *to_v);
 
-uint ShowAdditionalText(int x, int y, int w, EngineID engine);
+uint ShowAdditionalText(int x, int y, uint w, EngineID engine);
+uint ShowRefitOptionsList(int x, int y, uint w, EngineID engine);
 
 void ShowVehicleListWindow(PlayerID player, StationID station, byte vehicle_type);
 void ShowVehWithSharedOrders(Vehicle *v, byte vehicle_type);