-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct.tower.js
64 lines (60 loc) · 2.29 KB
/
struct.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
const splay = require('util.splay');
module.exports = {
TYPE: STRUCTURE_TOWER, // XXX use this, or just rely on the module name?
run: function(tower) {
var t = tower.memory.target; // indicates attack recently if not now
var target = Game.getObjectById(t);
if(target) {
var ret = tower.attack(target);
if(ret == OK) return;
if(ret == ERR_INVALID_TARGET) {
target = null;
};
}
if(!target && (t || splay.isTurn('tower', tower.id))) {
var targets = tower.room.find(FIND_HOSTILE_CREEPS);
var highValue = _.filter(targets, c => {
var body = _.groupBy(c.body, 'type');
// TODO(baptr): Go for healers first.
if(body[ATTACK] || body[RANGED_ATTACK] || body[CLAIM] || body[HEAL]) return true;
if(c.carry.energy > 100) return true;
return false;
});
if(highValue.length) targets = highValue;
target = tower.pos.findClosestByRange(targets);
if(target) {
tower.memory.target = target.id;
return tower.attack(target);
} else {
delete tower.memory.target;
}
let friend = tower.pos.findClosestByRange(FIND_MY_CREEPS, {filter: c => c.hits < c.hitsMax});
if(friend) {
return tower.heal(friend);
}
}
if(!splay.isTurn('tower', tower.id)) return;
if(tower.energy > 700) { // Save up some for defense.
var closestDamagedStructure = tower.pos.findClosestByRange(FIND_STRUCTURES, {
filter: (structure) => structure.hits / structure.hitsMax < 0.95
});
if(closestDamagedStructure) {
tower.repair(closestDamagedStructure);
}
}
}
};
if(!StructureTower.prototype.hasOwnProperty('memory')) Object.defineProperty(StructureTower.prototype, 'memory', {
get: function() {
if(this.id && !this.my) return undefined;
if(!Memory.structures) Memory.structures = {}; // XXX struct or tower?
return Memory.structures[this.id] = Memory.structures[this.id] || {};
},
set: function(value) {
if(this.id && !this.my) {
throw new Error("Could not set other player's tower memory");
}
if(!Memory.structures) Memory.structures = {};
Memory.structures[this.id] = value;
}
});