Merge remote-tracking branch 'origin/vendor/GCC80'
[dragonfly.git] / games / rogue / object.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. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * @(#)object.c 8.1 (Berkeley) 5/31/93
33  * $FreeBSD: src/games/rogue/object.c,v 1.5 1999/11/30 03:49:25 billf Exp $
34  */
35
36 /*
37  * object.c
38  *
39  * This source herein may be modified and/or distributed by anybody who
40  * so desires, with the following restrictions:
41  *    1.)  No portion of this notice shall be removed.
42  *    2.)  Credit shall not be taken for the creation of this source.
43  *    3.)  This code is not to be traded, sold, or used for personal
44  *         gain or profit.
45  *
46  */
47
48 #include "rogue.h"
49
50 object level_objects;
51 unsigned short dungeon[DROWS][DCOLS];
52 short foods = 0;
53 char *fruit = NULL;
54
55 static object *free_list = NULL;
56
57 fighter rogue = {
58         INIT_AW,        /* armor, weapon */
59         INIT_RINGS,     /* rings */
60         INIT_HP,INIT_HP,/* Hp current,max */
61         INIT_STR,       /* Str current,max */
62         INIT_PACK,      /* pack */
63         INIT_GOLD,      /* gold */
64         INIT_EXP,       /* exp level,points */
65         0, 0,           /* row, col */
66         INIT_CHAR,      /* char */
67         INIT_MOVES      /* moves */
68 };
69
70 struct id id_potions[POTIONS] = {
71 {100, "blue \0                           ", "of increase strength ", 0},
72 {250, "red \0                            ", "of restore strength ", 0},
73 {100, "green \0                          ", "of healing ", 0},
74 {200, "grey \0                           ", "of extra healing ", 0},
75  {10, "brown \0                          ", "of poison ", 0},
76 {300, "clear \0                          ", "of raise level ", 0},
77  {10, "pink \0                           ", "of blindness ", 0},
78  {25, "white \0                          ", "of hallucination ", 0},
79 {100, "purple \0                         ", "of detect monster ", 0},
80 {100, "black \0                          ", "of detect things ", 0},
81  {10, "yellow \0                         ", "of confusion ", 0},
82  {80, "plaid \0                          ", "of levitation ", 0},
83 {150, "burgundy \0                       ", "of haste self ", 0},
84 {145, "beige \0                          ", "of see invisible ", 0}
85 };
86
87 struct id id_scrolls[SCROLS] = {
88 {505, "                                   ", "of protect armor ", 0},
89 {200, "                                   ", "of hold monster ", 0},
90 {235, "                                   ", "of enchant weapon ", 0},
91 {235, "                                   ", "of enchant armor ", 0},
92 {175, "                                   ", "of identify ", 0},
93 {190, "                                   ", "of teleportation ", 0},
94  {25, "                                   ", "of sleep ", 0},
95 {610, "                                   ", "of scare monster ", 0},
96 {210, "                                   ", "of remove curse ", 0},
97  {80, "                                   ", "of create monster ",0},
98  {25, "                                   ", "of aggravate monster ",0},
99 {180, "                                   ", "of magic mapping ", 0},
100  {90, "                                   ", "of confuse monster ", 0}
101 };
102
103 struct id id_weapons[WEAPONS] = {
104         {150, "short bow ", "", 0},
105           {8, "darts ", "", 0},
106          {15, "arrows ", "", 0},
107          {27, "daggers ", "", 0},
108          {35, "shurikens ", "", 0},
109         {360, "mace ", "", 0},
110         {470, "long sword ", "", 0},
111         {580, "two-handed sword ", "", 0}
112 };
113
114 struct id id_armors[ARMORS] = {
115         {300, "leather armor ", "", (UNIDENTIFIED)},
116         {300, "ring mail ", "", (UNIDENTIFIED)},
117         {400, "scale mail ", "", (UNIDENTIFIED)},
118         {500, "chain mail ", "", (UNIDENTIFIED)},
119         {600, "banded mail ", "", (UNIDENTIFIED)},
120         {600, "splint mail ", "", (UNIDENTIFIED)},
121         {700, "plate mail ", "", (UNIDENTIFIED)}
122 };
123
124 struct id id_wands[WANDS] = {
125          {25, "                                 ", "of teleport away ",0},
126          {50, "                                 ", "of slow monster ", 0},
127           {8, "                                 ", "of invisibility ",0},
128          {55, "                                 ", "of polymorph ",0},
129           {2, "                                 ", "of haste monster ",0},
130          {20, "                                 ", "of magic missile ",0},
131          {20, "                                 ", "of cancellation ",0},
132           {0, "                                 ", "of do nothing ",0},
133          {35, "                                 ", "of drain life ",0},
134          {20, "                                 ", "of cold ",0},
135          {20, "                                 ", "of fire ",0}
136 };
137
138 struct id id_rings[RINGS] = {
139          {250, "                                 ", "of stealth ",0},
140          {100, "                                 ", "of teleportation ", 0},
141          {255, "                                 ", "of regeneration ",0},
142          {295, "                                 ", "of slow digestion ",0},
143          {200, "                                 ", "of add strength ",0},
144          {250, "                                 ", "of sustain strength ",0},
145          {250, "                                 ", "of dexterity ",0},
146           {25, "                                 ", "of adornment ",0},
147          {300, "                                 ", "of see invisible ",0},
148          {290, "                                 ", "of maintain armor ",0},
149          {270, "                                 ", "of searching ",0},
150 };
151
152 static void gr_armor(object *);
153 static void gr_potion(object *);
154 static void gr_scroll(object *);
155 static void gr_wand(object *);
156 static void gr_weapon(object *, int);
157 static unsigned short gr_what_is(void);
158 static void make_party(void);
159 static void plant_gold(short, short, boolean);
160 static void put_gold(void);
161 static void rand_place(object *);
162
163 void
164 put_objects(void)
165 {
166         short i, n;
167         object *obj;
168
169         if (cur_level < max_level) {
170                 return;
171         }
172         n = coin_toss() ? get_rand(2, 4) : get_rand(3, 5);
173         while (rand_percent(33)) {
174                 n++;
175         }
176         if (party_room != NO_ROOM) {
177                 make_party();
178         }
179         for (i = 0; i < n; i++) {
180                 obj = gr_object();
181                 rand_place(obj);
182         }
183         put_gold();
184 }
185
186 static void
187 put_gold(void)
188 {
189         short i, j;
190         short row,col;
191         boolean is_maze, is_room;
192
193         for (i = 0; i < MAXROOMS; i++) {
194                 is_maze = (rooms[i].is_room & R_MAZE) ? 1 : 0;
195                 is_room = (rooms[i].is_room & R_ROOM) ? 1 : 0;
196
197                 if (!(is_room || is_maze)) {
198                         continue;
199                 }
200                 if (is_maze || rand_percent(GOLD_PERCENT)) {
201                         for (j = 0; j < 50; j++) {
202                                 row = get_rand(rooms[i].top_row+1,
203                                 rooms[i].bottom_row-1);
204                                 col = get_rand(rooms[i].left_col+1,
205                                 rooms[i].right_col-1);
206                                 if ((dungeon[row][col] == FLOOR) ||
207                                         (dungeon[row][col] == TUNNEL)) {
208                                         plant_gold(row, col, is_maze);
209                                         break;
210                                 }
211                         }
212                 }
213         }
214 }
215
216 static void
217 plant_gold(short row, short col, boolean is_maze)
218 {
219         object *obj;
220
221         obj = alloc_object();
222         obj->row = row; obj->col = col;
223         obj->what_is = GOLD;
224         obj->quantity = get_rand((2 * cur_level), (16 * cur_level));
225         if (is_maze) {
226                 obj->quantity += obj->quantity / 2;
227         }
228         dungeon[row][col] |= OBJECT;
229         add_to_pack(obj, &level_objects, 0);
230 }
231
232 void
233 place_at(object *obj, int row, int col)
234 {
235         obj->row = row;
236         obj->col = col;
237         dungeon[row][col] |= OBJECT;
238         add_to_pack(obj, &level_objects, 0);
239 }
240
241 object *
242 object_at(object *pack, short row, short col)
243 {
244         object *obj = NULL;
245
246         if (dungeon[row][col] & (MONSTER | OBJECT)) {
247                 obj = pack->next_object;
248
249                 while (obj && ((obj->row != row) || (obj->col != col))) {
250                         obj = obj->next_object;
251                 }
252                 if (!obj) {
253                         message("object_at(): inconsistent", 1);
254                 }
255         }
256         return(obj);
257 }
258
259 object *
260 get_letter_object(int ch)
261 {
262         object *obj;
263
264         obj = rogue.pack.next_object;
265
266         while (obj && (obj->ichar != ch)) {
267                 obj = obj->next_object;
268         }
269         return(obj);
270 }
271
272 void
273 free_stuff(object *objlist)
274 {
275         object *obj;
276
277         while (objlist->next_object) {
278                 obj = objlist->next_object;
279                 objlist->next_object =
280                         objlist->next_object->next_object;
281                 free_object(obj);
282         }
283 }
284
285 const char *
286 name_of(const object *obj)
287 {
288         const char *retstring;
289
290         switch(obj->what_is) {
291         case SCROL:
292                 retstring = obj->quantity > 1 ? "scrolls " : "scroll ";
293                 break;
294         case POTION:
295                 retstring = obj->quantity > 1 ? "potions " : "potion ";
296                 break;
297         case FOOD:
298                 if (obj->which_kind == RATION) {
299                         retstring = "food ";
300                 } else {
301                         retstring = fruit;
302                 }
303                 break;
304         case WAND:
305                 retstring = is_wood[obj->which_kind] ? "staff " : "wand ";
306                 break;
307         case WEAPON:
308                 switch(obj->which_kind) {
309                 case DART:
310                         retstring=obj->quantity > 1 ? "darts " : "dart ";
311                         break;
312                 case ARROW:
313                         retstring=obj->quantity > 1 ? "arrows " : "arrow ";
314                         break;
315                 case DAGGER:
316                         retstring=obj->quantity > 1 ? "daggers " : "dagger ";
317                         break;
318                 case SHURIKEN:
319                         retstring=obj->quantity > 1?"shurikens ":"shuriken ";
320                         break;
321                 default:
322                         retstring = id_weapons[obj->which_kind].title;
323                 }
324                 break;
325         case ARMOR:
326                 retstring = "armor ";
327                 break;
328         case RING:
329                         retstring = "ring ";
330                 break;
331         case AMULET:
332                 retstring = "amulet ";
333                 break;
334         default:
335                 retstring = "unknown ";
336                 break;
337         }
338         return(retstring);
339 }
340
341 object *
342 gr_object(void)
343 {
344         object *obj;
345
346         obj = alloc_object();
347
348         if (foods < (cur_level / 3)) {
349                 obj->what_is = FOOD;
350                 foods++;
351         } else {
352                 obj->what_is = gr_what_is();
353         }
354         switch(obj->what_is) {
355         case SCROL:
356                 gr_scroll(obj);
357                 break;
358         case POTION:
359                 gr_potion(obj);
360                 break;
361         case WEAPON:
362                 gr_weapon(obj, 1);
363                 break;
364         case ARMOR:
365                 gr_armor(obj);
366                 break;
367         case WAND:
368                 gr_wand(obj);
369                 break;
370         case FOOD:
371                 get_food(obj, 0);
372                 break;
373         case RING:
374                 gr_ring(obj, 1);
375                 break;
376         }
377         return(obj);
378 }
379
380 static unsigned short
381 gr_what_is(void)
382 {
383         short percent;
384         unsigned short what_is;
385
386         percent = get_rand(1, 91);
387
388         if (percent <= 30) {
389                 what_is = SCROL;
390         } else if (percent <= 60) {
391                 what_is = POTION;
392         } else if (percent <= 64) {
393                 what_is = WAND;
394         } else if (percent <= 74) {
395                 what_is = WEAPON;
396         } else if (percent <= 83) {
397                 what_is = ARMOR;
398         } else if (percent <= 88) {
399                 what_is = FOOD;
400         } else {
401                 what_is = RING;
402         }
403         return(what_is);
404 }
405
406 static void
407 gr_scroll(object *obj)
408 {
409         short percent;
410
411         percent = get_rand(0, 91);
412
413         obj->what_is = SCROL;
414
415         if (percent <= 5) {
416                 obj->which_kind = PROTECT_ARMOR;
417         } else if (percent <= 10) {
418                 obj->which_kind = HOLD_MONSTER;
419         } else if (percent <= 20) {
420                 obj->which_kind = CREATE_MONSTER;
421         } else if (percent <= 35) {
422                 obj->which_kind = IDENTIFY;
423         } else if (percent <= 43) {
424                 obj->which_kind = TELEPORT;
425         } else if (percent <= 50) {
426                 obj->which_kind = SLEEP;
427         } else if (percent <= 55) {
428                 obj->which_kind = SCARE_MONSTER;
429         } else if (percent <= 64) {
430                 obj->which_kind = REMOVE_CURSE;
431         } else if (percent <= 69) {
432                 obj->which_kind = ENCH_ARMOR;
433         } else if (percent <= 74) {
434                 obj->which_kind = ENCH_WEAPON;
435         } else if (percent <= 80) {
436                 obj->which_kind = AGGRAVATE_MONSTER;
437         } else if (percent <= 86) {
438                 obj->which_kind = CON_MON;
439         } else {
440                 obj->which_kind = MAGIC_MAPPING;
441         }
442 }
443
444 static void
445 gr_potion(object *obj)
446 {
447         short percent;
448
449         percent = get_rand(1, 118);
450
451         obj->what_is = POTION;
452
453         if (percent <= 5) {
454                 obj->which_kind = RAISE_LEVEL;
455         } else if (percent <= 15) {
456                 obj->which_kind = DETECT_OBJECTS;
457         } else if (percent <= 25) {
458                 obj->which_kind = DETECT_MONSTER;
459         } else if (percent <= 35) {
460                 obj->which_kind = INCREASE_STRENGTH;
461         } else if (percent <= 45) {
462                 obj->which_kind = RESTORE_STRENGTH;
463         } else if (percent <= 55) {
464                 obj->which_kind = HEALING;
465         } else if (percent <= 65) {
466                 obj->which_kind = EXTRA_HEALING;
467         } else if (percent <= 75) {
468                 obj->which_kind = BLINDNESS;
469         } else if (percent <= 85) {
470                 obj->which_kind = HALLUCINATION;
471         } else if (percent <= 95) {
472                 obj->which_kind = CONFUSION;
473         } else if (percent <= 105) {
474                 obj->which_kind = POISON;
475         } else if (percent <= 110) {
476                 obj->which_kind = LEVITATION;
477         } else if (percent <= 114) {
478                 obj->which_kind = HASTE_SELF;
479         } else {
480                 obj->which_kind = SEE_INVISIBLE;
481         }
482 }
483
484 static void
485 gr_weapon(object *obj, int assign_wk)
486 {
487         short percent;
488         short i;
489         short blessing, increment;
490
491         obj->what_is = WEAPON;
492         if (assign_wk) {
493                 obj->which_kind = get_rand(0, (WEAPONS - 1));
494         }
495         if ((obj->which_kind == ARROW) || (obj->which_kind == DAGGER) ||
496                 (obj->which_kind == SHURIKEN) | (obj->which_kind == DART)) {
497                 obj->quantity = get_rand(3, 15);
498                 obj->quiver = get_rand(0, 126);
499         } else {
500                 obj->quantity = 1;
501         }
502         obj->hit_enchant = obj->d_enchant = 0;
503
504         percent = get_rand(1, 96);
505         blessing = get_rand(1, 3);
506
507         if (percent <= 16) {
508                 increment = 1;
509         } else if (percent <= 32) {
510                 increment = -1;
511                 obj->is_cursed = 1;
512         }
513         if (percent <= 32) {
514                 for (i = 0; i < blessing; i++) {
515                         if (coin_toss()) {
516                                 obj->hit_enchant += increment;
517                         } else {
518                                 obj->d_enchant += increment;
519                         }
520                 }
521         }
522         switch(obj->which_kind) {
523         case BOW:
524         case DART:
525                 obj->damage = "1d1";
526                 break;
527         case ARROW:
528                 obj->damage = "1d2";
529                 break;
530         case DAGGER:
531                 obj->damage = "1d3";
532                 break;
533         case SHURIKEN:
534                 obj->damage = "1d4";
535                 break;
536         case MACE:
537                 obj->damage = "2d3";
538                 break;
539         case LONG_SWORD:
540                 obj->damage = "3d4";
541                 break;
542         case TWO_HANDED_SWORD:
543                 obj->damage = "4d5";
544                 break;
545         }
546 }
547
548 static void
549 gr_armor(object *obj)
550 {
551         short percent;
552         short blessing;
553
554         obj->what_is = ARMOR;
555         obj->which_kind = get_rand(0, (ARMORS - 1));
556         obj->class = obj->which_kind + 2;
557         if ((obj->which_kind == PLATE) || (obj->which_kind == SPLINT)) {
558                 obj->class--;
559         }
560         obj->is_protected = 0;
561         obj->d_enchant = 0;
562
563         percent = get_rand(1, 100);
564         blessing = get_rand(1, 3);
565
566         if (percent <= 16) {
567                 obj->is_cursed = 1;
568                 obj->d_enchant -= blessing;
569         } else if (percent <= 33) {
570                 obj->d_enchant += blessing;
571         }
572 }
573
574 static void
575 gr_wand(object *obj)
576 {
577         obj->what_is = WAND;
578         obj->which_kind = get_rand(0, (WANDS - 1));
579         obj->class = get_rand(3, 7);
580 }
581
582 void
583 get_food(object *obj, boolean force_ration)
584 {
585         obj->what_is = FOOD;
586
587         if (force_ration || rand_percent(80)) {
588                 obj->which_kind = RATION;
589         } else {
590                 obj->which_kind = FRUIT;
591         }
592 }
593
594 void
595 put_stairs(void)
596 {
597         short row, col;
598
599         gr_row_col(&row, &col, (FLOOR | TUNNEL));
600         dungeon[row][col] |= STAIRS;
601 }
602
603 short
604 get_armor_class(const object *obj)
605 {
606         if (obj) {
607                 return(obj->class + obj->d_enchant);
608         }
609         return(0);
610 }
611
612 object *
613 alloc_object(void)
614 {
615         object *obj;
616
617         if (free_list) {
618                 obj = free_list;
619                 free_list = free_list->next_object;
620         } else if (!(obj = (object *) md_malloc(sizeof(object)))) {
621                         message("cannot allocate object, saving game", 0);
622                         save_into_file(error_file);
623                         clean_up("alloc_object:  save failed");
624         }
625         obj->quantity = 1;
626         obj->ichar = 'L';
627         obj->picked_up = obj->is_cursed = 0;
628         obj->in_use_flags = NOT_USED;
629         obj->identified = UNIDENTIFIED;
630         obj->damage = "1d1";
631         return(obj);
632 }
633
634 void
635 free_object(object *obj)
636 {
637         obj->next_object = free_list;
638         free_list = obj;
639 }
640
641 static void
642 make_party(void)
643 {
644         short n;
645
646         party_room = gr_room();
647
648         n = rand_percent(99) ? party_objects(party_room) : 11;
649         if (rand_percent(99)) {
650                 party_monsters(party_room, n);
651         }
652 }
653
654 void
655 show_objects(void)
656 {
657         object *obj;
658         short mc, rc, row, col;
659         object *monster;
660
661         obj = level_objects.next_object;
662
663         while (obj) {
664                 row = obj->row;
665                 col = obj->col;
666
667                 rc = get_mask_char(obj->what_is);
668
669                 if (dungeon[row][col] & MONSTER) {
670                         if ((monster = object_at(&level_monsters, row, col))) {
671                                 monster->trail_char = rc;
672                         }
673                 }
674                 mc = mvinch(row, col);
675                 if (((mc < 'A') || (mc > 'Z')) &&
676                         ((row != rogue.row) || (col != rogue.col))) {
677                         mvaddch(row, col, rc);
678                 }
679                 obj = obj->next_object;
680         }
681
682         monster = level_monsters.next_object;
683
684         while (monster) {
685                 if (monster->m_flags & IMITATES) {
686                         mvaddch(monster->row, monster->col, (int)monster->disguise);
687                 }
688                 monster = monster->next_monster;
689         }
690 }
691
692 void
693 put_amulet(void)
694 {
695         object *obj;
696
697         obj = alloc_object();
698         obj->what_is = AMULET;
699         rand_place(obj);
700 }
701
702 static void
703 rand_place(object *obj)
704 {
705         short row, col;
706
707         gr_row_col(&row, &col, (FLOOR | TUNNEL));
708         place_at(obj, row, col);
709 }
710
711 void
712 c_object_for_wizard(void)
713 {
714         short ch, max, wk;
715         object *obj;
716         char buf[80];
717
718         max = 0;
719         if (pack_count(NULL) >= MAX_PACK_COUNT) {
720                 message("pack full", 0);
721                 return;
722         }
723         message("type of object?", 0);
724
725         while (r_index("!?:)]=/,\033", (ch = rgetchar()), 0) == -1) {
726                 sound_bell();
727         }
728         check_message();
729
730         if (ch == '\033') {
731                 return;
732         }
733         obj = alloc_object();
734
735         switch(ch) {
736         case '!':
737                 obj->what_is = POTION;
738                 max = POTIONS - 1;
739                 break;
740         case '?':
741                 obj->what_is = SCROL;
742                 max = SCROLS - 1;
743                 break;
744         case ',':
745                 obj->what_is = AMULET;
746                 break;
747         case ':':
748                 get_food(obj, 0);
749                 break;
750         case ')':
751                 gr_weapon(obj, 0);
752                 max = WEAPONS - 1;
753                 break;
754         case ']':
755                 gr_armor(obj);
756                 max = ARMORS - 1;
757                 break;
758         case '/':
759                 gr_wand(obj);
760                 max = WANDS - 1;
761                 break;
762         case '=':
763                 max = RINGS - 1;
764                 obj->what_is = RING;
765                 break;
766         }
767         if ((ch != ',') && (ch != ':')) {
768 GIL:
769                 if (get_input_line("which kind?", "", buf, "", 0, 1)) {
770                         wk = get_number(buf);
771                         if ((wk >= 0) && (wk <= max)) {
772                                 obj->which_kind = (unsigned short)wk;
773                                 if (obj->what_is == RING) {
774                                         gr_ring(obj, 0);
775                                 }
776                         } else {
777                                 sound_bell();
778                                 goto GIL;
779                         }
780                 } else {
781                         free_object(obj);
782                         return;
783                 }
784         }
785         get_desc(obj, buf);
786         message(buf, 0);
787         add_to_pack(obj, &rogue.pack, 1);
788 }