-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrole.SKRoomHauler.js
167 lines (152 loc) · 5.69 KB
/
role.SKRoomHauler.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
module.exports = {
// a function to run the logic for this role
/** @param {Creep} creep */
run: function(creep) {
// Make sure there aren't any hostile creeps within 5 spaces of the creep
closestTarget = creep.pos.findClosestByRange(FIND_HOSTILE_CREEPS);
if (closestTarget != undefined) {
if (creep.pos.inRangeTo(closestTarget, 5)) {
let targets = _.map(creep.room.find(FIND_HOSTILE_CREEPS),
c => ({
pos: c.pos,
range: 3
}));
let ret = PathFinder.search(
creep.pos, targets, {
// We need to set the defaults costs higher so that we
// can set the road cost lower in `roomCallback`
plainCost: 2,
swampCost: 10,
flee: true,
roomCallback: function(roomName) {
let room = Game.rooms[roomName];
// In this example `room` will always exist, but since
// PathFinder supports searches which span multiple rooms
// you should be careful!
if (!room) return;
let costs = new PathFinder.CostMatrix;
room.find(FIND_STRUCTURES).forEach(function(struct) {
if (struct.structureType === STRUCTURE_ROAD) {
// Favor roads over plain tiles
costs.set(struct.pos.x, struct.pos.y, 1);
}
});
// Avoid creeps in the room
room.find(FIND_CREEPS).forEach(function(creep) {
costs.set(creep.pos.x, creep.pos.y, 0xff);
});
return costs;
},
}
);
let pos = ret.path[0];
creep.move(creep.pos.getDirectionTo(pos));
return;
}
}
// 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) {
// find exit to target room
var exit = creep.room.findExitTo(creep.memory.target);
// move to exit
creep.travelTo(creep.pos.findClosestByRange(exit), {
maxRooms: 1
});
// return the function to not do anything else
return;
}
// if creep is bringing energy to a structure but has no energy left
if (creep.memory.working == true && creep.carry.energy == 0) {
// switch state
creep.memory.working = false;
}
// if creep is picking up energy but is full
else if (creep.memory.working == false && creep.carry.energy == creep.carryCapacity) {
// switch state
creep.memory.working = true;
delete creep.memory.targetContainer;
}
// if creep is supposed to transfer energy to a structure
if (creep.memory.working == true) {
// if in home room
creep.depositEnergy();
}
// if creep is supposed to get energy
else {
// if creep is in target room
if (creep.room.name == creep.memory.target) {
// if creep doesn't already have a target container in memory
if (creep.memory.targetContainer == undefined) {
// Find the container with the most energy and isn't empty.
var target = Game.rooms[creep.memory.target].find(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_CONTAINER && structure.store[RESOURCE_ENERGY] > 0);
}
});
if (target.length) {
var allContainer = [];
// Calculate the percentage of energy in each container.
for (var i = 0; i < target.length; i++) {
allContainer.push({
energyPercent: ((target[i].store.energy / target[i].storeCapacity) * 100),
id: target[i].id
});
}
// Get the container containing the most energy.
var highestContainer = _.max(allContainer, function(container) {
return container.energyPercent;
});
// set the target in memory so the creep dosen't
// change target in the middle of the room.
creep.memory.targetContainer = highestContainer.id;
}
}
// if creep has a target container
if (creep.memory.targetContainer != undefined) {
// try to withdraw energy, if the container is not in range
let container = Game.getObjectById(creep.memory.targetContainer);
if (creep.withdraw(container, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
// move towards it
creep.travelTo(container, {
maxRooms: 1
});
return;
}
}
// look for dropped energy if can't find a container
else {
var droppedEnergy = creep.pos.findClosestByRange(FIND_DROPPED_RESOURCES);
if (droppedEnergy) {
if (creep.pickup(droppedEnergy) == ERR_NOT_IN_RANGE) {
creep.travelTo(droppedEnergy);
return;
}
}
}
// if creep cannot find a target container, move to the idle flag
if (creep.memory.targetContainer == undefined) {
idleFlag = _.find(Game.flags, (c) =>
creep.room.name == creep.memory.target);
if (idleFlag != undefined) {
if (!creep.pos.inRangeTo(idleFlag, 1)) {
creep.travelTo(idleFlag.pos, {
maxRooms: 1
});
}
}
}
}
// if not in target room
else {
// find exit to target room
var exit = creep.room.findExitTo(creep.memory.target);
// move to exit
creep.travelTo(creep.pos.findClosestByRange(exit), {
maxRooms: 1
});
}
}
}
};