Files
screeps/ClassCreep.js

205 lines
5.4 KiB
JavaScript

// Replace creep class with _creep class
module.exports = {
setup: function () { Creep.prototype = _Creep.prototype; }
}
class _Creep extends Creep {
init() {
// if no role is assigned become harvester
if (this.memory.role == undefined) this.memory.role = ROLE_HARVESTER;
// start by gathering new energy
if (this.memory.gathering == undefined) this.memory.gathering = true;
this.memory.init = true;
}
//#region Target detection
findTarget(TARGET) {
if (this.memory.target == undefined) {
const target = this.pos.findClosestByPath(TARGET);
if (target == null) return ERR_NOT_FOUND;
else this.memory.target = target.id;
}
return OK;
}
findTarget(TARGET, filter) {
if (this.memory.target == undefined) {
const target = this.pos.findClosestByPath(TARGET, filter);
if (target == null) return ERR_NOT_FOUND;
else this.memory.target = target.id;
}
return OK;
}
findBestTarget(TARGET, sort) {
if (this.memory.target == undefined) {
const targets = this.room.find(TARGET);
const target = targets.sort(sort).shift();
if (target == null) return ERR_NOT_FOUND;
else this.memory.target = target.id;
}
return OK;
}
//#endregion
//#region Gathering
harvestSource() {
// search for source with highest energy level.
if (this.findBestTarget(FIND_SOURCES, function (s1, s2) { return s2.energy - s1.energy }) == ERR_NOT_FOUND) return;
const target = Game.getObjectById(this.memory.target);
if (this.moveToTarget() == OK) {
this.harvest(target);
this.toggleChargeMode();
}
}
//#endregion
//#region Roles
harvesterJobs() {
if (this.memory.counter == undefined) this.memory.counter = 0;
switch (this.memory.counter % 4) {
case 0: this.supplyStructures();
break;
case 1: this.upgradeRoom();
break;
case 2: this.repairStructures();
break;
case 3: this.buildConstructionSites();
break;
default:
this.memory.counter++;
break;
}
if (this.memory.target == undefined) this.memory.counter++;
}
supplyStructures() {
if (this.findTarget(FIND_MY_STRUCTURES, {
filter: (s) => {
return (s.structureType == STRUCTURE_SPAWN ||
s.structureType == STRUCTURE_EXTENSION ||
s.structureType == STRUCTURE_TOWER ||
s.structureType == STRUCTURE_STORAGE)
&& 0 < s.store.getFreeCapacity();
}
}) == ERR_NOT_FOUND) return;
const target = Game.getObjectById(this.memory.target);
if (target.store.getFreeCapacity(RESOURCE_ENERGY) == 0) {
this.memory.target = undefined;
this.supplyStructures();
return;
}
if (this.moveToTarget() == OK) {
this.transfer(target, RESOURCE_ENERGY);
this.toggleChargeMode();
}
}
upgradeRoom() {
this.memory.target = this.room.controller.id;
if (this.moveToTarget() == OK) {
if (this.upgradeController(this.room.controller) == OK) return;
this.toggleChargeMode();
}
}
repairStructures() {
this.findTarget(FIND_MY_STRUCTURES, {
filter: (s) => {
return s.hits < s.hitsMax &&
s.structureType != STRUCTURE_WALL &&
s.structureType != STRUCTURE_RAMPART;
}
});
if (this.findTarget(FIND_MY_STRUCTURES, {
filter: (s) => {
return s.hits < s.hitsMax;
}
}) == ERR_NOT_FOUND) return;
const target = Game.getObjectById(this.memory.target);
if (target.hits == target.hitsMax) {
this.memory.target = undefined;
this.repairStructures();
return;
}
if (this.moveToTarget() == OK) {
if (this.repair(target) == OK) return;
this.toggleChargeMode();
}
}
buildConstructionSites() {
if (this.findTarget(FIND_MY_CONSTRUCTION_SITES) == ERR_NOT_FOUND) return;
const target = Game.getObjectById(this.memory.target);
if (target == null) {
this.memory.target = undefined;
this.buildConstructionSites();
return;
}
if (this.moveToTarget() == OK) {
if (this.build(target) == OK) return;
this.toggleChargeMode();
}
}
//#endregion
//#region State
toggleChargeMode() {
if (this.store.getFreeCapacity(RESOURCE_ENERGY) == 0) {
this.memory.gathering = false;
} else if (this.store.getUsedCapacity(RESOURCE_ENERGY) == 0) {
this.memory.gathering = true;
} else return;
this.memory.target = undefined;
this.memory.path = undefined;
}
//#endregion
//#region Movement
moveToTarget() {
// Memory allocation
if (this.memory.move == undefined) this.memory.move = {};
// Get target to walk to.
const target = Game.getObjectById(this.memory.target)
if (target == null) return ERR_INVALID_TARGET;
// Check if arrived at target.
if (this.pos.inRangeTo(target.pos.x, target.pos.y, 1)) {
this.memory.move.path = undefined;
return OK;
}
// Check if in locked position every 5 ticks
if (this.memory.move.last_pos == undefined) this.memory.move.last_pos = this.pos;
if (this.ticksToLive % 5 == 0) {
if (this.memory.move.last_pos.x == this.pos.x &&
this.memory.move.last_pos.y == this.pos.y) {
this.moveTo(target);
this.memory.move.path = undefined;
return ERR_BUSY;
}
this.memory.move.last_pos = this.pos;
}
// Check if a new path is required.
if (this.memory.move.path == undefined) {
this.memory.move.path = this.pos.findPathTo(target.pos, {
ignoreCreeps: true, range: 1
});
}
// Perform move
const response = this.moveByPath(this.memory.move.path);
if (response == OK) return ERR_BUSY;
// If error occurs remove path and use moveTo to move.
if (response == ERR_INVALID_ARGS || response == ERR_NOT_FOUND) {
this.moveTo(target);
this.memory.move.path = undefined;
return ERR_BUSY;
}
}
//#endregion
}