2 * misc.c Phantasia miscellaneous support routines
11 /************************************************************************
13 / FUNCTION NAME: movelevel()
15 / FUNCTION: move player to new level
17 / AUTHOR: E. A. Estes, 12/4/85
23 / MODULES CALLED: death(), floor(), wmove(), drandom(), waddstr(), explevel()
25 / GLOBAL INPUTS: Player, *stdscr, *Statptr, Stattable[]
27 / GLOBAL OUTPUTS: Player, Changed
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
34 / Check for council of wise, and being too big to be king.
36 *************************************************************************/
40 struct charstats *statptr; /* for pointing into Stattable */
41 double new; /* new level */
42 double inc; /* increment between new and old levels */
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)];
52 new = explevel(Player.p_experience);
53 inc = new - Player.p_level;
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;
63 /* rest to maximum upon reaching new level */
64 Player.p_energy = Player.p_maxenergy + Player.p_shield;
66 if (Player.p_crowns > 0 && Player.p_level >= 1000.0)
67 /* no longer able to be king -- turn crowns into cash */
69 Player.p_gold += ((double) Player.p_crowns) * 5000.0;
73 if (Player.p_level >= 3000.0 && Player.p_specialtype < SC_COUNCIL)
74 /* make a member of the council */
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");
79 Player.p_specialtype = SC_COUNCIL;
81 /* no rings for council and above */
82 Player.p_ring.ring_type = R_NONE;
83 Player.p_ring.ring_duration = 0;
85 Player.p_lives = 3; /* three extra lives */
88 if (Player.p_level > 9999.0 && Player.p_specialtype != SC_VALAR)
92 /************************************************************************
94 / FUNCTION NAME: descrlocation()
96 / FUNCTION: return a formatted description of location
98 / AUTHOR: E. A. Estes, 12/4/85
101 / struct player playerp - pointer to player structure
102 / bool shortflag - set if short form is desired
104 / RETURN VALUE: pointer to string containing result
106 / MODULES CALLED: fabs(), floor(), sprintf(), distance()
108 / GLOBAL INPUTS: Databuf[]
110 / GLOBAL OUTPUTS: none
113 / Look at coordinates and return an appropriately formatted
116 *************************************************************************/
119 descrlocation(playerp, shortflag)
120 struct player *playerp;
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 */
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"
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)
138 if (MAX(fabs(playerp->p_x), fabs(playerp->p_y)) > D_BEYOND)
139 label = "The Point of No Return";
141 label = "The Ashen Mountains";
143 else if (circle >= 55)
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";
155 if (playerp->p_x == 0.0 && playerp->p_y == 0.0)
156 label = "The Lord's Chamber";
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];
167 sprintf(Databuf, "%.29s", label);
169 sprintf(Databuf, " is in %s (%.0f,%.0f)", label, playerp->p_x, playerp->p_y);
174 /************************************************************************
176 / FUNCTION NAME: tradingpost()
178 / FUNCTION: do trading post stuff
180 / AUTHOR: E. A. Estes, 12/4/85
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()
191 / GLOBAL INPUTS: Menu[], Circle, Player, *stdscr, Fileloc, Nobetter[]
193 / GLOBAL OUTPUTS: Player
196 / Different trading posts have different items.
197 / Merchants cannot be cheated, but they can be dishonest
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
204 / 2) In the real world, one cannot simply weld two swords
205 / together to make a bigger one.
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.
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.
217 *************************************************************************/
221 double numitems; /* number of items to purchase */
222 double cost; /* cost of purchase */
223 double blessingcost; /* cost of blessing */
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 */
230 Player.p_status = S_TRADING;
231 writerecord(&Player, Fileloc);
234 addstr("You are at a trading post. All purchases must be made with gold.");
236 size = sqrt(fabs(Player.p_x / 100)) + 1;
239 /* set up cost of blessing */
240 blessingcost = 1000.0 * (Player.p_level + 5.0);
244 for (loop = 0; loop < size; ++loop)
250 cost = Menu[loop].cost;
251 printw("(%d) %-12s: %6.0f\n", loop + 1, Menu[loop].item, cost);
254 mvprintw(5, 0, "L:Leave P:Purchase S:Sell Gems ? ");
258 adjuststats(); /* truncate any bad values */
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);
269 ch = getanswer("LPS", FALSE);
274 case 'L': /* leave */
276 altercoordinates(0.0, 0.0, A_NEAR);
279 case 'P': /* make purchase */
280 mvaddstr(15, 0, "What what would you like to buy ? ");
281 ch = getanswer(" 1234567", FALSE);
286 addstr("Sorry, this merchant doesn't have that.");
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;
295 if (cost > Player.p_gold || numitems < 0)
300 Player.p_gold -= cost;
301 if (drandom() < 0.02)
304 Player.p_mana += numitems;
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;
315 else if (cost > Player.p_gold || numitems < 0)
317 else if (numitems < Player.p_shield)
322 Player.p_gold -= cost;
323 if (drandom() < 0.02)
326 Player.p_shield = numitems;
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;
335 if (cost > Player.p_gold || numitems < 0)
340 Player.p_gold -= cost;
341 if (drandom() < 0.02)
343 else if (drandom() * numitems > Player.p_level / 10.0
346 printw("\nYou blew your mind!\n");
347 Player.p_brains /= 5;
351 Player.p_brains += floor(numitems) * ROLL(20, 8);
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;
363 else if (cost > Player.p_gold || numitems < 0)
365 else if (numitems < Player.p_sword)
370 Player.p_gold -= cost;
371 if (drandom() < 0.02)
374 Player.p_sword = numitems;
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;
383 if (cost > Player.p_gold || numitems < 0)
388 Player.p_gold -= cost;
389 if (drandom() < 0.02)
392 Player.p_charms += numitems;
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;
403 else if (cost > Player.p_gold || numitems < 0)
405 else if (numitems < Player.p_quksilver)
410 Player.p_gold -= cost;
411 if (drandom() < 0.02)
414 Player.p_quksilver = numitems;
419 if (Player.p_blessing)
421 addstr("You already have a blessing.");
425 printw("A blessing requires a %.0f gp donation. Still want one ? ", blessingcost);
426 ch = getanswer("NY", FALSE);
430 if (Player.p_gold < blessingcost)
435 Player.p_gold -= blessingcost;
436 if (drandom() < 0.02)
439 Player.p_blessing = TRUE;
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());
451 if (numitems > Player.p_gems || numitems < 0)
456 Player.p_gems -= numitems;
457 Player.p_gold += numitems * N_GEMVALUE;
462 mvaddstr(17, 0, "Come on, merchants aren't stupid. Stop cheating.\n");
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;
476 mvaddstr(17, 0, "The merchant stole your money!");
478 altercoordinates(Player.p_x - Player.p_x / 10.0,
479 Player.p_y - Player.p_y / 10.0, A_SPECIFIC);
486 /************************************************************************
488 / FUNCTION NAME: displaystats()
490 / FUNCTION: print out important player statistics
492 / AUTHOR: E. A. Estes, 12/4/85
498 / MODULES CALLED: descrstatus(), descrlocation(), mvprintw()
500 / GLOBAL INPUTS: Users, Player
502 / GLOBAL OUTPUTS: none
505 / Important player statistics are printed on the screen.
507 *************************************************************************/
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));
520 /************************************************************************
522 / FUNCTION NAME: allstatslist()
524 / FUNCTION: show player items
526 / AUTHOR: E. A. Estes, 12/4/85
532 / MODULES CALLED: mvprintw(), descrtype()
534 / GLOBAL INPUTS: Player
536 / GLOBAL OUTPUTS: none
539 / Print out some player statistics of lesser importance.
541 *************************************************************************/
545 static char *flags[] = /* to print value of some bools */
551 mvprintw( 8, 0, "Type: %s\n", descrtype(&Player, FALSE));
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);
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]);
573 /************************************************************************
575 / FUNCTION NAME: descrtype()
577 / FUNCTION: return a string specifying player type
579 / AUTHOR: E. A. Estes, 12/4/85
582 / struct player playerp - pointer to structure for player
583 / bool shortflag - set if short form is desired
585 / RETURN VALUE: pointer to string describing player type
587 / MODULES CALLED: strcpy()
589 / GLOBAL INPUTS: Databuf[]
591 / GLOBAL OUTPUTS: Databuf[]
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
598 / If 'shortflag' is TRUE, return a 3 character string.
600 *************************************************************************/
603 descrtype(playerp, shortflag)
604 struct player *playerp;
607 int type; /* for caluculating result subscript */
608 static char *results[] = /* description table */
610 " Magic User", " MU",
615 " Experimento", " EX",
618 " Council of Wise", " CW",
624 type = playerp->p_type;
626 switch (playerp->p_specialtype)
629 type = playerp->p_type;
649 type *= 2; /* calculate offset */
656 /* use short descriptions */
659 if (playerp->p_crowns > 0)
661 strcpy(Databuf, results[type]);
666 return(results[type]);
669 /************************************************************************
671 / FUNCTION NAME: findname()
673 / FUNCTION: find location in player file of given name
675 / AUTHOR: E. A. Estes, 12/4/85
678 / char *name - name of character to look for
679 / struct player *playerp - pointer of structure to fill
681 / RETURN VALUE: location of player if found, -1 otherwise
683 / MODULES CALLED: fread(), fseek(), strcmp()
685 / GLOBAL INPUTS: Wizard, *Playersfp
687 / GLOBAL OUTPUTS: none
690 / Search the player file for the player of the given name.
691 / If player is found, fill structure with player data.
693 *************************************************************************/
696 findname(name, playerp)
698 struct player *playerp;
700 long loc = 0; /* location in the file */
702 fseek(Playersfp, 0L, 0);
703 while (fread((char *) playerp, SZ_PLAYERSTRUCT, 1, Playersfp) == 1)
705 if (strcmp(playerp->p_name, name) == 0)
707 if (playerp->p_status != S_NOTUSED || Wizard)
711 loc += SZ_PLAYERSTRUCT;
717 /************************************************************************
719 / FUNCTION NAME: allocrecord()
721 / FUNCTION: find space in the player file for a new character
723 / AUTHOR: E. A. Estes, 12/4/85
727 / RETURN VALUE: location of free space in file
729 / MODULES CALLED: initplayer(), writerecord(), fread(), fseek()
731 / GLOBAL INPUTS: Other, *Playersfp
733 / GLOBAL OUTPUTS: Player
736 / Search the player file for an unused entry. If none are found,
737 / make one at the end of the file.
739 *************************************************************************/
744 long loc = 0L; /* location in file */
746 fseek(Playersfp, 0L, 0);
747 while (fread((char *) &Other, SZ_PLAYERSTRUCT, 1, Playersfp) == 1)
749 if (Other.p_status == S_NOTUSED)
750 /* found an empty record */
753 loc += SZ_PLAYERSTRUCT;
756 /* make a new record */
758 Player.p_status = S_OFF;
759 writerecord(&Other, loc);
764 /************************************************************************
766 / FUNCTION NAME: freerecord()
768 / FUNCTION: free up a record on the player file
770 / AUTHOR: E. A. Estes, 2/7/86
773 / struct player playerp - pointer to structure to free
774 / long loc - location in file to free
778 / MODULES CALLED: writerecord()
780 / GLOBAL INPUTS: none
782 / GLOBAL OUTPUTS: none
785 / Mark structure as not used, and update player file.
787 *************************************************************************/
789 freerecord(playerp, loc)
790 struct player *playerp;
793 playerp->p_name[0] = CH_MARKDELETE;
794 playerp->p_status = S_NOTUSED;
795 writerecord(playerp, loc);
798 /************************************************************************
800 / FUNCTION NAME: leavegame()
802 / FUNCTION: leave game
804 / AUTHOR: E. A. Estes, 12/4/85
810 / MODULES CALLED: freerecord(), writerecord(), cleanup()
812 / GLOBAL INPUTS: Player, Fileloc
814 / GLOBAL OUTPUTS: Player
817 / Mark player as inactive, and cleanup.
818 / Do not save players below level 1.
820 *************************************************************************/
825 if (Player.p_level < 1.0)
826 /* delete character */
827 freerecord(&Player, Fileloc);
830 Player.p_status = S_OFF;
831 writerecord(&Player, Fileloc);
838 /************************************************************************
840 / FUNCTION NAME: death()
842 / FUNCTION: death routine
844 / AUTHOR: E. A. Estes, 12/4/85
847 / char *how - pointer to string describing cause of death
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()
856 / GLOBAL INPUTS: Curmonster, Wizard, Player, *stdscr, Fileloc, *Monstfp
858 / GLOBAL OUTPUTS: Player
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.
867 *************************************************************************/
872 FILE *fp; /* for updating various files */
874 static char *deathmesg[] =
875 /* add more messages here, if desired */
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",
881 "You're a complete failure -- you've died!!\n",
882 "You have been dealt a fatal blow! "
887 if (strcmp(how, "Stupidity") != 0)
889 if (Player.p_level > 9999.0)
891 addstr("Characters must be retired upon reaching level 10000. Sorry.");
892 else if (Player.p_lives > 0)
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);
898 Player.p_energy = Player.p_maxenergy;
901 else if (Player.p_specialtype == SC_VALAR)
903 addstr("You had your chances, but Valar aren't totally\n");
904 addstr("immortal. You are now left to wither and die . . .\n");
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;
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 */
916 mvaddstr(4, 0, "Your ring saved you from death!\n");
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)
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 */
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);
939 enterscore(); /* update score board */
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);
948 /* let other players know */
949 fp = fopen(_PATH_MESS, "w");
950 fprintf(fp, "%s was killed by %s.", Player.p_name, how);
953 freerecord(&Player, Fileloc);
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);
964 execl(_PATH_GAMEPROG, "phantasia", "-s",
965 (Wizard ? "-S": (char *) NULL), (char *)0);
974 /************************************************************************
976 / FUNCTION NAME: writerecord()
978 / FUNCTION: update structure in player file
980 / AUTHOR: E. A. Estes, 12/4/85
983 / struct player *playerp - pointer to structure to write out
984 / long place - location in file to updata
988 / MODULES CALLED: fseek(), fwrite(), fflush()
990 / GLOBAL INPUTS: *Playersfp
992 / GLOBAL OUTPUTS: none
995 / Update location in player file with given structure.
997 *************************************************************************/
999 writerecord(playerp, place)
1000 struct player *playerp;
1003 fseek(Playersfp, place, 0);
1004 fwrite((char *) playerp, SZ_PLAYERSTRUCT, 1, Playersfp);
1008 /************************************************************************
1010 / FUNCTION NAME: explevel()
1012 / FUNCTION: calculate level based upon experience
1014 / AUTHOR: E. A. Estes, 12/4/85
1017 / double experience - experience to calculate experience level from
1019 / RETURN VALUE: experience level
1021 / MODULES CALLED: pow(), floor()
1023 / GLOBAL INPUTS: none
1025 / GLOBAL OUTPUTS: none
1028 / Experience level is a geometric progression. This has been finely
1029 / tuned over the years, and probably should not be changed.
1031 *************************************************************************/
1034 explevel(experience)
1037 if (experience < 1.1e7)
1038 return(floor(pow((experience / 1000.0), 0.4875)));
1040 return(floor(pow((experience / 1250.0), 0.4865)));
1043 /************************************************************************
1045 / FUNCTION NAME: truncstring()
1047 / FUNCTION: truncate trailing blanks off a string
1049 / AUTHOR: E. A. Estes, 12/4/85
1052 / char *string - pointer to null terminated string
1054 / RETURN VALUE: none
1056 / MODULES CALLED: strlen()
1058 / GLOBAL INPUTS: none
1060 / GLOBAL OUTPUTS: none
1063 / Put nul characters in place of spaces at the end of the string.
1065 *************************************************************************/
1070 int length; /* length of string */
1072 length = strlen(string);
1073 while (string[--length] == ' ')
1074 string[length] = '\0';
1077 /************************************************************************
1079 / FUNCTION NAME: altercoordinates()
1081 / FUNCTION: Alter x, y coordinates and set/check location flags
1083 / AUTHOR: E. A. Estes, 12/16/85
1086 / double xnew, ynew - new x, y coordinates
1087 / int operation - operation to perform with coordinates
1089 / RETURN VALUE: none
1091 / MODULES CALLED: fabs(), floor(), drandom(), distance()
1093 / GLOBAL INPUTS: Circle, Beyond, Player
1095 / GLOBAL OUTPUTS: Marsh, Circle, Beyond, Throne, Player, Changed
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
1102 *************************************************************************/
1104 altercoordinates(xnew, ynew, operation)
1111 case A_FORCED: /* move with no checks */
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 */
1119 case A_SPECIFIC: /* just move player */
1120 if (Beyond && fabs(xnew) < D_BEYOND && fabs(ynew) < D_BEYOND)
1122 * cannot move back from point of no return
1123 * pick the largest coordinate to remain unchanged
1126 if (fabs(xnew) > fabs(ynew))
1127 xnew = SGN(Player.p_x) * MAX(fabs(Player.p_x), D_BEYOND);
1129 ynew = SGN(Player.p_y) * MAX(fabs(Player.p_y), D_BEYOND);
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);
1139 /* now set location flags and adjust coordinates */
1140 Circle = CIRCLE(Player.p_x = floor(xnew), Player.p_y = floor(ynew));
1142 /* set up flags based upon location */
1143 Throne = Marsh = Beyond = FALSE;
1145 if (Player.p_x == 0.0 && Player.p_y == 0.0)
1147 else if (Circle < 35 && Circle >= 20)
1149 else if (MAX(fabs(Player.p_x), fabs(Player.p_y)) >= D_BEYOND)
1155 /************************************************************************
1157 / FUNCTION NAME: readrecord()
1159 / FUNCTION: read a player structure from file
1161 / AUTHOR: E. A. Estes, 12/4/85
1164 / struct player *playerp - pointer to structure to fill
1165 / int loc - location of record to read
1167 / RETURN VALUE: none
1169 / MODULES CALLED: fread(), fseek()
1171 / GLOBAL INPUTS: *Playersfp
1173 / GLOBAL OUTPUTS: none
1176 / Read structure information from player file.
1178 *************************************************************************/
1180 readrecord(playerp, loc)
1181 struct player *playerp;
1184 fseek(Playersfp, loc, 0);
1185 fread((char *) playerp, SZ_PLAYERSTRUCT, 1, Playersfp);
1188 /************************************************************************
1190 / FUNCTION NAME: adjuststats()
1192 / FUNCTION: adjust player statistics
1194 / AUTHOR: E. A. Estes, 12/4/85
1198 / RETURN VALUE: none
1200 / MODULES CALLED: death(), floor(), drandom(), explevel(), movelevel()
1202 / GLOBAL INPUTS: Player, *Statptr
1204 / GLOBAL OUTPUTS: Circle, Player, Timeout
1207 / Handle adjustment and maximums on various player characteristics.
1209 *************************************************************************/
1213 double dtemp; /* for temporary calculations */
1215 if (explevel(Player.p_experience) > Player.p_level)
1216 /* move one or more levels */
1219 if (Player.p_level > 5.0)
1223 if (Player.p_specialtype == SC_VALAR)
1225 Circle = Player.p_level / 5.0;
1227 /* calculate effective quickness */
1228 dtemp = ((Player.p_gold + Player.p_gems / 2.0) - 1000.0) / Statptr->c_goldtote
1230 dtemp = MAX(0.0, dtemp); /* gold slows player down */
1231 Player.p_speed = Player.p_quickness + Player.p_quksilver - dtemp;
1233 /* calculate effective strength */
1234 if (Player.p_poison > 0.0)
1235 /* poison makes player weaker */
1237 dtemp = 1.0 - Player.p_poison * Statptr->c_weakness / 800.0;
1238 dtemp = MAX(0.1, dtemp);
1242 Player.p_might = dtemp * Player.p_strength + Player.p_sword;
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);
1253 * some implementations have problems with floating point compare
1254 * we work around it with this stuff
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;
1260 if (Player.p_ring.ring_type != R_NONE)
1261 /* do ring things */
1264 Player.p_energy = Player.p_maxenergy + Player.p_shield;
1266 if (Player.p_ring.ring_duration <= 0)
1267 /* clean up expired rings */
1268 switch (Player.p_ring.ring_type)
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);
1275 case R_NAZREG: /* ring disappears */
1276 Player.p_ring.ring_type = R_NONE;
1279 case R_SPOILED: /* ring kills player */
1280 death("A cursed ring");
1283 case R_DLREG: /* this ring doesn't expire */
1284 Player.p_ring.ring_duration = 0;
1289 if (Player.p_age / N_AGE > Player.p_degenerated)
1290 /* age player slightly */
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;
1305 /************************************************************************
1307 / FUNCTION NAME: initplayer()
1309 / FUNCTION: initialize a character
1311 / AUTHOR: E. A. Estes, 12/4/85
1314 / struct player *playerp - pointer to structure to init
1316 / RETURN VALUE: none
1318 / MODULES CALLED: floor(), drandom()
1320 / GLOBAL INPUTS: none
1322 / GLOBAL OUTPUTS: none
1325 / Put a bunch of default values in the given structure.
1327 *************************************************************************/
1330 struct player *playerp;
1332 playerp->p_experience =
1334 playerp->p_strength =
1338 playerp->p_maxenergy =
1340 playerp->p_quickness =
1341 playerp->p_quksilver =
1343 playerp->p_magiclvl =
1349 playerp->p_1scratch =
1350 playerp->p_2scratch = 0.0;
1352 playerp->p_gold = ROLL(50.0, 75.0) + 0.1; /* give some gold */
1354 playerp->p_x = ROLL(-125.0, 251.0);
1355 playerp->p_y = ROLL(-125.0, 251.0); /* give random x, y */
1358 playerp->p_ring.ring_type = R_NONE;
1359 playerp->p_ring.ring_duration = 0;
1360 playerp->p_ring.ring_inuse = FALSE;
1362 playerp->p_age = 0L;
1364 playerp->p_degenerated = 1; /* don't degenerate initially */
1366 playerp->p_type = C_FIGHTER; /* default */
1367 playerp->p_specialtype = SC_NONE;
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;
1378 playerp->p_palantir =
1379 playerp->p_blessing =
1381 playerp->p_blindness = FALSE;
1383 playerp->p_name[0] =
1384 playerp->p_password[0] =
1385 playerp->p_login[0] = '\0';
1388 /************************************************************************
1390 / FUNCTION NAME: readmessage()
1392 / FUNCTION: read message from other players
1394 / AUTHOR: E. A. Estes, 12/4/85
1398 / RETURN VALUE: none
1400 / MODULES CALLED: fseek(), fgets(), wmove(), waddstr(), wclrtoeol()
1402 / GLOBAL INPUTS: *stdscr, Databuf[], *Messagefp
1404 / GLOBAL OUTPUTS: none
1407 / If there is a message from other players, print it.
1409 *************************************************************************/
1415 fseek(Messagefp, 0L, 0);
1416 if (fgets(Databuf, SZ_DATABUF, Messagefp) != NULL)
1420 /************************************************************************
1422 / FUNCTION NAME: error()
1424 / FUNCTION: process evironment error
1426 / AUTHOR: E. A. Estes, 12/4/85
1429 / char *whichfile - pointer to name of file which caused error
1431 / RETURN VALUE: none
1433 / MODULES CALLED: wclear(), cleanup()
1435 / GLOBAL INPUTS: errno, *stdscr, printw(), printf(), Windows
1437 / GLOBAL OUTPUTS: none
1440 / Print message about offending file, and exit.
1442 *************************************************************************/
1447 int (*funcp) __P((const char *, ...));
1457 (*funcp)("An unrecoverable error has occurred reading %s. (errno = %d)\n", whichfile, errno);
1458 (*funcp)("Please run 'setup' to determine the problem.\n");
1463 /************************************************************************
1465 / FUNCTION NAME: distance()
1467 / FUNCTION: calculate distance between two points
1469 / AUTHOR: E. A. Estes, 12/4/85
1472 / double x1, y1 - x, y coordinates of first point
1473 / double x2, y2 - x, y coordinates of second point
1475 / RETURN VALUE: distance between the two points
1477 / MODULES CALLED: sqrt()
1479 / GLOBAL INPUTS: none
1481 / GLOBAL OUTPUTS: none
1484 / This function is provided because someone's hypot() library function
1485 / fails if x1 == x2 && y1 == y2.
1487 *************************************************************************/
1490 distance(x1, x2, y1, y2)
1491 double x1, x2, y1, y2;
1493 double deltax, deltay;
1497 return(sqrt(deltax * deltax + deltay * deltay));
1501 /************************************************************************
1503 / FUNCTION NAME: ill_sig()
1505 / FUNCTION: exit upon trapping an illegal signal
1507 / AUTHOR: E. A. Estes, 12/4/85
1510 / int whichsig - signal which occured to cause jump to here
1512 / RETURN VALUE: none
1514 / MODULES CALLED: wclear(), printw(), cleanup()
1516 / GLOBAL INPUTS: *stdscr
1518 / GLOBAL OUTPUTS: none
1521 / When an illegal signal is caught, print a message, and cleanup.
1523 *************************************************************************/
1529 if (!(whichsig == SIGINT || whichsig == SIGQUIT))
1530 printw("Error: caught signal # %d.\n", whichsig);
1535 /************************************************************************
1537 / FUNCTION NAME: descrstatus()
1539 / FUNCTION: return a string describing the player status
1541 / AUTHOR: E. A. Estes, 3/3/86
1544 / struct player playerp - pointer to player structure to describe
1546 / RETURN VALUE: string describing player's status
1548 / MODULES CALLED: none
1550 / GLOBAL INPUTS: none
1552 / GLOBAL OUTPUTS: none
1555 / Return verbal description of player status.
1556 / If player status is S_PLAYING, check for low energy and blindness.
1558 *************************************************************************/
1561 descrstatus(playerp)
1562 struct player *playerp;
1564 switch (playerp->p_status)
1567 if (playerp->p_energy < 0.2 * (playerp->p_maxenergy + playerp->p_shield))
1568 return("Low Energy");
1569 else if (playerp->p_blindness)
1578 return("In Battle");
1581 return("Encounter");
1597 /************************************************************************
1599 / FUNCTION NAME: drandom()
1601 / FUNCTION: return a random floating point number from 0.0 < 1.0
1603 / AUTHOR: E. A. Estes, 2/7/86
1607 / RETURN VALUE: none
1609 / MODULES CALLED: random()
1611 / GLOBAL INPUTS: none
1613 / GLOBAL OUTPUTS: none
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.
1621 *************************************************************************/
1626 if (sizeof(int) != 2)
1627 /* use only low bits */
1628 return((double) (random() & 0x7fff) / 32768.0);
1630 return((double) random() / 32768.0);
1633 /************************************************************************
1635 / FUNCTION NAME: collecttaxes()
1637 / FUNCTION: collect taxes from current player
1639 / AUTHOR: E. A. Estes, 2/7/86
1642 / double gold - amount of gold to tax
1643 / double gems - amount of gems to tax
1645 / RETURN VALUE: none
1647 / MODULES CALLED: fread(), fseek(), fopen(), floor(), fwrite(), fclose()
1649 / GLOBAL INPUTS: Player
1651 / GLOBAL OUTPUTS: Player
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
1659 *************************************************************************/
1661 collecttaxes(gold, gems)
1665 FILE *fp; /* to update Goldfile */
1666 double dtemp; /* for temporary calculations */
1667 double taxes; /* tax liability */
1670 Player.p_gold += gold;
1671 Player.p_gems += gems;
1673 /* calculate tax liability */
1674 taxes = N_TAXAMOUNT / 100.0 * (N_GEMVALUE * gems + gold);
1676 if (Player.p_gold < taxes)
1677 /* not enough gold to pay taxes, must convert some gems to gold */
1679 dtemp = floor(taxes / N_GEMVALUE + 1.0); /* number of gems to convert */
1681 if (Player.p_gems >= dtemp)
1682 /* player has enough to convert */
1684 Player.p_gems -= dtemp;
1685 Player.p_gold += dtemp * N_GEMVALUE;
1688 /* take everything; this should never happen */
1690 Player.p_gold += Player.p_gems * N_GEMVALUE;
1691 Player.p_gems = 0.0;
1692 taxes = Player.p_gold;
1696 Player.p_gold -= taxes;
1698 if ((fp = fopen(_PATH_GOLD, "r+")) != NULL)
1702 fread((char *) &dtemp, sizeof(double), 1, fp);
1703 dtemp += floor(taxes);
1705 fwrite((char *) &dtemp, sizeof(double), 1, fp);