mapgenplugin
A level made from a recipe and a seed: its tiles and its actors.
Putting it in a scene
mapgen has no row, no settings and nothing drawn. Whoever makes a level calls build with a recipe, a theme and any set pieces, and writes what comes back as a Tiled map (.tmj) and the scene's actors, which the scene then loads as it would a map drawn by hand.
import { build } from '#plugins/mapgen/mapgen.sim.ts'
const { map, actors, roles } = build(seeded(7), recipe, theme, '../tiles/crypt.tsj', { altar })
The recipe
{
"size": [48, 48],
"layer": "walls",
"ground": { "layer": "floor", "roles": ["floor", "floor-n", "floor-w", "floor-nw", "floor-c", "hazard"] },
"width": 1,
"tries": 20,
"algorithm": "rooms",
"rooms": { "leaf": 10, "room": [4, 7], "door": 0.5 },
"stairs": { "apart": 30, "up": { "is": "stairs", "dir": "up" }, "down": { "is": "stairs", "dir": "down" } },
"place": [
{ "is": "chest", "on": "floor", "count": [2, 4], "apart": 6, "away": 10, "blocks": true },
{ "is": "torch", "on": "wall", "count": 12, "apart": 4, "faces": ["s", "e"] },
{ "is": "gate", "on": "door" }
],
"pieces": [{ "name": "altar", "where": "floor" }]
}
| Key | Meaning |
|---|---|
size | [across, down] in cells, each 3 or more. A cell is [c, r], c along a row and r down the rows, as in tilemap. The outer ring of cells is never walkable. |
layer | The tile layer every cell's tile is on, which is the render2d layer it is drawn on. |
ground | Optional { layer, roles }: cells of the listed roles go on the tile layer layer, drawn flat and first; every other cell stays on the map's layer, which render2d sorts with the things. |
width | Cells: how wide every corridor, walkway and tunnel is, 1 or more. Doors form only where a corridor one cell wide meets a room. |
tries | How many maps are made before build gives up and throws. A map is thrown away when a room, a set piece, the stairs or a placement finds no place on it. |
algorithm | rooms, caves or halls, with its settings in a block of the same name (below). |
stairs | { apart, up, down }: up and down are actor entries. The up stairs go on a rolled floor cell, and the down stairs on one at least apart steps from it by path. |
place | The actors, placed in order (below). |
pieces | Set pieces, { name, where }, stamped in order. where is floor, the whole piece on floor, or solid, the piece and a cell all round it on cells nothing walks on. |
rooms
{ leaf, room: [least, most], door }. The map is split across its longer side at a rolled point, and each half again, while a side is at least 2 × leaf, so every leaf is leaf to 2 × leaf − 1 cells a side. leaf is at least the least room side + 2. Each leaf holds one room with sides rolled from room, and each split's two halves are joined by an L-shaped corridor between their nearest rooms. door is the chance, 0 to 1, that a corridor cell beside a room's side is a door.
caves
{ fill, steps, born, stays, keep }. Every inner cell starts as floor with chance fill. Then, steps times, a rock cell with at least born of its 8 neighbours floor turns floor, and a floor cell with fewer than stays turns rock; both are 0 to 8. A region of floor under keep cells is filled in, and the rest are joined.
halls
{ count: [least, most], room: [least, most], gap, door }. Rooms over a hazard, such as lava, water or a pit, that nothing walks on. Each room has a ring of wall and at least gap cells of hazard between its ring and every other's, and is put at a rolled place among all the places it fits. Each room after the first is joined to the nearest earlier room by a walkway, and door is the chance a walkway cell on a ring is a door.
place
An entry is an actor entry, is and any fields, which every actor it places carries, with these beside them saying where.
| Key | Meaning |
|---|---|
on | floor, a cell of ground; door, a door's arch, the actor carrying role door-ns or door-ew; or wall, rock with ground on exactly one side, the actor carrying face, the side the ground is on. |
count | How many: a number, or [least, most] rolled. Left out, one on every cell that takes one. |
apart | Cells: no two of this entry's actors nearer than this across or diagonally. |
away | Steps by path: no actor nearer the up stairs than this. A wall actor is measured from the ground it faces. |
blocks | True for something nothing walks through. It is only put where every free walkable cell can still reach every other, and every blocking actor keeps a free cell beside it. |
faces | For wall: the sides, n, e, s, w, the ground may be on. All four when left out. |
Settling
After carving and stamping, every rock cell whose wall the theme has no tile for is opened into floor, and every walkable region is joined to the largest by an L-shaped tunnel width wide, until one region is left. Every walkable cell is then reachable from every other moving across sides. A door is kept only between two walls with walkable cells on its other two sides, and only where the theme draws its arch; any other door becomes floor.
Roles
Each cell gets a role, the thing it draws on its upper sides and its top corner. With render2d's ground [[64, 32], [-64, 32]] a cell is a diamond: its n side (r − 1) is the upper right, e (c + 1) the lower right, s (r + 1) the lower left and w (c − 1) the upper left. A wall is drawn by the cell below or right of it, so it stands exactly on the edge where walking stops.
| Role | Meaning |
|---|---|
floor | Bare ground. |
floor-n, floor-w, floor-nw | Ground with a room's back wall on that upper side, or both. |
floor-c | Ground with a column at its top corner, where two back walls meet. Drawn as floor when the theme has no tile for it. |
door-ns, door-ew | An arch with ground on both sides: on the n side in a wall along x, or on the w side in a wall along y. |
door-ns-w, door-ew-n | An arch with a wall on the cell's other upper side. |
hazard | Lava, water or a pit: not walkable, and no wall stands against it. |
void | Rock with nothing on its upper sides. With no tile for it in the theme, the cell holds no tile. |
wall-n, wall-w, wall-nw | Rock with ground beyond that upper side, or both: a front wall. |
wall-c | Rock with a column at its top corner, a room's inner corner nearest the viewer. Drawn as void when the theme has no tile for it. |
The floor and door roles are walkable; every other role blocks.
The theme
A Tiled tileset (.tsj). Each tile's type is its role, and its probability, 1 when left out, is how often it is picked among that role's tiles. A tile of probability 0 is only placed by a set piece. A tile with no type is ignored. A walkable tile must have no collision and every other tile must have collision, so the map blocks where tilemap reads it blocking. The map's grid is the theme's grid, or its tile size, orthogonal, when it has none.
Set pieces
A set piece is a Tiled map (.tmj) of fixed size, its tile layers in CSV. A tile from a tileset embedded in it is a role by its type, and the cell becomes that ground and is tiled as any other. A tile from the theme itself stands as painted where the cell's role matches it. An empty cell leaves the map as it was, and a flipped tile throws. Each object of an object layer is an actor: its type is its is, its custom properties its fields, and its pos its point or its rectangle's centre in cells from the piece's top left. Pieces are stamped before settling, so settling joins them to the rest.
Methods
From #plugins/mapgen/mapgen.sim.ts.
build( die, recipe, theme, source, pieces = {} ) : { map, actors, roles }
die hands out numbers in [0, 1), such as the engine's seeded(seed): the same die and inputs make the same map, tile for tile, and the same actors. source is the path the map names the theme by, from where the map will be written. pieces are the set pieces by the names the recipe uses.
| Field | Meaning |
|---|---|
map | A Tiled map of the recipe's size on the theme's grid, with the theme as its one tileset at first gid 1, and its tile layers as plain lists of gids, 0 for no tile, the ground layer first. |
actors | Each { is, pos: [x, y], …fields }, at its cell's centre, no two in one cell: the set pieces' first, then the up and down stairs, then the recipe's entries in order. |
roles | Each cell's role, roles[r][c]. |
autotile( ground ) : Role[][]
Every cell's role for a grid of grounds, ground[r][c], each 'rock', 'floor', 'door' or 'hazard'. A door not between two walls is floor.