xref: /netbsd-src/games/hack/hack.wizard.c (revision d5b021c7edb8313eee3804ec3819b28abef1d1a0)
1*d5b021c7Sdholland /*	$NetBSD: hack.wizard.c,v 1.10 2011/08/07 06:03:45 dholland Exp $	*/
23ea4a95cSchristos 
3210cab45Smycroft /*
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.
62210cab45Smycroft  */
63210cab45Smycroft 
643ea4a95cSchristos #include <sys/cdefs.h>
65210cab45Smycroft #ifndef lint
66*d5b021c7Sdholland __RCSID("$NetBSD: hack.wizard.c,v 1.10 2011/08/07 06:03:45 dholland Exp $");
67210cab45Smycroft #endif				/* not lint */
6861f28255Scgd 
6961f28255Scgd /* wizard code - inspired by rogue code from Merlyn Leroy (digi-g!brian) */
7061f28255Scgd 
7161f28255Scgd #include "hack.h"
723ea4a95cSchristos #include "extern.h"
7361f28255Scgd 
743ea4a95cSchristos #define	WIZSHOT	    6		/* one chance in WIZSHOT that wizard will try
753ea4a95cSchristos 				 * magic */
763ea4a95cSchristos #define	BOLT_LIM    8		/* from this distance D and 1 will try to hit
773ea4a95cSchristos 				 * you */
7861f28255Scgd 
799b92b189Sdholland static const char wizapp[] = "@DNPTUVXcemntx";
809b92b189Sdholland 
819b92b189Sdholland static void aggravate(void);
829b92b189Sdholland static void clonewiz(struct monst *);
839b92b189Sdholland 
8461f28255Scgd 
8561f28255Scgd /* If he has found the Amulet, make the wizard appear after some time */
863ea4a95cSchristos void
amulet(void)871fa8a9a6Sdholland amulet(void)
883ea4a95cSchristos {
893ea4a95cSchristos 	struct obj     *otmp;
903ea4a95cSchristos 	struct monst   *mtmp;
9161f28255Scgd 
9261f28255Scgd 	if (!flags.made_amulet || !flags.no_of_wizards)
9361f28255Scgd 		return;
9461f28255Scgd 	/* find wizard, and wake him if necessary */
9561f28255Scgd 	for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
9661f28255Scgd 		if (mtmp->data->mlet == '1' && mtmp->msleep && !rn2(40))
9761f28255Scgd 			for (otmp = invent; otmp; otmp = otmp->nobj)
9861f28255Scgd 				if (otmp->olet == AMULET_SYM && !otmp->spe) {
9961f28255Scgd 					mtmp->msleep = 0;
10061f28255Scgd 					if (dist(mtmp->mx, mtmp->my) > 2)
10161f28255Scgd 						pline(
10261f28255Scgd 						      "You get the creepy feeling that somebody noticed your taking the Amulet."
10361f28255Scgd 							);
10461f28255Scgd 					return;
10561f28255Scgd 				}
10661f28255Scgd }
10761f28255Scgd 
1083ea4a95cSchristos int
wiz_hit(struct monst * mtmp)1091fa8a9a6Sdholland wiz_hit(struct monst *mtmp)
11061f28255Scgd {
11161f28255Scgd 	/* if we have stolen or found the amulet, we disappear */
11261f28255Scgd 	if (mtmp->minvent && mtmp->minvent->olet == AMULET_SYM &&
11361f28255Scgd 	    mtmp->minvent->spe == 0) {
11461f28255Scgd 		/* vanish -- very primitive */
11561f28255Scgd 		fall_down(mtmp);
11661f28255Scgd 		return (1);
11761f28255Scgd 	}
11861f28255Scgd 	/* if it is lying around someplace, we teleport to it */
11961f28255Scgd 	if (!carrying(AMULET_OF_YENDOR)) {
1203ea4a95cSchristos 		struct obj     *otmp;
12161f28255Scgd 
12261f28255Scgd 		for (otmp = fobj; otmp; otmp = otmp->nobj)
12361f28255Scgd 			if (otmp->olet == AMULET_SYM && !otmp->spe) {
12461f28255Scgd 				if ((u.ux != otmp->ox || u.uy != otmp->oy) &&
12561f28255Scgd 				    !m_at(otmp->ox, otmp->oy)) {
12661f28255Scgd 
12761f28255Scgd 					/* teleport to it and pick it up */
12861f28255Scgd 					mtmp->mx = otmp->ox;
12961f28255Scgd 					mtmp->my = otmp->oy;
13061f28255Scgd 					freeobj(otmp);
13161f28255Scgd 					mpickobj(mtmp, otmp);
13261f28255Scgd 					pmon(mtmp);
13361f28255Scgd 					return (0);
13461f28255Scgd 				}
13561f28255Scgd 				goto hithim;
13661f28255Scgd 			}
13761f28255Scgd 		return (0);	/* we don't know where it is */
13861f28255Scgd 	}
13961f28255Scgd hithim:
14061f28255Scgd 	if (rn2(2)) {		/* hit - perhaps steal */
14161f28255Scgd 
1423ea4a95cSchristos 		/*
1433ea4a95cSchristos 		 * if hit 1/20 chance of stealing amulet & vanish - amulet is
1443ea4a95cSchristos 		 * on level 26 again.
1453ea4a95cSchristos 		 */
14661f28255Scgd 		if (hitu(mtmp, d(mtmp->data->damn, mtmp->data->damd))
1473c439f43Sdholland 		    && !rn2(20) && stealamulet(mtmp)) {
1483c439f43Sdholland 			/* nothing */
1493c439f43Sdholland 		}
1503ea4a95cSchristos 	} else
15161f28255Scgd 		inrange(mtmp);	/* try magic */
15261f28255Scgd 	return (0);
15361f28255Scgd }
15461f28255Scgd 
1553ea4a95cSchristos void
inrange(struct monst * mtmp)1561fa8a9a6Sdholland inrange(struct monst *mtmp)
15761f28255Scgd {
1583ea4a95cSchristos 	schar           tx, ty;
15961f28255Scgd 
16061f28255Scgd 	/* do nothing if cancelled (but make '1' say something) */
16161f28255Scgd 	if (mtmp->data->mlet != '1' && mtmp->mcan)
16261f28255Scgd 		return;
16361f28255Scgd 
16461f28255Scgd 	/* spit fire only when both in a room or both in a corridor */
1653ea4a95cSchristos 	if (inroom(u.ux, u.uy) != inroom(mtmp->mx, mtmp->my))
1663ea4a95cSchristos 		return;
16761f28255Scgd 	tx = u.ux - mtmp->mx;
16861f28255Scgd 	ty = u.uy - mtmp->my;
16961f28255Scgd 	if ((!tx && abs(ty) < BOLT_LIM) || (!ty && abs(tx) < BOLT_LIM)
17061f28255Scgd 	    || (abs(tx) == abs(ty) && abs(tx) < BOLT_LIM)) {
17161f28255Scgd 		switch (mtmp->data->mlet) {
17261f28255Scgd 		case 'D':
17361f28255Scgd 			/* spit fire in the direction of @ (not nec. hitting) */
17461f28255Scgd 			buzz(-1, mtmp->mx, mtmp->my, sgn(tx), sgn(ty));
17561f28255Scgd 			break;
17661f28255Scgd 		case '1':
1773ea4a95cSchristos 			if (rn2(WIZSHOT))
1783ea4a95cSchristos 				break;
1793ea4a95cSchristos 			/*
1803ea4a95cSchristos 			 * if you zapped wizard with wand of cancellation, he
1813ea4a95cSchristos 			 * has to shake off the effects before he can throw
1823ea4a95cSchristos 			 * spells successfully.  1/2 the time they fail
1833ea4a95cSchristos 			 * anyway
1843ea4a95cSchristos 			 */
18561f28255Scgd 			if (mtmp->mcan || rn2(2)) {
18661f28255Scgd 				if (canseemon(mtmp))
18761f28255Scgd 					pline("%s makes a gesture, then curses.",
18861f28255Scgd 					      Monnam(mtmp));
18961f28255Scgd 				else
19061f28255Scgd 					pline("You hear mumbled cursing.");
19161f28255Scgd 				if (!rn2(3)) {
19261f28255Scgd 					mtmp->mspeed = 0;
19361f28255Scgd 					mtmp->minvis = 0;
19461f28255Scgd 				}
19561f28255Scgd 				if (!rn2(3))
19661f28255Scgd 					mtmp->mcan = 0;
19761f28255Scgd 			} else {
19861f28255Scgd 				if (canseemon(mtmp)) {
19961f28255Scgd 					if (!rn2(6) && !Invis) {
20061f28255Scgd 						pline("%s hypnotizes you.", Monnam(mtmp));
20161f28255Scgd 						nomul(rn2(3) + 3);
20261f28255Scgd 						break;
20361f28255Scgd 					} else
20461f28255Scgd 						pline("%s chants an incantation.",
20561f28255Scgd 						      Monnam(mtmp));
20661f28255Scgd 				} else
20761f28255Scgd 					pline("You hear a mumbled incantation.");
20861f28255Scgd 				switch (rn2(Invis ? 5 : 6)) {
20961f28255Scgd 				case 0:
2103ea4a95cSchristos 					/*
2113ea4a95cSchristos 					 * create a nasty monster from a deep
2123ea4a95cSchristos 					 * level
2133ea4a95cSchristos 					 */
2143ea4a95cSchristos 					/*
2153ea4a95cSchristos 					 * (for the moment, 'nasty' is not
2163ea4a95cSchristos 					 * implemented)
2173ea4a95cSchristos 					 */
21861f28255Scgd 					(void) makemon((struct permonst *) 0, u.ux, u.uy);
21961f28255Scgd 					break;
22061f28255Scgd 				case 1:
22161f28255Scgd 					pline("\"Destroy the thief, my pets!\"");
2223ea4a95cSchristos 					aggravate();	/* aggravate all the
2233ea4a95cSchristos 							 * monsters */
224*d5b021c7Sdholland 					/* FALLTHROUGH */
22561f28255Scgd 				case 2:
22661f28255Scgd 					if (flags.no_of_wizards == 1 && rnd(5) == 0)
2273ea4a95cSchristos 						/*
2283ea4a95cSchristos 						 * if only 1 wizard, clone
2293ea4a95cSchristos 						 * himself
2303ea4a95cSchristos 						 */
23161f28255Scgd 						clonewiz(mtmp);
23261f28255Scgd 					break;
23361f28255Scgd 				case 3:
23461f28255Scgd 					if (mtmp->mspeed == MSLOW)
23561f28255Scgd 						mtmp->mspeed = 0;
23661f28255Scgd 					else
23761f28255Scgd 						mtmp->mspeed = MFAST;
23861f28255Scgd 					break;
23961f28255Scgd 				case 4:
24061f28255Scgd 					mtmp->minvis = 1;
24161f28255Scgd 					break;
24261f28255Scgd 				case 5:
24361f28255Scgd 					/* Only if not Invisible */
24461f28255Scgd 					pline("You hear a clap of thunder!");
2453ea4a95cSchristos 					/*
2463ea4a95cSchristos 					 * shoot a bolt of fire or cold, or a
2473ea4a95cSchristos 					 * sleep ray
2483ea4a95cSchristos 					 */
24961f28255Scgd 					buzz(-rnd(3), mtmp->mx, mtmp->my, sgn(tx), sgn(ty));
25061f28255Scgd 					break;
25161f28255Scgd 				}
25261f28255Scgd 			}
25361f28255Scgd 		}
2543ea4a95cSchristos 		if (u.uhp < 1)
2553ea4a95cSchristos 			done_in_by(mtmp);
25661f28255Scgd 	}
25761f28255Scgd }
25861f28255Scgd 
2593ea4a95cSchristos void
aggravate(void)2601fa8a9a6Sdholland aggravate(void)
26161f28255Scgd {
2623ea4a95cSchristos 	struct monst   *mtmp;
26361f28255Scgd 
26461f28255Scgd 	for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
26561f28255Scgd 		mtmp->msleep = 0;
26661f28255Scgd 		if (mtmp->mfroz && !rn2(5))
26761f28255Scgd 			mtmp->mfroz = 0;
26861f28255Scgd 	}
26961f28255Scgd }
27061f28255Scgd 
2713ea4a95cSchristos void
clonewiz(struct monst * mtmp)2721fa8a9a6Sdholland clonewiz(struct monst *mtmp)
27361f28255Scgd {
2743ea4a95cSchristos 	struct monst   *mtmp2;
27561f28255Scgd 
2763ea4a95cSchristos 	if ((mtmp2 = makemon(PM_WIZARD, mtmp->mx, mtmp->my)) != NULL) {
27761f28255Scgd 		flags.no_of_wizards = 2;
27861f28255Scgd 		unpmon(mtmp2);
27961f28255Scgd 		mtmp2->mappearance = wizapp[rn2(sizeof(wizapp) - 1)];
28061f28255Scgd 		pmon(mtmp);
28161f28255Scgd 	}
28261f28255Scgd }
283