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