From ba3428c8e91962046cd43519579654c56846fc98 Mon Sep 17 00:00:00 2001 From: XenoAmess Date: Fri, 9 Sep 2022 01:14:39 +0800 Subject: [PATCH 1/6] change from suicide to recycle. --- src/prototype_creep.js | 5 ++--- src/prototype_creep_clean.js | 3 +-- 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, 20 insertions(+), 24 deletions(-) diff --git a/src/prototype_creep.js b/src/prototype_creep.js index 276a3f4a4..6930eab8f 100644 --- a/src/prototype_creep.js +++ b/src/prototype_creep.js @@ -102,8 +102,7 @@ Creep.prototype.checkForHandle = function() { const role = this.memory.role; if (!role) { this.log('Creep role not defined for: ' + this.id + ' ' + this.name.split('-')[0].replace(/[0-9]/g, '')); - this.memory.killed = true; - this.suicide(); + Creep.recycleCreep(this); return false; } return true; @@ -117,7 +116,7 @@ Creep.prototype.handle = function() { try { if (!this.unit()) { this.log('Unknown role suiciding'); - this.suicide(); + Creep.recycleCreep(this); return; } diff --git a/src/prototype_creep_clean.js b/src/prototype_creep_clean.js index 63d3a125f..f0ea5e6a4 100644 --- a/src/prototype_creep_clean.js +++ b/src/prototype_creep_clean.js @@ -142,8 +142,7 @@ Creep.prototype.cleanSetTargetId = function() { } } this.memory.targetReached = true; - this.memory.killed = true; this.log('Nothing found, suicide'); - this.suicide(); + Creep.recycleCreep(this); // return Creep.recycleCreep(this); }; diff --git a/src/prototype_creep_move.js b/src/prototype_creep_move.js index fc90ca298..798b9919b 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.recycleCreep(creep); return true; } }; diff --git a/src/prototype_creep_routing.js b/src/prototype_creep_routing.js index 5ec77fa23..d7d1cc9fb 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(); + Creep.recycleCreep(this); return true; } if (!path) { diff --git a/src/prototype_room_controller.js b/src/prototype_room_controller.js index 981125b5e..ffba801eb 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) => Creep.recycleCreep(cs)); }; diff --git a/src/role_carry.js b/src/role_carry.js index f472cf091..26ccfe70d 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.recycleCreep(creep); 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.recycleCreep(creep); } } } @@ -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.recycleCreep(creep); } return true; diff --git a/src/role_claimer.js b/src/role_claimer.js index 3034b6e8d..a6b05f4b5 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.recycleCreep(creep); } return true; }; diff --git a/src/role_extractor.js b/src/role_extractor.js index 552613991..dd492a6de 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.recycleCreep(creep); return true; } const mineral = getMineral(creep); diff --git a/src/role_mineral.js b/src/role_mineral.js index 9d5068a95..01aae9f44 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.recycleCreep(creep); return true; } if (creep.ticksToLive < 50 && _.sum(creep.carry) === 0) { // early suicide to not waste minerals - creep.suicide(); + Creep.recycleCreep(creep); return true; } return false; diff --git a/src/role_quester.js b/src/role_quester.js index afdc95797..4fa053fed 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.recycleCreep(creep); }; 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.recycleCreep(creep); }; 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.recycleCreep(creep); return; } if (quest.quest === 'buildcs') { diff --git a/src/role_signer.js b/src/role_signer.js index 54a287e5e..93b72353a 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.recycleCreep(creep); // } return true; } else { diff --git a/src/role_sourcer.js b/src/role_sourcer.js index ee70819e2..f4446bd68 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.recycleCreep(creep); return false; } if (returnCode === ERR_NO_BODYPART) { creep.room.checkRoleToSpawn('defender', 2, undefined, creep.room.name); creep.respawnMe(); - creep.suicide(); + Creep.recycleCreep(creep); return false; } diff --git a/src/role_watcher.js b/src/role_watcher.js index aca6217b8..75c58cfd9 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].recycleCreep(); } } else { creep.moveToMy(pos, near); From 5a92e2046dfa3af7e407c25b4f92a3448ff64706 Mon Sep 17 00:00:00 2001 From: XenoAmess Date: Sat, 10 Sep 2022 06:51:59 +0800 Subject: [PATCH 2/6] change documents --- src/prototype_creep.js | 3 ++- src/prototype_creep_clean.js | 2 +- src/prototype_creep_routing.js | 2 +- src/role_carry.js | 4 ++-- src/role_quester.js | 2 +- src/role_sourcer.js | 2 +- 6 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/prototype_creep.js b/src/prototype_creep.js index 6930eab8f..2d84813bf 100644 --- a/src/prototype_creep.js +++ b/src/prototype_creep.js @@ -101,6 +101,7 @@ Creep.prototype.checkForHandle = function() { const role = this.memory.role; if (!role) { + this.memory.role = 'scout'; this.log('Creep role not defined for: ' + this.id + ' ' + this.name.split('-')[0].replace(/[0-9]/g, '')); Creep.recycleCreep(this); return false; @@ -115,7 +116,7 @@ Creep.prototype.handle = function() { } try { if (!this.unit()) { - this.log('Unknown role suiciding'); + this.log('Unknown role recycling creep'); Creep.recycleCreep(this); return; } diff --git a/src/prototype_creep_clean.js b/src/prototype_creep_clean.js index f0ea5e6a4..b9bb3c630 100644 --- a/src/prototype_creep_clean.js +++ b/src/prototype_creep_clean.js @@ -142,7 +142,7 @@ Creep.prototype.cleanSetTargetId = function() { } } this.memory.targetReached = true; - this.log('Nothing found, suicide'); + this.log('Nothing found, recycling'); Creep.recycleCreep(this); // return Creep.recycleCreep(this); }; diff --git a/src/prototype_creep_routing.js b/src/prototype_creep_routing.js index d7d1cc9fb..fceb97d33 100644 --- a/src/prototype_creep_routing.js +++ b/src/prototype_creep_routing.js @@ -142,7 +142,7 @@ Creep.prototype.followPath = function(action) { try { path = this.prepareRoutingMemory(); } catch (e) { - this.log(`Suiciding, cannot prepare routing ${e} ${e.stack}`); + this.log(`Recycling, cannot prepare routing ${e} ${e.stack}`); Creep.recycleCreep(this); return true; } diff --git a/src/role_carry.js b/src/role_carry.js index 26ccfe70d..23221817b 100644 --- a/src/role_carry.js +++ b/src/role_carry.js @@ -62,7 +62,7 @@ function checkHelperEmptyStorage(creep) { if (targetStructure.structureType === STRUCTURE_STORAGE) { creep.say('storage'); if (targetStructure.store.energy === 0) { - creep.log('Suiciding the storage I should get the energy from is empty'); + creep.log('Recycling, the storage I should get the energy from is empty'); Creep.recycleCreep(creep); } } @@ -398,7 +398,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.log('Recycling because end of path, no energy, do not want to get in the way of the sourcer (better recycle?)'); Creep.recycleCreep(creep); } diff --git a/src/role_quester.js b/src/role_quester.js index 4fa053fed..e4243cd43 100644 --- a/src/role_quester.js +++ b/src/role_quester.js @@ -66,7 +66,7 @@ roles.quester.action = function(creep) { creep.spawnReplacement(); const quest = Memory.quests[creep.memory.level]; if (!quest) { - creep.log(`Quest ${creep.memory.level} not found, suiciding`); + creep.log(`Quest ${creep.memory.level} not found, recycling`); Creep.recycleCreep(creep); return; } diff --git a/src/role_sourcer.js b/src/role_sourcer.js index f4446bd68..4d878c917 100644 --- a/src/role_sourcer.js +++ b/src/role_sourcer.js @@ -102,7 +102,7 @@ function harvest(creep) { } if (returnCode === ERR_NOT_OWNER) { - creep.log('Suiciding, someone else reserved the controller'); + creep.log('Recycling, someone else reserved the controller'); Creep.recycleCreep(creep); return false; } From 3d1b31b4ca967bd75f3346aceef741e4b9f7a90c Mon Sep 17 00:00:00 2001 From: XenoAmess Date: Mon, 12 Sep 2022 07:28:28 +0800 Subject: [PATCH 3/6] refine searchClosestSpawn --- src/prototype_creep_startup_tasks.js | 101 +++++++++++++++++++++++---- src/role_carry.js | 4 +- 2 files changed, 92 insertions(+), 13 deletions(-) diff --git a/src/prototype_creep_startup_tasks.js b/src/prototype_creep_startup_tasks.js index e1b675c78..eabeab191 100644 --- a/src/prototype_creep_startup_tasks.js +++ b/src/prototype_creep_startup_tasks.js @@ -98,30 +98,107 @@ Creep.buildRoads = function(creep) { return false; }; +/** + * search the closest spawn + * + * @param {Creep} creep + * @return {null|Spawn} + */ +Creep.searchClosestSpawn = function(creep) { + const spawn = creep.pos.findClosestByRangePropertyFilter(FIND_MY_STRUCTURES, 'structureType', [STRUCTURE_SPAWN]); + if (spawn) { + return spawn; + } + const roomMap = new Map(); + for (const currentSpawnId of Object.keys(Game.spawns)) { + const currentSpawn = Game.spawns[currentSpawnId]; + roomMap.set( + currentSpawn.room.name, + currentSpawn, + ); + } + if (roomMap.size === 0) { + return null; + } + let minDistance = Number.MAX_SAFE_INTEGER; + let closestRoomName = null; + for (const currentRoomName of roomMap.keys()) { + const currentDistance = Game.map.getRoomLinearDistance(creep.room.name, currentRoomName); + if (currentDistance < minDistance) { + minDistance = currentDistance; + closestRoomName = currentRoomName; + } + } + if (!closestRoomName) { + return null; + } + return roomMap.get(closestRoomName); +}; + Creep.recycleCreep = function(creep) { if (creep.memory.role === 'builder') { if (creep.room.buildStructures()) { creep.memory.recycle = false; } } - - let spawn = creep.pos.findClosestByRangePropertyFilter(FIND_MY_STRUCTURES, 'structureType', [STRUCTURE_SPAWN]); - if (!spawn) { - spawn = Game.rooms[creep.memory.base].findPropertyFilter(FIND_MY_STRUCTURES, 'structureType', [STRUCTURE_SPAWN])[0]; + creep.say('recycle'); + let spawn = null; + let recycleData = creep.memory.recycleData; + if (!recycleData) { + recycleData = {}; + creep.memory.recycleData = recycleData; } - if (spawn) { - if (creep.room === spawn.room) { - creep.moveToMy(spawn.pos); - } else { - // TODO make use of the proper routing logic - creep.moveTo(spawn); - } - creep.say('recycle'); + if (recycleData.targetId) { + spawn = Game.getObjectById(recycleData.targetId); const response = spawn.recycleCreep(creep); if (response === OK) { creep.memory.recycle = true; } + return true; + } + let path = recycleData.path; + if (path) { + const moveResult = creep.moveByPath(path); + if (moveResult === OK) { + recycleData.incompleteCount = 0; + creep.memory.recycleData = recycleData; + return true; + } + } + let incompleteCount = recycleData.incompleteCount; + if (!incompleteCount) { + incompleteCount = 1; + } else { + ++incompleteCount; + } + if (incompleteCount <= 25) { + return true; + } + recycleData.incompleteCount = incompleteCount = 0; + spawn = Creep.searchClosestSpawn(creep); + if (!spawn) { + creep.suicide(); + return true; + } + const targetPosObject = spawn.pos; + const search = PathFinder.search( + creep.pos, + { + pos: targetPosObject, + range: 1, + }, + ); + if (creep.ticksToLive < search.cost) { + creep.suicide(); + return true; + } + recycleData.targetId = spawn.id; + recycleData.path = path = search.path; + const moveResult = creep.moveByPath(path); + if (moveResult === OK) { + recycleData.incompleteCount = 0; } + creep.memory.recycleData = recycleData; return true; }; diff --git a/src/role_carry.js b/src/role_carry.js index 23221817b..800bfe6a2 100644 --- a/src/role_carry.js +++ b/src/role_carry.js @@ -399,7 +399,9 @@ 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('Recycling because end of path, no energy, do not want to get in the way of the sourcer (better recycle?)'); - Creep.recycleCreep(creep); + // no, recycle here will only make it more stuck, fail. + creep.memory.killed = true; + creep.suicide(); } return true; From fae773b361e9a36e7d170f5eddd1c9692490da4b Mon Sep 17 00:00:00 2001 From: XenoAmess Date: Thu, 15 Sep 2022 02:14:42 +0800 Subject: [PATCH 4/6] bugfix --- src/prototype_creep_startup_tasks.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/prototype_creep_startup_tasks.js b/src/prototype_creep_startup_tasks.js index eabeab191..9c9a3af1e 100644 --- a/src/prototype_creep_startup_tasks.js +++ b/src/prototype_creep_startup_tasks.js @@ -166,13 +166,17 @@ Creep.recycleCreep = function(creep) { } } let incompleteCount = recycleData.incompleteCount; - if (!incompleteCount) { - incompleteCount = 1; - } else { - ++incompleteCount; - } - if (incompleteCount <= 25) { - return true; + if (recycleData.targetId && recycleData.path) { + if (!incompleteCount) { + incompleteCount = 1; + } else { + ++incompleteCount; + } + recycleData.incompleteCount = incompleteCount; + if (incompleteCount <= 25) { + creep.memory.recycleData = recycleData; + return true; + } } recycleData.incompleteCount = incompleteCount = 0; spawn = Creep.searchClosestSpawn(creep); From 9693007223ebf6d958518a65966bfcc49c18d163 Mon Sep 17 00:00:00 2001 From: XenoAmess Date: Mon, 19 Sep 2022 05:07:02 +0800 Subject: [PATCH 5/6] bugfix2 for recycle --- src/prototype_creep_startup_tasks.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/prototype_creep_startup_tasks.js b/src/prototype_creep_startup_tasks.js index 9c9a3af1e..798a2c58b 100644 --- a/src/prototype_creep_startup_tasks.js +++ b/src/prototype_creep_startup_tasks.js @@ -161,6 +161,7 @@ Creep.recycleCreep = function(creep) { const moveResult = creep.moveByPath(path); if (moveResult === OK) { recycleData.incompleteCount = 0; + recycleData.path = path.subarray(1); creep.memory.recycleData = recycleData; return true; } From 1dd6026c6ce6d183ad0da2c5674689cbb311f190 Mon Sep 17 00:00:00 2001 From: XenoAmess Date: Sat, 24 Sep 2022 20:27:49 +0800 Subject: [PATCH 6/6] bugfix3 for recycle --- src/prototype_creep_startup_tasks.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/prototype_creep_startup_tasks.js b/src/prototype_creep_startup_tasks.js index 798a2c58b..c67957462 100644 --- a/src/prototype_creep_startup_tasks.js +++ b/src/prototype_creep_startup_tasks.js @@ -157,11 +157,15 @@ Creep.recycleCreep = function(creep) { return true; } let path = recycleData.path; - if (path) { + if (path && path.length) { + for (let i = 0; i < path.length; ++i) { + let roomPositionMemory = path[i]; + path[i] = new RoomPosition(roomPositionMemory.x, roomPositionMemory.y, roomPositionMemory.roomName); + } const moveResult = creep.moveByPath(path); if (moveResult === OK) { recycleData.incompleteCount = 0; - recycleData.path = path.subarray(1); + recycleData.path = path = path.shift(); creep.memory.recycleData = recycleData; return true; }