Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP][LOGIC REFINE]change from suicide to recycle. #664

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/prototype_creep.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ 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, ''));
this.memory.killed = true;
this.suicide();
Creep.recycleCreep(this);
XenoAmess marked this conversation as resolved.
Show resolved Hide resolved
return false;
}
return true;
Expand All @@ -116,8 +116,8 @@ Creep.prototype.handle = function() {
}
try {
if (!this.unit()) {
this.log('Unknown role suiciding');
this.suicide();
this.log('Unknown role recycling creep');
Creep.recycleCreep(this);
XenoAmess marked this conversation as resolved.
Show resolved Hide resolved
return;
}

Expand Down
5 changes: 2 additions & 3 deletions src/prototype_creep_clean.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ Creep.prototype.cleanSetTargetId = function() {
}
}
this.memory.targetReached = true;
this.memory.killed = true;
this.log('Nothing found, suicide');
this.suicide();
this.log('Nothing found, recycling');
Creep.recycleCreep(this);
// return Creep.recycleCreep(this);
};
2 changes: 1 addition & 1 deletion src/prototype_creep_move.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};
Expand Down
4 changes: 2 additions & 2 deletions src/prototype_creep_routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ Creep.prototype.followPath = function(action) {
try {
path = this.prepareRoutingMemory();
} catch (e) {
this.log(`Suiciding, cannot prepare routing ${e} ${e.stack}`);
this.suicide();
this.log(`Recycling, cannot prepare routing ${e} ${e.stack}`);
Creep.recycleCreep(this);
return true;
}
if (!path) {
Expand Down
110 changes: 98 additions & 12 deletions src/prototype_creep_startup_tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,30 +98,116 @@ 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 && 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 = path.shift();
creep.memory.recycleData = recycleData;
return true;
}
}
let incompleteCount = recycleData.incompleteCount;
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);
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;
};

Expand Down
2 changes: 1 addition & 1 deletion src/prototype_room_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
XenoAmess marked this conversation as resolved.
Show resolved Hide resolved
};
9 changes: 5 additions & 4 deletions src/role_carry.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ 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;
}

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.suicide();
creep.log('Recycling, the storage I should get the energy from is empty');
Creep.recycleCreep(creep);
}
}
}
Expand Down Expand Up @@ -398,7 +398,8 @@ 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?)');
// no, recycle here will only make it more stuck, fail.
creep.memory.killed = true;
creep.suicide();
}
Expand Down
2 changes: 1 addition & 1 deletion src/role_claimer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
XenoAmess marked this conversation as resolved.
Show resolved Hide resolved
}
return true;
};
2 changes: 1 addition & 1 deletion src/role_extractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/role_mineral.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions src/role_quester.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
XenoAmess marked this conversation as resolved.
Show resolved Hide resolved
};

roles.quester.questWon = function(creep, quest) {
Expand All @@ -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);
XenoAmess marked this conversation as resolved.
Show resolved Hide resolved
};

roles.quester.handleBuildConstructionSite = function(creep, quest) {
Expand Down Expand Up @@ -66,8 +66,8 @@ 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.suicide();
creep.log(`Quest ${creep.memory.level} not found, recycling`);
Creep.recycleCreep(creep);
return;
}
if (quest.quest === 'buildcs') {
Expand Down
2 changes: 1 addition & 1 deletion src/role_signer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 3 additions & 4 deletions src/role_sourcer.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,15 @@ function harvest(creep) {
}

if (returnCode === ERR_NOT_OWNER) {
creep.log('Suiciding, someone else reserved the controller');
creep.memory.killed = true;
creep.suicide();
creep.log('Recycling, someone else reserved the controller');
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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/role_watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down