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