Initial import from FreeBSD RELENG_4:
[dragonfly.git] / games / rogue / use.c
1 /*
2  * Copyright (c) 1988, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Timothy C. Stoehr.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)use.c       8.1 (Berkeley) 5/31/93";
40 #endif
41 static const char rcsid[] =
42  "$FreeBSD: src/games/rogue/use.c,v 1.4 1999/11/30 03:49:29 billf Exp $";
43 #endif /* not lint */
44
45 /*
46  * use.c
47  *
48  * This source herein may be modified and/or distributed by anybody who
49  * so desires, with the following restrictions:
50  *    1.)  No portion of this notice shall be removed.
51  *    2.)  Credit shall not be taken for the creation of this source.
52  *    3.)  This code is not to be traded, sold, or used for personal
53  *         gain or profit.
54  *
55  */
56
57 #include "rogue.h"
58
59 short halluc = 0;
60 short blind = 0;
61 short confused = 0;
62 short levitate = 0;
63 short haste_self = 0;
64 boolean see_invisible = 0;
65 short extra_hp = 0;
66 boolean detect_monster = 0;
67 boolean con_mon = 0;
68 const char *strange_feeling = "you have a strange feeling for a moment, then it passes";
69
70 extern short bear_trap;
71 extern char hunger_str[];
72 extern short cur_room;
73 extern long level_points[];
74 extern boolean being_held;
75 extern char *fruit, *you_can_move_again;
76 extern boolean sustain_strength;
77
78 quaff()
79 {
80         short ch;
81         char buf[80];
82         object *obj;
83
84         ch = pack_letter("quaff what?", POTION);
85
86         if (ch == CANCEL) {
87                 return;
88         }
89         if (!(obj = get_letter_object(ch))) {
90                 message("no such item.", 0);
91                 return;
92         }
93         if (obj->what_is != POTION) {
94                 message("you can't drink that", 0);
95                 return;
96         }
97         switch(obj->which_kind) {
98                 case INCREASE_STRENGTH:
99                         message("you feel stronger now, what bulging muscles!",
100                         0);
101                         rogue.str_current++;
102                         if (rogue.str_current > rogue.str_max) {
103                                 rogue.str_max = rogue.str_current;
104                         }
105                         break;
106                 case RESTORE_STRENGTH:
107                         rogue.str_current = rogue.str_max;
108                         message("this tastes great, you feel warm all over", 0);
109                         break;
110                 case HEALING:
111                         message("you begin to feel better", 0);
112                         potion_heal(0);
113                         break;
114                 case EXTRA_HEALING:
115                         message("you begin to feel much better", 0);
116                         potion_heal(1);
117                         break;
118                 case POISON:
119                         if (!sustain_strength) {
120                                 rogue.str_current -= get_rand(1, 3);
121                                 if (rogue.str_current < 1) {
122                                         rogue.str_current = 1;
123                                 }
124                         }
125                         message("you feel very sick now", 0);
126                         if (halluc) {
127                                 unhallucinate();
128                         }
129                         break;
130                 case RAISE_LEVEL:
131                         rogue.exp_points = level_points[rogue.exp - 1];
132                         message("you suddenly feel much more skillful", 0);
133                         add_exp(1, 1);
134                         break;
135                 case BLINDNESS:
136                         go_blind();
137                         break;
138                 case HALLUCINATION:
139                         message("oh wow, everything seems so cosmic", 0);
140                         halluc += get_rand(500, 800);
141                         break;
142                 case DETECT_MONSTER:
143                         show_monsters();
144                         if (!(level_monsters.next_monster)) {
145                                 message(strange_feeling, 0);
146                         }
147                         break;
148                 case DETECT_OBJECTS:
149                         if (level_objects.next_object) {
150                                 if (!blind) {
151                                         show_objects();
152                                 }
153                         } else {
154                                 message(strange_feeling, 0);
155                         }
156                         break;
157                 case CONFUSION:
158                         message((halluc ? "what a trippy feeling" :
159                         "you feel confused"), 0);
160                         cnfs();
161                         break;
162                 case LEVITATION:
163                         message("you start to float in the air", 0);
164                         levitate += get_rand(15, 30);
165                         being_held = bear_trap = 0;
166                         break;
167                 case HASTE_SELF:
168                         message("you feel yourself moving much faster", 0);
169                         haste_self += get_rand(11, 21);
170                         if (!(haste_self % 2)) {
171                                 haste_self++;
172                         }
173                         break;
174                 case SEE_INVISIBLE:
175                         sprintf(buf, "hmm, this potion tastes like %sjuice", fruit);
176                         message(buf, 0);
177                         if (blind) {
178                                 unblind();
179                         }
180                         see_invisible = 1;
181                         relight();
182                         break;
183         }
184         print_stats((STAT_STRENGTH | STAT_HP));
185         if (id_potions[obj->which_kind].id_status != CALLED) {
186                 id_potions[obj->which_kind].id_status = IDENTIFIED;
187         }
188         vanish(obj, 1, &rogue.pack);
189 }
190
191 read_scroll()
192 {
193         short ch;
194         object *obj;
195         char msg[DCOLS];
196
197         ch = pack_letter("read what?", SCROL);
198
199         if (ch == CANCEL) {
200                 return;
201         }
202         if (!(obj = get_letter_object(ch))) {
203                 message("no such item.", 0);
204                 return;
205         }
206         if (obj->what_is != SCROL) {
207                 message("you can't read that", 0);
208                 return;
209         }
210         switch(obj->which_kind) {
211                 case SCARE_MONSTER:
212                         message("you hear a maniacal laughter in the distance",
213                         0);
214                         break;
215                 case HOLD_MONSTER:
216                         hold_monster();
217                         break;
218                 case ENCH_WEAPON:
219                         if (rogue.weapon) {
220                                 if (rogue.weapon->what_is == WEAPON) {
221                                         sprintf(msg, "your %sglow%s %sfor a moment",
222                                         name_of(rogue.weapon),
223                                         ((rogue.weapon->quantity <= 1) ? "s" : ""),
224                                         get_ench_color());
225                                         message(msg, 0);
226                                         if (coin_toss()) {
227                                                 rogue.weapon->hit_enchant++;
228                                         } else {
229                                                 rogue.weapon->d_enchant++;
230                                         }
231                                 }
232                                 rogue.weapon->is_cursed = 0;
233                         } else {
234                                 message("your hands tingle", 0);
235                         }
236                         break;
237                 case ENCH_ARMOR:
238                         if (rogue.armor) {
239                                 sprintf(msg, "your armor glows %sfor a moment",
240                                 get_ench_color());
241                                 message(msg, 0);
242                                 rogue.armor->d_enchant++;
243                                 rogue.armor->is_cursed = 0;
244                                 print_stats(STAT_ARMOR);
245                         } else {
246                                 message("your skin crawls", 0);
247                         }
248                         break;
249                 case IDENTIFY:
250                         message("this is a scroll of identify", 0);
251                         obj->identified = 1;
252                         id_scrolls[obj->which_kind].id_status = IDENTIFIED;
253                         idntfy();
254                         break;
255                 case TELEPORT:
256                         tele();
257                         break;
258                 case SLEEP:
259                         message("you fall asleep", 0);
260                         take_a_nap();
261                         break;
262                 case PROTECT_ARMOR:
263                         if (rogue.armor) {
264                                 message( "your armor is covered by a shimmering gold shield",0);
265                                 rogue.armor->is_protected = 1;
266                                 rogue.armor->is_cursed = 0;
267                         } else {
268                                 message("your acne seems to have disappeared", 0);
269                         }
270                         break;
271                 case REMOVE_CURSE:
272                                 message((!halluc) ?
273                                         "you feel as though someone is watching over you" :
274                                         "you feel in touch with the universal oneness", 0);
275                         uncurse_all();
276                         break;
277                 case CREATE_MONSTER:
278                         create_monster();
279                         break;
280                 case AGGRAVATE_MONSTER:
281                         aggravate();
282                         break;
283                 case MAGIC_MAPPING:
284                         message("this scroll seems to have a map on it", 0);
285                         draw_magic_map();
286                         break;
287                 case CON_MON:
288                         con_mon = 1;
289                         sprintf(msg, "your hands glow %sfor a moment", get_ench_color());
290                         message(msg, 0);
291                         break;
292         }
293         if (id_scrolls[obj->which_kind].id_status != CALLED) {
294                 id_scrolls[obj->which_kind].id_status = IDENTIFIED;
295         }
296         vanish(obj, (obj->which_kind != SLEEP), &rogue.pack);
297 }
298
299 /* vanish() does NOT handle a quiver of weapons with more than one
300  *  arrow (or whatever) in the quiver.  It will only decrement the count.
301  */
302
303 vanish(obj, rm, pack)
304 object *obj;
305 short rm;
306 object *pack;
307 {
308         if (obj->quantity > 1) {
309                 obj->quantity--;
310         } else {
311                 if (obj->in_use_flags & BEING_WIELDED) {
312                         unwield(obj);
313                 } else if (obj->in_use_flags & BEING_WORN) {
314                         unwear(obj);
315                 } else if (obj->in_use_flags & ON_EITHER_HAND) {
316                         un_put_on(obj);
317                 }
318                 take_from_pack(obj, pack);
319                 free_object(obj);
320         }
321         if (rm) {
322                 (void) reg_move();
323         }
324 }
325
326 potion_heal(extra)
327 {
328         float ratio;
329         short add;
330
331         rogue.hp_current += rogue.exp;
332
333         ratio = ((float)rogue.hp_current) / rogue.hp_max;
334
335         if (ratio >= 1.00) {
336                 rogue.hp_max += (extra ? 2 : 1);
337                 extra_hp += (extra ? 2 : 1);
338                 rogue.hp_current = rogue.hp_max;
339         } else if (ratio >= 0.90) {
340                 rogue.hp_max += (extra ? 1 : 0);
341                 extra_hp += (extra ? 1 : 0);
342                 rogue.hp_current = rogue.hp_max;
343         } else {
344                 if (ratio < 0.33) {
345                         ratio = 0.33;
346                 }
347                 if (extra) {
348                         ratio += ratio;
349                 }
350                 add = (short)(ratio * ((float)rogue.hp_max - rogue.hp_current));
351                 rogue.hp_current += add;
352                 if (rogue.hp_current > rogue.hp_max) {
353                         rogue.hp_current = rogue.hp_max;
354                 }
355         }
356         if (blind) {
357                 unblind();
358         }
359         if (confused && extra) {
360                         unconfuse();
361         } else if (confused) {
362                 confused = (confused / 2) + 1;
363         }
364         if (halluc && extra) {
365                 unhallucinate();
366         } else if (halluc) {
367                 halluc = (halluc / 2) + 1;
368         }
369 }
370
371 idntfy()
372 {
373         short ch;
374         object *obj;
375         struct id *id_table;
376         char desc[DCOLS];
377 AGAIN:
378         ch = pack_letter("what would you like to identify?", ALL_OBJECTS);
379
380         if (ch == CANCEL) {
381                 return;
382         }
383         if (!(obj = get_letter_object(ch))) {
384                 message("no such item, try again", 0);
385                 message("", 0);
386                 check_message();
387                 goto AGAIN;
388         }
389         obj->identified = 1;
390         if (obj->what_is & (SCROL | POTION | WEAPON | ARMOR | WAND | RING)) {
391                 id_table = get_id_table(obj);
392                 id_table[obj->which_kind].id_status = IDENTIFIED;
393         }
394         get_desc(obj, desc);
395         message(desc, 0);
396 }
397
398 eat()
399 {
400         short ch;
401         short moves;
402         object *obj;
403         char buf[70];
404
405         ch = pack_letter("eat what?", FOOD);
406
407         if (ch == CANCEL) {
408                 return;
409         }
410         if (!(obj = get_letter_object(ch))) {
411                 message("no such item.", 0);
412                 return;
413         }
414         if (obj->what_is != FOOD) {
415                 message("you can't eat that", 0);
416                 return;
417         }
418         if ((obj->which_kind == FRUIT) || rand_percent(60)) {
419                 moves = get_rand(950, 1150);
420                 if (obj->which_kind == RATION) {
421                         message("yum, that tasted good", 0);
422                 } else {
423                         sprintf(buf, "my, that was a yummy %s", fruit);
424                         message(buf, 0);
425                 }
426         } else {
427                 moves = get_rand(750, 950);
428                 message("yuk, that food tasted awful", 0);
429                 add_exp(2, 1);
430         }
431         rogue.moves_left /= 3;
432         rogue.moves_left += moves;
433         hunger_str[0] = 0;
434         print_stats(STAT_HUNGER);
435
436         vanish(obj, 1, &rogue.pack);
437 }
438
439 hold_monster()
440 {
441         short i, j;
442         short mcount = 0;
443         object *monster;
444         short row, col;
445
446         for (i = -2; i <= 2; i++) {
447                 for (j = -2; j <= 2; j++) {
448                         row = rogue.row + i;
449                         col = rogue.col + j;
450                         if ((row < MIN_ROW) || (row > (DROWS-2)) || (col < 0) ||
451                                  (col > (DCOLS-1))) {
452                                 continue;
453                         }
454                         if (dungeon[row][col] & MONSTER) {
455                                 monster = object_at(&level_monsters, row, col);
456                                 monster->m_flags |= ASLEEP;
457                                 monster->m_flags &= (~WAKENS);
458                                 mcount++;
459                         }
460                 }
461         }
462         if (mcount == 0) {
463                 message("you feel a strange sense of loss", 0);
464         } else if (mcount == 1) {
465                 message("the monster freezes", 0);
466         } else {
467                 message("the monsters around you freeze", 0);
468         }
469 }
470
471 tele()
472 {
473         mvaddch(rogue.row, rogue.col, get_dungeon_char(rogue.row, rogue.col));
474
475         if (cur_room >= 0) {
476                 darken_room(cur_room);
477         }
478         put_player(get_room_number(rogue.row, rogue.col));
479         being_held = 0;
480         bear_trap = 0;
481 }
482
483 hallucinate()
484 {
485         object *obj, *monster;
486         short ch;
487
488         if (blind) return;
489
490         obj = level_objects.next_object;
491
492         while (obj) {
493                 ch = mvinch(obj->row, obj->col);
494                 if (((ch < 'A') || (ch > 'Z')) &&
495                         ((obj->row != rogue.row) || (obj->col != rogue.col)))
496                 if ((ch != ' ') && (ch != '.') && (ch != '#') && (ch != '+')) {
497                         addch(gr_obj_char());
498                 }
499                 obj = obj->next_object;
500         }
501         monster = level_monsters.next_monster;
502
503         while (monster) {
504                 ch = mvinch(monster->row, monster->col);
505                 if ((ch >= 'A') && (ch <= 'Z')) {
506                         addch(get_rand('A', 'Z'));
507                 }
508                 monster = monster->next_monster;
509         }
510 }
511
512 unhallucinate()
513 {
514         halluc = 0;
515         relight();
516         message("everything looks SO boring now", 1);
517 }
518
519 unblind()
520 {
521         blind = 0;
522         message("the veil of darkness lifts", 1);
523         relight();
524         if (halluc) {
525                 hallucinate();
526         }
527         if (detect_monster) {
528                 show_monsters();
529         }
530 }
531
532 relight()
533 {
534         if (cur_room == PASSAGE) {
535                 light_passage(rogue.row, rogue.col);
536         } else {
537                 light_up_room(cur_room);
538         }
539         mvaddch(rogue.row, rogue.col, rogue.fchar);
540 }
541
542 take_a_nap()
543 {
544         short i;
545
546         i = get_rand(2, 5);
547         md_sleep(1);
548
549         while (i--) {
550                 mv_mons();
551         }
552         md_sleep(1);
553         message(you_can_move_again, 0);
554 }
555
556 go_blind()
557 {
558         short i, j;
559
560         if (!blind) {
561                 message("a cloak of darkness falls around you", 0);
562         }
563         blind += get_rand(500, 800);
564
565         if (detect_monster) {
566                 object *monster;
567
568                 monster = level_monsters.next_monster;
569
570                 while (monster) {
571                         mvaddch(monster->row, monster->col, monster->trail_char);
572                         monster = monster->next_monster;
573                 }
574         }
575         if (cur_room >= 0) {
576                 for (i = rooms[cur_room].top_row + 1;
577                          i < rooms[cur_room].bottom_row; i++) {
578                         for (j = rooms[cur_room].left_col + 1;
579                                  j < rooms[cur_room].right_col; j++) {
580                                 mvaddch(i, j, ' ');
581                         }
582                 }
583         }
584         mvaddch(rogue.row, rogue.col, rogue.fchar);
585 }
586
587 const char *
588 get_ench_color()
589 {
590         if (halluc) {
591                 return(id_potions[get_rand(0, POTIONS-1)].title);
592         } else if (con_mon) {
593                 return("red ");
594         }
595         return("blue ");
596 }
597
598 cnfs()
599 {
600         confused += get_rand(12, 22);
601 }
602
603 unconfuse()
604 {
605         char msg[80];
606
607         confused = 0;
608         sprintf(msg, "you feel less %s now", (halluc ? "trippy" : "confused"));
609         message(msg, 1);
610 }
611
612 uncurse_all()
613 {
614         object *obj;
615
616         obj = rogue.pack.next_object;
617
618         while (obj) {
619                 obj->is_cursed = 0;
620                 obj = obj->next_object;
621         }
622 }