#include "header.h"
static char copyright[]="Larn is copyrighted 1986 by Noah Morgan.\n";
extern char *optsfile,boldon,sex,monstnamelist[],iven[],logname[],nowelcome;
extern char restorflag,ckpflag,*ckpfile,savefilename[],psname[],loginname[];
extern char nomove,wizard;
extern struct monst monster[];
extern short hit2flag;
extern int c[];
static char lastok=0;
int yrepcount=0,dayplay=0;
static char usermonster[27][40];	/* the user named monster name goes here */
static char usermpoint=0;			/* the user monster pointer */
static char optsf[40];
char *lgetw();

/*
	lexical analyzer for larn
 */
yylex()
	{
	char cc;
	register int k;
	if (hit2flag) { hit2flag=0;  yrepcount=0;  return(' '); }
	if (yrepcount>0)	{ --yrepcount;  return(lastok);	} else yrepcount=0;
	if (yrepcount==0) { bottomdo(); showplayer(); }	/*	show where the player is	*/
	lflush();  
	while (1)
		{
		c[BYTESIN]++;
		if (ckpflag)
		  if ((c[BYTESIN] % 400) == 0)
			{
#ifdef DOCHECKPOINTS
			savegame(ckpfile);
#else
			wait(0);	/* wait for other forks to finish */
			if (fork() == 0) { savegame(ckpfile); exit(); }
#endif

#ifdef TIMECHECK
		if (dayplay==0)
			if (playable())
				{
				cursor(1,19);
				lprcat("\n\7Sorry, but it is now time for work.  Your game has been saved.\n");
				lflush();  savegame(savefilename);  wizard=nomove=1;  sleep(4);
				died(-257);
				}
#endif TIMECHECK

			}
		if (read(0,&cc,1) != 1) return(lastok = -1);
		if ((cc <= '9') && (cc >= '0'))
			{ yrepcount = yrepcount*10 + cc - '0'; }
		else	{ if (yrepcount>0) --yrepcount;  return(lastok = cc); }
		}
	}

/*
	function to set the desired hardness 
	enter with hard= -1 for default hardness, else any desired hardness
 */
sethard(hard)
	int hard;
	{
	register int j,k,i;
	j=c[HARDGAME]; hashewon();  /* set c[HARDGAME] to proper value */
	if (restorflag==0)	/* dont set c[HARDGAME] if restoring game */
		{
		if (hard >= 0) c[HARDGAME]= hard;
		}
	else c[HARDGAME]=j; /* set hard to proper value (as from restorefile) */

	if (k=c[HARDGAME])
	  for (j=0; j<=MAXMONST+8; j++)
		{
		i = ((6+k)*monster[j].hitpoints+1)/6;
		monster[j].hitpoints = (i<0) ? 32767 : i;
		i = ((6+k)*monster[j].damage+1)/5;
		monster[j].damage = (i>127) ? 127 : i;
		i = (10*monster[j].gold)/(10+k);
		monster[j].gold = (i>32767) ? 32767 : i;
		i = monster[j].armorclass - k;
		monster[j].armorclass = (i< -127) ? -127 : i;
		i = (7*monster[j].experience)/(7+k) + 1;
		monster[j].experience = (i<=0) ? 1 : i;
		}				
	}

/*
	function to read and process the larn options file
 */
readopts()
	{
	register char *i;
	register int j,k;
	int flag;
	flag=1;	/* set to 0 if he specifies a name for his character */
	*optsf=0;	k=strcpy(optsf,getenv("HOME"));
	strcpy(optsf+k,optsfile); /* k is length of home directory pathname */
	if (lopen(optsf) < 0)
		{
		strcpy(logname,loginname); return; /* user name if no character name */
		}
	i = " ";
	while (*i)
	  {
	  i = (char *)lgetw();
	  switch(*i)
		{
		case 'b':	if (strcmp(i,"bold-objects") == 0)  boldon=1;
					break;

		case 'e':	if (strcmp(i,"enable-checkpointing") == 0) ckpflag=1;
					break;

		case 'i':	if (strcmp(i,"inverse-objects") == 0)  boldon=0;
					break;

		case 'f':	if (strcmp(i,"female") 	== 0)	sex=0; /* male or female */
					break;

		case 'm':	if (strcmp(i,"monster:")== 0)   /* name favorite monster */
						{
						strcpy(usermonster[usermpoint],lgetw()); 
						if (usermpoint >= 26) break; /* defined all of em */
						if (isalpha((j=usermonster[usermpoint][0])) != 0)
							{
							j = lcc(j);
							for (k=1; k<MAXMONST+8; k++)
							  if (monstnamelist[k] == j)
								{
								monster[k].name = &usermonster[usermpoint++][0];
								k=127;
								}
							}
						}
					else if (strcmp(i,"male") == 0)	sex=1;
					break;

		case 'n':	if (strcmp(i,"name:") == 0) /* defining players name */
						{
						strcpy(logname,lgetw()); flag=0;
						}
					else if (strcmp(i,"no-introduction") == 0) nowelcome=1;
					break;

		case 'p':	if (strcmp(i,"process-name:")== 0)  strcpy(psname,lgetw());
					else if (strcmp(i,"play-day-play") == 0)  dayplay=1;
					break;
		};
	  }
	if (flag)  strcpy(logname,loginname);
	}

