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