Page 1 of 1

.

PostPosted: Sat Mar 30, 2013 05:38
by jordan4ibanez
.

PostPosted: Sat Mar 30, 2013 14:05
by ashenk69
I know someone had posted before about lua optimization and one tip that I found was to use locals in high iteration blocks. For example
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
local getNode = minetest.env:get_node

This would speed up parts of your code where you want to get a node because instead of requiring 3 calls it only needs one local call. Local calls in lua are quicker than globals because locals are stored in the register.

PostPosted: Sat Mar 30, 2013 16:30
by Inocudom
What effects will this have on RAM and CPU usage?

PostPosted: Sat Mar 30, 2013 17:33
by Likwid H-Craft
At first, I was thinking "Surface Finding" about Windows Surface.
Image

PostPosted: Sat Mar 30, 2013 20:11
by jordan4ibanez
Inocudom wrote:What effects will this have on RAM and CPU usage?

Nobody knows! You'll have to try it for yourself. It's much faster than what I've seen before, especially for 100% accuracy.

Here is a new version! It tries to save cpu as MUCH as possible. The "Cancel search" nodes are "hard coded" into it, but I will update this when I make it modifiable.
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
--[[
    You can copy, use this in your mods, an change it, as long as you give me credit as the original coder. [Jordan4Ibanez]
]]--
num_nodes = {}
thickness = {}
function minetest.generate_nodes(node_id,base_block,node_table,node_thickness)
    thickness[node_id] = node_thickness
    num_nodes[node_id] = 0
    for k,v in pairs(node_table) do
        num_nodes[node_id] = num_nodes[node_id] + 1
    end
    minetest.register_on_generated(function(minp, maxp, blockseed)
        local testing_node = minetest.env:find_node_near({x=(minp.x + maxp.x)/2,y=(minp.y + maxp.y)/2,z=(minp.z + maxp.z)/2}, 40, base_block)
        if testing_node == nil then
            return
        end
        --let's find the lowest point of air
        baser = nil
        for y = minp.y, maxp.y do
            for x = minp.x, maxp.x do
                for z = minp.z, maxp.z do
                    local get_node = minetest.env.get_node
                    local test = get_node(minetest.env, {x=x,y=y,z=z}).name
                    if test == "air" then
                        baser = y - 1
                        break
                    end
                end
                if baser ~= nil then
                    break
                end
                z=0
            end
            if baser ~= nil then
                break
            end
            x=0
        end
        print(dump(baser))
        for x = minp.x,maxp.x do
            for z = minp.z,maxp.z do
                for y = baser,maxp.y do
                    local get_node = minetest.env.get_node
                    local test = get_node(minetest.env, {x=x,y=y,z=z}).name
                    --local test = minetest.env:get_node({x=x,y=y,z=z}).name
                    if test == base_block then
                        if minetest.env:get_node({x=x,y=y+1,z=z}).name == "air" then
                            if math.random()*100 < thickness[node_id] then
                                local noding = node_table[math.random(0,num_nodes[node_id])]
                                if noding ~= nil then
                                    local set_node = minetest.env.set_node
                                    local setter = set_node(minetest.env, {x=x,y=y+1,z=z}, {name=noding})
                                    --minetest.env:set_node({x=x,y=y+1,z=z}, {name=noding})
                                end
                            end
                        end
                        y = minp.y
                        z = z + 1
                    --Cancel search nodes
                    elseif test == "default:water_source" or test == "default:sand" or test == "default:clay" or test == "air" then
                        y = minp.y
                        z = z + 1           
                    end
                end
            end
            z=0
        end
    end)
end
minetest.generate_nodes("normal","default:dirt_with_grass",{"default:grass_1","default:grass_2","default:grass_3","default:grass_4","default:grass_5"},25)


Grassy Map code:

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
minetest.generate_nodes("normal","default:dirt_with_grass",{"default:grass_1","default:grass_2","default:grass_3","default:grass_4","default:grass_5"},25)


With this surface generation code, it generates grass like so:
Image

PostPosted: Tue Apr 02, 2013 17:40
by Sokomine
It might be intresting to know the average ground height in a chunk (as far as that can be determined) and how smooth the terrain is without having to actually look for the surface. Even my rather small trader huts usually need a support platform (which looks for the ground in a similar way like this algorithm) because most areas are not smooth enough. Knowing where the surface is might also be very intresting for mobs. Maybe this could be made available inside the engine?

PostPosted: Wed Apr 03, 2013 12:32
by jordan4ibanez
Sokomine wrote:It might be intresting to know the average ground height in a chunk (as far as that can be determined) and how smooth the terrain is without having to actually look for the surface. Even my rather small trader huts usually need a support platform (which looks for the ground in a similar way like this algorithm) because most areas are not smooth enough. Knowing where the surface is might also be very intresting for mobs. Maybe this could be made available inside the engine?

or even a "on_surface" function for on_generate!