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