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