Skip to content

Instantly share code, notes, and snippets.

@AlinaNova21
Created September 14, 2016 04:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlinaNova21/fb7486ec38947a9f68a12ddeb4642e5e to your computer and use it in GitHub Desktop.
Save AlinaNova21/fb7486ec38947a9f68a12ddeb4642e5e to your computer and use it in GitHub Desktop.
Screeps Source Keeper Source
PathFinder.use(true);
for (var i in Game.creeps) {
var creep = Game.creeps[i], source = undefined;
if (!creep.room) {
continue;
}
if (creep.memory.sourceId) {
source = Game.getObjectById(creep.memory.sourceId);
}
if(!source) {
source = creep.pos.findInRange(FIND_SOURCES, 5)[0] || creep.pos.findInRange(FIND_MINERALS, 5)[0];
if (source) {
creep.memory.sourceId = source.id;
}
}
if (source) {
if (!creep.pos.isNearTo(source)) {
if (creep.moveTo(source, {reusePath: 50}) == ERR_NO_PATH) {
delete creep.memory._move;
creep.moveTo(source, {reusePath: 50, ignoreDestructibleStructures: true});
}
}
}
var enemies = creep.pos.findInRange(FIND_HOSTILE_CREEPS, 1, { filter: function (i) { return i.owner.username != 'Invader' } });
if (enemies.length) {
enemies.sort(function (a, b) {
return a.hits - b.hits;
});
creep.attack(enemies[0]);
}
var enemies = creep.pos.findInRange(FIND_HOSTILE_CREEPS, 3, { filter: function (i) { return i.owner.username != 'Invader' } });
if (enemies.length) {
var massDmg = 0, distanceDmg = {1: 10, 2: 4, 3: 1};
for (var i in enemies) {
var distance = Math.max(Math.abs(enemies[i].pos.x - creep.pos.x), Math.abs(enemies[i].pos.y - creep.pos.y));
massDmg += distanceDmg[distance];
}
if (massDmg > 13) {
creep.rangedMassAttack();
} else {
enemies.sort(function (a, b) { return a.hits - b.hits; });
creep.rangedAttack(enemies[0]);
}
}
for (var i in Memory.creeps) {
if (!Game.creeps[i]) {
delete Memory.creeps[i];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment