From a629713ed5bb92c2a2a60c9f1a95cd14628ea35e Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Tue, 28 Jun 2016 21:28:33 -0400 Subject: [PATCH] change finding code, add more error logs --- main.js | 8 ++++++-- role.builder.js | 15 +++++++-------- role.harvester.js | 9 ++++----- role.upgrader.js | 8 ++++---- 4 files changed, 21 insertions(+), 19 deletions(-) diff --git a/main.js b/main.js index 6eb8d35..15bab9e 100644 --- a/main.js +++ b/main.js @@ -40,7 +40,11 @@ module.exports.loop = function(){ var newName = Game.spawns.Spawn1.createCreep(creepsConfig[roleName].bodyParts, undefined, {role: roleName}); - console.log("Spawning", newName); + if(newName == ERR_NOT_ENOUGH_ENERGY){ + console.log("not enough resources to spawn"); + } else { + console.log("Spawning", newName); + } } } @@ -50,5 +54,5 @@ module.exports.loop = function(){ var role = creep.memory.role; creepsConfig[role].role.run(creep); } - console.log("tick"); + console.log("#############################################################"); }; \ No newline at end of file diff --git a/role.builder.js b/role.builder.js index 026f6b8..b783e1e 100644 --- a/role.builder.js +++ b/role.builder.js @@ -10,15 +10,14 @@ var roleBuilder = { creep.memory.building = true; } if (creep.memory.building){ - 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) { + console.log("\tis building"); + var targets = creep.room.find(FIND_CONSTRUCTION_SITES); + + let transerErrorCode = creep.transfer(targets[0], RESOURCE_ENERGY); + if(transerErrorCode == ERR_NOT_IN_RANGE) { creep.moveTo(targets[0]); + } else if (transerErrorCode == ERR_INVALID_TARGET){ + console.log("\tError, invalid Target"); } } else { diff --git a/role.harvester.js b/role.harvester.js index 0c1854f..a4af61a 100644 --- a/role.harvester.js +++ b/role.harvester.js @@ -4,11 +4,10 @@ var roleHarvester = { 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]); + var source = creep.pos.findClosestByPath(FIND_SOURCES); + if(creep.harvest(source) == ERR_NOT_IN_RANGE) { + console.log("\tmoving to", source); + creep.moveTo(source); } } else { diff --git a/role.upgrader.js b/role.upgrader.js index 3d992db..850f48f 100644 --- a/role.upgrader.js +++ b/role.upgrader.js @@ -18,10 +18,10 @@ var roleUpgrader = { } } 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]); + var source = creep.pos.findClosestByPath(FIND_SOURCES); + if(creep.harvest(source) == ERR_NOT_IN_RANGE) { + console.log("\tmoving to", source); + creep.moveTo(source); } } }