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