-
Notifications
You must be signed in to change notification settings - Fork 0
/
role.multi.js
128 lines (121 loc) · 6.58 KB
/
role.multi.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
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 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;
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//Send creep to source
if(sourceFound == true) {
if(creep.harvest(sources[creep.memory.harSource]) == ERR_NOT_IN_RANGE) {
creep.moveTo(sources[creep.memory.harSource]);
//creep.say('move to ' + creep.memory.harSource)
}
} else if (creep.carry.energy == 0) {
//move to bored flag
//console.log(creep.room.name + ' Multi 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;
}
});
if(target != null) {
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//Structures found, deposit energy
if(creep.transfer(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(target);
}
} else {
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//Check for buildable structures
var targetTwo = creep.pos.findClosestByPath(FIND_CONSTRUCTION_SITES, { filter: (s) => s.structureType != STRUCTURE_ROAD });
if (targetTwo == null) {
// find closest constructionSite
var targetTwo = creep.pos.findClosestByRange(FIND_CONSTRUCTION_SITES);
}
if(creep.build(targetTwo) == ERR_NOT_IN_RANGE)
{
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//Buildable structures found, deploy
creep.moveTo(targetTwo);
} else if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//No other actions, upgrade controller, if energy is full
if(creep.carry.energy == creep.carryCapacity){
creep.moveTo(creep.room.controller);
} else {
creep.memory.working = false;
}
}
}
}
}
};
module.exports = roleMulti;