How to randomly generate trees?

User avatar
scifiboi
Member
 
Posts: 96
Joined: Wed Jul 18, 2012 21:28

How to randomly generate trees?

by scifiboi » Thu May 09, 2013 22:11

One of the main troubles I am having while programming my mod is making trees generate. I have an idea on how on how to make the trees generate without randomizing the leaves, but that takes a MASSIVE amount of repetitious code. How does minetest generate trees like it does? I have searched through the worldgen.lua in the default mod in the default game, but I couldn't find anything about trees. Could someone help with this?
This is a signature virus. Add me to your signature so that I can multiply.
My mod: Thaumtest
 

User avatar
PilzAdam
Member
 
Posts: 4026
Joined: Fri Jul 20, 2012 16:19
GitHub: PilzAdam
IRC: PilzAdam

by PilzAdam » Thu May 09, 2013 22:23

Look at src/treegen.cpp

I also have the treegen ported to Lua in my farming mod.
Last edited by PilzAdam on Thu May 09, 2013 22:23, edited 1 time in total.
 

User avatar
scifiboi
Member
 
Posts: 96
Joined: Wed Jul 18, 2012 21:28

by scifiboi » Thu May 09, 2013 22:46

I don't know cpp that well. :P But I'll have a look at your mod and see what I can figure out. Thanks! :)
EDIT: I'm having trouble finding code for any trees at all. In your post, you said something about rubber trees, but in the zip, I can't find anything to do with trees.
Last edited by scifiboi on Thu May 09, 2013 23:00, edited 1 time in total.
This is a signature virus. Add me to your signature so that I can multiply.
My mod: Thaumtest
 

User avatar
PilzAdam
Member
 
Posts: 4026
Joined: Fri Jul 20, 2012 16:19
GitHub: PilzAdam
IRC: PilzAdam

by PilzAdam » Thu May 09, 2013 23:34

 

User avatar
Mihobre
Member
 
Posts: 101
Joined: Tue Oct 30, 2012 08:45

by Mihobre » Tue May 21, 2013 13:56

Hey, if you ever know ho to generate a tree, make a mod that makes coal, iron, and mese trees, with stones as trunks
I wanna fly!!!
I wanna have feathery wings of silver, blue, and white!!!
I wanna soar high and touch the sky!!!
I wanna fly!!!
 

User avatar
Evergreen
Member
 
Posts: 2131
Joined: Sun Jan 06, 2013 01:22
GitHub: 4Evergreen4
IRC: EvergreenTree
In-game: Evergreen

by Evergreen » Tue May 21, 2013 13:59

This code makes the actual model for the tree, but Idk how to make them spawn randomly.
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
function farming:generate_tree(pos, trunk, leaves, underground, replacements)
    pos.y = pos.y-1
    local nodename = minetest.env:get_node(pos).name
    local ret = true
    for _,name in ipairs(underground) do
        if nodename == name then
            ret = false
            break
        end
    end
    pos.y = pos.y+1
    if not minetest.env:get_node_light(pos) then
        return
    end
    if ret or minetest.env:get_node_light(pos) < 8 then
        return
    end

    node = {name = ""}
    for dy=1,4 do
        pos.y = pos.y+dy
        if minetest.env:get_node(pos).name ~= "air" then
            return
        end
        pos.y = pos.y-dy
    end
    node.name = trunk
    for dy=0,4 do
        pos.y = pos.y+dy
        minetest.env:set_node(pos, node)
        pos.y = pos.y-dy
    end

    if not replacements then
        replacements = {}
    end

    node.name = leaves
    pos.y = pos.y+3
    for dx=-2,2 do
        for dz=-2,2 do
            for dy=0,3 do
                pos.x = pos.x+dx
                pos.y = pos.y+dy
                pos.z = pos.z+dz

                if dx == 0 and dz == 0 and dy==3 then
                    if minetest.env:get_node(pos).name == "air" and math.random(1, 5) <= 4 then
                        minetest.env:set_node(pos, node)
                        for name,rarity in pairs(replacements) do
                            if math.random(1, rarity) == 1 then
                                minetest.env:set_node(pos, {name=name})
                            end
                        end
                    end
                elseif dx == 0 and dz == 0 and dy==4 then
                    if minetest.env:get_node(pos).name == "air" and math.random(1, 5) <= 4 then
                        minetest.env:set_node(pos, node)
                        for name,rarity in pairs(replacements) do
                            if math.random(1, rarity) == 1 then
                                minetest.env:set_node(pos, {name=name})
                            end
                        end
                    end
                elseif math.abs(dx) ~= 2 and math.abs(dz) ~= 2 then
                    if minetest.env:get_node(pos).name == "air" then
                        minetest.env:set_node(pos, node)
                        for name,rarity in pairs(replacements) do
                            if math.random(1, rarity) == 1 then
                                minetest.env:set_node(pos, {name=name})
                            end
                        end
                    end
                else
                    if math.abs(dx) ~= 2 or math.abs(dz) ~= 2 then
                        if minetest.env:get_node(pos).name == "air" and math.random(1, 5) <= 4 then
                            minetest.env:set_node(pos, node)
                            for name,rarity in pairs(replacements) do
                                if math.random(1, rarity) == 1 then
                                minetest.env:set_node(pos, {name=name})
                                end
                            end
                        end
                    end
                end

                pos.x = pos.x-dx
                pos.y = pos.y-dy
                pos.z = pos.z-dz
            end
        end
    end
end
"Help! I searched for a mod but I couldn't find it!"
http://krock-works.16mb.com/MTstuff/modSearch.php
 

User avatar
VanessaE
Member
 
Posts: 3894
Joined: Sun Apr 01, 2012 12:38
GitHub: VanessaE
IRC: VanessaE
In-game: VanessaEzekowitz

by VanessaE » Tue May 21, 2013 14:16

Don't build trees with Lua, use the in-engine L-systems code instead. See my moretrees and plantlife mods for examples of how to use it.
You might like some of my stuff:
Plantlife ~ More Trees ~ Home Decor ~ Pipeworks ~ HDX Textures (16-512px)
Tips (BTC): 13LdcdUFcNCFAm7HfvAXh5GHTjCnnQj6KE
 

User avatar
VanessaE
Member
 
Posts: 3894
Joined: Sun Apr 01, 2012 12:38
GitHub: VanessaE
IRC: VanessaE
In-game: VanessaEzekowitz

by VanessaE » Tue May 21, 2013 15:01

Four nodes, actually. One field for the trunk, two kinds of leaves, and a field intended for specifying fruit, which could also be used to make a third type of leaves if so desired.
You might like some of my stuff:
Plantlife ~ More Trees ~ Home Decor ~ Pipeworks ~ HDX Textures (16-512px)
Tips (BTC): 13LdcdUFcNCFAm7HfvAXh5GHTjCnnQj6KE
 

User avatar
fessmK
Member
 
Posts: 46
Joined: Wed Sep 25, 2013 15:56

by fessmK » Sat Oct 19, 2013 20:26

nice code for the tree.
Can I use it?

You can use perlin noise (see the docfarming mod) or you can use an ABM that continues to generate them through the game.
 


Return to WIP Mods

Who is online

Users browsing this forum: No registered users and 12 guests

cron