screeps/main.js
2016-06-29 13:21:09 -04:00

77 lines
2.4 KiB
JavaScript

var roleHarvester = require('role.harvester');
var roleUpgrader = require('role.upgrader');
var roleBuilder = require('role.builder');
var roleGuard = require('role.guard');
var helpers = require('helpers');
var creepRolePriority = ['harvester', 'builder', 'upgrader', 'guard'];
var creepsConfig = {
harvester: {
bodyParts: [WORK, CARRY, MOVE, MOVE, MOVE],
minimumCreeps: 2,
role: roleHarvester
},
builder: {
bodyParts: [WORK, CARRY, MOVE, MOVE],
minimumCreeps: 0,
role: roleBuilder
},
upgrader: {
bodyParts: [WORK, CARRY, MOVE, MOVE, MOVE],
minimumCreeps: 3,
role: roleUpgrader
},
guard: {
bodyParts: [TOUGH, MOVE, ATTACK, MOVE, ATTACK],
minimumCreeps: 2,
role: roleGuard
}
};
module.exports.loop = function(){
console.log(' ');
// Cleanup Creeps
for (var name in Memory.creeps){
if (!Game.creeps[name]){
delete Memory.creeps[name];
console.log("Cleaning 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];
let creepsOfKind = _.filter(Game.creeps, (creep) => {return creep.memory.role == roleName});
if (creepsOfKind.length < defaults.minimumCreeps){
var newName = Game.spawns.Spawn1.createCreep(defaults.bodyParts,
helpers.generateName(roleName), // Make names show the roles. Change me to undefined if this gets annoying.
{role: roleName});
if(newName == ERR_NOT_ENOUGH_ENERGY){
console.log("not enough resources to spawn");
} else {
console.log("Spawning", newName);
}
}
}
// Run stuff for each creep
var roleCounts = {
builder: 0,
harvester: 0,
upgrader: 0,
guard: 0
}
for (var name in Game.creeps){
var creep = Game.creeps[name];
var role = creep.memory.role;
roleCounts[role] += 1;
creepsConfig[role].role.run(creep);
}
console.log(JSON.stringify(roleCounts));
console.log("#############################################################");
};