#include "wand_head.h"
#include <osbind.h>

extern int debug_disp;
extern long dictsize;	/* bjr */
extern FILE *dictfp;	/* bjr */

void scrn_passwd(num, passwd)	/* reads password num into passwd */
int num;
char *passwd;
{
    long position;

    position = PASSWD;
    while (position >= dictsize)
	position -= dictsize;
    fseek(dictfp,position,SEEK_SET);
    while (fgetc(dictfp) != '\n' && !feof(dictfp));
    /* read a word into passwd */
    if (fscanf(dictfp,"%s\n",passwd) == EOF) {
	rewind(dictfp);
	fscanf(dictfp,"%s\n",passwd);
    }
}

void showpass(num)
int num;
{
    long position;
    char correct[20];
    char buffer[100];
    char ch;

    if (!debug_disp)
	move(18,0);
    else
	move(20,0);
    scrn_passwd(num,correct);
    sprintf(buffer,"The password to jump to level %d ( using ~ ) is : %s        \n",(num+1),correct);
    addstr(buffer);
    addstr("PRESS ANY KEY TO REMOVE IT AND CONTINUE                          \n");
    refresh();
    ch = getch();
    if (!debug_disp)
	move(18,0);
    else
	move(20,0);
    addstr("                                                                        \n");
    addstr("                                              ");
    if (!debug_disp)
	move(18,0);
    else
	move(20,0);
    refresh();
}

int jumpscreen(num)
int num;
{
    char word[20], buffer[100], correct[20];
    int  index = 0, input;
    int  scrn;

    if (!debug_disp)
	move(16,0);
    else
	move(18,0);
    addstr("Enter number of desired level:");
    refresh();
    scrn = getnum();

    if (dictsize) {		/* if passwords are enabled */
	if (!debug_disp)
	    move(16,0);
	else
	    move(18,0);
	addstr("Please enter password of screen to jump to:");
	refresh();
	while (((word[index++] = getch()) != '\n') && (index < 19)) {
	    addch('*');
	    refresh();
	}
	word[--index]='\0';
	if (!debug_disp)
	    move(16,0);
	else
	    move(18,0);
	addstr("Validating...                                             \n");
	refresh();

	if (strcmp(word,MASTERPASSWORD) == 0) {
	    scrn_passwd(scrn-1,correct);
	    sprintf(buffer,"Certainly master, but the correct word is %s.       \n",correct);
	    if (!debug_disp)
		move(16,0);
	    else
		move(18,0);
	    addstr(buffer);
	    addstr("PRESS ANY KEY TO REMOVE IT AND CONTINUE                          \n");
	    refresh();
	    getchar();
	    if (!debug_disp)
		move(16,0);
	    else
		move(18,0);
	    addstr("                                                             ");
	    if (!debug_disp)
		move(17,0);
	    else
		move(19,0);
	    addstr("                                                             ");
	    if (!debug_disp)
		move(16,0);
	    else
		move(18,0);
	    refresh();
	    return scrn;
	}
    }

    if (scrn <= num) {
	if (!debug_disp)
	    move(16,0);
	else
	    move(18,0);
	addstr("No way, Jose! Back-jumping is prohibited!");
	refresh();
	if (!debug_disp)
	    move(16,0);
	else
	    move(18,0);
	addstr("                                                          ");
	return num;
    }

    if (!dictsize) {	/* if passwords are disabled */
	if (!debug_disp)
	    move(16,0);
	else
	    move(18,0);
	addstr("                                                ");
	return scrn;
    }

    scrn_passwd(scrn-1,correct);
    if (strcmp(correct,word) == 0) {
	if (!debug_disp)
	    move(16,0);
	else
	    move(18,0);
	addstr("Password Validated..... Jumping to desired screen.        ");
	refresh();
	return scrn;
    }

    if (!debug_disp)
	move(16,0);
    else
	move(18,0);
    addstr("PASSWORD NOT RECOGNISED!                    ");
    refresh();
    if (!debug_disp)
	move(16,0);
    else
	move(18,0);
    addstr("                                                          ");

    return num;
}

int getnum()
{
char ch;
int num = 0;

    for (ch = getch(), addch(ch), refresh(); ch >= '0' && ch <= '9';
	ch = getch(), addch(ch), refresh())
	num = num * 10 + ch - '0';
    return num;
}
