Skip to content

Instantly share code, notes, and snippets.

@OverlordQ
Created January 13, 2017 18:11
Show Gist options
  • Save OverlordQ/1d589cc54e0172906a3588b89d48398e to your computer and use it in GitHub Desktop.
Save OverlordQ/1d589cc54e0172906a3588b89d48398e to your computer and use it in GitHub Desktop.
var flee = require('flee');
function checkPath(pos1, pos2) {
var path = pos1.findPathTo(pos2);
if (!path.length) {
return false;
}
return path[path.length - 1].x == pos2.x && path[path.length - 1].y == pos2.y;
}
function costCallbackIgnoreRamparts(roomName, cm) {
var ramparts = Game.rooms[roomName].find(FIND_STRUCTURES, {
filter: i => i.structureType == STRUCTURE_RAMPART || i.structureType == STRUCTURE_WALL
});
ramparts.forEach(i => cm.set(i.pos.x, i.pos.y, 0));
}
module.exports = function(creep) {
if (!creep.getActiveBodyparts(ATTACK) && creep.getActiveBodyparts(RANGED_ATTACK) && flee(creep, 3)) {
return;
}
var target, healers = creep.room.find(FIND_MY_CREEPS, {
filter: function(i) {
return i.getActiveBodyparts('heal') > 0;
}
});
if (creep.hits < creep.hitsMax / 2 && healers.length > 0 && !creep.getActiveBodyparts(ATTACK)) {
target = creep.pos.findClosestByPath(FIND_MY_CREEPS, {
ignoreRoads: true,
filter: function(i) {
return i.getActiveBodyparts('heal') > 0;
}
});
if (!target || creep.moveTo(target, {
maxRooms: 1,
ignoreRoads: true
}) != OK) {
target = null;
}
}
var nearCreeps = creep.pos.findInRange(FIND_HOSTILE_CREEPS, 1, {
filter: function(i) {
return i.owner.username != 'Source Keeper'
}
});
if (nearCreeps) {
creep.attack(nearCreeps[0]);
}
if (!target) {
target = creep.pos.findClosestByPath(FIND_HOSTILE_CREEPS, {
ignoreRoads: true,
ignoreCreeps: true,
filter: function(i) {
return i.owner.username != 'Source Keeper'
}
});
if (target && (creep.getActiveBodyparts(ATTACK) || !creep.pos.inRangeTo(target, 3))) {
creep.moveTo(target, {
maxRooms: 1,
ignoreRoads: true,
ignoreCreeps: true
});
}
}
if (!target) {
target = creep.pos.findClosestByPath(FIND_HOSTILE_CREEPS, {
ignoreRoads: true,
filter: function(i) {
return i.owner.username != 'Source Keeper'
},
costCallback: costCallbackIgnoreRamparts
});
if (target && (creep.getActiveBodyparts(ATTACK) || !creep.pos.inRangeTo(target, 3))) {
creep.moveTo(target, {
maxRooms: 1,
ignoreRoads: true,
costCallback: costCallbackIgnoreRamparts
});
}
}
if (!target) {
target = creep.pos.findClosestByPath(FIND_HOSTILE_CREEPS, {
ignoreDestructibleStructures: true,
ignoreRoads: true,
filter: function(i) {
return i.owner.username != 'Source Keeper'
}
});
if (target && (creep.getActiveBodyparts(ATTACK) || !creep.pos.inRangeTo(target, 3))) {
creep.moveTo(target, {
ignoreDestructibleStructures: true,
maxRooms: 1,
ignoreRoads: true
});
}
}
if (!target) {
if (creep.room.controller && creep.room.controller.level > 0 && !creep.room.find(FIND_HOSTILE_CREEPS).length) {
var spawns = _.filter(creep.room.find(FIND_HOSTILE_SPAWNS), spawn => !checkPath(creep.pos, spawn.pos));
if (!spawns.length) {
creep.suicide();
return;
}
target = spawns[0];
if (target) {
creep.moveTo(target, {
ignoreDestructibleStructures: true,
maxRooms: 1,
ignoreRoads: true
});
}
}
return;
}
creep.attack(target);
if (creep.getActiveBodyparts(WORK) > 0 && creep.memory._move && creep.memory._move.path) {
var path = Room.deserializePath(creep.memory._move.path);
if (path.length && creep.pos.isNearTo(path[0].x, path[0].y)) {
var structures = creep.room.lookForAt('structure', path[0].x, path[0].y);
if (structures.length > 0) {
creep.dismantle(structures[0]);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment