# gizgame/seaway

the ways by sea of a world on a planet's ground: what an autopilot sails from a place to a port.

```gizlang
import * as Seaway from "gizgame/seaway";
```

## Types

### `Seaways`

```gizlang
class Seaways { private ground: Ground; private cell: f64; private wide: i64; private high: i64; private top: i64; private west: i64; private wet = new Map<i64, Point2>(); private deep = new Map<i64, f64>(); private dry = new Set<i64>(); private ranks: Vec<f64> = []; private cells: Vec<i64> = []; private gone = new Map<i64, f64>(); private came = new Map<i64, i64>(); private done = new Set<i64>(); private start: i64 = -1; private goal: i64 = -1; private from: Point2 = point2(0.0, 0.0); private to: Point2 = point2(0.0, 0.0); searching: bool = false; way: Vec<Point2> | undefined = undefined; constructor(ground: Ground, p: Projection, cell: f64 = 24.0) { this.ground = ground; this.cell = cell; this.wide = i64(Math.round(360.0*p.scale/cell)); this.high = i64(Math.round(180.0*p.scale/cell)); this.top = i64(Math.floor((p.lat-90.0)*p.scale/cell)); this.west = i64(Math.floor(-180.0*p.scale/cell)); } private at(column: i64, row: i64): i64 { return row*this.wide+(column%this.wide+this.wide)%this.wide; } private spot(c: i64): Point2 | undefined { const known = this.wet.get(c); if (known !== undefined || this.dry.has(c)) { return known; } const cell = this.cell; const ox = f64(c%this.wide+this.west)*cell; const oz = f64(c/this.wide+this.top)*cell; let best: Point2 | undefined = undefined; let least = this.ground.height(ox+cell*0.5, oz+cell*0.5); if (least <-SEAWAY_DEPTH) { best = point2(ox+cell*0.5, oz+cell*0.5); } else if (least <1.5) { least = -SEAWAY_DEPTH; for (const k of range(0, 25)) { if (k === 12) { continue; } const x = ox+cell*(f64(k%5)+0.5)*0.2; const z = oz+cell*(f64(k/5)+0.5)*0.2; const h = this.ground.height(x, z); if (h <least) { least = h; best = point2(x, z); } } } if (best === undefined) { this.dry.add(c); } else { this.wet.set(c, best!); this.deep.set(c, least); } return best; } private open(a: i64, b: i64): bool { if (this.deep.get(a)! <-SEAWAY_OPEN && this.deep.get(b)! <-SEAWAY_OPEN) { return true; } const s = this.spot(a)!; let e = this.spot(b)!; const round = f64(this.wide)*this.cell; if (e.x-s.x> round*0.5) { e = point2(e.x-round, e.y); } else if (s.x-e.x> round*0.5) { e = point2(e.x+round, e.y); } const steps = i64(Math.hypot(e.x-s.x, e.y-s.y)/3.0)+1; for (const n of range(1, steps)) { const t = f64(n)/f64(steps); if (this.ground.height(s.x+(e.x-s.x)*t, s.y+(e.y-s.y)*t)> -SEAWAY_DEPTH) { return false; } } return true; } private near(place: Point2): i64 { const cx = i64(Math.floor(place.x/this.cell))-this.west; const cz = i64(Math.floor(place.y/this.cell))-this.top; if (cz> = 0 && cz <this.high) { const h = this.ground.height(place.x, place.y); const c = this.at(cx, cz); if (h <-SEAWAY_DEPTH) { this.dry.delete(c); this.wet.set(c, place); this.deep.set(c, h); return c; } } for (const ring of range(0, 9)) { for (const dz of range(-ring, ring+1)) { for (const dx of range(-ring, ring+1)) { if (Math.maxI64(Math.absI64(dx), Math.absI64(dz)) !== ring || cz+dz <0 || cz+dz> = this.high) { continue; } const c = this.at(cx+dx, cz+dz); if (this.spot(c) !== undefined) { return c; } } } } return -1; } private ahead(c: i64): f64 { const dx = Math.absI64(c%this.wide-this.goal%this.wide); return Math.hypot(f64(Math.minI64(dx, this.wide-dx)), f64(c/this.wide-this.goal/this.wide))*SEAWAY_LEAN; } private push(rank: f64, c: i64): void { this.ranks.push(rank); this.cells.push(c); let n = this.ranks.length-1; while (n> 0) { const up = (n-1)/2; if (this.ranks[up] <= this.ranks[n]) { break; } this.swap(up, n); n = up; } } private swap(a: i64, b: i64): void { const r = this.ranks[a]; this.ranks[a] = this.ranks[b]; this.ranks[b] = r; const k = this.cells[a]; this.cells[a] = this.cells[b]; this.cells[b] = k; } private pop(): i64 { const first = this.cells[0]; const last = this.ranks.length-1; this.ranks[0] = this.ranks[last]; this.cells[0] = this.cells[last]; this.ranks.pop(); this.cells.pop(); let n = 0; while (true) { let least = n; const a = n*2+1; const b = a+1; if (a <last && this.ranks[a] <this.ranks[least]) { least = a; } if (b <last && this.ranks[b] <this.ranks[least]) { least = b; } if (least === n) { break; } this.swap(least, n); n = least; } return first; } plot(from: Point2, to: Point2): void { this.ranks = []; this.cells = []; this.gone = new Map<i64, f64>(); this.came = new Map<i64, i64>(); this.done = new Set<i64>(); this.way = undefined; this.from = from; this.to = to; this.start = this.near(from); this.goal = this.near(to); this.searching = this.start> = 0 && this.goal> = 0; if (this.searching) { this.gone.set(this.start, 0.0); this.push(this.ahead(this.start), this.start); } } step(most: i64): bool { if (!this.searching) { return true; } let looked: i64 = 0; while (this.ranks.length> 0 && looked <most) { const c = this.pop(); if (this.done.has(c)) { continue; } this.done.add(c); looked + = 1; if (c === this.goal) { let way: Vec<Point2> = []; let at = c; while (at !== this.start) { way.push(this.spot(at)!); at = this.came.get(at)!; } way.push(this.spot(this.start)!); way.reverse(); this.way = way; this.searching = false; return true; } const cx = c%this.wide; const cz = c/this.wide; const far = this.gone.get(c)!; for (const dz of range(-1, 2)) { for (const dx of range(-1, 2)) { if ((dx === 0 && dz === 0) || cz+dz <0 || cz+dz> = this.high) { continue; } const next = this.at(cx+dx, cz+dz); if (this.done.has(next) || this.spot(next) === undefined || !this.open(c, next)) { continue; } const reach = far+(dx !== 0 && dz !== 0 ? 1.41421356: 1.0); const had = this.gone.get(next); if (had === undefined || reach <had!) { this.gone.set(next, reach); this.came.set(next, c); this.push(reach+this.ahead(next), next); } } } } if (this.ranks.length === 0) { this.searching = false; } return !this.searching; } }
```

The ways by sea of a world on GROUND by the projection P: `plot` starts the search of a way and `step` goes on with
it, some cells a call, so a long way never holds a frame. A way is a place in each cell of CELL meters it crosses,
each in water a hull sails and joined to the next by such water, round the planet either way. What a search learns
of the sea stays for the next.

- `ground`: `M245.Ground`
- `cell`: `f64`
- `wide`: `i64`
- `high`: `i64`
- `top`: `i64`
- `west`: `i64`
- `wet`: `_gizlang_sdk_collections.Map<i64, gizlang_geometry.Point2>`
- `deep`: `_gizlang_sdk_collections.Map<i64, f64>`
- `dry`: `_gizlang_sdk_collections.Set<i64>`
- `ranks`: `Vec<f64>`
- `cells`: `Vec<i64>`
- `gone`: `_gizlang_sdk_collections.Map<i64, f64>`
- `came`: `_gizlang_sdk_collections.Map<i64, i64>`
- `done`: `_gizlang_sdk_collections.Set<i64>`
- `start`: `i64`
- `goal`: `i64`
- `from`: `gizlang_geometry.Point2`
- `to`: `gizlang_geometry.Point2`
- `searching`: `bool` — Whether a search goes on (step).
- `way`: `_gizlang_sdk_core.Option<Vec<gizlang_geometry.Point2>>` — The way the last search found, from its start to its end; none while it goes on or when no water joins them.

## Functions

### `constructor`

```gizlang
constructor(ground: Ground, p: Projection, cell: f64 = 24.0)
```

The ways of GROUND by P, in cells of CELL meters.

Effects: fail, write, alloc.

## Methods

### `Seaways.plot`

```gizlang
plot(from: Point2, to: Point2): void
```

Starts the search of the way from FROM to TO, places of the world; `step` goes on with it.

Effects: fail, read, write, console, io, alloc.

### `Seaways.step`

```gizlang
step(most: i64): bool
```

Goes on with the search for at most MOST cells; true when it ended (`way` is found, or none).

Effects: fail, read, write, console, io, alloc.

