Skip to content

Instantly share code, notes, and snippets.

@wshearn
Created July 17, 2016 09:04
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 wshearn/62ad4a227aa5f03928c4a594b68ca902 to your computer and use it in GitHub Desktop.
Save wshearn/62ad4a227aa5f03928c4a594b68ca902 to your computer and use it in GitHub Desktop.
var _super = require("./roles")
class Upgrader extends _super {
setupBody() {
// Creeps body parts http://support.screeps.com/hc/en-us/articles/208333929-StructureSpawn#canCreateCreep
this._body = [WORK, CARRY, MOVE];
this._memory = {role: 'upgrader'};
this._idleSpot = {x: 40, y: 40};
this._min = 2;
}
run(creep) {
if (_super.shouldSuicide(creep)) {
creep.suicide();
}
if(creep.memory.upgrading && creep.carry.energy == 0) {
creep.memory.upgrading = false;
}
if(!creep.memory.upgrading && creep.carry.energy == creep.carryCapacity) {
creep.memory.upgrading = true;
}
if(creep.memory.upgrading) {
if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
creep.moveTo(creep.room.controller);
}
}
else {
// Try withdrawing from structures first
var targets = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_EXTENSION ||
structure.structureType == STRUCTURE_SPAWN ||
structure.structureType == STRUCTURE_CONTAINER ||
structure.structureType == STRUCTURE_TOWER ) &&
structure.energy > 0;
}
});
if (targets.length > 0) {
if (creep.withdraw(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(targets[0]);
}
} else {
var sources = creep.room.find(FIND_SOURCES);
if (creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
creep.moveTo(sources[0]);
}
}
//if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
// creep.moveTo(sources[0]);
//}
}
}
}
module.exports = new Upgrader();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment