screeps/main.js
2016-06-28 19:38:52 -04:00

54 lines
1.7 KiB
JavaScript

var roleHarvester = require('role.harvester');
var roleUpgrader = require('role.upgrader');
var roleBuilder = require('role.builder');
var creepRolePriority = ['harvester', 'builder', 'upgrader'];
var creepsConfig = {
harvester: {
bodyParts: [WORK, CARRY, MOVE],
minimumCreeps: 2,
role: roleHarvester
},
builder: {
bodyParts: [WORK, CARRY, MOVE],
minimumCreeps: 2,
role: roleBuilder
},
upgrader: {
bodyParts: [WORK, CARRY, MOVE],
minimumCreeps: 2,
role: roleUpgrader
}
}
module.exports.loop = function(){
// Cleanup Creeps
for (var name in Memory.creeps){
if (!Game.creeps[name]){
delete Memory.creeps[name];
console.log("Clearning non-existing creep memory: ", name);
}
}
// Refactor some similar behavior for all roles
for (var i in creepRolePriority){
var roleName = creepRolePriority[i];
var defaults = creepsConfig[roleName];
var creepsOfKind = _.filter(Game.creeps, (creep) => creep.memory.role == roleName);
if (creepsOfKind.length < creepsConfig[roleName].minimumCreeps){
var newName = Game.spawns.Spawn1.createCreep(creepsConfig[roleName].bodyParts,
undefined,
{role: roleName});
console.log("Spawning", newName);
}
}
// Run stuff for each creep
for (var name in Game.creeps){
var creep = Game.creeps[name];
var role = creep.memory.role;
creepsConfig[role].role.run(creep);
}
console.log("tick");
}