Skip to content

Instantly share code, notes, and snippets.

@AlinaNova21
Created September 14, 2016 04:45
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlinaNova21/b9c9f8f16baca142be66cd8402a392a7 to your computer and use it in GitHub Desktop.
Save AlinaNova21/b9c9f8f16baca142be66cd8402a392a7 to your computer and use it in GitHub Desktop.
Screeps Invader Source Code
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, 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});
}
}
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]);
}
}
}
}
var rooms = require('rooms');
module.exports = function(creep, range) {
var nearCreeps = creep.pos.findInRange(FIND_HOSTILE_CREEPS, range-1, {filter: i => i.getActiveBodyparts(ATTACK) > 0 || i.getActiveBodyparts(RANGED_ATTACK) > 0});
if(nearCreeps.length > 0) {
var ret = PathFinder.search(creep.pos, _.map(nearCreeps, i => ({pos: i.pos, range: range})), { maxRooms: 1, flee: true, roomCallback(roomName) {
if(!rooms.rooms[roomName] || rooms.rooms[roomName].time < Game.time) {
rooms.rooms[roomName] = {
costMatrix: rooms.createCostMatrix(roomName),
time: Game.time
};
}
return rooms.rooms[roomName].costMatrix;
}});
if(ret.path.length) {
creep.moveTo(ret.path[0]);
creep.say('flee');
return true;
}
}
return false;
}
var flee = require('flee');
module.exports = function (creep) {
var target;
var healTargets = creep.pos.findInRange(FIND_MY_CREEPS, 3);
if(healTargets.length > 0) {
healTargets = healTargets.sort((a,b) => (b.hitsMax - b.hits) - (a.hitsMax - a.hits));
if (creep.pos.isNearTo(healTargets[0])) {
creep.heal(healTargets[0]);
} else {
creep.rangedHeal(healTargets[0]);
}
}
if (creep.hits < creep.hitsMax / 2) {
if (!flee(creep)) {
target = creep.pos.findClosestByPath(FIND_MY_CREEPS, {filter: i => i.getActiveBodyparts('heal') > 0});
if (target) {
creep.moveTo(target, {maxRooms: 1, ignoreRoads: true});
}
}
return;
}
target = creep.pos.findClosestByRange(FIND_MY_CREEPS, {filter: i => i.hits < i.hitsMax});
if (!target) {
if (flee(creep, 4)) {
return;
}
target = creep.pos.findClosestByRange(FIND_MY_CREEPS, {filter: i => i != creep && i.getActiveBodyparts(HEAL) == 0});
}
if (!target) {
creep.suicide();
return;
}
creep.moveTo(target, {maxRooms: 1, ignoreRoads: true, reusePath: 0});
if (creep.getActiveBodyparts(RANGED_ATTACK)) {
require('shootAtWill')(creep);
}
}
PathFinder.use(true);
var healer = require('healer'),
findAttack = require('findAttack');
for (var i in Game.creeps) {
var creep = Game.creeps[i];
if (!creep.room) { continue; }
if (creep.getActiveBodyparts('heal') > 0) {
healer(creep);
} else {
findAttack(Game.creeps[i]);
}
require('shootAtWill')(creep);
}
for (var i in Memory.creeps) {
if (!Game.creeps[i]) {
delete Memory.creeps[i];
}
}
module.exports = {
rooms: {},
createCostMatrix(roomName) {
var cm = new PathFinder.CostMatrix;
Game.rooms[roomName].find(FIND_CREEPS).forEach(i => cm.set(i.pos.x, i.pos.y, 255));
Game.rooms[roomName].find(FIND_STRUCTURES).forEach(i => {
if(i.structureType != STRUCTURE_ROAD && i.structureType != STRUCTURE_CONTAINER) {
cm.set(i.pos.x, i.pos.y, 255);
}
});
return cm;
}
}
module.exports = function (creep) {
if(!creep.getActiveBodyparts(RANGED_ATTACK)) {
return;
}
var targets = creep.pos.findInRange(FIND_HOSTILE_CREEPS, 3, { filter: function (i) { return i.owner.username != 'Source Keeper' } });
if (!targets.length) {
targets = creep.pos.findInRange(FIND_STRUCTURES, 3, { filter: function (i) { return i.structureType == STRUCTURE_RAMPART || i.structureType == STRUCTURE_WALL; } });
}
var min = -1, target;
for (var i in targets) {
if (min == -1 || min > targets[i].hits) {
target = targets[i];
min = targets[i].hits;
}
}
creep.rangedAttack(target);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment