I have altered a bit of my role.builders.js file to have my creeps first check to see if the spawn has enery, if it does not go harvest energy to build, otherwise get it from the spawn. Here is what I have:
var roleBuilder = {
/** @param {Creep} creep **/
run: function(creep) {
if(creep.memory.building && creep.carry.energy == 0) {
creep.memory.building = false;
creep.say('harvesting');
}
if(!creep.memory.building && creep.carry.energy == creep.carryCapacity) {
creep.memory.building = true;
creep.say('building');
}
if(creep.memory.building) {
var targets = creep.room.find(FIND_CONSTRUCTION_SITES);
if(targets.length) {
if(creep.build(targets[0]) == ERR_NOT_IN_RANGE) {
creep.moveTo(targets[0]);
}
}
}
else
{
if(!Game.spawns['MySpawn'].energy)
{
var sources = creep.room.find(FIND_SOURCES);
if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE)
{
creep.moveTo(sources[0]);
}
}
else
{
if(creep.withdraw(Game.spawns['MySpawn'],RESOURCE_ENERGY) == ERR_NOT_IN_RANGE)
{
creep.moveTo(Game.spawns['MySpawn']);
}
}
}
}
};
module.exports = roleBuilder;
Any suggestions on what I'm missing? The builders are frozen in place even though it says they are moving to the spawn.
Just like you already noticed, stuff like this works better if you use a preset variable for it.
For the sake of completeness, I'll repost your solution. But you don't need the else
part:
var spns = creep.room.find(FIND_MY_SPAWNS);
if(creep.withdraw(spns[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(spns[0]);
}
If you want your builders to only use one specific spawn, you can also use its ID for that:
var spwn = Game.getObjectById("42ef9e3288f108fe6e60ef23");
if(creep.withdraw(spwn, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(spwn);
}
But anyways, there's one more thing I noticed in your code! You tell your builders to go to the mining, if there is currently NO energy in the spawn.
if(!Game.spawns['MySpawn'].energy) { ... }
The problem with this is, there will almost always be some energy in there, because as long as you are below 300 energy in total, your spawn generates 1 energy every single tick! So your builders will at most go to harvest for one tick, and then will turn around to fetch it from the spawn - because there is at least 1 energy there now.
The next problem with this is, you'll run into a problem where you cannot build new creeps, because your builders always fetch all your energy before one could be spawned!
To compensate for both problems, try something like this:
var spwn = Game.getObjectById("42ef9e3288f108fe6e60ef23");
var secureEnergy = 300;
if(spwn.energy > secureEnergy) { ... }
Set the secureEnergy
to a higher amount than your average creep needs to be spawned (at least higher than your most important harvester creeps need!). For example a basic [WORK,CARRY,MOVE]
creep needs 250 energy and your builder can carry 50 energy, so you might want your builder to only withdraw its 50 energy when there are at least the 300 secureEnergy
in the spawn. This way you can always spawn a new creep nonetheless. Does that make any sense to you?
I hope I could help you a bit...and have fun in Screeps! ;)