xref: /openbsd-src/games/hack/hack.wizard.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /*	$OpenBSD: hack.wizard.c,v 1.6 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 /* wizard code - inspired by rogue code from Merlyn Leroy (digi-g!brian) */
65 
66 #include <stdlib.h>
67 #include "hack.h"
68 extern struct permonst pm_wizard;
69 
70 static void clonewiz(struct monst *);
71 
72 #define	WIZSHOT	    6	/* one chance in WIZSHOT that wizard will try magic */
73 #define	BOLT_LIM    8	/* from this distance D and 1 will try to hit you */
74 
75 char wizapp[] = "@DNPTUVXcemntx";
76 
77 /* If he has found the Amulet, make the wizard appear after some time */
78 void
79 amulet()
80 {
81 	struct obj *otmp;
82 	struct monst *mtmp;
83 
84 	if(!flags.made_amulet || !flags.no_of_wizards)
85 		return;
86 	/* find wizard, and wake him if necessary */
87 	for(mtmp = fmon; mtmp; mtmp = mtmp->nmon)
88 	    if(mtmp->data->mlet == '1' && mtmp->msleep && !rn2(40))
89 		for(otmp = invent; otmp; otmp = otmp->nobj)
90 		    if(otmp->olet == AMULET_SYM && !otmp->spe) {
91 			mtmp->msleep = 0;
92 			if(dist(mtmp->mx,mtmp->my) > 2)
93 			    pline(
94     "You get the creepy feeling that somebody noticed your taking the Amulet."
95 			    );
96 			return;
97 		    }
98 }
99 
100 int
101 wiz_hit(struct monst *mtmp)
102 {
103 	/* if we have stolen or found the amulet, we disappear */
104 	if(mtmp->minvent && mtmp->minvent->olet == AMULET_SYM &&
105 	    mtmp->minvent->spe == 0) {
106 		/* vanish -- very primitive */
107 		fall_down(mtmp);
108 		return(1);
109 	}
110 
111 	/* if it is lying around someplace, we teleport to it */
112 	if(!carrying(AMULET_OF_YENDOR)) {
113 	    struct obj *otmp;
114 
115 	    for(otmp = fobj; otmp; otmp = otmp->nobj)
116 		if(otmp->olet == AMULET_SYM && !otmp->spe) {
117 		    if((u.ux != otmp->ox || u.uy != otmp->oy) &&
118 		       !m_at(otmp->ox, otmp->oy)) {
119 
120 			/* teleport to it and pick it up */
121 			mtmp->mx = otmp->ox;
122 			mtmp->my = otmp->oy;
123 			freeobj(otmp);
124 			mpickobj(mtmp, otmp);
125 			pmon(mtmp);
126 			return(0);
127 		    }
128 		    goto hithim;
129 		}
130 	    return(0);				/* we don't know where it is */
131 	}
132 hithim:
133 	if(rn2(2)) {				/* hit - perhaps steal */
134 
135 	    /* if hit 1/20 chance of stealing amulet & vanish
136 		- amulet is on level 26 again. */
137 	    if(hitu(mtmp, d(mtmp->data->damn,mtmp->data->damd))
138 		&& !rn2(20) && stealamulet(mtmp))
139 		;
140 	}
141 	else
142 	    inrange(mtmp);			/* try magic */
143 	return(0);
144 }
145 
146 void
147 inrange(struct monst *mtmp)
148 {
149 	schar tx,ty;
150 
151 	/* do nothing if cancelled (but make '1' say something) */
152 	if(mtmp->data->mlet != '1' && mtmp->mcan)
153 		return;
154 
155 	/* spit fire only when both in a room or both in a corridor */
156 	if(inroom(u.ux,u.uy) != inroom(mtmp->mx,mtmp->my)) return;
157 	tx = u.ux - mtmp->mx;
158 	ty = u.uy - mtmp->my;
159 	if((!tx && abs(ty) < BOLT_LIM) || (!ty && abs(tx) < BOLT_LIM)
160 	    || (abs(tx) == abs(ty) && abs(tx) < BOLT_LIM)){
161 	    switch(mtmp->data->mlet) {
162 	    case 'D':
163 		/* spit fire in the direction of @ (not nec. hitting) */
164 		buzz(-1,mtmp->mx,mtmp->my,sgn(tx),sgn(ty));
165 		break;
166 	    case '1':
167 		if(rn2(WIZSHOT)) break;
168 		/* if you zapped wizard with wand of cancellation,
169 		he has to shake off the effects before he can throw
170 		spells successfully.  1/2 the time they fail anyway */
171 		if(mtmp->mcan || rn2(2)) {
172 		    if(canseemon(mtmp))
173 			pline("%s makes a gesture, then curses.",
174 			    Monnam(mtmp));
175 		    else
176 			pline("You hear mumbled cursing.");
177 		    if(!rn2(3)) {
178 			mtmp->mspeed = 0;
179 			mtmp->minvis = 0;
180 		    }
181 		    if(!rn2(3))
182 			mtmp->mcan = 0;
183 		} else {
184 		    if(canseemon(mtmp)){
185 			if(!rn2(6) && !Invis) {
186 			    pline("%s hypnotizes you.", Monnam(mtmp));
187 			    nomul(rn2(3) + 3);
188 			    break;
189 			} else
190 			    pline("%s chants an incantation.",
191 				Monnam(mtmp));
192 		    } else
193 			    pline("You hear a mumbled incantation.");
194 		    switch(rn2(Invis ? 5 : 6)) {
195 		    case 0:
196 			/* create a nasty monster from a deep level */
197 			/* (for the moment, 'nasty' is not implemented) */
198 			(void) makemon((struct permonst *)0, u.ux, u.uy);
199 			break;
200 		    case 1:
201 			pline("\"Destroy the thief, my pets!\"");
202 			aggravate();	/* aggravate all the monsters */
203 			/* fall into next case */
204 		    case 2:
205 			if (flags.no_of_wizards == 1 && rnd(5) == 0)
206 			    /* if only 1 wizard, clone himself */
207 			    clonewiz(mtmp);
208 			break;
209 		    case 3:
210 			if(mtmp->mspeed == MSLOW)
211 				mtmp->mspeed = 0;
212 			else
213 				mtmp->mspeed = MFAST;
214 			break;
215 		    case 4:
216 			mtmp->minvis = 1;
217 			break;
218 		    case 5:
219 			/* Only if not Invisible */
220 			pline("You hear a clap of thunder!");
221 			/* shoot a bolt of fire or cold, or a sleep ray */
222 			buzz(-rnd(3),mtmp->mx,mtmp->my,sgn(tx),sgn(ty));
223 			break;
224 		    }
225 		}
226 	    }
227 	    if(u.uhp < 1) done_in_by(mtmp);
228 	}
229 }
230 
231 void
232 aggravate()
233 {
234 	struct monst *mtmp;
235 
236 	for(mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
237 		mtmp->msleep = 0;
238 		if(mtmp->mfroz && !rn2(5))
239 			mtmp->mfroz = 0;
240 	}
241 }
242 
243 static void
244 clonewiz(struct monst *mtmp)
245 {
246 	struct monst *mtmp2;
247 
248 	if ((mtmp2 = makemon(PM_WIZARD, mtmp->mx, mtmp->my))) {
249 		flags.no_of_wizards = 2;
250 		unpmon(mtmp2);
251 		mtmp2->mappearance = wizapp[rn2(sizeof(wizapp)-1)];
252 		pmon(mtmp);
253 	}
254 }
255