-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrole.looter.js
92 lines (87 loc) · 3.31 KB
/
role.looter.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
module.exports = {
// a function to run the logic for this role
/** @param {Creep} creep */
run: function(creep) {
// if target is defined and creep is not in target room
if (creep.memory.target != undefined &&
creep.room.name != creep.memory.target &&
creep.memory.working == false) {
if (creep.ticksToLive > 120) {
creep.travelTo(new RoomPosition(25, 25, creep.memory.target));
return;
} else {
creep.suicide();
}
}
// if creep is bringing energy to a structure but has no energy left
if (creep.memory.working == true && _.sum(creep.carry) == 0) {
// switch state
creep.memory.working = false;
}
// if creep is picking up but is full
else if (creep.memory.working == false && _.sum(creep.carry) == creep.carryCapacity) {
// switch state
creep.memory.working = true;
}
// if creep is supposed to transfer energy to a structure
if (creep.memory.working == true) {
// if in home room
if (creep.room.name == creep.memory.home) {
// if(creep.store[0].resourceType == RESOURCE_ENERGY &&
// creep.room.energyAvailable < creep.room.energyCapacityAvailable){
// creep.depositEnergy();
// }
var terminal = creep.room.terminal;
if (terminal != null) {
if (_.sum(creep.carry) < terminal.storeCapacity - _.sum(terminal.store)) {
if (creep.transfer(terminal, _.findKey(creep.carry)) == ERR_NOT_IN_RANGE) {
// move towards it
creep.travelTo(terminal);
}
} else {
var storage = creep.pos.findClosestByPath(FIND_MY_STRUCTURES, {
filter: s => (s.structureType == STRUCTURE_STORAGE)
});
if (storage != null) {
if (_.sum(creep.carry) < storage.storeCapacity - _.sum(storage.store)) {
if (creep.transfer(storage, _.findKey(creep.carry)) == ERR_NOT_IN_RANGE) {
// move towards it
creep.travelTo(storage);
}
}
}
}
}
}
// if not in target room
else {
creep.travelTo(new RoomPosition(25, 25, creep.memory.home));
}
}
// if creep is supposed to collect
else {
// if creep is in target room
if (creep.room.name == creep.memory.target) {
var target = creep.pos.findClosestByPath(FIND_STRUCTURES, {
filter: (structure) => (structure.structureType == STRUCTURE_CONTAINER ||
structure.structureType == STRUCTURE_LAB ||
structure.structureType == STRUCTURE_EXTENSION ||
structure.structureType == STRUCTURE_LINK ||
structure.structureType == STRUCTURE_NUKER ||
structure.structureType == STRUCTURE_TOWER ||
structure.structureType == STRUCTURE_SPAWN ||
structure.structureType == STRUCTURE_STORAGE ||
structure.structureType == STRUCTURE_TERMINAL) &&
_.sum(structure.store) > 0
});
if (target != undefined) {
// get whatever resource is first in the storage
if (creep.withdraw(target, _.findKey(target.store)) == ERR_NOT_IN_RANGE) {
// move towards it
creep.travelTo(target);
}
}
}
}
}
};