forked from VladThePaler/screeps.behaviour-action-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
creep.behaviour.ranger.js
64 lines (62 loc) · 2.09 KB
/
creep.behaviour.ranger.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
let mod = {};
module.exports = mod;
mod.name = 'ranger';
mod.run = function(creep) {
creep.flee = creep.flee || !creep.hasActiveBodyparts([ATTACK, RANGED_ATTACK]);
creep.attacking = false;
creep.attackingRanged = false;
// Assign next Action
let oldTargetId = creep.data.targetId;
if (!creep.action || creep.action.name === 'idle' ||
(creep.action.name === 'guarding' &&
(!creep.flag || creep.flag.pos.roomName === creep.pos.roomName || creep.leaveBorder())
)
) {
if( creep.data.destiny && creep.data.destiny.task && Task[creep.data.destiny.task] && Task[creep.data.destiny.task].nextAction )
Task[creep.data.destiny.task].nextAction(creep);
else this.nextAction(creep);
}
// Do some work
if( creep.action && creep.target ) {
creep.action.step(creep);
} else {
logError('Creep without action/activity!\nCreep: ' + creep.name + '\ndata: ' + JSON.stringify(creep.data));
}
this.heal(creep);
};
mod.heal = function(creep){
if( creep.data.body.heal !== undefined && creep.hits < creep.hitsMax ){
if( !(creep.attacking || creep.hits >= creep.data.coreHits) ) {
creep.heal(creep);
}
}
};
mod.nextAction = function(creep){
let priority = [
Creep.action.defending,
Creep.action.invading,
Creep.action.guarding,
Creep.action.idle
];
for(var iAction = 0; iAction < priority.length; iAction++) {
var action = priority[iAction];
if(action.isValidAction(creep) &&
action.isAddableAction(creep) &&
action.assign(creep)) {
return;
}
}
};
mod.strategies = {
defaultStrategy: {
name: `default-${mod.name}`,
moveOptions: function(options) {
// // allow routing in and through hostile rooms
// if (_.isUndefined(options.allowHostile)) options.allowHostile = true;
return options;
}
}
};
mod.selectStrategies = function(actionName) {
return [mod.strategies.defaultStrategy, mod.strategies[actionName]];
};