xref: /openbsd/games/hack/hack.steal.c (revision cca36db2)
1 /*	$OpenBSD: hack.steal.c,v 1.5 2009/10/27 23:59:25 deraadt Exp $	*/
2 
3 /*
4  * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
5  * Amsterdam
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are
10  * met:
11  *
12  * - Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * - 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  *
19  * - Neither the name of the Stichting Centrum voor Wiskunde en
20  * Informatica, nor the names of its contributors may be used to endorse or
21  * promote products derived from this software without specific prior
22  * written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
28  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 /*
38  * Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
39  * All rights reserved.
40  *
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
44  * 1. Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in the
48  *    documentation and/or other materials provided with the distribution.
49  * 3. The name of the author may not be used to endorse or promote products
50  *    derived from this software without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
53  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
54  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
55  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
56  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
57  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
58  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
59  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
60  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
61  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62  */
63 
64 #include <stdlib.h>
65 #include "hack.h"
66 
67 extern void (*afternmv)(void);
68 
69 /* actually returns something that fits in an int */
70 long
71 somegold()
72 {
73 	return( (u.ugold < 100) ? u.ugold :
74 		(u.ugold > 10000) ? rnd(10000) : rnd((int) u.ugold) );
75 }
76 
77 void
78 stealgold(struct monst *mtmp)
79 {
80 	struct gold *gold = g_at(u.ux, u.uy);
81 	long tmp;
82 
83 	if(gold && ( !u.ugold || gold->amount > u.ugold || !rn2(5))) {
84 		mtmp->mgold += gold->amount;
85 		freegold(gold);
86 		if(Invisible) newsym(u.ux, u.uy);
87 		pline("%s quickly snatches some gold from between your feet!",
88 			Monnam(mtmp));
89 		if(!u.ugold || !rn2(5)) {
90 			rloc(mtmp);
91 			mtmp->mflee = 1;
92 		}
93 	} else if(u.ugold) {
94 		u.ugold -= (tmp = somegold());
95 		pline("Your purse feels lighter.");
96 		mtmp->mgold += tmp;
97 		rloc(mtmp);
98 		mtmp->mflee = 1;
99 		flags.botl = 1;
100 	}
101 }
102 
103 /* steal armor after he finishes taking it off */
104 static unsigned stealoid;		/* object to be stolen */
105 static unsigned stealmid;		/* monster doing the stealing */
106 
107 void
108 stealarm()
109 {
110 	struct monst *mtmp;
111 	struct obj *otmp;
112 
113 	for(otmp = invent; otmp; otmp = otmp->nobj)
114 	  if(otmp->o_id == stealoid) {
115 	    for(mtmp = fmon; mtmp; mtmp = mtmp->nmon)
116 	      if(mtmp->m_id == stealmid) {
117 		if(dist(mtmp->mx,mtmp->my) < 3) {
118 		  freeinv(otmp);
119 		  pline("%s steals %s!", Monnam(mtmp), doname(otmp));
120 		  mpickobj(mtmp,otmp);
121 		  mtmp->mflee = 1;
122 		  rloc(mtmp);
123 		}
124 		break;
125 	      }
126 	    break;
127 	  }
128 	stealoid = 0;
129 }
130 
131 /* returns 1 when something was stolen */
132 /* (or at least, when N should flee now) */
133 /* avoid stealing the object stealoid */
134 int
135 steal(struct monst *mtmp)
136 {
137 	struct obj *otmp;
138 	int tmp;
139 	int named = 0;
140 
141 	if(!invent){
142 	    if(Blind)
143 	      pline("Somebody tries to rob you, but finds nothing to steal.");
144 	    else
145 	      pline("%s tries to rob you, but she finds nothing to steal!",
146 		Monnam(mtmp));
147 	    return(1);	/* let her flee */
148 	}
149 	tmp = 0;
150 	for(otmp = invent; otmp; otmp = otmp->nobj) if(otmp != uarm2)
151 		tmp += ((otmp->owornmask & (W_ARMOR | W_RING)) ? 5 : 1);
152 	tmp = rn2(tmp);
153 	for(otmp = invent; otmp; otmp = otmp->nobj) if(otmp != uarm2)
154 		if((tmp -= ((otmp->owornmask & (W_ARMOR | W_RING)) ? 5 : 1))
155 			< 0) break;
156 	if(!otmp) {
157 		impossible("Steal fails!");
158 		return(0);
159 	}
160 	if(otmp->o_id == stealoid)
161 		return(0);
162 	if((otmp->owornmask & (W_ARMOR | W_RING))){
163 		switch(otmp->olet) {
164 		case RING_SYM:
165 			ringoff(otmp);
166 			break;
167 		case ARMOR_SYM:
168 			if(multi < 0 || otmp == uarms){
169 			  setworn((struct obj *) 0, otmp->owornmask & W_ARMOR);
170 			  break;
171 			}
172 		{ int curssv = otmp->cursed;
173 			otmp->cursed = 0;
174 			stop_occupation();
175 			pline("%s seduces you and %s off your %s.",
176 				Amonnam(mtmp, Blind ? "gentle" : "beautiful"),
177 				otmp->cursed ? "helps you to take"
178 					    : "you start taking",
179 				(otmp == uarmg) ? "gloves" :
180 				(otmp == uarmh) ? "helmet" : "armor");
181 			named++;
182 			(void) armoroff(otmp);
183 			otmp->cursed = curssv;
184 			if(multi < 0){
185 				stealoid = otmp->o_id;
186 				stealmid = mtmp->m_id;
187 				afternmv = stealarm;
188 				return(0);
189 			}
190 			break;
191 		}
192 		default:
193 			impossible("Tried to steal a strange worn thing.");
194 		}
195 	}
196 	else if(otmp == uwep)
197 		setuwep((struct obj *) 0);
198 	if(otmp->olet == CHAIN_SYM) {
199 		impossible("How come you are carrying that chain?");
200 	}
201 	if(Punished && otmp == uball){
202 		Punished = 0;
203 		freeobj(uchain);
204 		free((char *) uchain);
205 		uchain = (struct obj *) 0;
206 		uball->spe = 0;
207 		uball = (struct obj *) 0;	/* superfluous */
208 	}
209 	freeinv(otmp);
210 	pline("%s stole %s.", named ? "She" : Monnam(mtmp), doname(otmp));
211 	mpickobj(mtmp,otmp);
212 	return((multi < 0) ? 0 : 1);
213 }
214 
215 void
216 mpickobj(struct monst *mtmp, struct obj *otmp)
217 {
218 	otmp->nobj = mtmp->minvent;
219 	mtmp->minvent = otmp;
220 }
221 
222 int
223 stealamulet(struct monst *mtmp)
224 {
225 	struct obj *otmp;
226 
227 	for(otmp = invent; otmp; otmp = otmp->nobj) {
228 	    if(otmp->olet == AMULET_SYM) {
229 		/* might be an imitation one */
230 		if(otmp == uwep) setuwep((struct obj *) 0);
231 		freeinv(otmp);
232 		mpickobj(mtmp,otmp);
233 		pline("%s stole %s!", Monnam(mtmp), doname(otmp));
234 		return(1);
235 	    }
236 	}
237 	return(0);
238 }
239 
240 /* release the objects the killed animal has stolen */
241 void
242 relobj(struct monst *mtmp, int show)
243 {
244 	struct obj *otmp, *otmp2;
245 
246 	for(otmp = mtmp->minvent; otmp; otmp = otmp2){
247 		otmp->ox = mtmp->mx;
248 		otmp->oy = mtmp->my;
249 		otmp2 = otmp->nobj;
250 		otmp->nobj = fobj;
251 		fobj = otmp;
252 		stackobj(fobj);
253 		if(show & cansee(mtmp->mx,mtmp->my))
254 			atl(otmp->ox,otmp->oy,otmp->olet);
255 	}
256 	mtmp->minvent = (struct obj *) 0;
257 	if(mtmp->mgold || mtmp->data->mlet == 'L') {
258 		long tmp;
259 
260 		tmp = (mtmp->mgold > 10000) ? 10000 : mtmp->mgold;
261 		mkgold((long)(tmp + d(dlevel,30)), mtmp->mx, mtmp->my);
262 		if(show & cansee(mtmp->mx,mtmp->my))
263 			atl(mtmp->mx,mtmp->my,'$');
264 	}
265 }
266