AIVehicle.GetLocation not being updated during travel

Discuss the new AI features ("NoAI") introduced into OpenTTD 0.7, allowing you to implement custom AIs, and the new Game Scripts available in OpenTTD 1.2 and higher.

Moderator: OpenTTD Developers

Post Reply
jpedreira
Engineer
Engineer
Posts: 5
Joined: 08 Sep 2008 16:55

AIVehicle.GetLocation not being updated during travel

Post by jpedreira »

Im trying to code an algorithm to detect "busy airports" based on the distance of the airport heading airplanes. It seems that AIVehicle.GetLocation is not properly updated. You can test it with:

Code: Select all

local pos= AIOrder.ResolveOrderPosition(airplane, AIOrder.CURRENT_ORDER) ;
local destination_tile= AIOrder.GetOrderDestination(airplane, pos);
local current_tile= AIVehicle.GetLocation(airplane);
local airport= AIStation.GetStationID(destination_tile);
AILog.Info("Airplane "+AIVehicle.GetName(airplane)+ " going to "+ AIStation.GetName(airport)+", distance: "+AIMap.DistanceSquare(destination_tile, current_tile));
And you'll see the distance doesn't change from take off to landing.
jpedreira
Engineer
Engineer
Posts: 5
Joined: 08 Sep 2008 16:55

Re: AIVehicle.GetLocation not being updated during travel

Post by jpedreira »

I've been doing further investigations...

The problem is the next: while an airplane is flying, AIVehicle.GetLocation ALWAYS RETURN 0 (see "src/aircraft.cpp", function "AirController", where literally states "If vehicle is in the air, use tile coordinate 0").

I've solved it by patching the game as follows:

Code: Select all

Index: src/ai/api/ai_vehicle.cpp
===================================================================
--- src/ai/api/ai_vehicle.cpp	(revisión: 14276)
+++ src/ai/api/ai_vehicle.cpp	(copia de trabajo)
@@ -153,7 +153,13 @@
 {
 	if (!IsValidVehicle(vehicle_id)) return INVALID_TILE;
 
-	return ::GetVehicle(vehicle_id)->tile;
+	if(::GetVehicle(vehicle_id)->tile!= 0)
+		return ::GetVehicle(vehicle_id)->tile;
+	else{
+		int32 x_pos= ::GetVehicle(vehicle_id)->x_pos/TILE_SIZE;
+		int32 y_pos= ::GetVehicle(vehicle_id)->y_pos/TILE_SIZE;
+		return TileXY(x_pos, y_pos);
+	}
I'd be very gratefull if this patch could be merged...

http://bugs.openttd.org/task/2280


Thank you!

Julio Pedreira.
Rubidium
OpenTTD Developer
OpenTTD Developer
Posts: 3815
Joined: 09 Feb 2006 19:15

Re: AIVehicle.GetLocation not being updated during travel

Post by Rubidium »

You are aware that aircraft can be flying outside the map and that that will then return *very* invalid tiles?
kuifware
Engineer
Engineer
Posts: 26
Joined: 17 May 2008 21:10

Re: AIVehicle.GetLocation not being updated during travel

Post by kuifware »

Rubidium wrote:You are aware that aircraft can be flying outside the map and that that will then return *very* invalid tiles?
That's a good point. Perhaps AIVehicle.GetLocation can return INVALID_TILE or otherwise a clipped version (i.e. clipping x to [0, SizeX) and y to [0, SizeY) before calling TileXY) in these cases. In any case it would be nice if the docs mentioned what is done for planes.

Perhaps the best solution is to add AIVehicle.GetXPosition, AIVehicle.GetYPosition and AITile.TILE_SIZE (or AIMap.TILE_SIZE); this also allows more precise tracking of vehicles.
jpedreira
Engineer
Engineer
Posts: 5
Joined: 08 Sep 2008 16:55

Re: AIVehicle.GetLocation not being updated during travel

Post by jpedreira »

No, I was not aware... How can an airplane of my own be flying outside the map??
Rubidium
OpenTTD Developer
OpenTTD Developer
Posts: 3815
Joined: 09 Feb 2006 19:15

Re: AIVehicle.GetLocation not being updated during travel

Post by Rubidium »

Build an intercontinental airport at the edge of the map and the approach/holding state will bring the aircraft outside of the map.
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 13 guests