/*###################################################################################################
  #                                                                                                 #
  #                              Omicron Theta - Deep Space Rescue (tm)                             #
  #				                                                                    #
  #				    Time Space Thought software                                     #
  #                                                                                                 #
  #			   Coded and compiled in C by Aidan Gustard 1997                            #
  #                                                                                                 #
  #                            Compiled with gcc, Unixlib and !Clib.                                #
  #                                                                                                 #
  #          This program is distributable under the GNU General Public Licenseint laws             #
  #       as freeware.  You can modify it and redistribute it under the terms of the License        #
  #           I claim no responsability for any damage use of this program may incur                #
  #                                                                                                 #
  #                                                                                                 #
  #                                     Contact me by mailing to :                                  #
  #                                                                                                 #
  #                                       aidygus@argonet.co.uk                                     #
  #                                                                                                 #
  #					     Or writing to :                                        #
  #					                                                            #
  #				            Aidan Gustard                                           #
  #				            13 Highstead Ave                                        #
  #				            Northburn Wood                                          #
  #				            Cramlington                                             #
  #	   	 		            Northumberland                                          #
  #				            England                                                 #
  #				            NE23 9QT                                                #
  #			                                                                            #
  ###################################################################################################*/

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#include "set_var.h"

int compare(char *s, char **words);
int direction(int d, int rm);

int main()
{
  int l ,i,c,v,rm,g;
  int verb,obj;
  char s[32];
  char *w[32];
  extern char *adv_obj[];
  extern char *verbs[];
  extern int dead;
  
  set_mode(40);
  
  rm = 103;                            /*  103 locations  */
  l=0;
  dead=0;
  printf("It took 48 hours to get here.  Thats a long time for someone by them selves in space.  48 hours from leaving you home, the Piranha class frigate to check out why communications have been lost from the research base at Omicron Theta.  You are awoken from your light snooze by a beeping issued from the navigational computer telling you that orbit has been accomplished.  After a few fruitless attempts to raise the base, you decide to find out what is up.  Engaging the engines again, you enter the atmosphere and after coasting around half the globe, you land the craft on the pad.  Its time to see what has happened\n\n");
  
  while(dead != 1)                     /*  main game loop  */
  {
  printf("..............................................\n\n") ;
  display(rm);
  
  gets(s);
  i=0;

  while(s[i] != '\0')                         
  {
    s[i] = toupper(s[i]);              /* convert users text to upper case */
    i++;
  }
  
  split(s, w, 24);
  
  if (strcmp(w[1],"THE")==0){          /*  Look for the word THE and take out */
    for(i=1 ,c=2;w[c]!=NULL;i++,c++)
      w[i] = w[c];
    w[i] = NULL;}

  verb = compare(w[0],verbs);          /*  Find the verb and go to appropriate procedure  */
  
  switch (verb){
    case 0 :
      help(rm);
      break;
      
    case 1: case 19: case 20:
      carrying();
      break;
      
    case 2: case 3: case 4: case 5: case 6: case 7: case 8:
      if(verb == 2 && w[1] != NULL){
        verb =  compare(w[1],verbs);
      }
      rm = direction(verb,rm);
      air_status(rm);
      break;
      
    case 9: case 10:
      obj = compare(w[1],sim_obj);
      if (obj != -1)
        take(obj,rm);
      else
        printf("It is not here.\n");
      break;

    case 11: case 12:
      obj = compare(w[1],sim_obj);
      if (obj != -1)
        drop(obj,rm);
      else
        printf("You have no such thing.\n");
      break;
    
    case 13: case 14:
    obj = compare(w[1],sim_obj);
    if (obj != -1)
      printf("\n%s\n",adv_obj[obj]);
      break;
      
    case 15:
      obj = compare(w[1],sim_obj);
      if (obj != -1)
        use(obj,rm);
      else
        printf("You can not find that object to use it");
      break;
    
    case 16:
      save(rm);
      break;
    
    case 17:
      rm=load();
      break;
    
    case 18:
      printf("Quiting game\n");
      dead=1;
      break;
    
    case 21:
      talk(rm,w[2]);
      break;

    default:  /* none of the above */
      printf("Unknown verb\n");
      break;

  }
  if(status_flags[6]==0 && obj_flags[2]==1)
    dead=1;
    
  if(status_flags[10]==1)
    dead=1;
  }
  return 0;
  
}

display(int rm)
{
  int i;
  extern char *locations[];
  extern char *exits[];
  extern char *objects[];
  extern int  obj_location[];
  extern int obj_flags[];
  extern int status_flags[];
  printf("\nYou are %s.\n\nExits you can see are %s\n\n",locations[rm],exits[rm]);
  
  for(i=0;i<= MAXOBJ;i++){
    if((obj_location[i] == rm) && (obj_flags[i] != 0))
      printf("You can see a %s here\n",objects[i]);
  }
  if(status_flags[9]==1)
    printf("Steve and the rest of the scientist are following you\n\n");
}

help(int rm)
{
  int i ;
  extern char *verbs[];
  extern char *objects[];
  extern char *sim_obj[];
  extern int obj_flags[];
  extern int obj_location[];
   
  printf("\nWords I know\n\n");
  for(i=0;i<=21;i++)
    printf("%s ",verbs[i]);
  printf("\n\n");
  for(i=0;i<=MAXOBJ;i++){
    if(obj_flags[i]==0 || obj_location[i]==rm)
      printf("%40s  ==  %s\n",objects[i],sim_obj[i]);
  }
}

int direction(int d, int rm)
{
  extern int status_flags[];
  extern char *exits[];
  int i,c;
  char s[9];
  char r[9];
  c=0;
  for(i=0;exits[rm][i] != '\0';i++)
    r[i] = exits[rm][i];
  
  for(i=0;(r[i] != '\0') && (c != 1);i++){
    if(r[i] == 'N' && d == 3){
      rm = rm-=9;
      c=1;}
    else if (r[i] == 'E' && d == 4){
      rm = rm+=1;
      c=1;}
    else if (r[i] == 'S' && d == 5){
      rm = rm+=9;
      c=1;}
    else if (r[i] == 'W' && d == 6){
      rm = rm-=1;
      c=1;}
  }
  if (c == 1)
    printf("You go %s\n\n",verbs[d]);
  else
    printf("You cant go %s.  There is a wall in your way\n\n",verbs[d]);

  return rm;
}

int take(int obj,int rm)
{
  extern char *sim_obj[];
  extern char *objects[];
  extern int obj_flags[];
  extern int obj_location[];
  extern int obj_takeable[];
  int i;
    
  if(obj==29){
    for(i=0;i<=13;i++){
      if(obj_location[i]==rm && obj_flags[i]==1){
        printf("You take the %s.\n",objects[i]);
        obj_flags[i]=0;
      }
    }
  }
  else
  {
    if (obj_location[obj] != rm)
      printf("The %s isnt here.\n",objects[obj]);
    if((obj_location[obj] == rm) && (obj_flags[obj] == 1) && (obj_takeable[obj] == 1)){
      printf("You take the %s.\n",objects[obj]);
      obj_flags[obj] = 0;
    }
    else
      printf("You cant take the %s.\n",objects[obj]);
  }
}

int drop(int obj,int rm)
{
  extern char *sim_obj[];
  extern char *objects[];
  extern int dead;
  extern int obj_flags[];
  extern int obj_location[];
  extern int status_flags[];
  int i;
  
  if (obj_flags[obj] != 1){
    printf("drop the %s on the floor.\n",objects[obj]);
    obj_flags[obj] = 1;
    obj_location[obj] = rm;}
  else
    printf("You dont have the %s.\n",objects[obj]);
    
  if ((obj==2) && ((status_flags[2]==0) || (status_flags[6] == 0))){   /*  Drop the suit while outside/air is off */
    printf("your suit floats to the deck, you begin to feel very strange.  Your face turns blue and ou find breathing very hard.  You soon come to the realization that there is no air in here.  Before you can react, you fall into a coma and die of asphyxiation\n");
    dead=1;
  }
}


carrying()
{
  int i ;
  extern int obj_flags[];
  extern char *objects[];
  
  printf("\nYou are carrying : \n");
  for(i=0;i<=MAXOBJ;i++){
    if (obj_flags[i] == 0)
      printf("     %s\n",objects[i]);
  }
}

int compare(char *s, char **words)
{
  int i ;
  int verb;
  
  verb = -1;
  i=0;
  while((words[i][0] != '\0') && (verb < 0))
  {
    if (strcmp(words[i] ,s) == 0)
      verb = i;
    i++;
  }
    return verb;
}

air_status(int rm)
{
  extern int status_flags[];
  
  if (rm == (76-9) && status_flags[3] == 1)
    status_flags[6] = 1;
  else if (rm == (76+9))
    status_flags[6] = 0;
}

talk(int rm, char *w)
{
  extern int status_flags[];
  
  if (rm==18 && strcmp(w,"STEVE")==0){
    printf("Steve explains everything to you.  It appears that an alien life form was found in one of the frontier systems by a group of ore miners searching for eridium in the asteroid belt.  It was brought back to Omicron for examination.  The scientist studied it and to base doctors nursed it back to health.  Soon the humanoid nicknamed ALF began to communicate and a lot was learned about its society.  Then some a large objects were picked up on the long range scanners heading straight for the system.  Worried, Steve sent a coded level 1 transmission to central command asking for orders.  Attempts to hail the crafts failed and as a last resort, measures were begun to evacuate everyone from the base but nobody got that far.  When the craft reached orbit, they began to send hundreds of smaller craft and panic fled through the base.  In a last ditch effort, steve sent a broad band request for help and led everyone into the bunker.  In the haste ALF became misplaced and with no time left, the security lock out was placed and the entrance to the bunker sealed and hidden.\n\n");
     status_flags[9]=1;
  }
  else
  {
    printf("There is nobody here to listen to you\n");
  }
}


