-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrole.hauler.js
141 lines (132 loc) · 5.96 KB
/
role.hauler.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
var roleFunctions = require('roleFunctions');
var roleHauler = {
run: function(creep) {
var goal = creep.memory.goal;
creep.upgradeController(creep.room.controller);
if (!creep.memory.inPos) {
if (goal == 'collect')
{
targets = creep.pos.findInRange(FIND_DROPPED_RESOURCES, 10);
if (targets.length > 0) {
target = targets[0];
if (creep.pickup(targets[0]) == ERR_NOT_IN_RANGE) {
creep.moveTo(targets[0]);
}
else if (creep.store.getFreeCapacity() == 0) {
creep.memory.goal = 'deposit';
}
return;
}
var srcFlag = Game.flags['Haul' + creep.memory.source];
creep.moveTo(srcFlag, {visualizePathStyle: {stroke: '#00ff00'}});
if (creep.pos.isEqualTo(srcFlag.pos)) {
creep.memory.inPos = true;
creep.say('📥 collect');
} else if (creep.pos.isEqualTo(21, 40)) {
creep.move(BOTTOM);
}
} else if (goal == 'deposit'){
if (creep.depositResources()) {
if (creep.store.getUsedCapacity(RESOURCE_ENERGY) == 0) {
if (creep.room.energyAvailable < 400) {
// Move from storage to spawning energy pools
var target = creep.pos.findClosestByRange(FIND_STRUCTURES, {
filter: (structure) => {
return (
structure.structureType == STRUCTURE_CONTAINER ||
structure.structureType == STRUCTURE_STORAGE
) && structure.store.getUsedCapacity(RESOURCE_ENERGY) > 150;
}
});
if (target) {
if (creep.withdraw(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(target);
}
return;
}
}
creep.memory.goal = 'collect';
creep.memory.inPos = false;
}
} else {
creep.memory.goal = 'distribute';
var dumpFlag = Game.flags['DumpUpgrade'];
creep.moveTo(dumpFlag, {visualizePathStyle: {stroke: '#00ff00'}});
if (creep.pos.isEqualTo(dumpFlag.pos)) {
creep.memory.inPos = true;
creep.say('📤 distribute');
}
}
} else if (goal == 'distribute') {
var dumpFlag = Game.flags['DumpUpgrade'];
creep.moveTo(dumpFlag, {visualizePathStyle: {stroke: '#00ff00'}});
if (creep.pos.isEqualTo(dumpFlag.pos)) {
creep.memory.inPos = true;
creep.say('📤 distribute');
}
}
} else if (goal == 'collect') {
var adjCreeps = creep.pos.findInRange(
FIND_MY_CREEPS, 1, {
filter: (adjCreep) => {
return adjCreep.memory.role == 'harvester';
}
}
);
var length = adjCreeps.length;
if (length > 0) {
for (var i = 0; i < length; ++i)
{
var adjHarvester = adjCreeps[i];
adjHarvester.transfer(creep, RESOURCE_ENERGY);
creep.pickup(creep.pos.findClosestByRange(FIND_DROPPED_RESOURCES));
}
}
if (creep.store.getFreeCapacity() == 0) {
creep.memory.goal = 'deposit';
creep.memory.inPos = false;
}
} else if (goal == 'distribute') {
var adjCreeps = creep.pos.findInRange(
FIND_MY_CREEPS, 1, {
filter: (adjCreep) => {
return adjCreep.memory.role == 'upgrader' &&
adjCreep.store.getUsedCapacity(RESOURCE_ENERGY) <= 25;
}
}
);
if (adjCreeps.length > 0) {
creep.transfer(adjCreeps[0], RESOURCE_ENERGY);
}
else {
if (creep.store.getUsedCapacity(RESOURCE_ENERGY) > 0) {
creep.memory.goal = 'deposit';
creep.depositResources();
} else {
creep.goal = 'collect';
creep.inPos = false;
}
}
if (creep.store.getUsedCapacity(RESOURCE_ENERGY) == 0) {
creep.memory.goal = 'collect';
creep.memory.inPos = false;
}
} else if (goal == 'deposit') {
if (creep.depositResources()) {
if (creep.store.getUsedCapacity(RESOURCE_ENERGY) == 0) {
creep.memory.goal = 'collect';
creep.memory.inPos = false;
}
} else {
creep.memory.goal = 'distribute';
var dumpFlag = Game.flags['DumpUpgrade'];
creep.moveTo(dumpFlag, {visualizePathStyle: {stroke: '#00ff00'}});
if (creep.pos.isEqualTo(dumpFlag.pos)) {
creep.memory.inPos = true;
creep.say('📤 distribute');
}
}
}
}
}
module.exports = roleHauler;