screeps/battle.js
2016-06-28 19:38:52 -04:00

55 lines
1.5 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// usernames are all lowercase, we compare with to lower
var USERNAME_WHITELIST = ['chrisinajar', 'ho0ber', 'fractaloop', 'n7-anthony', 'overra', 'tyrel', 'fervens'];
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) {
return creep.room.controller;
}
return creep.pos.findClosestByPath(targets);
}
function run (creep) {
var target = findEnemy(creep);
if (!target) {
target = findWall(creep);
// getDirectionTo
if (!creep.pos.isNearTo(target)) {
return creep.moveTo(target);
} else {
creep.attack(target);
}
var direction = creep.pos.getDirectionTo(target);
creep.memory.wallDirection = direction;
} else {
creep.moveTo(target);
creep.attack(target);
}
}
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 true;
}
});
return creep.pos.findClosestByPath(targets);
}