summaryrefslogtreecommitdiff
path: root/src/creep.js
diff options
context:
space:
mode:
authorTom Harley2019-10-15 02:14:27 +0100
committerTom Harley2019-10-15 02:14:27 +0100
commit511fb4e6bd8a5e736f91b1af676eec94257be462 (patch)
tree0e0a1c1e7f2da020f0745a994082d90019a49e44 /src/creep.js
parentExtension-aware harvester energy transportation (diff)
downloadscreeps-ai-511fb4e6bd8a5e736f91b1af676eec94257be462.tar.gz
screeps-ai-511fb4e6bd8a5e736f91b1af676eec94257be462.zip

Various changes to pathfinding

Diffstat (limited to 'src/creep.js')
-rw-r--r--src/creep.js175
1 files changed, 173 insertions, 2 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");
}
};