<commit_before>var mod = {};
module.exports = mod;

mod.loop = function() {
    const flags = _.filter(Game.flags, o => FlagUtil.remoteMining.examine(o));
    for(const flag of flags) {
        //Extract flag data
        const destinedTarget = flag.name;
        let assignedRoom = flag.memory.assignedRoom;
        if(assignedRoom === undefined) {
            //Find suitable room to do this task
            //NOTE: Currently we only calculate range
            let range = Infinity;
            let selectedRoomName;
            for(const roomName in Game.rooms) {
                const room = Game.rooms[roomName];
                const roomType = room.memory.roomType;
                if(roomType && roomType === C.OWNED_ROOM && room.controller.level>=2) {
                    const r = Game.map.getRoomLinearDistance(flag.pos.roomName, roomName);
                    if(r < range) {
                        range = r;
                        selectedRoomName = roomName;
                    }
                }
            }
            assignedRoom = selectedRoomName;
            console.log(`Detect new remote mining flag => ${flag.name} and assigned to [${assignedRoom}]`);
        }
        //Do we have a assignedRoom now?
        if(assignedRoom) {
            //Write back to memory
            flag.memory.assignedRoom = assignedRoom;
            //Enqueue remote-mining task related creeps
            this.queueCreeps(assignedRoom, destinedTarget);
        }
    }
};

mod.queueCreeps = function(roomName, destinedTarget) {
    //=== Role count ===
    const roomCreeps = _.filter(Game.creeps, function(creep) { return creep.memory.destinedTarget === destinedTarget; });
    let cnt = {
        total: 0,
    };
    _.forEach(Role, o => cnt[o.roleName] = 0); //Init cnt
    //All role is required
    for(let creep of roomCreeps) {
        const role = creep.memory.role;
        cnt[role]++;
        cnt.total++;
    }
    //Count role in queue as well
    const queueRoom = Game.rooms[roomName];
    const queue = queueRoom.queue.extern;
    for(const setupArr of queue) {
        const setupName = setupArr[0];
        const extraMemory = setupArr[1];
        if(extraMemory.destinedTarget === destinedTarget) {
            const role = setupName;
            cnt[role]++;
            cnt.total++;
        }
    }
    
    //Prepare objects needed
    const extraMemory = {
        destinedTarget: destinedTarget
    };

    if(queueRoom.controller.level < 4 || !queueRoom.storage) {
        //Can't afford too many creeps
        // sent remoteWorkers
        let room = Game.rooms[Game.flags[destinedTarget].pos.roomName];
        if(room === undefined) {
            //No vision
            //Spawn a scout first
            
            if(cnt[C.SCOUT]===0) {
                queueRoom.queue.extern.push([C.SCOUT, extraMemory]);
                cnt[C.SCOUT]++;
            }
        } else {
            //=== Harvest all sources and spawn dedicated hauler ===
            let needWorker = room.sources.length*2 - cnt[C.REMOTE_WORKER];
            //Actually enqueue harvesters and haulers
            while(needWorker-- > 0) {
                queueRoom.queue.extern.push([C.REMOTE_WORKER, extraMemory]);
                cnt[C.REMOTE_WORKER]++;
            }
        }

        return;
    }

    //Do we need to spawn guardian ?
    //Guardian provide vision too
    if(cnt[C.REMOTE_GUARDIAN]===0) {
        queueRoom.queue.extern.unshift([C.REMOTE_GUARDIAN, extraMemory]);
        cnt[C.REMOTE_GUARDIAN]++;
        return;
    }
    //TODO normally send scout to provide vision
    //   only sent defense when
    //Do we have vision of that room?
    let room = Game.rooms[Game.flags[destinedTarget].pos.roomName];
    if(room === undefined) {
        //No vision
        //Spawn a scout first
        /*
        if(cnt[C.SCOUT]===0) {
            queueRoom.queue.extern.push([C.SCOUT, extraMemory]);
            cnt[C.SCOUT]++;
        }
        */
        return;
    }

    //=== Keep room reserved ===
    const controller = room.controller;
    if(controller) {
        const reservation = controller.reservation;
        if(reservation && reservation.username===C.USERNAME && reservation.ticksToEnd>1000) {
            //No need to spawn reserver
        } else if(cnt[C.RESERVER]===0) {
            queueRoom.queue.extern.push([C.RESERVER, extraMemory]);
            cnt[C.RESERVER]++;
        }
    }
<commit_msg>only sent guardian when the target room is under attack<commit_after>var mod = {};
module.exports = mod;

mod.loop = function() {
    const flags = _.filter(Game.flags, o => FlagUtil.remoteMining.examine(o));
    for(const flag of flags) {
        //Extract flag data
        const destinedTarget = flag.name;
        let assignedRoom = flag.memory.assignedRoom;
        if(assignedRoom === undefined) {
            //Find suitable room to do this task
            //NOTE: Currently we only calculate range
            let range = Infinity;
            let selectedRoomName;
            for(const roomName in Game.rooms) {
                const room = Game.rooms[roomName];
                const roomType = room.memory.roomType;
                if(roomType && roomType === C.OWNED_ROOM && room.controller.level>=2) {
                    const r = Game.map.getRoomLinearDistance(flag.pos.roomName, roomName);
                    if(r < range) {
                        range = r;
                        selectedRoomName = roomName;
                    }
                }
            }
            assignedRoom = selectedRoomName;
            console.log(`Detect new remote mining flag => ${flag.name} and assigned to [${assignedRoom}]`);
        }
        //Do we have a assignedRoom now?
        if(assignedRoom) {
            //Write back to memory
            flag.memory.assignedRoom = assignedRoom;
            //Enqueue remote-mining task related creeps
            this.queueCreeps(assignedRoom, destinedTarget);
        }
    }
};

mod.queueCreeps = function(roomName, destinedTarget) {
    //=== Role count ===
    const roomCreeps = _.filter(Game.creeps, function(creep) { return creep.memory.destinedTarget === destinedTarget; });
    let cnt = {
        total: 0,
    };
    _.forEach(Role, o => cnt[o.roleName] = 0); //Init cnt
    //All role is required
    for(let creep of roomCreeps) {
        const role = creep.memory.role;
        cnt[role]++;
        cnt.total++;
    }
    //Count role in queue as well
    const queueRoom = Game.rooms[roomName];
    const queue = queueRoom.queue.extern;
    for(const setupArr of queue) {
        const setupName = setupArr[0];
        const extraMemory = setupArr[1];
        if(extraMemory.destinedTarget === destinedTarget) {
            const role = setupName;
            cnt[role]++;
            cnt.total++;
        }
    }
    
    //Prepare objects needed
    const extraMemory = {
        destinedTarget: destinedTarget
    };
    const targetRoomName = Game.flags[destinedTarget].pos.roomName;
    const room = Game.rooms[targetRoomName];

    if(Memory.rooms[targetRoomName].underAttack) {
        if(cnt[C.REMOTE_GUARDIAN]===0) {
            queueRoom.queue.extern.unshift([C.REMOTE_GUARDIAN, extraMemory]);
            cnt[C.REMOTE_GUARDIAN]++;
        }
        return;
    }

    if(queueRoom.controller.level < 4 || !queueRoom.storage) {
        //Can't afford too many creeps
        // sent remoteWorkers
        if(room === undefined) {
            //No vision
            //Spawn a scout first
            
            if(cnt[C.SCOUT]===0) {
                queueRoom.queue.extern.push([C.SCOUT, extraMemory]);
                cnt[C.SCOUT]++;
            }
        } else {
            //=== Harvest all sources and spawn dedicated hauler ===
            let needWorker = room.sources.length*2 - cnt[C.REMOTE_WORKER];
            //Actually enqueue harvesters and haulers
            while(needWorker-- > 0) {
                queueRoom.queue.extern.push([C.REMOTE_WORKER, extraMemory]);
                cnt[C.REMOTE_WORKER]++;
            }
        }

        return;
    }

    //Do we have vision of that room?
    if(room === undefined) {
        //No vision
        //Spawn a scout first
        /*
        if(cnt[C.SCOUT]===0) {
            queueRoom.queue.extern.push([C.SCOUT, extraMemory]);
            cnt[C.SCOUT]++;
        }
        */
        return;
    }

    //=== Keep room reserved ===
    const controller = room.controller;
    if(controller) {
        const reservation = controller.reservation;
        if(reservation && reservation.username===C.USERNAME && reservation.ticksToEnd>1000) {
            //No need to spawn reserver
        } else if(cnt[C.RESERVER]===0) {
            queueRoom.queue.extern.push([C.RESERVER, extraMemory]);
            cnt[C.RESERVER]++;
        }
    }

{'fraction_non_alphanumeric': 0.11760045274476513, 'fraction_numerical': 0.003621958121109225, 'mean_word_length': 1.7759974866478165, 'pattern_counts': {'":': 0, '<': 7, '<?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': '2066687', 'n_tokens_mistral': 2600, 'n_tokens_neox': 2475, 'n_words': 571}