diff src/economy.cpp @ 18251:137b272acca5 draft

(svn r23087) -Feature: Auto-refitting of vehicles during loading at a station when the vehicle allows it.
author michi_cc <michi_cc@openttd.org>
date Fri, 04 Nov 2011 00:38:51 +0000
parents 3141f1ed78eb
children c7bd632d9cc2
line wrap: on
line diff
--- a/src/economy.cpp
+++ b/src/economy.cpp
@@ -1275,6 +1275,49 @@
 		/* Do not pick up goods when we have no-load set or loading is stopped. */
 		if (front->current_order.GetLoadType() & OLFB_NO_LOAD || HasBit(front->vehicle_flags, VF_STOP_LOADING)) continue;
 
+		/* This order has a refit, the vehicle is a normal vehicle and completely empty, do it now. */
+		if (front->current_order.IsRefit() && !v->IsArticulatedPart() && v->cargo.Count() == 0 &&
+				(v->type != VEH_AIRCRAFT || (Aircraft::From(v)->IsNormalAircraft() && v->Next()->cargo.Count() == 0))) {
+			CargoID new_cid = front->current_order.GetRefitCargo();
+			byte new_subtype = front->current_order.GetRefitSubtype();
+
+			Backup<CompanyByte> cur_company(_current_company, front->owner, FILE_LINE);
+
+			/* Check if all articulated parts are empty and collect refit mask. */
+			uint32 refit_mask = e->info.refit_mask;
+			Vehicle *w = v;
+			while (w->HasArticulatedPart()) {
+				w = w->GetNextArticulatedPart();
+				if (w->cargo.Count() > 0) new_cid = CT_NO_REFIT;
+				refit_mask |= EngInfo(w->engine_type)->refit_mask;
+			}
+
+			if (new_cid == CT_AUTO_REFIT) {
+				/* Get refittable cargo type with the most waiting cargo. */
+				int amount = 0;
+				CargoID cid;
+				FOR_EACH_SET_CARGO_ID(cid, refit_mask) {
+					if (cargo_left[cid] > amount) {
+						/* Try to find out if auto-refitting would succeed. In case the refit is allowed,
+						 * the returned refit capacity will be greater than zero. */
+						DoCommand(v->tile, v->index, cid | 1U << 6 | new_subtype << 8 | 1U << 16, DC_QUERY_COST, GetCmdRefitVeh(v)); // Auto-refit and only this vehicle including artic parts.
+						if (_returned_refit_capacity > 0) {
+							amount = cargo_left[cid];
+							new_cid = cid;
+						}
+					}
+				}
+			}
+
+			/* Refit if given a valid cargo. */
+			if (new_cid < NUM_CARGO) {
+				DoCommand(v->tile, v->index, new_cid | 1U << 6 | new_subtype << 8 | 1U << 16, DC_EXEC, GetCmdRefitVeh(v)); // Auto-refit and only this vehicle including artic parts.
+				ge = &st->goods[v->cargo_type];
+			}
+
+			cur_company.Restore();
+		}
+
 		/* update stats */
 		int t;
 		switch (front->type) {