forked from VladThePaler/screeps.behaviour-action-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
creep.action.attackController.js
68 lines (60 loc) · 2.3 KB
/
creep.action.attackController.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
let action = new Creep.Action('attackController');
module.exports = action;
action.isValidAction = function(creep){ return true; };
action.isValidTarget = function(target, creep){ return target && (!target.reservation || !Task.reputation.allyOwner(target.reservation)) && creep.flag; };
action.isAddableAction = function(){ return true; };
action.isAddableTarget = function(target){ return target &&
( target instanceof Flag || ( target.structureType === 'controller' && (target.reservation || target.owner)) );
};
action.newTarget = function(creep){
let validColor = flagEntry => (
Flag.compare(flagEntry, FLAG_COLOR.invade.attackController)
);
var flag;
if( creep.data.destiny ) flag = Game.flags[creep.data.destiny.targetName];
if ( !flag ) flag = FlagDir.find(validColor, creep.pos, false, FlagDir.reserveMod, creep.name);
if( flag ) {
Population.registerCreepFlag(creep, flag);
}
else return null;
// not there, go to flagged room
if( !creep.flag.room || creep.flag.pos.roomName != creep.pos.roomName){
return creep.flag;
}
return creep.flag.room.controller;
};
action.step = function(creep){
if(CHATTY) creep.say(this.name, SAY_PUBLIC);
if( creep.target.color ){
if( creep.flag.pos.roomName == creep.pos.roomName )
creep.data.targetId = null;
creep.travelTo(creep.target);
return;
}
let range = creep.pos.getRangeTo(creep.target);
if( range <= this.targetRange ) {
var workResult = this.work(creep);
if( workResult != OK ) {
creep.handleError({errorCode:workResult,action,target:creep.target,range,creep});
}
} else {
creep.travelTo(creep.target);
}
};
action.work = function(creep){
var workResult;
creep.controllerSign();
if( (creep.target.owner && !creep.target.my) ||
(creep.target.reservation && !Task.reputation.allyOwner(creep.target.reservation))){
workResult = creep.attackController(creep.target);
}
else {
workResult = creep.claimController(creep.target);
}
return workResult;
};
action.defaultStrategy.moveOptions = function(options) {
// // allow routing in and through hostile rooms
// if (_.isUndefined(options.allowHostile)) options.allowHostile = true;
return options;
};