Skip to content

Instantly share code, notes, and snippets.

@chrisinajar
Last active July 3, 2016 02:18
Show Gist options
  • Save chrisinajar/1fbe4ecfd3f503f96227db2433765599 to your computer and use it in GitHub Desktop.
Save chrisinajar/1fbe4ecfd3f503f96227db2433765599 to your computer and use it in GitHub Desktop.
Battle whitelist for screeps
var USERNAME_WHITELIST = ['chrisinajar', 'ho0ber', 'fractaloop', 'n7-anthony', 'overra', 'tyrel', 'fervens', 'devdaniel'];
module.exports = {
findEnemy: findEnemy,
run: run
};
/*
var target = findEnemy(creep);
*/
function findEnemy (creep) {
var targets = creep.room.find(FIND_HOSTILE_CREEPS, {
filter: (c) => {
return (USERNAME_WHITELIST.indexOf(c.owner.username.toLowerCase()) === -1);
}
});
if (!targets.length) {
targets = creep.room.find(FIND_STRUCTURES, {
filter: function(object) {
if (object.my) {
return false;
}
if (object.structureType !== STRUCTURE_TOWER && object.structureType !== STRUCTURE_SPAWN && object.structureType !== STRUCTURE_EXTENSION) {
return false;
}
return object.owner
? (USERNAME_WHITELIST.indexOf(object.owner.username.toLowerCase()) === -1)
: true;
}
});
}
return creep.pos.findClosestByPath(targets);
}
function run (creep) {
var target = findEnemy(creep);
if (!target) {
target = findWall(creep);
if (!target) {
return false;
}
// getDirectionTo
if (!creep.pos.isNearTo(target)) {
return creep.moveTo(target);
} else {
creep.attack(target);
}
var direction = creep.pos.getDirectionTo(target);
creep.memory.wallDirection = direction;
return true;
} else {
creep.moveTo(target);
creep.attack(target);
return true;
}
return false;
}
function findWall (creep) {
var targets = creep.room.find(FIND_STRUCTURES, {
filter: function(object) {
if (object.my) {
return false;
}
if (object.structureType !== STRUCTURE_TOWER && object.structureType !== STRUCTURE_WALL) {
return false;
}
return object.owner
? (USERNAME_WHITELIST.indexOf(object.owner.username.toLowerCase()) === -1)
: false;
}
});
return creep.pos.findClosestByPath(targets);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment