Source:NetHack 3.6.6/src/explode.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to explode.c from the source code of NetHack 3.6.6. To link to a particular line, write [[Source:NetHack 3.6.6/src/explode.c#line12]], for example.

The NetHack General Public License applies to screenshots, source code and other content from NetHack.

This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

1. /* NetHack 3.6	explode.c	$NHDT-Date: 1545182146 2018/12/19 01:15:46 $  $NHDT-Branch: NetHack-3.6.2-beta01 $:$NHDT-Revision: 1.60 $ */
2. /*      Copyright (C) 1990 by Ken Arromdee */
3. /* NetHack may be freely redistributed.  See license for details. */
4. 
5. #include "hack.h"
6. 
7. /* Note: Arrays are column first, while the screen is row first */
8. static int explosion[3][3] = { { S_explode1, S_explode4, S_explode7 },
9.                                { S_explode2, S_explode5, S_explode8 },
10.                                { S_explode3, S_explode6, S_explode9 } };
11. 
12. /* Note: I had to choose one of three possible kinds of "type" when writing
13.  * this function: a wand type (like in zap.c), an adtyp, or an object type.
14.  * Wand types get complex because they must be converted to adtyps for
15.  * determining such things as fire resistance.  Adtyps get complex in that
16.  * they don't supply enough information--was it a player or a monster that
17.  * did it, and with a wand, spell, or breath weapon?  Object types share both
18.  * these disadvantages....
19.  *
20.  * Important note about Half_physical_damage:
21.  *      Unlike losehp(), explode() makes the Half_physical_damage adjustments
22.  *      itself, so the caller should never have done that ahead of time.
23.  *      It has to be done this way because the damage value is applied to
24.  *      things beside the player. Care is taken within explode() to ensure
25.  *      that Half_physical_damage only affects the damage applied to the hero.
26.  */
27. void
28. explode(x, y, type, dam, olet, expltype)
29. int x, y;
30. int type; /* the same as in zap.c; passes -(wand typ) for some WAND_CLASS */
31. int dam;
32. char olet;
33. int expltype;
34. {
35.     int i, j, k, damu = dam;
36.     boolean starting = 1;
37.     boolean visible, any_shield;
38.     int uhurt = 0; /* 0=unhurt, 1=items damaged, 2=you and items damaged */
39.     const char *str = (const char *) 0;
40.     int idamres, idamnonres;
41.     struct monst *mtmp, *mdef = 0;
42.     uchar adtyp;
43.     int explmask[3][3]; /* 0=normal explosion, 1=do shieldeff, 2=do nothing */
44.     boolean shopdamage = FALSE, generic = FALSE, physical_dmg = FALSE,
45.             do_hallu = FALSE, inside_engulfer, grabbed, grabbing;
46.     coord grabxy;
47.     char hallu_buf[BUFSZ], killr_buf[BUFSZ];
48.     short exploding_wand_typ = 0;
49. 
50.     if (olet == WAND_CLASS) { /* retributive strike */
51.         /* 'type' is passed as (wand's object type * -1); save
52.            object type and convert 'type' itself to zap-type */
53.         if (type < 0) {
54.             type = -type;
55.             exploding_wand_typ = (short) type;
56.             /* most attack wands produce specific explosions;
57.                other types produce a generic magical explosion */
58.             if (objects[type].oc_dir == RAY
59.                 && type != WAN_DIGGING && type != WAN_SLEEP) {
60.                 type -= WAN_MAGIC_MISSILE;
61.                 if (type < 0 || type > 9) {
62.                     impossible("explode: wand has bad zap type (%d).", type);
63.                     type = 0;
64.                 }
65.             } else
66.                 type = 0;
67.         }
68.         switch (Role_switch) {
69.         case PM_PRIEST:
70.         case PM_MONK:
71.         case PM_WIZARD:
72.             damu /= 5;
73.             break;
74.         case PM_HEALER:
75.         case PM_KNIGHT:
76.             damu /= 2;
77.             break;
78.         default:
79.             break;
80.         }
81.     }
82.     /* muse_unslime: SCR_FIRE */
83.     if (expltype < 0) {
84.         /* hero gets credit/blame for killing this monster, not others */
85.         mdef = m_at(x, y);
86.         expltype = -expltype;
87.     }
88.     /* if hero is engulfed and caused the explosion, only hero and
89.        engulfer will be affected */
90.     inside_engulfer = (u.uswallow && type >= 0);
91.     /* held but not engulfed implies holder is reaching into second spot
92.        so might get hit by double damage */
93.     grabbed = grabbing = FALSE;
94.     if (u.ustuck && !u.uswallow) {
95.         if (Upolyd && sticks(youmonst.data))
96.             grabbing = TRUE;
97.         else
98.             grabbed = TRUE;
99.         grabxy.x = u.ustuck->mx;
100.         grabxy.y = u.ustuck->my;
101.     } else
102.         grabxy.x = grabxy.y = 0; /* lint suppression */
103.     /* FIXME:
104.      *  It is possible for a grabber to be outside the explosion
105.      *  radius and reaching inside to hold the hero.  If so, it ought
106.      *  to take damage (the extra half of double damage).  It is also
107.      *  possible for poly'd hero to be outside the radius and reaching
108.      *  in to hold a monster.  Hero should take damage in that situation.
109.      *
110.      *  Probably the simplest way to handle this would be to expand
111.      *  the radius used when collecting targets but exclude everything
112.      *  beyond the regular radius which isn't reaching inside.  Then
113.      *  skip harm to gear of any extended targets when inflicting damage.
114.      */
115. 
116.     if (olet == MON_EXPLODE) {
117.         /* when explode() is called recursively, killer.name might change so
118.            we need to retain a copy of the current value for this explosion */
119.         str = strcpy(killr_buf, killer.name);
120.         do_hallu = (Hallucination
121.                     && (strstri(str, "'s explosion")
122.                         || strstri(str, "s' explosion")));
123.         adtyp = AD_PHYS;
124.     } else
125.         switch (abs(type) % 10) {
126.         case 0:
127.             str = "magical blast";
128.             adtyp = AD_MAGM;
129.             break;
130.         case 1:
131.             str = (olet == BURNING_OIL) ? "burning oil"
132.                      : (olet == SCROLL_CLASS) ? "tower of flame" : "fireball";
133.             /* fire damage, not physical damage */
134.             adtyp = AD_FIRE;
135.             break;
136.         case 2:
137.             str = "ball of cold";
138.             adtyp = AD_COLD;
139.             break;
140.         case 4:
141.             str = (olet == WAND_CLASS) ? "death field"
142.                                        : "disintegration field";
143.             adtyp = AD_DISN;
144.             break;
145.         case 5:
146.             str = "ball of lightning";
147.             adtyp = AD_ELEC;
148.             break;
149.         case 6:
150.             str = "poison gas cloud";
151.             adtyp = AD_DRST;
152.             break;
153.         case 7:
154.             str = "splash of acid";
155.             adtyp = AD_ACID;
156.             break;
157.         default:
158.             impossible("explosion base type %d?", type);
159.             return;
160.         }
161. 
162.     any_shield = visible = FALSE;
163.     for (i = 0; i < 3; i++)
164.         for (j = 0; j < 3; j++) {
165.             if (!isok(i + x - 1, j + y - 1)) {
166.                 explmask[i][j] = 2;
167.                 continue;
168.             } else
169.                 explmask[i][j] = 0;
170. 
171.             if (i + x - 1 == u.ux && j + y - 1 == u.uy) {
172.                 switch (adtyp) {
173.                 case AD_PHYS:
174.                     explmask[i][j] = 0;
175.                     break;
176.                 case AD_MAGM:
177.                     explmask[i][j] = !!Antimagic;
178.                     break;
179.                 case AD_FIRE:
180.                     explmask[i][j] = !!Fire_resistance;
181.                     break;
182.                 case AD_COLD:
183.                     explmask[i][j] = !!Cold_resistance;
184.                     break;
185.                 case AD_DISN:
186.                     explmask[i][j] = (olet == WAND_CLASS)
187.                                          ? !!(nonliving(youmonst.data)
188.                                               || is_demon(youmonst.data))
189.                                          : !!Disint_resistance;
190.                     break;
191.                 case AD_ELEC:
192.                     explmask[i][j] = !!Shock_resistance;
193.                     break;
194.                 case AD_DRST:
195.                     explmask[i][j] = !!Poison_resistance;
196.                     break;
197.                 case AD_ACID:
198.                     explmask[i][j] = !!Acid_resistance;
199.                     physical_dmg = TRUE;
200.                     break;
201.                 default:
202.                     impossible("explosion type %d?", adtyp);
203.                     break;
204.                 }
205.             }
206.             /* can be both you and mtmp if you're swallowed or riding */
207.             mtmp = m_at(i + x - 1, j + y - 1);
208.             if (!mtmp && i + x - 1 == u.ux && j + y - 1 == u.uy)
209.                 mtmp = u.usteed;
210.             if (mtmp) {
211.                 if (DEADMONSTER(mtmp))
212.                     explmask[i][j] = 2;
213.                 else
214.                     switch (adtyp) {
215.                     case AD_PHYS:
216.                         break;
217.                     case AD_MAGM:
218.                         explmask[i][j] |= resists_magm(mtmp);
219.                         break;
220.                     case AD_FIRE:
221.                         explmask[i][j] |= resists_fire(mtmp);
222.                         break;
223.                     case AD_COLD:
224.                         explmask[i][j] |= resists_cold(mtmp);
225.                         break;
226.                     case AD_DISN:
227.                         explmask[i][j] |= (olet == WAND_CLASS)
228.                                               ? (nonliving(mtmp->data)
229.                                                  || is_demon(mtmp->data)
230.                                                  || is_vampshifter(mtmp))
231.                                               : resists_disint(mtmp);
232.                         break;
233.                     case AD_ELEC:
234.                         explmask[i][j] |= resists_elec(mtmp);
235.                         break;
236.                     case AD_DRST:
237.                         explmask[i][j] |= resists_poison(mtmp);
238.                         break;
239.                     case AD_ACID:
240.                         explmask[i][j] |= resists_acid(mtmp);
241.                         break;
242.                     default:
243.                         impossible("explosion type %d?", adtyp);
244.                         break;
245.                     }
246.             }
247.             if (mtmp && cansee(i + x - 1, j + y - 1) && !canspotmon(mtmp))
248.                 map_invisible(i + x - 1, j + y - 1);
249.             else if (!mtmp)
250.                 (void) unmap_invisible(i + x - 1, j + y - 1);
251.             if (cansee(i + x - 1, j + y - 1))
252.                 visible = TRUE;
253.             if (explmask[i][j] == 1)
254.                 any_shield = TRUE;
255.         }
256. 
257.     if (visible) {
258.         /* Start the explosion */
259.         for (i = 0; i < 3; i++)
260.             for (j = 0; j < 3; j++) {
261.                 if (explmask[i][j] == 2)
262.                     continue;
263.                 tmp_at(starting ? DISP_BEAM : DISP_CHANGE,
264.                        explosion_to_glyph(expltype, explosion[i][j]));
265.                 tmp_at(i + x - 1, j + y - 1);
266.                 starting = 0;
267.             }
268.         curs_on_u(); /* will flush screen and output */
269. 
270.         if (any_shield && flags.sparkle) { /* simulate shield effect */
271.             for (k = 0; k < SHIELD_COUNT; k++) {
272.                 for (i = 0; i < 3; i++)
273.                     for (j = 0; j < 3; j++) {
274.                         if (explmask[i][j] == 1)
275.                             /*
276.                              * Bypass tmp_at() and send the shield glyphs
277.                              * directly to the buffered screen.  tmp_at()
278.                              * will clean up the location for us later.
279.                              */
280.                             show_glyph(i + x - 1, j + y - 1,
281.                                        cmap_to_glyph(shield_static[k]));
282.                     }
283.                 curs_on_u(); /* will flush screen and output */
284.                 delay_output();
285.             }
286. 
287.             /* Cover last shield glyph with blast symbol. */
288.             for (i = 0; i < 3; i++)
289.                 for (j = 0; j < 3; j++) {
290.                     if (explmask[i][j] == 1)
291.                         show_glyph(
292.                             i + x - 1, j + y - 1,
293.                             explosion_to_glyph(expltype, explosion[i][j]));
294.                 }
295. 
296.         } else { /* delay a little bit. */
297.             delay_output();
298.             delay_output();
299.         }
300. 
301.         tmp_at(DISP_END, 0); /* clear the explosion */
302.     } else {
303.         if (olet == MON_EXPLODE) {
304.             str = "explosion";
305.             generic = TRUE;
306.         }
307.         if (!Deaf && olet != SCROLL_CLASS)
308.             You_hear("a blast.");
309.     }
310. 
311.     if (dam)
312.         for (i = 0; i < 3; i++)
313.             for (j = 0; j < 3; j++) {
314.                 if (explmask[i][j] == 2)
315.                     continue;
316.                 if (i + x - 1 == u.ux && j + y - 1 == u.uy)
317.                     uhurt = (explmask[i][j] == 1) ? 1 : 2;
318.                 /* for inside_engulfer, only <u.ux,u.uy> is affected */
319.                 else if (inside_engulfer)
320.                     continue;
321.                 idamres = idamnonres = 0;
322.                 if (type >= 0 && !u.uswallow)
323.                     (void) zap_over_floor((xchar) (i + x - 1),
324.                                           (xchar) (j + y - 1), type,
325.                                           &shopdamage, exploding_wand_typ);
326. 
327.                 mtmp = m_at(i + x - 1, j + y - 1);
328.                 if (!mtmp && i + x - 1 == u.ux && j + y - 1 == u.uy)
329.                     mtmp = u.usteed;
330.                 if (!mtmp)
331.                     continue;
332.                 if (do_hallu) {
333.                     /* replace "gas spore" with a different description
334.                        for each target (we can't distinguish personal names
335.                        like "Barney" here in order to suppress "the" below,
336.                        so avoid any which begins with a capital letter) */
337.                     do {
338.                         Sprintf(hallu_buf, "%s explosion",
339.                                 s_suffix(rndmonnam((char *) 0)));
340.                     } while (*hallu_buf != lowc(*hallu_buf));
341.                     str = hallu_buf;
342.                 }
343.                 if (u.uswallow && mtmp == u.ustuck) {
344.                     const char *adj = (char *) 0;
345. 
346.                     if (is_animal(u.ustuck->data)) {
347.                         switch (adtyp) {
348.                         case AD_FIRE:
349.                             adj = "heartburn";
350.                             break;
351.                         case AD_COLD:
352.                             adj = "chilly";
353.                             break;
354.                         case AD_DISN:
355.                             if (olet == WAND_CLASS)
356.                                 adj = "irradiated by pure energy";
357.                             else
358.                                 adj = "perforated";
359.                             break;
360.                         case AD_ELEC:
361.                             adj = "shocked";
362.                             break;
363.                         case AD_DRST:
364.                             adj = "poisoned";
365.                             break;
366.                         case AD_ACID:
367.                             adj = "an upset stomach";
368.                             break;
369.                         default:
370.                             adj = "fried";
371.                             break;
372.                         }
373.                         pline("%s gets %s!", Monnam(u.ustuck), adj);
374.                     } else {
375.                         switch (adtyp) {
376.                         case AD_FIRE:
377.                             adj = "toasted";
378.                             break;
379.                         case AD_COLD:
380.                             adj = "chilly";
381.                             break;
382.                         case AD_DISN:
383.                             if (olet == WAND_CLASS)
384.                                 adj = "overwhelmed by pure energy";
385.                             else
386.                                 adj = "perforated";
387.                             break;
388.                         case AD_ELEC:
389.                             adj = "shocked";
390.                             break;
391.                         case AD_DRST:
392.                             adj = "intoxicated";
393.                             break;
394.                         case AD_ACID:
395.                             adj = "burned";
396.                             break;
397.                         default:
398.                             adj = "fried";
399.                             break;
400.                         }
401.                         pline("%s gets slightly %s!", Monnam(u.ustuck), adj);
402.                     }
403.                 } else if (cansee(i + x - 1, j + y - 1)) {
404.                     if (mtmp->m_ap_type)
405.                         seemimic(mtmp);
406.                     pline("%s is caught in the %s!", Monnam(mtmp), str);
407.                 }
408. 
409.                 idamres += destroy_mitem(mtmp, SCROLL_CLASS, (int) adtyp);
410.                 idamres += destroy_mitem(mtmp, SPBOOK_CLASS, (int) adtyp);
411.                 idamnonres += destroy_mitem(mtmp, POTION_CLASS, (int) adtyp);
412.                 idamnonres += destroy_mitem(mtmp, WAND_CLASS, (int) adtyp);
413.                 idamnonres += destroy_mitem(mtmp, RING_CLASS, (int) adtyp);
414. 
415.                 if (explmask[i][j] == 1) {
416.                     golemeffects(mtmp, (int) adtyp, dam + idamres);
417.                     mtmp->mhp -= idamnonres;
418.                 } else {
419.                     /* call resist with 0 and do damage manually so 1) we can
420.                      * get out the message before doing the damage, and 2) we
421.                      * can call mondied, not killed, if it's not your blast
422.                      */
423.                     int mdam = dam;
424. 
425.                     if (resist(mtmp, olet, 0, FALSE)) {
426.                         /* inside_engulfer: <i+x-1,j+y-1> == <u.ux,u.uy> */
427.                         if (cansee(i + x - 1, j + y - 1) || inside_engulfer)
428.                             pline("%s resists the %s!", Monnam(mtmp), str);
429.                         mdam = (dam + 1) / 2;
430.                     }
431.                     /* if grabber is reaching into hero's spot and
432.                        hero's spot is within explosion radius, grabber
433.                        gets hit by double damage */
434.                     if (grabbed && mtmp == u.ustuck && distu(x, y) <= 2)
435.                         mdam *= 2;
436.                     /* being resistant to opposite type of damage makes
437.                        target more vulnerable to current type of damage
438.                        (when target is also resistant to current type,
439.                        we won't get here) */
440.                     if (resists_cold(mtmp) && adtyp == AD_FIRE)
441.                         mdam *= 2;
442.                     else if (resists_fire(mtmp) && adtyp == AD_COLD)
443.                         mdam *= 2;
444.                     mtmp->mhp -= mdam;
445.                     mtmp->mhp -= (idamres + idamnonres);
446.                 }
447.                 if (DEADMONSTER(mtmp)) {
448.                     int xkflg = ((adtyp == AD_FIRE
449.                                   && completelyburns(mtmp->data))
450.                                  ? XKILL_NOCORPSE : 0);
451. 
452.                     if (!context.mon_moving) {
453.                         xkilled(mtmp, XKILL_GIVEMSG | xkflg);
454.                     } else if (mdef && mtmp == mdef) {
455.                         /* 'mdef' killed self trying to cure being turned
456.                          * into slime due to some action by the player.
457.                          * Hero gets the credit (experience) and most of
458.                          * the blame (possible loss of alignment and/or
459.                          * luck and/or telepathy depending on mtmp) but
460.                          * doesn't break pacifism.  xkilled()'s message
461.                          * would be "you killed <mdef>" so give our own.
462.                          */
463.                         if (cansee(mtmp->mx, mtmp->my) || canspotmon(mtmp))
464.                             pline("%s is %s!", Monnam(mtmp),
465.                                   xkflg ? "burned completely"
466.                                         : nonliving(mtmp->data) ? "destroyed"
467.                                                                 : "killed");
468.                         xkilled(mtmp, XKILL_NOMSG | XKILL_NOCONDUCT | xkflg);
469.                     } else {
470.                         if (xkflg)
471.                             adtyp = AD_RBRE; /* no corpse */
472.                         monkilled(mtmp, "", (int) adtyp);
473.                     }
474.                 } else if (!context.mon_moving) {
475.                     /* all affected monsters, even if mdef is set */
476.                     setmangry(mtmp, TRUE);
477.                 }
478.             }
479. 
480.     /* Do your injury last */
481.     if (uhurt) {
482.         /* give message for any monster-induced explosion
483.            or player-induced one other than scroll of fire */
484.         if (flags.verbose && (type < 0 || olet != SCROLL_CLASS)) {
485.             if (do_hallu) { /* (see explanation above) */
486.                 do {
487.                     Sprintf(hallu_buf, "%s explosion",
488.                             s_suffix(rndmonnam((char *) 0)));
489.                 } while (*hallu_buf != lowc(*hallu_buf));
490.                 str = hallu_buf;
491.             }
492.             You("are caught in the %s!", str);
493.             iflags.last_msg = PLNMSG_CAUGHT_IN_EXPLOSION;
494.         }
495.         /* do property damage first, in case we end up leaving bones */
496.         if (adtyp == AD_FIRE)
497.             burn_away_slime();
498.         if (Invulnerable) {
499.             damu = 0;
500.             You("are unharmed!");
501.         } else if (adtyp == AD_PHYS || physical_dmg)
502.             damu = Maybe_Half_Phys(damu);
503.         if (adtyp == AD_FIRE)
504.             (void) burnarmor(&youmonst);
505.         destroy_item(SCROLL_CLASS, (int) adtyp);
506.         destroy_item(SPBOOK_CLASS, (int) adtyp);
507.         destroy_item(POTION_CLASS, (int) adtyp);
508.         destroy_item(RING_CLASS, (int) adtyp);
509.         destroy_item(WAND_CLASS, (int) adtyp);
510. 
511.         ugolemeffects((int) adtyp, damu);
512.         if (uhurt == 2) {
513.             /* if poly'd hero is grabbing another victim, hero takes
514.                double damage (note: don't rely on u.ustuck here because
515.                that victim might have been killed when hit by the blast) */
516.             if (grabbing && dist2((int) grabxy.x, (int) grabxy.y, x, y) <= 2)
517.                 damu *= 2;
518.             /* hero does not get same fire-resistant vs cold and
519.                cold-resistant vs fire double damage as monsters [why not?] */
520.             if (Upolyd)
521.                 u.mh -= damu;
522.             else
523.                 u.uhp -= damu;
524.             context.botl = 1;
525.         }
526. 
527.         if (u.uhp <= 0 || (Upolyd && u.mh <= 0)) {
528.             if (Upolyd) {
529.                 rehumanize();
530.             } else {
531.                 if (olet == MON_EXPLODE) {
532.                     if (generic) /* explosion was unseen; str=="explosion", */
533.                         ;        /* killer.name=="gas spore's explosion"    */
534.                     else if (str != killer.name && str != hallu_buf)
535.                         Strcpy(killer.name, str);
536.                     killer.format = KILLED_BY_AN;
537.                 } else if (type >= 0 && olet != SCROLL_CLASS) {
538.                     killer.format = NO_KILLER_PREFIX;
539.                     Sprintf(killer.name, "caught %sself in %s own %s", uhim(),
540.                             uhis(), str);
541.                 } else {
542.                     killer.format = (!strcmpi(str, "tower of flame")
543.                                      || !strcmpi(str, "fireball"))
544.                                         ? KILLED_BY_AN
545.                                         : KILLED_BY;
546.                     Strcpy(killer.name, str);
547.                 }
548.                 if (iflags.last_msg == PLNMSG_CAUGHT_IN_EXPLOSION
549.                     || iflags.last_msg == PLNMSG_TOWER_OF_FLAME) /*seffects()*/
550.                     pline("It is fatal.");
551.                 else
552.                     pline_The("%s is fatal.", str);
553.                 /* Known BUG: BURNING suppresses corpse in bones data,
554.                    but done does not handle killer reason correctly */
555.                 done((adtyp == AD_FIRE) ? BURNING : DIED);
556.             }
557.         }
558.         exercise(A_STR, FALSE);
559.     }
560. 
561.     if (shopdamage) {
562.         pay_for_damage((adtyp == AD_FIRE) ? "burn away"
563.                           : (adtyp == AD_COLD) ? "shatter"
564.                              : (adtyp == AD_DISN) ? "disintegrate"
565.                                 : "destroy",
566.                        FALSE);
567.     }
568. 
569.     /* explosions are noisy */
570.     i = dam * dam;
571.     if (i < 50)
572.         i = 50; /* in case random damage is very small */
573.     if (inside_engulfer)
574.         i = (i + 3) / 4;
575.     wake_nearto(x, y, i);
576. }
577. 
578. struct scatter_chain {
579.     struct scatter_chain *next; /* pointer to next scatter item */
580.     struct obj *obj;            /* pointer to the object        */
581.     xchar ox;                   /* location of                  */
582.     xchar oy;                   /*      item                    */
583.     schar dx;                   /* direction of                 */
584.     schar dy;                   /*      travel                  */
585.     int range;                  /* range of object              */
586.     boolean stopped;            /* flag for in-motion/stopped   */
587. };
588. 
589. /*
590.  * scflags:
591.  *      VIS_EFFECTS     Add visual effects to display
592.  *      MAY_HITMON      Objects may hit monsters
593.  *      MAY_HITYOU      Objects may hit hero
594.  *      MAY_HIT         Objects may hit you or monsters
595.  *      MAY_DESTROY     Objects may be destroyed at random
596.  *      MAY_FRACTURE    Stone objects can be fractured (statues, boulders)
597.  */
598. 
599. /* returns number of scattered objects */
600. long
601. scatter(sx, sy, blastforce, scflags, obj)
602. int sx, sy;     /* location of objects to scatter */
603. int blastforce; /* force behind the scattering */
604. unsigned int scflags;
605. struct obj *obj; /* only scatter this obj        */
606. {
607.     register struct obj *otmp;
608.     register int tmp;
609.     int farthest = 0;
610.     uchar typ;
611.     long qtmp;
612.     boolean used_up;
613.     boolean individual_object = obj ? TRUE : FALSE;
614.     struct monst *mtmp;
615.     struct scatter_chain *stmp, *stmp2 = 0;
616.     struct scatter_chain *schain = (struct scatter_chain *) 0;
617.     long total = 0L;
618. 
619.     if (individual_object && (obj->ox != sx || obj->oy != sy))
620.         impossible("scattered object <%d,%d> not at scatter site <%d,%d>",
621.                    obj->ox, obj->oy, sx, sy);
622. 
623.     while ((otmp = (individual_object ? obj : level.objects[sx][sy])) != 0) {
624.         if (otmp == uball || otmp == uchain) {
625.             boolean waschain = (otmp == uchain);
626.             pline_The("chain shatters!");
627.             unpunish();
628.             if (waschain)
629.                 continue;
630.         }
631.         if (otmp->quan > 1L) {
632.             qtmp = otmp->quan - 1L;
633.             if (qtmp > LARGEST_INT)
634.                 qtmp = LARGEST_INT;
635.             qtmp = (long) rnd((int) qtmp);
636.             otmp = splitobj(otmp, qtmp);
637.         } else {
638.             obj = (struct obj *) 0; /* all used */
639.         }
640.         obj_extract_self(otmp);
641.         used_up = FALSE;
642. 
643.         /* 9 in 10 chance of fracturing boulders or statues */
644.         if ((scflags & MAY_FRACTURE) != 0
645.             && (otmp->otyp == BOULDER || otmp->otyp == STATUE)
646.             && rn2(10)) {
647.             if (otmp->otyp == BOULDER) {
648.                 if (cansee(sx, sy))
649.                     pline("%s apart.", Tobjnam(otmp, "break"));
650.                 else
651.                     You_hear("stone breaking.");
652.                 fracture_rock(otmp);
653.                 place_object(otmp, sx, sy);
654.                 if ((otmp = sobj_at(BOULDER, sx, sy)) != 0) {
655.                     /* another boulder here, restack it to the top */
656.                     obj_extract_self(otmp);
657.                     place_object(otmp, sx, sy);
658.                 }
659.             } else {
660.                 struct trap *trap;
661. 
662.                 if ((trap = t_at(sx, sy)) && trap->ttyp == STATUE_TRAP)
663.                     deltrap(trap);
664.                 if (cansee(sx, sy))
665.                     pline("%s.", Tobjnam(otmp, "crumble"));
666.                 else
667.                     You_hear("stone crumbling.");
668.                 (void) break_statue(otmp);
669.                 place_object(otmp, sx, sy); /* put fragments on floor */
670.             }
671.             newsym(sx, sy); /* in case it's beyond radius of 'farthest' */
672.             used_up = TRUE;
673. 
674.             /* 1 in 10 chance of destruction of obj; glass, egg destruction */
675.         } else if ((scflags & MAY_DESTROY) != 0
676.                    && (!rn2(10) || (objects[otmp->otyp].oc_material == GLASS
677.                                     || otmp->otyp == EGG))) {
678.             if (breaks(otmp, (xchar) sx, (xchar) sy))
679.                 used_up = TRUE;
680.         }
681. 
682.         if (!used_up) {
683.             stmp = (struct scatter_chain *) alloc(sizeof *stmp);
684.             stmp->next = (struct scatter_chain *) 0;
685.             stmp->obj = otmp;
686.             stmp->ox = sx;
687.             stmp->oy = sy;
688.             tmp = rn2(8); /* get the direction */
689.             stmp->dx = xdir[tmp];
690.             stmp->dy = ydir[tmp];
691.             tmp = blastforce - (otmp->owt / 40);
692.             if (tmp < 1)
693.                 tmp = 1;
694.             stmp->range = rnd(tmp); /* anywhere up to that determ. by wt */
695.             if (farthest < stmp->range)
696.                 farthest = stmp->range;
697.             stmp->stopped = FALSE;
698.             if (!schain)
699.                 schain = stmp;
700.             else
701.                 stmp2->next = stmp;
702.             stmp2 = stmp;
703.         }
704.     }
705. 
706.     while (farthest-- > 0) {
707.         for (stmp = schain; stmp; stmp = stmp->next) {
708.             if ((stmp->range-- > 0) && (!stmp->stopped)) {
709.                 bhitpos.x = stmp->ox + stmp->dx;
710.                 bhitpos.y = stmp->oy + stmp->dy;
711.                 typ = levl[bhitpos.x][bhitpos.y].typ;
712.                 if (!isok(bhitpos.x, bhitpos.y)) {
713.                     bhitpos.x -= stmp->dx;
714.                     bhitpos.y -= stmp->dy;
715.                     stmp->stopped = TRUE;
716.                 } else if (!ZAP_POS(typ)
717.                            || closed_door(bhitpos.x, bhitpos.y)) {
718.                     bhitpos.x -= stmp->dx;
719.                     bhitpos.y -= stmp->dy;
720.                     stmp->stopped = TRUE;
721.                 } else if ((mtmp = m_at(bhitpos.x, bhitpos.y)) != 0) {
722.                     if (scflags & MAY_HITMON) {
723.                         stmp->range--;
724.                         if (ohitmon(mtmp, stmp->obj, 1, FALSE)) {
725.                             stmp->obj = (struct obj *) 0;
726.                             stmp->stopped = TRUE;
727.                         }
728.                     }
729.                 } else if (bhitpos.x == u.ux && bhitpos.y == u.uy) {
730.                     if (scflags & MAY_HITYOU) {
731.                         int hitvalu, hitu;
732. 
733.                         if (multi)
734.                             nomul(0);
735.                         hitvalu = 8 + stmp->obj->spe;
736.                         if (bigmonst(youmonst.data))
737.                             hitvalu++;
738.                         hitu = thitu(hitvalu, dmgval(stmp->obj, &youmonst),
739.                                      &stmp->obj, (char *) 0);
740.                         if (!stmp->obj)
741.                             stmp->stopped = TRUE;
742.                         if (hitu) {
743.                             stmp->range -= 3;
744.                             stop_occupation();
745.                         }
746.                     }
747.                 } else {
748.                     if (scflags & VIS_EFFECTS) {
749.                         /* tmp_at(bhitpos.x, bhitpos.y); */
750.                         /* delay_output(); */
751.                     }
752.                 }
753.                 stmp->ox = bhitpos.x;
754.                 stmp->oy = bhitpos.y;
755.             }
756.         }
757.     }
758.     for (stmp = schain; stmp; stmp = stmp2) {
759.         int x, y;
760. 
761.         stmp2 = stmp->next;
762.         x = stmp->ox;
763.         y = stmp->oy;
764.         if (stmp->obj) {
765.             if (x != sx || y != sy)
766.                 total += stmp->obj->quan;
767.             place_object(stmp->obj, x, y);
768.             stackobj(stmp->obj);
769.         }
770.         free((genericptr_t) stmp);
771.         newsym(x, y);
772.     }
773.     newsym(sx, sy);
774.     if (sx == u.ux && sy == u.uy && u.uundetected
775.         && hides_under(youmonst.data))
776.         (void) hideunder(&youmonst);
777.     return total;
778. }
779. 
780. /*
781.  * Splatter burning oil from x,y to the surrounding area.
782.  *
783.  * This routine should really take a how and direction parameters.
784.  * The how is how it was caused, e.g. kicked verses thrown.  The
785.  * direction is which way to spread the flaming oil.  Different
786.  * "how"s would give different dispersal patterns.  For example,
787.  * kicking a burning flask will splatter differently from a thrown
788.  * flask hitting the ground.
789.  *
790.  * For now, just perform a "regular" explosion.
791.  */
792. void
793. splatter_burning_oil(x, y, diluted_oil)
794. int x, y;
795. boolean diluted_oil;
796. {
797.     int dmg = d(diluted_oil ? 3 : 4, 4);
798. 
799. /* ZT_SPELL(ZT_FIRE) = ZT_SPELL(AD_FIRE-1) = 10+(2-1) = 11 */
800. #define ZT_SPELL_O_FIRE 11 /* value kludge, see zap.c */
801.     explode(x, y, ZT_SPELL_O_FIRE, dmg, BURNING_OIL, EXPL_FIERY);
802. }
803. 
804. /* lit potion of oil is exploding; extinguish it as a light source before
805.    possibly killing the hero and attempting to save bones */
806. void
807. explode_oil(obj, x, y)
808. struct obj *obj;
809. int x, y;
810. {
811.     boolean diluted_oil = obj->odiluted;
812. 
813.     if (!obj->lamplit)
814.         impossible("exploding unlit oil");
815.     end_burn(obj, TRUE);
816.     splatter_burning_oil(x, y, diluted_oil);
817. }
818. 
819. /*explode.c*/