[solved] OpenTTD hangs on Sleep()

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
User avatar
yorg
Engineer
Engineer
Posts: 18
Joined: 19 May 2008 21:48
Location: Peterborough, Ontario

[solved] OpenTTD hangs on Sleep()

Post by yorg »

Hi,

I have been working on an AI for a few days now, and have been having an absolute blast! First and foremost I want to congratulate the developers for creating such an amazing addition to OpenTTD. I don't even think I play OpenTTD anymore, when ever it's open it's 'cause i'm working on my AI. I'm looking forward to having something complete enough to share fairly soon.

I have just run into a problem that is really confusing me at the moment. My Start() function begins like this:

Code: Select all

AILog.Info("Welcome to robotAI.")
this.Sleep(1);
this.SetCompanyName();
this.name = AICompany.GetCompanyName(AICompany.MY_COMPANY);
AILog.Info("INIT: Name set: " + this.name);
this.Sleep(1);
this.myCargo = this.ChooseBestCargo(AICargo.CC_BULK);	
AILog.Info("INIT: Gonna go for cargo #" + myCargo);
this.Sleep(1);
I never had a problem with this until about 10 minutes ago, while I was working on a completely different part of the program. Now whenever I try to load my AI, it gets as far as the last Sleep() call in the code above, then OpenTTD hangs with my processor running at 100% utilization. If I remove that Sleep() call, the same thing happens at the Sleep() call before. I have tried changing Sleep(1) to Sleep(0) with the same results, and removing all three of the calls causes OpenTTD to hang after the first line.

I have looked through the documentation and this forum, and have no idea what could be causing this problem. I am running r13532 under Ubuntu 8.04, on a 750MHz Pentium 3.

If anyone has any suggestions as to a solution I would be very appreciative!

--
yorg
Last edited by yorg on 17 Jun 2008 22:17, edited 1 time in total.
Rubidium
OpenTTD Developer
OpenTTD Developer
Posts: 3815
Joined: 09 Feb 2006 19:15

Re: OpenTTD hangs on Sleep()

Post by Rubidium »

Sounds like an infinite loop that is running after your Sleep. NoAI is going to have a system that will let each AI run a few hundred lines of code each game tick, so big (and even infinite) loops don't hang OpenTTD anymore, but that is not done yet.
User avatar
yorg
Engineer
Engineer
Posts: 18
Joined: 19 May 2008 21:48
Location: Peterborough, Ontario

Re: OpenTTD hangs on Sleep()

Post by yorg »

Thanks for your help, Rubidium!

You were right that it was an infinite loop; it was getting stuck endlessly looking for station sites. Since the script was never giving OpenTTD a chance to display it's output I didn't realize it was getting that far in the program.

The problem resulted from the fact that a local variable declared inside a while loop is not seen by the loop itself, eg. this is an infinite loop:

Code: Select all

local count = 0;
while (count == 0) {
   local count = 1;
}
Taking away the "local" on the 3rd line causes the loop to terminate properly.

With this problem out of the way I can on with the fun stuff! Thanks again!
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 5 guests