-
Notifications
You must be signed in to change notification settings - Fork 0
/
role.towerRepair.js
153 lines (130 loc) · 5.36 KB
/
role.towerRepair.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
141
142
143
144
145
146
147
148
149
150
151
152
153
var roleHarvester = {
/** @param {Creep} creep **/
run: function(creep) {
var targets = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_TOWER) &&
structure.energy < structure.energyCapacity;
}
});
if (targets.length == 0) {
var targets = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_EXTENSION || structure.structureType == STRUCTURE_SPAWN || structure.structureType == STRUCTURE_TOWER) &&
structure.energy < structure.energyCapacity;
}
});
}
if(targets.length > 0) {
//console.log('Creep ' + creep.name + ' delivering:' + creep.memory.delivering + ' on board:' + creep.carry.energy);
if(creep.memory.delivering != true) {
if (creep.memory.source == undefined || creep.memory.source == "") {
var targets = roleHarvester.findTargetsByType(creep, creep.memory.esource);
var sourceType = creep.memory.esource;
if (targets.length == null || targets.length == 0) {
//console.log("looking to source #2");
var sourceType = creep.memory.esource2;
var targets = roleHarvester.findTargetsByType(creep, creep.memory.esource2);
}
source = creep.pos.findClosestByPath(targets);
if (source != null)
creep.memory.source = source.id;
}
if (creep.memory.source != undefined && creep.memory.source != "") {
switch(sourceType) {
case STORAGE:
case LINK:
case CONTAINER:
case STOREDANDLINKS:
case STORED:
case LINKSTORAGE:
//console.log("storage or link");
var successcode = creep.withdraw(Game.getObjectById(creep.memory.source), RESOURCE_ENERGY);
break;
default:
//console.log("default");
var successcode = creep.harvest(Game.getObjectById(creep.memory.source));
break;
}
//console.log("success code: " + successcode);
if(successcode == ERR_NOT_IN_RANGE) {
//console.log(creep.name + ": L4");
creep.moveTo(Game.getObjectById(creep.memory.source));
} else if (successcode == ERR_INVALID_TARGET) {
//console.log(creep.name + ": L5");
creep.memory.source = "";
}
}
if(creep.carry.energy == creep.carryCapacity) {
creep.memory.delivering = true;
creep.memory.source = "";
}
}
else {
if(creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(targets[0]);
}
if(creep.carry.energy == 0) {
creep.memory.delivering = false;
}
}
}
},
findTargetsByType: function(creep, type)
{
switch (type) {
case STORAGE:
var source = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_STORAGE )
}
});
break;
case LINK:
var source = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_LINK && structure.energy < structure.energyCapacity)
}
});
break;
case LINKSTORAGE:
var source = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return ((structure.structureType == STRUCTURE_LINK || structure.structureType == STRUCTURE_STORAGE)
&& structure.store[RESOURCE_ENERGY] > 0 )
}
});
break;
case STORED:
var source = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return ((structure.structureType == STRUCTURE_CONTAINER || structure.structureType == STRUCTURE_STORAGE)
&& structure.store[RESOURCE_ENERGY] > 0 )
}
});
break;
case CONTAINER:
var source = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_CONTAINER
&& structure.store[RESOURCE_ENERGY] > 0 )
}
});
break;
case STOREDANDLINKS:
var source = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return ((structure.structureType == STRUCTURE_CONTAINER || structure.structureType == STRUCTURE_STORAGE ||
structure.structureType == STRUCTURE_LINK) && structure.store[RESOURCE_ENERGY] > 0 )
}
});
break;
case ENERGY:
default:
source = creep.room.find(FIND_SOURCES);
break;
}
return source;
}
};
module.exports = roleHarvester;