Navigation

    forum

    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Users
    • Groups
    1. Home
    2. AnonymouSJS
    • Flag Profile
    • block_user
    • Profile
    • Following
    • Followers
    • Topics
    • Posts
    • Groups
    • Blog

    AnonymouSJS

    @AnonymouSJS

    1
    Posts
    746
    Profile views
    0
    Followers
    0
    Following
    Joined Last Online

    AnonymouSJS Follow

    Posts made by AnonymouSJS

    • Nooby issues trying to get my creeps to move by a path written to memory

      Hey all,

      Forgive me for my nooby question. I'm trying to define a path from my spawn to a source in an attempt to conserve cpu. I'm not getting an error, however my screeps are not moving. This is my first script im trying to write outside of the tutorial, so forgive me as im sure there are a bunch of nooby problems with it. Idk if I can call a creep to "moveByPath" the way I am. Or do I need to serialize the path first? I'm very sorry if this is a nooby question, if I need to give more detail just let me know! 🙂

      module.exports.loop = function () {
      var harvesterId = _.filter(Game.creeps, (creep) => creep.memory.role == 'drone'); 
      var harvesterRun = require('harvesters'); 
      var controllerLevel = StructureController.level;
      var newName = Game.time;
      var spawn1 =  Game.spawns['Spawn1']
      
      //Find sources from spawn, to be written into the memory of creeps later
      var sources = Game.spawns['Spawn1'].room.find(FIND_SOURCES);
      var source1 = sources[1].id; // Create two variables from the list "sources" defined above
      var source2 = sources[2].id; 
      
      //create flags and then path to them from spawn. then serialize into string that can be stored into memory
      spawn1.room.createFlag(sources[1].pos, 's1');
      spawn1.room.createFlag(sources[2].pos, 's2');
      const sourceFlag1 = Game.flags.s1;
      const sourceFlag2 = Game.flags.s2;
      const sourcePath1 = spawn1.pos.findPathTo(sourceFlag1);
      const sourcePath2 = spawn1.pos.findPathTo(sourceFlag2);
      //Opposite of above, searlize path from source to spawn to conserve cpu
      spawn1.room.createFlag(spawn1.pos, 'sp1');
      const spawnFlag1 = Game.flags.sp1;
      const spawnPathFromSource1 = sourceFlag1.pos.findPathTo(spawnFlag1);
      const spawnPathFromSource2 = sourceFlag2.pos.findPathTo(spawnFlag1);
      
      //Spawns
      if(harvesterId.length < 4) {
          spawn1.spawnCreep([WORK,CARRY,MOVE], newName, {memory: {role: 'drone', sp1: sourcePath1, sp2: sourcePath2, spawnPath1: spawnPathFromSource1, spawnPath2: spawnPathFromSource2, mine1: source1, mine2: source2, creepName: newName}}); //Create a creep if there arent enough. role "drone" with two sources defined as "mines" written to memory        
      };
      
      
      
      for(var name in Game.creeps) {
          var creep = Game.creeps[name];
          if(creep.memory.role == 'drone') {
              harvesterRun.run(creep);
          }
      }
      

      };

      (Below is a second document running on creeps with the "harvester" role in their memory)

      var roleHarvester = {

      /** @param {Creep} creep **/
      run: function(creep) {
          var freeCap = creep.store.getFreeCapacity(RESOURCE_ENERGY);
          var totalCap = creep.store.getCapacity;
          //var roomSpawn = creep.room.find(FIND_MY_STRUCTURES, { filter: { structureType: STRUCTURE_SPAWN }})
          var roomSpawn =  Game.spawns['Spawn1'] //Gross workaround as the code above doesnt work. Ideally in the future it isn't identified by name but rather but something like above. Maybe it can even be written to memory?
          var mine1 = Game.getObjectById(creep.memory.mine1);
          console.log(creep.memory.creepName+' '+freeCap)
          console.log('completely open?'+freeCap == totalCap)
          if(freeCap == 0){
              if(creep.transfer(roomSpawn, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
                  creep.moveTo(roomSpawn)
              }
          }
          if(freeCap != 0){
              if(creep.harvest(mine1) == ERR_NOT_IN_RANGE) {
                  creep.moveByPath(Memory.sourcePath1)
              }
          }
      }
      

      };

      module.exports = roleHarvester;

      posted in Help
      AnonymouSJS