forked from VladThePaler/screeps.behaviour-action-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
creep.action.defending.js
87 lines (86 loc) · 3.5 KB
/
creep.action.defending.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
let action = new Creep.Action('defending');
module.exports = action;
action.isValidAction = function(creep){ return creep.room.hostiles.length > 0; };
action.isAddableAction = function(){ return true; };
action.isAddableTarget = function(){ return true; };
action.isValidTarget = function(target){
return (
target &&
target.hits != null &&
target.hits > 0 &&
target.my == false );
};
action.newTarget = function(creep){
var closestHostile = creep.pos.findClosestByRange(creep.room.hostiles, {
function(hostile){ return _.some(hostile.body, {'type': HEAL}); }
});
if(!closestHostile) {
closestHostile = creep.pos.findClosestByRange(creep.room.hostiles);
}
return closestHostile;
};
action.step = function(creep){
if(CHATTY) creep.say(this.name, SAY_PUBLIC);
this.run[creep.data.creepType](creep);
};
action.run = {
ranger: function(creep) {
let range = creep.pos.getRangeTo(creep.target);
if( !creep.flee ){
if( range > 3 ){
let path = creep.room.findPath(creep.pos, creep.target.pos, {ignoreCreeps: false});
if( path && path.length > 0 ) {
let isRampart = COMBAT_CREEPS_RESPECT_RAMPARTS && _.some( creep.room.lookForAt(LOOK_STRUCTURES, path[0].x, path[0].y), {'structureType': STRUCTURE_RAMPART });
if(!isRampart){
creep.move(path[0].direction);
}
} else {
// no path -> try to move by direction
let direction = creep.pos.getDirectionTo(creep.target);
if( direction ) creep.move(direction);
}
}
if( range < 3 ){
let direction = creep.target.pos.getDirectionTo(creep);
if( direction ) creep.move(direction);
}
}
// attack ranged
let targets = creep.pos.findInRange(creep.room.hostiles, 3);
if(targets.length > 2) { // TODO: precalc damage dealt
if(CHATTY) creep.say('MassAttack');
creep.attackingRanged = creep.rangedMassAttack() == OK;
return;
}
if( range < 4 ) {
creep.attackingRanged = creep.rangedAttack(creep.target) == OK;
return;
}
if(targets.length > 0){
creep.attackingRanged = creep.rangedAttack(targets[0]) == OK;
}
},
melee: function(creep) {
if( !creep.flee ){
let path = creep.room.findPath(creep.pos, creep.target.pos);
// not standing in rampart or next step is rampart as well
if( path && path.length > 0 && (
!COMBAT_CREEPS_RESPECT_RAMPARTS ||
!_.some( creep.room.lookForAt(LOOK_STRUCTURES, creep.pos.x, creep.pos.y), {'structureType': STRUCTURE_RAMPART } ) ||
_.some( creep.room.lookForAt(LOOK_STRUCTURES, path[0].x, path[0].y), {'structureType': STRUCTURE_RAMPART }))
){
creep.move(path[0].direction);
}
}
// attack
let attacking = creep.attack(creep.target);
if( attacking == ERR_NOT_IN_RANGE ) {
let targets = creep.pos.findInRange(creep.room.hostiles, 1);
if( targets.length > 0)
creep.attacking = creep.attack(targets[0]) == OK;
} else creep.attacking = attacking == OK;
}
};
action.onAssignment = function(creep, target) {
if( SAY_ASSIGNMENT ) creep.say(String.fromCharCode(9876), SAY_PUBLIC);
};