summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/creep.js175
-rw-r--r--src/main.js26
-rw-r--r--src/role.builder.js110
-rw-r--r--src/role.harvester.js6
-rw-r--r--src/role.offender.js2
-rw-r--r--src/role.spawner.js19
-rw-r--r--src/role.upgrader.js8
7 files changed, 302 insertions, 44 deletions
diff --git a/src/creep.js b/src/creep.js
index 48688ec..9725dc9 100644
--- a/src/creep.js
+++ b/src/creep.js
@@ -1,5 +1,176 @@
+// vim: tw=80 ts=2 sw=2 et
+
+const _ = require("lodash");
+
+const states = {
+ IDLE: 1,
+ COLLECTING: "collecting",
+ MAINTINING: 3,
+ BUILDING: 4
+};
+
+Room.prototype.createCostMatrices = function() {
+ let roadGrid = new PathFinder.CostMatrix();
+ let structGrid = new PathFinder.CostMatrix();
+
+ for (let i = 0; i < 625; i++) {
+ if (i % 2 === 0) {
+ roadGrid.set(i % 25, Math.floor(i / 25), 20);
+ } else {
+ structGrid.set(i % 25, Math.floor(i / 25), 20);
+ }
+ }
+
+ this.memory.cm.road = roadGrid;
+ this.memory.cm.struct = structGrid;
+}
+
+Structure.prototype.log = function(msg) {
+ console.log(`${this.name}: ${msg}`);
+};
+
+Creep.prototype.isFull = function() {
+ return _.sum(this.carry) >= this.carryCapacity;
+}
+
+Creep.prototype.log = function(msg, say) {
+ this.say(say || msg);
+ console.log(`${this.name}: ${msg}`);
+};
+
+Creep.prototype.error = function(msg, say) {
+ this.log(msg, say || "???");
+};
+
+Creep.prototype.construct = function() {
+ const targetID = this.memory.target.id;
+ const target = Game.getObjectById(targetID);
+
+ const res = this.build(target);
+ switch (res) {
+ case OK:
+ break;
+ case ERR_NOT_IN_RANGE:
+ this.moveTo(target);
+ break;
+ default:
+ this.log(res);
+ this.goIdle();
+ }
+};
+
+Creep.prototype.maintain = function() {
+ const target = this.memory.target;
+
+ switch (this.repair(target)) {
+ case OK:
+ break;
+ case ERR_NOT_IN_RANGE:
+ this.moveTo(target);
+ break;
+ default:
+ return this.goIdle();
+ }
+};
+
+Creep.prototype.collect = function() {
+ const targetID = this.memory.target.id;
+ // const target = Game.getObjectById(targetID);
+ const target = Game.spawns['Spawn1'];
+
+ if (this.isFull()) {
+ return this.goIdle();
+ }
+
+ const res = this.withdraw(target, RESOURCE_ENERGY);
+ switch (res) {
+ case OK:
+ break;
+ case ERR_NOT_IN_RANGE:
+ this.moveTo(target);
+ break;
+ default:
+ // goIdle(this);
+ }
+};
+
+Creep.prototype.goIdle = function() {
+ this.memory.state = states.IDLE;
+ delete this.memory.target;
+};
+
+Creep.prototype.needsNewPath = function(target) {
+ if (this.memory.path == null) {
+ return true;
+ }
+
+ if (this.memory.path.length == 0) {
+ return true;
+ }
+
+ const endOfPath = this.memory.path[this.memory.path.length - 1];
+
+ /*
+ * Not entirely sure what was happening, but for some reason a creep had an
+ * empty object for a path, which I couldn't replace. This check prevents that
+ * issue from breaking the creep's pathfinding.
+ */
+ if (endOfPath == null) {
+ return true;
+ }
+
+ if (!target.pos.isNearTo(endOfPath.x, endOfPath.y)) {
+ return true;
+ }
+
+ return false;
+}
+
+Creep.prototype._moveTo = Creep.prototype.moveTo;
+Creep.prototype.moveTo = function(target, opts) {
+
+ /*
+ * TODO
+ * Currently, creeps are getting stuck behind each other, because this system
+ * doesn't recalculate the path if things are in the way. Might be worth
+ * storing in each creep's memory whether or not it's moving, as if it's
+ * stationary it can block an entire queue of creeps.
+ */
+
+ if (this.needsNewPath(target)) {
+ // this.log(`moving to ${target.pos}`, `->${target.pos.x},${target.pos.y}`);
+ const pathfinderOpts = {
+ visualizePathStyle: {},
+ ...opts
+ };
+ this.memory.path = this.pos.findPathTo(target, pathfinderOpts);
+ }
+
+ return this.moveByPath(this.memory.path);
+};
+
module.exports = {
- move: (creep, target, opts) => {
- creep.moveTo(target, { visualizePathStyle: {}, ...opts });
+ resetCreepField: (field, set) => {
+ for (const name in Game.creeps) {
+ const creep = Game.creeps[name];
+ if (set === undefined) {
+ delete creep.memory[field];
+ } else {
+ Memory.creeps[name][field] = set;
+ // creep.memory.field = set;
+ console.log(Memory.creeps[name][field]);
+ }
+ }
+ },
+ buildRoadsBetween: (a, b) => {
+ const pfres = PathFinder.search(a, b, {
+ plainCost: 1,
+ swampCost: 1
+ });
+
+ pfres.path.forEach((p) => p.createConstructionSite(STRUCTURE_ROAD));
+ },
+ amILoaded: () => {
+ console.log("yes");
}
};
diff --git a/src/main.js b/src/main.js
index 398ce5c..a4ff3b9 100644
--- a/src/main.js
+++ b/src/main.js
@@ -1,3 +1,5 @@
+// vim: tw=80 ts=2 sw=2 et
+
const jobs = {
harvester: require('role.harvester'),
upgrader: require('role.upgrader'),
@@ -6,20 +8,28 @@ const jobs = {
offender: require('role.offender')
};
+Memory.counts = {};
+
module.exports.loop = () => {
- for (const job in Memory.counts) {
+ for (const job in jobs) {
Memory.counts[job] = 0;
}
- for (const name in Game.creeps) {
- const creep = Game.creeps[name];
- const job = jobs[creep.memory.job];
- Memory.counts[creep.memory.job]++;
- if (job) {
- job.run(creep);
+
+ for (const name in Memory.creeps) {
+ if (!Game.creeps[name]) {
+ delete Memory.creeps[name];
} else {
- error.log(`${creep.name} cannot find ${creep.memory.job} instructions`);
+ const creep = Game.creeps[name];
+ const job = jobs[creep.memory.job];
+ Memory.counts[creep.memory.job]++;
+ if (job) {
+ job.run(creep);
+ } else {
+ creep.error(`cannot find ${creep.memory.job} instructions`);
+ }
}
}
+
for (const name in Game.spawns) {
jobs.spawner.run(Game.spawns[name]);
}
diff --git a/src/role.builder.js b/src/role.builder.js
index f243eac..fd183da 100644
--- a/src/role.builder.js
+++ b/src/role.builder.js
@@ -1,31 +1,87 @@
+// vim: tw=80 ts=2 sw=2 et
+
const util = require('creep');
-module.exports = {
- run: (creep) => {
- if (creep.memory.state === "building") {
- if (creep.carry.energy > 0) {
- // still building
- const targets = creep.room.find(FIND_CONSTRUCTION_SITES);
- if (targets.length) {
- if (creep.build(targets[0]) == ERR_NOT_IN_RANGE) {
- util.move(creep, targets[0]);
- }
- }
- } else {
- // just finished building
- creep.memory.state = "collecting";
- }
- } else {
- if (creep.carry.energy < creep.carryCapacity) {
- // still filling up
- const spawn = Game.spawns['Initial'];
- if (creep.withdraw(spawn, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
- util.move(creep, spawn);
- }
- } else {
- // just finished filling up
- creep.memory.state = "building";
- }
- }
+const states = {
+ IDLE: 1,
+ COLLECTING: "collecting",
+ MAINTINING: 3,
+ BUILDING: 4
+};
+
+/*
+ * these three functions are a bit repetitve, might be enough to use the state
+ * value as a key to the right method to use.
+ * TODO
+ */
+function findWork(creep) {
+
+ /*
+ * first, fill up on power, if we're not full
+ */
+ if (!creep.isFull()) {
+ const target = creep.room.find(FIND_SOURCES_ACTIVE)[0];
+
+ creep.memory.target = target;
+ creep.memory.state = states.COLLECTING;
+ creep.log(
+ `collecting energy from ${target}`,
+ `${target.pos.x},${target.pos.y}`);
+ return creep.collect();
+ }
+
+ /*
+ * if we're full, then find a building to repair
+ */
+ const damagedBuildings = creep.room.find(FIND_MY_STRUCTURES, {
+ filter: (s) => s.hits < s.hitsMax * 0.5,
+ });
+ if (damagedBuildings.length) {
+ const target = damagedBuildings[0];
+ creep.memory.target = target;
+ creep.memory.state = states.MAINTAINING;
+ creep.log(
+ `repairing ${target}`,
+ `${target.pos.x},${target.pos.y}`);
+ return creep.maintain();
}
+
+ /*
+ * if there's nothing to fix, find something to construct
+ */
+ const constructionSites = creep.room.find(FIND_MY_CONSTRUCTION_SITES);
+ if (constructionSites.length) {
+ const target = constructionSites[0];
+ creep.memory.target = target;
+ creep.memory.state = states.BUILDING;
+ creep.log(`building ${target}`, `${target.pos.x},${target.pos.y}`);
+ return creep.construct();
+ }
+
+ /*
+ * if there's nothing else to do, continue being idle
+ */
+};
+
+function run(creep) {
+ switch (creep.memory.state) {
+ case states.BUILDING:
+ creep.construct();
+ break;
+ case states.COLLECTING:
+ creep.collect();
+ break;
+ case states.MAINTAINING:
+ creep.maintain();
+ break;
+ case states.IDLE:
+ findWork(creep);
+ break;
+ default:
+ creep.error(`error: state ${creep.memory.state} unrecognised`);
+ }
+};
+
+module.exports = {
+ run: run
};
diff --git a/src/role.harvester.js b/src/role.harvester.js
index 31e05d1..5c6f85c 100644
--- a/src/role.harvester.js
+++ b/src/role.harvester.js
@@ -1,3 +1,5 @@
+// vim: tw=80 ts=2 sw=2 et
+
const util = require('creep');
module.exports = {
@@ -5,7 +7,7 @@ module.exports = {
if (creep.carry.energy < creep.carryCapacity) {
const source = creep.pos.findClosestByPath(FIND_SOURCES);
if (creep.harvest(source) == ERR_NOT_IN_RANGE) {
- util.move(creep, source);
+ creep.moveTo(source);
}
} else {
const spawn = creep.pos.findClosestByPath(FIND_MY_STRUCTURES, {
@@ -16,7 +18,7 @@ module.exports = {
}
});
if (creep.transfer(spawn, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
- util.move(creep, spawn);
+ creep.moveTo(spawn);
}
}
}
diff --git a/src/role.offender.js b/src/role.offender.js
index 91d131e..8cb95ac 100644
--- a/src/role.offender.js
+++ b/src/role.offender.js
@@ -1,3 +1,5 @@
+// vim: tw=80 ts=2 sw=2 et
+
const util = require('creep');
module.exports = {
diff --git a/src/role.spawner.js b/src/role.spawner.js
index e609612..9d1eebf 100644
--- a/src/role.spawner.js
+++ b/src/role.spawner.js
@@ -1,13 +1,28 @@
+// vim: tw=80 ts=2 sw=2 et
+
const creeps = new Map([
[ 'upgrader', 1 ],
- [ 'offender', 3 ],
+ [ 'offender', 0 ],
[ 'builder', 3 ],
[ 'harvester', 5 ]
]);
const spawnCreep = (spawn, job, tools) => {
const name = job + Game.time;
- if (spawn.spawnCreep(tools, name, { memory: { job: job } }) == OK) {
+ const new_memory = {
+ job: job,
+ path: {},
+
+ /*
+ * TODO: Is it better to keep the spawner's name (for retreival via
+ * Game.spawns[]) or to keep its id (for retrieval via
+ * Game.getObjectById())?
+ */
+ spawned_from: spawn.name
+ };
+
+ if (spawn.spawnCreep(tools, name, { memory: new_memory }) == OK) {
+ spawn.log(`spawning a new ${job}`);
Memory.counts[job]++;
}
};
diff --git a/src/role.upgrader.js b/src/role.upgrader.js
index 26ccdf6..baf8ffd 100644
--- a/src/role.upgrader.js
+++ b/src/role.upgrader.js
@@ -1,11 +1,13 @@
+// vim: tw=80 ts=2 sw=2 et
+
const util = require('creep');
module.exports = {
run: (creep) => {
if (creep.memory.state === "withdrawing") {
- const source = Game.spawns['Initial'];
+ const source = Game.spawns[creep.memory.spawned_from];
if (creep.withdraw(source, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
- util.move(creep, source);
+ creep.moveTo(source);
}
if (creep.carry.energy == creep.carryCapacity) {
creep.memory.state = "upgrading";
@@ -13,7 +15,7 @@ module.exports = {
} else {
const controller = creep.room.controller;
if (creep.upgradeController(controller) == ERR_NOT_IN_RANGE) {
- util.move(creep, controller, { range: 3 });
+ creep.moveTo(controller, { range: 3 });
}
if (creep.carry.energy == 0) {
creep.memory.state = "withdrawing";