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