Bean Counter game script?

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
leifbk
Chairman
Chairman
Posts: 810
Joined: 23 Dec 2013 16:33
Location: Bærum, Norway

Bean Counter game script?

Post by leifbk »

I sometimes run long experiments, and try to keep track of finances. See for instance viewtopic.php?f=29&t=75052&start=20#p1173635

I've just started to look at game scripts, and at the moment I'm just wondering if it's possible to write one that extracts the financial status at the start of a new year and output it to a file, similar to my hand-written pradtown.txt? Manually recording these figures at 1st January each year is sheer drudgery, and sometimes I'd just like to keep the game on fast forward.

I've been a casual programmer since the 1980s, and I've just had my first look at the "Minimal GS". I'd like to give it a try, but I may need some advice :)
PropH
Engineer
Engineer
Posts: 45
Joined: 10 Jun 2016 08:45
Location: Russia

Re: Bean Counter game script?

Post by PropH »

No, you can't [Yes, you can make such GS.]
Documentation:
1. NoGo - GSCompany contains information how to get values(balance, loan, expenses, etc.), also GSDate would be useful
2. Squirell - to print the data into the file
Also, it seems you can open any GS, that you can download on BaNaNaS, etc.
UPD: Maybe i missed something, but i really thought that Squirell was fully included. Anyway, you can make it as patch. And it should be easier
Last edited by PropH on 24 Jul 2016 11:55, edited 2 times in total.
Sorry for my "great" English
Alberth
OpenTTD Developer
OpenTTD Developer
Posts: 4763
Joined: 09 Sep 2007 05:03
Location: home

Re: Bean Counter game script?

Post by Alberth »

Writing a game script to record the information is quite doable.
Writing it to something external is quite not doable.

For safety reasons, most of the standard Squirrel libraries has been stripped out. You can't open an external file.
Listing it in a game script window, and taking a screen shot would work.
Being a retired OpenTTD developer does not mean I know what I am doing.
leifbk
Chairman
Chairman
Posts: 810
Joined: 23 Dec 2013 16:33
Location: Bærum, Norway

Re: Bean Counter game script?

Post by leifbk »

Thanks to both PropH and Alberth for your replies. There are of course other methods than a game script. Provided that I auto-save the game at the end of each year, which I currently do, it should be possible to access the save file from eg. an external Perl script, which I know how to write, and get the figures out. That way, I only need a clear documentation of the save file format. Where do I find that?
PropH
Engineer
Engineer
Posts: 45
Joined: 10 Jun 2016 08:45
Location: Russia

Re: Bean Counter game script?

Post by PropH »

If you know C++, you can try to modify CompaniesYearlyLoop().
I wish that's cheaper than deal with savegames. Anyway, it would be better to wait for Alberth's answer, because i could be wrong, as it was with my first answer
Sorry for my "great" English
leifbk
Chairman
Chairman
Posts: 810
Joined: 23 Dec 2013 16:33
Location: Bærum, Norway

Re: Bean Counter game script?

Post by leifbk »

PropH wrote:If you know C++, you can try to modify CompaniesYearlyLoop().
I wish that's cheaper than deal with savegames. Anyway, it would be better to wait for Alberth's answer, because i could be wrong, as it was with my first answer
Thanks, but I'd rather eat oatmeal with toe nail clippings than hacking my way through a C++ file. I've actually worked through Kernighan & Ritchie's classic "The C Programming Language" and once started with Moo & Koenig's "Accelerated C++" but I just don't get the hang of it. In my eyes, OOP is a horrible tangle of spaghetti code.
PropH
Engineer
Engineer
Posts: 45
Joined: 10 Jun 2016 08:45
Location: Russia

Re: Bean Counter game script?

Post by PropH »

By the way, i can help you. I didn't learn all aspects of C++, but it's easy, or at least it seems to be.
UPD: i mean, it's easy to solve current problem, not to learn program language
Last edited by PropH on 24 Jul 2016 16:59, edited 1 time in total.
Sorry for my "great" English
leifbk
Chairman
Chairman
Posts: 810
Joined: 23 Dec 2013 16:33
Location: Bærum, Norway

Re: Bean Counter game script?

Post by leifbk »

I found this and this after some googling. Now I'm gonna try and unpack a savegame and scrutinise it with a hex editor and try to make some sense of it.
Alberth
OpenTTD Developer
OpenTTD Developer
Posts: 4763
Joined: 09 Sep 2007 05:03
Location: home

Re: Bean Counter game script?

Post by Alberth »

Hex-editor won't do, if you expect some value at a constant offset.
OpenTTD compresses its savegames, and uses chunks of varying size to store its data (there is a varying number of vehicles, industries, stations, companies etc in the game, fixed offsets make little sense). Also, save game format varies through time, what works today may break tomorrow.

Simplest "hex editor" is OpenTTD itself, it knows how to load its own data :p
Being a retired OpenTTD developer does not mean I know what I am doing.
leifbk
Chairman
Chairman
Posts: 810
Joined: 23 Dec 2013 16:33
Location: Bærum, Norway

Re: Bean Counter game script?

Post by leifbk »

So it would appear that the "open" in OpenTTD doesn't extend to the savegame format. I have been working with proprietary database formats that's been a lot easier to unravel.

I don't intend this as a criticism of the project; I understand that (as I can understand why) you have simply continued working with a legacy format, created at a time when computer resources were very limited.
Alberth
OpenTTD Developer
OpenTTD Developer
Posts: 4763
Joined: 09 Sep 2007 05:03
Location: home

Re: Bean Counter game script?

Post by Alberth »

leifbk wrote:So it would appear that the "open" in OpenTTD doesn't extend to the savegame format.
Oh, it's open alright. Compression is some external library that gets linked, and the source code defines the chunks completely (everything built around tables of fields from structures that should be saved or loaded, for every version of save games). There is no external documentation, mostly for the practical reason that it's always obsolete.

Changing the savegame format is just a matter of changing one or more tables.

It does mean however in practice that you have to program a reader to load the file. Take the tables from OpenTTD, and implement the code that uses the tables to load the data. You typically want to store the read data too, so you need storage structures too. If you want to load older save games, there is also code that converts old save game formats to the current version, so it makes sense in todays context.

This is of course exactly what OpenTTD is doing already. For this reason, if you intend to read savegame data, it's always simpler to just take OpenTTD, and hook your code into it. A reader is a non-trivial piece of software,why implement it if a perfect working implementation is readily available in source form?


Note that data formats like this exist at other places too. A .gif, .jpg, or a .png is not a simple linear array of pixel colours. You need to write a decoder, a reader to make sense of the data, and expand it to pixels with colours.

leifbk wrote:I don't intend this as a criticism of the project; I understand that (as I can understand why) you have simply continued working with a legacy format, created at a time when computer resources were very limited.
I wasn't there at the time, but the old legacy format is much more crappy than what we have now, as far as I know. There is also code that tries to guess what version old file we actually get (not sure this is for original game, more likely it is for TTDPatch files, that have partial support afaik).
Being a retired OpenTTD developer does not mean I know what I am doing.
leifbk
Chairman
Chairman
Posts: 810
Joined: 23 Dec 2013 16:33
Location: Bærum, Norway

Re: Bean Counter game script?

Post by leifbk »

Alberth wrote:Note that data formats like this exist at other places too. A .gif, .jpg, or a .png is not a simple linear array of pixel colours. You need to write a decoder, a reader to make sense of the data, and expand it to pixels with colours.
I'm perfectly aware of that. I'm also perfectly aware of how to find documentation of eg. the .png format that enables me to implement a decoder in any program language that I may consider fit for the purpose. As far as I am concerned, C++ is not, or will ever be, one of those languages.
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: Bean Counter game script?

Post by Zuu »

A GS can write to the log. If you increase the log level for script source to iirc 5, then you get messages from GS and AIs included.

On Linux you just direct stderr or stdout to a file. Then parse that.

Another option is to make GS send messages to the admin port client that you write and connect to your multiplayer server. Have a look on ServerGS for some help on this either to write a custom GS oruse ServerGS as your GS.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
zooks
Transport Coordinator
Transport Coordinator
Posts: 262
Joined: 29 Jun 2006 08:36

Re: Bean Counter game script?

Post by zooks »

Zuu wrote:A GS can write to the log. If you increase the log level for script source to iirc 5, then you get messages from GS and AIs included.
I was thinking this too. Then remove all the lines from the log that are not relevant with an external program (or automate it with e.g. python), and you have the finance report file you're after
leifbk
Chairman
Chairman
Posts: 810
Joined: 23 Dec 2013 16:33
Location: Bærum, Norway

Re: Bean Counter game script?

Post by leifbk »

Zuu wrote:A GS can write to the log. If you increase the log level for script source to iirc 5, then you get messages from GS and AIs included.

On Linux you just direct stderr or stdout to a file. Then parse that.
I'm a happy Gentoo Linux user and have been so since abt. 2003. Thanks for the hint, maybe I'm gonna grok this after all. It should be rather trivial to parse the log with a Perl script; that's one of the things that I've actually done for a living. Now I'm a retiree and have too much time on my hands.
Alberth
OpenTTD Developer
OpenTTD Developer
Posts: 4763
Joined: 09 Sep 2007 05:03
Location: home

Re: Bean Counter game script?

Post by Alberth »

Good point Zuu, didn't think of that
Being a retired OpenTTD developer does not mean I know what I am doing.
lcd_47
Engineer
Engineer
Posts: 78
Joined: 27 Sep 2006 18:04

Re: Bean Counter game script?

Post by lcd_47 »

Here's a quick and dirty savegame unpacker written in Perl. This should be good enough for savegames created with recent versions of OpenTTD, but of course it won't work with the old formats. You'll probably need the IO::Uncompress::UnLzma and IO::Uncompress::RawInflate modules.

The result should be a (hopefully) still valid savegame.
leifbk
Chairman
Chairman
Posts: 810
Joined: 23 Dec 2013 16:33
Location: Bærum, Norway

Re: Bean Counter game script?

Post by leifbk »

lcd_47 wrote:Here's a quick and dirty savegame unpacker written in Perl. This should be good enough for savegames created with recent versions of OpenTTD, but of course it won't work with the old formats. You'll probably need the IO::Uncompress::UnLzma and IO::Uncompress::RawInflate modules.

The result should be a (hopefully) still valid savegame.
Nice :) I got it working and am browsing the data in Bless.
lcd_47
Engineer
Engineer
Posts: 78
Joined: 27 Sep 2006 18:04

Re: Bean Counter game script?

Post by lcd_47 »

This just pushes the problem a little further, you still need to decode the RIFF chunks. You probably need to either adapt the program posted by @frosch here, or extend File::Format::RIFF. You might get away with just finding and decoding PLYR chunks, but you'll need to look at the sources for the exact format anyway.
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 7 guests