# gizgame/planet

A planet's coast: the rings of its land as a cooked file, read once, and asked two things at any latitude and longitude: whether it is land, and how far the coast is. A coast is a line, so the file holds lines: a picture of the land gave a coast as coarse as its samples.

```gizlang
import * as Planet from "gizgame/planet";
```

## Types

### `Coast`

```gizlang
class Coast { private lon: Vec<f64> = []; private lat: Vec<f64> = []; private next: Vec<i64> = []; private first: Vec<i64> = []; private held: Vec<i64> = []; private dry: Vec<bool> = []; private tiles: Map<i64, Tile> = new Map<i64, Tile>(); private drawn: Vec<i64> = []; private serial: i64 = 0; constructor(data: bytes) throws string { if (data.length <4) { throw "A coast file has no rings"; } const rings = i64(data.readU32LE(0)); let at = 4; for (const ring of range(0, rings)) { if (at+4> data.length) { throw "A coast file ends within a ring"; } const count = i64(data.readU32LE(at)); at+ = 4; const start = this.lon.length; if (at+count*4> data.length) { throw "A coast file ends within a ring"; } for (const n of range(0, count)) { this.lon.push(f64(i64(data.readU16LE(at)))/STEPS*360.0-180.0); this.lat.push(f64(i64(data.readU16LE(at+2)))/STEPS*180.0-90.0); this.next.push(n+1 <count ? start+n+1: start); at+ = 4; } } const edges = this.lon.length; this.drawn = new Array(edges).fill(-1); let counts: Vec<i64> = new Array(360*180+1).fill(0); for (const pass of range(0, 2)) { for (const e of range(0, edges)) { const to = this.next[e]; const c0 = column(Math.min(this.lon[e], this.lon[to])); const c1 = column(Math.max(this.lon[e], this.lon[to])); const r0 = row(Math.min(this.lat[e], this.lat[to])); const r1 = row(Math.max(this.lat[e], this.lat[to])); for (const r of range(r0, r1+1)) { for (const c of range(c0, c1+1)) { const cell = r*360+c; if (pass === 0) { counts[cell+1]+ = 1; } else { this.held[counts[cell]] = e; counts[cell]+ = 1; } } } } if (pass === 0) { for (const cell of range(0, 360*180)) { counts[cell+1]+ = counts[cell]; } this.first = [...counts]; this.held = new Array(counts[360*180]).fill(0); } } let turns: Vec<bool> = new Array(360*180).fill(false); for (const e of range(0, edges)) { const to = this.next[e]; const y0 = this.lat[e]; const y1 = this.lat[to]; if (y0 === y1) { continue; } for (const r of range(row(Math.min(y0, y1)-0.5), row(Math.max(y0, y1))+1)) { const y = f64(r)+0.5-90.0; if ((y0 <= y) === (y1 <= y)) { continue; } const x = this.lon[e]+(y-y0)/(y1-y0)*(this.lon[to]-this.lon[e]); const c = Math.maxI64(0, i64(Math.floor(x+179.5))+1); if (c <360) { turns[r*360+c] = !turns[r*360+c]; } } } for (const r of range(0, 180)) { let land = false; for (const c of range(0, 360)) { if (turns[r*360+c]) { land = !land; } this.dry.push(land); } } } points(): i64 { return this.lon.length; } land(lat: f64, lon: f64): bool { const x = wrapped(lon); const y = Math.max(-90.0, Math.min(90.0, lat)); const cell = row(y)*360+column(x); const cx = f64(column(x))+0.5-180.0; const cy = f64(row(y))+0.5-90.0; let land = this.dry[cell]; for (const at of range(this.first[cell], this.first[cell+1])) { const e = this.held[at]; const to = this.next[e]; const ax = this.lon[e]; const ay = this.lat[e]; const bx = this.lon[to]; const by = this.lat[to]; const d1 = (bx-ax)*(cy-ay)-(by-ay)*(cx-ax); const d2 = (bx-ax)*(y-ay)-(by-ay)*(x-ax); if ((d1> 0) === (d2> 0)) { continue; } const d3 = (x-cx)*(ay-cy)-(y-cy)*(ax-cx); const d4 = (x-cx)*(by-cy)-(y-cy)*(bx-cx); if ((d3> 0) !== (d4> 0)) { land = !land; } } return land; } distance(lat: f64, lon: f64, grain: f64): f64 { let level = 0; while (level <COARSEST && f64(1 <<(level+1))/f64(SIDE) <= grain) { level+ = 1; } const size = f64(1 <<level); const x = wrapped(lon)+180.0; const y = Math.max(0.0, Math.min(180.0-1e-9, lat+90.0)); const tx = i64(Math.floor(x/size)); const ty = i64(Math.floor(y/size)); const key = (level*1024+ty)*1024+tx; let tile = this.tiles.get(key); if (tile === undefined) { if (this.tiles.size> = KEPT) { this.tiles = new Map<i64, Tile>(); } tile = this.tile(f64(tx)*size-180.0, f64(ty)*size-90.0, size); this.tiles.set(key, tile!); } if (tile!.samples.length === 0) { return tile!.value; } const u = (x-f64(tx)*size)/size*f64(SIDE); const v = (y-f64(ty)*size)/size*f64(SIDE); const i = Math.minI64(SIDE-1, i64(Math.floor(u))); const j = Math.minI64(SIDE-1, i64(Math.floor(v))); const fu = u-f64(i); const fv = v-f64(j); const s = tile!.samples; const w = SIDE+1; const a = s[j*w+i]+(s[j*w+i+1]-s[j*w+i])*fu; const b = s[(j+1)*w+i]+(s[(j+1)*w+i+1]-s[(j+1)*w+i])*fu; return a+(b-a)*fv; } private tile(west: f64, south: f64, size: f64): Tile { const pitch = size/f64(SIDE*FINE); const w = SIDE*FINE+REACH*2+1; const left = west-f64(REACH)*pitch; const low = south-f64(REACH)*pitch; const far = f64(REACH)*pitch; this.serial+ = 1; let edges: Vec<i64> = []; let turnsOf: Vec<f64> = []; const c0 = i64(Math.floor(left+180.0)); const c1 = i64(Math.floor(left+f64(w-1)*pitch+180.0)); const r0 = row(low); const r1 = row(low+f64(w-1)*pitch); for (const c of range(c0, c1+1)) { const real = ((c % 360)+360) % 360; const turn = f64(c-real); for (const r of range(r0, r1+1)) { for (const at of range(this.first[r*360+real], this.first[r*360+real+1])) { const e = this.held[at]; if (this.drawn[e] === this.serial) { continue; } this.drawn[e] = this.serial; edges.push(e); turnsOf.push(turn); } } } const start = this.land(low, left); if (edges.length === 0) { return { value: start ? far: -far, samples: [] }; } let turns: Vec<bool> = new Array(w*w).fill(false); for (const [n, e] of edges.entries()) { const to = this.next[e]; const y0 = this.lat[e]; const y1 = this.lat[to]; if (y0 === y1) { continue; } const x0 = this.lon[e]+turnsOf[n]; const x1 = this.lon[to]+turnsOf[n]; const from = Math.maxI64(0, i64(Math.floor((Math.min(y0, y1)-low)/pitch))); const until = Math.minI64(w-1, i64(Math.floor((Math.max(y0, y1)-low)/pitch))+1); for (const r of range(from, until+1)) { const y = low+f64(r)*pitch; if ((y0 <= y) === (y1 <= y)) { continue; } const x = x0+(y-y0)/(y1-y0)*(x1-x0); if (x <= left) { continue; } const c = i64(Math.floor((x-left)/pitch))+1; if (c <w) { turns[r*w+c] = !turns[r*w+c]; } } } let dry: Vec<bool> = []; let wet: Vec<bool> = []; for (const r of range(0, w)) { let land = this.land(low+f64(r)*pitch, left); for (const c of range(0, w)) { if (turns[r*w+c]) { land = !land; } dry.push(land); wet.push(!land); } } const toSea = spread(wet, w, w); const toLand = spread(dry, w, w); let samples: Vec<f64> = []; for (const j of range(0, SIDE+1)) { for (const i of range(0, SIDE+1)) { const at = (REACH+j*FINE)*w+REACH+i*FINE; samples.push(dry[at] ? Math.min(far, (Math.sqrt(toSea[at])-0.5)*pitch): -Math.min(far, (Math.sqrt(toLand[at])-0.5)*pitch)); } } return { value: 0.0, samples }; } }
```

A coast read from a coast file (encodeCoast).

- `lon`: `Vec<f64>`
- `lat`: `Vec<f64>`
- `next`: `Vec<i64>`
- `first`: `Vec<i64>`
- `held`: `Vec<i64>`
- `dry`: `Vec<bool>`
- `tiles`: `_gizlang_sdk_collections.Map<i64, M233.Tile>`
- `drawn`: `Vec<i64>`
- `serial`: `i64`

### `Planet`

```gizlang
type Planet = { height: (lat: f64, lon: f64, grain: f64) => f64; }
```

A planet's ground: its height in real meters at a latitude and a longitude, the sea at 0, as fine as a GRAIN in
degrees asks. A coast is where it crosses 0. Nothing that reads a planet knows what made it.

- `height`: `(arg0: f64, arg1: f64, arg2: f64) => f64`

### `Projection`

```gizlang
type Projection = { lat: f64; lon: f64; scale: f64; }
```

Where a world lies on its planet: the latitude and longitude of the world's origin, and the meters of the world for
a degree (x to the east, z to the south).

- `lat`: `f64`
- `lon`: `f64`
- `scale`: `f64`

### `Harbor`

```gizlang
type Harbor = { at: Point2; sea: f64; out: Point2; }
```

A harbor: the place AT of its shore, where its town's quay meets the water, and the heading SEA from it to open
water (the unit x and z of that way are in OUT). Its town lies behind the shore and its berth before it.

- `at`: `gizlang_geometry.Point2`
- `sea`: `f64`
- `out`: `gizlang_geometry.Point2`

### `Ground`

```gizlang
type Ground = { height: (x: f64, z: f64) => f64; seen: (x: f64, z: f64, meters: f64) => f64; }
```

The ground of a world on a planet: its one HEIGHT at a place of the world, which the scene, the solid ground and
the laws of the sea read and nothing else (Game.terrain), and the same height SEEN from afar, as fine as a cell of
METERS asks: what a chart of an ocean reads.

- `height`: `(arg0: f64, arg1: f64) => f64`
- `seen`: `(arg0: f64, arg1: f64, arg2: f64) => f64`

## Functions

### `encodeCoast`

```gizlang
function encodeCoast(rings: Vec<Vec<f64>>): bytes
```

The rings RINGS of a land (each a list of longitude, latitude pairs in degrees, closed by its first point) as a coast
file: the count of rings, then each ring's count of points and its points as two u16 (longitude, latitude).

Effects: fail, alloc.

### `constructor`

```gizlang
constructor(data: bytes) throws string
```

The coast of the file DATA.

Effects: fail, read, write, alloc.

### `coastal`

```gizlang
function coastal(coast: Coast, seed: i64 = 1, hills: f64 = 1500.0): Planet
```

The planet of the coast COAST: a shelf under the sea and a plain ashore by the distance from the coast, and hills
of the seed SEED inland, up to HILLS meters over the plain.

Effects: fail, alloc.

### `continents`

```gizlang
function continents(): Procgen.Shape
```

The shape of a planet of continents: a continent some 50 degrees wide, its land rising to 6,000 m at the most, coasts
warped by 12 degrees, and the sea over the lower 0.53 of the noise (some six tenths of the surface).

### `generated`

```gizlang
function generated(seed: i64, shape: Procgen.Shape = continents()): Planet
```

The planet of the seed SEED and the SHAPE: fractal noise on the sphere, so no seam at any longitude and no pinch at
a pole. The shape's scale and warp are degrees, its height the meters of its highest land, its sea the share of the
noise under the water. Its octaves stop at the grain. The same seed is the same planet on every machine.

Effects: fail, alloc.

### `placed`

```gizlang
function placed(p: Projection, lat: f64, lon: f64): Point2
```

The place in the world of LAT, LON, by the shorter way round.

### `degrees`

```gizlang
function degrees(p: Projection, x: f64, z: f64): Point2
```

The latitude (x of the result) and longitude (y) of the world's X, Z.

### `moored`

```gizlang
function moored(b: Harbor, u: f64, v: f64): Point2
```

The place in the world of U meters along the shore of the harbor B and V meters out to its sea (behind its quay
under 0): a town is laid out in its harbor's frame, whichever way its sea is.

### `risen`

```gizlang
function risen(meters: f64): f64
```

The game's height for a planet's real meters: a curve that keeps the coast at 0, so a mountain and a trench fit a
world a ship crosses in minutes.

Effects: fail.

### `harbor`

```gizlang
function harbor(planet: Planet, p: Projection, lat: f64, lon: f64, room: f64 = 14.0): Harbor
```

The harbor of a port at LAT, LON on PLANET: found, not carved. It is the shore of the land nearest the port, where
open water (water ROOM meters of the world from any land) is nearest, so a ship that leaves it sails at once.

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

### `ground`

```gizlang
function ground(planet: Planet, p: Projection, harbors: Vec<Harbor>): Ground
```

The ground of a world on PLANET by the projection P, with its HARBORS: each harbor's town is levelled behind its
shore and a channel is dredged from its quay to the open water.

Effects: fail, alloc.

### `tone`

```gizlang
function tone(height: f64): i64
```

The tone of the game's HEIGHT, an index of TONES.

### `pictured`

```gizlang
function pictured(planet: Planet, width: i64, height: i64): bytes
```

The picture of the whole of PLANET, WIDTH by HEIGHT pixels of RGBA: longitude -180 at its left, the north at its
top, in the chart's tones. A world map shows a planet with no picture of its own by it.

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

## Methods

### `Coast.points`

```gizlang
points(): i64
```

The count of the coast's points.

Effects: fail, read.

### `Coast.land`

```gizlang
land(lat: f64, lon: f64): bool
```

Whether LAT, LON is land: exact to the file's rings. From the middle of its cell, whose side is known, each edge
that the way to it crosses changes the side.

Effects: fail, read.

### `Coast.distance`

```gizlang
distance(lat: f64, lon: f64, grain: f64): f64
```

The distance in degrees from LAT, LON to the coast, as fine as GRAIN degrees asks: over 0 on the land, under 0
in the sea, and no farther than its tile reaches (a quarter of 64 grains, or more). A chart of an ocean and the
ground under a ship each read a few tiles.

Effects: fail, read, write, alloc.

## Constants

### `TONES`

```gizlang
const TONES: Vec<Vec<f64>> = [[0.05, 0.17, 0.33, 1], [0.13, 0.40, 0.62, 1], [0.84, 0.78, 0.56, 1], [0.33, 0.55, 0.29, 1], [0.55, 0.52, 0.44, 1]]
```

The tones of a chart's ground, from the deep sea to the high land: one palette for the chart in a corner and the
map of the whole planet.

