Added WIP SetupOverlord
Added an overlord for the room setup which creates the first creeps and starts harvesting energy. Currently still WIP.main
parent
1f5296ad26
commit
c211e84801
@ -1,13 +1,43 @@
|
|||||||
|
const { sum } = require("lodash");
|
||||||
|
|
||||||
var roleHarvester = {
|
var roleHarvester = {
|
||||||
|
|
||||||
minimum_parts: [WORK, MOVE, CARRY],
|
minimum_parts: [WORK, MOVE, CARRY],
|
||||||
recommended_parts: [WORK, MOVE, CARRY],
|
recommended_parts: [WORK, MOVE, CARRY],
|
||||||
|
min_cost: sum([BODYPART_COST["work"], BODYPART_COST["move"], BODYPART_COST["carry"]]),
|
||||||
|
|
||||||
/* runs every tick for each creep belonging to the harvester role */
|
/**
|
||||||
// creep: name of current creep
|
* Orders the given spawn to create a creep fitting for this role according to the recommended_parts and if impossible use minimum_parts instead
|
||||||
run: function(creep) {
|
* @param {spawn} spawn
|
||||||
console.log("roleHarvester: run() " + creep)
|
* @param {string} postfix string to postfix to the creep
|
||||||
}
|
* @param {string} owner_id strind id of who shall be entered as owner of the new creep
|
||||||
|
*
|
||||||
|
* @return {string} name of the new creep
|
||||||
|
*/
|
||||||
|
spawn: function(spawn, postfix, owner_id) {
|
||||||
|
if (spawn.spawnCreep(this.recommended_parts, "harvester-"+postfix, {dryRun: true}) === OK) {
|
||||||
|
console.log("roleHarvester: Spawn "+spawn.name+" is creating harvester-"+postfix+" with recommended parts ...");
|
||||||
|
spawn.spawnCreep(this.recommended_parts, "harvester-"+postfix, {memory: {"owner": owner_id, "role": "Harv"}});
|
||||||
|
}
|
||||||
|
else if (spawn.spawnCreep(this.minimum_parts, "harvester-"+postfix, {dryRun: true}) === OK) {
|
||||||
|
console.log("roleHarvester: Spawn "+spawn.name+" is creating harvester-"+postfix+" with minimum parts ...");
|
||||||
|
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:");
|
||||||
|
throw "Not enough energy!";
|
||||||
|
}
|
||||||
|
|
||||||
|
return "harvester-"+postfix;
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Runs the logic and actions of the harvester
|
||||||
|
* @param {string} creep_name
|
||||||
|
*/
|
||||||
|
run: function(creep_name) {
|
||||||
|
console.log("roleHarvester: run() " + creep_name)
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = roleHarvester;
|
module.exports = roleHarvester;
|
@ -1,22 +1,73 @@
|
|||||||
/**
|
/**
|
||||||
* Overlord that sets up a room given only a single spawn with a minimum energy to build one harvester.
|
* Generic overlord prototype
|
||||||
*
|
|
||||||
* Setup is considered finished once 3 spawns have been built and at least one creep is maintaining the RCL
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function overlord (name, goal) { // TODO can I do makeshift inheritance?
|
function overlord (name) {
|
||||||
|
|
||||||
/* Unique identifier, set at creation or by deserialize() */
|
/** Unique identifier, set at creation or by deserialize() */
|
||||||
this.id = name;
|
this.id = name;
|
||||||
|
|
||||||
/* Goal the overlord follows */
|
/** Goal the overlord follows */
|
||||||
this.goal = goal;
|
this.goal = undefined;
|
||||||
|
|
||||||
/* Serializes the overlord state so that it can survive in memory */
|
this.underlings = {
|
||||||
this.serialize = function(memory_key) {};
|
"spawns": [],
|
||||||
|
"creeps": [],
|
||||||
|
};
|
||||||
|
|
||||||
|
/** Serializes the overlord state so that it can survive in memory */
|
||||||
|
this.serialize = function() {
|
||||||
|
var data = {
|
||||||
|
"id": this.id,
|
||||||
|
"goal": this.goal,
|
||||||
|
"underlings": this.underlings,
|
||||||
|
};
|
||||||
|
Memory.overlords[this.id] = data;
|
||||||
|
console.log("Overlord: Wrote overlord "+this.id+" to memory!");
|
||||||
|
};
|
||||||
|
|
||||||
|
/** Reconstructs the overlord state from memory given its identifier */
|
||||||
|
this.deserialize = function() {
|
||||||
|
this.goal = Memory.overlords[this.id]["goal"];
|
||||||
|
this.underlings = Memory.overlords[this.id]["underlings"];
|
||||||
|
|
||||||
|
console.log("Overlord: Read overlord "+this.id+" from memory!");
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function to add a spawn to the underlings of this overlord
|
||||||
|
* @param {string} spawn_name
|
||||||
|
*/
|
||||||
|
this.add_spawn = function(spawn_name) {
|
||||||
|
let spawn = Game.spawns[spawn_name];
|
||||||
|
if (spawn.memory.owner !== undefined) { // spawn already owned by someone
|
||||||
|
throw "Spawn already taken!";
|
||||||
|
}
|
||||||
|
spawn.memory.owner = this.id;
|
||||||
|
this.underlings["spawns"].push(spawn_name);
|
||||||
|
console.log("Overlord: assigned spawn \""+spawn_name+"\" to overlord \""+this.id+"\"!");
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function to add a creep to the underlings of this overlord
|
||||||
|
* @param {string} creep_name
|
||||||
|
*/
|
||||||
|
this.add_creep = function(creep_name) {
|
||||||
|
let creep = Game.creeps[creep_name];
|
||||||
|
if (creep.memory.owner !== undefined) { // creep already owned by someone
|
||||||
|
console.log("Overlord: ERROR Creep was already taken!");
|
||||||
|
throw "Creep already taken!";
|
||||||
|
}
|
||||||
|
creep.memory.owner = this.id;
|
||||||
|
this.underlings["creeps"].push(creep_name);
|
||||||
|
console.log("Overlord: assigned creep \""+creep_name+"\" to overlord \""+this.id+"\"!");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* run function to be implemented by non-generic overlords
|
||||||
|
*/
|
||||||
|
//this.run = function () {console.log("Trying to run a generic overlord! Bad!");};
|
||||||
|
|
||||||
/* Reconstructs the overlord state from memory given its identifier */
|
|
||||||
this.deserialize = function(memory_key) {};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = overlord;
|
module.exports = overlord;
|
@ -0,0 +1,36 @@
|
|||||||
|
var setup_overlord = require('overlords_overlord');
|
||||||
|
var harvester = require('creeps_harvester');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Overlord that sets up a room given only a single spawn with a minimum energy to build one harvester.
|
||||||
|
*
|
||||||
|
* Setup is considered finished once 3 spawns have been built and at least one creep is maintaining the RCL
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
/** Checks the room and sets out the tasks to achieve the goal */
|
||||||
|
setup_overlord.prototype.run = function () { // TODO somehow these aren't changing anything
|
||||||
|
console.log("SetupOverlord: Running \""+this.id+"\"! :) Chilling for now ...");
|
||||||
|
console.log("SetupOverlord: has the following underlings: ", this.underlings['spawns'], this.underlings['creeps']);
|
||||||
|
|
||||||
|
if (this.underlings['creeps'].length === 0 && this.underlings['spawns'].length === 0) { // got NOTHING to work with
|
||||||
|
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 ...");
|
||||||
|
|
||||||
|
let spawn_name = null;
|
||||||
|
for (spawn_name of this.underlings['spawns']) {
|
||||||
|
let spawn = Game.spawns[this.underlings['spawns']];
|
||||||
|
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+"-0", this.id);
|
||||||
|
this.add_creep(new_harvester_name);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//spawn = Game.spawns[this.underlings['spawns']]
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = setup_overlord;
|
Loading…
Reference in New Issue