/*
 * Stonage: a puzzle game
 * Copyright (C) 2001 Luc Saillard <luc@saillard.org>
 * 
 * Some functions to deal with the password level.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * 
 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "level_code.h"
#include "stonage.h"

static char voyelles[]="AIUEO";
static char consonnes[]="BVSLFHDM";

static int level_hash(int lvl_num);

/**
 * Convert a level number to a password level.
 * This function generate a password to permit the user to continue at this
 * level. The pass phrase can work on any computer/plateform. This is a word of
 * 6 letters.
 * @param lvl_num the level number
 * @return a static string corresponding to the level.
 */
const char *
level_encode(int lvl_num)
{
  int n,i;
  int voyelle,consonne;
  static char lvl_word[7];

  n=level_hash(lvl_num);
  for (i=0;i<3;i++)
   {
     consonne=(n%8);
     n/=8;
     voyelle=(n%5);
     n/=5;
     lvl_word[i*2]=consonnes[consonne];
     lvl_word[i*2+1]=voyelles[voyelle];
   }
  lvl_word[6]=0;

  return lvl_word;
}

static int
level_hash(int lvl_num)
{
  unsigned int n;
  int i;

  n=0;
  for (i=0;i<2;i++)
   {
     n += lvl_num << 4;
     n += lvl_num >> 4;
     n *= 11;
   }
  return n;
}

/**
 * Convert a password level to the corresponding level number.
 * This function return the level number of the password, if the password is
 * correct. For now, this is not optimized. If the password is incorrect,
 * return 0.
 * @param lvl_name The password level (need to terminate with a null byte and
 * upper character)
 * @return a level number or 0 if it isn't correct.
 */
int
level_decode(const char *lvl_name)
{
  int i;

  if (lvl_name==NULL || strlen(lvl_name)!=6)
    return 0;

  for (i=1;i<4096;i++)
   {
     if (memcmp(level_encode(i),lvl_name,6)==0)
       return i;
   }
  return 0;
}


#ifdef TEST

#include <string.h>

int main(int argc, char **argv)
{
  int i,dump=0;
  char *lvl_name;

  if (argc==2)
   {
     if (strcmp(argv[1],"dump")==0)
       dump=1;
   }
  
  for (i=0;i<4096;i++)
   {
     lvl_name=strdup(level_encode(i));
     if (dump)
       printf("Level %d: %s\n",i,lvl_name);
     if (level_decode(lvl_name)!=i)
      {
	fprintf(stderr,"Bug in encoding/decondig level (%d) encode=%s decode=%d\n",
	    i,lvl_name,level_decode(lvl_name));
       exit(1);
      }
     free(lvl_name);
   }
  return 0;
}

#endif

