From b5807be921c671a66db35f3afe1675873cfdfe4a Mon Sep 17 00:00:00 2001 From: HybridDog Date: Sun, 10 Jan 2016 13:42:16 +0100 Subject: [PATCH] use functions from vector_extras which should improve speed --- nether/depends.txt | 1 + nether/init.lua | 112 +++++++++++++++++++-------------------------- 2 files changed, 48 insertions(+), 65 deletions(-) diff --git a/nether/depends.txt b/nether/depends.txt index 2f29bdb..a6b36a8 100644 --- a/nether/depends.txt +++ b/nether/depends.txt @@ -2,5 +2,6 @@ default glow riesenpilz stairs +vector_extras fence_registration? watershed? diff --git a/nether/init.lua b/nether/init.lua index fcfc91a..aac9bf1 100644 --- a/nether/init.lua +++ b/nether/init.lua @@ -55,6 +55,9 @@ local mushroom_rarity = 80 -- Frequency of trees in the nether forest (higher is less frequent) local tree_rarity = 200 +local abm_tree_interval = 864 +local abm_tree_chance = 100 + -- height of the nether generation's end nether.start = f_h_max+100 @@ -689,6 +692,10 @@ function nether.grow_netherstructure(pos, generated) end +local set = vector.set_data_to_pos +local get = vector.get_data_from_pos +local remove = vector.remove_data_from_pos + local function soft_node(id) return id == c.air or id == c.ignore end @@ -698,7 +705,6 @@ local function update_minmax(min, max, p) max.x = math.max(max.x, p.x) min.z = math.min(min.z, p.z) max.z = math.max(max.z, p.z) - return min, max end local fruit_chances = {} @@ -769,18 +775,18 @@ function nether.grow_tree(pos, generated) for j = 1,r do local x = p.x+j*dir[1] local z = p.z+j*dir[2] - trunks[x.." "..p.y.." "..z] = dir[3] + set(trunks, z,p.y,x, dir[3]) end r = r+1 p.x = p.x+r*dir[1] p.z = p.z+r*dir[2] - trunk_corners[p.x.." "..p.y.." "..p.z] = dir[4] or dir[3] + set(trunk_corners, p.z,p.y,p.x, dir[4] or dir[3]) local h = math.random(h_arm_min, h_arm_max) for i = 1,h do - trunks[p.x.." "..p.y+i.." "..p.z] = true + set(trunks, p.z,p.y+i,p.x, true) end p.y = p.y+h - --n = #todo+1 -- causes small trees + --n = #todo+1 -- caused small trees todo[#todo+1] = p end if p.y > pos.y+h_trunk_max then @@ -793,13 +799,17 @@ function nether.grow_tree(pos, generated) local fruits = {} local trunk_ps = {} local count = 0 - for n,par in pairs(trunks) do - local p = {} - p.x, p.y, p.z = unpack(string.split(n, " ")) - if par ~= true then - p.par = par + local ps, trmin, trmax, trunk_count = vector.get_data_pos_table(trunks) + + update_minmax(min, max, trmin) + update_minmax(min, max, trmax) + + for _,d in pairs(ps) do + if d[4] == true then + d[4] = nil end - table.insert(trunk_ps, p) + trunk_ps[#trunk_ps+1] = d + local pz, py, px = unpack(d) count = count+1 if count > leaf_thickness then count = 0 @@ -807,18 +817,21 @@ function nether.grow_tree(pos, generated) local fruit_chance = fruit_chances[y] for z = -2,2 do for x = -2,2 do - local dist = math.sqrt(x*x+y*y+z*z) - if math.floor(dist) ~= 0 - and math.random(1, dist) == 1 then - local pstr = p.x+x.." "..p.y+y.." "..p.z+z - if not trunks[pstr] then - if math.random(1, fruit_rarity) == 1 - and fruit_chance + local distq = x*x+y*y+z*z + if distq ~= 0 + and math.random(1, math.sqrt(distq)) == 1 then + local x = x+px + local y = y+py + local z = z+pz + if not get(trunks, z,y,x) then + if fruit_chance + and math.random(1, fruit_rarity) == 1 and math.random(1, fruit_chance) == 1 then - fruits[pstr] = true + set(fruits, z,y,x, true) else - leaves[pstr] = true + set(leaves, z,y,x, true) end + update_minmax(min, max, {x=x, z=z}) end end end @@ -827,37 +840,8 @@ function nether.grow_tree(pos, generated) end end - local leaf_ps = {} - for n,_ in pairs(leaves) do - local p = {} - p.x, p.y, p.z = unpack(string.split(n, " ")) - table.insert(leaf_ps, p) - min, max = update_minmax(min, max, p) - end - - for n,_ in pairs(trunks) do - local p = {} - p.x, _, p.z = unpack(string.split(n, " ")) - min, max = update_minmax(min, max, p) - end - for i = -1,h_stem+1 do - table.insert(trunk_ps, {x=pos.x, y=pos.y+i, z=pos.z, par=0}) - end - - local trunk_corner_ps = {} - for n,par in pairs(trunk_corners) do - local p = {} - p.x, p.y, p.z = unpack(string.split(n, " ")) - p.par = par - table.insert(trunk_corner_ps, p) - end - - local fruit_ps = {} - for n,_ in pairs(fruits) do - local p = {} - p.x, p.y, p.z = unpack(string.split(n, " ")) - table.insert(fruit_ps, p) + trunk_ps[#trunk_ps+1] = {pos.z, pos.y+i, pos.x, 0} -- par 0 because of leaves end local manip = minetest.get_voxel_manip() @@ -866,8 +850,8 @@ function nether.grow_tree(pos, generated) local nodes = manip:get_data() local param2s = manip:get_param2_data() - for _,p in pairs(leaf_ps) do - p = area:indexp(p) + for _,p in pairs(vector.get_data_pos_table(leaves)) do + p = area:index(p[3], p[2], p[1]) if soft_node(nodes[p]) then nodes[p] = c.nether_leaves param2s[p] = math.random(0,179) @@ -875,30 +859,28 @@ function nether.grow_tree(pos, generated) end end - for _,p in pairs(fruit_ps) do - p = area:indexp(p) + for _,p in pairs(vector.get_data_pos_table(fruits)) do + p = area:index(p[3], p[2], p[1]) if soft_node(nodes[p]) then nodes[p] = c.nether_apple end end for _,p in pairs(trunk_ps) do - local par = p.par - p = area:indexp(p) + local par = p[4] + p = area:index(p[3], p[2], p[1]) if par then param2s[p] = par end nodes[p] = c.nether_tree end - for _,p in pairs(trunk_corner_ps) do - local par = p.par - p = area:indexp(p) - nodes[p] = c.nether_tree_corner - param2s[p] = par + for _,p in pairs(vector.get_data_pos_table(trunk_corners)) do + local vi = area:index(p[3], p[2], p[1]) + nodes[vi] = c.nether_tree_corner + param2s[vi] = p[4] end - --calculating took ca. 0.07 - 0.18 [s] manip:set_data(nodes) manip:set_param2_data(param2s) manip:write_to_map() @@ -906,7 +888,7 @@ function nether.grow_tree(pos, generated) if generated then spam = 3 end - nether:inform("a nether tree grew at ("..pos.x.."|"..pos.y.."|"..pos.z..")", spam, t1) + nether:inform("a nether tree with "..trunk_count.." branch trunk nodes grew at ("..pos.x.."|"..pos.y.."|"..pos.z..")", spam, t1) if not generated then local t1 = os.clock() manip:update_map() @@ -935,8 +917,8 @@ minetest.register_abm({ minetest.register_abm({ nodenames = {"nether:tree_sapling"}, neighbors = {"group:nether_dirt"}, - interval = 864, - chance = 100, + interval = abm_tree_interval, + chance = abm_tree_chance, action = function(pos) if minetest.get_node({x=pos.x, y=pos.y+2, z=pos.z}).name == "air" and minetest.get_node({x=pos.x, y=pos.y+1, z=pos.z}).name == "air" then