#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,h;
  float distance1;
  float xold,yold;
  int target=0;
  float sx=0,sy=0,sz=0,rsx=0,rsy=0,rsz=0,tx,ty,tz;
  float sx2=0,sy2=0,sz2=0,rsx2=0,rsy2=0,rsz2=0;
  float trsx,trsy,trsz;
  char str[120],str2[120];
  int oldkey=0;
  int current=0;
  float xdir,ydir,zdir,tempx,tempy,tempz,zold;
  int totalo,totalco,totaloe,totalce;
  int m1=0;//m2=0,m3=0,m4=0,m5=0,m6=0,m7=0,m8=0;
  float x,y,z,rx,ry,rz;
  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(128000,100,100);

//  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();

//********************* OBJECTS **********************
    if (current==0)
    {
      if (!key[_S])
      {
        rsx2=rsy2=rsz2=0;
        if (key[_X] && sy<50) sy2=sy2+1;
        else if (key[_W] && sy2>-50) sy2=sy2-1;
        else sy2=sy2/1.1;
        if (key[_A] && sx>-50) sx2=sx2-1;
        else if (key[_D] && sx<50) sx2=sx2+1;
        else sx2=sx2/1.1;
        if (key[_C] && sx2>-50) sz2=sz2-1;
        else if (key[_E] && sx2<50) sz2=sz2+1;
        else sz2=sz2/1.1;
        o[target].cy+=sy2;
        o[target].cx+=sx2;
        o[target].cz+=sz2;
      }
      else
      {
        sx2=sy2=sz2=0;
        if (key[_W] && sy<5 ) rsx2=rsx2+.05;
        else if (key[_X] && sy>-5) rsx2=rsx2-.05;
        else rsx2=rsx2/1.1;
        if (key[_A] && sx>-5) rsy2=rsy2-.05;
        else if (key[_D] && sx<5) rsy2=rsy2+.05;
        else rsy2=rsy2/1.1;
        if (key[_C] && sx>-5) rsz2=rsz2-.05;
        else if (key[_E] && sx<5) rsz2=rsz2+.05;
        else rsz2=rsz2/1.1;
        spinobject(target,o[target].cx,o[target].cy,o[target].cz,rsx2,rsy2,rsz2,0);
      }
      if ((!key[_L_SHIFT])&&(!key[_R_SHIFT]))
      {
        if (key[_2]) //Shape 79 fuck it up
        {
          if (oldkey==0)
          {
            o[target].shape++;
            if (o[target].shape>100) o[target].shape=10;
//            if (o[target].shape==79) o[target].shape=80; //Crash prevention - joes bug. WILL BE REMOVED
            if (o[target].guns>0) { free( o[target].g ); o[target].guns=0; }
            if (o[target].turrets>0) { free( o[target].t ); o[target].turrets=0; }
            if (o[target].crashzones>0) { free( o[target].c ); o[target].crashzones=0; }
            if (o[target].hangerzones>0) { free( o[target].h ); o[target].hangerzones=0; }
            if (o[target].type>-1) createship(target,o[target].type,o[target].shape,
              o[target].cx,o[target].cy,o[target].cz, 0, 0 , 0,
              o[target].rsx, o[target].rsy, o[target].rsz, o[target].sx, o[target].sy, o[target].sz,
              o[target].stubborn, o[target].wingleader, o[target].wingtype,
              o[target].wingx,o[target].wingy,o[target].wingz,
              o[target].destination, o[target].name ,o[target].rank , o[target].target,o[target].mother,o[target].motherhangerzone );
          }
          oldkey++;
        }
        else if (key[_1])
        {
          if (oldkey==0) o[target].type++;
          if (o[target].guns>0) { free( o[target].g ); o[target].guns=0; }
          if (o[target].turrets>0) { free( o[target].t ); o[target].turrets=0; }
          if (o[target].crashzones>0) { free( o[target].c ); o[target].crashzones=0; }
          if (o[target].hangerzones>0) { free( o[target].h ); o[target].hangerzones=0; }
          if (o[target].type>-1) createship(target,o[target].type,o[target].shape,
            o[target].cx,o[target].cy,o[target].cz, 0, 0 , 0,
            o[target].rsx, o[target].rsy, o[target].rsz, o[target].sx, o[target].sy, o[target].sz,
            o[target].stubborn, o[target].wingleader, o[target].wingtype,
            o[target].wingx,o[target].wingy,o[target].wingz,
            o[target].destination, o[target].name ,o[target].rank , o[target].target,o[target].mother,o[target].motherhangerzone );
          if (o[target].type>7) o[target].type=-1;
          oldkey++;
        }
        else if (key[_3])
        {
          if (oldkey==0) o[target].side++;
          if (o[target].side>5) o[target].side=0;
          oldkey++;
        }
        else if (key[_4])
        {
          if (oldkey==0) o[target].target++;
          if (o[target].target>ships-1) o[target].target=-1;
          oldkey++;
        }
        else if (key[_5])
        {
          if (oldkey==0) o[target].stubborn++;
          if (o[target].stubborn>2) o[target].stubborn=0;
          oldkey++;
        }
        else if (key[_6])
        {
          if (oldkey==0) o[target].wingleader++;
          if (o[target].wingleader>ships-1) o[target].wingleader=-1;
          oldkey++;
        }
        else if (key[_7])
        {
          if (oldkey==0) o[target].wingtype++;
          if (o[target].wingtype>5) o[target].wingtype=0;
          oldkey++;
        }
        else if (key[_8])
        {
          if (oldkey==0) o[target].destination++;
          if (o[target].destination>9) o[target].destination=0;
          oldkey++;
        }
        else if (key[_0])
        {
          if (oldkey==0) o[target].rank++;
          if (o[target].rank>9) o[target].rank=0;
          oldkey++;
        }
        else if (key[_T])
        {
          if (oldkey==0) o[target].sx+=1;
          if (o[target].sx>200) o[target].sx=-200;
          oldkey+=15;
        }
        else if (key[_Y])
        {
          if (oldkey==0) o[target].sy+=1;
          if (o[target].sy>200) o[target].sy=-200;
          oldkey+=15;
        }
        else if (key[_U])
        {
          if (oldkey==0) o[target].sz+=1;
          if (o[target].sz>200) o[target].sz=-200;
          oldkey+=15;
        }
        else if (key[_G])
        {
          if (oldkey==0) o[target].rsx+=0.01;
          if (o[target].rsx>50) o[target].rsx=-50;
          oldkey+=15;
        }
        else if (key[_H])
        {
          if (oldkey==0) o[target].rsy+=0.01;
          if (o[target].rsy>50) o[target].rsy=-50;
          oldkey+=15;
        }
        else if (key[_J])
        {
          if (oldkey==0) o[target].rsz+=0.01;
          if (o[target].rsz>50) o[target].rsz=-50;
          oldkey+=15;
        }
        else if (key[_M])
        {
          if (oldkey==0) o[target].mother++;
          if (o[target].mother>ships-1) o[target].mother=-1;
          oldkey++;
        }
        else if (key[_N])
        {
          if (oldkey==0) o[target].motherhangerzone++;
//          if (o[target].motherhangerzone>o[o[target].mother].hangerzones-1) o[target].motherhangerzone=0;
          oldkey++;
        }
        else if (key[_B])
        {
          if (oldkey==0) o[target].dpt++;
          if (o[target].dpt>1) o[target].dpt=0;
          oldkey++;
        }
        else if (key[_EQUALS])
        {
          if (oldkey==0) target++;
          oldkey++;
          if (target>ships-1) target=0;
          if (target<0) target=ships-1;
        }
        else if (key[_MINUS])
        {
          if (oldkey==0) target--;
          oldkey++;
          if (target>ships-1) target=0;
          if (target<0) target=ships-1;
        }
        else
        {
          oldkey=0;
        }
      }
      else
      {
        if (key[_2])
        {
          if (oldkey==0)
          {
            o[target].shape--;
            if (o[target].shape<10) o[target].shape=100;
//            if (o[target].shape==79) o[target].shape=78; //Crash prevention - joes bug. WILL BE REMOVED
            if (o[target].guns>0) { free( o[target].g ); o[target].guns=0; }
            if (o[target].turrets>0) { free( o[target].t ); o[target].turrets=0; }
            if (o[target].crashzones>0) { free( o[target].c ); o[target].crashzones=0; }
            if (o[target].hangerzones>0) { free( o[target].h ); o[target].hangerzones=0; }
            if (o[target].type>-1) createship(target,o[target].type,o[target].shape,
              o[target].cx,o[target].cy,o[target].cz, 0,0,0,
              o[target].rsx, o[target].rsy, o[target].rsz, o[target].sx, o[target].sy, o[target].sz,
              o[target].stubborn, o[target].wingleader, o[target].wingtype,
              o[target].wingx,o[target].wingy,o[target].wingz,
              o[target].destination, o[target].name ,o[target].rank , o[target].target,o[target].mother,o[target].motherhangerzone );
          }
          oldkey++;
        }
        else if (key[_1])
        {
          if (oldkey==0) o[target].type--;
          if (o[target].type<-1) o[target].type=7;
          oldkey++;
          if (o[target].guns>0) { free( o[target].g ); o[target].guns=0; }
          if (o[target].turrets>0) { free( o[target].t ); o[target].turrets=0; }
          if (o[target].crashzones>0) { free( o[target].c ); o[target].crashzones=0; }
          if (o[target].hangerzones>0) { free( o[target].h ); o[target].hangerzones=0; }
          if (o[target].type>-1) createship(target,o[target].type,o[target].shape,
            o[target].cx,o[target].cy,o[target].cz, 0, 0 , 0,
            o[target].rsx, o[target].rsy, o[target].rsz, o[target].sx, o[target].sy, o[target].sz,
            o[target].stubborn, o[target].wingleader, o[target].wingtype,
            o[target].wingx,o[target].wingy,o[target].wingz,
            o[target].destination, o[target].name ,o[target].rank , o[target].target,o[target].mother,o[target].motherhangerzone );
        }
        else if (key[_3])
        {
          if (oldkey==0) o[target].side--;
          if (o[target].side<0) o[target].side=5;
          oldkey++;
        }
        else if (key[_4])
        {
          if (oldkey==0) o[target].target--;
          if (o[target].target<-1) o[target].target=ships-1;
          oldkey++;
        }
        else if (key[_5])
        {
          if (oldkey==0) o[target].stubborn--;
          if (o[target].stubborn<0) o[target].stubborn=2;
          oldkey++;
        }
        else if (key[_6])
        {
          if (oldkey==0) o[target].wingleader--;
          if (o[target].wingleader<-1) o[target].wingleader=ships-1;
          oldkey++;
        }
        else if (key[_7])
        {
          if (oldkey==0) o[target].wingtype--;
          if (o[target].wingtype<0) o[target].wingtype=5;
          oldkey++;
        }
        else if (key[_8])
        {
          if (oldkey==0) o[target].destination--;
          if (o[target].destination<0) o[target].destination=9;
          oldkey++;
        }
        else if (key[_0])
        {
          if (oldkey==0) o[target].rank--;
          if (o[target].rank<0) o[target].rank=8;
          oldkey++;
        }
        else if (key[_T])
        {
          if (oldkey==0) o[target].sx-=1;
          if (o[target].sx<-200) o[target].sx=200;
          oldkey+=15;
        }
        else if (key[_Y])
        {
          if (oldkey==0) o[target].sy-=1;
          if (o[target].sy<-200) o[target].sy=200;
          oldkey+=15;
        }
        else if (key[_U])
        {
          if (oldkey==0) o[target].sz-=1;
          if (o[target].sz<-200) o[target].sz=200;
          oldkey+=15;
        }
        else if (key[_G])
        {
          if (oldkey==0) o[target].rsx-=0.01;
          if (o[target].rsx<-50) o[target].rsx=50;
          oldkey+=15;
        }
        else if (key[_H])
        {
          if (oldkey==0) o[target].rsy-=0.01;
          if (o[target].rsy<-50) o[target].rsy=50;
          oldkey+=15;
        }
        else if (key[_J])
        {
          if (oldkey==0) o[target].rsz-=0.01;
          if (o[target].rsz<-50) o[target].rsz=50;
          oldkey+=15;
        }
        else if (key[_M])
        {
          if (oldkey==0) o[target].mother--;
          if (o[target].mother<0) o[target].mother=ships-1;
          oldkey++;
        }
        else if (key[_N])
        {
          if (oldkey==0) o[target].motherhangerzone--;
          if (o[target].motherhangerzone<0) o[target].motherhangerzone=o[o[target].mother].hangerzones-1;
          oldkey++;
        }
        else if (key[_B])
        {
          if (oldkey==0) o[target].dpt--;
          if (o[target].dpt<0) o[target].dpt=1;
          oldkey++;
        }
        else
        {
          oldkey=0;
        }
      }
      if (oldkey>0)
      {
        oldkey++; if (oldkey>10) oldkey=0;
      }
      if (key[_9]) input(&o[target].name,10,54);
      strcpy(str , "2) SHIP        : ");
      gcvt( target ,3 , str2);
      strcat(str , str2); strcat(str , " : " );
      strcat(str ,  shapedata[o[target].shape].name );
      apigameprint3(144 , 10 , 12 , str);
      strcpy(str , "1) TYPE        : ");
      if (o[target].type==0) strcat( str, "DERELICT" );
      else if (o[target].type==1) strcat( str, "NORMAL" );
      else if (o[target].type==2) strcat( str, "APPROXIMATE" );
      else if (o[target].type==3) strcat( str, "BROADSIDE" );
      else if (o[target].type==4) strcat( str, "AMBUSH" );
      else if (o[target].type==5) strcat( str, "NON SENTIENT;(AI FOR TURRETS)" );
      else if (o[target].type==6) strcat( str, "CONVOY" );
      else if (o[target].type==7) strcat( str, "DUMB CONVOY" );
      else strcat( str, "NOTHING" );
      apigameprint3(1 , 10 , 6 , str);
      if (o[target].type>-1)
      {
        strcpy(str , "3) ALLEGANCE   : ");
        if (o[target].side==0) strcat( str, "NEUTRAL" );
        else if (o[target].side==1) strcat( str, "BELT" );
        else if (o[target].side==2) strcat( str, "GOVERNMENT" );
        else if (o[target].side==4) strcat( str, "U_CORP" );
        else if (o[target].side==5) strcat( str, "ROYAL" );
        else strcat( str, "EXTRA SOLARIS" );
        apigameprint3(144 , 10 , 18 , str);
        strcpy(str , "4) TARGET      : ");
        if (o[target].target!=-1)
        {
          gcvt( o[target].target ,3 , str2);
          strcat(str , str2); strcat(str , " : " );
          strcat(str ,  shapedata[o[o[target].target].shape].name );
        }
        else strcat( str , "NONE");
        apigameprint3(144 , 10 , 24 , str);
        strcpy(str , "5) BASIC       : ");
        if (o[target].stubborn==0) strcat( str, "NORMAL, BORING" );
        else if (o[target].stubborn==1) strcat( str, "STUBBORN TARGETTER" );
        else if (o[target].stubborn==2) strcat( str, "PATHALOGICAL KILLER" );
        apigameprint3(144 , 10 , 30 , str);
        strcpy(str , "6) WINGLEADER  : ");
        if (o[target].wingleader!=-1)
        {
          gcvt( o[target].wingleader ,3 , str2);
          strcat(str , str2); strcat(str , " : " );
          strcat(str ,  shapedata[o[o[target].wingleader].shape].name );
        }
        else strcat( str , "NONE");
        apigameprint3(144 , 10 , 36 , str);
        strcpy(str , "7) SUBCONSIOUS : ");
        if (o[target].wingtype==0) strcat( str, "NORMAL, LOYAL" );
        else if (o[target].wingtype==1) strcat( str, "NORMAL, DISLOYAL" );
        else if (o[target].wingtype==2) strcat( str, "SLAVE TARGETTER" );
        else if (o[target].wingtype==3) strcat( str, "FREE SPIRIT, LOYAL" );
        else if (o[target].wingtype==4) strcat( str, "FREE SPIRIT, DISLOYAL" );
        else if (o[target].wingtype==5) strcat( str, "FORMATION ONLY" );
        apigameprint3(144 , 10 , 42 , str);
        strcpy(str , "8) DESTINATION : WAYPOINT");
        gcvt( o[target].destination ,3 , str2);
        strcat( str , str2 );
        apigameprint3(144 , 10 , 48 , str);
        strcpy(str , "9) NAME        : ");
        strcat( str , o[target].name );
        apigameprint3(144 , 10 , 54 , str);
        strcpy(str , "0) RANK        : ");
        if (o[target].rank==0) strcat( str, "2ND LT" );
        else if (o[target].rank==1) strcat( str, "1ST LT" );
        else if (o[target].rank==2) strcat( str, "CAPTAIN" );
        else if (o[target].rank==3) strcat( str, "MAJOR" );
        else if (o[target].rank==4) strcat( str, "LT CNL" );
        else if (o[target].rank==5) strcat( str, "CNL" );
        else if (o[target].rank==6) strcat( str, "ADMIRAL" );
        else if (o[target].rank==7) strcat( str, "GRAND ADMIRAL" );
        else strcat( str , "MINOR DEITY" );
        apigameprint3(144 , 10 , 60 , str);
        strcpy(str , "TURRETS        : ");
        if (o[target].turrets>0)
        {
          gcvt( o[target].turrets ,3 , str2);
          strcat (str , str2);
        }
        else strcat( str , "NONE");
        apigameprint3(144 , 10 , 66 , str);
        strcpy(str , "GUN MOUNTS     : ");
        if (o[target].guns>0)
        {
          gcvt( o[target].guns ,3 , str2);
          strcat (str , str2);
        }
        else strcat( str , "NONE");
        apigameprint3(144 , 10 , 72 , str);
        strcpy(str , "CRASH ZONES    : ");
        if (o[target].crashzones>0)
        {
          gcvt( o[target].crashzones ,3 , str2);
          strcat (str , str2);
        }
        else strcat( str , "NONE");
        apigameprint3(144 , 10 , 78 , str);
        strcpy(str , "HANGER ZONES   : ");
        if (o[target].hangerzones>0)
        {
          gcvt( o[target].hangerzones ,3 , str2);
          strcat (str , str2);
        }
        else strcat( str , "NONE");
        apigameprint3(144 , 10 , 84 , str);
        strcpy(str , "T) SX     : ");
        gcvt( o[target].sx ,5 , str2);
        strcat( str , str2 );
        apigameprint3(144 , 10 , 90 , str);
        strcpy(str , "Y) SY     : ");
        gcvt( o[target].sy ,5 , str2);
        strcat( str , str2 );
        apigameprint3(144 , 10 , 96 , str);
        strcpy(str , "U) SZ     : ");
        gcvt( o[target].sz ,5 , str2);
        strcat( str , str2 );
        apigameprint3(144 , 10 , 102 , str);
        strcpy(str , "G) RSX    : ");
        gcvt( o[target].rsx   ,5 , str2);
        strcat( str , str2 );
        apigameprint3(144 , 10 , 108 , str);
        strcpy(str , "H) RSY    : ");
        gcvt( o[target].rsy   ,5 , str2);
        strcat( str , str2 );
        apigameprint3(144 , 10 , 114 , str);
        strcpy(str , "J) RSZ    : ");
        gcvt( o[target].rsz   ,5 , str2);
        strcat( str , str2 );
        apigameprint3(144 , 10 , 120 , str);
        strcpy(str , "M) MOTHER : ");
        gcvt( o[target].mother   ,5 , str2);
        if (o[target].mother>-1)
        {
          gcvt( o[target].mother ,3 , str2);
          strcat(str , str2); strcat(str , " : " );
          strcat(str ,  shapedata[o[o[target].mother].shape].name );
        }
        else strcat( str , "NONE");
        apigameprint3(144 , 10 , 126 , str);
        if (o[target].mother>-1)
        {
          strcpy(str , "N) HANGER : ");
          gcvt( o[target].motherhangerzone   ,5 , str2);
          strcat( str , str2 );
          apigameprint3(144 , 10 , 132 , str);
        }
        strcpy(str , "B) DEPEND : ");
        if (o[target].dpt==1) strcat(str,"YES");
        else  strcat(str,"NO");
        apigameprint3(144 , 10 , 138 , str);
      }
      strcpy(str , "SHIP " );
      gcvt( target , 2 , str2 );
      strcat(str , str2);
      apigameprint3(1 , 5 , 194 , str);
    }

//********************* CREATE OBJECTS **********************F2
    else if (current==1)
    {
      if (!key[_S])
      {
        rsx2=rsy2=rsz2=0;
        if ((key[_X])&&(sy<50)) sy2=sy2+1;
        else if ((key[_W])&&(sy2>-50)) sy2=sy2-1;
        else sy2=sy2/1.1;
        if ((key[_A])&&(sx>-50)) sx2=sx2-1;
        else if ((key[_D])&&(sx<50)) sx2=sx2+1;
        else sx2=sx2/1.1;
        if ((key[_C])&&(sx2>-50)) sz2=sz2-1;
        else if ((key[_E])&&(sx2<50)) sz2=sz2+1;
        else sz2=sz2/1.1;
        co[target].cy+=sy2;
        co[target].cx+=sx2;
        co[target].cz+=sz2;
      }
      else
      {
        sx2=sy2=sz2=0;
        if ((key[_W])&&(sy<5)) rsx2=rsx2+.05;
        else if ((key[_X])&&(sy>-5)) rsx2=rsx2-.05;
        else rsx2=rsx2/1.1;
        if ((key[_A])&&(sx>-5)) rsy2=rsy2-.05;
        else if ((key[_D])&&(sx<5)) rsy2=rsy2+.05;
        else rsy2=rsy2/1.1;
        if ((key[_C])&&(sx>-5)) rsz2=rsz2-.05;
        else if ((key[_E])&&(sx<5)) rsz2=rsz2+.05;
        else rsz2=rsz2/1.1;
        spincreateobjects(target, co[target].cx,co[target].cy,co[target].cz, rsx2,rsy2, rsz2 );
      }
      if ((!key[_L_SHIFT])&&(!key[_R_SHIFT]))
      {
        if (key[_1])
        {
          if(oldkey==0) co[target].time++;
          if(key[_SPACEBAR]) co[target].time+=9;
          if(co[target].time>10000) co[target].time=-1;
          oldkey+=5;
          if (co[target].time>-1) createcreateobject(target,co[target].time,co[target].no,co[target].shape,
              co[target].type,co[target].side,co[target].target,
              co[target].cx,co[target].cy,co[target].cz,co[target].sx,co[target].sy,co[target].sz,
              co[target].rx,co[target].ry,co[target].rz,co[target].rsx,co[target].rsy,co[target].rsz,
              co[target].stubborn,co[target].wingleader,co[target].wingtype,
              co[target].wingx,co[target].wingy,co[target].wingz,co[target].destination,co[target].name,
              co[target].rank,co[target].mother,co[target].motherhangerzone);
        }
        else if (key[_2])
        {
          if (oldkey==0) co[target].no++;
          if (co[target].no>ships-1) co[target].shape=0;
          oldkey++;
          if (co[target].time>-1) createcreateobject(target,co[target].time,co[target].no,co[target].shape,
              co[target].type,co[target].side,co[target].target,
              co[target].cx,co[target].cy,co[target].cz,co[target].sx,co[target].sy,co[target].sz,
              co[target].rx,co[target].ry,co[target].rz,co[target].rsx,co[target].rsy,co[target].rsz,
              co[target].stubborn,co[target].wingleader,co[target].wingtype,
              co[target].wingx,co[target].wingy,co[target].wingz,co[target].destination,co[target].name,
              co[target].rank,co[target].mother,co[target].motherhangerzone);
        }
        else if (key[_3])
        {
          if (oldkey==0) co[target].shape++;
          if (co[target].shape>98) co[target].shape=10;
          oldkey++;
          if (co[target].time>-1) createcreateobject(target,co[target].time,co[target].no,co[target].shape,
              co[target].type,co[target].side,co[target].target,
              co[target].cx,co[target].cy,co[target].cz,co[target].sx,co[target].sy,co[target].sz,
              co[target].rx,co[target].ry,co[target].rz,co[target].rsx,co[target].rsy,co[target].rsz,
              co[target].stubborn,co[target].wingleader,co[target].wingtype,
              co[target].wingx,co[target].wingy,co[target].wingz,co[target].destination,co[target].name,
              co[target].rank,co[target].mother,co[target].motherhangerzone);
        }
        else if (key[_4])
        {
          if (oldkey==0) co[target].type++;
          if (co[target].type>7) co[target].type=0;
          oldkey++;
        }
        else if (key[_5])
        {
          if (oldkey==0) co[target].side++;
          if (co[target].side>4) co[target].side=0;
          oldkey++;
        }
        else if (key[_6])
        {
          if (oldkey==0) co[target].target++;
          if (co[target].target>ships-1) co[target].target=-1;
          oldkey++;
        }
        else if (key[_7])
        {
          if (oldkey==0) co[target].stubborn++;
          if (co[target].stubborn>2) co[target].stubborn=0;
          oldkey++;
        }
        else if (key[_8])
        {
          if (oldkey==0) co[target].wingleader++;
          if (co[target].wingleader>ships-1) co[target].wingleader=-1;
          oldkey++;
        }
        else if (key[_9])
        {
          if (oldkey==0) co[target].wingtype++;
          if (co[target].wingtype>5) co[target].wingtype=0;
          oldkey++;
        }
        else if (key[_0])
        {
          if (oldkey==0) co[target].destination++;
          if (co[target].destination>9) co[target].destination=0;
          oldkey++;
        }
        else if (key[_P])
        {
          if (oldkey==0) co[target].rank++;
          if (co[target].rank>9) co[target].rank=0;
          oldkey++;
        }
        else if (key[_M])
        {
          if (oldkey==0) co[target].mother++;
          if (co[target].mother>ships-1) co[target].mother=-1;
          oldkey++;
        }
        else if (key[_N])
        {
          if (oldkey==0) co[target].motherhangerzone++;
          if (co[target].motherhangerzone>o[co[target].mother].hangerzones-1) co[target].motherhangerzone=0;
          oldkey++;
        }
        else if (key[_B])
        {
          if (oldkey==0) co[target].createtype++;
          if (co[target].createtype>1) co[target].createtype=0;
          oldkey++;
        }
        else if (key[_V])
        {
          if (oldkey==0) co[target].dpt++;
          if (co[target].dpt>1) co[target].dpt=0;
          oldkey++;
        }
        else if (key[_EQUALS])
        {
          if (oldkey==0) { target++; if (target>ships-1) target=1; }
          oldkey++;
        }
        else if (key[_MINUS])
        {
          if (oldkey==0) { target--; if (target<1) target=ships-1; }
          oldkey++;
        }
        else oldkey=0;
      }
      else
      {
        if (key[_1])
        {
          if (oldkey==0) co[target].time--;
          if (key[_SPACEBAR]) co[target].time-=9;
          if (co[target].time<-1) co[target].time=10000;
          oldkey+=5;
          if (co[target].time>-1) createcreateobject(target,co[target].time,co[target].no,co[target].shape,
              co[target].type,co[target].side,co[target].target,
              co[target].cx,co[target].cy,co[target].cz,co[target].sx,co[target].sy,co[target].sz,
              co[target].rx,co[target].ry,co[target].rz,co[target].rsx,co[target].rsy,co[target].rsz,
              co[target].stubborn,co[target].wingleader,co[target].wingtype,
              co[target].wingx,co[target].wingy,co[target].wingz,co[target].destination,co[target].name,
              co[target].rank,co[target].mother,co[target].motherhangerzone);
        }
        else if (key[_2])
        {
          if (oldkey==0) co[target].no--;
          if (co[target].no<-1) co[target].no=ships-1;
          oldkey++;
          if (co[target].time>-1) createcreateobject(target,co[target].time,co[target].no,co[target].shape,
              co[target].type,co[target].side,co[target].target,
              co[target].cx,co[target].cy,co[target].cz,co[target].sx,co[target].sy,co[target].sz,
              co[target].rx,co[target].ry,co[target].rz,co[target].rsx,co[target].rsy,co[target].rsz,
              co[target].stubborn,co[target].wingleader,co[target].wingtype,
              co[target].wingx,co[target].wingy,co[target].wingz,co[target].destination,co[target].name,
              co[target].rank,co[target].mother,co[target].motherhangerzone);
        }
        else if (key[_3])
        {
          if (oldkey==0) co[target].shape--;
          if (co[target].shape<10) co[target].shape=98;
          oldkey++;
          if (co[target].time>-1) createcreateobject(target,co[target].time,co[target].no,co[target].shape,
              co[target].type,co[target].side,co[target].target,
              co[target].cx,co[target].cy,co[target].cz,co[target].sx,co[target].sy,co[target].sz,
              co[target].rx,co[target].ry,co[target].rz,co[target].rsx,co[target].rsy,co[target].rsz,
              co[target].stubborn,co[target].wingleader,co[target].wingtype,
              co[target].wingx,co[target].wingy,co[target].wingz,co[target].destination,co[target].name,
              co[target].rank,co[target].mother,co[target].motherhangerzone);
        }
        else if (key[_4])
        {
          if (oldkey==0) co[target].type--;
          if (co[target].type<0) co[target].type=7;
          oldkey++;
        }
        else if (key[_5])
        {
          if (oldkey==0) co[target].side--;
          if (co[target].side<0) co[target].side=4;
          oldkey++;
        }
        else if (key[_6])
        {
          if (oldkey==0) co[target].target--;
          if (co[target].target<-1) co[target].target=ships-1;
          oldkey++;
        }
        else if (key[_7])
        {
          if (oldkey==0) co[target].stubborn--;
          if (co[target].stubborn<0) co[target].stubborn=2;
          oldkey++;
        }
        else if (key[_8])
        {
          if (oldkey==0) co[target].wingleader--;
          if (co[target].wingleader<-1) co[target].wingleader=ships-1;
          oldkey++;
        }
        else if (key[_9])
        {
          if (oldkey==0) co[target].wingtype--;
          if (co[target].wingtype<0) co[target].wingtype=5;
          oldkey++;
        }
        else if (key[_0])
        {
          if (oldkey==0) co[target].destination--;
          if (co[target].destination<0) co[target].destination=9;
          oldkey++;
        }
        else if (key[_P])
        {
          if (oldkey==0) co[target].rank--;
          if (co[target].rank<0) co[target].rank=8;
          oldkey++;
        }
        else if (key[_M])
        {
          if (oldkey==0) co[target].mother--;
          if (co[target].mother<0) co[target].mother=ships-1;
          oldkey++;
        }
        else if (key[_N])
        {
          if (oldkey==0) co[target].motherhangerzone--;
          if (co[target].motherhangerzone<0) co[target].motherhangerzone=o[co[target].mother].hangerzones-1;
          oldkey++;
        }
        else if (key[_B])
        {
          if (oldkey==0) co[target].createtype--;
          if (co[target].createtype<0) co[target].createtype=1;
          oldkey++;
        }
        else if (key[_V])
        {
          if (oldkey==0) co[target].dpt--;
          if (co[target].dpt<0) co[target].dpt=1;
          oldkey++;
        }
        else oldkey=0;
      }
      if (oldkey>0)
      {
        oldkey++;
        if (oldkey>10) oldkey=0;
      }
      if (key[_O]) input(&co[target].name,10,66);
      strcpy(str , "1) TIME        : ");
      gcvt( co[target].time ,5 , str2);
      strcat(str ,  str2 );
      apigameprint3(1 , 10 , 6 , str);
      if (co[target].time>-1)
      {
        strcpy(str , "2) SHIP NUMBER : ");
        gcvt( co[target].no ,4 , str2);
        strcat(str , str2);
        apigameprint3(144 , 10 , 12 , str);
        strcpy(str , "3) SHAPE       : ");
        gcvt( co[target].shape ,4 , str2);
        strcat(str , str2); strcat (str , " : " );
        strcat(str ,  shapedata[co[target].shape].name );
        apigameprint3(144 , 10 , 18 , str);
        strcpy(str , "4) TYPE        : ");
        if (co[target].type==0) strcat( str, "DERELICT" );
        else if (co[target].type==1) strcat( str, "NORMAL" );
        else if (co[target].type==2) strcat( str, "APPROXIMATE" );
        else if (co[target].type==3) strcat( str, "BROADSIDE" );
        else if (co[target].type==4) strcat( str, "AMBUSH" );
        else if (co[target].type==5) strcat( str, "NON SENTIENT;(AI FOR TURRETS)" );
        else if (co[target].type==6) strcat( str, "CONVOY" );
        else if (co[target].type==7) strcat( str, "DUMB CONVOY" );
        else strcat( str, "NOTHING" );
        apigameprint3(144 , 10 , 24 , str);
        strcpy(str , "5) ALLEGANCE   : ");
        if (co[target].side==0) strcat( str, "NEUTRAL" );
        else if (co[target].side==1) strcat( str, "BELT" );
        else if (co[target].side==2) strcat( str, "GOVERNMENT" );
        else if (co[target].side==4) strcat( str, "U_CORP" );
        else if (co[target].side==5) strcat( str, "ROYAL" );
        else strcat( str, "EXTRA SOLARIS" );
        apigameprint3(144 , 10 , 30 , str);
        strcpy(str , "6) TARGET      : ");
        if (co[target].target!=-1)
        {
          gcvt( co[target].target ,3 , str2);
          strcat(str , str2); strcat(str , " : " );
          strcat(str ,  shapedata[o[co[target].target].shape].name );
        }
        else strcat( str , "NONE");
        apigameprint3(144 , 10 , 36 , str);
        strcpy(str , "7) BASIC       : ");
        if (co[target].stubborn==0) strcat( str, "NORMAL, BORING" );
        else if (co[target].stubborn==1) strcat( str, "STUBBORN TARGETTER" );
        else if (co[target].stubborn==2) strcat( str, "PATHALOGICAL KILLER" );
        apigameprint3(144 , 10 , 42 , str);
        strcpy(str , "8) WINGLEADER  : ");
        if (co[target].wingleader!=-1)
        {
          gcvt( co[target].wingleader ,3 , str2);
          strcat(str , str2); strcat(str , " : " );
          strcat(str ,  shapedata[o[co[target].wingleader].shape].name );
        }
        else strcat( str , "NONE");
        apigameprint3(144 , 10 , 48 , str);
        strcpy(str , "9) SUBCONSIOUS : ");
        if (co[target].wingtype==0) strcat( str, "NORMAL, LOYAL " );
        else if (co[target].wingtype==1) strcat( str, "NORMAL, DISLOYAL " );
        else if (co[target].wingtype==2) strcat( str, "SLAVE TARGETTER " );
        else if (co[target].wingtype==3) strcat( str, "FREE SPIRIT, LOYAL " );
        else if (co[target].wingtype==4) strcat( str, "FREE SPIRIT, DISLOYAL " );
        else if (co[target].wingtype==5) strcat( str, "FORMATION ONLY " );
        else co[target].wingtype=0;
        apigameprint3(144 , 10 , 54 , str);
        strcpy(str , "0) DESTINATION : WAYPOINT");
        gcvt( co[target].destination ,3 , str2);
        strcat( str , str2 );
        apigameprint3(144 , 10 , 60 , str);
        strcpy(str , "O) NAME        : ");
        strcat( str , co[target].name );
        apigameprint3(144 , 10 , 66 , str);
        strcpy(str , "P) RANK        : ");
        if (co[target].rank==0) strcat( str, "2ND LT" );
        else if (co[target].rank==1) strcat( str, "1ST LT" );
        else if (co[target].rank==2) strcat( str, "CAPTAIN" );
        else if (co[target].rank==3) strcat( str, "MAJOR" );
        else if (co[target].rank==4) strcat( str, "LT CNL" );
        else if (co[target].rank==5) strcat( str, "CNL" );
        else if (co[target].rank==6) strcat( str, "ADMIRAL" );
        else if (co[target].rank==7) strcat( str, "GRAND ADMIRAL" );
        else strcat( str , "MINOR DEITY" );
        apigameprint3(144 , 10 , 72 , str);
        strcpy(str , "M) MOTHER      : ");
        gcvt( co[target].mother   ,5 , str2);
        if (co[target].mother>-1)
        {
          gcvt( co[target].mother ,3 , str2);
          strcat(str , str2); strcat(str , " : " );
          if (o[co[target].mother].type>-1) strcat(str ,  shapedata[o[co[target].mother].shape].name );
        }
        else strcat( str , "NONE");
        apigameprint3(144 , 10 , 78 , str);
        if (co[target].mother>-1)
        {
          strcpy(str , "N) HANGER : ");
          gcvt( co[target].motherhangerzone   ,5 , str2);
          strcat( str , str2 );
          apigameprint3(144 , 10 , 84 , str);
        }
        strcpy(str , "B) CREATE TYPE : ");
        if (co[target].createtype==0) strcat(str,"HYPERSPACE IN" );
        else if (co[target].createtype==1) strcat(str,"LAUNCH FROM MOTHER" );
        else strcat( str, "NOTHING" );
        apigameprint3(144 , 10 , 90,str);
        strcpy(str , "V) DEPEND : ");
        if (co[target].dpt==1) strcat(str,"YES");
        else  strcat(str,"NO");
        apigameprint3(144 , 10 , 96 , str);
      }
      // end of some bollocks that I wrote when rat arsed.
      strcpy(str , "POSSIBLE " );
      gcvt( target , 2 , str2 );
      strcat(str , str2);
      apigameprint3(1 , 5 , 194 , str);
    }
    // end of controlling type =1

//********************* OBJECT EVENTS **********************

    else if (current==2)
    {
      if ((!key[_L_SHIFT])&&(!key[_R_SHIFT]))
      {
        if (key[_1])
        {
          if (oldkey==0) oe[target].time++;
          if (key[_SPACEBAR]) oe[target].time+=9;
          if (oe[target].time>10000) oe[target].time=-1;
          oldkey+=15;
        }
        else if (key[_2])
        {
          if (oldkey==0) oe[target].no++;
          if (oe[target].no>shapes-1) oe[target].no=0;
          oldkey++;
        }
        else if (key[_3])
        {
          if (oldkey==0) oe[target].type++;
          if (oe[target].type>7) oe[target].type=-1;
          oldkey++;
        }
        else if (key[_4])
        {
          if (oldkey==0)
          {
            if (oe[target].doing==-1) oe[target].doing=0;
            else if (oe[target].doing==0) oe[target].doing=1;
            else if (oe[target].doing==1) oe[target].doing=2;
            else if (oe[target].doing==2) oe[target].doing=4;
            else if (oe[target].doing==4) oe[target].doing=5;
            else if (oe[target].doing==5) oe[target].doing=6;
            else if (oe[target].doing==6) oe[target].doing=7;
            else if (oe[target].doing==7) oe[target].doing=8;
            else if (oe[target].doing==8) oe[target].doing=9;
            else if (oe[target].doing==9) oe[target].doing=10;
            else if (oe[target].doing==10) oe[target].doing=21;
            else if (oe[target].doing==21) oe[target].doing=-1;
          }
          oldkey++;
        }
        else if (key[_5])
        {
          if (oldkey==0) oe[target].side++;
          if (oe[target].side>4) oe[target].side=-1;
          oldkey++;
        }
        else if (key[_6])
        {
          if (oldkey==0) oe[target].target++;
          if (oe[target].target>ships-1) oe[target].target=-1;
          oldkey++;
        }
        else if (key[_7])
        {
          if (oldkey==0) oe[target].stubborn++;
          if (oe[target].stubborn>2) oe[target].stubborn=-1;
          oldkey++;
        }
        else if (key[_8])
        {
          if (oldkey==0) oe[target].wingleader++;
          if (oe[target].wingleader>ships-1) oe[target].wingleader=-1;
          oldkey++;
        }
        else if (key[_9])
        {
          if (oldkey==0) oe[target].wingtype++;
          if (oe[target].wingtype>5) oe[target].wingtype=-1;
          oldkey++;
        }
        else if (key[_0])
        {
          if (oldkey==0) oe[target].destination++;
          if (oe[target].destination>9) oe[target].destination=0;
          oldkey++;
        }
        else if (key[_P])
        {
          if (oldkey==0) oe[target].rank++;
          if (oe[target].rank>9) oe[target].rank=-1;
          oldkey++;
        }
        else if (key[_O])
        {
          if (oldkey==0) oe[target].topspeed++;
          if (oe[target].topspeed>100) oe[target].topspeed=-1;
          oldkey+=5;
        }
        else if (key[_I])
        {
          if (oldkey==0) oe[target].toprevs++;
          if (oe[target].toprevs>100) oe[target].toprevs=-1;
          oldkey+=5;
        }
        else if (key[_U])
        {
          if (oldkey==0) oe[target].shields++;
          if (oe[target].shields>9999) oe[target].shields=-1;
          oldkey+=15;
        }
        else if (key[_Y])
        {
          if (oldkey==0) oe[target].maxshields++;
          if (oe[target].maxshields>9999) oe[target].maxshields=-1;
          oldkey+=15;
        }
        else if (key[_T])
        {
          if (oldkey==0) oe[target].armour++;
          if (oe[target].armour>9999) oe[target].armour=-1;
          oldkey+=15;
        }
        else if (key[_R])
        {
          if (oldkey==0) oe[target].maxarmour++;
          if (oe[target].maxarmour>9999) oe[target].maxarmour=-1;
          oldkey+=15;
        }
        else if (key[_EQUALS])
        {
          if (oldkey==0) target++;
          oldkey++;
          if (target>shapes-1) target=0;
          if (target<0) target=shapes-1;
        }
        else if (key[_MINUS])
        {
          if (oldkey==0) target--;
          oldkey++;
          if (target>shapes-1) target=0;
          if (target<0) target=shapes-1;
        }
        else
        {
          oldkey=0;
        }
      }
      else
      {
        if (key[_1])
        {
          if (oldkey==0) oe[target].time--;
          if (key[_SPACEBAR]) oe[target].time-=9;
          if (oe[target].time<-1) oe[target].time=10000;
          oldkey+=15;
        }
        else if (key[_2])
        {
          if (oldkey==0) oe[target].no--;
          if (oe[target].no<-1) oe[target].no=ships-1;
          oldkey++;
        }
        else if (key[_3])
        {
          if (oldkey==0) oe[target].type--;
          if (oe[target].type<-1) oe[target].type=7;
          oldkey++;
        }
        else if (key[_4])
        {
          if (oldkey==0)
          {
            if (oe[target].doing==-1) oe[target].doing=21;
            else if (oe[target].doing==21) oe[target].doing=10;
            else if (oe[target].doing==10) oe[target].doing=9;
            else if (oe[target].doing==9) oe[target].doing=8;
            else if (oe[target].doing==8) oe[target].doing=7;
            else if (oe[target].doing==7) oe[target].doing=6;
            else if (oe[target].doing==6) oe[target].doing=5;
            else if (oe[target].doing==5) oe[target].doing=4;
            else if (oe[target].doing==4) oe[target].doing=2;
            else if (oe[target].doing==2) oe[target].doing=1;
            else if (oe[target].doing==1) oe[target].doing=0;
            else if (oe[target].doing==0) oe[target].doing=-1;
          }
          oldkey++;
        }
        else if (key[_5])
        {
          if (oldkey==0) oe[target].side--;
          if (oe[target].side<-1) oe[target].side=4;
          oldkey++;
        }
        else if (key[_6])
        {
          if (oldkey==0) oe[target].target--;
          if (oe[target].target<-1) oe[target].target=ships-1;
          oldkey++;
        }
        else if (key[_7])
        {
          if (oldkey==0) oe[target].stubborn--;
          if (oe[target].stubborn<-1) oe[target].stubborn=2;
          oldkey++;
        }
        else if (key[_8])
        {
          if (oldkey==0) oe[target].wingleader--;
          if (oe[target].wingleader<-1) oe[target].wingleader=ships-1;
          oldkey++;
        }
        else if (key[_9])
        {
          if (oldkey==0) oe[target].wingtype--;
          if (oe[target].wingtype<-1) oe[target].wingtype=5;
          oldkey++;
        }
        else if (key[_0])
        {
          if (oldkey==0) oe[target].destination--;
          if (oe[target].destination<-1) oe[target].destination=9;
          oldkey++;
        }
        else if (key[_P])
        {
          if (oldkey==0) oe[target].rank--;
          if (oe[target].rank<-1) oe[target].rank=8;
          oldkey++;
        }
        else if (key[_O])
        {
          if (oldkey==0) oe[target].topspeed--;
          if (oe[target].topspeed<-1) oe[target].topspeed=100;
          oldkey+=5;
        }
        else if (key[_I])
        {
          if (oldkey==0) oe[target].toprevs--;
          if (oe[target].toprevs<-1) oe[target].toprevs=100;
          oldkey+=5;
        }
        else if (key[_U])
        {
          if (oldkey==0) oe[target].shields--;
          if (oe[target].shields<-1) oe[target].shields=9999;
          oldkey+=15;
        }
        else if (key[_Y])
        {
          if (oldkey==0) oe[target].maxshields--;
          if (oe[target].maxshields<-1) oe[target].maxshields=9999;
          oldkey+=15;
        }
        else if (key[_T])
        {
          if (oldkey==0) oe[target].armour--;
          if (oe[target].armour<-1) oe[target].armour=9999;
          oldkey+=15;
        }
        else if (key[_R])
        {
          if (oldkey==0) oe[target].maxarmour--;
          if (oe[target].maxarmour<-1) oe[target].maxarmour=9999;
          oldkey+=15;
        }
        else oldkey=0;
      }
      if (oldkey>0)
      {
        oldkey++; if (oldkey>10) oldkey=0;
      }
      strcpy(str,"1) TIME        : ");
      gcvt(oe[target].time,5,str2);
      strcat(str,str2);
      apigameprint3(1,10,6,str);
      if (oe[target].time>-1)
      {
        strcpy(str,"2) SHIP NUMBER : ");
        gcvt(oe[target].no,4,str2);
        strcat(str,str2);
        strcat(str," ; ");
        if(oe[target].no!=-1 && o[oe[target].no].shape!=-1)
        {
          strcat(str,shapedata[o[oe[target].no].shape].name);
          apigameprint3(144,10,12,str);
        }
        strcpy(str , "3) TYPE        : ");
        if (oe[target].type==0) strcat( str, "AMBUSH" );
        else if (oe[target].type==1) strcat( str, "NORMAL" );
        else if (oe[target].type==2) strcat( str, "APPROXIMATE" );
        else if (oe[target].type==3) strcat( str, "BROADSIDE" );
        else if (oe[target].type==4) strcat( str, "AMBUSH" );
        else if (oe[target].type==5) strcat( str, "NON SENTIENT;(AI FOR TURRETS)" );
        else if (oe[target].type==6) strcat( str, "CONVOY" );
        else if (oe[target].type==7) strcat( str, "DUMB CONVOY" );
        else if (oe[target].type==-1) strcat( str, "NO CHANGE" );
        else strcat( str, "NOTHING" );
        apigameprint3(144 , 10 , 18 , str);
        strcpy(str , "4) DOING       : ");
        if (oe[target].doing==0) strcat( str, "NORMAL" );
        else if (oe[target].doing==1) strcat( str, "EVADING" );
        else if (oe[target].doing==2) strcat( str, "PANICING" );
        else if (oe[target].doing==4) strcat( str, "APPROACHING" );
        else if (oe[target].doing==5) strcat( str, "LINING UP" );
        else if (oe[target].doing==6) strcat( str, "LANDING" );
        else if (oe[target].doing==7) strcat( str, "PREPAREING TO BOARD" );
        else if (oe[target].doing==8) strcat( str, "BOARDING" );
        else if (oe[target].doing==9) strcat( str, "BOARDED" );
        else if (oe[target].doing==10) strcat( str, "DEATH" );
        else if (oe[target].doing==21) strcat( str, "ENTERING HYPERSPACE" );
        else if (oe[target].doing==-1) strcat( str, "NO CHANGE" );
        else strcat( str, "NOTHING" );
        apigameprint3(144 , 10 , 24 , str);
        strcpy(str , "5) ALLEGANCE   : ");
        if (oe[target].side==0) strcat( str, "NEUTRAL" );
        else if (oe[target].side==1) strcat( str, "BELT" );
        else if (oe[target].side==2) strcat( str, "GOVERNMENT" );
        else if (oe[target].side==4) strcat( str, "U_CORP" );
        else if (oe[target].side==5) strcat( str, "ROYAL" );
        else if (oe[target].side==-1) strcat( str, "NO CHANGE" );
        else strcat( str, "EXTRA SOLARIS" );
        apigameprint3(144 , 10 , 30 , str);
        strcpy(str , "6) TARGET      : ");
        if (oe[target].target!=-1)
        {
          gcvt( oe[target].target ,3 , str2);
          strcat(str , str2); strcat(str , " : " );
          strcat(str ,  shapedata[o[oe[target].target].shape].name );
        }
        else strcat( str , "NO CHANGE");
        apigameprint3(144 , 10 , 36 , str);
        strcpy(str , "7) BASIC       : ");
        if (oe[target].stubborn==0) strcat( str, "NORMAL, BORING" );
        else if (oe[target].stubborn==1) strcat( str, "STUBBORN TARGETTER" );
        else if (oe[target].stubborn==2) strcat( str, "PATHALOGICAL KILLER" );
        else if (oe[target].stubborn==-1) strcat( str, "NO CHANGE" );
        apigameprint3(144 , 10 , 42 , str);
        strcpy(str , "8) WINGLEADER  : ");
        if (oe[target].wingleader!=-1)
        {
          gcvt( oe[target].wingleader ,3 , str2);
          strcat(str , str2); strcat(str , " : " );
          strcat(str ,  shapedata[o[oe[target].wingleader].shape].name );
        }
        else strcat( str , "NO CHANGE");
        apigameprint3(144 , 10 , 48 , str);
        strcpy(str , "9) SUBCONSIOUS : ");
        if (oe[target].wingtype==0) strcat( str, "NORMAL, LOYAL " );
        else if (oe[target].wingtype==1) strcat( str, "NORMAL, DISLOYAL " );
        else if (oe[target].wingtype==2) strcat( str, "SLAVE TARGETTER " );
        else if (oe[target].wingtype==3) strcat( str, "FREE SPIRIT, LOYAL " );
        else if (oe[target].wingtype==4) strcat( str, "FREE SPIRIT, DISLOYAL " );
        else if (oe[target].wingtype==5) strcat( str, "FORMATION ONLY " );
        else if (oe[target].wingtype==-1) strcat( str, "NO CHANGE" );
        else oe[target].wingtype=0;
        apigameprint3(144 , 10 , 54 , str);
        strcpy(str , "0) DESTINATION : WAYPOINT ");
        if (oe[target].destination==-1) strcat( str, "NO CHANGE" );
        else
        {
          gcvt( oe[target].destination ,3 , str2);
          strcat( str , str2 );
        }
        apigameprint3(144 , 10 , 60 , str);
        strcpy(str , "P) RANK        : ");
        if (oe[target].rank==0) strcat( str, "2ND LT" );
        else if (oe[target].rank==1) strcat( str, "1ST LT" );
        else if (oe[target].rank==2) strcat( str, "CAPTAIN" );
        else if (oe[target].rank==3) strcat( str, "MAJOR" );
        else if (oe[target].rank==4) strcat( str, "LT CNL" );
        else if (oe[target].rank==5) strcat( str, "CNL" );
        else if (oe[target].rank==6) strcat( str, "ADMIRAL" );
        else if (oe[target].rank==7) strcat( str, "GRAND ADMIRAL" );
        else strcat( str , "MINOR DEITY" );
        apigameprint3(144 , 10 , 66 , str);
        strcpy(str , "O) TOP SPEED   : ");
        if (oe[target].topspeed!=-1)
        {
          gcvt( oe[target].topspeed ,4 , str2);
          strcat(str , str2);
        }
        else strcat( str , "NO CHANGE");
        apigameprint3(144 , 10 , 72 , str);
        strcpy(str , "I) TOP REVS    : ");
        if (oe[target].toprevs!=-1)
        {
          gcvt( oe[target].toprevs ,4 , str2);
          strcat(str , str2);
        }
        else strcat( str , "NO CHANGE");
        apigameprint3(144 , 10 , 78 , str);
        strcpy(str , "U) SHIELDS     : ");
        if (oe[target].shields!=-1)
        {
          gcvt( oe[target].shields ,5 , str2);
          strcat(str , str2);
        }
        else strcat( str , "NO CHANGE");
        apigameprint3(144 , 10 , 84 , str);
        strcpy(str , "Y) MAX SHIELDS : ");
        if (oe[target].maxshields!=-1)
        {
          gcvt( oe[target].maxshields ,5 , str2);
          strcat(str , str2);
        }
        else strcat( str , "NO CHANGE");
        apigameprint3(144 , 10 , 90 , str);
        strcpy(str , "T) ARMOUR      : ");
        if (oe[target].armour!=-1)
        {
          gcvt( oe[target].armour ,5 , str2);
          strcat(str , str2);
        }
        else strcat( str , "NO CHANGE");
        apigameprint3(144 , 10 , 96 , str);
        strcpy(str , "R) MAX ARMOUR  : ");
        if (oe[target].maxarmour!=-1)
        {
          gcvt(oe[target].maxarmour ,5 , str2);
          strcat(str , str2);
        }
        else strcat( str , "NO CHANGE");
        apigameprint3(144 , 10 , 102 , str);
      }
      // end of some bollocks that I wrote when rat arsed.
      strcpy(str,"EVENT " );
      gcvt(target,2 , str2 );
      strcat(str , str2);
      apigameprint3(1 , 5 , 194 , str);
    }
//********************* CREATE COMMS **********************
    else if (current==3)
    {
      if ((!key[_L_SHIFT])&&(!key[_R_SHIFT]))
      {
        if (key[_1])
        {
          if (oldkey==0) ce[target].time++;
          if (key[_SPACEBAR]) ce[target].time+=9;
          if (ce[target].time>10000) ce[target].time=-1;
          oldkey+=5;
        }
        else if (key[_2])
        {
          if (oldkey==0) ce[target].from++;
          if (ce[target].from>ships-1) ce[target].from=0;
          oldkey++;
        }
        else if (key[_3])
        {
          if (oldkey==0)
          {
            ce[target].side++;
          }
          if (ce[target].side>4) ce[target].side=0;
          oldkey++;
        }
        else if (key[_EQUALS])
        {
          if (oldkey==0) target++;
          if (target>shapes-1) target=0;
          oldkey+=5;
        }
        else if (key[_MINUS])
        {
          if (oldkey==0) target--;
          if (target<0) target=shapes-1;
          oldkey+=5;
        }
        else
        {
          oldkey=0;
        }
      }
      else
      {
        if (key[_1])
        {
          if (oldkey==0) ce[target].time--;
          if (key[_SPACEBAR]) ce[target].time-=9;
          if (ce[target].time<-1) ce[target].time=10000;
          oldkey+=5;
        }
        else if (key[_2])
        {
          if (oldkey==0) ce[target].from--;
          if (ce[target].from<0) ce[target].from=ships-1;
          oldkey++;
        }
        else if (key[_3])
        {
          if (oldkey==0)
          {
            co[target].side--;
          }
          if (co[target].side<0)        co[target].side=4;
          oldkey++;
        }
        else
        {
          oldkey=0;
        }
      }
      if (oldkey>0)
      {
        oldkey++; if (oldkey>10) oldkey=0;
      }
      if (key[_4]) input(&ce[target].msg,10,24);
      strcpy(str , "1) TIME        : ");
      gcvt( ce[target].time ,5 , str2);
      strcat(str ,  str2 );
      apigameprint3(144 , 10 , 6 , str);
      if (ce[target].time>-1)
      {
        strcpy(str , "2) FROM SHIP   : ");
        gcvt( ce[target].from ,4 , str2);
        strcat(str,str2);
        strcat(str," ; ");
        if(ce[target].from!=-1 && o[ce[target].from].shape!=-1)
          strcat(str,shapedata[o[ce[target].from].shape].name);
        apigameprint3(144 , 10 , 12 , str);
        strcpy(str , "3) SIDE        : ");
        if (ce[target].side==0) strcat( str, "NEUTRAL" );
        else if (ce[target].side==1) strcat( str, "BELT" );
        else if (ce[target].side==2) strcat( str, "GOVERNMENT FORCES" );
        else strcat( str, "EXTRA SOLARIS" );
        apigameprint3(144 , 10 , 18 , str);
        strcpy(str , "4) MESSAGE     : ");
        strcat( str, ce[target].msg );
        apigameprint3(144 , 10 , 24 , str);
      }
      strcpy(str,"COMMUNICATION ");
      gcvt(target,4,str2);
      strcat(str,str2);
      apigameprint3(1,5,194,str);
    }

//********************* PLANETS AND STARS **********************
    else if (current==4)
    {
      if (!key[_S])
      {
        rsx2=rsy2=rsz2=0;
        if ((key[_X])&&(sy<50)) sy2=sy2+1;
        else if ((key[_W])&&(sy2>-50)) sy2=sy2-1;
        else sy2=sy2/1.1;
        if ((key[_A])&&(sx>-50)) sx2=sx2-1;
        else if ((key[_D])&&(sx<50)) sx2=sx2+1;
        else sx2=sx2/1.1;
        if ((key[_C])&&(sx2>-50)) sz2=sz2-1;
        else if ((key[_E])&&(sx2<50)) sz2=sz2+1;
        else sz2=sz2/1.1;
        p[target].cy+=sy2;
        p[target].cx+=sx2;
        p[target].cz+=sz2;
      }
      else
      {
        sx2=sy2=sz2=0;
        if ((key[_W])&&(sy<5)) rsx2=rsx2+.05;
        else if ((key[_X])&&(sy>-5)) rsx2=rsx2-.05;
        else rsx2=rsx2/1.1;
        if ((key[_A])&&(sx>-5)) rsy2=rsy2-.05;
        else if ((key[_D])&&(sx<5)) rsy2=rsy2+.05;
        else rsy2=rsy2/1.1;
        if ((key[_C])&&(sx>-5)) rsz2=rsz2-.05;
        else if ((key[_E])&&(sx<5)) rsz2=rsz2+.05;
        else rsz2=rsz2/1.1;
        spinprojectile(target, p[target].cx,p[target].cy,p[target].cz, rsx2, rsy2, rsz2,0);
      }
      if ((!key[_L_SHIFT])&&(!key[_R_SHIFT]))
      {
        if (key[_1])
        {
          if (oldkey==0) p[target].type++;
          if (p[target].type>0) p[target].type=-1;
          if (p[target].type>-1) createstellar(target,0,p[target].shape, p[target].cx,p[target].cy,p[target].cz, p[target].rx,p[target].ry,p[target].rz, 0,0,0 ,0,0,0 ) ;
          oldkey++;
        }
        else if (key[_2])
        {
          if (oldkey==0) p[target].shape++;
          if (p[target].shape>100) p[target].shape=1;
          if (p[target].type>-1) createstellar(target,0,p[target].shape, p[target].cx,p[target].cy,p[target].cz, p[target].rx,p[target].ry,p[target].rz, 0,0,0 ,0,0,0 ) ;
          oldkey++;
        }
        else if (key[_EQUALS])
        {
          if (oldkey==0) target++;
          oldkey++;
          if (target>shapes-1) target=shapes-10;
        }
        else if (key[_MINUS])
        {
          if (oldkey==0) target--;
          oldkey++;
          if (target<shapes-10) target=shapes-1;
        }
        else
        {
          oldkey=0;
        }
      }
      else
      {
        if (key[_1])
        {
          if (oldkey==0) p[target].type--;
          if (p[target].type<-1) p[target].type=0;
          if (p[target].type>-1) createstellar(target,0,p[target].shape, p[target].cx,p[target].cy,p[target].cz, p[target].rx,p[target].ry,p[target].rz, 0,0,0 ,0,0,0 ) ;
          oldkey++;
        }
        else if (key[_2])
        {
          if (oldkey==0) p[target].shape--;
          if (p[target].shape<1) p[target].shape=100;
          if (p[target].type>-1) createstellar(target,0,p[target].shape, p[target].cx,p[target].cy,p[target].cz, p[target].rx,p[target].ry,p[target].rz, 0,0,0 ,0,0,0);
          oldkey++;
        }
        else
        {
          oldkey=0;
        }
      }
      if (oldkey>0)
      {
        oldkey++;
        if (oldkey>10) oldkey=0;
      }
      strcpy(str , "1) EXISTS      : ");
      if (p[target].type==-1) strcat(str,"NO");
      else strcat(str, "YES") ;
      apigameprint3(1 , 10 , 6 , str);
      if (p[target].type>-1)
      {
        strcpy(str , "2) SHAPE   P   : ");
        gcvt( p[target].shape ,4 , str2);
        strcat(str , str2);
        apigameprint3(144,10,12,str);

        strcpy(str , "X              : ");
        gcvt( p[target].cx ,5 , str2);
        strcat( str , str2 );
        apigameprint3(144 , 10 , 18 , str);

        strcpy(str , "Y              : ");
        gcvt( p[target].cy ,5 , str2);
        strcat( str , str2 );
        apigameprint3(144 , 10 , 24 , str);

        strcpy(str , "Z              : ");
        gcvt( p[target].cz ,5 , str2);
        strcat( str , str2 );
        apigameprint3(144 , 10 , 30 , str);
      }
      strcpy(str , "STELLAR OBJECT " );
      gcvt(target,4,str2);
      strcat(str,str2);
      apigameprint3(1,5,194,str);
    }
//********************* OBJECTIVES **********************
    else if (current==5)
    {
      if ((!key[_L_SHIFT])&&(!key[_R_SHIFT]))
      {
        if (key[_1])
        {
          if (oldkey==0) matter[target]++;
          if (matter[target]>2) matter[target]=0;
          oldkey+=1;
        }
        else if (key[_EQUALS])
        {
          if (oldkey==0) target++;
          oldkey+=1;
          if (target>ships-1) target=0;
          if (target<0) target=ships-1;
        }
        else if (key[_MINUS])
        {
          if (oldkey==0) target--;
          oldkey+=1;
          if (target>ships-1) target=0;
          if (target<0) target=ships-1;
        }
        else
        {
          oldkey=0;
        }
      }
      else
      {
        if (key[_1])
        {
          if (oldkey==0) matter[target]--;
          if (matter[target]<0) matter[target]=2;
          oldkey+=1;
        }
      }
      if (oldkey>0)
      {
        oldkey++; if (oldkey>10) oldkey=0;
      }
      if (target>ships-1) target=0;
      if (target<0) target=ships-1;
      strcpy(str , "1) SHIP        : ");
      gcvt( target ,3 , str2);
      strcat(str , str2); strcat(str , "," );
      if (o[target].type==-1) strcat (str , " (DOESNT EXIST) " );
      strcat(str ,  shapedata[o[target].shape].name );
      if (matter[target]==0) strcat( str , " DOESNT MATTER" );
      else if (matter[target]==1) strcat( str , " MUST BE DESTROYED" );
      else if (matter[target]==2) strcat( str , " MUST SURVIVE" );
      else strcat (str , "MUST DO A POOH" );
      apigameprint3(1 , 10 , 6 , str);
      strcpy(str , "OBJECTIVE " );
      gcvt( target , 4 , str2 );
      strcat(str , str2);
      apigameprint3(1 , 5 , 194 , str);
    }
//********************* WAYPOINTS **********************
    else if (current==6)
    {
      if (!key[_S])
      {
        rsx2=rsy2=rsz2=0;
        if ((key[_X])&&(sy<150)) sy2=sy2+10;
        else if ((key[_W])&&(sy2>-50)) sy2=sy2-10;
        else sy2=sy2/2;
        if ((key[_A])&&(sx>-150)) sx2=sx2-10;
        else if ((key[_D])&&(sx<50)) sx2=sx2+10;
        else sx2=sx2/2;
        if ((key[_C])&&(sx2>-150)) sz2=sz2-10;
        else if ((key[_E])&&(sx2<150)) sz2=sz2+10;
        else sz2=sz2/2;
        w[target].y+=sy2;
        w[target].x+=sx2;
        w[target].z+=sz2;
      }
      if ((!key[_L_SHIFT])&&(!key[_R_SHIFT]))
      {
        if (key[_EQUALS])
        {
          if (oldkey==0) target++;
          oldkey++;
          if (target>ships-1) target=0;
          if (target<0) target=ships-1;
        }
        else if (key[_MINUS])
        {
          if (oldkey==0) target--;
          oldkey++;
          if (target>ships-1) target=0;
          if (target<0) target=ships-1;
        }
        else
        {
          oldkey=0;
        }
      }
      else
      {
        oldkey=0;
      }
      if (target>9) target=0;
      if (target<0) target=9;
      if (oldkey>0)
      {
        oldkey++; if (oldkey>10) oldkey=0;
      }
      strcpy(str , "X     : ");
      gcvt( w[target].x ,5 , str2);
      strcat( str , str2 );
      apigameprint3(144 , 10 , 6 , str);
      strcpy(str , "Y     : ");
      gcvt( w[target].y ,5 , str2);
      strcat( str , str2 );
      apigameprint3(144 , 10 , 12 , str);
      strcpy(str , "Z     : ");
      gcvt( w[target].z ,5 , str2);
      strcat( str , str2 );
      apigameprint3(144 , 10 , 18 , str);
      strcpy(str , "WAYPOINT " );
      gcvt( target , 4 , str2 );
      strcat(str , str2);
      apigameprint3(1 , 5 , 194 , str);
    }
    /* GROUND */
    else if (current==7)
    {
      if (!key[_S])
      {
        rsx2=rsy2=rsz2=0;
        if ((key[_X])&&(sy<150)) sy2=sy2+10;
        else if ((key[_W])&&(sy2>-50)) sy2=sy2-10;
        else sy2=sy2/2;
        if ((key[_A])&&(sx>-150)) sx2=sx2-10;
        else if ((key[_D])&&(sx<50)) sx2=sx2+10;
        else sx2=sx2/2;
        if ((key[_C])&&(sx2>-150)) sz2=sz2-10;
        else if ((key[_E])&&(sx2<150)) sz2=sz2+10;
        else sz2=sz2/2;
        for(f=0;f<9;f++)
        {
          gr[f].cy+=sy2;
          gr[f].cx+=sx2;
          gr[f].cz+=sz2;
        }
      }
      if ((!key[_L_SHIFT])&&(!key[_R_SHIFT]))
      {
        if (key[_EQUALS])
        {
          if (oldkey==0) target++;
          oldkey++;
          if (target>3) target=0;
          createground(target,50);
        }
        else if (key[_MINUS])
        {
          if (oldkey==0) target--;
          oldkey++;
          if (target<0) target=3;
          createground(target,50);
        }
        else
        {
          oldkey=0;
        }
      }
      else
      {
        oldkey=0;
      }
      if (oldkey>0)
      {
        oldkey++; if (oldkey>10) oldkey=0;
      }
      if(ground==1) sprintf(str,"MARS");
      else if(ground==2) sprintf(str,"ROCK");
      else if(ground==3) sprintf(str,"ICE");
      else sprintf(str,"NONE");
      apigameprint3(1 , 10 , 6 , str);
    }
    else if (current==8)
    {
      if ((!key[_L_SHIFT])&&(!key[_R_SHIFT]))
      {
        if (key[_1])
        {
          if (oldkey==0) m1++;
          if (m1>45) m1=0;
          oldkey+=3;
        }
        else if (key[_2])
        {
        for (f=0; f<shapes; f++)
        {
          if(ground==0 || f!=shapes-1)
          {
            p[f].shape=33;
            p[f].type=-1;
            p[f].frame1=p[f].frame2=p[f].frame3=0;
            p[f].side=0;
            p[f].target=-1;
            p[f].speed=p[f].topspeed=p[f].toprevs=p[f].currentrevs=1;
            p[f].cx=0;      p[f].cy=0;      p[f].cz=5000;
            p[f].sx=0;      p[f].sy=0;      p[f].sz=0;
            p[f].rx=0;      p[f].ry=0;      p[f].rz=0;
            p[f].rsx=0;     p[f].rsy=0;     p[f].rsz=0;
            p[f].dx[0]=1;   p[f].dy[0]=1;   p[f].dz[0]=100;
            p[f].dx[1]=1;   p[f].dy[1]=100; p[f].dz[1]=1;
            p[f].dx[2]=100; p[f].dy[2]=1;   p[f].dz[2]=1;
          }
        }
//        for(f=shapes-5; f<shapes-9; f++)
//        {
//          p[f].dx[0]=1;   p[f].dy[0]=1;   p[f].dz[0]=100;
//          p[f].dx[1]=1;   p[f].dy[1]=100; p[f].dz[1]=1;
//          p[f].dx[2]=100; p[f].dy[2]=1;   p[f].dz[2]=1;
//        }
          p[shapes-9].cx=0;
          p[shapes-9].cy=0;
          p[shapes-9].cz=-300;
          light=dddCreateLight(VECTOR_LIGHT,255,1000);
          p[shapes-9].type=1;
          p[shapes-9].object=dddLoad("sun.3dw");
          p[shapes-9].shape=60;
          spinprojectile(shapes-9, 0,0,0, 10,0,0,0); // Stick the sun up in the sky.
          p[shapes-8].cx=0;
          p[shapes-8].cy=0;
          p[shapes-8].cz=0;
          p[shapes-8].type=1;
          p[shapes-8].object=dddLoad("galaxy.3dw");
          p[shapes-8].shape=83;
          spinprojectile(shapes-8, 0,0,0, 5,0,9,0); // Make it a nice milky way
          p[shapes-7].cx=0;
          p[shapes-7].cy=0;
          p[shapes-7].cz=500;
          p[shapes-7].type=1;
          p[shapes-7].object=dddLoad("neb1.3dw");
          p[shapes-7].shape=101;
          spinprojectile(shapes-7, 0,0,0, -15,0,4,0); // Make it a nice SPLUT
          p[shapes-6].cx=0;
          p[shapes-6].cy=0;
          p[shapes-6].cz=500;
          p[shapes-6].type=1;
          p[shapes-6].object=dddLoad("neb2.3dw");
          p[shapes-6].shape=102;
          spinprojectile(shapes-6, 0,0,0, 30,5,0,0); // Make it a nice THINGY
          p[shapes-5].cx=0;
          p[shapes-5].cy=0;
          p[shapes-5].cz=400;
          p[shapes-5].type=1;
          p[shapes-5].object=dddLoad("neb3.3dw");
          p[shapes-5].shape=103;
          spinprojectile(shapes-5, 0,0,0, 20,20,5,0); // Make it a nice THINGY
        }
        else if (key[_I])
        {
          for(f=0;f<m1;f++)
          {
            h=0;
            for(g=1;g<49;g++)
            {
              if(h==0 && o[g].type==-1)
              {
                 x=saferandom(10000);
                 y=saferandom(10000);
                 z=saferandom(10000);
                 rx=(saferandom(8));
                 ry=(saferandom(8));
                 rz=(saferandom(8));
                 rx=rx/4;
                 ry=ry/4;
                 rz=rz/4;
                 createship(g,0,36+apirandom(3),
                 x,y,z,0,0,0,rx,ry,rz,0,0,0,
                 0,-1,0,100,100,100,0,"ROCK",0,-1,-1,0);
                 h=1;
              } // End of create
            } // end of g
          } // end of f
        } // end of _I
        else if (key[_O])
        {

        }
        else if (key[_P])
        {

        }
        else
        {
          oldkey=0;
        }
      }
      else
      {
        if (key[_1])
        {
          if (oldkey==0) m1--;
          if (m1<0) m1=45;
          oldkey+=3;
        }
        else if (key[_2])
        {
        }

        else
        {
          oldkey=0;
        }
      }
      if (oldkey>0)
      {
        oldkey++; if (oldkey>10) oldkey=0;
      }
      sprintf(str,"1    I: CREATE %d ASTEROIDS",m1);
      apigameprint3(144 , 10 , 6 , str );
      sprintf(str,"2       RESET STELLARFIELD");
      apigameprint3(144 , 10 , 12 , str );
//      sprintf(str,"234  O: CREATE %d SHIPS %d SHAPE %d SIDE",m2,m3,m4);
//      apigameprint3(144 , 10 , 12 , str );
//      sprintf(str,"5678 P: %d CREATESHIPS %d SHAPE %d MOTHER AT %d TIME",m5,m6,m7,m8);
//      apigameprint3(144 , 10 , 18 , str );
    }

    strcpy(str , "NAME : " );
    strcat(str , filename);
    apigameprint3(1 , 5 , 176 , str);
    strcpy(str , "TYPE : " );
    if(norand==0) strcat(str , "NORMAL MISSION");
    else if(norand==1) strcat(str , "AI CUTSCENE");
    apigameprint3(1 , 5 , 182 , str);
    sprintf(str , "TIME : %d", finishtime );
    apigameprint3(1 , 5 , 188 , str);
    strcpy(str,"START TYPE: ");
    if(startflag==HYPERSPACEIN) strcat(str,"HYPERSPACE IN");
    else if(startflag==TAKEOFF) strcat(str,"TAKE OFF");
    apigameprint3(1,140,194,str);

    sprintf(str,"SHIPS : %i",totalo);
    apigameprint3(1,270,176,str);
    sprintf(str,"CREATE: %i",totalco);
    apigameprint3(1,270,182,str);
    sprintf(str,"EVENTS: %i",totaloe);
    apigameprint3(1,270,188,str);
    sprintf(str,"COMMS : %i",totalce);
    apigameprint3(1,270,194,str);

    for (f=1; f<ships; f++)
    {
      if ((o[f].cz>10)&&(o[f].type>-1))
      {
        xold=(o[f].cx)/(o[f].cz/0x100);
        yold=(o[f].cy)/(o[f].cz/0x100);
        g=2;
        if ((f==target) && (current==0) ) g=1;
        if ( (xold>-150)&&(xold<150)&&(yold>-90)&&(yold<90) )
        {
          apiline(g,158+xold,(100+yold),156+xold,(100+yold));
          apiline(g,162+xold,(100+yold),164+xold,(100+yold));
        }
      }
      if ((co[f].cz>10)&&(co[f].time>-1))
      {
        xold=(co[f].cx)/(co[f].cz/0x100);
        yold=(co[f].cy)/(co[f].cz/0x100);
        g=2;
        if ((f==target) && (current==1) ) g=1;
        if ( (xold>-150)&&(xold<150)&&(yold>-90)&&(yold<90) )
        {
          apiline(g,158+xold,(100+yold),156+xold,(100+yold));
          apiline(g,162+xold,(100+yold),164+xold,(100+yold));
        }
      }
    }
    if (current==9)
    {
      strcpy(str,"1) BRIEFING");
      apigameprint3(1,10,6,str);
      strcpy(str,"2) GOOD END");
      apigameprint3(1,10,12,str);
      strcpy(str,"3) BAD END");
      apigameprint3(1,10,18,str);
      if(key[_1]) blurb(filename);
      if(key[_2]) goodend(filename);
      if(key[_3]) badend(filename);
    }
    if (key[_F1]) { current=0; if(target>ships) target=0; }
    if (key[_F2]) { current=1; if(target>ships || target<1) target=1; }
    if (key[_F3]) { current=2; if(target>ships || target<1) target=1; }
    if (key[_F4]) { current=3; /*target=0;*/ }
    if (key[_F5]) { current=4; target=290; }
    if (key[_F6]) { current=5; if(target>ships) target=0; }
    if (key[_F7]) { current=6; if(target>8 || target<1) target=0; }
    if (key[_Q]) { current=7; target=ground; }
    if (key[_Z]) { current=8; }
    if (key[_F8]){current=9;}
    // blurb(filename);
    if (key[_F9])
    {
      delay(150);
      if (norand==0) norand=1;
      else norand=0;
    }
    if (key[_F10])
    {
      reset();
      target=0;
      current=0;
      o[0].speed=0;
      o[0].rsx=o[0].rsy=o[0].rsz=0;
    }
    if(key[_SLASH])
    {
      if(startflag==HYPERSPACEIN) startflag=TAKEOFF;
      else startflag=HYPERSPACEIN;
    }
    if (key[_FULL_STOP] && finishtime<1000)
    {
      finishtime++;
      if (key[_SPACEBAR] && finishtime<990) finishtime+=9;
    }
    if (key[_COMMA] && finishtime>0)
    {
      finishtime--;
      if (key[_SPACEBAR] && finishtime>10) finishtime-=9;
    }
    if (key[_L])
    {
      reset();
      load(filename);
      o[0].speed=0;
    }

    if (key[_K]) save (filename) ;
    if (key[_F]) input(&filename,5,176);
    if (key[_F1] || key[_F2] || key[_F3] ||
        key[_F4] || key[_F5] || key[_F6] ||
        key[_F7] || key[_F] || key[_K] ||
        key[_L] || key[_F9] || key[_F10] || key[_SLASH] || key[_Q] ||
        key[_FULL_STOP] || key[_COMMA] || key[_Z])
    {
      apigameprint3(1,255,6,"F1 : OBJECTS");
      apigameprint3(1,255,12,"F2 : POSSIBLES");
      apigameprint3(1,255,18,"F3 : EVENTS");
      apigameprint3(1,255,24,"F4 : COMMS");
      apigameprint3(1,255,30,"F5 : STELLARS");
      apigameprint3(1,255,36,"F6 : OBJECTIVES");
      apigameprint3(1,255,42,"F7 : WAYPOINTS");
      apigameprint3(1,255,48,"F8 : BRIEFING");
      apigameprint3(1,255,54,"F9 : MSN TYPE");
      apigameprint3(1,255,60,"<> : MSN TIME");
      apigameprint3(1,255,66,"Q  : GROUND");
      apigameprint3(1,255,72,"F  : FILENAME");
      apigameprint3(1,255,78,"K  : KEEP MSN");
      apigameprint3(1,255,84,"L  : LOAD MSN");
      apigameprint3(1,255,90,"F10: RESET MSN");
      apigameprint3(1,255,96,"/  : STARTFLAG");
      apigameprint3(1,255,102,"Z  : MACROS");

    }
    apiline(213,150,100,155,100);
    apiline(213,165,100,170,100);

    if(current==0)
    {
      if (key[_V] )
      {
        if (o[target].guns>0) for (g=0; g<o[target].guns; g++)
        {
          xold=((o[target].cx + o[target].dx[0] * 0.8) + (o[target].dx[2] * o[target].g[g].fireflag * o[target].g[g].gun ) + (o[target].dx[0] * o[target].g[g].gunz ) + (o[target].dx[1] * o[target].g[g].guny ))
          / (((o[target].cz + o[target].dz[0] * 0.8) + (o[target].dz[2] * o[target].g[g].fireflag * o[target].g[g].gun ) + (o[target].dz[0] * o[target].g[g].gunz ) + (o[target].dz[1] * o[target].g[g].guny ))/0x100);
          yold=   ((o[target].cy + o[target].dy[0] * 0.8) + (o[target].dy[2] * o[target].g[g].fireflag * o[target].g[g].gun ) + (o[target].dy[0] * o[target].g[g].gunz ) + (o[target].dy[1] * o[target].g[g].guny )) /
          (((o[target].cz + o[target].dz[0] * 0.8) + (o[target].dz[2] * o[target].g[g].fireflag * o[target].g[g].gun ) + (o[target].dz[0] * o[target].g[g].gunz ) + (o[target].dz[1] * o[target].g[g].guny ))/0x100);
          safelineellipse(2,160+xold,(100+yold), 7,7);
          f=22;
        }
      }
      if (key[_B])
      {
        xold=(o[target].cx)/(o[target].cz/0x100);
        yold=(o[target].cy)/(o[target].cz/0x100);
        if ( (xold>-150)&&(xold<150)&&(yold>-90)&&(yold<90) )
        {
          if (o[target].crashzones>0) for (g=0; g<o[target].crashzones; g++)
          {
            tx=o[target].cx+(0.01*( (o[target].dx[2]*o[target].c[g].x)+(o[target].dx[0]*o[target].c[g].z)+(o[target].dx[1]*o[target].c[g].y)   )  ) ;
            ty=o[target].cy+(0.01*( (o[target].dy[2]*o[target].c[g].x)+(o[target].dy[0]*o[target].c[g].z)+(o[target].dy[1]*o[target].c[g].y)   )  ) ;
            tz=o[target].cz+(0.01*( (o[target].dz[2]*o[target].c[g].x)+(o[target].dz[0]*o[target].c[g].z)+(o[target].dz[1]*o[target].c[g].y)   )  ) ;
            xold = (tx/(tz/0x100));
            yold = (ty/(tz/0x100));
            zold= o[target].c[g].size / (o[target].cz / 0x100) ;
            safelineellipse(1,160+xold,(100+yold), zold,zold);
          }
        }
      }
    }

    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);
}


