@@ -3,6 +3,18 @@ import dijkstra from "dijkstrajs";
3
3
import { ForkNode , reverseConnection } from "./ForkNode" ;
4
4
import { StairNode , onFloor } from "./StairNode" ;
5
5
import { serializeNode , nodeFromString , Node } from "./node" ;
6
+ import { OneWay } from "./Hallway" ;
7
+
8
+ export type HallConnectorsStructures <
9
+ ForkName extends string ,
10
+ StairName extends string
11
+ > = {
12
+ nodes : {
13
+ nodeId : Node < ForkName , StairName > ;
14
+ edgeLengthFromPreviousNodeInHallway : number ;
15
+ } [ ] ;
16
+ oneWay : OneWay ;
17
+ } [ ] ;
6
18
7
19
/**
8
20
* @ignore
@@ -11,12 +23,10 @@ function getHallwayConnections<
11
23
ForkName extends string ,
12
24
StairName extends string
13
25
> (
14
- hallConnections : {
15
- nodeId : Node < ForkName , StairName > ;
16
- edgeLengthFromPreviousNodeInHallway : number ;
17
- } [ ] [ ]
26
+ hallConnections : HallConnectorsStructures < ForkName , StairName >
18
27
) : [ string , string ] [ ] {
19
28
return hallConnections
29
+ . map ( hallway => hallway . nodes )
20
30
. flat ( )
21
31
. map ( thing => thing . nodeId )
22
32
. filter (
@@ -32,12 +42,10 @@ function getHallwayConnections<
32
42
33
43
/** @ignore */
34
44
function getStairConnections < ForkName extends string , StairName extends string > (
35
- hallConnections : {
36
- nodeId : Node < ForkName , StairName > ;
37
- edgeLengthFromPreviousNodeInHallway : number ;
38
- } [ ] [ ]
45
+ hallConnections : HallConnectorsStructures < ForkName , StairName >
39
46
) : string [ ] [ ] {
40
47
const stairNodes = hallConnections
48
+ . map ( hallway => hallway . nodes )
41
49
. flat ( )
42
50
. map ( thing => thing . nodeId )
43
51
. filter ( ( st ) : st is StairNode < StairName > => st instanceof StairNode ) ;
@@ -59,30 +67,30 @@ function getStairConnections<ForkName extends string, StairName extends string>(
59
67
* @returns The graph to be used by getShortestPath
60
68
*/
61
69
export function getGraph < ForkName extends string , StairName extends string > (
62
- hallConnectorsStructures : {
63
- nodeId : Node < ForkName , StairName > ;
64
- edgeLengthFromPreviousNodeInHallway : number ;
65
- } [ ] [ ]
70
+ hallConnectorsStructures : HallConnectorsStructures < ForkName , StairName >
66
71
) {
67
- const hallConnectors = hallConnectorsStructures . map ( hall =>
68
- hall . map ( ( { nodeId, edgeLengthFromPreviousNodeInHallway } ) => ( {
69
- nodeId : serializeNode ( nodeId ) ,
70
- edgeLengthFromPreviousNodeInHallway,
71
- } ) )
72
- ) ;
72
+ const hallConnectors = hallConnectorsStructures . map ( hall => ( {
73
+ oneWay : hall . oneWay ,
74
+ nodes : hall . nodes . map (
75
+ ( { nodeId, edgeLengthFromPreviousNodeInHallway } ) => ( {
76
+ nodeId : serializeNode ( nodeId ) ,
77
+ edgeLengthFromPreviousNodeInHallway,
78
+ } )
79
+ ) ,
80
+ } ) ) ;
73
81
const stairConnections = getStairConnections ( hallConnectorsStructures ) ;
74
82
const hallwayConnections = getHallwayConnections ( hallConnectorsStructures ) ;
75
83
76
84
const graph : dijkstra . Graph = { } ;
77
- hallConnectors . forEach ( hall => {
85
+ hallConnectors . forEach ( ( { oneWay , nodes : hall } ) => {
78
86
hall . forEach ( ( node , ind ) => {
79
87
const id = node . nodeId ;
80
88
const edgesTo : { [ key : string ] : number } = { } ;
81
- if ( ind != 0 ) {
89
+ if ( ind !== 0 && oneWay !== "forward" ) {
82
90
edgesTo [ hall [ ind - 1 ] . nodeId ] =
83
91
hall [ ind ] . edgeLengthFromPreviousNodeInHallway ;
84
92
}
85
- if ( ind != hall . length - 1 ) {
93
+ if ( ind !== hall . length - 1 && oneWay !== "backward" ) {
86
94
edgesTo [ hall [ ind + 1 ] . nodeId ] =
87
95
hall [ ind + 1 ] . edgeLengthFromPreviousNodeInHallway ;
88
96
}
0 commit comments