changeset 5031:093e6ab2a0f7 draft

(svn r7073) -Feature: Add cargo refit support to both AIs for road vehicles
author peter1138 <peter1138@openttd.org>
date Sun, 05 Nov 2006 22:49:34 +0000
parents 181ba32542ac
children 4dd31afce48f
files ai/default/default.c ai/trolly/build.c
diffstat 2 files changed, 27 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/ai/default/default.c
+++ b/ai/default/default.c
@@ -177,14 +177,18 @@
 		}
 
 		/* Skip vehicles which can't take our cargo type */
-		if (rvi->cargo_type != cargo) continue;
+		if (rvi->cargo_type != cargo && !CanRefitTo(i, cargo)) continue;
 
 		/* Rate and compare the engine by speed & capacity */
 		rating = rvi->max_speed * rvi->capacity;
 		if (rating <= best_veh_rating) continue;
 
 		ret = DoCommand(tile, i, 0, 0, CMD_BUILD_ROAD_VEH);
-		if (CmdFailed(ret) || ret > money) continue;
+		if (CmdFailed(ret)) continue;
+
+		/* Add the cost of refitting */
+		if (rvi->cargo_type != cargo) ret += GetRefitCost(i);
+		if (ret > money) continue;
 
 		best_veh_rating = rating;
 		best_veh_index = i;
@@ -3184,6 +3188,15 @@
 
 	loco_id = _new_vehicle_id;
 
+	if (GetVehicle(loco_id)->cargo_type != p->ai.cargo_type) {
+		/* Cargo type doesn't match, so refit it */
+		if (CmdFailed(DoCommand(tile, loco_id, p->ai.cargo_type, DC_EXEC, CMD_REFIT_ROAD_VEH))) {
+			/* Refit failed... sell the vehicle */
+			DoCommand(tile, loco_id, 0, DC_EXEC, CMD_SELL_ROAD_VEH);
+			return;
+		}
+	}
+
 	for (i = 0; p->ai.order_list_blocks[i] != 0xFF; i++) {
 		const AiBuildRec* aib = &p->ai.src + p->ai.order_list_blocks[i];
 		bool is_pass = (
--- a/ai/trolly/build.c
+++ b/ai/trolly/build.c
@@ -246,7 +246,7 @@
 			int32 ret;
 
 			/* Skip vehicles which can't take our cargo type */
-			if (rvi->cargo_type != p->ainew.cargo) continue;
+			if (rvi->cargo_type != p->ainew.cargo && !CanRefitTo(i, p->ainew.cargo)) continue;
 
 			// Is it availiable?
 			// Also, check if the reliability of the vehicle is above the AI_VEHICLE_MIN_RELIABILTY
@@ -276,6 +276,17 @@
 	if (success) {
 		p->ainew.state = AI_STATE_GIVE_ORDERS;
 		p->ainew.veh_id = _new_vehicle_id;
+
+		if (GetVehicle(p->ainew.veh_id)->cargo_type != p->ainew.cargo) {
+			/* Cargo type doesn't match, so refit it */
+			debug("doing refit");
+			if (CmdFailed(DoCommand(tile, p->ainew.veh_id, p->ainew.cargo, DC_EXEC, CMD_REFIT_ROAD_VEH))) {
+				debug("refit failed, selling");
+				/* Refit failed, so sell the vehicle */
+				DoCommand(tile, p->ainew.veh_id, 0, DC_EXEC, CMD_SELL_ROAD_VEH);
+				p->ainew.state = AI_STATE_NOTHING;
+			}
+		}
 	} else {
 		/* XXX this should be handled more gracefully */
 		p->ainew.state = AI_STATE_NOTHING;