
compiler_options _max_process=10000;

PROGRAM m7_Draw;




CONST
white=0;
red=80;
yellow=112;
green=144;
cyan=176;
blue=208;
magenta=240;


   pressies=10;


scr_w=800;
scr_h=600;

scr_cx=scr_w/2;
scr_cy=scr_h/2;



GLOBAL

col_id[6] = white,red,yellow,green,cyan,blue,magenta;

spot_id;
//file0;
fpg;
cam_id;


m7map[2];
roomsize=2048;

roomheight=1600;


collision_map;


mvx;
mvy;
mx[1];
my[1];
mdx;
mdy;


vox[255];



fire_laser;
lightmap[16];

//----------------------------
tree_id;

struct cam
distance;
posX;
posY; height;
posZ;
depth;
angle;
vx; vy; vz;
cx;
cy;
h,
w,
fx;
fy;
fz;
END

pitch;
yaw;
struct laser[6]
graph[16];
col;
end

struct ray
graph;
col;
x0; y0; h0; xl; yl;
x; y; h;
 length;
 angle;
 hit;
 hitlength;
col_id;
fire;
dangle;  angle0;
end









handler_id;

   snowbox_id;


//------------------------------------


PRIVATE
i; j; x0; y0; z0;  k; l;    c1; c2;
//------------------------------------

BEGIN
set_fps(24,0);
fpg=load_fpg("yulewars.FPG");
set_mode(scr_w*1000+scr_h);

define_region(1,0,0,128,128);

spot_id=id;

for(j=0; j<16; j+=1);
lightmap[j]=new_map(128,128,63,63,0);

for(i=0; i<63; i+=1)
k=i+1-j*4;
if(k<0) k=0; end
draw(5,k,15,1,i,i,127-i,127-i);
end
frame;
screen_copy(1,0,lightmap[j],0,0,128,128);
delete_draw(all_drawing);

end


define_region(1,0,0,roomsize/4,roomsize/4);

m7map=new_map(roomsize,roomsize,0,0,1);
I=draw(5,1,15,1,0,0,roomsize/4,roomsize/4);
j=new_map(roomsize/4,roomsize/4,0,0,0);
frame;
screen_copy(1,fpg,j,0,0,roomsize,roomsize);
delete_draw(i);
map_xput(fpg,m7map,j,0,0,0,400,0);
unload_map(j);



map_xput(fpg,m7map,lightmap[0],roomsize/2,roomsize/2,0,150,0);




m7map[1]=new_map(roomsize,roomsize,0,0,0);
map_block_copy(0,m7map[1],0,0,m7map,0,0,roomsize,roomsize);







cam.fX=roomsize/2;
cam.POSY=535;
cam.FZ=roomsize/2;
cam.posx=0;
cam.posz=0;

cam.angle=0;
cam.cx=scr_cx;
cam.w=scr_w/2;
cam.h=scr_h/2;



    cam.posy=800;
    cam.distance=700;
    cam.depth=380;
  cam.cy=80;


groundplane();
//write_int(0,scr_w+150,scr_h-20,0,&fps);
cam_id=update_cam();


init_mode7_layer();

make_lasers();
make_voxels();



for(l=0;l<pressies; l+=1)
i=rand(50,600);
j=l*360000/pressies;
x0=get_distx(j,i)+roomsize/2;
y0=get_disty(j,i)+roomsize/2;
k=0;
c1=rand(1,6);
c2=(c1+3);
if(c2>6) c2-=6; end
c1=col_id[c1];
c2=col_id[c2];
pressie(x0,y0,k,c1,c2);
end

drone(roomsize/4,roomsize/4,200);


//snow_machine();

snowbox_id=snowzone();

//snowbox_id=spotsnow();



for(i=0; i<12; i+=3)
fir();
j=roomsize/2;
son.x=roomsize/2+get_distx(i*30000,j);
son.y=roomsize/2+get_disty(i*30000,j);

end



handler_id=collisionhandler();



refreshmap();

loop


//show_data();
 if(key(_esc))
 exit("exit",0);
 end


height=roomheight;
 draw_m7line(0,0,0,0,height,0,15);
 draw_m7line(roomsize,0,0,roomsize,height,0,15);
 draw_m7line(roomsize,0,roomsize,roomsize,height,roomsize,15);
 draw_m7line(0,0,roomsize,0,height,roomsize,15);

    frame;
end

END

//=======================================================================================================================================================


process snowzone();
private
wd=100;
ln;
x1,y1;  z1;
x0; y0; z0;
col=8;
dgrid=100;
snowed;
begin
graph=new_map(roomsize/8,wd/4, 0,wd/8,16);
x=scr_cx; y=scr_cy;
    ln=roomsize/2;
    loop
     snowed+=1;
     /*
     x1=cos(ray.angle)*wd/1000;
     y1=sin(ray.angle)*wd/1000;

        //draw_m7line(ray.x0,ray.h0,ray.y0,ray.x,ray.h0,ray.y,red);

        draw_m7line(ray.x0+x1,ray.h0,ray.y0+y1,ray.xl+x1,ray.h0,ray.yl+y1,col);
        draw_m7line(ray.x0-x1,ray.h0,ray.y0-y1,ray.xl-x1,ray.h0,ray.yl-y1,col);

        draw_m7line(ray.x0+x1,0,ray.y0+y1,ray.xl+x1,0,ray.yl+y1,col);
        draw_m7line(ray.x0-x1,0,ray.y0-y1,ray.xl-x1,0,ray.yl-y1,col);

        draw_m7line(ray.x0+x1,ray.h0,ray.y0+y1,ray.x0+x1,0,ray.y0+y1,col);
        draw_m7line(ray.x0-x1,ray.h0,ray.y0-y1,ray.x0-x1,0,ray.y0-y1,col);

        draw_m7line(ray.xl+x1,ray.h0,ray.yl+y1,ray.xl+x1,0,ray.yl+y1,col);
        draw_m7line(ray.xl-x1,ray.h0,ray.yl-y1,ray.xl-x1,0,ray.yl-y1,col);
       */
       if(snowed>=50)
        for(z0=0;z0<ray.h0; z0+=dgrid)
            for(y0=-wd; y0<=wd; y0+=dgrid)
                for(x0=0;  x0<roomsize/2; x0+=dgrid*2)
                    snowdrop(x0,y0,z0,ray.angle);
                end
            end
        end

        snowed=1;
        break;
        end
        //if(mouse.right) snowed=0; end
        frame;

    end

end





process refreshmap()
begin
priority=1000;
loop

//map_block_copy(0,m7map,0,0,m7map[1],0,0,roomsize,roomsize);
frame;

end
end
//----------------------------------------------


process spotsnow()
private i;
begin
while(i<30)
if(rand(0,2)==1)
i+=1;
spotsnow_drop(spot_id.x+rand(-100,100),spot_id.y+rand(-100,100),200);
end
frame;
end
end

process spotsnow_drop(x,y,height);
private
light0=0;
dl;
light;
jx; jy; jh;
lx; ly; lh;
x0; y0:h0;
dir; dist;
begin
ctype=c_m7;

size=rand(100,200);
jh=-20;
loop

light=map_get_pixel(0,m7map,x,y);

if(light<=1)

dir=fget_angle(x,y,spot_id.x,spot_id.y):
x=spot_id.x+get_distx(dir+rand(-30000,30000),128);
y=spot_id.y+get_disty(dir+rand(-30000,30000),128);
graph=0;
else
light*=2;
if(light>63) light=63; end
graph=vox[light];

end
//light=76;

jx+=jx<rand(-10,10);
jx-=jx>rand(-10,10);
jy+=jy<rand(-10,10);
jy-=jy>rand(-10,10);

jh+=jh<rand(-14,4);
jh-=jh>rand(-14,4);
height+=jh;

x+=jx;
y+=jy;
if(height<=0)
height=200;
x=spot_id.x+rand(-100,100);
y=spot_id.y+rand(-100,100);
end




frame;
end
end
//___________________________________________________________________________________________
function make_voxels()
private i;
s=5;
begin

for(i=0; i<=255; i+=1);
vox[i]=new_map(s,s,s/2,s/2,i);
end

end
//___________________________________________________________________________________________

process candle(x,y,height)
private
struct v[5]
x0,y0,h0;
x,y,h;
col;
end
sz=5;
i; l;
h=5;
dcol;
dsz;
begin
    for(i=0; i<h; i+=1)
        v[l].x0=0; v[l].y0=0; v[l].h0=i*4;
            if(i <h-2)
                v[l].col=48;
            else
                v[l].col=yellow;
            end
    l+=1;

    end

    loop
        for(i=0; i<4; i+=1);
            dcol=0;
            dsz=0;
            if(i>=h-2)
            dcol=rand(-2,2);
            dsz=rand(-100,100);
            end
            draw_vox(x+v[i].x0,y+v[i].y0,height+15+v[i].h0,v[i].col+dcol,200+dsz,0);
        end
        frame;
    end
end

//___________________________________________________________________________________________


process drone(x,y,height)
private
i;  j; k; l;
collision_id;
struct v[4]
x0,y0,h0;
x,y,h;
col;
end
sz=10;
shot;
vh;
col;

begin
priority=spot_id.priority-4;
v[1].x0=-sz; v[1].y0=-sz; v[1].h0=0;
v[2].x0=sz; v[2].y0=-sz; v[2].h0=0;
v[3].x0=sz; v[3].y0=sz; v[3].h0=0;
v[4].x0=-sz; v[4].y0=sz; v[4].h0=0;
v[1].col=red;
v[2].col=green;
v[3].col=green;
v[4].col=red;

loop
advance(10*(key(_up)-key(_down)));
angle+=6000*(key(_left)-key(_right));

for(i=1; i<=4; i+=1);
v[i].x=1000*(v[i].x0*cos(angle))/1000000+1000*(v[i].y0*sin(angle))/1000000;
v[i].y=1000*(v[i].y0*cos(angle))/1000000-1000*(v[i].x0*sin(angle))/1000000;
if(i<4 and i>1)
v[i].h=(key(_up)-key(_down))*-10;
else
v[i].h=(key(_up)-key(_down))*10;
end
k+=3000;
k=k mod 360000;
col=(16*sin(k)/1000);
if(col>0)
col=v[i].col-16+col;
else
col=0;
end
draw_vox(x+v[i].x,y+v[i].y,height+25+v[i].h,col,200,0);
map_put(0,m7map,vox[col],x+v[i].x,y+v[i].y);
end
frame;
end

end


process pressie(x,y,height,c1,c2)
private
i;  j; k; l;
collision_id;
struct v[27]
x0,y0,h0;
x,y,h;
col;
end
sz=5;
shot;
vh;

begin

priority=spot_id.priority-2;

for(i=-sz; i<=sz; i+=sz)
    for(j=-sz; j<=sz; j+=sz)
        for(k=-sz; k<=sz; k+=sz)
            l+=1;
            v[l].x0=i; v[l].y0=j; v[l].h0=k*2;
            if(i mod (2*sz)==0  or j mod 2==0)
                v[l].col=c1;
            else
                v[l].col=c2;
            end
        end
    end
end


collision_id=cbody(3*sz,15);

loop
for(i=1; i<=27; i+=1);

draw_vox(x+v[i].x0,y+v[i].y0,height+25+v[i].h0,v[i].col-16,500,1);
end

if(collision_id.z==1 )
//shot=true;
end

if(shot)

height+=100;
end
if(height>0)
vh-=4;
height+=vh;
shot=0;
else
height=0;
vh=0;
end



frame;
z=0;
end
end

process draw_vox(x,y,height,col,size,light)
private dl;
begin
priority=-10;
if(light==1)
dl=map_get_pixel(0,m7map,x,y)/2;
if(dl>=2)
dl+=col;
if(dl>255) dl=255; end
graph=vox[dl];
else
graph=vox[1];
end
else
graph  =vox[col];
end
ctype=c_m7;
//size=500;
frame;
end

//___________________________________________________________________________________________
process snow_machine()
private i;
begin
size=80;
loop
//for(i=0; i<10; i+=1)
if(rand(0,10)>9)
//snowdrop(roomsize/2-rand(-size,size),roomsize/2-rand(-size,size),roomheight-400);

end
//end
frame;
end
end


//---------------------------------------------------------------------------------
process snowdrop(lx,ly,height,angle)
private
light0=0;
//dl;
light;
jx; jy; jh;
ang;
dang;
dist;
x0;y0;
tmp;

begin
ctype=c_m7;
size=rand(100,200);
jh=-10;
loop




jx+=jx<rand(-10,10);
jx-=jx>rand(-10,10);
jy+=jy<rand(-10,10);
jy-=jy>rand(-10,10);

jh+=jh<rand(-14,4);
jh-=jh>rand(-14,4);
height+=jh;

lx+=jx;
ly+=jy;

if(ray.dangle<>0)
tmp=(lx*cos(ray.dangle)+sin(ray.dangle)*ly)/1000;
ly=(ly*cos(ray.dangle)-sin(ray.dangle)*lx)/1000;
lx=tmp;

end
if(ly<-100)
    ly+=200;
else
    if(ly>100)
        ly-=200;
    end
end


if(height<=0)
    height=ray.h0+20;
    angle=ray.angle;
    //lx=rand(0,roomsize/2);
    ly=rand(-100,100);

end

if(lx>roomsize/2 or  lx<0)
    lx=rand(0,roomsize/2);
end

      angle=ray.angle;
     y0=-(cos(angle)*lx-sin(angle)*ly)/1000;
     x0=(cos(angle)*ly+sin(angle)*lx)/1000;
     x=ray.x0+x0;
     y=ray.y0+y0;


light0=63;
light0-=(lx/16);



light0/=1+abs(ly)/20;

dang=fget_angle(ray.y0,ray.x0,y,x);
dang-=ray.angle;
if(dang>180000) dang-=360000; end
if(dang<=-180000) dang+=360000; end


if(abs(dang)<150000)
light0=0;
end





dang=fget_angle(0,ray.h0,lx,height)-pitch;
dang=dang mod 360000;
if(dang>180000) dang-=360000; end
if(dang<=-180000) dang+=360000; end

light0/=1+(abs(dang)/1000);

if(abs(dang)>5000)
light0=0;
end




if(light0<0)
if(light0<-20)
    ly=rand(-100,100);
    lx=rand(0,roomsize/2);
    angle=ray.angle;
    height=ray.h0+20;
end
light=0;
else light=light0;
end
if(light>0 and rand(0,10)==10)
light=2*light;
if(light>63) light=63; end
end

graph=vox[light];

frame;
end
end
//------------------------------------------------------------------------------------


function flake_out(x,y)
begin
graph=vox[63];
x=scr_cx+x/4;
y=scr_cy+y/4;
if(collision(type snowzone))
return(1);
else
return(0);
end
end

process fir()
private
collision_id;
width=40;
hight=50;
spin;
begin
graph=new_map(width,hight,width/2,hight/2,22+rand(0,10));
ctype=c_m7;

collision_id=cbody(width,hight);
size=1000;
height=896;
loop
if(collision_id.z<>0 )
unload_map(graph);
graph=new_map(width,hight,width/2,hight/2,col_id[ray.col]);

end


frame;
end
end


process cbody(w,h);
private
dir;
dir1;
dist0;
elv;
elv1;
tx;
h;
begin
graph=new_map(w,7,w/2,3,12+16*rand(0,10));


y=0;

//graph=father.graph;
size=60;
cbody2(h);

loop

flags=0;
x=scr_cx+(father.x-roomsize/2)/4;
y=scr_cy+(father.y-roomsize/2)/4;
angle=fget_angle(father.x,father.y,roomsize/2,roomsize/2)+90000;
if(collision(type  collisionhandler))
flags=4;
else
flags=0;
end
frame;
if(flags==4 and son.flags==4)
z=1;
else
z=0;
end

end
end

process cbody2(h)
private dist;
begin
graph=new_map(h/2,h,h/4,h/2,15);
size=240;

loop
dist=fget_dist(father.father.x,father.father.y,roomsize/2,roomsize/2);
x=40+dist/4;
y=scr_h-100-father.father.height/4;
angle=fget_angle(40,scr_h-100-ray.h0/4,x,y)+90000;
if(collision(type  collisionhandler2))
flags=4;
else
flags=0;
end

frame;
end
end


process collisionhandler2(graph)
private
begin
loop
x=40;
y=scr_h-100-ray.h0/4;
angle=-pitch;
frame;
end
end


process collisionhandler()
private
cmap;
i; j; k;
c_id;
begin
graph=new_map(roomsize/6,5,0,2,44);
x=400;
y=300;
collisionhandler2(graph);
loop


c_id=collision(type cbody);

angle=-(ray.angle-90000);
frame;
end
end

process colliderdot(id1);
begin
id1.flags=4;
end







process grounddot();
private
sz; dist;   graph0;  dgraph;
begin
    size=400;
    ctype=c_m7;
    height=0;

    loop
        if(ray.h<=0)
            graph=vox[col_id[ray.col]];
            x=roomsize/2+(ray.x-ray.x0)*(0-ray.h0)/(1+ray.h-ray.h0);
            y=roomsize/2+(ray.y-ray.y0)*(0-ray.h0)/(1+ray.h-ray.h0);
            dist=fget_dist(roomsize/2,roomsize/2,x,y);
            sz=dist/2;

            dgraph=dist/64;
            if(dgraph<16)
                graph0=lightmap[dgraph];

                map_xput(0,m7map,graph0,x,y,0,sz,4);
            end
        end
        frame;
        map_block_copy(0,m7map,0,0,m7map[1],0,0,roomsize,roomsize);

     end
end


//-----------------------------------------------------------------------------------

function make_lasers()
private
g; l;
begin
laser[0].col=0;
laser[1].col=16;
laser[2].col=64;
laser[3].col=32;
laser[4].col=96;
laser[5].col=48;
laser[6].col=80;

    for(l=0; l<=6; l+=1)
        for(g=0; g<16; g+=1)
        laser[l].graph[g]=new_map(5,5,2,2,laser[l].col+g);
        end
    end
end


function show_data()
private
dy=20;
begin
y=20;

write(0,100,y,0,"CAMERA");
write(0,300,y,0,"MODE 7");
y+=dy;
write(0,200,y,0,"height");
write_int(0,100,y,0,&cam.height);
write_int(0,300,y,0,&m7.height);

y+=dy;
write(0,200,y,0,"angle");
write_int(0,100,y,0,&cam.angle);
write_int(0,300,y,0,&m7.camera.angle);


y+=dy;
write(0,200,y,0,"distance");
write_int(0,100,y,0,&cam.distance);
write_int(0,300,y,0,&m7.distance);

y+=dy;
write(0,200,y,0,"focus");
write_int(0,100,y,0,&cam.depth);
write_int(0,300,y,0,&m7.focus);

y+=dy;
write(0,200,y,0,"horizon");
write_int(0,100,y,0,&cam.cy);
write_int(0,300,y,0,&m7.horizon);


end

//--------------------------------------------------------------------

process marker(x,y,col)


begin

ctype=c_m7;
graph=new_map(11,11,5,5,col+7);
    loop
  draw_m7line(x,father.height-200,y,x,father.height+200,y,col+7);
        frame;
    end


end

//--------------------------------------------------------------------
process init_mode7_layer()
private
df; dh; dd;
i;
begin
ctype=c_m7;



start_mode7(0,fpg,m7map,0,0,cam.h);
m7.color=0;
m7.camera=id;
tree_id=tree(roomsize/2,roomsize/2);


m7.distance=cam.distance;
m7.height=cam.height;
m7.focus=cam.depth;
m7.horizon=cam.cy;

loop
x=cam.fx;
y=cam.fz;
angle=cam.angle+90000;
//m7.distance+=(1+9*key(_space))*(key(_s)-key(_w));
//m7.focus+=(1+9*key(_space))*(key(_2)-key(_1));

m7.height=1600*cam.height/1000;

m7.focus=cam.depth;
m7.distance=cam.distance;
m7.horizon=cam.cy;
m7.z=-100;
frame;
end
end


//--------------------------------------------------------------------
process star(x,y,height)
private
points=7;
p;
len;
dl;
px; py;
i;
pen;
sz=256;
col;
cr; cg; cb;
struct point[7]
x,y,
angle;
next;
last;
end
rad;
fire;

 firer; fireg; fireb;
dot_id;

q_,w_, e_;  sp_;
begin
rad=90;
pen=new_map(5,5,2,2,255);
graph=new_map(sz,sz,sz/2,sz-1,0);
size=50;
ctype=c_m7;
cnumber=0;
point.x=sz/2;
point.y=sz/2;
  for(i=1; i<=7; i+=1)
     point[i].angle=90000+i*360000/7;
     point[i].next=(i+2);
     if(point[i].next>7)
        point[i].next-=7;
     end
     point[i].last=(i+3);
     if(point[i].last>7)
        point[i].last-=7;
     end


     point[i].x=point.x+get_distx(point[i].angle,rad);
     point[i].y=point.y+get_disty(point[i].angle,rad);

  end
  for(i=1; i<=7; i+=1)
    len=fget_dist(point[i].x,point[i].y,point[point[i].next].x,point[point[i].next].y);
    for(dl=0; dl<=len; dl+=1)
      px=point[i].x+dl*(point[point[i].next].x-point[i].x)/len;
      py=point[i].y+dl*(point[point[i].next].y-point[i].y)/len;
      map_put(0,graph,pen,px,py);
    end

    len=fget_dist(point[i].x,point[i].y,point[point[i].last].x,point[point[i].last].y);
    for(dl=0; dl<=len; dl+=1)
      px=point[i].x+dl*(point[point[i].last].x-point[i].x)/len;
      py=point[i].y+dl*(point[point[i].last].y-point[i].y)/len;
      map_put(0,graph,pen,px,py);
    end

      frame;

  end

dot_id=laserdot();
    loop


        firer=0;
        fireg=0;
        fireb=0;
        if(cr<60) cr+=3; end
        if(cg<60) cg+=3; end
        if(cb<60) cb+=3; end

      //  if(key(_q) and cr==60) cr-=57;  firer=1; fire=1; end
      //  if(key(_w) and cg==60) cg-=57;  fireg=1; fire=1; end
      //  if(key(_e) and cb==60) cb-=57;  fireb=1; fire=1;   end

      //  if(fire==1)



          if(key(_q))
              firer=1;
          else

          end
          if(key(_w))
              fireg=1;
          end
          if(key(_e))
              fireb=1;
          end

            if(firer)
                if(fireg)
                    if(fireb)
                        fire=7;
                    else
                        fire=2;
                    end
                else
                    if(fireb)
                        fire=6;
                    else
                        fire=1;
                    end
                end

            else
                if(fireg)
                    if(fireb)
                        fire=4;
                    else
                    fire=3;
                    end
                else
                    if(fireb)
                       fire=5;
                    else
                    fire=0;
                    end
                end

            end


           col=laser[fire mod 7].col+8;
           ray.col=fire mod 7;
           ray.fire=fire;
           //ray.col_id=col_id[ray.col];
           ray.col_id=col_id[ray.col];
        if(mouse.left)
            if(sp_==0)
                if(fire>0)
                fire_laser=true;
                end
                sp_=1;
            end
        else
        sp_=0;
        end

         if(firer==1) cr=40; else cr=60; end
         if(fireg==1) cg=40; else cg=60; end
         if(fireb==1) cb=40; else cb=60; end


         set_color( 255,cr+rand(-3,3),cg+rand(-3,3),cb+rand(-3,3));

        frame;
    end
end

//--------------------------------------------------------------------
process laserdot();

private
lh;
hitx;
hity;
hith;
stps;
stp;
dl;
ddl;
dx; dh; dy;
x0; y0; h0;
length;
pos;
raysize;
t;
//pitch;
begin
    ctype=c_m7;
    x=father.x;
    y=father.y;
    height=father.height+60;
    x0=father.x;
    y0=father.y;
    h0=height;
    length=roomsize;
    stps=100;
    dl=length/stps;
    ddl=dl/stps;

        yaw=0;
        mvx=0;
        mvy=0;
        pitch=0;
        spot_id=grounddot();
    loop


            if(not mouse.right) yaw-=200*mvx; end
            if(yaw>60000) yaw=60000; end
            if(yaw<-60000) yaw=-60000; end
            if(abs(yaw)==60000 or mouse.right)
                cam_id.angle+=200*mvx;

            end

            if(mouse.middle)
             cam_id.angle-=yaw/5;
             yaw-=yaw/5;

            end

        height=h0+get_disty(pitch,length);
        lh=get_distx(pitch,length);
        x=father.x+get_distx(cam.angle+90000+yaw,lh);         // was length for lh
        y=father.y+get_disty(cam.angle+90000+yaw,lh);


        ray.x=x; ray.y=y; ray.h=height;
        ray.x0=x0; ray.y0=y0; ray.h0=h0;
        ray.length=length;
        ray.angle=cam_id.angle-yaw;
        ray.dangle=ray.angle-ray.angle0;


        ray.xl=father.x+get_distx(cam.angle+90000+yaw,roomsize/2);         // was length for lh
        ray.yl=father.y+get_disty(cam.angle+90000+yaw,roomsize/2);         // was length for lh


        if(ray.hit==0)
        dx=ray.x;
        dy=ray.y; dh=ray.h;
        else
            dx=ray.x0+(ray.hitlength)*(ray.x-ray.x0)/ray.length;
            dy=ray.y0+(ray.hitlength)*(ray.y-ray.y0)/ray.length;
            dh=ray.h0+(ray.hitlength)*(ray.h-ray.h0)/ray.length;

        end

                t-=1;
                if(t<0)
                t=10;
                end

             if(fire_laser==0) raysize=0;

             else
             raysize=1;

             fire_laser=false;
             end

        if(ray.fire>0)
            for(stp=0; stp<=stps; stp+=1)


                if(raysize==0 and rand(0,10)<9)
                    continue;
                end

                dx=x0+stp*dl*(x-x0)/length;
                dy=y0+stp*dl*(y-y0)/length;
                dh=h0+stp*dl*(height-h0)/length;
                if(dh<0 or dx<0 or dy<0 or dx>roomsize or dy>roomsize)
                break;
                end
                draw_m7point(3,dx,dh,dy,ray.col_id+4,raysize);
                   if(stp*dl>=ray.hitlength)
                    break;

                end

                if(raysize==1)
                    laser_particle(dx,dy,dh);
                end
            end
        end

        ray.hit=0;
        ray.hitlength=ray.length;

        frame;
        ray.angle0=ray.angle;
    end
end

//--------------------------------------------------------------------



process laser_particle(x,y,height)
private
graph0;
life=4;
txt;
begin
 size=500;
graph0=laser[ray.col].graph[1];
graph=graph0;
ctype=c_m7;
cnumber=c_0;

    while(life>0)
        life-=1;
        graph=laser[ray.col].graph[4*life];
        graph=vox[col_id[ray.col]];
    frame;
    end
end


//--------------------------------------------------------------------



process tree(x,y);
  private
  h;   b;   l;
  bob;
begin
ctype=c_m7;
graph=1;
size=140;
cnumber=c_0;

FOR(angle=0; angle<360000*60; angle+=225500)
h=120+angle/32000;
b+=1;
l=(1200-h)/100;
branch(x,y,h,ANGLE,l,b);
end
star(x,y,h);
loop
x=father.x;
y=father.y;

if(key(_b))
bob=get_id(type bauble);
if(bob) bob.size=200; end
end

frame;
end
end

//--------------------------------------------------------------------
process branch(x,y,height,angle,l,b);
private i;
next_twig;
add_b;
add_c;
begin
    size=200-(height/5);
    for(i=1+(500-height)/250;i<=l; i+=1)
        next_twig=twig(x+get_distx(angle,(i)*(size/24)),y+get_disty(angle,(size/24)*i),height,i,b);
    end
    if(b mod 3==0)
       // add_b=1-add_b;
       // if(add_b==1)
            bauble(
                next_twig.x+get_distx(angle,2),
                next_twig.y+get_disty(angle,2),

            next_twig.height-10);
       // end
    else
        if(b mod 3==1)
            add_c=1-add_c;
            //if(add_c==1)
                candle(
                next_twig.x+get_distx(angle,2),
                next_twig.y+get_disty(angle,2),
                next_twig.height+10);
            //end
        end
        if(b mod 3==2)
                mushroom(
                next_twig.x+get_distx(angle,4),
                next_twig.y+get_disty(angle,4),
                next_twig.height);
         end

    end
loop
frame;
end
end

//--------------------------------------------------------------------
process twig(x,y,height,index,b);
private
graph0=16;
begin
ctype=c_m7;
cnumber=c_0;
if(b mod 2)
    graph=graph0+index/2;;
else
    graph=graph0+index;;

end
size=(300-10*index)/(1+height/400);
loop
if(size<50)
break;
end

frame;
end
end
//--------------------------------------------------------------------
process bauble(x,y,height)
private
vx; vh; vy;
begin
ctype=c_m7;
cnumber=c_0;
graph=rand(101,107);
size=(1800-height)/10;
loop
if(size==200)
vh=10;
size=300;
end
if(size==300)
height+=vh;
vh-=2;
if(height<0)
break;
end
end
frame;
end
end
//--------------------------------------------------------------------
process mushroom(x,y,height)

begin
ctype=c_m7;
cnumber=c_0;
graph=2;
 size=40;
loop
frame;
end
end
//--------------------------------------------------------------------


process update_cam()
private

odd;
begin
y=200;
ctype=c_m7;
angle=0000;
    //draw(1,4,15,0,0,cam.cy,scr_w,cam.cy);


    loop

    cam.cy+=(key(_ins)-key(_del));
    cam.angle=-angle;
    cam.posy+=(key(_pgup)-key(_pgdn));
    cam.height=cam.posy;
    cam.posx=cam.fx+get_disty(angle,cam.distance);
    cam.posz=cam.fz+get_distx(angle,cam.distance);
    cam.depth+=(1+9*key(_space))*(key(_home)-key(_end));
    odd=1-odd;
    mx[odd]=mouse.x;
    my[odd]=mouse.y;
    mvx=mx[odd]-mx[1-odd];
    mvy=my[odd]-my[1-odd];

   // angle+=200*mvx;

    pitch+=500*mvy;
    if(pitch>80000) pitch=80000; end
    if(pitch<-30000) pitch=-30000; end


        frame;

    mdx=scr_cx-mouse.x;
    mdy=scr_cy-mouse.y;
    mouse.x=scr_cx;
    mouse.y=scr_cy;
    mx[odd]+=mdx;
    my[odd]+=mdy;
    mx[1-odd]+=mdx;
    my[1-odd]+=mdy;

    end
end
//---------------------------------------------------------------------------------------


PROCESS groundplane()
private
I;
BEGIN
i=100;
LOOP


FRAME;
END
END




//-----------------------------------------------------------------------------------
function screenTransVert(x0,y0,z0,sx,sy)
 private tmp;
 fx;
 fy;
  BEGIN

        x= x0-cam.posX;
        y= y0-cam.posy;
        Z= z0-cam.posZ;

        tmp=(cos(cam.angle)*x-sin(cam.angle)*z)/1000;
        z=(cos(cam.angle)*z+sin(cam.angle)*x)/1000;
        x=tmp;


        if(z==0) z=1; END
        if(z>0) return(0); end
     // modev transformation
       // *sx=cam.cx-(x*cam.depth/z;
       // *sy=cam.cy+y*cam.depth/z;
     //mode7 transformations
        *sx=cam.cx-(x*cam.depth*scr_cx)/(z*160);
        *sy=cam.cy+y*cam.depth/z;

        return(1);

end
//--------------------------------------------------------------------------------




PROCESS draw_m7Line(x0,y0,z0,x1,y1,z1,col)
PRIVATE

k;

DS;
draw_edge;
px[1];
py[1];
pz[1];
draw_me;
struct cv[1]
x,y,z,
END;

sx[1];
sy[1];

tmp;
sx0; sy0;
sx1; sy1;
BEGIN
y0=y0*100/160;
y1=y1*100/160;

px[0]=x0; py[0]=y0; pz[0]=z0;
px[1]=x1; py[1]=y1; pz[1]=z1;

draw_me=screenTransVert(x0,y0,z0,&sx0,&sy0);
if(draw_me==1)
    draw_me=screenTransVert(x1,y1,z1,&sx1,&sy1);
    if(draw_me==1)

        draw_edge=draw(1,col,15,0,sx0,sy0,sx1,sy1);
    end
end
FRAME;




if(draw_edge<>0) delete_draw(draw_edge); end
END


//-----------------------------------------------------------------------------------------------------------

PROCESS draw_m7point(shape,x0,y0,z0,col,size)
PRIVATE
sx;
sy;
draw_point;
BEGIN
y0=y0*100/160;
screenTransVert(x0,y0,z0,&sx,&sy);
     draw_point=draw(shape,col,15,0,sx-size,sy-size,sx+size,sy+size);


FRAME;
DELETE_DRAW(draw_point);

END


