Home | History | Annotate | Line # | Download | only in rogue
pack.c revision 1.9
      1 /*	$NetBSD: pack.c,v 1.9 2008/01/14 00:23:52 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.9 2008/01/14 00:23:52 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 
    279 short
    280 pack_letter(prompt, mask)
    281 	const char *prompt;
    282 	unsigned short mask;
    283 {
    284 	short ch;
    285 	unsigned short tmask = mask;
    286 
    287 	if (!mask_pack(&rogue.pack, mask)) {
    288 		messagef(0, "nothing appropriate");
    289 		return(CANCEL);
    290 	}
    291 	for (;;) {
    292 
    293 		messagef(0, "%s", prompt);
    294 
    295 		for (;;) {
    296 			ch = rgetchar();
    297 			if (!is_pack_letter(&ch, &mask)) {
    298 				sound_bell();
    299 			} else {
    300 				break;
    301 			}
    302 		}
    303 
    304 		if (ch == LIST) {
    305 			check_message();
    306 			mask = tmask;
    307 			inventory(&rogue.pack, mask);
    308 		} else {
    309 			break;
    310 		}
    311 		mask = tmask;
    312 	}
    313 	check_message();
    314 	return(ch);
    315 }
    316 
    317 void
    318 take_off()
    319 {
    320 	char desc[DCOLS];
    321 	object *obj;
    322 
    323 	if (rogue.armor) {
    324 		if (rogue.armor->is_cursed) {
    325 			messagef(0, "%s", curse_message);
    326 		} else {
    327 			mv_aquatars();
    328 			obj = rogue.armor;
    329 			unwear(rogue.armor);
    330 			get_desc(obj, desc, sizeof(desc));
    331 			messagef(0, "was wearing %s", desc);
    332 			print_stats(STAT_ARMOR);
    333 			(void)reg_move();
    334 		}
    335 	} else {
    336 		messagef(0, "not wearing any");
    337 	}
    338 }
    339 
    340 void
    341 wear()
    342 {
    343 	short ch;
    344 	object *obj;
    345 	char desc[DCOLS];
    346 
    347 	if (rogue.armor) {
    348 		messagef(0, "you're already wearing some");
    349 		return;
    350 	}
    351 	ch = pack_letter("wear what?", ARMOR);
    352 
    353 	if (ch == CANCEL) {
    354 		return;
    355 	}
    356 	if (!(obj = get_letter_object(ch))) {
    357 		messagef(0, "no such item.");
    358 		return;
    359 	}
    360 	if (obj->what_is != ARMOR) {
    361 		messagef(0, "you can't wear that");
    362 		return;
    363 	}
    364 	obj->identified = 1;
    365 	get_desc(obj, desc, sizeof(desc));
    366 	messagef(0, "wearing %s", desc);
    367 	do_wear(obj);
    368 	print_stats(STAT_ARMOR);
    369 	(void)reg_move();
    370 }
    371 
    372 void
    373 unwear(obj)
    374 	object *obj;
    375 {
    376 	if (obj) {
    377 		obj->in_use_flags &= (~BEING_WORN);
    378 	}
    379 	rogue.armor = (object *)0;
    380 }
    381 
    382 void
    383 do_wear(obj)
    384 	object *obj;
    385 {
    386 	rogue.armor = obj;
    387 	obj->in_use_flags |= BEING_WORN;
    388 	obj->identified = 1;
    389 }
    390 
    391 void
    392 wield()
    393 {
    394 	short ch;
    395 	object *obj;
    396 	char desc[DCOLS];
    397 
    398 	if (rogue.weapon && rogue.weapon->is_cursed) {
    399 		messagef(0, "%s", curse_message);
    400 		return;
    401 	}
    402 	ch = pack_letter("wield what?", WEAPON);
    403 
    404 	if (ch == CANCEL) {
    405 		return;
    406 	}
    407 	if (!(obj = get_letter_object(ch))) {
    408 		messagef(0, "No such item.");
    409 		return;
    410 	}
    411 	if (obj->what_is & (ARMOR | RING)) {
    412 		messagef(0, "you can't wield %s",
    413 			((obj->what_is == ARMOR) ? "armor" : "rings"));
    414 		return;
    415 	}
    416 	if (obj->in_use_flags & BEING_WIELDED) {
    417 		messagef(0, "in use");
    418 	} else {
    419 		unwield(rogue.weapon);
    420 		get_desc(obj, desc, sizeof(desc));
    421 		messagef(0, "wielding %s", desc);
    422 		do_wield(obj);
    423 		(void)reg_move();
    424 	}
    425 }
    426 
    427 void
    428 do_wield(obj)
    429 	object *obj;
    430 {
    431 	rogue.weapon = obj;
    432 	obj->in_use_flags |= BEING_WIELDED;
    433 }
    434 
    435 void
    436 unwield(obj)
    437 	object *obj;
    438 {
    439 	if (obj) {
    440 		obj->in_use_flags &= (~BEING_WIELDED);
    441 	}
    442 	rogue.weapon = (object *)0;
    443 }
    444 
    445 void
    446 call_it()
    447 {
    448 	short ch;
    449 	object *obj;
    450 	struct id *id_table;
    451 	char buf[MAX_TITLE_LENGTH+2];
    452 
    453 	ch = pack_letter("call what?", (SCROL | POTION | WAND | RING));
    454 
    455 	if (ch == CANCEL) {
    456 		return;
    457 	}
    458 	if (!(obj = get_letter_object(ch))) {
    459 		messagef(0, "no such item.");
    460 		return;
    461 	}
    462 	if (!(obj->what_is & (SCROL | POTION | WAND | RING))) {
    463 		messagef(0, "surely you already know what that's called");
    464 		return;
    465 	}
    466 	id_table = get_id_table(obj);
    467 
    468 	if (get_input_line("call it:", "", buf, sizeof(buf),
    469 			id_table[obj->which_kind].title, 1, 1)) {
    470 		id_table[obj->which_kind].id_status = CALLED;
    471 		(void)strlcpy(id_table[obj->which_kind].title, buf,
    472 				sizeof(id_table[obj->which_kind].title));
    473 	}
    474 }
    475 
    476 short
    477 pack_count(new_obj)
    478 	const object *new_obj;
    479 {
    480 	object *obj;
    481 	short count = 0;
    482 
    483 	obj = rogue.pack.next_object;
    484 
    485 	while (obj) {
    486 		if (obj->what_is != WEAPON) {
    487 			count += obj->quantity;
    488 		} else if (!new_obj) {
    489 			count++;
    490 		} else if ((new_obj->what_is != WEAPON) ||
    491 			((obj->which_kind != ARROW) &&
    492 			(obj->which_kind != DAGGER) &&
    493 			(obj->which_kind != DART) &&
    494 			(obj->which_kind != SHURIKEN)) ||
    495 			(new_obj->which_kind != obj->which_kind) ||
    496 			(obj->quiver != new_obj->quiver)) {
    497 			count++;
    498 		}
    499 		obj = obj->next_object;
    500 	}
    501 	return(count);
    502 }
    503 
    504 boolean
    505 mask_pack(pack, mask)
    506 	const object *pack;
    507 	unsigned short mask;
    508 {
    509 	while (pack->next_object) {
    510 		pack = pack->next_object;
    511 		if (pack->what_is & mask) {
    512 			return(1);
    513 		}
    514 	}
    515 	return(0);
    516 }
    517 
    518 boolean
    519 is_pack_letter(c, mask)
    520 	short *c;
    521 	unsigned short *mask;
    522 {
    523 	if (((*c == '?') || (*c == '!') || (*c == ':') || (*c == '=') ||
    524 		(*c == ')') || (*c == ']') || (*c == '/') || (*c == ','))) {
    525 		switch(*c) {
    526 		case '?':
    527 			*mask = SCROL;
    528 			break;
    529 		case '!':
    530 			*mask = POTION;
    531 			break;
    532 		case ':':
    533 			*mask = FOOD;
    534 			break;
    535 		case ')':
    536 			*mask = WEAPON;
    537 			break;
    538 		case ']':
    539 			*mask = ARMOR;
    540 			break;
    541 		case '/':
    542 			*mask = WAND;
    543 			break;
    544 		case '=':
    545 			*mask = RING;
    546 			break;
    547 		case ',':
    548 			*mask = AMULET;
    549 			break;
    550 		}
    551 		*c = LIST;
    552 		return(1);
    553 	}
    554 	return(((*c >= 'a') && (*c <= 'z')) || (*c == CANCEL) || (*c == LIST));
    555 }
    556 
    557 boolean
    558 has_amulet()
    559 {
    560 	return(mask_pack(&rogue.pack, AMULET));
    561 }
    562 
    563 void
    564 kick_into_pack()
    565 {
    566 	object *obj;
    567 	char desc[DCOLS];
    568 	short stat;
    569 
    570 	if (!(dungeon[rogue.row][rogue.col] & OBJECT)) {
    571 		messagef(0, "nothing here");
    572 	} else {
    573 		if ((obj = pick_up(rogue.row, rogue.col, &stat)) != NULL) {
    574 			get_desc(obj, desc, sizeof(desc));
    575 			if (obj->what_is == GOLD) {
    576 				messagef(0, "%s", desc);
    577 				free_object(obj);
    578 			} else {
    579 				messagef(0, "%s(%c)", desc, obj->ichar);
    580 			}
    581 		}
    582 		if (obj || (!stat)) {
    583 			(void)reg_move();
    584 		}
    585 	}
    586 }
    587