diff --git a/ClassCreep.js b/ClassCreep.js index 785bda4..d55fa60 100644 --- a/ClassCreep.js +++ b/ClassCreep.js @@ -18,31 +18,38 @@ class _Creep extends Creep { this.memory.path = undefined; } - findTarget(target) { + 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; - } + const target = this.pos.findClosestByPath(TARGET); + if (target == null) return ERR_NOT_FOUND; + else this.memory.target = target.id; } return OK; } - findTarget(target, filter) { + 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; - } + 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) == ERR_NOT_FOUND) return; - const target = Game.getObjectById(this.memory.target.id); + 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(); @@ -70,13 +77,14 @@ class _Creep extends Creep { supplyStructures() { if (this.findTarget(FIND_MY_STRUCTURES, { filter: (s) => { - return s.structureType == STRUCTURE_SPAWN || + return (s.structureType == STRUCTURE_SPAWN || s.structureType == STRUCTURE_EXTENSION || s.structureType == STRUCTURE_TOWER || - s.structureType == STRUCTURE_STORAGE; + s.structureType == STRUCTURE_STORAGE) + && 0 < s.store.getFreeCapacity(); } }) == ERR_NOT_FOUND) return; - const target = Game.getObjectById(this.memory.target.id); + const target = Game.getObjectById(this.memory.target); if (this.moveToTarget() == OK) { this.transfer(target, RESOURCE_ENERGY); this.toggleChargeMode(); @@ -84,14 +92,9 @@ class _Creep extends Creep { } 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); + this.memory.target = this.room.controller.id; if (this.moveToTarget() == OK) { - this.upgradeController(); + if (this.upgradeController(this.room.controller) == OK) return; this.toggleChargeMode(); } } @@ -126,30 +129,27 @@ class _Creep extends Creep { } } - // Moves to the target. moveToTarget() { - if (this.pos.inRangeTo(this.memory.target.pos.x, this.memory.target.pos.y, 1)) { + 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) { - var path = this.pos.findPathTo(this.memory.target.pos, { + this.memory.path = this.pos.findPathTo(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) { + 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; } - this.memory.path.push(step); } } diff --git a/ClassRoom.js b/ClassRoom.js index 67fe4dd..15b2020 100644 --- a/ClassRoom.js +++ b/ClassRoom.js @@ -15,16 +15,8 @@ class _Room extends Room { //#region Checks - // Is the level changed? - isNewLevel() { - if (this.memory.level == undefined) this.memory.level = -1; - const value = this.controller.my && this.controller.level != this.memory.level; - if (value) this.memory.level = this.controller.level; - return value; - } - // Is the max amount of construction sites achieved? - isConstructionQueueFull() { return this.find(FIND_CONSTRUCTION_SITES).length > 90; } + canConstruct() { return this.find(FIND_CONSTRUCTION_SITES).length < 90; } //#endregion //#region Construcion @@ -62,7 +54,6 @@ class _Room extends Room { }); // Build a road for every permutation of the targets. - // TODO: When performance to high make this loop recoverable. for (let i = 0; i < target_array.length; i++) { const begin = target_array[i]; for (let j = 0; j < target_array.length; j++) { diff --git a/ClassSpawn.js b/ClassSpawn.js index d43fb53..b116582 100644 --- a/ClassSpawn.js +++ b/ClassSpawn.js @@ -14,7 +14,7 @@ class _Spawn extends StructureSpawn { switch (role) { case ROLE_HARVESTER: - return roleCount < 5; + return roleCount < 10; break; default: diff --git a/_creeps.js b/_creeps.js index 5b1afbf..ad19946 100644 --- a/_creeps.js +++ b/_creeps.js @@ -1,3 +1,5 @@ +const { round } = require("lodash"); + module.exports = { // Gets called when recompiling and when Restart command is called. start: function () { @@ -11,7 +13,10 @@ module.exports = { update: function () { for (const creep_name in Memory.creeps) { const creep = Game.creeps[creep_name]; - if (creep == undefined) delete Memory.creeps[creep_name]; + if (creep == undefined) { + delete Memory.creeps[creep_name]; + return; + } switch (creep.memory.role) { case ROLE_HARVESTER: if (creep.memory.recharge) creep.harvestSource(); diff --git a/_rooms.js b/_rooms.js index be0de35..3b55701 100644 --- a/_rooms.js +++ b/_rooms.js @@ -9,9 +9,9 @@ module.exports = { update: function () { for (const room_name in Game.rooms) { const room = Game.rooms[room_name]; - if (room.isNewLevel()) { + if (Game.time % 1000 == 0) { // Place new building types if the max construcion sites isn't achieved yet. - if (!room.isConstructionQueueFull()) { + if (room.canConstruct()) { room.planRoads(); room.planStructures(); }