-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrole.harvester.js
83 lines (73 loc) · 2.08 KB
/
role.harvester.js
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
const CreepBase = require('./creep.base');
const STATUSES = require('./creep.status');
const ROLE = 'harvester';
const BODY_PARTS = [WORK, WORK, CARRY, MOVE];
const MIN_AMOUNT = 2;
class RoleHarvester extends CreepBase {
constructor() {
super(ROLE, BODY_PARTS, MIN_AMOUNT);
}
run(creep) {
if (!this.hasRole(creep)) return false;
switch (creep.memory.status) {
case STATUSES.Recharge:
this.runRechargeProcess(creep);
break;
case STATUSES.Idle:
case STATUSES.Harvest:
this.runHarvestProcess(creep);
break;
default:
console.log(
this.getCreepName(creep),
`⛔ status not handled: ${creep.memory.status}`
);
}
}
runRechargeProcess(creep) {
if (creep.store[RESOURCE_ENERGY] > 0) {
this.recharge(creep);
} else {
this.setStatus(creep, STATUSES.Harvest);
}
}
runHarvestProcess(creep) {
if (creep.store.getFreeCapacity() > 0) {
this.harvest(creep);
} else {
this.recharge(creep);
}
}
recharge(creep) {
let source = this.getCurrentTarget(creep);
if (!source) {
const targets = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return (
(structure.structureType === STRUCTURE_EXTENSION ||
structure.structureType === STRUCTURE_SPAWN ||
structure.structureType === STRUCTURE_TOWER) &&
structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0
);
},
});
source = (targets.length) ? targets[0] : undefined;
}
if (source) {
const code = creep.transfer(source, RESOURCE_ENERGY);
switch (code) {
case ERR_NOT_IN_RANGE:
this.moveTo(creep, source, '#169612');
break;
case OK:
creep.say('🔋 charge');
this.setStatus(creep, STATUSES.Recharge);
this.removeCurrentTarget(creep);
break;
default:
console.log(this.getCreepName(creep), `⛔ code not handled: ${code}`);
}
}
}
}
module.exports = new RoleHarvester();