-
Notifications
You must be signed in to change notification settings - Fork 0
/
role.repair.js
135 lines (127 loc) · 6.8 KB
/
role.repair.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
var helper = require('helper');
var roleRepair = {
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;
} }
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//Send creep to source
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 + ' Repair 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 //structure.structureType == STRUCTURE_EXTENSION || structure.structureType == STRUCTURE_SPAWN || structure.structureType == STRUCTURE_CONTAINER ||
//structure.structureType == STRUCTURE_ROAD &&
var percent = 0;
if (creep.room.find(FIND_CONSTRUCTION_SITES) != null) {
percent=0.5;
} else {
percent=0.9;
}
var target = creep.pos.findClosestByRange(FIND_STRUCTURES, { filter: (structure) =>
{
return (structure.hits < (structure.hitsMax * percent));
}
});
if(target != null) {
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//Damaged structure found, move and repair
var resp = creep.repair(target)
if(resp == ERR_NOT_IN_RANGE) {
creep.moveTo(target);
} else if (resp == OK) {
creep.say('Repairing')
}
} else {
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//Damaged structure not found, try to build, and as last resort, move to spawn point
var targetTwo = creep.pos.findClosestByPath(FIND_CONSTRUCTION_SITES, { filter: (s) => s.structureType != STRUCTURE_ROAD });
//console.log(creep.name + " --------- '" + targetTwo + "'");
if (targetTwo == null ) {
// find closest constructionSite
// console.log(creep.name + " moving to build " + targetTwo);
var targetTwo = creep.pos.findClosestByRange(FIND_CONSTRUCTION_SITES);
}
if (targetTwo == null) {
var moveToConsSpawn = ""
for(var rm in Game.rooms){
var curRoom = Game.rooms[rm]
var spawn = Game.rooms[rm].find(FIND_CONSTRUCTION_SITES, { filter: (structure) => { return (structure.structureType == STRUCTURE_SPAWN); } })
if (spawn[0] != null) {
moveToConsSpawn = spawn[0]
}
}
if (moveToConsSpawn != ""){
creep.moveTo(moveToConsSpawn);
}
} else {
creep.say('building')
if(creep.build(targetTwo) == ERR_NOT_IN_RANGE)
{
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//Buildable structures found, deploy
creep.moveTo(targetTwo);
}
}
}
}
}
};
module.exports = roleRepair;