MCPcopy Create free account
hub / github.com/Effect-TS/effect / sequenceFromPath

Function sequenceFromPath

packages/effect/test/Pathfinding.test.ts:156–199  ·  view source on GitHub ↗
(
  terrain: Terrain,
  path: Option.Option<Graph.PathResult<number>>
)

Source from the content-addressed store, hash-verified

154 * Derive a sequence of moves from a path.
155 */
156const sequenceFromPath = (
157 terrain: Terrain,
158 path: Option.Option<Graph.PathResult<number>>
159): Sequence | undefined => {
160 if (Option.isNone(path) || path.value.path.length === 0) {
161 return undefined
162 }
163
164 const pathValue = path.value
165
166 const sequence = []
167 const start = terrain.coordinates.get(pathValue.path[0])!
168 let previous = Graph.getNode(terrain.graph, terrain.nodes.get(start)!).pipe(
169 Option.getOrThrowWith(() => new Error(`Start location ${start} not found in terrain`))
170 )
171
172 for (const index of pathValue.path.slice(1)) {
173 const current = Graph.getNode(terrain.graph, index).pipe(
174 Option.getOrThrowWith(() => new Error(`Location ${index} not found in terrain`))
175 )
176
177 if (current.x === previous.x) {
178 if (current.y === previous.y - 1) {
179 sequence.push("↑")
180 } else if (current.y === previous.y + 1) {
181 sequence.push("↓")
182 } else {
183 throw new Error(`Invalid move ${current.x}:${current.y} -> ${previous.x}:${previous.y}`)
184 }
185 } else if (current.y === previous.y) {
186 if (current.x === previous.x - 1) {
187 sequence.push("←")
188 } else if (current.x === previous.x + 1) {
189 sequence.push("→")
190 } else {
191 throw new Error(`Invalid move ${current.x}:${current.y} -> ${previous.x}:${previous.y}`)
192 }
193 }
194
195 previous = current
196 }
197
198 return `${start};${sequence.join("")}`
199}
200
201// =============================================================================
202// Terrain Parser

Callers 1

assertPathFunction · 0.85

Calls 4

pushMethod · 0.80
joinMethod · 0.80
getMethod · 0.65
pipeMethod · 0.65

Tested by

no test coverage detected