43 lines
1.7 KiB
JavaScript
43 lines
1.7 KiB
JavaScript
var do_idle = function(creep) {
|
|
// No structure needs filling and my charge is full, move to an idle location
|
|
if (!creep.memory.idlePosition) {
|
|
var idlePosition;
|
|
switch (creep.room.name){
|
|
case "E16N2":
|
|
min_x = 36;
|
|
min_y = 7;
|
|
max_x = 40;
|
|
max_y = 8;
|
|
|
|
idlePosition = new RoomPosition(Math.floor(Math.random() * (max_x - min_x)) + min_x, Math.floor(Math.random() * (max_y - min_y)) + min_y, creep.room.name);
|
|
break;
|
|
default:
|
|
// Calculate a good idle position next to the closest spawn station
|
|
idlePosition = creep.pos.findClosestByRange(FIND_MY_SPAWNS).pos;
|
|
idlePosition.x = idlePosition.x - (Math.floor(Math.random() * 4) + 2); // Number between 2 and 4
|
|
idlePosition.y = idlePosition.y + (Math.floor(Math.random() * 5) - 2); // Number between -2 and 2
|
|
break;
|
|
}
|
|
creep.memory.idlePosition = {'x': idlePosition.x, 'y': idlePosition.y};
|
|
}
|
|
|
|
// If we are not at the idle position, move there.
|
|
if(!creep.pos.isEqualTo(creep.memory.idlePosition.x, creep.memory.idlePosition.y)) {
|
|
var result = creep.moveTo(new RoomPosition(creep.memory.idlePosition.x, creep.memory.idlePosition.y, creep.room.name), {visualizePathStyle: {stroke: '#aaaaaa'}});
|
|
if (result == ERR_NO_PATH) {
|
|
creep.say('No path');
|
|
delete creep.memory.idlePosition;
|
|
}
|
|
}
|
|
}
|
|
|
|
var clear_idle = function(creep) {
|
|
delete creep.memory.idlePosition;
|
|
}
|
|
|
|
var baseIdler = {
|
|
do_idle: do_idle,
|
|
clear_idle: clear_idle,
|
|
}
|
|
|
|
module.exports = baseIdler; |