(svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
authorpeter1138
Wed, 07 Jun 2006 07:20:28 +0000
changeset 3973 9d3cd1ed6ac0
parent 3972 0c55ea48562d
child 3974 08b438ca3429
(svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
aircraft_cmd.c
vehicle.c
vehicle.h
--- a/aircraft_cmd.c	Mon Jun 05 18:16:41 2006 +0000
+++ b/aircraft_cmd.c	Wed Jun 07 07:20:28 2006 +0000
@@ -21,6 +21,7 @@
 #include "vehicle_gui.h"
 #include "table/sprites.h"
 #include "newgrf_engine.h"
+#include "newgrf_callbacks.h"
 
 static bool AirportMove(Vehicle *v, const AirportFTAClass *Airport);
 static bool AirportSetBlocks(Vehicle *v, AirportFTA *current_pos, const AirportFTAClass *Airport);
@@ -156,6 +157,34 @@
 }
 
 
+/**
+ * Calculates cargo capacity based on an aircraft's passenger
+ * and mail capacities.
+ * @param cid Which cargo type to calculate a capacity for.
+ * @param engine Which engine to find a cargo capacity for.
+ * @return New cargo capacity value.
+ */
+static uint16 AircraftDefaultCargoCapacity(CargoID cid, EngineID engine_type)
+{
+	const AircraftVehicleInfo *avi = AircraftVehInfo(engine_type);
+
+	assert(cid != CT_INVALID);
+
+	/* An aircraft can carry twice as much goods as normal cargo,
+	 * and four times as many passengers. */
+	switch (cid) {
+		case CT_PASSENGERS:
+			return avi->passenger_capacity;
+		case CT_MAIL:
+			return avi->passenger_capacity + avi->mail_capacity;
+		case CT_GOODS:
+			return (avi->passenger_capacity + avi->mail_capacity) / 2;
+		default:
+			return (avi->passenger_capacity + avi->mail_capacity) / 4;
+	}
+}
+
+
 /** Build an aircraft.
  * @param tile tile of depot where aircraft is built
  * @param p1 aircraft type being built (engine)
@@ -192,6 +221,7 @@
 		return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
 
 	if (flags & DC_EXEC) {
+		CargoID cargo;
 		uint x;
 		uint y;
 
@@ -256,6 +286,32 @@
 
 		u->subtype = 4;
 
+		/* Danger, Will Robinson!
+		 * If the aircraft is refittable, but cannot be refitted to
+		 * passengers, we select the cargo type from the refit mask.
+		 * This is a fairly nasty hack to get around the fact that TTD
+		 * has no default cargo type specifier for planes... */
+		cargo = FindFirstRefittableCargo(p1);
+		if (cargo != CT_INVALID && cargo != CT_PASSENGERS) {
+			uint16 callback = CALLBACK_FAILED;
+
+			v->cargo_type = cargo;
+
+			if (HASBIT(EngInfo(p1)->callbackmask, CBM_REFIT_CAPACITY)) {
+				callback = GetVehicleCallback(CBID_VEHICLE_REFIT_CAPACITY, 0, 0, v->engine_type, v);
+			}
+
+			if (callback == CALLBACK_FAILED) {
+				/* Callback failed, or not executed; use the default cargo capacity */
+				v->cargo_cap = AircraftDefaultCargoCapacity(v->cargo_type, v->engine_type);
+			} else {
+				v->cargo_cap = callback;
+			}
+
+			/* Set the 'second compartent' capacity to none */
+			u->cargo_cap = 0;
+		}
+
 		e = GetEngine(p1);
 		v->reliability = e->reliability;
 		v->reliability_spd_dec = e->reliability_spd_dec;
--- a/vehicle.c	Mon Jun 05 18:16:41 2006 +0000
+++ b/vehicle.c	Wed Jun 07 07:20:28 2006 +0000
@@ -692,6 +692,25 @@
 	return HASBIT(EngInfo(engine_type)->refit_mask, cid);
 }
 
+/** Find the first cargo type that an engine can be refitted to.
+ * @param engine Which engine to find cargo for.
+ * @return A climate dependent cargo type. CT_INVALID is returned if not refittable.
+ */
+CargoID FindFirstRefittableCargo(EngineID engine_type)
+{
+	CargoID cid;
+	uint32 refit_mask = EngInfo(engine_type)->refit_mask;
+
+	if (refit_mask != 0) {
+		for (cid = CT_PASSENGERS; cid < NUM_CARGO; cid++) {
+			if (HASBIT(refit_mask, _global_cargo_id[_opt_ptr->landscape][cid])) return cid;
+		}
+	}
+
+	return CT_INVALID;
+}
+
+
 static void DoDrawVehicle(const Vehicle *v)
 {
 	uint32 image = v->cur_image;
--- a/vehicle.h	Mon Jun 05 18:16:41 2006 +0000
+++ b/vehicle.h	Wed Jun 07 07:20:28 2006 +0000
@@ -267,6 +267,7 @@
 
 bool CanFillVehicle(Vehicle *v);
 bool CanRefitTo(EngineID engine_type, CargoID cid_to);
+CargoID FindFirstRefittableCargo(EngineID engine_type);
 
 void ViewportAddVehicles(DrawPixelInfo *dpi);