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.
main
Peery 2 years ago
parent cbc99ae65b
commit 167f20d8b3

@ -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]);
},
};

@ -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
}
},

@ -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!";
}

@ -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
}
};

Loading…
Cancel
Save