What sort of optimisation is it? (solved)

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
Brumi
President
President
Posts: 921
Joined: 18 Jul 2009 17:54

What sort of optimisation is it? (solved)

Post by Brumi »

I'm working on my AI to handle the case when it is interrupted while building a passing lane section.
Here's a part of the code:

Code: Select all

success = success && AIRail.BuildRail(tile + vector, tile, tile - rvector);
success = success && AIRail.BuildRail(tile, tile - rvector, tile - rvector - vector);
success = success && AIRail.BuildSignal(centre - vector, centre, signaltype);
success = success && AIRail.BuildSignal(centre - vector - vector + rvector, centre - vector - vector, signaltype);
success = success && AIRail.BuildSignal(centre + rvector + vector + vector, centre + rvector + vector, signaltype);
success = success && AIRail.BuildSignal(centre + vector + vector + vector, centre + vector + vector + vector + rvector, signaltype);
if (!success) {
	AILog.Warning("Passing lane construction was interrupted.");
	cBuilder.RemoveRailLine(end[0][1]);
	cBuilder.RemoveRailLine(end[1][1]);
	return null;
}
I tested this by placing an obstacle where the AI intended to build its passing lane section. I expected that the AI would ignore it at first, and then realise that it was interrupted. But in reality the AI realised it immediately when it ran into the obstacle.
So, this is really not a problem, I'm only curious, how is it optimised? Sorry for being so n00bish about programming. :D
Last edited by Brumi on 15 Mar 2010 17:59, edited 1 time in total.
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: What sort of optimisation is it?

Post by Yexo »

I'm not sure I udnerstand your question. if there is an obstable AIRail.BuildRail will return false, so your AI will notice immediately that building the rail failed.
Brumi
President
President
Posts: 921
Joined: 18 Jul 2009 17:54

Re: What sort of optimisation is it?

Post by Brumi »

I'll try to explain myself:
With this code structure, I expect the AI to build this:
screenshot #1.png
screenshot #1.png (19.59 KiB) Viewed 2239 times
And then realise that it was interrupted, print the "construction interrupted" message and delete the whole thing.

Instead, my AI does this:
screenshot #2.png
screenshot #2.png (7.33 KiB) Viewed 2239 times
This is the point when it prints the message and deletes the rail.
Kogut
Tycoon
Tycoon
Posts: 2493
Joined: 26 Aug 2009 06:33
Location: Poland

Re: What sort of optimisation is it?

Post by Kogut »

That is how drag & drop works, for ais it is working like for humans. (To make screenshoot #1 you needed to place two parts of railway, right?)
Correct me If I am wrong - PM me if my English is bad
AIAI - AI for OpenTTD
Brumi
President
President
Posts: 921
Joined: 18 Jul 2009 17:54

Re: What sort of optimisation is it?

Post by Brumi »

My AI builds the track tile by tile, so according to my expectations the first failure will only set the success variable to false and then go on. It is only handled at the end of the function.
Anyway, it might be that I'm a bit tired and I'm not getting somethingt really trivial. :?
svetovoi
Engineer
Engineer
Posts: 87
Joined: 12 Oct 2007 14:07

Re: What sort of optimisation is it?

Post by svetovoi »

For other result you should write

Code: Select all

success = [Your build code] && success;
When success becomes false there is no need to calculate further variables - since expression result is calculated from left to right, and result of false && [something] is false, no matter what [something] is.
That is why [something] calculation is such cases usually ignored. See this
Last edited by svetovoi on 15 Mar 2010 18:08, edited 1 time in total.
Brumi
President
President
Posts: 921
Joined: 18 Jul 2009 17:54

Re: What sort of optimisation is it? (solved)

Post by Brumi »

That's true, I didn't think of that. :D Thank you!
Anyway, it's better like this, I just wanted to know why this happened.
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: What sort of optimisation is it?

Post by Zuu »

Kogut wrote:That is how drag & drop works, for ais it is working like for humans. (To make screenshoot #1 you needed to place two parts of railway, right?)
IIRC both rail and road is now consistent for AIs using the way road used to be for humas. Eg. if you drag over serveral tiles and one of them fails then nothing will be built and the function will return false.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: Google [Bot] and 4 guests