2 * misc.c Phantasia miscellaneous support routines
4 * $FreeBSD: src/games/phantasia/misc.c,v 1.7 1999/11/16 02:57:34 billf Exp $
5 * $DragonFly: src/games/phantasia/misc.c,v 1.4 2004/01/26 18:14:43 drhodus Exp $
12 /************************************************************************
14 / FUNCTION NAME: movelevel()
16 / FUNCTION: move player to new level
18 / AUTHOR: E. A. Estes, 12/4/85
24 / MODULES CALLED: death(), floor(), wmove(), drandom(), waddstr(), explevel()
26 / GLOBAL INPUTS: Player, *stdscr, *Statptr, Stattable[]
28 / GLOBAL OUTPUTS: Player, Changed
31 / Use lookup table to increment important statistics when
32 / progressing to new experience level.
33 / Players are rested to maximum as a bonus for making a new
35 / Check for council of wise, and being too big to be king.
37 *************************************************************************/
41 struct charstats *statptr; /* for pointing into Stattable */
42 double new; /* new level */
43 double inc; /* increment between new and old levels */
47 if (Player.p_type == C_EXPER)
48 /* roll a type to use for increment */
49 statptr = &Stattable[(int) ROLL(C_MAGIC, C_HALFLING - C_MAGIC + 1)];
53 new = explevel(Player.p_experience);
54 inc = new - Player.p_level;
57 /* add increments to statistics */
58 Player.p_strength += statptr->c_strength.increase * inc;
59 Player.p_mana += statptr->c_mana.increase * inc;
60 Player.p_brains += statptr->c_brains.increase * inc;
61 Player.p_magiclvl += statptr->c_magiclvl.increase * inc;
62 Player.p_maxenergy += statptr->c_energy.increase * inc;
64 /* rest to maximum upon reaching new level */
65 Player.p_energy = Player.p_maxenergy + Player.p_shield;
67 if (Player.p_crowns > 0 && Player.p_level >= 1000.0)
68 /* no longer able to be king -- turn crowns into cash */
70 Player.p_gold += ((double) Player.p_crowns) * 5000.0;
74 if (Player.p_level >= 3000.0 && Player.p_specialtype < SC_COUNCIL)
75 /* make a member of the council */
77 mvaddstr(6, 0, "You have made it to the Council of the Wise.\n");
78 addstr("Good Luck on your search for the Holy Grail.\n");
80 Player.p_specialtype = SC_COUNCIL;
82 /* no rings for council and above */
83 Player.p_ring.ring_type = R_NONE;
84 Player.p_ring.ring_duration = 0;
86 Player.p_lives = 3; /* three extra lives */
89 if (Player.p_level > 9999.0 && Player.p_specialtype != SC_VALAR)
93 /************************************************************************
95 / FUNCTION NAME: descrlocation()
97 / FUNCTION: return a formatted description of location
99 / AUTHOR: E. A. Estes, 12/4/85
102 / struct player playerp - pointer to player structure
103 / bool shortflag - set if short form is desired
105 / RETURN VALUE: pointer to string containing result
107 / MODULES CALLED: fabs(), floor(), sprintf(), distance()
109 / GLOBAL INPUTS: Databuf[]
111 / GLOBAL OUTPUTS: none
114 / Look at coordinates and return an appropriately formatted
117 *************************************************************************/
120 descrlocation(playerp, shortflag)
121 struct player *playerp;
124 double circle; /* corresponding circle for coordinates */
125 int quadrant; /* quandrant of grid */
126 char *label; /* pointer to place name */
127 static char *nametable[4][4] = /* names of places */
129 "Anorien", "Ithilien", "Rohan", "Lorien",
130 "Gondor", "Mordor", "Dunland", "Rovanion",
131 "South Gondor", "Khand", "Eriador", "The Iron Hills",
132 "Far Harad", "Near Harad", "The Northern Waste", "Rhun"
135 if (playerp->p_specialtype == SC_VALAR)
136 return(" is in Valhala");
137 else if ((circle = CIRCLE(playerp->p_x, playerp->p_y)) >= 1000.0)
139 if (MAX(fabs(playerp->p_x), fabs(playerp->p_y)) > D_BEYOND)
140 label = "The Point of No Return";
142 label = "The Ashen Mountains";
144 else if (circle >= 55)
146 else if (circle >= 35)
147 label = "Kennaquahair";
148 else if (circle >= 20)
149 label = "The Dead Marshes";
150 else if (circle >= 9)
151 label = "The Outer Waste";
152 else if (circle >= 5)
153 label = "The Moors Adventurous";
156 if (playerp->p_x == 0.0 && playerp->p_y == 0.0)
157 label = "The Lord's Chamber";
160 /* this expression is split to prevent compiler loop with some compilers */
161 quadrant = ((playerp->p_x > 0.0) ? 1 : 0);
162 quadrant += ((playerp->p_y >= 0.0) ? 2 : 0);
163 label = nametable[((int) circle) - 1][quadrant];
168 sprintf(Databuf, "%.29s", label);
170 sprintf(Databuf, " is in %s (%.0f,%.0f)", label, playerp->p_x, playerp->p_y);
175 /************************************************************************
177 / FUNCTION NAME: tradingpost()
179 / FUNCTION: do trading post stuff
181 / AUTHOR: E. A. Estes, 12/4/85
187 / MODULES CALLED: writerecord(), adjuststats(), fabs(), more(), sqrt(),
188 / sleep(), floor(), wmove(), drandom(), wclear(), printw(),
189 / altercoordinates(), infloat(), waddstr(), wrefresh(), mvprintw(), getanswer(),
190 / wclrtoeol(), wclrtobot()
192 / GLOBAL INPUTS: Menu[], Circle, Player, *stdscr, Fileloc, Nobetter[]
194 / GLOBAL OUTPUTS: Player
197 / Different trading posts have different items.
198 / Merchants cannot be cheated, but they can be dishonest
201 / Shields, swords, and quicksilver are not cumulative. This is
202 / one major area of complaint, but there are two reasons for this:
203 / 1) It becomes MUCH too easy to make very large versions
205 / 2) In the real world, one cannot simply weld two swords
206 / together to make a bigger one.
208 / At one time, it was possible to sell old weapons at half the purchase
209 / price. This resulted in huge amounts of gold floating around,
210 / and the game lost much of its challenge.
212 / Also, purchasing gems defeats the whole purpose of gold. Gold
213 / is small change for lower level players. They really shouldn't
214 / be able to accumulate more than enough gold for a small sword or
215 / a few books. Higher level players shouldn't even bother to pick
216 / up gold, except maybe to buy mana once in a while.
218 *************************************************************************/
222 double numitems; /* number of items to purchase */
223 double cost; /* cost of purchase */
224 double blessingcost; /* cost of blessing */
226 int size; /* size of the trading post */
227 int loop; /* loop counter */
228 int cheat = 0; /* number of times player has tried to cheat */
229 bool dishonest = FALSE;/* set when merchant is dishonest */
231 Player.p_status = S_TRADING;
232 writerecord(&Player, Fileloc);
235 addstr("You are at a trading post. All purchases must be made with gold.");
237 size = sqrt(fabs(Player.p_x / 100)) + 1;
240 /* set up cost of blessing */
241 blessingcost = 1000.0 * (Player.p_level + 5.0);
245 for (loop = 0; loop < size; ++loop)
251 cost = Menu[loop].cost;
252 printw("(%d) %-12s: %6.0f\n", loop + 1, Menu[loop].item, cost);
255 mvprintw(5, 0, "L:Leave P:Purchase S:Sell Gems ? ");
259 adjuststats(); /* truncate any bad values */
261 /* print some important statistics */
262 mvprintw(1, 0, "Gold: %9.0f Gems: %9.0f Level: %6.0f Charms: %6d\n",
263 Player.p_gold, Player.p_gems, Player.p_level, Player.p_charms);
264 printw("Shield: %9.0f Sword: %9.0f Quicksilver:%3.0f Blessed: %s\n",
265 Player.p_shield, Player.p_sword, Player.p_quksilver,
266 (Player.p_blessing ? " True" : "False"));
267 printw("Brains: %9.0f Mana: %9.0f", Player.p_brains, Player.p_mana);
270 ch = getanswer("LPS", FALSE);
275 case 'L': /* leave */
277 altercoordinates(0.0, 0.0, A_NEAR);
280 case 'P': /* make purchase */
281 mvaddstr(15, 0, "What what would you like to buy ? ");
282 ch = getanswer(" 1234567", FALSE);
287 addstr("Sorry, this merchant doesn't have that.");
292 printw("Mana is one per %.0f gold piece. How many do you want (%.0f max) ? ",
293 Menu[0].cost, floor(Player.p_gold / Menu[0].cost));
294 cost = (numitems = floor(infloat())) * Menu[0].cost;
296 if (cost > Player.p_gold || numitems < 0)
301 Player.p_gold -= cost;
302 if (drandom() < 0.02)
305 Player.p_mana += numitems;
310 printw("Shields are %.0f per +1. How many do you want (%.0f max) ? ",
311 Menu[1].cost, floor(Player.p_gold / Menu[1].cost));
312 cost = (numitems = floor(infloat())) * Menu[1].cost;
316 else if (cost > Player.p_gold || numitems < 0)
318 else if (numitems < Player.p_shield)
323 Player.p_gold -= cost;
324 if (drandom() < 0.02)
327 Player.p_shield = numitems;
332 printw("A book costs %.0f gp. How many do you want (%.0f max) ? ",
333 Menu[2].cost, floor(Player.p_gold / Menu[2].cost));
334 cost = (numitems = floor(infloat())) * Menu[2].cost;
336 if (cost > Player.p_gold || numitems < 0)
341 Player.p_gold -= cost;
342 if (drandom() < 0.02)
344 else if (drandom() * numitems > Player.p_level / 10.0
347 printw("\nYou blew your mind!\n");
348 Player.p_brains /= 5;
352 Player.p_brains += floor(numitems) * ROLL(20, 8);
358 printw("Swords are %.0f gp per +1. How many + do you want (%.0f max) ? ",
359 Menu[3].cost, floor(Player.p_gold / Menu[3].cost));
360 cost = (numitems = floor(infloat())) * Menu[3].cost;
364 else if (cost > Player.p_gold || numitems < 0)
366 else if (numitems < Player.p_sword)
371 Player.p_gold -= cost;
372 if (drandom() < 0.02)
375 Player.p_sword = numitems;
380 printw("A charm costs %.0f gp. How many do you want (%.0f max) ? ",
381 Menu[4].cost, floor(Player.p_gold / Menu[4].cost));
382 cost = (numitems = floor(infloat())) * Menu[4].cost;
384 if (cost > Player.p_gold || numitems < 0)
389 Player.p_gold -= cost;
390 if (drandom() < 0.02)
393 Player.p_charms += numitems;
398 printw("Quicksilver is %.0f gp per +1. How many + do you want (%.0f max) ? ",
399 Menu[5].cost, floor(Player.p_gold / Menu[5].cost));
400 cost = (numitems = floor(infloat())) * Menu[5].cost;
404 else if (cost > Player.p_gold || numitems < 0)
406 else if (numitems < Player.p_quksilver)
411 Player.p_gold -= cost;
412 if (drandom() < 0.02)
415 Player.p_quksilver = numitems;
420 if (Player.p_blessing)
422 addstr("You already have a blessing.");
426 printw("A blessing requires a %.0f gp donation. Still want one ? ", blessingcost);
427 ch = getanswer("NY", FALSE);
431 if (Player.p_gold < blessingcost)
436 Player.p_gold -= blessingcost;
437 if (drandom() < 0.02)
440 Player.p_blessing = TRUE;
447 case 'S': /* sell gems */
448 mvprintw(15, 0, "A gem is worth %.0f gp. How many do you want to sell (%.0f max) ? ",
449 (double) N_GEMVALUE, Player.p_gems);
450 numitems = floor(infloat());
452 if (numitems > Player.p_gems || numitems < 0)
457 Player.p_gems -= numitems;
458 Player.p_gold += numitems * N_GEMVALUE;
463 mvaddstr(17, 0, "Come on, merchants aren't stupid. Stop cheating.\n");
466 mvaddstr(17, 0, "You had your chance. This merchant happens to be\n");
467 printw("a %.0f level magic user, and you made %s mad!\n",
468 ROLL(Circle * 20.0, 40.0), (drandom() < 0.5) ? "him" : "her");
469 altercoordinates(0.0, 0.0, A_FAR);
470 Player.p_energy /= 2.0;
477 mvaddstr(17, 0, "The merchant stole your money!");
479 altercoordinates(Player.p_x - Player.p_x / 10.0,
480 Player.p_y - Player.p_y / 10.0, A_SPECIFIC);
487 /************************************************************************
489 / FUNCTION NAME: displaystats()
491 / FUNCTION: print out important player statistics
493 / AUTHOR: E. A. Estes, 12/4/85
499 / MODULES CALLED: descrstatus(), descrlocation(), mvprintw()
501 / GLOBAL INPUTS: Users, Player
503 / GLOBAL OUTPUTS: none
506 / Important player statistics are printed on the screen.
508 *************************************************************************/
512 mvprintw(0, 0, "%s%s\n", Player.p_name, descrlocation(&Player, FALSE));
513 mvprintw(1, 0, "Level :%7.0f Energy :%9.0f(%9.0f) Mana :%9.0f Users:%3d\n",
514 Player.p_level, Player.p_energy, Player.p_maxenergy + Player.p_shield,
515 Player.p_mana, Users);
516 mvprintw(2, 0, "Quick :%3.0f(%3.0f) Strength:%9.0f(%9.0f) Gold :%9.0f %s\n",
517 Player.p_speed, Player.p_quickness + Player.p_quksilver, Player.p_might,
518 Player.p_strength + Player.p_sword, Player.p_gold, descrstatus(&Player));
521 /************************************************************************
523 / FUNCTION NAME: allstatslist()
525 / FUNCTION: show player items
527 / AUTHOR: E. A. Estes, 12/4/85
533 / MODULES CALLED: mvprintw(), descrtype()
535 / GLOBAL INPUTS: Player
537 / GLOBAL OUTPUTS: none
540 / Print out some player statistics of lesser importance.
542 *************************************************************************/
546 static char *flags[] = /* to print value of some bools */
552 mvprintw( 8, 0, "Type: %s\n", descrtype(&Player, FALSE));
554 mvprintw(10, 0, "Experience: %9.0f", Player.p_experience);
555 mvprintw(11, 0, "Brains : %9.0f", Player.p_brains);
556 mvprintw(12, 0, "Magic Lvl : %9.0f", Player.p_magiclvl);
557 mvprintw(13, 0, "Sin : %9.5f", Player.p_sin);
558 mvprintw(14, 0, "Poison : %9.5f", Player.p_poison);
559 mvprintw(15, 0, "Gems : %9.0f", Player.p_gems);
560 mvprintw(16, 0, "Age : %9d", Player.p_age);
561 mvprintw(10, 40, "Holy Water: %9d", Player.p_holywater);
562 mvprintw(11, 40, "Amulets : %9d", Player.p_amulets);
563 mvprintw(12, 40, "Charms : %9d", Player.p_charms);
564 mvprintw(13, 40, "Crowns : %9d", Player.p_crowns);
565 mvprintw(14, 40, "Shield : %9.0f", Player.p_shield);
566 mvprintw(15, 40, "Sword : %9.0f", Player.p_sword);
567 mvprintw(16, 40, "Quickslver: %9.0f", Player.p_quksilver);
569 mvprintw(18, 0, "Blessing: %s Ring: %s Virgin: %s Palantir: %s",
570 flags[Player.p_blessing], flags[Player.p_ring.ring_type != R_NONE],
571 flags[Player.p_virgin], flags[Player.p_palantir]);
574 /************************************************************************
576 / FUNCTION NAME: descrtype()
578 / FUNCTION: return a string specifying player type
580 / AUTHOR: E. A. Estes, 12/4/85
583 / struct player playerp - pointer to structure for player
584 / bool shortflag - set if short form is desired
586 / RETURN VALUE: pointer to string describing player type
588 / MODULES CALLED: strcpy()
590 / GLOBAL INPUTS: Databuf[]
592 / GLOBAL OUTPUTS: Databuf[]
595 / Return a string describing the player type.
596 / King, council, valar, supercedes other types.
597 / The first character of the string is '*' if the player
599 / If 'shortflag' is TRUE, return a 3 character string.
601 *************************************************************************/
604 descrtype(playerp, shortflag)
605 struct player *playerp;
608 int type; /* for caluculating result subscript */
609 static char *results[] = /* description table */
611 " Magic User", " MU",
616 " Experimento", " EX",
619 " Council of Wise", " CW",
625 type = playerp->p_type;
627 switch (playerp->p_specialtype)
630 type = playerp->p_type;
650 type *= 2; /* calculate offset */
657 /* use short descriptions */
660 if (playerp->p_crowns > 0)
662 strcpy(Databuf, results[type]);
667 return(results[type]);
670 /************************************************************************
672 / FUNCTION NAME: findname()
674 / FUNCTION: find location in player file of given name
676 / AUTHOR: E. A. Estes, 12/4/85
679 / char *name - name of character to look for
680 / struct player *playerp - pointer of structure to fill
682 / RETURN VALUE: location of player if found, -1 otherwise
684 / MODULES CALLED: fread(), fseek(), strcmp()
686 / GLOBAL INPUTS: Wizard, *Playersfp
688 / GLOBAL OUTPUTS: none
691 / Search the player file for the player of the given name.
692 / If player is found, fill structure with player data.
694 *************************************************************************/
697 findname(name, playerp)
699 struct player *playerp;
701 long loc = 0; /* location in the file */
703 fseek(Playersfp, 0L, 0);
704 while (fread((char *) playerp, SZ_PLAYERSTRUCT, 1, Playersfp) == 1)
706 if (strcmp(playerp->p_name, name) == 0)
708 if (playerp->p_status != S_NOTUSED || Wizard)
712 loc += SZ_PLAYERSTRUCT;
718 /************************************************************************
720 / FUNCTION NAME: allocrecord()
722 / FUNCTION: find space in the player file for a new character
724 / AUTHOR: E. A. Estes, 12/4/85
728 / RETURN VALUE: location of free space in file
730 / MODULES CALLED: initplayer(), writerecord(), fread(), fseek()
732 / GLOBAL INPUTS: Other, *Playersfp
734 / GLOBAL OUTPUTS: Player
737 / Search the player file for an unused entry. If none are found,
738 / make one at the end of the file.
740 *************************************************************************/
745 long loc = 0L; /* location in file */
747 fseek(Playersfp, 0L, 0);
748 while (fread((char *) &Other, SZ_PLAYERSTRUCT, 1, Playersfp) == 1)
750 if (Other.p_status == S_NOTUSED)
751 /* found an empty record */
754 loc += SZ_PLAYERSTRUCT;
757 /* make a new record */
759 Player.p_status = S_OFF;
760 writerecord(&Other, loc);
765 /************************************************************************
767 / FUNCTION NAME: freerecord()
769 / FUNCTION: free up a record on the player file
771 / AUTHOR: E. A. Estes, 2/7/86
774 / struct player playerp - pointer to structure to free
775 / long loc - location in file to free
779 / MODULES CALLED: writerecord()
781 / GLOBAL INPUTS: none
783 / GLOBAL OUTPUTS: none
786 / Mark structure as not used, and update player file.
788 *************************************************************************/
790 freerecord(playerp, loc)
791 struct player *playerp;
794 playerp->p_name[0] = CH_MARKDELETE;
795 playerp->p_status = S_NOTUSED;
796 writerecord(playerp, loc);
799 /************************************************************************
801 / FUNCTION NAME: leavegame()
803 / FUNCTION: leave game
805 / AUTHOR: E. A. Estes, 12/4/85
811 / MODULES CALLED: freerecord(), writerecord(), cleanup()
813 / GLOBAL INPUTS: Player, Fileloc
815 / GLOBAL OUTPUTS: Player
818 / Mark player as inactive, and cleanup.
819 / Do not save players below level 1.
821 *************************************************************************/
826 if (Player.p_level < 1.0)
827 /* delete character */
828 freerecord(&Player, Fileloc);
831 Player.p_status = S_OFF;
832 writerecord(&Player, Fileloc);
839 /************************************************************************
841 / FUNCTION NAME: death()
843 / FUNCTION: death routine
845 / AUTHOR: E. A. Estes, 12/4/85
848 / char *how - pointer to string describing cause of death
852 / MODULES CALLED: freerecord(), enterscore(), more(), exit(), fread(),
853 / fseek(), execl(), fopen(), floor(), wmove(), drandom(), wclear(), strcmp(),
854 / fwrite(), fflush(), printw(), strcpy(), fclose(), waddstr(), cleanup(),
855 / fprintf(), wrefresh(), getanswer(), descrtype()
857 / GLOBAL INPUTS: Curmonster, Wizard, Player, *stdscr, Fileloc, *Monstfp
859 / GLOBAL OUTPUTS: Player
862 / Kill off current player.
863 / Handle rings, and multiple lives.
864 / Print an appropriate message.
865 / Update scoreboard, lastdead, and let other players know about
866 / the demise of their comrade.
868 *************************************************************************/
873 FILE *fp; /* for updating various files */
875 static char *deathmesg[] =
876 /* add more messages here, if desired */
878 "You have been wounded beyond repair. ",
879 "You have been disemboweled. ",
880 "You've been mashed, mauled, and spit upon. (You're dead.)\n",
882 "You're a complete failure -- you've died!!\n",
883 "You have been dealt a fatal blow! "
888 if (strcmp(how, "Stupidity") != 0)
890 if (Player.p_level > 9999.0)
892 addstr("Characters must be retired upon reaching level 10000. Sorry.");
893 else if (Player.p_lives > 0)
896 addstr("You should be more cautious. You've been killed.\n");
897 printw("You only have %d more chance(s).\n", --Player.p_lives);
899 Player.p_energy = Player.p_maxenergy;
902 else if (Player.p_specialtype == SC_VALAR)
904 addstr("You had your chances, but Valar aren't totally\n");
905 addstr("immortal. You are now left to wither and die . . .\n");
907 Player.p_brains = Player.p_level / 25.0;
908 Player.p_energy = Player.p_maxenergy /= 5.0;
909 Player.p_quksilver = Player.p_sword = 0.0;
910 Player.p_specialtype = SC_COUNCIL;
913 else if (Player.p_ring.ring_inuse &&
914 (Player.p_ring.ring_type == R_DLREG || Player.p_ring.ring_type == R_NAZREG))
915 /* good ring in use - saved from death */
917 mvaddstr(4, 0, "Your ring saved you from death!\n");
919 Player.p_ring.ring_type = R_NONE;
920 Player.p_energy = Player.p_maxenergy / 12.0 + 1.0;
921 if (Player.p_crowns > 0)
925 else if (Player.p_ring.ring_type == R_BAD
926 || Player.p_ring.ring_type == R_SPOILED)
927 /* bad ring in possession; name idiot after player */
930 "Your ring has taken control of you and turned you into a monster!\n");
931 fseek(Monstfp, 13L * SZ_MONSTERSTRUCT, 0);
932 fread((char *) &Curmonster, SZ_MONSTERSTRUCT, 1, Monstfp);
933 strcpy(Curmonster.m_name, Player.p_name);
934 fseek(Monstfp, 13L * SZ_MONSTERSTRUCT, 0);
935 fwrite((char *) &Curmonster, SZ_MONSTERSTRUCT, 1, Monstfp);
940 enterscore(); /* update score board */
942 /* put info in last dead file */
943 fp = fopen(_PATH_LASTDEAD, "w");
944 fprintf(fp,"%s (%s, run by %s, level %.0f, killed by %s)",
945 Player.p_name, descrtype(&Player, TRUE),
946 Player.p_login, Player.p_level, how);
949 /* let other players know */
950 fp = fopen(_PATH_MESS, "w");
951 fprintf(fp, "%s was killed by %s.", Player.p_name, how);
954 freerecord(&Player, Fileloc);
958 addstr(deathmesg[(int) ROLL(0.0, (double) sizeof(deathmesg) / sizeof(char *))]);
959 addstr("Care to give it another try ? ");
960 ch = getanswer("NY", FALSE);
965 execl(_PATH_GAMEPROG, "phantasia", "-s",
966 (Wizard ? "-S": (char *) NULL), 0);
975 /************************************************************************
977 / FUNCTION NAME: writerecord()
979 / FUNCTION: update structure in player file
981 / AUTHOR: E. A. Estes, 12/4/85
984 / struct player *playerp - pointer to structure to write out
985 / long place - location in file to updata
989 / MODULES CALLED: fseek(), fwrite(), fflush()
991 / GLOBAL INPUTS: *Playersfp
993 / GLOBAL OUTPUTS: none
996 / Update location in player file with given structure.
998 *************************************************************************/
1000 writerecord(playerp, place)
1001 struct player *playerp;
1004 fseek(Playersfp, place, 0);
1005 fwrite((char *) playerp, SZ_PLAYERSTRUCT, 1, Playersfp);
1009 /************************************************************************
1011 / FUNCTION NAME: explevel()
1013 / FUNCTION: calculate level based upon experience
1015 / AUTHOR: E. A. Estes, 12/4/85
1018 / double experience - experience to calculate experience level from
1020 / RETURN VALUE: experience level
1022 / MODULES CALLED: pow(), floor()
1024 / GLOBAL INPUTS: none
1026 / GLOBAL OUTPUTS: none
1029 / Experience level is a geometric progression. This has been finely
1030 / tuned over the years, and probably should not be changed.
1032 *************************************************************************/
1035 explevel(experience)
1038 if (experience < 1.1e7)
1039 return(floor(pow((experience / 1000.0), 0.4875)));
1041 return(floor(pow((experience / 1250.0), 0.4865)));
1044 /************************************************************************
1046 / FUNCTION NAME: truncstring()
1048 / FUNCTION: truncate trailing blanks off a string
1050 / AUTHOR: E. A. Estes, 12/4/85
1053 / char *string - pointer to null terminated string
1055 / RETURN VALUE: none
1057 / MODULES CALLED: strlen()
1059 / GLOBAL INPUTS: none
1061 / GLOBAL OUTPUTS: none
1064 / Put nul characters in place of spaces at the end of the string.
1066 *************************************************************************/
1071 int length; /* length of string */
1073 length = strlen(string);
1074 while (string[--length] == ' ')
1075 string[length] = '\0';
1078 /************************************************************************
1080 / FUNCTION NAME: altercoordinates()
1082 / FUNCTION: Alter x, y coordinates and set/check location flags
1084 / AUTHOR: E. A. Estes, 12/16/85
1087 / double xnew, ynew - new x, y coordinates
1088 / int operation - operation to perform with coordinates
1090 / RETURN VALUE: none
1092 / MODULES CALLED: fabs(), floor(), drandom(), distance()
1094 / GLOBAL INPUTS: Circle, Beyond, Player
1096 / GLOBAL OUTPUTS: Marsh, Circle, Beyond, Throne, Player, Changed
1099 / This module is called whenever the player's coordinates are altered.
1100 / If the player is beyond the point of no return, he/she is forced
1103 *************************************************************************/
1105 altercoordinates(xnew, ynew, operation)
1112 case A_FORCED: /* move with no checks */
1115 case A_NEAR: /* pick random coordinates near */
1116 xnew = Player.p_x + ROLL(1.0, 5.0);
1117 ynew = Player.p_y - ROLL(1.0, 5.0);
1118 /* fall through for check */
1120 case A_SPECIFIC: /* just move player */
1121 if (Beyond && fabs(xnew) < D_BEYOND && fabs(ynew) < D_BEYOND)
1123 * cannot move back from point of no return
1124 * pick the largest coordinate to remain unchanged
1127 if (fabs(xnew) > fabs(ynew))
1128 xnew = SGN(Player.p_x) * MAX(fabs(Player.p_x), D_BEYOND);
1130 ynew = SGN(Player.p_y) * MAX(fabs(Player.p_y), D_BEYOND);
1134 case A_FAR: /* pick random coordinates far */
1135 xnew = Player.p_x + SGN(Player.p_x) * ROLL(50 * Circle, 250 * Circle);
1136 ynew = Player.p_y + SGN(Player.p_y) * ROLL(50 * Circle, 250 * Circle);
1140 /* now set location flags and adjust coordinates */
1141 Circle = CIRCLE(Player.p_x = floor(xnew), Player.p_y = floor(ynew));
1143 /* set up flags based upon location */
1144 Throne = Marsh = Beyond = FALSE;
1146 if (Player.p_x == 0.0 && Player.p_y == 0.0)
1148 else if (Circle < 35 && Circle >= 20)
1150 else if (MAX(fabs(Player.p_x), fabs(Player.p_y)) >= D_BEYOND)
1156 /************************************************************************
1158 / FUNCTION NAME: readrecord()
1160 / FUNCTION: read a player structure from file
1162 / AUTHOR: E. A. Estes, 12/4/85
1165 / struct player *playerp - pointer to structure to fill
1166 / int loc - location of record to read
1168 / RETURN VALUE: none
1170 / MODULES CALLED: fread(), fseek()
1172 / GLOBAL INPUTS: *Playersfp
1174 / GLOBAL OUTPUTS: none
1177 / Read structure information from player file.
1179 *************************************************************************/
1181 readrecord(playerp, loc)
1182 struct player *playerp;
1185 fseek(Playersfp, loc, 0);
1186 fread((char *) playerp, SZ_PLAYERSTRUCT, 1, Playersfp);
1189 /************************************************************************
1191 / FUNCTION NAME: adjuststats()
1193 / FUNCTION: adjust player statistics
1195 / AUTHOR: E. A. Estes, 12/4/85
1199 / RETURN VALUE: none
1201 / MODULES CALLED: death(), floor(), drandom(), explevel(), movelevel()
1203 / GLOBAL INPUTS: Player, *Statptr
1205 / GLOBAL OUTPUTS: Circle, Player, Timeout
1208 / Handle adjustment and maximums on various player characteristics.
1210 *************************************************************************/
1214 double dtemp; /* for temporary calculations */
1216 if (explevel(Player.p_experience) > Player.p_level)
1217 /* move one or more levels */
1220 if (Player.p_level > 5.0)
1224 if (Player.p_specialtype == SC_VALAR)
1226 Circle = Player.p_level / 5.0;
1228 /* calculate effective quickness */
1229 dtemp = ((Player.p_gold + Player.p_gems / 2.0) - 1000.0) / Statptr->c_goldtote
1231 dtemp = MAX(0.0, dtemp); /* gold slows player down */
1232 Player.p_speed = Player.p_quickness + Player.p_quksilver - dtemp;
1234 /* calculate effective strength */
1235 if (Player.p_poison > 0.0)
1236 /* poison makes player weaker */
1238 dtemp = 1.0 - Player.p_poison * Statptr->c_weakness / 800.0;
1239 dtemp = MAX(0.1, dtemp);
1243 Player.p_might = dtemp * Player.p_strength + Player.p_sword;
1245 /* insure that important things are within limits */
1246 Player.p_quksilver = MIN(99.0, Player.p_quksilver);
1247 Player.p_mana = MIN(Player.p_mana,
1248 Player.p_level * Statptr->c_maxmana + 1000.0);
1249 Player.p_brains = MIN(Player.p_brains,
1250 Player.p_level * Statptr->c_maxbrains + 200.0);
1251 Player.p_charms = MIN(Player.p_charms, Player.p_level + 10.0);
1254 * some implementations have problems with floating point compare
1255 * we work around it with this stuff
1257 Player.p_gold = floor(Player.p_gold) + 0.1;
1258 Player.p_gems = floor(Player.p_gems) + 0.1;
1259 Player.p_mana = floor(Player.p_mana) + 0.1;
1261 if (Player.p_ring.ring_type != R_NONE)
1262 /* do ring things */
1265 Player.p_energy = Player.p_maxenergy + Player.p_shield;
1267 if (Player.p_ring.ring_duration <= 0)
1268 /* clean up expired rings */
1269 switch (Player.p_ring.ring_type)
1271 case R_BAD: /* ring drives player crazy */
1272 Player.p_ring.ring_type = R_SPOILED;
1273 Player.p_ring.ring_duration = (short) ROLL(10.0, 25.0);
1276 case R_NAZREG: /* ring disappears */
1277 Player.p_ring.ring_type = R_NONE;
1280 case R_SPOILED: /* ring kills player */
1281 death("A cursed ring");
1284 case R_DLREG: /* this ring doesn't expire */
1285 Player.p_ring.ring_duration = 0;
1290 if (Player.p_age / N_AGE > Player.p_degenerated)
1291 /* age player slightly */
1293 ++Player.p_degenerated;
1294 if (Player.p_quickness > 23.0)
1295 Player.p_quickness *= 0.99;
1296 Player.p_strength *= 0.97;
1297 Player.p_brains *= 0.95;
1298 Player.p_magiclvl *= 0.97;
1299 Player.p_maxenergy *= 0.95;
1300 Player.p_quksilver *= 0.95;
1301 Player.p_sword *= 0.93;
1302 Player.p_shield *= 0.93;
1306 /************************************************************************
1308 / FUNCTION NAME: initplayer()
1310 / FUNCTION: initialize a character
1312 / AUTHOR: E. A. Estes, 12/4/85
1315 / struct player *playerp - pointer to structure to init
1317 / RETURN VALUE: none
1319 / MODULES CALLED: floor(), drandom()
1321 / GLOBAL INPUTS: none
1323 / GLOBAL OUTPUTS: none
1326 / Put a bunch of default values in the given structure.
1328 *************************************************************************/
1331 struct player *playerp;
1333 playerp->p_experience =
1335 playerp->p_strength =
1339 playerp->p_maxenergy =
1341 playerp->p_quickness =
1342 playerp->p_quksilver =
1344 playerp->p_magiclvl =
1350 playerp->p_1scratch =
1351 playerp->p_2scratch = 0.0;
1353 playerp->p_gold = ROLL(50.0, 75.0) + 0.1; /* give some gold */
1355 playerp->p_x = ROLL(-125.0, 251.0);
1356 playerp->p_y = ROLL(-125.0, 251.0); /* give random x, y */
1359 playerp->p_ring.ring_type = R_NONE;
1360 playerp->p_ring.ring_duration = 0;
1361 playerp->p_ring.ring_inuse = FALSE;
1363 playerp->p_age = 0L;
1365 playerp->p_degenerated = 1; /* don't degenerate initially */
1367 playerp->p_type = C_FIGHTER; /* default */
1368 playerp->p_specialtype = SC_NONE;
1372 playerp->p_amulets =
1373 playerp->p_holywater =
1374 playerp->p_lastused = 0;
1375 playerp->p_status = S_NOTUSED;
1376 playerp->p_tampered = T_OFF;
1377 playerp->p_istat = I_OFF;
1379 playerp->p_palantir =
1380 playerp->p_blessing =
1382 playerp->p_blindness = FALSE;
1384 playerp->p_name[0] =
1385 playerp->p_password[0] =
1386 playerp->p_login[0] = '\0';
1389 /************************************************************************
1391 / FUNCTION NAME: readmessage()
1393 / FUNCTION: read message from other players
1395 / AUTHOR: E. A. Estes, 12/4/85
1399 / RETURN VALUE: none
1401 / MODULES CALLED: fseek(), fgets(), wmove(), waddstr(), wclrtoeol()
1403 / GLOBAL INPUTS: *stdscr, Databuf[], *Messagefp
1405 / GLOBAL OUTPUTS: none
1408 / If there is a message from other players, print it.
1410 *************************************************************************/
1416 fseek(Messagefp, 0L, 0);
1417 if (fgets(Databuf, SZ_DATABUF, Messagefp) != NULL)
1421 /************************************************************************
1423 / FUNCTION NAME: error()
1425 / FUNCTION: process evironment error
1427 / AUTHOR: E. A. Estes, 12/4/85
1430 / char *whichfile - pointer to name of file which caused error
1432 / RETURN VALUE: none
1434 / MODULES CALLED: wclear(), cleanup()
1436 / GLOBAL INPUTS: errno, *stdscr, printw(), printf(), Windows
1438 / GLOBAL OUTPUTS: none
1441 / Print message about offending file, and exit.
1443 *************************************************************************/
1448 int (*funcp) (const char *, ...);
1452 funcp = (void *)printw;
1458 (*funcp)("An unrecoverable error has occurred reading %s. (errno = %d)\n", whichfile, errno);
1459 (*funcp)("Please run 'setup' to determine the problem.\n");
1464 /************************************************************************
1466 / FUNCTION NAME: distance()
1468 / FUNCTION: calculate distance between two points
1470 / AUTHOR: E. A. Estes, 12/4/85
1473 / double x1, y1 - x, y coordinates of first point
1474 / double x2, y2 - x, y coordinates of second point
1476 / RETURN VALUE: distance between the two points
1478 / MODULES CALLED: sqrt()
1480 / GLOBAL INPUTS: none
1482 / GLOBAL OUTPUTS: none
1485 / This function is provided because someone's hypot() library function
1486 / fails if x1 == x2 && y1 == y2.
1488 *************************************************************************/
1491 distance(x1, x2, y1, y2)
1492 double x1, x2, y1, y2;
1494 double deltax, deltay;
1498 return(sqrt(deltax * deltax + deltay * deltay));
1502 /************************************************************************
1504 / FUNCTION NAME: ill_sig()
1506 / FUNCTION: exit upon trapping an illegal signal
1508 / AUTHOR: E. A. Estes, 12/4/85
1511 / int whichsig - signal which occured to cause jump to here
1513 / RETURN VALUE: none
1515 / MODULES CALLED: wclear(), printw(), cleanup()
1517 / GLOBAL INPUTS: *stdscr
1519 / GLOBAL OUTPUTS: none
1522 / When an illegal signal is caught, print a message, and cleanup.
1524 *************************************************************************/
1530 if (!(whichsig == SIGINT || whichsig == SIGQUIT))
1531 printw("Error: caught signal # %d.\n", whichsig);
1536 /************************************************************************
1538 / FUNCTION NAME: descrstatus()
1540 / FUNCTION: return a string describing the player status
1542 / AUTHOR: E. A. Estes, 3/3/86
1545 / struct player playerp - pointer to player structure to describe
1547 / RETURN VALUE: string describing player's status
1549 / MODULES CALLED: none
1551 / GLOBAL INPUTS: none
1553 / GLOBAL OUTPUTS: none
1556 / Return verbal description of player status.
1557 / If player status is S_PLAYING, check for low energy and blindness.
1559 *************************************************************************/
1562 descrstatus(playerp)
1563 struct player *playerp;
1565 switch (playerp->p_status)
1568 if (playerp->p_energy < 0.2 * (playerp->p_maxenergy + playerp->p_shield))
1569 return("Low Energy");
1570 else if (playerp->p_blindness)
1579 return("In Battle");
1582 return("Encounter");
1598 /************************************************************************
1600 / FUNCTION NAME: drandom()
1602 / FUNCTION: return a random floating point number from 0.0 < 1.0
1604 / AUTHOR: E. A. Estes, 2/7/86
1608 / RETURN VALUE: none
1610 / MODULES CALLED: random()
1612 / GLOBAL INPUTS: none
1614 / GLOBAL OUTPUTS: none
1617 / Convert random integer from library routine into a floating
1618 / point number, and divide by the largest possible random number.
1619 / We mask large integers with 32767 to handle sites that return
1620 / 31 bit random integers.
1622 *************************************************************************/
1627 if (sizeof(int) != 2)
1628 /* use only low bits */
1629 return((double) (random() & 0x7fff) / 32768.0);
1631 return((double) random() / 32768.0);
1634 /************************************************************************
1636 / FUNCTION NAME: collecttaxes()
1638 / FUNCTION: collect taxes from current player
1640 / AUTHOR: E. A. Estes, 2/7/86
1643 / double gold - amount of gold to tax
1644 / double gems - amount of gems to tax
1646 / RETURN VALUE: none
1648 / MODULES CALLED: fread(), fseek(), fopen(), floor(), fwrite(), fclose()
1650 / GLOBAL INPUTS: Player
1652 / GLOBAL OUTPUTS: Player
1655 / Pay taxes on gold and gems. If the player does not have enough
1656 / gold to pay taxes on the added gems, convert some gems to gold.
1657 / Add taxes to tax data base; add remaining gold and gems to
1660 *************************************************************************/
1662 collecttaxes(gold, gems)
1666 FILE *fp; /* to update Goldfile */
1667 double dtemp; /* for temporary calculations */
1668 double taxes; /* tax liability */
1671 Player.p_gold += gold;
1672 Player.p_gems += gems;
1674 /* calculate tax liability */
1675 taxes = N_TAXAMOUNT / 100.0 * (N_GEMVALUE * gems + gold);
1677 if (Player.p_gold < taxes)
1678 /* not enough gold to pay taxes, must convert some gems to gold */
1680 dtemp = floor(taxes / N_GEMVALUE + 1.0); /* number of gems to convert */
1682 if (Player.p_gems >= dtemp)
1683 /* player has enough to convert */
1685 Player.p_gems -= dtemp;
1686 Player.p_gold += dtemp * N_GEMVALUE;
1689 /* take everything; this should never happen */
1691 Player.p_gold += Player.p_gems * N_GEMVALUE;
1692 Player.p_gems = 0.0;
1693 taxes = Player.p_gold;
1697 Player.p_gold -= taxes;
1699 if ((fp = fopen(_PATH_GOLD, "r+")) != NULL)
1703 fread((char *) &dtemp, sizeof(double), 1, fp);
1704 dtemp += floor(taxes);
1706 fwrite((char *) &dtemp, sizeof(double), 1, fp);