forked from VladThePaler/screeps.behaviour-action-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreep.action.invading.js
161 lines (159 loc) · 5.74 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
let action = new Creep.Action('invading');
module.exports = action;
action.isValidAction = function(creep){ return FlagDir.hasInvasionFlag(); };
action.isAddableAction = function(){ return true; };
action.isAddableTarget = function(){ return true; };
action.getFlaggedStructure = function(flagColor, pos){
let flagsEntries = FlagDir.filter(flagColor, pos, true);
let target = [];
let checkFlag = flagEntry => {
var flag = Game.flags[flagEntry.name];
if( flag && flag.pos.roomName == pos.roomName && flag.room !== undefined ){ // room is visible
var targets = flag.room.lookForAt(LOOK_STRUCTURES, flag.pos.x, flag.pos.y);
if( targets && targets.length > 0){
addTarget = structure => {
structure.destroyFlag = flag;
target.push(structure);
};
targets.forEach(addTarget);
}
else { // remove flag. try next flag
flag.remove();
}
}
};
flagsEntries.forEach(checkFlag);
if( target && target.length > 0 ) return pos.findClosestByRange(target);
return null;
};
action.newTarget = function(creep){
var destroy = this.getFlaggedStructure(FLAG_COLOR.destroy, creep.pos);
if( destroy ) {
if( destroy.destroyFlag ) Population.registerCreepFlag(creep, destroy.destroyFlag);
return destroy;
}
// move to invasion room
var flag = FlagDir.find(FLAG_COLOR.invade, creep.pos);
if( flag && (!flag.room || flag.pos.roomName != creep.pos.roomName)){
Population.registerCreepFlag(creep, flag);
return flag; // other room
}
if( !flag ){
// unregister
creep.action = null;
delete creep.data.actionName;
delete creep.data.targetId;
return;
}
if( !flag.room.controller || !flag.room.controller.my ) {
//attack healer
var target = creep.pos.findClosestByRange(creep.room.hostiles, {
function(hostile){ return _.some(hostile.body, {'type': HEAL}); }
});
if( target )
return target;
//attack attacker
target = creep.pos.findClosestByRange(creep.room.hostiles, {
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(creep.room.hostiles);
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);
if( (creep.target instanceof Flag) && (creep.target.pos.roomName == creep.pos.roomName))
this.assign(creep);
this.run[creep.data.creepType](creep);
};
action.run = {
melee: function(creep){
if( !creep.flee ){
if( creep.target instanceof Flag ){
creep.travelTo( creep.target );
return;
} else if( creep.target instanceof ConstructionSite ){
creep.travelTo( creep.target, {range:0});
return;
}
creep.travelTo( creep.target );
}
if( !creep.target.my )
creep.attacking = creep.attack(creep.target) == OK;
},
ranger: function(creep){
var range = creep.pos.getRangeTo(creep.target);
if( !creep.flee ){
if( creep.target instanceof Flag ){
creep.travelTo( creep.target );
return;
} else if( creep.target instanceof ConstructionSite ){
creep.travelTo( creep.target, {range:0});
return;
}
if( range > 3 ){
creep.travelTo( creep.target );
}
if( range < 3 ){
creep.move(creep.target.pos.getDirectionTo(creep));
}
}
// attack
var targets = creep.pos.findInRange(creep.room.hostiles, 3);
if(targets.length > 2) { // TODO: calc 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;
}
}
};
action.onAssignment = function(creep, target) {
if( SAY_ASSIGNMENT ) creep.say(String.fromCharCode(9876), SAY_PUBLIC);
};
action.defaultStrategy.moveOptions = function(options) {
// allow routing in and through hostile rooms
if (_.isUndefined(options.allowHostile)) options.allowHostile = true;
return options;
};