mapfile

package
v0.25.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 15 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

A level in files

A level is a folder: a manifest, the ground's heights as an image and what lies on the ground as GeoJSON. It is the map a person or a tool writes; what a game derives from it (a ground's points, a board's cells) is worked out as it loads and never stored in it.

level/
  level.yaml          the manifest
  heights.png         the ground's heights
  features.geojson    what lies on the ground

mapfile.Load(fsys, "level/level.yaml") reads one; the files it names lie beside the manifest. The package lays nothing: a board takes its geography from the File, a square or hex one by its level's geography: (board.Lay, the board's doc.go), the mosaic by mosaic.Load. A level that works is testdata/level here and examples/look/ground/level.

Units and axes

A metre is a unit, everywhere. A place in the features is metres east and north of the level's south-west corner, as a map is read. The world's y runs down, so a reader turns a feature's y into size.y - y. The image's first row is the level's north edge: it needs no turning.

The manifest

size: [1536, 1536]      # metres east to west and north to south
seed: 7                 # what the ground's points are scattered from
heights:
  file: heights.png
  pixel: 2              # metres between two pixels
  lowest: -60           # the height of the value 0
  step: 0.002           # metres a step of the value
features: features.geojson
corner: 8               # the radius the kerbs round a junction's corners at; 8 unset
roads:                  # how a road of each highway lies across
  residential:
    lanes: 2
    lane: 3.5           # a lane's width
    walk: 2.5           # a pavement's, either side
    kerb: 0.15          # how high the pavement stands
    verge: 1.5          # the verge's past it

size is required. heights and features are each optional: a level without the first is flat at zero, without the second bare.

Any other key at the top is a part of a reader's own, read by that reader alone and skipped by the rest. The mosaic's:

mosaic:
  apart: 2              # how far apart the ground's points lie at their finest; 2 unset
  rungs: 6              # over how many rungs they thin out where the heights let them; 6 unset

Heights

A PNG of 16-bit grey. A pixel is a corner, not a square: the pixel (i, j) is the height at i × pixel metres east and j × pixel metres south of the north-west corner, so an image covering size has size / pixel + 1 pixels a side. Its height is lowest + value × step; between the pixels the ground is even (bilinear).

Refused: an image of another depth or colour, pixel or step unset, an image not reaching size.

Features

A GeoJSON FeatureCollection. A feature's properties are named as OpenStreetMap's tags are, so a map drawn there, in QGIS or in JOSM is a level's features with its places turned to metres. Every feature is read as it is, a Point, a LineString or a Polygon (its first ring the outline, the rest holes) with its id and its properties as tags; another geometry is refused. What a feature becomes is its reader's:

Feature Geometry Reader What it becomes
highway=<kind> LineString the mosaic a road's middle line, laid across as roads.<kind> says
highway=<kind> LineString a square or hex board a road of the kind the level's table names, over the cells the line's points lie in
waterway=<kind> LineString a square or hex board a river, its last point where the water goes
any tag the level's table names Polygon a square or hex board the kind of the cells whose middles it holds

The mosaic refuses a highway the manifest names no road for, never skips it: a road missing from the picture is harder to find than a message. Every road it lays is of one section yet.

Where roads meet

Junctions are never written. Lines that share a point (to a centimetre) meet there:

  • a line passing through a point another line has is cut there;
  • where exactly two ends of one highway meet, they are joined into one road, so a road may be drawn in as many lines as is handy;
  • where three or more meet, there is a meeting: a junction, its corners rounded at corner.

A point twice in a row is one point. The mosaic lays a junction only where its roads leave no more than a straight angle open between two neighbours: a kerb cannot be led round a corner wider than that yet, and the level is refused with the junction's place.

A level in tiles

A level of an island's size is cut into tiles by mapfile.Cut(fsys, "level/level.yaml", out, mapfile.Cutting{...}) (mosaic.Cut with the mosaic's defaults). The folder above stays what a person edits; the tiles are cut anew from it and never edited by hand. Load reads either.

out/
  level.yaml                      the manifest, the files it named taken out, its tiles said
  0/<column>_<row>/heights.png    the level as it is
  0/<column>_<row>/features.geojson
  3/<column>_<row>/...            the overview
tiles: {side: 1024, margin: 32, tiers: [0, 3]}
  • A tile's side is a power of two metres and a whole number of the heights' pixels; a game picks a multiple of its world's sector side. Tiles count from the north-west corner, as an image's rows do; one at the level's edge is as small as the level leaves it.
  • Two tiers. 0 is the level as it is. 3, the overview, has tiles eight times the side, heights every eighth pixel (taken, not averaged) and only what the cutter is told is drawn from far off (Cutting.Overview; the mosaic's, unset: the roads and whatever is 64 m or more across). A line is never simplified, in either.
  • A tile with no height over zero and nothing on it is not written, and a tile absent is the sea: level zero, nothing on it.
  • A tile's heights reach one pixel into the next tile, the edge both share, and every tile is of the manifest's one lowest and step: no seam shows.
  • A point, a line and an area no wider than a tile lie whole in every tile whose box grown by the margin they touch, and are read once, by their id. Give the features ids: one without is named by its place in the file, which the next cut may change.
  • An area wider than a tile is cut to each tile and its margin, the piece tagged clipped=true and keeping the area's id.
  • Places stay the level's, metres east and north of its south-west corner, in every tile.

Not there yet (doc/roadmap.md): the mosaic builds its whole ground as the game starts, so a level in tiles is read whole; the ground built tile by tile as the camera moves, and what is derived from a tile kept beside it, come after.

Documentation

Overview

Package mapfile reads a map kept in files, in metres: a manifest, the ground's heights as an image and what lies on the ground as GeoJSON, whole or cut into tiles. It is the geography a board lays itself by, whichever its grid; the format is README.md's.

File

Load reads a File: its Heights, its [Feature]s with their places turned to the world's, y down, the parts of the manifest that are a reader's own (File.Part) and its lines tagged highway as [Road]s from Meeting to meeting (File.Network).

Tiles

Cut writes a map as tiles of two tiers, the map as it is and its Overview, which Load reads alike: heights as a place is asked for, features by File.Within.

Index

Constants

View Source
const DefaultCorner = 8

DefaultCorner is the radius of a junction's corners where the manifest sets none.

View Source
const Overview = 3

Overview is the tier of the tiles that is drawn from far off: tiles eight times the side, heights every eighth pixel.

Variables

This section is empty.

Functions

func Cut

func Cut(fsys fs.FS, name, out string, c Cutting) error

Cut writes the level whose manifest is name in fsys, one in one piece, into the folder out as tiles of two tiers: the level as it is and its overview.

Types

type Cutting

type Cutting struct {
	// Tile is a tile's side in metres, a power of two; Margin how far past a tile a feature
	// may lie and still be the tile's.
	Tile   float32
	Margin float32
	// Overview says whether a feature of these tags, extent metres across, is drawn from far
	// off: it lies in the overview tier too.
	Overview func(tags map[string]string, extent float32) bool
}

Cutting is how a level is cut into tiles.

type Feature

type Feature struct {
	// ID is the feature's own name in its file, empty where it has none.
	ID    string
	Tags  map[string]string
	Shape Shape
	// Path is the place, the line's points or the area's outline, Holes the outlines cut out
	// of an area.
	Path  []geom.Vec
	Holes [][]geom.Vec
}

Feature is a thing lying on the ground: what it is by its tags, named as OpenStreetMap's, and where, in the world's places.

type File

type File struct {
	// Width and Height are the level's size, metres.
	Width  float32
	Height float32
	// Seed is what a reader scatters from where it must.
	Seed uint64
	// Heights is the ground's heights of a level in one piece; nil, a level in tiles or a
	// ground level at zero.
	Heights *Heights
	// Features is what lies on a level in one piece; a level in tiles gives its own by Within.
	Features []Feature
	// Sections is how a road of each highway lies across, Corner the radius the kerbs round
	// a junction's corners at.
	Sections map[string]Section
	Corner   float32
	// contains filtered or unexported fields
}

File is a level read: its ground and what lies on it, whole or in tiles read as asked for.

func Load

func Load(fsys fs.FS, name string) (*File, error)

Load is the level whose manifest is name in fsys, the files it names beside it; of a level in tiles nothing but the manifest is read yet.

func (*File) Grounded

func (l *File) Grounded() bool

Grounded is whether the level has heights of its own.

func (*File) HeightAt

func (l *File) HeightAt(x, y float32) float32

HeightAt is the ground's height at a place of the world.

func (*File) HeightAtTier

func (l *File) HeightAtTier(x, y float32, tier int) float32

HeightAtTier is the ground's height at a place as a tier of the tiles has it; a level in one piece has one ground.

func (*File) Network

func (l *File) Network() ([]Road, []Meeting)

Network is the level's lines tagged highway as roads and the meetings they end at: a line cut where another shares a point within it, two of one highway joined where they alone end.

func (*File) Part

func (l *File) Part(name string, into any) (bool, error)

Part decodes the manifest's part of a name, a reader's own, into what into points at, and says whether the manifest has one.

func (*File) Whole

func (l *File) Whole() error

Whole reads all of a level in tiles at its finest, so nothing is left to fail later.

func (*File) Within

func (l *File) Within(box geom.AABB, tier int) ([]Feature, error)

Within is what lies on the level at a tier of the tiles where it may touch a box: all of a level in one piece, the features of the tiles the box touches of one in tiles, each whole one once.

type Heights

type Heights struct {
	Columns int
	Rows    int
	Pixel   float32
	// Lowest is the height of the value 0, Step the metres a step of the value.
	Lowest float32
	Step   float32
	// contains filtered or unexported fields
}

Heights is a ground's heights a corner a pixel: Columns by Rows of them, Pixel metres apart, the first row the north edge.

func (*Heights) At

func (h *Heights) At(x, y float32) float32

At is the height at a place of the world: a pixel's at a pixel, even between them, the edge's past it.

func (*Heights) Value

func (h *Heights) Value(i, j int) uint16

Value is the value kept at a pixel.

type Meeting

type Meeting struct {
	At   geom.Vec
	Ways []geom.Vec
}

Meeting is where three or more roads end: the ways they leave it, in the order that turns left as the world lies.

type Road

type Road struct {
	Highway string
	Path    []geom.Vec
}

Road is a road's middle line from one meeting or end to the next, of one highway.

type Section

type Section struct {
	Lanes int     `yaml:"lanes"`
	Lane  float32 `yaml:"lane"`
	Walk  float32 `yaml:"walk"`
	Kerb  float32 `yaml:"kerb"`
	Verge float32 `yaml:"verge"`
}

Section is a road across: its lanes, and a pavement and a verge either side.

type Shape

type Shape uint8

Shape is what a feature's places make.

const (
	Point Shape = iota
	Line
	Area
)

The shapes: a place, a line through places, an area within an outline.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL