Repair (STRUCTURE_)ROAD



  • ###Code for repairing roads.
    improvements are welcomed

    module.exports = function (creep) {
    
    if(creep.energy == 0) {
            var spwn = creep.pos.findClosest(FIND_MY_SPAWNS);
            creep.moveTo(spwn);
                if((spwn) > [199]) {
                spwn.transferEnergy(creep);
                }}
        else{
    
    var SR = creep.pos.findClosest(FIND_STRUCTURES, {
                       filter: function(object){
                           if(object.structureType != STRUCTURE_ROAD ) {
                               return false;
                           }
                           if(object.hits > object.hitsMax / 3) {
                            return false;
                          }
                           return true;
                       } 
                    });     
        creep.moveTo(SR);
        creep.repair(SR);} 
        };
    


  • 1) use strict equality:
    === instead of == and !== instead of !=. This way you are avoiding type coercion
    2) whatever (spwn) > [199] is supposed to do, do the check the right way. I assume you want to check the energy level. The correct way would be: if( spwn.energy > 199)
    3) check return values, as they might give you hints, if something went wrong in your code
    var result = spwn.transferEnergy(creep);
    4) use properly named variables: spawn instead of spwn, roadsToRepair instead of SR. Also, naming convention suggests, that Classes be uppercase and variables are lowercase. This makes your code easier to read.
    5) improved filter: filter:function(o){ return o.structuretype === STRUCTURE_ROAD && (o.hits > o.hitsMax /3) }
    6) use correct intendation

    Altogether I would write it like:

    if(creep.energy === 0) {
        var spawn = creep.pos.findClosest(FIND_MY_SPAWNS);
        var moveResult = creep.moveTo(spawn);
        /*
          check moveResult here
        */
        if( spawn.energy > 199)) {
            var transferResult = spawn.transferEnergy(creep);
            /*
                check transferResult here
            */
    }
    else{
    
        var roadToRepair = creep.pos.findClosest(FIND_STRUCTURES, {
            filter: function(object){
                return object.structureType === STRUCTURE_ROAD && (object.hits > object.hitsMax / 3);
            } 
        });
    
        if (roadToRepair){
            creep.moveTo(roadToRepair);
            creep.repair(roadToRepair);
    
            // perhaps check the results again?
    
        } else {
    
            // nothing to repair, let's do something else?
    
        }
    } 
    


  • homework: spot all mismatching brackets (should be easy to spot with proper intendation)

    advanced homework: Cache your find results, as they consume a lot of cpu and should be computed only if really necessary (at most once per tick).



  • Hint: you can save a lot of movement ( and it adds up) if you check for arrival condition before issuing move command and start moving to target right after transfer

    For example:

    var isMoveRequired = false;
    var moveTarget = null;
    if(creep.energy === 0) {
    var spawn = creep.pos.findClosest(FINDMYSPAWNS);
    if (creep.pos.isNextTo(spawn)) {
    if( spawn.energy > 199)) {
    var transferResult = spawn.transferEnergy(creep);
    
    } else {
    isMoveRequired = true;
    moveTarget = spawn;
    }
    }
    if (creep.energy > 0) {
    ... similar code for repair
    }
    
    if (isMoveRequired)
    creep.moveTo(moveTarget)
    


  • Well i am still learning... tnx for the advice and suggestions for the code improvement.

    I was doing a quicky 😉 on SR (just sort for STRUCTURE_ROAD) it allows me to faster programming and i know what i mean ... when i make a not proper var. "of course when i 'll share the work next time i'll take a extra look".

    PS. it is a old bad habit for my (basic time) to even use single Capital letters as a variable "like if A = X then C+B=A end if just a sample plz dont kill me ;-)"

    Ohw how would this do on CPU movement control?

    if(!creep.pos.isNearTo(roadsToRepair)) {
    creep.moveTo(roadsToRepair);
    }