REM ***********************************************
REM Title: Random Tree
REM Author: unknown
REM Downloaded from: http://dbcc.zimnox.com/
REM ***********************************************
 
set display mode 1024,768,16
sync on:autocam off:hide mouse
position camera 0,40,-50:point camera 0,35,0
backdrop on:color backdrop rgb(96,96,160)
randomize timer()
 
rem leaf texture
create bitmap 1,128,128:set current bitmap 1:cls 0:ink rgb(0,160,0),0
for x=0 to 64:y=sqrt(4096-x*x):box 64-x,64-y,64+x,64+y:next x
get image 1,0,0,128,128
 
rem branch texture
cls 0
for x=0 to 127 step 16:y=0
while y<127
ink rgb(rnd(128)+64,rnd(64)+64,0),0:y1=y+rnd(16)+16
if y1>127 then y1=127
box x,y,x+15,y1:y=y1
endwhile
next x
get image 2,0,0,128,128
set current bitmap 0:ink rgb(255,255,255),0
 
make object plain 1,0.01,0.01:position object 1,0,0,0
set object 1,1,0,0
tree(1,10.0,3.0,9,0,0)
 
do
set cursor 0,0:print screen fps()
turn object right 1,1:sync
loop
 
function tree(o,h#,b#,l,p,a)
m=1:while limb exist(o,m)=1:m=m+1:endwhile
if l>=1
   if b#<1
      make object box o+1,b#,h#,b#
   else
      make object cylinder o+1,1
      scale object o+1,100*b#,100*h#,100*b#
   endif
else
   make object plain o+1,h#/3,h#
   yrotate object o+1,90
endif
position object o+1,0,h#/2,0
make mesh from object m,o+1:delete object o+1
add limb o,m,m:offset limb o,m,0,h#*1.2,0
if p>0 then link limb o,m,p
if m>1 then rotate limb 1,m,0,a,20+rnd(20)
if l=0
   texture limb o,m,1
else
   texture limb o,m,2
   if b#>=1 then scale limb texture o,m,4,1
endif
set cursor 0,0:print screen fps()
turn object right 1,1:sync
if l>=1
   a=wrapvalue(a+10+rnd(30))
   tree(o,h#/1.2,b#/1.4,l-1,m,a)
   a=wrapvalue(a+180)
   tree(o,h#/1.2,b#/1.4,l-1,m,a)
endif
endfunction