1/*	$OpenBSD: hack.steal.c,v 1.7 2016/01/09 18:33:15 mestre 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
66#include "hack.h"
67
68extern void (*afternmv)(void);
69
70/* actually returns something that fits in an int */
71long
72somegold(void)
73{
74	return( (u.ugold < 100) ? u.ugold :
75		(u.ugold > 10000) ? rnd(10000) : rnd((int) u.ugold) );
76}
77
78void
79stealgold(struct monst *mtmp)
80{
81	struct gold *gold = g_at(u.ux, u.uy);
82	long tmp;
83
84	if(gold && ( !u.ugold || gold->amount > u.ugold || !rn2(5))) {
85		mtmp->mgold += gold->amount;
86		freegold(gold);
87		if(Invisible) newsym(u.ux, u.uy);
88		pline("%s quickly snatches some gold from between your feet!",
89			Monnam(mtmp));
90		if(!u.ugold || !rn2(5)) {
91			rloc(mtmp);
92			mtmp->mflee = 1;
93		}
94	} else if(u.ugold) {
95		u.ugold -= (tmp = somegold());
96		pline("Your purse feels lighter.");
97		mtmp->mgold += tmp;
98		rloc(mtmp);
99		mtmp->mflee = 1;
100		flags.botl = 1;
101	}
102}
103
104/* steal armor after he finishes taking it off */
105static unsigned stealoid;		/* object to be stolen */
106static unsigned stealmid;		/* monster doing the stealing */
107
108void
109stealarm(void)
110{
111	struct monst *mtmp;
112	struct obj *otmp;
113
114	for(otmp = invent; otmp; otmp = otmp->nobj)
115	  if(otmp->o_id == stealoid) {
116	    for(mtmp = fmon; mtmp; mtmp = mtmp->nmon)
117	      if(mtmp->m_id == stealmid) {
118		if(dist(mtmp->mx,mtmp->my) < 3) {
119		  freeinv(otmp);
120		  pline("%s steals %s!", Monnam(mtmp), doname(otmp));
121		  mpickobj(mtmp,otmp);
122		  mtmp->mflee = 1;
123		  rloc(mtmp);
124		}
125		break;
126	      }
127	    break;
128	  }
129	stealoid = 0;
130}
131
132/* returns 1 when something was stolen */
133/* (or at least, when N should flee now) */
134/* avoid stealing the object stealoid */
135int
136steal(struct monst *mtmp)
137{
138	struct obj *otmp;
139	int tmp;
140	int named = 0;
141
142	if(!invent){
143	    if(Blind)
144	      pline("Somebody tries to rob you, but finds nothing to steal.");
145	    else
146	      pline("%s tries to rob you, but she finds nothing to steal!",
147		Monnam(mtmp));
148	    return(1);	/* let her flee */
149	}
150	tmp = 0;
151	for(otmp = invent; otmp; otmp = otmp->nobj) if(otmp != uarm2)
152		tmp += ((otmp->owornmask & (W_ARMOR | W_RING)) ? 5 : 1);
153	tmp = rn2(tmp);
154	for(otmp = invent; otmp; otmp = otmp->nobj) if(otmp != uarm2)
155		if((tmp -= ((otmp->owornmask & (W_ARMOR | W_RING)) ? 5 : 1))
156			< 0) break;
157	if(!otmp) {
158		impossible("Steal fails!");
159		return(0);
160	}
161	if(otmp->o_id == stealoid)
162		return(0);
163	if((otmp->owornmask & (W_ARMOR | W_RING))){
164		switch(otmp->olet) {
165		case RING_SYM:
166			ringoff(otmp);
167			break;
168		case ARMOR_SYM:
169			if(multi < 0 || otmp == uarms){
170			  setworn((struct obj *) 0, otmp->owornmask & W_ARMOR);
171			  break;
172			}
173		{ int curssv = otmp->cursed;
174			otmp->cursed = 0;
175			stop_occupation();
176			pline("%s seduces you and %s off your %s.",
177				Amonnam(mtmp, Blind ? "gentle" : "beautiful"),
178				otmp->cursed ? "helps you to take"
179					    : "you start taking",
180				(otmp == uarmg) ? "gloves" :
181				(otmp == uarmh) ? "helmet" : "armor");
182			named++;
183			(void) armoroff(otmp);
184			otmp->cursed = curssv;
185			if(multi < 0){
186				stealoid = otmp->o_id;
187				stealmid = mtmp->m_id;
188				afternmv = stealarm;
189				return(0);
190			}
191			break;
192		}
193		default:
194			impossible("Tried to steal a strange worn thing.");
195		}
196	}
197	else if(otmp == uwep)
198		setuwep((struct obj *) 0);
199	if(otmp->olet == CHAIN_SYM) {
200		impossible("How come you are carrying that chain?");
201	}
202	if(Punished && otmp == uball){
203		Punished = 0;
204		freeobj(uchain);
205		free(uchain);
206		uchain = (struct obj *) 0;
207		uball->spe = 0;
208		uball = (struct obj *) 0;	/* superfluous */
209	}
210	freeinv(otmp);
211	pline("%s stole %s.", named ? "She" : Monnam(mtmp), doname(otmp));
212	mpickobj(mtmp,otmp);
213	return((multi < 0) ? 0 : 1);
214}
215
216void
217mpickobj(struct monst *mtmp, struct obj *otmp)
218{
219	otmp->nobj = mtmp->minvent;
220	mtmp->minvent = otmp;
221}
222
223int
224stealamulet(struct monst *mtmp)
225{
226	struct obj *otmp;
227
228	for(otmp = invent; otmp; otmp = otmp->nobj) {
229	    if(otmp->olet == AMULET_SYM) {
230		/* might be an imitation one */
231		if(otmp == uwep) setuwep((struct obj *) 0);
232		freeinv(otmp);
233		mpickobj(mtmp,otmp);
234		pline("%s stole %s!", Monnam(mtmp), doname(otmp));
235		return(1);
236	    }
237	}
238	return(0);
239}
240
241/* release the objects the killed animal has stolen */
242void
243relobj(struct monst *mtmp, int show)
244{
245	struct obj *otmp, *otmp2;
246
247	for(otmp = mtmp->minvent; otmp; otmp = otmp2){
248		otmp->ox = mtmp->mx;
249		otmp->oy = mtmp->my;
250		otmp2 = otmp->nobj;
251		otmp->nobj = fobj;
252		fobj = otmp;
253		stackobj(fobj);
254		if(show & cansee(mtmp->mx,mtmp->my))
255			atl(otmp->ox,otmp->oy,otmp->olet);
256	}
257	mtmp->minvent = (struct obj *) 0;
258	if(mtmp->mgold || mtmp->data->mlet == 'L') {
259		long tmp;
260
261		tmp = (mtmp->mgold > 10000) ? 10000 : mtmp->mgold;
262		mkgold((long)(tmp + d(dlevel,30)), mtmp->mx, mtmp->my);
263		if(show & cansee(mtmp->mx,mtmp->my))
264			atl(mtmp->mx,mtmp->my,'$');
265	}
266}
267