Harvester Roll Help



  • Quite a noob question im sure. Im new to coding, little experience in C++ (Arduino hobby) and so far have created this script, however, i would like my creep to fill up its inventory, then go to the controller and empty it completely. Ive tried everything my small brain can muster but cant figure out how. Would love any recommendations!

    module.exports.loop = function () {

    Game.spawns['Spawn1'].spawnCreep([WORK, CARRY, MOVE, MOVE], 'testCreep');

    var mycreep = Game.creeps['testCreep'];

    if (mycreep.store.getFreeCapacity() > 0) {

    var energysource = mycreep.pos.findClosestByPath(FIND_SOURCES); mycreep.moveTo(energysource); mycreep.harvest(energysource);

    } else { var homecontroller = mycreep.room.controller; mycreep.moveTo(homecontroller); mycreep.upgradeController(homecontroller); }

    }



  • You can use a state which only changes when the creep is either completely full, or completely empty.

    eg. (completely untested)

    module.exports.loop = function () {
      Game.spawns['Spawn1'].spawnCreep([WORK, CARRY, MOVE, MOVE], 'testCreep');
      var mycreep = Game.creeps['testCreep'];
    
      if (mycreep.store.getFreeCapacity() === 0) {
        mycreep.memory.state = 'UPGRADE';
      }
      else if (mycreep.store.getUsedCapacity() === 0) {
        mycreep.memory.state = 'HARVEST';
      }
    
      if (mycreep.memory.state === 'HARVEST') {
        var energysource = mycreep.pos.findClosestByPath(FIND_SOURCES);
        mycreep.moveTo(energysource); 
        mycreep.harvest(energysource);
      }
      else {
        var homecontroller = mycreep.room.controller;
        mycreep.moveTo(homecontroller); 
        mycreep.upgradeController(homecontroller);
      }
    }
    

    Also, you'll get a much quicker response from the #help channel of Slack (https://screeps.slack.com/). 🙂



  • @threen aha, okay yeah that makes sense, my next guess was going to be something along the lines of booleans. however as i'm finding out, they work a little differently then they do in C++ lol. thanks for the help! Ill see what I can do with this.



  • @threen said in Harvester Roll Help:

    You can use a state which only changes when the creep is either completely full, or completely empty.

    eg. (completely untested)

    module.exports.loop = function () {
      Game.spawns['Spawn1'].spawnCreep([WORK, CARRY, MOVE, MOVE], 'testCreep');
      var mycreep = Game.creeps['testCreep'];
    
      if (mycreep.store.getFreeCapacity() === 0) {
        mycreep.memory.state = 'UPGRADE';
      }
      else if (mycreep.store.getUsedCapacity() === 0) {
        mycreep.memory.state = 'HARVEST';
      }
    
      if (mycreep.memory.state === 'HARVEST') {
        var energysource = mycreep.pos.findClosestByPath(FIND_SOURCES);
        mycreep.moveTo(energysource); 
        mycreep.harvest(energysource);
      }
      else {
        var homecontroller = mycreep.room.controller;
        mycreep.moveTo(homecontroller); 
        mycreep.upgradeController(homecontroller);
      }
    }
    

    Also, you'll get a much quicker response from the #help channel of Slack (https://screeps.slack.com/). 🙂

    Is actually an interesting way, never thought.🎓

    @detox said in Transfer energy from spawn to controller:

    let creep = _.filter(Game.creeps, function(c){ return c.getActiveBodyparts(WORK) > 0 && c.getActiveBodyparts(CARRY) > 0; })[0];
    let source = creep.room.find(FIND_SOURCES)[0];
    if(creep.store.getFreeCapacity() == 0) creep.memory.storing = true;
    if(creep.store.getUsedCapacity() == 0) creep.memory.storing = false;
    if(creep.memory.storing){
       if(creep.pos.getRangeTo(creep.room.controller) <= 3) creep.upgradeController(creep.room.controller); else creep.moveTo(creep.room.controller);
    } else {
       if(creep.pos.getRangeTo(source) == 1) creep.harvest(source); else creep.moveTo(source);
    }
    

    I think.