Console Commands?

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
reylas
Engineer
Engineer
Posts: 52
Joined: 22 Dec 2007 01:04

Console Commands?

Post by reylas »

I was just wondering, would it be possible to add the ability to run AI commands at the console and have it affect the game? May be A LOT of work for little pay off, but it would be handy sometimes to be able to run a command to see what certain variables hold or to place a sign by doing an AISign.BuildSign(tile,"foo") at the console.

Just wondering. May help me understand my slope issues :)

MarkS
TrueBrain
OpenTTD Developer
OpenTTD Developer
Posts: 1370
Joined: 31 May 2004 09:21

Re: Console Commands?

Post by TrueBrain »

We talked about this in the past. Currently there is some work on a Squirrel-based console, which in fact can allow such things. Even without it, bypasses are possible to get things executed on the AI's side. But what you get in return is a way to make an AI do something it isn't doing on its own.. which is a nice description for cheating :) So we decided not to add such feature, although it can be very useful for debugging. Downside of adding 'debug only' stuff, when you remove it, people complain.

To give a small example: once the Windows build had a 'feature' to get the screen in double-size (so pixels were 2x2 instead of 1x1). This was meant as debug-tool only. When it was removed, as it was Windows only, and therefor silly, people complained. In fact, people are still complaining they miss this feature.

So the lesson? Don't include something for debug-only if you are not willing to keep it there for ever. And as this can clearly be abused, I rather not have it in the NoAI framework :)
On the other side, such 'backdoor' can be very dangerous. The current code executed by the AI have to be aborted to execute your requested code, after which he should continue with his original program. I think everyone understand this is very tricky business. The only way would be via an Event, which you need to add support for in your AI, which makes the whole framework only more complex. So also from the point of view, I think it is better not to have it :)

Nevertheless, I am glad you bring it up :) As I walked with the idea myself for quiet some time.

And to help you a bit with your problem: to debug, I suggest adding tons of print() statements. They help you trace your problem easier, than any other method :)
The only thing necessary for the triumph of evil is for good men to do nothing.
reylas
Engineer
Engineer
Posts: 52
Joined: 22 Dec 2007 01:04

Re: Console Commands?

Post by reylas »

TrueLight wrote:We talked about this in the past. Currently there is some work on a Squirrel-based console, which in fact can allow such things. Even without it, bypasses are possible to get things executed on the AI's side. But what you get in return is a way to make an AI do something it isn't doing on its own.. which is a nice description for cheating :) So we decided not to add such feature, although it can be very useful for debugging. Downside of adding 'debug only' stuff, when you remove it, people complain.

To give a small example: once the Windows build had a 'feature' to get the screen in double-size (so pixels were 2x2 instead of 1x1). This was meant as debug-tool only. When it was removed, as it was Windows only, and therefor silly, people complained. In fact, people are still complaining they miss this feature.

So the lesson? Don't include something for debug-only if you are not willing to keep it there for ever. And as this can clearly be abused, I rather not have it in the NoAI framework :)
On the other side, such 'backdoor' can be very dangerous. The current code executed by the AI have to be aborted to execute your requested code, after which he should continue with his original program. I think everyone understand this is very tricky business. The only way would be via an Event, which you need to add support for in your AI, which makes the whole framework only more complex. So also from the point of view, I think it is better not to have it :)

Nevertheless, I am glad you bring it up :) As I walked with the idea myself for quiet some time.

And to help you a bit with your problem: to debug, I suggest adding tons of print() statements. They help you trace your problem easier, than any other method :)
Ok.. Good enough. I was about scared to bring it up, but now I am glad I did. I could see the cheating being a problem, just was looking for a way to figure out why my slopes made no sense.

I am headed out for vacation, maybe the sand will clear my head :)

MarkS.
TrueBrain
OpenTTD Developer
OpenTTD Developer
Posts: 1370
Joined: 31 May 2004 09:21

Re: Console Commands?

Post by TrueBrain »

reylas wrote:(..)
I am headed out for vacation, maybe the sand will clear my head :)

MarkS.
Have a good one! Bring some sand back for us ;)
The only thing necessary for the triumph of evil is for good men to do nothing.
Finaldeath
Engineer
Engineer
Posts: 72
Joined: 09 Apr 2006 23:49
Location: UK
Contact:

Re: Console Commands?

Post by Finaldeath »

If you do want the AI to do something when it detects a sign, you can get all the signs and simply loop over until you find the text you want, or the owner you want (ie; the player).

I plan to do this once I have something working, so I can "force a route" if two points are detected and named (eg; two points for stations are named "road" and "road" :) ). I should post some code up once I get it working.
Finaldeath
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: Console Commands?

Post by Yexo »

To give you a start, this are the first lines of the code of my ai, used to remove all sign of the same company. I use this so I can reload my ai and it'll clean up all signs from previous runs on the same map. It even tries to remove signs from other companies, but that'll silently fail.

Code: Select all

	for(local i=0; i<AISign.GetMaxSignID(); i++) {
		if (AISign.IsValidSign(i))
			AISign.RemoveSign(i);
	}
User avatar
Zutty
Director
Director
Posts: 565
Joined: 22 Jan 2008 16:33

Re: Console Commands?

Post by Zutty »

Finaldeath wrote:If you do want the AI to do something when it detects a sign, you can get all the signs and simply loop over until you find the text you want, or the owner you want (ie; the player).

I plan to do this once I have something working, so I can "force a route" if two points are detected and named (eg; two points for stations are named "road" and "road" :) ). I should post some code up once I get it working.
DAMN YOU - GET OUT OF MY BRAIN!! :wink: I had a very similar idea on the ride home from work today! :D

It would be cool to allow the player (a human player mind you - lets not explout ackdoors in each others AIs!!!) some control over the AI, just to dynamically configure it. I was thinking of something like "AI:REMOVE" over a tile would instruct it to delete whatever it built on that tile and possibly re-route, e.g. if the AI has built something that conflict the with the player's carefully hatched plans. Other useful commands could include stuff like "AI:EASIER"/"AI:HARDER" to allow the player to change the difficulty on the fly, "AI:IGNORE <asset>"/"AI:FOCUS <asset>" to instruct an AI to specialise/generalise (e.g. AI:IGNORE Menworth, AI:IGNORE Coal, AI:FOCUS Trams) to prevent the AI from stomping on the player's toes if they don't want.

I also thought it might be cool to create an AI that is completely subservient to the player, for players who enjoy playing "train-set" style games. The AI would do absolutely nothing without being explicitly instructed. If for example you wanted the AI to build a road from one to to another, you could drop a sign in a town saying something like "AI: ROUTEFROM SuchAndSuchTown" to order the AI to build a route from the specified town to the town that the sign is laid in.

Just a crazy idea at this stage!

As for how to do it, I was thinking that you just keep polling AISign.GetMaxSignID() to detect new signs.

Edit: And Yexo has aleady demonstrated that!!
PathZilla - A networking AI - Now with tram support.
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: Console Commands?

Post by Yexo »

It's going a bit offtopic, but anyway. Some weeks ago I created an AIChat patch, so AI's are able to send and receive chat messages. If there is interest I'll update it and post it tomorrow. It won't be included in noai for now, because it makes the diff with trunk a lot bigger. For those of you compiling yourself, it might be nice to try.
Finaldeath
Engineer
Engineer
Posts: 72
Joined: 09 Apr 2006 23:49
Location: UK
Contact:

Re: Console Commands?

Post by Finaldeath »

Chat would be good, I want to build some personality into my AI so it can taunt the other players :)

Just a note, this is my version (which is, of course, really similar to yours Yexo) which I should have posted, but it only removes the current companies signs. When I intend to test more then one AI it stops the reload function stopping all the debugging of all the AI's (or if this "sign placement" is used from the player, having those removed accidentally).

Code: Select all

/**
 * RemoveAllSigns
 *
 * Cleanup function. Signs are not removed on a AI's death, so this clears them all up (all from the map). Used at the start of an AI run.
 */
function RemoveAllSigns()
{
	for(local i=0; i <= AISign.GetMaxSignID(); i++)
	{
		if(AISign.IsValidSign(i) && AICompany.IsMine(AISign.GetOwner(i)))
		{
			AISign.RemoveSign(i);
		}
	}
}
A simple change can find a sign with a certain name:

Code: Select all

// Returns the sign id matching text, or -1 (invalid sign) on error.
function FindSignMatchingText(text)
{
	for(local i=0; i <= AISign.GetMaxSignID(); i++)
	{
		if(AISign.IsValidSign(i) && AISign.GetText(i) == text)
		{
			return i;
		}
	}
	return -1;
}
Finaldeath
reylas
Engineer
Engineer
Posts: 52
Joined: 22 Dec 2007 01:04

Re: Console Commands?

Post by reylas »

Actually, I was trying more to find information about certain tiles. Say for instance, AITile.GetSlope and be able to query tiles on the fly. I wanted to use it for less devious purposes :)
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: Console Commands?

Post by Yexo »

Finaldeath wrote:When I intend to test more then one AI it stops the reload function stopping all the debugging of all the AI's (or if this "sign placement" is used from the player, having those removed accidentally).
While your code is more clean, it does exactly the same :) The ai isn't allowed to remove signs from other companies, so although my code tries to do that, it'll fail on those signs.
Roujin
Tycoon
Tycoon
Posts: 1884
Joined: 08 Apr 2007 04:07

Re: Console Commands?

Post by Roujin »

Yexo wrote:
Finaldeath wrote:When I intend to test more then one AI it stops the reload function stopping all the debugging of all the AI's (or if this "sign placement" is used from the player, having those removed accidentally).
While your code is more clean, it does exactly the same :) The ai isn't allowed to remove signs from other companies, so although my code tries to do that, it'll fail on those signs.
Why's that? Players can do that too, no?
* @Belugas wonders what is worst... a mom or a wife...
<Lakie> Well, they do the same thing but the code is different.

______________
My patches
check my wiki page (sticky button) for a complete list

ImageImage
ImageImageImageImageImageImageImage
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: Console Commands?

Post by Yexo »

Roujin wrote:
Yexo wrote:
Finaldeath wrote:When I intend to test more then one AI it stops the reload function stopping all the debugging of all the AI's (or if this "sign placement" is used from the player, having those removed accidentally).
While your code is more clean, it does exactly the same :) The ai isn't allowed to remove signs from other companies, so although my code tries to do that, it'll fail on those signs.
Why's that? Players can do that too, no?
Hmm, I thought I tested that, but apparently I was wrong. AIs can remove signs from other companies. In that case it's better to use Finaldeath's code, so you can have mulitpe AIs usings sign simultaniously.
Finaldeath
Engineer
Engineer
Posts: 72
Joined: 09 Apr 2006 23:49
Location: UK
Contact:

Re: Console Commands?

Post by Finaldeath »

Yeah, I did wonder and thus now why we can get the sign owner (thanks guys :) ). It was surprising you could take signs of others (and remove them, change to your colour, etc), I never realised since I don't play multiplayer! :D

They are also not removed on bankruptcy - but if a company takes over that "slot" (0-7 company id number) then they take the signs back.

reylas; you could do just that. You have an AI that debugs a sign-faced tile to the console. So for that query; sloping tiles:

Code: Select all

// In our main loop...

// Check for sign
local sign = FindSignMatchingText("slopes")
if(sign != -1)
{
    // Get location
    local tile = AISign.GetLocation(sign);

    // Remove it
    AISign.RemoveSign(sign);

    // Debug this slope
    AILog.Info("We see tile id [" + tile + "] as slope: [" + AITile.GetSlope(tile) "]");
}
Something like that. Could do a proper string replacement of GetSlope's output if you wanted to make it easier, heh. Or you could rename the sign with whatever slope id appears so it is visible on the map (or just change the AILog.Info to a new build sign).

I'll take a look at implementing this in my own test AI, I want to do some tests for water, and where ships can go, and where docks can be built (since docks need more then a shore and a piece of water in front of it, it needs some specific space for it).
Finaldeath
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 31 guests