/* main.js
 *
 * Entry point routine for Screeps.
 *
 */ 
'use strict';

var Globals = require('globals');
var RoomControl = require('room_control');
var Utils = require('utils');

function initialize() {

    // initialize commands buffer
    if (!('commands' in Memory)) {
        Memory.commands = Object.assign({}, Globals.GAME_MEMORY.commands);
    }


    // survey the creep population
    for (var r in Game.rooms) {
        Game.rooms[r].memory.population = Object.assign({}, Globals.ROOM_MEMORY_OBJS.population);
    }
    for (var c in Game.creeps) {
        var creep = Game.creeps[c];
        var room = creep.room;
        if (!(_.includes(room.memory.population[creep.memory.role], creep.id))) {
            room.memory.population[creep.memory.role].push(creep.id);
        }
    }
}

function cleanup() {
    // clean up creep memory
    for (var name in Memory.creeps) {
        if (!Game.creeps[name]) {
            delete Memory.creeps[name];
            console.log("Clearing creep memory: ", name);
        }
    }

    // check for CPU overruns
    if (Game.cpu.getUsed() > (Game.cpu.tickLimit * 0.9)) {
        Utils.err("WARNING: CPU OVERRUN! used: " + Game.cpu.getUsed().toFixed(2) + ", limit: " + Game.cpu.tickLimit + ", bucket: " + Game.cpu.bucket);
    }
}


module.exports.loop = function () {

    // INITIALIZE
    //==================================
    initialize();

    // EXECUTE ROOM CONTROL
    //==================================
    for (var r in Game.rooms) {
        RoomControl.run(Game.rooms[r]);
    }

    // CLEANUP
    //==================================
    cleanup();

};

@@ -1157,16 +1157,51 @@
 t * 0.9)
+ %7C%7C%0D%0A        Game.cpu.bucket %3C 1000
 ) %7B%0D%0A   
@@ -1233,15 +1233,13 @@
 CPU 
-OVERRUN
+USAGE
 ! us

modify cpu warning conditions