From be4b33d728b761a0e9fcfb80134d068f716d9188 Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Wed, 29 Jun 2016 13:21:09 -0400 Subject: [PATCH] guard, and change some numbers --- battle.js | 108 +++++++++++++++++----------------- helpers.js | 26 ++++----- main.js | 145 ++++++++++++++++++++++++---------------------- role.builder.js | 64 ++++++++++---------- role.guard.js | 17 ++++++ role.harvester.js | 62 ++++++++++---------- role.upgrader.js | 58 +++++++++---------- 7 files changed, 252 insertions(+), 228 deletions(-) create mode 100644 role.guard.js diff --git a/battle.js b/battle.js index 3d0e6d9..00a9fc1 100644 --- a/battle.js +++ b/battle.js @@ -1,55 +1,55 @@ -// usernames are all lowercase, we compare with to lower -var USERNAME_WHITELIST = ['chrisinajar', 'ho0ber', 'fractaloop', 'n7-anthony', 'overra', 'tyrel', 'fervens']; -​ -module.exports = { - findEnemy: findEnemy, - run: run -}; -​ -/* - var target = findEnemy(creep); -*/ -function findEnemy (creep) { - var targets = creep.room.find(FIND_HOSTILE_CREEPS, { - filter: (c) => { - return (USERNAME_WHITELIST.indexOf(c.owner.username.toLowerCase()) === -1); - } - }); - if (!targets.length) { - return creep.room.controller; - } - return creep.pos.findClosestByPath(targets); -} -​ -function run (creep) { - var target = findEnemy(creep); - if (!target) { - target = findWall(creep); - // getDirectionTo - if (!creep.pos.isNearTo(target)) { - return creep.moveTo(target); - } else { - creep.attack(target); - } - var direction = creep.pos.getDirectionTo(target); - creep.memory.wallDirection = direction; - } else { - creep.moveTo(target); - creep.attack(target); - } -} -​ -function findWall (creep) { - var targets = creep.room.find(FIND_STRUCTURES, { - filter: function(object) { - if (object.my) { - return false; - } - if (object.structureType !== STRUCTURE_TOWER && object.structureType !== STRUCTURE_WALL) { - return false; - } - return true; - } - }); - return creep.pos.findClosestByPath(targets); +// usernames are all lowercase, we compare with to lower +var USERNAME_WHITELIST = ['chrisinajar', 'ho0ber', 'fractaloop', 'n7-anthony', 'overra', 'tyrel', 'fervens']; + +module.exports = { + findEnemy: findEnemy, + run: run +}; + +/* + var target = findEnemy(creep); +*/ +function findEnemy (creep) { + var targets = creep.room.find(FIND_HOSTILE_CREEPS, { + filter: (c) => { + return (USERNAME_WHITELIST.indexOf(c.owner.username.toLowerCase()) === -1); + } + }); + if (!targets.length) { + return creep.room.controller; + } + return creep.pos.findClosestByPath(targets); +} + +function run (creep) { + var target = findEnemy(creep); + if (!target) { + target = findWall(creep); + // getDirectionTo + if (!creep.pos.isNearTo(target)) { + return creep.moveTo(target); + } else { + creep.attack(target); + } + var direction = creep.pos.getDirectionTo(target); + creep.memory.wallDirection = direction; + } else { + creep.moveTo(target); + creep.attack(target); + } +} + +function findWall (creep) { + var targets = creep.room.find(FIND_STRUCTURES, { + filter: function(object) { + if (object.my) { + return false; + } + if (object.structureType !== STRUCTURE_TOWER && object.structureType !== STRUCTURE_WALL) { + return false; + } + return true; + } + }); + return creep.pos.findClosestByPath(targets); } \ No newline at end of file diff --git a/helpers.js b/helpers.js index 2f349fa..4a9952a 100644 --- a/helpers.js +++ b/helpers.js @@ -1,14 +1,14 @@ -/** - * Created by tsouza on 6/28/2016. - */ - -var helpers = { - generateName: function(role){ - return role + '_xxxxxxxx'.replace(/[xy]/g, function(c) { - var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); - return v.toString(16); - }); - } -}; - +/** + * Created by tsouza on 6/28/2016. + */ + +var helpers = { + generateName: function(role){ + return role + '_xxxxxxxx'.replace(/[xy]/g, function(c) { + var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); + return v.toString(16); + }); + } +}; + module.exports = helpers; \ No newline at end of file diff --git a/main.js b/main.js index 9fdc350..8574160 100644 --- a/main.js +++ b/main.js @@ -1,70 +1,77 @@ -var roleHarvester = require('role.harvester'); -var roleUpgrader = require('role.upgrader'); -var roleBuilder = require('role.builder'); -var helpers = require('helpers'); - -var creepRolePriority = ['harvester', 'builder', 'upgrader']; -var creepsConfig = { - harvester: { - bodyParts: [WORK, CARRY, MOVE, MOVE], - minimumCreeps: 6, - role: roleHarvester - }, - builder: { - bodyParts: [WORK, CARRY, MOVE, MOVE], - minimumCreeps: 1, - role: roleBuilder - }, - upgrader: { - bodyParts: [WORK, CARRY, MOVE, MOVE], - minimumCreeps: 1, - role: roleUpgrader - } -}; - - -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 - } - 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(JSON.stringify(roleCounts)); - console.log("#############################################################"); +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: 2, + role: roleHarvester + }, + builder: { + bodyParts: [WORK, CARRY, MOVE, MOVE], + minimumCreeps: 0, + role: roleBuilder + }, + upgrader: { + bodyParts: [WORK, CARRY, MOVE, MOVE, MOVE], + minimumCreeps: 3, + role: roleUpgrader + }, + guard: { + bodyParts: [TOUGH, MOVE, ATTACK, MOVE, ATTACK], + minimumCreeps: 2, + 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 + } + 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(JSON.stringify(roleCounts)); + console.log("#############################################################"); }; \ No newline at end of file diff --git a/role.builder.js b/role.builder.js index 077a096..c563137 100644 --- a/role.builder.js +++ b/role.builder.js @@ -1,32 +1,32 @@ -var BreakException= {}; - -var roleBuilder = { - run: function(creep){ - console.log(creep, "running Builder"); - if(creep.memory.building && creep.carry.energy == 0){ - creep.memory.building = false; - } - if (!creep.memory.building && creep.carry.energy == creep.carryCapacity){ - creep.memory.building = true; - } - if (creep.memory.building){ - console.log("\t is building"); - var targets = creep.room.find(FIND_CONSTRUCTION_SITES); - - var transferErrorCode = creep.build(targets[0]); - if(transferErrorCode == ERR_NOT_IN_RANGE) { - console.log("\t moving to", targets[0]); - creep.moveTo(targets[0]); - } - } else { - console.log("\t","isFindingSources"); - var sources = creep.room.find(FIND_SOURCES); - if (creep.harvest(sources[0]) == ERR_NOT_IN_RANGE){ - creep.moveTo(sources[0]); - } - } - - - } -}; -module.exports = roleBuilder; +var BreakException= {}; + +var roleBuilder = { + run: function(creep){ + console.log(creep, "running Builder"); + if(creep.memory.building && creep.carry.energy == 0){ + creep.memory.building = false; + } + if (!creep.memory.building && creep.carry.energy == creep.carryCapacity){ + creep.memory.building = true; + } + if (creep.memory.building){ + console.log("\t is building"); + var targets = creep.room.find(FIND_CONSTRUCTION_SITES); + + var transferErrorCode = creep.build(targets[0]); + if(transferErrorCode == ERR_NOT_IN_RANGE) { + console.log("\t moving to", targets[0]); + creep.moveTo(targets[0]); + } + } else { + console.log("\t","isFindingSources"); + var sources = creep.room.find(FIND_SOURCES); + if (creep.harvest(sources[0]) == ERR_NOT_IN_RANGE){ + creep.moveTo(sources[0]); + } + } + + + } +}; +module.exports = roleBuilder; diff --git a/role.guard.js b/role.guard.js new file mode 100644 index 0000000..22cb9e0 --- /dev/null +++ b/role.guard.js @@ -0,0 +1,17 @@ +var battle = require("battle") + +var roleGuard = { + run: function(creep){ + console.log(creep, "running Guard") + var target = battle.findEnemy(creep); + console.log("\t", creep, "tageting:",target) + if(target && creep.hits > creep.hitsMax - 500 /* no more attack */) { + creep.moveTo(target); + creep.attack(target); + } else { + creep.moveTo(Game.spawns.Spawn1); + } + + } +} +module.exports = roleGuard; \ No newline at end of file diff --git a/role.harvester.js b/role.harvester.js index a4af61a..a5a92b1 100644 --- a/role.harvester.js +++ b/role.harvester.js @@ -1,32 +1,32 @@ -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]); - } - } - } - } -}; - +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 diff --git a/role.upgrader.js b/role.upgrader.js index 850f48f..3e31484 100644 --- a/role.upgrader.js +++ b/role.upgrader.js @@ -1,30 +1,30 @@ -var roleUpgrader = { - - /** @param {Creep} creep **/ - run: function(creep) { - console.log(creep, "running Upgrade"); - - 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) { - console.log("\tmoving to", creep.room.controller); - creep.moveTo(creep.room.controller); - } - } - else { - var source = creep.pos.findClosestByPath(FIND_SOURCES); - if(creep.harvest(source) == ERR_NOT_IN_RANGE) { - console.log("\tmoving to", source); - creep.moveTo(source); - } - } - } -}; - +var roleUpgrader = { + + /** @param {Creep} creep **/ + run: function(creep) { + console.log(creep, "running Upgrade"); + + 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) { + console.log("\tmoving to", creep.room.controller); + creep.moveTo(creep.room.controller); + } + } + else { + var source = creep.pos.findClosestByPath(FIND_SOURCES); + if(creep.harvest(source) == ERR_NOT_IN_RANGE) { + console.log("\tmoving to", source); + creep.moveTo(source); + } + } + } +}; + module.exports = roleUpgrader; \ No newline at end of file