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: 3, role: roleHarvester }, builder: { bodyParts: [WORK, CARRY, MOVE, MOVE], minimumCreeps: 1, role: roleBuilder }, upgrader: { bodyParts: [WORK, CARRY, MOVE, MOVE, MOVE], minimumCreeps: 4, role: roleUpgrader }, guard: { bodyParts: [TOUGH, MOVE, ATTACK, MOVE, ATTACK], minimumCreeps: 3, 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 } var expected = { 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); } for (var role in creepsConfig){ expected[role] = creepsConfig[role].minimumCreeps; } console.log("current:", JSON.stringify(roleCounts)); console.log("expect: ", JSON.stringify(expected)); console.log("#############################################################"); };