- v50 information can now be added to pages in the main namespace. v0.47 information can still be found in the DF2014 namespace. See here for more details on the new versioning policy.
- Use this page to report any issues related to the migration.
Difference between revisions of "Lua functions"
(→Generators: Materials and entities) |
|||
Line 195: | Line 195: | ||
|} | |} | ||
− | == | + | ==Generation Tables== |
The ``generate()`` function is defined in ``init/generators.lua``, and is called to generate objects. | The ``generate()`` function is defined in ``init/generators.lua``, and is called to generate objects. | ||
Each type of random object is defined through a function that generates a table of raws. To generate objects for specific purposes, ``generate()`` can call functions from a table and provide inputs such as ``tok`` (unique token string). | Each type of random object is defined through a function that generates a table of raws. To generate objects for specific purposes, ``generate()`` can call functions from a table and provide inputs such as ``tok`` (unique token string). | ||
For example, [[forgotten beast]]s are unique and generated for each cave region. If multiple functions are defined in ``creatures.fb``, such as both ``creatures.fb.default`` and a modded forgotten beast, the game uses the ``weight`` output to influence which one will be randomly chosen. | For example, [[forgotten beast]]s are unique and generated for each cave region. If multiple functions are defined in ``creatures.fb``, such as both ``creatures.fb.default`` and a modded forgotten beast, the game uses the ``weight`` output to influence which one will be randomly chosen. | ||
+ | |||
+ | By default, objects are generated in this order: unittests{{Tooltip|*|if debug_level>=0}}, preprocess, do_once, materials, items, languages, creatures, interactions, entities, and postprocess. Do note that you can register raws at any step. For example, each [[vault]] entity generates a set of [[divine equipment]] during the entity step, and [[werebeast]]s generate a curse interaction during the creature step. | ||
===Custom=== | ===Custom=== | ||
− | All functions in these tables are run each tick, but global variable ``random_object_parameters`` is only ever populated with values once per generation. | + | All functions in these tables are run each tick, but global variable ``random_object_parameters`` is only ever populated with values once per generation. Functions called through these tables are not supplied with arguments. |
{| {{prettytable}} | {| {{prettytable}} | ||
Line 229: | Line 231: | ||
| unittests | | unittests | ||
| Runs before preprocess if the global ``debug_level`` is greater than 0. Expects a boolean output. | | Runs before preprocess if the global ``debug_level`` is greater than 0. Expects a boolean output. | ||
+ | |||
+ | |- | ||
+ | | languages | ||
+ | | For each function in this table, registers a language. Expects table of key-value pairs where the key is the [[Language token#WORD|word token]] and the value is the translated string. | ||
|} | |} | ||
Line 294: | Line 300: | ||
| function(subregion,tok) | | function(subregion,tok) | ||
| [[Titan]] | | [[Titan]] | ||
+ | |||
+ | |} | ||
+ | |||
+ | ===Entities=== | ||
+ | |||
+ | Generated entities are supplied with the ``idx`` parameter, which is a number that starts at 1 and increments by 1 for each entity of that type. It can be used to give a unique ID to [[divine equipment]]. | ||
+ | |||
+ | {| {{prettytable}} | ||
+ | |- style='background-color:#ddd' | ||
+ | ! width="40%" | Table | ||
+ | ! width="40%" | Inputs | ||
+ | ! width="60%" | Articles | ||
+ | |||
+ | |- | ||
+ | | entities.vault_guardian.default | ||
+ | | function(idx,tok) | ||
+ | | [[Vault]] ([[angel]]s) | ||
+ | |||
+ | |||
+ | |- | ||
+ | | entities.mythical_guardian.default | ||
+ | | function(idx,tok) | ||
+ | | [[Mysterious dungeon]] ([[dungeon guardian]]s) | ||
+ | |||
+ | |} | ||
+ | |||
+ | ===Materials=== | ||
+ | {| {{prettytable}} | ||
+ | |- style='background-color:#ddd' | ||
+ | ! width="20%" | Table | ||
+ | ! width="15%" | Inputs | ||
+ | ! width="15%" | Article | ||
+ | ! width="60%" | Notes | ||
+ | |||
+ | |- | ||
+ | | materials.clouds | ||
+ | materials.rain | ||
+ | | function() | ||
+ | | [[Evil weather]] | ||
+ | | Associated regional [[interaction]]s are automatically written by ``init/generators.lua``. | ||
+ | |||
+ | |- | ||
+ | | materials.divine.metal | ||
+ | materials.divine.silk | ||
+ | | function([[sphere]]) | ||
+ | | [[Divine metal]] | ||
+ | [[Divine fabric]] | ||
+ | | The list of potential spheres is determined by the individual function. See [[Lua script examples#New divine metal]]. | ||
+ | |||
+ | |- | ||
+ | | materials.mythical_remnant | ||
+ | | function([[sphere]]) | ||
+ | | [[Primordial remnant]] | ||
+ | | Possible spheres are determined by ``random_object_parameters.mythical_sphere``. | ||
+ | |||
+ | |- | ||
+ | | materials.mythical_healing | ||
+ | | function() | ||
+ | | [[Mythical substance]] | ||
+ | | | ||
|} | |} |
Revision as of 16:52, 25 March 2025
Modding |
---|
Tokens |
Audio · Biome · Graphics · Tile page · Interaction · Mod info · Plant · Speech · Sphere · Syndrome · World |
Body tokens |
Body · Body detail plan · Bodygloss · Tissue |
Creature tokens |
Creature · Creature mannerism · Personality facet · Creature variation · Procedural graphics layer |
Descriptor tokens |
Descriptor color · Color · Descriptor pattern · Descriptor shape |
Entity tokens |
Entity · Ethic · Language · Value · Position |
Job tokens |
Building · Labor · Reaction · Skill · Unit type |
Item tokens |
Item type · Item definition · Ammo · Armor · Instrument · Tool · Trap component · Weapon |
Material tokens |
Material type · Material definition · Inorganic material definition |
Lua |
Scripting · Examples · Functions |
Dwarf Fortress defines a number of functions in addition to those standard for Lua 5.4.
C++ Function Calls
Function | Description |
---|---|
int trandom(int n) | Returns a 32-bit integer from 0 to n-1. Uses DF's internal RNG system instead of math.random() .
|
str capitalize_string_words(str s) | Capitalizes every word in a string. |
str capitalize_string_first_word(str s) | Capitalizes the first word in a string. |
str utterance() | Returns a word from the kobold language. |
void lua_log(str s) | Prints a string to Dwarf Fortress/lualog.txt . The log() function is more robust and should be used instead.
|
void raws.register_creatures(table lines) |
Takes a table of tokens and reads them as that type of raw file. It is not necessary to add a header. |
Globals
Helper functions are defined in init/globals.lua
, and can be accessed by
Generation
This function is defined in init/generators.lua
.
Function | Description |
---|---|
table add_generated_info(table tbl) | Adds [GENERATED] to the input table, and [SOURCE_HFID] /[SOURCE_ENID] if IDs are defined. Necessary for generated raws to be saved properly.
|
Randomization
Function | Description |
---|---|
userdata pick_random(table t) | Returns a random value from a table. |
userdata pick_random_no_replace(table t) | Returns a random value from a table, then removes it from the table. |
userdata pick_random_conditional(table t, function cond,...) | Returns a random value from a table that satisfies cond(...) .
|
bool one_in(num x) | Returns true with a one in x chance. |
userdata pick_random_pairs(table tbl) | Returns a random key from a table. For example, pick_random_pairs( {WATER = true} ) returns "WATER".
|
userdata pick_weighted_from_table(table tbl) | Requires a table of tables with weight keys. Returns a weighted random value.
At debug level >=4, logs the roll. |
userdata generate_from_list(table tbl,...) | Requires a table of functions that return a weight key. Runs each function and returns a weighted random output. Used by werebeasts to generate an interaction and link to options from it.
|
Tables
Function | Description |
---|---|
table split_to_lines(table tbl,string str) | Adds a string into a table, with each line being a separate key. |
table map_merge(table tbl1, table tbl2) | Combines two tables. If tbl1 already has a value for a given key, it will not be overwritten. Used for sets such as { WATER = true } .
|
table table_merge(table tbl1, table tbl2) | Adds each value from tbl2 onto the end of tbl1 .
|
bool find_in_array_part(table tbl, userdata item) | Returns true if item is a value in tbl .
|
void convert_array_to_set(table tbl) | For each key in a table, sets the value to true. |
void add_unique(table tbl, userdata item) | Adds item to the end of tbl if not already present.
|
void remove_item(table tbl, userdata item) | Removes all instances of item from tbl .
|
table shallow_copy(table tbl) | Returns a table copied from all values in the input table. |
table deep_copy(table tbl) | Returns a table recursively copied from all values in the input table. |
Debug
Function | Description |
---|---|
void log(...) | Logs the input to Dwarf Fortress/lualog.txt . Used for most cases.
|
string get_caller_loc_string() | Returns the debug source info and the current line. |
string get_debug_logger(num level=1,...) | Logs get_caller_loc_string() and any overloads if the debug_level is at least level . debug_level is a global variable that defaults to 0.
|
function partial_function(function f, arg,...) | Returns f(arg,...) .
|
function log_table(table tbl, num debug_level=0, num nest_level=0, num added_debug_from_nest=0) | Logs a table if the global debug_level is at least the input debug_level . nest_level starts at 0 and adds added_debug_from_nest for each nesting to the input debug level.
|
function print_table(table tbl, num nest_level=0) | Logs a table, regardless of debug level. |
Spheres
Function | Description |
---|---|
string get_random_sphere_adjective(string sphere) | Returns a random string from global table random_sphere_adjective[sph] .
|
table get_random_sphere_noun(string sphere) | Returns a random table tbl from global table random_sphere_nouns[sphere] .
|
table add_sphere_mpp(table sphere_list, string new_s, table available_sphere, table available_sphere_cur) | Adds new_s to sphere_list and all parents and children. Sets the added spheres in available_sphere and available_sphere_cur to false, and sets all enemies in available_sphere_cur to false.
At debug level >= 2, will be logged. |
Generation Tables
The generate()
function is defined in init/generators.lua
, and is called to generate objects.
Each type of random object is defined through a function that generates a table of raws. To generate objects for specific purposes, generate()
can call functions from a table and provide inputs such as tok
(unique token string).
For example, forgotten beasts are unique and generated for each cave region. If multiple functions are defined in creatures.fb
, such as both creatures.fb.default
and a modded forgotten beast, the game uses the weight
output to influence which one will be randomly chosen.
By default, objects are generated in this order: unittests*, preprocess, do_once, materials, items, languages, creatures, interactions, entities, and postprocess. Do note that you can register raws at any step. For example, each vault entity generates a set of divine equipment during the entity step, and werebeasts generate a curse interaction during the creature step.
Custom
All functions in these tables are run each tick, but global variable random_object_parameters
is only ever populated with values once per generation. Functions called through these tables are not supplied with arguments.
Table | Description |
---|---|
preprocess | Runs once the world's terrain has been finalized and before history can begin.
Includes two functions by default: |
do_once | Runs immediately after all functions in preprocess, and before all other generation steps. Only runs if random_object_parameters.main_world_randoms = true , for added safety.
Recommended for generating custom objects. |
postprocess | Runs after all other generation steps are finished. |
unittests | Runs before preprocess if the global debug_level is greater than 0. Expects a boolean output.
|
languages | For each function in this table, registers a language. Expects table of key-value pairs where the key is the word token and the value is the translated string. |
Creatures
Table | Inputs | Article |
---|---|---|
creatures.angel.great_beast |
function(tok) | Angel (see types) |
creatures.demon | function(demon_type,difficulty,tok) | Demon |
creatures.experiment.beast_large |
function(tok) | Experiment |
creatures.fb | function(layer_type,tok) | Forgotten beast |
creatures.night_creature.bogeyman | function(tok) | Bogeyman |
creatures.night_creature.nightmare | function(tok) | Nightmare |
creatures.night_creature.troll | function(tok) | Night troll |
creatures.night_creature.werebeast
werebeast_origin_interactions* |
function(tok)
function(tok,name,options) |
Werebeast |
creatures.titan | function(subregion,tok) | Titan |
Entities
Generated entities are supplied with the idx
parameter, which is a number that starts at 1 and increments by 1 for each entity of that type. It can be used to give a unique ID to divine equipment.
Table | Inputs | Articles |
---|---|---|
entities.vault_guardian.default | function(idx,tok) | Vault (angels)
|
entities.mythical_guardian.default | function(idx,tok) | Mysterious dungeon (dungeon guardians) |
Materials
Table | Inputs | Article | Notes |
---|---|---|---|
materials.clouds
materials.rain |
function() | Evil weather | Associated regional interactions are automatically written by init/generators.lua .
|
materials.divine.metal
materials.divine.silk |
function(sphere) | Divine metal | The list of potential spheres is determined by the individual function. See Lua script examples#New divine metal. |
materials.mythical_remnant | function(sphere) | Primordial remnant | Possible spheres are determined by random_object_parameters.mythical_sphere .
|
materials.mythical_healing | function() | Mythical substance |