Skip to content

Instantly share code, notes, and snippets.

@Denis101
Last active November 30, 2016 00:24
Show Gist options
  • Save Denis101/4ce18d2d2fe350e27f39bd36bb002015 to your computer and use it in GitHub Desktop.
Save Denis101/4ce18d2d2fe350e27f39bd36bb002015 to your computer and use it in GitHub Desktop.
machinegame.com bots
function getClosest(pos, items) {
if (items.length <= 0) {
return null;
}
var posMag = Math.sqrt(Math.pow(Math.abs(pos.x), 2) + Math.pow(Math.abs(pos.y), 2));
var smallestMag = -1;
var smallest = null;
for (var i = 0; i < items.length; ++i) {
var item = items[i];
if (item.x == pos.x && item.y == pos.y) {
return item;
}
var itemMag = Math.sqrt(Math.pow(Math.abs(item.x), 2) + Math.pow(Math.abs(item.y), 2));
var diff = Math.abs(posMag - itemMag);
if (smallestMag < 0 || diff < smallestMag) {
smallestMag = diff;
smallest = item;
}
}
return smallest;
}
function moveRandom(bot) {
var moveX = (5 - (Math.floor(Math.random() * 10) + 1));
var moveY = (5 - (Math.floor(Math.random() * 10) + 1));
bot.moveTo({
x: bot.x + moveX,
y: bot.y + moveY
});
}
function samePos(a, b) {
return a.x == b.x && a.y == b.y;
}
function withinRange(a, b, range = 1) {
var xDiff = Math.abs(a.x) - Math.abs(b.x);
var yDiff = Math.abs(a.y) - Math.abs(b.y);
return xDiff <= range && yDiff <= range;
}
function mutate(percentage) {
var value = Math.floor(Math.random() * 100) + 1;
return value <= percentage;
}
function castleBeyondThreshold(bots, castle, threshold) {
var total = 0;
for (var i = 0; i < bots.length; ++i) {
if (total > threshold) {
return true;
}
if (samePos(bots[i], castle)) {
total++;
}
}
return false;
}
function play(state){
for (var i = 0; i < state.bots.length; ++i) {
var bot = state.bots[i];
if (state.others.length > 0) {
if (mutate(10)) {
moveRandom(bot);
continue;
}
var closestEnemy = getClosest(bot, state.others);
if (withinRange(bot, closestEnemy)) {
bot.attack(closestEnemy);
continue;
} else {
bot.moveTo(closestEnemy);
continue;
}
// otherwise run
}
if (state.castles.length > 0 && state.bots.length > 127) {
for (var j = 0; j < state.castles.length; ++j) {
var castle = state.castles[j];
if (!castleBeyondThreshold(state.bots, castle, 5)) {
if (samePos(bot, castle)) {
continue;
}
bot.moveTo(castle);
}
}
}
if (mutate(5)) {
moveRandom(bot);
continue;
}
if (bot.wrenches > 1 && bot.wrenches % 2 == 1) {
bot.build();
continue;
}
if (state.bots.length == 256 || bot.wrenches >= 5) {
moveRandom(bot);
continue;
}
var closest = getClosest(bot, state.wrenches);
if (!closest) {
if (!state.wrenches[0]) {
moveRandom(bot);
continue;
}
if (samePos(bot, state.wrenches[0])) {
bot.collect();
} else {
bot.moveTo(state.wrenches[0]);
}
continue;
}
if (samePos(bot, closest)) {
bot.collect();
} else {
bot.moveTo(closest);
}
}
}
function getClosest(pos, items) {
if (items.length <= 0) {
return null;
}
var posMag = Math.sqrt(Math.pow(Math.abs(pos.x), 2) + Math.pow(Math.abs(pos.y), 2));
var smallestMag = -1;
var smallest = null;
for (var i = 0; i < items.length; ++i) {
var item = items[i];
if (item.x == pos.x && item.y == pos.y) {
return item;
}
var itemMag = Math.sqrt(Math.pow(Math.abs(item.x), 2) + Math.pow(Math.abs(item.y), 2));
var diff = Math.abs(posMag - itemMag);
if (smallestMag < 0 || diff < smallestMag) {
smallestMag = diff;
smallest = item;
}
}
return smallest;
}
function moveRandom(bot) {
var moveX = (5 - (Math.floor(Math.random() * 10) + 1));
var moveY = (5 - (Math.floor(Math.random() * 10) + 1));
bot.moveTo({
x: bot.x + moveX,
y: bot.y + moveY
});
}
function samePos(a, b) {
return a.x == b.x && a.y == b.y;
}
function withinRange(a, b, range = 1) {
var xDiff = Math.abs(a.x) - Math.abs(b.x);
var yDiff = Math.abs(a.y) - Math.abs(b.y);
return xDiff <= range && yDiff <= range;
}
function mutate(percentage) {
var value = Math.floor(Math.random() * 100) + 1;
return value <= percentage;
}
function castleBeyondThreshold(bots, castle, threshold) {
var total = 0;
for (var i = 0; i < bots.length; ++i) {
if (total > threshold) {
return true;
}
if (samePos(bots[i], castle)) {
total++;
}
}
return false;
}
var globalCastles = [];
function globalContains(castle) {
if (globalCastles.length <= 0) {
return false;
}
for (var i = 0; i < globalCastles.length; ++i) {
var g = globalCastles[i];
if (g.x == castle.x && g.y == castle.y) {
return true;
}
}
return false;
}
function play(state){
for (var i = 0; i < state.bots.length; ++i) {
var bot = state.bots[i];
if (state.others.length > 0) {
if (mutate(10)) {
moveRandom(bot);
continue;
}
var closestEnemy = getClosest(bot, state.others);
if (withinRange(bot, closestEnemy)) {
bot.attack(closestEnemy);
continue;
} else {
bot.moveTo(closestEnemy);
continue;
}
// otherwise run
}
if (bot.wrenches >= 5 && state.castles.length > 0 && state.bots.length > 127) {
for (var j = 0; j < state.castles.length; ++j) {
var castle = state.castles[j];
if (!globalContains(castle)) {
globalCastles.push(castle);
}
}
for (var j = 0; j < globalCastles.length; ++j) {
var castle = globalCastles[j];
if (!castleBeyondThreshold(state.bots, castle, 5)) {
if (samePos(bot, castle)) {
continue;
}
bot.moveTo(castle);
}
}
}
if (mutate(5)) {
moveRandom(bot);
continue;
}
if (bot.wrenches > 1 && bot.wrenches % 2 == 1) {
bot.build();
continue;
}
if (state.bots.length == 256 || bot.wrenches >= 5) {
moveRandom(bot);
continue;
}
var closest = getClosest(bot, state.wrenches);
if (!closest) {
if (!state.wrenches[0]) {
moveRandom(bot);
continue;
}
if (samePos(bot, state.wrenches[0])) {
bot.collect();
} else {
bot.moveTo(state.wrenches[0]);
}
continue;
}
if (samePos(bot, closest)) {
bot.collect();
} else {
bot.moveTo(closest);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment