forked from VladThePaler/screeps.behaviour-action-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
creep.action.feeding.js
34 lines (34 loc) · 1.32 KB
/
creep.action.feeding.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
let action = new Creep.Action('feeding');
module.exports = action;
action.maxPerTarget = 1;
action.isValidAction = function(creep){
return ( creep.carry.energy > 0 && creep.room.energyAvailable < creep.room.energyCapacityAvailable );
};
action.isValidTarget = function(target){
return ( (target != null) && (target.energy != null) && (target.energy < target.energyCapacity) );
};
action.isAddableTarget = function(target){
return ( target.my &&
(!target.targetOf || _.filter(target.targetOf, {'actionName':'feeding'}).length < this.maxPerTarget));
};
action.newTarget = function(creep){
if (creep.room.energyAvailable === creep.room.energyCapacityAvailable) {
return null;
}
var that = this;
return creep.pos.findClosestByRange(creep.room.structures.all, {
filter: (structure) => {
return ((structure.structureType == STRUCTURE_EXTENSION ||
structure.structureType == STRUCTURE_SPAWN )
&& that.isValidTarget(structure) && that.isAddableTarget(structure, creep));
}
});
};
action.work = function(creep){
let result = creep.transfer(creep.target, RESOURCE_ENERGY);
if (result == OK && creep.carry.energy > creep.target.energyCapacity-creep.target.energy) {
creep.target = null;
this.assign(creep);
}
return result;
};