SimpleAI v14 - trying to remake the old AI

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

Brumi
President
President
Posts: 904
Joined: 18 Jul 2009 17:54

Re: SimpleAI v5 - trying to remake the old AI

Post by Brumi » 21 Nov 2010 12:05

SirkoZ wrote:Brumi - the algorithm for bankrupting a company is in a file named economy.cpp - for version r21266 of trunk - it is line 465:
Thanks for that, I'll have a look when I have more time.
SirkoZ wrote:Plus I've got another idea; if path-signals are selected for railway it would be beneficial for lorry drivers to build one on each end of single-rail station assembly such as this:
That's a good idea, I'm also annoyed by the fact that SimpleAI often tends to crush road vehicles, despite that there is quite a high penalty for level crossings in the pathfinder.
Placing signals in front of the stations is very easy, it can be done by adding this after line 51 in railbuilder.nut into cBuilder::BuildSingleRailStation:

Code: Select all

if (AIController.GetSetting("signaltype") == 2) {
	success = success && AIRail.BuildSignal(stafront, depfront, AIRail.SIGNALTYPE_PBS);
}
The problem here is that level crossings on the line will be red in roughly 50% of the time, which may cause huge jams. Anyway, here is a modified version of railbuilder.nut, see whether it causes problems or not.
railbuilder.nut
(32.26 KiB) Downloaded 175 times
The second version is not so easy, most likely I would have to modify something in cBuilder::BuildRail. But then I would have to keep sure that those extra signals are only built on single railway lines.
This feature will most likely be there in the next version, maybe as a fourth option to the signal types if this causes jams.

Brumi
President
President
Posts: 904
Joined: 18 Jul 2009 17:54

Re: SimpleAI v5 - trying to remake the old AI

Post by Brumi » 14 Jun 2011 11:14

Huge bump! :D
Just to inform you that I'm still working on SimpleAI, there is a release candidate available exclusively here on the forums for version 6. It's in the first post.

There are no big features introduced, the most important piece of work was that I commented the code properly. I hope it will be better for me and others who read the code :)
I did my comments in Doxygen style, but I cannot make Doxygen generate documentation for Squirrel. Is that possible?
There is also a new readme available :)

And from the players' point of view, SimpleAI is now able to refit train wagons (I don't know why I hadn't implemented this before). There is a new setting to use path signals at single rail stations as well (SirkoZ's idea). I also fixed the problem that Samu reported, now the AI takes station maintenance costs into consideration as well, it will survive in the savegame in this post. I also fixed a bug I found recently in my tests, some old vehicles didn't get replaced at all.
I also added a setting to use NewGRF rail stations, and it is configurable with a setting. It is currently off by default (old AI behaviour), and the setting may be a little bit hard to find, do you think it should be on instead by default?

After adding refitting support for train wagons, I was surprised to see that SimpleAI could do fairly well with the ECS vectors and FIRS, using the ECS & FIRS original vehicle set NewGRF 8) The UK trainset is still too hard...

User avatar
Lord Aro
Tycoon
Tycoon
Posts: 2365
Joined: 25 Jun 2009 16:42
Location: Location, Location
Contact:

Re: SimpleAI v5 - trying to remake the old AI

Post by Lord Aro » 14 Jun 2011 11:27

With regards to the Newgrf stations parameter:
I would say you should look at the AI by saying, 'if the original ai was included now, would this be implemented?'
i would say yes :)
AroAI - A really feeble attempt at an AI

It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration. --Edsger Dijkstra

User avatar
SirkoZ
Tycoon
Tycoon
Posts: 1501
Joined: 06 Mar 2004 23:51
Location: The sunny side of Alps

Re: SimpleAI v5 - trying to remake the old AI

Post by SirkoZ » 15 Jun 2011 20:40

Thank you, Brumi, for your effort. Nice to see also that all needed for it to cope with all kinds of additional cargoes is just to give it the ability to refit the wagons. Sweet. 8)

Brumi
President
President
Posts: 904
Joined: 18 Jul 2009 17:54

Re: SimpleAI v6 - trying to remake the old AI

Post by Brumi » 23 Jun 2011 12:11

I've just uploaded version 6 to Bananas and the first post. The only change is that now building NewGRF rail stations is enabled by default.

I'm still unable to generate Doxygen documentation for SimpleAI, is that possible for Squirrel?

Yexo
Tycoon
Tycoon
Posts: 3653
Joined: 20 Dec 2007 12:49

Re: SimpleAI v6 - trying to remake the old AI

Post by Yexo » 23 Jun 2011 12:14

Doxygen doesn't support the squirrel syntax, but with some hacks you can work around that a bit. I had a doxygen file + sed script that could create usable (although far from perfect) documentation, but I've lost those :(.

Hephi
Engineer
Engineer
Posts: 69
Joined: 25 Oct 2009 09:56
Location: Belgium

Re: SimpleAI v6 - trying to remake the old AI

Post by Hephi » 29 Jul 2011 20:13

I'm a big fan of SimpleAI and I always enable it when playing myself and when playtesting my own AI.
With the UKRS train set I found Simple AI having problems attaching passenger carts to the HST engine,
it did not detect the failure when attaching and kept buying new passenger carts spending all it's money
on them and only deleting them after several years of negative profit.

I've had similar problems with engines that need a 'special' kind of wagon to be attached so I have my AI
run over all the wagons till it finds one that fits. (it does a bit more before and after as well though)

Code: Select all

	//source is the 'best' wagon found earlier in the code
		if(source != null) moved = AIVehicle.MoveWagon(source,0,new_veh,0);
		if(!moved)
		{	
			AIVehicle.SellVehicle(source);
			new_wagon = null;
			foreach(wagon, _ in RoutePlanner.GetWagonList())
			{
				source = AIVehicle.BuildVehicle(depot, wagon);
				if(source != null) moved = AIVehicle.MoveWagon(source,0,new_veh,0);
				if(moved)
				{
					new_wagon = wagon;
					break;
				} else
				{
					AIVehicle.SellVehicle(source);	
				}
			}	
		}
//you still need to catch the possibility of not finding any good ones after this code but this should rarely happen
Attachments
simple.png
not attaching carts
(133.16 KiB) Downloaded 1 time
--------------------------------------------------
MailAI, a casual postal service for openTTD.
--------------------------------------------------

User avatar
planetmaker
OpenTTD Developer
OpenTTD Developer
Posts: 9278
Joined: 07 Nov 2007 22:44
Location: Sol d

Re: SimpleAI v6 - trying to remake the old AI

Post by planetmaker » 29 Jul 2011 20:29

That seems to be a common problem which all train AIs need to solve somehow... maybe it's worth to implement this in form of a library or to add it to an existing one?

Brumi
President
President
Posts: 904
Joined: 18 Jul 2009 17:54

Re: SimpleAI v6 - trying to remake the old AI

Post by Brumi » 05 Aug 2011 15:10

Sorry for the late reply, I was away 8)

In fact, I've already started tinkering with SimpleAI earlier this summer to create better support for NewGRF trainsets, namely UKRS and NARS. I've changed the engine selection algorithm and I also had to change those parts when train length is considered.
I've also had this problem with multiple unit engines, so thanks for the solution :)
My other current problem is the 3x Freightliner container rake in UKRS, as AIVehicle::GetLength() only returns a third of its real length.

I cannot promise anything, but NewGRF trainset support is on the way :)

Hephi
Engineer
Engineer
Posts: 69
Joined: 25 Oct 2009 09:56
Location: Belgium

Re: SimpleAI v6 - trying to remake the old AI

Post by Hephi » 12 Aug 2011 17:08

And if you run the getLength() on the vehicle after attaching the wagon to the engine instead of just calling it on the wagon?

I use while (total_length < variable){this.AddWagon(veh); total_length = AIVehicle.GetLength(veh);}
--------------------------------------------------
MailAI, a casual postal service for openTTD.
--------------------------------------------------

Brumi
President
President
Posts: 904
Joined: 18 Jul 2009 17:54

Re: SimpleAI v6 - trying to remake the old AI

Post by Brumi » 13 Aug 2011 08:26

And what do you put into 'variable'? I think this would need selling the last wagon when the train gets too long. I'd rather avoid it, but if that's the only solution...

WaffleEggnog
Engineer
Engineer
Posts: 3
Joined: 03 Jan 2012 16:47

Re: SimpleAI v6 - trying to remake the old AI

Post by WaffleEggnog » 04 Jan 2012 21:28

Really nice AI. I like how there is nothing special about it, but i do have a suggestion, could you make it so that the AI knows when to stop building? In most of my games it builds itself to death and before long its in so much dept that it just dies. I could leave it in a map alone and it would lose.

I know you want to make this like the original AI, but just having the option would be nice, to make it more realistic and to make it more challenging. I wasint expecting it to be the most skilled AI, but its way too easy, and i don't want that to ruin it.

Brumi
President
President
Posts: 904
Joined: 18 Jul 2009 17:54

Re: SimpleAI v6 - trying to remake the old AI

Post by Brumi » 04 Jan 2012 23:02

Although it's true that my goal is to imitate the old AI, but going bankrupt 'like the old AI' is a bit too much :D
I have tried to test it with different settings and under harder conditions, it doesn't really go bankrupt in my tests. So could you provide me a savegame in which SimpleAI goes bankrupt?

Anyway, a temporary solution to your problem could be that you increase the waiting time between constructing two routes, it's there among the settings. There is also the 'Slowdown effect' setting, which makes this waiting interval increase over time, so the AI will become slower after some years.

annaiak
Engineer
Engineer
Posts: 5
Joined: 13 May 2012 08:04

Re: SimpleAI v6 - trying to remake the old AI

Post by annaiak » 13 May 2012 08:23

Hey,

Dunno where to post bugreport so I place it here and hopefully it finds its addresse:

Code: Select all

dbg: [script] [2] [S] Your script made an error: the index 'AI_ET_SUBSIDY_AWARDED' does not exist
dbg: [script] [2] [S] 
dbg: [script] [2] [S] *FUNCTION [CheckEvents()] simpleai-6/manager.nut line [35]
dbg: [script] [2] [S] *FUNCTION [Start()] simpleai-6/main.nut line [121]
dbg: [script] [2] [S] 
dbg: [script] [2] [S] [eventtype] 11
dbg: [script] [2] [S] [isloaded] false
dbg: [script] [2] [S] [loadedevent] ARRAY
dbg: [script] [2] [S] [event] INSTANCE
dbg: [script] [2] [S] [this] INSTANCE
dbg: [script] [2] [S] [this] INSTANCE
dbg: [script] The script died unexpectedly.
$ uname -a
Linux Khedron 3.2.0-2-amd64 #1 SMP Sun Apr 15 16:47:38 UTC 2012 x86_64 GNU/Linux

openttd 1.2.0

Brumi
President
President
Posts: 904
Joined: 18 Jul 2009 17:54

Re: SimpleAI v6 - trying to remake the old AI

Post by Brumi » 13 May 2012 10:52

Thanks for the report :)
This one is strange, I'm not able to reproduce it in OpenTTD 1.2.0, it seems to be some API compatibility bug, a similar bug has been mentioned in the DictatorAI thread. I'm still not sure whether it's my fault or something is broken in the compatibility scripts, I'll have a look at it.

By chance, do you have a file named compat_1.0.nut in the ai directory of your OpenTTD installation? If not, that might be the case.

EDIT: It turns out that krinn was using a constant which is not defined in the 1.0 API, but AI_ET_SUBSIDY_AWARDED is definitely part of the 1.0 API. I'm really puzzled ?(

Alberth
OpenTTD Developer
OpenTTD Developer
Posts: 4720
Joined: 09 Sep 2007 05:03
Location: home

Re: SimpleAI v6 - trying to remake the old AI

Post by Alberth » 13 May 2012 11:12

It's called a 'bug in OpenTTD' :)
Please report it to flyspray.

frosch
OpenTTD Developer
OpenTTD Developer
Posts: 979
Joined: 20 Dec 2006 13:31
Location: Aschaffenburg

Re: SimpleAI v6 - trying to remake the old AI

Post by frosch » 13 May 2012 13:45

annaiak wrote: $ uname -a
Linux Khedron 3.2.0-2-amd64 #1 SMP Sun Apr 15 16:47:38 UTC 2012 x86_64 GNU/Linux

openttd 1.2.0
How did you install OpenTTD? Did you use the debian-provided package? Is "openttd-data" installed? Do you have the file
/usr/share/games/openttd/ai/compat_1.0.nut ?
⢇⡸⢸⠢⡇⡇⢎⡁⢎⡱⢸⡱⢸⣭⠀⢸⢜⢸⢸⣀⢸⣀⢸⣭⢸⡱⠀⢰⠭⡆⣫⠰⣉⢸⢸⠀⢰⠭⡆⡯⡆⢹⠁⠀⢐⠰⡁

annaiak
Engineer
Engineer
Posts: 5
Joined: 13 May 2012 08:04

Re: SimpleAI v6 - trying to remake the old AI

Post by annaiak » 13 May 2012 19:52

Who/what is flyspray? :-) (Sorry didn't report bug in ottd yet so I dunno.)

Yep from the deb package:

Code: Select all

$ dpkg -l openttd*
||/ Name                                Version      
+++-===================================-=============
ii  openttd                             1.2.0-1      
ii  openttd-data                        1.2.0-1      
ii  openttd-opengfx                     0.4.4-1      
ii  openttd-openmsx                     0.3.1-2      
ii  openttd-opensfx                     0.2.3-3  


The file seems to be fine

Code: Select all

$ ls -l /usr/share/games/openttd/ai/compat_1.0.nut
-rw-r--r-- 1 root root 5134 Apr 18 15:56 /usr/share/games/openttd/ai/compat_1.0.nut

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

Re: SimpleAI v6 - trying to remake the old AI

Post by Zuu » 13 May 2012 21:38

what does it say if you try to list all compat_1.0.nut files on your system?

Code: Select all

locate compat_1.0.nut
It might be the case that youhave an old compat_1.0.nut somewhere at your system from OpenTTD pre 1.2 which your 1.2 installation finds and uses instead of the installed one.


You could also upload the compat_1.0.nut file here and we can take a look at it to ensure that it is not broken. The compat_1.0.nut that I got in my 1.2.0 installation does not show the problem that you have.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)

annaiak
Engineer
Engineer
Posts: 5
Joined: 13 May 2012 08:04

Re: SimpleAI v6 - trying to remake the old AI

Post by annaiak » 13 May 2012 22:25

That's it I guess!! :)
Not really sure how to verify, if the AI doesn't crash after deleting the file (the AI doesn't get to that event that often I guess). But I guess that the old file in my home is the cause of the issue.

Code: Select all

$ locate compat_1.0.nut
~/.openttd/ai/compat_1.0.nut
~/.openttd/ai/compat_1.0.nut.bak
/usr/share/games/openttd/ai/compat_1.0.nut

$ ls -l ~/.openttd/ai/compat_1.0.nut*
-rw-r--r-- 1 roman roman 2651 Sep 12  2011 ~/.openttd/ai/compat_1.0.nut
-rwxr-xr-x 1 roman roman  646 Aug  1  2010 ~/.openttd/ai/compat_1.0.nut.bak

$ ls -l /usr/share/games/openttd/ai/compat_1.0.nut
-rw-r--r-- 1 root root 5134 Apr 18 15:56 /usr/share/games/openttd/ai/compat_1.0.nut
Sorry for causing all that fuzz :oops:

Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 1 guest