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