Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * required creep memory arcitecture:
- * {role:"harvester",
- * home:<STRING: Room Name>,
- * renew:<BOOLEAN: renew enabled>
- * }
- * required Memory architecture:
- * {rooms:
- * <home name>:{
- * targets:{
- * harvest:[
- * <STRING:Room Name>,
- * <STRING:Room Name>,
- * <...>
- * ],
- * <...>:<ARRAY:...>
- * },
- * <...>:<OBJECT:...>
- * },
- * <...>:<OBJECT:...>
- * }
- */
- var settings = {
- renew:[800,1200],
- status:{
- renew:"♻ RENEW",
- harvesting:"⛏ HARVEST",
- delivering:"🛢 DELIVER",
- idle:"🛏 IDLE"
- },
- visualization:{
- harvesting: {visualizePathStyle: {stroke: '#ff0000'}},
- idle: {visualizePathStyle: {stroke: '#ffff00'}},
- delivering: {visualizePathStyle: {stroke: '#00ff00'}},
- renew: {visualizePathStyle: {stroke: '#ff00ff'}}
- }
- }
- function setStatus(creep,status){
- if(creep.memory.status !== status){
- creep.memory.status = status;
- creep.say(settings.status[status]);
- }
- }
- module.exports = {
- run: function(creep){
- var newDestination = function(){
- creep.memory.target = Memory.rooms[creep.memory.home].targets.harvest[Object.keys(Memory.rooms[creep.memory.home].targets.harvest)[Math.round(Math.random()*Memory.rooms[creep.memory.home].targets.harvest.length-1)]];
- }
- if(!creep.memory.target){
- newDestination();
- }
- if(!creep.memory.status){
- creep.memory.status = "harvesting";
- }
- if(creep.memory.renew && creep.ticksToLive < settings.renew[0]){
- setStatus(creep,"renew");
- var target = Game.rooms[creep.memory.home].find(FIND_MY_SPAWNS)[0];
- if(target){
- creep.moveTo(target.pos, settings.visualization[creep.memory.status]);
- }
- if(creep.pos.isNearTo(target.pos)){
- if(creep.carry.energy > 0){
- creep.transfer(target.pos,RESOURCE_ENERGY)
- }
- }
- }
- if(creep.memory.status === "renew" && creep.ticksToLive < settings.renew[1]){
- return;
- } else if(creep.memory.status === "renew" && creep.ticksToLive >= settings.renew[1]){
- if(creep.memory.delivering){
- setStatus(creep, "delivering");
- } else {
- setStatus(creep, "harvesting");
- }
- }
- if(creep.carry.energy === 0){
- creep.memory.delivering = false;
- }
- if(!creep.memory.delivering){
- setStatus(creep,"harvesting");
- }
- if((creep.memory.status === "delivering" || creep.memory.status === "idle" || (!creep.memory.renew && creep.memory.status === "renew")) && creep.memory.delivering){
- if(creep.room.name == creep.memory.home){
- var target = creep.pos.findClosestByPath(FIND_STRUCTURES,{
- filter: (structure)=>{
- return structure.my && ((structure.structureType === STRUCTURE_SPAWN && structure.energyCapacity > structure.energy) ||
- (structure.structureType === STRUCTURE_STORAGE && structure.storeCapacity > _.sum(container.store)) ||
- (structure.structureType === STRUCTURE_EXTENSION && structure.energyCapacity > structure.energy) ||
- (structure.structureType === STRUCTURE_CONTAINER && structure.storeCapacity > _.sum(container.store)))
- }
- });
- creep.memory.target = creep.memory.home;
- if(target){
- if(creep.memory.status === "idle"){
- setStatus(creep,"delivering")
- }
- if(creep.transfer(target,RESOURCE_ENERGY) === ERR_NOT_IN_RANGE){
- creep.moveTo(target, settings.visualization[creep.memory.status]);
- }
- } else {
- setStatus(creep,"idle");
- if(Object.keys(Game.flags).indexOf("Rest"+creep.memory.home) === -1){
- creep.moveTo(new RoomPosition(25,25,creep.memory.home),settings.visualization[creep.memory.status]);
- } else {
- creep.moveTo(Game.flags["Rest"+creep.memory.home].pos, settings.visualization[creep.memory.status]);
- }
- }
- } else {
- creep.moveTo(new RoomPosition(25,25,creep.memory.home));
- }
- }
- if(creep.memory.status === "harvesting"){
- if(creep.room.name !== creep.memory.target){
- creep.moveTo(new RoomPosition(25,25,creep.memory.target));
- } else {
- if(creep.carry.energy === creep.carryCapacity){
- newDestination();
- setStatus(creep,"delivering");
- creep.memory.delivering = true;
- } else {
- var target = creep.pos.findClosestByPath(FIND_SOURCES,{
- filter:(source)=>{
- return source.energy > 0
- }
- });
- if(target){
- if(creep.harvest(target) === ERR_NOT_IN_RANGE){
- creep.moveTo(target,settings.visualization[creep.memory.status]);
- }
- } else {
- newDestination();
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment