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