#include <stdio.h>
#include <dos.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <malloc.h>
#include <io.h>
#include "3dproj.h"
#include "3ddefs.h"
#include "3dprep.h"
#include "3dvarse.h"

void newside(void)
{
  int f,temp,quit,big;
  long frame1=0;//,fadeflag=0;

  createguiobject(9,0,31, 1000,-5,3000, 4,25,-3 ,0,0,0 ,0,0,0);
  createguiobject(8,0,71, 0,0 ,0, 4,25,-3 ,0,0,0 ,0,0,0);
//  createguiobject(7,0,23, -1000,1000 ,0, 4,25,-3 ,0,0,0 ,0,0,0);
  createguiobject(1,0,36, -2000,0 ,900, 4,2,-2 ,1,-0.4,2 ,0,0,0);
  createguiobject(2,0,36, 1000,-1000 ,-500, 4,2,1 ,-1,-0.4,0.2 ,0,0,0);
  createguiobject(3,0,36, 4000,2000 ,-1200, 4,1,3 ,0.2,1,0 ,0,0,0);
  createguiobject(4,0,36, 3000,-1000 ,0, 0,0,-3 ,0,0,0.1 ,0,0,0);

  light=dddCreateLight(VECTOR_LIGHT,255,1000);
  camera=dddCreateCamera(32000,100,100);
  viewz=1000;
  big=1;
  while (big==1)
  {
    quit=0;
    while (quit==0)
    {
      frame1++;
      apisetviewangles(0,0,0);
      apisetviewpos(0,0,-1000);
      jjjtimer_start(0);
      apiclearcliparea(); // Set up the view
      apiclearobjects();
      drawstarfield(0,0.4,0);
      drawguiobjects(0,0.4,0);
      dddSelectCamera(camera);
      dddInsertObject(light);
      apidrawobjects();
      apiniceprint3(1,130,93,30,"S   E   C   E   S   S   I   O   N");
      if(demo!=0) apiniceprint3(32,40,302,194,"DEMO");

      apiline(1, 90,37,230,37);
      apiline(1, 90,27,230,27);
      for(f=0; f<16; f++)
      {
        apiplot(175-f,231+f,37);
        apiplot(175-f,89-f,37);
        apiplot(175-f,231+f,27);
        apiplot(175-f,89-f,27);
      }
      if(demo!=2)
      {
        apiniceprint3(119,134, 75 , 69 , "  1: BEGIN BELT CAMPAIGN" );
        apiniceprint3(32,38, 75 , 79 , "  2: BEGIN UEA CAMPAIGN" );
        apiniceprint3(119,134, 75 , 89 , "  3: CONTINUE THE FIGHT" );
        apiniceprint3(119,134, 75 , 99 , "  4: PLOT" );
        apiniceprint3(119,134, 75 , 109 , "  5: CREDITS" );
        apiniceprint3(119,134, 75 , 119 , "  0: QUIT" );
      }
      else if(demo==2)
      {
        apiniceprint3(119,134, 75 , 69 , "  1: BEGIN DEMO CAMPAIGN" );
        apiniceprint3(32,38, 75 , 79 , "  2: BEGIN UEA CAMPAIGN" );
        apiniceprint3(119,134, 75 , 89 , "  3: CONTINUE THE FIGHT" );
        apiniceprint3(119,134, 75 , 99 , "  4: PLOT" );
        apiniceprint3(119,134, 75 , 109 , "  5: CREDITS" );
        apiniceprint3(119,134, 75 , 119 , "  0: QUIT" );
      }
      while (this_time<maxspeed) this_time=apireadtimer();
      apiscreenswap();
      if(key[_0] || key[_ESC]) quit=-1;
      if(key[_1]) quit=1;
      if(key[_3]) quit=3;
      if(key[_4]) printcredits1();
      if(key[_5]) printcredits2();
    }

  //  if(key[_0] ||key[_ESC])
    if(quit==-1)
    {
      while(key[_0]) {}
      while(key[_ESC]) {}
      quit=1;
      pilot.side=-1;
      return;
    }
//  if(key[_1])
    if(quit==1)
    {
      while(key[_1]) {}
      f=newpilot();
      if(f==0)
      {
        pilot.side=1;
        pilot.campaign=1;
        pilot.mission=1;
        numcampaigns=10;
        pilot.campaignstatus=CAMPAIGN_SUCCESS;  //Default val for campstatus
        savepilot();
        quit=1;
        return;
      }
      else
      {
        while(key[_ESC]) {}
      }
    }
/*  if(key[_2])
  {
    while(key[_2]) {}
    newpilot();
    pilot.side=2;
    pilot.campaign=1;
    pilot.mission=1;
    numcampaigns=10;
    pilot.campaignstatus=CAMPAIGN_SUCCESS;  //Default val for campstatus
    savepilot();
    return;
  }
*/
//  if(key[_3])
    if(quit==3)
    {
      while(key[_3]) {}
      temp=awakenpilot();
      if(temp!=-1)
      {
        quit=1;
        numcampaigns=10;
        return;
      }
    }
  }
}

void  newcampaign()
{
  int f;
  if(pilot.mission>5)
  {
    for(f=0;f<49;f++) dpt[f]=1;
    pilot.campaign++;
    pilot.mission=1;
  }
  if(demo==2)
  {
    if(pilot.campaign==1)
    {
        switch(pilot.mission)
        {
          case 1:
            sprintf(missionname[0],"BSC3");
            missiontune[0]=2; missiontype[0]=MISSION_ALWAYS;  missionessential[0]=MISESS_NO;
            sprintf(missionname[1],"BS2");
            missiontune[1]=2; missiontype[1]=MISSION;         missionessential[1]=MISESS_NO;
            nummissions=2; actualmission=1;
            break;
          case 2:
            sprintf(missionname[0],"BSC5");
            missiontune[0]=1; missiontype[0]=MISSION_ALWAYS;  missionessential[0]=MISESS_NO;
            sprintf(missionname[1],"BENDEM1");
            missiontune[1]=1; missiontype[1]=MISSION; missionessential[1]=MISESS_NO;
            nummissions=2; actualmission=1;
            break;
          case 3:
            sprintf(missionname[0],"BSC6");
            missiontune[0]=1; missiontype[0]=MISSION; missionessential[0]=MISESS_NO;
            sprintf(missionname[1],"SAT5");
            missiontune[1]=1; missiontype[1]=MISSION; missionessential[1]=MISESS_NO;
            nummissions=2; actualmission=1;
            break;
          case 4:
            sprintf(missionname[0],"CLEAN3");
            missiontune[0]=3; missiontype[0]=MISSION; missionessential[0]=MISESS_NO;
            nummissions=1; actualmission=0;
            break;
          case 5:
            sprintf(missionname[0],"SAC4");
            missiontune[0]=2; missiontype[0]=MISSION_ALWAYS;  missionessential[0]=MISESS_NO;
            sprintf(missionname[1],"SA4");
            missiontune[1]=2; missiontype[1]=MISSION; missionessential[1]=MISESS_NO;
            nummissions=2; actualmission=1;
            break;
        }
    }
  }
  else if(pilot.side==1)
  {
    if(pilot.campaign==1)
    {
        switch(pilot.mission)
        {
          case 1:
            sprintf(missionname[0],"BSC1");
            missiontune[0]=1; missiontype[0]=MISSION_ALWAYS;  missionessential[0]=MISESS_NO;
            sprintf(missionname[1],"BS1");
            missiontune[1]=1; missiontype[1]=MISSION; missionessential[1]=MISESS_NO;
            sprintf(missionname[2],"BSGE1");
            missiontune[2]=1; missiontype[2]=MISSION_SUCCESS; missionessential[2]=MISESS_NO;
            sprintf(missionname[3],"BSBE1");
            missiontune[3]=1; missiontype[3]=MISSION_FAILURE; missionessential[3]=MISESS_NO;
            nummissions=4; actualmission=1;
            break;
          case 2:
            sprintf(missionname[0],"BSC3");
            missiontune[0]=2; missiontype[0]=MISSION_ALWAYS;  missionessential[0]=MISESS_NO;
            sprintf(missionname[1],"BS2");
            missiontune[1]=2; missiontype[1]=MISSION; missionessential[1]=MISESS_NO;
            nummissions=2; actualmission=1;
            break;
          case 3:
            sprintf(missionname[0],"BSC4");
            missiontune[0]=1; missiontype[0]=MISSION_ALWAYS;  missionessential[0]=MISESS_NO;
            sprintf(missionname[1],"BS3");
            missiontune[1]=1; missiontype[1]=MISSION; missionessential[1]=MISESS_NO;
            nummissions=2; actualmission=1;
            break;
          case 4:
            sprintf(missionname[0],"BSC5");
            missiontune[0]=3; missiontype[0]=MISSION_ALWAYS; missionessential[0]=MISESS_NO;
            sprintf(missionname[1],"BS4");
            missiontune[1]=3; missiontype[1]=MISSION; missionessential[1]=MISESS_NO;
            sprintf(missionname[2],"BSGE2");
            missiontune[2]=3; missiontype[2]=MISSION_SUCCESS; missionessential[2]=MISESS_NO;
            nummissions=3; actualmission=1;
            break;
          case 5:
            sprintf(missionname[0],"BSC6");
            missiontune[0]=2; missiontype[0]=MISSION_ALWAYS; missionessential[0]=MISESS_NO;
            sprintf(missionname[1],"BS5");
            missiontune[1]=2; missiontype[1]=MISSION; missionessential[1]=MISESS_NO;
            nummissions=2; actualmission=1;
            break;
        }
    }
    if(pilot.campaign==2)
    {
        switch(pilot.mission)
        {
          case 1:
            sprintf(missionname[0],"AC1");
            missiontune[0]=1; missiontype[0]=MISSION_ALWAYS;  missionessential[0]=MISESS_NO;
            nummissions=1; actualmission=0;
            break;
          case 2:
            sprintf(missionname[0],"AC2");
            missiontune[0]=2; missiontype[0]=MISSION; missionessential[0]=MISESS_NO;
            nummissions=1; actualmission=0;
            break;
          case 3:
            sprintf(missionname[0],"AC3");
            missiontune[0]=3; missiontype[0]=MISSION; missionessential[0]=MISESS_NO;
            nummissions=1; actualmission=0;
            break;
          case 4:
            sprintf(missionname[0],"AC4");
            missiontune[0]=1; missiontype[0]=MISSION; missionessential[0]=MISESS_NO;
            nummissions=1; actualmission=0;
            break;
          case 5:
            sprintf(missionname[0],"AC5");
            missiontune[0]=2; missiontype[0]=MISSION; missionessential[0]=MISESS_NO;
            nummissions=1; actualmission=0;
            break;
        }
    }
    if(pilot.campaign==3)
    {
        switch(pilot.mission)
        {
          case 1:
            sprintf(missionname[0],"SA1");
            missiontune[0]=2; missiontype[0]=MISSION_ALWAYS;  missionessential[0]=MISESS_NO;
            nummissions=1; actualmission=0;
            break;
          case 2:
            sprintf(missionname[0],"SA2");
            missiontune[0]=3; missiontype[0]=MISSION; missionessential[0]=MISESS_NO;
            nummissions=1; actualmission=0;
            break;
          case 3:
            sprintf(missionname[0],"SA3");
            missiontune[0]=1; missiontype[0]=MISSION; missionessential[0]=MISESS_NO;
            nummissions=1; actualmission=0;
            break;
          case 4:
            sprintf(missionname[0],"SA4");
            missiontune[0]=2; missiontype[0]=MISSION; missionessential[0]=MISESS_NO;
            nummissions=1; actualmission=0;
            break;
          case 5:
            sprintf(missionname[0],"SA5");
            missiontune[0]=1; missiontype[0]=MISSION; missionessential[0]=MISESS_NO;
            nummissions=1; actualmission=0;
            break;
        }
    } // end of belt campaigns
  } // end of pilot side=1
  else if(pilot.side==2)
  {
    switch(pilot.campaign)
    {
      case 1: //Campaign one for the uea plot tree
        switch(pilot.mission)
        {
          case 1:
            sprintf(missionname[0],"USCUT1"); missiontype[0]=MISSION_ALWAYS;  missionessential[0]=MISESS_NO;
            sprintf(missionname[1],"US1");    missiontype[1]=MISSION;         missionessential[1]=MISESS_NO;
            sprintf(missionname[2],"USCUT3"); missiontype[2]=MISSION_SUCCESS; missionessential[2]=MISESS_NO;
            sprintf(missionname[3],"USCUT4"); missiontype[3]=MISSION_FAILURE; missionessential[3]=MISESS_NO;
            nummissions=4; actualmission=1;
            break;
          case 2:
            sprintf(missionname[0],"US2");  missiontype[0]=MISSION; missionessential[0]=MISESS_NO;
            nummissions=1; actualmission=0;
            break;
        } // end of pilot missions for campaign 1
        break;
      case 2: // campaign 2 for the UEA
        break;
      break;
    } // end of pilot campaign switch
  } // end of pilot side=2
}

void loadwindowships()
{
  int f;
  for(f=0;f<10;f++) gui[f].type=-1;
  if (pilot.side==1) // Belt campaigns
  {
    switch(pilot.campaign)
    {
      case 1:
        createguiobject(9,0,32, 1000,-5,6000, 5,-25,-5 ,0,0,0 ,0,0,0); // SATURN
        createguiobject(1,0,36, -3000,0 ,6000, 4,25,-2 ,1,0,1 ,0,0,0);
        createguiobject(2,0,37, 1000,-100 ,3500, 4,2,1 ,-1,1,0 ,0,0,0);
        createguiobject(8,0,23, 2000,0 ,2000, 0,25,0 ,0,0,0 ,0,0,0);
        createguiobject(7,0,23, 2000,-200,3000, 0,25,0 ,0,0,0 ,0,0,0);
        break;
      case 2:
        createguiobject(9,0,21, 1000,-5,6000, 0,25,0 ,0,0,0 ,0,0,0); // SATURN
        createguiobject(8,0,21, 2000,-200,3000, 0,25,0 ,0,0,0 ,0,0,0);
        createguiobject(7,0,21, -1000,-5+100,6000, 0,25,0 ,0,0,0 ,0,0,0); // SATURN
        createguiobject(6,0,21, 0,-200+100,3000, 0,25,0 ,0,0,0 ,0,0,0);
        createguiobject(5,0,25, 800,300,5000, 0,25,0 ,0,0,0 ,0,0,0);
        break;
      case 3:
        createguiobject(9,0,21, 1000,-5,6000, 0,25,0 ,0,0,0 ,0,0,0); // SATURN
        createguiobject(8,0,21, 2000,-200,3000, 0,25,0 ,0,0,0 ,0,0,0);
        createguiobject(7,0,21, -1000,-5+100,6000, 0,25,0 ,0,0,0 ,0,0,0); // SATURN
        createguiobject(6,0,21, 0,-200+100,3000, 0,25,0 ,0,0,0 ,0,0,0);
        createguiobject(5,0,33, 0,0,6000, 0,25,0 ,0,0,0 ,0,0,0);
        break;
    }
  }
  if (pilot.side==2) // UEA campaigns
  {
    switch(pilot.campaign)
    {
      case 1:
        createguiobject(9,0,32, -3000,-5,8000, 5,-25,-5 ,0,0,0 ,0,0,0); // SATURN
        createguiobject(8,0,24, 2000,0 ,1000, 0,25,0 ,0,0,0 ,0,0,0); // Destroyer
        createguiobject(7,0,26, 2000,-200,3000, 0,25,0 ,0,0,0 ,0,0,0); // Battleship
        createguiobject(6,0,24, 1000,0 ,5000, 0,25,0 ,0,0,0 ,0,0,0); // Destroyer
        break;
    }
  }
  camera=dddCreateCamera(32000,100,100);
  light=dddCreateLight(VECTOR_LIGHT,255,0);
  dddRotateTo(light,0,12000,8000);
}

int roomlounge() //Controlflag 0, returns what to do next
{
  long frame1=0;
  int oldbutton,returnval=-1;
  float rx,ry,rz,pz,ox,rsy;
  VtkObj room,light2;

  room=dddLoad("main2.3dw");
  light2=dddCreateLight(OMNI_LIGHT,25,0);
  dddMoveTo(light2,0,0,100);
  camera=dddCreateCamera(32000,100,100);
  rx=ry=rz=0;

  ox=apimouse_x;
  oldbutton=apilbutton;
  apigetmousepos();
  rsy=((512-ox)-(512-apimouse_x))*75/16384;
  ry=(512-apimouse_x)*6;
  pz=1250-(512-apimouse_y/2);
  while(!key[_ESC])
  {
    p[shapes-8].cx-=2;
    p[shapes-7].cx-=2;
    frame1++;
    apisetviewangles(0,0,0);
    apisetviewpos(0,0,0);
    jjjtimer_start(0);
    apiclearcliparea();         // Set up the view
    apiclearobjects();
    drawstarfield(0,rsy,0);
    drawguiobjects(0,rsy,0);
    dddInsertObject(light);
    dddSelectCamera(camera);
    apidrawobjects();
    apiclearobjects();
    dddSelectCamera(camera);
    dddInsertObject(light2);
    apiguishape(DRAWROOM,room,1,0,0,pz,rx,ry,rz );
    apidrawobjects();
    if ((apimouse_x>900)&&(apimouse_x<1024)&&(apimouse_y>300)&&(apimouse_y<700))
    {
      apiniceprint3(21,36, 250 , 124 ,  "BRIEFING"  );
      if (oldbutton==0 && apilbutton==1) { returnval=2; break; } //Go to briefing room
    }
    if ((apimouse_x>=0)&&(apimouse_x<150)&&(apimouse_y>300)&&(apimouse_y<700))
    {
      apiniceprint3(21,36, 20 , 124 ,  "QUARTERS"  );
      if (oldbutton==0 && apilbutton==1) { returnval=1; break; }
    }
    if ((apimouse_x>150)&&(apimouse_x<300)&&(apimouse_y>600)&&(apimouse_y<900))
    {
      apiniceprint3(21,36, 50 , 124 ,  "DATABASE"  );
      if (oldbutton==0 && apilbutton==1) { returnval=3; break; }
    }
    if ((apimouse_x>300)&&(apimouse_x<450)&&(apimouse_y>600)&&(apimouse_y<900))
    {
      apiniceprint3(21,36, 80 , 124 ,  "RECORD"  );
      if (oldbutton==0 && apilbutton==1) { returnval=9; break; }
    }
    apiniceprint3(21,36, 5 , 5 ,  "MAIN LOUNGE"  );

    if ((apimouse_x>=0)&&(apimouse_x/3.3<319)&&(apimouse_y>=0)&&(apimouse_y/5.3<199))
    {
      apiline(1 ,1+(apimouse_x/3.3)-1 , (apimouse_y/5.3),1+(apimouse_x/3.3)+1,(apimouse_y/5.3));
      apiline(1 ,1+(apimouse_x/3.3) , (apimouse_y/5.3)-1,1+(apimouse_x/3.3),(apimouse_y/5.3)+1);
    }
//    sprintf(s1,"MISSION   %i",pilot.mission);
//    apigameprint3(1, 10 , 20 , s1  );
//    sprintf(s1,"TERMINATE %i",terminateflag);
//    apigameprint3(1, 10 , 30 , s1  );
    apiscreenswap();
    ox=apimouse_x;
    oldbutton=apilbutton;
    apigetmousepos();
    rsy=((512-ox)-(512-apimouse_x))*75/16384;
    ry=(512-apimouse_x)*6;
    pz=1250-(512-apimouse_y/2);

    while (this_time < maxspeed) this_time = apireadtimer();
  }
  //Hack to reposition mouse before moving rooms
  ox=apimouse_x;
  oldbutton=apilbutton;
  apimouse_x=512;
  rsy=((512-ox)-(512-apimouse_x))*75/16384;
  ry=(512-apimouse_x)*6;
  pz=1250-(512-apimouse_y/2);
  drawstarfield(0,rsy,0);
  drawguiobjects(0,rsy,0);
  while(key[_ESC]) {}
  return(returnval);
}

int roomquarters() //Controlflag 1, returns what to do next
{
  long frame1=0;
  int oldbutton,returnval=-1;
  float rx,ry,rz,pz,ox,rsy;
  VtkObj room,light2;

  room=dddLoad("main1.3dw");
  light2=dddCreateLight(OMNI_LIGHT,25,0);
  dddMoveTo(light2,0,0,100);
  camera=dddCreateCamera(32000,100,100);
  rx=ry=rz=0;
  ox=apimouse_x;
  oldbutton=apilbutton;
  apigetmousepos();
  rsy=((512-ox)-(512-apimouse_x))*1000/65536;
  ry=(512-apimouse_x)*20;
  pz=750-(512-apimouse_y);
  while(!key[_ESC])
  {
    p[shapes-8].cx-=2;
    p[shapes-7].cx-=2;
    frame1++;
    apisetviewangles(0,0,0);
    apisetviewpos(0,0,0);
    jjjtimer_start(0);
    apiclearcliparea();         // Set up the view
    apiclearobjects();
    drawstarfield(0,rsy,0);
    drawguiobjects(0,rsy,0);
    dddInsertObject(light);
    dddSelectCamera(camera);
    apidrawobjects();
    apiclearobjects();
    dddSelectCamera(camera);
    dddInsertObject(light2);

    apiguishape(DRAWROOM,room,1,0,0,pz, rx,ry,rz );
    apidrawobjects();
    if ((apimouse_x>900)&&(apimouse_x<1024)&&(apimouse_y>300)&&(apimouse_y<800))
    {
      apiniceprint3(21,36, 270 , 94 ,  "LOUNGE"  );
      if (oldbutton==0 && apilbutton==1) { returnval=0; break; }
    }
    if ((apimouse_x>100)&&(apimouse_x<500)&&(apimouse_y>700)&&(apimouse_y<950))
    {
      apiniceprint3(21,36,70,144 ,"AWAKEN PILOT");
      if (oldbutton==0 && apilbutton==1) { returnval=10; break; }
    }
    apiniceprint3(21,36,5,5,"SLEEPING QUARTERS");
    if ((apimouse_x>=0)&&(apimouse_x/3.3<319)&&(apimouse_y>=0)&&(apimouse_y/5.3<199))
    {
      apiline(1 ,1+(apimouse_x/3.3)-1 , (apimouse_y/5.3),1+(apimouse_x/3.3)+1,(apimouse_y/5.3));
      apiline(1 ,1+(apimouse_x/3.3) , (apimouse_y/5.3)-1,1+(apimouse_x/3.3),(apimouse_y/5.3)+1);
    }
    apiscreenswap();
    ox=apimouse_x;
    oldbutton=apilbutton;
    apigetmousepos();
    rsy=((512-ox)-(512-apimouse_x))*1000/65536;
    ry=(512-apimouse_x)*20;
    pz=750-(512-apimouse_y);

    while (this_time < maxspeed) this_time = apireadtimer();
  }
  ox=apimouse_x;
  oldbutton=apilbutton;
  apimouse_x=512;
  rsy=((512-ox)-(512-apimouse_x))*1000/65536;
  ry=(512-apimouse_x)*20;
  pz=750-(512-apimouse_y);
  drawstarfield(0,rsy,0);
  drawguiobjects(0,rsy,0);

  while(key[_ESC]) {}
  return(returnval);
}

int roombriefing() //Controlflag 2, returns what to do next
{
  long frame1=0;
  int oldbutton,returnval=-1;
  float rx,ry,rz,pz,ox,rsy;
  VtkObj room,light2;

  room=dddLoad("main3.3dw");
  light2=dddCreateLight(OMNI_LIGHT,25,0);
  dddMoveTo(light2,0,0,100);
  camera=dddCreateCamera(32000,100,100);
  rx=ry=rz=0;
  ox=apimouse_x;
  oldbutton=apilbutton;
  apigetmousepos();
  rsy=((512-ox)-(512-apimouse_x))*1000/65536;
  ry=(512-apimouse_x)*20;
  pz=750-(512-apimouse_y/2);
  while(!key[_ESC])
  {
    p[shapes-8].cx-=2;
    p[shapes-7].cx-=2;
    frame1++;
    apisetviewangles(0,0,0);
    apisetviewpos(0,0,0);
    jjjtimer_start(0);
    apiclearcliparea();         // Set up the view
    apiclearobjects();
    drawstarfield(0,rsy,0);
    drawguiobjects(0,rsy,0);
    dddInsertObject(light);
    dddSelectCamera(camera);
    apidrawobjects();
    apiclearobjects();
    dddSelectCamera(camera);
    dddInsertObject(light2);
    apiguishape(DRAWROOM,room,1,0,150,pz, rx,ry,32676-rz );
    apidrawobjects();

    if ((apimouse_x>250)&&(apimouse_x<650)&&(apimouse_y>650)&&(apimouse_y<850))
    {
      apiniceprint3(21, 36,140 , 154 ,  "HOLOGRAM"  );
      if (oldbutton==0 && apilbutton==1) { returnval=5; break; }//DO BRIEFING
    }
    if ((apimouse_x>0)&&(apimouse_x<150)&&(apimouse_y>200)&&(apimouse_y<800))
    {
      apiniceprint3(21,36, 10 , 104 ,  "LOUNGE"  );
      if (oldbutton==0 && apilbutton==1) { returnval=0; break; }
    }
    if ((apimouse_x>250)&&(apimouse_x<450)&&(apimouse_y>300)&&(apimouse_y<600))
    {
      apiniceprint3(21, 36,70 , 54 ,  "BACKGROUND"  );
      if (oldbutton==0 && apilbutton==1) { returnval=4; break; } //DO BACKGROUND
    }
    if ((apimouse_x>650)&&(apimouse_x<800)&&(apimouse_y>200)&&(apimouse_y<800))
    {
      apiniceprint3(21,36, 200 , 104 ,  "LAUNCH"  );
      if (oldbutton==0 && apilbutton==1) { returnval=6; break; }
    }
    apiniceprint3(21,36, 5 , 5 ,  "BRIEFING ROOM"  );
    if ((apimouse_x>=0)&&(apimouse_x/3.3<319)&&(apimouse_y>=0)&&(apimouse_y/5.3<199))
    {
      apiline(1 ,1+(apimouse_x/3.3)-1 , (apimouse_y/5.3),1+(apimouse_x/3.3)+1,(apimouse_y/5.3));
      apiline(1 ,1+(apimouse_x/3.3) , (apimouse_y/5.3)-1,1+(apimouse_x/3.3),(apimouse_y/5.3)+1);
    }
    apiscreenswap();
    ox=apimouse_x;
    oldbutton=apilbutton;
    apigetmousepos();
    rsy=((512-ox)-(512-apimouse_x))*1000/65536;
    ry=(512-apimouse_x)*20;
    pz=750-(512-apimouse_y/2);

    while (this_time < maxspeed) this_time = apireadtimer();
  }
  ox=apimouse_x;
  oldbutton=apilbutton;
  apimouse_x=512;
  rsy=((512-ox)-(512-apimouse_x))*1000/65536;
  ry=(512-apimouse_x)*20;
  pz=750-(512-apimouse_y/2);
  drawstarfield(0,rsy,0);
  drawguiobjects(0,rsy,0);

  while(key[_ESC]) {}
  return(returnval);
}

int database(void)
{
  char str[100],str2[100];
  int f,g,place,buttons,oldmouse_x;
  int flag=1,ship=10;
  float d=0.5,e=-0.3,dd=0.01;

  free_memory_for_ship(50);
  createship(50,1,ship, 10,10,10, 0,9000,0, 1.5,1.5,0, 0,0,0,  0,-1,0, 10,10,10,0, "DAVE",1,-1,-1,0);
  if(o[50].crashzones==0) { o[50].cx=0; o[50].cy=40; o[50].cz=300; }
  else { o[50].cx=0; o[50].cy=200; o[50].cz=2000; }

  light=dddCreateLight(VECTOR_LIGHT,255,0);
  camera=dddCreateCamera(32000,100,100);

  oldmouse_x=apimouse_x;
  while(!(key[_RETURN]) && !(key[_ESC]))
  {
    apisetviewangles(0,0,0);
    apisetviewpos(0,0,0);
    jjjtimer_start(0);
    apiclearcliparea();         // Set up the view
    apiclearobjects();
    apiclearobjects();
    drawstarfield(0,0,0);
    apidrawobjects();

    o[50].rsz=0;
    o[50].rsy=e;
    o[50].rsx=d; //+
    d+=dd;
    if(d>1) dd=-0.01;
    if(d<-1) dd=0.01;
    if(o[50].rsx>1.5 || o[50].rsx<-1.5) d=-d;
    if(o[50].rsz>1.5 || o[50].rsz<-1.5) e=-e;

    apiguishape(DRAWFULL,o[50].object,1,o[50].cx,o[50].cy,o[50].cz,o[50].rx,o[50].ry,o[50].rz);
    for(g=0;g<o[50].turrets;g++)
    {
      apiguishape(DRAWFULL,o[50].t[g].object,1,
        o[50].cx+o[50].t[g].cx ,o[50].cy+o[50].t[g].cy ,o[50].cz+o[50].t[g].cz,
        o[50].t[g].rx,o[50].t[g].ry,o[50].t[g].rz );
      apiguishape(DRAWFULL,o[50].t[g].gun,1,
        o[50].cx+o[50].t[g].cx ,o[50].cy+o[50].t[g].cy ,o[50].cz+o[50].t[g].cz-2,
        o[50].t[g].srx,o[50].t[g].sry,o[50].t[g].srz );
    }
    for(g=0;g<o[50].hangerzones;g++) if (o[50].h[g].type==1)
    {
      apiguishape(DRAWFULL,o[50].h[g].object,1,
        o[50].cx+o[50].h[g].cx ,o[50].cy+o[50].h[g].cy ,o[50].cz+o[50].h[g].cz,
        o[50].h[g].rx,o[50].h[g].ry,o[50].h[g].rz );
    }

    dddInsertObject(light);
    dddSelectCamera(camera);
    apidrawobjects();
    spinobject(50, o[50].cx,o[50].cy,o[50].cz, o[50].rsx,o[50].rsy,-o[50].rsz ,2 );

    apigetmousepos();
    buttons=apibuttons;
    strcpy(str, shapedata[o[50].shape].name );
    strcat(str, " CLASS " );
    strcat(str,shapedata[o[50].shape].type);
    apiniceprint3(21,36,30,30,str);
    strcpy(str, "ENGINE THRUST          :" );
    gcvt( o[50].topspeed,7,str2); strcat(str2, "0 MPS SQUARED ");
    strcat(str,str2);
    apiniceprint3(21,36,30,44,str);
    strcpy(str, "SHIP ROLL/PITCH/YAW    :" );
    gcvt( o[50].toprevs,7,str2); strcat(str2, " RADS");
    strcat(str,str2);
    apiniceprint3(21,36,30,50,str);
    strcpy(str, "SHIELD WATTAGE         :" );
    gcvt( o[50].maxshields-o[50].side*5,7,str2); strcat(str2, "0 MW");

    strcat(str,str2);
    apiniceprint3(21,36,30,56,str);
    strcpy(str, "ARMOUR PLATING         :" );
    gcvt( o[50].maxarmour+o[50].side*5,7,str2); strcat(str2, " CM");
    strcat(str,str2);
    apiniceprint3(21,36,30,62,str);
    strcpy(str, "WEAPON MOUNTS          :" );
    gcvt( o[50].guns,7,str2);
    strcat(str,str2);
    apiniceprint3(21,36,30,68,str);
    place=74;
    for(f=0; f<o[50].guns; f++)
    {
      strcpy (str,"  MOUNT ");
      gcvt( 1+f,3,str2); strcat (str2," : ");
      strcat (str,str2);
      if (o[50].g[f].type==1) strcpy(str2, "PLASMA CANNON" );
      if (o[50].g[f].type==2) strcpy(str2, "ACCELERATED ANTI PROTON BEAM" );
      if (o[50].g[f].type==3) strcpy(str2, "ION CHUNK LAUNCHER" );
      if (o[50].g[f].type==4)
      {
        gcvt(o[50].g[f].left , 3, str2);
        strcat(str2, " HE BOMBS" );
      }
      if (o[50].g[f].type==5)
      {
        gcvt(o[50].g[f].left , 3, str2);
        strcat(str2, " CRITICAL MASS BOMBS" );
      }
      if (o[50].g[f].type==6)
      {
        gcvt(o[50].g[f].left , 3, str2);
        strcat(str2, " MATTER ANTIMATTER TORPEDOES" );
      }
      if (o[50].g[f].type==7)
      {
        gcvt(o[50].g[f].left , 3, str2);
        strcat(str2, " MISSILES" );
      }
      if (o[50].g[f].type==8) strcpy(str2, "HIGH ENERGY PLASMA CANNON" );
      if (o[50].g[f].type==11) strcpy(str2, "CHAIN CANNON" );
      strcat (str,str2);
      apiniceprint3(165,104,30,place,str);
      place+=6;
    }
    strcpy(str, "TURRETED CANNONS       :" );
    gcvt( o[50].turrets,7,str2);
    strcat(str,str2);
    apiniceprint3(21,36,30,place,str);
    place+=6;

    if ((apirbutton!=0 || key[_C_UP]) && flag==0)
    {
      ship++;
      flag=1;
      if (ship==17) ship++;
      if (ship>28) ship=10;
      if (o[50].guns>0)
      {
        free( o[50].g );     o[50].guns=0;
      }
      if (o[50].turrets>0)
      {
        free( o[50].t );  o[50].turrets=0;
      }
      if (o[50].crashzones>0)
      {
        free( o[50].c );o[50].crashzones=0;
      }
      o[50].type=-1;
      createship(50,1,ship, 10,10,10, 0,9000,0, 1.5,1.5,0, 0,0,0,  0,-1,0, 10,10,10,0, "DAVE",1,-1,-1,0);
      if(o[50].crashzones==0) { o[50].cx=0; o[50].cy=40; o[50].cz=300; }
      else { o[50].cx=0; o[50].cy=200; o[50].cz=2000; }
    }
    if ((apilbutton!=0 || key[_C_DOWN]) && flag==0)
    {
      ship--;
      flag=1;
      if (ship==17) ship--;
      if (ship<10) ship=28;
      if (o[50].guns>0)
      {
        free( o[50].g );     o[50].guns=0;
      }
      if (o[50].turrets>0)
      {
        free( o[50].t );  o[50].turrets=0;
      }
      if (o[50].crashzones>0)
      {
        free( o[50].c );o[50].crashzones=0;
      }
      o[50].type=-1;
      createship(50,1,ship, 10,10,10, 0,9000,0, 2.5,2.5,0, 0,0,0,  0,-1,0, 10,10,10,0, "DAVE",1,-1,-1,0);
      if(o[50].crashzones==0) { o[50].cx=0; o[50].cy=40; o[50].cz=300; }
      else { o[50].cx=0; o[50].cy=200; o[50].cz=2000; }
    }
    if (!apilbutton!=0 && !apirbutton!=0 && !key[_C_DOWN] && !key[_C_UP]) flag=0;
    if (flag>0)
    {
      flag++;
      if (flag>10) flag=0;
    }
    apiniceprint3(21,36, 280 , 194 ,  "RETURN"  );
    apiscreenswap();
    while (this_time < maxspeed) this_time = apireadtimer();
  }
  free_memory_for_ship(50); //Create ship;

  apimouse_x=oldmouse_x;
  if(key[_RETURN]) while(key[_RETURN]) {}
  if(key[_ESC]) while(key[_ESC]) {}
  return(0);
}

int indepth(void)
{
  FILE *stream;
  int f,oldmouse_x;
  char str2[100];
  char l[40][150];
  int lines;

  light=dddCreateLight(VECTOR_LIGHT,255,0);
  camera=dddCreateCamera(32000,100,100);
  strcpy(str2,missionname[actualmission] );
  strcat(str2,".MST");
  stream=fopen(str2,"r");
  for(f=0;f<35;f++) sprintf(l[f]," ");
  lines=0;
  do
  {
    fgets(l[lines],100,stream);
    lines++;
  }
  while(!feof(stream) && lines<30);
  fclose(stream);
  oldmouse_x=apimouse_x;
  while(!(key[_RETURN]) && !(key[_ESC]))
  {
    apisetviewangles(0,0,0);
    apisetviewpos(0,0,0);
    jjjtimer_start(0);
    apiclearcliparea();         // Set up the view
    apiclearobjects();
    drawstarfield(0.01,0.1,0);

    dddInsertObject(light);
    dddSelectCamera(camera);
    apidrawobjects();
    apiniceprint3(1,126,112,5,"B  R  I  E  F  I  N  G");
    for(f=0;f<lines;f++) apiniceprint3(144+(f/5),156, 10, 20+f*7, l[f]);

    apiniceprint3(21,36, 280 , 194 ,  "RETURN"  );
    apiscreenswap();
    while (this_time < maxspeed) this_time = apireadtimer();
  }
  apimouse_x=oldmouse_x;
  if(key[_RETURN]) while(key[_RETURN]) {}
  if(key[_ESC]) while(key[_ESC]) {}
  return(2);
}

int briefing( void )
{
  int f;
  long frame1=0;
  char s1[80];
  int objectives,oldmouse_x;
  int charges;
  int cy,cx;
  unsigned int y;
  float vx,vy,vz;

  light=dddCreateLight(VECTOR_LIGHT,255,0);
  camera=dddCreateCamera(32000,100,100);
  objectives=0;
  charges=0;
  for (f=0; f<ships; f++)
  {
    if (matter[f]==1) objectives++;
    if (matter[f]==2) charges++;
  }
  y=0;
  oldmouse_x=apimouse_x;
  vx=0;
  vy=40;
  vz=200;
  viewx=viewy=viewz=0;
  while (!(key[_RETURN]) && !(key[_ESC]))
  {
    frame1++;
    if(frame1>9) frame1=0;
    y-=655.35/5;
    rotatesummat(&vx,&vy,&vz,0,0.2,0);
    jjjtimer_start(0);
    apisetviewangles(0,y,0);
    apisetviewpos(0,0,0);
    apiclearcliparea();         // Set up the view
    apiclearobjects();
    drawstarfield(0,0,0);

//    drawplanets();
    for (f=shapes-7; f<shapes; f++) if (p[f].type>-1)
    {
      spinprojectile(f, 0,0,0, o[0].rsx,o[0].rsy,o[0].rsz,0);
      if (planets!=0) apiplanet(p[f].object,p[f].shape,viewx+p[f].cx,viewy+p[f].cy,viewz+p[f].cz , p[f].rx,p[f].ry,p[f].rz);
    }
    if(ground>0)drawground();
    dddInsertObject(light);
    dddRotateTo(light,0,-y,0);
    dddSelectCamera(camera);

    apidrawobjects();

    apiclearobjects();
    if (o[0].crashzones==0)
      apiguishape(DRAWFULL, o[0].object , 1, vx,vy,vz , o[0].rx,o[0].ry,o[0].rz);
    else
      apiguishape(DRAWFULL, o[0].object , 1, 0,200,2000 , o[0].rx,o[0].ry,o[0].rz);

    for (f=1; f<ships; f++) if (o[f].type>-1)
    {
      apiguishape(DRAWFULL, o[f].object , 1, o[f].cx,o[f].cy,o[f].cz ,o[f].rx,o[f].ry,o[f].rz);
      if(matter[f]==2 && frame1<5)
      {
        dddSetLineOnColor(150);
        apiguishape(DRAWLINE, o[f].object , 1, o[f].cx,o[f].cy,o[f].cz ,o[f].rx,o[f].ry,o[f].rz);
      }
      else if(matter[f]==1 && frame1>=5)
      {
        dddSetLineOnColor(160);
        apiguishape(DRAWLINE, o[f].object , 1, o[f].cx,o[f].cy,o[f].cz ,o[f].rx,o[f].ry,o[f].rz);
      }
    }

    dddSelectCamera(camera);
    dddInsertObject(light);
    apidrawobjects();
    apiniceprint3(1,130,105,30,"H   O   L   O   G   R   A   M");
    apiline(1, 90,37,230,37);
    apiline(1, 90,27,230,27);
    for(f=0; f<16; f++)
    {
      apiplot(175-f,231+f,37);
      apiplot(175-f,89-f,37);
      apiplot(175-f,231+f,27);
      apiplot(175-f,89-f,27);
    }
    strcpy( s1 , "CRAFT       : " );
    strcat( s1 , shapedata[o[0].shape].name );
    strcat( s1 , " CLASS " );
    strcat( s1 , shapedata[o[0].shape].type );
    apiniceprint3(21,36, 30 , 60 , s1 );
    if (objectives>0)
    apiniceprint3(160,103, 30 , 72 , "TARGETS" );
    cx=170; if (objectives==0) cx=30;
    if (charges>0)
    apiniceprint3(144,152, cx , 72 , "CHARGES" );
    cy=80;
    for (f=0; f<ships; f++)
    {
      if (matter[f]==1)
      {
        strcpy( s1 , shapedata[o[f].shape].type );
        strcat( s1 , " ");
        strcat( s1 , o[f].name );
        apiniceprint3(160,103, 30 , cy , s1 );
        cy+=6;
      }
    }
    cy=80;
    cx=170; if (objectives==0) cx=30;
    for (f=0; f<ships; f++)
    {
      if (matter[f]==2)
      {
        strcpy( s1 , shapedata[o[f].shape].type );
        strcat( s1 , " ");
        strcat( s1 , o[f].name );
        apiniceprint3(144,152, cx , cy , s1 );
        cy+=6;
      }
    }
    apiniceprint3(21,36, 290 , 194 ,  "RETURN"  );
    while (this_time < maxspeed) this_time = apireadtimer();
    apiscreenswap();
  }
  apimouse_x=oldmouse_x;
  if(key[_RETURN]) while(key[_RETURN]) {}
  if(key[_ESC]) while(key[_ESC]) {}
  return(2);
}

void  launch(void)
{
  int time;
  float x,y,z,rx,ry,rz;
  float sx,sy,sz,srx,sry,srz;
  int r1,r2;
  float c=2;
  VtkObj o1,o2,light,camera;
  light=dddCreateLight(VECTOR_LIGHT,255,0);
  dddRotateTo(light,8000,12000,12000);
  camera=dddCreateCamera(32000,100,100);
  o1=dddLoad(shapedata[70].filename);
  o2=dddLoad(shapedata[o[0].shape].filename);
//  sound1[0]=LoadWave("engine2.wav",&length1[0]);

  x=0; y=0; z=100;
  rx=12000; ry=0; rz=0;
  sx=0; sy=10; sz=100;
  srx=5000; sry=32767; srz=0;
  time=0;
  if (apirandom(10)>5) r1=1; else r1=-1;
  if (apirandom(10)>3) r2=1; else r2=1.5;
  while((time<220)&&(!key[_RETURN])&&(!key[_ESC]))
  {
    jjjtimer_start(0);
    frame2++; if (frame2>2) frame2=0;
    if(frame2==0) playing[0]=0;
    apiclearcliparea();         // Set up the view
    apiclearobjects();
    apiguishape(DRAWROOM,o1,1,x,y,z,rx,ry,rz);
    apiguishape(DRAWROOM,o2,1,sx,sy,sz,srx,sry,srz);
    dddInsertObject(light);
    dddSelectCamera(camera);
    apidrawobjects();
    time+=1*c;
    z+=10*c;
    rx-=10*c;
    if (time<50)
    {
      sz+=10*c;
      srx-=10*c;
//      playsoundeffect(0,(120-time)*50);
    }
    else if (time<100)
    {
      sz+=5*c;
      srx-=30*c;
      sy+=1*c;
//      playsoundeffect(0,(120-time)*50);
    }
    else if (time<150)
    {
      sz-=10*c;
      srx-=100*c;
      srz-=30*r1*c;
      sy-=1*c;
//      if(time<120) playsoundeffect(0,(120-time)*50);
//      else playsoundeffect(0,0);
    }
    else if (time<200)
    {
      z+=10*c;
      sz-=8*c;
      sy-=1*r2*c;
      srz+=80*r1*c;
      sx+=1*r1*c;
//      playsoundeffect(0,0);
    }
    else if (time<250)
    {
      z+=10*c;
      sz-=8*c;
      sy-=3*r2*c;
      sx+=2*r1*c;
//      playsoundeffect(0,0);
    }
    apiniceprint3(21,36, 280 , 194 ,  "RETURN"  );
    apiscreenswap();
    while (this_time < maxspeed) this_time = apireadtimer();
  }
//  free(sound1[0]);
}

void  launching(void)
{
 int time;
  float x,y,z,rx,ry,rz;
  float sx,sy,sz,srx,sry,srz;
  float rsy;
  float c=2;

  VtkObj o1,o2,light,camera;
  light=dddCreateLight(VECTOR_LIGHT,255,0);
  dddRotateTo(light,8000,22000,1000);
  camera=dddCreateCamera(32000,100,100);
  o1=dddLoad("hanger2.3dw");
  o2=dddLoad(shapedata[o[0].shape].filename);
//  sound1[0]=LoadWave("engine2.wav",&length1[0]);

  x=240; y=0; z=30;
  rx=0; ry=-10000; rz=0;
  rsy=0;
  sx=-300; sy=-40; sz=500;
  srx=0; sry=22767; srz=5000;
  time=0;
  while ((time<200)&&(!key[_RETURN]) && (!key[_ESC]))
  {
    jjjtimer_start(0);
    frame2++; if (frame2>2) frame2=0;
    if(frame2==0) playing[0]=0;
    apisetviewangles(0,0,0);
    apisetviewpos(0,0,0);
    apiclearcliparea();
    apiclearobjects();
    drawstarfield(0,-rsy/1638,0);
    apiguishape(DRAWROOM,o1,o[0].side,x,y,z,rx,ry,rz );
    apiguishape(DRAWROOM,o2,1,sx,sy,sz,srx,sry,srz );
    dddInsertObject(light);
    dddSelectCamera(camera);
    apidrawobjects();
    time+=1*c;
    if(time<50)
    {
      x-=4.7*c;
      z+=2*c;
      sx+=2*c;
      sy+=1*c;
      srz-=100*c;
//      playsoundeffect(0,0);
    }
    else if(time<100)
    {
      z+=2*c;
      if (time<75)
      {
        sx+=10*c;
      }
      else
      {
        sx+=2*c;
        sz+=15*c;
      }
      srz-=100*c;
//      if(time>90) playsoundeffect(0,(time-90)*50);
//      else playsoundeffect(0,0);
    }
    else if (time<200)
    {
      sx+=1*c;
      sz+=15*c;
      srz+=100*c;
      z+=2*c;
//      playsoundeffect(0,(time-90)*50);
    }
    if(time>20 && time<100)
    {
      if (time<60)
      {
        rsy-=15*c;
      }
      else
      {
        rsy+=15*c;
      }
      ry+=rsy*c;
      sry+=rsy*c;
    }
    apiniceprint3(21,36, 280 , 194 ,  "RETURN"  );
    apiscreenswap();

    while (this_time < maxspeed) this_time = apireadtimer();
  }
//  free(sound1[0]);
  if(key[_RETURN]) while(key[_RETURN]) {}
  if(key[_ESC]) while(key[_ESC]) {}
}

void  landing(void)
{
 int time;
  float x,y,z,rx,ry,rz;
  float sx,sy,sz,srx,sry,srz;
  float c=2;
  VtkObj o1,o2,light,camera;

  light=dddCreateLight(VECTOR_LIGHT,255,0);
  dddRotateTo(light,8000,12000,12000);
  camera=dddCreateCamera(32000,100,100);
  o1=dddLoad("hanger2.3dw");
  o2=dddLoad(shapedata[o[0].shape].filename);

  x=-20; y=0; z=500;
  rx=0; ry=10000; rz=0;
  sx=-300; sy=-40; sz=500;
  srx=0; sry=15000; srz=5000;
  time=0;
  while ((time<350)&&(!key[_RETURN])&&!(key[_ESC]))
  {
    apisetviewangles(0,0,0);
    apisetviewpos(0,0,0);
    jjjtimer_start(0);
    apiclearcliparea();
    apiclearobjects();
    drawstarfield(0,0,0);
    apiguishape(DRAWROOM,o1,o[0].side,x,y,z,rx,ry,rz);
    apiguishape(DRAWROOM,o2,1,sx,sy,sz,srx,sry,srz);
    dddInsertObject(light);
    dddSelectCamera(camera);
    apidrawobjects();
    time+=1*c;

    if (time<100)
    {
      if (time<50)
      {
        sx+=5*c;
        sy+=1*c;
        srz-=80*c;
      }
      else if(time<80)
      {
        sx+=3*c;
        sz+=1*c;
      }
      else
      {
        sx+=1*c;
        sz+=3*c;
        ry-=25*c;
        z-=2*c;
      }
      z-=1*c;
      ry-=50*c;
      sry-=50*c;
    }
    else if(time<150)
    {
      z-=4*c;
      ry-=25*c;
      sz+=1*c;
      sx+=1*c;
      sry-=25*c;
      srz+=50*c;
    }
    else if(time<250)
    {
      z-=4*c;
      ry-=25*c;
      sz-=3*c;
      sry-=25*c;
      srz-=50*c;
    }
    else if(time<300)
    {
      z-=3*c;
      x+=1*c;
      sz+=3*c;
      sx+=2*c;
      sry-=25*c;
      srz-=50*c;
    }
    else if(time<350)
    {
      z-=2*c;
      sz+=3*c;
      sry-=25*c;
      srz+=100*c;
      srx-=200*c;
    }
    apiniceprint3(21,36, 280 , 194 ,  "RETURN"  );
    apiscreenswap();
    while (this_time < maxspeed) this_time = apireadtimer();
  }
}

void  land(void)
{
  int time;
  float x,y,z,rx,ry,rz;
  float sx,sy,sz,srx,sry,srz;
  float c=2;
  VtkObj o1,o2,light,camera;
  light=dddCreateLight(VECTOR_LIGHT,255,0);
  dddRotateTo(light,8000,12000,12000);
  camera=dddCreateCamera(32000,100,100);
  o1=dddLoad("main4.3dw");
  o2=dddLoad(shapedata[o[0].shape].filename);
  x=0; y=0; z=500;
  rx=11000; ry=0; rz=0;
  sx=0; sy=-200; sz=500;
  srx=3000; sry=20000; srz=0;
  time=0;
  while ((time<400)&&(!key[_RETURN])&&(!key[_ESC]))
  {
    apisetviewangles(0,0,0);
    apisetviewpos(0,0,0);
    jjjtimer_start(0);
    apiclearcliparea();         // Set up the view
    apiclearobjects();
    apiguishape(DRAWROOM,o1,1,x,y,z,rx,ry,rz );
    apiguishape(DRAWROOM,o2,o[0].side,sx,sy,sz,srx,sry,srz );
    dddInsertObject(light);
    dddSelectCamera(camera);
    apidrawobjects();
    time+=1*c;
    if (time<100)
    {
      sy+=1*c;
      sry+=100*c;
      z-=1*c;
      sz-=1*c;
    }
    else if (time<140)
    {
      rx+=100*c;
      srx-=100*c;
      sy+=1*c;
      z-=1*c;
    }
    else if(time<200)
    {
      z-=1*c;
      srx-=10*c;
      sy+=0.5*c;
    }
    else if (time<250)
    {
      z-=1*c;
      sy+=0.7*c;
      srx+=30*c;
    }
    else if (time<300)
    {
      z-=1*c;
      sz-=0.5*c;
      sy+=0.5*c;
      srx+=10*c;
    }
    else
    {
      z-=1*c;
      sz-=2*c;
    }
    apiniceprint3(21,36, 280 , 194 ,  "RETURN"  );
    apiscreenswap();
    while (this_time < maxspeed) this_time = apireadtimer();
  }
  if(key[_RETURN]) while(key[_RETURN]) {}
  if(key[_ESC]) while(key[_ESC]) {}
}

int pilotinfo(void)
{
  char s1[80];
  VtkObj object,light1,camera;
  object=dddLoad("fite1.3dw"); // for safetys sake
  if(pilot.side==1)
  {
    switch(pilot.campaign)
    {
      case 1:
        object=dddLoad("saturn.3dw");
        dddMoveTo(object,0,0,3000);
        dddRotateTo(object,1000,0,0);
        break;
      case 2:
        object=dddLoad("jupiter.3dw");
        dddMoveTo(object,0,0,10000);
        dddRotateTo(object,-16384,0,0);
        break;
      case 3:
        object=dddLoad("neptune.3dw");
        dddMoveTo(object,0,0,3000);
        dddRotateTo(object,1000,0,0);
        break;
    }
  }
  if(pilot.side==2)
  {
    switch(pilot.campaign)
    {
      case 1:
        object=dddLoad("saturn.3dw");
        dddMoveTo(object,0,0,3000);
        dddRotateTo(object,1000,0,0);
        break;
      case 2:
        object=dddLoad("jupiter.3dw");
        dddMoveTo(object,0,0,10000);
        dddRotateTo(object,-16384,0,0);
        break;
      case 3:
        object=dddLoad("neptune.3dw");
        dddMoveTo(object,0,0,3000);
        dddRotateTo(object,1000,0,0);
        break;
    }
  }
//

  dddSetRenderMethod(object,RENDERED|ZBUFFERED|GOURAUD_SHADE);
  dddSetShapeFlags(object,1);
  camera=dddCreateCamera(32000,100,100);
  light1=dddCreateLight(VECTOR_LIGHT,122,0);
  dddRotateTo(light1,6000,2000,0);

  while (!(key[_RETURN]) && !(key[_ESC]))
  {
    apisetviewangles(0,0,0);
    apisetviewpos(0,0,0);
    jjjtimer_start(0);
    apiclearcliparea();
    apiclearobjects();
    apigetmousepos();
    drawstarfield(0,0.1,0);
    dddInsertObject(object);
    dddInsertObject(light1);
    dddSelectCamera(camera);
    apidrawobjects();
    dddRotateBy(object,0,112,0);

    apiniceprint3(1,130,105,30,"P  I  L  O  T     I  N  F  O");

    sprintf(s1,"%s, %s",pilot.sirname,pilot.firstname);
    apiniceprint3(21,36,75,60,"NAME                :");
    apiniceprint3(21,36,170,60,s1);
    if (pilot.side==1) sprintf(s1,"OUTER SYSTEM");
    if (pilot.side==2) sprintf(s1,"UNITED EARTH ALLIANCE");
    apiniceprint3(21,36,75,70,"ALLEGANCE           :");
    apiniceprint3(21,36,170,70,s1);
    switch (pilot.rank)
    {
      case 0:
        sprintf(s1,"SECOND LT.");
        break;
      case 1:
        sprintf(s1,"FIRST LT.");
        break;
      case 2:
        sprintf(s1,"CAPTAIN");
        break;
      case 3:
        sprintf(s1,"MAJOR");
        break;
      case 4:
        sprintf(s1,"LT CNL");
        break;
      case 5:
        sprintf(s1,"CNL");
        break;
    }
    apiniceprint3(21,36,75,80,"RANK                :");
    apiniceprint3(21,36,170,80,s1);
    switch (pilot.campaign)
    {
      case 1:
        if(pilot.side==1) sprintf(s1,"DEFENDING SATURN FROM UEA ATTACK");
        if(pilot.side==2) sprintf(s1,"ATTACKING THE OUTER SYSTEM AT SATURN");
        break;
      case 2:
        if(pilot.side==1) sprintf(s1,"REGROUPING FORCES ABOVE ECLIPTIC");
        break;
      case 3:
        if(pilot.side==1) sprintf(s1,"ATTACKING UEA REINFORCEMENT FLEET");
        break;
    }
    apiniceprint3(21,36,75,90,"CURRENT CAMPAIGN    :");
    apiniceprint3(21,36,170,90,s1);
    sprintf(s1,"%d",pilot.mission);
    apiniceprint3(21,36,75,100,"CURRENT MISSION     :");
    apiniceprint3(21,36,170,100,s1);
    sprintf(s1,"%d",pilot.kills);
    apiniceprint3(21,36,75,110,"KILLS               :");
    apiniceprint3(21,36,170,110,s1);
    sprintf(s1,"%d",pilot.deaths);
    apiniceprint3(21,36,75,120,"DEATHS              :");
    apiniceprint3(21,36,170,120,s1);
    sprintf(s1,"%d",pilot.restarts);
    apiniceprint3(21,36,75,130,"RESTARTS            :");
    apiniceprint3(21,36,170,130,s1);
    apiniceprint3(21,36, 280 , 194 ,  "RETURN"  );
    while (this_time < maxspeed) this_time = apireadtimer();
    apiscreenswap();

  }
  if(key[_RETURN]) while(key[_RETURN]) {}
  if(key[_ESC]) while(key[_ESC]) {}
  return(0);
}

int awakenpilot(void)
{
  char pilotname[80][80];
  struct find_t fileinfo;
  int f,pilots,cursor,select=0,offset=0;
  unsigned int rc;
  FILE *stream;
  VtkObj object,light1,camera;
  object=dddLoad("mars.3dw");
  dddSetRenderMethod(object,RENDERED|ZBUFFERED|GOURAUD_SHADE);
  dddSetShapeFlags(object,1);
  dddMoveTo(object,0,0,125);
  camera=dddCreateCamera(32000,100,100);
  light1=dddCreateLight(VECTOR_LIGHT,122,0);
  dddRotateTo(light1,6000,2000,0);

  f=0;
  cursor=0;
  rc = _dos_findfirst("*.plt",_A_NORMAL,&fileinfo);
  if(rc!=0) return(0);
  while (rc==0)
  {
    sprintf(pilotname[f],"%14s", fileinfo.name);
    rc = _dos_findnext(&fileinfo);
    f++;
  }
  pilots=f;

  while (!(key[_RETURN]) && !(key[_ESC]))
  {
    apisetviewangles(0,0,0);
    apisetviewpos(0,0,0);
    jjjtimer_start(0);
    apiclearcliparea();
    apiclearobjects();
    apigetmousepos();
    drawstarfield(0,0.1,0);
    dddInsertObject(object);
    dddInsertObject(light1);
    dddSelectCamera(camera);
    apidrawobjects();
    dddRotateBy(object,0,-112,0);

    apiniceprint3(1,130,95,30,"A  W  A  K  E  N    P  I  L  O  T");

    f=0;
    while(f<pilots && f<10)
    {
      apiniceprint3(21,36,130,50+(f*10),pilotname[offset+f]);
      f++;
    }
     if (!key[_C_DOWN] && !key[_C_UP] && !key[_SPACEBAR] &&
        !key[_EQUALS] && !key[_MINUS] && !key[_RETURN]) select=0;
    if (select>0)
    {
      select++; if(select>5000) select=0;
    }
    else
    {
      if(key[_C_DOWN])
      {
        if(cursor+offset<pilots-1) cursor++;
      }
      if(key[_C_UP])
      {
        if (cursor>0) cursor--;
      }
    }
    apiline(99,120,(53+cursor*10),140,(53+cursor*10));
    apiline(99,130,(49+cursor*10),140,(53+cursor*10));
    apiline(99,130,(57+cursor*10),140,(53+cursor*10));
    apiline(2,120,(52+cursor*10),140,(52+cursor*10));
    apiline(2,130,(48+cursor*10),140,(52+cursor*10));
    apiline(2,130,(56+cursor*10),140,(52+cursor*10));
    apiniceprint3(21,36, 280 , 194 ,  "RETURN"  );
    while (this_time < maxspeed) this_time = apireadtimer();
    apiscreenswap();
    delay(50);
  }
  if(key[_RETURN])
  {
    stream=fopen(pilotname[offset+cursor],"r");
    fscanf(stream,"%d\n",&pilot.campaign);
    fscanf(stream,"%d\n",&pilot.side);
    fscanf(stream,"%d\n",&pilot.mission);
    fscanf(stream,"%d\n",&pilot.missions);
    fscanf(stream,"%d\n",&pilot.kills);
    fscanf(stream,"%d\n",&pilot.rank);
    fscanf(stream,"%s\n",&pilot.sirname);
    fscanf(stream,"%s\n",&pilot.firstname);
    fscanf(stream,"%d\n",&pilot.deaths);
    fscanf(stream,"%d\n",&pilot.restarts);
    fscanf(stream,"%d\n",&pilot.score);
    fscanf(stream,"%d\n",&pilot.campaignstatus);
    for(f=0; f<49; f++) fscanf(stream,"%d\n",&dpt[f]);
    fclose(stream);
    while(key[_RETURN]) {}
    loadwindowships();
    return(0);
  }
  if(key[_ESC]) while(key[_ESC]) {}
  return(-1);
}

void roomdebriefing(void)
{
  int f;
  char s1[80];
  VtkObj object,light1,camera;
  char str2[100];
  FILE *stream;
  char l[40][150];
  int lines;
  for(f=0;f<35;f++) sprintf(l[f]," ");
  object=dddLoad("jupiter.3dw");
  dddSetRenderMethod(object,RENDERED|ZBUFFERED|GOURAUD_SHADE);
  dddSetShapeFlags(object,1);
  dddMoveTo(object,0,0,2900);
  dddRotateTo(object,-16384,0,0);
  camera=dddCreateCamera(32000,100,100);
  light1=dddCreateLight(VECTOR_LIGHT,122,0);
  dddRotateTo(light1,6000,2000,0);

  f=apirandom(5);
  if(successflag==1)
  {
    strcpy(str2,missionname[actualmission]);
    strcat(str2,".MGE");
    switch(f)
     {
     case 0: sprintf(s1,"THE MISSION WAS A SUCCESS."); break;
      case 1: sprintf(s1,"CONGRATULATIONS ON A GOOD JOB."); break;
      case 2: sprintf(s1,"ALL WENT ACCORDING TO PLAN."); break;
      case 3: sprintf(s1,"YOUR MISSION WAS SUCCESSFUL."); break;
      case 4: sprintf(s1,"YOU FOUGHT WELL."); break;
    }
  }
  else if(successflag==-1)
  {
    strcpy(str2,missionname[actualmission]);
    strcat(str2,".MBE");
    switch(f)
    {
      case 0: sprintf(s1,"THE MISSION WAS A FAILURE."); break;
      case 1: sprintf(s1,"YOU AND YOUR MEN FAILED YOUR NATION."); break;
      case 2: sprintf(s1,"YOU HAVE DISSAPOINTED US."); break;
      case 3: sprintf(s1,"THE MISSION WAS A COMPLETE DISASTER."); break;
      case 4: sprintf(s1,"DISASTER."); break;
    }
  }
  stream=fopen(str2,"r");
  lines=0;
  do
  {
    fgets(l[lines],100,stream);
    lines++;
  }
  while(!feof(stream) && lines<30);
  fclose(stream);

  while (!(key[_RETURN]) && !(key[_ESC]))
  {
    apisetviewangles(0,0,0);
    apisetviewpos(0,0,0);
    jjjtimer_start(0);
    apiclearcliparea();
    apiclearobjects();
    apigetmousepos();
    drawstarfield(0,0.1,0);
    dddInsertObject(object);
    dddInsertObject(light1);
    dddSelectCamera(camera);
    apidrawobjects();
    dddRotateBy(object,0,112,0);
    dddRotateBy(light1,0,-112,0);

    apiniceprint3(1,130,105,5,"D  E  B  R  I  E  F  I  N  G");
    apiniceprint3(144,156, 10 , 20 ,  s1  ); // s1

    for(f=0;f<lines;f++) apiniceprint3(144+(f/5),156, 10, 33+f*7, l[f]);
    apiniceprint3(21,36, 280 , 194 ,  "RETURN"  );
    while (this_time< maxspeed) this_time = apireadtimer();
    apiscreenswap();
    delay(50);
  }
  if(key[_ESC]) while(key[_ESC]) {}
  if(key[_RETURN]) while(key[_RETURN]) {}
}

void printcredits1(void)
{
  int f,g;
  long frame1=0;
  FILE *stream;
  char l[80][80];
  int ly[80];
  int lines;
  int rc;
  light=dddCreateLight(VECTOR_LIGHT,255,1000);
  camera=dddCreateCamera(32000,100,100);
  viewz=1000;

  for(f=0;f<80;f++)
  {
    sprintf(l[f]," ");
    ly[f]=200+(f*10);
  }
  rc=jjjtargt_open("TEXT.GUN");
  if(rc!=0) crashout("GUN: You don't have a big gun");
  stream=fopen(jjjtargt_tempextract("CRED1.TXT"),"r");
  lines=0;
  do
  {
    fgets(l[lines],80,stream);
    lines++;
  }
  while(!feof(stream) && lines<80);
  fclose(stream);
  jjjtargt_close();

  while(!key[_RETURN] && !key[_ESC] && ly[lines]>60)
  {
    frame1++;
    if(frame1>4) frame1=0;
    apisetviewangles(0,0,0);
    apisetviewpos(0,0,-1000);
    jjjtimer_start(0);
    apiclearcliparea(); // Set up the view
    apiclearobjects();
    drawstarfield(0,0.4,0);
    drawguiobjects(0,0.4,0);
    dddSelectCamera(camera);
    dddInsertObject(light);
    apidrawobjects();
    apiniceprint3(1,130,93,30,"S   E   C   E   S   S   I   O   N");
    apiline(1, 90,37,230,37);
    apiline(1, 90,27,230,27);
    for(f=0; f<16; f++)
    {
      apiplot(175-f,231+f,37);
      apiplot(175-f,89-f,37);
      apiplot(175-f,231+f,27);
      apiplot(175-f,89-f,27);
    }
    for(f=0;f<lines;f++)
    {
      g=(200-ly[f])/4.4;
      if(ly[f]>60 && ly[f]<200) apiniceprint3(16+g,32+(g/2),2,ly[f],l[f]);
      if(frame1==0) ly[f]--;
    }
    while (this_time<maxspeed) this_time=apireadtimer();
    apiscreenswap();
  }
  while(key[_ESC]) {}
  while(key[_RETURN]) {}
}

void printcredits2(void)
{
  int f,g;
  long frame1=0;
  FILE *stream;
  char l[80][80];
  int ly[80];
  int lines;
  int rc;
  light=dddCreateLight(VECTOR_LIGHT,255,1000);
  camera=dddCreateCamera(32000,100,100);
  viewz=1000;

  for(f=0;f<80;f++)
  {
    sprintf(l[f]," ");
    ly[f]=200+(f*10);
  }
  rc=jjjtargt_open("TEXT.GUN");
  if(rc!=0) crashout("GUN: You don't have a big gun");
  stream=fopen(jjjtargt_tempextract("CRED2.TXT"),"r");
  lines=0;
  do
  {
    fgets(l[lines],80,stream);
    lines++;
  }
  while(!feof(stream) && lines<80);
  fclose(stream);
  jjjtargt_close();

  while(!key[_RETURN] && !key[_ESC] && ly[lines]>60)
  {
    frame1++;
    if(frame1>4) frame1=0;
    apisetviewangles(0,0,0);
    apisetviewpos(0,0,-1000);
    jjjtimer_start(0);
    apiclearcliparea(); // Set up the view
    apiclearobjects();
    drawstarfield(0,0.4,0);
    drawguiobjects(0,0.4,0);
    dddSelectCamera(camera);
    dddInsertObject(light);
    apidrawobjects();
    apiniceprint3(1,130,93,30,"S   E   C   E   S   S   I   O   N");
    apiline(1, 90,37,230,37);
    apiline(1, 90,27,230,27);
    for(f=0; f<16; f++)
    {
      apiplot(175-f,231+f,37);
      apiplot(175-f,89-f,37);
      apiplot(175-f,231+f,27);
      apiplot(175-f,89-f,27);
    }
    for(f=0;f<lines;f++)
    {
      g=(200-ly[f])/4.4;
      if(ly[f]>60 && ly[f]<200) apiniceprint3(16+g,32+(g/2),2,ly[f],l[f]);
      if(frame1==0) ly[f]--;
    }
    while (this_time<maxspeed) this_time=apireadtimer();
    apiscreenswap();
  }
  while(key[_ESC]) {}
  while(key[_RETURN]) {}
}

