public
Last active

Map extractor bookmarklet for Planet Wars Google AI Challenge

  • Download Gist
map_extractor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
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();
})();

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.