-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtower.js
69 lines (65 loc) · 3.03 KB
/
tower.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
let towerDefense = {
run: function (myRoomName) {
var hostiles = Game.rooms[myRoomName].find(FIND_HOSTILE_CREEPS);
var hostileHealers = Game.rooms[myRoomName].find(FIND_HOSTILE_CREEPS, {
filter: (s) => (s.getActiveBodyparts(HEAL) > 0)
});
var hostileAttackers = Game.rooms[myRoomName].find(FIND_HOSTILE_CREEPS, {
filter: (s) => (s.getActiveBodyparts(ATTACK) > 0 || s.getActiveBodyparts(RANGED_ATTACK) > 0)
});
var hostiles = Game.rooms[myRoomName].find(FIND_HOSTILE_CREEPS);
var towers = Game.rooms[myRoomName].find(FIND_MY_STRUCTURES, {
filter: {
structureType: STRUCTURE_TOWER
}
});
var healerHit = false;
//Alternative to finding towers: // var towers = _.filter(Game.structures, s => s.structureType == STRUCTURE_TOWER); //if there are hostileHealers - attakc them
if (hostileHealers.length > 0 && healerHit == false) {
towers.forEach(tower => tower.attack(hostileHealers[0]));
healerHit = true;
}
//if there are hostileAttackers - attakc them
else if (hostileAttackers.length > 0) {
towers.forEach(tower => tower.attack(hostileAttackers[0]));
healerHit = false;
}
//if there are ANY Hostiles - attakc them
else if (hostiles.length > 0) {
towers.forEach(tower => tower.attack(hostiles[0]));
healerHit = false;
}
//if there are no hostiles....
if (hostiles.length === 0) {
//....first heal any damaged creeps
for (let name in Game.creeps) {
// get the creep object
var creep = Game.creeps[name];
if (creep.hits < creep.hitsMax) {
towers.forEach(tower => tower.heal(creep));
}
}
for (var i in towers) {
//...repair Buildings! :) But ONLY until HALF the energy of the tower is gone.
//Because we don't want to be exposed if something shows up at our door :)
if (towers[i].energy > towers[i].energyCapacity / 10) {
var repairitnow = towers[i].pos.findClosestByRange(FIND_STRUCTURES, {
filter: (s) => {
return (s.hits < s.hitsMax / 2000 && s.hits < 1000);
}
});
//Find the closest damaged Structure
var closestDamagedStructure = towers[i].pos.findClosestByRange(FIND_STRUCTURES, {
filter: (s) => s.hits < s.hitsMax / 1000 && s.hits < 1000 && s.hits > 0 && s.structureType != STRUCTURE_WALL
});
if (repairitnow) {
towers[i].repair(repairitnow);
} else if (closestDamagedStructure) {
towers[i].repair(closestDamagedStructure);
}
}
}
}
}
}
module.exports = towerDefense;