/* UnderWorldCheat (uwc.exe) */

/* i never write comments unless i'm getting paid */

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <io.h>
#include <math.h>

int main()
{

   typedef struct {
      char desc[11];
      int offst;
      unsigned char val;
   } oneline;

   oneline dat[27];
   char pad[23],name[13];
   FILE *in,*out;
   unsigned char xx,yy;
   int i,x,choice;

   for (i=1;i<=3;++i) dat[i-1].offst=i+30;
   dat[3].offst=55;
   dat[4].offst=57;
   dat[5].offst=62;
   for (i=6;i<=25;++i) dat[i].offst=i+28;
   strcpy(dat[0].desc,"Str");
   strcpy(dat[1].desc,"Dex");
   strcpy(dat[2].desc,"Int");
   strcpy(dat[3].desc,"Vit (max)");
   strcpy(dat[4].desc,"Mana (max)");
   strcpy(dat[5].desc,"Level");
   strcpy(dat[6].desc,"Attack");
   strcpy(dat[7].desc,"Defense");
   strcpy(dat[8].desc,"Unarmed");
   strcpy(dat[9].desc,"Sword");
   strcpy(dat[10].desc,"Axe");
   strcpy(dat[11].desc,"Mace");
   strcpy(dat[12].desc,"Missile");
   strcpy(dat[13].desc,"Mana");
   strcpy(dat[14].desc,"Lore");
   strcpy(dat[15].desc,"Casting");
   strcpy(dat[16].desc,"Traps");
   strcpy(dat[17].desc,"Search");
   strcpy(dat[18].desc,"Track");
   strcpy(dat[19].desc,"Sneak");
   strcpy(dat[20].desc,"Repair");
   strcpy(dat[21].desc,"Charm");
   strcpy(dat[22].desc,"Picklock");
   strcpy(dat[23].desc,"Acrobat");
   strcpy(dat[24].desc,"Appraise");
   strcpy(dat[25].desc,"Swimming");

   if ((in=fopen("player.dat","rb"))==NULL)
      {
	 fprintf(stderr,"\nCannot open input file (player.dat).\n");
	 printf("Put this program in one of the UW save directories\n");
	 printf("i.e. SAVE1,SAVE2,SAVE3, or SAVE4.\n");
	 printf("This program is looking for the file 'player.dat'\n");
	 return 1;
      }

   tmpnam(name);

   if ((out=fopen(name,"w+b")) == NULL)
      {
	 fprintf(stderr,"Cannot open output file.\n");
	 return 1;
      }

   xx=fgetc(in);
   yy=0;

   while(!feof(in))
   {
      fputc(yy^xx,out);
      xx=xx+3;
      yy=fgetc(in);
   }
   fclose(in);

   for(i=0;i<=25;++i)
   {
      fseek(out,dat[i].offst,SEEK_SET);
      dat[i].val=fgetc(out);
   }

   choice=-1;
   do
   {
      if ( !((choice>25)||(choice<0)) )
      {
	 printf("Change '%s' to ->",dat[choice].desc);
	 scanf("%d",&i);
	 if(i>255)i=255;
	 if(i<0)i=0;
	 fflush(stdin);
	 dat[choice].val=i;
      }

      printf("\n\n\n\n");
      printf("Personal Statistics:\n");
      for(i=0;i<=5;++i)
      {
	 printf ("%2d: %10s=%3d  \n",i,dat[i].desc,dat[i].val);
      }
      x=1;
      printf("Skills:\n");

      for(i=6;i<=25;++i)
      {
	 printf ("%2d: %10s=%3d                ",i,dat[i].desc,dat[i].val);
	 x=x^1;
	 if(x==1)printf("\n");
      }

      printf("\nChange which number? (26 to stop editing) ->");
      scanf("%d",&choice);
      fflush(stdin);
   } while (choice != 26);

   printf("\nType 's' to save or anything else to abort ->");
   xx=getc(stdin);

   if ((xx=='s')||(xx=='S'))
   {
      printf("Save..");
      remove("player.bak");
      rename("player.dat","player.bak");
      for(i=0;i<=25;++i)
      {
	 fseek(out,dat[i].offst,SEEK_SET);
	 fputc(dat[i].val,out);
      }
      in=fopen("player.dat","w+b");
      fseek(out,0,SEEK_SET);
      xx=fgetc(out);
      yy=0;
      while(!feof(out))
      {
	 fputc(yy^xx,in);
	 xx=xx+3;
	 yy=fgetc(out);
      }
      fclose(in);
      fclose(out);
      remove(name);
      printf("done!\n");
   }
   else
   {
      printf("Don't save\n");
      fclose(out);
      remove(name);
   }

/*You cheat like a Stygian lizard man */

   return 0; /* END (uwc) */
}