## Lua interface to perlin noise generators

For people working on the C++ code.
Member

Posts: 12
Joined: Sat Jan 21, 2012 13:56

### Lua interface to perlin noise generators

patch

Demo code for 2d noise
Code: Select all
`local test_noise = function( minp, maxp, seed )    --  var = new PerlinNoise(seed, octaves, persisitence)    -- or    -- minetest.env:get_perlinnoise(seed_diff, octaves, persistence)    -- it will use world seed + seed_diff as a seed value    local noise = minetest.env:get_perlinnoise(4, 8, 0.6)    local pr = PseudoRandom(seed)    local cell_size = 4    local cell_volume =  cell_size * cell_size    local ore_in_cell_min = 3    local ore_in_cell_max = 8    if maxp.x < 0 or minp.x > 0 then        return    end    for j = minp.y, maxp.y do        for k = minp.z, maxp.z do            local p = { x= 0 , y= j, z = k}            minetest.env:set_node(p, {name ="default:sandstone"} )        end    end    for j = minp.y, maxp.y, cell_size do        for k = minp.z, maxp.z, cell_size do            local p2d = { x= 0.5 + j/50 , y= 0.5 + k/50 }            local noiseval = noise:get2d(p2d)            print ( dump(p2d) .. " " .. dump(noiseval))            local ore = math.floor( noiseval * ore_in_cell_max + 1)            if  ore >= ore_in_cell_min  then                for n = 1, ore do                    local jd = pr:next(0, cell_size - 1 )                    local kd = pr:next(0, cell_size - 1 )                    local p = { x = 0, y = j + jd, z = k + kd }                    minetest.env:set_node(p, {name ="default:mese"} )                end            end        end    endendminetest.register_on_generated(test_noise)`

Result:

Demo code for 3D noise:
Code: Select all
`local test_noise = function( minp, maxp, seed )    local noise = minetest.env:get_perlinnoise(3, 8, 0.6)    local pr = PseudoRandom(seed)    local cell_size = 16    local chunks_per_cell_min = 2    local chunks_per_cell_max = 5    local chunk_size = 3    local chunk_volume = chunk_size * chunk_size * chunk_size    local ore_per_chunk = 5    for i = minp.x, maxp.x, cell_size do        for j = minp.y, maxp.y, cell_size do            for k = minp.z, maxp.z, cell_size do                local noise_p = { x= 0.5 + i/200 , y= 0.5 + j/200, z =  0.5 + k/200 }                local noiseval = noise:get3d(noise_p)                local chunks = math.floor( noiseval * chunks_per_cell_max + 1)                -- print ( dump(noise_p) .. " " .. dump(noiseval) .. "  " .. dump(chunks))                if chunks >= chunks_per_cell_min then                    for n = 1, chunks do                        local id = pr:next(0, cell_size - chunk_size )                        local jd = pr:next(0, cell_size - chunk_size )                        local kd = pr:next(0, cell_size - chunk_size )                        for ci = i + id , i + id + chunk_size - 1 do                            for cj = j + id, j + jd + chunk_size - 1 do                                for ck = k + kd, k + kd + chunk_size -1 do                                    local p = { x = ci, y =  cj , z =  ck}                                    if pr:next(1, chunk_volume) < ore_per_chunk then                                        minetest.env:set_node(p, {name ="default:mese"} )                                    else                                        minetest.env:set_node(p, {name = "default:glass"} )                                    end                                end                            end                        end                    end                end            end        end    endendminetest.register_on_generated(test_noise)`

Result:

Death Dealer
Member

Posts: 1379
Joined: Wed Feb 15, 2012 18:46
Location: Limbo
What is this for?
Keep calm and code python^_^

Member

Posts: 12
Joined: Sat Jan 21, 2012 13:56
It can be used for ore placement, for example. Current way in upstream an in moreores/gems is way too boring.
Last edited by inkelyad on Sun Apr 01, 2012 15:10, edited 1 time in total.

Death Dealer
Member

Posts: 1379
Joined: Wed Feb 15, 2012 18:46
Location: Limbo
Oh very cool:D
Keep calm and code python^_^

jordan4ibanez
Member

Posts: 1895
Joined: Tue Sep 27, 2011 18:44
Location: Rhode Island, USA
GitHub: jordan4ibanez
IRC: jordan4ibanez
In-game: jordan4ibanez
holy crap that is genius
I've been gone for a long time

Member

Posts: 12
Joined: Sat Jan 21, 2012 13:56
jordan4ibanez wrote:holy crap that is genius

I very much doubt it.

But if you want to see this patch in main minetest code you should trouble dev team about it.

I am far too lazy for that.

Jordach
Member

Posts: 4523
Joined: Mon Oct 03, 2011 17:58
Location: Blender Scene
GitHub: Jordach
IRC: Jordach
In-game: Jordach
Celeron must have added it into master:

Code: Select all
`+  double cave_amount = noise2d_perlin(      1476    +      0.5+(double)node_min.X/250, 0.5+(double)node_min.Y/250,      1477    +      data->seed+34329, 3, 0.50);      1478    +  cave_amount = MYMAX(0.0, 4.0*cave_amount+2.0);      1479    +  u32 caves_count = cave_amount * volume_nodes / 100000;      1480    +  u32 bruises_count = cave_amount * volume_nodes / 400000;      1481    +  bruises_count *= MYMAX(1, -node_max.Y/100);`
viewtopic.php?f=10&t=19056 Solar Plains Dev Server

Hackeridze
Member

Posts: 310
Joined: Thu Nov 03, 2011 13:35
+1
My game: RTMG
GENTOO USER