From 167f20d8b36c47bac2857d74d45b7b0090994e54 Mon Sep 17 00:00:00 2001 From: Peery Date: Wed, 5 Oct 2022 19:31:55 +0200 Subject: [PATCH] Very Basic Harvester logic & comments Added more comments for explaining the code's intent Added very basic pathfinding in the harvesterRole. Currently only moves the creep towards the nearest source and repeats the pathfinding process every tick. --- src/creeps/harvester.js | 22 +++++++++++++++++----- src/hivemind/hivemind.js | 2 +- src/overlords/overlord.js | 6 +++--- src/overlords/setup_overlord.js | 11 +++++------ 4 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/creeps/harvester.js b/src/creeps/harvester.js index 70637d6..b4522e0 100644 --- a/src/creeps/harvester.js +++ b/src/creeps/harvester.js @@ -24,7 +24,7 @@ var roleHarvester = { spawn.spawnCreep(this.minimum_parts, "harvester-"+postfix, {memory: {"owner": owner_id, "role": "Harv"}}); } else { - console.log("roleHarvester: Spawn \""+spawn.name+"\" couldn't afford a harvester! D:"); + console.log("roleHarvester: Spawn \""+spawn.name+"\" couldn't afford a harvester! D: Result: ", spawn.spawnCreep(this.minimum_parts, "harvester-"+postfix, {dryRun: true})); throw "Not enough energy!"; } @@ -36,11 +36,23 @@ var roleHarvester = { * @param {Creep} creep */ run: function(creep) { - console.log("roleHarvester: run() " + creep); - let sources = creep.room.find(FIND_SOURCES_ACTIVE); - for (let source in sources) { - console.log("Found source ", source.name); + if (creep.id === undefined) { // id is undefined if the creep was created this tick + return } + console.log("roleHarvester ["+creep.name+"]: run() " + creep + " id:", creep.id); + + // TODO implement state machine here about what the harvester is up to. Currently it only moves to the nearest source. + + let goals = _.map(creep.room.find(FIND_SOURCES), function(source) { + return {pos: source.pos, range: 1}; + }); + + let nearest_source = PathFinder.search(creep.pos, goals); + console.log("roleHarvester ["+creep.name+"]: run() moving creept towards " + nearest_source.path[nearest_source.path.length-1], nearest_source); + + console.log("roleHarvester ["+creep.name+"]: run() moving to direction " + nearest_source.path[0]); + creep.moveTo(nearest_source.path[0]); + }, }; diff --git a/src/hivemind/hivemind.js b/src/hivemind/hivemind.js index 47939b3..5b98671 100644 --- a/src/hivemind/hivemind.js +++ b/src/hivemind/hivemind.js @@ -9,6 +9,7 @@ var hivemind = { /** Lets the hivemind run. That means at the start initializing the hivemind's memory or advancing its state*/ run: function () { + this.overlords = []; // fixing issue where the next tick still had this list when the script kept running if (Memory.last_tick === undefined) { hivemind.initialize_memory(); } @@ -21,7 +22,6 @@ var hivemind = { console.log("Hivemind: Saving all overlords ..."); hivemind.serialize_overlords(); - this.overlords = []; // fixing issue where the next tick still had this list when the script kept running } }, diff --git a/src/overlords/overlord.js b/src/overlords/overlord.js index 9a13009..a8e82ca 100644 --- a/src/overlords/overlord.js +++ b/src/overlords/overlord.js @@ -11,8 +11,8 @@ function overlord (name) { this.goal = undefined; this.underlings = { - "spawns": [], - "creeps": [], + "spawns": [], // list of spawn names + "creeps": [], // list of creep names (NOT ids!) This is because creeps have no ID while being spawned. }; /** Serializes the overlord state so that it can survive in memory */ @@ -54,7 +54,7 @@ function overlord (name) { */ this.add_creep = function(creep_name) { let creep = Game.creeps[creep_name]; - if (creep.memory.owner !== undefined) { // creep already owned by someone + if (creep.memory.owner !== undefined && creep.memory.owner !== this.id) { // creep already owned by someone console.log("Overlord: ERROR Creep was already taken!"); throw "Creep already taken!"; } diff --git a/src/overlords/setup_overlord.js b/src/overlords/setup_overlord.js index cebab27..b207c5b 100644 --- a/src/overlords/setup_overlord.js +++ b/src/overlords/setup_overlord.js @@ -17,19 +17,18 @@ setup_overlord.prototype.run = function () { // TODO somehow these aren't chang console.log("SetupOverlord: ERROR "+this.id+" has nothing! ;W;"); } else if (this.underlings['creeps'].length === 0 && this.underlings['spawns'].length > 0) { - console.log("SetupOverlord: \""+this.id+"\" is trying to creat a harvester ..."); + console.log("SetupOverlord: \""+this.id+"\" is trying to create a harvester ..."); - let spawn_name = null; - for (spawn_name of this.underlings['spawns']) { - let spawn = Game.spawns[this.underlings['spawns']]; + for (let spawn_name of this.underlings['spawns']) { + let spawn = Game.spawns[spawn_name]; if (spawn.store[RESOURCE_ENERGY] > harvester.min_cost) { // can afford a harvester console.log("SetupOverlord: Spawn "+spawn.name+" can afford a harvester! ("+harvester.min_cost+") \\o/"); let new_harvester_name = harvester.spawn(spawn, this.id+"-"+this.underlings['creeps'].length, this.id); - this.add_creep(new_harvester_name); + this.add_creep(new_harvester_name); // TODO replace with ID? creep.id is only defined next tick though ... break; } } - //spawn = Game.spawns[this.underlings['spawns']] + //spawn = Game.spawns[this.underlings['spawns']] // TODO set up some goals / overlord tasks } };