-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrole.defender.js
79 lines (71 loc) · 2.64 KB
/
srole.defender.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
var helper = require('helper');
var foreman = require('service.foreman');
var sroleDefender = {
spawn: function(spawner, memory) {
spawner.createCreep([MOVE,MOVE,MOVE,MOVE,ATTACK,RANGED_ATTACK,RANGED_ATTACK,RANGED_ATTACK], undefined, memory);
},
/** @param {Creep} creep **/
run: function(creep) {
var myOwner = creep.owner;
var remotePos = Game.flags[creep.memory.flagRemote].pos;
var hostiles = creep.room.find(FIND_HOSTILE_CREEPS);
if (Memory.defenderGather !== undefined) {
var gatherFlag = Game.flags[Memory.defenderGather];
if (creep.pos.getRangeTo(gatherFlag) > 3) {
creep.say('D->&');
creep.moveTo(gatherFlag);
return;
}
creep.say('D...&');
return;
}
var hostile = null;
if (hostiles.length > 0) {
var friends = [
'damphlett',
'Synapse791'
];
_.forEach(hostiles, function(h) {
if ( ! _.includes(friends, h.owner.username)) {
hostile = h;
console.log('Defender sees enemy '+h.toString());
return false;
}
})
}
if (hostile !== null) {
var rangeToHostile = creep.pos.getRangeTo(hostile);
var pathToHostile = creep.pos.findPathTo(hostile);
var firstPath = pathToHostile.length > 0 ? pathToHostile[0] : null;
if (rangeToHostile < 2) {
// attack and run
creep.say('D.<-');
creep.attack(hostile);
creep.move(helper.opposite_direction(firstPath.direction));
} else if (rangeToHostile < 4) {
// attack
creep.say('D.*');
creep.rangedAttack(hostile);
} else if (rangeToHostile < 7) {
// circle (and try ranged attack just in case)
creep.say('D.0');
creep.move(helper.away_from_stay_in_room(creep.pos, hostile.pos, pathToHostile));
creep.rangedAttack(hostile);
} else {
// move toward
creep.say('D.x');
creep.moveTo(hostile);
}
} else {
// get close but not too close to the remote pos
var rangeToRemote = creep.pos.getRangeTo(remotePos);
if (rangeToRemote > 5) {
creep.say('D->');
creep.moveTo(remotePos);
} else {
creep.say('D...');
}
}
}
};
module.exports = sroleDefender;