changeset 5029:26aa4c33a28c draft

(svn r7070) -Codechange: Make the AI choose road vehicles based on a rating (currently max speed * capacity) instead of either the cost or the index of the vheicle.
author peter1138 <peter1138@openttd.org>
date Sun, 05 Nov 2006 21:50:08 +0000
parents 43d379025dc8
children 181ba32542ac
files ai/default/default.c ai/trolly/build.c
diffstat 2 files changed, 29 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/ai/default/default.c
+++ b/ai/default/default.c
@@ -162,12 +162,14 @@
 static EngineID AiChooseRoadVehToBuild(CargoID cargo, int32 money, TileIndex tile)
 {
 	EngineID best_veh_index = INVALID_ENGINE;
-	int32 best_veh_cost = 0;
+	int32 best_veh_rating = 0;
 	EngineID i = ROAD_ENGINES_INDEX;
 	EngineID end = i + NUM_ROAD_ENGINES;
 
 	for (; i != end; i++) {
+		const RoadVehicleInfo *rvi = RoadVehInfo(i);
 		const Engine* e = GetEngine(i);
+		int32 rating;
 		int32 ret;
 
 		if (!HASBIT(e->player_avail, _current_player) || e->reliability < 0x8A3D) {
@@ -175,13 +177,17 @@
 		}
 
 		/* Skip vehicles which can't take our cargo type */
-		if (RoadVehInfo(i)->cargo_type != cargo) continue;
+		if (rvi->cargo_type != 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 && ret >= best_veh_cost) {
-			best_veh_cost = ret;
-			best_veh_index = i;
-		}
+		if (CmdFailed(ret) || ret > money) continue;
+
+		best_veh_rating = rating;
+		best_veh_index = i;
 	}
 
 	return best_veh_index;
--- a/ai/trolly/build.c
+++ b/ai/trolly/build.c
@@ -232,27 +232,39 @@
 		// Not supported yet
 		return INVALID_ENGINE;
 	} else {
+		EngineID best_veh_index = INVALID_ENGINE;
+		int32 best_veh_rating = 0;
 		EngineID start = ROAD_ENGINES_INDEX;
 		EngineID end   = ROAD_ENGINES_INDEX + NUM_ROAD_ENGINES;
 		EngineID i;
 
 		// Let's check it backwards.. we simply want to best engine available..
-		for (i = end - 1; i >= start; i--) {
+		for (i = start; i != end; i--) {
+			const RoadVehicleInfo *rvi = RoadVehInfo(i);
 			const Engine* e = GetEngine(i);
+			int32 rating;
 			int32 ret;
 
 			/* Skip vehicles which can't take our cargo type */
-			if (RoadVehInfo(i)->cargo_type != p->ainew.cargo) continue;
+			if (rvi->cargo_type != p->ainew.cargo) continue;
 
 			// Is it availiable?
 			// Also, check if the reliability of the vehicle is above the AI_VEHICLE_MIN_RELIABILTY
 			if (!HASBIT(e->player_avail, _current_player) || e->reliability * 100 < AI_VEHICLE_MIN_RELIABILTY << 16) continue;
+
+			/* Rate and compare the engine by speed & capacity */
+			rating = rvi->max_speed * rvi->capacity;
+			if (rating <= best_veh_rating) continue;
+
 			// Can we build it?
 			ret = AI_DoCommand(0, i, 0, DC_QUERY_COST, CMD_BUILD_ROAD_VEH);
-			if (!CmdFailed(ret)) return i;
+			if (CmdFailed(ret)) continue;
+
+			best_veh_rating = rating;
+			best_veh_index = i;
 		}
-		// We did not find a vehicle :(
-		return INVALID_ENGINE;
+
+		return best_veh_index;
 	}
 }