-
Notifications
You must be signed in to change notification settings - Fork 2
/
tanker.js
67 lines (64 loc) · 2.5 KB
/
tanker.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
var util = require('util');
module.exports = function (mon, creep) {
creep.memory.state = creep.memory.state || 'gather';
if (creep.memory.state == 'gather') {
util.gather(mon, creep, 'deliver', 'Deliver');
}
else {
if (creep.carry.energy == 0) {
creep.say("Gather");
creep.memory.state = 'gather';
}
else {
var distance = [];
var storages = creep.room.find(FIND_MY_STRUCTURES, {
filter: function(s) {
if (s.structureType == STRUCTURE_EXTENSION && s.energy < s.energyCapacity) {
distance[ s.id ] = util.crowDistance(creep.pos, s.pos);
return true;
}
return false;
}
})
storages.sort(function(a, b) {
var needA = a.energyCapacity - a.energy;
var needB = b.energyCapacity - b.energy;
return needB - needA || distance[a.id] - distance[b.id];
});
if (Game.spawns.Home.energy <= 250) {
creep.moveTo(Game.spawns.Home);
creep.transferEnergy(Game.spawns.Home)
}
else if (storages.length) {
creep.moveTo(storages[0]);
creep.transferEnergy(storages[0]);
}
else if (Game.spawns.Home.energy < Game.spawns.Home.energyCapacity) {
creep.moveTo(Game.spawns.Home);
creep.transferEnergy(Game.spawns.Home)
}
else {
var creeps = mon.findMyCreeps(creep.room).filter(
function (c) {
distance[ c.id ] = util.crowDistance(creep.pos, c.pos);
return (
c.memory.role == 'builder'
|| c.memory.role == 'keeper'
)
&& c.carry.energy < c.carryCapacity
&& c.carry.energy > 5; // they aren't gathering yet
}
);
creeps.sort(function(a, b) {
var needA = a.carryCapacity - a.carry.energy;
var needB = b.carryCapacity - b.carry.energy;
return distance[a.id] - distance[b.id] || needB - needA;
});
if (creeps.length) {
creep.moveTo(creeps[0]);
creep.transferEnergy(creeps[0]);
}
}
}
}
}