-
Notifications
You must be signed in to change notification settings - Fork 7
/
construction.exitWalls.js
97 lines (83 loc) · 3.69 KB
/
construction.exitWalls.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
const edgeMappings = {
[TOP]: { parallel: "x", orthogonal: "y", orthogonalPos: 2, orthogonalSide: 1 },
[BOTTOM]: { parallel: "x", orthogonal: "y", orthogonalPos: 47, orthogonalSide: 48 },
[LEFT]: { parallel: "y", orthogonal: "x", orthogonalPos: 2, orthogonalSide: 1 },
[RIGHT]: { parallel: "y", orthogonal: "x", orthogonalPos: 47, orthogonalSide: 48 }
}
function eachWallPosition(exit, callback) {
let edge = edgeMappings[exit.edge];
for(let parallelPos = exit.start - 2; parallelPos <= exit.end + 2; parallelPos++) {
callback({ [edge.parallel]: parallelPos, [edge.orthogonal]: edge.orthogonalPos });
}
callback({ [edge.parallel]: exit.start - 2, [edge.orthogonal]: edge.orthogonalSide });
callback({ [edge.parallel]: exit.end + 2, [edge.orthogonal]: edge.orthogonalSide });
}
function posToEdge(position) {
if(position.x === 0) return LEFT;
if(position.y === 0) return TOP;
if(position.x === 49) return RIGHT;
if(position.y === 49) return BOTTOM;
}
function extendExit(exit, position) {
let edge = posToEdge(position);
if(exit.edge !== edge) return false;
let edgeInfo = edgeMappings[edge];
if(position[edgeInfo.parallel] === exit.start - 1) {
exit.start -= 1;
return true;
}
if(position[edgeInfo.parallel] === exit.end + 1) {
exit.end += 1;
return true;
}
return false;
}
module.exports = {
type: "exitWalls",
outline: function(room, exit) {
let edge = edgeMappings[exit.edge];
let positions = [
{ [edge.orthogonal]: edge.orthogonalSide, [edge.parallel]: exit.start - 2 },
{ [edge.orthogonal]: edge.orthogonalPos, [edge.parallel]: exit.start - 2 },
{ [edge.orthogonal]: edge.orthogonalPos, [edge.parallel]: exit.end + 2 },
{ [edge.orthogonal]: edge.orthogonalSide, [edge.parallel]: exit.end + 2 }
];
room.visual.poly(_.map(positions, (p) => [p.x, p.y]), { stroke: "#77f" });
},
build: function(proxy, exit) {
// keeping number of construction sites in low-level rooms down
if(proxy.room.controller.level < 5) return;
eachWallPosition(exit, (pos) => proxy.planConstruction(pos.x, pos.y, STRUCTURE_RAMPART));
},
updateCostMatrix: function(matrix, exit) { },
addBuilding: function(memory, flag) { },
removeBuilding: function(memory, flag) {
let edge = posToEdge(flag.pos);
let pos = flag.pos[edgeMappings[edge].parallel];
let wall = _.find(memory, (w) => w.edge === edge && w.start <= pos && w.end >= pos);
if(wall) {
let index = _.findIndex(memory, wall);
if(index >= 0) memory.splice(index, 1);
}
},
plan: function(spaceFinder, buildings, room) {
let existingWalls = _.map(_.filter(buildings, (b) => b.type === this.type), (b) => b.memory);
let result = [];
let currentExit = {};
for(let pos of room.find(FIND_EXIT)) {
if(!extendExit(currentExit, pos)) {
if(currentExit.edge && !_.any(existingWalls, (w) => w.edge === currentExit.edge && w.start === currentExit.start)) result.push(currentExit);
let edge = posToEdge(pos);
currentExit = {
edge: edge,
start: pos[edgeMappings[edge].parallel],
end: pos[edgeMappings[edge].parallel]
};
}
}
if(currentExit.edge && !_.any(existingWalls, (w) => w.edge === currentExit.edge && w.start === currentExit.start)) result.push(currentExit);
return result;
}
};
const profiler = require("screeps-profiler");
profiler.registerObject(module.exports, 'construction.exitWalls');