-
Notifications
You must be signed in to change notification settings - Fork 3
/
role.transporter.js
62 lines (59 loc) · 2.7 KB
/
role.transporter.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
Creep.prototype.roleTransporter = function () {
//Find flag
var flagName = this.findMyFlag("transporter");
var destinationFlag = _.filter(Game.flags,{ name: flagName})[0];
if (destinationFlag != null) {
if (_.sum(this.carry) == 0) {
this.memory.empty = true;
}
if (_.sum(this.carry) == this.carryCapacity) {
this.memory.empty = false;
}
var resource = destinationFlag.memory.resource;
if (this.memory.empty == true) {
// Transporter empty
if (this.memory.targetContainer != undefined) {
delete this.memory.targetContainer;
}
if (this.goToHomeRoom() == true) {
//Transporter at home
var originContainer = this.findResource(resource, STRUCTURE_STORAGE, STRUCTURE_CONTAINER, STRUCTURE_LINK);
if (originContainer != null && this.withdraw(originContainer, resource) == ERR_NOT_IN_RANGE) {
this.moveTo(originContainer, {reusePath: moveReusePath()});
}
}
}
else {
if (this.room.name == destinationFlag.pos.roomName) {
//Creep in destination room
let targetContainer;
if (this.memory.targetContainer == undefined || this.memory.targetContainer == null || Game.time % 8 == 0) {
if (this.room.controller.owner != undefined && this.room.controller.owner.username == playerUsername) {
targetContainer = this.findResource(RESOURCE_SPACE, STRUCTURE_CONTAINER);
}
else {
targetContainer = this.pos.findClosestByPath(FIND_STRUCTURES, {filter: (s) => s.structureType == STRUCTURE_CONTAINER && _.sum(s.store) < s.storeCapacity});
}
if (targetContainer != undefined && targetContainer != null) {
this.memory.targetContainer = targetContainer.id;
}
}
else {
targetContainer = Game.getObjectById(this.memory.targetContainer);
}
if (targetContainer != null) {
let result = this.transfer(targetContainer, resource);
if (result == ERR_NOT_IN_RANGE) {
this.moveTo(targetContainer, {reusePath: moveReusePath()});
}
else if (result != OK) {
delete this.memory.targetContainer;
}
}
}
else {
this.moveTo(destinationFlag, {reusePath: moveReusePath()})
}
}
}
};