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