screeps/main.js

75 lines
2.5 KiB
JavaScript
Raw Normal View History

2016-06-29 17:21:09 +00:00
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],
2016-06-29 18:42:30 +00:00
minimumCreeps: 3,
2016-06-29 17:21:09 +00:00
role: roleHarvester
},
builder: {
bodyParts: [WORK, CARRY, MOVE, MOVE],
2016-06-30 00:52:07 +00:00
minimumCreeps: 1,
2016-06-29 17:21:09 +00:00
role: roleBuilder
},
upgrader: {
bodyParts: [WORK, CARRY, MOVE, MOVE, MOVE],
2016-06-29 18:42:30 +00:00
minimumCreeps: 4,
2016-06-29 17:21:09 +00:00
role: roleUpgrader
},
guard: {
bodyParts: [TOUGH, MOVE, ATTACK, MOVE, ATTACK],
2016-06-29 18:42:30 +00:00
minimumCreeps: 3,
2016-06-30 00:52:07 +00:00
role: roleGuard,
2016-06-29 17:21:09 +00:00
}
};
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);
2016-06-29 17:21:09 +00:00
}
}
// 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');
2016-06-29 17:21:09 +00:00
} else {
console.log('Spawning', newName);
2016-06-29 17:21:09 +00:00
}
}
}
// Run stuff for each creep
var roleCounts = {harvester: 0, builder: 0, upgrader: 0, guard: 0};
var expected = _(creepsConfig).mapValues('minimumCreeps');
2016-06-29 17:21:09 +00:00
for (var name in Game.creeps){
var creep = Game.creeps[name];
var role = creep.memory.role;
roleCounts[role] += 1;
creepsConfig[role].role.run(creep);
2016-06-29 18:42:30 +00:00
}
console.log('current\t', JSON.stringify(roleCounts));
console.log('expect\t', JSON.stringify(expected));
console.log('#############################################################');
2016-06-29 18:42:30 +00:00
};