I need help with a few things



  • Hey guys, I really like the game, but I've got a few problems I need some help with. First of all, my harvesters are just using one of my sources (my room has 2) and I can't get them to use both. this is my code for that: 

    [code] 

    if(creep.carry.energy < creep.carryCapacity) {
    var sources = creep.room.find(FIND_SOURCES_ACTIVE);
    if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
    creep.moveTo(sources[0]);
    }
    }

     [/code]

    also, in my last room I got attacked by another player, and my tower didn't attack at all. after watching it back (i wasn't there at the time), I noticed, that it didn't repair my roads and stuff anymore, either. It just stopped working for some reason. This is my tower code: 

      [code]

    var tower = Game.getObjectById('577ddb750cb165f62157b3db');
    if(tower) {
    var closestDamagedWall = tower.pos.findClosestByRange(FIND_STRUCTURES, {
    filter: (structure) => structure.hits < structure.hitsMax / 3000 && structure.structureType == STRUCTURE_WALL || structure.structureType == STRUCTURE_CONTAINER || structure.structureType == STRUCTURE_RAMPART
    });
    var closestDamagedRoad = tower.pos.findClosestByRange(FIND_STRUCTURES, {
    filter: (structure) => structure.hits < structure.hitsMax && structure.structureType == STRUCTURE_ROAD
    });
    if(closestDamagedWall) {
    tower.repair(closestDamagedWall);
    }
    else {
    tower.repair(closestDamagedRoad);
    }
    var closestHostile = tower.pos.findClosestByRange(FIND_HOSTILE_CREEPS);
    if(closestHostile) {
    tower.attack(closestHostile);
    }
    }

      [/code]

    I checked the ID, it was definitely correct. I don't see why this isn't working, please help 😃



  • for sources you'll probably want to use creep.harvest(sources[1]) somehow, you would need code to segment your workers between the two sources, usually easiest done with creep.memory.target = sources[1] or something similar.

    for the towers i'm not sure, if it's repairing correctly it's possibly working correctly now - it could've been out of energy or more likely your code crashed due to a bug somewhere in the code. one thing i will say is that the tower will possibly be repairing instead of attacking because it checks for hostiles after it determines if it needs to repair anything.



  • Hi there,

    I just had the Idea, if it realy prefers repairing, and you have a Rampart on the same spot of the Tower, he will repair this first to the Max. And you will not see any visual effect for this, except decreasing Energy.

    But its just an idea



  • Thank you guys, I got it to work now. Maybe I just didn't see it repairing beacause I just watched the replay back, I wasn't actually there when it happened. Anyway, I put the tower code right after the memory clearing in the main loop, and I made it so that it checks for hostiles before it starts repairing. Also, my builders are now repairing as well, when they don't have anything to build.

    For the problem with the 2 sources, I just made it so my building guy Uses one of them, which only has 1 spot anyway. Not sure how I'll solve that with multiple rooms though, but I think that's some time away anyway 😃 If you have any tips on managing harvesters in a second room though, feel free to share them 😃



  • For sources Question:

    In an early edition of my scripts I used:

    var source = creep.pos.findClosestByPath(FIND_SOURCES_ACTIVE)

    Then I added some logic when I noticed they were clustering around one and waiting

    function rememberClosestActiveSource(creep) {
    if(!creep.memory.sourceId ||
    Game.getObjectById(creep.memory.sourceId).energy == 0) {
    var source = creep.pos.findClosestByPath(FIND_SOURCES_ACTIVE, {
    filter: (source) => {
    creep.moveTo(source) != ERR_NO_PATH
    }
    })

    if (source) {
    creep.memory.sourceId = source.id
    }
    }
    }


    What I have now is classified, but the gist is this:

    When the creep is assigned a new job (like repairing a wall, or filling up energy storage) he find the source nearest to that target, stores the id and the path in his own memory. and does the job until its complete .. then grabs another job and repeats.  Lots of little tweaks to save CPU and mem .. but thats the basics