diff --git a/battle.js b/battle.js index 6439906..3d0e6d9 100644 --- a/battle.js +++ b/battle.js @@ -1,10 +1,11 @@ // usernames are all lowercase, we compare with to lower var USERNAME_WHITELIST = ['chrisinajar', 'ho0ber', 'fractaloop', 'n7-anthony', 'overra', 'tyrel', 'fervens']; - +​ module.exports = { - findEnemy: findEnemy + findEnemy: findEnemy, + run: run }; - +​ /* var target = findEnemy(creep); */ @@ -14,5 +15,41 @@ function findEnemy (creep) { 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/main.js b/main.js index 4196b1c..bc2fd8d 100644 --- a/main.js +++ b/main.js @@ -2,10 +2,11 @@ var roleHarvester = require('role.harvester'); var roleUpgrader = require('role.upgrader'); var roleBuilder = require('role.builder'); +var creepRolePriority = ['harvester', 'builder', 'upgrader']; var creepsConfig = { harvester: { bodyParts: [WORK, CARRY, MOVE], - minimumCreeps: 4, + minimumCreeps: 2, role: roleHarvester }, builder: { @@ -27,23 +28,27 @@ module.exports.loop = function(){ delete Memory.creeps[name]; console.log("Clearning non-existing creep memory: ", name); } - } - // Refactor some similar behavior for all roles - for (var roleName in creepsConfig){ + } + + // Refactor some similar behavior for all roles + for (var i in creepRolePriority){ + var roleName = creepRolePriority[i]; + var defaults = creepsConfig[roleName]; var creepsOfKind = _.filter(Game.creeps, (creep) => creep.memory.role == roleName); if (creepsOfKind.length < creepsConfig[roleName].minimumCreeps){ var newName = Game.spawns.Spawn1.createCreep(creepsConfig[roleName].bodyParts, undefined, {role: roleName}); - console.log("Spawning new ", roleName, ": ", newName); + console.log("Spawning", newName); } - }; + } // Run stuff for each creep - for (var name in Game.creeps){ + for (var name in Game.creeps){ var creep = Game.creeps[name]; var role = creep.memory.role; creepsConfig[role].role.run(creep); } + console.log("tick"); } \ No newline at end of file diff --git a/role.builder.js b/role.builder.js index dcaeffc..026f6b8 100644 --- a/role.builder.js +++ b/role.builder.js @@ -1,5 +1,8 @@ +var BreakException= {}; + var roleBuilder = { run: function(creep){ + console.log(creep, "running Builder"); if(creep.memory.building && creep.carry.energy == 0){ creep.memory.building = false; } @@ -7,14 +10,19 @@ var roleBuilder = { creep.memory.building = true; } if (creep.memory.building){ - var targets = creep.room.find(FIND_CONSTRUCTION_SITES); - if(targets.length){ - if (creep.build(targets[0]) == ERR_NOT_IN_RANGE){ + console.log("\t","isBuilding"); + var targets = creep.room.find(FIND_CONSTRUCTION_SITES, { + filter: (target) => { + return target.energy < target.energyCapacity; + } + }); + console.log("") + if(creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { 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]); diff --git a/role.harvester.js b/role.harvester.js index aa1328d..0c1854f 100644 --- a/role.harvester.js +++ b/role.harvester.js @@ -2,9 +2,12 @@ var roleHarvester = { /** @param {Creep} creep **/ run: function(creep) { + console.log(creep, "running Harvest"); if(creep.carry.energy < creep.carryCapacity) { var sources = creep.room.find(FIND_SOURCES); + // var source = Game.getObjectById("576a9c8257110ab231d8934b"); if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) { + console.log(creep, "moving to", sources[0]); creep.moveTo(sources[0]); } } @@ -19,6 +22,7 @@ var roleHarvester = { }); 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]); } } diff --git a/role.upgrader.js b/role.upgrader.js index 7374160..3d992db 100644 --- a/role.upgrader.js +++ b/role.upgrader.js @@ -2,6 +2,7 @@ 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; @@ -12,12 +13,14 @@ var roleUpgrader = { 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 sources = creep.room.find(FIND_SOURCES); if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) { + console.log("\tmoving to", sources[0]); creep.moveTo(sources[0]); } }