screeps/main.js

54 lines
1.7 KiB
JavaScript
Raw Normal View History

2016-06-28 20:31:39 +00:00
var roleHarvester = require('role.harvester');
var roleUpgrader = require('role.upgrader');
var roleBuilder = require('role.builder');
2016-06-28 23:38:52 +00:00
var creepRolePriority = ['harvester', 'builder', 'upgrader'];
2016-06-28 21:00:10 +00:00
var creepsConfig = {
harvester: {
bodyParts: [WORK, CARRY, MOVE],
2016-06-28 23:38:52 +00:00
minimumCreeps: 2,
2016-06-28 21:17:03 +00:00
role: roleHarvester
2016-06-28 21:00:10 +00:00
},
2016-06-28 21:17:03 +00:00
builder: {
2016-06-28 21:00:10 +00:00
bodyParts: [WORK, CARRY, MOVE],
minimumCreeps: 2,
2016-06-28 21:17:03 +00:00
role: roleBuilder
2016-06-28 21:00:10 +00:00
},
upgrader: {
bodyParts: [WORK, CARRY, MOVE],
minimumCreeps: 2,
2016-06-28 21:17:03 +00:00
role: roleUpgrader
2016-06-28 21:00:10 +00:00
}
}
2016-06-28 21:17:03 +00:00
2016-06-28 20:31:39 +00:00
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);
}
2016-06-28 23:38:52 +00:00
2016-06-28 20:31:39 +00:00
}
2016-06-28 20:49:31 +00:00
2016-06-28 23:38:52 +00:00
// Refactor some similar behavior for all roles
for (var i in creepRolePriority){
var roleName = creepRolePriority[i];
var defaults = creepsConfig[roleName];
2016-06-28 20:31:39 +00:00
var creepsOfKind = _.filter(Game.creeps, (creep) => creep.memory.role == roleName);
2016-06-28 21:00:10 +00:00
if (creepsOfKind.length < creepsConfig[roleName].minimumCreeps){
2016-06-28 21:17:03 +00:00
var newName = Game.spawns.Spawn1.createCreep(creepsConfig[roleName].bodyParts,
undefined,
{role: roleName});
2016-06-28 23:38:52 +00:00
console.log("Spawning", newName);
2016-06-28 20:31:39 +00:00
}
2016-06-28 23:38:52 +00:00
}
2016-06-28 20:31:39 +00:00
// Run stuff for each creep
2016-06-28 23:38:52 +00:00
for (var name in Game.creeps){
2016-06-28 20:31:39 +00:00
var creep = Game.creeps[name];
2016-06-28 21:00:10 +00:00
var role = creep.memory.role;
2016-06-28 21:17:03 +00:00
creepsConfig[role].role.run(creep);
2016-06-28 20:31:39 +00:00
}
2016-06-28 23:38:52 +00:00
console.log("tick");
2016-06-28 20:31:39 +00:00
}