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