added creep config file

This commit is contained in:
Tyrel Souza 2016-07-05 10:36:43 -04:00
parent b34776552a
commit ee75b48814
No known key found for this signature in database
GPG Key ID: 2EECB5087209E6A5
7 changed files with 157 additions and 136 deletions

View File

@ -1,5 +1,5 @@
// usernames are all lowercase, we compare with to lower // usernames are all lowercase, we compare with to lower
var USERNAME_WHITELIST = ['chrisinajar', 'ho0ber', 'fractaloop', 'n7-anthony', 'overra', 'tyrel', 'fervens']; var USERNAME_WHITELIST = ['chrisinajar', 'ho0ber', 'fractaloop', 'n7-anthony', 'overra', 'tyrel', 'fervens', 'devdaniel'];
module.exports = { module.exports = {
findEnemy: findEnemy, findEnemy: findEnemy,

27
creeps.config.js Normal file
View File

@ -0,0 +1,27 @@
var roleHarvester = require('role.harvester');
var roleUpgrader = require('role.upgrader');
var roleBuilder = require('role.builder');
var roleGuard = require('role.guard');
module.exports = {
harvester: {
bodyParts: [WORK,WORK, CARRY,CARRY, MOVE, MOVE],
minimumCreeps: 4,
role: roleHarvester
},
builder: {
bodyParts: [WORK, CARRY, CARRY, CARRY, MOVE, MOVE],
minimumCreeps: 0,
role: roleBuilder
},
upgrader: {
bodyParts: [WORK, CARRY, WORK, MOVE, CARRY, MOVE, MOVE],
minimumCreeps: 4,
role: roleUpgrader
},
guard: {
bodyParts: [TOUGH, MOVE, ATTACK, MOVE, ATTACK],
minimumCreeps: 0,
role: roleGuard,
}
};

127
main.js
View File

@ -1,74 +1,53 @@
var roleHarvester = require('role.harvester'); var structureTower = require('structure.tower');
var roleUpgrader = require('role.upgrader'); var creepsConfig = require('creeps.config');
var roleBuilder = require('role.builder'); var helpers = require('helpers');
var roleGuard = require('role.guard'); var creepRolePriority = ['harvester', 'builder', 'upgrader', 'guard'];
var helpers = require('helpers');
var creepRolePriority = ['harvester', 'builder', 'upgrader', 'guard']; module.exports.loop = function(){
var creepsConfig = { console.log(' ');
harvester: { // Cleanup Creeps
bodyParts: [WORK, CARRY, MOVE, MOVE, MOVE], for (var name in Memory.creeps){
minimumCreeps: 4, if (!Game.creeps[name]){
role: roleHarvester delete Memory.creeps[name];
}, console.log('Cleaning non-existing creep memory:', name);
builder: { }
bodyParts: [WORK, CARRY, CARRY, MOVE], }
minimumCreeps: 5,
role: roleBuilder // Refactor some similar behavior for all roles
}, for (var i in creepRolePriority){
upgrader: { var roleName = creepRolePriority[i];
bodyParts: [WORK, CARRY, CARRY, MOVE, MOVE], var defaults = creepsConfig[roleName];
minimumCreeps: 2,
role: roleUpgrader let creepsOfKind = _.filter(Game.creeps, (creep) => {return creep.memory.role == roleName});
}, if (creepsOfKind.length < defaults.minimumCreeps){
guard: { var newName = Game.spawns.Spawn1.createCreep(defaults.bodyParts,
bodyParts: [TOUGH, MOVE, ATTACK, MOVE, ATTACK], helpers.generateName(roleName), // Make names show the roles. Change me to undefined if this gets annoying.
minimumCreeps: 4, {role: roleName});
role: roleGuard, if(newName == ERR_NOT_ENOUGH_ENERGY){
} console.log('not enough resources to spawn');
}; } else {
console.log('Spawning', newName);
}
module.exports.loop = function(){ }
console.log(' '); }
// Cleanup Creeps
for (var name in Memory.creeps){ var tower = Game.getObjectById('577683641404f96570a580d2');
if (!Game.creeps[name]){ if(tower) {
delete Memory.creeps[name]; structureTower.run(tower);
console.log('Cleaning non-existing creep memory:', name); }
}
// Run stuff for each creep
} var roleCounts = {harvester: 0, builder: 0, upgrader: 0, guard: 0};
var expected = _(creepsConfig).mapValues('minimumCreeps');
// Refactor some similar behavior for all roles
for (var i in creepRolePriority){ for (var name in Game.creeps){
var roleName = creepRolePriority[i]; var creep = Game.creeps[name];
var defaults = creepsConfig[roleName]; var role = creep.memory.role;
roleCounts[role] += 1;
let creepsOfKind = _.filter(Game.creeps, (creep) => {return creep.memory.role == roleName}); creepsConfig[role].role.run(creep);
if (creepsOfKind.length < defaults.minimumCreeps){ }
var newName = Game.spawns.Spawn1.createCreep(defaults.bodyParts, console.log('current\t', JSON.stringify(roleCounts));
helpers.generateName(roleName), // Make names show the roles. Change me to undefined if this gets annoying. console.log('expect\t', JSON.stringify(expected));
{role: roleName}); console.log('#############################################################');
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('#############################################################');
};

View File

@ -1,3 +1,5 @@
var BreakException= {}; var BreakException= {};
var roleBuilder = { var roleBuilder = {
@ -16,9 +18,10 @@ var roleBuilder = {
creep.moveTo(targets[0]); creep.moveTo(targets[0]);
} }
} else { } else {
var sources = creep.room.find(FIND_SOURCES); var source = creep.pos.findClosestByPath(FIND_SOURCES);
if (creep.harvest(sources[0]) == ERR_NOT_IN_RANGE){ if(creep.harvest(source) == ERR_NOT_IN_RANGE) {
creep.moveTo(sources[0]); console.log('\tmoving to', source);
creep.moveTo(source);
} }
} }
@ -26,3 +29,4 @@ var roleBuilder = {
} }
}; };
module.exports = roleBuilder; module.exports = roleBuilder;

View File

@ -17,7 +17,6 @@ var roleGuard = {
run: function(creep){ run: function(creep){
this.creep = creep; this.creep = creep;
this.lastFlag = helpers.getLastFlag("patrol_"); this.lastFlag = helpers.getLastFlag("patrol_");
console.log(creep, 'running Guard')
creep.memory.patrolDestination = creep.memory.patrolDestination || 1; creep.memory.patrolDestination = creep.memory.patrolDestination || 1;
var target = battle.findEnemy(creep); var target = battle.findEnemy(creep);
if(target != creep.room.controller && creep.hits > creep.hitsMax - 500 /* no more attack */) { if(target != creep.room.controller && creep.hits > creep.hitsMax - 500 /* no more attack */) {

View File

@ -1,32 +1,37 @@
var roleHarvester = { var roleHarvester = {
/** @param {Creep} creep **/ /** @param {Creep} creep **/
run: function(creep) { run: function(creep) {
console.log(creep, 'running Harvest'); console.log(creep, 'running Harvest');
if(creep.carry.energy < creep.carryCapacity) { if(creep.carry.energy < creep.carryCapacity) {
var source = creep.pos.findClosestByPath(FIND_SOURCES); var source = creep.pos.findClosestByPath(FIND_SOURCES);
if(creep.harvest(source) == ERR_NOT_IN_RANGE) { if(creep.harvest(source) == ERR_NOT_IN_RANGE) {
console.log('\tmoving to', source); console.log('\tmoving to', source);
creep.moveTo(source); creep.moveTo(source);
} }
} }
else { else {
var targets = creep.room.find(FIND_STRUCTURES, { var targets = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => { filter: (structure) => {
return (structure.structureType == STRUCTURE_EXTENSION return (structure.structureType == STRUCTURE_CONTAINER
|| structure.structureType == STRUCTURE_SPAWN || structure.structureType == STRUCTURE_EXTENSION
|| structure.structureType == STRUCTURE_TOWER || structure.structureType == STRUCTURE_SPAWN
) && structure.energy < structure.energyCapacity; || structure.structureType == STRUCTURE_TOWER
} ) && structure.energy < structure.energyCapacity;
}); }
if(targets.length > 0) { });
if(creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { if(targets.length > 0) {
console.log('\tmoving to', targets[0]); if(creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(targets[0]); console.log('\tmoving to', targets[0]);
} creep.moveTo(targets[0]);
} }
} } else {
} creep.moveTo(Game.spawns.Spawn1);
}; }
}
module.exports = roleHarvester; }
};
module.exports = roleHarvester;

View File

@ -1,26 +1,33 @@
var roleUpgrader = { var roleUpgrader = {
/** @param {Creep} creep **/ /** @param {Creep} creep **/
run: function(creep) { run: function(creep) {
if(creep.memory.upgrading && creep.carry.energy == 0) { if(creep.memory.upgrading && creep.carry.energy == 0) {
creep.memory.upgrading = false; creep.memory.upgrading = false;
} }
if(!creep.memory.upgrading && creep.carry.energy == creep.carryCapacity) { if(!creep.memory.upgrading && creep.carry.energy == creep.carryCapacity) {
creep.memory.upgrading = true; creep.memory.upgrading = true;
} }
if(creep.memory.upgrading) { if(creep.memory.upgrading) {
if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) { if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
creep.moveTo(creep.room.controller); creep.moveTo(creep.room.controller);
} }
} }
else { else {
var source = creep.pos.findClosestByPath(FIND_SOURCES); var source = creep.pos.findClosestByPath(FIND_SOURCES);
if(creep.harvest(source) == ERR_NOT_IN_RANGE) { console.log(source);
creep.moveTo(source); if(creep.harvest(source) == ERR_NOT_IN_RANGE) {
} creep.moveTo(source);
} }
} }
};
}
};
module.exports = roleUpgrader; module.exports = roleUpgrader;