-
Notifications
You must be signed in to change notification settings - Fork 0
/
role.upgrader.js
91 lines (85 loc) · 4.48 KB
/
role.upgrader.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
89
90
91
var helper = require('helper');
var roleMulti = {
/** @param {Creep} creep **/
run: function(creep) {
if(creep.carry.energy == 0){
creep.memory.working = false;
}
if(creep.carry.energy < creep.carryCapacity && creep.memory.working == false) {
//init vars
var storage = creep.pos.findClosestByRange(FIND_STRUCTURES, { filter: (structure) => { return (structure.structureType == STRUCTURE_STORAGE ) && structure.store[RESOURCE_ENERGY] > 0; } });
var sources = creep.room.find(FIND_SOURCES);
var creepCount = new Array()
if (storage == null) {
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//Has harvesting source been determined? Check
if (creep.memory.harSource = -1) {
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//add up creepCount for each source
for (i = 0; i < sources.length; i++) {
if (!creepCount[i]) {
creepCount[i] = 0;
}
}
for (i = 0; i < sources.length; i++) {
for(var cName in Game.creeps) {
var cre = Game.creeps[cName];
if (cre.memory.harSource != -1) {
creepCount[cre.memory.harSource] += 1;
//console.log('CreepCount for Source ' + i + ': ' + creepCount[cre.memory.harSource])
}
}
}
//console.log('CreepCount[0]: ' + creepCount[0] + ' CreepCount[1]: ' + creepCount[1])
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//Determine best source to send creep to
var lowestCreepSource = 0;
for (i = 0; i < sources.length; i++) {
if (sources[i].energy > 0) {
if (creepCount[i] <= creepCount[lowestCreepSource]) {
lowestCreepSource = i;
}
}
}
//console.log('LowestCreepSource: ' +lowestCreepSource)
if(creep.memory.harSource == -1 || sources[creep.memory.harSource].energy==0){
creep.memory.harSource = lowestCreepSource;
}
var sourceFound = false;
for (i = 0; i < sources.length; i++) {
if (sources[i].energy > 0) {
sourceFound = true;
break;
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//Send creep to source
if(sourceFound == true) {
if(creep.harvest(sources[creep.memory.harSource]) == ERR_NOT_IN_RANGE) {
creep.moveTo(sources[creep.memory.harSource]);
}
} else {
//No source energy avail, attempt to work
creep.memory.working = true;
creep.memory.harSource = -1;
}
} else {
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//Pull energy from storage (preferred)
if(creep.withdraw(storage,RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(storage);
}
}
} else {
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//Harvesting complete. Reinit and distribute to task
creep.memory.working = true;
creep.memory.harSource = -1;
if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
creep.moveTo(creep.room.controller);
}
}
}
};
module.exports = roleMulti;