-
Notifications
You must be signed in to change notification settings - Fork 0
/
creep.role.cachedTargetTemplate.js
66 lines (58 loc) · 2.28 KB
/
creep.role.cachedTargetTemplate.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
// Template pattern expressed using composition for a creep role with a cached target.
// primitives: {
// String roleMessage,
// function harvestEnergy(creep),
// function findTarget(creep),
// function validateTarget(target),
// function executeRole(creep, target),
// function idle(creep)
// }
/**
* Create a template object for the given primitive implementation.
*
* @param {Object} primitives - The implementation of the role.
* @returns {Object} An object with a run(creep) method.
*/
module.exports.create = function (primitives) {
return {
/**
* Run the role for the specified creep.
*
* @param {Creep} creep - The creep to run.
*/
run: function (creep) {
if (!creep.isCarryingMaximumEnergy()) {
var tombstoneOrPickup = creep.withdrawNearByTombstoneEnergy() || creep.pickupNearByDroppedEnergy();
}
if (creep.memory.executingRole && creep.isCarryingZeroEnergy()) {
creep.memory.executingRole = false;
creep.memory.cachedTargetId = undefined;
creep.say('⛏️harvest');
}
if (!creep.memory.executingRole && creep.isCarryingMaximumEnergy()) {
creep.memory.executingRole = true;
creep.say(primitives.roleMessage);
}
if (creep.memory.executingRole) {
let target;
if (creep.memory.cachedTargetId) {
target = Game.getObjectById(creep.memory.cachedTargetId)
if (!target || !primitives.validateTarget(target)) {
target = creep.memory.cachedTargetId = undefined;
}
}
if (!target) {
target = primitives.findTarget(creep);
}
if (target) {
creep.memory.cachedTargetId = target.id;
primitives.executeRole(creep, target);
} else {
primitives.idle(creep);
}
} else if (!tombstoneOrPickup) {
primitives.harvestEnergy(creep);
}
}
};
};