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.

Difference between revisions of "Lua scripting"

From Dwarf Fortress Wiki
Jump to navigation Jump to search
m (Preformatted text)
m (Better syntax highlighting)
 
Line 12: Line 12:
 
===Divine language===
 
===Divine language===
 
This is the [[divine language]], which generates a bunch of random-sounding words from a set of syllables.
 
This is the [[divine language]], which generates a bunch of random-sounding words from a set of syllables.
<pre>languages.GEN_DIVINE=function()
+
{{Main:Divine language/script}}
 
 
    local letters={}
 
    letters.vowel={}
 
    letters.cons={}
 
    letters.vowel.COMMON_NUM=5
 
    letters.vowel.NUM=35
 
    letters.cons.COMMON_NUM=12
 
    letters.cons.NUM=22
 
    letters.vowel.lookup={
 
        "a","e","i","o","u",
 
        "ae","ai","ao","au","ea","ei","eo","eu","ia","ie","io","iu","oa","oe","oi","ou","ua","ue","ui","uo","ah","eh","ih","oh","uh","ay","ey","iy","oy","uy"
 
    }
 
    letters.cons.lookup={
 
        "b","p","g","k","c","z","s","d","t","m","n","ng",
 
        "v","f","w","h","j","l","r","q","x","y"
 
    }
 
 
 
    for k,v in pairs(letters) do
 
        v.common={}
 
        v.rare={}
 
        for i=1,5 do
 
            if trandom(5)~=0 then v.common[i]=v.lookup[trandom(v.COMMON_NUM)+1] else v.common[i]=v.lookup[trandom(v.NUM)+1] end
 
        end
 
        for i=1,15 do
 
            v.rare[i]=v.lookup[trandom(v.NUM)+1]
 
        end
 
    end
 
 
 
    local function letter(t)
 
        if trandom(5)~=0 then
 
            return pick_random(t.common)
 
        else
 
            return pick_random(t.rare)
 
        end
 
    end
 
    local gen_divine={}
 
    for k,v in ipairs(world.language.word) do
 
        local str=""
 
        if trandom(2)~=0 then
 
            str=str..letter(letters.cons)
 
            str=str..letter(letters.vowel)
 
        else
 
            str=str..letter(letters.vowel)
 
        end
 
        local num_letters=trandom(3)
 
        str=str..letter(letters.cons)
 
        if num_letters>0 then str=str..letter(letters.vowel) end
 
        if num_letters>1 then str=str..letter(letters.cons) end
 
        gen_divine[v.token]=str
 
    end
 
 
 
    return gen_divine
 
end
 
</pre>
 
 
 
 
===Identity language===
 
===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.
 
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.
  
<pre>languages.GEN_IDENTITY=function()
+
<syntaxhighlight lang="lua" line>
 +
languages.GEN_IDENTITY=function()
 
     -- just to demonstrate the absolute most basic method of generating one of these
 
     -- just to demonstrate the absolute most basic method of generating one of these
 
     -- also so that you can just mod stuff to use GEN_IDENTITY
 
     -- also so that you can just mod stuff to use GEN_IDENTITY
Line 88: Line 34:
 
     return tbl
 
     return tbl
 
end
 
end
</pre>
+
</syntaxhighlight>
 
[[Category:Modding]]
 
[[Category:Modding]]
 
[[Category:Lua]]
 
[[Category:Lua]]

Latest revision as of 04:29, 28 November 2024

Dwarven science stretched.png Research Pending!
This article or section is incomplete/under construction (likely due to recent changes) and may still be outdated or missing details. Feel free to do some testing and expand it.


This article is about procedural raw generation. Information on Utility:DFHack scripting can be found at https://docs.dfhack.org/en/stable/.

Lua scripting is an upcoming feature. It is used to create custom procedurally-generated objects that were previously created by hardcoded methods. It was announced in a video, with the stated goal of "supporting future magical endeavors."

Inorganic materials, languages, creatures, interactions, items (currently excluding instruments), reactions, entities, and plants are open to this system.

Code Samples[edit]

Divine language[edit]

This is the divine language, which generates a bunch of random-sounding words from a set of syllables.

 1languages.GEN_DIVINE=function()
 2
 3    local letters={}
 4    letters.vowel={}
 5    letters.cons={}
 6    letters.vowel.COMMON_NUM=5
 7    letters.vowel.NUM=35
 8    letters.cons.COMMON_NUM=12
 9    letters.cons.NUM=22
10    letters.vowel.lookup={
11        "a","e","i","o","u",
12        "ae","ai","ao","au","ea","ei","eo","eu","ia","ie","io","iu","oa","oe","oi","ou","ua","ue","ui","uo","ah","eh","ih","oh","uh","ay","ey","iy","oy","uy"
13    }
14    letters.cons.lookup={
15        "b","p","g","k","c","z","s","d","t","m","n","ng",
16        "v","f","w","h","j","l","r","q","x","y"
17    }
18
19    for k,v in pairs(letters) do
20        v.common={}
21        v.rare={}
22        for i=1,5 do
23            if trandom(5)~=0 then v.common[i]=v.lookup[trandom(v.COMMON_NUM)+1] else v.common[i]=v.lookup[trandom(v.NUM)+1] end
24        end
25        for i=1,15 do 
26            v.rare[i]=v.lookup[trandom(v.NUM)+1]
27        end
28    end
29
30    local function letter(t)
31        if trandom(5)~=0 then
32            return pick_random(t.common)
33        else
34            return pick_random(t.rare)
35        end
36    end
37    local gen_divine={}
38    for k,v in ipairs(world.language.word) do
39        local str=""
40        if trandom(2)~=0 then
41            str=str..letter(letters.cons)
42            str=str..letter(letters.vowel)
43        else
44            str=str..letter(letters.vowel)
45        end
46        local num_letters=trandom(3)
47        str=str..letter(letters.cons)
48        if num_letters>0 then str=str..letter(letters.vowel) end
49        if num_letters>1 then str=str..letter(letters.cons) end
50        gen_divine[v.token]=str
51    end
52
53    return gen_divine
54end

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.

 1languages.GEN_IDENTITY=function()
 2    -- just to demonstrate the absolute most basic method of generating one of these
 3    -- also so that you can just mod stuff to use GEN_IDENTITY
 4    local tbl={}
 5    local unempty = function(str1, str2) 
 6        return str1=='' and str2 or str1
 7    end
 8    for k,v in ipairs(world.language.word) do
 9        local str=''
10        str=unempty(str,v.NOUN_SING)
11        str=unempty(str,v.ADJ)
12        str=unempty(str,v.VERB_FIRST_PRES)
13        str=unempty(str,string.lower(v.token))
14        tbl[v.token]=str
15    end
16    return tbl
17end