-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenericFunctions.js
94 lines (87 loc) · 3.32 KB
/
genericFunctions.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
module.exports = {
/** @param {Creep} creep
@param {int} minimumEnergy **/
pickUpNearSource: function(creep, minimumEnergy = 0) {
if (minimumEnergy == 0)
minimumEnergy = creep.carryCapacity - creep.carry[RESOURCE_ENERGY];
//Find the near droped energy, if there is no dropped energy, loock for the near container
var nearSource = creep.pos.findClosestByRange(FIND_DROPPED_RESOURCES,{
filter: (energy) => {return (energy.amount > minimumEnergy && energy.resourceType == RESOURCE_ENERGY)}
});
if(nearSource != undefined){
if(creep.pickup(nearSource) == ERR_NOT_IN_RANGE) {
creep.moveTo(nearSource);
}
return true;
}else{
//find the nearest container with energy
nearSource = creep.pos.findClosestByPath(FIND_STRUCTURES,{
filter: (structure) => {return (structure.structureType == STRUCTURE_CONTAINER && structure.store[RESOURCE_ENERGY] > minimumEnergy)}
});
if(nearSource != undefined){
if(creep.withdraw(nearSource, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(nearSource);
}
return true;
}
}
return false;
},
pickUpNearEnergy: function(creep) {
var nearSource = creep.room.storage;
if (nearSource) {
if( nearSource.store[RESOURCE_ENERGY] > creep.carryCapacity){
if(creep.withdraw(creep.room.storage, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(creep.room.storage);
}
}
}else{
pickUpNearSource(creep);
}
},
getNearestSafeZone: function(creep){
for(var thisFlag in Game.flags) {
var name = Game.flags[thisFlag].name;
if (name.startsWith("SafeZone")){
return thisFlag;
}
}
},
findThisFlag: function(thisFlagNameStartsWith){
for(var thisFlag in Game.flags) {
var name = Game.flags[thisFlag].name;
if (name.startsWith(thisFlagNameStartsWith)){
return thisFlag;
}
}
},
harvestNearSource: function(creep){
var nearSource = creep.pos.findClosestByPath(FIND_SOURCES_ACTIVE);
if(creep.harvest(nearSource) == ERR_NOT_IN_RANGE) {
creep.moveTo(nearSource);
}
},
goToBase: function(creep){
var nearestSafeZone = getNearestSafeZone(creep);
if(creep.room != Game.flags[nearestSafeZone].room){
creep.moveTo(Game.flags[nearestSafeZone]);
console.log(creep.name + " moving to a safezone: " + nearestSafeZone);
}else{
pickUpNearSource(creep);
}
},
getSpawnerByName: function(spawnerName){
if(!Game.spawns[spawnerName]){
console.log("Spawn " + spawnerName + " doesn't exists");
return false;
}
return Game.spawns[spawnerName];
},
getRoomByName: function(roomName){
if(!Game.rooms[roomName]){
console.log("Room " + roomName + " doesn't exists");
return false;
}
return Game.rooms[roomName];
}
};