<commit_before>/*
 * Module code goes here. Use 'module.exports' to export things:
 * module.exports = 'a thing';
 *
 * You can import it from another modules like this:
 * var mod = require('spawners'); // -> 'a thing'
 */
var generics = require('_generics');
var roles = require('roles');

function creepNameGenerator(prefix) {
    var name;
    do {
        name = prefix + ' ' + generics.generator();
    } while (name in Game.creeps);

    return name;
}

/*
 * Error codes:
 * -1: Creep not found
 * -2: Not enough energy to spawn creep
 */
function createCreep(spawn, role) {
    if (role in roles) {
        var body = roles[role].build();

        if (generics.getCreepCost(body) > spawn.energy) {
            return -2;
        }

        var name = creepNameGenerator(role);
        console.log('Spawner: Creating ' + role + ' ' + name);

        var result = spawn.createCreep(body, name, {role:role});

        if (result !== name) {
            console.log('Spawner: Spawn error: ' + result);
        }
    } else {
        return -1
    }
}

function spawner(spawn) {
    var result;
    var i;

    // First be spawn specific
    if (Memory.spawns[spawn.name]) {

        // If there is any creep in the priority queue, spawn or abord
        if (Memory.spawns[spawn.name].spawnPriorityQueue && Memory.spawns[spawn.name].spawnPriorityQueue.length > 0) {
            if (generics.getCreepCost(roles[Memory.spawns[spawn.name].spawnPriorityQueue[0]].build()) <= spawn.energy) {
                result = createCreep(spawn, Memory.spawns[spawn.name].spawnPriorityQueue[0]);

                if (result == undefined) {
                    Memory.spawns[spawn.name].spawnPriorityQueue.splice(0, 1);
                }
            }

            return;
        }

        // Find first creep possible to be created
        if (Memory.spawns[spawn.name].spawnQueue) {
            for (i = 0; i < Memory.spawns[spawn.name].spawnQueue.length; i++) {
                if (generics.getCreepCost(roles[Memory.spawns[spawn.name].spawnQueue[i]].build()) <= spawn.energy) {
                    result = createCreep(spawn, Memory.spawns[spawn.name].spawnQueue[i]);

                    if (result == undefined) {
                        Memory.spawns[spawn.name].spawnQueue.splice(i, 1);
                        return;
                    }
                }
            }
        }
    }

    // If there is any creep in the priority queue, spawn or abord
    if (Memory.spawnPriorityQueue.length > 0) {
        if (generics.getCreepCost(roles[Memory.spawnPriorityQueue[0]].build()) <= spawn.energy) {
            result = createCreep(spawn, Memory.spawnPriorityQueue[0]);

            if (result == undefined) {
                Memory.spawnPriorityQueue.splice(0, 1);
            }
        }

        return;
    }

    // Find first creep possible to be created
    for (i = 0; i < Memory.spawnQueue.length; i++) {
        if (generics.getCreepCost(roles[Memory.spawnQueue[i]].build()) <= spawn.energy) {
            result = createCreep(spawn, Memory.spawnQueue[i]);

            if (result == undefined) {
                Memory.spawnQueue.splice(i, 1);
                return;
            }
        }
    }

    // TODO Insert here what to spawn on default if wanted
    // createCreep(spawn, 'FOO');
}

module.exports = function(spawn) {

    var name;
    for (name in Game.spawns) {
        var spawn = Game.spawns[name];

        if (spawn.spawning == undefined) {
            spawner(spawn);
        }
    }
}<commit_msg>Unify spawn code<commit_after>/*
 * Module code goes here. Use 'module.exports' to export things:
 * module.exports = 'a thing';
 *
 * You can import it from another modules like this:
 * var mod = require('spawners'); // -> 'a thing'
 */
var generics = require('_generics');
var roles = require('roles');

function creepNameGenerator(prefix) {
    var name;
    do {
        name = prefix + ' ' + generics.generator();
    } while (name in Game.creeps);

    return name;
}

/*
 * Error codes:
 * -1: Creep not found
 * -2: Not enough energy to spawn creep
 */
function createCreep(spawn, role) {
    if (role in roles) {
        var body = roles[role].build();

        if (generics.getCreepCost(body) > spawn.energy) {
            return -2;
        }

        var name = creepNameGenerator(role);
        console.log('Spawner: Creating ' + role + ' ' + name);

        var result = spawn.createCreep(body, name, {role:role});

        if (result !== name) {
            console.log('Spawner: Spawn error: ' + result);
        }
    } else {
        return -1;
    }
}

/**
 * Attempts to spawn a creep from spawner
 *
 * Non-priority mode: spawns first possible creep
 * Priority mode: spawns first or fails
 *
 * @param spawn spawn
 * @param array queue
 * @param bool  priority
 *
 * Returns undefined when queue is empty
 * Returns true when a creep could be spawned
 * Returns false when no creep could be spawned
 */
function spawnAttempt(spawn, queue, priority) {
    if (!queue) return;

    for (var max = priority ? 1 : queue.length, i = 0, result; i < max; i++) {
        result = createCreep(spawn, queue[i]);

        if (result == undefined) {
            queue.splice(0, 1);
            return true;
        }
    }

    if (i === queue.length || (i > 0 && priority)) {
        return false;
    }
}

function spawner(spawn) {
    // Note: when priorityQueue has items and can't spawn, spawning ends immediately

    // Spawn specific
    if (Memory.spawns[spawn.name]) {
        if (spawnAttempt(spawn, Memory.spawns[spawn.name].spawnPriorityQueue, true) !== undefined) return;
        if (spawnAttempt(spawn, Memory.spawns[spawn.name].spawnQueue, false)) return;
    }

    // Global
    if (spawnAttempt(spawn, Memory.spawnPriorityQueue, true) !== undefined) return;
    if (spawnAttempt(spawn, Memory.spawnQueue, false)) return;

    // TODO Insert here what to spawn on default if wanted
    // createCreep(spawn, 'FOO');
}

module.exports = function(spawn) {

    var name;
    for (name in Game.spawns) {
        var spawn = Game.spawns[name];

        if (spawn.spawning == undefined) {
            spawner(spawn);
        }
    }
}
{'fraction_non_alphanumeric': 0.12895927601809956, 'fraction_numerical': 0.004363283775048481, 'mean_word_length': 2.1673490276356193, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, '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': '484694', 'n_tokens_mistral': 1971, 'n_tokens_neox': 1894, 'n_words': 496}