Skip to content

Instantly share code, notes, and snippets.

@Ratstail91
Created July 3, 2019 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ratstail91/b7b7490277bf0e1187b5cfb1241540d1 to your computer and use it in GitHub Desktop.
Save Ratstail91/b7b7490277bf0e1187b5cfb1241540d1 to your computer and use it in GitHub Desktop.
const { roleLength } = require('utils');
const ROLE_NAME = 'scout';
function spawn(origin, max, roleName, type = 'small') {
if (roleLength(Game.creeps, roleName, origin) >= max) {
return;
}
//determine the size to use
let body;
switch(type) {
case 'small':
body = [MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, ATTACK, ATTACK, TOUGH, TOUGH, TOUGH, TOUGH];
break;
case 'tough':
body = [
//800 total
MOVE, TOUGH, //60
MOVE, TOUGH, //60
MOVE, TOUGH, //60
MOVE, TOUGH, //60
MOVE, TOUGH, //60
MOVE, TOUGH, //60
MOVE, TOUGH, //60
MOVE, TOUGH, //60
MOVE, TOUGH, //60
ATTACK, MOVE, //130
ATTACK, MOVE, //130
];
break;
default:
throw new Error(`Unknown spawn type ${type}`);
}
return Game.spawns[origin].spawnCreep(
body,
roleName + Game.time,
{ memory: { role: roleName, origin: origin }}
);
}
function run(creep) {
if (Game.flags['rallypointoverride']) {
creep.moveTo(Game.flags['rallypointoverride'], { visualizePathStyle: {}});
return;
}
const targets = findHostileTargets(creep);
const isObject = (a) => (!!a) && (a.constructor === Object);
//exclude unreachable items from targets (cached because CPU)
if (!isObject(creep.memory.exclude)) {
creep.memory.exclude = {};
}
while(targets.length && creep.memory.exclude[targets[0].id]) {
targets.shift(); //TODO: should use a map
}
if (targets.length == 0 || (creep.room.controller && creep.room.controller.safeMode)) {
if (Game.flags['rallypoint']) {
creep.moveTo(Game.flags['rallypoint'], { visualizePathStyle: {}});
} else {
creep.moveTo(Game.flags['home'], { visualizePathStyle: {}});
}
} else {
//for each potential target (limit exclusion checks to 100 targets for CPU's sake)
for (let i = 0; i < 100; i++) {
//if following targets are excluded
if (creep.memory.exclude[targets[0].id]) { //NOTE: bug here?
targets.shift();
continue;
}
//check for ramparts (because attacks against ramparts return OK)
const ramparts = findRampartsAt(creep, targets[0].pos);
if (ramparts.length > 0) {
creep.memory.exclude[targets[0].id] = true;
targets.shift();
continue;
}
//can't attack, can't reach, exclude it
if (creep.attack(targets[0]) != OK) {
//paths connect?
const pathResult = creep.room.findPath(creep.pos, targets[0].pos);
const pathEndPos = creep.room.getPositionAt(pathResult[pathResult.length-1].x, pathResult[pathResult.length-1].y);
if (!pathEndPos.isEqualTo(targets[0].pos)) {
creep.memory.exclude[targets[0].id] = true;
targets.shift();
continue;
}
//move to the target, or exclude it
const moveResult = creep.moveTo(targets[0], { visualizePathStyle: {stroke: '#ff0000'}});
if (moveResult == OK || moveResult == ERR_TIRED) {
break;
} else if (moveResult == ERR_NO_PATH) {
creep.memory.exclude[targets[0].id] = true;
targets.shift();
} else {
throw new Error(`Unknown moveResult: ${moveResult}`);
}
}
}
}
}
function findHostileTargets(creep) {
const towers = creep.room.find(FIND_HOSTILE_STRUCTURES, {filter: (structure) => structure.structureType == STRUCTURE_TOWER});
const creeps = creep.room.find(FIND_HOSTILE_CREEPS);
const structures = creep.room.find(FIND_HOSTILE_STRUCTURES , {filter: (structure) => structure.structureType != STRUCTURE_RAMPART });
const spawns = creep.room.find(FIND_HOSTILE_SPAWNS);
return [...towers, ...spawns, ...structures, ...creeps];
}
function findRampartsAt(creep, pos) {
return creep.room.find(FIND_HOSTILE_STRUCTURES , {filter: (structure) => structure.structureType == STRUCTURE_RAMPART && structure.pos.isEqualTo(pos) });
}
module.exports = {
ROLE_NAME: ROLE_NAME,
spawn: (origin, max, type) => spawn(origin, max, ROLE_NAME, type),
run: run
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment