(svn r2949) The AI no longer needs to 'cheat' to build aircraft; eg it builds them now from a hangar. Also, to query the price of a new aircraft tile information is not needed
authorDarkvater
Tue, 13 Sep 2005 13:30:18 +0000
changeset 2423 4266590b549d
parent 2422 897a01f7c624
child 2424 f31a0d616358
(svn r2949) The AI no longer needs to 'cheat' to build aircraft; eg it builds them now from a hangar. Also, to query the price of a new aircraft tile information is not needed
ai/default/default.c
aircraft_cmd.c
--- a/ai/default/default.c	Tue Sep 13 12:19:27 2005 +0000
+++ b/ai/default/default.c	Tue Sep 13 13:30:18 2005 +0000
@@ -1,4 +1,4 @@
-/* $Id: ai_old.c 2701 2005-07-24 14:12:37Z tron $ */
+/* $Id$ */
 
 #include "../../stdafx.h"
 #include "../../openttd.h"
@@ -191,7 +191,7 @@
 			if (i>=253) continue;
 		}
 
-		ret = DoCommandByTile(0, i, 0, 0, CMD_BUILD_AIRCRAFT);
+		ret = DoCommandByTile(0, i, 0, DC_QUERY_COST, CMD_BUILD_AIRCRAFT);
 		if (!CmdFailed(ret) && ret <= money && ret >= best_veh_cost) {
 			best_veh_cost = ret;
 			best_veh_index = i;
@@ -3502,10 +3502,11 @@
 	tile = TILE_ADD(p->ai.src.use_tile, ToTileIndexDiff(ptr->tileoffs));
 
 	veh = AiChooseAircraftToBuild(p->player_money, p->ai.build_kind!=0 ? 1 : 0);
-	if (veh == -1) {
-		return;
-	}
-
+	if (veh == -1) return;
+
+	/* XXX - Have the AI pick the hangar terminal in an airport. Eg get airport-type
+	 * and offset to the FIRST depot because the AI picks the st->xy tile */
+	tile += ToTileIndexDiff(GetAirport(GetStation(_m[tile].m2)->airport_type)->airport_depots[0]);
 	if (CmdFailed(DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_AIRCRAFT))) return;
 	loco_id = _new_aircraft_id;
 
--- a/aircraft_cmd.c	Tue Sep 13 12:19:27 2005 +0000
+++ b/aircraft_cmd.c	Tue Sep 13 13:30:18 2005 +0000
@@ -165,17 +165,15 @@
 
 	if (!IsEngineBuildable(p1, VEH_Aircraft)) return CMD_ERROR;
 
-	// Workaround: TODO: make AI players try to build planes in a hangar instead of just an airport tile.
-	if (!IsAircraftHangarTile(tile) && IS_HUMAN_PLAYER(_current_player)) return CMD_ERROR;
+	value = EstimateAircraftCost(p1);
 
-	if (!IsTileOwner(tile, _current_player) && IS_HUMAN_PLAYER(_current_player)) return CMD_ERROR;
+	// to just query the cost, it is not neccessary to have a valid tile (automation/AI)
+	if (flags & DC_QUERY_COST) return value;
+
+	if (!IsAircraftHangarTile(tile) || !IsTileOwner(tile, _current_player)) return CMD_ERROR;
 
 	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
 
-	value = EstimateAircraftCost(p1);
-
-	if (flags & DC_QUERY_COST) return value;
-
 	avi = AircraftVehInfo(p1);
 	// allocate 2 or 3 vehicle structs, depending on type
 	if (!AllocateVehicles(vl, (avi->subtype & 1) == 0 ? 3 : 2) ||
@@ -253,23 +251,21 @@
 
 		_new_aircraft_id = v->index;
 
-		// the old AI doesn't click on a tile to build airplanes, so the below code will
-		// never work. Therefore just assume the AI's planes always come from Hangar0
-		v->u.air.pos = _is_old_ai_player ? 0 : MAX_ELEMENTS;
+		v->u.air.pos = MAX_ELEMENTS;
 
-		/* When we click on hangar we know the tile (it is in var 'tile')it is on. By that we know
-			its position in the array of depots the airport has.....we can search
-			->layout for #th position of depot. Since layout must start with depots, it is simple
-		*/
+		/* When we click on hangar we know the tile it is on. By that we know
+		 * its position in the array of depots the airport has.....we can search
+		 * layout for #th position of depot. Since layout must start with a listing
+		 * of all depots, it is simple */
 		{
 			const Station* st = GetStation(_m[tile].m2);
-			const AirportFTAClass* Airport = GetAirport(st->airport_type);
+			const AirportFTAClass* apc = GetAirport(st->airport_type);
 			uint i;
 
-			for (i = 0; i < Airport->nof_depots; i++) {
-				if (st->airport_tile + ToTileIndexDiff(Airport->airport_depots[i]) == tile) {
-					assert(Airport->layout[i].heading == HANGAR);
-					v->u.air.pos = Airport->layout[i].position;
+			for (i = 0; i < apc->nof_depots; i++) {
+				if (st->airport_tile + ToTileIndexDiff(apc->airport_depots[i]) == tile) {
+					assert(apc->layout[i].heading == HANGAR);
+					v->u.air.pos = apc->layout[i].position;
 					break;
 				}
 			}