Files
screeps/main.js

43 lines
1.3 KiB
JavaScript

// ########################################
// ########## MAIN ##########
// ########################################
const creepClass = require("ClassCreep");
const roomClass = require("ClassRoom");
const spawnClass = require("ClassSpawn");
module.exports.loop = function () {
if(!global.compiled) onRecompile();
else if (!global.started) onRestart();
else onTick();
}
function onRecompile(){
creepClass.setup();
roomClass.setup();
spawnClass.setup();
console.log("Script recompiled...");
global.compiled = true;
}
function onRestart(){
Object.values(Game.rooms).forEach(room => room.begin());
Object.values(Game.spawns).forEach(spawn => spawn.begin());
Object.values(Game.creeps).forEach(creep => creep.begin());
global.started = true;
}
function onTick(){
Object.values(Game.rooms).forEach(room => room.tick());
Object.values(Game.spawns).forEach(spawn => spawn.tick());
Object.values(Game.creeps).forEach(creep => creep.tick());
if(!(Game.time % 100)) cleanUp();
}
function cleanUp(){
Object.keys(Memory.rooms).forEach(roomName => { if(!Game.rooms[roomName]) Memory.rooms[roomName] = undefined; });
Object.keys(Memory.creeps).forEach(creepName => { if(!Game.creeps[creepName]) Memory.creeps[creepName] = undefined; });
}