Building returns true in AITestMode but False in AIExecMode

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
kirky_D
Engineer
Engineer
Posts: 24
Joined: 25 Mar 2010 15:34

Building returns true in AITestMode but False in AIExecMode

Post by kirky_D »

Hi there!

Having a bit of difficulty with AITestMode and AIExecMode. I have a bit of code which builds a station plus a depot and a few tracks. Running it in AITestMode all building returns true. As soon as anything is built in AIExecMode it returns false but STILL BUILDS IT.

I can't seem to work it out. It doesn't matter what is actually being built either, station, depot and track all return false. Here is the code in question and a screenshot of the Debug window and built station.

Code: Select all

function buildStationTracks(tile, direction)
{
	local success = true;
	local test_mode = true;
	AILog.Info("          success " + success);
	AITestMode();
	
	while(true)
	{
		switch(direction)
		{
			case Direction.NE:
			{
				AILog.Info("1          success " + success);
				success = success && AIRail.BuildRailDepot(tile + AIMap.GetTileIndex(3,0), tile + AIMap.GetTileIndex(3,1));
				success = success && AIRail.BuildRailStation(tile + AIMap.GetTileIndex(4,1), AIRail.RAILTRACK_NE_SW, 2, 3, AIStation.STATION_NEW);
				AILog.Info("3          success " + success);
				success = success && AIRail.BuildRail(tile + AIMap.GetTileIndex(1,1), tile + AIMap.GetTileIndex(2,1), tile + AIMap.GetTileIndex(4,1));
				success = success && AIRail.BuildRail(tile + AIMap.GetTileIndex(2,2), tile + AIMap.GetTileIndex(3,2), tile + AIMap.GetTileIndex(4,2));
				AILog.Info("4          success " + success);
				success = success && AIRail.BuildRailTrack(tile + AIMap.GetTileIndex(3,1), AIRail.RAILTRACK_NE_SE);
				success = success && AIRail.BuildRailTrack(tile + AIMap.GetTileIndex(3,1), AIRail.RAILTRACK_NW_NE);
				success = success && AIRail.BuildRailTrack(tile + AIMap.GetTileIndex(3,1), AIRail.RAILTRACK_NW_SW);
				success = success && AIRail.BuildRailTrack(tile + AIMap.GetTileIndex(3,1), AIRail.RAILTRACK_SW_SE);
				success = success && AIRail.BuildRailTrack(tile + AIMap.GetTileIndex(3,1), AIRail.RAILTRACK_NW_SE);
				AILog.Info("5          success " + success);
				success = success && AIRail.BuildRailTrack(tile + AIMap.GetTileIndex(3,2), AIRail.RAILTRACK_NW_SW);
				success = success && AIRail.BuildRailTrack(tile + AIMap.GetTileIndex(3,2), AIRail.RAILTRACK_NW_NE);
				AILog.Info("6          success " + success);
				break;
			} 
...
}		
		if(!success) 
		{
			AILog.Info("buildStationTracks(tile, direction):");
			AILog.Info("          !success " + success);
			return false;
		}
		
		if(success && test_mode)
		{			
			AILog.Info("buildStationTracks(tile, direction):");
			AILog.Info("          success && test_mode");
			AIExecMode();
			test_mode = false;
		}
		else if(success && !test_mode)
		{
			AILog.Info("buildStationTracks(tile, direction):");
			AILog.Info("          success && !test_mode");
			return true;
		}
		else
		{
			AILog.Info("buildStationTracks(tile, direction):");
			AILog.Info("          else");
			return false;
		}
	}
}
Note that I have only included the northeast direction in the switch statement because all the other directions simply repeat the same code with different offsets.
Lintston Transport, 25th May 1958.png
(106.98 KiB) Downloaded 2 times
Thanks for your help
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: Building returns true in AITestMode but False in AIExecM

Post by Yexo »

You have to store the result of AITestMode() / AIExecMode() in a local variable. The mode will end as soon as that variable goes out of scope. What currently happens is that you enter TestMode on line 6 and immediately leave it again so you go back to the default ExecMode().

So what happens is that the first run is done in ExecMode, this succeeds but you log it as a succeed in testmode. Now you run it again (also in ExecMode) and it fails, so you log a failure in the ExecCode.
kirky_D
Engineer
Engineer
Posts: 24
Joined: 25 Mar 2010 15:34

Re: Building returns true in AITestMode but False in AIExecM

Post by kirky_D »

Ah I see. So I don't actually build anything in test mode?

Thanks for your help!
Kogut
Tycoon
Tycoon
Posts: 2493
Joined: 26 Aug 2009 06:33
Location: Poland

Re: Building returns true in AITestMode but False in AIExecM

Post by Kogut »

kirky_D wrote:Ah I see. So I don't actually build anything in test mode?!
Yes
Correct me If I am wrong - PM me if my English is bad
AIAI - AI for OpenTTD
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 5 guests