xref: /openbsd-src/games/hack/hack.mkobj.c (revision 043fbe51c197dbbcd422e917b65f765d8b5f8874)
1*043fbe51Sderaadt /*	$OpenBSD: hack.mkobj.c,v 1.7 2009/10/27 23:59:25 deraadt Exp $	*/
2d0b779f3Sniklas 
3df930be7Sderaadt /*
4d25013f2Scamield  * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
5d25013f2Scamield  * Amsterdam
6d25013f2Scamield  * All rights reserved.
7d25013f2Scamield  *
8d25013f2Scamield  * Redistribution and use in source and binary forms, with or without
9d25013f2Scamield  * modification, are permitted provided that the following conditions are
10d25013f2Scamield  * met:
11d25013f2Scamield  *
12d25013f2Scamield  * - Redistributions of source code must retain the above copyright notice,
13d25013f2Scamield  * this list of conditions and the following disclaimer.
14d25013f2Scamield  *
15d25013f2Scamield  * - Redistributions in binary form must reproduce the above copyright
16d25013f2Scamield  * notice, this list of conditions and the following disclaimer in the
17d25013f2Scamield  * documentation and/or other materials provided with the distribution.
18d25013f2Scamield  *
19d25013f2Scamield  * - Neither the name of the Stichting Centrum voor Wiskunde en
20d25013f2Scamield  * Informatica, nor the names of its contributors may be used to endorse or
21d25013f2Scamield  * promote products derived from this software without specific prior
22d25013f2Scamield  * written permission.
23d25013f2Scamield  *
24d25013f2Scamield  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25d25013f2Scamield  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26d25013f2Scamield  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27d25013f2Scamield  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
28d25013f2Scamield  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29d25013f2Scamield  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30d25013f2Scamield  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31d25013f2Scamield  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32d25013f2Scamield  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33d25013f2Scamield  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34d25013f2Scamield  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35d25013f2Scamield  */
36d25013f2Scamield 
37d25013f2Scamield /*
38d25013f2Scamield  * Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
39d25013f2Scamield  * All rights reserved.
40d25013f2Scamield  *
41d25013f2Scamield  * Redistribution and use in source and binary forms, with or without
42d25013f2Scamield  * modification, are permitted provided that the following conditions
43d25013f2Scamield  * are met:
44d25013f2Scamield  * 1. Redistributions of source code must retain the above copyright
45d25013f2Scamield  *    notice, this list of conditions and the following disclaimer.
46d25013f2Scamield  * 2. Redistributions in binary form must reproduce the above copyright
47d25013f2Scamield  *    notice, this list of conditions and the following disclaimer in the
48d25013f2Scamield  *    documentation and/or other materials provided with the distribution.
49d25013f2Scamield  * 3. The name of the author may not be used to endorse or promote products
50d25013f2Scamield  *    derived from this software without specific prior written permission.
51d25013f2Scamield  *
52d25013f2Scamield  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
53d25013f2Scamield  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
54d25013f2Scamield  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
55d25013f2Scamield  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
56d25013f2Scamield  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
57d25013f2Scamield  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
58d25013f2Scamield  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
59d25013f2Scamield  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
60d25013f2Scamield  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
61d25013f2Scamield  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62df930be7Sderaadt  */
63df930be7Sderaadt 
64df930be7Sderaadt #include "hack.h"
65df930be7Sderaadt 
66df930be7Sderaadt char mkobjstr[] = "))[[!!!!????%%%%/=**))[[!!!!????%%%%/=**(%";
67df930be7Sderaadt 
68df930be7Sderaadt struct obj *
mkobj_at(int let,int x,int y)694a5fbbc4Spjanzen mkobj_at(int let, int x, int y)
70df930be7Sderaadt {
714a5fbbc4Spjanzen 	struct obj *otmp = mkobj(let);
72df930be7Sderaadt 	otmp->ox = x;
73df930be7Sderaadt 	otmp->oy = y;
74df930be7Sderaadt 	otmp->nobj = fobj;
75df930be7Sderaadt 	fobj = otmp;
76df930be7Sderaadt 	return(otmp);
77df930be7Sderaadt }
78df930be7Sderaadt 
794a5fbbc4Spjanzen void
mksobj_at(int otyp,int x,int y)804a5fbbc4Spjanzen mksobj_at(int otyp, int x, int y)
81df930be7Sderaadt {
824a5fbbc4Spjanzen 	struct obj *otmp = mksobj(otyp);
83df930be7Sderaadt 	otmp->ox = x;
84df930be7Sderaadt 	otmp->oy = y;
85df930be7Sderaadt 	otmp->nobj = fobj;
86df930be7Sderaadt 	fobj = otmp;
87df930be7Sderaadt }
88df930be7Sderaadt 
89df930be7Sderaadt struct obj *
mkobj(int let)904a5fbbc4Spjanzen mkobj(int let)
914a5fbbc4Spjanzen {
92df930be7Sderaadt 	if(!let)
93df930be7Sderaadt 		let = mkobjstr[rn2(sizeof(mkobjstr) - 1)];
94df930be7Sderaadt 	return(
95df930be7Sderaadt 	    mksobj(
96df930be7Sderaadt 		letter(let) ?
97df930be7Sderaadt 		    CORPSE + ((let > 'Z') ? (let-'a'+'Z'-'@'+1) : (let-'@'))
98df930be7Sderaadt 		:   probtype(let)
99df930be7Sderaadt 	    )
100df930be7Sderaadt 	);
101df930be7Sderaadt }
102df930be7Sderaadt 
103df930be7Sderaadt 
104df930be7Sderaadt struct obj zeroobj;
105df930be7Sderaadt 
106df930be7Sderaadt struct obj *
mksobj(int otyp)1074a5fbbc4Spjanzen mksobj(int otyp)
108df930be7Sderaadt {
1094a5fbbc4Spjanzen 	struct obj *otmp;
110df930be7Sderaadt 	char let = objects[otyp].oc_olet;
111df930be7Sderaadt 
112df930be7Sderaadt 	otmp = newobj(0);
113df930be7Sderaadt 	*otmp = zeroobj;
114df930be7Sderaadt 	otmp->age = moves;
115df930be7Sderaadt 	otmp->o_id = flags.ident++;
116df930be7Sderaadt 	otmp->quan = 1;
117df930be7Sderaadt 	otmp->olet = let;
118df930be7Sderaadt 	otmp->otyp = otyp;
119180acc8fSmillert 	otmp->dknown = strchr("/=!?*", let) ? 0 : 1;
120df930be7Sderaadt 	switch(let) {
121df930be7Sderaadt 	case WEAPON_SYM:
122df930be7Sderaadt 		otmp->quan = (otmp->otyp <= ROCK) ? rn1(6,6) : 1;
123df930be7Sderaadt 		if(!rn2(11)) otmp->spe = rnd(3);
124df930be7Sderaadt 		else if(!rn2(10)) {
125df930be7Sderaadt 			otmp->cursed = 1;
126df930be7Sderaadt 			otmp->spe = -rnd(3);
127df930be7Sderaadt 		}
128df930be7Sderaadt 		break;
129df930be7Sderaadt 	case FOOD_SYM:
130df930be7Sderaadt 		if(otmp->otyp >= CORPSE) break;
131df930be7Sderaadt #ifdef NOT_YET_IMPLEMENTED
132df930be7Sderaadt 		/* if tins are to be identified, need to adapt doname() etc */
133df930be7Sderaadt 		if(otmp->otyp == TIN)
134df930be7Sderaadt 			otmp->spe = rnd(...);
13554da88e4Spjanzen #endif /* NOT_YET_IMPLEMENTED */
136df930be7Sderaadt 		/* fall into next case */
137df930be7Sderaadt 	case GEM_SYM:
138df930be7Sderaadt 		otmp->quan = rn2(6) ? 1 : 2;
139df930be7Sderaadt 	case TOOL_SYM:
140df930be7Sderaadt 	case CHAIN_SYM:
141df930be7Sderaadt 	case BALL_SYM:
142df930be7Sderaadt 	case ROCK_SYM:
143df930be7Sderaadt 	case POTION_SYM:
144df930be7Sderaadt 	case SCROLL_SYM:
145df930be7Sderaadt 	case AMULET_SYM:
146df930be7Sderaadt 		break;
147df930be7Sderaadt 	case ARMOR_SYM:
148df930be7Sderaadt 		if(!rn2(8)) otmp->cursed = 1;
149df930be7Sderaadt 		if(!rn2(10)) otmp->spe = rnd(3);
150df930be7Sderaadt 		else if(!rn2(9)) {
151df930be7Sderaadt 			otmp->spe = -rnd(3);
152df930be7Sderaadt 			otmp->cursed = 1;
153df930be7Sderaadt 		}
154df930be7Sderaadt 		break;
155df930be7Sderaadt 	case WAND_SYM:
156df930be7Sderaadt 		if(otmp->otyp == WAN_WISHING) otmp->spe = 3; else
157df930be7Sderaadt 		otmp->spe = rn1(5,
158df930be7Sderaadt 			(objects[otmp->otyp].bits & NODIR) ? 11 : 4);
159df930be7Sderaadt 		break;
160df930be7Sderaadt 	case RING_SYM:
161df930be7Sderaadt 		if(objects[otmp->otyp].bits & SPEC) {
162df930be7Sderaadt 			if(!rn2(3)) {
163df930be7Sderaadt 				otmp->cursed = 1;
164df930be7Sderaadt 				otmp->spe = -rnd(2);
165df930be7Sderaadt 			} else otmp->spe = rnd(2);
166df930be7Sderaadt 		} else if(otmp->otyp == RIN_TELEPORTATION ||
167df930be7Sderaadt 			  otmp->otyp == RIN_AGGRAVATE_MONSTER ||
168df930be7Sderaadt 			  otmp->otyp == RIN_HUNGER || !rn2(9))
169df930be7Sderaadt 			otmp->cursed = 1;
170df930be7Sderaadt 		break;
171df930be7Sderaadt 	default:
172df930be7Sderaadt 		panic("impossible mkobj");
173df930be7Sderaadt 	}
174df930be7Sderaadt 	otmp->owt = weight(otmp);
175df930be7Sderaadt 	return(otmp);
176df930be7Sderaadt }
177df930be7Sderaadt 
1784a5fbbc4Spjanzen int
letter(int c)1794a5fbbc4Spjanzen letter(int c)
1804a5fbbc4Spjanzen {
181df930be7Sderaadt 	return(('@' <= c && c <= 'Z') || ('a' <= c && c <= 'z'));
182df930be7Sderaadt }
183df930be7Sderaadt 
1844a5fbbc4Spjanzen int
weight(struct obj * obj)1854a5fbbc4Spjanzen weight(struct obj *obj)
186df930be7Sderaadt {
1874a5fbbc4Spjanzen 	int wt = objects[obj->otyp].oc_weight;
1884a5fbbc4Spjanzen 
189df930be7Sderaadt 	return(wt ? wt*obj->quan : (obj->quan + 1)/2);
190df930be7Sderaadt }
191df930be7Sderaadt 
1924a5fbbc4Spjanzen void
mkgold(long num,int x,int y)1934a5fbbc4Spjanzen mkgold(long num, int x, int y)
194df930be7Sderaadt {
1954a5fbbc4Spjanzen 	struct gold *gold;
1964a5fbbc4Spjanzen 	long amount = (num ? num : 1 + (rnd(dlevel+2) * rnd(30)));
197df930be7Sderaadt 
1984a5fbbc4Spjanzen 	if ((gold = g_at(x,y)))
199df930be7Sderaadt 		gold->amount += amount;
200df930be7Sderaadt 	else {
201df930be7Sderaadt 		gold = newgold();
202df930be7Sderaadt 		gold->ngold = fgold;
203df930be7Sderaadt 		gold->gx = x;
204df930be7Sderaadt 		gold->gy = y;
205df930be7Sderaadt 		gold->amount = amount;
206df930be7Sderaadt 		fgold = gold;
207df930be7Sderaadt 		/* do sth with display? */
208df930be7Sderaadt 	}
209df930be7Sderaadt }
210