- 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 script examples"
m |
(Tutorialization scripts for materials) |
||
Line 24: | Line 24: | ||
end}} | end}} | ||
− | == | + | ==Languages== |
===Identity language=== | ===Identity language=== | ||
Line 64: | Line 64: | ||
end}} | end}} | ||
− | ===New divine | + | ==Generators== |
− | You can add new metal descriptions for divine metal pretty easily, for example: | + | |
+ | ===Non-random generated material=== | ||
+ | |||
+ | Here's an example of an object registered through the ``do_once`` table. There are no random elements, it is equivalent (save for being {{token|GENERATED|mat}}) to an object defined through [[Material definition token]]s and registered through the ``raws.register_inorganics()`` function. It also prints itself to the lualog for debugging purposes. | ||
+ | |||
+ | {{Scriptdata | ||
+ | |title=Single material | ||
+ | |script=do_once.cobalt = function() | ||
+ | local lines = {} | ||
+ | |||
+ | -- basic inorganic definition | ||
+ | lines[#lines+1] = "[INORGANIC:COBALT]" | ||
+ | -- add [GENERATED] to save properly | ||
+ | add_generated_info(lines) | ||
+ | lines[#lines+1] = "[USE_MATERIAL_TEMPLATE:METAL_TEMPLATE]" | ||
+ | lines[#lines+1] = "[STATE_NAME_ADJ:ALL_SOLID:cobalt]" | ||
+ | lines[#lines+1] = "[STATE_NAME_ADJ:LIQUID:liquid cobalt]" | ||
+ | lines[#lines+1] = "[STATE_NAME_ADJ:GAS:boiling cobalt]" | ||
+ | lines[#lines+1] = "[STATE_COLOR:ALL_SOLID:COBALT]" | ||
+ | lines[#lines+1] = "[SPECIAL]" | ||
+ | |||
+ | raws.register_inorganics(lines) | ||
+ | |||
+ | -- show in lualog | ||
+ | print_table(lines) | ||
+ | end | ||
+ | }} | ||
+ | <br><br> | ||
+ | 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. | ||
+ | |||
+ | {{Scriptdata | ||
+ | |title=Chromatic metals | ||
+ | |script=do_once.chromatic_metal = function() | ||
+ | local lines = {} | ||
+ | local reaction_lines = {} | ||
+ | |||
+ | local color_tokens = { | ||
+ | "AMETHYST", | ||
+ | "AQUAMARINE", | ||
+ | "CARDINAL", | ||
+ | "COBALT", | ||
+ | "EMERALD", | ||
+ | "JADE", | ||
+ | "MOSS_GREEN", | ||
+ | "PEARL", | ||
+ | "SAFFRON", | ||
+ | "TURQUOISE", | ||
+ | "WHITE", | ||
+ | } | ||
+ | |||
+ | -- make a metal for each color token | ||
+ | for k,v in pairs(color_tokens) do | ||
+ | -- begin definition with ID token, [GENERATED] and template | ||
+ | lines[#lines+1] = "[INORGANIC:CHROMATICMETAL"..v.."]" | ||
+ | add_generated_info(lines) | ||
+ | lines[#lines+1] = "[USE_MATERIAL_TEMPLATE:METAL_TEMPLATE]" | ||
+ | |||
+ | -- look up the metal's color in the world table | ||
+ | -- using string.lower(v) would result in "moss_green" | ||
+ | local metalname = world.descriptor.color[v].name | ||
+ | lines[#lines+1] = "[STATE_NAME_ADJ:ALL_SOLID:"..metalname.."]" | ||
+ | lines[#lines+1] = "[STATE_NAME_ADJ:LIQUID:liquid "..metalname.."]" | ||
+ | lines[#lines+1] = "[STATE_NAME_ADJ:GAS:boiling "..metalname.."]" | ||
+ | |||
+ | -- appearance | ||
+ | lines[#lines+1] = "[STATE_COLOR:ALL_SOLID:"..v.."]" | ||
+ | lines[#lines+1] = "[SPECIAL]" | ||
+ | |||
+ | -- create a corresponding reaction | ||
+ | reaction_lines[#reaction_lines+1] = "[REACTION:CHROMATICMETAL"..v.."]" | ||
+ | add_generated_info(reaction_lines) | ||
+ | reaction_lines[#reaction_lines+1] = "[NAME:create "..metalname.." bars]" | ||
+ | reaction_lines[#reaction_lines+1] = "[ADVENTURE_MODE_ENABLED]" | ||
+ | -- make sure we're consistent with the inorganic ID | ||
+ | reaction_lines[#reaction_lines+1] = "[PRODUCT:100:1:BAR:NONE:INORGANIC:CHROMATICMETAL"..v.."]" | ||
+ | reaction_lines[#reaction_lines+1] = "[PRODUCT_DIMENSION:150]" | ||
+ | end | ||
+ | raws.register_inorganics(lines) | ||
+ | raws.register_reactions(reaction_lines) | ||
+ | end | ||
+ | }} | ||
+ | |||
+ | ===Random generation=== | ||
+ | |||
+ | 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. | ||
+ | |||
+ | {{Scriptdata | ||
+ | |title=Kobold metals | ||
+ | |script=do_once.kobold_metal = function() | ||
+ | local lines = {} | ||
+ | local reaction_lines = {} | ||
+ | |||
+ | local color_tokens = { | ||
+ | "AMETHYST", | ||
+ | "AQUAMARINE", | ||
+ | "CARDINAL", | ||
+ | "COBALT", | ||
+ | "EMERALD", | ||
+ | "JADE", | ||
+ | "MOSS_GREEN", | ||
+ | "PEARL", | ||
+ | "SAFFRON", | ||
+ | "TURQUOISE", | ||
+ | "WHITE", | ||
+ | } | ||
+ | |||
+ | -- trandom() is expressed as (1dN)-1 because it uses C++ math that starts at 0 | ||
+ | local max_loops = trandom(10)+1 | ||
+ | |||
+ | -- create 1-10 metals | ||
+ | for i = 1,max_loops do | ||
+ | -- begin definition with ID token, [GENERATED] and template | ||
+ | lines[#lines+1] = "[INORGANIC:KOBOLDMETAL"..i.."]" | ||
+ | add_generated_info(lines) | ||
+ | lines[#lines+1] = "[USE_MATERIAL_TEMPLATE:METAL_TEMPLATE]" | ||
+ | |||
+ | -- this is the kobold name function | ||
+ | local metalname = utterance().."ite" | ||
+ | lines[#lines+1] = "[STATE_NAME_ADJ:ALL_SOLID:"..metalname.."]" | ||
+ | lines[#lines+1] = "[STATE_NAME_ADJ:LIQUID:liquid "..metalname.."]" | ||
+ | lines[#lines+1] = "[STATE_NAME_ADJ:GAS:boiling "..metalname.."]" | ||
+ | -- no_replace removes the value from the table | ||
+ | -- we don't need a fallback because there's more values than metals | ||
+ | lines[#lines+1] = "[STATE_COLOR:ALL_SOLID:"..pick_random_no_replace(color_tokens).."]" | ||
+ | lines[#lines+1] = "[SPECIAL]" | ||
+ | |||
+ | -- create a corresponding reaction | ||
+ | reaction_lines[#reaction_lines+1] = "[REACTION:KOBOLDMETAL"..i.."]" | ||
+ | add_generated_info(reaction_lines) | ||
+ | reaction_lines[#reaction_lines+1] = "[NAME:create "..metalname.." bars]" | ||
+ | reaction_lines[#reaction_lines+1] = "[ADVENTURE_MODE_ENABLED]" | ||
+ | -- make sure we're consistent with the inorganic ID | ||
+ | reaction_lines[#reaction_lines+1] = "[PRODUCT:100:1:BAR:NONE:INORGANIC:KOBOLDMETAL"..i.."]" | ||
+ | reaction_lines[#reaction_lines+1] = "[PRODUCT_DIMENSION:150]" | ||
+ | end | ||
+ | raws.register_inorganics(lines) | ||
+ | raws.register_reactions(reaction_lines) | ||
+ | end | ||
+ | }} | ||
+ | |||
+ | ===New divine metals=== | ||
+ | |||
+ | 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: | ||
{{Scriptdata | {{Scriptdata | ||
Line 76: | Line 221: | ||
} | } | ||
}} | }} | ||
+ | <br><br> | ||
+ | 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. | ||
+ | |||
+ | {{Scriptdata | ||
+ | |title=Divine kobold metal | ||
+ | |script=materials.divine.metal.kobold = function(sphere) | ||
+ | if not foo then | ||
+ | log(#metal_by_sphere) | ||
+ | log(#world.spheres) | ||
+ | foo = true | ||
+ | end | ||
+ | local lines = {} | ||
+ | --generation function handles ID, registration, generated info | ||
+ | lines[#lines+1] = "[USE_MATERIAL_TEMPLATE:METAL_TEMPLATE]" | ||
+ | --add_generated_info(lines) | ||
+ | local metalname = utterance().."ite" | ||
+ | lines[#lines+1] = "[STATE_NAME_ADJ:ALL_SOLID:"..metalname.."]" | ||
+ | lines[#lines+1] = "[STATE_NAME_ADJ:LIQUID:liquid "..metalname.."]" | ||
+ | lines[#lines+1] = "[STATE_NAME_ADJ:GAS:boiling "..metalname.."]" | ||
+ | |||
+ | local color_tokens = { | ||
+ | "AMETHYST", | ||
+ | "AQUAMARINE", | ||
+ | "CARDINAL", | ||
+ | "COBALT", | ||
+ | "EMERALD", | ||
+ | "JADE", | ||
+ | "MOSS_GREEN", | ||
+ | "PEARL", | ||
+ | "SAFFRON", | ||
+ | "TURQUOISE", | ||
+ | "WHITE", | ||
+ | } | ||
+ | --allow for duplicate colors | ||
+ | lines[#lines+1] = "[STATE_COLOR:ALL_SOLID:"..pick_random(color_tokens).."]" | ||
+ | |||
+ | --add a block of tokens | ||
+ | lines=split_to_lines(lines,[[ | ||
+ | [MATERIAL_VALUE:200] | ||
+ | [SPEC_HEAT:7500] | ||
+ | [MELTING_POINT:NONE] | ||
+ | [BOILING_POINT:NONE] | ||
+ | [ITEMS_WEAPON][ITEMS_WEAPON_RANGED][ITEMS_AMMO][ITEMS_DIGGER][ITEMS_ARMOR][ITEMS_ANVIL] | ||
+ | [ITEMS_HARD] | ||
+ | [ITEMS_METAL] | ||
+ | [ITEMS_BARRED] | ||
+ | [ITEMS_SCALED] | ||
+ | [SOLID_DENSITY:1000] | ||
+ | [LIQUID_DENSITY:1000] | ||
+ | [MOLAR_MASS:20000] | ||
+ | [IMPACT_YIELD:1000000] | ||
+ | [IMPACT_FRACTURE:2000000] | ||
+ | [IMPACT_STRAIN_AT_YIELD:0] | ||
+ | [COMPRESSIVE_YIELD:1000000] | ||
+ | [COMPRESSIVE_FRACTURE:2000000] | ||
+ | [COMPRESSIVE_STRAIN_AT_YIELD:0] | ||
+ | [TENSILE_YIELD:1000000] | ||
+ | [TENSILE_FRACTURE:2000000] | ||
+ | [TENSILE_STRAIN_AT_YIELD:0] | ||
+ | [TORSION_YIELD:1000000] | ||
+ | [TORSION_FRACTURE:2000000] | ||
+ | [TORSION_STRAIN_AT_YIELD:0] | ||
+ | [SHEAR_YIELD:1000000] | ||
+ | [SHEAR_FRACTURE:2000000] | ||
+ | [SHEAR_STRAIN_AT_YIELD:0] | ||
+ | [BENDING_YIELD:1000000] | ||
+ | [BENDING_FRACTURE:2000000] | ||
+ | [BENDING_STRAIN_AT_YIELD:0] | ||
+ | [MAX_EDGE:12000] | ||
+ | ]]) | ||
+ | --sends this to get registered | ||
+ | return {raws=lines,weight=1} | ||
+ | end | ||
+ | }} | ||
+ | |||
+ | ===Remove default functions=== | ||
+ | |||
+ | Just as easily as you can add new functions and table entries, you can overwrite default entries so that they cannot generate. This snippet removes the default forgotten beasts. | ||
+ | |||
+ | <syntaxhighlight lang="lua" > | ||
+ | creatures.fb.default=nil | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | ===New forgotten beasts=== | ||
+ | You can add new types of forgotten beasts, generating 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" [[demon]]s, are malevolent floating beings made of gas or dust. | |
− | |||
{{Scriptdata | {{Scriptdata | ||
Line 128: | Line 360: | ||
end | end | ||
}} | }} | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
===Adamantine alloys=== | ===Adamantine alloys=== |
Revision as of 04:12, 29 June 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 |
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
Search by reaction class
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.
[show][Select all] get_all_by_reaction_class() |
---|
Languages
Identity language
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.
[show][Select all] GEN_IDENTITY |
---|
Kobold language
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.
[show][Select all] GEN_KOBOLD |
---|
Generators
Non-random generated material
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.
[show][Select all] Single material |
---|
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.
[show][Select all] Chromatic metals |
---|
Random generation
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.
[show][Select all] Kobold metals |
---|
New divine metals
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:
[show][Select all] Laughing metal |
---|
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.
[show][Select all] Divine kobold metal |
---|
Remove default functions
Just as easily as you can add new functions and table entries, you can overwrite default entries so that they cannot generate. This snippet removes the default forgotten beasts.
creatures.fb.default=nil
New forgotten beasts
You can add new types of forgotten beasts, generating 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.
[show][Select all] Unbidden spirit |
---|
Adamantine alloys
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.
[show][Select all] Adamantine alloys |
---|