]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - phantasia/misc.c
sprinkle some more const
[bsdgames-darwin.git] / phantasia / misc.c
1 /* $NetBSD: misc.c,v 1.17 2009/05/25 23:14:33 dholland Exp $ */
2
3 /*
4 * misc.c Phantasia miscellaneous support routines
5 */
6
7 #include "include.h"
8 #undef bool
9 #include <curses.h>
10
11
12 void
13 movelevel(void)
14 {
15 const struct charstats *statptr; /* for pointing into Stattable */
16 double new; /* new level */
17 double inc; /* increment between new and old levels */
18
19 Changed = TRUE;
20
21 if (Player.p_type == C_EXPER)
22 /* roll a type to use for increment */
23 statptr = &Stattable[(int) ROLL(C_MAGIC, C_HALFLING - C_MAGIC + 1)];
24 else
25 statptr = Statptr;
26
27 new = explevel(Player.p_experience);
28 inc = new - Player.p_level;
29 Player.p_level = new;
30
31 /* add increments to statistics */
32 Player.p_strength += statptr->c_strength.increase * inc;
33 Player.p_mana += statptr->c_mana.increase * inc;
34 Player.p_brains += statptr->c_brains.increase * inc;
35 Player.p_magiclvl += statptr->c_magiclvl.increase * inc;
36 Player.p_maxenergy += statptr->c_energy.increase * inc;
37
38 /* rest to maximum upon reaching new level */
39 Player.p_energy = Player.p_maxenergy + Player.p_shield;
40
41 if (Player.p_crowns > 0 && Player.p_level >= 1000.0)
42 /* no longer able to be king -- turn crowns into cash */
43 {
44 Player.p_gold += ((double) Player.p_crowns) * 5000.0;
45 Player.p_crowns = 0;
46 }
47 if (Player.p_level >= 3000.0 && Player.p_specialtype < SC_COUNCIL)
48 /* make a member of the council */
49 {
50 mvaddstr(6, 0, "You have made it to the Council of the Wise.\n");
51 addstr("Good Luck on your search for the Holy Grail.\n");
52
53 Player.p_specialtype = SC_COUNCIL;
54
55 /* no rings for council and above */
56 Player.p_ring.ring_type = R_NONE;
57 Player.p_ring.ring_duration = 0;
58
59 Player.p_lives = 3; /* three extra lives */
60 }
61 if (Player.p_level > 9999.0 && Player.p_specialtype != SC_VALAR)
62 death("Old age");
63 }
64
65 const char *
66 descrlocation(struct player *playerp, phbool shortflag)
67 {
68 double circle; /* corresponding circle for coordinates */
69 int quadrant; /* quandrant of grid */
70 const char *label; /* pointer to place name */
71 static const char *const nametable[4][4] = /* names of places */
72 {
73 {"Anorien", "Ithilien", "Rohan", "Lorien"},
74 {"Gondor", "Mordor", "Dunland", "Rovanion"},
75 {"South Gondor", "Khand", "Eriador", "The Iron Hills"},
76 {"Far Harad", "Near Harad", "The Northern Waste", "Rhun"}
77 };
78
79 if (playerp->p_specialtype == SC_VALAR)
80 return (" is in Valhala");
81 else
82 if ((circle = CIRCLE(playerp->p_x, playerp->p_y)) >= 1000.0) {
83 if (MAX(fabs(playerp->p_x), fabs(playerp->p_y)) > D_BEYOND)
84 label = "The Point of No Return";
85 else
86 label = "The Ashen Mountains";
87 } else
88 if (circle >= 55)
89 label = "Morannon";
90 else
91 if (circle >= 35)
92 label = "Kennaquahair";
93 else
94 if (circle >= 20)
95 label = "The Dead Marshes";
96 else
97 if (circle >= 9)
98 label = "The Outer Waste";
99 else
100 if (circle >= 5)
101 label = "The Moors Adventurous";
102 else {
103 if (playerp->p_x == 0.0 && playerp->p_y == 0.0)
104 label = "The Lord's Chamber";
105 else {
106 /* this
107 *
108 * expr
109 * essi
110 * on
111 * is
112 * spli
113 * t
114 * to
115 * prev
116 * ent
117 * comp
118 * iler
119 *
120 * loop
121 *
122 * with
123 *
124 * some
125 *
126 * comp
127 * iler
128 * s */
129 quadrant = ((playerp->p_x > 0.0) ? 1 : 0);
130 quadrant += ((playerp->p_y >= 0.0) ? 2 : 0);
131 label = nametable[((int) circle) - 1][quadrant];
132 }
133 }
134
135 if (shortflag)
136 snprintf(Databuf, SZ_DATABUF, "%.29s", label);
137 else
138 snprintf(Databuf, SZ_DATABUF,
139 " is in %s (%.0f,%.0f)",
140 label, playerp->p_x, playerp->p_y);
141
142 return (Databuf);
143 }
144
145 void
146 tradingpost(void)
147 {
148 double numitems; /* number of items to purchase */
149 double cost; /* cost of purchase */
150 double blessingcost; /* cost of blessing */
151 int ch; /* input */
152 int size; /* size of the trading post */
153 int loop; /* loop counter */
154 int cheat = 0; /* number of times player has tried to cheat */
155 bool dishonest = FALSE; /* set when merchant is dishonest */
156
157 Player.p_status = S_TRADING;
158 writerecord(&Player, Fileloc);
159
160 clear();
161 addstr("You are at a trading post. All purchases must be made with gold.");
162
163 size = sqrt(fabs(Player.p_x / 100)) + 1;
164 size = MIN(7, size);
165
166 /* set up cost of blessing */
167 blessingcost = 1000.0 * (Player.p_level + 5.0);
168
169 /* print Menu */
170 move(7, 0);
171 for (loop = 0; loop < size; ++loop)
172 /* print Menu */
173 {
174 if (loop == 6)
175 cost = blessingcost;
176 else
177 cost = Menu[loop].cost;
178 printw("(%d) %-12s: %6.0f\n", loop + 1, Menu[loop].item, cost);
179 }
180
181 mvprintw(5, 0, "L:Leave P:Purchase S:Sell Gems ? ");
182
183 for (;;) {
184 adjuststats(); /* truncate any bad values */
185
186 /* print some important statistics */
187 mvprintw(1, 0, "Gold: %9.0f Gems: %9.0f Level: %6.0f Charms: %6d\n",
188 Player.p_gold, Player.p_gems, Player.p_level, Player.p_charms);
189 printw("Shield: %9.0f Sword: %9.0f Quicksilver:%3.0f Blessed: %s\n",
190 Player.p_shield, Player.p_sword, Player.p_quksilver,
191 (Player.p_blessing ? " True" : "False"));
192 printw("Brains: %9.0f Mana: %9.0f", Player.p_brains, Player.p_mana);
193
194 move(5, 36);
195 ch = getanswer("LPS", FALSE);
196 move(15, 0);
197 clrtobot();
198 switch (ch) {
199 case 'L': /* leave */
200 case '\n':
201 altercoordinates(0.0, 0.0, A_NEAR);
202 return;
203
204 case 'P': /* make purchase */
205 mvaddstr(15, 0, "What what would you like to buy ? ");
206 ch = getanswer(" 1234567", FALSE);
207 move(15, 0);
208 clrtoeol();
209
210 if (ch - '0' > size)
211 addstr("Sorry, this merchant doesn't have that.");
212 else
213 switch (ch) {
214 case '1':
215 printw("Mana is one per %.0f gold piece. How many do you want (%.0f max) ? ",
216 Menu[0].cost, floor(Player.p_gold / Menu[0].cost));
217 cost = (numitems = floor(infloat())) * Menu[0].cost;
218
219 if (cost > Player.p_gold || numitems < 0)
220 ++cheat;
221 else {
222 cheat = 0;
223 Player.p_gold -= cost;
224 if (drandom() < 0.02)
225 dishonest = TRUE;
226 else
227 Player.p_mana += numitems;
228 }
229 break;
230
231 case '2':
232 printw("Shields are %.0f per +1. How many do you want (%.0f max) ? ",
233 Menu[1].cost, floor(Player.p_gold / Menu[1].cost));
234 cost = (numitems = floor(infloat())) * Menu[1].cost;
235
236 if (numitems == 0.0)
237 break;
238 else
239 if (cost > Player.p_gold || numitems < 0)
240 ++cheat;
241 else
242 if (numitems < Player.p_shield)
243 NOBETTER();
244 else {
245 cheat = 0;
246 Player.p_gold -= cost;
247 if (drandom() < 0.02)
248 dishonest = TRUE;
249 else
250 Player.p_shield = numitems;
251 }
252 break;
253
254 case '3':
255 printw("A book costs %.0f gp. How many do you want (%.0f max) ? ",
256 Menu[2].cost, floor(Player.p_gold / Menu[2].cost));
257 cost = (numitems = floor(infloat())) * Menu[2].cost;
258
259 if (cost > Player.p_gold || numitems < 0)
260 ++cheat;
261 else {
262 cheat = 0;
263 Player.p_gold -= cost;
264 if (drandom() < 0.02)
265 dishonest = TRUE;
266 else
267 if (drandom() * numitems > Player.p_level / 10.0
268 && numitems != 1) {
269 printw("\nYou blew your mind!\n");
270 Player.p_brains /= 5;
271 } else {
272 Player.p_brains += floor(numitems) * ROLL(20, 8);
273 }
274 }
275 break;
276
277 case '4':
278 printw("Swords are %.0f gp per +1. How many + do you want (%.0f max) ? ",
279 Menu[3].cost, floor(Player.p_gold / Menu[3].cost));
280 cost = (numitems = floor(infloat())) * Menu[3].cost;
281
282 if (numitems == 0.0)
283 break;
284 else
285 if (cost > Player.p_gold || numitems < 0)
286 ++cheat;
287 else
288 if (numitems < Player.p_sword)
289 NOBETTER();
290 else {
291 cheat = 0;
292 Player.p_gold -= cost;
293 if (drandom() < 0.02)
294 dishonest = TRUE;
295 else
296 Player.p_sword = numitems;
297 }
298 break;
299
300 case '5':
301 printw("A charm costs %.0f gp. How many do you want (%.0f max) ? ",
302 Menu[4].cost, floor(Player.p_gold / Menu[4].cost));
303 cost = (numitems = floor(infloat())) * Menu[4].cost;
304
305 if (cost > Player.p_gold || numitems < 0)
306 ++cheat;
307 else {
308 cheat = 0;
309 Player.p_gold -= cost;
310 if (drandom() < 0.02)
311 dishonest = TRUE;
312 else
313 Player.p_charms += numitems;
314 }
315 break;
316
317 case '6':
318 printw("Quicksilver is %.0f gp per +1. How many + do you want (%.0f max) ? ",
319 Menu[5].cost, floor(Player.p_gold / Menu[5].cost));
320 cost = (numitems = floor(infloat())) * Menu[5].cost;
321
322 if (numitems == 0.0)
323 break;
324 else
325 if (cost > Player.p_gold || numitems < 0)
326 ++cheat;
327 else
328 if (numitems < Player.p_quksilver)
329 NOBETTER();
330 else {
331 cheat = 0;
332 Player.p_gold -= cost;
333 if (drandom() < 0.02)
334 dishonest = TRUE;
335 else
336 Player.p_quksilver = numitems;
337 }
338 break;
339
340 case '7':
341 if (Player.p_blessing) {
342 addstr("You already have a blessing.");
343 break;
344 }
345 printw("A blessing requires a %.0f gp donation. Still want one ? ", blessingcost);
346 ch = getanswer("NY", FALSE);
347
348 if (ch == 'Y') {
349 if (Player.p_gold < blessingcost)
350 ++cheat;
351 else {
352 cheat = 0;
353 Player.p_gold -= blessingcost;
354 if (drandom() < 0.02)
355 dishonest = TRUE;
356 else
357 Player.p_blessing = TRUE;
358 }
359 }
360 break;
361 }
362 break;
363
364 case 'S': /* sell gems */
365 mvprintw(15, 0, "A gem is worth %.0f gp. How many do you want to sell (%.0f max) ? ",
366 (double) N_GEMVALUE, Player.p_gems);
367 numitems = floor(infloat());
368
369 if (numitems > Player.p_gems || numitems < 0)
370 ++cheat;
371 else {
372 cheat = 0;
373 Player.p_gems -= numitems;
374 Player.p_gold += numitems * N_GEMVALUE;
375 }
376 }
377
378 if (cheat == 1)
379 mvaddstr(17, 0, "Come on, merchants aren't stupid. Stop cheating.\n");
380 else
381 if (cheat == 2) {
382 mvaddstr(17, 0, "You had your chance. This merchant happens to be\n");
383 printw("a %.0f level magic user, and you made %s mad!\n",
384 ROLL(Circle * 20.0, 40.0), (drandom() < 0.5) ? "him" : "her");
385 altercoordinates(0.0, 0.0, A_FAR);
386 Player.p_energy /= 2.0;
387 ++Player.p_sin;
388 more(23);
389 return;
390 } else
391 if (dishonest) {
392 mvaddstr(17, 0, "The merchant stole your money!");
393 refresh();
394 altercoordinates(Player.p_x - Player.p_x / 10.0,
395 Player.p_y - Player.p_y / 10.0, A_SPECIFIC);
396 sleep(2);
397 return;
398 }
399 }
400 }
401
402 void
403 displaystats(void)
404 {
405 mvprintw(0, 0, "%s%s\n", Player.p_name, descrlocation(&Player, FALSE));
406 mvprintw(1, 0, "Level :%7.0f Energy :%9.0f(%9.0f) Mana :%9.0f Users:%3d\n",
407 Player.p_level, Player.p_energy, Player.p_maxenergy + Player.p_shield,
408 Player.p_mana, Users);
409 mvprintw(2, 0, "Quick :%3.0f(%3.0f) Strength:%9.0f(%9.0f) Gold :%9.0f %s\n",
410 Player.p_speed, Player.p_quickness + Player.p_quksilver, Player.p_might,
411 Player.p_strength + Player.p_sword, Player.p_gold, descrstatus(&Player));
412 }
413
414 void
415 allstatslist(void)
416 {
417 static const char *const flags[] = /* to print value of some bools */
418 {
419 "False",
420 " True"
421 };
422
423 mvprintw(8, 0, "Type: %s\n", descrtype(&Player, FALSE));
424
425 mvprintw(10, 0, "Experience: %9.0f", Player.p_experience);
426 mvprintw(11, 0, "Brains : %9.0f", Player.p_brains);
427 mvprintw(12, 0, "Magic Lvl : %9.0f", Player.p_magiclvl);
428 mvprintw(13, 0, "Sin : %9.5f", Player.p_sin);
429 mvprintw(14, 0, "Poison : %9.5f", Player.p_poison);
430 mvprintw(15, 0, "Gems : %9.0f", Player.p_gems);
431 mvprintw(16, 0, "Age : %9ld", Player.p_age);
432 mvprintw(10, 40, "Holy Water: %9d", Player.p_holywater);
433 mvprintw(11, 40, "Amulets : %9d", Player.p_amulets);
434 mvprintw(12, 40, "Charms : %9d", Player.p_charms);
435 mvprintw(13, 40, "Crowns : %9d", Player.p_crowns);
436 mvprintw(14, 40, "Shield : %9.0f", Player.p_shield);
437 mvprintw(15, 40, "Sword : %9.0f", Player.p_sword);
438 mvprintw(16, 40, "Quickslver: %9.0f", Player.p_quksilver);
439
440 mvprintw(18, 0, "Blessing: %s Ring: %s Virgin: %s Palantir: %s",
441 flags[(int)Player.p_blessing],
442 flags[Player.p_ring.ring_type != R_NONE],
443 flags[(int)Player.p_virgin],
444 flags[(int)Player.p_palantir]);
445 }
446
447 const char *
448 descrtype(struct player *playerp, phbool shortflag)
449 {
450 int type; /* for caluculating result subscript */
451 static const char *const results[] =/* description table */
452 {
453 " Magic User", " MU",
454 " Fighter", " F ",
455 " Elf", " E ",
456 " Dwarf", " D ",
457 " Halfling", " H ",
458 " Experimento", " EX",
459 " Super", " S ",
460 " King", " K ",
461 " Council of Wise", " CW",
462 " Ex-Valar", " EV",
463 " Valar", " V ",
464 " ? ", " ? "
465 };
466
467 type = playerp->p_type;
468
469 switch (playerp->p_specialtype) {
470 case SC_NONE:
471 type = playerp->p_type;
472 break;
473
474 case SC_KING:
475 type = 7;
476 break;
477
478 case SC_COUNCIL:
479 type = 8;
480 break;
481
482 case SC_EXVALAR:
483 type = 9;
484 break;
485
486 case SC_VALAR:
487 type = 10;
488 break;
489 }
490
491 type *= 2; /* calculate offset */
492
493 if (type > 20)
494 /* error */
495 type = 22;
496
497 if (shortflag)
498 /* use short descriptions */
499 ++type;
500
501 if (playerp->p_crowns > 0) {
502 strcpy(Databuf, results[type]);
503 Databuf[0] = '*';
504 return (Databuf);
505 } else
506 return (results[type]);
507 }
508
509 long
510 findname(const char *name, struct player *playerp)
511 {
512 long loc = 0; /* location in the file */
513
514 fseek(Playersfp, 0L, SEEK_SET);
515 while (fread((char *) playerp, SZ_PLAYERSTRUCT, 1, Playersfp) == 1) {
516 if (strcmp(playerp->p_name, name) == 0) {
517 if (playerp->p_status != S_NOTUSED || Wizard)
518 /* found it */
519 return (loc);
520 }
521 loc += SZ_PLAYERSTRUCT;
522 }
523
524 return (-1);
525 }
526
527 long
528 allocrecord(void)
529 {
530 long loc = 0L; /* location in file */
531
532 fseek(Playersfp, 0L, SEEK_SET);
533 while (fread((char *) &Other, SZ_PLAYERSTRUCT, 1, Playersfp) == 1) {
534 if (Other.p_status == S_NOTUSED)
535 /* found an empty record */
536 return (loc);
537 else
538 loc += SZ_PLAYERSTRUCT;
539 }
540
541 /* make a new record */
542 initplayer(&Other);
543 Player.p_status = S_OFF;
544 writerecord(&Other, loc);
545
546 return (loc);
547 }
548
549 void
550 freerecord(struct player *playerp, long loc)
551 {
552 playerp->p_name[0] = CH_MARKDELETE;
553 playerp->p_status = S_NOTUSED;
554 writerecord(playerp, loc);
555 }
556
557 void
558 leavegame(void)
559 {
560
561 if (Player.p_level < 1.0)
562 /* delete character */
563 freerecord(&Player, Fileloc);
564 else {
565 Player.p_status = S_OFF;
566 writerecord(&Player, Fileloc);
567 }
568
569 cleanup(TRUE);
570 /* NOTREACHED */
571 }
572
573 void
574 death(const char *how)
575 {
576 FILE *fp; /* for updating various files */
577 int ch; /* input */
578 static const char *const deathmesg[] =
579 /* add more messages here, if desired */
580 {
581 "You have been wounded beyond repair. ",
582 "You have been disemboweled. ",
583 "You've been mashed, mauled, and spit upon. (You're dead.)\n",
584 "You died! ",
585 "You're a complete failure -- you've died!!\n",
586 "You have been dealt a fatal blow! "
587 };
588
589 clear();
590
591 if (strcmp(how, "Stupidity") != 0) {
592 if (Player.p_level > 9999.0)
593 /* old age */
594 addstr("Characters must be retired upon reaching level 10000. Sorry.");
595 else
596 if (Player.p_lives > 0)
597 /* extra lives */
598 {
599 addstr("You should be more cautious. You've been killed.\n");
600 printw("You only have %d more chance(s).\n", --Player.p_lives);
601 more(3);
602 Player.p_energy = Player.p_maxenergy;
603 return;
604 } else
605 if (Player.p_specialtype == SC_VALAR) {
606 addstr("You had your chances, but Valar aren't totally\n");
607 addstr("immortal. You are now left to wither and die . . .\n");
608 more(3);
609 Player.p_brains = Player.p_level / 25.0;
610 Player.p_energy = Player.p_maxenergy /= 5.0;
611 Player.p_quksilver = Player.p_sword = 0.0;
612 Player.p_specialtype = SC_COUNCIL;
613 return;
614 } else
615 if (Player.p_ring.ring_inuse &&
616 (Player.p_ring.ring_type == R_DLREG || Player.p_ring.ring_type == R_NAZREG))
617 /* good ring in use - saved
618 * from death */
619 {
620 mvaddstr(4, 0, "Your ring saved you from death!\n");
621 refresh();
622 Player.p_ring.ring_type = R_NONE;
623 Player.p_energy = Player.p_maxenergy / 12.0 + 1.0;
624 if (Player.p_crowns > 0)
625 --Player.p_crowns;
626 return;
627 } else
628 if (Player.p_ring.ring_type == R_BAD
629 || Player.p_ring.ring_type == R_SPOILED)
630 /* bad ring in
631 * possession; name
632 * idiot after player */
633 {
634 mvaddstr(4, 0,
635 "Your ring has taken control of you and turned you into a monster!\n");
636 fseek(Monstfp, 13L * SZ_MONSTERSTRUCT, SEEK_SET);
637 fread((char *) &Curmonster, SZ_MONSTERSTRUCT, 1, Monstfp);
638 strcpy(Curmonster.m_name, Player.p_name);
639 fseek(Monstfp, 13L * SZ_MONSTERSTRUCT, SEEK_SET);
640 fwrite((char *) &Curmonster, SZ_MONSTERSTRUCT, 1, Monstfp);
641 fflush(Monstfp);
642 }
643 }
644 enterscore(); /* update score board */
645
646 /* put info in last dead file */
647 fp = fopen(_PATH_LASTDEAD, "w");
648 fprintf(fp, "%s (%s, run by %s, level %.0f, killed by %s)",
649 Player.p_name, descrtype(&Player, TRUE),
650 Player.p_login, Player.p_level, how);
651 fclose(fp);
652
653 /* let other players know */
654 fp = fopen(_PATH_MESS, "w");
655 fprintf(fp, "%s was killed by %s.", Player.p_name, how);
656 fclose(fp);
657
658 freerecord(&Player, Fileloc);
659
660 clear();
661 move(10, 0);
662 addstr(deathmesg[(int) ROLL(0.0, (double) sizeof(deathmesg) / sizeof(char *))]);
663 addstr("Care to give it another try ? ");
664 ch = getanswer("NY", FALSE);
665
666 if (ch == 'Y') {
667 cleanup(FALSE);
668 execl(_PATH_GAMEPROG, "phantasia", "-s",
669 (Wizard ? "-S" : (char *) NULL), (char *) NULL);
670 exit(0);
671 /* NOTREACHED */
672 }
673 cleanup(TRUE);
674 /* NOTREACHED */
675 }
676
677 void
678 writerecord(struct player *playerp, long place)
679 {
680 fseek(Playersfp, place, SEEK_SET);
681 fwrite((char *) playerp, SZ_PLAYERSTRUCT, 1, Playersfp);
682 fflush(Playersfp);
683 }
684
685 double
686 explevel(double experience)
687 {
688 if (experience < 1.1e7)
689 return (floor(pow((experience / 1000.0), 0.4875)));
690 else
691 return (floor(pow((experience / 1250.0), 0.4865)));
692 }
693
694 void
695 truncstring(char *string)
696 {
697 int length; /* length of string */
698
699 length = strlen(string);
700 while (string[--length] == ' ')
701 string[length] = '\0';
702 }
703
704 void
705 altercoordinates(double xnew, double ynew, int operation)
706 {
707 switch (operation) {
708 case A_FORCED: /* move with no checks */
709 break;
710
711 case A_NEAR: /* pick random coordinates near */
712 xnew = Player.p_x + ROLL(1.0, 5.0);
713 ynew = Player.p_y - ROLL(1.0, 5.0);
714 /* fall through for check */
715
716 case A_SPECIFIC: /* just move player */
717 if (Beyond && fabs(xnew) < D_BEYOND && fabs(ynew) < D_BEYOND)
718 /*
719 * cannot move back from point of no return
720 * pick the largest coordinate to remain unchanged
721 */
722 {
723 if (fabs(xnew) > fabs(ynew))
724 xnew = SGN(Player.p_x) * MAX(fabs(Player.p_x), D_BEYOND);
725 else
726 ynew = SGN(Player.p_y) * MAX(fabs(Player.p_y), D_BEYOND);
727 }
728 break;
729
730 case A_FAR: /* pick random coordinates far */
731 xnew = Player.p_x + SGN(Player.p_x) * ROLL(50 * Circle, 250 * Circle);
732 ynew = Player.p_y + SGN(Player.p_y) * ROLL(50 * Circle, 250 * Circle);
733 break;
734 }
735
736 /* now set location flags and adjust coordinates */
737 Circle = CIRCLE(Player.p_x = floor(xnew), Player.p_y = floor(ynew));
738
739 /* set up flags based upon location */
740 Throne = Marsh = Beyond = FALSE;
741
742 if (Player.p_x == 0.0 && Player.p_y == 0.0)
743 Throne = TRUE;
744 else
745 if (Circle < 35 && Circle >= 20)
746 Marsh = TRUE;
747 else
748 if (MAX(fabs(Player.p_x), fabs(Player.p_y)) >= D_BEYOND)
749 Beyond = TRUE;
750
751 Changed = TRUE;
752 }
753
754 void
755 readrecord(struct player *playerp, long loc)
756 {
757 fseek(Playersfp, loc, SEEK_SET);
758 fread((char *) playerp, SZ_PLAYERSTRUCT, 1, Playersfp);
759 }
760
761 void
762 adjuststats(void)
763 {
764 double dtemp; /* for temporary calculations */
765
766 if (explevel(Player.p_experience) > Player.p_level)
767 /* move one or more levels */
768 {
769 movelevel();
770 if (Player.p_level > 5.0)
771 Timeout = TRUE;
772 }
773 if (Player.p_specialtype == SC_VALAR)
774 /* valar */
775 Circle = Player.p_level / 5.0;
776
777 /* calculate effective quickness */
778 dtemp = ((Player.p_gold + Player.p_gems / 2.0) - 1000.0) / Statptr->c_goldtote
779 - Player.p_level;
780 dtemp = MAX(0.0, dtemp);/* gold slows player down */
781 Player.p_speed = Player.p_quickness + Player.p_quksilver - dtemp;
782
783 /* calculate effective strength */
784 if (Player.p_poison > 0.0)
785 /* poison makes player weaker */
786 {
787 dtemp = 1.0 - Player.p_poison * Statptr->c_weakness / 800.0;
788 dtemp = MAX(0.1, dtemp);
789 } else
790 dtemp = 1.0;
791 Player.p_might = dtemp * Player.p_strength + Player.p_sword;
792
793 /* insure that important things are within limits */
794 Player.p_quksilver = MIN(99.0, Player.p_quksilver);
795 Player.p_mana = MIN(Player.p_mana,
796 Player.p_level * Statptr->c_maxmana + 1000.0);
797 Player.p_brains = MIN(Player.p_brains,
798 Player.p_level * Statptr->c_maxbrains + 200.0);
799 Player.p_charms = MIN(Player.p_charms, Player.p_level + 10.0);
800
801 /*
802 * some implementations have problems with floating point compare
803 * we work around it with this stuff
804 */
805 Player.p_gold = floor(Player.p_gold) + 0.1;
806 Player.p_gems = floor(Player.p_gems) + 0.1;
807 Player.p_mana = floor(Player.p_mana) + 0.1;
808
809 if (Player.p_ring.ring_type != R_NONE)
810 /* do ring things */
811 {
812 /* rest to max */
813 Player.p_energy = Player.p_maxenergy + Player.p_shield;
814
815 if (Player.p_ring.ring_duration <= 0)
816 /* clean up expired rings */
817 switch (Player.p_ring.ring_type) {
818 case R_BAD: /* ring drives player crazy */
819 Player.p_ring.ring_type = R_SPOILED;
820 Player.p_ring.ring_duration = (short) ROLL(10.0, 25.0);
821 break;
822
823 case R_NAZREG: /* ring disappears */
824 Player.p_ring.ring_type = R_NONE;
825 break;
826
827 case R_SPOILED: /* ring kills player */
828 death("A cursed ring");
829 break;
830
831 case R_DLREG: /* this ring doesn't expire */
832 Player.p_ring.ring_duration = 0;
833 break;
834 }
835 }
836 if (Player.p_age / N_AGE > Player.p_degenerated)
837 /* age player slightly */
838 {
839 ++Player.p_degenerated;
840 if (Player.p_quickness > 23.0)
841 Player.p_quickness *= 0.99;
842 Player.p_strength *= 0.97;
843 Player.p_brains *= 0.95;
844 Player.p_magiclvl *= 0.97;
845 Player.p_maxenergy *= 0.95;
846 Player.p_quksilver *= 0.95;
847 Player.p_sword *= 0.93;
848 Player.p_shield *= 0.93;
849 }
850 }
851
852 void
853 initplayer(struct player *playerp)
854 {
855 playerp->p_experience =
856 playerp->p_level =
857 playerp->p_strength =
858 playerp->p_sword =
859 playerp->p_might =
860 playerp->p_energy =
861 playerp->p_maxenergy =
862 playerp->p_shield =
863 playerp->p_quickness =
864 playerp->p_quksilver =
865 playerp->p_speed =
866 playerp->p_magiclvl =
867 playerp->p_mana =
868 playerp->p_brains =
869 playerp->p_poison =
870 playerp->p_gems =
871 playerp->p_sin =
872 playerp->p_1scratch =
873 playerp->p_2scratch = 0.0;
874
875 playerp->p_gold = ROLL(50.0, 75.0) + 0.1; /* give some gold */
876
877 playerp->p_x = ROLL(-125.0, 251.0);
878 playerp->p_y = ROLL(-125.0, 251.0); /* give random x, y */
879
880 /* clear ring */
881 playerp->p_ring.ring_type = R_NONE;
882 playerp->p_ring.ring_duration = 0;
883 playerp->p_ring.ring_inuse = FALSE;
884
885 playerp->p_age = 0L;
886
887 playerp->p_degenerated = 1; /* don't degenerate initially */
888
889 playerp->p_type = C_FIGHTER; /* default */
890 playerp->p_specialtype = SC_NONE;
891 playerp->p_lives =
892 playerp->p_crowns =
893 playerp->p_charms =
894 playerp->p_amulets =
895 playerp->p_holywater =
896 playerp->p_lastused = 0;
897 playerp->p_status = S_NOTUSED;
898 playerp->p_tampered = T_OFF;
899 playerp->p_istat = I_OFF;
900
901 playerp->p_palantir =
902 playerp->p_blessing =
903 playerp->p_virgin =
904 playerp->p_blindness = FALSE;
905
906 playerp->p_name[0] =
907 playerp->p_password[0] =
908 playerp->p_login[0] = '\0';
909 }
910
911 void
912 readmessage(void)
913 {
914 move(3, 0);
915 clrtoeol();
916 fseek(Messagefp, 0L, SEEK_SET);
917 if (fgets(Databuf, SZ_DATABUF, Messagefp) != NULL)
918 addstr(Databuf);
919 }
920
921 void
922 error(const char *whichfile)
923 {
924 int (*funcp)(const char *,...);
925
926 if (Windows) {
927 funcp = printw;
928 clear();
929 } else
930 funcp = printf;
931
932 (*funcp) ("An unrecoverable error has occurred reading %s. (%s)\n", whichfile, strerror(errno));
933 (*funcp) ("Please run 'setup' to determine the problem.\n");
934 cleanup(TRUE);
935 /* NOTREACHED */
936 }
937
938 double
939 distance(double x_1, double x_2, double y_1, double y_2)
940 {
941 double deltax, deltay;
942
943 deltax = x_1 - x_2;
944 deltay = y_1 - y_2;
945 return (sqrt(deltax * deltax + deltay * deltay));
946 }
947
948 void
949 ill_sig(int whichsig)
950 {
951 clear();
952 if (!(whichsig == SIGINT || whichsig == SIGQUIT))
953 printw("Error: caught signal # %d.\n", whichsig);
954 cleanup(TRUE);
955 /* NOTREACHED */
956 }
957
958 const char *
959 descrstatus(struct player *playerp)
960 {
961 switch (playerp->p_status) {
962 case S_PLAYING:
963 if (playerp->p_energy < 0.2 * (playerp->p_maxenergy + playerp->p_shield))
964 return ("Low Energy");
965 else
966 if (playerp->p_blindness)
967 return ("Blind");
968 else
969 return ("In game");
970
971 case S_CLOAKED:
972 return ("Cloaked");
973
974 case S_INBATTLE:
975 return ("In Battle");
976
977 case S_MONSTER:
978 return ("Encounter");
979
980 case S_TRADING:
981 return ("Trading");
982
983 case S_OFF:
984 return ("Off");
985
986 case S_HUNGUP:
987 return ("Hung up");
988
989 default:
990 return ("");
991 }
992 }
993
994 double
995 drandom(void)
996 {
997 if (sizeof(int) != 2)
998 /* use only low bits */
999 return ((double) (random() & 0x7fff) / 32768.0);
1000 else
1001 return ((double) random() / 32768.0);
1002 }
1003
1004 void
1005 collecttaxes(double gold, double gems)
1006 {
1007 FILE *fp; /* to update Goldfile */
1008 double dtemp; /* for temporary calculations */
1009 double taxes; /* tax liability */
1010
1011 /* add to cache */
1012 Player.p_gold += gold;
1013 Player.p_gems += gems;
1014
1015 /* calculate tax liability */
1016 taxes = N_TAXAMOUNT / 100.0 * (N_GEMVALUE * gems + gold);
1017
1018 if (Player.p_gold < taxes)
1019 /* not enough gold to pay taxes, must convert some gems to
1020 * gold */
1021 {
1022 dtemp = floor(taxes / N_GEMVALUE + 1.0); /* number of gems to
1023 * convert */
1024
1025 if (Player.p_gems >= dtemp)
1026 /* player has enough to convert */
1027 {
1028 Player.p_gems -= dtemp;
1029 Player.p_gold += dtemp * N_GEMVALUE;
1030 } else
1031 /* take everything; this should never happen */
1032 {
1033 Player.p_gold += Player.p_gems * N_GEMVALUE;
1034 Player.p_gems = 0.0;
1035 taxes = Player.p_gold;
1036 }
1037 }
1038 Player.p_gold -= taxes;
1039
1040 if ((fp = fopen(_PATH_GOLD, "r+")) != NULL)
1041 /* update taxes */
1042 {
1043 dtemp = 0.0;
1044 fread((char *) &dtemp, sizeof(double), 1, fp);
1045 dtemp += floor(taxes);
1046 fseek(fp, 0L, SEEK_SET);
1047 fwrite((char *) &dtemp, sizeof(double), 1, fp);
1048 fclose(fp);
1049 }
1050 }