SWAG98

Screeps Harvester

Mar 5th, 2017
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. *   required creep memory arcitecture:
  3. *       {role:"harvester",
  4. *        home:<STRING: Room Name>,
  5. *        renew:<BOOLEAN: renew enabled>
  6. *       }
  7. *   required Memory architecture:
  8. *       {rooms:
  9. *           <home name>:{
  10. *               targets:{
  11. *                   harvest:[
  12. *                       <STRING:Room Name>,
  13. *                       <STRING:Room Name>,
  14. *                       <...>
  15. *                   ],
  16. *                   <...>:<ARRAY:...>
  17. *               },
  18. *               <...>:<OBJECT:...>
  19. *           },
  20. *           <...>:<OBJECT:...>
  21. *       }
  22. */
  23. var settings = {
  24.     renew:[800,1200],
  25.     status:{
  26.         renew:"♻ RENEW",
  27.         harvesting:"⛏ HARVEST",
  28.         delivering:"🛢 DELIVER",
  29.         idle:"🛏 IDLE"
  30.     },
  31.     visualization:{
  32.         harvesting: {visualizePathStyle: {stroke: '#ff0000'}},
  33.         idle: {visualizePathStyle: {stroke: '#ffff00'}},
  34.         delivering: {visualizePathStyle: {stroke: '#00ff00'}},
  35.         renew: {visualizePathStyle: {stroke: '#ff00ff'}}
  36.     }
  37. }
  38.  
  39. function setStatus(creep,status){
  40.     if(creep.memory.status !== status){
  41.         creep.memory.status = status;
  42.         creep.say(settings.status[status]);
  43.     }
  44. }
  45. module.exports = {
  46.     run: function(creep){
  47.        
  48.         var newDestination = function(){
  49.             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)]];
  50.         }
  51.        
  52.         if(!creep.memory.target){
  53.             newDestination();
  54.         }
  55.        
  56.         if(!creep.memory.status){
  57.             creep.memory.status = "harvesting";
  58.         }
  59.        
  60.         if(creep.memory.renew && creep.ticksToLive < settings.renew[0]){
  61.             setStatus(creep,"renew");
  62.             var target = Game.rooms[creep.memory.home].find(FIND_MY_SPAWNS)[0];
  63.             if(target){
  64.                 creep.moveTo(target.pos, settings.visualization[creep.memory.status]);
  65.             }
  66.             if(creep.pos.isNearTo(target.pos)){
  67.                 if(creep.carry.energy > 0){
  68.                     creep.transfer(target.pos,RESOURCE_ENERGY)
  69.                 }
  70.             }
  71.         }
  72.        
  73.         if(creep.memory.status === "renew" && creep.ticksToLive < settings.renew[1]){
  74.             return;
  75.         } else if(creep.memory.status === "renew" && creep.ticksToLive >= settings.renew[1]){
  76.            
  77.             if(creep.memory.delivering){
  78.                 setStatus(creep, "delivering");
  79.             } else {
  80.                 setStatus(creep, "harvesting");
  81.             }
  82.            
  83.         }
  84.        
  85.         if(creep.carry.energy === 0){
  86.             creep.memory.delivering = false;
  87.         }
  88.        
  89.         if(!creep.memory.delivering){
  90.             setStatus(creep,"harvesting");
  91.         }
  92.        
  93.         if((creep.memory.status === "delivering" || creep.memory.status === "idle" || (!creep.memory.renew && creep.memory.status === "renew")) && creep.memory.delivering){
  94.             if(creep.room.name == creep.memory.home){
  95.                 var target = creep.pos.findClosestByPath(FIND_STRUCTURES,{
  96.                     filter: (structure)=>{
  97.                         return structure.my && ((structure.structureType === STRUCTURE_SPAWN && structure.energyCapacity > structure.energy) ||
  98.                         (structure.structureType === STRUCTURE_STORAGE && structure.storeCapacity > _.sum(container.store)) ||
  99.                         (structure.structureType === STRUCTURE_EXTENSION && structure.energyCapacity > structure.energy) ||
  100.                         (structure.structureType === STRUCTURE_CONTAINER && structure.storeCapacity > _.sum(container.store)))
  101.                     }
  102.                 });
  103.                 creep.memory.target = creep.memory.home;
  104.                 if(target){
  105.                     if(creep.memory.status === "idle"){
  106.                         setStatus(creep,"delivering")
  107.                     }
  108.                     if(creep.transfer(target,RESOURCE_ENERGY) === ERR_NOT_IN_RANGE){
  109.                         creep.moveTo(target, settings.visualization[creep.memory.status]);
  110.                     }
  111.                 } else {
  112.                     setStatus(creep,"idle");
  113.                     if(Object.keys(Game.flags).indexOf("Rest"+creep.memory.home) === -1){
  114.                         creep.moveTo(new RoomPosition(25,25,creep.memory.home),settings.visualization[creep.memory.status]);
  115.                     } else {
  116.                         creep.moveTo(Game.flags["Rest"+creep.memory.home].pos, settings.visualization[creep.memory.status]);
  117.                     }
  118.                 }
  119.             } else {
  120.                 creep.moveTo(new RoomPosition(25,25,creep.memory.home));
  121.             }
  122.         }
  123.        
  124.         if(creep.memory.status === "harvesting"){
  125.            
  126.             if(creep.room.name !== creep.memory.target){
  127.                 creep.moveTo(new RoomPosition(25,25,creep.memory.target));
  128.             } else {
  129.                
  130.                 if(creep.carry.energy === creep.carryCapacity){
  131.                     newDestination();
  132.                     setStatus(creep,"delivering");
  133.                     creep.memory.delivering = true;
  134.                 } else {
  135.                     var target = creep.pos.findClosestByPath(FIND_SOURCES,{
  136.                         filter:(source)=>{
  137.                             return source.energy > 0
  138.                         }
  139.                     });
  140.                    
  141.                     if(target){
  142.                         if(creep.harvest(target) === ERR_NOT_IN_RANGE){
  143.                             creep.moveTo(target,settings.visualization[creep.memory.status]);
  144.                         }
  145.                     } else {
  146.                         newDestination();
  147.                     }
  148.                    
  149.                 }
  150.                
  151.             }
  152.            
  153.         }
  154.        
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment