1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
// 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);
}
}
if (this.memory.cm == null) {
this.memory.cm = {};
}
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 = {
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");
}
};
|