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