#include <stdio.h>

#include "file.h"

extern void save(int rm)
{
  FILE *fp;
  
  int i, c;
  char s[32];
  char f[32];
  extern int obj_flags[];
  extern int status_flags[];
  extern char *exits[];
  extern int obj_location[];
  
  printf("Enter a file name ->");
  gets(s);
  
  fp=fopen(("<saves$dir>.%s",s),"w");
  
  for(i=0;i<=21;i++)
    putc(obj_flags[i], fp);
  
  for(i=0;i<=21;i++)
    putc(obj_location[i],fp);
  
  for(i=0;i<=10;i++)
    putc(status_flags[i],fp);
   
 putc(rm,fp);
 
 fclose(fp);
}

extern int load(void)
{
  FILE *fp;
  
  int i, c, rm;
  int temp[60];
  char s[12];
  extern int obj_flags[];
  extern int status_flags[];
  extern int obj_location[];
  extern char *exits[];
  extern char *locations[];
  
  printf("Enter a file name ->");
  gets(s);
  
  if(!(fp=fopen(("<saves$dir>.%s",s),"r"))){
    printf("Can't open file %s\n",s);
  }
  else
  {
    for(i=0;i<=21;i++)
      obj_flags[i]=getc(fp);
    
    for(i=0;i<=21;i++)
      obj_location[i]=getc(fp);
    
    for(i=0;i<=10;i++)
      status_flags[i]=getc(fp);
      
    rm = getc(fp);
    
    if(obj_location[9]==90){
      exits[106]="EW";
    }
    
    if(obj_location[10]==90){
      exits[61]="ESW";
      exits[71]="NSW";
      exits[79]="NES";
    }
    
    if(obj_location[11]==90)
      exits[37]="ESW";
    
    if(obj_location[12]==90 && obj_location[13]!=90)
      exits[58]="NS";
    else if(obj_location[13]==90 && obj_location[12]!=90)
      exits[58]="ES";
    else if(obj_location[12]==90 && obj_location[13]==90)
      exits[58]="NES";
    else
      exits[58]="S";
      
    if(obj_location[19]==90){
      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";
    }
  
    if(status_flags[3]==0){
      exits[99]="ES";
      exits[107]="NSW";
    }
    
    if(status_flags[7]==1){
      locations[40]="You are in the northen security cell.  There is a secret door behind the pannel to the east";
      exits[40]="ES";
    }
    
    if(status_flags[8]==1){
      locations[41]="in a secret room next to the northern security cell";
      exits[41]="NW";
    }
  
  fclose(fp);
  return rm;
  }
}
