main.c revision 1.7 1 1.3 mycroft #ifndef lint
2 1.7 cgd static char rcsid[] = "$NetBSD: main.c,v 1.7 1995/04/24 12:24:01 cgd Exp $";
3 1.3 mycroft #endif /* not lint */
4 1.3 mycroft
5 1.1 cgd /* main.c */
6 1.1 cgd #include <sys/types.h>
7 1.1 cgd #include "header.h"
8 1.1 cgd #include <pwd.h>
9 1.7 cgd #include <string.h>
10 1.7 cgd
11 1.1 cgd static char copyright[]="\nLarn is copyrighted 1986 by Noah Morgan.\n";
12 1.1 cgd int srcount=0; /* line counter for showstr() */
13 1.1 cgd int dropflag=0; /* if 1 then don't lookforobject() next round */
14 1.1 cgd int rmst=80; /* random monster creation counter */
15 1.1 cgd int userid; /* the players login user id number */
16 1.1 cgd char nowelcome=0,nomove=0; /* if (nomove) then don't count next iteration as a move */
17 1.1 cgd static char viewflag=0;
18 1.1 cgd /* if viewflag then we have done a 99 stay here and don't showcell in the main loop */
19 1.1 cgd char restorflag=0; /* 1 means restore has been done */
20 1.1 cgd static char cmdhelp[] = "\
21 1.1 cgd Cmd line format: larn [-slicnh] [-o<optsifle>] [-##] [++]\n\
22 1.1 cgd -s show the scoreboard\n\
23 1.1 cgd -l show the logfile (wizard id only)\n\
24 1.1 cgd -i show scoreboard with inventories of dead characters\n\
25 1.1 cgd -c create new scoreboard (wizard id only)\n\
26 1.1 cgd -n suppress welcome message on starting game\n\
27 1.1 cgd -## specify level of difficulty (example: -5)\n\
28 1.1 cgd -h print this help text\n\
29 1.1 cgd ++ restore game from checkpoint file\n\
30 1.1 cgd -o<optsfile> specify .larnopts filename to be used instead of \"~/.larnopts\"\n\
31 1.1 cgd ";
32 1.1 cgd #ifdef VT100
33 1.1 cgd static char *termtypes[] = { "vt100", "vt101", "vt102", "vt103", "vt125",
34 1.1 cgd "vt131", "vt140", "vt180", "vt220", "vt240", "vt241", "vt320", "vt340",
35 1.1 cgd "vt341" };
36 1.1 cgd #endif VT100
37 1.1 cgd /*
38 1.1 cgd ************
39 1.1 cgd MAIN PROGRAM
40 1.1 cgd ************
41 1.1 cgd */
42 1.1 cgd main(argc,argv)
43 1.1 cgd int argc;
44 1.1 cgd char **argv;
45 1.1 cgd {
46 1.1 cgd register int i,j;
47 1.1 cgd int hard;
48 1.1 cgd char *ptr=0,*ttype;
49 1.1 cgd struct passwd *pwe;
50 1.1 cgd
51 1.1 cgd /*
52 1.1 cgd * first task is to identify the player
53 1.1 cgd */
54 1.1 cgd #ifndef VT100
55 1.1 cgd init_term(); /* setup the terminal (find out what type) for termcap */
56 1.1 cgd #endif VT100
57 1.1 cgd if (((ptr = getlogin()) == 0) || (*ptr==0)) /* try to get login name */
58 1.1 cgd if (pwe=getpwuid(getuid())) /* can we get it from /etc/passwd? */
59 1.1 cgd ptr = pwe->pw_name;
60 1.1 cgd else
61 1.1 cgd if ((ptr = getenv("USER")) == 0)
62 1.1 cgd if ((ptr = getenv("LOGNAME")) == 0)
63 1.1 cgd {
64 1.1 cgd noone: write(2, "Can't find your logname. Who Are You?\n",39);
65 1.1 cgd exit();
66 1.1 cgd }
67 1.1 cgd if (ptr==0) goto noone;
68 1.1 cgd if (strlen(ptr)==0) goto noone;
69 1.1 cgd /*
70 1.1 cgd * second task is to prepare the pathnames the player will need
71 1.1 cgd */
72 1.1 cgd strcpy(loginname,ptr); /* save loginname of the user for logging purposes */
73 1.1 cgd strcpy(logname,ptr); /* this will be overwritten with the players name */
74 1.1 cgd if ((ptr = getenv("HOME")) == 0) ptr = ".";
75 1.1 cgd strcpy(savefilename, ptr);
76 1.1 cgd strcat(savefilename, "/Larn.sav"); /* save file name in home directory */
77 1.1 cgd sprintf(optsfile, "%s/.larnopts",ptr); /* the .larnopts filename */
78 1.1 cgd
79 1.1 cgd /*
80 1.1 cgd * now malloc the memory for the dungeon
81 1.1 cgd */
82 1.1 cgd cell = (struct cel *)malloc(sizeof(struct cel)*(MAXLEVEL+MAXVLEVEL)*MAXX*MAXY);
83 1.1 cgd if (cell == 0) died(-285); /* malloc failure */
84 1.1 cgd lpbuf = malloc((5* BUFBIG)>>2); /* output buffer */
85 1.1 cgd inbuffer = malloc((5*MAXIBUF)>>2); /* output buffer */
86 1.1 cgd if ((lpbuf==0) || (inbuffer==0)) died(-285); /* malloc() failure */
87 1.1 cgd
88 1.1 cgd lcreat((char*)0); newgame(); /* set the initial clock */ hard= -1;
89 1.1 cgd
90 1.1 cgd #ifdef VT100
91 1.1 cgd /*
92 1.1 cgd * check terminal type to avoid users who have not vt100 type terminals
93 1.1 cgd */
94 1.1 cgd ttype = getenv("TERM");
95 1.1 cgd for (j=1, i=0; i<sizeof(termtypes)/sizeof(char *); i++)
96 1.1 cgd if (strcmp(ttype,termtypes[i]) == 0) { j=0; break; }
97 1.1 cgd if (j)
98 1.1 cgd {
99 1.1 cgd lprcat("Sorry, Larn needs a VT100 family terminal for all it's features.\n"); lflush();
100 1.1 cgd exit();
101 1.1 cgd }
102 1.1 cgd #endif VT100
103 1.1 cgd
104 1.1 cgd /*
105 1.1 cgd * now make scoreboard if it is not there (don't clear)
106 1.1 cgd */
107 1.1 cgd if (access(scorefile,0) == -1) /* not there */
108 1.1 cgd makeboard();
109 1.1 cgd
110 1.1 cgd /*
111 1.1 cgd * now process the command line arguments
112 1.1 cgd */
113 1.1 cgd for (i=1; i<argc; i++)
114 1.1 cgd {
115 1.1 cgd if (argv[i][0] == '-')
116 1.1 cgd switch(argv[i][1])
117 1.1 cgd {
118 1.1 cgd case 's': showscores(); exit(); /* show scoreboard */
119 1.1 cgd
120 1.1 cgd case 'l': /* show log file */
121 1.1 cgd diedlog(); exit();
122 1.1 cgd
123 1.1 cgd case 'i': showallscores(); exit(); /* show all scoreboard */
124 1.1 cgd
125 1.1 cgd case 'c': /* anyone with password can create scoreboard */
126 1.1 cgd lprcat("Preparing to initialize the scoreboard.\n");
127 1.1 cgd if (getpassword() != 0) /*make new scoreboard*/
128 1.1 cgd {
129 1.1 cgd makeboard(); lprc('\n'); showscores();
130 1.1 cgd }
131 1.1 cgd exit();
132 1.1 cgd
133 1.1 cgd case 'n': /* no welcome msg */ nowelcome=1; argv[i][0]=0; break;
134 1.1 cgd
135 1.1 cgd case '0': case '1': case '2': case '3': case '4': case '5':
136 1.1 cgd case '6': case '7': case '8': case '9': /* for hardness */
137 1.1 cgd sscanf(&argv[i][1],"%d",&hard);
138 1.1 cgd break;
139 1.1 cgd
140 1.1 cgd case 'h': /* print out command line arguments */
141 1.1 cgd write(1,cmdhelp,sizeof(cmdhelp)); exit();
142 1.1 cgd
143 1.1 cgd case 'o': /* specify a .larnopts filename */
144 1.1 cgd strncpy(optsfile,argv[i]+2,127); break;
145 1.1 cgd
146 1.1 cgd default: printf("Unknown option <%s>\n",argv[i]); exit();
147 1.1 cgd };
148 1.1 cgd
149 1.1 cgd if (argv[i][0] == '+')
150 1.1 cgd {
151 1.1 cgd clear(); restorflag = 1;
152 1.1 cgd if (argv[i][1] == '+')
153 1.1 cgd {
154 1.1 cgd hitflag=1; restoregame(ckpfile); /* restore checkpointed game */
155 1.1 cgd }
156 1.1 cgd i = argc;
157 1.1 cgd }
158 1.1 cgd }
159 1.1 cgd
160 1.1 cgd readopts(); /* read the options file if there is one */
161 1.1 cgd
162 1.1 cgd
163 1.1 cgd #ifdef UIDSCORE
164 1.1 cgd userid = geteuid(); /* obtain the user's effective id number */
165 1.1 cgd #else UIDSCORE
166 1.1 cgd userid = getplid(logname); /* obtain the players id number */
167 1.1 cgd #endif UIDSCORE
168 1.1 cgd if (userid < 0) { write(2,"Can't obtain playerid\n",22); exit(); }
169 1.1 cgd
170 1.1 cgd #ifdef HIDEBYLINK
171 1.1 cgd /*
172 1.1 cgd * this section of code causes the program to look like something else to ps
173 1.1 cgd */
174 1.1 cgd if (strcmp(psname,argv[0])) /* if a different process name only */
175 1.1 cgd {
176 1.1 cgd if ((i=access(psname,1)) < 0)
177 1.1 cgd { /* link not there */
178 1.1 cgd if (link(argv[0],psname)>=0)
179 1.1 cgd {
180 1.1 cgd argv[0] = psname; execv(psname,argv);
181 1.1 cgd }
182 1.1 cgd }
183 1.1 cgd else
184 1.1 cgd unlink(psname);
185 1.1 cgd }
186 1.1 cgd
187 1.1 cgd for (i=1; i<argc; i++)
188 1.1 cgd {
189 1.1 cgd szero(argv[i]); /* zero the argument to avoid ps snooping */
190 1.1 cgd }
191 1.1 cgd #endif HIDEBYLINK
192 1.1 cgd
193 1.1 cgd if (access(savefilename,0)==0) /* restore game if need to */
194 1.1 cgd {
195 1.1 cgd clear(); restorflag = 1;
196 1.1 cgd hitflag=1; restoregame(savefilename); /* restore last game */
197 1.1 cgd }
198 1.1 cgd sigsetup(); /* trap all needed signals */
199 1.1 cgd sethard(hard); /* set up the desired difficulty */
200 1.1 cgd setupvt100(); /* setup the terminal special mode */
201 1.1 cgd if (c[HP]==0) /* create new game */
202 1.1 cgd {
203 1.1 cgd makeplayer(); /* make the character that will play */
204 1.1 cgd newcavelevel(0);/* make the dungeon */
205 1.1 cgd predostuff = 1; /* tell signals that we are in the welcome screen */
206 1.1 cgd if (nowelcome==0) welcome(); /* welcome the player to the game */
207 1.1 cgd }
208 1.1 cgd drawscreen(); /* show the initial dungeon */
209 1.1 cgd predostuff = 2; /* tell the trap functions that they must do a showplayer()
210 1.1 cgd from here on */
211 1.1 cgd /* nice(1); /* games should be run niced */
212 1.1 cgd yrepcount = hit2flag = 0;
213 1.1 cgd while (1)
214 1.1 cgd {
215 1.1 cgd if (dropflag==0) lookforobject(); /* see if there is an object here */
216 1.1 cgd else dropflag=0; /* don't show it just dropped an item */
217 1.1 cgd if (hitflag==0) { if (c[HASTEMONST]) movemonst(); movemonst(); } /* move the monsters */
218 1.1 cgd if (viewflag==0) showcell(playerx,playery); else viewflag=0; /* show stuff around player */
219 1.1 cgd if (hit3flag) flushall();
220 1.1 cgd hitflag=hit3flag=0; nomove=1;
221 1.1 cgd bot_linex(); /* update bottom line */
222 1.1 cgd while (nomove)
223 1.1 cgd {
224 1.1 cgd if (hit3flag) flushall();
225 1.1 cgd nomove=0; parse();
226 1.1 cgd } /* get commands and make moves */
227 1.1 cgd regen(); /* regenerate hp and spells */
228 1.1 cgd if (c[TIMESTOP]==0)
229 1.1 cgd if (--rmst <= 0)
230 1.1 cgd { rmst = 120-(level<<2); fillmonst(makemonst(level)); }
231 1.1 cgd }
232 1.1 cgd }
233 1.1 cgd
234 1.1 cgd /*
236 1.1 cgd showstr()
237 1.1 cgd
238 1.1 cgd show character's inventory
239 1.1 cgd */
240 1.1 cgd showstr()
241 1.1 cgd {
242 1.1 cgd register int i,number;
243 1.1 cgd for (number=3, i=0; i<26; i++)
244 1.1 cgd if (iven[i]) number++; /* count items in inventory */
245 1.1 cgd t_setup(number); qshowstr(); t_endup(number);
246 1.1 cgd }
247 1.1 cgd
248 1.1 cgd qshowstr()
249 1.1 cgd {
250 1.1 cgd register int i,j,k,sigsav;
251 1.1 cgd srcount=0; sigsav=nosignal; nosignal=1; /* don't allow ^c etc */
252 1.1 cgd if (c[GOLD]) { lprintf(".) %d gold pieces",(long)c[GOLD]); srcount++; }
253 1.1 cgd for (k=26; k>=0; k--)
254 1.1 cgd if (iven[k])
255 1.1 cgd { for (i=22; i<84; i++)
256 1.1 cgd for (j=0; j<=k; j++) if (i==iven[j]) show3(j); k=0; }
257 1.1 cgd
258 1.1 cgd lprintf("\nElapsed time is %d. You have %d mobuls left",(long)((gtime+99)/100+1),(long)((TIMELIMIT-gtime)/100));
259 1.1 cgd more(); nosignal=sigsav;
260 1.1 cgd }
261 1.1 cgd
262 1.1 cgd /*
263 1.1 cgd * subroutine to clear screen depending on # lines to display
264 1.1 cgd */
265 1.1 cgd t_setup(count)
266 1.1 cgd register int count;
267 1.1 cgd {
268 1.1 cgd if (count<20) /* how do we clear the screen? */
269 1.1 cgd {
270 1.1 cgd cl_up(79,count); cursor(1,1);
271 1.1 cgd }
272 1.1 cgd else
273 1.1 cgd {
274 1.1 cgd resetscroll(); clear();
275 1.1 cgd }
276 1.1 cgd }
277 1.1 cgd
278 1.1 cgd /*
279 1.1 cgd * subroutine to restore normal display screen depending on t_setup()
280 1.1 cgd */
281 1.1 cgd t_endup(count)
282 1.1 cgd register int count;
283 1.1 cgd {
284 1.1 cgd if (count<18) /* how did we clear the screen? */
285 1.1 cgd draws(0,MAXX,0,(count>MAXY) ? MAXY : count);
286 1.1 cgd else
287 1.1 cgd {
288 1.1 cgd drawscreen(); setscroll();
289 1.1 cgd }
290 1.1 cgd }
291 1.1 cgd
292 1.1 cgd /*
293 1.1 cgd function to show the things player is wearing only
294 1.1 cgd */
295 1.1 cgd showwear()
296 1.1 cgd {
297 1.1 cgd register int i,j,sigsav,count;
298 1.1 cgd sigsav=nosignal; nosignal=1; /* don't allow ^c etc */
299 1.1 cgd srcount=0;
300 1.1 cgd
301 1.1 cgd for (count=2,j=0; j<=26; j++) /* count number of items we will display */
302 1.1 cgd if (i=iven[j])
303 1.1 cgd switch(i)
304 1.1 cgd {
305 1.1 cgd case OLEATHER: case OPLATE: case OCHAIN:
306 1.1 cgd case ORING: case OSTUDLEATHER: case OSPLINT:
307 1.1 cgd case OPLATEARMOR: case OSSPLATE: case OSHIELD:
308 1.1 cgd count++;
309 1.1 cgd };
310 1.1 cgd
311 1.1 cgd t_setup(count);
312 1.1 cgd
313 1.1 cgd for (i=22; i<84; i++)
314 1.1 cgd for (j=0; j<=26; j++)
315 1.1 cgd if (i==iven[j])
316 1.1 cgd switch(i)
317 1.1 cgd {
318 1.1 cgd case OLEATHER: case OPLATE: case OCHAIN:
319 1.1 cgd case ORING: case OSTUDLEATHER: case OSPLINT:
320 1.1 cgd case OPLATEARMOR: case OSSPLATE: case OSHIELD:
321 1.1 cgd show3(j);
322 1.1 cgd };
323 1.1 cgd more(); nosignal=sigsav; t_endup(count);
324 1.1 cgd }
325 1.1 cgd
326 1.1 cgd /*
327 1.1 cgd function to show the things player can wield only
328 1.1 cgd */
329 1.1 cgd showwield()
330 1.1 cgd {
331 1.1 cgd register int i,j,sigsav,count;
332 1.1 cgd sigsav=nosignal; nosignal=1; /* don't allow ^c etc */
333 1.1 cgd srcount=0;
334 1.1 cgd
335 1.1 cgd for (count=2,j=0; j<=26; j++) /* count how many items */
336 1.1 cgd if (i=iven[j])
337 1.1 cgd switch(i)
338 1.1 cgd {
339 1.1 cgd case ODIAMOND: case ORUBY: case OEMERALD: case OSAPPHIRE:
340 1.1 cgd case OBOOK: case OCHEST: case OLARNEYE: case ONOTHEFT:
341 1.1 cgd case OSPIRITSCARAB: case OCUBEofUNDEAD:
342 1.1 cgd case OPOTION: case OSCROLL: break;
343 1.1 cgd default: count++;
344 1.1 cgd };
345 1.1 cgd
346 1.1 cgd t_setup(count);
347 1.1 cgd
348 1.1 cgd for (i=22; i<84; i++)
349 1.1 cgd for (j=0; j<=26; j++)
350 1.1 cgd if (i==iven[j])
351 1.1 cgd switch(i)
352 1.1 cgd {
353 1.1 cgd case ODIAMOND: case ORUBY: case OEMERALD: case OSAPPHIRE:
354 1.1 cgd case OBOOK: case OCHEST: case OLARNEYE: case ONOTHEFT:
355 1.1 cgd case OSPIRITSCARAB: case OCUBEofUNDEAD:
356 1.1 cgd case OPOTION: case OSCROLL: break;
357 1.1 cgd default: show3(j);
358 1.1 cgd };
359 1.1 cgd more(); nosignal=sigsav; t_endup(count);
360 1.1 cgd }
361 1.1 cgd
362 1.1 cgd /*
363 1.1 cgd * function to show the things player can read only
364 1.1 cgd */
365 1.1 cgd showread()
366 1.1 cgd {
367 1.1 cgd register int i,j,sigsav,count;
368 1.1 cgd sigsav=nosignal; nosignal=1; /* don't allow ^c etc */
369 1.1 cgd srcount=0;
370 1.1 cgd
371 1.1 cgd for (count=2,j=0; j<=26; j++)
372 1.1 cgd switch(iven[j])
373 1.1 cgd {
374 1.1 cgd case OBOOK: case OSCROLL: count++;
375 1.1 cgd };
376 1.1 cgd t_setup(count);
377 1.1 cgd
378 1.1 cgd for (i=22; i<84; i++)
379 1.1 cgd for (j=0; j<=26; j++)
380 1.1 cgd if (i==iven[j])
381 1.1 cgd switch(i)
382 1.1 cgd {
383 1.1 cgd case OBOOK: case OSCROLL: show3(j);
384 1.1 cgd };
385 1.1 cgd more(); nosignal=sigsav; t_endup(count);
386 1.1 cgd }
387 1.1 cgd
388 1.1 cgd /*
389 1.1 cgd * function to show the things player can eat only
390 1.1 cgd */
391 1.1 cgd showeat()
392 1.1 cgd {
393 1.1 cgd register int i,j,sigsav,count;
394 1.1 cgd sigsav=nosignal; nosignal=1; /* don't allow ^c etc */
395 1.1 cgd srcount=0;
396 1.1 cgd
397 1.1 cgd for (count=2,j=0; j<=26; j++)
398 1.1 cgd switch(iven[j])
399 1.1 cgd {
400 1.1 cgd case OCOOKIE: count++;
401 1.1 cgd };
402 1.1 cgd t_setup(count);
403 1.1 cgd
404 1.1 cgd for (i=22; i<84; i++)
405 1.1 cgd for (j=0; j<=26; j++)
406 1.1 cgd if (i==iven[j])
407 1.1 cgd switch(i)
408 1.1 cgd {
409 1.1 cgd case OCOOKIE: show3(j);
410 1.1 cgd };
411 1.1 cgd more(); nosignal=sigsav; t_endup(count);
412 1.1 cgd }
413 1.1 cgd
414 1.1 cgd /*
415 1.1 cgd function to show the things player can quaff only
416 1.1 cgd */
417 1.1 cgd showquaff()
418 1.1 cgd {
419 1.1 cgd register int i,j,sigsav,count;
420 1.1 cgd sigsav=nosignal; nosignal=1; /* don't allow ^c etc */
421 1.1 cgd srcount=0;
422 1.1 cgd
423 1.1 cgd for (count=2,j=0; j<=26; j++)
424 1.1 cgd switch(iven[j])
425 1.1 cgd {
426 1.1 cgd case OPOTION: count++;
427 1.1 cgd };
428 1.1 cgd t_setup(count);
429 1.1 cgd
430 1.1 cgd for (i=22; i<84; i++)
431 1.1 cgd for (j=0; j<=26; j++)
432 1.1 cgd if (i==iven[j])
433 1.1 cgd switch(i)
434 1.1 cgd {
435 1.1 cgd case OPOTION: show3(j);
436 1.1 cgd };
437 1.1 cgd more(); nosignal=sigsav; t_endup(count);
438 1.1 cgd }
439 1.1 cgd
440 1.1 cgd show1(idx,str2)
441 1.1 cgd register int idx;
442 1.1 cgd register char *str2[];
443 1.4 mycroft {
444 1.4 mycroft lprintf("\n%c) %s",idx+'a',objectname[iven[idx]]);
445 1.1 cgd if (str2!=0 && str2[ivenarg[idx]][0]!=0) lprintf(" of%s",str2[ivenarg[idx]]);
446 1.1 cgd }
447 1.1 cgd
448 1.1 cgd show3(index)
449 1.1 cgd register int index;
450 1.1 cgd {
451 1.1 cgd switch(iven[index])
452 1.1 cgd {
453 1.1 cgd case OPOTION: show1(index,potionname); break;
454 1.1 cgd case OSCROLL: show1(index,scrollname); break;
455 1.1 cgd
456 1.1 cgd case OLARNEYE: case OBOOK: case OSPIRITSCARAB:
457 1.1 cgd case ODIAMOND: case ORUBY: case OCUBEofUNDEAD:
458 1.1 cgd case OEMERALD: case OCHEST: case OCOOKIE:
459 1.1 cgd case OSAPPHIRE: case ONOTHEFT: show1(index,(char **)0); break;
460 1.1 cgd
461 1.1 cgd default: lprintf("\n%c) %s",index+'a',objectname[iven[index]]);
462 1.1 cgd if (ivenarg[index]>0) lprintf(" + %d",(long)ivenarg[index]);
463 1.1 cgd else if (ivenarg[index]<0) lprintf(" %d",(long)ivenarg[index]);
464 1.1 cgd break;
465 1.1 cgd }
466 1.1 cgd if (c[WIELD]==index) lprcat(" (weapon in hand)");
467 1.1 cgd if ((c[WEAR]==index) || (c[SHIELD]==index)) lprcat(" (being worn)");
468 1.1 cgd if (++srcount>=22) { srcount=0; more(); clear(); }
469 1.1 cgd }
470 1.1 cgd
471 1.1 cgd /*
472 1.1 cgd subroutine to randomly create monsters if needed
473 1.1 cgd */
474 1.1 cgd randmonst()
475 1.1 cgd {
476 1.1 cgd if (c[TIMESTOP]) return; /* don't make monsters if time is stopped */
477 1.1 cgd if (--rmst <= 0)
478 1.1 cgd {
479 1.1 cgd rmst = 120 - (level<<2); fillmonst(makemonst(level));
480 1.1 cgd }
481 1.1 cgd }
482 1.1 cgd
483 1.1 cgd
484 1.1 cgd /*
486 1.1 cgd parse()
487 1.1 cgd
488 1.1 cgd get and execute a command
489 1.1 cgd */
490 1.1 cgd parse()
491 1.1 cgd {
492 1.1 cgd register int i,j,k,flag;
493 1.1 cgd while (1)
494 1.1 cgd {
495 1.1 cgd k = yylex();
496 1.1 cgd switch(k) /* get the token from the input and switch on it */
497 1.1 cgd {
498 1.1 cgd case 'h': moveplayer(4); return; /* west */
499 1.1 cgd case 'H': run(4); return; /* west */
500 1.1 cgd case 'l': moveplayer(2); return; /* east */
501 1.1 cgd case 'L': run(2); return; /* east */
502 1.1 cgd case 'j': moveplayer(1); return; /* south */
503 1.1 cgd case 'J': run(1); return; /* south */
504 1.1 cgd case 'k': moveplayer(3); return; /* north */
505 1.1 cgd case 'K': run(3); return; /* north */
506 1.1 cgd case 'u': moveplayer(5); return; /* northeast */
507 1.1 cgd case 'U': run(5); return; /* northeast */
508 1.1 cgd case 'y': moveplayer(6); return; /* northwest */
509 1.1 cgd case 'Y': run(6); return; /* northwest */
510 1.1 cgd case 'n': moveplayer(7); return; /* southeast */
511 1.1 cgd case 'N': run(7); return; /* southeast */
512 1.1 cgd case 'b': moveplayer(8); return; /* southwest */
513 1.1 cgd case 'B': run(8); return; /* southwest */
514 1.1 cgd
515 1.1 cgd case '.': if (yrepcount) viewflag=1; return; /* stay here */
516 1.1 cgd
517 1.1 cgd case 'w': yrepcount=0; wield(); return; /* wield a weapon */
518 1.1 cgd
519 1.1 cgd case 'W': yrepcount=0; wear(); return; /* wear armor */
520 1.1 cgd
521 1.1 cgd case 'r': yrepcount=0;
522 1.1 cgd if (c[BLINDCOUNT]) { cursors(); lprcat("\nYou can't read anything when you're blind!"); } else
523 1.1 cgd if (c[TIMESTOP]==0) readscr(); return; /* to read a scroll */
524 1.1 cgd
525 1.1 cgd case 'q': yrepcount=0; if (c[TIMESTOP]==0) quaff(); return; /* quaff a potion */
526 1.1 cgd
527 1.1 cgd case 'd': yrepcount=0; if (c[TIMESTOP]==0) dropobj(); return; /* to drop an object */
528 1.1 cgd
529 1.1 cgd case 'c': yrepcount=0; cast(); return; /* cast a spell */
530 1.1 cgd
531 1.1 cgd case 'i': yrepcount=0; nomove=1; showstr(); return; /* status */
532 1.1 cgd
533 1.1 cgd case 'e': yrepcount=0;
534 1.1 cgd if (c[TIMESTOP]==0) eatcookie(); return; /* to eat a fortune cookie */
535 1.1 cgd
536 1.1 cgd case 'D': yrepcount=0; seemagic(0); nomove=1; return; /* list spells and scrolls */
537 1.1 cgd
538 1.1 cgd case '?': yrepcount=0; help(); nomove=1; return; /* give the help screen*/
539 1.1 cgd
540 1.1 cgd case 'S': clear(); lprcat("Saving . . ."); lflush();
541 1.1 cgd savegame(savefilename); wizard=1; died(-257); /* save the game - doesn't return */
542 1.1 cgd
543 1.1 cgd case 'Z': yrepcount=0; if (c[LEVEL]>9) { oteleport(1); return; }
544 1.1 cgd cursors(); lprcat("\nAs yet, you don't have enough experience to use teleportation");
545 1.1 cgd return; /* teleport yourself */
546 1.1 cgd
547 1.1 cgd case '^': /* identify traps */ flag=yrepcount=0; cursors();
548 1.1 cgd lprc('\n'); for (j=playery-1; j<playery+2; j++)
549 1.1 cgd {
550 1.1 cgd if (j < 0) j=0; if (j >= MAXY) break;
551 1.1 cgd for (i=playerx-1; i<playerx+2; i++)
552 1.1 cgd {
553 1.1 cgd if (i < 0) i=0; if (i >= MAXX) break;
554 1.1 cgd switch(item[i][j])
555 1.1 cgd {
556 1.1 cgd case OTRAPDOOR: case ODARTRAP:
557 1.1 cgd case OTRAPARROW: case OTELEPORTER:
558 1.1 cgd lprcat("\nIts "); lprcat(objectname[item[i][j]]); flag++;
559 1.1 cgd };
560 1.1 cgd }
561 1.1 cgd }
562 1.1 cgd if (flag==0) lprcat("\nNo traps are visible");
563 1.1 cgd return;
564 1.1 cgd
565 1.1 cgd #if WIZID
566 1.1 cgd case '_': /* this is the fudge player password for wizard mode*/
567 1.1 cgd yrepcount=0; cursors(); nomove=1;
568 1.1 cgd if (userid!=wisid)
569 1.1 cgd {
570 1.1 cgd lprcat("Sorry, you are not empowered to be a wizard.\n");
571 1.1 cgd scbr(); /* system("stty -echo cbreak"); */
572 1.1 cgd lflush(); return;
573 1.1 cgd }
574 1.1 cgd if (getpassword()==0)
575 1.1 cgd {
576 1.1 cgd scbr(); /* system("stty -echo cbreak"); */ return;
577 1.1 cgd }
578 1.1 cgd wizard=1; scbr(); /* system("stty -echo cbreak"); */
579 1.1 cgd for (i=0; i<6; i++) c[i]=70; iven[0]=iven[1]=0;
580 1.1 cgd take(OPROTRING,50); take(OLANCE,25); c[WIELD]=1;
581 1.1 cgd c[LANCEDEATH]=1; c[WEAR] = c[SHIELD] = -1;
582 1.1 cgd raiseexperience(6000000L); c[AWARENESS] += 25000;
583 1.1 cgd {
584 1.1 cgd register int i,j;
585 1.1 cgd for (i=0; i<MAXY; i++)
586 1.2 mycroft for (j=0; j<MAXX; j++) know[j][i]=1;
587 1.2 mycroft for (i=0; i<SPNUM; i++) spelknow[i]=1;
588 1.1 cgd for (i=0; i<MAXSCROLL; i++) scrollname[i]=scrollhide[i];
589 1.1 cgd for (i=0; i<MAXPOTION; i++) potionname[i]=potionhide[i];
590 1.1 cgd }
591 1.1 cgd for (i=0; i<MAXSCROLL; i++)
592 1.1 cgd if (strlen(scrollname[i])>2) /* no null items */
593 1.1 cgd { item[i][0]=OSCROLL; iarg[i][0]=i; }
594 1.1 cgd for (i=MAXX-1; i>MAXX-1-MAXPOTION; i--)
595 1.1 cgd if (strlen(potionname[i-MAXX+MAXPOTION])>2) /* no null items */
596 1.1 cgd { item[i][0]=OPOTION; iarg[i][0]=i-MAXX+MAXPOTION; }
597 1.1 cgd for (i=1; i<MAXY; i++)
598 1.1 cgd { item[0][i]=i; iarg[0][i]=0; }
599 1.1 cgd for (i=MAXY; i<MAXY+MAXX; i++)
600 1.1 cgd { item[i-MAXY][MAXY-1]=i; iarg[i-MAXY][MAXY-1]=0; }
601 1.1 cgd for (i=MAXX+MAXY; i<MAXX+MAXY+MAXY; i++)
602 1.1 cgd { item[MAXX-1][i-MAXX-MAXY]=i; iarg[MAXX-1][i-MAXX-MAXY]=0; }
603 1.1 cgd c[GOLD]+=25000; drawscreen(); return;
604 1.1 cgd #endif
605 1.1 cgd
606 1.1 cgd case 'T': yrepcount=0; cursors(); if (c[SHIELD] != -1) { c[SHIELD] = -1; lprcat("\nYour shield is off"); bottomline(); } else
607 1.1 cgd if (c[WEAR] != -1) { c[WEAR] = -1; lprcat("\nYour armor is off"); bottomline(); }
608 1.1 cgd else lprcat("\nYou aren't wearing anything");
609 1.1 cgd return;
610 1.1 cgd
611 1.1 cgd case 'g': cursors();
612 1.1 cgd lprintf("\nThe stuff you are carrying presently weighs %d pounds",(long)packweight());
613 1.1 cgd case ' ': yrepcount=0; nomove=1; return;
614 1.1 cgd
615 1.1 cgd case 'v': yrepcount=0; cursors();
616 1.1 cgd lprintf("\nCaverns of Larn, Version %d.%d, Diff=%d",(long)VERSION,(long)SUBVERSION,(long)c[HARDGAME]);
617 1.1 cgd if (wizard) lprcat(" Wizard"); nomove=1;
618 1.1 cgd if (cheat) lprcat(" Cheater");
619 1.1 cgd lprcat(copyright);
620 1.1 cgd return;
621 1.1 cgd
622 1.1 cgd case 'Q': yrepcount=0; quit(); nomove=1; return; /* quit */
623 1.1 cgd
624 1.1 cgd case 'L'-64: yrepcount=0; drawscreen(); nomove=1; return; /* look */
625 1.1 cgd
626 1.1 cgd #if WIZID
627 1.1 cgd #ifdef EXTRA
628 1.1 cgd case 'A': yrepcount=0; nomove=1; if (wizard) { diag(); return; } /* create diagnostic file */
629 1.1 cgd return;
630 1.1 cgd #endif
631 1.1 cgd #endif
632 1.1 cgd case 'P': cursors();
633 1.1 cgd if (outstanding_taxes>0)
634 1.1 cgd lprintf("\nYou presently owe %d gp in taxes.",(long)outstanding_taxes);
635 1.1 cgd else
636 1.1 cgd lprcat("\nYou do not owe any taxes.");
637 1.1 cgd return;
638 1.1 cgd };
639 1.1 cgd }
640 1.1 cgd }
641 1.1 cgd
642 1.1 cgd parse2()
643 1.1 cgd {
644 1.1 cgd if (c[HASTEMONST]) movemonst(); movemonst(); /* move the monsters */
645 1.1 cgd randmonst(); regen();
646 1.1 cgd }
647 1.1 cgd
648 1.1 cgd run(dir)
649 1.1 cgd int dir;
650 1.1 cgd {
651 1.1 cgd register int i;
652 1.1 cgd i=1; while (i)
653 1.1 cgd {
654 1.1 cgd i=moveplayer(dir);
655 1.1 cgd if (i>0) { if (c[HASTEMONST]) movemonst(); movemonst(); randmonst(); regen(); }
656 1.1 cgd if (hitflag) i=0;
657 1.1 cgd if (i!=0) showcell(playerx,playery);
658 1.1 cgd }
659 1.1 cgd }
660 1.1 cgd
661 1.1 cgd /*
662 1.1 cgd function to wield a weapon
663 1.1 cgd */
664 1.1 cgd wield()
665 1.1 cgd {
666 1.1 cgd register int i;
667 1.1 cgd while (1)
668 1.1 cgd {
669 1.1 cgd if ((i = whatitem("wield"))=='\33') return;
670 1.1 cgd if (i != '.')
671 1.1 cgd {
672 1.1 cgd if (i=='*') showwield();
673 1.1 cgd else if (iven[i-'a']==0) { ydhi(i); return; }
674 1.1 cgd else if (iven[i-'a']==OPOTION) { ycwi(i); return; }
675 1.1 cgd else if (iven[i-'a']==OSCROLL) { ycwi(i); return; }
676 1.1 cgd else if ((c[SHIELD]!= -1) && (iven[i-'a']==O2SWORD)) { lprcat("\nBut one arm is busy with your shield!"); return; }
677 1.1 cgd else { c[WIELD]=i-'a'; if (iven[i-'a'] == OLANCE) c[LANCEDEATH]=1; else c[LANCEDEATH]=0; bottomline(); return; }
678 1.1 cgd }
679 1.1 cgd }
680 1.1 cgd }
681 1.1 cgd
682 1.1 cgd /*
683 1.1 cgd common routine to say you don't have an item
684 1.1 cgd */
685 1.1 cgd ydhi(x)
686 1.1 cgd int x;
687 1.1 cgd { cursors(); lprintf("\nYou don't have item %c!",x); }
688 1.1 cgd ycwi(x)
689 1.1 cgd int x;
690 1.1 cgd { cursors(); lprintf("\nYou can't wield item %c!",x); }
691 1.1 cgd
692 1.1 cgd /*
693 1.1 cgd function to wear armor
694 1.1 cgd */
695 1.1 cgd wear()
696 1.1 cgd {
697 1.1 cgd register int i;
698 1.1 cgd while (1)
699 1.1 cgd {
700 1.1 cgd if ((i = whatitem("wear"))=='\33') return;
701 1.1 cgd if (i != '.')
702 1.1 cgd {
703 1.1 cgd if (i=='*') showwear(); else
704 1.1 cgd switch(iven[i-'a'])
705 1.1 cgd {
706 1.1 cgd case 0: ydhi(i); return;
707 1.1 cgd case OLEATHER: case OCHAIN: case OPLATE: case OSTUDLEATHER:
708 1.1 cgd case ORING: case OSPLINT: case OPLATEARMOR: case OSSPLATE:
709 1.1 cgd if (c[WEAR] != -1) { lprcat("\nYou're already wearing some armor"); return; }
710 1.1 cgd c[WEAR]=i-'a'; bottomline(); return;
711 1.1 cgd case OSHIELD: if (c[SHIELD] != -1) { lprcat("\nYou are already wearing a shield"); return; }
712 1.1 cgd if (iven[c[WIELD]]==O2SWORD) { lprcat("\nYour hands are busy with the two handed sword!"); return; }
713 1.1 cgd c[SHIELD] = i-'a'; bottomline(); return;
714 1.1 cgd default: lprcat("\nYou can't wear that!");
715 1.1 cgd };
716 1.1 cgd }
717 1.1 cgd }
718 1.1 cgd }
719 1.1 cgd
720 1.1 cgd /*
721 1.1 cgd function to drop an object
722 1.1 cgd */
723 1.1 cgd dropobj()
724 1.1 cgd {
725 1.1 cgd register int i;
726 1.1 cgd register char *p;
727 1.1 cgd long amt;
728 1.1 cgd p = &item[playerx][playery];
729 1.1 cgd while (1)
730 1.1 cgd {
731 1.1 cgd if ((i = whatitem("drop"))=='\33') return;
732 1.1 cgd if (i=='*') showstr(); else
733 1.1 cgd {
734 1.1 cgd if (i=='.') /* drop some gold */
735 1.1 cgd {
736 1.1 cgd if (*p) { lprcat("\nThere's something here already!"); return; }
737 1.1 cgd lprcat("\n\n");
738 1.1 cgd cl_dn(1,23);
739 1.1 cgd lprcat("How much gold do you drop? ");
740 1.1 cgd if ((amt=readnum((long)c[GOLD])) == 0) return;
741 1.1 cgd if (amt>c[GOLD])
742 1.1 cgd { lprcat("\nYou don't have that much!"); return; }
743 1.1 cgd if (amt<=32767)
744 1.1 cgd { *p=OGOLDPILE; i=amt; }
745 1.1 cgd else if (amt<=327670L)
746 1.1 cgd { *p=ODGOLD; i=amt/10; amt = 10*i; }
747 1.1 cgd else if (amt<=3276700L)
748 1.1 cgd { *p=OMAXGOLD; i=amt/100; amt = 100*i; }
749 1.1 cgd else if (amt<=32767000L)
750 1.1 cgd { *p=OKGOLD; i=amt/1000; amt = 1000*i; }
751 1.1 cgd else
752 1.1 cgd { *p=OKGOLD; i=32767; amt = 32767000L; }
753 1.1 cgd c[GOLD] -= amt;
754 1.1 cgd lprintf("You drop %d gold pieces",(long)amt);
755 1.1 cgd iarg[playerx][playery]=i; bottomgold();
756 1.1 cgd know[playerx][playery]=0; dropflag=1; return;
757 1.1 cgd }
758 1.1 cgd drop_object(i-'a');
759 1.1 cgd return;
760 1.1 cgd }
761 1.1 cgd }
762 1.1 cgd }
763 1.1 cgd
764 1.1 cgd /*
765 1.1 cgd * readscr() Subroutine to read a scroll one is carrying
766 1.1 cgd */
767 1.1 cgd readscr()
768 1.1 cgd {
769 1.1 cgd register int i;
770 1.1 cgd while (1)
771 1.1 cgd {
772 1.1 cgd if ((i = whatitem("read"))=='\33') return;
773 1.1 cgd if (i != '.')
774 1.1 cgd {
775 1.1 cgd if (i=='*') showread(); else
776 1.1 cgd {
777 1.1 cgd if (iven[i-'a']==OSCROLL) { read_scroll(ivenarg[i-'a']); iven[i-'a']=0; return; }
778 1.1 cgd if (iven[i-'a']==OBOOK) { readbook(ivenarg[i-'a']); iven[i-'a']=0; return; }
779 1.1 cgd if (iven[i-'a']==0) { ydhi(i); return; }
780 1.1 cgd lprcat("\nThere's nothing on it to read"); return;
781 1.1 cgd }
782 1.1 cgd }
783 1.1 cgd }
784 1.1 cgd }
785 1.1 cgd
786 1.1 cgd /*
787 1.1 cgd * subroutine to eat a cookie one is carrying
788 1.1 cgd */
789 1.1 cgd eatcookie()
790 1.1 cgd {
791 1.1 cgd register int i;
792 1.1 cgd char *p;
793 1.1 cgd while (1)
794 1.1 cgd {
795 1.1 cgd if ((i = whatitem("eat"))=='\33') return;
796 1.1 cgd if (i != '.')
797 1.1 cgd if (i=='*') showeat(); else
798 1.1 cgd {
799 1.1 cgd if (iven[i-'a']==OCOOKIE)
800 1.1 cgd {
801 1.1 cgd lprcat("\nThe cookie was delicious.");
802 1.1 cgd iven[i-'a']=0;
803 1.6 cgd if (!c[BLINDCOUNT])
804 1.1 cgd {
805 1.1 cgd if (p=fortune())
806 1.1 cgd {
807 1.1 cgd lprcat(" Inside you find a scrap of paper that says:\n");
808 1.1 cgd lprcat(p);
809 1.1 cgd }
810 1.1 cgd }
811 1.1 cgd return;
812 1.1 cgd }
813 1.1 cgd if (iven[i-'a']==0) { ydhi(i); return; }
814 1.1 cgd lprcat("\nYou can't eat that!"); return;
815 1.1 cgd }
816 1.1 cgd }
817 1.1 cgd }
818 1.1 cgd
819 1.1 cgd /*
820 1.1 cgd * subroutine to quaff a potion one is carrying
821 1.1 cgd */
822 1.1 cgd quaff()
823 1.1 cgd {
824 1.1 cgd register int i;
825 1.1 cgd while (1)
826 1.1 cgd {
827 1.1 cgd if ((i = whatitem("quaff"))=='\33') return;
828 1.1 cgd if (i != '.')
829 1.1 cgd {
830 1.1 cgd if (i=='*') showquaff(); else
831 1.1 cgd {
832 1.1 cgd if (iven[i-'a']==OPOTION) { quaffpotion(ivenarg[i-'a']); iven[i-'a']=0; return; }
833 1.1 cgd if (iven[i-'a']==0) { ydhi(i); return; }
834 1.1 cgd lprcat("\nYou wouldn't want to quaff that, would you? "); return;
835 1.1 cgd }
836 1.1 cgd }
837 1.1 cgd }
838 1.1 cgd }
839 1.1 cgd
840 1.1 cgd /*
841 1.1 cgd function to ask what player wants to do
842 1.1 cgd */
843 1.1 cgd whatitem(str)
844 1.1 cgd char *str;
845 1.1 cgd {
846 1.1 cgd int i;
847 1.1 cgd cursors(); lprintf("\nWhat do you want to %s [* for all] ? ",str);
848 1.1 cgd i=0; while (i>'z' || (i<'a' && i!='*' && i!='\33' && i!='.')) i=getchar();
849 1.1 cgd if (i=='\33') lprcat(" aborted");
850 1.1 cgd return(i);
851 1.1 cgd }
852 1.1 cgd
853 1.1 cgd /*
854 1.1 cgd subroutine to get a number from the player
855 1.1 cgd and allow * to mean return amt, else return the number entered
856 1.1 cgd */
857 1.1 cgd unsigned long readnum(mx)
858 1.1 cgd long mx;
859 1.1 cgd {
860 1.1 cgd register int i;
861 1.1 cgd register unsigned long amt=0;
862 1.1 cgd sncbr();
863 1.1 cgd if ((i=getchar()) == '*') amt = mx; /* allow him to say * for all gold */
864 1.1 cgd else
865 1.1 cgd while (i != '\n')
866 1.1 cgd {
867 1.1 cgd if (i=='\033') { scbr(); lprcat(" aborted"); return(0); }
868 1.1 cgd if ((i <= '9') && (i >= '0') && (amt<99999999))
869 1.1 cgd amt = amt*10+i-'0';
870 1.1 cgd i = getchar();
871 1.1 cgd }
872 1.1 cgd scbr(); return(amt);
873 1.1 cgd }
874 1.1 cgd
875 1.1 cgd #ifdef HIDEBYLINK
876 1.1 cgd /*
877 1.1 cgd * routine to zero every byte in a string
878 1.1 cgd */
879 1.1 cgd szero(str)
880 1.1 cgd register char *str;
881 1.1 cgd {
882 1.1 cgd while (*str)
883 1.1 cgd *str++ = 0;
884 }
885 #endif HIDEBYLINK
886