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) { 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; } harvestSource() { if (this.findTarget(FIND_SOURCES, { filter: (s) => { const area = s.room.lookAtArea(s.pos.y - 1, s.pos.x - 1, s.pos.y + 1, s.pos.y + 1, true); var free_space = 0; area.forEach(entry => { if (entry.type == "creep") free_space--; else if (entry.type == "terrain" && entry.terrain == "plain") free_space++; else if (entry.type == "terrain" && entry.terrain == "swamp") free_space++; }); return free_space > 0; } }) == ERR_NOT_FOUND) return; const target = Game.getObjectById(this.memory.target); 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) && 0 < s.store.getFreeCapacity(); } }) == ERR_NOT_FOUND) return; const target = Game.getObjectById(this.memory.target); 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() { 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(); } } moveToTarget() { const target = Game.getObjectById(this.memory.target) if (this.pos.inRangeTo(target.pos.x, target.pos.y, 1)) { this.memory.path = undefined; return OK; } if (this.pos.inRangeTo(target.pos.x, target.pos.y, 5)) { this.moveTo(target) return ERR_BUSY; } if (this.memory.path == undefined) { this.memory.path = this.pos.findPathTo(target.pos, { ignoreCreeps: true, range: 1 }); } const response = this.moveByPath(this.memory.path); if (response == OK) return ERR_BUSY; if (response == ERR_INVALID_ARGS || response == ERR_NOT_FOUND) { this.moveTo(target); this.memory.path = undefined; return ERR_BUSY; } } }