-
Notifications
You must be signed in to change notification settings - Fork 1
/
creep.harvester.js
88 lines (81 loc) · 2.42 KB
/
creep.harvester.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
* The heavy harvester :
* - goes (slowly) to a free source
* - stays near it for its all its life
* - it harvests the energy (10 / tick) and throw it on the ground
*/
let objects = require('./objects');
module.exports.__proto__ = require('./creep');
/**
* Body parts for a heavy harvester
* MOVE, WORK x 5
* - consumes 550 energy units
*/
module.exports.body_parts = [MOVE, WORK, WORK, WORK, WORK, WORK];
module.exports.role = 'harvester';
module.exports.single_source = true;
module.exports.target_work = false;
/**
* Returns true if the creep can continue its source work, without any consideration about its source state
*
* Default behaviour : the creep can always work : it fills its carry capacity, then drops resource on the ground
*
* @returns boolean true
*/
module.exports.canWorkSource = function()
{
return true;
};
/**
* The harvester does the standard job.
* If it has a 'CARRY' part and a nearly LINK, then it immediately stores the harvester energy into the link
*
* @param creep Creep
* @return number 0 if no error, error code if error during the job
*/
module.exports.sourceJob = function(creep)
{
let result = module.exports.__proto__.sourceJob(creep);
if (creep.memory.link && (result === OK) && objects.can(creep, CARRY)) {
let link = objects.get(creep, creep.memory.link);
if (link) {
result = creep.transfer(link, RESOURCE_ENERGY);
if (objects.energyRatio(link) > .9) {
let target;
if (!Memory.links) Memory.links = {};
if (!Memory.links[link.id]) Memory.links[link.id] = {};
if (Memory.links[link.id].target) {
target = Game.getObjectById(Memory.links[link.id].target);
}
else {
target = link.room.find(FIND_MY_STRUCTURES, { filter: structure =>
(structure.structureType === STRUCTURE_LINK) && (structure.id !== link.id)
}).shift();
Memory.links[link.id].target = target.id;
}
if (target) link.transferEnergy(target);
}
}
}
return result;
};
/**
* Select the source with the minimum count of harvesters.
*
* @param context RoomObject
* @return string[] Sources id
*/
module.exports.sources = function(context)
{
if (!this.source_work) return [];
let min_count = 99;
let min_source = null;
for (let source of context.room.find(FIND_SOURCES_ACTIVE)) {
let count = this.sourceCount(source, context);
if (count < min_count) {
min_count = count;
min_source = source;
}
}
return min_source ? [min_source] : [];
};