<commit_before>import CreepWrapper, {CreepClass, CreepClassTypes, getCreepClass, BodyPart} from './wrappers/CreepWrapper';
import {Role} from './wrappers/CreepWrapper';
import {SPAWN_ERROR} from './constants';

type roleMap = { [key: string]: CreepWrapper[] };

class SpawnQueueItem implements ISpawnQueueItem {
    constructor(
        public classType: CreepClassTypes | number,
        public defaultRole: Role | number,
        public fallBack: ISpawnQueueItem[] = [],
    ) { }
}

function createCreep(spawn: Spawn, creepClass: CreepClass) {
    let creepResult: number | string = spawn.canCreateCreep(creepClass.body);
    if (creepResult === OK) {
        creepResult = spawn.createCreep(creepClass.body);
        console.log(creepResult);

        if (!isNaN(creepResult as number)) {
            console.log(SPAWN_ERROR[creepResult]);
            return creepResult;
        }

        Memory.totalCreepsAlive++;

        const creep = new CreepWrapper(Game.creeps[creepResult]);
        creep.assignRole(creepClass.role, true);

        return creepResult;
    }
    return creepResult;
}

function processWorkerCreationQueue(spawn: Spawn) {
    const spawnQueue = Memory.spawnQueue;
    if (spawnQueue.length === 0) {
        return;
    }
    const spawnQueueItem = spawnQueue[0];
    let result = createCreep(spawn, getCreepClass(spawnQueueItem.classType));
    if (result === OK) {
        Memory.spawnQueue.shift();
        return result;
    }
    for (const queueItem of spawnQueueItem.fallBack) {
        result = createCreep(spawn, getCreepClass(queueItem.classType));
        if (result === OK) {
            Memory.spawnQueue.shift();
            return result;
        }
    }
}

function enqueueWorkers(spawn: Spawn, creeps: CreepWrapper[]) {
    const standardWorkerBody = (BodyPart.WORK | BodyPart.CARRY | BodyPart.MOVE);
    const creepsWithStandardWorkerBodies = creeps.filter(creep => (creep.bodyFlags & standardWorkerBody) === standardWorkerBody);

    if (creepsWithStandardWorkerBodies.length > 4) {
        const carrierWorkerBody = (BodyPart.CARRY | BodyPart.MOVE);
        const creepsWithCarrierBodies = creeps.filter(creep =>
            // doesn't have WORK body
            ((creep.bodyFlags & carrierWorkerBody) === carrierWorkerBody) && ((creep.bodyFlags & BodyPart.WORK) === 0) );
        if (creepsWithCarrierBodies.length < 2 && Memory.spawnQueue.filter(x => x.defaultRole === Role.Carrier).length < 2) {
            Memory.spawnQueue.push(new SpawnQueueItem(CreepClassTypes.CarrierClass2, Role.Carrier, [ new SpawnQueueItem(CreepClassTypes.CarrierClass1, Role.Carrier)]));
        }

        const stationaryHarvesterBody = (BodyPart.WORK | BodyPart.MOVE);
        const creepsWithStationaryHarvesterBodies = creeps.filter(creep =>
            // doesn't have CARRY body
            ((creep.bodyFlags & stationaryHarvesterBody) === stationaryHarvesterBody) && ((creep.bodyFlags & BodyPart.CARRY) === 0));
        if (creepsWithStationaryHarvesterBodies.length < 2 && Memory.spawnQueue.filter(x => x.defaultRole === Role.Harvester).length < 2) {
            Memory.spawnQueue.push(new SpawnQueueItem(CreepClassTypes.HarvesterClass2, Role.Harvester, [ new SpawnQueueItem(CreepClassTypes.HarvesterClass1, Role.Harvester)]));
        }
        return;
    }

    Memory.spawnQueue.unshift(new SpawnQueueItem(CreepClassTypes.WorkerClass1, Role.Harvester));
}

export default class GameManager {

    public static loop() {

        console.log(`======= ${Game.time} =======`);

        const creeps = Object.values(Game.creeps).map(creep => new CreepWrapper(creep));

        Memory.totalCreepsAlive = Memory.totalCreepsAlive || creeps.length;
        Memory.spawnQueue = Memory.spawnQueue || [];

        for (const name in Memory.creeps) {
            if (!Game.creeps[name]) {
                console.log('deleting old creep data');
                Memory.totalCreepsAlive--;
                delete Memory.creeps[name];
            }
        }

        const spawn = Game.spawns['Spawn1'];

        const hostiles = spawn.room.find<Creep>(FIND_HOSTILE_CREEPS);

        let fightersNeeded = hostiles.length > 0 ? hostiles.length * 2 - creeps.filter(creep => creep.memory.currentRole === Role.Fighter).length : 0;

        if (fightersNeeded) {<commit_msg>prevent spawn queue from overflowing<commit_after>import CreepWrapper, {CreepClass, CreepClassTypes, getCreepClass, BodyPart} from './wrappers/CreepWrapper';
import {Role} from './wrappers/CreepWrapper';
import {SPAWN_ERROR} from './constants';

type roleMap = { [key: string]: CreepWrapper[] };

class SpawnQueueItem implements ISpawnQueueItem {
    constructor(
        public classType: CreepClassTypes | number,
        public defaultRole: Role | number,
        public fallBack: ISpawnQueueItem[] = [],
    ) { }
}

function createCreep(spawn: Spawn, creepClass: CreepClass) {
    let creepResult: number | string = spawn.canCreateCreep(creepClass.body);
    if (creepResult === OK) {
        creepResult = spawn.createCreep(creepClass.body);
        console.log(creepResult);

        if (!isNaN(creepResult as number)) {
            console.log(SPAWN_ERROR[creepResult]);
            return creepResult;
        }

        Memory.totalCreepsAlive++;

        const creep = new CreepWrapper(Game.creeps[creepResult]);
        creep.assignRole(creepClass.role, true);

        return creepResult;
    }
    return creepResult;
}

function processWorkerCreationQueue(spawn: Spawn) {
    const spawnQueue = Memory.spawnQueue;
    if (spawnQueue.length === 0) {
        return;
    }
    const spawnQueueItem = spawnQueue[0];
    let result = createCreep(spawn, getCreepClass(spawnQueueItem.classType));
    if (result === OK) {
        Memory.spawnQueue.shift();
        return result;
    }
    for (const queueItem of spawnQueueItem.fallBack) {
        result = createCreep(spawn, getCreepClass(queueItem.classType));
        if (result === OK) {
            Memory.spawnQueue.shift();
            return result;
        }
    }
}

function enqueueWorkers(spawn: Spawn, creeps: CreepWrapper[]) {
    const standardWorkerBody = (BodyPart.WORK | BodyPart.CARRY | BodyPart.MOVE);
    const creepsWithStandardWorkerBodies = creeps.filter(creep => (creep.bodyFlags & standardWorkerBody) === standardWorkerBody);

    if (creepsWithStandardWorkerBodies.length > 4) {
        const carrierWorkerBody = (BodyPart.CARRY | BodyPart.MOVE);
        const creepsWithCarrierBodies = creeps.filter(creep =>
            // doesn't have WORK body
            ((creep.bodyFlags & carrierWorkerBody) === carrierWorkerBody) && ((creep.bodyFlags & BodyPart.WORK) === 0) );
        if (creepsWithCarrierBodies.length < 2 && Memory.spawnQueue.filter(x => x.defaultRole === Role.Carrier).length < 2) {
            Memory.spawnQueue.push(new SpawnQueueItem(CreepClassTypes.CarrierClass2, Role.Carrier, [ new SpawnQueueItem(CreepClassTypes.CarrierClass1, Role.Carrier)]));
        }

        const stationaryHarvesterBody = (BodyPart.WORK | BodyPart.MOVE);
        const creepsWithStationaryHarvesterBodies = creeps.filter(creep =>
            // doesn't have CARRY body
            ((creep.bodyFlags & stationaryHarvesterBody) === stationaryHarvesterBody) && ((creep.bodyFlags & BodyPart.CARRY) === 0));
        if (creepsWithStationaryHarvesterBodies.length < 2 && Memory.spawnQueue.filter(x => x.defaultRole === Role.Harvester).length < 2) {
            Memory.spawnQueue.push(new SpawnQueueItem(CreepClassTypes.HarvesterClass2, Role.Harvester, [ new SpawnQueueItem(CreepClassTypes.HarvesterClass1, Role.Harvester)]));
        }
        return;
    }

    if (Memory.spawnQueue.filter(x => x.classType === CreepClassTypes.WorkerClass1).length === 0) {
        Memory.spawnQueue.unshift(new SpawnQueueItem(CreepClassTypes.WorkerClass1, Role.Harvester));
    }

    if (creepsWithStandardWorkerBodies.length < 4 &&
        ((Memory.spawnQueue.length > 0 && Memory.spawnQueue[0].classType !== CreepClassTypes.WorkerClass1) || Memory.spawnQueue.length === 0)
    ) {
        let index = Memory.spawnQueue.findIndex(x => x.classType !== CreepClassTypes.WorkerClass1);
        while (index > -1) {
            Memory.spawnQueue.splice(index, 1);
            index = Memory.spawnQueue.findIndex(x => x.classType !== CreepClassTypes.WorkerClass1);
        }
        Memory.spawnQueue.unshift(new SpawnQueueItem(CreepClassTypes.WorkerClass1, Role.Harvester));
    }
}

export default class GameManager {

    public static loop() {

        console.log(`======= ${Game.time} =======`);

        const creeps = Object.values(Game.creeps).map(creep => new CreepWrapper(creep));

        Memory.totalCreepsAlive = Memory.totalCreepsAlive || creeps.length;
        Memory.spawnQueue = Memory.spawnQueue || [];

        for (const name in Memory.creeps) {
            if (!Game.creeps[name]) {
                console.log('deleting old creep data');
                Memory.totalCreepsAlive--;
                delete Memory.creeps[name];
            }
        }

        const spawn = Game.spawns['Spawn1'];

        const hostiles = spawn.room.find<Creep>(FIND_HOSTILE_CREEPS);

        let fightersNeeded = hostiles.length > 0 ? hostiles.length * 2 - creeps.filter(creep => creep.memory.currentRole === Role.Fighter).length : 0;

        if (fightersNeeded) {
{'fraction_non_alphanumeric': 0.12150134048257373, 'fraction_numerical': 0.00514745308310992, 'mean_word_length': 3.1101806963420007, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 28, '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': '6156706', 'n_tokens_mistral': 2936, 'n_tokens_neox': 2788, 'n_words': 519}