Unique identifier for a game

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
aidos
Engineer
Engineer
Posts: 9
Joined: 14 May 2015 02:47

Unique identifier for a game

Post by aidos »

Is there something I can use that will uniquely identify a game? I am thinking of something like a GUID for each game instance?
aidos
Engineer
Engineer
Posts: 9
Joined: 14 May 2015 02:47

Re: Unique identifier for a game

Post by aidos »

Reading through the squirrel docs there are methods such as time() and getenv() that I could use to generate a /reasonably/ unique id, however I cant work out how to get access to the system functions.
User avatar
planetmaker
OpenTTD Developer
OpenTTD Developer
Posts: 9432
Joined: 07 Nov 2007 22:44
Location: Sol d

Re: Unique identifier for a game

Post by planetmaker »

aidos wrote:Is there something I can use that will uniquely identify a game? I am thinking of something like a GUID for each game instance?
Do you mean savegame or map? Then the answer is 'no'. But a GameScript must provide a unique identifier (GSInfo::GetShortName()), something like a name with exactly four letters.

Other than that... I don't quite get what kind of unique identifier you could mean or as to what you aim for. Maybe you can describe a bit more what you try to achieve?
You might try to check for certain map-specific settings, like the random seed, the map size, the climate and maybe some other variables. Checkout class GSGameSettings:
http://nogo.openttd.org/api/trunk/class ... tings.html
Cheers,
pm
frosch
OpenTTD Developer
OpenTTD Developer
Posts: 988
Joined: 20 Dec 2006 13:31
Location: Aschaffenburg

Re: Unique identifier for a game

Post by frosch »

If you have a generated map (no heightmap, and no scenario started from flat map), you can query the map generation seed:

Code: Select all

GSGameSettings.GetValue("game_creation.generation_seed")
⢇⡸⢸⠢⡇⡇⢎⡁⢎⡱⢸⡱⢸⣭⠀⢸⢜⢸⢸⣀⢸⣀⢸⣭⢸⡱⠀⢰⠭⡆⣫⠰⣉⢸⢸⠀⢰⠭⡆⡯⡆⢹⠁⠀⢐⠰⡁
aidos
Engineer
Engineer
Posts: 9
Joined: 14 May 2015 02:47

Re: Unique identifier for a game

Post by aidos »

I will try and explain what I am looking for.

When a new multi-player game of OpenTTD is started, there is some unique identifier (GUID etc.) that is generated that uniquely identifies that game from every other multi-player game of OpenTTD that is (or ever was) being played. This identifier should be generated at game start, and be saved/loaded so any time the game is ever played it can be identified as being the same game (perhaps the game is saved, and the server rebooted and the game loaded again).

I think I can make this identifier using a mixture of the hostname of the server, and the start time of the game - adding the code in my gamescript to save/load this information. To make this happen though I need access to the getenv() and time() functions from the system package (which I can't seem to access).
Alberth
OpenTTD Developer
OpenTTD Developer
Posts: 4763
Joined: 09 Sep 2007 05:03
Location: home

Re: Unique identifier for a game

Post by Alberth »

I already understood what a unique ID is, the more interesting question is how is a uid relevant to a game script, as one script instance is always associated with one game instance.
In other words, what's the use-case for a uid in a game script?

As for making one, indeed squirrel is quite limited. The proper solution to me would be to add such an ID in C++ to the game, and make the value available through an additional GameScript API function.
The TODO list at the wiki has a usecase for such uids too.

It may be worthwhile to have a look at a proper uuid library, if it's portable, it may be a better solution than a cook-your-own-pseudo-uid.
Being a retired OpenTTD developer does not mean I know what I am doing.
aidos
Engineer
Engineer
Posts: 9
Joined: 14 May 2015 02:47

Re: Unique identifier for a game

Post by aidos »

Sorry, I should have specified what I was trying to do :-/.

My use case is that I am extracting data from the game using the gamescript API and storing the data in a database. Rather than clearing the database between runs, I would like to be able to store the data from lots of different games in the database so I can process many different game instances.

Thanks for the suggestion, I will look at making the change - I agree with not rolling my own UUID implementation, the hostname/starttime idea was me trying to find a way to do it in a gamescript that just used the Squirrel "system" functions (getenv() and time() - a combination of which should be enough for my purposes) rather than having to change the source code for the game.

Thanks a lot for your advice.
krinn
Transport Coordinator
Transport Coordinator
Posts: 339
Joined: 29 Dec 2010 19:36

Re: Unique identifier for a game

Post by krinn »

aidos
Engineer
Engineer
Posts: 9
Joined: 14 May 2015 02:47

Re: Unique identifier for a game

Post by aidos »

The system time is good - but I was hoping that I could get a little more identifying information. Perhaps I'll combine it with the randomisation seed for the map as well.
Alberth
OpenTTD Developer
OpenTTD Developer
Posts: 4763
Joined: 09 Sep 2007 05:03
Location: home

Re: Unique identifier for a game

Post by Alberth »

Depending on your definition of 'same game', a md5 hash of all settings might work too.
I think it would be a nice addition for OpenTTD.
Being a retired OpenTTD developer does not mean I know what I am doing.
frosch
OpenTTD Developer
OpenTTD Developer
Posts: 988
Joined: 20 Dec 2006 13:31
Location: Aschaffenburg

Re: Unique identifier for a game

Post by frosch »

Whatever you do, it comes down to the question: Does a scenario have a single unique id, or does starting the same scenario multiple times result in different ids?
⢇⡸⢸⠢⡇⡇⢎⡁⢎⡱⢸⡱⢸⣭⠀⢸⢜⢸⢸⣀⢸⣀⢸⣭⢸⡱⠀⢰⠭⡆⣫⠰⣉⢸⢸⠀⢰⠭⡆⡯⡆⢹⠁⠀⢐⠰⡁
aidos
Engineer
Engineer
Posts: 9
Joined: 14 May 2015 02:47

Re: Unique identifier for a game

Post by aidos »

Sounds like there are two different requirements here:

1. Some unique identifier (same settings will still generate a new id)
2. Settings hash (same settings will generate the same hash)

Interesting, I hadn't really considered the second scenario.
Alberth
OpenTTD Developer
OpenTTD Developer
Posts: 4763
Joined: 09 Sep 2007 05:03
Location: home

Re: Unique identifier for a game

Post by Alberth »

The notion of "equal" has many different forms, and depending on what you want to achieve, you need a different one.
As such, it's not two requirements, it's two solutions for the same requirement. (And there are many more solutions, for example, a settings hash doesn't take the newgrf list or newgrf parameters into account.)

However, I think Frosch asked the right question to be answered first, is a scenario a savegame or something else?
Being a retired OpenTTD developer does not mean I know what I am doing.
aidos
Engineer
Engineer
Posts: 9
Joined: 14 May 2015 02:47

Re: Unique identifier for a game

Post by aidos »

.
Last edited by aidos on 07 Jun 2015 14:27, edited 1 time in total.
aidos
Engineer
Engineer
Posts: 9
Joined: 14 May 2015 02:47

Re: Unique identifier for a game

Post by aidos »

For me - with the id generated at the start of the game, if you load a save of that game the id should still be the same, it's about the "instance" of the world that is being used.
Alberth
OpenTTD Developer
OpenTTD Developer
Posts: 4763
Joined: 09 Sep 2007 05:03
Location: home

Re: Unique identifier for a game

Post by Alberth »

Sure, but when does a game start when you use a scenario?
(with "play scenario" or with "create scenario"?).

I am aware there isn't much to play when you press "create scenario", but if you pick that, "play scenario" will just copy the ID generated with "screate scenario", and the game you "play scenario" now will be the same game as the one you play next year.


Edit: FYI, there isn't much point in making completely blank messages, the usual form is to add "Edit: <more text>" lines at the end of the message, like I do here.
That way people can read both versions, and see what you changed or emphasized.
Being a retired OpenTTD developer does not mean I know what I am doing.
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 6 guests