xref: /openbsd/games/hack/hack.zap.c (revision 78b63d65)
1 /*	$OpenBSD: hack.zap.c,v 1.4 2001/08/06 22:59:13 pjanzen Exp $	*/
2 
3 /*
4  * Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985.
5  */
6 
7 #ifndef lint
8 static char rcsid[] = "$OpenBSD: hack.zap.c,v 1.4 2001/08/06 22:59:13 pjanzen Exp $";
9 #endif /* not lint */
10 
11 #include "hack.h"
12 
13 extern struct obj *mkobj_at();
14 extern struct monst *makemon(), *mkmon_at(), youmonst;
15 struct monst *bhit();
16 char *exclam();
17 
18 char *fl[]= {
19 	"magic missile",
20 	"bolt of fire",
21 	"sleep ray",
22 	"bolt of cold",
23 	"death ray"
24 };
25 
26 /* Routines for IMMEDIATE wands. */
27 /* bhitm: monster mtmp was hit by the effect of wand otmp */
28 bhitm(mtmp, otmp)
29 register struct monst *mtmp;
30 register struct obj *otmp;
31 {
32 	wakeup(mtmp);
33 	switch(otmp->otyp) {
34 	case WAN_STRIKING:
35 		if(u.uswallow || rnd(20) < 10+mtmp->data->ac) {
36 			register int tmp = d(2,12);
37 			hit("wand", mtmp, exclam(tmp));
38 			mtmp->mhp -= tmp;
39 			if(mtmp->mhp < 1) killed(mtmp);
40 		} else miss("wand", mtmp);
41 		break;
42 	case WAN_SLOW_MONSTER:
43 		mtmp->mspeed = MSLOW;
44 		break;
45 	case WAN_SPEED_MONSTER:
46 		mtmp->mspeed = MFAST;
47 		break;
48 	case WAN_UNDEAD_TURNING:
49 		if(strchr(UNDEAD,mtmp->data->mlet)) {
50 			mtmp->mhp -= rnd(8);
51 			if(mtmp->mhp < 1) killed(mtmp);
52 			else mtmp->mflee = 1;
53 		}
54 		break;
55 	case WAN_POLYMORPH:
56 		if( newcham(mtmp,&mons[rn2(CMNUM)]) )
57 			objects[otmp->otyp].oc_name_known = 1;
58 		break;
59 	case WAN_CANCELLATION:
60 		mtmp->mcan = 1;
61 		break;
62 	case WAN_TELEPORTATION:
63 		rloc(mtmp);
64 		break;
65 	case WAN_MAKE_INVISIBLE:
66 		mtmp->minvis = 1;
67 		break;
68 #ifdef WAN_PROBING
69 	case WAN_PROBING:
70 		mstatusline(mtmp);
71 		break;
72 #endif /* WAN_PROBING */
73 	default:
74 		impossible("What an interesting wand (%u)", otmp->otyp);
75 	}
76 }
77 
78 bhito(obj, otmp)	/* object obj was hit by the effect of wand otmp */
79 register struct obj *obj, *otmp;	/* returns TRUE if sth was done */
80 {
81 	register int res = TRUE;
82 
83 	if(obj == uball || obj == uchain)
84 		res = FALSE;
85 	else
86 	switch(otmp->otyp) {
87 	case WAN_POLYMORPH:
88 		/* preserve symbol and quantity, but turn rocks into gems */
89 		mkobj_at((obj->otyp == ROCK || obj->otyp == ENORMOUS_ROCK)
90 			? GEM_SYM : obj->olet,
91 			obj->ox, obj->oy) -> quan = obj->quan;
92 		delobj(obj);
93 		break;
94 	case WAN_STRIKING:
95 		if(obj->otyp == ENORMOUS_ROCK)
96 			fracture_rock(obj);
97 		else
98 			res = FALSE;
99 		break;
100 	case WAN_CANCELLATION:
101 		if(obj->spe && obj->olet != AMULET_SYM) {
102 			obj->known = 0;
103 			obj->spe = 0;
104 		}
105 		break;
106 	case WAN_TELEPORTATION:
107 		rloco(obj);
108 		break;
109 	case WAN_MAKE_INVISIBLE:
110 		obj->oinvis = 1;
111 		break;
112 	case WAN_UNDEAD_TURNING:
113 		res = revive(obj);
114 		break;
115 	case WAN_SLOW_MONSTER:		/* no effect on objects */
116 	case WAN_SPEED_MONSTER:
117 #ifdef WAN_PROBING
118 	case WAN_PROBING:
119 #endif /* WAN_PROBING */
120 		res = FALSE;
121 		break;
122 	default:
123 		impossible("What an interesting wand (%u)", otmp->otyp);
124 	}
125 	return(res);
126 }
127 
128 dozap()
129 {
130 	register struct obj *obj;
131 	xchar zx,zy;
132 
133 	obj = getobj("/", "zap");
134 	if(!obj) return(0);
135 	if(obj->spe < 0 || (obj->spe == 0 && rn2(121))) {
136 		pline("Nothing Happens.");
137 		return(1);
138 	}
139 	if(obj->spe == 0)
140 		pline("You wrest one more spell from the worn-out wand.");
141 	if(!(objects[obj->otyp].bits & NODIR) && !getdir(1))
142 		return(1);	/* make him pay for knowing !NODIR */
143 	obj->spe--;
144 	if(objects[obj->otyp].bits & IMMEDIATE) {
145 		if(u.uswallow)
146 			bhitm(u.ustuck, obj);
147 		else if(u.dz) {
148 			if(u.dz > 0) {
149 				register struct obj *otmp = o_at(u.ux, u.uy);
150 				if(otmp)
151 					(void) bhito(otmp, obj);
152 			}
153 		} else
154 			(void) bhit(u.dx,u.dy,rn1(8,6),0,bhitm,bhito,obj);
155 	} else {
156 	    switch(obj->otyp){
157 		case WAN_LIGHT:
158 			litroom(TRUE);
159 			break;
160 		case WAN_SECRET_DOOR_DETECTION:
161 			if(!findit()) return(1);
162 			break;
163 		case WAN_CREATE_MONSTER:
164 			{ register int cnt = 1;
165 			if(!rn2(23)) cnt += rn2(7) + 1;
166 			while(cnt--)
167 			    (void) makemon((struct permonst *) 0, u.ux, u.uy);
168 			}
169 			break;
170 		case WAN_WISHING:
171 			{ char buf[BUFSZ];
172 			  register struct obj *otmp;
173 			  extern struct obj *readobjnam(), *addinv();
174 		      if(u.uluck + rn2(5) < 0) {
175 			pline("Unfortunately, nothing happens.");
176 			break;
177 		      }
178 		      pline("You may wish for an object. What do you want? ");
179 		      getlin(buf);
180 		      if(buf[0] == '\033') buf[0] = 0;
181 		      otmp = readobjnam(buf);
182 		      otmp = addinv(otmp);
183 		      prinv(otmp);
184 		      break;
185 			}
186 		case WAN_DIGGING:
187 			/* Original effect (approximately):
188 			 * from CORR: dig until we pierce a wall
189 			 * from ROOM: piece wall and dig until we reach
190 			 * an ACCESSIBLE place.
191 			 * Currently: dig for digdepth positions;
192 			 * also down on request of Lennart Augustsson.
193 			 */
194 			{ register struct rm *room;
195 			  register int digdepth;
196 			if(u.uswallow) {
197 				register struct monst *mtmp = u.ustuck;
198 
199 				pline("You pierce %s's stomach wall!",
200 					monnam(mtmp));
201 				mtmp->mhp = 1;	/* almost dead */
202 				unstuck(mtmp);
203 				mnexto(mtmp);
204 				break;
205 			}
206 			if(u.dz) {
207 			    if(u.dz < 0) {
208 				pline("You loosen a rock from the ceiling.");
209 				pline("It falls on your head!");
210 				losehp(1, "falling rock");
211 				mksobj_at(ROCK, u.ux, u.uy);
212 				fobj->quan = 1;
213 				stackobj(fobj);
214 				if(Invisible) newsym(u.ux, u.uy);
215 			    } else {
216 				dighole();
217 			    }
218 			    break;
219 			}
220 			zx = u.ux+u.dx;
221 			zy = u.uy+u.dy;
222 			digdepth = 8 + rn2(18);
223 			Tmp_at(-1, '*');	/* open call */
224 			while(--digdepth >= 0) {
225 				if(!isok(zx,zy)) break;
226 				room = &levl[zx][zy];
227 				Tmp_at(zx,zy);
228 				if(!xdnstair){
229 					if(zx < 3 || zx > COLNO-3 ||
230 					    zy < 3 || zy > ROWNO-3)
231 						break;
232 					if(room->typ == HWALL ||
233 					    room->typ == VWALL){
234 						room->typ = ROOM;
235 						break;
236 					}
237 				} else
238 				if(room->typ == HWALL || room->typ == VWALL ||
239 				   room->typ == SDOOR || room->typ == LDOOR){
240 					room->typ = DOOR;
241 					digdepth -= 2;
242 				} else
243 				if(room->typ == SCORR || !room->typ) {
244 					room->typ = CORR;
245 					digdepth--;
246 				}
247 				mnewsym(zx,zy);
248 				zx += u.dx;
249 				zy += u.dy;
250 			}
251 			mnewsym(zx,zy);	/* not always necessary */
252 			Tmp_at(-1,-1);	/* closing call */
253 			break;
254 			}
255 		default:
256 			buzz((int) obj->otyp - WAN_MAGIC_MISSILE,
257 				u.ux, u.uy, u.dx, u.dy);
258 			break;
259 		}
260 		if(!objects[obj->otyp].oc_name_known) {
261 			objects[obj->otyp].oc_name_known = 1;
262 			more_experienced(0,10);
263 		}
264 	}
265 	return(1);
266 }
267 
268 char *
269 exclam(force)
270 register int force;
271 {
272 	/* force == 0 occurs e.g. with sleep ray */
273 	/* note that large force is usual with wands so that !! would
274 		require information about hand/weapon/wand */
275 	return( (force < 0) ? "?" : (force <= 4) ? "." : "!" );
276 }
277 
278 hit(str,mtmp,force)
279 register char *str;
280 register struct monst *mtmp;
281 register char *force;		/* usually either "." or "!" */
282 {
283 	if(!cansee(mtmp->mx,mtmp->my)) pline("The %s hits it.", str);
284 	else pline("The %s hits %s%s", str, monnam(mtmp), force);
285 }
286 
287 miss(str,mtmp)
288 register char *str;
289 register struct monst *mtmp;
290 {
291 	if(!cansee(mtmp->mx,mtmp->my)) pline("The %s misses it.",str);
292 	else pline("The %s misses %s.",str,monnam(mtmp));
293 }
294 
295 /* bhit: called when a weapon is thrown (sym = obj->olet) or when an
296    IMMEDIATE wand is zapped (sym = 0); the weapon falls down at end of
297    range or when a monster is hit; the monster is returned, and bhitpos
298    is set to the final position of the weapon thrown; the ray of a wand
299    may affect several objects and monsters on its path - for each of
300    these an argument function is called. */
301 /* check !u.uswallow before calling bhit() */
302 
303 struct monst *
304 bhit(ddx,ddy,range,sym,fhitm,fhito,obj)
305 register int ddx,ddy,range;		/* direction and range */
306 char sym;				/* symbol displayed on path */
307 int (*fhitm)(), (*fhito)();		/* fns called when mon/obj hit */
308 struct obj *obj;			/* 2nd arg to fhitm/fhito */
309 {
310 	register struct monst *mtmp;
311 	register struct obj *otmp;
312 	register int typ;
313 
314 	bhitpos.x = u.ux;
315 	bhitpos.y = u.uy;
316 
317 	if(sym) tmp_at(-1, sym);	/* open call */
318 	while(range-- > 0) {
319 		bhitpos.x += ddx;
320 		bhitpos.y += ddy;
321 		typ = levl[bhitpos.x][bhitpos.y].typ;
322 		if(mtmp = m_at(bhitpos.x,bhitpos.y)){
323 			if(sym) {
324 				tmp_at(-1, -1);	/* close call */
325 				return(mtmp);
326 			}
327 			(*fhitm)(mtmp, obj);
328 			range -= 3;
329 		}
330 		if(fhito && (otmp = o_at(bhitpos.x,bhitpos.y))){
331 			if((*fhito)(otmp, obj))
332 				range--;
333 		}
334 		if(!ZAP_POS(typ)) {
335 			bhitpos.x -= ddx;
336 			bhitpos.y -= ddy;
337 			break;
338 		}
339 		if(sym) tmp_at(bhitpos.x, bhitpos.y);
340 	}
341 
342 	/* leave last symbol unless in a pool */
343 	if(sym)
344 	   tmp_at(-1, (levl[bhitpos.x][bhitpos.y].typ == POOL) ? -1 : 0);
345 	return(0);
346 }
347 
348 struct monst *
349 boomhit(dx,dy) {
350 	register int i, ct;
351 	register struct monst *mtmp;
352 	char sym = ')';
353 	extern schar xdir[], ydir[];
354 
355 	bhitpos.x = u.ux;
356 	bhitpos.y = u.uy;
357 
358 	for(i=0; i<8; i++) if(xdir[i] == dx && ydir[i] == dy) break;
359 	tmp_at(-1, sym);	/* open call */
360 	for(ct=0; ct<10; ct++) {
361 		if(i == 8) i = 0;
362 		sym = ')' + '(' - sym;
363 		tmp_at(-2, sym);	/* change let call */
364 		dx = xdir[i];
365 		dy = ydir[i];
366 		bhitpos.x += dx;
367 		bhitpos.y += dy;
368 		if(mtmp = m_at(bhitpos.x, bhitpos.y)){
369 			tmp_at(-1,-1);
370 			return(mtmp);
371 		}
372 		if(!ZAP_POS(levl[bhitpos.x][bhitpos.y].typ)) {
373 			bhitpos.x -= dx;
374 			bhitpos.y -= dy;
375 			break;
376 		}
377 		if(bhitpos.x == u.ux && bhitpos.y == u.uy) { /* ct == 9 */
378 			if(rn2(20) >= 10+u.ulevel){	/* we hit ourselves */
379 				(void) thitu(10, rnd(10), "boomerang");
380 				break;
381 			} else {	/* we catch it */
382 				tmp_at(-1,-1);
383 				pline("Skillfully, you catch the boomerang.");
384 				return(&youmonst);
385 			}
386 		}
387 		tmp_at(bhitpos.x, bhitpos.y);
388 		if(ct % 5 != 0) i++;
389 	}
390 	tmp_at(-1, -1);	/* do not leave last symbol */
391 	return(0);
392 }
393 
394 char
395 dirlet(dx,dy) register dx,dy; {
396 	return
397 		(dx == dy) ? '\\' : (dx && dy) ? '/' : dx ? '-' : '|';
398 }
399 
400 /* type == -1: monster spitting fire at you */
401 /* type == -1,-2,-3: bolts sent out by wizard */
402 /* called with dx = dy = 0 with vertical bolts */
403 buzz(type,sx,sy,dx,dy)
404 register int type;
405 register xchar sx,sy;
406 register int dx,dy;
407 {
408 	int abstype = abs(type);
409 	register char *fltxt = (type == -1) ? "blaze of fire" : fl[abstype];
410 	struct rm *lev;
411 	xchar range;
412 	struct monst *mon;
413 
414 	if(u.uswallow) {
415 		register int tmp;
416 
417 		if(type < 0) return;
418 		tmp = zhit(u.ustuck, type);
419 		pline("The %s rips into %s%s",
420 			fltxt, monnam(u.ustuck), exclam(tmp));
421 		return;
422 	}
423 	if(type < 0) pru();
424 	range = rn1(7,7);
425 	Tmp_at(-1, dirlet(dx,dy));	/* open call */
426 	while(range-- > 0) {
427 		sx += dx;
428 		sy += dy;
429 		if((lev = &levl[sx][sy])->typ) Tmp_at(sx,sy);
430 		else {
431 			int bounce = 0;
432 			if(cansee(sx-dx,sy-dy))
433 				pline("The %s bounces!", fltxt);
434 			if(ZAP_POS(levl[sx][sy-dy].typ))
435 				bounce = 1;
436 			if(ZAP_POS(levl[sx-dx][sy].typ)) {
437 				if(!bounce || rn2(2)) bounce = 2;
438 			}
439 			switch(bounce){
440 			case 0:
441 				dx = -dx;
442 				dy = -dy;
443 				continue;
444 			case 1:
445 				dy = -dy;
446 				sx -= dx;
447 				break;
448 			case 2:
449 				dx = -dx;
450 				sy -= dy;
451 				break;
452 			}
453 			Tmp_at(-2,dirlet(dx,dy));
454 			continue;
455 		}
456 		if(lev->typ == POOL && abstype == 1 /* fire */) {
457 			range -= 3;
458 			lev->typ = ROOM;
459 			if(cansee(sx,sy)) {
460 				mnewsym(sx,sy);
461 				pline("The water evaporates.");
462 			} else
463 				pline("You hear a hissing sound.");
464 		}
465 		if((mon = m_at(sx,sy)) &&
466 		   (type != -1 || mon->data->mlet != 'D')) {
467 			wakeup(mon);
468 			if(rnd(20) < 18 + mon->data->ac) {
469 				register int tmp = zhit(mon,abstype);
470 				if(mon->mhp < 1) {
471 					if(type < 0) {
472 					    if(cansee(mon->mx,mon->my))
473 					      pline("%s is killed by the %s!",
474 						Monnam(mon), fltxt);
475 					    mondied(mon);
476 					} else
477 					    killed(mon);
478 				} else
479 					hit(fltxt, mon, exclam(tmp));
480 				range -= 2;
481 			} else
482 				miss(fltxt,mon);
483 		} else if(sx == u.ux && sy == u.uy) {
484 			nomul(0);
485 			if(rnd(20) < 18+u.uac) {
486 				register int dam = 0;
487 				range -= 2;
488 				pline("The %s hits you!",fltxt);
489 				switch(abstype) {
490 				case 0:
491 					dam = d(2,6);
492 					break;
493 				case 1:
494 					if(Fire_resistance)
495 						pline("You don't feel hot!");
496 					else dam = d(6,6);
497 					if(!rn2(3))
498 						burn_scrolls();
499 					break;
500 				case 2:
501 					nomul(-rnd(25)); /* sleep ray */
502 					break;
503 				case 3:
504 					if(Cold_resistance)
505 						pline("You don't feel cold!");
506 					else dam = d(6,6);
507 					break;
508 				case 4:
509 					u.uhp = -1;
510 				}
511 				losehp(dam,fltxt);
512 			} else pline("The %s whizzes by you!",fltxt);
513 			stop_occupation();
514 		}
515 		if(!ZAP_POS(lev->typ)) {
516 			int bounce = 0, rmn;
517 			if(cansee(sx,sy)) pline("The %s bounces!",fltxt);
518 			range--;
519 			if(!dx || !dy || !rn2(20)){
520 				dx = -dx;
521 				dy = -dy;
522 			} else {
523 			  if(ZAP_POS(rmn = levl[sx][sy-dy].typ) &&
524 			    (IS_ROOM(rmn) || ZAP_POS(levl[sx+dx][sy-dy].typ)))
525 				bounce = 1;
526 			  if(ZAP_POS(rmn = levl[sx-dx][sy].typ) &&
527 			    (IS_ROOM(rmn) || ZAP_POS(levl[sx-dx][sy+dy].typ)))
528 				if(!bounce || rn2(2))
529 					bounce = 2;
530 
531 			  switch(bounce){
532 			  case 0:
533 				dy = -dy;
534 				dx = -dx;
535 				break;
536 			  case 1:
537 				dy = -dy;
538 				break;
539 			  case 2:
540 				dx = -dx;
541 				break;
542 			  }
543 			  Tmp_at(-2, dirlet(dx,dy));
544 			}
545 		}
546 	}
547 	Tmp_at(-1,-1);
548 }
549 
550 zhit(mon,type)			/* returns damage to mon */
551 register struct monst *mon;
552 register type;
553 {
554 	register int tmp = 0;
555 
556 	switch(type) {
557 	case 0:			/* magic missile */
558 		tmp = d(2,6);
559 		break;
560 	case -1:		/* Dragon blazing fire */
561 	case 1:			/* fire */
562 		if(strchr("Dg", mon->data->mlet)) break;
563 		tmp = d(6,6);
564 		if(strchr("YF", mon->data->mlet)) tmp += 7;
565 		break;
566 	case 2:			/* sleep*/
567 		mon->mfroz = 1;
568 		break;
569 	case 3:			/* cold */
570 		if(strchr("YFgf", mon->data->mlet)) break;
571 		tmp = d(6,6);
572 		if(mon->data->mlet == 'D') tmp += 7;
573 		break;
574 	case 4:			/* death*/
575 		if(strchr(UNDEAD, mon->data->mlet)) break;
576 		tmp = mon->mhp+1;
577 		break;
578 	}
579 	mon->mhp -= tmp;
580 	return(tmp);
581 }
582 
583 #define	CORPSE_I_TO_C(otyp)	(char) ((otyp >= DEAD_ACID_BLOB)\
584 		     ?  'a' + (otyp - DEAD_ACID_BLOB)\
585 		     :	'@' + (otyp - DEAD_HUMAN))
586 revive(obj)
587 register struct obj *obj;
588 {
589 	register struct monst *mtmp;
590 
591 	if(obj->olet == FOOD_SYM && obj->otyp > CORPSE) {
592 		/* do not (yet) revive shopkeepers */
593 		/* Note: this might conceivably produce two monsters
594 			at the same position - strange, but harmless */
595 		mtmp = mkmon_at(CORPSE_I_TO_C(obj->otyp),obj->ox,obj->oy);
596 		delobj(obj);
597 	}
598 	return(!!mtmp);		/* TRUE if some monster created */
599 }
600 
601 rloco(obj)
602 register struct obj *obj;
603 {
604 	register tx,ty,otx,oty;
605 
606 	otx = obj->ox;
607 	oty = obj->oy;
608 	do {
609 		tx = rn1(COLNO-3,2);
610 		ty = rn2(ROWNO);
611 	} while(!goodpos(tx,ty));
612 	obj->ox = tx;
613 	obj->oy = ty;
614 	if(cansee(otx,oty))
615 		newsym(otx,oty);
616 }
617 
618 fracture_rock(obj)	/* fractured by pick-axe or wand of striking */
619 register struct obj *obj;			   /* no texts here! */
620 {
621 	/* unpobj(obj); */
622 	obj->otyp = ROCK;
623 	obj->quan = 7 + rn2(60);
624 	obj->owt = weight(obj);
625 	obj->olet = WEAPON_SYM;
626 	if(cansee(obj->ox,obj->oy))
627 		prl(obj->ox,obj->oy);
628 }
629 
630 burn_scrolls()
631 {
632 	register struct obj *obj, *obj2;
633 	register int cnt = 0;
634 
635 	for(obj = invent; obj; obj = obj2) {
636 		obj2 = obj->nobj;
637 		if(obj->olet == SCROLL_SYM) {
638 			cnt++;
639 			useup(obj);
640 		}
641 	}
642 	if(cnt > 1) {
643 		pline("Your scrolls catch fire!");
644 		losehp(cnt, "burning scrolls");
645 	} else if(cnt) {
646 		pline("Your scroll catches fire!");
647 		losehp(1, "burning scroll");
648 	}
649 }
650