-
Notifications
You must be signed in to change notification settings - Fork 0
/
role.eminer.js
145 lines (122 loc) · 5.34 KB
/
role.eminer.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
var roleEMiner = {
/** @param {Creep} creep **/
run: function(creep) {
var targets = roleEMiner.findTargetsByType(creep, creep.memory.dest);
var destType = creep.memory.dest;
if (targets.length == null || targets.length == 0) {
//console.log("looking to source #2");
var destType = creep.memory.dest2;
//console.log(destType);
var targets = roleEMiner.findTargetsByType(creep, creep.memory.dest2);
}
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 == "") {
source = creep.pos.findClosestByRange(FIND_SOURCES);
if (source != null)
creep.memory.source = source.id;
}
if (creep.memory.source != undefined && creep.memory.source != "")
if(creep.harvest(Game.getObjectById(creep.memory.source)) == ERR_NOT_IN_RANGE)
creep.moveTo(Game.getObjectById(creep.memory.source));
if(creep.carry.energy == creep.carryCapacity) {
creep.memory.delivering = true;
creep.memory.source = "";
creep.memory.destination = "";
}
}
else {
if (creep.memory.destination == undefined || creep.memory.destination == "") {
target = creep.pos.findClosestByRange(targets);
if (target != null)
creep.memory.destination = target.id;
}
switch(destType) {
case CONTAINER:
//console.log("in container");
if (Game.getObjectById(creep.memory.destination) != undefined && Game.getObjectById(creep.memory.destination) != null &&(Game.getObjectById(creep.memory.destination).hits / Game.getObjectById(creep.memory.destination).hitsMax) < containerRepairPercentage) {
var successcode = creep.repair(Game.getObjectById(creep.memory.destination));
creep.say("Repairing");
break;
}
case STORAGE:
case LINK:
case STOREDANDLINKS:
case STORED:
case LINKSTORAGE:
default:
//console.log("default");
var successcode = creep.transfer(Game.getObjectById(creep.memory.destination), RESOURCE_ENERGY);
break;
}
//console.log(successcode);
if(successcode == ERR_NOT_IN_RANGE) {
creep.moveTo(Game.getObjectById(creep.memory.destination));
} else if(successcode == ERR_INVALID_TARGET || successcode == ERR_FULL) {
creep.memory.destination = "";
}
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 )
}
});
break;
case CONTAINER:
var source = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_CONTAINER && (_.sum(structure.store) < structure.storeCapacity))
}
});
//console.log("looking for containers " + source.length + " " + _.sum(source[0].store) + "/" + source[0].storeCapacity);
break;
case LINKSTORAGE:
var source = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return ((structure.structureType == STRUCTURE_LINK || structure.structureType == STRUCTURE_STORAGE)
&& (_.sum(structure.store) < structure.storeCapacity) )
}
});
break;
case STORED:
var source = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return ((structure.structureType == STRUCTURE_CONTAINER || structure.structureType == STRUCTURE_STORAGE)
&& (_.sum(structure.store) < structure.storeCapacity) )
}
});
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) && (_.sum(structure.store) < structure.storeCapacity) )
}
});
break;
case ENERGY:
default:
source = creep.room.find(FIND_SOURCES);
break;
}
return source;
}
};
module.exports = roleEMiner;