-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathscreeps-perf.js
112 lines (100 loc) · 3.83 KB
/
screeps-perf.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
var originalFindPath = Room.prototype.findPath;
var setup = false;
function creepMemoryCleanUp() {
if (Game.time - Memory.screepsPerf.lastMemoryCleanUp > 100) {
Object.keys(Memory.creeps).forEach(creepName => {
if (!Game.creeps[creepName]) {
Memory.creeps[creepName] = undefined;
}
});
Memory.screepsPerf.lastMemoryCleanUp = Game.time;
}
};
module.exports = function(options) {
if (!setup) {
options = options || {};
Memory.screepsPerf = Memory.screepsPerf || {
lastMemoryCleanUp: Game.time
};
if (options.speedUpArrayFunctions || options.speedUpArrayFunctions === undefined) {
Array.prototype.filter = function(callback, thisArg) {
var results = [];
var arr = this;
for (var iterator = 0; iterator < arr.length; iterator++) {
if (callback.call(thisArg, arr[iterator], iterator, arr)) {
results.push(arr[iterator]);
}
}
return results;
};
Array.prototype.forEach = function(callback, thisArg) {
var arr = this;
for (var iterator = 0; iterator < arr.length; iterator++) {
callback.call(thisArg, arr[iterator], iterator, arr);
}
};
Array.prototype.map = function(callback, thisArg) {
var arr = this;
var returnVal = [];
for (var iterator = 0; iterator < arr.length; iterator++) {
returnVal.push(callback.call(thisArg, arr[iterator], iterator, arr));
}
return returnVal;
};
}
/**
* Creep memory clean up... this speeds up the initial memory parse each tick.
*/
if (options.cleanUpCreepMemory || options.cleanUpCreepMemory === undefined) {
// Monkey patch creating creeps so that we can clean up their memory without forcing the user to make a call.
var originalCreateCreep = Spawn.prototype.createCreep;
Spawn.prototype.createCreep = function() {
creepMemoryCleanUp();
return originalCreateCreep.apply(this, arguments);
};
}
/**
* FIND PATH OPTIMIZATION
* This cache's the built in findPath results in memory and reuses them as long as that same path is used at least 1/300 ticks.
* The cached path is also refreshed every 2000 ticks. This helps to ensure that creeps respond to changing room terrain.
*/
if (options.optimizePathFinding || options.optimizePathFinding === undefined) {
function roomPositionIdentifier(roomPosition) {
return roomPosition.roomName + 'x' + roomPosition.x + 'y' + roomPosition.y;
};
Room.prototype.findPath = function(fromPos, toPos, options) {
creepMemoryCleanUp();
if (!Memory.pathOptimizer) {
Memory.pathOptimizer = { lastCleaned: Game.time};
}
if (Game.time - Memory.pathOptimizer.lastCleaned > 40 && !this._cleanedUp) {
var keys = Object.keys(Memory.pathOptimizer);
keys.forEach((key) => {
var val = Memory.pathOptimizer[key];
if (val && ((val.used / (Game.time - val.tick) < 1 / 300) || Game.time - val.tick > 2000)) {
Memory.pathOptimizer[key] = undefined;
}
});
this._cleanedUp = true;
Memory.pathOptimizer.lastCleaned = Game.time;
}
var pathIdentifier = roomPositionIdentifier(fromPos) + roomPositionIdentifier(toPos);
if (!Memory.pathOptimizer[pathIdentifier]) {
var path = originalFindPath.apply(this, arguments);
Memory.pathOptimizer[pathIdentifier] = {
tick: Game.time,
path: Room.serializePath(path),
used: 1
}
} else {
Memory.pathOptimizer[pathIdentifier].used++;
}
return Room.deserializePath(Memory.pathOptimizer[pathIdentifier].path);
}
}
setup = true;
}
return {
originalFindPath: originalFindPath
}
};