From 8ec2132bc67d61bceb10493add92c0f3a71188f0 Mon Sep 17 00:00:00 2001 From: XenoAmess Date: Fri, 9 Sep 2022 01:14:39 +0800 Subject: [PATCH] change from suicide to recycle. --- src/prototype_creep.js | 16 ++++++++++++++++ src/prototype_creep_clean.js | 2 +- src/prototype_creep_move.js | 2 +- src/prototype_creep_routing.js | 2 +- src/prototype_room_controller.js | 2 +- src/role_carry.js | 7 +++---- src/role_claimer.js | 2 +- src/role_extractor.js | 2 +- src/role_mineral.js | 4 ++-- src/role_quester.js | 6 +++--- src/role_signer.js | 2 +- src/role_sourcer.js | 5 ++--- src/role_watcher.js | 2 +- 13 files changed, 34 insertions(+), 20 deletions(-) diff --git a/src/prototype_creep.js b/src/prototype_creep.js index 276a3f4a4..52572ff9b 100644 --- a/src/prototype_creep.js +++ b/src/prototype_creep.js @@ -442,3 +442,19 @@ Creep.prototype.setNextSpawn = function() { } } }; + +Creep.prototype.recycle = function() { + let recycleSpawn = this.memory.recycleSpawn; + if (!recycleSpawn) { + recycleSpawn = this.pos.findClosestByRangePropertyFilter(FIND_HOSTILE_STRUCTURES, 'structureType', [STRUCTURE_SPAWN]); + this.memory.recycleSpawn = recycleSpawn; + } + const response = recycleSpawn.recycle(this); + if (response === OK || response === ERR_INVALID_TARGET) { + return; + } else if (response === ERR_NOT_OWNER || response === ERR_RCL_NOT_ENOUGH) { + this.memory.recycleSpawn = recycleSpawn = recycleSpawn = this.pos.findClosestByRangePropertyFilter(FIND_HOSTILE_STRUCTURES, 'structureType', [STRUCTURE_SPAWN]); + } else if (response === ERR_NOT_IN_RANGE) { + this.moveToMy(recycleSpawn); + } +}; diff --git a/src/prototype_creep_clean.js b/src/prototype_creep_clean.js index 63d3a125f..5390e0789 100644 --- a/src/prototype_creep_clean.js +++ b/src/prototype_creep_clean.js @@ -144,6 +144,6 @@ Creep.prototype.cleanSetTargetId = function() { this.memory.targetReached = true; this.memory.killed = true; this.log('Nothing found, suicide'); - this.suicide(); + this.recycle(); // return Creep.recycleCreep(this); }; diff --git a/src/prototype_creep_move.js b/src/prototype_creep_move.js index fc90ca298..c635d8c0f 100644 --- a/src/prototype_creep_move.js +++ b/src/prototype_creep_move.js @@ -193,7 +193,7 @@ Creep.prototype.moveCreepCheckRoleAndTarget = function(creep, direction) { if (role === 'upgrader' && (targetRole === 'universal' || targetRole === 'sourcer' || targetRole === 'upgrader')) { this.log('config_creep_move suicide ' + targetRole); - creep.suicide(); + creep.recycle(); return true; } }; diff --git a/src/prototype_creep_routing.js b/src/prototype_creep_routing.js index 5ec77fa23..e8b62f299 100644 --- a/src/prototype_creep_routing.js +++ b/src/prototype_creep_routing.js @@ -143,7 +143,7 @@ Creep.prototype.followPath = function(action) { path = this.prepareRoutingMemory(); } catch (e) { this.log(`Suiciding, cannot prepare routing ${e} ${e.stack}`); - this.suicide(); + this.recycle(); return true; } if (!path) { diff --git a/src/prototype_room_controller.js b/src/prototype_room_controller.js index 981125b5e..730d666df 100644 --- a/src/prototype_room_controller.js +++ b/src/prototype_room_controller.js @@ -116,5 +116,5 @@ Room.prototype.clearRoom = function() { _.each(constructionSites, (cs) => cs.remove()); const creeps = this.findMyCreeps(); - _.each(creeps, (cs) => cs.suicide()); + _.each(creeps, (cs) => cs.recycle()); }; diff --git a/src/role_carry.js b/src/role_carry.js index f472cf091..712931f9e 100644 --- a/src/role_carry.js +++ b/src/role_carry.js @@ -55,7 +55,7 @@ function checkHelperEmptyStorage(creep) { if (creep.room.name === creep.memory.routing.targetRoom) { const targetStructure = Game.getObjectById(creep.memory.routing.targetId); if (targetStructure === null) { - creep.suicide(); + creep.recycle(); return; } @@ -63,7 +63,7 @@ function checkHelperEmptyStorage(creep) { creep.say('storage'); if (targetStructure.store.energy === 0) { creep.log('Suiciding the storage I should get the energy from is empty'); - creep.suicide(); + creep.recycle(); } } } @@ -399,8 +399,7 @@ roles.carry.action = function(creep) { // End of path, can't harvest, suicide (otherwise the sourcer gets stuck) if (!reverse && creep.body.filter((part) => part.type === WORK).length === 0) { // creep.log('Suiciding because end of path, no energy, do not want to get in the way of the sourcer (better recycle?)'); - creep.memory.killed = true; - creep.suicide(); + creep.recycle(); } return true; diff --git a/src/role_claimer.js b/src/role_claimer.js index 3034b6e8d..da13170e9 100644 --- a/src/role_claimer.js +++ b/src/role_claimer.js @@ -22,7 +22,7 @@ roles.claimer.action = function(creep) { const returnCode = creep.claimController(creep.room.controller); if (returnCode === OK) { creep.creepLog('New claimer, in room, claimed'); - creep.suicide(); + creep.recycle(); } return true; }; diff --git a/src/role_extractor.js b/src/role_extractor.js index 552613991..0dd1ce007 100644 --- a/src/role_extractor.js +++ b/src/role_extractor.js @@ -38,7 +38,7 @@ function getMineral(creep) { roles.extractor.action = function(creep) { if (!creep.room.terminal) { - creep.suicide(); + creep.recycle(); return true; } const mineral = getMineral(creep); diff --git a/src/role_mineral.js b/src/role_mineral.js index 9d5068a95..b35ee5df7 100644 --- a/src/role_mineral.js +++ b/src/role_mineral.js @@ -251,12 +251,12 @@ function transfer(creep, target, resource) { */ function checkForSuicide(creep) { if (!creep.room.terminal) { - creep.suicide(); + creep.recycle(); return true; } if (creep.ticksToLive < 50 && _.sum(creep.carry) === 0) { // early suicide to not waste minerals - creep.suicide(); + creep.recycle(); return true; } return false; diff --git a/src/role_quester.js b/src/role_quester.js index afdc95797..529b3683a 100644 --- a/src/role_quester.js +++ b/src/role_quester.js @@ -16,7 +16,7 @@ roles.quester.settings = { roles.quester.questLost = function(creep, quest, reason, value) { creep.log(`Quest lost cs: ${value} ${JSON.stringify(quest)}`); delete Memory.quests[creep.memory.level]; - creep.suicide(); + creep.recycle(); }; roles.quester.questWon = function(creep, quest) { @@ -33,7 +33,7 @@ roles.quester.questWon = function(creep, quest) { }; creep.room.terminal.send(RESOURCE_ENERGY, 100, quest.player.room, JSON.stringify(response)); delete Memory.quests[creep.memory.level]; - creep.suicide(); + creep.recycle(); }; roles.quester.handleBuildConstructionSite = function(creep, quest) { @@ -67,7 +67,7 @@ roles.quester.action = function(creep) { const quest = Memory.quests[creep.memory.level]; if (!quest) { creep.log(`Quest ${creep.memory.level} not found, suiciding`); - creep.suicide(); + creep.recycle(); return; } if (quest.quest === 'buildcs') { diff --git a/src/role_signer.js b/src/role_signer.js index 54a287e5e..f7644ee0a 100644 --- a/src/role_signer.js +++ b/src/role_signer.js @@ -21,7 +21,7 @@ roles.signer.action = function(creep) { // creep.memory.routing = creep.memory.nextTarget.routing; // creep.memory.nextTarget = creep.memory.nextTarget.nextTarget; // } else { - creep.suicide(); + creep.recycle(); // } return true; } else { diff --git a/src/role_sourcer.js b/src/role_sourcer.js index ee70819e2..887751a6a 100644 --- a/src/role_sourcer.js +++ b/src/role_sourcer.js @@ -103,15 +103,14 @@ function harvest(creep) { if (returnCode === ERR_NOT_OWNER) { creep.log('Suiciding, someone else reserved the controller'); - creep.memory.killed = true; - creep.suicide(); + creep.recycle(); return false; } if (returnCode === ERR_NO_BODYPART) { creep.room.checkRoleToSpawn('defender', 2, undefined, creep.room.name); creep.respawnMe(); - creep.suicide(); + creep.recycle(); return false; } diff --git a/src/role_watcher.js b/src/role_watcher.js index aca6217b8..7d0a0b36a 100644 --- a/src/role_watcher.js +++ b/src/role_watcher.js @@ -45,7 +45,7 @@ roles.watcher.action = function(creep) { }); if (creepOfRole.length > 1) { creepOfRole = _.sortBy(creepOfRole, (c) => c.ticksToLive); - creepOfRole[0].suicide(); + creepOfRole[0].recycle(); } } else { creep.moveToMy(pos, near);