xref: /netbsd-src/games/hack/hack.do_wear.c (revision 274254cdae52594c1aa480a736aef78313d15c9c)
1 /*	$NetBSD: hack.do_wear.c,v 1.5 2003/04/02 18:36:36 jsm 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 <sys/cdefs.h>
65 #ifndef lint
66 __RCSID("$NetBSD: hack.do_wear.c,v 1.5 2003/04/02 18:36:36 jsm Exp $");
67 #endif				/* not lint */
68 
69 #include "hack.h"
70 #include "extern.h"
71 
72 void
73 off_msg(otmp)
74 	struct obj     *otmp;
75 {
76 	pline("You were wearing %s.", doname(otmp));
77 }
78 
79 int
80 doremarm()
81 {
82 	struct obj     *otmp;
83 	if (!uarm && !uarmh && !uarms && !uarmg) {
84 		pline("Not wearing any armor.");
85 		return (0);
86 	}
87 	otmp = (!uarmh && !uarms && !uarmg) ? uarm :
88 		(!uarms && !uarm && !uarmg) ? uarmh :
89 		(!uarmh && !uarm && !uarmg) ? uarms :
90 		(!uarmh && !uarm && !uarms) ? uarmg :
91 		getobj("[", "take off");
92 	if (!otmp)
93 		return (0);
94 	if (!(otmp->owornmask & (W_ARMOR - W_ARM2))) {
95 		pline("You can't take that off.");
96 		return (0);
97 	}
98 	if (otmp == uarmg && uwep && uwep->cursed) {	/* myers@uwmacc */
99 		pline("You seem not able to take off the gloves while holding your weapon.");
100 		return (0);
101 	}
102 	(void) armoroff(otmp);
103 	return (1);
104 }
105 
106 int
107 doremring()
108 {
109 	if (!uleft && !uright) {
110 		pline("Not wearing any ring.");
111 		return (0);
112 	}
113 	if (!uleft)
114 		return (dorr(uright));
115 	if (!uright)
116 		return (dorr(uleft));
117 	if (uleft && uright)
118 		while (1) {
119 			char            answer;
120 
121 			pline("What ring, Right or Left? [ rl?]");
122 			if (strchr(quitchars, (answer = readchar())))
123 				return (0);
124 			switch (answer) {
125 			case 'l':
126 			case 'L':
127 				return (dorr(uleft));
128 			case 'r':
129 			case 'R':
130 				return (dorr(uright));
131 			case '?':
132 				(void) doprring();
133 				/* might look at morc here %% */
134 			}
135 		}
136 	/* NOTREACHED */
137 	return (0);
138 }
139 
140 int
141 dorr(otmp)
142 	struct obj     *otmp;
143 {
144 	if (cursed(otmp))
145 		return (0);
146 	ringoff(otmp);
147 	off_msg(otmp);
148 	return (1);
149 }
150 
151 int
152 cursed(otmp)
153 	struct obj     *otmp;
154 {
155 	if (otmp->cursed) {
156 		pline("You can't. It appears to be cursed.");
157 		return (1);
158 	}
159 	return (0);
160 }
161 
162 int
163 armoroff(otmp)
164 	struct obj     *otmp;
165 {
166 	int             delay = -objects[otmp->otyp].oc_delay;
167 	if (cursed(otmp))
168 		return (0);
169 	setworn((struct obj *) 0, otmp->owornmask & W_ARMOR);
170 	if (delay) {
171 		nomul(delay);
172 		switch (otmp->otyp) {
173 		case HELMET:
174 			nomovemsg = "You finished taking off your helmet.";
175 			break;
176 		case PAIR_OF_GLOVES:
177 			nomovemsg = "You finished taking off your gloves";
178 			break;
179 		default:
180 			nomovemsg = "You finished taking off your suit.";
181 		}
182 	} else {
183 		off_msg(otmp);
184 	}
185 	return (1);
186 }
187 
188 int
189 doweararm()
190 {
191 	struct obj     *otmp;
192 	int             delay;
193 	int             err = 0;
194 	long            mask = 0;
195 
196 	otmp = getobj("[", "wear");
197 	if (!otmp)
198 		return (0);
199 	if (otmp->owornmask & W_ARMOR) {
200 		pline("You are already wearing that!");
201 		return (0);
202 	}
203 	if (otmp->otyp == HELMET) {
204 		if (uarmh) {
205 			pline("You are already wearing a helmet.");
206 			err++;
207 		} else
208 			mask = W_ARMH;
209 	} else if (otmp->otyp == SHIELD) {
210 		if (uarms)
211 			pline("You are already wearing a shield."), err++;
212 		if (uwep && uwep->otyp == TWO_HANDED_SWORD)
213 			pline("You cannot wear a shield and wield a two-handed sword."), err++;
214 		if (!err)
215 			mask = W_ARMS;
216 	} else if (otmp->otyp == PAIR_OF_GLOVES) {
217 		if (uarmg) {
218 			pline("You are already wearing gloves.");
219 			err++;
220 		} else if (uwep && uwep->cursed) {
221 			pline("You cannot wear gloves over your weapon.");
222 			err++;
223 		} else
224 			mask = W_ARMG;
225 	} else {
226 		if (uarm) {
227 			if (otmp->otyp != ELVEN_CLOAK || uarm2) {
228 				pline("You are already wearing some armor.");
229 				err++;
230 			}
231 		}
232 		if (!err)
233 			mask = W_ARM;
234 	}
235 	if (otmp == uwep && uwep->cursed) {
236 		if (!err++)
237 			pline("%s is welded to your hand.", Doname(uwep));
238 	}
239 	if (err)
240 		return (0);
241 	setworn(otmp, mask);
242 	if (otmp == uwep)
243 		setuwep((struct obj *) 0);
244 	delay = -objects[otmp->otyp].oc_delay;
245 	if (delay) {
246 		nomul(delay);
247 		nomovemsg = "You finished your dressing manoeuvre.";
248 	}
249 	otmp->known = 1;
250 	return (1);
251 }
252 
253 int
254 dowearring()
255 {
256 	struct obj     *otmp;
257 	long            mask = 0;
258 	long            oldprop;
259 
260 	if (uleft && uright) {
261 		pline("There are no more ring-fingers to fill.");
262 		return (0);
263 	}
264 	otmp = getobj("=", "wear");
265 	if (!otmp)
266 		return (0);
267 	if (otmp->owornmask & W_RING) {
268 		pline("You are already wearing that!");
269 		return (0);
270 	}
271 	if (otmp == uleft || otmp == uright) {
272 		pline("You are already wearing that.");
273 		return (0);
274 	}
275 	if (otmp == uwep && uwep->cursed) {
276 		pline("%s is welded to your hand.", Doname(uwep));
277 		return (0);
278 	}
279 	if (uleft)
280 		mask = RIGHT_RING;
281 	else if (uright)
282 		mask = LEFT_RING;
283 	else
284 		do {
285 			char            answer;
286 
287 			pline("What ring-finger, Right or Left? ");
288 			if (strchr(quitchars, (answer = readchar())))
289 				return (0);
290 			switch (answer) {
291 			case 'l':
292 			case 'L':
293 				mask = LEFT_RING;
294 				break;
295 			case 'r':
296 			case 'R':
297 				mask = RIGHT_RING;
298 				break;
299 			}
300 		} while (!mask);
301 	setworn(otmp, mask);
302 	if (otmp == uwep)
303 		setuwep((struct obj *) 0);
304 	oldprop = u.uprops[PROP(otmp->otyp)].p_flgs;
305 	u.uprops[PROP(otmp->otyp)].p_flgs |= mask;
306 	switch (otmp->otyp) {
307 	case RIN_LEVITATION:
308 		if (!oldprop)
309 			float_up();
310 		break;
311 	case RIN_PROTECTION_FROM_SHAPE_CHANGERS:
312 		rescham();
313 		break;
314 	case RIN_GAIN_STRENGTH:
315 		u.ustr += otmp->spe;
316 		u.ustrmax += otmp->spe;
317 		if (u.ustr > 118)
318 			u.ustr = 118;
319 		if (u.ustrmax > 118)
320 			u.ustrmax = 118;
321 		flags.botl = 1;
322 		break;
323 	case RIN_INCREASE_DAMAGE:
324 		u.udaminc += otmp->spe;
325 		break;
326 	}
327 	prinv(otmp);
328 	return (1);
329 }
330 
331 void
332 ringoff(obj)
333 	struct obj     *obj;
334 {
335 	long            mask;
336 	mask = obj->owornmask & W_RING;
337 	setworn((struct obj *) 0, obj->owornmask);
338 	if (!(u.uprops[PROP(obj->otyp)].p_flgs & mask))
339 		impossible("Strange... I didnt know you had that ring.");
340 	u.uprops[PROP(obj->otyp)].p_flgs &= ~mask;
341 	switch (obj->otyp) {
342 	case RIN_FIRE_RESISTANCE:
343 		/* Bad luck if the player is in hell... --jgm */
344 		if (!Fire_resistance && dlevel >= 30) {
345 			pline("The flames of Hell burn you to a crisp.");
346 			killer = "stupidity in hell";
347 			done("burned");
348 		}
349 		break;
350 	case RIN_LEVITATION:
351 		if (!Levitation) {	/* no longer floating */
352 			float_down();
353 		}
354 		break;
355 	case RIN_GAIN_STRENGTH:
356 		u.ustr -= obj->spe;
357 		u.ustrmax -= obj->spe;
358 		if (u.ustr > 118)
359 			u.ustr = 118;
360 		if (u.ustrmax > 118)
361 			u.ustrmax = 118;
362 		flags.botl = 1;
363 		break;
364 	case RIN_INCREASE_DAMAGE:
365 		u.udaminc -= obj->spe;
366 		break;
367 	}
368 }
369 
370 void
371 find_ac()
372 {
373 	int             uac = 10;
374 	if (uarm)
375 		uac -= ARM_BONUS(uarm);
376 	if (uarm2)
377 		uac -= ARM_BONUS(uarm2);
378 	if (uarmh)
379 		uac -= ARM_BONUS(uarmh);
380 	if (uarms)
381 		uac -= ARM_BONUS(uarms);
382 	if (uarmg)
383 		uac -= ARM_BONUS(uarmg);
384 	if (uleft && uleft->otyp == RIN_PROTECTION)
385 		uac -= uleft->spe;
386 	if (uright && uright->otyp == RIN_PROTECTION)
387 		uac -= uright->spe;
388 	if (uac != u.uac) {
389 		u.uac = uac;
390 		flags.botl = 1;
391 	}
392 }
393 
394 void
395 glibr()
396 {
397 	struct obj     *otmp;
398 	int             xfl = 0;
399 	if (!uarmg)
400 		if (uleft || uright) {
401 			/* Note: at present also cursed rings fall off */
402 			pline("Your %s off your fingers.",
403 			   (uleft && uright) ? "rings slip" : "ring slips");
404 			xfl++;
405 			if ((otmp = uleft) != Null(obj)) {
406 				ringoff(uleft);
407 				dropx(otmp);
408 			}
409 			if ((otmp = uright) != Null(obj)) {
410 				ringoff(uright);
411 				dropx(otmp);
412 			}
413 		}
414 	if ((otmp = uwep) != Null(obj)) {
415 		/* Note: at present also cursed weapons fall */
416 		setuwep((struct obj *) 0);
417 		dropx(otmp);
418 		pline("Your weapon %sslips from your hands.",
419 		      xfl ? "also " : "");
420 	}
421 }
422 
423 struct obj     *
424 some_armor()
425 {
426 	struct obj     *otmph = uarm;
427 	if (uarmh && (!otmph || !rn2(4)))
428 		otmph = uarmh;
429 	if (uarmg && (!otmph || !rn2(4)))
430 		otmph = uarmg;
431 	if (uarms && (!otmph || !rn2(4)))
432 		otmph = uarms;
433 	return (otmph);
434 }
435 
436 void
437 corrode_armor()
438 {
439 	struct obj     *otmph = some_armor();
440 	if (otmph) {
441 		if (otmph->rustfree ||
442 		    otmph->otyp == ELVEN_CLOAK ||
443 		    otmph->otyp == LEATHER_ARMOR ||
444 		    otmph->otyp == STUDDED_LEATHER_ARMOR) {
445 			pline("Your %s not affected!",
446 			      aobjnam(otmph, "are"));
447 			return;
448 		}
449 		pline("Your %s!", aobjnam(otmph, "corrode"));
450 		otmph->spe--;
451 	}
452 }
453