-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscouting.scoutingUtility.js
145 lines (127 loc) · 4.5 KB
/
scouting.scoutingUtility.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
const { SK_PATHING_COST, pathSets } = require("./constants");
const { cachePathMatrix } = require("./extension.betterPathing");
/**
* Performs a breadth-first search of neighbouring rooms until an unexplored room has been found.
* @param {string} startRoomName The name of the room to start in.
* @param {number} maxDepth The max iterations to search for. After this, null will be returned.
* @returns {string} The name of the first unexplored room found.
*/
const searchForUnexploredRoomsNearby = (startRoomName, maxDepth) => {
let current = Object.values(Game.map.describeExits(startRoomName));
for (let i = 0; i < maxDepth; i++) {
let next = [];
for (const room of current) {
if (!Memory.scoutData[room]) {
return room;
}
next.push(...Object.values(Game.map.describeExits(room)));
}
current = next;
}
// None found before maxDepth expired
return null;
};
const packageScoutingData = (room) => {
const recordPosAndID = (structure) => {
return {
pos: {
x: structure.pos.x,
y: structure.pos.y,
},
id: structure.id,
};
};
const roomData = getScoutingData(room.name) || {};
// Record some important scouting data
roomData.lastVisit = Game.time;
if (room.controller) {
roomData.controller = recordPosAndID(room.controller);
if (room.controller.owner) {
roomData.controller.owner = room.controller.owner.username;
roomData.controller.level = room.controller.level;
}
}
roomData.sources = room
.find(FIND_SOURCES)
.map((source) => recordPosAndID(source));
roomData.minerals = room.find(FIND_MINERALS).map((mineral) => {
const info = recordPosAndID(mineral);
info.density = mineral.density;
info.type = mineral.mineralType;
return info;
});
roomData.towers = 0;
roomData.spawns = 0;
roomData.invaderCores = [];
roomData.keeperLairs = [];
for (const structure of room.find(FIND_STRUCTURES)) {
if (structure.structureType === STRUCTURE_INVADER_CORE) {
roomData.invaderCores.push({
x: structure.pos.x,
y: structure.pos.y,
});
} else if (structure.structureType === STRUCTURE_KEEPER_LAIR) {
roomData.keeperLairs.push({
x: structure.pos.x,
y: structure.pos.y,
});
}
// If this room has towers and isn't ours, we'll mark it
// to avoid in our pathsets
if (structure.structureType === STRUCTURE_TOWER) {
roomData.towers++;
cachePathMatrix(false, pathSets.travel, room.name);
} else if (structure.structureType === STRUCTURE_SPAWN) {
roomData.spawns++;
}
}
// If this room is an SK room, we'll update our "travel" pathset to avoid the keeper patrol points
// We'll also mark down the positions so we know where they are next time
if (roomData.keeperLairs.length && roomData.towers === 0) {
roomData.keeperPositions = room
.find(FIND_HOSTILE_CREEPS)
.filter((c) => c.owner.username === "Source Keeper")
.map((c) => c.pos);
const matrix = createSKMatrix(roomData.keeperPositions);
cachePathMatrix(matrix, pathSets.travel, room.name);
}
return roomData;
};
const getScoutingData = (roomName) => {
return Memory.scoutData[roomName];
};
const setScoutingData = (roomName, newData) => {
Memory.scoutData[roomName] = newData;
};
const createSKMatrix = (avoidPositions) => {
const newMatrix = new PathFinder.CostMatrix();
for (const pos of avoidPositions) {
for (let x = pos.x - 3; x <= pos.x + 3; x++) {
for (let y = pos.y - 3; y <= pos.y + 3; y++) {
if (x < 0 || x > 49 || y < 0 || y > 49) {
continue;
}
newMatrix.set(x, y, SK_PATHING_COST);
}
}
}
return newMatrix;
};
const restoreSKMatrices = () => {
for (const room in Memory.scoutData) {
if (Memory.scoutData[room].keeperPositions) {
cachePathMatrix(
createSKMatrix(Memory.scoutData[room].keeperPositions),
pathSets.travel,
room
);
}
}
};
module.exports = {
searchForUnexploredRoomsNearby,
packageScoutingData,
getScoutingData,
setScoutingData,
restoreSKMatrices,
};