-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrole.worker.js
More file actions
86 lines (74 loc) · 2.28 KB
/
role.worker.js
File metadata and controls
86 lines (74 loc) · 2.28 KB
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
let styles = require('styles')
const MAX_WORKERS = 3
module.exports = class {
constructor(creep){
this.creep = creep
}
find(){
return _.reduce(Game.creeps, function(workers, creep, key) {
if(creep.memory.role == "worker"){
workers.push(creep)
}
return workers
}, [])
}
wanted(){
// Find a way to do all free energy
//return Math.floor(Game.allFreeEnergy()/1000)
let totalEnergy =_.reduce(_.first(_.values(Game.rooms)).allEnergy(), function(sum, value, key){
sum += _.reduce(value, function(subSum, subValue, subKey){
subSum += subValue.netEnergy
return subSum
}, 0)
return sum
}, 0)
return Math.max(Math.floor((totalEnergy)/2000), 2)
}
run(){
this.socialism()
if(this.creep.netEnergy == 0){
this.creep.memory.target = ""
this.creep.acquireEnergy()
}
let target = this.selectTarget()
if(this.work(target) == ERR_NOT_IN_RANGE){
this.moveTo(target, styles.work)
}
}
socialism(){
if(this.creep.netEnergy < 25 ){ return OK }
let nearby = this.creep.pos.findInRange(FIND_MY_CREEPS, 1, {
filter: (c) => {
return c.memory.role == "worker"
}
})
if(nearby.length > 0){
let lowest = _.sortBy(nearby, nearCon => nearCon.store.getUsedCapacity(RESOURCE_ENERGY))[0]
let transferAmount = Math.floor((this.creep.store.getUsedCapacity(RESOURCE_ENERGY) - lowest.store.getUsedCapacity(RESOURCE_ENERGY))/2)
this.creep.transfer(lowest, RESOURCE_ENERGY, transferAmount)
}
}
selectTarget(){
let conSites = _.sortBy(Game.constructionSites, c => 1 - c.progress/c.progressTotal)
for(let conSite of conSites){
if(conSite.targeted().length <= MAX_WORKERS){
return conSite
}
}
let controllers = _.filter(Game.structures, c => {return c.structureType == STRUCTURE_CONTROLLER})
return _.sortBy(controllers, c => c.targeted().length)[0]
}
work(target){
if(target.level !== undefined){
return this.creep.upgradeController(target)
}
if(target.progress !== undefined){
return this.creep.build(target)
}
return ERR_INVALID_TARGET
}
moveTo(dest, style){
this.creep.moveTo(dest, {visualizePathStyle: style})
//Memory.towQueue[this.creep.id] = dest.pos
}
}