forked from VladThePaler/screeps.behaviour-action-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreep.action.withdrawing.js
39 lines (37 loc) · 1.3 KB
/
creep.action.withdrawing.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
let action = new Creep.Action('withdrawing');
module.exports = action;
action.isValidAction = function(creep){
return (
creep.room.storage &&
creep.room.storage.store.energy > 0 &&
creep.data.creepType != 'privateer' &&
creep.sum < creep.carryCapacity &&
(!creep.room.conserveForDefense || creep.room.relativeEnergyAvailable < 0.8)
);
};
action.isValidTarget = function(target){
return ( (target != null) && (target.store != null) && (target.store.energy > 0) );
};
action.newTarget = function(creep){
return creep.room.storage;
};
action.work = function(creep){
return creep.withdraw(creep.target, RESOURCE_ENERGY);
};
action.onAssignment = function(creep, target) {
//if( SAY_ASSIGNMENT ) creep.say(String.fromCharCode(9738), SAY_PUBLIC);
if( SAY_ASSIGNMENT ) creep.say('\u{1F4E4}\u{FE0E}', SAY_PUBLIC);
};
action.debounce = function(creep, outflowActions, callback, thisArg) {
let shouldCall = false;
if (creep.data.lastAction === 'storing' && creep.data.lastTarget === creep.room.storage.id) {
// cycle detected
shouldCall = _.some(outflowActions, a => a.newTarget(creep));
} else {
shouldCall = true;
}
if (shouldCall) {
return _.invoke([thisArg], callback, this)[0];
}
return undefined;
};