Advertisement
Zoc

role.harvester - screeps test

Zoc
Mar 13th, 2019
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var roleHarvester = {
  2.  
  3.     /** @param {Creep} creep **/
  4.     run: function(creep)
  5.     {
  6. //        console.log(creep.memory.building)
  7.        
  8.         var targets = creep.room.find(FIND_STRUCTURES,
  9.         {
  10.             filter: (structure) =>
  11.             {
  12.                 return (structure.structureType == STRUCTURE_EXTENSION ||
  13.                         structure.structureType == STRUCTURE_SPAWN ||
  14.                         structure.structureType == STRUCTURE_TOWER) && structure.energy < structure.energyCapacity;
  15.             }
  16.         });
  17.        
  18.         if (targets.length > 0)
  19.         {
  20.            
  21.             if(creep.carry.energy < creep.carryCapacity) {
  22.                 var sources = creep.room.find(FIND_SOURCES);
  23.                 if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
  24.                     creep.moveTo(sources[0], {visualizePathStyle: {stroke: '#ffaa00'}});
  25.                 }
  26.             }
  27.             else {
  28.                 var targets = creep.room.find(FIND_STRUCTURES, {
  29.                         filter: (structure) => {
  30.                             return (structure.structureType == STRUCTURE_EXTENSION ||
  31.                                     structure.structureType == STRUCTURE_SPAWN ||
  32.                                     structure.structureType == STRUCTURE_TOWER) && structure.energy < structure.energyCapacity;
  33.                         }
  34.                 });
  35.                 if(targets.length > 0) {
  36.                     if(creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
  37.                         creep.moveTo(targets[0], {visualizePathStyle: {stroke: '#ffffff'}});
  38.                     }
  39.                 }
  40.             }
  41.         }
  42.         else
  43.         {
  44.             if(creep.carry.energy == 0)
  45.             {
  46.                 creep.memory.building = false;
  47.             }
  48.             else if (creep.carry.energy >= creep.carryCapacity)
  49.             {
  50.                 creep.memory.building = true;
  51.             }
  52.            
  53.             if(creep.memory.building)
  54.             {
  55.                 var targets = creep.room.find(FIND_CONSTRUCTION_SITES);
  56.                 if(targets.length)
  57.                 {
  58.                     if(targets[0].structureType == STRUCTURE_EXTENSION)
  59.                     {
  60.                         if(creep.build(targets[0]) == ERR_NOT_IN_RANGE)
  61.                         {
  62.                             creep.moveTo(targets[0], {visualizePathStyle: {stroke: '#33ff33'}});
  63.                         }
  64.                     }
  65.                 }
  66.             }
  67.             else
  68.             {
  69.                 var sources = creep.room.find(FIND_SOURCES);
  70.                 if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
  71.                     creep.moveTo(sources[0], {visualizePathStyle: {stroke: '#ffaa00'}});
  72.                 }
  73.             }
  74.         }
  75.     }
  76. };
  77.  
  78. module.exports = roleHarvester;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement