This repository was archived by the owner on Aug 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuilder.ts
57 lines (53 loc) · 2.07 KB
/
Builder.ts
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
import WorkerCreep from "./WorkerCreep";
import Settings from "./Settings";
export default class Builder extends WorkerCreep {
constructor(creep: Creep) {
super(creep);
}
gatherEnergy(): void {
const creep = this.creep;
const containers = creep.room.find<Container>(FIND_STRUCTURES, {
filter: (structure) => structure.structureType === STRUCTURE_CONTAINER && _.sum(structure.store) > 0
});
if (containers.length > 0) {
for (let container of containers) {
const outcome = container.transfer(creep, RESOURCE_ENERGY);
if (outcome === ERR_NOT_ENOUGH_RESOURCES) continue;
if (outcome === ERR_NOT_IN_RANGE) {
creep.moveTo(container);
return;
}
if (outcome === OK) return;
}
}
super.gatherEnergy();
}
expendEnergy() {
const creep = this.creep;
const targets = creep.room.find<ConstructionSite>(FIND_CONSTRUCTION_SITES);
if (targets.length) {
if (creep.build(targets[0]) === ERR_NOT_IN_RANGE) {
creep.moveTo(targets[0]);
}
} else {
const damaged = creep.room.find<Structure>(FIND_STRUCTURES,
{
filter: (structure) => structure.hits < structure.hitsMax && structure.structureType !== STRUCTURE_WALL
});
if (damaged.length > 0) {
if (creep.repair(damaged[0]) === ERR_NOT_IN_RANGE) {
creep.moveTo(damaged[0]);
}
} else {
let walls = creep.room.find<Structure>(FIND_STRUCTURES,
{
filter: (structure) => structure.hits < structure.hitsMax && structure.structureType === STRUCTURE_WALL
});
walls = walls.sort((first, second) => first.hits - second.hits);
if (creep.repair(walls[0]) === ERR_NOT_IN_RANGE) {
creep.moveTo(walls[0]);
}
}
}
}
}