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