screeps/main.js

75 lines
2.5 KiB
JavaScript
Raw Normal View History

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: 4,
role: roleHarvester
},
builder: {
2016-06-30 14:59:43 +00:00
bodyParts: [WORK, CARRY, CARRY, MOVE],
minimumCreeps: 5,
role: roleBuilder
},
upgrader: {
bodyParts: [WORK, CARRY, CARRY, MOVE, MOVE],
2016-06-30 14:59:43 +00:00
minimumCreeps: 2,
role: roleUpgrader
},
guard: {
bodyParts: [TOUGH, MOVE, ATTACK, MOVE, ATTACK],
minimumCreeps: 4,
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 = {harvester: 0, builder: 0, upgrader: 0, guard: 0};
var expected = _(creepsConfig).mapValues('minimumCreeps');
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('current\t', JSON.stringify(roleCounts));
console.log('expect\t', JSON.stringify(expected));
console.log('#############################################################');
};