AI spectate or watch replays?

AI spectate or watch replays?

Here is where ideas can be collected for the skirmish AI in development

Moderators: hoijui, Moderators

Post Reply
User avatar
PauloMorfeo
Posts: 2004
Joined: 15 Dec 2004, 20:53

AI spectate or watch replays?

Post by PauloMorfeo » 17 Jan 2016, 14:01

Is there any way to have an AI spectate games? Either spectating running games or, preferably, watching a replay?

Basically, what I want is to "watch" games in a way that allows code that can extract and dump to files what's going on in the game. Ex:
- Minute 1: Player A 50m, 800e, 8.8m income, 55e income, Comm at (x;y), AK at (x;y); AK at (x;y), etc; Player B ...
- Minute 2: ...

The intent of that is to collect data on which I can then apply learning algorithms, similar to Fil's overall concept in his thread:
viewtopic.php?f=15&t=34042
Except he's (I assume) relying on logging data of what's happening in the games his AI is playing.

I'd prefer to aim at realistic human-like behaviour as far as possible - so preferably watching humans play instead of watching AIs. Also, machine learning needs a lot of data examples to learn from so, ideally, I'd like to be able to watch the plentiful replays of BA.
0 x

gajop
Moderator
Posts: 3022
Joined: 05 Aug 2009, 20:42

Re: AI spectate or watch replays?

Post by gajop » 17 Jan 2016, 14:11

Extracting replay data is probably easier to do in Lua.
Just serialize the gamestate (WIP library) to JSON and save it in a file.
0 x

User avatar
Silentwings
Moderator
Posts: 3580
Joined: 25 Oct 2008, 00:23

Re: AI spectate or watch replays?

Post by Silentwings » 19 Jan 2016, 01:41

Write a cute little python script that looks in your replay folder, manufactures a script.txt that will load the replay and add your AI as a spectator, then starts spring with that script, then after it exits repeat with next replay?
0 x

8611z
Posts: 169
Joined: 08 Jul 2015, 20:20

Re: AI spectate or watch replays?

Post by 8611z » 19 Jan 2016, 08:48

From https://github.com/spring/spring/blob/1 ... Format.txt I think unlike players AIs can not be added as spectators or in replays because
// A skirmish AI (controlls a team)
[AI0]
misses the entries
Spectator=0/1
IsFromDemo=0/1
of
// A player (controlls a team, player 0 is the host only if IsHost is not set)
[PLAYER0]
Is there any way to have an AI spectate games? Either spectating running games or, preferably, watching a replay?
You could set startposition of your AI's team in some remote corner and comment out its do-things functions. If modifying the mod is an option than you can disabling spawning units for your AI.

Basically, what I want is to "watch" games in a way that allows code that can extract and dump to files what's going on in the game. Ex:
- Minute 1: Player A 50m, 800e, 8.8m income, 55e income, Comm at (x;y), AK at (x;y); AK at (x;y), etc; Player B ...
- Minute 2: ...
The eco data is in spring already, you see it as graph at end of game.
https://springfiles.com/spring/tools/sp ... ats-viewer is a phython script that extracts it. it is broken.
0 x

User avatar
PauloMorfeo
Posts: 2004
Joined: 15 Dec 2004, 20:53

Re: AI spectate or watch replays?

Post by PauloMorfeo » 20 Jan 2016, 22:26

Thanks for all the answers.
gajop wrote:Extracting replay data is probably easier to do in Lua.
Just serialize the gamestate (WIP library) to JSON and save it in a file.
Since I've seen your code I started looking into the whole thing Lua Sync/Async Gadgets and Widgets. However, I .. erm, spend more time spectating Spring games than actually doing anything useful :mrgreen: so please pardon any stupid question.

The code you've linked me to contains both a "Widget" and a "Gadget", right? From the context of this I assume that the "Gadget" will also work during replays - is that assumption correct?

Since the Gadget is attaching itself to the global game simulation, if I spectate a running game instead of a replay will anything abnormal happen even if all it's doing is querying game state? (ex: going out of sync | fail to join | the Gadget not working)

A "Widget" seems safer since it will run locally on my presentation only, thus never affecting the sync. Is there any drawback to using a Widget (ex: no access to some the game state)?
0 x

User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7014
Joined: 16 Nov 2004, 13:08

Re: AI spectate or watch replays?

Post by zwzsg » 20 Jan 2016, 22:49

I don't think you can add a gadget to a mod and still play an existing replay that was recorded without. As you said, the gadget is part of the game simulation, they count as things that potentially affect the syncedness, and there's a checksum to prevent mods with modified gadgets to play replays recorded without. Then maybe there's way around that with a springignore.txt

And yes, widgets have less power, though as a full spec, I'm not sure exactly what would be missing.
According to https://springrts.com/wiki/Category:Lua (where LuaUI means Widgets and LuaRules Gadgets), not much is missing.
I thought some callins were missing, but then I looked at https://springrts.com/wiki/Lua:Callins and saw that UnitDestroyed are the like are shared.
0 x

User avatar
Silentwings
Moderator
Posts: 3580
Joined: 25 Oct 2008, 00:23

Re: AI spectate or watch replays?

Post by Silentwings » 20 Jan 2016, 22:51

There used to be more difference between what callins were available where - they were bought closer to parity fairly recently. Some shared callins have the obvious sorts of (broadly effective) masks to prevent LuaUI getting hold of info that the player can't see themselves.
0 x

User avatar
PauloMorfeo
Posts: 2004
Joined: 15 Dec 2004, 20:53

Re: AI spectate or watch replays?

Post by PauloMorfeo » 20 Jan 2016, 22:53

8611z wrote:... The eco data is in spring already, you see it as graph at end of game.
https://springfiles.com/spring/tools/sp ... ats-viewer is a phython script that extracts it. it is broken.
I had a look at the python script and it appears it is extracting some meta-data (pre-calculated data) that has been recorded in the replay file (ex: units produced, units killed). I had a look at one of those replay files and they're pretty much inscrutable. How did you achieve that script - is there anywhere info on the format of the replay files or do you just know/dig through the source code to know what's going on in the replay file?

Fetching data from the replay file directly would be the most efficient way to extract the data I want. However, since I don't know the format of the data in the replay file, I don't even know whether that's possible. Do you know how Spring logs the data into the replay file?
#1 - Every frame / second / whatever, take a snapshot of the entire game state
#2 - Keep logging all the actions and events taken.

My bet is the 2nd, making (almost) none of the actual game state exist in the file requiring the game state to be re-constructed by a re-execution of the simulation...
0 x

User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7014
Joined: 16 Nov 2004, 13:08

Re: AI spectate or watch replays?

Post by zwzsg » 21 Jan 2016, 01:02

It's 2. Only the orders player give are transmitted online and stored in replays.

So, no you cannot parse a replay to extract the data you want.
0 x

gajop
Moderator
Posts: 3022
Joined: 05 Aug 2009, 20:42

Re: AI spectate or watch replays?

Post by gajop » 21 Jan 2016, 07:57

Simply use the widget for replays.
s11n is just an API, you will still have to invoke it to save stuff
LuaUI (widget) callins are mostly the same as LuaRules (gadget), although it may depend on your widget handler (it usually doesn't)
0 x

8611z
Posts: 169
Joined: 08 Jul 2015, 20:20

Re: AI spectate or watch replays?

Post by 8611z » 21 Jan 2016, 08:05

The phython was not written by me.
none of the actual game state exist in the file requiring the game state to be re-constructed by a re-execution of the simulation...
Yes.
I wrote "eco data" for a reson ;) What replays contain:
-startscript (teams, players, names, map,game,...)
-the statistics that spring gathers and which you can see at end of a game (if the game does not show custom stuff instead. http://i.imgur.com/FbyZO4D.png)
-chat
-lua messages. At the moment the only real-world use I know is games sending lua-messages in certain format, which the replay-site then interprets to show custom awards. ("Player x built most defenses")
-game result / winners

That is probally all not very useful..more for sake of completness because runnings replays is slow.
In theory the games could add Lua that logs more statistics into the replay, but downside would be larger replays :/

Imo logging with widget is best way.
To write all position of all units into file every second is few lines, something like:

Code: Select all

widget:gameframe(frame)
  if frame%30 == 0 then
    for _,id in ipairs(Spring.GetAllUnits()) do
      x,y,z = Spring.GetUnitPosition(id)
      file-write (frame, id, x,y,z)
    end
 end
end
If you want more info like unit's health then simply add the relevant function like Spring.GetUnitHealth() etc
There should be several such widgets flying around in forum or you can make another, to your needs.
My tip would for the log-files would be first think how you want to read them.
If you use a Lua-widget it might be tempting to also save the logs as Lua-tables. But that is only useful if you read them back with Lua. Otherwise all the ({][]{}]}[ will be annoying and something simple like csv is maybe better, especially if you maybe want to put the data into Excel or gnuplot.
When I tried stats-stuff I made mistake to invent my own log-format and log-file-reader which kinda worked http://abload.de/img/tabula-v2_xta_6530784c3k4g.png but was silly and not compatible with anything.
IIRC adding gadgets to game and then running replays should still work as long the gadget does not alter anything. But it is more fiddling and imo not nessecary here.
0 x

User avatar
PauloMorfeo
Posts: 2004
Joined: 15 Dec 2004, 20:53

Re: AI spectate or watch replays?

Post by PauloMorfeo » 22 Jan 2016, 23:43

Silentwings wrote:Write a cute little python script that looks in your replay folder, manufactures a script.txt that will load the replay and add your AI as a spectator, then starts spring with that script, then after it exits repeat with next replay?
8611z wrote:... I think unlike players AIs can not be added as spectators or in replays because ...
Doesn't appear to work indeed.

I managed to inject a new player into a replay and it starts the game and spawns the commander. It does get into a desync state, warning constantly that the frames are not validating against the checksums found in the replays, which could possibly be fine.

However, the injected AI doesn't run. In hindsight, this is pretty obvious - the game simulation won't re-execute the AI because it might not be deterministic or not even exist at all in the replayer. Therefore, only the commands are replayed as for the regular users and the "AIs" are nothing more than titles - there's no AI running.

Well, at least I had some fun trying, even if it was an obvious fail right from the start had I thought it properly.
0 x

User avatar
PauloMorfeo
Posts: 2004
Joined: 15 Dec 2004, 20:53

Re: AI spectate or watch replays?

Post by PauloMorfeo » 22 Jan 2016, 23:51

:shock: That's FREAKING cool... The middle graph, at least, which I assume shows the comm path - and death :-) - throughout the game.

Why is that not part of any mods? I'd love to see the quircky and sometimes whimsical paths of the commanders over the length of a game.

And thinking about it, I reckon it'd also be quite cool to see a final graph showing where everything has died (one mini-explosion plotted per each kill, showing off the locations of most intense fighting). Maybe unit paths like the bottom graph appears to be. Maybe also buildings, too.

But the commanders' paths and ultimate demise, definitely, that'd kick ass.
0 x

User avatar
PepeAmpere
Posts: 586
Joined: 03 Jun 2010, 01:28

Re: AI spectate or watch replays?

Post by PepeAmpere » 07 Feb 2016, 01:14

You do not need "AI" player to extract data from game, just Spectator who run assist widget who saves the data for you. There was/is notAtv which was doing the thing with unit positions viewtopic.php?f=55&t=28808 and there were other attempts from other spring people around.

I think the old notAtv widget is still somewhere on the springfiles or other repo, just the SVG it generates is no longer valid for current browsers (fixing is matter of few mins, but once tv is not in new lobby yet, it has no priority for us)
0 x

User avatar
Jools
XTA Developer
Posts: 2804
Joined: 23 Feb 2009, 16:29

Re: AI spectate or watch replays?

Post by Jools » 07 Feb 2016, 03:20

What were you doing in those lakes? Playing with ducks?
0 x

Post Reply

Return to “AI”