v50 Steam/Premium information for editors
  • 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.
This notice may be cached—the current version can be found here.

Lua functions

From Dwarf Fortress Wiki
Revision as of 19:54, 7 July 2025 by DPhKraken (talk | contribs) (→‎Creature patching: Generalized description, timing, local functions)
Jump to navigation Jump to search


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_reactions(table lines)
void  raws.register_creatures(table lines)
void  raws.register_entities(table lines)
void  raws.register_items(table lines)
void  raws.register_inorganics(table lines)
void  raws.register_interactions(table lines)
void  raws.register_languages(table lines)
void  raws.register_plants(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 any script even if vanilla_procedural is not loaded.

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
value  pick_random(table t) Returns a random value from a table.
value  pick_random_no_replace(table t) Returns a random value from a table, then removes it from the table.
value  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.
value  pick_random_pairs(table tbl) Returns a random key from a table. For example, pick_random_pairs( {WATER = true} ) returns "WATER".
value  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.

value  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)