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 script examples

From Dwarf Fortress Wiki
Jump to navigation Jump to search


Main article: Lua scripting

Snippets of vanilla generation can be found in Category:Lua script pages, and all vanilla scripts can be found in data/vanilla/vanilla_procedural/scripts/.

Helper functions[edit]

Search by reaction class[edit]

This script returns a table of all inorganic materials with a given [REACTION_CLASS]. The mat table also has reaction_product_class, which includes both [MATERIAL_REACTION_PRODUCT] and [ITEM_REACTION_PRODUCT] IDs.

Languages[edit]

Identity language[edit]

This makes a language called GEN_IDENTITY which is like: "Abbey abbeyabbeys the abbey of abbeys" - i.e. it's the "English" language you might see occasionally. It is present in vanilla_procedural and can be used for [TRANSLATION] by default.

Kobold language[edit]

This generates a language made of [UTTERANCES]. This is essentially a proper translation based on the kobold language. Note that the hardcoded utterance() function generates words independently of any existing words in the language, so you may get duplicate words.

Generators[edit]

Non-random generated material[edit]

Here's an example of an object registered through the do_once table. There are no random elements, it is equivalent (save for being [GENERATED]) to an object defined through Material definition tokens and registered through the raws.register_inorganics() function. It also prints itself to the lualog for debugging purposes.



You can register multiple objects at the same time. This script takes a table of color tokens, and makes a metal named after each of them, with a corresponding cheaty adventure reaction.

Random generation[edit]

Here's an example of various DF-specific randomizers in use:

  • trandom() is used to determine how many metals generate this way.
  • utterance() generates utterances from the Kobold language, e.g. "gorsnus", "stogodilmus", "gaylgis"
  • pick_random_no_replace() determines the color from the table, but removes the rolled value so there's no repeats.

New divine metals[edit]

Many of the tables used by vanilla procedural objects are global, and thus can be added to or overwritten by mods. You can add new metal descriptions for divine metal pretty easily, for example:



You can also add alternatives to the default divine metal function, such as one based on the aforementioned kobold metals. Vanilla divine metal uses metal_by_sphere to determine its properties, and is thus valid only if the input sphere has an entry in that table. Note that even if the weights are nominally the same; because it is valid for all input spheres, it will outnumber instances of the more limited vanilla material.

Remove default functions[edit]

Just as easily as new functions and table entries can be added, default entries can be overwritten so that they cannot generate. This snippet removes the default forgotten beasts.

See Lua functions#Generation Tables for a list of the tables the default functions are stored in.

creatures.fb.default=nil

Basic generated creature[edit]

This is essentially the simplest possible creature. It has no extraordinary options (beyond an association with animals and creation), and each world generates one species from this function. It doesn't have any biome tokens or likewise, so it can only be spawned in the arena, but it is still functional.

A sample output would be "A quadruped composed of flame. It has two narrow tails and it has a regal bearing."

Making your own RCP[edit]

You don't have to be limited to the vanilla list of random creature variants. Mods can add new kinds of random creature profiles, materials, etc that can be seamlessly integrated into how it generates creatures.

This script generates a single creature based on a eurypterid. It has a unique RCP, set in the function itself.



Adding a eurypterid profile to random_creature_profiles allows any creature to access it, if their profile is determined randomly.

This script gives it proper flippers in its body base function. It's also associated with water-based random creatures, so it can potentially generate as an aquatic forgotten beast species.

Other feature variants stored in tables, like materials, attacks, or descriptions, can be added in this way.

Werebugs[edit]

You can make a custom function to determine what RCPs are available to a given creature.

These scripts are for an arthropoid version of a werebeast.

  • arthropod_rcp is a list of RCP keys which fit this theme (plus a few worms on there for fun), which the creature function rolls on to pick its form.
  • is_bloodsucking_by_key, along with rcp.must_suck_blood_through_proboscis and rcp.must_suck_blood_through_mouth are checked to assign [BLOODSUCKER] to it, which allows certain creature types to exhibit vampiric behavior if their berserk rampage wasn't enough fun.

Otherwise, the function should work the same as vanilla werebeasts, generating an associated major curse in the same way and becoming "twisted into humanoid form" (at the expense of their additional limbs).

New forgotten beasts[edit]

You can add new types of forgotten beasts (or more appropriately, [FEATURE_BEAST]s). These generate as alternatives when populating the caverns with unique monsters. There are a number of options to interact with shared generation functions.

Unbidden spirits only appear in dry cave layers, and like "spirit" demons, are malevolent floating beings made of gas or dust.



Elementals are defined by the material they're made of, using a table of options to set the right properties. In a dry chasm layer, they'll roll on the fb_elements table, while in a water layer, they'll be a water elemental. Should they ever carry a syndrome, they would inflict dyskrasia.

The most important feature is setting options.sphere_rcm to a key in random_creature_materials, so fire elementals are made of "FLAME" and earth elementals are made of "ANY_MINERAL".

Tweaking creatures[edit]

This function is run in build_body_from_rcp() right before tweaks are determined. If a generated creature's size is greater than 500,000 Γ (about as much as an elephant), this patch adds [POWER] and the like to make forgotten beasts, titans, etc capable of impersonating deities and ruling civilizations.

Adamantine alloys[edit]

You can add your own arbitrary generated objects, though as of right now there's no way to make settings for them. This allows for some truly wild stuff; here's a fun example: adamantine-metal alloys for every single non-special metal, giving you an average of the properties of them.

CancelHideAbout

Rating Lua script examples