-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollect.controller.js
111 lines (96 loc) · 2.83 KB
/
collect.controller.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
const Controller = require('controller')
const PRIOS = require('priority.manager')
const SRC = require('source.manager')
const TRIGGER_PICK = 50
const TRIGGER_ABS_HARV = 10
const TRIGGER_CAPA = 0.7
class CollectController extends Controller {
constructor() {
super('collect')
}
control() {
super.control();
this.doForEachCreep(
creep => {
creep.memory.collect = creep.carry[RESOURCE_ENERGY] == 0 || (creep.memory.collect && creep.carry[RESOURCE_ENERGY]/creep.carryCapacity < TRIGGER_CAPA)
if (creep.memory.collect) {
if (!creep.memory.source){
SRC.affectCollect(creep)
}
let sources
if (creep.memory.source){
sources = SRC.getSources(creep)[creep.memory.source].harvest
}else{
sources = Controller.creeps('harvest')
}
sources = sources.map(c => Game.getObjectById(c))
.filter(c => c.carry[RESOURCE_ENERGY] >= TRIGGER_ABS_HARV)
.sort((c1, c2) => c2.carry[RESOURCE_ENERGY] / c2.carryCapacity - c1.carry[RESOURCE_ENERGY]/c1.carryCapacity)
if (sources.length) {
if (Controller.transferEnergy(sources[0], creep) == ERR_NOT_IN_RANGE) {
creep.moveTo(sources[0]);
}
}
else {
let targets = creep.room.find(FIND_DROPPED_ENERGY, {filter: (d=>d.amount >= TRIGGER_PICK)})
if (targets.length) {
if (creep.pickup(targets[0]) == ERR_NOT_IN_RANGE) {
creep.moveTo(targets[0]);
}
}
else {
creep.memory.collect = false
}
}
}else{
if (creep.memory.target) {
let target = Game.getObjectById(creep.memory.target);
if(target && Controller.roomForEnergy(target)){
let r = Controller.transferEnergy(creep, target);
switch (r) {
case ERR_NOT_IN_RANGE :
creep.moveTo(target)
break;
case 0 :
delete creep.memory.target
break;
default :
console.log('Collect',creep.name,'Fail give energy',r)
delete creep.memory.target
}
}else{
delete creep.memory.target
}
}else {
this.findTargetStructure(creep)
}
}
}
)
}
findTargetStructure(creep) {
let targets = creep.room.find(
FIND_MY_STRUCTURES, {
filter: (s) => (
s.structureType == STRUCTURE_EXTENSION || s.structureType == STRUCTURE_SPAWN
) && Controller.roomForEnergy(s)
}
)
targets.sort(
(t1, t2) => PRIOS.getPriority(t2.structureType) - PRIOS.getPriority(t1.structureType)
)
if (targets.length) {
creep.memory.target = targets[0].id
}
else if (creep.room.storage && Controller.roomForEnergy(creep.room.storage)) {
creep.memory.target = creep.room.storage.id
}
else {
console.log('Collect', creep.name, 'No target available')
}
}
newCreep() {
return [MOVE, CARRY, MOVE, CARRY, MOVE, CARRY, MOVE, CARRY, MOVE, CARRY]
}
}
module.exports = new CollectController()