#include <stdio.h>
#include <dos.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <malloc.h>
#include <conio.h>
#include <stdlib.h>
#include <io.h>
#include <fcntl.h>
#include "3dproj.h"
#include "3ddefs.h"
#include "3dprep.h"
#include "3dvars.h"
#include "3dedit.h"
char filename[61];

void main(int argc, char *argv[] )
{
  char s1[61];
  int i,knownarg,loadit=0,depend=0;
  printf("%s",version);
  printf("%s",notice);
  sprintf(filename,"DEMO");
  for (i=1; i<argc; i++)
  {
    knownarg=0;
    argv[i] = strupr(argv[i]);
    if(strcmp(argv[i],"DEPEND")==0) { depend=1; knownarg=1;}
    if(knownarg==0)
    {
      sprintf(s1,argv[i]);
      sprintf(filename,argv[i]);
      loadit=1;
    }
  }

  thereisnosound=1;
  allocate_memory_for_everything();
  initialise_data();
  reset();
  for(i=0;i<49;i++) dpt[i]=1;
  apiclearcliparea();
  if(depend==1) for(i=0;i<49;i++) dpt[i]=0;
  if(loadit!=0) load(s1);
  editor(); //Sets terminateflag and missioncompleted
  deinitialise_data();
  free_memory_for_everything();
  printf("%s",version);
  printf("%s",notice);
}

void editor(void)
{
  int f,g;
  float distance1;
  float xold,yold;
  int target=0;
  float sx=0,sy=0,sz=0,rsx=0,rsy=0,rsz=0;
  float trsx,trsy,trsz;
  float xdir,ydir,zdir,tempx,tempy,tempz,zold;
  int totalo,totalco,totaloe,totalce;

  // Okay, time for Joe to attack the _____________________
  //                                  Bulk Mission Designer
/*
	Give a bunch of variables with which the missions can be designed.
	Randomize
int missiontype
	1)  Dog fight
	2)  Furball
	3)  Culling

	4)  Surgical Strike
	5)  Large Strike
	6)  Fleet Strike
	7)  Boarding Strike
	8)  Bombing
	9) Convoy raid

	10)  Ambush
	11) Escort
	12) Small Convoy
	13) Large Convoy
	14) Air Cover
	15) CAP
int mission subtype
	1) Thrust
	2) Feint
	3) Parry
	4) Probe
	5) Back peddle
	6) Run awwwaaaaayyyyy
	7) Reinforce

int localplanets
	1) Moon
	2) Earth
	3) Mars
	4) Jupiter
	5) Saturn
	6) Uranus
	7) Neptune

int asteroids (0-100)
	0-85  No
	86-99 Yes

int lowfriendlycapshipstrength
int highfriendlycapshipstrength
int lowenemycapshipstrength
int highenemycapshipstrength
	1) Pickets, Corvettes
	2) Frigates
	3) Destroyers
	4) Carriers
	5) Battleships

int lowfriendlyfighterstrength
int highfriendlyfighterstrength
int lowenemyfighterstrength
int highenemyfighterstrength
	1) light
	2) medium
	3) heavy
	4) assault
	4) bomber

float forcesdistance
int royalactivity (0-100)
int ucorpactivity (0-100)
int alienactivity (0-100)
	% chance each mission will have the above

Names.
BELT Fighter names
  Bravo
  Apple
  Bounder
  Strike
  March
  Avril
  Mohawk
  Tara
  Grouper
  Alpha Beta Gamma

UEA Fighter names
  Shark
  Viper
  Vixen
  Snake
  Hound
  Wolf
  Dragon
  One Two Three

ROYAL Fighter names
  Jehova
  Gareth
  Militant
  Paul

UCORP Fighter names
  F49 F3029 F109A


Capital ship names

*/

  for(f=0;f<49;f++) dpt[f]=1;
  o[0].target=-1;
  o[0].speed=0;
  statistics=0;
  o[0].rsx=o[0].rsy=o[0].rsz=0;
  camera=dddCreateCamera(96000,100*ar,100*ar);

// createship( 1,1,100, 0,50,3700, 0,25,0,0,0,0,0,0,0,  0,-1,0, 100,100,100,0,"K-NAW 1",1,-1, 49,0);
// target=1;
// createground(2,50);

  while (!key[_ESC])              // The main program loop
  {
    totalo=0; totalco=0; totaloe=0; totalce=0;
    dddCls();
    dddZBCls();
    dddClearObjects();
    trsx=o[0].rsx;
    trsy=o[0].rsy;
    trsz=o[0].rsz;
    o[0].rsx=rsx;
    o[0].rsy=rsy;
	 o[0].rsz=rsz;
    o[0].target=target;

    apiclearcliparea();
    apiclearobjects();
    drawstarfield(o[0].rsx,o[0].rsy,o[0].rsz);
    drawplanets();
    if(ground>0)
    {
      for(f=0;f<9;f++)
      {
        gr[f].cy+=sy;
        gr[f].cx+=sx;
        gr[f].cz+=sz;
      }
      drawground();
    }
    dddSelectCamera(camera);
    apidrawobjects();

    apiclearobjects();
    waypoints();

    o[0].rsx=trsx;
    o[0].rsy=trsy;
    o[0].rsz=trsz;
    o[0].target=-1;

    for (f=0; f<9; f++)
    {
      w[f].x+=sx;
      w[f].y+=sy;
      w[f].z+=sz;
    }
    dddInsertObject(light);
    dddSelectCamera(camera);
    dddDrawObjects();
    dddClearObjects();
//****************** Move your ships *******************
    if (!key[_L_SHIFT] && !key[_R_SHIFT])
    {
      rsx=rsy=rsz=0;
      if ((key[_C_DOWN])&&(sy<250)) sy=sy+1;
      else if ((key[_C_UP])&&(sy>-250)) sy=sy-1;
      else sy=sy/1.1;
      if ((key[_C_LEFT])&&(sx>-250)) sx=sx-1;
      else if ((key[_C_RIGHT])&&(sx<250)) sx=sx+1;
      else sx=sx/1.1;
      if ((key[_C_PGUP])&&(sz>-250)) sz=sz-1;
      else if ((key[_C_PGDN])&&(sz<250)) sz=sz+1;
      else sz=sz/1.1;
    }
    else
    {
      sx=sy=sz=0;
      if ((key[_C_UP])&&(sy<5)) rsx=rsx+.01;
      else if ((key[_C_DOWN])&&(sy>-5)) rsx=rsx-.01;
      else rsx=rsx/1.1;
      if ((key[_C_LEFT])&&(sx>-5)) rsy=rsy-.01;
      else if (key[_C_RIGHT]&&(sx<5)) rsy=rsy+.01;
      else rsy=rsy/1.1;
      if ((key[_C_PGUP])&&(sx>-5)) rsz=rsz-.01;
      else if ((key[_C_PGDN])&&(sx<5)) rsz=rsz+.01;
      else rsz=rsz/1.1;
    }

    for (f=1; f<shapes; f++) if (ce[f].time>-1) totalce++;
    for (f=1; f<ships; f++)
    {
      if (co[f].time>-1) totalco++;
      if (oe[f].time>-1) totaloe++;
      if (o[f].type>-1)
      {
        totalo++;
        if (o[f].target>-1) if ((o[o[f].target].type==-1)||(f==o[f].target))
        {
          o[f].target++;
          if (o[f].target>ships-1) o[f].target=-1;
        }
        if (o[f].wingleader>-1 && f==target &&
         (key[_F10] || key[_W] || key[_E] ||
          key[_A] || key[_S] || key[_D] ||
          key[_Z] || key[_X] || key[_C]))
        {
          apigameprint3(2 , 5 , 170 , "AUTO WINGMAN ADJUSTING");
          o[f].wingx = (-o[o[f].wingleader].cx + o[f].cx) ;
          o[f].wingy = (-o[o[f].wingleader].cy + o[f].cy) ;
          o[f].wingz = (-o[o[f].wingleader].cz + o[f].cz) ;
          if ((o[o[f].wingleader].type==-1)||(f==o[f].wingleader))
          {
            o[f].wingleader++;
            if (o[f].wingleader>ships-1) o[f].wingleader=-1;
          }
        }
        o[f].cy+=sy;
        o[f].cx+=sx;
        o[f].cz+=sz;
        spinobject(f, 0,0,0, rsx,rsy, rsz ,0 );
        distance1=pythag3d(o[f].cx,o[f].cy,o[f].cz)-pythag3d(viewx,viewy,viewz);
        if (distance1<0) distance1=-distance1;
        if (distance1>-1 && distance1<30000)
        {
          if((o[f].shape==99 || o[f].shape==100) && ground>0)
          {
            apiground(DRAWFULL,o[f].object, o[f].cx,o[f].cy,o[f].cz, o[f].rx,o[f].ry,o[f].rz);
          }
          else
            apishape(DRAWFULL,o[f].object,0, o[f].cx,o[f].cy,o[f].cz, o[f].rx,o[f].ry,o[f].rz);
          for (g=0; g<o[f].turrets; g++)
          {
            apiprojectile(DRAWFULL,o[f].t[g].object,o[f].t[g].type,
              o[f].cx+o[f].t[g].cx ,o[f].cy+o[f].t[g].cy ,o[f].cz+o[f].t[g].cz,
              o[f].t[g].rx,o[f].t[g].ry,o[f].t[g].rz );
            apiprojectile(DRAWFULL,o[f].t[g].gun,o[f].t[g].type,
              o[f].cx+o[f].t[g].cx ,o[f].cy+o[f].t[g].cy ,o[f].cz+o[f].t[g].cz-2,
              o[f].t[g].srx,o[f].t[g].sry,o[f].t[g].srz );
          }  // End of turrets
          for (g=0; g<o[f].thingys; g++)
          {
            if(o[f].thingytype[g]==1) dddRotateBy(o[f].thingy[g],0,100,0);
            else if(o[f].thingytype[g]==2) dddRotateBy(o[f].thingy[g],0,400,0);
            else if(o[f].thingytype[g]==3) dddRotateBy(o[f].thingy[g],100,0,0);
            else if(o[f].thingytype[g]==4) dddRotateBy(o[f].thingy[g],400,0,0);
            else if(o[f].thingytype[g]==11) dddRotateBy(o[f].thingy[g],0,0,10600);
            else if(o[f].thingytype[g]==12) dddRotateBy(o[f].thingy[g],0,0,-8400);
            else if(o[f].thingytype[g]==13) dddRotateBy(o[f].thingy[g],0,0,9400);
            else if(o[f].thingytype[g]>100 && o[f].thingytype[g]<200)
            {
              if(o[f].thingytype[g]==101) dddRotateBy(o[f].thingy[g],0,0,10600);
              if(o[f].thingytype[g]==102) dddRotateBy(o[f].thingy[g],0,0,-8400);
              if(o[f].thingytype[g]==103) dddRotateBy(o[f].thingy[g],0,0,9400);
              dddSetShapeFlags(o[f].thingy[g],2);
            }
          }
          for (g=0; g<o[f].hangerzones; g++) if(o[f].h[g].type==1)
          {
            apishape(DRAWFULL,o[f].h[g].object,0,
              o[f].cx+o[f].h[g].cx ,o[f].cy+o[f].h[g].cy ,o[f].cz+o[f].h[g].cz,
              o[f].h[g].rx,o[f].h[g].ry,o[f].h[g].rz );
          } // End of hangers
        }
      }
    }
    dddInsertObject(light);
    dddSelectCamera(camera);
    dddDrawObjects();
    dddClearObjects();

    for (f=1; f<ships; f++)
    {
      if (co[f].time>-1)
      {
        co[f].cy+=sy;
        co[f].cx+=sx;
        co[f].cz+=sz;
        spincreateobjects(f, 0,0,0, rsx,rsy,rsz );
        xdir=0; ydir=0; zdir=0;
        ydir=angle ( co[f].dx[0] , co[f].dz[0] );
        if ( (co[f].dz[0]<=0) && (co[f].dx[0]>0) ) ydir+=PI;
        if ( (co[f].dz[0]<=0) && (co[f].dx[0]<0) ) ydir-=PI;
        xdir=angle ( co[f].dy[0] ,
        sqrt(pow(co[f].dx[0],2)+pow(co[f].dz[0],2) ) );
        tempx=co[f].dx[2];
        tempy=co[f].dy[2];
        tempz=co[f].dz[2];
        // Rotate around y
        xold=tempx; zold=tempz;
        tempx=(xold*cos(ydir)-zold*sin(ydir));
        tempz=(xold*sin(ydir)+zold*cos(ydir));
        // rotate around x
        yold=tempy; zold=tempz;
        tempy=(yold*cos(xdir)-zold*sin(xdir));
        tempz=(yold*sin(xdir)+zold*cos(xdir));
        zdir=angle( tempy , sqrt( pow(tempx,2)+pow(tempz,2) ) ) ;
        ydir=ydir*10430; // Translate the radians into the dddware 'degrees' (0-65535=0-2pi)
        xdir=xdir*10430;
        zdir=-zdir*10430;
		  dddSetLineOnColor(2);
		  apishape(DRAWLINE,co[f].object,0,co[f].cx,co[f].cy,co[f].cz,xdir,ydir,zdir);
		}
	 }
	 dddInsertObject(light);
	 dddSelectCamera(camera);
	 dddDrawObjects();
	 dddClearObjects();

	 dddScreenCopy();
  }
}

void input(char *strout, int x, int y)
{
  char str[80],str2[80];
  int oldkey,f;
  oldkey=1;
  strcpy(str , "" );
  while (!key[_RETURN])
  {
    if (key[_A])
    {
      if (oldkey==0) strcat (str ,"A" ); oldkey=1;
    }
    else if (key[_B])
    {
      if (oldkey==0) strcat (str ,"B" );oldkey=1;
    }
    else if (key[_C])
    {
      if (oldkey==0) strcat (str ,"C" );oldkey=1;
    }
    else if (key[_D])
    {
      if (oldkey==0) strcat (str ,"D" );oldkey=1;
    }
    else if (key[_E])
	 {
      if (oldkey==0) strcat (str ,"E" );oldkey=1;
    }
    else if (key[_F])
    {
      if (oldkey==0) strcat (str ,"F" );oldkey=1;
    }
    else if (key[_G])
    {
      if (oldkey==0) strcat (str ,"G" );oldkey=1;
    }
    else if (key[_H])
    {
      if (oldkey==0) strcat (str ,"H" );oldkey=1;
    }
    else if (key[_I])
    {
      if (oldkey==0) strcat (str ,"I" );oldkey=1;
    }
    else if (key[_J])
    {
      if (oldkey==0) strcat (str ,"J" );oldkey=1;
    }
    else if (key[_K])
    {
      if (oldkey==0) strcat (str ,"K" );oldkey=1;
	 }
    else if (key[_L])
    {
      if (oldkey==0) strcat (str ,"L" );oldkey=1;
    }
    else if (key[_M])
    {
      if (oldkey==0) strcat (str ,"M" );oldkey=1;
    }
    else if (key[_N])
    {
      if (oldkey==0) strcat (str ,"N" );oldkey=1;
    }
    else if (key[_O])
    {
      if (oldkey==0) strcat (str ,"O" );oldkey=1;
    }
    else if (key[_P])
    {
      if (oldkey==0) strcat (str ,"P" );oldkey=1;
    }
    else if (key[_Q])
    {
      if (oldkey==0) strcat (str ,"Q" );oldkey=1;
    }
    else if (key[_R])
	 {
      if (oldkey==0) strcat (str ,"R" );oldkey=1;
    }
    else if (key[_S])
    {
      if (oldkey==0) strcat (str ,"S" );oldkey=1;
    }
    else if (key[_T])
    {
      if (oldkey==0) strcat (str ,"T" );oldkey=1;
    }
    else if (key[_U])
    {
      if (oldkey==0) strcat (str ,"U" );oldkey=1;
    }
    else if (key[_V])
    {
      if (oldkey==0) strcat (str ,"V" );oldkey=1;
    }
    else if (key[_W])
    {
      if (oldkey==0) strcat (str ,"W" );oldkey=1;
    }
    else if (key[_X])
    {
      if (oldkey==0) strcat (str ,"X" );oldkey=1;
	 }
    else if (key[_Y])
    {
      if (oldkey==0) strcat (str ,"Y" );oldkey=1;
    }
    else if (key[_Z])
    {
      if (oldkey==0) strcat (str ,"Z" );oldkey=1;
    }
    else if (key[_SPACEBAR])
    {
      if (oldkey==0) strcat (str ," "  );oldkey=1;
    }
    else if (key[_1])
    {
      if (oldkey==0) strcat (str ,"1" );oldkey=1;
    }
    else if (key[_2])
    {
      if (oldkey==0) strcat (str ,"2" );oldkey=1;
    }
    else if (key[_3])
    {
      if (oldkey==0) strcat (str ,"3" );oldkey=1;
    }
    else if (key[_4])
	 {
      if (oldkey==0) strcat (str ,"4" );oldkey=1;
    }
    else if (key[_5])
    {
      if (oldkey==0) strcat (str ,"5" );oldkey=1;
    }
    else if (key[_6])
    {
      if (oldkey==0) strcat (str ,"6" );oldkey=1;
    }
    else if (key[_7])
    {
      if (oldkey==0) strcat (str ,"7" );oldkey=1;
    }
    else if (key[_8])
    {
      if (oldkey==0) strcat (str ,"8" );oldkey=1;
    }
    else if (key[_9])
    {
      if (oldkey==0) strcat (str ,"9" );oldkey=1;
    }
    else if (key[_0])
    {
      if (oldkey==0) strcat (str ,"0" );oldkey=1;
	 }
    else if (key[_FULL_STOP])
    {
      if (oldkey==0) strcat (str ,"." );oldkey=1;
    }
    else if (key[_COMMA])
    {
		if (oldkey==0) strcat (str ,"," );oldkey=1;
    }
    else if (key[_SEMICOLON])
    {
      if (oldkey==0) strcat (str ,";" );oldkey=1;
    }
    else if (key[_BACKSPACE])
    {
      if (oldkey==0)
      {
        f=strlen(str)-1;
        str[f]='\0';
        oldkey=1;
      }
    }
    else oldkey=0;
    if (oldkey>0)
    {
      oldkey++;
		if (oldkey>10) oldkey=0;
    }
    dddInsertObject(light);
    dddSelectCamera(camera);
    apiclearcliparea();
    apiclearobjects();
    strcpy(str2 , "9) NAME        : ");
	  strcat( str2 , str );
    apigameprint3(144 , x , y, str2);
    apiscreenswap();
  }
  strcat(str,"\0");
  strcpy(strout , str );
}

void save(char filename[20])
{
  char file[30];
  char encrypt[30];
  int f,g,handle;

  sprintf(file,"%s.MSN",filename);
  handle=open(file,O_WRONLY|O_BINARY);
  if(handle==-1)
  {
    handle=creat(file,S_IWRITE);
	 handle=open(file,O_WRONLY|O_BINARY);
    if(handle==-1) crashout("failed to open mission file");
  }
  strcpy(encrypt,file);
  for(f=0;f<strlen(encrypt);f++)
  {
    encrypt[f]=encrypt[f]+f;
  }
  write(handle,&encrypt,sizeof(encrypt));

  write(handle,&norand,sizeof(norand));
  write(handle,&finishtime,sizeof(finishtime));
  write(handle,&startflag,sizeof(startflag));
  write(handle,&ground,sizeof(ground));
  for (f=0; f<9; f++)      write(handle,&w[f],sizeof(w[f]));
  for (f=0; f<ships; f++)  write(handle,&matter[f],sizeof(matter[f]));
  for (f=0; f<ships; f++)  write(handle,&oe[f],sizeof(oe[f]));
  for (f=0; f<ships; f++)  write(handle,&co[f],sizeof(co[f]));
  for (f=0; f<shapes; f++) write(handle,&ce[f],sizeof(ce[f]));
  for (f=shapes-10; f<shapes; f++) write(handle,&p[f],sizeof(p[f]));
  for (f=0; f<9; f++) write(handle,&gr[f],sizeof(gr[f]));
  for (f=0; f<ships; f++)
  {
    write(handle,&o[f].shape,sizeof(o[f].shape));
    write(handle,&o[f].type,sizeof(o[f].type));
    write(handle,&o[f].frame1,sizeof(o[f].frame1));
	 write(handle,&o[f].frame2,sizeof(o[f].frame2));
    write(handle,&o[f].frame3,sizeof(o[f].frame3));
    write(handle,&o[f].doing,sizeof(o[f].doing));
    write(handle,&o[f].smoking,sizeof(o[f].smoking));
    write(handle,&o[f].sparking,sizeof(o[f].sparking));
    write(handle,&o[f].side,sizeof(o[f].side));
    write(handle,&o[f].target,sizeof(o[f].target));
	 write(handle,&o[f].speed,sizeof(o[f].speed));
    write(handle,&o[f].topspeed,sizeof(o[f].topspeed));
    write(handle,&o[f].toprevs,sizeof(o[f].toprevs));
    write(handle,&o[f].currentrevs,sizeof(o[f].currentrevs));
    write(handle,&o[f].shields,sizeof(o[f].shields));
    write(handle,&o[f].maxshields,sizeof(o[f].maxshields));
    write(handle,&o[f].armour,sizeof(o[f].armour));
    write(handle,&o[f].maxarmour,sizeof(o[f].maxarmour));
    write(handle,&o[f].sparkx,sizeof(o[f].sparkx));
    write(handle,&o[f].sparky,sizeof(o[f].sparky));
    write(handle,&o[f].sparkz,sizeof(o[f].sparkz));
    write(handle,&o[f].cx,sizeof(o[f].cx));
    write(handle,&o[f].cy,sizeof(o[f].cy));
    write(handle,&o[f].cz,sizeof(o[f].cz));
    write(handle,&o[f].sx,sizeof(o[f].sx));
    write(handle,&o[f].sy,sizeof(o[f].sy));
    write(handle,&o[f].sz,sizeof(o[f].sz));
    write(handle,&o[f].dx[0],sizeof(o[f].dx[0]));
    write(handle,&o[f].dy[0],sizeof(o[f].dy[0]));
	 write(handle,&o[f].dz[0],sizeof(o[f].dz[0]));
    write(handle,&o[f].dx[1],sizeof(o[f].dx[1]));
    write(handle,&o[f].dy[1],sizeof(o[f].dy[1]));
    write(handle,&o[f].dz[1],sizeof(o[f].dz[1]));
    write(handle,&o[f].dx[2],sizeof(o[f].dx[2]));
    write(handle,&o[f].dy[2],sizeof(o[f].dy[2]));
    write(handle,&o[f].dz[2],sizeof(o[f].dz[2]));
	 write(handle,&o[f].rx,sizeof(o[f].rx));
    write(handle,&o[f].ry,sizeof(o[f].ry));
    write(handle,&o[f].rz,sizeof(o[f].rz));
    write(handle,&o[f].rsx,sizeof(o[f].rsx));
    write(handle,&o[f].rsy,sizeof(o[f].rsy));
    write(handle,&o[f].rsz,sizeof(o[f].rsz));
    write(handle,&o[f].turrets,sizeof(o[f].turrets));
    write(handle,&o[f].guns,sizeof(o[f].guns));
    write(handle,&o[f].crashzones,sizeof(o[f].crashzones));
    write(handle,&o[f].hangerzones,sizeof(o[f].hangerzones));
    write(handle,&o[f].stubborn,sizeof(o[f].stubborn));
    write(handle,&o[f].wingleader,sizeof(o[f].wingleader));
    write(handle,&o[f].wingtype,sizeof(o[f].wingtype));
    write(handle,&o[f].wingx,sizeof(o[f].wingx));
    write(handle,&o[f].wingy,sizeof(o[f].wingy));
    write(handle,&o[f].wingz,sizeof(o[f].wingz));
    write(handle,&o[f].destination,sizeof(o[f].destination));
    write(handle,&o[f].name,sizeof(o[f].name));
    write(handle,&o[f].rank,sizeof(o[f].rank));
	 write(handle,&o[f].mother,sizeof(o[f].mother));
    write(handle,&o[f].motherhangerzone,sizeof(o[f].motherhangerzone));
    if (o[f].type>-1)
    {
      if (o[f].turrets>0) for (g=0; g<o[f].turrets; g++)
      {
        write(handle,&o[f].t[g].shape,sizeof(o[f].t[g].shape));
		  write(handle,&o[f].t[g].type,sizeof(o[f].t[g].type));
        write(handle,&o[f].t[g].fireframe,sizeof(o[f].t[g].fireframe));
        write(handle,&o[f].t[g].target,sizeof(o[f].t[g].target));
        write(handle,&o[f].t[g].cx,sizeof(o[f].t[g].cx));
        write(handle,&o[f].t[g].cy,sizeof(o[f].t[g].cy));
        write(handle,&o[f].t[g].cz,sizeof(o[f].t[g].cz));
        write(handle,&o[f].t[g].dx[0],sizeof(o[f].t[g].dx[0]));
        write(handle,&o[f].t[g].dy[0],sizeof(o[f].t[g].dy[0]));
        write(handle,&o[f].t[g].dz[0],sizeof(o[f].t[g].dz[0]));
        write(handle,&o[f].t[g].dx[1],sizeof(o[f].t[g].dx[1]));
        write(handle,&o[f].t[g].dy[1],sizeof(o[f].t[g].dy[1]));
        write(handle,&o[f].t[g].dz[1],sizeof(o[f].t[g].dz[1]));
        write(handle,&o[f].t[g].dx[2],sizeof(o[f].t[g].dx[2]));
        write(handle,&o[f].t[g].dy[2],sizeof(o[f].t[g].dy[2]));
        write(handle,&o[f].t[g].dz[2],sizeof(o[f].t[g].dz[2]));
        write(handle,&o[f].t[g].rx,sizeof(o[f].t[g].rx));
        write(handle,&o[f].t[g].ry,sizeof(o[f].t[g].ry));
        write(handle,&o[f].t[g].rz,sizeof(o[f].t[g].rz));
        write(handle,&o[f].t[g].sx,sizeof(o[f].t[g].sx));
		  write(handle,&o[f].t[g].sy,sizeof(o[f].t[g].sy));
        write(handle,&o[f].t[g].sz,sizeof(o[f].t[g].sz));
        write(handle,&o[f].t[g].sx2,sizeof(o[f].t[g].sx2));
        write(handle,&o[f].t[g].sy2,sizeof(o[f].t[g].sy2));
        write(handle,&o[f].t[g].sz2,sizeof(o[f].t[g].sz2));
        write(handle,&o[f].t[g].srx,sizeof(o[f].t[g].srx));
        write(handle,&o[f].t[g].sry,sizeof(o[f].t[g].sry));
		  write(handle,&o[f].t[g].srz,sizeof(o[f].t[g].srz));
        write(handle,&o[f].t[g].hits,sizeof(o[f].t[g].hits));
        write(handle,&o[f].t[g].damage,sizeof(o[f].t[g].damage));
        write(handle,&o[f].t[g].deadframe,sizeof(o[f].t[g].deadframe));
      }
      if (o[f].guns>0) for (g=0; g<o[f].guns; g++)
      {
        write(handle,&o[f].g[g].type,sizeof(o[f].g[g].type));
        write(handle,&o[f].g[g].selected,sizeof(o[f].g[g].selected));
        write(handle,&o[f].g[g].fireframe,sizeof(o[f].g[g].fireframe));
        write(handle,&o[f].g[g].fireflag,sizeof(o[f].g[g].fireflag));
        write(handle,&o[f].g[g].left,sizeof(o[f].g[g].left));
        write(handle,&o[f].g[g].gun,sizeof(o[f].g[g].gun));
        write(handle,&o[f].g[g].gunz,sizeof(o[f].g[g].gunz));
        write(handle,&o[f].g[g].guny,sizeof(o[f].g[g].guny));
      }
      if (o[f].crashzones>0) for (g=0; g<o[f].crashzones; g++)
      {
        write(handle,&o[f].c[g].x,sizeof(o[f].c[g].x));
		  write(handle,&o[f].c[g].y,sizeof(o[f].c[g].y));
        write(handle,&o[f].c[g].z,sizeof(o[f].c[g].z));
        write(handle,&o[f].c[g].size,sizeof(o[f].c[g].size));
      }
      if (o[f].hangerzones>0) for (g=0; g<o[f].hangerzones; g++)
      {
        write(handle,&o[f].h[g].type,sizeof(o[f].h[g].type));
		  write(handle,&o[f].h[g].frame1,sizeof(o[f].h[g].frame1));
        write(handle,&o[f].h[g].cx,sizeof(o[f].h[g].cx));
        write(handle,&o[f].h[g].cy,sizeof(o[f].h[g].cy));
        write(handle,&o[f].h[g].cz,sizeof(o[f].h[g].cz));
        write(handle,&o[f].h[g].dx[0],sizeof(o[f].h[g].dx[0]));
        write(handle,&o[f].h[g].dy[0],sizeof(o[f].h[g].dy[0]));
        write(handle,&o[f].h[g].dz[0],sizeof(o[f].h[g].dz[0]));
        write(handle,&o[f].h[g].dx[1],sizeof(o[f].h[g].dx[1]));
        write(handle,&o[f].h[g].dy[1],sizeof(o[f].h[g].dy[1]));
        write(handle,&o[f].h[g].dz[1],sizeof(o[f].h[g].dz[1]));
        write(handle,&o[f].h[g].dx[2],sizeof(o[f].h[g].dx[2]));
        write(handle,&o[f].h[g].dy[2],sizeof(o[f].h[g].dy[2]));
        write(handle,&o[f].h[g].dz[2],sizeof(o[f].h[g].dz[2]));
        write(handle,&o[f].h[g].rx,sizeof(o[f].h[g].rx));
        write(handle,&o[f].h[g].ry,sizeof(o[f].h[g].ry));
        write(handle,&o[f].h[g].rz,sizeof(o[f].h[g].rz));
      }
    }
  }
  close(handle);
  apigameprint5(1,90,95,"S A V E D" );
  apiscreenswap();
  delay(200);
}


