-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.functions.js
139 lines (125 loc) · 3.64 KB
/
util.functions.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
class utilFunctions {
constructor(){}
allKnownRooms(){
let rooms = []
for(let roomName in Game.rooms){
let room = Game.rooms[roomName]
rooms.push(room)
}
return rooms
}
otherTargeters(target, role){
let others = []
for(let creep of this.findCreepsByRole(role)){
if(creep.memory.target == target){
others.push(creep)
}
}
return others
}
allFreeEnergy(){
let energy = 0
for(let e of this.allFreeResources()){
energy += e.amount
}
for(let e of this.allContainers()){
energy += e.store.getUsedCapacity(RESOURCE_ENERGY)
}
return energy
}
allContainers(){
let containers = []
for(let room of this.allKnownRooms()){
let roomContainers = room.find(FIND_STRUCTURES, {
filter: (struct) => {
return struct.structureType == STRUCTURE_CONTAINER
}
})
containers = containers.concat(roomContainers)
}
return containers
}
allFreeResources(){
let resources = []
for(let room of this.allKnownRooms()){
let roomResources = room.find(FIND_DROPPED_RESOURCES)
resources = resources.concat(roomResources)
}
return resources
}
allSources(){
let sources = []
for(let room of this.allKnownRooms()){
let roomSources = room.find(FIND_SOURCES)
sources = sources.concat(roomSources)
}
return sources
}
creepCost(body){
let cost = 0
for(let part of body){
cost += BODYPART_COST[part]
}
return cost
}
allSpawns(){
let allSpawns = []
for(let room of this.allKnownRooms()){
let spawns = room.find(FIND_STRUCTURES, {
filter: (struct) => {
return (
struct.structureType == STRUCTURE_SPAWN &&
struct.my
)
}
})
allSpawns = allSpawns.concat(spawns)
}
return allSpawns
}
findCreepsByRole(role){
let creeps = []
for(let name in Game.creeps){
let creep = Game.creeps[name]
if(creep.memory.role == role){
creeps.push(creep)
}
}
return creeps
}
allMyControllers(){
let allControllers = []
for(let room of this.allKnownRooms()){
let controllers = room.find(FIND_STRUCTURES, {
filter: (struct) => {
return (
struct.structureType == STRUCTURE_CONTROLLER &&
struct.my
)
}
})
allControllers = allControllers.concat(controllers)
}
return allControllers
}
isTargeted(source){
let count = 0
for(let name in Game.creeps){
let creep = Game.creeps[name]
if(creep.memory.target == source.id){
count++
}
}
return count
}
isMiningTargeted(source){
for(let name in Game.creeps){
let creep = Game.creeps[name]
if(creep.memory.miningTarget == source.id){
return true
}
}
return false
}
}
global.util = new(utilFunctions)