kernel - Fix deadlock in sound system
[dragonfly.git] / games / rogue / object.c
blob93396122d7ee429c24113174bc803211b35d72f2
1 /*-
2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Timothy C. Stoehr.
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.
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.
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 * $DragonFly: src/games/rogue/object.c,v 1.4 2006/09/02 19:31:07 pavalos Exp $
38 * object.c
40 * This source herein may be modified and/or distributed by anybody who
41 * so desires, with the following restrictions:
42 * 1.) No portion of this notice shall be removed.
43 * 2.) Credit shall not be taken for the creation of this source.
44 * 3.) This code is not to be traded, sold, or used for personal
45 * gain or profit.
49 #include "rogue.h"
51 object level_objects;
52 unsigned short dungeon[DROWS][DCOLS];
53 short foods = 0;
54 object *free_list = NULL;
55 char *fruit = NULL;
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 */
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}
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}
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}
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)}
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}
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},
152 extern short cur_level, max_level;
153 extern short party_room;
154 extern boolean is_wood[];
156 static void gr_armor(object *);
157 static void gr_potion(object *);
158 static void gr_scroll(object *);
159 static void gr_wand(object *);
160 static void gr_weapon(object *, int);
161 static unsigned short gr_what_is(void);
162 static void make_party(void);
163 static void plant_gold(short, short, boolean);
164 static void put_gold(void);
165 static void rand_place(object *);
167 void
168 put_objects(void)
170 short i, n;
171 object *obj;
173 if (cur_level < max_level) {
174 return;
176 n = coin_toss() ? get_rand(2, 4) : get_rand(3, 5);
177 while (rand_percent(33)) {
178 n++;
180 if (party_room != NO_ROOM) {
181 make_party();
183 for (i = 0; i < n; i++) {
184 obj = gr_object();
185 rand_place(obj);
187 put_gold();
190 static void
191 put_gold(void)
193 short i, j;
194 short row,col;
195 boolean is_maze, is_room;
197 for (i = 0; i < MAXROOMS; i++) {
198 is_maze = (rooms[i].is_room & R_MAZE) ? 1 : 0;
199 is_room = (rooms[i].is_room & R_ROOM) ? 1 : 0;
201 if (!(is_room || is_maze)) {
202 continue;
204 if (is_maze || rand_percent(GOLD_PERCENT)) {
205 for (j = 0; j < 50; j++) {
206 row = get_rand(rooms[i].top_row+1,
207 rooms[i].bottom_row-1);
208 col = get_rand(rooms[i].left_col+1,
209 rooms[i].right_col-1);
210 if ((dungeon[row][col] == FLOOR) ||
211 (dungeon[row][col] == TUNNEL)) {
212 plant_gold(row, col, is_maze);
213 break;
220 static void
221 plant_gold(short row, short col, boolean is_maze)
223 object *obj;
225 obj = alloc_object();
226 obj->row = row; obj->col = col;
227 obj->what_is = GOLD;
228 obj->quantity = get_rand((2 * cur_level), (16 * cur_level));
229 if (is_maze) {
230 obj->quantity += obj->quantity / 2;
232 dungeon[row][col] |= OBJECT;
233 add_to_pack(obj, &level_objects, 0);
236 void
237 place_at(object *obj, int row, int col)
239 obj->row = row;
240 obj->col = col;
241 dungeon[row][col] |= OBJECT;
242 add_to_pack(obj, &level_objects, 0);
245 object *
246 object_at(object *pack, short row, short col)
248 object *obj = NULL;
250 if (dungeon[row][col] & (MONSTER | OBJECT)) {
251 obj = pack->next_object;
253 while (obj && ((obj->row != row) || (obj->col != col))) {
254 obj = obj->next_object;
256 if (!obj) {
257 message("object_at(): inconsistent", 1);
260 return(obj);
263 object *
264 get_letter_object(int ch)
266 object *obj;
268 obj = rogue.pack.next_object;
270 while (obj && (obj->ichar != ch)) {
271 obj = obj->next_object;
273 return(obj);
276 void
277 free_stuff(object *objlist)
279 object *obj;
281 while (objlist->next_object) {
282 obj = objlist->next_object;
283 objlist->next_object =
284 objlist->next_object->next_object;
285 free_object(obj);
289 const char *
290 name_of(const object *obj)
292 const char *retstring;
294 switch(obj->what_is) {
295 case SCROL:
296 retstring = obj->quantity > 1 ? "scrolls " : "scroll ";
297 break;
298 case POTION:
299 retstring = obj->quantity > 1 ? "potions " : "potion ";
300 break;
301 case FOOD:
302 if (obj->which_kind == RATION) {
303 retstring = "food ";
304 } else {
305 retstring = fruit;
307 break;
308 case WAND:
309 retstring = is_wood[obj->which_kind] ? "staff " : "wand ";
310 break;
311 case WEAPON:
312 switch(obj->which_kind) {
313 case DART:
314 retstring=obj->quantity > 1 ? "darts " : "dart ";
315 break;
316 case ARROW:
317 retstring=obj->quantity > 1 ? "arrows " : "arrow ";
318 break;
319 case DAGGER:
320 retstring=obj->quantity > 1 ? "daggers " : "dagger ";
321 break;
322 case SHURIKEN:
323 retstring=obj->quantity > 1?"shurikens ":"shuriken ";
324 break;
325 default:
326 retstring = id_weapons[obj->which_kind].title;
328 break;
329 case ARMOR:
330 retstring = "armor ";
331 break;
332 case RING:
333 retstring = "ring ";
334 break;
335 case AMULET:
336 retstring = "amulet ";
337 break;
338 default:
339 retstring = "unknown ";
340 break;
342 return(retstring);
345 object *
346 gr_object(void)
348 object *obj;
350 obj = alloc_object();
352 if (foods < (cur_level / 3)) {
353 obj->what_is = FOOD;
354 foods++;
355 } else {
356 obj->what_is = gr_what_is();
358 switch(obj->what_is) {
359 case SCROL:
360 gr_scroll(obj);
361 break;
362 case POTION:
363 gr_potion(obj);
364 break;
365 case WEAPON:
366 gr_weapon(obj, 1);
367 break;
368 case ARMOR:
369 gr_armor(obj);
370 break;
371 case WAND:
372 gr_wand(obj);
373 break;
374 case FOOD:
375 get_food(obj, 0);
376 break;
377 case RING:
378 gr_ring(obj, 1);
379 break;
381 return(obj);
384 static unsigned short
385 gr_what_is(void)
387 short percent;
388 unsigned short what_is;
390 percent = get_rand(1, 91);
392 if (percent <= 30) {
393 what_is = SCROL;
394 } else if (percent <= 60) {
395 what_is = POTION;
396 } else if (percent <= 64) {
397 what_is = WAND;
398 } else if (percent <= 74) {
399 what_is = WEAPON;
400 } else if (percent <= 83) {
401 what_is = ARMOR;
402 } else if (percent <= 88) {
403 what_is = FOOD;
404 } else {
405 what_is = RING;
407 return(what_is);
410 static void
411 gr_scroll(object *obj)
413 short percent;
415 percent = get_rand(0, 91);
417 obj->what_is = SCROL;
419 if (percent <= 5) {
420 obj->which_kind = PROTECT_ARMOR;
421 } else if (percent <= 10) {
422 obj->which_kind = HOLD_MONSTER;
423 } else if (percent <= 20) {
424 obj->which_kind = CREATE_MONSTER;
425 } else if (percent <= 35) {
426 obj->which_kind = IDENTIFY;
427 } else if (percent <= 43) {
428 obj->which_kind = TELEPORT;
429 } else if (percent <= 50) {
430 obj->which_kind = SLEEP;
431 } else if (percent <= 55) {
432 obj->which_kind = SCARE_MONSTER;
433 } else if (percent <= 64) {
434 obj->which_kind = REMOVE_CURSE;
435 } else if (percent <= 69) {
436 obj->which_kind = ENCH_ARMOR;
437 } else if (percent <= 74) {
438 obj->which_kind = ENCH_WEAPON;
439 } else if (percent <= 80) {
440 obj->which_kind = AGGRAVATE_MONSTER;
441 } else if (percent <= 86) {
442 obj->which_kind = CON_MON;
443 } else {
444 obj->which_kind = MAGIC_MAPPING;
448 static void
449 gr_potion(object *obj)
451 short percent;
453 percent = get_rand(1, 118);
455 obj->what_is = POTION;
457 if (percent <= 5) {
458 obj->which_kind = RAISE_LEVEL;
459 } else if (percent <= 15) {
460 obj->which_kind = DETECT_OBJECTS;
461 } else if (percent <= 25) {
462 obj->which_kind = DETECT_MONSTER;
463 } else if (percent <= 35) {
464 obj->which_kind = INCREASE_STRENGTH;
465 } else if (percent <= 45) {
466 obj->which_kind = RESTORE_STRENGTH;
467 } else if (percent <= 55) {
468 obj->which_kind = HEALING;
469 } else if (percent <= 65) {
470 obj->which_kind = EXTRA_HEALING;
471 } else if (percent <= 75) {
472 obj->which_kind = BLINDNESS;
473 } else if (percent <= 85) {
474 obj->which_kind = HALLUCINATION;
475 } else if (percent <= 95) {
476 obj->which_kind = CONFUSION;
477 } else if (percent <= 105) {
478 obj->which_kind = POISON;
479 } else if (percent <= 110) {
480 obj->which_kind = LEVITATION;
481 } else if (percent <= 114) {
482 obj->which_kind = HASTE_SELF;
483 } else {
484 obj->which_kind = SEE_INVISIBLE;
488 static void
489 gr_weapon(object *obj, int assign_wk)
491 short percent;
492 short i;
493 short blessing, increment;
495 obj->what_is = WEAPON;
496 if (assign_wk) {
497 obj->which_kind = get_rand(0, (WEAPONS - 1));
499 if ((obj->which_kind == ARROW) || (obj->which_kind == DAGGER) ||
500 (obj->which_kind == SHURIKEN) | (obj->which_kind == DART)) {
501 obj->quantity = get_rand(3, 15);
502 obj->quiver = get_rand(0, 126);
503 } else {
504 obj->quantity = 1;
506 obj->hit_enchant = obj->d_enchant = 0;
508 percent = get_rand(1, 96);
509 blessing = get_rand(1, 3);
511 if (percent <= 16) {
512 increment = 1;
513 } else if (percent <= 32) {
514 increment = -1;
515 obj->is_cursed = 1;
517 if (percent <= 32) {
518 for (i = 0; i < blessing; i++) {
519 if (coin_toss()) {
520 obj->hit_enchant += increment;
521 } else {
522 obj->d_enchant += increment;
526 switch(obj->which_kind) {
527 case BOW:
528 case DART:
529 obj->damage = "1d1";
530 break;
531 case ARROW:
532 obj->damage = "1d2";
533 break;
534 case DAGGER:
535 obj->damage = "1d3";
536 break;
537 case SHURIKEN:
538 obj->damage = "1d4";
539 break;
540 case MACE:
541 obj->damage = "2d3";
542 break;
543 case LONG_SWORD:
544 obj->damage = "3d4";
545 break;
546 case TWO_HANDED_SWORD:
547 obj->damage = "4d5";
548 break;
552 static void
553 gr_armor(object *obj)
555 short percent;
556 short blessing;
558 obj->what_is = ARMOR;
559 obj->which_kind = get_rand(0, (ARMORS - 1));
560 obj->class = obj->which_kind + 2;
561 if ((obj->which_kind == PLATE) || (obj->which_kind == SPLINT)) {
562 obj->class--;
564 obj->is_protected = 0;
565 obj->d_enchant = 0;
567 percent = get_rand(1, 100);
568 blessing = get_rand(1, 3);
570 if (percent <= 16) {
571 obj->is_cursed = 1;
572 obj->d_enchant -= blessing;
573 } else if (percent <= 33) {
574 obj->d_enchant += blessing;
578 static void
579 gr_wand(object *obj)
581 obj->what_is = WAND;
582 obj->which_kind = get_rand(0, (WANDS - 1));
583 obj->class = get_rand(3, 7);
586 void
587 get_food(object *obj, boolean force_ration)
589 obj->what_is = FOOD;
591 if (force_ration || rand_percent(80)) {
592 obj->which_kind = RATION;
593 } else {
594 obj->which_kind = FRUIT;
598 void
599 put_stairs(void)
601 short row, col;
603 gr_row_col(&row, &col, (FLOOR | TUNNEL));
604 dungeon[row][col] |= STAIRS;
607 short
608 get_armor_class(const object *obj)
610 if (obj) {
611 return(obj->class + obj->d_enchant);
613 return(0);
616 object *
617 alloc_object(void)
619 object *obj;
621 if (free_list) {
622 obj = free_list;
623 free_list = free_list->next_object;
624 } else if (!(obj = (object *) md_malloc(sizeof(object)))) {
625 message("cannot allocate object, saving game", 0);
626 save_into_file(error_file);
628 obj->quantity = 1;
629 obj->ichar = 'L';
630 obj->picked_up = obj->is_cursed = 0;
631 obj->in_use_flags = NOT_USED;
632 obj->identified = UNIDENTIFIED;
633 obj->damage = "1d1";
634 return(obj);
637 void
638 free_object(object *obj)
640 obj->next_object = free_list;
641 free_list = obj;
644 static void
645 make_party(void)
647 short n;
649 party_room = gr_room();
651 n = rand_percent(99) ? party_objects(party_room) : 11;
652 if (rand_percent(99)) {
653 party_monsters(party_room, n);
657 void
658 show_objects(void)
660 object *obj;
661 short mc, rc, row, col;
662 object *monster;
664 obj = level_objects.next_object;
666 while (obj) {
667 row = obj->row;
668 col = obj->col;
670 rc = get_mask_char(obj->what_is);
672 if (dungeon[row][col] & MONSTER) {
673 if ((monster = object_at(&level_monsters, row, col))) {
674 monster->trail_char = rc;
677 mc = mvinch(row, col);
678 if (((mc < 'A') || (mc > 'Z')) &&
679 ((row != rogue.row) || (col != rogue.col))) {
680 mvaddch(row, col, rc);
682 obj = obj->next_object;
685 monster = level_monsters.next_object;
687 while (monster) {
688 if (monster->m_flags & IMITATES) {
689 mvaddch(monster->row, monster->col, (int)monster->disguise);
691 monster = monster->next_monster;
695 void
696 put_amulet(void)
698 object *obj;
700 obj = alloc_object();
701 obj->what_is = AMULET;
702 rand_place(obj);
705 static void
706 rand_place(object *obj)
708 short row, col;
710 gr_row_col(&row, &col, (FLOOR | TUNNEL));
711 place_at(obj, row, col);
714 void
715 c_object_for_wizard(void)
717 short ch, max, wk;
718 object *obj;
719 char buf[80];
721 max = 0;
722 if (pack_count(NULL) >= MAX_PACK_COUNT) {
723 message("pack full", 0);
724 return;
726 message("type of object?", 0);
728 while (r_index("!?:)]=/,\033", (ch = rgetchar()), 0) == -1) {
729 sound_bell();
731 check_message();
733 if (ch == '\033') {
734 return;
736 obj = alloc_object();
738 switch(ch) {
739 case '!':
740 obj->what_is = POTION;
741 max = POTIONS - 1;
742 break;
743 case '?':
744 obj->what_is = SCROL;
745 max = SCROLS - 1;
746 break;
747 case ',':
748 obj->what_is = AMULET;
749 break;
750 case ':':
751 get_food(obj, 0);
752 break;
753 case ')':
754 gr_weapon(obj, 0);
755 max = WEAPONS - 1;
756 break;
757 case ']':
758 gr_armor(obj);
759 max = ARMORS - 1;
760 break;
761 case '/':
762 gr_wand(obj);
763 max = WANDS - 1;
764 break;
765 case '=':
766 max = RINGS - 1;
767 obj->what_is = RING;
768 break;
770 if ((ch != ',') && (ch != ':')) {
771 GIL:
772 if (get_input_line("which kind?", "", buf, "", 0, 1)) {
773 wk = get_number(buf);
774 if ((wk >= 0) && (wk <= max)) {
775 obj->which_kind = (unsigned short)wk;
776 if (obj->what_is == RING) {
777 gr_ring(obj, 0);
779 } else {
780 sound_bell();
781 goto GIL;
783 } else {
784 free_object(obj);
785 return;
788 get_desc(obj, buf);
789 message(buf, 0);
790 add_to_pack(obj, &rogue.pack, 1);