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