]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - larn/global.c
Move the playerids file to /var/games, as suggest by Rafal Boni.
[bsdgames-darwin.git] / larn / global.c
1 #ifndef lint
2 static char rcsid[] = "$Id: global.c,v 1.2 1993/08/02 17:20:02 mycroft Exp $";
3 #endif /* not lint */
4
5 /* global.c Larn is copyrighted 1986 by Noah Morgan.
6 *
7 * raiselevel() subroutine to raise the player one level
8 * loselevel() subroutine to lower the player by one level
9 * raiseexperience(x) subroutine to increase experience points
10 * loseexperience(x) subroutine to lose experience points
11 * losehp(x) subroutine to remove hit points from the player
12 * losemhp(x) subroutine to remove max # hit points from the player
13 * raisehp(x) subroutine to gain hit points
14 * raisemhp(x) subroutine to gain maximum hit points
15 * losespells(x) subroutine to lose spells
16 * losemspells(x) subroutine to lose maximum spells
17 * raisespells(x) subroutine to gain spells
18 * raisemspells(x) subroutine to gain maximum spells
19 * recalc() function to recalculate the armor class of the player
20 * makemonst(lev) function to return monster number for a randomly selected monster
21 * positionplayer() function to be sure player is not in a wall
22 * quit() subroutine to ask if the player really wants to quit
23 */
24
25 #include "header.h"
26 extern int score[],srcount,dropflag;
27 extern int random;/* the random number seed */
28 extern short playerx,playery,lastnum;
29 extern char cheat,level,monstnamelist[];
30 extern char lastmonst[],*what[],*who[];
31 extern char winner[];
32 extern char logname[],monstlevel[];
33 extern char sciv[SCORESIZE+1][26][2],*potionname[],*scrollname[];
34 /*
35 ***********
36 RAISE LEVEL
37 ***********
38 raiselevel()
39
40 subroutine to raise the player one level
41 uses the skill[] array to find level boundarys
42 uses c[EXPERIENCE] c[LEVEL]
43 */
44 raiselevel()
45 {
46 if (c[LEVEL] < MAXPLEVEL) raiseexperience((long)(skill[c[LEVEL]]-c[EXPERIENCE]));
47 }
48
49 /*
50 ***********
51 LOOSE LEVEL
52 ***********
53 loselevel()
54
55 subroutine to lower the players character level by one
56 */
57 loselevel()
58 {
59 if (c[LEVEL] > 1) loseexperience((long)(c[EXPERIENCE] - skill[c[LEVEL]-1] + 1));
60 }
61
62 /*
63 ****************
64 RAISE EXPERIENCE
65 ****************
66 raiseexperience(x)
67
68 subroutine to increase experience points
69 */
70 raiseexperience(x)
71 register long x;
72 {
73 register int i,tmp;
74 i=c[LEVEL]; c[EXPERIENCE]+=x;
75 while (c[EXPERIENCE] >= skill[c[LEVEL]] && (c[LEVEL] < MAXPLEVEL))
76 {
77 tmp = (c[CONSTITUTION]-c[HARDGAME])>>1;
78 c[LEVEL]++; raisemhp((int)(rnd(3)+rnd((tmp>0)?tmp:1)));
79 raisemspells((int)rund(3));
80 if (c[LEVEL] < 7-c[HARDGAME]) raisemhp((int)(c[CONSTITUTION]>>2));
81 }
82 if (c[LEVEL] != i)
83 {
84 cursors();
85 beep(); lprintf("\nWelcome to level %d",(long)c[LEVEL]); /* if we changed levels */
86 }
87 bottomline();
88 }
89
90 /*
91 ****************
92 LOOSE EXPERIENCE
93 ****************
94 loseexperience(x)
95
96 subroutine to lose experience points
97 */
98 loseexperience(x)
99 register long x;
100 {
101 register int i,tmp;
102 i=c[LEVEL]; c[EXPERIENCE]-=x;
103 if (c[EXPERIENCE] < 0) c[EXPERIENCE]=0;
104 while (c[EXPERIENCE] < skill[c[LEVEL]-1])
105 {
106 if (--c[LEVEL] <= 1) c[LEVEL]=1; /* down one level */
107 tmp = (c[CONSTITUTION]-c[HARDGAME])>>1; /* lose hpoints */
108 losemhp((int)rnd((tmp>0)?tmp:1)); /* lose hpoints */
109 if (c[LEVEL] < 7-c[HARDGAME]) losemhp((int)(c[CONSTITUTION]>>2));
110 losemspells((int)rund(3)); /* lose spells */
111 }
112 if (i!=c[LEVEL])
113 {
114 cursors();
115 beep(); lprintf("\nYou went down to level %d!",(long)c[LEVEL]);
116 }
117 bottomline();
118 }
119
120 /*
121 ********
122 LOOSE HP
123 ********
124 losehp(x)
125 losemhp(x)
126
127 subroutine to remove hit points from the player
128 warning -- will kill player if hp goes to zero
129 */
130 losehp(x)
131 register int x;
132 {
133 if ((c[HP] -= x) <= 0)
134 {
135 beep(); lprcat("\n"); nap(3000); died(lastnum);
136 }
137 }
138
139 losemhp(x)
140 register int x;
141 {
142 c[HP] -= x; if (c[HP] < 1) c[HP]=1;
143 c[HPMAX] -= x; if (c[HPMAX] < 1) c[HPMAX]=1;
144 }
145
146 /*
147 ********
148 RAISE HP
149 ********
150 raisehp(x)
151 raisemhp(x)
152
153 subroutine to gain maximum hit points
154 */
155 raisehp(x)
156 register int x;
157 {
158 if ((c[HP] += x) > c[HPMAX]) c[HP] = c[HPMAX];
159 }
160
161 raisemhp(x)
162 register int x;
163 {
164 c[HPMAX] += x; c[HP] += x;
165 }
166
167 /*
168 ************
169 RAISE SPELLS
170 ************
171 raisespells(x)
172 raisemspells(x)
173
174 subroutine to gain maximum spells
175 */
176 raisespells(x)
177 register int x;
178 {
179 if ((c[SPELLS] += x) > c[SPELLMAX]) c[SPELLS] = c[SPELLMAX];
180 }
181
182 raisemspells(x)
183 register int x;
184 {
185 c[SPELLMAX]+=x; c[SPELLS]+=x;
186 }
187
188 /*
189 ************
190 LOOSE SPELLS
191 ************
192 losespells(x)
193 losemspells(x)
194
195 subroutine to lose maximum spells
196 */
197 losespells(x)
198 register int x;
199 {
200 if ((c[SPELLS] -= x) < 0) c[SPELLS]=0;
201 }
202
203 losemspells(x)
204 register int x;
205 {
206 if ((c[SPELLMAX] -= x) < 0) c[SPELLMAX]=0;
207 if ((c[SPELLS] -= x) < 0) c[SPELLS]=0;
208 }
209
210 /*
211 makemonst(lev)
212 int lev;
213
214 function to return monster number for a randomly selected monster
215 for the given cave level
216 */
217 makemonst(lev)
218 register int lev;
219 {
220 register int tmp,x;
221 if (lev < 1) lev = 1; if (lev > 12) lev = 12;
222 tmp=WATERLORD;
223 if (lev < 5)
224 while (tmp==WATERLORD) tmp=rnd((x=monstlevel[lev-1])?x:1);
225 else while (tmp==WATERLORD)
226 tmp=rnd((x=monstlevel[lev-1]-monstlevel[lev-4])?x:1)+monstlevel[lev-4];
227
228 while (monster[tmp].genocided && tmp<MAXMONST) tmp++; /* genocided? */
229 return(tmp);
230 }
231
232 /*
233 positionplayer()
234
235 function to be sure player is not in a wall
236 */
237 positionplayer()
238 {
239 int try;
240 try = 2;
241 while ((item[playerx][playery] || mitem[playerx][playery]) && (try))
242 if (++playerx >= MAXX-1)
243 {
244 playerx = 1;
245 if (++playery >= MAXY-1)
246 { playery = 1; --try; }
247 }
248 if (try==0) lprcat("Failure in positionplayer\n");
249 }
250
251 /*
252 recalc() function to recalculate the armor class of the player
253 */
254 recalc()
255 {
256 register int i,j,k;
257 c[AC] = c[MOREDEFENSES];
258 if (c[WEAR] >= 0)
259 switch(iven[c[WEAR]])
260 {
261 case OSHIELD: c[AC] += 2 + ivenarg[c[WEAR]]; break;
262 case OLEATHER: c[AC] += 2 + ivenarg[c[WEAR]]; break;
263 case OSTUDLEATHER: c[AC] += 3 + ivenarg[c[WEAR]]; break;
264 case ORING: c[AC] += 5 + ivenarg[c[WEAR]]; break;
265 case OCHAIN: c[AC] += 6 + ivenarg[c[WEAR]]; break;
266 case OSPLINT: c[AC] += 7 + ivenarg[c[WEAR]]; break;
267 case OPLATE: c[AC] += 9 + ivenarg[c[WEAR]]; break;
268 case OPLATEARMOR: c[AC] += 10 + ivenarg[c[WEAR]]; break;
269 case OSSPLATE: c[AC] += 12 + ivenarg[c[WEAR]]; break;
270 }
271
272 if (c[SHIELD] >= 0) if (iven[c[SHIELD]] == OSHIELD) c[AC] += 2 + ivenarg[c[SHIELD]];
273 if (c[WIELD] < 0) c[WCLASS] = 0; else
274 {
275 i = ivenarg[c[WIELD]];
276 switch(iven[c[WIELD]])
277 {
278 case ODAGGER: c[WCLASS] = 3 + i; break;
279 case OBELT: c[WCLASS] = 7 + i; break;
280 case OSHIELD: c[WCLASS] = 8 + i; break;
281 case OSPEAR: c[WCLASS] = 10 + i; break;
282 case OFLAIL: c[WCLASS] = 14 + i; break;
283 case OBATTLEAXE: c[WCLASS] = 17 + i; break;
284 case OLANCE: c[WCLASS] = 19 + i; break;
285 case OLONGSWORD: c[WCLASS] = 22 + i; break;
286 case O2SWORD: c[WCLASS] = 26 + i; break;
287 case OSWORD: c[WCLASS] = 32 + i; break;
288 case OSWORDofSLASHING: c[WCLASS] = 30 + i; break;
289 case OHAMMER: c[WCLASS] = 35 + i; break;
290 default: c[WCLASS] = 0;
291 }
292 }
293 c[WCLASS] += c[MOREDAM];
294
295 /* now for regeneration abilities based on rings */
296 c[REGEN]=1; c[ENERGY]=0;
297 j=0; for (k=25; k>0; k--) if (iven[k]) {j=k; k=0; }
298 for (i=0; i<=j; i++)
299 {
300 switch(iven[i])
301 {
302 case OPROTRING: c[AC] += ivenarg[i] + 1; break;
303 case ODAMRING: c[WCLASS] += ivenarg[i] + 1; break;
304 case OBELT: c[WCLASS] += ((ivenarg[i]<<1)) + 2; break;
305
306 case OREGENRING: c[REGEN] += ivenarg[i] + 1; break;
307 case ORINGOFEXTRA: c[REGEN] += 5 * (ivenarg[i]+1); break;
308 case OENERGYRING: c[ENERGY] += ivenarg[i] + 1; break;
309 }
310 }
311 }
312
313
314 /*
315 quit()
316
317 subroutine to ask if the player really wants to quit
318 */
319 quit()
320 {
321 register int i;
322 cursors(); strcpy(lastmonst,"");
323 lprcat("\n\nDo you really want to quit?");
324 while (1)
325 {
326 i=getchar();
327 if (i == 'y') { died(300); return; }
328 if ((i == 'n') || (i == '\33')) { lprcat(" no"); lflush(); return; }
329 lprcat("\n"); setbold(); lprcat("Yes"); resetbold(); lprcat(" or ");
330 setbold(); lprcat("No"); resetbold(); lprcat(" please? Do you want to quit? ");
331 }
332 }
333
334 /*
335 function to ask --more-- then the user must enter a space
336 */
337 more()
338 {
339 lprcat("\n --- press "); standout("space"); lprcat(" to continue --- ");
340 while (getchar() != ' ');
341 }
342
343 /*
344 function to put something in the players inventory
345 returns 0 if success, 1 if a failure
346 */
347 take(itm,arg)
348 int itm,arg;
349 {
350 register int i,limit;
351 /* cursors(); */
352 if ((limit = 15+(c[LEVEL]>>1)) > 26) limit=26;
353 for (i=0; i<limit; i++)
354 if (iven[i]==0)
355 {
356 iven[i] = itm; ivenarg[i] = arg; limit=0;
357 switch(itm)
358 {
359 case OPROTRING: case ODAMRING: case OBELT: limit=1; break;
360 case ODEXRING: c[DEXTERITY] += ivenarg[i]+1; limit=1; break;
361 case OSTRRING: c[STREXTRA] += ivenarg[i]+1; limit=1; break;
362 case OCLEVERRING: c[INTELLIGENCE] += ivenarg[i]+1; limit=1; break;
363 case OHAMMER: c[DEXTERITY] += 10; c[STREXTRA]+=10;
364 c[INTELLIGENCE]-=10; limit=1; break;
365
366 case OORBOFDRAGON: c[SLAYING]++; break;
367 case OSPIRITSCARAB: c[NEGATESPIRIT]++; break;
368 case OCUBEofUNDEAD: c[CUBEofUNDEAD]++; break;
369 case ONOTHEFT: c[NOTHEFT]++; break;
370 case OSWORDofSLASHING: c[DEXTERITY] +=5; limit=1; break;
371 };
372 lprcat("\nYou pick up:"); srcount=0; show3(i);
373 if (limit) bottomline(); return(0);
374 }
375 lprcat("\nYou can't carry anything else"); return(1);
376 }
377
378 /*
379 subroutine to drop an object returns 1 if something there already else 0
380 */
381 drop_object(k)
382 int k;
383 {
384 int itm;
385 if ((k<0) || (k>25)) return(0);
386 itm = iven[k]; cursors();
387 if (itm==0) { lprintf("\nYou don't have item %c! ",k+'a'); return(1); }
388 if (item[playerx][playery])
389 { beep(); lprcat("\nThere's something here already"); return(1); }
390 if (playery==MAXY-1 && playerx==33) return(1); /* not in entrance */
391 item[playerx][playery] = itm;
392 iarg[playerx][playery] = ivenarg[k];
393 srcount=0; lprcat("\n You drop:"); show3(k); /* show what item you dropped*/
394 know[playerx][playery] = 0; iven[k]=0;
395 if (c[WIELD]==k) c[WIELD]= -1; if (c[WEAR]==k) c[WEAR] = -1;
396 if (c[SHIELD]==k) c[SHIELD]= -1;
397 adjustcvalues(itm,ivenarg[k]);
398 dropflag=1; /* say dropped an item so wont ask to pick it up right away */
399 return(0);
400 }
401
402 /*
403 function to enchant armor player is currently wearing
404 */
405 enchantarmor()
406 {
407 register int tmp;
408 if (c[WEAR]<0) { if (c[SHIELD] < 0)
409 { cursors(); beep(); lprcat("\nYou feel a sense of loss"); return; }
410 else { tmp=iven[c[SHIELD]]; if (tmp != OSCROLL) if (tmp != OPOTION) { ivenarg[c[SHIELD]]++; bottomline(); } } }
411 tmp = iven[c[WEAR]];
412 if (tmp!=OSCROLL) if (tmp!=OPOTION) { ivenarg[c[WEAR]]++; bottomline(); }
413 }
414
415 /*
416 function to enchant a weapon presently being wielded
417 */
418 enchweapon()
419 {
420 register int tmp;
421 if (c[WIELD]<0)
422 { cursors(); beep(); lprcat("\nYou feel a sense of loss"); return; }
423 tmp = iven[c[WIELD]];
424 if (tmp!=OSCROLL) if (tmp!=OPOTION)
425 { ivenarg[c[WIELD]]++;
426 if (tmp==OCLEVERRING) c[INTELLIGENCE]++; else
427 if (tmp==OSTRRING) c[STREXTRA]++; else
428 if (tmp==ODEXRING) c[DEXTERITY]++; bottomline(); }
429 }
430
431 /*
432 routine to tell if player can carry one more thing
433 returns 1 if pockets are full, else 0
434 */
435 pocketfull()
436 {
437 register int i,limit;
438 if ((limit = 15+(c[LEVEL]>>1)) > 26) limit=26;
439 for (i=0; i<limit; i++) if (iven[i]==0) return(0);
440 return(1);
441 }
442
443 /*
444 function to return 1 if a monster is next to the player else returns 0
445 */
446 nearbymonst()
447 {
448 register int tmp,tmp2;
449 for (tmp=playerx-1; tmp<playerx+2; tmp++)
450 for (tmp2=playery-1; tmp2<playery+2; tmp2++)
451 if (mitem[tmp][tmp2]) return(1); /* if monster nearby */
452 return(0);
453 }
454
455 /*
456 function to steal an item from the players pockets
457 returns 1 if steals something else returns 0
458 */
459 stealsomething()
460 {
461 register int i,j;
462 j=100;
463 while (1)
464 {
465 i=rund(26);
466 if (iven[i]) if (c[WEAR]!=i) if (c[WIELD]!=i) if (c[SHIELD]!=i)
467 {
468 srcount=0; show3(i);
469 adjustcvalues(iven[i],ivenarg[i]); iven[i]=0; return(1);
470 }
471 if (--j <= 0) return(0);
472 }
473 }
474
475 /*
476 function to return 1 is player carrys nothing else return 0
477 */
478 emptyhanded()
479 {
480 register int i;
481 for (i=0; i<26; i++)
482 if (iven[i]) if (i!=c[WIELD]) if (i!=c[WEAR]) if (i!=c[SHIELD]) return(0);
483 return(1);
484 }
485
486 /*
487 function to create a gem on a square near the player
488 */
489 creategem()
490 {
491 register int i,j;
492 switch(rnd(4))
493 {
494 case 1: i=ODIAMOND; j=50; break;
495 case 2: i=ORUBY; j=40; break;
496 case 3: i=OEMERALD; j=30; break;
497 default: i=OSAPPHIRE; j=20; break;
498 };
499 createitem(i,rnd(j)+j/10);
500 }
501
502 /*
503 function to change character levels as needed when dropping an object
504 that affects these characteristics
505 */
506 adjustcvalues(itm,arg)
507 int itm,arg;
508 {
509 register int flag;
510 flag=0;
511 switch(itm)
512 {
513 case ODEXRING: c[DEXTERITY] -= arg+1; flag=1; break;
514 case OSTRRING: c[STREXTRA] -= arg+1; flag=1; break;
515 case OCLEVERRING: c[INTELLIGENCE] -= arg+1; flag=1; break;
516 case OHAMMER: c[DEXTERITY] -= 10; c[STREXTRA] -= 10;
517 c[INTELLIGENCE] += 10; flag=1; break;
518 case OSWORDofSLASHING: c[DEXTERITY] -= 5; flag=1; break;
519 case OORBOFDRAGON: --c[SLAYING]; return;
520 case OSPIRITSCARAB: --c[NEGATESPIRIT]; return;
521 case OCUBEofUNDEAD: --c[CUBEofUNDEAD]; return;
522 case ONOTHEFT: --c[NOTHEFT]; return;
523 case OLANCE: c[LANCEDEATH]=0; return;
524 case OPOTION: case OSCROLL: return;
525
526 default: flag=1;
527 };
528 if (flag) bottomline();
529 }
530
531 /*
532 function to read a string from token input "string"
533 returns a pointer to the string
534 */
535 gettokstr(str)
536 register char *str;
537 {
538 register int i,j;
539 i=50;
540 while ((getchar() != '"') && (--i > 0));
541 i=36;
542 while (--i > 0)
543 {
544 if ((j=getchar()) != '"') *str++ = j; else i=0;
545 }
546 *str = 0;
547 i=50;
548 if (j != '"') while ((getchar() != '"') && (--i > 0)); /* if end due to too long, then find closing quote */
549 }
550
551 /*
552 function to ask user for a password (no echo)
553 returns 1 if entered correctly, 0 if not
554 */
555 static char gpwbuf[33];
556 getpassword()
557 {
558 register int i,j;
559 register char *gpwp;
560 extern char *password;
561 scbr(); /* system("stty -echo cbreak"); */
562 gpwp = gpwbuf; lprcat("\nEnter Password: "); lflush();
563 i = strlen(password);
564 for (j=0; j<i; j++) read(0,gpwp++,1); gpwbuf[i]=0;
565 sncbr(); /* system("stty echo -cbreak"); */
566 if (strcmp(gpwbuf,password) != 0)
567 { lprcat("\nSorry\n"); lflush(); return(0); }
568 else return(1);
569 }
570
571 /*
572 subroutine to get a yes or no response from the user
573 returns y or n
574 */
575 getyn()
576 {
577 register int i;
578 i=0; while (i!='y' && i!='n' && i!='\33') i=getchar();
579 return(i);
580 }
581
582 /*
583 function to calculate the pack weight of the player
584 returns the number of pounds the player is carrying
585 */
586 packweight()
587 {
588 register int i,j,k;
589 k=c[GOLD]/1000; j=25; while ((iven[j]==0) && (j>0)) --j;
590 for (i=0; i<=j; i++)
591 switch(iven[i])
592 {
593 case 0: break;
594 case OSSPLATE: case OPLATEARMOR: k += 40; break;
595 case OPLATE: k += 35; break;
596 case OHAMMER: k += 30; break;
597 case OSPLINT: k += 26; break;
598 case OSWORDofSLASHING: case OCHAIN:
599 case OBATTLEAXE: case O2SWORD: k += 23; break;
600 case OLONGSWORD: case OSWORD:
601 case ORING: case OFLAIL: k += 20; break;
602 case OLANCE: case OSTUDLEATHER: k += 15; break;
603 case OLEATHER: case OSPEAR: k += 8; break;
604 case OORBOFDRAGON: case OBELT: k += 4; break;
605 case OSHIELD: k += 7; break;
606 case OCHEST: k += 30 + ivenarg[i]; break;
607 default: k++;
608 };
609 return(k);
610 }
611
612 #ifndef MACRORND
613 /* macros to generate random numbers 1<=rnd(N)<=N 0<=rund(N)<=N-1 */
614 rnd(x)
615 int x;
616 {
617 return((((randx=randx*1103515245+12345)>>7)%(x))+1);
618 }
619
620 rund(x)
621 int x;
622 {
623 return((((randx=randx*1103515245+12345)>>7)%(x)) );
624 }
625 #endif MACRORND