-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.haulingRequestManager.js
164 lines (155 loc) · 6.14 KB
/
manager.haulingRequestManager.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
const Colony = require("./data.colony");
const remoteUtility = require("./remote.remoteUtility");
const { getPlanData, keys } = require("./base.planningUtility");
const profiler = require("./debug.profiler");
class HaulingRequestManager {
/**
* Creates some basic requests for a colony, including:
* Dropoff requests:
* - Spawns and extensions
* Pickup requests:
* - Miner containers + overflow
* - Dropped energy
* - Ruins
* - Tombstones
* @param {Colony} colony The base to create requests for.
*/
generateBasicRequests(colony) {
profiler.startSample("basic structures");
const spawnStructuresAndTowers = colony.structures[
STRUCTURE_SPAWN
].concat(colony.structures[STRUCTURE_EXTENSION] || []).concat(
colony.structures[STRUCTURE_TOWER] || []
);
for (const spawnStructure of spawnStructuresAndTowers) {
const freeCapacity =
spawnStructure.store.getFreeCapacity(RESOURCE_ENERGY);
if (!freeCapacity) {
continue;
}
// Towers don't always need to be full
const isUrgent =
spawnStructure.structureType !== STRUCTURE_TOWER ||
colony.enemies.length ||
spawnStructure.store[RESOURCE_ENERGY] <
spawnStructure.store.getCapacity() / 2;
profiler.wrap("create request", () =>
colony.createDropoffRequest(
freeCapacity,
RESOURCE_ENERGY,
[spawnStructure.id],
isUrgent
)
);
}
profiler.endSample("basic structures");
profiler.startSample("upgraders");
const upgraderContainer = colony.getUpgraderContainer();
if (
colony.upgraders.length &&
upgraderContainer &&
// Some arbitrary value here to prevent haulers from constantly depositing small amounts
upgraderContainer.store.getFreeCapacity() > 500
) {
// Request energy for our container
colony.createDropoffRequest(
upgraderContainer.store.getFreeCapacity(),
RESOURCE_ENERGY,
[upgraderContainer.id]
);
}
profiler.endSample("upgraders");
// Add miner containers for our base
profiler.startSample("containers");
const sourceContainers = getPlanData(
colony.room.name,
keys.sourceContainerPositions
).map((p) => new RoomPosition(p.x, p.y, colony.room.name));
for (const containerPos of sourceContainers) {
const container = containerPos
.lookFor(LOOK_STRUCTURES)
.find((s) => s.structureType === STRUCTURE_CONTAINER);
colony.createPickupRequest(
container ? container.store[RESOURCE_ENERGY] : 0,
RESOURCE_ENERGY,
SOURCE_ENERGY_CAPACITY / ENERGY_REGEN_TIME,
true,
containerPos
);
}
profiler.endSample("containers");
// Here we'll add all containers as pickup requests, and track remote rooms
profiler.startSample("remotes");
if (colony.remotePlans) {
const importantRooms = new Set();
importantRooms.add(colony.room.name);
for (const remote of colony.remotePlans) {
if (!Game.rooms[remote.room]) {
continue;
}
const containerPos = new RoomPosition(
remote.container.x,
remote.container.y,
remote.container.roomName
);
const container = containerPos
.lookFor(LOOK_STRUCTURES)
.find((s) => s.structureType === STRUCTURE_CONTAINER);
colony.createPickupRequest(
container ? container.store[RESOURCE_ENERGY] : 0,
RESOURCE_ENERGY,
SOURCE_ENERGY_CAPACITY / ENERGY_REGEN_TIME,
true,
containerPos
);
importantRooms.add(remote.room);
}
// Then, for each remote, let's search and add all dropped resources
for (const room of importantRooms) {
const droppedResources = Game.rooms[room].find(
FIND_DROPPED_RESOURCES
);
for (const dropped of droppedResources) {
const hasSourceNeighbour = () => {
for (
let x = dropped.pos.x - 1;
x <= dropped.pos.x + 1;
x++
) {
for (
let y = dropped.pos.y - 1;
y <= dropped.pos.y + 1;
y++
) {
if (x <= 0 || x >= 49 || y <= 0 || y >= 49) {
continue;
}
if (
Game.rooms[room].lookForAt(
LOOK_SOURCES,
x,
y
)[0]
) {
return true;
}
}
}
return false;
};
colony.createPickupRequest(
dropped.amount,
dropped.resourceType,
Math.ceil(dropped.amount / ENERGY_DECAY),
hasSourceNeighbour(),
dropped.pos
);
}
// TODO //
// Tombstones and ruins
}
}
profiler.endSample("remotes");
}
}
module.exports = HaulingRequestManager;