SWAG98

screeps energy harvester

Mar 4th, 2017
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var settings = {
  2.     renew:[400,1200],
  3.     status:{
  4.         renewing:"♻ RENEW",
  5.         harvesting:"⛏ HARVEST",
  6.         delivering:"🛢 DELIVER",
  7.         idle:"🛏 IDLE"
  8.     },
  9.     visualization:{
  10.         "harvest": {visualizePathStyle: {stroke: '#ff0000'}},
  11.         "idle": {visualizePathStyle: {stroke: '#ffff00'}},
  12.         "deliver": {visualizePathStyle: {stroke: '#00ff00'}},
  13.         "renew": {visualizePathStyle: {stroke: '#ff00ff'}}
  14.     }
  15. }
  16.  
  17. function setStatus(creep,status){
  18.     if(creep.memory.status !== status){
  19.         creep.memory.status = status;
  20.         creep.say(settings.status[status]);
  21.     }
  22. }
  23. module.exports = {
  24.     run: function(creep){
  25.        
  26.         if(!creep.memory.status){
  27.             creep.memory.status = "harvesting";
  28.         }
  29.        
  30.         if(creep.memory.renew && creep.ticksToLive < settings.renew[0]){
  31.             setStatus(creep,"renew");
  32.             var target = creep.pos.findClosestByPath(FIND_MY_SPAWNS);
  33.             if(target){
  34.                 creep.moveTo(target.pos, settings.visualization[creep.memory.status]);
  35.             }
  36.             if(creep.pos.isNearTo(target.pos)){
  37.                 if(creep.carry.energy > 0){
  38.                     creep.transfer(target.pos,RESOURCE_ENERGY)
  39.                 }
  40.             }
  41.         }
  42.        
  43.         if(creep.memory.status === "renew" && creep.ticksToLive < settings.renew[1]){
  44.             return;
  45.         } else if(creep.memory.status === "renew" && creep.ticksToLive >= settings.renew[1]){
  46.            
  47.             if(creep.memory.delivering){
  48.                 setStatus(creep, "delivering");
  49.             } else {
  50.                 setStatus(creep, "harvesting");
  51.             }
  52.            
  53.         }
  54.        
  55.         if(creep.carry.energy === 0){
  56.             creep.memory.delivering = false;
  57.         }
  58.        
  59.         if(!creep.memory.delivering){
  60.             setStatus(creep,"harvesting");
  61.         }
  62.        
  63.         if((creep.memory.status === "delivering" || creep.memory.status === "idle" || (!creep.memory.renew && creep.memory.status === "renew")) && creep.memory.delivering){
  64.            
  65.             var target = creep.pos.findClosestByPath(FIND_STRUCTURES,{
  66.                 filter: (structure)=>{
  67.                     return structure.my && ((structure.structureType === STRUCTURE_SPAWN && structure.energyCapacity > structure.energy) ||
  68.                     (structure.structureType === STRUCTURE_STORAGE && structure.storeCapacity > _.sum(container.store)) ||
  69.                     (structure.structureType === STRUCTURE_EXTENSION && structure.energyCapacity > structure.energy) ||
  70.                     (structure.structureType === STRUCTURE_CONTAINER && structure.storeCapacity > _.sum(container.store)))
  71.                 }
  72.             });
  73.            
  74.             if(target){
  75.                 if(creep.transfer(target,RESOURCE_ENERGY) === ERR_NOT_IN_RANGE){
  76.                     creep.moveTo(target, settings.visualization[creep.memory.status]);
  77.                 }
  78.             } else {
  79.                 setStatus(creep,"idle");
  80.                 creep.moveTo(25,25,settings.visualization[creep.memory.status]);
  81.             }
  82.            
  83.         }
  84.        
  85.         if(creep.memory.status === "harvesting"){
  86.            
  87.             if(creep.carry.energy === creep.carryCapacity){
  88.                 setStatus(creep,"delivering");
  89.                 creep.memory.delivering = true;
  90.             } else {
  91.                 var target = creep.pos.findClosestByPath(FIND_SOURCES,{
  92.                     filter:(source)=>{
  93.                         return source.energy > 0
  94.                     }
  95.                 });
  96.                
  97.                 if(target){
  98.                     if(creep.harvest(target) === ERR_NOT_IN_RANGE){
  99.                         creep.moveTo(target,settings.visualization[creep.memory.status]);
  100.                     }
  101.                 }
  102.                
  103.             }
  104.            
  105.         }
  106.        
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment