forked from VladThePaler/screeps.behaviour-action-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
creep.action.dismantling.js
52 lines (51 loc) · 2.08 KB
/
creep.action.dismantling.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
let action = new Creep.Action('dismantling');
module.exports = action;
action.maxPerAction = 3;
action.maxPerTarget = 3;
action.renewTarget = false;
action.isValidAction = function(creep){
return creep.carryCapacity === 0 || creep.sum < creep.carryCapacity;
};
action.isValidTarget = function(target){
return target != null;
};
action.newTarget = function(creep){
let target;
let flag = FlagDir.find(FLAG_COLOR.destroy.dismantle, creep.pos, true);
if( flag ){
if( flag.room !== undefined ){ // room is visible
var targets = flag.room.lookForAt(LOOK_STRUCTURES, flag.pos.x, flag.pos.y);
if( targets && targets.length > 0)
return targets[0];
else { // remove flag. try next flag
let oldName = flag.name;
FlagDir.removeFromDir(flag.name);
flag.remove();
let otherFlagMod = (range, flagItem, args) => {
if(flagItem.name == args) return Infinity;
return range;
};
flag = FlagDir.find(FLAG_COLOR.destroy.dismantle, creep.pos, true, otherFlagMod, oldName);
if( oldName == flag.name ) logError('Removed flag found again in dismantling.newTarget!');
if( flag ){
if( flag.room !== undefined ){ // room is visible
var targets = flag.room.lookForAt(LOOK_STRUCTURES, flag.pos.x, flag.pos.y);
if( targets && targets.length > 0)
return targets[0];
else { // remove flag. try next flag
Room.costMatrixInvalid.trigger(flag.room);
FlagDir.removeFromDir(flag.name);
flag.remove();
}
}
else target = flag; // target in other room
}
}
}
else target = flag; // target in other room
}
return target;
};
action.work = function(creep){
return creep.dismantle(creep.target);
};