Noob - questions

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
Eton
Engineer
Engineer
Posts: 4
Joined: 29 Aug 2008 17:21

Noob - questions

Post by Eton »

Hey there,

I have some basic questions concerning AI-writing.

1) How can you get the squareroot from a number, exists there a command like "sqrt{9}"?

2) When I use the noAI-build, set speed to fast forward and look how my AI is developing, after some time the game flow slows down and gets unsteady. Sometimes noAI-TTD hang-up. Where's the problem?

3) How do you use the transaction mode correctly? Until now I avioded using it, but it would be very helpful and make thinks easier.
My prolem is the following:

local scout = 0;
AISign.BuildSign(tileindex1, "Hello1");
scout = AITransactionMode();
AISign.BuildSign(tileindex2, "Hello2");
scout.Execute();
AISign.BuildSign(tileindex3, "Hello3");

The result is a "Hello1" sign, a "Schild"* sign and no third sign. (*I use German as ingame language: Schild = sign)
In transaction mode the sign isn't labeled, why?
The Execute() command should end the transaction mode and switch back to the normal mode or not?

I would be happy if someone could help me...
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: Noob - questions

Post by Yexo »

Eton wrote:Hey there,

I have some basic questions concerning AI-writing.
Hello and welcome to the forums.
1) How can you get the squareroot from a number, exists there a command like "sqrt{9}"?
You can't, but you shouldn't need to. Why do you want to do this?
2) When I use the noAI-build, set speed to fast forward and look how my AI is developing, after some time the game flow slows down and gets unsteady. Sometimes noAI-TTD hang-up. Where's the problem?
What revision of NoAI are you running? If you have this problem with the latest build, can you post your AI somewhere? (Or in case you don't want to reveal it, pm it privatly to TrueBrain so he can try to reproduce / fix it.
3) How do you use the transaction mode correctly? Until now I avioded using it, but it would be very helpful and make thinks easier.
My prolem is the following:

local scout = 0;
AISign.BuildSign(tileindex1, "Hello1");
scout = AITransactionMode();
AISign.BuildSign(tileindex2, "Hello2");
scout.Execute();
AISign.BuildSign(tileindex3, "Hello3");

The result is a "Hello1" sign, a "Schild"* sign and no third sign. (*I use German as ingame language: Schild = sign)
In transaction mode the sign isn't labeled, why?
The Execute() command should end the transaction mode and switch back to the normal mode or not?
The Execute() command does indeed stop recording commands into 'scout', but it does not switch back to execute mode. To do that, either let the object scout go out of scope or create an AIExecMode object.

That the second sign reads "Sign" (or "Schild" in your case) instead of "Hello2" is an error, I'm looking into that now.
Edit: it's clearly a bug, I'm not sure how it can be fixed though.
Eton
Engineer
Engineer
Posts: 4
Joined: 29 Aug 2008 17:21

Re: Noob - questions

Post by Eton »

Thanks for your fast reply.
Yexo wrote:
Eton wrote: 1) How can you get the squareroot from a number, exists there a command like "sqrt{9}"?
You can't, but you shouldn't need to. Why do you want to do this?
I wanted to use it with AITile.GetSquareDistance(). Now I use it to valuate, but it would be fine to have the exact distance...
2) When I use the noAI-build, set speed to fast forward and look how my AI is developing, after some time the game flow slows down and gets unsteady. Sometimes noAI-TTD hang-up. Where's the problem?
What revision of NoAI are you running? If you have this problem with the latest build, can you post your AI somewhere? (Or in case you don't want to reveal it, pm it privatly to TrueBrain so he can try to reproduce / fix it.
I use r14122-noai and the computer power should be sufficient. The AI is in an early stadium, but I try to post it later.
3) How do you use the transaction mode correctly? Until now I avioded using it, but it would be very helpful and make thinks easier.
My prolem is the following:

local scout = 0;
AISign.BuildSign(tileindex1, "Hello1");
scout = AITransactionMode();
AISign.BuildSign(tileindex2, "Hello2");
scout.Execute();
AISign.BuildSign(tileindex3, "Hello3");

The result is a "Hello1" sign, a "Schild"* sign and no third sign. (*I use German as ingame language: Schild = sign)
In transaction mode the sign isn't labeled, why?
The Execute() command should end the transaction mode and switch back to the normal mode or not?
The Execute() command does indeed stop recording commands into 'scout', but it does not switch back to execute mode. To do that, either let the object scout go out of scope or create an AIExecMode object.
How can I let the object scout go out of scope??

Sign "Hello2" is a bug? Ok, maybe other things building is ok...
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: Noob - questions

Post by Yexo »

Eton wrote:I use r14122-noai and the computer power should be sufficient. The AI is in an early stadium, but I try to post it later.
If you can reproduce the problem with your AI, but not with other AIs, the best is to post your AI as it is now (no matter how much debug code or whether it works or not
How can I let the object scout go out of scope??

Sign "Hello2" is a bug? Ok, maybe other things building is ok...
I think the following code does what your want (except for renaming the first instead of the second sign to "Hello2", but as I stated before, that is a bug of AISign.BuildSign in combination with AITransactionMode).

Code: Select all

	local tileindex = 10000;
	AISign.BuildSign(tileindex, "Hello1");
	{
		local scout = AITransactionMode();
		AISign.BuildSign(tileindex + 2, "Hello2");
		scout.Execute();
	}
	AISign.BuildSign(tileindex + 4, "Hello3");
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: Noob - questions

Post by Yexo »

Eton wrote:
Yexo wrote:
Eton wrote: 1) How can you get the squareroot from a number, exists there a command like "sqrt{9}"?
You can't, but you shouldn't need to. Why do you want to do this?
I wanted to use it with AITile.GetSquareDistance(). Now I use it to valuate, but it would be fine to have the exact distance...
Most ingame distances (ie for profit) are calculated with the manhattan distance. If you want to compare the square root of AITile.GetSquareDistance() to 5, why not compare it (without square root) to 25?
User avatar
Zutty
Director
Director
Posts: 565
Joined: 22 Jan 2008 16:33

Re: Noob - questions

Post by Zutty »

Eton wrote:1) How can you get the squareroot from a number, exists there a command like "sqrt{9}"?
Thankfully you can approximate sqrt as follows. This is using integer mathematics so its not perfect, but its better than nothing.

Code: Select all

/*
 * Integer square root. This function was converted from the C++ version found 
 * at codecodex.com.
 */
function sqrt(num) {
    if (0 == num) {
    	return 0;
    }
    
    local n = (num / 2) + 1;
    local n1 = (n + (num / n)) / 2;
    
    while (n1 < n) {
		n = n1;
		n1 = (n + (num / n)) / 2;
    }
    
    return n;
}
PathZilla - A networking AI - Now with tram support.
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: Noob - questions

Post by Yexo »

Zutty wrote:
Eton wrote:1) How can you get the squareroot from a number, exists there a command like "sqrt{9}"?
Thankfully you can approximate sqrt as follows. This is using integer mathematics so its not perfect, but its better than nothing.
Of course this is possible, but again: Why is it usefull?
User avatar
Michiel
Transport Coordinator
Transport Coordinator
Posts: 345
Joined: 13 Jul 2008 00:57
Contact:

Re: Noob - questions

Post by Michiel »

Well, sqrt also quite useful to work out an approximate "city area" from its population.
Eton
Engineer
Engineer
Posts: 4
Joined: 29 Aug 2008 17:21

Re: Noob - questions

Post by Eton »

It is not a problem that only concerns my ai. When I run WrightAI or Convoy the game speed also gets slower.
Especially with Convoy and it's amount of vehicles.

How should I post the AI, I can't link it and it would spam this thread...

@Yexo: Your transaction mode solution is good for me, I'll use it.

Concerning sqrt{}, it is not really useful for me, but it looks more nice in the code...
@Zutty: Thanks for the formula, but I already compare the squaredistance with i * i and it works, so i'll leave it.
Finaldeath
Engineer
Engineer
Posts: 72
Joined: 09 Apr 2006 23:49
Location: UK
Contact:

Re: Noob - questions

Post by Finaldeath »

I already stated ages ago myself that using sqrt for calculating diagonal distances (ie; the ones planes, ships and trains can use) may help if you want to know how fast a route is, not just how physically far apart the game thinks two locations are.
Finaldeath
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: Noob - questions

Post by Zuu »

Eton wrote:How should I post the AI, I can't link it and it would spam this thread...
Either make a tar-file if you want to use a NoAI-comptible file format for storing your ai without needing other people to "decode" the tar-file. (tar-files are not compromized just many files in one file). Or you put them in a zip-file. But upload the archive file to the forum using post attachement. If you fail to make any archive-file then you can rename your main.nut as main.nut.txt and upload it as an attachment. And possible the same with the info.nut file.

There is also paste-bins such as paste.openttd.org, but it is better if you use attachments as they are more permanent than paste-bins and don't rely on another system than the forum-system.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
Eton
Engineer
Engineer
Posts: 4
Joined: 29 Aug 2008 17:21

Re: Noob - questions

Post by Eton »

Ok, here my AI, the name is still "MtestAI";-)
mtestai.zip
(8.75 KiB) Downloaded 143 times
It builds only one air route, very simple.
It has difficulties with water and a start date between 1956 and 1989 is good (because of the airport size).

I have still problems with the game speed, bud I doubt that it is because of my AI.
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 5 guests