]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - games/phantasia/misc.c
This commit was generated by cvs2svn to compensate for changes in r80231,
[FreeBSD/FreeBSD.git] / games / phantasia / misc.c
1 /*
2  * misc.c  Phantasia miscellaneous support routines
3  *
4  * $FreeBSD$
5  */
6
7 #include <string.h>
8 #include "include.h"
9
10
11 /************************************************************************
12 /
13 / FUNCTION NAME: movelevel()
14 /
15 / FUNCTION: move player to new level
16 /
17 / AUTHOR: E. A. Estes, 12/4/85
18 /
19 / ARGUMENTS: none
20 /
21 / RETURN VALUE: none
22 /
23 / MODULES CALLED: death(), floor(), wmove(), drandom(), waddstr(), explevel()
24 /
25 / GLOBAL INPUTS: Player, *stdscr, *Statptr, Stattable[]
26 /
27 / GLOBAL OUTPUTS: Player, Changed
28 /
29 / DESCRIPTION:
30 /       Use lookup table to increment important statistics when
31 /       progressing to new experience level.
32 /       Players are rested to maximum as a bonus for making a new
33 /       level.
34 /       Check for council of wise, and being too big to be king.
35 /
36 *************************************************************************/
37
38 movelevel()
39 {
40 struct charstats        *statptr;       /* for pointing into Stattable */
41 double  new;                    /* new level */
42 double  inc;                    /* increment between new and old levels */
43
44     Changed = TRUE;
45
46     if (Player.p_type == C_EXPER)
47         /* roll a type to use for increment */
48         statptr = &Stattable[(int) ROLL(C_MAGIC, C_HALFLING - C_MAGIC + 1)];
49     else
50         statptr = Statptr;
51
52     new = explevel(Player.p_experience);
53     inc = new - Player.p_level;
54     Player.p_level = new;
55
56     /* add increments to statistics */
57     Player.p_strength += statptr->c_strength.increase * inc;
58     Player.p_mana += statptr->c_mana.increase * inc;
59     Player.p_brains += statptr->c_brains.increase * inc;
60     Player.p_magiclvl += statptr->c_magiclvl.increase * inc;
61     Player.p_maxenergy += statptr->c_energy.increase * inc;
62
63     /* rest to maximum upon reaching new level */
64     Player.p_energy = Player.p_maxenergy + Player.p_shield;
65
66     if (Player.p_crowns > 0 && Player.p_level >= 1000.0)
67         /* no longer able to be king -- turn crowns into cash */
68         {
69         Player.p_gold += ((double) Player.p_crowns) * 5000.0;
70         Player.p_crowns = 0;
71         }
72
73     if (Player.p_level >= 3000.0 && Player.p_specialtype < SC_COUNCIL)
74         /* make a member of the council */
75         {
76         mvaddstr(6, 0, "You have made it to the Council of the Wise.\n");
77         addstr("Good Luck on your search for the Holy Grail.\n");
78
79         Player.p_specialtype = SC_COUNCIL;
80
81         /* no rings for council and above */
82         Player.p_ring.ring_type = R_NONE;
83         Player.p_ring.ring_duration = 0;
84
85         Player.p_lives = 3;             /* three extra lives */
86         }
87
88     if (Player.p_level > 9999.0 && Player.p_specialtype != SC_VALAR)
89         death("Old age");
90 }
91 /*\f*/
92 /************************************************************************
93 /
94 / FUNCTION NAME: descrlocation()
95 /
96 / FUNCTION: return a formatted description of location
97 /
98 / AUTHOR: E. A. Estes, 12/4/85
99 /
100 / ARGUMENTS:
101 /       struct player playerp - pointer to player structure
102 /       bool shortflag - set if short form is desired
103 /
104 / RETURN VALUE: pointer to string containing result
105 /
106 / MODULES CALLED: fabs(), floor(), sprintf(), distance()
107 /
108 / GLOBAL INPUTS: Databuf[]
109 /
110 / GLOBAL OUTPUTS: none
111 /
112 / DESCRIPTION:
113 /       Look at coordinates and return an appropriately formatted
114 /       string.
115 /
116 *************************************************************************/
117
118 char    *
119 descrlocation(playerp, shortflag)
120 struct player   *playerp;
121 bool    shortflag;
122 {
123 double  circle;                 /* corresponding circle for coordinates */
124 int     quadrant;       /* quandrant of grid */
125 char    *label;         /* pointer to place name */
126 static char     *nametable[4][4] =   /* names of places */
127         {
128         "Anorien",      "Ithilien",     "Rohan",        "Lorien",
129         "Gondor",       "Mordor",       "Dunland",      "Rovanion",
130         "South Gondor", "Khand",        "Eriador",      "The Iron Hills",
131         "Far Harad",    "Near Harad",   "The Northern Waste", "Rhun"
132         };
133
134     if (playerp->p_specialtype == SC_VALAR)
135         return(" is in Valhala");
136     else if ((circle = CIRCLE(playerp->p_x, playerp->p_y)) >= 1000.0)
137         {
138         if (MAX(fabs(playerp->p_x), fabs(playerp->p_y)) > D_BEYOND)
139             label = "The Point of No Return";
140         else
141             label = "The Ashen Mountains";
142         }
143     else if (circle >= 55)
144         label = "Morannon";
145     else if (circle >= 35)
146         label = "Kennaquahair";
147     else if (circle >= 20)
148         label = "The Dead Marshes";
149     else if (circle >= 9)
150         label = "The Outer Waste";
151     else if (circle >= 5)
152         label = "The Moors Adventurous";
153     else
154         {
155         if (playerp->p_x == 0.0 && playerp->p_y == 0.0)
156             label = "The Lord's Chamber";
157         else
158             {
159             /* this expression is split to prevent compiler loop with some compilers */
160             quadrant = ((playerp->p_x > 0.0) ? 1 : 0);
161             quadrant += ((playerp->p_y >= 0.0) ? 2 : 0);
162             label = nametable[((int) circle) - 1][quadrant];
163             }
164         }
165
166     if (shortflag)
167         sprintf(Databuf, "%.29s", label);
168     else
169         sprintf(Databuf, " is in %s  (%.0f,%.0f)", label, playerp->p_x, playerp->p_y);
170
171     return(Databuf);
172 }
173 /*\f*/
174 /************************************************************************
175 /
176 / FUNCTION NAME: tradingpost()
177 /
178 / FUNCTION: do trading post stuff
179 /
180 / AUTHOR: E. A. Estes, 12/4/85
181 /
182 / ARGUMENTS: none
183 /
184 / RETURN VALUE: none
185 /
186 / MODULES CALLED: writerecord(), adjuststats(), fabs(), more(), sqrt(),
187 /       sleep(), floor(), wmove(), drandom(), wclear(), printw(),
188 /       altercoordinates(), infloat(), waddstr(), wrefresh(), mvprintw(), getanswer(),
189 /       wclrtoeol(), wclrtobot()
190 /
191 / GLOBAL INPUTS: Menu[], Circle, Player, *stdscr, Fileloc, Nobetter[]
192 /
193 / GLOBAL OUTPUTS: Player
194 /
195 / DESCRIPTION:
196 /       Different trading posts have different items.
197 /       Merchants cannot be cheated, but they can be dishonest
198 /       themselves.
199 /
200 /       Shields, swords, and quicksilver are not cumulative.  This is
201 /       one major area of complaint, but there are two reasons for this:
202 /               1) It becomes MUCH too easy to make very large versions
203 /                  of these items.
204 /               2) In the real world, one cannot simply weld two swords
205 /                  together to make a bigger one.
206 /
207 /       At one time, it was possible to sell old weapons at half the purchase
208 /       price.  This resulted in huge amounts of gold floating around,
209 /       and the game lost much of its challenge.
210 /
211 /       Also, purchasing gems defeats the whole purpose of gold.  Gold
212 /       is small change for lower level players.  They really shouldn't
213 /       be able to accumulate more than enough gold for a small sword or
214 /       a few books.  Higher level players shouldn't even bother to pick
215 /       up gold, except maybe to buy mana once in a while.
216 /
217 *************************************************************************/
218
219 tradingpost()
220 {
221 double  numitems;       /* number of items to purchase */
222 double  cost;           /* cost of purchase */
223 double  blessingcost;   /* cost of blessing */
224 int     ch;             /* input */
225 int     size;   /* size of the trading post */
226 int     loop;   /* loop counter */
227 int     cheat = 0;      /* number of times player has tried to cheat */
228 bool    dishonest = FALSE;/* set when merchant is dishonest */
229
230     Player.p_status = S_TRADING;
231     writerecord(&Player, Fileloc);
232
233     clear();
234     addstr("You are at a trading post. All purchases must be made with gold.");
235
236     size = sqrt(fabs(Player.p_x / 100)) + 1;
237     size = MIN(7, size);
238
239     /* set up cost of blessing */
240     blessingcost = 1000.0 * (Player.p_level + 5.0);
241
242     /* print Menu */
243     move(7, 0);
244     for (loop = 0; loop < size; ++loop)
245         /* print Menu */
246         {
247         if (loop == 6)
248             cost = blessingcost;
249         else
250             cost = Menu[loop].cost;
251         printw("(%d) %-12s: %6.0f\n", loop + 1, Menu[loop].item, cost);
252         }
253
254     mvprintw(5, 0, "L:Leave  P:Purchase  S:Sell Gems ? ");
255
256     for (;;)
257         {
258         adjuststats();  /* truncate any bad values */
259
260         /* print some important statistics */
261         mvprintw(1, 0, "Gold:   %9.0f  Gems:  %9.0f  Level:   %6.0f  Charms: %6d\n",
262             Player.p_gold, Player.p_gems, Player.p_level, Player.p_charms);
263         printw("Shield: %9.0f  Sword: %9.0f  Quicksilver:%3.0f  Blessed: %s\n",
264             Player.p_shield, Player.p_sword, Player.p_quksilver,
265             (Player.p_blessing ? " True" : "False"));
266         printw("Brains: %9.0f  Mana:  %9.0f", Player.p_brains, Player.p_mana);
267
268         move(5, 36);
269         ch = getanswer("LPS", FALSE);
270         move(15, 0);
271         clrtobot();
272         switch(ch)
273             {
274             case 'L':           /* leave */
275             case '\n':
276                 altercoordinates(0.0, 0.0, A_NEAR);
277                 return;
278
279             case 'P':           /* make purchase */
280                 mvaddstr(15, 0, "What what would you like to buy ? ");
281                 ch = getanswer(" 1234567", FALSE);
282                 move(15, 0);
283                 clrtoeol();
284
285                 if (ch - '0' > size)
286                     addstr("Sorry, this merchant doesn't have that.");
287                 else
288                     switch (ch)
289                         {
290                         case '1':
291                             printw("Mana is one per %.0f gold piece.  How many do you want (%.0f max) ? ",
292                                 Menu[0].cost, floor(Player.p_gold / Menu[0].cost));
293                             cost = (numitems = floor(infloat())) * Menu[0].cost;
294
295                             if (cost > Player.p_gold || numitems < 0)
296                                 ++cheat;
297                             else
298                                 {
299                                 cheat = 0;
300                                 Player.p_gold -= cost;
301                                 if (drandom() < 0.02)
302                                     dishonest = TRUE;
303                                 else
304                                     Player.p_mana += numitems;
305                                 }
306                             break;
307
308                         case '2':
309                             printw("Shields are %.0f per +1.  How many do you want (%.0f max) ? ",
310                                 Menu[1].cost, floor(Player.p_gold / Menu[1].cost));
311                             cost = (numitems = floor(infloat())) * Menu[1].cost;
312
313                             if (numitems == 0.0)
314                                 break;
315                             else if (cost > Player.p_gold || numitems < 0)
316                                 ++cheat;
317                             else if (numitems < Player.p_shield)
318                                 NOBETTER();
319                             else
320                                 {
321                                 cheat = 0;
322                                 Player.p_gold -= cost;
323                                 if (drandom() < 0.02)
324                                     dishonest = TRUE;
325                                 else
326                                     Player.p_shield = numitems;
327                                 }
328                             break;
329
330                         case '3':
331                             printw("A book costs %.0f gp.  How many do you want (%.0f max) ? ",
332                                 Menu[2].cost, floor(Player.p_gold / Menu[2].cost));
333                             cost = (numitems = floor(infloat())) * Menu[2].cost;
334
335                             if (cost > Player.p_gold || numitems < 0)
336                                 ++cheat;
337                             else
338                                 {
339                                 cheat = 0;
340                                 Player.p_gold -= cost;
341                                 if (drandom() < 0.02)
342                                     dishonest = TRUE;
343                                 else if (drandom() * numitems > Player.p_level / 10.0
344                                     && numitems != 1)
345                                     {
346                                     printw("\nYou blew your mind!\n");
347                                     Player.p_brains /= 5;
348                                     }
349                                 else
350                                     {
351                                     Player.p_brains += floor(numitems) * ROLL(20, 8);
352                                     }
353                                 }
354                             break;
355
356                         case '4':
357                             printw("Swords are %.0f gp per +1.  How many + do you want (%.0f max) ? ",
358                                 Menu[3].cost, floor(Player.p_gold / Menu[3].cost));
359                             cost = (numitems = floor(infloat())) * Menu[3].cost;
360
361                             if (numitems == 0.0)
362                                 break;
363                             else if (cost > Player.p_gold || numitems < 0)
364                                 ++cheat;
365                             else if (numitems < Player.p_sword)
366                                 NOBETTER();
367                             else
368                                 {
369                                 cheat = 0;
370                                 Player.p_gold -= cost;
371                                 if (drandom() < 0.02)
372                                     dishonest = TRUE;
373                                 else
374                                     Player.p_sword = numitems;
375                                 }
376                             break;
377
378                         case '5':
379                             printw("A charm costs %.0f gp.  How many do you want (%.0f max) ? ",
380                                 Menu[4].cost, floor(Player.p_gold / Menu[4].cost));
381                             cost = (numitems = floor(infloat())) * Menu[4].cost;
382
383                             if (cost > Player.p_gold || numitems < 0)
384                                 ++cheat;
385                             else
386                                 {
387                                 cheat = 0;
388                                 Player.p_gold -= cost;
389                                 if (drandom() < 0.02)
390                                     dishonest = TRUE;
391                                 else
392                                     Player.p_charms += numitems;
393                                 }
394                             break;
395
396                         case '6':
397                             printw("Quicksilver is %.0f gp per +1.  How many + do you want (%.0f max) ? ",
398                                 Menu[5].cost, floor(Player.p_gold / Menu[5].cost));
399                             cost = (numitems = floor(infloat())) * Menu[5].cost;
400
401                             if (numitems == 0.0)
402                                 break;
403                             else if (cost > Player.p_gold || numitems < 0)
404                                 ++cheat;
405                             else if (numitems < Player.p_quksilver)
406                                 NOBETTER();
407                             else
408                                 {
409                                 cheat = 0;
410                                 Player.p_gold -= cost;
411                                 if (drandom() < 0.02)
412                                     dishonest = TRUE;
413                                 else
414                                     Player.p_quksilver = numitems;
415                                 }
416                             break;
417
418                         case '7':
419                             if (Player.p_blessing)
420                                 {
421                                 addstr("You already have a blessing.");
422                                 break;
423                                 }
424
425                             printw("A blessing requires a %.0f gp donation.  Still want one ? ", blessingcost);
426                             ch = getanswer("NY", FALSE);
427
428                             if (ch == 'Y')
429                                 {
430                                 if (Player.p_gold < blessingcost)
431                                     ++cheat;
432                                 else
433                                     {
434                                     cheat = 0;
435                                     Player.p_gold -= blessingcost;
436                                     if (drandom() < 0.02)
437                                         dishonest = TRUE;
438                                     else
439                                         Player.p_blessing = TRUE;
440                                     }
441                                 }
442                             break;
443                         }
444             break;
445
446             case 'S':           /* sell gems */
447                 mvprintw(15, 0, "A gem is worth %.0f gp.  How many do you want to sell (%.0f max) ? ",
448                     (double) N_GEMVALUE, Player.p_gems);
449                 numitems = floor(infloat());
450
451                 if (numitems > Player.p_gems || numitems < 0)
452                     ++cheat;
453                 else
454                     {
455                     cheat = 0;
456                     Player.p_gems -= numitems;
457                     Player.p_gold += numitems * N_GEMVALUE;
458                     }
459             }
460
461         if (cheat == 1)
462             mvaddstr(17, 0, "Come on, merchants aren't stupid.  Stop cheating.\n");
463         else if (cheat == 2)
464             {
465             mvaddstr(17, 0, "You had your chance.  This merchant happens to be\n");
466             printw("a %.0f level magic user, and you made %s mad!\n",
467                 ROLL(Circle * 20.0, 40.0), (drandom() < 0.5) ? "him" : "her");
468             altercoordinates(0.0, 0.0, A_FAR);
469             Player.p_energy /= 2.0;
470             ++Player.p_sin;
471             more(23);
472             return;
473             }
474         else if (dishonest)
475             {
476             mvaddstr(17, 0, "The merchant stole your money!");
477             refresh();
478             altercoordinates(Player.p_x - Player.p_x / 10.0,
479                 Player.p_y - Player.p_y / 10.0, A_SPECIFIC);
480             sleep(2);
481             return;
482             }
483         }
484 }
485 /*\f*/
486 /************************************************************************
487 /
488 / FUNCTION NAME: displaystats()
489 /
490 / FUNCTION: print out important player statistics
491 /
492 / AUTHOR: E. A. Estes, 12/4/85
493 /
494 / ARGUMENTS: none
495 /
496 / RETURN VALUE: none
497 /
498 / MODULES CALLED: descrstatus(), descrlocation(), mvprintw()
499 /
500 / GLOBAL INPUTS: Users, Player
501 /
502 / GLOBAL OUTPUTS: none
503 /
504 / DESCRIPTION:
505 /       Important player statistics are printed on the screen.
506 /
507 *************************************************************************/
508
509 displaystats()
510 {
511     mvprintw(0, 0, "%s%s\n", Player.p_name, descrlocation(&Player, FALSE));
512     mvprintw(1, 0, "Level :%7.0f   Energy  :%9.0f(%9.0f)  Mana :%9.0f  Users:%3d\n",
513         Player.p_level, Player.p_energy, Player.p_maxenergy + Player.p_shield,
514         Player.p_mana, Users);
515     mvprintw(2, 0, "Quick :%3.0f(%3.0f)  Strength:%9.0f(%9.0f)  Gold :%9.0f  %s\n",
516         Player.p_speed, Player.p_quickness + Player.p_quksilver, Player.p_might,
517         Player.p_strength + Player.p_sword, Player.p_gold, descrstatus(&Player));
518 }
519 /*\f*/
520 /************************************************************************
521 /
522 / FUNCTION NAME: allstatslist()
523 /
524 / FUNCTION: show player items
525 /
526 / AUTHOR: E. A. Estes, 12/4/85
527 /
528 / ARGUMENTS: none
529 /
530 / RETURN VALUE: none
531 /
532 / MODULES CALLED: mvprintw(), descrtype()
533 /
534 / GLOBAL INPUTS: Player
535 /
536 / GLOBAL OUTPUTS: none
537 /
538 / DESCRIPTION:
539 /       Print out some player statistics of lesser importance.
540 /
541 *************************************************************************/
542
543 allstatslist()
544 {
545 static  char    *flags[] =      /* to print value of some bools */
546             {
547             "False",
548             " True"
549             };
550
551     mvprintw( 8,  0, "Type: %s\n",  descrtype(&Player,  FALSE));
552
553     mvprintw(10,  0, "Experience: %9.0f", Player.p_experience);
554     mvprintw(11,  0, "Brains    : %9.0f", Player.p_brains);
555     mvprintw(12,  0, "Magic Lvl : %9.0f", Player.p_magiclvl);
556     mvprintw(13,  0, "Sin       : %9.5f", Player.p_sin);
557     mvprintw(14,  0, "Poison    : %9.5f", Player.p_poison);
558     mvprintw(15,  0, "Gems      : %9.0f", Player.p_gems);
559     mvprintw(16,  0, "Age       : %9d", Player.p_age);
560     mvprintw(10, 40, "Holy Water: %9d", Player.p_holywater);
561     mvprintw(11, 40, "Amulets   : %9d", Player.p_amulets);
562     mvprintw(12, 40, "Charms    : %9d", Player.p_charms);
563     mvprintw(13, 40, "Crowns    : %9d", Player.p_crowns);
564     mvprintw(14, 40, "Shield    : %9.0f", Player.p_shield);
565     mvprintw(15, 40, "Sword     : %9.0f", Player.p_sword);
566     mvprintw(16, 40, "Quickslver: %9.0f", Player.p_quksilver);
567
568     mvprintw(18,  0, "Blessing: %s   Ring: %s   Virgin: %s   Palantir: %s",
569         flags[Player.p_blessing], flags[Player.p_ring.ring_type != R_NONE],
570         flags[Player.p_virgin], flags[Player.p_palantir]);
571 }
572 /*\f*/
573 /************************************************************************
574 /
575 / FUNCTION NAME: descrtype()
576 /
577 / FUNCTION: return a string specifying player type
578 /
579 / AUTHOR: E. A. Estes, 12/4/85
580 /
581 / ARGUMENTS:
582 /       struct player playerp - pointer to structure for player
583 /       bool shortflag - set if short form is desired
584 /
585 / RETURN VALUE: pointer to string describing player type
586 /
587 / MODULES CALLED: strcpy()
588 /
589 / GLOBAL INPUTS: Databuf[]
590 /
591 / GLOBAL OUTPUTS: Databuf[]
592 /
593 / DESCRIPTION:
594 /       Return a string describing the player type.
595 /       King, council, valar, supercedes other types.
596 /       The first character of the string is '*' if the player
597 /       has a crown.
598 /       If 'shortflag' is TRUE, return a 3 character string.
599 /
600 *************************************************************************/
601
602 char    *
603 descrtype(playerp, shortflag)
604 struct player *playerp;
605 bool    shortflag;
606 {
607 int type;       /* for caluculating result subscript */
608 static char     *results[] =    /* description table */
609                         {
610                         " Magic User", " MU",
611                         " Fighter", " F ",
612                         " Elf", " E ",
613                         " Dwarf", " D ",
614                         " Halfling", " H ",
615                         " Experimento", " EX",
616                         " Super", " S ",
617                         " King", " K ",
618                         " Council of Wise", " CW",
619                         " Ex-Valar", " EV",
620                         " Valar", " V ",
621                         " ? ", " ? "
622                         };
623
624     type = playerp->p_type;
625
626     switch (playerp->p_specialtype)
627         {
628         case SC_NONE:
629             type = playerp->p_type;
630             break;
631
632         case SC_KING:
633             type = 7;
634             break;
635
636         case SC_COUNCIL:
637             type = 8;
638             break;
639
640         case SC_EXVALAR:
641             type = 9;
642             break;
643
644         case SC_VALAR:
645             type = 10;
646             break;
647         }
648
649     type *= 2;          /* calculate offset */
650
651     if (type > 20)
652         /* error */
653         type = 22;
654
655     if (shortflag)
656         /* use short descriptions */
657         ++type;
658
659     if (playerp->p_crowns > 0)
660         {
661         strcpy(Databuf, results[type]);
662         Databuf[0] = '*';
663         return(Databuf);
664         }
665     else
666         return(results[type]);
667 }
668 /*\f*/
669 /************************************************************************
670 /
671 / FUNCTION NAME: findname()
672 /
673 / FUNCTION: find location in player file of given name
674 /
675 / AUTHOR: E. A. Estes, 12/4/85
676 /
677 / ARGUMENTS:
678 /       char *name - name of character to look for
679 /       struct player *playerp - pointer of structure to fill
680 /
681 / RETURN VALUE: location of player if found, -1 otherwise
682 /
683 / MODULES CALLED: fread(), fseek(), strcmp()
684 /
685 / GLOBAL INPUTS: Wizard, *Playersfp
686 /
687 / GLOBAL OUTPUTS: none
688 /
689 / DESCRIPTION:
690 /       Search the player file for the player of the given name.
691 /       If player is found, fill structure with player data.
692 /
693 *************************************************************************/
694
695 long
696 findname(name, playerp)
697 char    *name;
698 struct player *playerp;
699 {
700 long    loc = 0;                        /* location in the file */
701
702     fseek(Playersfp, 0L, 0);
703     while (fread((char *) playerp, SZ_PLAYERSTRUCT, 1, Playersfp) == 1)
704         {
705         if (strcmp(playerp->p_name, name) == 0)
706             {
707             if (playerp->p_status != S_NOTUSED || Wizard)
708                 /* found it */
709                 return(loc);
710             }
711         loc += SZ_PLAYERSTRUCT;
712         }
713
714     return(-1);
715 }
716 /*\f*/
717 /************************************************************************
718 /
719 / FUNCTION NAME: allocrecord()
720 /
721 / FUNCTION: find space in the player file for a new character
722 /
723 / AUTHOR: E. A. Estes, 12/4/85
724 /
725 / ARGUMENTS: none
726 /
727 / RETURN VALUE: location of free space in file
728 /
729 / MODULES CALLED: initplayer(), writerecord(), fread(), fseek()
730 /
731 / GLOBAL INPUTS: Other, *Playersfp
732 /
733 / GLOBAL OUTPUTS: Player
734 /
735 / DESCRIPTION:
736 /       Search the player file for an unused entry.  If none are found,
737 /       make one at the end of the file.
738 /
739 *************************************************************************/
740
741 long
742 allocrecord()
743 {
744 long    loc = 0L;               /* location in file */
745
746     fseek(Playersfp, 0L, 0);
747     while (fread((char *) &Other, SZ_PLAYERSTRUCT, 1, Playersfp) == 1)
748         {
749         if (Other.p_status == S_NOTUSED)
750             /* found an empty record */
751             return(loc);
752         else
753             loc += SZ_PLAYERSTRUCT;
754         }
755
756     /* make a new record */
757     initplayer(&Other);
758     Player.p_status = S_OFF;
759     writerecord(&Other, loc);
760
761     return(loc);
762 }
763 /*\f*/
764 /************************************************************************
765 /
766 / FUNCTION NAME: freerecord()
767 /
768 / FUNCTION: free up a record on the player file
769 /
770 / AUTHOR: E. A. Estes, 2/7/86
771 /
772 / ARGUMENTS:
773 /       struct player playerp - pointer to structure to free
774 /       long loc - location in file to free
775 /
776 / RETURN VALUE: none
777 /
778 / MODULES CALLED: writerecord()
779 /
780 / GLOBAL INPUTS: none
781 /
782 / GLOBAL OUTPUTS: none
783 /
784 / DESCRIPTION:
785 /       Mark structure as not used, and update player file.
786 /
787 *************************************************************************/
788
789 freerecord(playerp, loc)
790 struct player   *playerp;
791 long    loc;
792 {
793     playerp->p_name[0] = CH_MARKDELETE;
794     playerp->p_status = S_NOTUSED;
795     writerecord(playerp, loc);
796 }
797 /*\f*/
798 /************************************************************************
799 /
800 / FUNCTION NAME: leavegame()
801 /
802 / FUNCTION: leave game
803 /
804 / AUTHOR: E. A. Estes, 12/4/85
805 /
806 / ARGUMENTS: none
807 /
808 / RETURN VALUE: none
809 /
810 / MODULES CALLED: freerecord(), writerecord(), cleanup()
811 /
812 / GLOBAL INPUTS: Player, Fileloc
813 /
814 / GLOBAL OUTPUTS: Player
815 /
816 / DESCRIPTION:
817 /       Mark player as inactive, and cleanup.
818 /       Do not save players below level 1.
819 /
820 *************************************************************************/
821
822 leavegame()
823 {
824
825     if (Player.p_level < 1.0)
826         /* delete character */
827         freerecord(&Player, Fileloc);
828     else
829         {
830         Player.p_status = S_OFF;
831         writerecord(&Player, Fileloc);
832         }
833
834     cleanup(TRUE);
835     /*NOTREACHED*/
836 }
837 /*\f*/
838 /************************************************************************
839 /
840 / FUNCTION NAME: death()
841 /
842 / FUNCTION: death routine
843 /
844 / AUTHOR: E. A. Estes, 12/4/85
845 /
846 / ARGUMENTS:
847 /       char *how - pointer to string describing cause of death
848 /
849 / RETURN VALUE: none
850 /
851 / MODULES CALLED: freerecord(), enterscore(), more(), exit(), fread(),
852 /       fseek(), execl(), fopen(), floor(), wmove(), drandom(), wclear(), strcmp(),
853 /       fwrite(), fflush(), printw(), strcpy(), fclose(), waddstr(), cleanup(),
854 /       fprintf(), wrefresh(), getanswer(), descrtype()
855 /
856 / GLOBAL INPUTS: Curmonster, Wizard, Player, *stdscr, Fileloc, *Monstfp
857 /
858 / GLOBAL OUTPUTS: Player
859 /
860 / DESCRIPTION:
861 /       Kill off current player.
862 /       Handle rings, and multiple lives.
863 /       Print an appropriate message.
864 /       Update scoreboard, lastdead, and let other players know about
865 /       the demise of their comrade.
866 /
867 *************************************************************************/
868
869 death(how)
870 char    *how;
871 {
872 FILE    *fp;            /* for updating various files */
873 int     ch;             /* input */
874 static  char    *deathmesg[] =
875         /* add more messages here, if desired */
876         {
877         "You have been wounded beyond repair.  ",
878         "You have been disemboweled.  ",
879         "You've been mashed, mauled, and spit upon.  (You're dead.)\n",
880         "You died!  ",
881         "You're a complete failure -- you've died!!\n",
882         "You have been dealt a fatal blow!  "
883         };
884
885     clear();
886
887     if (strcmp(how, "Stupidity") != 0)
888         {
889         if (Player.p_level > 9999.0)
890             /* old age */
891             addstr("Characters must be retired upon reaching level 10000.  Sorry.");
892         else if (Player.p_lives > 0)
893             /* extra lives */
894             {
895             addstr("You should be more cautious.  You've been killed.\n");
896             printw("You only have %d more chance(s).\n", --Player.p_lives);
897             more(3);
898             Player.p_energy = Player.p_maxenergy;
899             return;
900             }
901         else if (Player.p_specialtype == SC_VALAR)
902             {
903             addstr("You had your chances, but Valar aren't totally\n");
904             addstr("immortal.  You are now left to wither and die . . .\n");
905             more(3);
906             Player.p_brains = Player.p_level / 25.0;
907             Player.p_energy = Player.p_maxenergy /= 5.0;
908             Player.p_quksilver = Player.p_sword = 0.0;
909             Player.p_specialtype = SC_COUNCIL;
910             return;
911             }
912         else if (Player.p_ring.ring_inuse &&
913             (Player.p_ring.ring_type == R_DLREG || Player.p_ring.ring_type == R_NAZREG))
914             /* good ring in use - saved from death */
915             {
916             mvaddstr(4, 0, "Your ring saved you from death!\n");
917             refresh();
918             Player.p_ring.ring_type = R_NONE;
919             Player.p_energy = Player.p_maxenergy / 12.0 + 1.0;
920             if (Player.p_crowns > 0)
921                 --Player.p_crowns;
922             return;
923             }
924         else if (Player.p_ring.ring_type == R_BAD
925             || Player.p_ring.ring_type == R_SPOILED)
926             /* bad ring in possession; name idiot after player */
927             {
928             mvaddstr(4, 0,
929                 "Your ring has taken control of you and turned you into a monster!\n");
930             fseek(Monstfp, 13L * SZ_MONSTERSTRUCT, 0);
931             fread((char *) &Curmonster, SZ_MONSTERSTRUCT, 1, Monstfp);
932             strcpy(Curmonster.m_name, Player.p_name);
933             fseek(Monstfp, 13L * SZ_MONSTERSTRUCT, 0);
934             fwrite((char *) &Curmonster, SZ_MONSTERSTRUCT, 1, Monstfp);
935             fflush(Monstfp);
936             }
937         }
938
939     enterscore();               /* update score board */
940
941     /* put info in last dead file */
942     fp = fopen(_PATH_LASTDEAD, "w");
943     fprintf(fp,"%s (%s, run by %s, level %.0f, killed by %s)",
944         Player.p_name, descrtype(&Player, TRUE),
945         Player.p_login, Player.p_level, how);
946     fclose(fp);
947
948     /* let other players know */
949     fp = fopen(_PATH_MESS, "w");
950     fprintf(fp, "%s was killed by %s.", Player.p_name, how);
951     fclose(fp);
952
953     freerecord(&Player, Fileloc);
954
955     clear();
956     move(10, 0);
957     addstr(deathmesg[(int) ROLL(0.0, (double) sizeof(deathmesg) / sizeof(char *))]);
958     addstr("Care to give it another try ? ");
959     ch = getanswer("NY", FALSE);
960
961     if (ch == 'Y')
962         {
963         cleanup(FALSE);
964         execl(_PATH_GAMEPROG, "phantasia", "-s",
965             (Wizard ? "-S": (char *) NULL), (char *)0);
966         exit(0);
967         /*NOTREACHED*/
968         }
969
970     cleanup(TRUE);
971     /*NOTREACHED*/
972 }
973 /*\f*/
974 /************************************************************************
975 /
976 / FUNCTION NAME: writerecord()
977 /
978 / FUNCTION: update structure in player file
979 /
980 / AUTHOR: E. A. Estes, 12/4/85
981 /
982 / ARGUMENTS:
983 /       struct player *playerp - pointer to structure to write out
984 /       long place - location in file to updata
985 /
986 / RETURN VALUE: none
987 /
988 / MODULES CALLED: fseek(), fwrite(), fflush()
989 /
990 / GLOBAL INPUTS: *Playersfp
991 /
992 / GLOBAL OUTPUTS: none
993 /
994 / DESCRIPTION:
995 /       Update location in player file with given structure.
996 /
997 *************************************************************************/
998
999 writerecord(playerp, place)
1000 struct player   *playerp;
1001 long    place;
1002 {
1003     fseek(Playersfp, place, 0);
1004     fwrite((char *) playerp, SZ_PLAYERSTRUCT, 1, Playersfp);
1005     fflush(Playersfp);
1006 }
1007 /*\f*/
1008 /************************************************************************
1009 /
1010 / FUNCTION NAME: explevel()
1011 /
1012 / FUNCTION: calculate level based upon experience
1013 /
1014 / AUTHOR: E. A. Estes, 12/4/85
1015 /
1016 / ARGUMENTS:
1017 /       double experience - experience to calculate experience level from
1018 /
1019 / RETURN VALUE: experience level
1020 /
1021 / MODULES CALLED: pow(), floor()
1022 /
1023 / GLOBAL INPUTS: none
1024 /
1025 / GLOBAL OUTPUTS: none
1026 /
1027 / DESCRIPTION:
1028 /       Experience level is a geometric progression.  This has been finely
1029 /       tuned over the years, and probably should not be changed.
1030 /
1031 *************************************************************************/
1032
1033 double
1034 explevel(experience)
1035 double  experience;
1036 {
1037     if (experience < 1.1e7)
1038         return(floor(pow((experience / 1000.0), 0.4875)));
1039     else
1040         return(floor(pow((experience / 1250.0), 0.4865)));
1041 }
1042 /*\f*/
1043 /************************************************************************
1044 /
1045 / FUNCTION NAME: truncstring()
1046 /
1047 / FUNCTION: truncate trailing blanks off a string
1048 /
1049 / AUTHOR: E. A. Estes, 12/4/85
1050 /
1051 / ARGUMENTS:
1052 /       char *string - pointer to null terminated string
1053 /
1054 / RETURN VALUE: none
1055 /
1056 / MODULES CALLED: strlen()
1057 /
1058 / GLOBAL INPUTS: none
1059 /
1060 / GLOBAL OUTPUTS: none
1061 /
1062 / DESCRIPTION:
1063 /       Put nul characters in place of spaces at the end of the string.
1064 /
1065 *************************************************************************/
1066
1067 truncstring(string)
1068 char    *string;
1069 {
1070 int     length;         /* length of string */
1071
1072     length = strlen(string);
1073     while (string[--length] == ' ')
1074         string[length] = '\0';
1075 }
1076 /*\f*/
1077 /************************************************************************
1078 /
1079 / FUNCTION NAME: altercoordinates()
1080 /
1081 / FUNCTION: Alter x, y coordinates and set/check location flags
1082 /
1083 / AUTHOR: E. A. Estes, 12/16/85
1084 /
1085 / ARGUMENTS:
1086 /       double xnew, ynew - new x, y coordinates
1087 /       int operation - operation to perform with coordinates
1088 /
1089 / RETURN VALUE: none
1090 /
1091 / MODULES CALLED: fabs(), floor(), drandom(), distance()
1092 /
1093 / GLOBAL INPUTS: Circle, Beyond, Player
1094 /
1095 / GLOBAL OUTPUTS: Marsh, Circle, Beyond, Throne, Player, Changed
1096 /
1097 / DESCRIPTION:
1098 /       This module is called whenever the player's coordinates are altered.
1099 /       If the player is beyond the point of no return, he/she is forced
1100 /       to stay there.
1101 /
1102 *************************************************************************/
1103
1104 altercoordinates(xnew, ynew, operation)
1105 double  xnew;
1106 double  ynew;
1107 int     operation;
1108 {
1109     switch (operation)
1110         {
1111         case A_FORCED:  /* move with no checks */
1112             break;
1113
1114         case A_NEAR:    /* pick random coordinates near */
1115             xnew = Player.p_x + ROLL(1.0, 5.0);
1116             ynew = Player.p_y - ROLL(1.0, 5.0);
1117             /* fall through for check */
1118
1119         case A_SPECIFIC:        /* just move player */
1120             if (Beyond && fabs(xnew) < D_BEYOND && fabs(ynew) < D_BEYOND)
1121                 /*
1122                  * cannot move back from point of no return
1123                  * pick the largest coordinate to remain unchanged
1124                  */
1125                 {
1126                 if (fabs(xnew) > fabs(ynew))
1127                     xnew = SGN(Player.p_x) * MAX(fabs(Player.p_x), D_BEYOND);
1128                 else
1129                     ynew = SGN(Player.p_y) * MAX(fabs(Player.p_y), D_BEYOND);
1130                 }
1131             break;
1132
1133         case A_FAR:     /* pick random coordinates far */
1134             xnew = Player.p_x + SGN(Player.p_x) * ROLL(50 * Circle, 250 * Circle);
1135             ynew = Player.p_y + SGN(Player.p_y) * ROLL(50 * Circle, 250 * Circle);
1136             break;
1137         }
1138
1139     /* now set location flags and adjust coordinates */
1140     Circle = CIRCLE(Player.p_x = floor(xnew), Player.p_y = floor(ynew));
1141
1142     /* set up flags based upon location */
1143     Throne = Marsh = Beyond = FALSE;
1144
1145     if (Player.p_x == 0.0 && Player.p_y == 0.0)
1146         Throne = TRUE;
1147     else if (Circle < 35 && Circle >= 20)
1148         Marsh = TRUE;
1149     else if (MAX(fabs(Player.p_x), fabs(Player.p_y)) >= D_BEYOND)
1150         Beyond = TRUE;
1151
1152     Changed = TRUE;
1153 }
1154 /*\f*/
1155 /************************************************************************
1156 /
1157 / FUNCTION NAME: readrecord()
1158 /
1159 / FUNCTION: read a player structure from file
1160 /
1161 / AUTHOR: E. A. Estes, 12/4/85
1162 /
1163 / ARGUMENTS:
1164 /       struct player *playerp - pointer to structure to fill
1165 /       int loc - location of record to read
1166 /
1167 / RETURN VALUE: none
1168 /
1169 / MODULES CALLED: fread(), fseek()
1170 /
1171 / GLOBAL INPUTS: *Playersfp
1172 /
1173 / GLOBAL OUTPUTS: none
1174 /
1175 / DESCRIPTION:
1176 /       Read structure information from player file.
1177 /
1178 *************************************************************************/
1179
1180 readrecord(playerp, loc)
1181 struct player   *playerp;
1182 long    loc;
1183 {
1184     fseek(Playersfp, loc, 0);
1185     fread((char *) playerp, SZ_PLAYERSTRUCT, 1, Playersfp);
1186 }
1187 /*\f*/
1188 /************************************************************************
1189 /
1190 / FUNCTION NAME: adjuststats()
1191 /
1192 / FUNCTION: adjust player statistics
1193 /
1194 / AUTHOR: E. A. Estes, 12/4/85
1195 /
1196 / ARGUMENTS: none
1197 /
1198 / RETURN VALUE: none
1199 /
1200 / MODULES CALLED: death(), floor(), drandom(), explevel(), movelevel()
1201 /
1202 / GLOBAL INPUTS: Player, *Statptr
1203 /
1204 / GLOBAL OUTPUTS: Circle, Player, Timeout
1205 /
1206 / DESCRIPTION:
1207 /       Handle adjustment and maximums on various player characteristics.
1208 /
1209 *************************************************************************/
1210
1211 adjuststats()
1212 {
1213 double  dtemp;                          /* for temporary calculations */
1214
1215     if (explevel(Player.p_experience) > Player.p_level)
1216         /* move one or more levels */
1217         {
1218         movelevel();
1219         if (Player.p_level > 5.0)
1220             Timeout = TRUE;
1221         }
1222
1223     if (Player.p_specialtype == SC_VALAR)
1224         /* valar */
1225         Circle = Player.p_level / 5.0;
1226
1227     /* calculate effective quickness */
1228     dtemp = ((Player.p_gold + Player.p_gems / 2.0) - 1000.0) / Statptr->c_goldtote
1229         - Player.p_level;;
1230     dtemp = MAX(0.0, dtemp);            /* gold slows player down */
1231     Player.p_speed = Player.p_quickness + Player.p_quksilver - dtemp;
1232
1233     /* calculate effective strength */
1234     if (Player.p_poison > 0.0)
1235         /* poison makes player weaker */
1236         {
1237         dtemp = 1.0 - Player.p_poison * Statptr->c_weakness / 800.0;
1238         dtemp = MAX(0.1, dtemp);
1239         }
1240     else
1241         dtemp = 1.0;
1242     Player.p_might = dtemp *  Player.p_strength + Player.p_sword;
1243
1244     /* insure that important things are within limits */
1245     Player.p_quksilver = MIN(99.0, Player.p_quksilver);
1246     Player.p_mana = MIN(Player.p_mana,
1247         Player.p_level * Statptr->c_maxmana + 1000.0);
1248     Player.p_brains = MIN(Player.p_brains,
1249         Player.p_level * Statptr->c_maxbrains + 200.0);
1250     Player.p_charms = MIN(Player.p_charms, Player.p_level + 10.0);
1251
1252     /*
1253      * some implementations have problems with floating point compare
1254      * we work around it with this stuff
1255      */
1256     Player.p_gold = floor(Player.p_gold) + 0.1;
1257     Player.p_gems = floor(Player.p_gems) + 0.1;
1258     Player.p_mana = floor(Player.p_mana) + 0.1;
1259
1260     if (Player.p_ring.ring_type != R_NONE)
1261         /* do ring things */
1262         {
1263         /* rest to max */
1264         Player.p_energy = Player.p_maxenergy + Player.p_shield;
1265
1266         if (Player.p_ring.ring_duration <= 0)
1267             /* clean up expired rings */
1268             switch (Player.p_ring.ring_type)
1269                 {
1270                 case R_BAD:     /* ring drives player crazy */
1271                     Player.p_ring.ring_type = R_SPOILED;
1272                     Player.p_ring.ring_duration = (short) ROLL(10.0, 25.0);
1273                     break;
1274
1275                 case R_NAZREG:  /* ring disappears */
1276                     Player.p_ring.ring_type = R_NONE;
1277                     break;
1278
1279                 case R_SPOILED: /* ring kills player */
1280                     death("A cursed ring");
1281                     break;
1282
1283                 case R_DLREG:   /* this ring doesn't expire */
1284                     Player.p_ring.ring_duration = 0;
1285                     break;
1286                 }
1287         }
1288
1289     if (Player.p_age / N_AGE > Player.p_degenerated)
1290         /* age player slightly */
1291         {
1292         ++Player.p_degenerated;
1293         if (Player.p_quickness > 23.0)
1294             Player.p_quickness *= 0.99;
1295         Player.p_strength *= 0.97;
1296         Player.p_brains *= 0.95;
1297         Player.p_magiclvl *= 0.97;
1298         Player.p_maxenergy *= 0.95;
1299         Player.p_quksilver *= 0.95;
1300         Player.p_sword *= 0.93;
1301         Player.p_shield *= 0.93;
1302         }
1303 }
1304 /*\f*/
1305 /************************************************************************
1306 /
1307 / FUNCTION NAME: initplayer()
1308 /
1309 / FUNCTION: initialize a character
1310 /
1311 / AUTHOR: E. A. Estes, 12/4/85
1312 /
1313 / ARGUMENTS:
1314 /       struct player *playerp - pointer to structure to init
1315 /
1316 / RETURN VALUE: none
1317 /
1318 / MODULES CALLED: floor(), drandom()
1319 /
1320 / GLOBAL INPUTS: none
1321 /
1322 / GLOBAL OUTPUTS: none
1323 /
1324 / DESCRIPTION:
1325 /       Put a bunch of default values in the given structure.
1326 /
1327 *************************************************************************/
1328
1329 initplayer(playerp)
1330 struct  player   *playerp;
1331 {
1332     playerp->p_experience =
1333     playerp->p_level =
1334     playerp->p_strength =
1335     playerp->p_sword =
1336     playerp->p_might =
1337     playerp->p_energy =
1338     playerp->p_maxenergy =
1339     playerp->p_shield =
1340     playerp->p_quickness =
1341     playerp->p_quksilver =
1342     playerp->p_speed =
1343     playerp->p_magiclvl =
1344     playerp->p_mana =
1345     playerp->p_brains =
1346     playerp->p_poison =
1347     playerp->p_gems =
1348     playerp->p_sin =
1349     playerp->p_1scratch =
1350     playerp->p_2scratch = 0.0;
1351
1352     playerp->p_gold = ROLL(50.0, 75.0) + 0.1;   /* give some gold */
1353
1354     playerp->p_x = ROLL(-125.0, 251.0);
1355     playerp->p_y = ROLL(-125.0, 251.0);         /* give random x, y */
1356
1357     /* clear ring */
1358     playerp->p_ring.ring_type = R_NONE;
1359     playerp->p_ring.ring_duration = 0;
1360     playerp->p_ring.ring_inuse = FALSE;
1361
1362     playerp->p_age = 0L;
1363
1364     playerp->p_degenerated = 1;                 /* don't degenerate initially */
1365
1366     playerp->p_type = C_FIGHTER;                /* default */
1367     playerp->p_specialtype = SC_NONE;
1368     playerp->p_lives =
1369     playerp->p_crowns =
1370     playerp->p_charms =
1371     playerp->p_amulets =
1372     playerp->p_holywater =
1373     playerp->p_lastused = 0;
1374     playerp->p_status = S_NOTUSED;
1375     playerp->p_tampered = T_OFF;
1376     playerp->p_istat = I_OFF;
1377
1378     playerp->p_palantir =
1379     playerp->p_blessing =
1380     playerp->p_virgin =
1381     playerp->p_blindness = FALSE;
1382
1383     playerp->p_name[0] =
1384     playerp->p_password[0] =
1385     playerp->p_login[0] = '\0';
1386 }
1387 /*\f*/
1388 /************************************************************************
1389 /
1390 / FUNCTION NAME: readmessage()
1391 /
1392 / FUNCTION: read message from other players
1393 /
1394 / AUTHOR: E. A. Estes, 12/4/85
1395 /
1396 / ARGUMENTS: none
1397 /
1398 / RETURN VALUE: none
1399 /
1400 / MODULES CALLED: fseek(), fgets(), wmove(), waddstr(), wclrtoeol()
1401 /
1402 / GLOBAL INPUTS: *stdscr, Databuf[], *Messagefp
1403 /
1404 / GLOBAL OUTPUTS: none
1405 /
1406 / DESCRIPTION:
1407 /       If there is a message from other players, print it.
1408 /
1409 *************************************************************************/
1410
1411 readmessage()
1412 {
1413     move(3, 0);
1414     clrtoeol();
1415     fseek(Messagefp, 0L, 0);
1416     if (fgets(Databuf, SZ_DATABUF, Messagefp) != NULL)
1417         addstr(Databuf);
1418 }
1419 /*\f*/
1420 /************************************************************************
1421 /
1422 / FUNCTION NAME: error()
1423 /
1424 / FUNCTION: process evironment error
1425 /
1426 / AUTHOR: E. A. Estes, 12/4/85
1427 /
1428 / ARGUMENTS:
1429 /       char *whichfile - pointer to name of file which caused error
1430 /
1431 / RETURN VALUE: none
1432 /
1433 / MODULES CALLED: wclear(), cleanup()
1434 /
1435 / GLOBAL INPUTS: errno, *stdscr, printw(), printf(), Windows
1436 /
1437 / GLOBAL OUTPUTS: none
1438 /
1439 / DESCRIPTION:
1440 /       Print message about offending file, and exit.
1441 /
1442 *************************************************************************/
1443
1444 error(whichfile)
1445         char    *whichfile;
1446 {
1447         int     (*funcp) __P((const char *, ...));
1448
1449     if (Windows)
1450         {
1451         funcp = printw;
1452         clear();
1453         }
1454     else
1455         funcp = printf;
1456
1457     (*funcp)("An unrecoverable error has occurred reading %s.  (errno = %d)\n", whichfile, errno);
1458     (*funcp)("Please run 'setup' to determine the problem.\n");
1459     cleanup(TRUE);
1460     /*NOTREACHED*/
1461 }
1462 /*\f*/
1463 /************************************************************************
1464 /
1465 / FUNCTION NAME: distance()
1466 /
1467 / FUNCTION: calculate distance between two points
1468 /
1469 / AUTHOR: E. A. Estes, 12/4/85
1470 /
1471 / ARGUMENTS:
1472 /       double x1, y1 - x, y coordinates of first point
1473 /       double x2, y2 - x, y coordinates of second point
1474 /
1475 / RETURN VALUE: distance between the two points
1476 /
1477 / MODULES CALLED: sqrt()
1478 /
1479 / GLOBAL INPUTS: none
1480 /
1481 / GLOBAL OUTPUTS: none
1482 /
1483 / DESCRIPTION:
1484 /       This function is provided because someone's hypot() library function
1485 /       fails if x1 == x2 && y1 == y2.
1486 /
1487 *************************************************************************/
1488
1489 double
1490 distance(x1, x2, y1, y2)
1491 double  x1, x2, y1, y2;
1492 {
1493 double  deltax, deltay;
1494
1495     deltax = x1 - x2;
1496     deltay = y1 - y2;
1497     return(sqrt(deltax * deltax + deltay * deltay));
1498 }
1499
1500 /*\f*/
1501 /************************************************************************
1502 /
1503 / FUNCTION NAME: ill_sig()
1504 /
1505 / FUNCTION: exit upon trapping an illegal signal
1506 /
1507 / AUTHOR: E. A. Estes, 12/4/85
1508 /
1509 / ARGUMENTS:
1510 /       int whichsig - signal which occured to cause jump to here
1511 /
1512 / RETURN VALUE: none
1513 /
1514 / MODULES CALLED: wclear(), printw(), cleanup()
1515 /
1516 / GLOBAL INPUTS: *stdscr
1517 /
1518 / GLOBAL OUTPUTS: none
1519 /
1520 / DESCRIPTION:
1521 /       When an illegal signal is caught, print a message, and cleanup.
1522 /
1523 *************************************************************************/
1524
1525 ill_sig(whichsig)
1526 int whichsig;
1527 {
1528     clear();
1529     if (!(whichsig == SIGINT || whichsig == SIGQUIT))
1530         printw("Error: caught signal # %d.\n", whichsig);
1531     cleanup(TRUE);
1532     /*NOTREACHED*/
1533 }
1534 /*\f*/
1535 /************************************************************************
1536 /
1537 / FUNCTION NAME: descrstatus()
1538 /
1539 / FUNCTION: return a string describing the player status
1540 /
1541 / AUTHOR: E. A. Estes, 3/3/86
1542 /
1543 / ARGUMENTS:
1544 /       struct player playerp - pointer to player structure to describe
1545 /
1546 / RETURN VALUE: string describing player's status
1547 /
1548 / MODULES CALLED: none
1549 /
1550 / GLOBAL INPUTS: none
1551 /
1552 / GLOBAL OUTPUTS: none
1553 /
1554 / DESCRIPTION:
1555 /       Return verbal description of player status.
1556 /       If player status is S_PLAYING, check for low energy and blindness.
1557 /
1558 *************************************************************************/
1559
1560 char    *
1561 descrstatus(playerp)
1562 struct player   *playerp;
1563 {
1564     switch (playerp->p_status)
1565         {
1566         case S_PLAYING:
1567             if (playerp->p_energy < 0.2 * (playerp->p_maxenergy + playerp->p_shield))
1568                 return("Low Energy");
1569             else if (playerp->p_blindness)
1570                 return("Blind");
1571             else
1572                 return("In game");
1573
1574         case S_CLOAKED:
1575             return("Cloaked");
1576
1577         case S_INBATTLE:
1578             return("In Battle");
1579
1580         case S_MONSTER:
1581             return("Encounter");
1582
1583         case S_TRADING:
1584             return("Trading");
1585
1586         case S_OFF:
1587             return("Off");
1588
1589         case S_HUNGUP:
1590             return("Hung up");
1591
1592         default:
1593             return("");
1594         }
1595 }
1596 /*\f*/
1597 /************************************************************************
1598 /
1599 / FUNCTION NAME: drandom()
1600 /
1601 / FUNCTION: return a random floating point number from 0.0 < 1.0
1602 /
1603 / AUTHOR: E. A. Estes, 2/7/86
1604 /
1605 / ARGUMENTS: none
1606 /
1607 / RETURN VALUE: none
1608 /
1609 / MODULES CALLED: random()
1610 /
1611 / GLOBAL INPUTS: none
1612 /
1613 / GLOBAL OUTPUTS: none
1614 /
1615 / DESCRIPTION:
1616 /       Convert random integer from library routine into a floating
1617 /       point number, and divide by the largest possible random number.
1618 /       We mask large integers with 32767 to handle sites that return
1619 /       31 bit random integers.
1620 /
1621 *************************************************************************/
1622
1623 double
1624 drandom()
1625 {
1626     if (sizeof(int) != 2)
1627         /* use only low bits */
1628         return((double) (random() & 0x7fff) / 32768.0);
1629     else
1630         return((double) random() / 32768.0);
1631 }
1632 /*\f*/
1633 /************************************************************************
1634 /
1635 / FUNCTION NAME: collecttaxes()
1636 /
1637 / FUNCTION: collect taxes from current player
1638 /
1639 / AUTHOR: E. A. Estes, 2/7/86
1640 /
1641 / ARGUMENTS:
1642 /       double gold - amount of gold to tax
1643 /       double gems - amount of gems to tax
1644 /
1645 / RETURN VALUE: none
1646 /
1647 / MODULES CALLED: fread(), fseek(), fopen(), floor(), fwrite(), fclose()
1648 /
1649 / GLOBAL INPUTS: Player
1650 /
1651 / GLOBAL OUTPUTS: Player
1652 /
1653 / DESCRIPTION:
1654 /       Pay taxes on gold and gems.  If the player does not have enough
1655 /       gold to pay taxes on the added gems, convert some gems to gold.
1656 /       Add taxes to tax data base; add remaining gold and gems to
1657 /       player's cache.
1658 /
1659 *************************************************************************/
1660
1661 collecttaxes(gold, gems)
1662 double  gold;
1663 double  gems;
1664 {
1665 FILE    *fp;            /* to update Goldfile */
1666 double  dtemp;          /* for temporary calculations */
1667 double  taxes;          /* tax liability */
1668
1669     /* add to cache */
1670     Player.p_gold += gold;
1671     Player.p_gems += gems;
1672
1673     /* calculate tax liability */
1674     taxes = N_TAXAMOUNT / 100.0 * (N_GEMVALUE * gems + gold);
1675
1676     if (Player.p_gold < taxes)
1677         /* not enough gold to pay taxes, must convert some gems to gold */
1678         {
1679         dtemp = floor(taxes / N_GEMVALUE + 1.0); /* number of gems to convert */
1680
1681         if (Player.p_gems >= dtemp)
1682             /* player has enough to convert */
1683             {
1684             Player.p_gems -= dtemp;
1685             Player.p_gold += dtemp * N_GEMVALUE;
1686             }
1687         else
1688             /* take everything; this should never happen */
1689             {
1690             Player.p_gold += Player.p_gems * N_GEMVALUE;
1691             Player.p_gems = 0.0;
1692             taxes = Player.p_gold;
1693             }
1694         }
1695
1696     Player.p_gold -= taxes;
1697
1698     if ((fp = fopen(_PATH_GOLD, "r+")) != NULL)
1699         /* update taxes */
1700         {
1701         dtemp = 0.0;
1702         fread((char *) &dtemp, sizeof(double), 1, fp);
1703         dtemp += floor(taxes);
1704         fseek(fp, 0L, 0);
1705         fwrite((char *) &dtemp, sizeof(double), 1, fp);
1706         fclose(fp);
1707         }
1708 }