From ee75b4881442c8aab4ab947f727262c6a850843b Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Tue, 5 Jul 2016 10:36:43 -0400 Subject: [PATCH] added creep config file --- battle.js | 2 +- creeps.config.js | 27 ++++++++++ main.js | 127 +++++++++++++++++++--------------------------- role.builder.js | 10 ++-- role.guard.js | 1 - role.harvester.js | 69 +++++++++++++------------ role.upgrader.js | 57 ++++++++++++--------- 7 files changed, 157 insertions(+), 136 deletions(-) create mode 100644 creeps.config.js diff --git a/battle.js b/battle.js index 320434f..3c28ebc 100644 --- a/battle.js +++ b/battle.js @@ -1,5 +1,5 @@ // 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 = { findEnemy: findEnemy, diff --git a/creeps.config.js b/creeps.config.js new file mode 100644 index 0000000..9833d4f --- /dev/null +++ b/creeps.config.js @@ -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, + } +}; \ No newline at end of file diff --git a/main.js b/main.js index 76b6c40..c37af1b 100644 --- a/main.js +++ b/main.js @@ -1,74 +1,53 @@ -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: { - bodyParts: [WORK, CARRY, CARRY, MOVE], - minimumCreeps: 5, - role: roleBuilder - }, - upgrader: { - bodyParts: [WORK, CARRY, CARRY, MOVE, MOVE], - 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('#############################################################'); -}; +var structureTower = require('structure.tower'); +var creepsConfig = require('creeps.config'); +var helpers = require('helpers'); +var creepRolePriority = ['harvester', 'builder', 'upgrader', 'guard']; + + +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); + } + } + } + + var tower = Game.getObjectById('577683641404f96570a580d2'); + if(tower) { + structureTower.run(tower); + } + + // 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('#############################################################'); +}; diff --git a/role.builder.js b/role.builder.js index d28678a..a41e2b7 100644 --- a/role.builder.js +++ b/role.builder.js @@ -1,3 +1,5 @@ + + var BreakException= {}; var roleBuilder = { @@ -16,9 +18,10 @@ var roleBuilder = { creep.moveTo(targets[0]); } } else { - var sources = creep.room.find(FIND_SOURCES); - if (creep.harvest(sources[0]) == ERR_NOT_IN_RANGE){ - creep.moveTo(sources[0]); + var source = creep.pos.findClosestByPath(FIND_SOURCES); + if(creep.harvest(source) == ERR_NOT_IN_RANGE) { + console.log('\tmoving to', source); + creep.moveTo(source); } } @@ -26,3 +29,4 @@ var roleBuilder = { } }; module.exports = roleBuilder; + diff --git a/role.guard.js b/role.guard.js index 9525038..b0f9ec8 100644 --- a/role.guard.js +++ b/role.guard.js @@ -17,7 +17,6 @@ var roleGuard = { run: function(creep){ this.creep = creep; this.lastFlag = helpers.getLastFlag("patrol_"); - console.log(creep, 'running Guard') creep.memory.patrolDestination = creep.memory.patrolDestination || 1; var target = battle.findEnemy(creep); if(target != creep.room.controller && creep.hits > creep.hitsMax - 500 /* no more attack */) { diff --git a/role.harvester.js b/role.harvester.js index f5ce22b..e45c395 100644 --- a/role.harvester.js +++ b/role.harvester.js @@ -1,32 +1,37 @@ -var roleHarvester = { - - /** @param {Creep} creep **/ - run: function(creep) { - console.log(creep, 'running Harvest'); - if(creep.carry.energy < creep.carryCapacity) { - var source = creep.pos.findClosestByPath(FIND_SOURCES); - if(creep.harvest(source) == ERR_NOT_IN_RANGE) { - console.log('\tmoving to', source); - creep.moveTo(source); - } - } - else { - var targets = creep.room.find(FIND_STRUCTURES, { - filter: (structure) => { - return (structure.structureType == STRUCTURE_EXTENSION - || structure.structureType == STRUCTURE_SPAWN - || structure.structureType == STRUCTURE_TOWER - ) && structure.energy < structure.energyCapacity; - } - }); - if(targets.length > 0) { - if(creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { - console.log('\tmoving to', targets[0]); - creep.moveTo(targets[0]); - } - } - } - } -}; - -module.exports = roleHarvester; \ No newline at end of file +var roleHarvester = { + + /** @param {Creep} creep **/ + run: function(creep) { + console.log(creep, 'running Harvest'); + if(creep.carry.energy < creep.carryCapacity) { + var source = creep.pos.findClosestByPath(FIND_SOURCES); + if(creep.harvest(source) == ERR_NOT_IN_RANGE) { + console.log('\tmoving to', source); + creep.moveTo(source); + } + } + else { + var targets = creep.room.find(FIND_STRUCTURES, { + filter: (structure) => { + return (structure.structureType == STRUCTURE_CONTAINER + || structure.structureType == STRUCTURE_EXTENSION + || structure.structureType == STRUCTURE_SPAWN + || structure.structureType == STRUCTURE_TOWER + ) && structure.energy < structure.energyCapacity; + } + }); + if(targets.length > 0) { + if(creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + console.log('\tmoving to', targets[0]); + creep.moveTo(targets[0]); + } + } else { + creep.moveTo(Game.spawns.Spawn1); + } + } + } +}; + +module.exports = roleHarvester; + + diff --git a/role.upgrader.js b/role.upgrader.js index e6ae8fa..8160345 100644 --- a/role.upgrader.js +++ b/role.upgrader.js @@ -1,26 +1,33 @@ -var roleUpgrader = { - - /** @param {Creep} creep **/ - run: function(creep) { - if(creep.memory.upgrading && creep.carry.energy == 0) { - creep.memory.upgrading = false; - } - if(!creep.memory.upgrading && creep.carry.energy == creep.carryCapacity) { - creep.memory.upgrading = true; - } - - if(creep.memory.upgrading) { - if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) { - creep.moveTo(creep.room.controller); - } - } - else { - var source = creep.pos.findClosestByPath(FIND_SOURCES); - if(creep.harvest(source) == ERR_NOT_IN_RANGE) { - creep.moveTo(source); - } - } - } -}; - +var roleUpgrader = { + + /** @param {Creep} creep **/ + run: function(creep) { + if(creep.memory.upgrading && creep.carry.energy == 0) { + creep.memory.upgrading = false; + } + if(!creep.memory.upgrading && creep.carry.energy == creep.carryCapacity) { + creep.memory.upgrading = true; + } + + if(creep.memory.upgrading) { + if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) { + creep.moveTo(creep.room.controller); + } + } + else { + var source = creep.pos.findClosestByPath(FIND_SOURCES); + console.log(source); + if(creep.harvest(source) == ERR_NOT_IN_RANGE) { + creep.moveTo(source); + } + } + + + + + + + } +}; + module.exports = roleUpgrader; \ No newline at end of file