|
14 | 14 | * - gridMovement: If true, possible movement is defined through connections on spaces. Defaults to false. |
15 | 15 | * - symmetricConnections: If true, any connections between spaces are assumed to go both ways. |
16 | 16 | * Only relevant if gridMovement is true. Defaults to true. |
| 17 | + * - cacheGraph: If true, the created map of connections between spaces is stored between game iterations. |
| 18 | + * Only use if the connections do not change within or between games. Defaults to false. |
17 | 19 | * |
18 | 20 | * @param {Array} spacesDataArray: An array of objects describing each space on the track. |
19 | 21 | * See Space class for details. |
|
23 | 25 | class Track { |
24 | 26 | constructor(trackData, spacesDataArray = false, pawnsDataArray = false) { |
25 | 27 | // Add default settings, overwrite with provided data. |
26 | | - Object.assign(this, applyDefaults(global.defaults.track, trackData)); |
| 28 | + Object.assign(this, applyDefaults(BPTstatic.defaults.track, trackData)); |
27 | 29 | // Verify that an ID is present. |
28 | 30 | if (this.id === undefined) |
29 | 31 | throw('Tracks must have an id property set.'); |
@@ -60,19 +62,29 @@ class Track { |
60 | 62 | // Build a graph of how the spaces connect, if advanced movement is used. |
61 | 63 | // Data used by the a-star algorithm, to find paths in the grid. |
62 | 64 | if (this.gridMovement) { |
63 | | - this.graph = []; |
64 | | - for (let i = 0; i < this.spaces.length; i++) |
65 | | - this.graph.push([]); |
66 | | - for (let s of this.spaces) { |
67 | | - for (let c of s.connectsTo) { |
68 | | - let target = new ObjectFilter({id: c}).findFirstInArray(this.spaces); |
69 | | - this.graph[s.index][target.index] = 1; |
70 | | - if (this.symmetricConnections) |
71 | | - this.graph[target.index][s.index] = 1; |
| 65 | + if (this.cacheGraph) { |
| 66 | + this.graph = getCache('track.' + this.id + '.grid'); |
| 67 | + this.heuristic = getCache('track.' + this.id + '.heuristic'); |
| 68 | + } |
| 69 | + else |
| 70 | + this.graph = false; |
| 71 | + if (!this.graph) { |
| 72 | + this.graph = []; |
| 73 | + for (let i = 0; i < this.spaces.length; i++) |
| 74 | + this.graph.push([]); |
| 75 | + for (let s of this.spaces) { |
| 76 | + for (let c of s.connectsTo) { |
| 77 | + let target = new ObjectFilter({id: c}).findFirstInArray(this.spaces); |
| 78 | + this.graph[s.index][target.index] = 1; |
| 79 | + if (this.symmetricConnections) |
| 80 | + this.graph[target.index][s.index] = 1; |
| 81 | + } |
72 | 82 | } |
| 83 | + let row = Array(this.graph.length).fill(1); |
| 84 | + this.heuristic = Array(this.graph.length).fill(row); |
| 85 | + setCache('track.' + this.id + '.grid', this.graph); |
| 86 | + setCache('track.' + this.id + '.heuristic', this.heuristic); |
73 | 87 | } |
74 | | - let row = Array(this.graph.length).fill(1); |
75 | | - this.heuristic = Array(this.graph.length).fill(row); |
76 | 88 | this.pawnPaths = {}; |
77 | 89 | } |
78 | 90 |
|
@@ -138,7 +150,7 @@ class Track { |
138 | 150 | if (this.assumePresent) { |
139 | 151 | return new Pawn({id: pawnId}, this); |
140 | 152 | } |
141 | | - if (global.debugRunning) |
| 153 | + if (BPTstatic.debugRunning) |
142 | 154 | return false; |
143 | 155 | throw('Tried to get pawn ' + pawnId + ' but no such pawn exist on track + ' + this.id + '.'); |
144 | 156 | } |
|
0 commit comments