master
rnd1 2015-10-20 14:23:29 +02:00
commit fdac1a87a7
1 changed files with 117 additions and 0 deletions

117
init.lua Normal file
View File

@ -0,0 +1,117 @@
-- naturally growing trees
-- rnd, 2015
local TREE_SIZE = 20;
local TRUNK_SIZE = 4;
local BRANCH_LENGTH = 10.;
minetest.register_node("rnd_trees:tree", {
description = "naturally growing tree",
tiles = {"default_tree.png"},
is_ground_content = true,
groups = {cracky=3, stone=1},
drop = 'default:tree',
after_place_node = function(pos, placer, itemstack, pointed_thing)
local meta = minetest.get_meta(pos);
meta:set_string("infotext","growth started");
meta:set_int("life",TREE_SIZE);
meta:set_int("branch",0);
end
})
minetest.register_abm({
nodenames = {"rnd_trees:tree"},
neighbors = {"air"},
interval = 1.0,
chance = 2,
action = function(pos, node, active_object_count, active_object_count_wider)
local meta = minetest.get_meta(pos);
local life = meta:get_int("life");
local branch = meta:get_int("branch");
minetest.set_node(pos, {name = "default:tree"});
-- LEAVES
if life<=0 or (life<TREE_SIZE-TRUNK_SIZE and math.random(5)==1) then -- either end of growth or above trunk randomly
local r;
if life <=0 then r = math.random(2)+1; -- determine leaves region size
else r = math.random(2);
end
local i,j,k
for i=-r,r do
for j=-r,r do
for k = -r,r do
local p = {x=pos.x+i,y=pos.y+j,z=pos.z+k};
if minetest.get_node(p).name == "air" and math.random(3)==1 then
minetest.set_node(p,{name="default:leaves"});
end
end
end
end
end
if life<=0 then return end -- stop growth
local above = {x=pos.x,y=pos.y+1,z=pos.z};
local nodename = minetest.get_node(above).name
-- GROWTH
if nodename == "air" or nodename == "default:leaves" then -- can we grow up
if math.random(3)==1 then -- occasionaly change direction of growth a little
above.x=above.x+math.random(3)-2;
above.z=above.z+math.random(3)-2;
end
-- BRANCHING
if math.random(3)==1 and life<TREE_SIZE-TRUNK_SIZE and branch == 0 then -- not yet in branch
local dir = {x=math.random(5)-3,y=0,z=math.random(5)-3};
if math.random(2)==1 then dir.y=(math.random(3)-2) end -- occassionaly branch nonhorizontaly
local dirlen = math.sqrt(dir.x*dir.x+dir.y*dir.y+dir.z*dir.z);
if dirlen == 0 then dirlen = 1 end; dir.x=dir.x/dirlen; dir.y=dir.y/dirlen; dir.z=dir.z/dirlen; -- normalize
local length = math.random(life/TREE_SIZE*BRANCH_LENGTH)+1; -- length of branch
for i=1,length-1 do
local p = {x=above.x+dir.x*i,y=above.y+dir.y*i,z=above.z+dir.z*i};
nodename = minetest.get_node(p).name;
if nodename== "air" or nodename == "default:leaves" then
minetest.set_node(p,{name="default:tree"});
end
end
local grow = {x=above.x+dir.x*length,y=above.y+dir.y*length,z=above.z+dir.z*length};
minetest.set_node(grow,{name="rnd_trees:tree"});
meta = minetest.get_meta(grow);
meta:set_int("life",life-1);meta:set_int("branch",branch+1); -- remember that we branched
meta:set_string("infotext","branch, life ".. life-1);
end
-- add new growing part
minetest.set_node(above,{name="rnd_trees:tree"});
meta = minetest.get_meta(above);
meta:set_int("life",life-1);meta:set_int("branch",branch); -- decrease life
meta:set_string("infotext","growing, life ".. life-math.random(TREE_SIZE*0.25));
if branch==0 then -- make main trunk a bit thicker
-- for i = -1,1 do
-- for j = -1,1 do
-- if math.random(4)==1 then
minetest.set_node({x=pos.x+1,y=pos.y,z=pos.z},{name="default:tree"});
minetest.set_node({x=pos.x-1,y=pos.y,z=pos.z},{name="default:tree"});
minetest.set_node({x=pos.x,y=pos.y,z=pos.z+1},{name="default:tree"});
minetest.set_node({x=pos.x,y=pos.y,z=pos.z-1},{name="default:tree"});
-- end
-- end
-- end
end
end
end,
})