xref: /netbsd-src/games/hack/hack.mkobj.c (revision d5b021c7edb8313eee3804ec3819b28abef1d1a0)
1*d5b021c7Sdholland /*	$NetBSD: hack.mkobj.c,v 1.9 2011/08/07 06:03:45 dholland Exp $	*/
23ea4a95cSchristos 
302ded532Smycroft /*
41c7f94e5Sjsm  * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
51c7f94e5Sjsm  * Amsterdam
61c7f94e5Sjsm  * All rights reserved.
71c7f94e5Sjsm  *
81c7f94e5Sjsm  * Redistribution and use in source and binary forms, with or without
91c7f94e5Sjsm  * modification, are permitted provided that the following conditions are
101c7f94e5Sjsm  * met:
111c7f94e5Sjsm  *
121c7f94e5Sjsm  * - Redistributions of source code must retain the above copyright notice,
131c7f94e5Sjsm  * this list of conditions and the following disclaimer.
141c7f94e5Sjsm  *
151c7f94e5Sjsm  * - Redistributions in binary form must reproduce the above copyright
161c7f94e5Sjsm  * notice, this list of conditions and the following disclaimer in the
171c7f94e5Sjsm  * documentation and/or other materials provided with the distribution.
181c7f94e5Sjsm  *
191c7f94e5Sjsm  * - Neither the name of the Stichting Centrum voor Wiskunde en
201c7f94e5Sjsm  * Informatica, nor the names of its contributors may be used to endorse or
211c7f94e5Sjsm  * promote products derived from this software without specific prior
221c7f94e5Sjsm  * written permission.
231c7f94e5Sjsm  *
241c7f94e5Sjsm  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
251c7f94e5Sjsm  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
261c7f94e5Sjsm  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
271c7f94e5Sjsm  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
281c7f94e5Sjsm  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
291c7f94e5Sjsm  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
301c7f94e5Sjsm  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
311c7f94e5Sjsm  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
321c7f94e5Sjsm  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
331c7f94e5Sjsm  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
341c7f94e5Sjsm  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
351c7f94e5Sjsm  */
361c7f94e5Sjsm 
371c7f94e5Sjsm /*
381c7f94e5Sjsm  * Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
391c7f94e5Sjsm  * All rights reserved.
401c7f94e5Sjsm  *
411c7f94e5Sjsm  * Redistribution and use in source and binary forms, with or without
421c7f94e5Sjsm  * modification, are permitted provided that the following conditions
431c7f94e5Sjsm  * are met:
441c7f94e5Sjsm  * 1. Redistributions of source code must retain the above copyright
451c7f94e5Sjsm  *    notice, this list of conditions and the following disclaimer.
461c7f94e5Sjsm  * 2. Redistributions in binary form must reproduce the above copyright
471c7f94e5Sjsm  *    notice, this list of conditions and the following disclaimer in the
481c7f94e5Sjsm  *    documentation and/or other materials provided with the distribution.
491c7f94e5Sjsm  * 3. The name of the author may not be used to endorse or promote products
501c7f94e5Sjsm  *    derived from this software without specific prior written permission.
511c7f94e5Sjsm  *
521c7f94e5Sjsm  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
531c7f94e5Sjsm  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
541c7f94e5Sjsm  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
551c7f94e5Sjsm  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
561c7f94e5Sjsm  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
571c7f94e5Sjsm  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
581c7f94e5Sjsm  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
591c7f94e5Sjsm  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
601c7f94e5Sjsm  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
611c7f94e5Sjsm  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
6202ded532Smycroft  */
6302ded532Smycroft 
643ea4a95cSchristos #include <sys/cdefs.h>
6502ded532Smycroft #ifndef lint
66*d5b021c7Sdholland __RCSID("$NetBSD: hack.mkobj.c,v 1.9 2011/08/07 06:03:45 dholland Exp $");
6702ded532Smycroft #endif				/* not lint */
6861f28255Scgd 
6961f28255Scgd #include "hack.h"
703ea4a95cSchristos #include "extern.h"
7161f28255Scgd 
729b92b189Sdholland static const char mkobjstr[] = "))[[!!!!????%%%%/=**))[[!!!!????%%%%/=**(%";
7361f28255Scgd 
7461f28255Scgd struct obj     *
mkobj_at(int let,int x,int y)751fa8a9a6Sdholland mkobj_at(int let, int x, int y)
7661f28255Scgd {
773ea4a95cSchristos 	struct obj     *otmp = mkobj(let);
7861f28255Scgd 	otmp->ox = x;
7961f28255Scgd 	otmp->oy = y;
8061f28255Scgd 	otmp->nobj = fobj;
8161f28255Scgd 	fobj = otmp;
8261f28255Scgd 	return (otmp);
8361f28255Scgd }
8461f28255Scgd 
853ea4a95cSchristos void
mksobj_at(int otyp,int x,int y)861fa8a9a6Sdholland mksobj_at(int otyp, int x, int y)
8761f28255Scgd {
883ea4a95cSchristos 	struct obj     *otmp = mksobj(otyp);
8961f28255Scgd 	otmp->ox = x;
9061f28255Scgd 	otmp->oy = y;
9161f28255Scgd 	otmp->nobj = fobj;
9261f28255Scgd 	fobj = otmp;
9361f28255Scgd }
9461f28255Scgd 
9561f28255Scgd struct obj     *
mkobj(int let)961fa8a9a6Sdholland mkobj(int let)
973ea4a95cSchristos {
9861f28255Scgd 	if (!let)
9961f28255Scgd 		let = mkobjstr[rn2(sizeof(mkobjstr) - 1)];
10061f28255Scgd 	return (
10161f28255Scgd 		mksobj(
10261f28255Scgd 		       letter(let) ?
10361f28255Scgd 	  CORPSE + ((let > 'Z') ? (let - 'a' + 'Z' - '@' + 1) : (let - '@'))
10461f28255Scgd 		       : probtype(let)
10561f28255Scgd 		       )
10661f28255Scgd 		);
10761f28255Scgd }
10861f28255Scgd 
10961f28255Scgd 
11061f28255Scgd struct obj      zeroobj;
11161f28255Scgd 
11261f28255Scgd struct obj     *
mksobj(int otyp)1131fa8a9a6Sdholland mksobj(int otyp)
11461f28255Scgd {
1153ea4a95cSchristos 	struct obj     *otmp;
11661f28255Scgd 	char            let = objects[otyp].oc_olet;
11761f28255Scgd 
11861f28255Scgd 	otmp = newobj(0);
11961f28255Scgd 	*otmp = zeroobj;
12061f28255Scgd 	otmp->age = moves;
12161f28255Scgd 	otmp->o_id = flags.ident++;
12261f28255Scgd 	otmp->quan = 1;
12361f28255Scgd 	otmp->olet = let;
12461f28255Scgd 	otmp->otyp = otyp;
1253ea4a95cSchristos 	otmp->dknown = strchr("/=!?*", let) ? 0 : 1;
12661f28255Scgd 	switch (let) {
12761f28255Scgd 	case WEAPON_SYM:
12861f28255Scgd 		otmp->quan = (otmp->otyp <= ROCK) ? rn1(6, 6) : 1;
1293ea4a95cSchristos 		if (!rn2(11))
1303ea4a95cSchristos 			otmp->spe = rnd(3);
13161f28255Scgd 		else if (!rn2(10)) {
13261f28255Scgd 			otmp->cursed = 1;
13361f28255Scgd 			otmp->spe = -rnd(3);
13461f28255Scgd 		}
13561f28255Scgd 		break;
13661f28255Scgd 	case FOOD_SYM:
1373ea4a95cSchristos 		if (otmp->otyp >= CORPSE)
1383ea4a95cSchristos 			break;
13961f28255Scgd #ifdef NOT_YET_IMPLEMENTED
14061f28255Scgd 		/* if tins are to be identified, need to adapt doname() etc */
14161f28255Scgd 		if (otmp->otyp == TIN)
14261f28255Scgd 			otmp->spe = rnd(...);
1433ea4a95cSchristos #endif	/* NOT_YET_IMPLEMENTED */
144*d5b021c7Sdholland 		/* FALLTHROUGH */
14561f28255Scgd 	case GEM_SYM:
14661f28255Scgd 		otmp->quan = rn2(6) ? 1 : 2;
147*d5b021c7Sdholland 		break;
14861f28255Scgd 	case TOOL_SYM:
14961f28255Scgd 	case CHAIN_SYM:
15061f28255Scgd 	case BALL_SYM:
15161f28255Scgd 	case ROCK_SYM:
15261f28255Scgd 	case POTION_SYM:
15361f28255Scgd 	case SCROLL_SYM:
15461f28255Scgd 	case AMULET_SYM:
15561f28255Scgd 		break;
15661f28255Scgd 	case ARMOR_SYM:
1573ea4a95cSchristos 		if (!rn2(8))
1583ea4a95cSchristos 			otmp->cursed = 1;
1593ea4a95cSchristos 		if (!rn2(10))
1603ea4a95cSchristos 			otmp->spe = rnd(3);
16161f28255Scgd 		else if (!rn2(9)) {
16261f28255Scgd 			otmp->spe = -rnd(3);
16361f28255Scgd 			otmp->cursed = 1;
16461f28255Scgd 		}
16561f28255Scgd 		break;
16661f28255Scgd 	case WAND_SYM:
1673ea4a95cSchristos 		if (otmp->otyp == WAN_WISHING)
1683ea4a95cSchristos 			otmp->spe = 3;
1693ea4a95cSchristos 		else
17061f28255Scgd 			otmp->spe = rn1(5,
17161f28255Scgd 			       (objects[otmp->otyp].bits & NODIR) ? 11 : 4);
17261f28255Scgd 		break;
17361f28255Scgd 	case RING_SYM:
17461f28255Scgd 		if (objects[otmp->otyp].bits & SPEC) {
17561f28255Scgd 			if (!rn2(3)) {
17661f28255Scgd 				otmp->cursed = 1;
17761f28255Scgd 				otmp->spe = -rnd(2);
1783ea4a95cSchristos 			} else
1793ea4a95cSchristos 				otmp->spe = rnd(2);
18061f28255Scgd 		} else if (otmp->otyp == RIN_TELEPORTATION ||
18161f28255Scgd 			   otmp->otyp == RIN_AGGRAVATE_MONSTER ||
18261f28255Scgd 			   otmp->otyp == RIN_HUNGER || !rn2(9))
18361f28255Scgd 			otmp->cursed = 1;
18461f28255Scgd 		break;
18561f28255Scgd 	default:
18661f28255Scgd 		panic("impossible mkobj");
18761f28255Scgd 	}
18861f28255Scgd 	otmp->owt = weight(otmp);
18961f28255Scgd 	return (otmp);
19061f28255Scgd }
19161f28255Scgd 
1923ea4a95cSchristos int
letter(int c)1931fa8a9a6Sdholland letter(int c)
1943ea4a95cSchristos {
19561f28255Scgd 	return (('@' <= c && c <= 'Z') || ('a' <= c && c <= 'z'));
19661f28255Scgd }
19761f28255Scgd 
1983ea4a95cSchristos int
weight(struct obj * obj)1991fa8a9a6Sdholland weight(struct obj *obj)
20061f28255Scgd {
2013ea4a95cSchristos 	int             wt = objects[obj->otyp].oc_weight;
20261f28255Scgd 	return (wt ? wt * obj->quan : (obj->quan + 1) / 2);
20361f28255Scgd }
20461f28255Scgd 
2053ea4a95cSchristos void
mkgold(long num,int x,int y)206ab8b6343Sjsm mkgold(long num, int x, int y)
20761f28255Scgd {
2083ea4a95cSchristos 	struct gold    *gold;
2093ea4a95cSchristos 	long            amount = (num ? num : 1 + (rnd(dlevel + 2) * rnd(30)));
21061f28255Scgd 
2113ea4a95cSchristos 	if ((gold = g_at(x, y)) != NULL)
21261f28255Scgd 		gold->amount += amount;
21361f28255Scgd 	else {
21461f28255Scgd 		gold = newgold();
21561f28255Scgd 		gold->ngold = fgold;
21661f28255Scgd 		gold->gx = x;
21761f28255Scgd 		gold->gy = y;
21861f28255Scgd 		gold->amount = amount;
21961f28255Scgd 		fgold = gold;
22061f28255Scgd 		/* do sth with display? */
22161f28255Scgd 	}
22261f28255Scgd }
223