Page 1 of 1

AIVehicle.GetLocation not being updated during travel

Posted: 08 Sep 2008 17:16
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.

Re: AIVehicle.GetLocation not being updated during travel

Posted: 08 Sep 2008 23:43
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.

Re: AIVehicle.GetLocation not being updated during travel

Posted: 09 Sep 2008 06:04
by Rubidium
You are aware that aircraft can be flying outside the map and that that will then return *very* invalid tiles?

Re: AIVehicle.GetLocation not being updated during travel

Posted: 09 Sep 2008 07:18
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.

Re: AIVehicle.GetLocation not being updated during travel

Posted: 10 Sep 2008 19:54
by jpedreira
No, I was not aware... How can an airplane of my own be flying outside the map??

Re: AIVehicle.GetLocation not being updated during travel

Posted: 10 Sep 2008 19:56
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.