-
Notifications
You must be signed in to change notification settings - Fork 0
/
role.attacker.js
52 lines (49 loc) · 1.4 KB
/
role.attacker.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
module.exports = class {
constructor(creep){
this.creep = creep
}
find(){
return _.reduce(Game.creeps, function(workers, creep, key) {
if(creep.memory.role == "attacker"){
workers.push(creep)
}
return workers
}, [])
}
wanted(){
for(let room of _.values(Game.rooms)){
if(room.find(FIND_HOSTILE_CREEPS).length > 0){
Memory.room.push(room.name)
}
}
if(Memory.enemyRoom.length > 0){
return 2
}
return 0
}
run(){
if(Memory.enemyRoom && Memory.enemyRoom[0] && this.creep.room.name != Memory.enemyRoom[0]){
let exitDir = this.creep.room.findExitTo(Memory.enemyRoom[0])
let exit = this.creep.pos.findClosestByRange(exitDir)
this.creep.moveTo(exit)
}
if(this.creep.room.name == Memory.enemyRoom[0]){
let enemies = this.creep.room.enemyTargets()
if(enemies.length == 0){
for(let i in Memory.enemyRoom){
if(Memory.enemyRoom[i] == this.creep.room.name){
delete Memory.enemyRooms[i]
}
}
}
let sortedEnemies = _.sortBy(enemies, enemy => this.creep.pos.getRangeTo(enemy))
if(this.creep.attack(sortedEnemies[0]) == ERR_NOT_IN_RANGE){
this.creep.moveTo(sortedEnemies[0])
}
}
if(Memory.enemyRoom.length == 0){
this.creep.memory.suicide = true
this.creep.moveTo(Game.getObjectById(Memory.homespawn))
}
}
}