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