It is currently Fri May 24, 2013 10:06 am Advanced search

Game map extractor bookmarklet

Code won't compile? Found a bug? Post here!

Game map extractor bookmarklet

Postby iouri_ » Sat Sep 25, 2010 5:47 pm

Ever wanted to figure out why your bot did that silly thing in the replay on the contest website at move 34 (or any other move)? Now you can do the following:

- As you're watching the replay on the website (e.g. this http://ai-contest.com/visualizer.php?game_id=4738603), pause it just before the move in question.
- Copy and paste the code below into the website's URL bar (including the "javascript:" part in the begining; tested on Chrome and FF) :
Code: Select all
javascript: (function() {
   var povSelectorString = '<select id="me_povSelector" style="margin:10px;"></select>';
   var refreshButton = '<input id="me_refresh" type="Submit" value="Refresh" style="margin-left:50px;"/>';
   var wholeReplayCheckbox = '<input id="me_whole_replay" type="checkbox" style="margin: 10px 3px 15px 50px;"/> Whole Replay';
   var povContainerString = '<p>Point of view: ' + povSelectorString +
                        refreshButton + wholeReplayCheckbox +'</p>';
   var textAreaString = '<textarea id="me_mapState" cols="80" rows="30"' +
              ' style="margin-top: 10px;"></textarea>';
   var formString = '<form style="margin-top: 10px;">' + povContainerString +
               textAreaString + '</form>';
   
   var visualizerContainer = $("#visualizer");
   if (visualizerContainer.length == 0) {
      visualizerContainer = $("#main");
   }
   
   visualizerContainer.append(formString);
   var povSelector = $('#me_povSelector');
   var refreshButton = $("#me_refresh");
   var mapElement = $('#me_mapState');
   var wholeReplayCheckbox = $('#me_whole_replay');

   povSelector.append('<option value="1">' + Visualizer.players[0] + '</option>');
   povSelector.append('<option value="2">' + Visualizer.players[1] + '</option>');

   var mapElement = $('#me_mapState');

   function getMapState(pov) {
      var mapText = "";

      var planets = Visualizer.planets;
      var numPlanets = planets.length;

      for (var p = 0; p < numPlanets; p++) {
         var planet = planets[p];
         var actual_owner = planet.owner;
         var pov_owner = 0;

         if (actual_owner != 0) {
            pov_owner = (pov == actual_owner ? 1 : 2);
         }

         mapText += "P " + planet.x + " " + planet.y + " " + pov_owner +
               " " + planet.numShips + " " + planet.growthRate + "\n";
      }

      var frame = Math.floor(Visualizer.frame);
      var fleets = Visualizer.moves[frame].moving;

      var numFleets = fleets.length;
      var i = 0;

      for (var f = 0; f < numFleets; f++) {
         var fleet = fleets[f];
         var actual_owner = fleet.owner;
         var pov_owner = (pov == actual_owner ? 1 : 2);

         var sourceX = fleet.source.x;
         var sourceY = fleet.source.y;
         var destinationX = fleet.destination.x;
         var destinationY = fleet.destination.y;

         var source = -1;
         var destination = -1;
         var foundSource = false;
         var foundDestination = false;

         for (i = 0; i < numPlanets; i++) {
            var planet = planets[i];
            if (planet.x == sourceX && planet.y == sourceY) {
                source = i;

            } else if (planet.x == destinationX && planet.y == destinationY) {
                destination = i;
            }
         }

         mapText += "F " + pov_owner + " " + fleet.numShips + " " +
                 source + " " + destination + " " + fleet.tripLength +
                 " " + (fleet.tripLength - fleet.progress) + "\n";
      }

      mapText += "go\n\n";
      
      return mapText;
   }
   
   function getGameReplay(pov) {
      var upToTurn = Math.floor(Visualizer.frame);
      var wholeFrame = true;
      var replayText = "";
      
      for (var t = 0; t <= upToTurn; t++) {
         Visualizer.setFrame(t, wholeFrame);
         Visualizer.drawFrame(Visualizer.frame);
         replayText += getMapState(pov);
      }
      
      return replayText;
   }
   
   povSelector.change(function() {
      var pov = $(this).val();
      var outputText = '';
      
      if ($('#me_whole_replay:checked').length > 0) {
         outputText = getGameReplay(pov);
      
      } else {
         outputText = getMapState(pov);
      }
      
       mapElement.text(outputText);
   });

   refreshButton.click(function() {
       povSelector.change();
       return false;
   });

   wholeReplayCheckbox.change(function() {
       povSelector.change();
   });

   povSelector.change();
})();


- Scroll down to the bottom where the new textarea appears; there you will see the map data as (theoretically) accepted by game engine. The map is from point of view of the player in the drop-down box (i.e. that player is player 1 in the map data). You can change that by changing the player. Also, you can forward/reverse the game any number of steps and click "refresh" -- that should update the map data in the textarea.

- Start your bot in debug mode (without the game engine; just run it, a blank console screen should appear), and then paste the map directly as the bot's input, including the "go" at the end of the game state data.

- If you'd like your bot to replay the game all the way from the start to the turn in question, check the "Whole Replay" check box, copy everything in the text area, start your bot (by itself, without a game engine), and paste everything into it. It should then (hopefully) replay the game from start all the way to the turn of interest.

- Alternatively, copy the map data in the text area (minus the "go" at the end), create a new map file ("new_map.txt"), paste it there. Feed it to the game engine, or your favourite bot debugging tool (e.g. mine, but if you're gonna use it make sure to download a newer version here or it'll crash.). Debug your bot as usual.

- if you don't feel like copying the code above every time, you can create a bookmarklet by creating a browser bookmark with the code above instead of a URL. Then next time you're on the game replay page, you can just click on the bookmark.
Last edited by iouri_ on Fri Nov 26, 2010 3:42 pm, edited 6 times in total.
iouri_
Brigadier-General
 
Posts: 105
Joined: Thu Feb 11, 2010 4:16 pm
Location: Toronto, Canada

Re: Game map extractor bookmarklet

Postby DanielVonFange » Sat Sep 25, 2010 8:25 pm

Rock on!
DanielVonFange
Brigadier-General
 
Posts: 149
Joined: Wed Sep 08, 2010 1:15 pm

Re: Game map extractor bookmarklet

Postby hexist » Sun Sep 26, 2010 1:35 pm

Awesome :)
hexist
Cadet
 
Posts: 9
Joined: Sat Feb 06, 2010 5:14 am

Re: Game map extractor bookmarklet

Postby Torquemada » Tue Sep 28, 2010 12:26 am

Nice!
Torquemada
Major
 
Posts: 30
Joined: Tue Sep 21, 2010 12:39 pm

Re: Game map extractor bookmarklet

Postby RebelXT » Tue Sep 28, 2010 1:14 pm

Very useful! thanks

For anyone who wants to replay the whole game locally, there is also dmj's python script:

http://github.com/apinkin/planetwars-py ... ate_url.py
http://github.com/apinkin/planetwars-py ... /replay.sh
RebelXT
Colonel
 
Posts: 81
Joined: Fri Sep 10, 2010 2:05 pm

Re: Game map extractor bookmarklet

Postby Mistmanov » Tue Oct 19, 2010 1:27 pm

I could've sworn a similar tool for the tcp server has been posted somewhere on this forum.. but I just can't find it anymore. If it actually exists, would someone please point me in the right direction? :oops:
Mistmanov
Colonel
 
Posts: 70
Joined: Fri Sep 24, 2010 6:50 pm

Re: Game map extractor bookmarklet

Postby barabanus » Tue Oct 19, 2010 1:52 pm

Mistmanov wrote:I could've sworn a similar tool for the tcp server has been posted somewhere on this forum.. but I just can't find it anymore. If it actually exists, would someone please point me in the right direction? :oops:


There's no big need in that: you could log all tcp games locally.
barabanus
Lieutenant-Colonel
 
Posts: 44
Joined: Fri Sep 24, 2010 6:01 am

Re: Game map extractor bookmarklet

Postby iouri_ » Wed Nov 10, 2010 3:13 pm

Was reading the thread on the the new visualizer.js and remembered that even though I fixed the bookmarklet to work on the TCP server, I forgot to post it.

Anyway, I've updated the bookmarklet in the original post with the following changes:

- It now works on dhartmei's TCP server
- It also shows an option to append "go<enter>" at the end of the map, to make it easier to feed the game state into the bot's input directly.

Also, I've made it available on Gist: https://gist.github.com/670964
iouri_
Brigadier-General
 
Posts: 105
Joined: Thu Feb 11, 2010 4:16 pm
Location: Toronto, Canada

Re: Game map extractor bookmarklet

Postby iouri_ » Fri Nov 26, 2010 3:37 pm

Made a couple more changes to the game state bookmarklet:

- There is now an option to extract all game states from turn 1 up to and including the current turn. The extracted list of states can be then fed to the bot directly, and the bot (at least the C++ starter package one) should replay the game more or less exactly up to the turn in question.

- The bookmarklet by default appends "go" to the game map.

The updated bookmarklet is now available in the original post and on gist.
iouri_
Brigadier-General
 
Posts: 105
Joined: Thu Feb 11, 2010 4:16 pm
Location: Toronto, Canada


Return to Technical Issues

Who is online

Users browsing this forum: No registered users and 0 guests