Spawn Algorithm

From Minetest Developer Wiki
Revision as of 11:25, 6 June 2022 by Wuzzy (talk | contribs) (Created page with "The '''spawn algorithm''' tries to find a suitable spawn or respawn position for players. It is run whenever a player spawns or respawns. This page describes how Minetest’s...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

The spawn algorithm tries to find a suitable spawn or respawn position for players. It is run whenever a player spawns or respawns.

This page describes how Minetest’s builtin spawn algorithm works, as of version 5.5.0. Note that individual mods and games may choose to override. The setting static_spawn_point can also override this.

Overview

Basically, Minetest checks random position in a square centered on (0,0). Then it tries to find a suitable Y height to spawn the player in. If it did, that position is the spawn position. Otherwise, it increases the square size and tries again (with a new random position in that square).

The first square has a size of 3×3. If the first attempt failed, the search radius increases by 1, now it picks a random position in a 5×5 square (still centered on (0,0)). This is repeated until a valid spawn position is found. If the search square becomes too big or out of mapgen limits, the spawn search aborts and (0,0,0) will be used as the spawn position as a desperate fallback.

The algorithm makes up to 4001 attempts before giving up.

The Spawn Algorithm

Follow the following steps to get a spawn position.

  1. Set number range to 1
  2. If range >= 4000, or range > mapgen limit, terminate and return (0,0,0) as the spawn position. Otherwise, continue.
  3. Pick a random 2D position in the range from the XZ coordinates (-range, -range) to (+range, +range) (save it into pos)
  4. Get spawn level of this 2D position to get the Y coordinate (save into pos2)
  5. If spawn level not found: Increase range by 1 and go to step 2. Otherwise, continue.
  6. If the node at pos2 and the node directly above it are either Air or Ignore: Terminate algorithm and return pos2 as the spawn position
  7. Increase range by 1 and go to step 2

Note: This is a slight simplification of the actual code. The actual code for this is the function Server::findSpawnPos in server.cpp.

Spawn Level

The spawn level is the Y coordinate of a XZ position at which players will spawn. The particular algorithm/formula for this depends on the mapgen. The general goal here is to spawn the player

A rough overview of spawn levels:

Mapgen Spawn level Failure condition
v6 Base terrain height If below or at water level, or 16 nodes above water level
fractal Lowest 3 consecutive air nodes, starting from Y=0, or water level if the 'terrain' mapgen flag is set If no suitable air nodes were found for 4096 consecutive nodes
singlenode Always 0 None.
v5, v7, valleys, carpathian It's complicated ... It's complicated ...