vehicles and orders?

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
wilco_moerman
Engineer
Engineer
Posts: 70
Joined: 05 Jun 2008 15:51

vehicles and orders?

Post by wilco_moerman »

I have questions about vehicles and orders:

- is it possible to get the current order that a vehicle is executing?

- is there a way to determine whether a vehicle is stopped by the AIVehicle.StartStopVehicle command?
Nunc dimittis servum tuum Domine secundum verbum tuum in pace
User avatar
Zutty
Director
Director
Posts: 565
Joined: 22 Jan 2008 16:33

Re: vehicles and orders?

Post by Zutty »

wilco_moerman wrote:I have questions about vehicles and orders:

- is it possible to get the current order that a vehicle is executing?
Not as far as I know.
- is there a way to determine whether a vehicle is stopped by the AIVehicle.StartStopVehicle command?
I don;t know if this is exactly what you mean, but you could try...

Code: Select all

local isStopped = AIVehicle.GetCurrentSpeed(vehicle_id) == 0 && !AIVehicle.IsStoppedInDepot(vehicle_id);
PathZilla - A networking AI - Now with tram support.
wilco_moerman
Engineer
Engineer
Posts: 70
Joined: 05 Jun 2008 15:51

Re: vehicles and orders?

Post by wilco_moerman »

Zutty wrote:
wilco_moerman wrote:I have questions about vehicles and orders:

- is it possible to get the current order that a vehicle is executing?
Not as far as I know.
hmm, but a human player does have access to that kind of information. Same goes for "first truck at .... station". There is no event for that occasion in the noai framework.
- is there a way to determine whether a vehicle is stopped by the AIVehicle.StartStopVehicle command?
I don;t know if this is exactly what you mean, but you could try...

Code: Select all

local isStopped = AIVehicle.GetCurrentSpeed(vehicle_id) == 0 && !AIVehicle.IsStoppedInDepot(vehicle_id);
I was thinking along the same lines, but it occurred to me that a vehicle that is stopped at a station would also yield TRUE this way. And I can't find a way to detect if a vehicle is stopped at a station.
Nunc dimittis servum tuum Domine secundum verbum tuum in pace
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: vehicles and orders?

Post by Yexo »

wilco_moerman wrote:
Zutty wrote:
wilco_moerman wrote:I have questions about vehicles and orders:

- is it possible to get the current order that a vehicle is executing?
Not as far as I know.
hmm, but a human player does have access to that kind of information. Same goes for "first truck at .... station". There is no event for that occasion in the noai framework.
The current order will be a usefull addition to the framework, for the first vehicle at ... station event, see the wiki:
http://wiki.openttd.org/index.php/AI:TODO wrote: Events

* Arriving of vehicles at stations (for the first time)
User avatar
Zutty
Director
Director
Posts: 565
Joined: 22 Jan 2008 16:33

Re: vehicles and orders?

Post by Zutty »

wilco_moerman wrote:I was thinking along the same lines, but it occurred to me that a vehicle that is stopped at a station would also yield TRUE this way. And I can't find a way to detect if a vehicle is stopped at a station.
We CAN do that, but its a little long winded...

Code: Select all

local isVehicleAtStation = AIStation.IsValidStation(AIStation.GetStationID(AIVehicle.GetLocation(vehicle_id)));
local isStopped = AIVehicle.GetCurrentSpeed(vehicle_id) == 0
                   && !AIVehicle.IsStoppedInDepot(vehicle_id)
                   && !isVehicleAtStation;
PathZilla - A networking AI - Now with tram support.
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: vehicles and orders?

Post by Yexo »

Zutty wrote:
wilco_moerman wrote:I was thinking along the same lines, but it occurred to me that a vehicle that is stopped at a station would also yield TRUE this way. And I can't find a way to detect if a vehicle is stopped at a station.
We CAN do that, but its a little long winded...

Code: Select all

local isVehicleAtStation = AIStation.IsValidStation(AIStation.GetStationID(AIVehicle.GetLocation(vehicle_id)));
local isStopped = AIVehicle.GetCurrentSpeed(vehicle_id) == 0
                   && !AIVehicle.IsStoppedInDepot(vehicle_id)
                   && !isVehicleAtStation;
That doesn't always work, because a vehicle could be stopped on a station tile, so it's not a foolproof solution.
User avatar
Zutty
Director
Director
Posts: 565
Joined: 22 Jan 2008 16:33

Re: vehicles and orders?

Post by Zutty »

Yexo wrote:
Zutty wrote:
wilco_moerman wrote:I was thinking along the same lines, but it occurred to me that a vehicle that is stopped at a station would also yield TRUE this way. And I can't find a way to detect if a vehicle is stopped at a station.
We CAN do that, but its a little long winded...

Code: Select all

local isVehicleAtStation = AIStation.IsValidStation(AIStation.GetStationID(AIVehicle.GetLocation(vehicle_id)));
local isStopped = AIVehicle.GetCurrentSpeed(vehicle_id) == 0
                   && !AIVehicle.IsStoppedInDepot(vehicle_id)
                   && !isVehicleAtStation;
That doesn't always work, because a vehicle could be stopped on a station tile, so it's not a foolproof solution.
Not sure I quite follow you. Do you mean stopped at station that it isn't loading at? Perhaps if a bus breaks down in a DTRS?
PathZilla - A networking AI - Now with tram support.
User avatar
Ralph
Engineer
Engineer
Posts: 87
Joined: 21 Jun 2004 15:25

Re: vehicles and orders?

Post by Ralph »

Zutty wrote:Not sure I quite follow you. Do you mean stopped at station that it isn't loading at? Perhaps if a bus breaks down in a DTRS?
Vehicle != Road Vehicle
Last edited by Ralph on 15 Jun 2008 16:42, edited 1 time in total.
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: vehicles and orders?

Post by Yexo »

Zutty wrote:
...
That doesn't always work, because a vehicle could be stopped on a station tile, so it's not a foolproof solution.
Not sure I quite follow you. Do you mean stopped at station that it isn't loading at? Perhaps if a bus breaks down in a DTRS?[/quote]
The ai could've stopped the vehicle earlier using AIVehicle.StartStopVehicle while it was on a station tile. Read your own code again:

Code: Select all

local isVehicleAtStation = AIStation.IsValidStation(AIStation.GetStationID(AIVehicle.GetLocation(vehicle_id)));
This sets isVehicleAtStation to true when the vehicle is manually stopped on a station tile.
User avatar
Zutty
Director
Director
Posts: 565
Joined: 22 Jan 2008 16:33

Re: vehicles and orders?

Post by Zutty »

Yexo wrote:This sets isVehicleAtStation to true when the vehicle is manually stopped on a station tile.
Oh yes I see.
PathZilla - A networking AI - Now with tram support.
wilco_moerman
Engineer
Engineer
Posts: 70
Joined: 05 Jun 2008 15:51

Re: vehicles and orders?

Post by wilco_moerman »

isn't it possible to just add a AIVehicle.vehicleIsStopped(...) or a vehicleIsRunning(...) method to the noai framework? I figure that the vehicles are probably just simple finite state machines or just have one simple variable "stopped" or whatever, so my guess would be it can't be that hard?
Nunc dimittis servum tuum Domine secundum verbum tuum in pace
User avatar
Zutty
Director
Director
Posts: 565
Joined: 22 Jan 2008 16:33

Re: vehicles and orders?

Post by Zutty »

wilco_moerman wrote:isn't it possible to just add a AIVehicle.vehicleIsStopped(...) or a vehicleIsRunning(...) method to the noai framework? I figure that the vehicles are probably just simple finite state machines or just have one simple variable "stopped" or whatever, so my guess would be it can't be that hard?
The devs are generally very helpful when it comes to adding new features. This one sounds fair enough to me.
PathZilla - A networking AI - Now with tram support.
wilco_moerman
Engineer
Engineer
Posts: 70
Joined: 05 Jun 2008 15:51

Re: vehicles and orders?

Post by wilco_moerman »

Zutty wrote:
wilco_moerman wrote:isn't it possible to just add a AIVehicle.vehicleIsStopped(...) or a vehicleIsRunning(...) method to the noai framework? I figure that the vehicles are probably just simple finite state machines or just have one simple variable "stopped" or whatever, so my guess would be it can't be that hard?
The devs are generally very helpful when it comes to adding new features. This one sounds fair enough to me.
my sollution for now is, to make a new class MyVehicle that has a field aiVehicle where I store the reference to a AIVehicle, and a boolean isStopped. I just rerout all commands to the AIVehicle through MyVehicle, and store whether I have stopped or started the vehicle. But this obviously only works if the AI the only one that actually stops or starts vehicles, and there are no function in the game that do so autonomously.

Making a wrapper around AIVehicle also has other advantages: I can use java style programming.

Code: Select all

class MyVehicle {
  aiVehicle = null;
//
//extra information
  isStopped = true;


//
//initialize
constructor(_aiVehicle) {
  aiVehicle = _aiVehicle;
  isStopped = true;
}


//
//one static method needed for construction
static function BuildVehicle(depot, type) {
  local vehicle = MyVehicle(AIVehicle.BuildVehicle(depot,type));
  //do some initializing or registering or whatever
  return vehicle;
}

//
//non-static methods for easy access 
function GetName() {return AIVehicle.GetName(this.aiVehicle);}

//
function start() {
  if (stopped) {
    stopped = false;
    AIVehicle.StartStopVehicle(this.aiVehicle);
  }
  else AILog.Warning(this + " is already running (invalid start() command");
}


//etc.

}//end class MyVehicle
this way I can use someVehicle.SomeFunction() instead of AIVehicle.SomeFunction(someVehicle). If you are used to Java, it allows you to write cleaner (i.e. Java-style :) ) code
Nunc dimittis servum tuum Domine secundum verbum tuum in pace
TrueBrain
OpenTTD Developer
OpenTTD Developer
Posts: 1370
Joined: 31 May 2004 09:21

Re: vehicles and orders?

Post by TrueBrain »

I will add those two functions for the TODO:

AIVehicle.GetVehicleStatus(), which returns: STOPPED, BROKEN, RUNNING, IN_DEPOT, or something similar
AIOrder.GetCurrentOrder(), or something similar, returning the current OrderID.
The only thing necessary for the triumph of evil is for good men to do nothing.
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 23 guests