-
Notifications
You must be signed in to change notification settings - Fork 0
/
role.farharvester.js
140 lines (128 loc) · 6.69 KB
/
role.farharvester.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
var helper = require('helper');
var roleMulti = {
/** @param {Creep} creep **/
run: function(creep) {
/*var exits = Game.map.describeExits(creep.room.name)
var totExits = 4;
console.log('Exits: ' + totExits)
for (var x in exits){
for (var r in Game.rooms) {
if (Game.rooms[r].name == exits[x]) {
delete exits[x];
totExits -= 1;
break;
}
}
}
console.log(creep.room.name + ' Exits: ' + totExits)*/
/*for (var i = 0; i < 50; i++) {
var ter = Game.map.getTerrainAt(0,i,creep.room.name);
if (ter == plain || ter == swamp) {
}
}*/
if(creep.carry.energy == 0){
creep.memory.working = false;
}
if(creep.carry.energy < creep.carryCapacity && creep.memory.working == false) {
//init vars
var sources = creep.room.find(FIND_SOURCES);
var creepCount = new Array()
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//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;
}
}
}
//check for dropped energy
var dropenergy = creep.pos.findClosestByPath(FIND_DROPPED_ENERGY, { filter: (d) => {return (d.resourceType == RESOURCE_ENERGY)} });
if (dropenergy) {
if (creep.pickup(dropenergy) == ERR_NOT_IN_RANGE) {
creep.moveTo(dropenergy);
}
} else {
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//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 if (creep.carry.energy == 0) {
//move to bored flag
//console.log(creep.room.name + ' Harvester moving to bored')
for (var flag in Game.flags){
if (Game.flags[flag].pos.roomName == creep.room.name && creep.room.name.substring(0,5) == 'Bored') {
creep.moveTo(Game.flags[flag]);
break;
}
}
} else {
//No source energy avail, attempt to work
creep.memory.working = true;
creep.memory.harSource = -1;
}
}
} else {
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//Harvesting complete. Reinit and distribute to task
creep.memory.working = true;
creep.memory.harSource = -1;
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//Search for structure with less then max energy
//////pos.findClosestByPath //creep.room.find
var target = creep.pos.findClosestByRange(FIND_STRUCTURES, { filter: (structure) =>
{
return ( structure.structureType == STRUCTURE_EXTENSION || structure.structureType == STRUCTURE_SPAWN || structure.structureType == STRUCTURE_CONTAINER || structure.structureType == STRUCTURE_TOWER ) &&
(structure.energy < structure.energyCapacity);
}
});
//structure.store[RESOURCE_ENERGY] < structure.storeCapacity
if(target == null) {
target = creep.pos.findClosestByRange(FIND_STRUCTURES, { filter: (structure) => { return structure.structureType == STRUCTURE_STORAGE && _.sum(structure.store) < structure.storeCapacity; } });
}
if(target != null) {
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//Structures found, deposit energy
if(creep.transfer(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(target);
}
}
}
}
};
module.exports = roleMulti;