Simplify the return value of sysctlnametomib.
[dragonfly.git] / games / rogue / object.c
blob0946a3d95ca9aeac9ec5ee6237cbfb922db22abf
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. 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.
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.
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.4 2006/09/02 19:31:07 pavalos Exp $
42 * object.c
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.
53 #include "rogue.h"
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;
61 fighter rogue = {
62 INIT_AW, /* armor, weapon */
63 INIT_RINGS, /* rings */
64 INIT_HP,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 */
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}
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}
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}
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)}
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}
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},
156 extern short cur_level, max_level;
157 extern short party_room;
158 extern boolean is_wood[];
160 static void put_gold(void);
161 static void plant_gold(short, short, boolean);
162 static unsigned short gr_what_is(void);
163 static void gr_scroll(object *);
164 static void gr_potion(object *);
165 static void gr_weapon(object *, int);
166 static void gr_armor(object *);
167 static void gr_wand(object *);
168 static void make_party(void);
169 static void rand_place(object *);
171 void
172 put_objects(void)
174 short i, n;
175 object *obj;
177 if (cur_level < max_level) {
178 return;
180 n = coin_toss() ? get_rand(2, 4) : get_rand(3, 5);
181 while (rand_percent(33)) {
182 n++;
184 if (party_room != NO_ROOM) {
185 make_party();
187 for (i = 0; i < n; i++) {
188 obj = gr_object();
189 rand_place(obj);
191 put_gold();
194 static void
195 put_gold(void)
197 short i, j;
198 short row,col;
199 boolean is_maze, is_room;
201 for (i = 0; i < MAXROOMS; i++) {
202 is_maze = (rooms[i].is_room & R_MAZE) ? 1 : 0;
203 is_room = (rooms[i].is_room & R_ROOM) ? 1 : 0;
205 if (!(is_room || is_maze)) {
206 continue;
208 if (is_maze || rand_percent(GOLD_PERCENT)) {
209 for (j = 0; j < 50; j++) {
210 row = get_rand(rooms[i].top_row+1,
211 rooms[i].bottom_row-1);
212 col = get_rand(rooms[i].left_col+1,
213 rooms[i].right_col-1);
214 if ((dungeon[row][col] == FLOOR) ||
215 (dungeon[row][col] == TUNNEL)) {
216 plant_gold(row, col, is_maze);
217 break;
224 static void
225 plant_gold(short row, short col, boolean is_maze)
227 object *obj;
229 obj = alloc_object();
230 obj->row = row; obj->col = col;
231 obj->what_is = GOLD;
232 obj->quantity = get_rand((2 * cur_level), (16 * cur_level));
233 if (is_maze) {
234 obj->quantity += obj->quantity / 2;
236 dungeon[row][col] |= OBJECT;
237 add_to_pack(obj, &level_objects, 0);
240 void
241 place_at(object *obj, int row, int col)
243 obj->row = row;
244 obj->col = col;
245 dungeon[row][col] |= OBJECT;
246 add_to_pack(obj, &level_objects, 0);
249 object *
250 object_at(object *pack, short row, short col)
252 object *obj = (object *) 0;
254 if (dungeon[row][col] & (MONSTER | OBJECT)) {
255 obj = pack->next_object;
257 while (obj && ((obj->row != row) || (obj->col != col))) {
258 obj = obj->next_object;
260 if (!obj) {
261 message("object_at(): inconsistent", 1);
264 return(obj);
267 object *
268 get_letter_object(int ch)
270 object *obj;
272 obj = rogue.pack.next_object;
274 while (obj && (obj->ichar != ch)) {
275 obj = obj->next_object;
277 return(obj);
280 void
281 free_stuff(object *objlist)
283 object *obj;
285 while (objlist->next_object) {
286 obj = objlist->next_object;
287 objlist->next_object =
288 objlist->next_object->next_object;
289 free_object(obj);
293 const char *
294 name_of(const object *obj)
296 const char *retstring;
298 switch(obj->what_is) {
299 case SCROL:
300 retstring = obj->quantity > 1 ? "scrolls " : "scroll ";
301 break;
302 case POTION:
303 retstring = obj->quantity > 1 ? "potions " : "potion ";
304 break;
305 case FOOD:
306 if (obj->which_kind == RATION) {
307 retstring = "food ";
308 } else {
309 retstring = fruit;
311 break;
312 case WAND:
313 retstring = is_wood[obj->which_kind] ? "staff " : "wand ";
314 break;
315 case WEAPON:
316 switch(obj->which_kind) {
317 case DART:
318 retstring=obj->quantity > 1 ? "darts " : "dart ";
319 break;
320 case ARROW:
321 retstring=obj->quantity > 1 ? "arrows " : "arrow ";
322 break;
323 case DAGGER:
324 retstring=obj->quantity > 1 ? "daggers " : "dagger ";
325 break;
326 case SHURIKEN:
327 retstring=obj->quantity > 1?"shurikens ":"shuriken ";
328 break;
329 default:
330 retstring = id_weapons[obj->which_kind].title;
332 break;
333 case ARMOR:
334 retstring = "armor ";
335 break;
336 case RING:
337 retstring = "ring ";
338 break;
339 case AMULET:
340 retstring = "amulet ";
341 break;
342 default:
343 retstring = "unknown ";
344 break;
346 return(retstring);
349 object *
350 gr_object(void)
352 object *obj;
354 obj = alloc_object();
356 if (foods < (cur_level / 3)) {
357 obj->what_is = FOOD;
358 foods++;
359 } else {
360 obj->what_is = gr_what_is();
362 switch(obj->what_is) {
363 case SCROL:
364 gr_scroll(obj);
365 break;
366 case POTION:
367 gr_potion(obj);
368 break;
369 case WEAPON:
370 gr_weapon(obj, 1);
371 break;
372 case ARMOR:
373 gr_armor(obj);
374 break;
375 case WAND:
376 gr_wand(obj);
377 break;
378 case FOOD:
379 get_food(obj, 0);
380 break;
381 case RING:
382 gr_ring(obj, 1);
383 break;
385 return(obj);
388 static unsigned short
389 gr_what_is(void)
391 short percent;
392 unsigned short what_is;
394 percent = get_rand(1, 91);
396 if (percent <= 30) {
397 what_is = SCROL;
398 } else if (percent <= 60) {
399 what_is = POTION;
400 } else if (percent <= 64) {
401 what_is = WAND;
402 } else if (percent <= 74) {
403 what_is = WEAPON;
404 } else if (percent <= 83) {
405 what_is = ARMOR;
406 } else if (percent <= 88) {
407 what_is = FOOD;
408 } else {
409 what_is = RING;
411 return(what_is);
414 static void
415 gr_scroll(object *obj)
417 short percent;
419 percent = get_rand(0, 91);
421 obj->what_is = SCROL;
423 if (percent <= 5) {
424 obj->which_kind = PROTECT_ARMOR;
425 } else if (percent <= 10) {
426 obj->which_kind = HOLD_MONSTER;
427 } else if (percent <= 20) {
428 obj->which_kind = CREATE_MONSTER;
429 } else if (percent <= 35) {
430 obj->which_kind = IDENTIFY;
431 } else if (percent <= 43) {
432 obj->which_kind = TELEPORT;
433 } else if (percent <= 50) {
434 obj->which_kind = SLEEP;
435 } else if (percent <= 55) {
436 obj->which_kind = SCARE_MONSTER;
437 } else if (percent <= 64) {
438 obj->which_kind = REMOVE_CURSE;
439 } else if (percent <= 69) {
440 obj->which_kind = ENCH_ARMOR;
441 } else if (percent <= 74) {
442 obj->which_kind = ENCH_WEAPON;
443 } else if (percent <= 80) {
444 obj->which_kind = AGGRAVATE_MONSTER;
445 } else if (percent <= 86) {
446 obj->which_kind = CON_MON;
447 } else {
448 obj->which_kind = MAGIC_MAPPING;
452 static void
453 gr_potion(object *obj)
455 short percent;
457 percent = get_rand(1, 118);
459 obj->what_is = POTION;
461 if (percent <= 5) {
462 obj->which_kind = RAISE_LEVEL;
463 } else if (percent <= 15) {
464 obj->which_kind = DETECT_OBJECTS;
465 } else if (percent <= 25) {
466 obj->which_kind = DETECT_MONSTER;
467 } else if (percent <= 35) {
468 obj->which_kind = INCREASE_STRENGTH;
469 } else if (percent <= 45) {
470 obj->which_kind = RESTORE_STRENGTH;
471 } else if (percent <= 55) {
472 obj->which_kind = HEALING;
473 } else if (percent <= 65) {
474 obj->which_kind = EXTRA_HEALING;
475 } else if (percent <= 75) {
476 obj->which_kind = BLINDNESS;
477 } else if (percent <= 85) {
478 obj->which_kind = HALLUCINATION;
479 } else if (percent <= 95) {
480 obj->which_kind = CONFUSION;
481 } else if (percent <= 105) {
482 obj->which_kind = POISON;
483 } else if (percent <= 110) {
484 obj->which_kind = LEVITATION;
485 } else if (percent <= 114) {
486 obj->which_kind = HASTE_SELF;
487 } else {
488 obj->which_kind = SEE_INVISIBLE;
492 static void
493 gr_weapon(object *obj, int assign_wk)
495 short percent;
496 short i;
497 short blessing, increment;
499 obj->what_is = WEAPON;
500 if (assign_wk) {
501 obj->which_kind = get_rand(0, (WEAPONS - 1));
503 if ((obj->which_kind == ARROW) || (obj->which_kind == DAGGER) ||
504 (obj->which_kind == SHURIKEN) | (obj->which_kind == DART)) {
505 obj->quantity = get_rand(3, 15);
506 obj->quiver = get_rand(0, 126);
507 } else {
508 obj->quantity = 1;
510 obj->hit_enchant = obj->d_enchant = 0;
512 percent = get_rand(1, 96);
513 blessing = get_rand(1, 3);
515 if (percent <= 16) {
516 increment = 1;
517 } else if (percent <= 32) {
518 increment = -1;
519 obj->is_cursed = 1;
521 if (percent <= 32) {
522 for (i = 0; i < blessing; i++) {
523 if (coin_toss()) {
524 obj->hit_enchant += increment;
525 } else {
526 obj->d_enchant += increment;
530 switch(obj->which_kind) {
531 case BOW:
532 case DART:
533 obj->damage = "1d1";
534 break;
535 case ARROW:
536 obj->damage = "1d2";
537 break;
538 case DAGGER:
539 obj->damage = "1d3";
540 break;
541 case SHURIKEN:
542 obj->damage = "1d4";
543 break;
544 case MACE:
545 obj->damage = "2d3";
546 break;
547 case LONG_SWORD:
548 obj->damage = "3d4";
549 break;
550 case TWO_HANDED_SWORD:
551 obj->damage = "4d5";
552 break;
556 static void
557 gr_armor(object *obj)
559 short percent;
560 short blessing;
562 obj->what_is = ARMOR;
563 obj->which_kind = get_rand(0, (ARMORS - 1));
564 obj->class = obj->which_kind + 2;
565 if ((obj->which_kind == PLATE) || (obj->which_kind == SPLINT)) {
566 obj->class--;
568 obj->is_protected = 0;
569 obj->d_enchant = 0;
571 percent = get_rand(1, 100);
572 blessing = get_rand(1, 3);
574 if (percent <= 16) {
575 obj->is_cursed = 1;
576 obj->d_enchant -= blessing;
577 } else if (percent <= 33) {
578 obj->d_enchant += blessing;
582 static void
583 gr_wand(object *obj)
585 obj->what_is = WAND;
586 obj->which_kind = get_rand(0, (WANDS - 1));
587 obj->class = get_rand(3, 7);
590 void
591 get_food(object *obj, boolean force_ration)
593 obj->what_is = FOOD;
595 if (force_ration || rand_percent(80)) {
596 obj->which_kind = RATION;
597 } else {
598 obj->which_kind = FRUIT;
602 void
603 put_stairs(void)
605 short row, col;
607 gr_row_col(&row, &col, (FLOOR | TUNNEL));
608 dungeon[row][col] |= STAIRS;
611 short
612 get_armor_class(const object *obj)
614 if (obj) {
615 return(obj->class + obj->d_enchant);
617 return(0);
620 object *
621 alloc_object(void)
623 object *obj;
625 if (free_list) {
626 obj = free_list;
627 free_list = free_list->next_object;
628 } else if (!(obj = (object *) md_malloc(sizeof(object)))) {
629 message("cannot allocate object, saving game", 0);
630 save_into_file(error_file);
632 obj->quantity = 1;
633 obj->ichar = 'L';
634 obj->picked_up = obj->is_cursed = 0;
635 obj->in_use_flags = NOT_USED;
636 obj->identified = UNIDENTIFIED;
637 obj->damage = "1d1";
638 return(obj);
641 void
642 free_object(object *obj)
644 obj->next_object = free_list;
645 free_list = obj;
648 static void
649 make_party(void)
651 short n;
653 party_room = gr_room();
655 n = rand_percent(99) ? party_objects(party_room) : 11;
656 if (rand_percent(99)) {
657 party_monsters(party_room, n);
661 void
662 show_objects(void)
664 object *obj;
665 short mc, rc, row, col;
666 object *monster;
668 obj = level_objects.next_object;
670 while (obj) {
671 row = obj->row;
672 col = obj->col;
674 rc = get_mask_char(obj->what_is);
676 if (dungeon[row][col] & MONSTER) {
677 if ((monster = object_at(&level_monsters, row, col))) {
678 monster->trail_char = rc;
681 mc = mvinch(row, col);
682 if (((mc < 'A') || (mc > 'Z')) &&
683 ((row != rogue.row) || (col != rogue.col))) {
684 mvaddch(row, col, rc);
686 obj = obj->next_object;
689 monster = level_monsters.next_object;
691 while (monster) {
692 if (monster->m_flags & IMITATES) {
693 mvaddch(monster->row, monster->col, (int) monster->disguise);
695 monster = monster->next_monster;
699 void
700 put_amulet(void)
702 object *obj;
704 obj = alloc_object();
705 obj->what_is = AMULET;
706 rand_place(obj);
709 static void
710 rand_place(object *obj)
712 short row, col;
714 gr_row_col(&row, &col, (FLOOR | TUNNEL));
715 place_at(obj, row, col);
718 void
719 c_object_for_wizard(void)
721 short ch, max = 0, wk;
722 object *obj;
723 char buf[80];
725 if (pack_count((object *) 0) >= MAX_PACK_COUNT) {
726 message("pack full", 0);
727 return;
729 message("type of object?", 0);
731 while (r_index("!?:)]=/,\033", (ch = rgetchar()), 0) == -1) {
732 sound_bell();
734 check_message();
736 if (ch == '\033') {
737 return;
739 obj = alloc_object();
741 switch(ch) {
742 case '!':
743 obj->what_is = POTION;
744 max = POTIONS - 1;
745 break;
746 case '?':
747 obj->what_is = SCROL;
748 max = SCROLS - 1;
749 break;
750 case ',':
751 obj->what_is = AMULET;
752 break;
753 case ':':
754 get_food(obj, 0);
755 break;
756 case ')':
757 gr_weapon(obj, 0);
758 max = WEAPONS - 1;
759 break;
760 case ']':
761 gr_armor(obj);
762 max = ARMORS - 1;
763 break;
764 case '/':
765 gr_wand(obj);
766 max = WANDS - 1;
767 break;
768 case '=':
769 max = RINGS - 1;
770 obj->what_is = RING;
771 break;
773 if ((ch != ',') && (ch != ':')) {
774 GIL:
775 if (get_input_line("which kind?", "", buf, "", 0, 1)) {
776 wk = get_number(buf);
777 if ((wk >= 0) && (wk <= max)) {
778 obj->which_kind = (unsigned short) wk;
779 if (obj->what_is == RING) {
780 gr_ring(obj, 0);
782 } else {
783 sound_bell();
784 goto GIL;
786 } else {
787 free_object(obj);
788 return;
791 get_desc(obj, buf);
792 message(buf, 0);
793 add_to_pack(obj, &rogue.pack, 1);