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 »

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.
gajop
Moderator
Posts: 3051
Joined: 05 Aug 2009, 20:42

Re: AI spectate or watch replays?

Post by gajop »

Extracting replay data is probably easier to do in Lua.
Just serialize the gamestate (WIP library) to JSON and save it in a file.
User avatar
Silentwings
Posts: 3720
Joined: 25 Oct 2008, 00:23

Re: AI spectate or watch replays?

Post by Silentwings »

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
Posts: 169
Joined: 08 Jul 2015, 20:20

Re: AI spectate or watch replays?

Post by 8611z »

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.
User avatar
PauloMorfeo
Posts: 2004
Joined: 15 Dec 2004, 20:53

Re: AI spectate or watch replays?

Post by PauloMorfeo »

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)?
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7049
Joined: 16 Nov 2004, 13:08

Re: AI spectate or watch replays?

Post by zwzsg »

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.
User avatar
Silentwings
Posts: 3720
Joined: 25 Oct 2008, 00:23

Re: AI spectate or watch replays?

Post by Silentwings »

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.
User avatar
PauloMorfeo
Posts: 2004
Joined: 15 Dec 2004, 20:53

Re: AI spectate or watch replays?

Post by PauloMorfeo »

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...
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7049
Joined: 16 Nov 2004, 13:08

Re: AI spectate or watch replays?

Post by zwzsg »

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.
gajop
Moderator
Posts: 3051
Joined: 05 Aug 2009, 20:42

Re: AI spectate or watch replays?

Post by gajop »

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)
8611z
Posts: 169
Joined: 08 Jul 2015, 20:20

Re: AI spectate or watch replays?

Post by 8611z »

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.
User avatar
PauloMorfeo
Posts: 2004
Joined: 15 Dec 2004, 20:53

Re: AI spectate or watch replays?

Post by PauloMorfeo »

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.
User avatar
PauloMorfeo
Posts: 2004
Joined: 15 Dec 2004, 20:53

Re: AI spectate or watch replays?

Post by PauloMorfeo »

: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.
User avatar
PepeAmpere
Posts: 589
Joined: 03 Jun 2010, 01:28

Re: AI spectate or watch replays?

Post by PepeAmpere »

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)
User avatar
Jools
XTA Developer
Posts: 2816
Joined: 23 Feb 2009, 16:29

Re: AI spectate or watch replays?

Post by Jools »

What were you doing in those lakes? Playing with ducks?
Post Reply

Return to “AI”