-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServiceRoomCalc.js
90 lines (73 loc) · 1.9 KB
/
ServiceRoomCalc.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
"use strict";
const Service = require('Service');
module.exports = class ServiceRoomCalc extends Service
{
constructor(locator)
{
super();
this.screepsApi = locator.getService(SERVICE_NAMES.SCREEPS_API);
}
openPosAroundTakeNearestExcept(aroundThisPos, nearestThisPos, exceptThesePos)
{
let candidates =
_.filter(
this.filterBlockedPositions(
this.getRoomPositionsInRange(aroundThisPos.x, aroundThisPos.y, aroundThisPos.roomName, 1)
),
function(rp)
{
for(let index in exceptThesePos)
if( exceptThesePos[index][0] === rp.x &&
exceptThesePos[index][1] === rp.y &&
exceptThesePos[index][2] === rp.roomName)
return false;
return true;
}
);
return nearestThisPos.findClosestByPath(candidates, {ignoreCreeps: true, ignoreRoads: true});
}
openPosAroundTakeNearest(aroundThisPos, nearestThisPos)
{
let candidates = this.filterBlockedPositions(
this.getRoomPositionsInRange(aroundThisPos.x, aroundThisPos.y, aroundThisPos.roomName, 1));
return nearestThisPos.findClosestByPath(candidates, {ignoreCreeps: true, ignoreRoads: true});
}
getRoomPositionsInRange(centerX, centerY, roomName, range)
{
let results = [];
for(let x = centerX - range; x <= centerX + range; x++)
{
if(! this.isOnRoomInside(x))
continue;
for(let y = centerY - range; y <= centerY + range; y++)
{
if(! this.isOnRoomInside(y) || (x === centerX && y === centerY))
continue;
results.push(this.screepsApi.getRoomPosition([x, y, roomName]));
}
}
return results;
}
filterBlockedPositions(positions)
{
let result = [];
positions.forEach((position) =>
{
if(position.lookFor(LOOK_TERRAIN)[0] !== "wall")
result.push(position);
});
return result;
}
isInRoom(val)
{
return val >= 0 && val <= 49;
}
isOnRoomInside(val)
{
return val > 0 && val < 49;
}
isOnRoomBoarder(val)
{
return val === 0 || val === 49;
}
};