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