<commit_before>let mod = new ActionObj('FromContainer');
module.exports = mod;

const targetInitFunc = function(creep) {
    const need = creep.carryCapacity - creep.carry.energy;
    const role = creep.memory.role;
    const room = creep.room;
    if(role === C.UPGRADER) {
        if(room.controller.container && room.controller.container.store[RESOURCE_ENERGY]>need) {
            return room.controller.container;
        } else {
            return false;
        }
    }
    //Find source container
    let suitableContainer = false;;
    for(let source of room.sources) {
        const container = source.container || false;
        if(container) {
            if(!suitableContainer || container.store[RESOURCE_ENERGY] > suitableContainer.store[RESOURCE_ENERGY]) suitableContainer = container;
        }
    }
    if(suitableContainer && suitableContainer.store[RESOURCE_ENERGY]>0) return suitableContainer;

    //Find mineral container
    if(role === C.HAULER || role === C.REMOTE_HAULER) {
        const mineral = room.mineral;
        if(mineral && mineral.container && _.sum(mineral.container.store)>=need) {
            return mineral.container;
        }
    }

    return false;
};

mod.nextTarget = function() {
    return Util.Mark.handleMark(this.creep, targetInitFunc, this.actionName, validateFunc);
};

const validateFunc = function(creep, target) {
    return target.room && target.room.name===creep.room.name && _.sum(target.store) > 0;
};

mod.word = '⬅︎Container';

mod.loop = function(creep) {
    return this.loop0(creep, (creep, target) => {
        let result = OK;
        if(creep.memory.role===C.HAULER || creep.memory.role===C.REMOTE_HAULER) {
            //Only haulers are allowed to get resources other than energy
            for(const resourceType in target.store) {
                result = creep.withdraw(target, resourceType);
            }
        } else {
            result = creep.withdraw(target, RESOURCE_ENERGY);
        }
        if(result === ERR_NOT_IN_RANGE) {
            creep.moveTo(target, {range: 1, maxRooms: 1, visualizePathStyle: {stroke: '#ffaa00'}});
        } else if(result === OK) {
            Util.Mark.unmarkTarget(creep, this.actionName);
        }
    });
};<commit_msg>hauler prefer mineral container; only go and withdraw from container which contains enough resources<commit_after>let mod = new ActionObj('FromContainer');
module.exports = mod;

const targetInitFunc = function(creep) {
    const need = creep.carryCapacity - _.sum(creep.carry);
    const role = creep.memory.role;
    const room = creep.room;
    if(role === C.UPGRADER) {
        if(room.controller.container && room.controller.container.store[RESOURCE_ENERGY]>need) {
            return room.controller.container;
        } else {
            return false;
        }
    }

    //Find mineral container
    if(role === C.HAULER || role === C.REMOTE_HAULER) {
        const mineral = room.mineral;
        if(mineral && mineral.container && _.sum(mineral.container.store)>=need) {
            return mineral.container;
        }
    }

    //Find source container
    let suitableContainer = false;;
    for(let source of room.sources) {
        const container = source.container || false;
        if(container) {
            if(!suitableContainer || container.store[RESOURCE_ENERGY] > suitableContainer.store[RESOURCE_ENERGY]) suitableContainer = container;
        }
    }
    if(suitableContainer && suitableContainer.store[RESOURCE_ENERGY]>need) return suitableContainer;

    return false;
};

mod.nextTarget = function() {
    return Util.Mark.handleMark(this.creep, targetInitFunc, this.actionName, validateFunc);
};

const validateFunc = function(creep, target) {
    const need = creep.carryCapacity - _.sum(creep.carry);
    return target.room && target.room.name===creep.room.name && _.sum(target.store) > need;
};

mod.word = '⬅︎Container';

mod.loop = function(creep) {
    return this.loop0(creep, (creep, target) => {
        let result = OK;
        if(creep.memory.role===C.HAULER || creep.memory.role===C.REMOTE_HAULER) {
            //Only haulers are allowed to get resources other than energy
            for(const resourceType in target.store) {
                result = creep.withdraw(target, resourceType);
            }
        } else {
            result = creep.withdraw(target, RESOURCE_ENERGY);
        }
        if(result === ERR_NOT_IN_RANGE) {
            creep.moveTo(target, {range: 1, maxRooms: 1, visualizePathStyle: {stroke: '#ffaa00'}});
        } else if(result === OK) {
            Util.Mark.unmarkTarget(creep, this.actionName);
        }
    });
};
{'fraction_non_alphanumeric': 0.1262135922330097, 'fraction_numerical': 0.0025889967637540453, 'mean_word_length': 2.7177225340817963, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '6617041', 'n_tokens_mistral': 1408, 'n_tokens_neox': 1367, 'n_words': 285}