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