Levels
Based on game build 714014 | Last updated: 2026-02-27
Overview
The levels.lua module serves as the central registry and management system for all world configuration types in the game: base Level definitions, SettingsPreset definitions, and WorldGen level definitions. It supports both built-in and mod-added content, handles deduplication of IDs across sources, and enables merging of location-level defaults with specific overrides. This module is used by the frontend (level selection UI), world generation system, and custom preset managers.
Key responsibilities:
- Register and query levels for different categories (
LEVELCATEGORY.LEVEL,LEVELCATEGORY.SETTINGS,LEVELCATEGORY.WORLDGEN,LEVELCATEGORY.COMBINED). - Support location-based filtering and frontend-hiding logic.
- Merge location-level data (from
map/locations.lua) with per-level overrides usingMergeMapsDeep. - Store and manage mod-added data separately and clear it when reloading mods.
This module does not define a component but exports a set of module-scoped functions as its public API.
Usage example
local Levels = require("map/levels")
-- Retrieve all level options for the forest world (frontend view)
local level_options = Levels.GetLevelList(LEVELTYPE.SURVIVAL, nil, true)
for _, opt in ipairs(level_options) do
print(opt.text, opt.data)
end
-- Get full data for a specific level ID
local level_data = Levels.GetDataForLevelID("forest")
if level_data then
print("Level name:", level_data.name)
print("Location:", level_data.location)
end
-- Get the default settings preset for lava arena
local default_settings = Levels.GetDefaultSettingsData(LEVELTYPE.LAVAARENA)
if default_settings then
print("Settings name:", default_settings.settings_name)
end
Dependencies & tags
Components used: None. This module is a pure data and logic registry, not an entity component.
Tags: None identified.
Properties
No instance-based properties are defined in this module. It uses global mutable tables (levellist, settingslist, worldgenlist, modlevellist, modsettingslist, modworldgenlist, locations, modlocations, playstyle_defs, playstyle_order) internal to the module.
Main functions
GetDataForLocation(location)
- Description: Returns a deep copy of the location-level default data for the given
locationstring, checking mod-added data first, then built-inlocations. - Parameters:
location: String specifying the location key (e.g.,"forest","caves").
- Returns: Table containing location defaults (e.g., overrides, name, desc) or
nilif not found. - Error states: Returns
nilif no data exists for the location.
ClearModData(mod)
- Description: Clears all mod-added data (levels, settings, worldgen, locations) for the given mod name. If
modisnil, clears all modded data. - Parameters:
mod: String mod identifier ornilto clear all modded data.
- Returns: None.
- Error states: No explicit error behavior; silently ignores missing keys.
GetLevelList(leveltype, location, frontend)
- Description: Returns a list of level options (with
textanddatafields) for the givenleveltypeand optionallocation. Filters out frontend-hidden levels iffrontendistrue. Includes modded and profile presets. - Parameters:
leveltype: EnumLEVELTYPE.*.location: Optional string for location filtering.frontend: Boolean; iftrue, excludes entries wherehideinfrontendistrue.
- Returns: Array of tables
{text=string, data=id}. - Error states: Returns empty array if no matches found.
GetDataForLevelID(id, nolocation)
- Description: Returns merged level data for the given
id(case-insensitive), combining location-level defaults and level-specific overrides. Ifnolocationistrue, returns only level-specific overrides. - Parameters:
id: String level identifier (case-insensitive).nolocation: Optional boolean; iftrue, skips merging location defaults.
- Returns: Full level data table or
nil. - Error states: Returns
nilif ID is not found. Asserts if a custom preset lacks alocation.
GetTypeForLevelID(id)
- Description: Returns the
LEVELTYPE.*for the givenid, orLEVELTYPE.UNKNOWNif not found or if it’s a custom preset. - Parameters:
id: String level identifier.
- Returns:
LEVELTYPE.*enum value.
GetNameForLevelID(level_id)
- Description: Returns the
namefield of the level data, ornil.
GetDescForLevelID(level_id)
- Description: Returns the
descfield of the level data, ornil.
GetLocationForLevelID(level_id)
- Description: Returns the
locationfield of the level data, ornil.
GetSettingsList(leveltype, location, frontend)
- Description: Returns settings preset options with
text,data, and optionalmoddedflag. Filters bylocation,frontendhiding, and includes custom presets. - Parameters:
leveltype: EnumLEVELTYPE.*.location: Optional string for filtering.frontend: Boolean to hide frontend-hidden entries.
- Returns: Array of tables
{text=string, data=id, [modded=bool]}.
MergeLocationSettings(level)
- Description: Merges location defaults into
level.overrides, keeping only overrides belonging to theLEVELCATEGORY.SETTINGScategory (based onmap/customize.lua). - Parameters:
level: Table with alocationandoverridesfield.
- Returns: New merged level table with
overridespopulated.
GetDataForSettingsID(id, nolocation)
- Description: Returns merged settings preset data for
id, usingMergeLocationSettings. Behavior mirrorsGetDataForLevelIDbut for settings.
GetTypeForSettingsID(id)
- Description: Returns
LEVELTYPE.*for settings ID, orLEVELTYPE.CUSTOMPRESETfor custom presets.
GetNameForSettingsID(level_id), GetDescForSettingsID(level_id), GetLocationForSettingsID(level_id)
- Description: Same semantics as their
*ForLevelIDcounterparts, but for settings presets.
GetWorldGenList(leveltype, location, frontend)
- Description: Returns world generation options; mirrors
GetSettingsListbut for worldgen levels. UsesCustomPresetManagerto fetch custom worldgen presets.
MergeLocationWorldGen(level)
- Description: Merges location defaults into
level, excluding settings-category overrides (to retain only worldgen-relevant overrides). - Returns: New
Levelobject (viaLevel()constructor) with merged overrides.
GetDataForWorldGenID(id, nolocation)
- Description: Returns merged worldgen data, or
nil.
GetTypeForWorldGenID(id)
- Description: Returns
LEVELTYPE.*orLEVELTYPE.CUSTOMPRESET.
GetNameForWorldGenID(level_id), GetDescForWorldGenID(level_id), GetLocationForWorldGenID(level_id)
- Description: Same semantics as
*ForLevelID.
GetCombinedList(leveltype, location, frontend)
- Description: Returns a deduplicated list of settings and worldgen entries, ensuring IDs don’t collide. Used when presenting combined world customization lists.
GetList(category, ...)
- Description: Generic dispatcher to
Get*Listfunctions based onLEVELCATEGORY.*.
GetDefaultData(category, ...), GetDataForID(category, ...), GetTypeForID(category, ...), GetNameForID(category, ...), GetDescForID(category, ...), GetLocationForID(category, ...)
- Description: Generic dispatchers for corresponding category-specific functions.
GetPlaystyles(), GetPlaystyleDef(id)
- Description: Returns ordered list of playstyle IDs (
playstyle_order) and full definition for a givenid.
CalcPlaystyleForSettings(settings_overrides)
- Description: Computes the best-matching playstyle for the given
settings_overridesby scoring each playstyle’s required overrides. Returns the ID of the highest-scoring playstyle (scores: default=0.5, match=1, mismatch=0).
Events & listeners
This module defines no components, so it does not register or emit events.