<commit_before>var _ = require('lodash');

module.exports = {

    /**
     * Determines whether a structure of the given type can be built in the given room
     * based on the structure type limitations tied to the room's controller level.
     *
     * @param {String} structureType One of the STRUCTURE_* constants
     * @param {Room} room The room where the check should be made
     * @returns {boolean} True, if structures of the given type can still legally
     * be built in the given room, false otherwise
     */
    canBuildStructure: function (structureType, room) {
        var roomLevel = (room.controller ? room.controller.level : 0) + '';
        var currentAmount = this.countStructures(room, structureType, true);
        return _.has(CONTROLLER_STRUCTURES, structureType) &&
                _.has(CONTROLLER_STRUCTURES[structureType], roomLevel) &&
                currentAmount <= CONTROLLER_STRUCTURES[structureType][roomLevel];
    },

    /**
     * Counts the amount of friendly creeps. The creeps that are included can
     * be narrowed down with the parameters.
     *
     * @param {Room} room If provided, only creeps in the given room are counted
     * @param {String|Array} roles The names of the roles the counted creeps must have,
     * or any falsy value to count all friendly creeps
     * @returns {int}
     */
    countCreeps: function (room = null, roles = []) {
        if (_.isString(roles)) {
            roles = [roles];
        }

        if (room instanceof Room) {
            return room.find(FIND_MY_CREEPS, {
                filter: creep => _.isEmpty(roles) || _.contains(roles, creep.memory.role)
            }).length;
        } else {
            return _.filter(Game.creeps, creep => _.isEmpty(roles) || _.contains(roles, creep.memory.role)).length;
        }
    },

    /**
     * Counts the amount of structures (and possibly construction sites) of the given type
     * in the given room.
     *
     * @param {Room} room The room whose structures to count<commit_msg>Fixed a bug where utils.canBuildStructure() falsely reported that a structure could be built even though the maximum amount was already reached<commit_after>var _ = require('lodash');

module.exports = {

    /**
     * Determines whether a structure of the given type can be built in the given room
     * based on the structure type limitations tied to the room's controller level.
     *
     * @param {String} structureType One of the STRUCTURE_* constants
     * @param {Room} room The room where the check should be made
     * @returns {boolean} True, if structures of the given type can still legally
     * be built in the given room, false otherwise
     */
    canBuildStructure: function (structureType, room) {
        var roomLevel = (room.controller ? room.controller.level : 0) + '';
        var currentAmount = this.countStructures(room, structureType, true);
        return _.has(CONTROLLER_STRUCTURES, structureType) &&
                _.has(CONTROLLER_STRUCTURES[structureType], roomLevel) &&
                currentAmount < CONTROLLER_STRUCTURES[structureType][roomLevel];
    },

    /**
     * Counts the amount of friendly creeps. The creeps that are included can
     * be narrowed down with the parameters.
     *
     * @param {Room} room If provided, only creeps in the given room are counted
     * @param {String|Array} roles The names of the roles the counted creeps must have,
     * or any falsy value to count all friendly creeps
     * @returns {int}
     */
    countCreeps: function (room = null, roles = []) {
        if (_.isString(roles)) {
            roles = [roles];
        }

        if (room instanceof Room) {
            return room.find(FIND_MY_CREEPS, {
                filter: creep => _.isEmpty(roles) || _.contains(roles, creep.memory.role)
            }).length;
        } else {
            return _.filter(Game.creeps, creep => _.isEmpty(roles) || _.contains(roles, creep.memory.role)).length;
        }
    },

    /**
     * Counts the amount of structures (and possibly construction sites) of the given type
     * in the given room.
     *
     * @param {Room} room The room whose structures to count
{'fraction_non_alphanumeric': 0.09305654974946313, 'fraction_numerical': 0.0004772130756382725, 'mean_word_length': 2.5706984667802386, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '4957886', 'n_tokens_mistral': 1145, 'n_tokens_neox': 1114, 'n_words': 417}