My AI doesn't build something

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
.weasel
Engineer
Engineer
Posts: 4
Joined: 03 Apr 2011 21:38

My AI doesn't build something

Post by .weasel »

Hej guys,

i started to write an AI on my own.
I simply started with a Car Depot building. But my AI doesn't build it.
Here is the Code.

Code: Select all

 function TestAI::BuildSomething()
 {
	local firstTile = AIMap.GetTileIndex(2,2);
	local secondTile = AIMap.GetTileIndex(2,3);
	
	AIRoad.BuildRoadDepot(firstTile, secondTile);	
 }
I wonder why this won't work?! The terrain is flat and buildable. If I change the depot build command to the HQ build command, the AI will build its home. But neither depot nor road.
:?

Thanks for help.
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: My AI doesn't build something

Post by Yexo »

When anything related to building on the map, buying vehicles, changes orders etc. doesn't work try to find out why first. The easiest way you can do that is by printing the error message to the AI debug log. You can do that like this:

Code: Select all

AILog.Error(AIError.GetLastErrorString());
Before building anything related to roads you must first call AIRoad::SetCurrentRoadType
.weasel
Engineer
Engineer
Posts: 4
Joined: 03 Apr 2011 21:38

Re: My AI doesn't build something

Post by .weasel »

Thanks for the clue! :)
The road type was missing.

:bow:
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: My AI doesn't build something

Post by Zuu »

Also IIRC you have to call AIController.Sleep(1); before doing the first action (DoCommand) as the first tick doesn't allow any actions/DoCommands.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
.weasel
Engineer
Engineer
Posts: 4
Joined: 03 Apr 2011 21:38

Re: My AI doesn't build something

Post by .weasel »

Hej,
I've got a new Problem :D
These Enumerators are really confusing.

Code: Select all

aSlope = AITile.GetSlope(aTile);
(Enumerators of Slops)
So far. So good. But what is now inside "aSlope"? There is a number. But I don't know what to do with it. When I've got a SLOPE_NW on my screen, it returns number 9?! :?
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: My AI doesn't build something

Post by Yexo »

It's actually a bitmask.

SLOPE_NW = SLOPE_N | SLOPE_W;
.weasel
Engineer
Engineer
Posts: 4
Joined: 03 Apr 2011 21:38

Re: My AI doesn't build something

Post by .weasel »

Thanks for your fast reply. It helped me a lot!

Another question: Why are these two lines the same:
(both are initiated with local tileList = AITileList(); )

Code: Select all

tileList.AddRectangle(start, end);

Code: Select all

tileList.AddRectangle(end, start);
Both lists start with the end tile?!?!
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: My AI doesn't build something

Post by Yexo »

If you want to sort the list you have to use the AIList.Sort() function. By default lists are sorted ascending by item.
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 3 guests