<commit_before>var roleBuilder = {

    /** @param {Creep} creep **/
    run: function(creep, source) {

    	setBuildFlagForCreep(creep);

	    if(creep.memory.building) {
	        var targets = creep.room.find(FIND_CONSTRUCTION_SITES);
            if(targets.length) {
                if(creep.build(targets[0]) == ERR_NOT_IN_RANGE) {
                    creep.moveTo(targets[0]);
                }
            }else{
            	var structures = getTargetsForRepair(creep);
            	
    			for (var i=0;i<structures.length;i++){
    				var item = structures[i];	
    				if (!item){
    					continue;
    				}

    				if (!creep.pos.isNearTo(item)) {
    					creep.moveTo(item);
    				} else {
    					creep.repair(item)
    				}
    			}
            }
	    }
	    else {	     
            if(creep.harvest(source) == ERR_NOT_IN_RANGE) {
                creep.moveTo(source);
            }
	    }
	}
};

function setBuildFlagForCreep(creep){	
    
    if(creep.memory.building && creep.carry.energy == 0) {
        creep.memory.building = false;
    }
    if(!creep.memory.building && creep.carry.energy == creep.carryCapacity) {
        creep.memory.building = true;
    }

}


function getTargetsForRepair(creep){	
	 if (creep.memory.targetsForRepair){
    	for (var i ; i<creep.memory.targetsForRepair.length;i++){
    		if (item[i].hits==item[i].hitsMax){
    			delete creep.memory.targetsForRepair[i];   
    		}
    	}
	 }
		
	if (!creep.memory.targetsForRepair){
		creep.memory.targetsForRepair = creep.room.find(FIND_STRUCTURES, {
	        filter: (structure) => {
	            return (structure.structureType === STRUCTURE_EXTENSION ||
	                    structure.structureType === STRUCTURE_SPAWN ||
	                    structure.structureType === STRUCTURE_TOWER ||
	                    ((structure.structureType ===  STRUCTURE_ROAD) && (structure.hits < (structure.hitsMax/4)))||    
	                    structure.structureType ===  STRUCTURE_WALL ||
	                    structure.structureType ===  STRUCTURE_RAMPART) && (structure.hits < structure.hitsMax);
	        }
		});
	}else{
		return creep.memory.targetsForRepair;
	}				
}

module.exports = roleBuilder;<commit_msg>Bugfix. Cache the targets for repair.<commit_after>var roleBuilder = {

    /** @param {Creep} creep **/
    run: function(creep, source) {

    	setBuildFlagForCreep(creep);

	    if(creep.memory.building) {
	        var targets = creep.room.find(FIND_CONSTRUCTION_SITES);
            if(targets.length) {
                if(creep.build(targets[0]) == ERR_NOT_IN_RANGE) {
                    creep.moveTo(targets[0]);
                }
            }else{
            	var structures = getTargetsForRepair(creep);
            	
    			for (var i=0;i<structures.length;i++){
    				var item = structures[i];	
    				if (!item){
    					continue;
    				}

    				if (!creep.pos.isNearTo(item)) {
    					creep.moveTo(item);
    				} else {
    					creep.repair(item)
    				}
    			}
            }
	    }
	    else {	     
            if(creep.harvest(source) == ERR_NOT_IN_RANGE) {
                creep.moveTo(source);
            }
	    }
	}
};

function setBuildFlagForCreep(creep){	
    
    if(creep.memory.building && creep.carry.energy == 0) {
        creep.memory.building = false;
    }
    if(!creep.memory.building && creep.carry.energy == creep.carryCapacity) {
        creep.memory.building = true;
    }

}


function getTargetsForRepair(creep){	
	
	if ((!creep.memory.targetsForRepair)||(creep.memory.targetsForRepair.length===0)){
		creep.memory.targetsForRepair = creep.room.find(FIND_STRUCTURES, {
	        filter: (structure) => {
	            return (structure.structureType === STRUCTURE_EXTENSION ||
	                    structure.structureType === STRUCTURE_SPAWN ||
	                    structure.structureType === STRUCTURE_TOWER ||
	                    ((structure.structureType ===  STRUCTURE_ROAD) && (structure.hits < (structure.hitsMax/4)))||    
	                    structure.structureType ===  STRUCTURE_WALL ||
	                    structure.structureType ===  STRUCTURE_RAMPART) && (structure.hits < structure.hitsMax);
	        }
		});
		return creep.memory.targetsForRepair;
	}
			
	for (var i ; i<creep.memory.targetsForRepair.length;i++){
		if (item[i].hits==item[i].hitsMax){
			creep.memory.targetsForRepair.splice[i];
		}
	}
	
	return creep.memory.targetsForRepair;
 }
		

module.exports = roleBuilder;
{'fraction_non_alphanumeric': 0.13073651220058205, 'fraction_numerical': 0.0024625027982986343, 'mean_word_length': 2.0006715916722633, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4844134', 'n_tokens_mistral': 1622, 'n_tokens_neox': 1469, 'n_words': 184}