156 lines
4.0 KiB
JavaScript
156 lines
4.0 KiB
JavaScript
module.exports = {
|
|
setup: function () { Creep.prototype = _Creep.prototype; }
|
|
}
|
|
|
|
class _Creep extends Creep {
|
|
init() {
|
|
if (this.memory.role == undefined) this.memory.role = ROLE_HARVESTER;
|
|
if (this.memory.recharge == undefined) this.memory.recharge = true;
|
|
}
|
|
|
|
toggleChargeMode() {
|
|
if (this.store.getFreeCapacity(RESOURCE_ENERGY) == 0) {
|
|
this.memory.recharge = false;
|
|
} else if (this.store.getUsedCapacity(RESOURCE_ENERGY) == 0) {
|
|
this.memory.recharge = true;
|
|
} else return;
|
|
this.memory.target = undefined;
|
|
this.memory.path = undefined;
|
|
}
|
|
|
|
findTarget(target) {
|
|
if (this.memory.target == undefined) {
|
|
this.memory.target = this.pos.findClosestByPath(target);
|
|
if (this.memory.target == null) {
|
|
this.memory.target = undefined;
|
|
return ERR_NOT_FOUND;
|
|
}
|
|
}
|
|
return OK;
|
|
}
|
|
|
|
findTarget(target, filter) {
|
|
if (this.memory.target == undefined) {
|
|
this.memory.target = this.pos.findClosestByPath(target, filter);
|
|
if (this.memory.target == null) {
|
|
this.memory.target = undefined;
|
|
return ERR_NOT_FOUND;
|
|
}
|
|
}
|
|
return OK;
|
|
}
|
|
|
|
harvestSource() {
|
|
if (this.findTarget(FIND_SOURCES) == ERR_NOT_FOUND) return;
|
|
const target = Game.getObjectById(this.memory.target.id);
|
|
if (this.moveToTarget() == OK) {
|
|
this.harvest(target);
|
|
this.toggleChargeMode();
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
}) == ERR_NOT_FOUND) return;
|
|
const target = Game.getObjectById(this.memory.target.id);
|
|
if (this.moveToTarget() == OK) {
|
|
this.transfer(target, RESOURCE_ENERGY);
|
|
this.toggleChargeMode();
|
|
}
|
|
}
|
|
|
|
upgradeRoom() {
|
|
if (this.findTarget(FIND_MY_STRUCTURES, {
|
|
filter: (s) => {
|
|
return s.structureType == STRUCTURE_CONTROLLER;
|
|
}
|
|
}) == ERR_NOT_FOUND) return;
|
|
const target = Game.getObjectById(this.memory.target.id);
|
|
if (this.moveToTarget() == OK) {
|
|
this.upgradeController();
|
|
this.toggleChargeMode();
|
|
}
|
|
}
|
|
|
|
repairStructures() {
|
|
if (this.findTarget(FIND_MY_STRUCTURES, {
|
|
filter: (s) => {
|
|
return s.hits < s.hitsMax &&
|
|
s.structureType != STRUCTURE_WALL &&
|
|
s.structureType != STRUCTURE_RAMPART;
|
|
}
|
|
}) == ERR_NOT_FOUND);
|
|
if (this.findTarget(FIND_MY_STRUCTURES, {
|
|
filter: (s) => {
|
|
return s.hits < s.hitsMax;
|
|
}
|
|
}) == ERR_NOT_FOUND) return;
|
|
|
|
const target = Game.getObjectById(this.memory.target.id);
|
|
if (this.moveToTarget() == OK) {
|
|
this.repair(target);
|
|
this.toggleChargeMode();
|
|
}
|
|
}
|
|
|
|
buildConstructionSites() {
|
|
if (this.findTarget(FIND_MY_CONSTRUCTION_SITES) == ERR_NOT_FOUND) return;
|
|
const target = Game.getObjectById(this.memory.target.id);
|
|
if (this.moveToTarget() == OK) {
|
|
this.build(target);
|
|
this.toggleChargeMode();
|
|
}
|
|
}
|
|
|
|
// Moves to the target.
|
|
moveToTarget() {
|
|
if (this.pos.inRangeTo(this.memory.target.pos.x, this.memory.target.pos.y, 1)) {
|
|
this.memory.path = undefined;
|
|
return OK;
|
|
}
|
|
if (this.memory.path == undefined) {
|
|
var path = this.pos.findPathTo(this.memory.target.pos, {
|
|
ignoreCreeps: true, range: 1
|
|
});
|
|
path.reverse();
|
|
this.memory.path = path;
|
|
}
|
|
const step = this.memory.path.pop();
|
|
var response = this.move(step.direction);
|
|
if (response == OK) {
|
|
if (this.pos.x == step.x &&
|
|
this.pos.y == step.y) return ERR_BUSY;
|
|
response = this.moveTo(this.memory.target.pos.x, this.memory.target.pos.y);
|
|
}
|
|
if (response == OK || response == ERR_INVALID_ARGS || response == ERR_NOT_FOUND) {
|
|
this.memory.path = undefined;
|
|
return ERR_BUSY;
|
|
}
|
|
this.memory.path.push(step);
|
|
}
|
|
}
|