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

small fixes and improvements #707

Open
wants to merge 2 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: 5 additions & 3 deletions src/brain_memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ function handleUnexpectedDeadCreeps(name, creepMemory) {
console.log(`${Game.time} ${name} handleUnexpectedDeadCreeps no creepMemory.room creepMemory: ${JSON.stringify(creepMemory)} data: ${JSON.stringify(global.data.creeps[name])}`);
}

debugLog(`${Game.time} ${creepMemory.room} ${name} memory hostile: ${data.hostileCreepCount}`);
if (data.hostileCreepCount > 0) {
debugLog(`${Game.time} ${creepMemory.room} ${name} memory hostile: ${data.hostileCreepCount}`);
debugLog('brain', `${creepMemory.room} ${name} Not in Game.creeps with hostiles lived ${Game.time - creepMemory.born} hostiles: ${data.hostileCreepCount} - I guess killed by hostile`); // eslint-disable-line max-len
} else {
console.log(`${Game.time} ${creepMemory.room} ${name} Not in Game.creeps without hostiles lived ${Game.time - creepMemory.born} hostiles: ${data.hostileCreepCount}`); // eslint-disable-line max-len
console.log(`${Game.time} ${creepMemory.room} ${name} Not in Game.creeps without hostiles lived ${Game.time - creepMemory.born}`); // eslint-disable-line max-len
}

if (Game.time - creepMemory.born < 20) {
Expand Down Expand Up @@ -169,7 +169,9 @@ function cleanRooms() {
delete Memory.rooms[name];
}
if (Memory.myRooms.indexOf(name) < 0) {
console.log(Game.time, `Deleting ${name} from memory, no myRoom ${JSON.stringify(Memory.rooms[name])}`);
if (Object.keys(Memory.rooms[name]).length > 0) {
Copy link
Owner

Choose a reason for hiding this comment

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

So, this idea is to only store myRooms in Memory. All other rooms can be stored in the heap and recalculated. This makes sure that we don't reach the memory limit.

Currently some other rooms are also stored in memory, but mostly with empty values.

Do you know a case where we want to have external rooms stored in memory?

console.log(Game.time, `Deleting ${name} from memory, no myRoom ${JSON.stringify(Memory.rooms[name])}`);
}
delete Memory.rooms[name];
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ global.config = {

commodities: {
disabled: false,
minRCL: 7,
range: 6,
},

pixel: {
Expand Down
3 changes: 3 additions & 0 deletions src/helper_findMyRooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function findRoomsWithinReach(room) {
}
return rooms;
}

module.exports.findRoomsWithinReach = findRoomsWithinReach;

/**
Expand All @@ -35,6 +36,7 @@ function findMyRoomsSortByDistance(roomName) {

return _.sortBy(Memory.myRooms, sortByDistance);
}

module.exports.findMyRoomsSortByDistance = findMyRoomsSortByDistance;

/**
Expand Down Expand Up @@ -67,4 +69,5 @@ function getMyRoomWithinRange(roomName, range, minRCL) {
}
return false;
}

module.exports.getMyRoomWithinRange = getMyRoomWithinRange;
8 changes: 6 additions & 2 deletions src/prototype_creep.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,6 @@ Creep.prototype.buildRoad = function() {
constructionSites.length <= config.buildRoad.maxConstructionSitesRoom &&
Object.keys(Game.constructionSites).length < config.buildRoad.maxConstructionSitesTotal &&
this.memory.routing.pathPos >= 0
// && this.pos.inPath()
) {
const returnCode = this.pos.createConstructionSite(STRUCTURE_ROAD);
if (returnCode === OK) {
Expand Down Expand Up @@ -403,6 +402,11 @@ Creep.prototype.killPrevious = function(path) {
return true;
};

/**
* respawn creep no mater what, copy of memory
*
* @return {number} in queue
*/
Creep.prototype.respawnMe = function() {
const routing = {
targetRoom: this.memory.routing.targetRoom,
Expand All @@ -416,7 +420,7 @@ Creep.prototype.respawnMe = function() {
level: this.memory.level,
routing: routing,
};
Game.rooms[this.memory.base].memory.queue.push(spawn);
return Game.rooms[this.memory.base].memory.queue.push(spawn);
};

Creep.prototype.spawnReplacement = function(maxOfRole) {
Expand Down
13 changes: 9 additions & 4 deletions src/prototype_creep_move.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @param {boolean} withinRoom - Stays within the room
* @return {object} - Response from PathFinder.search
**/
Creep.prototype.searchPath = function(target, range=1, withinRoom=false) {
Creep.prototype.searchPath = function(target, range = 1, withinRoom = false) {
let costMatrixCallback;
if (this.room.memory.misplacedSpawn) {
costMatrixCallback = this.room.getBasicCostMatrixCallback(withinRoom);
Expand Down Expand Up @@ -59,7 +59,7 @@ Creep.prototype.moveMy = function(target) {
* @param {boolean} withinRoom - Stays within the room
* @return {boolean|OK|ERR_TIRED} - Success of the execution
**/
Creep.prototype.moveToMy = function(target, range=1, withinRoom=false) {
Creep.prototype.moveToMy = function(target, range = 1, withinRoom = false) {
this.creepLog(`moveToMy(${target}, ${range}) pos: ${this.pos}`);
if (this.fatigue > 0) {
return true;
Expand All @@ -68,8 +68,13 @@ Creep.prototype.moveToMy = function(target, range=1, withinRoom=false) {
const search = this.searchPath(target, range, withinRoom);
// Fallback to moveTo when the path is incomplete and the creep is only switching positions
if (search.path.length < 2 && search.incomplete) {
this.log(`moveToMy search.path too short ${JSON.stringify(search)} target: ${target}`);
return this.moveTo(target, {range: range});
if (this.memory.role === 'repairer') {
// repairer found energy behind a wall, can not pickup
return false;
} else {
this.log(`moveToMy search.path too short ${JSON.stringify(search)} target: ${target}`);
return this.moveTo(target, {range: range});
}
}
target = search.path[0] || target.pos || target;

Expand Down
10 changes: 9 additions & 1 deletion src/prototype_creep_routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,16 @@ Creep.prototype.getRoute = function() {
}

let route = [];
let useHighWay = false;
switch (this.memory.role) {
case 'carry':
case 'claimer':
case 'nextroomer':
useHighWay = true;
}
Copy link
Owner

Choose a reason for hiding this comment

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

Not sure about this.

Other creeps like sourcer or reserver might need to route through highway rooms as well. So which creeps shouldn't move throw highway rooms? repairer and universal, ... - creeps which shouldn't route at all.


if (this.memory.base !== this.memory.routing.targetRoom) {
route = this.room.findRoute(this.memory.base, this.memory.routing.targetRoom);
route = this.room.findRoute(this.memory.base, this.memory.routing.targetRoom, useHighWay);
if (route < 0) {
route = this.room.findRoute(this.memory.base, this.memory.routing.targetRoom, true);
}
Expand Down
2 changes: 1 addition & 1 deletion src/prototype_room_costmatrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function updateCostMatrixFromPositions(room, costMatrix) {
if (positionType === 'version') {
continue;
}
for (let i = 0; i<room.data.positions[positionType][type].length; i++) {
for (let i = 0; i < room.data.positions[positionType][type].length; i++) {
const pos = room.data.positions[positionType][type][i];
if (!pos) {
room.log(`No pos for positionType: ${positionType} type: ${type} i: ${i}`);
Expand Down
10 changes: 6 additions & 4 deletions src/prototype_room_creepbuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Room.prototype.spawnCheckForCreate = function() {
this.memory.queue.shift();
return false;
}
creep.ttl = creep.ttl || config.creep.queueTtl;
creep.ttl = creep.ttl || Math.ceil(config.creep.queueTtl * _.size(CONTROLLER_DOWNGRADE) / this.controller.level);
Copy link
Owner

Choose a reason for hiding this comment

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

Can you please add a comment or extract it in a method with proper naming, so that it is clearer what it does.

I have no clue why to integrate downgrade or controller level.

if (this.findSpawnsNotSpawning().length === 0) {
creep.ttl--;
}
Expand Down Expand Up @@ -195,9 +195,11 @@ Room.prototype.checkRoleToSpawn = function(role, amount, targetId, targetRoom, l
if (this.inQueue(creepMemory) || this.inRoom(creepMemory, amount)) {
return false;
}
if (targetRoom !== base && typeof Game.map.getRoomStatus(targetRoom) !== 'undefined' && typeof Game.map.getRoomStatus(base) !== 'undefined') {
if (Game.map.getRoomStatus(targetRoom).status !== Game.map.getRoomStatus(base).status) {
return false;
if (targetRoom !== base) { // should fix ci Error: No runtime status data
if (Game.time > 230 && typeof Game.map.getRoomStatus(targetRoom) !== 'undefined' && typeof Game.map.getRoomStatus(base) !== 'undefined') {
Copy link
Owner

Choose a reason for hiding this comment

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

I added a try catch block.

But Game.map.getRoomStatus should only be called once and stored as variable, to save CPU and looks cleaner.

if (Game.map.getRoomStatus(targetRoom).status !== Game.map.getRoomStatus(base).status) {
return false;
}
}
}
return this.memory.queue.push(creepMemory);
Expand Down
3 changes: 3 additions & 0 deletions src/prototype_room_defense.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Room.prototype.findAttackCreeps = function(object) {
if (object.owner.username === 'Source Keeper') {
return false;
}
if (friends.indexOf(object.owner.username) > -1) {
return false;
}

for (const item of Object.keys(object.body)) {
const part = object.body[item];
Expand Down
2 changes: 1 addition & 1 deletion src/prototype_room_external.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ Room.prototype.harvestCommodities = function() {
return;
}
this.debugLog('commodities', `Commodities found ${JSON.stringify(deposits)}`);
const baseRoomName = getMyRoomWithinRange(this.name, 6, 6);
const baseRoomName = getMyRoomWithinRange(this.name, config.commodities.range, config.commodities.minRCL);
if (!baseRoomName) {
this.debugLog('commodities', 'No room for commodity farming found');
return;
Expand Down
36 changes: 24 additions & 12 deletions src/prototype_room_my.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ Room.prototype.getLinkStorage = function() {
*/
function isUnexpectedLinkTransferReturnCode(returnCode) {
return returnCode !== OK &&
returnCode !== ERR_NOT_ENOUGH_RESOURCES &&
returnCode !== ERR_TIRED &&
returnCode !== ERR_RCL_NOT_ENOUGH;
returnCode !== ERR_NOT_ENOUGH_RESOURCES &&
returnCode !== ERR_TIRED &&
returnCode !== ERR_RCL_NOT_ENOUGH;
}

Room.prototype.handleLinksTransferEnergy = function(links, linkIndex, linkStorage) {
Expand Down Expand Up @@ -305,8 +305,10 @@ Room.prototype.handleReviveRoomQueueCarry = function(myRoomName) {
if (Game.map.getRoomLinearDistance(this.name, room.name) > 10) {
return;
}
this.log(`---!!! ${room.name} send energy to ${this.name} !!!---`);
room.checkRoleToSpawn('carry', config.carryHelpers.maxHelpersAmount, room.storage.id, room.name, undefined, this.name);
if (this.controller.level > 1) {
this.log(`---!!! ${room.name} send energy to ${this.name} !!!---`);
room.checkRoleToSpawn('carry', config.carryHelpers.maxHelpersAmount, room.storage.id, room.name, undefined, this.name);
}
};

Room.prototype.handleReviveRoomSendCarry = function() {
Expand Down Expand Up @@ -496,10 +498,10 @@ Room.prototype.isConstructingSpawnEmergency = function() {
if (!this.controller || !this.controller.my || !config.useConstructingSpawnEmergencyOperations.enabled) {
return false;
}
this.data.isConstructingSpawn = this.findPropertyFilter(FIND_MY_STRUCTURES, 'structureType', [STRUCTURE_SPAWN], {}).length === 0;
if ([true, false].includes(this.data.isConstructingSpawn)) {
return this.data.isConstructingSpawn;
}
this.data.isConstructingSpawn = this.findPropertyFilter(FIND_MY_STRUCTURES, 'structureType', [STRUCTURE_SPAWN], {}).length === 0;
return this.data.isConstructingSpawn;
};

Expand Down Expand Up @@ -549,7 +551,7 @@ Room.prototype.executeRoom = function() {
};

const checkForRoute = function(room, roomOther) {
const route = room.findRoute(roomOther.name, room.name);
const route = room.findRoute(roomOther.name, room.name, true);
// TODO Instead of skipping we could try to free up the way: attack with nextroomer or squad
if (route.length === 0) {
roomOther.log('No route to other room: ' + roomOther.name);
Expand Down Expand Up @@ -580,6 +582,7 @@ Room.prototype.reviveMyNowHelperValid = function(helperRoom) {

Room.prototype.reviveMyNow = function() {
const myRooms = findMyRoomsSortByDistance(this.name);
const retVal = [];
for (const helperRoomName of myRooms) {
if (this.name === helperRoomName) {
continue;
Expand All @@ -590,13 +593,22 @@ Room.prototype.reviveMyNow = function() {
}

const hostileCreep = this.findEnemies();
let queueLength;
if (hostileCreep.length > 0) {
this.debugLog('revive', `Send defender from ${helperRoomName}`);
helperRoom.checkRoleToSpawn('defender', 1, undefined, this.name);
queueLength = helperRoom.checkRoleToSpawn('defender', 1, undefined, this.name);
if (queueLength) {
this.debugLog('revive', `Send defender from ${helperRoomName}`);
}
retVal.push([helperRoom.name, queueLength]);
} else {
queueLength = helperRoom.checkRoleToSpawn('nextroomer', 1, undefined, this.name);
if (queueLength) {
this.debugLog('revive', `Send nextroomer from ${helperRoomName}`);
}
retVal.push([helperRoom.name, queueLength]);
}
helperRoom.checkRoleToSpawn('nextroomer', 1, undefined, this.name);
this.debugLog('revive', `Send nextroomer from ${helperRoomName}`);
}
return retVal;
};

Room.prototype.setRoomInactive = function() {
Expand All @@ -616,7 +628,7 @@ Room.prototype.setRoomInactive = function() {
}];
}
if (tokens.length > 0) {
tokens.sort((a, b) => b.price-a.price);
tokens.sort((a, b) => b.price - a.price);
reputationChange = Math.min(-1 * reputationChange, -1 * Math.abs(tokens[0].price));
}
const hostileCreeps = this.findPropertyFilter(FIND_HOSTILE_CREEPS, 'owner.username', ['Invader'], {inverse: true});
Expand Down
25 changes: 18 additions & 7 deletions src/visualizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,24 @@ const getLines = function(room) {
const storedE = room.storage ? room.storage.store[RESOURCE_ENERGY] : 0;
const queueL = room.memory.queue ? room.memory.queue.length : 0;
const rclP = Math.floor(100 * (room.controller.progressTotal ? room.controller.progress / room.controller.progressTotal : 1));

const lines = [
{label: `Energy Average :`, value: energy, coefficient: energy / room.energyCapacityAvailable},
{label: `Stored Energy :`, value: storedE, coefficient: Math.min(storedE, 500000) / 500000},
{label: `Queue length :`, value: queueL, coefficient: (20 - Math.min(queueL, 20)) / 20},
{label: `RCL ${room.controller.level} progress :`, value: rclP, coefficient: rclP / 100},
];
const role = room.memory.queue ? room.memory.queue[0] ? room.memory.queue[0].role : '' : '';
let lines;
if (role !== '') {
lines = [
{label: `Energy Average :`, value: energy, coefficient: energy / room.energyCapacityAvailable},
{label: `Stored Energy :`, value: storedE, coefficient: Math.min(storedE, 500000) / 500000},
{label: `Queue length :`, value: queueL, coefficient: (20 - Math.min(queueL, 20)) / 20},
{label: `1st Role/Queue :`, value: role, coefficient: role},
{label: `RCL ${room.controller.level} progress :`, value: rclP, coefficient: rclP / 100},
];
} else {
lines = [
{label: `Energy Average :`, value: energy, coefficient: energy / room.energyCapacityAvailable},
{label: `Stored Energy :`, value: storedE, coefficient: Math.min(storedE, 500000) / 500000},
{label: `Queue length :`, value: queueL, coefficient: (20 - Math.min(queueL, 20)) / 20},
{label: `RCL ${room.controller.level} progress :`, value: rclP, coefficient: rclP / 100},
];
}
return lines;
};

Expand Down