-
Notifications
You must be signed in to change notification settings - Fork 0
/
role.gatherer.js
60 lines (56 loc) · 2.1 KB
/
role.gatherer.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
var stages = {
extracting: function(creep) {
var target = Game.getObjectById(creep.memory.targetId);
if (creep.harvest(target) == ERR_NOT_IN_RANGE) {
creep.moveTo(target);
}
},
unloading: function(creep) {
var target = creep.room.terminal;
if (!target) target = creep.room.storage;
if (creep.transfer(target, creep.memory.mineralType) == ERR_NOT_IN_RANGE) {
creep.moveTo(target);
}
},
suicide: function(creep) {
var spawn;
if ('spawnId' in creep.memory) {
spawn = Game.getObjectById(creep.memory.spawnId);
} else {
spawn = creep.pos.findClosestByPath(FIND_MY_SPAWNS);
creep.memory.spawnId = spawn.id;
}
if (creep.pos.isNearTo(spawn)) {
spawn.recycleCreep(creep);
} else {
creep.moveTo(spawn);
}
},
}
var gatherer = {
run: function(creep) {
if (!('stage' in creep.memory)) {
var minerals = creep.room.find(FIND_MINERALS);
if (minerals.length == 0) {
creep.memory.stage = 'suicide';
} else {
creep.memory.stage = 'extracting';
creep.memory.targetId = minerals[0].id;
creep.memory.mineralType = minerals[0].mineralType;
}
}
if (creep.spawning) return;
var creepWork = creep.getActiveBodyparts(WORK);
if (creep.memory.stage == 'extracting' && _.sum(creep.carry) > creep.carryCapacity - creepWork*HARVEST_MINERAL_POWER) creep.memory.stage = 'unloading';
if (creep.memory.stage == 'unloading' && _.sum(creep.carry) == 0) creep.memory.stage = creep.ticksToLive > (EXTRACTOR_COOLDOWN*creep.carryCapacity/creepWork+100) ? 'extracting' : 'suicide';
if (creep.memory.stage in stages) {
stages[creep.memory.stage](creep);
} else {
console.log(creep+' is in unknown stage '+creep.memory.stage+'!');
}
},
describeTarget: function(creep) {
return 'room '+creep.pos.roomName;
}
}
module.exports = gatherer;