2017-11-17 02:49:43 -05:00
|
|
|
if (process.argv.length < 3) {
|
2019-02-21 12:02:18 -05:00
|
|
|
console.log("Usage: node paper-io-bot.js <socket-url> [<name>]")
|
2019-01-15 10:42:15 -05:00
|
|
|
process.exit(1);
|
2017-11-17 02:49:43 -05:00
|
|
|
}
|
|
|
|
|
2019-02-21 20:36:35 -05:00
|
|
|
var core = require("./src/core");
|
|
|
|
var client = require("./src/game-client");
|
|
|
|
var consts = require("./config.json").consts;
|
2017-11-17 02:49:43 -05:00
|
|
|
|
2019-02-03 01:49:23 -05:00
|
|
|
var MOVES = [[-1, 0], [0, 1], [1, 0], [0, -1]];
|
2017-11-17 02:49:43 -05:00
|
|
|
|
|
|
|
var startFrame = -1;
|
|
|
|
var endFrame = -1;
|
|
|
|
var grid, others, user, playerPortion = {}, claim = [];
|
|
|
|
|
|
|
|
function mod(x) {
|
2019-01-15 10:42:15 -05:00
|
|
|
x %= 4;
|
|
|
|
if (x < 0) x += 4;
|
|
|
|
return x;
|
2017-11-17 02:49:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function connect() {
|
2019-02-21 20:36:35 -05:00
|
|
|
var prefixes = consts.PREFIXES.split(" ");
|
|
|
|
var names = consts.NAMES.split(" ");
|
2019-02-22 02:10:20 -05:00
|
|
|
var name = process.argv[3] || [prefixes[Math.floor(Math.random() * prefixes.length)], names[Math.floor(Math.random() * names.length)]].join(" ");
|
|
|
|
client.connectGame(process.argv[2], "[BOT] " + name, function(success, msg) {
|
2019-02-22 00:22:21 -05:00
|
|
|
if (!success) {
|
|
|
|
console.error(msg);
|
|
|
|
setTimeout(connect, 1000);
|
|
|
|
}
|
2019-01-15 10:42:15 -05:00
|
|
|
});
|
2017-11-17 02:49:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function Loc(row, col) {
|
2019-01-15 10:42:15 -05:00
|
|
|
if (this.constructor != Loc) return new Loc(row, col);
|
2017-11-17 02:49:43 -05:00
|
|
|
|
2019-01-15 10:42:15 -05:00
|
|
|
this.row = row;
|
|
|
|
this.col = col;
|
2017-11-17 02:49:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function update(frame) {
|
2019-01-15 23:15:38 -05:00
|
|
|
if (startFrame == -1) startFrame = frame;
|
2019-01-15 10:42:15 -05:00
|
|
|
endFrame = frame;
|
|
|
|
|
|
|
|
if (frame % 6 == (startFrame + 1) % 6) {
|
|
|
|
grid = client.grid;
|
|
|
|
others = client.getOthers();
|
|
|
|
|
|
|
|
//Note: the code below isn't really my own code. This code is in fact the
|
|
|
|
//approximate algorithm used by the paper.io game. It has been modified from
|
|
|
|
//the original code (i.e. deobfuscating) and made more efficient in some
|
|
|
|
//areas (and some tweaks), otherwise, the original logic is about the same.
|
|
|
|
var row = user.row, col = user.col, dir = user.currentHeading;
|
2019-02-21 12:39:14 -05:00
|
|
|
var thres = (.05 + .1 * Math.random()) * consts.GRID_COUNT * consts.GRID_COUNT;
|
2019-01-15 10:42:15 -05:00
|
|
|
|
2019-02-21 12:39:14 -05:00
|
|
|
if (row < 0 || col < 0 || row >= consts.GRID_COUNT || col >= consts.GRID_COUNT) return;
|
2019-01-15 10:42:15 -05:00
|
|
|
|
|
|
|
if (grid.get(row, col) === user) {
|
|
|
|
//When we are inside our territory
|
|
|
|
claim = [];
|
|
|
|
weights = [25, 25, 25, 25];
|
|
|
|
weights[dir] = 100;
|
|
|
|
weights[mod(dir + 2)] = -9999;
|
|
|
|
|
|
|
|
for (var nd = 0; nd < 4; nd++) {
|
|
|
|
for (var S = 1; S < 20; S++) {
|
|
|
|
var nr = MOVES[nd][0] * S + row;
|
|
|
|
var nc = MOVES[nd][1] * S + col;
|
|
|
|
|
2019-02-21 12:39:14 -05:00
|
|
|
if (nr < 0 || nc < 0 || nr >= consts.GRID_COUNT || nc >= consts.GRID_COUNT) {
|
2019-01-15 10:42:15 -05:00
|
|
|
if (S > 1) weights[nd]--;
|
|
|
|
else weights[nd] = -9999;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (grid.get(nr, nc) !== user) weights[nd]--;
|
|
|
|
|
|
|
|
var tailed = undefined;
|
|
|
|
for (var o of others) {
|
|
|
|
if (o.tail.hitsTail(new Loc(nr, nc))) {
|
|
|
|
tailed = o;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tailed) {
|
|
|
|
if (o.name.indexOf("PAPER") != -1) weights[nd] += 3 * (30 - S); //Don't really try to kill our own kind
|
|
|
|
else weights[nd] += 30 * (30 - S);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//View a selection of choices based on the weights we computed
|
|
|
|
var choices = [];
|
|
|
|
for (var d = 0; d < 4; d++) {
|
|
|
|
for (var S = 1; S < weights[d]; S++) {
|
|
|
|
choices.push(d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (choices.length === 0) choices.push(dir);
|
|
|
|
dir = choices[Math.floor(Math.random() * choices.length)];
|
|
|
|
}
|
|
|
|
else if (playerPortion[user.num] < thres) {
|
|
|
|
//Claim some land if we are relatively tiny and have little to risk.
|
|
|
|
if (claim.length === 0) {
|
|
|
|
var breadth = 4 * Math.random() + 2;
|
|
|
|
var length = 4 * Math.random() + 2;
|
|
|
|
var ccw = 2 * Math.floor(2 * Math.random()) - 1;
|
|
|
|
|
|
|
|
turns = [dir, mod(dir + ccw), mod(dir + ccw * 2), mod(dir + ccw * 3)];
|
|
|
|
lengths = [breadth, length, breadth + 2 * Math.random() + 1, length];
|
|
|
|
|
|
|
|
for (var i = 0; i < turns.length; i++) {
|
|
|
|
for (var j = 0; j < lengths[i]; j++) {
|
|
|
|
claim.push(turns[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (claim.length !== 0) dir = claim.shift();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
claim = [];
|
|
|
|
//We are playing a little bit more cautious when we are outside and have a
|
|
|
|
//lot of land
|
|
|
|
weights = [5, 5, 5, 5];
|
|
|
|
weights[dir] = 50;
|
|
|
|
weights[mod(dir + 2)] = -9999;
|
|
|
|
|
|
|
|
for (var nd = 0; nd < 4; nd++) {
|
|
|
|
for (var S = 1; S < 20; S++) {
|
|
|
|
var nr = MOVES[nd][0] * S + row;
|
|
|
|
var nc = MOVES[nd][1] * S + col;
|
|
|
|
|
2019-02-21 12:39:14 -05:00
|
|
|
if (nr < 0 || nc < 0 || nr >= consts.GRID_COUNT || nc >= consts.GRID_COUNT) {
|
2019-01-15 10:42:15 -05:00
|
|
|
if (S > 1) weights[nd]--;
|
|
|
|
else weights[nd] = -9999;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (user.tail.hitsTail(new Loc(nr, nc))) {
|
|
|
|
if (S > 1) weights[nd] -= 50 - S;
|
|
|
|
else weights[nd] = -9999;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (grid.get(nr, nc) === user) weights[nd] += 10 + S;
|
|
|
|
|
|
|
|
var tailed = undefined;
|
|
|
|
for (var o of others) {
|
|
|
|
if (o.tail.hitsTail(new Loc(nr, nc))) {
|
|
|
|
tailed = o;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tailed) {
|
|
|
|
if (o.name.indexOf("PAPER") != -1) weights[nd] += 3 * (30 - S); //Don't really try to kill our own kind
|
|
|
|
else weights[nd] += 30 * (30 - S);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//View a selection of choices based on the weights we computed
|
|
|
|
var choices = [];
|
|
|
|
for (var d = 0; d < 4; d++) {
|
|
|
|
for (var S = 1; S < weights[d]; S++) {
|
|
|
|
choices.push(d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (choices.length === 0) choices.push(dir);
|
|
|
|
dir = choices[Math.floor(Math.random() * choices.length)];
|
|
|
|
}
|
|
|
|
client.changeHeading(dir);
|
|
|
|
}
|
2017-11-17 02:49:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function calcFavorability(params) {
|
2019-01-15 10:42:15 -05:00
|
|
|
return params.portion + params.kills * 50 + params.survival / 100;
|
2017-11-17 02:49:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
client.allowAnimation = false;
|
|
|
|
client.renderer = {
|
2019-01-15 10:42:15 -05:00
|
|
|
addPlayer: function(player) {
|
|
|
|
playerPortion[player.num] = 0;
|
|
|
|
},
|
|
|
|
disconnect: function() {
|
|
|
|
var dt = (endFrame - startFrame);
|
|
|
|
startFrame = -1;
|
|
|
|
|
2019-02-22 00:22:21 -05:00
|
|
|
console.log(`[${new Date()}] I died... (survived for ${dt} frames.)`);
|
|
|
|
console.log(`[${new Date()}] I killed ${client.kills} player(s).`);
|
2019-02-21 11:19:33 -05:00
|
|
|
setTimeout(connect, 5000);
|
2019-01-15 10:42:15 -05:00
|
|
|
},
|
|
|
|
removePlayer: function(player) {
|
|
|
|
delete playerPortion[player.num];
|
|
|
|
},
|
|
|
|
setUser: function(u) {
|
|
|
|
user = u;
|
|
|
|
},
|
|
|
|
update: update,
|
|
|
|
updateGrid: function(row, col, before, after) {
|
|
|
|
before && playerPortion[before.num]--;
|
|
|
|
after && playerPortion[after.num]++;
|
|
|
|
}
|
2017-11-17 02:49:43 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
connect();
|