-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCostMatrix.js
More file actions
110 lines (105 loc) · 4.06 KB
/
CostMatrix.js
File metadata and controls
110 lines (105 loc) · 4.06 KB
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
'use strict';
const obj = {
getCostMatrixFunction(creep, targetPos) {
if (creep.room.name === targetPos.roomName) {
return undefined;
} else {
return preferHighwayRooms;
}
function defaultCostMatrix(roomName, matrix) {
if (Game.rooms[roomName]) {
let room = Game.rooms[roomName];
for (let s of room.find(FIND_STRUCTURES, {
filter: (s) => s.structureType !== STRUCTURE_ROAD &&
s.structureType !== STRUCTURE_CONTAINER &&
!(s.structureType === STRUCTURE_RAMPART && s.my)
}).concat(room.find(FIND_CREEPS)).concat(room.find(FIND_FLAGS, {
filter: (f) => f.color === COLOR_ORANGE
}))) {
matrix.set(s.pos.x, s.pos.y, 255);
}
}
}
function preferHighwayRooms(roomName, matrix)
{
if(roomName === targetPos.roomName){
defaultCostMatrix(roomName, matrix);
return;
}
if(isSKRoom(roomName)){
skRoom(roomName, matrix);
return;
}
let {x, y} = Room.parseRoomName(roomName);
let rt = new Room.Terrain(roomName);
if (x % 10 !== 0 && y % 10 !== 0) {
for (let x = 0; x < 50; x++) {
for (let y = 0; y < 50; y++) {
if (rt.get(x, y) === TERRAIN_MASK_SWAMP) {
matrix.set(x, y, 20);
} else if (rt.get(x, y) === TERRAIN_MASK_WALL) {
matrix.set(x, y, 255);
} else {
matrix.set(x, y, 4);
}
}
}
}
if (Game.rooms[roomName]) {
let room = Game.rooms[roomName];
for (let s of room.find(FIND_STRUCTURES, {
filter: (s) => s.structureType !== STRUCTURE_ROAD &&
s.structureType !== STRUCTURE_CONTAINER &&
!(s.structureType === STRUCTURE_RAMPART && s.my)
}).concat(room.find(FIND_CREEPS)).concat(room.find(FIND_FLAGS, {
filter: (f) => f.color === COLOR_ORANGE
}))) {
matrix.set(s.pos.x, s.pos.y, 255);
}
}
}
function skRoom(roomName, matrix) {
if(!Game.rooms[roomName]) return;
let sks = Game.rooms[roomName].find(FIND_HOSTILE_CREEPS, {
filter: (c) => c.owner.username === 'Source Keeper'
});
for(let sk of sks){
for(let x = -3; x <= 3; x++){
for(let y = -3; y <= 3; y++){
let px = sk.pos.x + x;
let py = sk.pos.y + y;
if(px >= 0 && px <= 49 && py >=0 && py <= 49){
matrix.set(px, py, 0xff)
}
}
}
}
}
},
/**
* returns a default costMatrix, which sets impassable structures to 255
* roads to 1
* @param roomName {string}
*/
getDefaultCostMatrix(roomName){
if(!Game.rooms[roomName]) return undefined;
let room = Game.rooms[roomName];
let matrix = new PathFinder.CostMatrix();
for(let s of room.find(FIND_STRUCTURES)){
if(s.structureType === STRUCTURE_ROAD){
matrix.set(s.pos.x, s.pos.y, 1);
}else if(s.structureType !== STRUCTURE_CONTAINER &&
(s.structureType !== STRUCTURE_RAMPART || !s.my)){
matrix.set(s.pos.x, s.pos.y, 0xff);
}
}
return matrix;
}
};
function isSKRoom(roomName){
let {x, y} = Room.parseRoomName(roomName);
return (x % 10 === 4 || x % 10 === 5 || x % 10 === 6) &&
(y % 10 === 4 || y % 10 === 5 || y % 10 === 6) &&
!(x % 10 === 5 && y % 10 === 5)
}
module.exports = obj;