Skip to content

fixed: mineral handling #703

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

Open
wants to merge 3 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
86 changes: 86 additions & 0 deletions src/prototype_room_mineral.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,89 @@ Room.prototype.handleTerminal = function() {
delete this.memory.mineralOrder[roomOtherName];
return true;
};

Room.prototype.cleanupNotEnoughResources = function() {
if (!this.memory.reaction) {
return false;
}
const lab1 = Game.getObjectById(this.memory.reaction.labs[1]);
const lab2 = Game.getObjectById(this.memory.reaction.labs[2]);
const findCreep = this.findMyCreepsOfRole('mineral');
const mineralCreep = findCreep.length ? findCreep[0] : false;
if (mineralCreep && this.memory.reaction && this.memory.reaction.result) {
if (!mineralCreep.checkLabEnoughMineral(lab1, this.memory.reaction.result.first) || !mineralCreep.checkLabEnoughMineral(lab2, this.memory.reaction.result.second)) {
roles.mineral.cleanUpLabs(mineralCreep);
}
}
return true;
};

Room.prototype.handleReaction = function() {
if (!this.memory.reaction) {
return false;
}

const labsToReact = this.findStructuresOfStructureType(STRUCTURE_LAB).filter((lab) => {
if (lab.store[this.memory.reaction.result.first] > 0 || lab.store[this.memory.reaction.result.second] > 0) {
return false;
}
return true;
});

if (labsToReact.length === 0) {
return false;
}

const lab0 = Game.getObjectById(this.memory.reaction.labs[0]);
const lab1 = Game.getObjectById(this.memory.reaction.labs[1]);
const lab2 = Game.getObjectById(this.memory.reaction.labs[2]);
if (lab0 === null || lab1 === null || lab2 === null) {
delete this.memory.reaction;
return false;
} else {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} else {
}

(move the } up)
The if clause ends with a return. It is a bit easier to read.

const labsToReactResponse = labsToReact.map((lab0) => {
const inRange = lab0.pos.getRangeTo(lab1) < 3 && lab0.pos.getRangeTo(lab2) < 3;
if (lab0.cooldown === 0 && inRange) {
if (lab0.mineralAmount > lab0.mineralCapacity - 100 && this.memory.reaction) {
this.memory.fullLab = 1;
return 'ERR_FULL_LAB';
}
if (lab0.mineralAmount < 100) {
this.memory.fullLab = 0;
}
const returnCode = lab0.runReaction(lab1, lab2);
if (returnCode === ERR_NOT_ENOUGH_RESOURCES) {
this.cleanupNotEnoughResources();
return 'ERR_NOT_ENOUGH_RESOURCES';
}
if (returnCode === ERR_NOT_IN_RANGE) {
return 'ERR_NOT_IN_RANGE';
}
if (returnCode === OK) {
return 'OK';
}
} else {
return false;
}
}).reduce((a, t) => a || t, false);

if (this.memory.reaction.result) {
const resultLab = labsToReact.filter((s) => s.cooldown === 0 && s.store[this.memory.reaction.result.result]);
if (config.debug.mineral && typeof labsToReactResponse !== 'undefined' && labsToReactResponse !== false && labsToReact.length > 1) {
this.debugLog(this.memory.reaction.result.result, labsToReactResponse, resultLab);
}
}
this.checkMineralToSpawn();
}
};

Room.prototype.checkMineralToSpawn = function() {
if (Game.time % 150 && this.isRoomReadyForMineralHandling()) {
let amount = 1;
// room has a lot energy in terminal; move energy to storage
if (this.getEnergy() * 0.7 <= this.terminal.store.getUsedCapacity(RESOURCE_ENERGY)) {
amount = 2;
}
this.checkRoleToSpawn('mineral', amount);
}
};
33 changes: 25 additions & 8 deletions src/prototype_room_my.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,28 @@ Room.prototype.handleReputation = function() {
}
};

/**
* todo move this to prototype_room_resources.js ?
*
* @return {number}
*/
Room.prototype.getEnergy = function() {
let amount = 0;
if (this.terminal) {
amount += this.terminal.store[RESOURCE_ENERGY];
}
if (this.storage) {
amount += this.storage.store[RESOURCE_ENERGY];
}
return amount;
};

Room.prototype.getBuilderAmount = function(constructionSites) {
let amount = 1;
const energyInRoom = this.getEnergy();
if (energyInRoom > (config.terminal.minEnergyAmount / 2)) {
amount = 2;
}
if (this.controller.level >= 4 && this.memory.misplacedSpawn) {
amount = 3;
}
Expand Down Expand Up @@ -430,15 +450,13 @@ Room.prototype.checkForMineral = function() {
return false;
}

// const labs = this.findPropertyFilter(FIND_MY_STRUCTURES, 'structureType', [STRUCTURE_LAB]);
// if ((!this.memory.cleanup || this.memory.cleanup <= 10) && (_.size(labs) > 2)) {
if (this.memory.reaction) {
this.handleReaction();
}

if (this.isHealthy() && ((this.data.mineralLastRecyled || Game.time) + 1500 > Game.time)) {
this.checkRoleToSpawn('mineral');
}
// }
// if ((Game.time + this.controller.pos.x + this.controller.pos.y) % 10000 < 10) {
// this.memory.cleanup = 0;
// }
};

Room.prototype.handleEconomyStructures = function() {
Expand Down Expand Up @@ -485,8 +503,7 @@ Room.prototype.isHealthy = function() {
if (this.isStruggling()) {
return false;
}
// TODO extract as config variable
if (this.storage.store.energy < config.room.isHealthyStorageThreshold) {
if (this.getEnergy() < config.room.isHealthyStorageThreshold) {
return false;
}
return true;
Expand Down
Loading