-
Notifications
You must be signed in to change notification settings - Fork 2
/
Harvesters.js
74 lines (64 loc) · 2.3 KB
/
Harvesters.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
var Creeps = require('Creeps');
function Harvesters(mon, creeps) {
this.memoryKey = 'Harvesters';
Creeps.call(this, mon, creeps);
}
Harvesters.prototype = new Creeps;
Harvesters.prototype.initCreep = function (creep) {
var sources = this.mon.findSources(creep.room);
creep.memory.role = 'harvester';
creep.memory.state = creep.memory.state || 'harvest';
creep.memory.source = creep.memory.source || sources[creep.memory.index % sources.length].id;
}
Harvesters.prototype.doContinuityChange = function () {
console.log('Harvesters: Assigning Sources');
var room = this.creeps[0].room;
room.memory.sources = room.memory.sources || {};
var memory = room.memory.sources;
var sources = this.mon.findSources(room);
var i = 0;
for (var n in sources) {
var source = sources[n];
memory[ source.id ] = memory[ source.id ] || {};
memory[ source.id ].count = memory[ source.id ].count || 3;
var count = memory[ source.id ].count;
var start = i;
for (; i < start+count; i++) {
if (i >= this.creeps.length) return;
this.creeps[i].source = source.id;
}
}
}
Harvesters.prototype.states = Object.create(Creeps.prototype.states);
Harvesters.prototype.states.harvest = function (creep) {
var mySource = Game.getObjectById(creep.memory.source);
if (creep.carry.energy < creep.carryCapacity) {
creep.moveTo(mySource);
creep.harvest(mySource);
}
else {
this.changeState(creep, 'deliver');
}
}
Harvesters.prototype.states.deliver = function (creep) {
if (creep.carry.energy == 0) {
this.changeState(creep, 'harvest');
}
else {
var storages = this.mon.findStructuresNeedingEnergy(creep.room);
storages.sort(function(a, b) {
var needA = a.energyCapacity ? a.energyCapacity - a.energy : a.storeCapacity - a.store.energy;
var needB = b.energyCapacity ? b.energyCapacity - b.energy : b.storeCapacity - b.store.energy;
return needB - needA;
});
if (storages.length) {
creep.moveTo(storages[0]);
creep.transferEnergy(storages[0]);
}
else {
creep.moveTo(Game.spawns.Home);
creep.transferEnergy(Game.spawns.Home)
}
}
}
module.exports = Harvesters;