228 lines
9.2 KiB
JavaScript
228 lines
9.2 KiB
JavaScript
var idler = require('idle.base');
|
|
|
|
// Finds energy laying around on the map
|
|
var find_energy = function(creep) {
|
|
if (!creep.memory.spot) {
|
|
max_energy = 0;
|
|
max_energy_obj = null;
|
|
|
|
for(let x of creep.room.find(FIND_DROPPED_RESOURCES, {filter: (s) => {return s.resourceType == RESOURCE_ENERGY}})){
|
|
if(x.amount > max_energy) {
|
|
if(x.pos.lookFor(LOOK_CREEPS).length == 0){
|
|
max_energy = x.amount;
|
|
max_energy_obj = x;
|
|
}
|
|
}
|
|
}
|
|
|
|
for (let x of creep.room.find(FIND_TOMBSTONES, {filter: (t) => {return t.store.getUsedCapacity(RESOURCE_ENERGY) > 0}})) {
|
|
if(x.store.getUsedCapacity(RESOURCE_ENERGY) > max_energy) {
|
|
if(x.pos.lookFor(LOOK_CREEPS).length == 0){
|
|
max_energy = x.store.getUsedCapacity(RESOURCE_ENERGY);
|
|
max_energy_obj = x;
|
|
}
|
|
}
|
|
}
|
|
|
|
if(max_energy_obj != null){
|
|
return [max_energy_obj.pos.x, max_energy_obj.pos.y, max_energy_obj.room.name, max_energy_obj.id];
|
|
}
|
|
}
|
|
}
|
|
|
|
// Finds the container near the current spot of the creep
|
|
var find_container = function(creep) {
|
|
var container = creep.pos.findClosestByRange(FIND_STRUCTURES, {
|
|
filter: (x) => {
|
|
return (x.structureType == STRUCTURE_CONTAINER || x.structureType == STRUCTURE_STORAGE) && x.store.getFreeCapacity(RESOURCE_ENERGY) > 0;
|
|
}
|
|
});
|
|
if (container) {
|
|
return container.id;
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
var rolePickup = {
|
|
|
|
/**
|
|
@param {Creep} creep
|
|
@param {int} behaviourLevel
|
|
**/
|
|
run: function(creep, behaviourLevel) {
|
|
switch (creep.memory.state) {
|
|
case "idle":
|
|
idler.do_idle(creep);
|
|
if(creep.memory.idle_time == undefined){
|
|
creep.memory.idle_time = 0;
|
|
}
|
|
creep.memory.idle_time += 1;
|
|
|
|
// If we've been idling for a while, check if there is a spot available
|
|
if (creep.memory.idle_time > 5) {
|
|
delete creep.memory.idle_time;
|
|
if(creep.store.getUsedCapacity() > 0){
|
|
creep.memory.state = "walk_to_container";
|
|
creep.say("Dropping...");
|
|
} else {
|
|
creep.memory.state = "find_spot";
|
|
creep.say("Looking...");
|
|
}
|
|
}
|
|
break;
|
|
|
|
case "find_spot":
|
|
// If we don't have a spot yet, find one
|
|
if (!creep.memory.spot) {
|
|
creep.memory.spot = find_energy(creep);
|
|
creep.memory.container = find_container(creep);
|
|
}
|
|
// If no spot was found, go back to idling
|
|
if (!creep.memory.spot) {
|
|
creep.memory.state = "idle";
|
|
creep.say("No drop!");
|
|
} else {
|
|
creep.memory.state = "walk_to_spot";
|
|
creep.say("Walking...");
|
|
}
|
|
break;
|
|
|
|
case "walk_to_spot":
|
|
if (creep.memory.spot) {
|
|
if(creep.memory.spot[0] == null || creep.memory.spot[1] == null || creep.memory.spot[2] == null){
|
|
// We don't have a spot
|
|
creep.memory.spot = null;
|
|
creep.memory.container = null;
|
|
creep.memory.state = "find_spot";
|
|
creep.say("Looking...");
|
|
} else {
|
|
if (creep.pos.x != creep.memory.spot[0] || creep.pos.y != creep.memory.spot[1]) {
|
|
creep.moveTo(creep.memory.spot[0], creep.memory.spot[1], {visualizePathStyle: {stroke: '#0044aa'}})
|
|
// Check if our spot is still available
|
|
if ((new RoomPosition(creep.memory.spot[0], creep.memory.spot[1], creep.memory.spot[2])).lookFor(LOOK_CREEPS).length > 0){
|
|
creep.memory.spot = null;
|
|
creep.memory.container = null;
|
|
creep.memory.state = "idle";
|
|
creep.say("Occupied:(");
|
|
}
|
|
} else {
|
|
// We arrived! Start grabbing.
|
|
creep.memory.state = "grab_energy";
|
|
creep.say("Grabbing...");
|
|
}
|
|
}
|
|
} else {
|
|
// We don't have a spot
|
|
creep.memory.state = "find_spot";
|
|
creep.say("Looking...");
|
|
}
|
|
break;
|
|
|
|
case "walk_to_container":
|
|
if (creep.memory.spot) {
|
|
delete creep.memory.spot;
|
|
}
|
|
if (creep.memory.container) {
|
|
|
|
var container = Game.getObjectById(creep.memory.container);
|
|
|
|
if (creep.pos.x != container.pos.x || creep.pos.y != container.pos.y) {
|
|
creep.moveTo(container, {visualizePathStyle: {stroke: '#0044aa'}});
|
|
} else {
|
|
// We arrived! Start dropping.
|
|
creep.memory.state = "drop_energy";
|
|
creep.say("Grabbing...");
|
|
}
|
|
} else {
|
|
// We don't have a container
|
|
creep.memory.container = find_container(creep);
|
|
if(!creep.memory.container){
|
|
creep.memory.state = "idle"
|
|
creep.say("No container:(");
|
|
}
|
|
}
|
|
break;
|
|
|
|
case "grab_energy":
|
|
if (creep.memory.spot) {
|
|
|
|
var target = Game.getObjectById(creep.memory.spot[3]);
|
|
if(target instanceof Tombstone) {
|
|
creep.withdraw(target, RESOURCE_ENERGY);
|
|
} else if (target instanceof Resource) {
|
|
creep.pickup(target);
|
|
} else {
|
|
creep.say("Unkn.Tgt.");
|
|
}
|
|
|
|
// If we have more space, look for a new spot
|
|
if(creep.store.getFreeCapacity > 0) {
|
|
creep.memory.spot = null;
|
|
creep.memory.container = null;
|
|
creep.memory.state = "find_spot";
|
|
creep.say("Looking...");
|
|
// Else, go to drop off
|
|
} else {
|
|
// We are full, dump the energy into the container
|
|
creep.memory.state = "drop_energy";
|
|
creep.say("Dumping...");
|
|
}
|
|
} else {
|
|
// We don't have a spot
|
|
creep.memory.state = "find_spot";
|
|
creep.say("Looking...");
|
|
}
|
|
break;
|
|
|
|
case "drop_energy":
|
|
if (creep.memory.container) {
|
|
var container = Game.getObjectById(creep.memory.container);
|
|
if (container) {
|
|
if (container.store.getFreeCapacity(RESOURCE_ENERGY) > 0 && creep.store.getUsedCapacity() > 0) {
|
|
if(creep.transfer(container, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
|
|
creep.moveTo(container);
|
|
}
|
|
} else if (container.store.getFreeCapacity(RESOURCE_ENERGY) == 0) {
|
|
// Find new container
|
|
creep.memory.container = find_container(creep);
|
|
if(!creep.memory.container){
|
|
creep.memory.state = "idle"
|
|
creep.say("No container:(");
|
|
}
|
|
}
|
|
} else {
|
|
// No container?
|
|
creep.memory.container = find_container(creep);
|
|
if(!creep.memory.container){
|
|
creep.memory.state = "idle"
|
|
creep.say("No container:(");
|
|
}
|
|
}
|
|
if (creep.store.getUsedCapacity() == 0) {
|
|
// Everything dumped, search for new energy
|
|
creep.memory.spot = null;
|
|
creep.memory.container = null;
|
|
creep.memory.state = "find_spot";
|
|
creep.say("Grabbing...");
|
|
}
|
|
} else {
|
|
// No container?
|
|
creep.memory.spot = null;
|
|
creep.memory.container = null;
|
|
creep.memory.state = "find_spot";
|
|
creep.say("Looking...");
|
|
}
|
|
break;
|
|
|
|
default:
|
|
creep.memory.spot = null;
|
|
creep.memory.container = null;
|
|
creep.memory.state = "find_spot";
|
|
creep.say('Looking...');
|
|
}
|
|
}
|
|
};
|
|
|
|
module.exports = rolePickup;
|