Home | History | Annotate | Line # | Download | only in larn
tok.c revision 1.9
      1  1.9  dholland /*	$NetBSD: tok.c,v 1.9 2008/02/03 20:11:05 dholland Exp $	*/
      2  1.5  christos 
      3  1.5  christos /* tok.c		Larn is copyrighted 1986 by Noah Morgan. */
      4  1.5  christos #include <sys/cdefs.h>
      5  1.2   mycroft #ifndef lint
      6  1.9  dholland __RCSID("$NetBSD: tok.c,v 1.9 2008/02/03 20:11:05 dholland Exp $");
      7  1.5  christos #endif				/* not lint */
      8  1.2   mycroft 
      9  1.1       cgd #include <sys/types.h>
     10  1.5  christos #include <string.h>
     11  1.1       cgd #include <sys/ioctl.h>
     12  1.5  christos #include <stdlib.h>
     13  1.5  christos #include <unistd.h>
     14  1.5  christos #include <sys/wait.h>
     15  1.9  dholland #include <ctype.h>
     16  1.1       cgd #include "header.h"
     17  1.5  christos #include "extern.h"
     18  1.1       cgd 
     19  1.5  christos static char     lastok = 0;
     20  1.8  dholland int             yrepcount = 0;
     21  1.1       cgd #ifndef FLUSHNO
     22  1.1       cgd #define FLUSHNO 5
     23  1.5  christos #endif	/* FLUSHNO */
     24  1.5  christos static int      flushno = FLUSHNO;	/* input queue flushing threshold */
     25  1.5  christos #define MAXUM 52		/* maximum number of user re-named monsters */
     26  1.5  christos #define MAXMNAME 40		/* max length of a monster re-name */
     27  1.5  christos static char     usermonster[MAXUM][MAXMNAME];	/* the user named monster
     28  1.5  christos 						 * name goes here */
     29  1.5  christos static u_char     usermpoint = 0;	/* the user monster pointer */
     30  1.1       cgd 
     31  1.1       cgd /*
     32  1.1       cgd 	lexical analyzer for larn
     33  1.1       cgd  */
     34  1.5  christos int
     35  1.1       cgd yylex()
     36  1.5  christos {
     37  1.5  christos 	char            cc;
     38  1.5  christos 	int             ic;
     39  1.5  christos 	if (hit2flag) {
     40  1.5  christos 		hit2flag = 0;
     41  1.5  christos 		yrepcount = 0;
     42  1.5  christos 		return (' ');
     43  1.5  christos 	}
     44  1.5  christos 	if (yrepcount > 0) {
     45  1.5  christos 		--yrepcount;
     46  1.5  christos 		return (lastok);
     47  1.5  christos 	} else
     48  1.5  christos 		yrepcount = 0;
     49  1.5  christos 	if (yrepcount == 0) {
     50  1.5  christos 		bottomdo();
     51  1.5  christos 		showplayer();
     52  1.5  christos 	}			/* show where the player is	 */
     53  1.5  christos 	lflush();
     54  1.5  christos 	while (1) {
     55  1.1       cgd 		c[BYTESIN]++;
     56  1.1       cgd 		if (ckpflag)
     57  1.5  christos 			if ((c[BYTESIN] % 400) == 0) {	/* check for periodic
     58  1.5  christos 							 * checkpointing */
     59  1.1       cgd #ifndef DOCHECKPOINTS
     60  1.5  christos 				savegame(ckpfile);
     61  1.1       cgd #else
     62  1.5  christos 				wait(0);	/* wait for other forks to
     63  1.5  christos 						 * finish */
     64  1.5  christos 				if (fork() == 0) {
     65  1.5  christos 					savegame(ckpfile);
     66  1.5  christos 					exit();
     67  1.5  christos 				}
     68  1.1       cgd #endif
     69  1.1       cgd 			}
     70  1.5  christos 		do {		/* if keyboard input buffer is too big, flush
     71  1.5  christos 				 * some of it */
     72  1.5  christos 			ioctl(0, FIONREAD, &ic);
     73  1.5  christos 			if (ic > flushno)
     74  1.5  christos 				read(0, &cc, 1);
     75  1.5  christos 		}
     76  1.5  christos 		while (ic > flushno);
     77  1.5  christos 
     78  1.5  christos 		if (read(0, &cc, 1) != 1)
     79  1.5  christos 			return (lastok = -1);
     80  1.1       cgd 
     81  1.5  christos 		if (cc == 'Y' - 64) {	/* control Y -- shell escape */
     82  1.5  christos 			resetscroll();
     83  1.5  christos 			clear();/* scrolling region, home, clear, no
     84  1.5  christos 				 * attributes */
     85  1.5  christos 			if ((ic = fork()) == 0) {	/* child */
     86  1.6       mrg 				execl("/bin/csh", "/bin/csh", NULL);
     87  1.5  christos 				exit(1);
     88  1.1       cgd 			}
     89  1.1       cgd 			wait(0);
     90  1.5  christos 			if (ic < 0) {	/* error */
     91  1.5  christos 				write(2, "Can't fork off a shell!\n", 25);
     92  1.5  christos 				sleep(2);
     93  1.5  christos 			}
     94  1.1       cgd 			setscroll();
     95  1.5  christos 			return (lastok = 'L' - 64);	/* redisplay screen */
     96  1.5  christos 		}
     97  1.5  christos 		if ((cc <= '9') && (cc >= '0')) {
     98  1.5  christos 			yrepcount = yrepcount * 10 + cc - '0';
     99  1.5  christos 		} else {
    100  1.5  christos 			if (yrepcount > 0)
    101  1.5  christos 				--yrepcount;
    102  1.5  christos 			return (lastok = cc);
    103  1.1       cgd 		}
    104  1.1       cgd 	}
    105  1.5  christos }
    106  1.1       cgd 
    107  1.1       cgd /*
    108  1.1       cgd  *	flushall()		Function to flush all type-ahead in the input buffer
    109  1.1       cgd  */
    110  1.5  christos void
    111  1.1       cgd flushall()
    112  1.5  christos {
    113  1.5  christos 	char            cc;
    114  1.5  christos 	int             ic;
    115  1.5  christos 	for (;;) {		/* if keyboard input buffer is too big, flush
    116  1.5  christos 				 * some of it */
    117  1.5  christos 		ioctl(0, FIONREAD, &ic);
    118  1.5  christos 		if (ic <= 0)
    119  1.5  christos 			return;
    120  1.5  christos 		while (ic > 0) {
    121  1.5  christos 			read(0, &cc, 1);
    122  1.5  christos 			--ic;
    123  1.5  christos 		}		/* gobble up the byte */
    124  1.1       cgd 	}
    125  1.5  christos }
    126  1.1       cgd 
    127  1.1       cgd /*
    128  1.5  christos 	function to set the desired hardness
    129  1.1       cgd 	enter with hard= -1 for default hardness, else any desired hardness
    130  1.1       cgd  */
    131  1.5  christos void
    132  1.1       cgd sethard(hard)
    133  1.5  christos 	int             hard;
    134  1.5  christos {
    135  1.5  christos 	int    j, k, i;
    136  1.5  christos 	j = c[HARDGAME];
    137  1.5  christos 	hashewon();
    138  1.5  christos 	if (restorflag == 0) {	/* don't set c[HARDGAME] if restoring game */
    139  1.5  christos 		if (hard >= 0)
    140  1.5  christos 			c[HARDGAME] = hard;
    141  1.5  christos 	} else
    142  1.5  christos 		c[HARDGAME] = j;/* set c[HARDGAME] to proper value if
    143  1.5  christos 				 * restoring game */
    144  1.5  christos 
    145  1.5  christos 	if ((k = c[HARDGAME]) != 0)
    146  1.5  christos 		for (j = 0; j <= MAXMONST + 8; j++) {
    147  1.5  christos 			i = ((6 + k) * monster[j].hitpoints + 1) / 6;
    148  1.5  christos 			monster[j].hitpoints = (i < 0) ? 32767 : i;
    149  1.5  christos 			i = ((6 + k) * monster[j].damage + 1) / 5;
    150  1.5  christos 			monster[j].damage = (i > 127) ? 127 : i;
    151  1.5  christos 			i = (10 * monster[j].gold) / (10 + k);
    152  1.5  christos 			monster[j].gold = (i > 32767) ? 32767 : i;
    153  1.5  christos 			i = monster[j].armorclass - k;
    154  1.5  christos 			monster[j].armorclass = (i < -127) ? -127 : i;
    155  1.5  christos 			i = (7 * monster[j].experience) / (7 + k) + 1;
    156  1.5  christos 			monster[j].experience = (i <= 0) ? 1 : i;
    157  1.1       cgd 		}
    158  1.5  christos }
    159  1.1       cgd 
    160  1.1       cgd /*
    161  1.1       cgd 	function to read and process the larn options file
    162  1.1       cgd  */
    163  1.5  christos void
    164  1.1       cgd readopts()
    165  1.5  christos {
    166  1.7  dholland 	const char  *i;
    167  1.5  christos 	int    j, k;
    168  1.5  christos 	int             flag;
    169  1.5  christos 	flag = 1;		/* set to 0 if he specifies a name for his
    170  1.5  christos 				 * character */
    171  1.5  christos 	if (lopen(optsfile) < 0) {
    172  1.5  christos 		strcpy(logname, loginname);
    173  1.5  christos 		return;		/* user name if no character name */
    174  1.5  christos 	}
    175  1.1       cgd 	i = " ";
    176  1.5  christos 	while (*i) {
    177  1.7  dholland 		if ((i = lgetw()) == NULL)
    178  1.5  christos 			break;	/* check for EOF */
    179  1.5  christos 		while ((*i == ' ') || (*i == '\t'))
    180  1.5  christos 			i++;	/* eat leading whitespace */
    181  1.5  christos 		switch (*i) {
    182  1.5  christos 		case 'b':
    183  1.5  christos 			if (strcmp(i, "bold-objects") == 0)
    184  1.5  christos 				boldon = 1;
    185  1.5  christos 			break;
    186  1.5  christos 
    187  1.5  christos 		case 'e':
    188  1.5  christos 			if (strcmp(i, "enable-checkpointing") == 0)
    189  1.5  christos 				ckpflag = 1;
    190  1.5  christos 			break;
    191  1.5  christos 
    192  1.5  christos 		case 'i':
    193  1.5  christos 			if (strcmp(i, "inverse-objects") == 0)
    194  1.5  christos 				boldon = 0;
    195  1.5  christos 			break;
    196  1.5  christos 
    197  1.5  christos 		case 'f':
    198  1.5  christos 			if (strcmp(i, "female") == 0)
    199  1.5  christos 				sex = 0;	/* male or female */
    200  1.5  christos 			break;
    201  1.5  christos 
    202  1.5  christos 		case 'm':
    203  1.5  christos 			if (strcmp(i, "monster:") == 0) {	/* name favorite monster */
    204  1.5  christos 				if ((i = lgetw()) == 0)
    205  1.1       cgd 					break;
    206  1.7  dholland 				strlcpy(usermonster[usermpoint], i, MAXMNAME);
    207  1.5  christos 				if (usermpoint >= MAXUM)
    208  1.5  christos 					break;	/* defined all of em */
    209  1.5  christos 				if (isalpha(j = usermonster[usermpoint][0])) {
    210  1.5  christos 					for (k = 1; k < MAXMONST + 8; k++)	/* find monster */
    211  1.5  christos 						if (monstnamelist[k] == j) {
    212  1.5  christos 							monster[k].name = &usermonster[usermpoint++][0];
    213  1.5  christos 							break;
    214  1.1       cgd 						}
    215  1.5  christos 				}
    216  1.5  christos 			} else if (strcmp(i, "male") == 0)
    217  1.5  christos 				sex = 1;
    218  1.5  christos 			break;
    219  1.5  christos 
    220  1.5  christos 		case 'n':
    221  1.5  christos 			if (strcmp(i, "name:") == 0) {	/* defining players name */
    222  1.5  christos 				if ((i = lgetw()) == 0)
    223  1.1       cgd 					break;
    224  1.7  dholland 				strlcpy(logname, i, LOGNAMESIZE);
    225  1.5  christos 				flag = 0;
    226  1.5  christos 			} else if (strcmp(i, "no-introduction") == 0)
    227  1.5  christos 				nowelcome = 1;
    228  1.5  christos 			else if (strcmp(i, "no-beep") == 0)
    229  1.5  christos 				nobeep = 1;
    230  1.5  christos 			break;
    231  1.5  christos 
    232  1.5  christos 		case 'p':
    233  1.5  christos 			if (strcmp(i, "process-name:") == 0) {
    234  1.5  christos 				if ((i = lgetw()) == 0)
    235  1.1       cgd 					break;
    236  1.7  dholland 				strlcpy(psname, i, PSNAMESIZE);
    237  1.8  dholland 			} else if (strcmp(i, "play-day-play") == 0) {
    238  1.8  dholland 				/* bypass time restrictions: ignored */
    239  1.8  dholland 			}
    240  1.5  christos 			break;
    241  1.5  christos 
    242  1.5  christos 		case 's':
    243  1.5  christos 			if (strcmp(i, "savefile:") == 0) {	/* defining savefilename */
    244  1.5  christos 				if ((i = lgetw()) == 0)
    245  1.1       cgd 					break;
    246  1.5  christos 				strcpy(savefilename, i);
    247  1.5  christos 				flag = 0;
    248  1.5  christos 			}
    249  1.5  christos 			break;
    250  1.1       cgd 		};
    251  1.1       cgd 	}
    252  1.5  christos 	if (flag)
    253  1.5  christos 		strcpy(logname, loginname);
    254  1.5  christos }
    255