.

User avatar
jordan4ibanez
Member
 
Posts: 1865
Joined: Tue Sep 27, 2011 18:44
GitHub: jordan4ibanez
IRC: jordan4ibanez
In-game: jordan4ibanez

.

by jordan4ibanez » Sat Mar 30, 2013 05:38

.
Last edited by jordan4ibanez on Wed May 01, 2013 01:54, edited 1 time in total.
If you can think it, you can make it.
 

ashenk69
Member
 
Posts: 230
Joined: Tue Jul 03, 2012 00:08

by ashenk69 » Sat Mar 30, 2013 14:05

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.
 

User avatar
Inocudom
Member
 
Posts: 2889
Joined: Sat Sep 29, 2012 01:14
IRC: Inocudom
In-game: Inocudom

by Inocudom » Sat Mar 30, 2013 16:30

What effects will this have on RAM and CPU usage?
 

User avatar
Likwid H-Craft
Member
 
Posts: 1113
Joined: Sun Jan 06, 2013 14:20

by Likwid H-Craft » Sat Mar 30, 2013 17:33

At first, I was thinking "Surface Finding" about Windows Surface.
Image
My Domain's/others:
http://likwidtest.hj.cx/ (Not Done)
 

User avatar
jordan4ibanez
Member
 
Posts: 1865
Joined: Tue Sep 27, 2011 18:44
GitHub: jordan4ibanez
IRC: jordan4ibanez
In-game: jordan4ibanez

by jordan4ibanez » Sat Mar 30, 2013 20:11

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
Last edited by jordan4ibanez on Sat Mar 30, 2013 20:18, edited 1 time in total.
If you can think it, you can make it.
 

Sokomine
Member
 
Posts: 2980
Joined: Sun Sep 09, 2012 17:31

by Sokomine » Tue Apr 02, 2013 17:40

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?
A list of my mods can be found here.
 

User avatar
jordan4ibanez
Member
 
Posts: 1865
Joined: Tue Sep 27, 2011 18:44
GitHub: jordan4ibanez
IRC: jordan4ibanez
In-game: jordan4ibanez

by jordan4ibanez » Wed Apr 03, 2013 12:32

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!
If you can think it, you can make it.
 


Return to Minetest General

Who is online

Users browsing this forum: No registered users and 8 guests

cron