Advertisement
Guest User

For You Elite

a guest
Nov 19th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module.exports = {
  2.     // a function to run the logic for this role
  3.     run: function(creep) {
  4.         // if creep is bringing energy to a structure but has no energy left
  5.         if (creep.memory.working == true && creep.carry.energy == 0) {
  6.             // switch state
  7.             creep.memory.working = false;
  8.         }
  9.         // if creep is harvesting energy but is full
  10.         else if (creep.memory.working == false && creep.carry.energy == creep.carryCapacity) {
  11.             // switch state
  12.             creep.memory.working = true;
  13.         }
  14.  
  15.         // if creep is supposed to transfer energy to a structure
  16.         if (creep.memory.working == true) {
  17.             // if in home room
  18.             if (creep.room.name == creep.memory.home) {
  19.                 // find closest spawn, extension or tower which is not full
  20.                 var structure = creep.pos.findClosestByPath(FIND_MY_STRUCTURES, {
  21.                     // the second argument for findClosestByPath is an object which takes
  22.                     // a property called filter which can be a function
  23.                     // we use the arrow operator to define it
  24.                     filter: (s) => (s.structureType == STRUCTURE_SPAWN
  25.                                  || s.structureType == STRUCTURE_EXTENSION
  26.                                  || s.structureType == STRUCTURE_TOWER)
  27.                                  && s.energy < s.energyCapacity
  28.                 });
  29.  
  30.                 // if we found one
  31.                 if (structure != undefined) {
  32.                     // try to transfer energy, if it is not in range
  33.                     if (creep.transfer(structure, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
  34.                         // move towards it
  35.                         creep.moveTo(structure);
  36.                     }
  37.                 }
  38.             }
  39.             else {
  40.                 if (creep.room.name == Game.rooms.W10N23) {
  41.                   var homeW10N23 = creep.room.findExitTo(creep.memory.home);
  42.                   creep.moveTo(creep.pos.findClosestByRange(homeW10N23));
  43.                 }
  44.                 else if (creep.room.name == Game.Rooms.W10N22) {
  45.                   var homeW10N22 = creep.room.findExitTo(Game.rooms.W10N23);
  46.                   creep.moveTo(creep.pos.findClosestByRange(homeW10N22));
  47.                 }
  48.                 else if (creep.room.name == Game.Rooms.W11N22) {
  49.                   var homeW11N22 = creep.room.findExitTo(Game.rooms.W10N22);
  50.                   creep.moveTo(creep.pos.findClosestByRange(homeW11N22));
  51.                 }
  52.                 else {
  53.                   console.log("I am lost" + creep.room.name);
  54.                 }
  55.             }
  56.         }
  57.         else {
  58.             if (creep.room.name == creep.memory.target) {
  59.                 var source = creep.room.find(FIND_SOURCES)[creep.memory.sourceIndex];
  60.                 if (creep.harvest(source) == ERR_NOT_IN_RANGE) {
  61.                     creep.moveTo(source);
  62.                 }
  63.             }
  64.             else {
  65.               if (creep.room.name == creep.memory.home) {
  66.                 var workW10N23 = creep.room.findExitTo(Game.rooms.W10N23);
  67.                 creep.moveTo(creep.pos.findClosestByRange(workW10N23));
  68.               }
  69.                 else if (creep.room.name == Game.Rooms.W10N23) {
  70.                   var workW10N22 = creep.room.findExitTo(Game.rooms.W10N22);
  71.                   creep.moveTo(creep.pos.findClosestByRange(workW10N22));
  72.                 }
  73.                 else if (creep.room.name == Game.rooms.W10N22) {
  74.                   var target = creep.room.findExitTo(creep.memory.target);
  75.                   creep.moveTo(creep.pos.findClosestByRange(target));
  76.                 }
  77.                 else {
  78.                   console.log("I am lost" + creep.room.name);
  79.                 }
  80.             }
  81.         }
  82.     }
  83. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement