Home | History | Annotate | Line # | Download | only in rogue
pack.c revision 1.8
      1 /*	$NetBSD: pack.c,v 1.8 2007/12/27 23:53:00 dholland 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[] = "@(#)pack.c	8.1 (Berkeley) 5/31/93";
     39 #else
     40 __RCSID("$NetBSD: pack.c,v 1.8 2007/12/27 23:53:00 dholland Exp $");
     41 #endif
     42 #endif /* not lint */
     43 
     44 /*
     45  * pack.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 const char *curse_message = "you can't, it appears to be cursed";
     59 
     60 object *
     61 add_to_pack(obj, pack, condense)
     62 	object *obj, *pack;
     63 	int condense;
     64 {
     65 	object *op;
     66 
     67 	if (condense) {
     68 		if ((op = check_duplicate(obj, pack)) != NULL) {
     69 			free_object(obj);
     70 			return(op);
     71 		} else {
     72 			obj->ichar = next_avail_ichar();
     73 		}
     74 	}
     75 	if (pack->next_object == 0) {
     76 		pack->next_object = obj;
     77 	} else {
     78 		op = pack->next_object;
     79 
     80 		while (op->next_object) {
     81 			op = op->next_object;
     82 		}
     83 		op->next_object = obj;
     84 	}
     85 	obj->next_object = 0;
     86 	return(obj);
     87 }
     88 
     89 void
     90 take_from_pack(obj, pack)
     91 	object *obj, *pack;
     92 {
     93 	while (pack->next_object != obj) {
     94 		pack = pack->next_object;
     95 	}
     96 	pack->next_object = pack->next_object->next_object;
     97 }
     98 
     99 /* Note: *status is set to 0 if the rogue attempts to pick up a scroll
    100  * of scare-monster and it turns to dust.  *status is otherwise set to 1.
    101  */
    102 
    103 object *
    104 pick_up(row, col, status)
    105 	short *status;
    106 	int row, col;
    107 {
    108 	object *obj;
    109 
    110 	*status = 1;
    111 
    112 	if (levitate) {
    113 		messagef(0, "you're floating in the air!");
    114 		return((object *) 0);
    115 	}
    116 	obj = object_at(&level_objects, row, col);
    117 	if (!obj) {
    118 		messagef(1, "pick_up(): inconsistent");
    119 		return(obj);
    120 	}
    121 	if (	(obj->what_is == SCROL) &&
    122 			(obj->which_kind == SCARE_MONSTER) &&
    123 			obj->picked_up) {
    124 		messagef(0, "the scroll turns to dust as you pick it up");
    125 		dungeon[row][col] &= (~OBJECT);
    126 		vanish(obj, 0, &level_objects);
    127 		*status = 0;
    128 		if (id_scrolls[SCARE_MONSTER].id_status == UNIDENTIFIED) {
    129 			id_scrolls[SCARE_MONSTER].id_status = IDENTIFIED;
    130 		}
    131 		return((object *) 0);
    132 	}
    133 	if (obj->what_is == GOLD) {
    134 		rogue.gold += obj->quantity;
    135 		dungeon[row][col] &= ~(OBJECT);
    136 		take_from_pack(obj, &level_objects);
    137 		print_stats(STAT_GOLD);
    138 		return(obj);	/* obj will be free_object()ed in caller */
    139 	}
    140 	if (pack_count(obj) >= MAX_PACK_COUNT) {
    141 		messagef(1, "pack too full");
    142 		return((object *) 0);
    143 	}
    144 	dungeon[row][col] &= ~(OBJECT);
    145 	take_from_pack(obj, &level_objects);
    146 	obj = add_to_pack(obj, &rogue.pack, 1);
    147 	obj->picked_up = 1;
    148 	return(obj);
    149 }
    150 
    151 void
    152 drop()
    153 {
    154 	object *obj, *new;
    155 	short ch;
    156 	char desc[DCOLS];
    157 
    158 	if (dungeon[rogue.row][rogue.col] & (OBJECT | STAIRS | TRAP)) {
    159 		messagef(0, "there's already something there");
    160 		return;
    161 	}
    162 	if (!rogue.pack.next_object) {
    163 		messagef(0, "you have nothing to drop");
    164 		return;
    165 	}
    166 	if ((ch = pack_letter("drop what?", ALL_OBJECTS)) == CANCEL) {
    167 		return;
    168 	}
    169 	if (!(obj = get_letter_object(ch))) {
    170 		messagef(0, "no such item.");
    171 		return;
    172 	}
    173 	if (obj->in_use_flags & BEING_WIELDED) {
    174 		if (obj->is_cursed) {
    175 			messagef(0, "%s", curse_message);
    176 			return;
    177 		}
    178 		unwield(rogue.weapon);
    179 	} else if (obj->in_use_flags & BEING_WORN) {
    180 		if (obj->is_cursed) {
    181 			messagef(0, "%s", curse_message);
    182 			return;
    183 		}
    184 		mv_aquatars();
    185 		unwear(rogue.armor);
    186 		print_stats(STAT_ARMOR);
    187 	} else if (obj->in_use_flags & ON_EITHER_HAND) {
    188 		if (obj->is_cursed) {
    189 			messagef(0, "%s", curse_message);
    190 			return;
    191 		}
    192 		un_put_on(obj);
    193 	}
    194 	obj->row = rogue.row;
    195 	obj->col = rogue.col;
    196 
    197 	if ((obj->quantity > 1) && (obj->what_is != WEAPON)) {
    198 		obj->quantity--;
    199 		new = alloc_object();
    200 		*new = *obj;
    201 		new->quantity = 1;
    202 		obj = new;
    203 	} else {
    204 		obj->ichar = 'L';
    205 		take_from_pack(obj, &rogue.pack);
    206 	}
    207 	place_at(obj, rogue.row, rogue.col);
    208 	get_desc(obj, desc, sizeof(desc));
    209 	messagef(0, "dropped %s", desc);
    210 	(void) reg_move();
    211 }
    212 
    213 object *
    214 check_duplicate(obj, pack)
    215 	object *obj, *pack;
    216 {
    217 	object *op;
    218 
    219 	if (!(obj->what_is & (WEAPON | FOOD | SCROL | POTION))) {
    220 		return(0);
    221 	}
    222 	if ((obj->what_is == FOOD) && (obj->which_kind == FRUIT)) {
    223 		return(0);
    224 	}
    225 	op = pack->next_object;
    226 
    227 	while (op) {
    228 		if ((op->what_is == obj->what_is) &&
    229 			(op->which_kind == obj->which_kind)) {
    230 
    231 			if ((obj->what_is != WEAPON) ||
    232 			((obj->what_is == WEAPON) &&
    233 			((obj->which_kind == ARROW) ||
    234 			(obj->which_kind == DAGGER) ||
    235 			(obj->which_kind == DART) ||
    236 			(obj->which_kind == SHURIKEN)) &&
    237 			(obj->quiver == op->quiver))) {
    238 				op->quantity += obj->quantity;
    239 				return(op);
    240 			}
    241 		}
    242 		op = op->next_object;
    243 	}
    244 	return(0);
    245 }
    246 
    247 short
    248 next_avail_ichar()
    249 {
    250 	object *obj;
    251 	int i;
    252 	boolean ichars[26];
    253 
    254 	for (i = 0; i < 26; i++) {
    255 		ichars[i] = 0;
    256 	}
    257 	obj = rogue.pack.next_object;
    258 	while (obj) {
    259 		if (obj->ichar >= 'a' && obj->ichar <= 'z') {
    260 			ichars[(obj->ichar - 'a')] = 1;
    261 		}
    262 		obj = obj->next_object;
    263 	}
    264 	for (i = 0; i < 26; i++) {
    265 		if (!ichars[i]) {
    266 			return(i + 'a');
    267 		}
    268 	}
    269 	return('?');
    270 }
    271 
    272 void
    273 wait_for_ack()
    274 {
    275 	while (rgetchar() != ' ') ;
    276 }
    277 
    278 short
    279 pack_letter(prompt, mask)
    280 	const char *prompt;
    281 	unsigned short mask;
    282 {
    283 	short ch;
    284 	unsigned short tmask = mask;
    285 
    286 	if (!mask_pack(&rogue.pack, mask)) {
    287 		messagef(0, "nothing appropriate");
    288 		return(CANCEL);
    289 	}
    290 	for (;;) {
    291 
    292 		messagef(0, "%s", prompt);
    293 
    294 		for (;;) {
    295 			ch = rgetchar();
    296 			if (!is_pack_letter(&ch, &mask)) {
    297 				sound_bell();
    298 			} else {
    299 				break;
    300 			}
    301 		}
    302 
    303 		if (ch == LIST) {
    304 			check_message();
    305 			mask = tmask;
    306 			inventory(&rogue.pack, mask);
    307 		} else {
    308 			break;
    309 		}
    310 		mask = tmask;
    311 	}
    312 	check_message();
    313 	return(ch);
    314 }
    315 
    316 void
    317 take_off()
    318 {
    319 	char desc[DCOLS];
    320 	object *obj;
    321 
    322 	if (rogue.armor) {
    323 		if (rogue.armor->is_cursed) {
    324 			messagef(0, "%s", curse_message);
    325 		} else {
    326 			mv_aquatars();
    327 			obj = rogue.armor;
    328 			unwear(rogue.armor);
    329 			get_desc(obj, desc, sizeof(desc));
    330 			messagef(0, "was wearing %s", desc);
    331 			print_stats(STAT_ARMOR);
    332 			(void) reg_move();
    333 		}
    334 	} else {
    335 		messagef(0, "not wearing any");
    336 	}
    337 }
    338 
    339 void
    340 wear()
    341 {
    342 	short ch;
    343 	object *obj;
    344 	char desc[DCOLS];
    345 
    346 	if (rogue.armor) {
    347 		messagef(0, "you're already wearing some");
    348 		return;
    349 	}
    350 	ch = pack_letter("wear what?", ARMOR);
    351 
    352 	if (ch == CANCEL) {
    353 		return;
    354 	}
    355 	if (!(obj = get_letter_object(ch))) {
    356 		messagef(0, "no such item.");
    357 		return;
    358 	}
    359 	if (obj->what_is != ARMOR) {
    360 		messagef(0, "you can't wear that");
    361 		return;
    362 	}
    363 	obj->identified = 1;
    364 	get_desc(obj, desc, sizeof(desc));
    365 	messagef(0, "wearing %s", desc);
    366 	do_wear(obj);
    367 	print_stats(STAT_ARMOR);
    368 	(void) reg_move();
    369 }
    370 
    371 void
    372 unwear(obj)
    373 	object *obj;
    374 {
    375 	if (obj) {
    376 		obj->in_use_flags &= (~BEING_WORN);
    377 	}
    378 	rogue.armor = (object *) 0;
    379 }
    380 
    381 void
    382 do_wear(obj)
    383 	object *obj;
    384 {
    385 	rogue.armor = obj;
    386 	obj->in_use_flags |= BEING_WORN;
    387 	obj->identified = 1;
    388 }
    389 
    390 void
    391 wield()
    392 {
    393 	short ch;
    394 	object *obj;
    395 	char desc[DCOLS];
    396 
    397 	if (rogue.weapon && rogue.weapon->is_cursed) {
    398 		messagef(0, "%s", curse_message);
    399 		return;
    400 	}
    401 	ch = pack_letter("wield what?", WEAPON);
    402 
    403 	if (ch == CANCEL) {
    404 		return;
    405 	}
    406 	if (!(obj = get_letter_object(ch))) {
    407 		messagef(0, "No such item.");
    408 		return;
    409 	}
    410 	if (obj->what_is & (ARMOR | RING)) {
    411 		messagef(0, "you can't wield %s",
    412 			((obj->what_is == ARMOR) ? "armor" : "rings"));
    413 		return;
    414 	}
    415 	if (obj->in_use_flags & BEING_WIELDED) {
    416 		messagef(0, "in use");
    417 	} else {
    418 		unwield(rogue.weapon);
    419 		get_desc(obj, desc, sizeof(desc));
    420 		messagef(0, "wielding %s", desc);
    421 		do_wield(obj);
    422 		(void) reg_move();
    423 	}
    424 }
    425 
    426 void
    427 do_wield(obj)
    428 	object *obj;
    429 {
    430 	rogue.weapon = obj;
    431 	obj->in_use_flags |= BEING_WIELDED;
    432 }
    433 
    434 void
    435 unwield(obj)
    436 	object *obj;
    437 {
    438 	if (obj) {
    439 		obj->in_use_flags &= (~BEING_WIELDED);
    440 	}
    441 	rogue.weapon = (object *) 0;
    442 }
    443 
    444 void
    445 call_it()
    446 {
    447 	short ch;
    448 	object *obj;
    449 	struct id *id_table;
    450 	char buf[MAX_TITLE_LENGTH+2];
    451 
    452 	ch = pack_letter("call what?", (SCROL | POTION | WAND | RING));
    453 
    454 	if (ch == CANCEL) {
    455 		return;
    456 	}
    457 	if (!(obj = get_letter_object(ch))) {
    458 		messagef(0, "no such item.");
    459 		return;
    460 	}
    461 	if (!(obj->what_is & (SCROL | POTION | WAND | RING))) {
    462 		messagef(0, "surely you already know what that's called");
    463 		return;
    464 	}
    465 	id_table = get_id_table(obj);
    466 
    467 	if (get_input_line("call it:", "", buf, sizeof(buf),
    468 			id_table[obj->which_kind].title, 1, 1)) {
    469 		id_table[obj->which_kind].id_status = CALLED;
    470 		(void) strlcpy(id_table[obj->which_kind].title, buf,
    471 				sizeof(id_table[obj->which_kind].title));
    472 	}
    473 }
    474 
    475 short
    476 pack_count(new_obj)
    477 	const object *new_obj;
    478 {
    479 	object *obj;
    480 	short count = 0;
    481 
    482 	obj = rogue.pack.next_object;
    483 
    484 	while (obj) {
    485 		if (obj->what_is != WEAPON) {
    486 			count += obj->quantity;
    487 		} else if (!new_obj) {
    488 			count++;
    489 		} else if ((new_obj->what_is != WEAPON) ||
    490 			((obj->which_kind != ARROW) &&
    491 			(obj->which_kind != DAGGER) &&
    492 			(obj->which_kind != DART) &&
    493 			(obj->which_kind != SHURIKEN)) ||
    494 			(new_obj->which_kind != obj->which_kind) ||
    495 			(obj->quiver != new_obj->quiver)) {
    496 			count++;
    497 		}
    498 		obj = obj->next_object;
    499 	}
    500 	return(count);
    501 }
    502 
    503 boolean
    504 mask_pack(pack, mask)
    505 	const object *pack;
    506 	unsigned short mask;
    507 {
    508 	while (pack->next_object) {
    509 		pack = pack->next_object;
    510 		if (pack->what_is & mask) {
    511 			return(1);
    512 		}
    513 	}
    514 	return(0);
    515 }
    516 
    517 boolean
    518 is_pack_letter(c, mask)
    519 	short *c;
    520 	unsigned short *mask;
    521 {
    522 	if (((*c == '?') || (*c == '!') || (*c == ':') || (*c == '=') ||
    523 		(*c == ')') || (*c == ']') || (*c == '/') || (*c == ','))) {
    524 		switch(*c) {
    525 		case '?':
    526 			*mask = SCROL;
    527 			break;
    528 		case '!':
    529 			*mask = POTION;
    530 			break;
    531 		case ':':
    532 			*mask = FOOD;
    533 			break;
    534 		case ')':
    535 			*mask = WEAPON;
    536 			break;
    537 		case ']':
    538 			*mask = ARMOR;
    539 			break;
    540 		case '/':
    541 			*mask = WAND;
    542 			break;
    543 		case '=':
    544 			*mask = RING;
    545 			break;
    546 		case ',':
    547 			*mask = AMULET;
    548 			break;
    549 		}
    550 		*c = LIST;
    551 		return(1);
    552 	}
    553 	return(((*c >= 'a') && (*c <= 'z')) || (*c == CANCEL) || (*c == LIST));
    554 }
    555 
    556 boolean
    557 has_amulet()
    558 {
    559 	return(mask_pack(&rogue.pack, AMULET));
    560 }
    561 
    562 void
    563 kick_into_pack()
    564 {
    565 	object *obj;
    566 	char desc[DCOLS];
    567 	short stat;
    568 
    569 	if (!(dungeon[rogue.row][rogue.col] & OBJECT)) {
    570 		messagef(0, "nothing here");
    571 	} else {
    572 		if ((obj = pick_up(rogue.row, rogue.col, &stat)) != NULL) {
    573 			get_desc(obj, desc, sizeof(desc));
    574 			if (obj->what_is == GOLD) {
    575 				messagef(0, "%s", desc);
    576 				free_object(obj);
    577 			} else {
    578 				messagef(0, "%s(%c)", desc, obj->ichar);
    579 			}
    580 		}
    581 		if (obj || (!stat)) {
    582 			(void) reg_move();
    583 		}
    584 	}
    585 }
    586