-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprototype.creep.js
131 lines (120 loc) · 4.46 KB
/
prototype.creep.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
var roles = {
harvester: require('role.harvester'),
upgrader: require('role.upgrader'),
builder: require('role.builder'),
repairer: require('role.repairer'),
rampartRepairer: require('role.rampartRepairer'),
longDistanceHarvester: require('role.longDistanceHarvester'),
longDistanceHauler: require('role.longDistanceHauler'),
enemyRoomHauler: require('role.enemyRoomHauler'),
newRoomBuilder: require('role.newRoomBuilder'),
newRoomRepairer: require('role.newRoomRepairer'),
claimer: require('role.claimer'),
miner: require('role.miner'),
lorry: require('role.lorry'),
mineralHarvester: require('role.mineralHarvester'),
miningRoomDefender: require('role.miningRoomDefender'),
localMover: require('role.localMover'),
attacker: require('role.attacker'),
healer: require('role.healer'),
roomReserver: require('role.roomReserver'),
drainer: require('role.drainer'),
dismantler: require('role.dismantler'),
controllerAttacker: require('role.controllerAttacker'),
skRoomAttacker: require('role.SKRoomAttacker'),
SKRoomEnergyMiner: require('role.SKRoomEnergyMiner'),
SKRoomHauler: require('role.SKRoomHauler'),
looter: require('role.looter')
};
Creep.prototype.runRole =
function() {
roles[this.memory.role].run(this);
};
/** @function
@param {bool} useContainer
@param {bool} useSource */
Creep.prototype.getEnergy =
function(useContainer, useSource) {
/** @type {StructureContainer} */
let container;
// if the Creep should look for containers
if (useContainer) {
// find closest container or storage with energy
container = this.pos.findClosestByPath(FIND_STRUCTURES, {
filter: s => (s.structureType == STRUCTURE_CONTAINER ||
s.structureType == STRUCTURE_STORAGE) &&
s.store[RESOURCE_ENERGY] > 0
});
// if one was found
if (container != undefined) {
// try to withdraw energy, if the container is not in range
if (this.withdraw(container, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
// move towards it
this.travelTo(container);
}
}
}
// if no container was found look for sources if there are no miners
if (container == undefined && useSource) {
// find closest source
var source = this.pos.findClosestByPath(FIND_SOURCES_ACTIVE);
// try to harvest energy, if the source is not in range
if (this.harvest(source) == ERR_NOT_IN_RANGE) {
// move towards it
this.travelTo(source);
}
}
};
Creep.prototype.depositEnergy =
function() {
creep = this;
var targetStructure;
var controllerContainer;
var homeRoom = Game.rooms[creep.memory.home];
// if we can see our home room...
if (homeRoom != undefined) {
// find closest spawn or extension which is not full
if (targetStructure == undefined) {
targetStructure = creep.pos.findClosestByPath(FIND_MY_STRUCTURES, {
filter: (s) => (s.structureType == STRUCTURE_SPAWN ||
s.structureType == STRUCTURE_EXTENSION) &&
s.energy < s.energyCapacity
});
}
// if the storage is full, drop it into the container by the controller
if (targetStructure == undefined) {
controllerContainer = homeRoom.controller.pos.findInRange(FIND_STRUCTURES, 3, {
filter: s => s.structureType == STRUCTURE_CONTAINER &&
s.store[RESOURCE_ENERGY] < s.storeCapacity
})[0];
if (controllerContainer != undefined) {
targetStructure = controllerContainer;
}
}
// if the storage and the controllerContainer are full, store it in the terminal
if (targetStructure == undefined) {
if (homeRoom.terminal != undefined) {
if (homeRoom.terminal.store[RESOURCE_ENERGY] <= 50000) {
targetStructure = homeRoom.terminal;
}
}
}
// look for storage
if (homeRoom.storage != undefined && targetStructure == undefined) {
targetStructure = homeRoom.storage;
}
// if we found a target
if (targetStructure != undefined) {
// try to transfer energy, if it is not in range
for (const resourceType in creep.carry) {
if (creep.transfer(targetStructure, resourceType) == ERR_NOT_IN_RANGE) {
creep.travelTo(targetStructure);
}
}
}
}
// if we can't see our home room, set up a scouting mission to go to it
else {
console.log("Cannot see room ", homeRoom);
}
}