hammer viconfig: remove 2nd 'rebalance'
[dragonfly.git] / games / larn / tok.c
blob1cfefd1170bf2346c711bd88dd2d81d9ecd9cdc0
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"
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 */
21 lexical analyzer for larn
23 int
24 yylex(void)
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)
34 c[BYTESIN]++;
35 if (ckpflag)
36 if ((c[BYTESIN] % 400) == 0) /* check for periodic checkpointing */
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
46 #ifdef TIMECHECK
47 if (dayplay==0)
48 if (playable())
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);
55 #endif /* TIMECHECK */
59 do /* if keyboard input buffer is too big, flush some of it */
61 ioctl(0,FIONREAD,&ic);
62 if (ic>flushno) read(0,&cc,1);
64 while (ic>flushno);
66 if (read(0,&cc,1) != 1) return(lastok = -1);
68 if (cc == 'Y'-64) /* control Y -- shell escape */
70 resetscroll(); clear(); /* scrolling region, home, clear, no attributes */
71 if ((ic=fork())==0) /* child */
73 /* revoke */
74 setgid(getgid());
75 execl("/bin/csh", "csh", NULL); exit(1);
77 wait(0);
78 if (ic<0) /* error */
80 write(2,"Can't fork off a shell!\n",25); sleep(2);
83 setscroll();
84 return(lastok = 'L'-64); /* redisplay screen */
87 if ((cc <= '9') && (cc >= '0'))
88 { yrepcount = yrepcount*10 + cc - '0'; }
89 else { if (yrepcount>0) --yrepcount; return(lastok = cc); }
94 * flushall() Function to flush all type-ahead in the input buffer
96 void
97 flushall(void)
99 char cc;
100 int ic;
101 for (;;) /* if keyboard input buffer is too big, flush some of it */
103 ioctl(0,FIONREAD,&ic);
104 if (ic<=0) return;
105 while (ic>0) { read(0,&cc,1); --ic; } /* gobble up the byte */
110 function to set the desired hardness
111 enter with hard= -1 for default hardness, else any desired hardness
113 void
114 sethard(int hard)
116 int j,k,i;
117 j=c[HARDGAME]; hashewon();
118 if (restorflag==0) /* don't set c[HARDGAME] if restoring game */
120 if (hard >= 0) c[HARDGAME]= hard;
122 else c[HARDGAME]=j; /* set c[HARDGAME] to proper value if restoring game */
124 if ((k=c[HARDGAME]))
125 for (j=0; j<=MAXMONST+8; j++)
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;
141 function to read and process the larn options file
143 void
144 readopts(void)
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)
152 strcpy(logname,loginname); return; /* user name if no character name */
156 if ((i=(char *)lgetw()) == 0) break; /* check for EOF */
157 while ((*i==' ') || (*i=='\t')) i++; /* eat leading whitespace */
158 switch(*i)
160 case 'b': if (strcmp(i,"bold-objects") == 0) boldon=1;
161 break;
163 case 'e': if (strcmp(i,"enable-checkpointing") == 0) ckpflag=1;
164 break;
166 case 'i': if (strcmp(i,"inverse-objects") == 0) boldon=0;
167 break;
169 case 'f': if (strcmp(i,"female") == 0) sex=0; /* male or female */
170 break;
172 case 'm': if (strcmp(i,"monster:")== 0) /* name favorite monster */
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]))
180 for (k=1; k<MAXMONST+8; k++) /* find monster */
181 if (monstnamelist[k] == j)
183 monster[k].name = &usermonster[(int)usermpoint++][0];
184 break;
188 else if (strcmp(i,"male") == 0) sex=1;
189 break;
191 case 'n': if (strcmp(i,"name:") == 0) /* defining players name */
193 if ((i=lgetw())==0) break;
194 if (strlen(i)>=LOGNAMESIZE) i[LOGNAMESIZE-1]=0;
195 strcpy(logname,i); flag=0;
197 else if (strcmp(i,"no-introduction") == 0) nowelcome=1;
198 else if (strcmp(i,"no-beep") == 0) nobeep=1;
199 break;
201 case 'p': if (strcmp(i,"process-name:")== 0)
203 if ((i=lgetw())==0) break;
204 if (strlen(i)>=PSNAMESIZE) i[PSNAMESIZE-1]=0;
205 strcpy(psname,i);
207 else if (strcmp(i,"play-day-play") == 0) dayplay=1;
208 break;
210 case 's': if (strcmp(i,"savefile:") == 0) /* defining savefilename */
212 if ((i=lgetw())==0) break;
213 strcpy(savefilename,i); flag=0;
215 break;
217 } while(*i);
218 if (flag) strcpy(logname,loginname);