-
Notifications
You must be signed in to change notification settings - Fork 38
/
creep.action.invading.js
127 lines (127 loc) · 4.51 KB
/
creep.action.invading.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
var action = new Creep.Action('invading');
action.reusePath = 0;
action.isValidAction = function(){ return true; };
action.isAddableAction = function(){ return true; };
action.isAddableTarget = function(){ return true; };
action.getFlaggedStructure = function(flagColor){
var flags = _.filter(Game.flags, flagColor.filter); // TODO: find nearest (in room or count rooms?)
var target = null;
for( var iFlag = 0; iFlag < flags.length; iFlag++ ){
var flag = flags[iFlag];
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]; // prefer target in same room so return
else { // remove flag. try next flag
flag.remove();
}
}
else target = flag; // target in other room
}
return target;
}
action.newTarget = function(creep){
var destroyFlag = this.getFlaggedStructure(FLAG_COLOR.destroy);
if( destroyFlag ) return destroyFlag;
// move to invasion room
var flag = _.find(Game.flags, FLAG_COLOR.invade.filter);
if( flag && (!flag.room || flag.room.name != creep.room.name))
return flag; // other room
if( !flag ){
// unregister
creep.action = null;
return;
}
if( !flag.room.controller || !flag.room.controller.my ) {
//attack healer
var target = creep.pos.findClosestByRange(FIND_HOSTILE_CREEPS, {
filter: function(hostile){ return _.some(hostile.body, {'type': HEAL}); }
});
if( target )
return target;
//attack attacker
target = creep.pos.findClosestByRange(FIND_HOSTILE_CREEPS, {
filter: function(hostile){ return _.some(hostile.body, function(part){return part.type == ATTACK || part.type == RANGED_ATTACK}); }
});
if( target )
return target;
// attack tower
target = creep.pos.findClosestByPath(FIND_HOSTILE_STRUCTURES, {
filter: (structure) => {
return structure.structureType == STRUCTURE_TOWER;
}
});
if( target )
return target;
// attack remaining creeps
target = creep.pos.findClosestByRange(FIND_HOSTILE_CREEPS);
if( target )
return target;
// attack spawn
target = creep.pos.findClosestByPath(FIND_HOSTILE_STRUCTURES, {
filter: (structure) => {
return structure.structureType == STRUCTURE_SPAWN;
}
});
if( target )
return target;
// attack structures
target = creep.pos.findClosestByPath(FIND_HOSTILE_STRUCTURES, {
filter: (structure) => {
return structure.structureType != STRUCTURE_CONTROLLER;
}
});
if( target )
return target;
// attack construction sites
target = creep.pos.findClosestByPath(FIND_HOSTILE_CONSTRUCTION_SITES);
if( target )
return target;
}
// no target found
flag.remove();
return null;
};
action.step = function(creep){
if(CHATTY) creep.say(this.name);
this.setup[creep.type](creep);
}
action.setup = {
melee: function(creep){
if( creep.target.color ){
creep.moveTo(creep.target, {reusePath: 15});
return;
}
var moveResult = creep.moveTo(creep.target, {reusePath: this.reusePath});
if( !creep.target.my )
var workResult = creep.attack(creep.target);
},
ranger: function(creep){
if( creep.target.color ){
creep.moveTo(creep.target, {reusePath: 15});
return;
}
var range = creep.pos.getRangeTo(creep.target);
if( range > 3 ){
creep.moveTo(creep.target, {reusePath: this.reusePath});
}
if( range < 3 ){
creep.move(creep.target.pos.getDirectionTo(creep));
}
// attack
var targets = creep.pos.findInRange(FIND_HOSTILE_CREEPS, 3);
if(targets.length > 2) { // TODO: calc damage dealt
if(CHATTY) creep.say('MassAttack');
creep.rangedMassAttack();
return;
}
if( range < 4 ) {
creep.rangedAttack(creep.target);
return;
}
if(targets.length > 0){
creep.rangedAttack(targets[0]);
}
}
};
module.exports = action;