#include <stdio.h>

#include "use.h"

extern int use(int obj,int rm)
{
  int i,l,ok,c;
  char colour[32];
  char *key[32];
  extern int status_flags[];
  extern int obj_flags[];
  extern int obj_location[];
  extern char *locations[];
  extern char *exits[];
  extern char *sim_obj[];
  extern char *objects[];
  
  ok = 0;
  
  if ((obj==0) && (obj_flags[0]==0)){
    ok=1;
    if (rm==41)
    {
      printf("You rip out your gun and put it to the highest setting, tickle.  Aiming the gun you fire at the door and within seconds it is nothing more than a heap of molten metal.\n");
      exits[41]="NW";
      locations[41]="in a secret room next to the northern security cell";
      status_flags[8]=1;
    }
    else
    {
      printf("Ripping your gun out of the quick release holster, you think better and put it away\n");
    }
  }
    
  if ((obj==1) && (obj_flags[1]==0)){
    if ((rm==36)||(rm==37)||(rm==38)||(rm==29)||(rm==20)||(rm==11)||(rm==10)||(rm==9))
      printf("You sweep the scanner in a full 360 degree circle and you get a faint pulse.  You turn up the sensitivity, but due to the metallic content of the walls, you can't seem to get a lock on the signal.\n");
    else {
      printf("\n\nSearching ");
      for(i=0;i<=6;i++){
        for(l=0;l<=5000;l++);
        printf(".");
      }
      printf("\nATMOSPHERIC CONTENT  :\n");
      ok=1;
      if (status_flags[6]==0)
        printf("			VACUUM\n");
      else
        printf("	OXYGEN = 	21.2%%\n	NITROGEN = 	77.6%%\n	ARGON = 	1.15%%\n	OTHERS = 	0.05%%\n");
      printf("MINERAL CONTENT      :\n	TITANIUM     = 	87.5%%\n	SILICON      = 	12.2%%\n	OTHERS       = 	0.3%%\nLIFE SIGNS           = 	NONE\n");
    }
  }

  if ((obj==2) && (obj_flags[2]==0)){
    printf("You are already using that.\n");
    ok=1;
  }
    
  if ((obj==3) && (obj_flags[3]==0)){
    if (rm==49){
      printf("Holding the metal, you carefully slide it between the beams.  Then with slow movements and holding it at an angle away from you, you cut into one of the beams.  The surface reflects it and you direct it to the control unit.  There is a loud bang and the repulsar beams deactivate.  The piece of metal is also destroyed.\n");
      exits[49]="NESW";
      locations[49]="in the heart of the security centre.  There are 3 cells here but none of them are in use at the moment";
      obj_location[19]=90;
      obj_location[3]=90;
      obj_flags[3]=1;
      ok=1;
    }
  }
    
  if ((obj==4)&&(rm==98)&&(obj_flags[4]==0)){
    if(obj_flags[8]==0){
      printf("Using the screw driver you loosen the screws and bridge the gap with the wire strip.\n");
      obj_flags[4]=1;
      obj_location[4]=90;
      ok=1;
    }
    else{
      printf("Looking at the screws, you decide that you need something to loosen them.\n");
      ok=1;
    }
  }
      
  if ((obj==7) && (obj_flags[7]==0)){
    if (rm == 52){
      ok=1;
      printf("You plug the computer terminal into the network station ");
      if (status_flags[0]==1){
        printf("and the network comes online.\n");
        status_flags[2]=1;
      }
      else
        printf("and nothing happens.  The power is still off.\n");
    }
    if ((rm == 49) || (rm == 71) || (rm == 108)){
      ok=1;
      printf("You plug the computer terminal into the network ");
      if (status_flags[2]==1){
        printf("and you are welcomed by the log on screen prompt\n");
        computer();
      }
      else
        printf("and nothing happnens.  It appears that the network is still off line\n");
    }
  }
  
  if(obj==9 || obj==10 || obj==11 || obj==12 || obj==13)
    ok = keys(obj,rm,ok);
    
    if(obj==15 && rm==98)
    {
      printf("You press the start button and ");
      ok=1;
      if (obj_location[4]==90){
        printf("the generator shudders into life.\n");
        status_flags[0]=1;
      }
      else
        printf("nothing happens.  There is no fuse attached.\n");
    }
    
    if(obj==16 && rm==116){
      printf("You press the start button and a terminal comes to life flashing the line\n\n          Generator on standby.\n               Ready.\n");
      ok=1;
      status_flags[5]=1;
    }
    
    if(obj==17 && rm==108){
      printf("You wouldn't know where to start looking to operate this piece of equipment.\n");
      ok=1;
    }
    
    if(obj==18 && rm==108){
      ok=1;
      if(status_flags[1]==0)
        printf("The terminal is not operating.  Looks like you need to turn on the main power as well.\n");
      else if(status_flags[1]==1){
        printf("Standing before the terminal you type 'turn environmental on' at the prompt and you begin to feel the ground shake under your feet.  Turning round you see that the control is on and very operational.\n");
        status_flags[3]=1;
      }
    }
    
    if(obj==20 && rm==40){
      printf("Placing the crow bar into the crack, you take up the strain and after a few heaves it comes free.  looking behind the panel, you see a secret room!\n");
      locations[40]="You are in the northern security cell.  There is a secret door behind the panel to the east";
      exits[40]="ES";
      status_flags[7]=1;
      ok=1;
    }
    
    if(obj==22 && rm==103){
      ok=1;
      printf("\nYou activate the navcom and the screen flashes into life showing you the currently selectable options\n\n");
      navcom();     
    }
    
    c=0;
    if(obj==29){
      for(i=9;i<=12;i++){
        if(obj_flags[i]==0){
          ++c;
          obj=i;
        }
      }
      if(c==1){
        ok = keys(obj,rm,ok);
      }
        
      if(c>1){
        i=9;
        printf("You can use ");
        while(i<13){
          if(obj_flags[i]==0)
            printf("a %s ",objects[i]);
          ++i;
        }
        printf("\nWhich key?\n");
        gets(colour);
        i=0;
        while(colour[i] != '\0')
        {
          colour[i] = toupper(colour[i]);  /* convert users text to upper case */    
          i++;  
        }
        split(colour, key, 24);
        printf("%s ",key[0]);
        if(strcmp(key[0],"THE")==0){
          key[0]=key[1];
          key[1]=NULL;
        }
        printf("%s\n",key[0]);
        obj=compare(key[0],sim_obj);
        
        if(obj != -1)
          ok = keys(obj,rm,ok);
        else
          printf("You don't have the colour\n");
      }
    }
                
  if (ok==0)
    printf("You can't use that here\n");
}

int keys(int obj,int rm,int ok)
{
  extern int obj_location[];
  extern int obj_flags[];
  extern char *exits[];
  
    if ((obj==9) && (obj_flags[9]==0)){
    if (rm==106){
      ok=1;
      printf("You slide the card into the slot and the door slides open.\n");
      exits[rm]="EW";
      obj_location[9]=90;
      obj_flags[9]=1;
    }
  }
  
  if ((obj==10) && (obj_flags[10]==0) && ((rm==61) || (rm==71) || (rm==79))){
  {
    ok=1;
      printf("You slide the card into the slot and the door opens.\n");
      exits[61]="ESW";
      exits[71]="NSW";
      exits[79]="NES";
      obj_location[10]=90;
      obj_flags[10]=1;
    }
  }
  
 if ((obj==11) && (obj_flags[11]==0))
    if (rm==37){
      ok=1;
      printf("You slide the key into the slot and the door opens\n");
      exits[37]="ESW";
      obj_location[11]=90;
      obj_flags[11]=1;
    }
    
  if ((obj==12) && (obj_flags[12]==0)){
    if (rm==58){
      ok=1;
      printf("You slide the key into the slot and the door opens\n");
      obj_location[12]=90;
      obj_flags[12]=1;
      if (strcmp(exits[58],"S")==0)
        exits[58]="NS";
      else
        exits[58]="NES";
    }
  }
  
  if ((obj==13) && (obj_flags[13]==0))
    if (rm==58){
      ok=1;
      printf("You slide the key into the slot and the door opens\n");
      obj_location[13]=90;
      obj_flags[13]=1;
      if (strcmp(exits[58],"S")==0)
        exits[58]="ES";
      else
        exits[58]="NES";
    }
    
    return ok;
}


navcom(void)
{
  char s[24];
  char *w[24];
  char *com[] = {"SYSTEM", "LAUNCH", "EXIT", "",};
  int i;
  int c;
  int q;
  extern int status_flags[];
  
  q=0;
  
  while(q!=1){
  
  printf("System Data\nLaunch\nExit\n\n-->");
  
  gets(s);
  i=0;
  while(s[i] != '\0')
  {
    s[i] = toupper(s[i]);
    i++;
  }
  
  split(s, w, 24);
  
  c=-1;
  for(i=0;(com[i][0] != '\0') && (c<0);i++){
    if(strcmp(w[0],com[i])==0)
      c=i;
  }
  
  switch (c){
    
    case 0:
      printf("System Data\n\nName : Omicron Theta			alliance : Union of Stars and Systems\n\nType : White star system		Planets : 12\n\nComments/History :	Colonised in 2702, this system was one of the frontier worlds used as a supply depot for outgoing expeditions to the inner galaxy.  When the Federation was founded in 2942, Omicron found it's self on the border and thus was taken over as a military base.  At present, it is one of the foremost research foundations where many of the Unions 'secreter' projects are matured.  It is currently an out-of-bounds system pattroled by a section of the Navy that have orders to shoot-to-kill.\n\n");
      break;
    
    case 1:
      status_flags[10]=1;
      q=1;
      if(status_flags[9]==1){
        printf("Everyone piles into the craft as you make your self comfortable in the pilots seat.  Steve settles into the co-pilots seat and thanks you for taking his men off the base.  You enter in the co-ordinates and the craft lifts off the landing pad\n\nCongratulations\n\nYou have now completed the game\n");
      }
      else
        printf("You enter in the co-ordinates and the craft takes off from the pad.  You had better have a good story planed for your CO when you get back\n\n");
      break;
      
    case 2:
      printf("\nExiting navcom\n");
      q=1;
      break;
    
    default:
      printf("Command not recognised\n\n");
      break;
  
  }
  }
}

