//GLITCHES
//OCCASIONAL draw of objects with edges crossing the 0z coordinate when at higher abs x values


//TODO
//draw order



PROGRAM divbattlearenazone;


global
obj_count=0;
file0;
scr_w;
scr_h;
scr_cx;
scr_cy;

box_id[100];

tank_id[1];

IED;
TOWER;
SHELL[1];
TANK[1];
HULL[1];
TURRET[1];
SHAFT[1];
BARREL[1];

struct tank_data[1]
self;
part[6];
turret;
barrel;
hull;
shaft;
END


struct part[4]
Nverts;
struct vert[8]
x,y,z; xi,yl,zl;
END
struct edge[12]
v0;v1;col;
END
END

tank_draw[1];
radar_id[1];
tank_count;
box_count;


//----------------------------


struct cam[2]
distance;
posX;
posY;
posZ;
depth;
fov;
angle;
vx; vy; vz;
cx;
cy;
h,
w,
END


//----------------------
struct obj[100]
vertex_count ;
edge_count;
anglez;
anglex;
angley=0;
vel; vely;
col0;
struct vertex[100]
x,y,z;
xl, yl,zl;
sX,sy; sz;
END

struct edge[100]
v0;
v1;
col;
END

END


//-------


struct tmp_vertex[100]
x,y,z;

END


struct tmp_edge[100]
v0; v1;
END






LOCAL
c;

PRIVATE
i; j; x0; y0; z0;

struct vertex[40]
x,y,z;

END

struct edge[30]
v0; v1;
END



BEGIN
set_fps(60,0);
scr_w=1200;
scr_h=800;
//scr_w=1000;
//scr_h=600;

scr_cx=scr_w/2;
scr_cy=scr_h/2;
set_mode(scr_w*1000+scr_h);
file0=load_fpg("battlezone.FPG");
define_region(1, 0, 0, scr_w, scr_h/2);
define_region(2, 0, scr_h/2, scr_w, scr_h/2);

//camera_control();



for(i=0; i<3; i+=1)
cam[i].posX=0;
cam[i].posY=0;
cam[i].posZ=600;
cam[i].angle=0;
cam[i].cx=scr_cx;
cam[i].w=scr_w;
cam[i].h=scr_h/2;

cam[i].depth=600;  //Gives a FOV of 45^ for defined screen width  of1200

cam[i].fov=atan2(scr_cx,cam[i].depth);

END


cam[0].cx=50;
cam[0].cy=50;
cam[0].w=25;
cam[0].h=25;


cam[1].cy=scr_cy/2;
cam[2].cy=3*scr_cy/2;

camera_control();


moon(1,101);
moon(2,101);


radar_id[0]=radar(0);
radar_id[1]=radar(1);



draw(2,58,15,0,0,0,scr_w-1,scr_cy-1);
draw(2,24,15,0,0,scr_cy+1,scr_w-1,scr_h-1);

draw(1,34,15,0,0,cam[1].cy,scr_w,cam[1].cy);
draw(1,34,15,0,0,cam[2].cy,scr_w,cam[2].cy);






init_objects();


 write_int(0,10,10,0,&fps);
write_int(0,100,100,0,&cam[1].depth);
write_int(0,100,110,0,&cam[1].fov);

END


//============================================================================================
//============================================================================================


PROCESS init_objects()

BEGIN

  //TANKS MUSTCOME FIRST

 TAnk[0]=obj_count;
 init_tank(0,0,1000,0,_up,_down,_left,_right,_r_shift,56);
 TANK[1]=obj_Count;
 init_tank(0,0,-1000,180000,_w,_s,_a,_d,_tab,24);


  add_tank_parts(0);
  add_tank_parts(1);



init_gem(4,-200,0,0,86,40);

init_logo();
TOWER=obj_count;
init_box(100,200,300,0,0,0,0,10);

IED=obj_count;
init_box(100,100,100,600,2000,0,0,24);


SHELL[0]=obj_count;
init_box(5,5,5,100,400,100,0,56);
//TODO

//init_gunbarrel
//init flag
//init_radardish




update_objs();
END

//--------------------------------------------------------------------------------
PROCESS init_logo()
   PRIVATE
   parts[7];
  i;
BEGIN

 parts[0]=obj_count;
 init_prism(40,22,0,40,-300,0,10,5,0,0,0);
 parts[1]=obj_count;
 init_prism(41,16,0,40,-300,0,58,10,0,0,0);
 parts[2]=obj_count;
 init_prism(42,24,0,40,-300,0,58,10,0,0,0);
 parts[3]=obj_count;
 init_prism(43,22,0,40,-300,0,70,10,0,0,0);
 parts[4]=obj_count;
 init_prism(44,22,0,40,-300,0,70,10,0,0,0);
 parts[5]=obj_count;
 init_prism(45,22,0,40,-300,0,70,10,0,0,0);


LOOP
for(i=0; i<=5; i+=1)
rotate_obj(parts[i],0,1000,0);
END
FRAME;
END
END





//--------------------------------------------------------------------------------
process init_tank(x0,y0,z0,ang,up,dn,lf,rt,ctrl,col)

PRIVATE x0; y0; z0; i; j; k;  l;
verts;
edges;
g;
half_v;
v;
BEGIN
verts=11;
v=8;  //number of verts on draw edges
half_v=v/2;
edges=12;
graph=20;    g=graph;
getpoint(0,0,g);
getpoint(1,1,g);
getpoint(2,2,g);
getpoint(3,6,g);
getpoint(4,7,g);
getpoint(5,8,g);
getpoint(6,9,g);
getpoint(7,13,g);
getpoint(8,14,g);
getpoint(9,24,g);
getpoint(10,25,g);
getpoint(11,26,g);

For(i=1;i<=half_v; i+=1)
tmp_edge[i].v0=i; tmp_edge[i].v1=i+half_v;
tmp_edge[i+half_v].v0=i; tmp_edge[i+half_v].v1=i+1; IF(I==half_v) tmp_edge[i+half_v].v1=1;      END
tmp_edge[i+v].v0=i+half_v; tmp_edge[i+v].v1=i+half_v+1; IF(I==half_v) tmp_edge[i+v].v1=half_v+1;    END
END
build_object(obj_count,verts,edges,col);

tank_data[tank_count].self=tankobj(obj_count,verts,edges,x0,y0,z0,ang,up,dn,lf,rt,ctrl,col);
obj_count+=1;
tank_count+=1;



END




//--------------------------------------------------------------------------------


PROCESS getpoint(v,cp,graph)
PRIVATE
empty;
BEGIN
x=0; y=0;

get_real_point(cp,&tmp_vertex[v].z,&tmp_vertex[v].y);
tmp_vertex[v].y=-tmp_vertex[v].y;
graph+=1;
get_real_point(cp,&tmp_vertex[v].x,&empty);
END



//--------------------------------------------------------------------------------

PROCESS add_tank_parts(n)

PRIVATE x0; y0; z0; i; j; k;  l; ang; col=27;
verts;
edges;
g;
half_v;
v;
fix;
dx; dy; dz;
parent;
BEGIN


//-------------------------------------------


verts=8;
v=8;  //number of verts on draw edges
half_v=v/2;
edges=12;
graph=20;    g=graph;
getpoint(0,24,g);
getpoint(1,2,g);
getpoint(2,3,g);
getpoint(3,5,g);
getpoint(4,6,g);
getpoint(5,9,g);
getpoint(6,10,g);
getpoint(7,12,g);
getpoint(8,13,g);

dx=tmp_vertex[0].x;
dy=tmp_vertex[0].y;
dz=tmp_vertex[0].z;
for(i=0; i<=verts; i+=1)
tmp_vertex[i].x-=dx;
tmp_vertex[i].y-=dy;
tmp_vertex[i].z-=dz;

END

For(i=1;i<=half_v; i+=1)
tmp_edge[i].v0=i; tmp_edge[i].v1=i+half_v;
tmp_edge[i+half_v].v0=i; tmp_edge[i+half_v].v1=i+1; IF(I==half_v) tmp_edge[i+half_v].v1=1;      END
tmp_edge[i+v].v0=i+half_v; tmp_edge[i+v].v1=i+half_v+1; IF(I==half_v) tmp_edge[i+v].v1=half_v+1;    END
END
build_object(obj_count,verts,edges,col);

 fix=9; parent=n;
tank_data[n].hull=tank_part(obj_count,verts,edges,x0,y0,z0,ang,n,1,parent,fix,col);
HULL[n]=obj_count;
obj_count+=1;
//-------------------------------------------


verts=7;
v=6;;  //number of verts on draw edges
half_v=v/2;
edges=9;
graph=20;    g=graph;
getpoint(0,25,g);
getpoint(1,3,g);
getpoint(2,4,g);
getpoint(3,5,g);
getpoint(4,10,g);
getpoint(5,11,g);
getpoint(6,12,g);
getpoint(7,27,g);


x=tmp_vertex[0].x;
dy=tmp_vertex[0].y;
dz=tmp_vertex[0].z;
for(i=0; i<=verts; i+=1)
tmp_vertex[i].x-=dx;
tmp_vertex[i].y-=dy;
tmp_vertex[i].z-=dz;

END




For(i=1;i<=half_v; i+=1)
tmp_edge[i].v0=i; tmp_edge[i].v1=i+half_v;
tmp_edge[i+half_v].v0=i; tmp_edge[i+half_v].v1=i+1; IF(I==half_v) tmp_edge[i+half_v].v1=1;      END
tmp_edge[i+v].v0=i+half_v; tmp_edge[i+v].v1=i+half_v+1; IF(I==half_v) tmp_edge[i+v].v1=half_v+1;    END
END
build_object(obj_count,verts,edges,col);
 fix=10;  parent=n;
tank_data[n].turret=tank_part(obj_count,verts,edges,x0,y0,z0,ang,n,2,parent,fix,col);
TURRET[n]=obj_count;
obj_count+=1;
//-------------------------------------------

verts=2;
v=verts;;  //number of verts on draw edges
half_v=v/2;
edges=1;
graph=20;    g=graph;
getpoint(0,27,g);
getpoint(1,26,g);

x=tmp_vertex[0].x;
dy=tmp_vertex[0].y;
dz=tmp_vertex[0].z;
for(i=0; i<=verts; i+=1)
tmp_vertex[i].x-=dx;
tmp_vertex[i].y-=dy;
tmp_vertex[i].z-=dz;

END

tmp_edge[1].v0=0; tmp_edge[1].v1=1;

build_object(obj_count,verts,edges,col);
 fix=7;  parent=obj_count-1;
tank_data[n].shaft=tank_part(obj_count,verts,edges,x0,y0,z0,ang,n,3,parent,fix,col);
SHAFT[n]=obj_count;
obj_count+=1;



//-------------------------------------------

verts=8;
v=8;  //number of verts on draw edges
half_v=v/2;
edges=12;
graph=20;    g=graph;
getpoint(0,26,g);
getpoint(1,15,g);
getpoint(2,16,g);
getpoint(3,17,g);
getpoint(4,18,g);
getpoint(5,19,g);
getpoint(6,20,g);
getpoint(7,21,g);
getpoint(8,22,g);


x=tmp_vertex[0].x;
dy=tmp_vertex[0].y;
dz=tmp_vertex[0].z;
for(i=0; i<=verts; i+=1)
tmp_vertex[i].x-=dx;
tmp_vertex[i].y-=dy;
tmp_vertex[i].z-=dz;

END




For(i=1;i<=half_v; i+=1)
tmp_edge[i].v0=i; tmp_edge[i].v1=i+half_v;
tmp_edge[i+half_v].v0=i; tmp_edge[i+half_v].v1=i+1; IF(I==half_v) tmp_edge[i+half_v].v1=1;      END
tmp_edge[i+v].v0=i+half_v; tmp_edge[i+v].v1=i+half_v+1; IF(I==half_v) tmp_edge[i+v].v1=half_v+1;    END
END
build_object(obj_count,verts,edges,col);
 fix=1;  parent =obj_count-1;
tank_data[n].barrel=tank_part(obj_count,verts,edges,x0,y0,z0,ang,n,4,parent,fix,col);
BARREL[n]=obj_count;
obj_count+=1;





END






PROCESS init_box(w,d,h,x0,y0,z0,ang,col)
PRIVATE
i; j; k;  l;
verts;
edges;
 ang;
BEGIN




verts=8;
edges=12;

tmp_vertex[0].x=0: tmp_vertex[0].y=0; tmp_vertex[0].z=0;


tmp_vertex[1].x=-w: tmp_vertex[1].y=h; tmp_vertex[1].z=-d;
tmp_vertex[2].x=w: tmp_vertex[2].y=h; tmp_vertex[2].z=-d;
tmp_vertex[3].x=w: tmp_vertex[3].y=-h; tmp_vertex[3].z=-d;
tmp_vertex[4].x=-w: tmp_vertex[4].y=-h; tmp_vertex[4].z=-d;
tmp_vertex[5].x=-w: tmp_vertex[5].y=h; tmp_vertex[5].z=d;
tmp_vertex[6].x=w: tmp_vertex[6].y=h; tmp_vertex[6].z=d;
tmp_vertex[7].x=w: tmp_vertex[7].y=-h; tmp_vertex[7].z=d;
tmp_vertex[8].x=-w: tmp_vertex[8].y=-h; tmp_vertex[8].z=d;


tmp_edge[1].v0=1; tmp_edge[1].v1=2;
tmp_edge[2].v0=2; tmp_edge[2].v1=3;
tmp_edge[3].v0=3; tmp_edge[3].v1=4;
tmp_edge[4].v0=4; tmp_edge[4].v1=1;
tmp_edge[5].v0=5; tmp_edge[5].v1=6;
tmp_edge[6].v0=6; tmp_edge[6].v1=7;
tmp_edge[7].v0=7; tmp_edge[7].v1=8;
tmp_edge[8].v0=8; tmp_edge[8].v1=5;
tmp_edge[9].v0=1; tmp_edge[9].v1=5;
tmp_edge[10].v0=2; tmp_edge[10].v1=6;
tmp_edge[11].v0=3; tmp_edge[11].v1=7;
tmp_edge[12].v0=4; tmp_edge[12].v1=8;

  build_object(obj_count,verts,edges,col);
  box_id[box_count]=box(obj_count,verts,edges,x0,h,z0,ang,id,3,col);
obj_count+=1;
box_count+=1;



END //PROC

//---------------------------------------------------------------------------------------
PROCESS init_gem(order,x0,y0,z0,col,size)
PRIVATE
i; j; k;  l;
verts;
edges;
 ang;
BEGIN




verts=order*2;
edges=verts*2;

tmp_vertex[0].x=0: tmp_vertex[0].y=0; tmp_vertex[0].z=0;

For(i=1; i<=order; i+=1)
tmp_vertex[i].x=get_distx(i*360000/order,size); tmp_vertex[i+order].x=get_distx(i*360000/order,size/2);
tmp_vertex[i].y=size;  tmp_vertex[i+order].y=3*size/2;
tmp_vertex[i].z=get_disty(i*360000/order,size); tmp_vertex[i+order].z=get_disty(i*360000/order,size/2);
tmp_vertex[i].y=-tmp_vertex[i].y;
tmp_vertex[i+order].y=-tmp_vertex[i+order].y;
END

//For(i=0; i<=order; i+=1)
//tmp_vertex[i].y=-tmp_vertex[i].y;
//tmp_vertex[i+order].y=-tmp_vertex[i+order].y;
//END


for(i=1;i<=order; i+=1)
tmp_edge[i].v0=0; tmp_edge[i].v1=i;
j=i+order;
tmp_edge[j].v0=i; tmp_edge[j].v1=j;
k=i+order*2;
tmp_edge[k].v0=i; tmp_edge[k].v1=i+1;  if(i%order==0) tmp_edge[k].v1=i+1-order; END
l=i+order*3;
tmp_edge[l].v0=j; tmp_edge[l].v1=j+1;  if(i%order==0) tmp_edge[l].v1=j+1-order; END

END

  build_object(obj_count,verts,edges,col);

box_id[box_count]=draw_obj(obj_count,verts,edges,x0,3*size/2,z0,ang,id,3,90);
obj_count+=1;
box_count+=1;



END //PROC

//---------------------------------------------------------------------------------------

PRocess init_prism(graph,verts,x0,y0,z0,ang,col,depth,axis,incam,cp0)

PRIVATE x0; y0; z0; i; j; k;  l;   v;
edges;
ang;

BEGIN




v=verts/2;
edges=verts+v;

get_real_point(0,&tmp_vertex[0].x,&tmp_vertex[0].y);
tmp_vertex[0].z=0;

if(axis==0)
For(i=0; i<=v; i+=1)
get_real_point(i,&tmp_vertex[i].x,&tmp_vertex[i].y);
END


For(i=1; i<=v; i+=1)
tmp_vertex[i].y=-tmp_vertex[i].y ;
tmp_vertex[i+v].x= tmp_vertex[i].x;
tmp_vertex[i+v].y =tmp_vertex[i].y;
tmp_vertex[i].z =depth; tmp_vertex[i+v].z=-depth;

END

ELSE
For(i=cp0; i<=v; i+=1)
get_real_point(i,&tmp_vertex[i].x,&tmp_vertex[i].z);
END
tmp_vertex[0].y=0;
For(i=1; i<=v; i+=1)
tmp_vertex[i].y=-tmp_vertex[i].y ;

tmp_vertex[i+v].x= tmp_vertex[i].x;
tmp_vertex[i+v].z =tmp_vertex[i].z;
tmp_vertex[i].y =depth; tmp_vertex[i+v].y=-depth;

END




END

FOR(i=1; i<=v; i+=1)
tmp_edge[i].v0=i; tmp_edge[i].v1=i+1;  if(i==v) tmp_edge[i].v1-=v; END
tmp_edge[i+v].v0=i; tmp_edge[i+v].v1=i+v;
tmp_edge[i+verts].v0=i+v; tmp_edge[i+verts].v1=i+v+1;  if(i==v) tmp_edge[i+verts].v1-=v; END

END



build_object(obj_count,verts,edges,col);
box_id[box_count]=draw_obj(obj_count,verts,edges,x0,y0,z0,ang,id,incam,24);
obj_count+=1;
box_count+=1;

END  //proc


//---------------------------------------------------------------------------------------------------------------------



//---------------------------------------------------------------------------------------------------------------------


//CODE TO DRAW 3 PLANAR VIEWS OF OBJ BEING CONSTRUCTED TO CHECK IT'S CORRECT
/*

//process draw_plans(GETPARAMETERS HERE)

for(i=1; i<=edges; i+=1)
x0=150; y0= 420;
draw(1,54,8,0,
                x0+obj[obj_count].vertex[obj[obj_count].edge[i].v0].x,y0+obj[obj_count].vertex[obj[obj_count].edge[i].v0].z,
                x0+obj[obj_count].vertex[obj[obj_count].edge[i].v1].x,y0+obj[obj_count].vertex[obj[obj_count].edge[i].v1].z);

x0=380; y0= 420;
draw(1,22,8,0,
                x0+obj[obj_count].vertex[obj[obj_count].edge[i].v0].y,y0+obj[obj_count].vertex[obj[obj_count].edge[i].v0].z,
                x0+obj[obj_count].vertex[obj[obj_count].edge[i].v1].y,y0+obj[obj_count].vertex[obj[obj_count].edge[i].v1].z);


x0=150; y0= 600;
draw(1,38,8,0,
                x0+obj[obj_count].vertex[obj[obj_count].edge[i].v0].x,y0+obj[obj_count].vertex[obj[obj_count].edge[i].v0].y,
                x0+obj[obj_count].vertex[obj[obj_count].edge[i].v1].x,y0+obj[obj_count].vertex[obj[obj_count].edge[i].v1].y);


END

//--------------------------------------------------
//CHECK VERTS ARE CORRECT
//put_pixel(vertex[i].x+150,vertex[i].z+420,63);
//put_pixel(vertex[i].y+380,vertex[i].z+420,31);
//put_pixel(vertex[i].x+150,vertex[i].y+600,47);
//--------------------------------------------------




END


 */

//--------------------------------------------------------------------------------


//--------------------------------------------------------------------------------
PROCESS update_objs();
PRIVATE
vel;
BEGIN

update_shell(0);

LOOP


rotate_obj(TOWER,0,1000,0);





FRAME;
END
END


//---------------------------------------------------------------------------------------

PROCESS update_shell(n)
PRIVATE
status;
velh;
velv;
BEGIN
status=0;
loop
switch (status)
case 0:

obj[shell[n]].angley=obj[turret[n]].angley;
obj[shell[n]].anglex=obj[barrel[n]].anglex;
Position_obj(SHELL[n],obj[turret[n]].vertex[n].x,obj[turret[n]].vertex[n].y,obj[turret[n]].vertex[n].z);
rotate_obj(SHELL[n],0,0,0);


if(key(_space))
velh= get_distx(obj[shell[n]].anglex,80);
velv= -get_disty(obj[shell[n]].anglex,80);
status=1; END
END
case 1:
move_objxz(shell[n],velh);
move_objy(shell[n],velv);
if(obj[shell[n]].vertex[0].y>0)
velv-=1;
ELSE
velv=-velv/2;
if(velv==0) status=0; END
END

if(key(_control)) status=0; END
END
default:
END

END




frame;
END
END

//---------------------------------------------------------------------------------------
PROCESS rotate_obj(n,angX, angy, angz)
PRIVATE i;
dh;

tmp;
BEGIN





obj[n].anglex+=angx;
obj[n].angley+=angy;
obj[n].anglez+=angz;


for( i=1; i<=obj[n].vertex_count; i+=1)

obj[n].vertex[i].y=get_distx(obj[n].anglex,obj[n].vertex[i].yl)
                        +get_disty(obj[n].anglex,obj[n].vertex[i].zl);

obj[n].vertex[i].z=get_distx(obj[n].anglex,obj[n].vertex[i].zl)
                        -get_disty(obj[n].anglex,obj[n].vertex[i].yl);



obj[n].vertex[i].x=obj[n].vertex[i].xl;

END






for( i=1; i<=obj[n].vertex_count; i+=1)

tmp=get_distx(obj[n].angley,obj[n].vertex[i].x)
                        -get_disty(obj[n].angley,obj[n].vertex[i].z);

obj[n].vertex[i].z=get_distx(obj[n].angley,obj[n].vertex[i].z)
                        +get_disty(obj[n].angley,obj[n].vertex[i].x);

obj[n].vertex[i].x=tmp:

//obj[n].vertex[i].y=obj[n].vertex[i].yl;

END



for( i=1; i<=obj[n].vertex_count; i+=1)
    obj[n].vertex[i].y+=obj[n].vertex[0].y;
    obj[n].vertex[i].x+=obj[n].vertex[0].x;
    obj[n].vertex[i].z+=obj[n].vertex[0].z;
END


if(obj[n].angley>180000) obj[n].angley-=360000; END
if(obj[n].angley<=-180000) obj[n].angley+=360000; END


END





//--------------------------------------------------------------------------------
PROCESS angle_obj(n,angX,angY,angZ)
PRIVATE
i;
BEGIN

obj[n].anglez=angz;


for( i=1; i<=obj[n].vertex_count; i+=1)

obj[n].vertex[i].x=get_distx(obj[n].angley,obj[n].vertex[i].xl)
                        -get_disty(obj[n].angley,obj[n].vertex[i].zl);

obj[n].vertex[i].z=get_distx(obj[n].angley,obj[n].vertex[i].zl)
                       +get_disty(obj[n].angley,obj[n].vertex[i].xl);


//obj[n].vertex[i].y=obj[n].vertex[i].yl;


obj[n].vertex[i].x+=obj[n].vertex[0].x;
obj[n].vertex[i].z+=obj[n].vertex[0].z;

END
END




//--------------------------------------------------------------------------------
PROCESS position_obj(n,posx,posy,posz)
private i;
BEGIN
FOR(I=1; I<=obj[n].vertex_count; i+=1)
obj[n].vertex[i].x+=posx-obj[n].vertex[0].x;
obj[n].vertex[i].y+=posy-obj[n].vertex[0].y;
obj[n].vertex[i].z+=posz-obj[n].vertex[0].z;

END
obj[n].vertex[0].x+=posx-obj[n].vertex[0].x;
obj[n].vertex[0].y+=posy-obj[n].vertex[0].y;
obj[n].vertex[0].z+=posz-obj[n].vertex[0].z;



END

//--------------------------------------------------------------------------------


PROCESS move_objxz(n,vel)
PRIVATE i;
BEGIN

obj[n].vel=vel;

FOR(I=0; I<=obj[n].vertex_count; i+=1)
obj[n].vertex[i].x+=get_disty(obj[n].angley,obj[n].vel);
obj[n].vertex[i].z-=get_distx(obj[n].angley,obj[n].vel);
END



END



//--------------------------------------------------------------------------------


PROCESS move_objy(n,vel)
PRIVATE i;
BEGIN

obj[n].vely=vel;

FOR(I=0; I<=obj[n].vertex_count; i+=1)
obj[n].vertex[i].y+=obj[n].vely;
END



END


//--------------------------------------------------------------------------------


PROCESS tankobj(index,verts,edges,x0,y0,z0,ang,up,dn,lf,rt,ctrl,col)
PRIVATE
i; n;  velx; velz;
tilt;
twist;
vel;
BEGIN
n=index;
obj[index].angley=ang;
draw_obj(index,verts,edges,x0,y0,z0,ang,id,2-index,col);


FOR(i=0; i<=verts; i+=1)
obj[n].vertex[i].x+=x0;
obj[n].vertex[i].y+=y0;
obj[n].vertex[i].z+=z0;

END

frame;
LOOP
 tilt=0; twist=0;  vel=0;  ang=0;
if(key(ctrl))
tilt=(key(up)-key(dn))*1000;
ELSE
//obj[n].vel=(key(up)-key(dn))*10;
vel=(key(up)-key(dn))*30;

END

tank_data[index].shaft.angle+=tilt;


//move_obj(n,obj[n].vel);
move_objxz(n,vel);
if(key(ctrl))
twist=(key(lf)-key(rt))*1000;
ELSE
ang= (key(lf)-key(rt))*3000;
//obj[n].angley+=ang;
rotate_obj(index,0,ang,0);
END
tank_data[index].turret.angle+=twist;



FRAME;
END

END




//--------------------------------------------------------------------------------
PROCESS tank_part(objNum,verts,edges,x0,y0,z0,ang,n,part,parent,fix,col)
private i;
turret_angle;
shaft_angle;
BEGIN
if(part<>3)
     draw_obj(objNum,verts,edges,x0,y0,z0,ang,id,2-n,90);
END
FOR(i=0; i<=verts; i+=1)
obj[objNum].vertex[i].x+=x0;
obj[objNum].vertex[i].y+=y0;
obj[objNum].vertex[i].z+=z0;

END


LOOP
obj[objnum].angley=obj[parent].angley;
obj[objnum].anglex=obj[parent].anglex;

position_obj(objNum,obj[parent].vertex[fix].x,obj[parent].vertex[fix].y,obj[parent].vertex[fix].z);

rotate_obj(objNum,0,0,0);
if(id==tank_data[n].turret)
rotate_obj(objNum,0,angle,0);
END

if(id==tank_data[n].shaft)
rotate_obj(objNum,angle,0,0);
END

x=obj[objNum].vertex[0].x;
y=obj[objNum].vertex[0].y;
z=obj[objNum].vertex[0].z;

FRAME;


END



END

//--------------------------------------------------------------------------------

//--------------------------------------------------------------------------------
PROCESS box(objNum,verts,edges,x0,h,z0,ang,id2,incam,col)

BEGIN

  draw_obj(objNum,verts,edges,x0,h,z0,ang,id2,3,90);
LOOP

//rotate_obj2(objNum,1000,0,0);
FRAME;
END

END





//--------------------------------------------------------------------------------
PROCESS moon(cam_id,graph)
BEGIN
LOOP
y=cam[cam_id].cy-scr_cy/4;

x=scr_cx-scr_cx*(cam[cam_id].angle)/cam[cam_id].fov;
FRAME:
END
END


//--------------------------------------------------------------------------------
PROCESS radar(index)
private r;
cvx; cvy; cvz;
blipx;
blipy;
n;
BEGIN
r=scr_cy/4;

x=scr_W-r; y=cam[index+1].cy+r;
draw(4,30,15,0,x-r,y-r,x+r,y+r);
draw(5,30,15,0,x-1,y-1,x+1,y+1);


LOOP frame; end

END



//--------------------------------------------------------------------------------
PROCESS camera_control();

private
move[2];
BEGIN

frame;
LOOP

//cam[1].depth+=key(_r_shift)-key(_control)*10;
//cam[2].depth=cam[1].depth;
for(c=1; c<=2; c+=1)

//cam[c].fov=atan2(scr_cx,cam[c].depth);





cam[c].angle=-obj[c-1].angley-tank_data[c-1].turret.angle;
cam[c].posX= obj[c-1].vertex[0].x-get_Disty(obj[c-1].angley,100);
cam[c].posY= obj[c-1].vertex[0].y+64;
cam[c].posZ= obj[c-1].vertex[0].z+get_Distx(obj[c-1].angley,100);

end


FRAME;
END
END


//--------------------------------------------------------------------------------


//--------------------------------------------------------------------------------

PROCESS build_object(obj_count,verts,edges,col);
PRIVATE
i;
BEGIN
obj[obj_count].vertex_count=verts;
for(i=0; i<=verts; i+=1)
obj[obj_count].vertex[i].x=tmp_vertex[i].x;
obj[obj_count].vertex[i].y=tmp_vertex[i].y;
obj[obj_count].vertex[i].z=tmp_vertex[i].z;

    obj[obj_count].vertex[i].xl=tmp_vertex[i].x;
    obj[obj_count].vertex[i].yl=tmp_vertex[i].y;
    obj[obj_count].vertex[i].zl=tmp_vertex[i].z;
END


obj[obj_count].edge_count=edges;
for(i=1; i<=edges; i+=1)
    obj[obj_count].edge[i].v0=tmp_edge[i].v0;
    obj[obj_count].edge[i].v1=tmp_edge[i].v1;
     obj[obj_count].edge[i].col=col;

END

END








//--------------------------------------------------------------------------------
process draw_obj(n,verts,edges,x0,y0,z0,ang,id2,incam,col)

/*
ANG parameter does nothing to change angles of objects create a controllng process)
*/
PRIVATE
 i;j; k;  Xs; Xw; ds; dw;   Ys[2]; yW;   cvx; cvy; cvz;
draw_edge[200];     tmp;      draw_blip;   blipx[1]; blipy[1];
col;    draw_me[2];        clip=-150;
BEGIN

obj[n].vertex[0].x=x0;
obj[n].vertex[0].y=y0;
obj[n].vertex[0].z=z0;
for(i=1; i<=verts; i+=1)
obj[n].vertex[i].x=x0+obj[n].vertex[i].xl;
obj[n].vertex[i].y=y0+obj[n].vertex[i].yl;
obj[n].vertex[i].z=z0+obj[n].vertex[i].zl;


END

LOOP





IF(incam==3)
for (k=1; k<=2; k+=1)
DS=cam[k].depth;

FOR(i=0; i<=verts; i+=1)
 cvx=obj[n].vertex[i].x-cam[k].posX;
cvy=obj[n].vertex[i].y-cam[k].posy;
cvZ=obj[n].vertex[i].z-cam[k].posZ;

tmp=(cos(cam[k].angle)*cvx+sin(cam[k].angle)*cvz)/1000;
cvz=(cos(cam[k].angle)*cvz-sin(cam[k].angle)*cvX)/1000;
cvx=tmp;
if(cvz==0)cvz=1; END
Xw=cvx;
Xs=cam[k].cx-cvx*DS/cvz;
Yw=cvy;
Ys=cam[k].cy+cvy*DS/cvz;

obj[n].vertex[i].sx=Xs;
obj[n].vertex[i].sy=Ys;

END

draw_me[k]=false;
if(cvz<clip)
draw_me[k]=true;

for(c=1; c<=edges; c+=1)

if (obj[n].edge[c].col<>0) col=obj[n].edge[c].col; END


draw_edge[c+edges*(k-1)]=draw(1,col+rand(0,3),15,k,
             obj[n].vertex[obj[n].edge[c].v0].sx,
                           obj[n].vertex[obj[n].edge[c].v0].sy,
                           obj[n].vertex[obj[n].edge[c].v1].sx,
                           obj[n].vertex[obj[n].edge[c].v1].sy);

END
END

IF(incam<>0)
blipx[k-1]=radar_id[k-1].x+cvx/40;
blipy[k-1]=radar_id[k-1].y+cvz/40;

blip(blipx,blipy,col);
blip(blipx[1],blipy[1],col);
END

END


ELSE
DS=cam[incam].depth;
FOR(i=0; i<=verts; i+=1)
 cvx=obj[n].vertex[i].x-cam[incam].posX;
cvy=obj[n].vertex[i].y-cam[incam].posy;
cvZ=obj[n].vertex[i].z-cam[incam].posZ;

tmp=(cos(cam[incam].angle)*cvx+sin(cam[incam].angle)*cvz)/1000;
cvz=(cos(cam[incam].angle)*cvz-sin(cam[incam].angle)*cvX)/1000;
cvx=tmp;
//tmp=DS/cvz;
if(cvz==0)cvz=1; END
Xs=cam[incam].cx-cvx*Ds/cvz;
Ys=cam[incam].cy+cvy*Ds/cvz;

obj[n].vertex[i].sx=Xs;
obj[n].vertex[i].sy=Ys;

END


draw_me=false;
if( cvz<clip)
draw_me=true;
for(c=1; c<=edges; c+=1)


if (obj[n].edge[c].col<>0) col=obj[n].edge[c].col; END

draw_edge[c]=draw(1,col+rand(0,3),15,incam,

                           obj[n].vertex[obj[n].edge[c].v0].sx,
                           obj[n].vertex[obj[n].edge[c].v0].sy,
                           obj[n].vertex[obj[n].edge[c].v1].sx,
                           obj[n].vertex[obj[n].edge[c].v1].sy);
END
END



if(incam<>0)
blipx=radar_id[incam-1].x+cvx/40;
blipy=radar_id[incam-1].y+cvz/40;
blip(blipx,blipy,col);
END

END

FRAME;
if(incam==3)
for(k=1; k<=2; k+=1)
if(draw_me[k]==True)
for(c=1; c<=edges; c+=1)
delete_draw(draw_edge[c+edges*(k-1)]);
END
END
END
else
if(draw_me==True)
for(c=1; c<=edges; c+=1)
delete_draw(draw_edge[c]);
END
END
END

END



END

//--------------------------------------------------------------------------------


//--------------------------------------------------------------------------------
  PROCESS blip(x,y,col)
     PRIVATE
     draw_id;
     r=2;
  BEGIN
       draw_id=draw(4,col,15,0,x-r,y-r,x+r,y+r);

  FRAME;

    delete_draw(draw_id);
  END
//--------------------------------------------------------------------------------
