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