Source:NetHack 5.0.0/src/mcastu.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to mcastu.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/mcastu.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 5.0	mcastu.c	$NHDT-Date: 1770949988 2026/02/12 18:33:08 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.111 $ */
2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3. /*-Copyright (c) Robert Patrick Rankin, 2011. */
4. /* NetHack may be freely redistributed.  See license for details. */
5. 
6. #include "hack.h"
7. 
8. #define MCASTU_ENUM
9. enum mcast_spells {
10.     #include "mcastu.h"
11. };
12. #undef MCASTU_ENUM
13. 
14. struct _mcast_data {
15.     int level;
16.     int flags;
17. };
18. 
19. #define MCASTU_INIT
20. static struct _mcast_data mcast_data[] = {
21.     #include "mcastu.h"
22. };
23. #undef MCASTU_INIT
24. 
25. /* spell lists for specific monster casters */
26. /* the spells in the list should be in ascending level order */
27. static int mon_cleric_spells[] = {
28.     MCAST_OPEN_WOUNDS, MCAST_CURE_SELF, MCAST_CONFUSE_YOU, MCAST_PARALYZE,
29.     MCAST_BLIND_YOU, MCAST_INSECTS, MCAST_CURSE_ITEMS, MCAST_LIGHTNING,
30.     MCAST_FIRE_PILLAR, MCAST_GEYSER
31. };
32. static int mon_wizard_spells[] = {
33.     MCAST_PSI_BOLT, MCAST_CURE_SELF, MCAST_HASTE_SELF, MCAST_STUN_YOU,
34.     MCAST_DISAPPEAR, MCAST_WEAKEN_YOU, MCAST_DESTRY_ARMR, MCAST_CURSE_ITEMS,
35.     MCAST_AGGRAVATION, MCAST_SUMMON_MONS, MCAST_CLONE_WIZ, MCAST_DEATH_TOUCH
36. };
37. 
38. staticfn void cursetxt(struct monst *, boolean);
39. staticfn int choose_monster_spell(struct monst *, int);
40. staticfn int m_cure_self(struct monst *, int);
41. staticfn void mcast_death_touch(struct monst *);
42. staticfn void mcast_clone_wiz(struct monst *);
43. staticfn void mcast_summon_mons(struct monst *);
44. staticfn void mcast_destroy_armor(void);
45. staticfn void mcast_weaken_you(struct monst *, int);
46. staticfn void mcast_disappear(struct monst *);
47. staticfn void mcast_stun_you(int);
48. staticfn int mcast_geyser(int);
49. staticfn int mcast_fire_pillar(struct monst *, int);
50. staticfn int mcast_lightning(struct monst *, int);
51. staticfn int mcast_psi_bolt(int);
52. staticfn int mcast_open_wounds(int);
53. staticfn void mcast_insects(struct monst *);
54. staticfn void mcast_blind_you(void);
55. staticfn int mcast_paralyze(struct monst *);
56. staticfn void mcast_confuse_you(struct monst *);
57. staticfn void mcast_spell(struct monst *, int, int);
58. staticfn boolean is_undirected_spell(int);
59. staticfn boolean spell_would_be_useless(struct monst *, int);
60. 
61. /* feedback when frustrated monster couldn't cast a spell */
62. staticfn void
63. cursetxt(struct monst *mtmp, boolean undirected)
64. {
65.     if (canseemon(mtmp) && couldsee(mtmp->mx, mtmp->my)) {
66.         const char *point_msg; /* spellcasting monsters are impolite */
67. 
68.         if (undirected)
69.             point_msg = "all around, then curses";
70.         else if ((Invis && !perceives(mtmp->data)
71.                   && (mtmp->mux != u.ux || mtmp->muy != u.uy))
72.                  || is_obj_mappear(&gy.youmonst, STRANGE_OBJECT)
73.                  || u.uundetected)
74.             point_msg = "and curses in your general direction";
75.         else if (Displaced && (mtmp->mux != u.ux || mtmp->muy != u.uy))
76.             point_msg = "and curses at your displaced image";
77.         else
78.             point_msg = "at you, then curses";
79. 
80.         pline_mon(mtmp, "%s points %s.", Monnam(mtmp), point_msg);
81.     } else if ((!(svm.moves % 4) || !rn2(4))) {
82.         if (!Deaf)
83.             Norep("You hear a mumbled curse.");   /* Deaf-aware */
84.     }
85. }
86. 
87. /* choose a spell for monster to cast */
88. staticfn int
89. choose_monster_spell(struct monst *mtmp, int adtyp)
90. {
91.     int *list = NULL;
92.     int i, spellval, len = 0;
93.     int maxlev;
94. 
95.     /* which spell list to use? */
96.     if (adtyp == AD_SPEL) {
97.         list = mon_wizard_spells;
98.         len = SIZE(mon_wizard_spells);
99.     } else if (adtyp == AD_CLRC) {
100.         list = mon_cleric_spells;
101.         len = SIZE(mon_cleric_spells);
102.     }
103. 
104.     if (!list || len < 1)
105.         return MCAST_PSI_BOLT;
106. 
107.     /* max spell level in this monster spell list */
108.     maxlev = mcast_data[list[len - 1]].level;
109. 
110.     /* which level spell to cast? */
111.     spellval = rn2(mtmp->m_lev);
112.     if (spellval > maxlev && rn2(maxlev))
113.         spellval = rn2(maxlev);
114. 
115.     /* find the highest spell in the list we could cast */
116.     for (i = len-1; i >= 0; i--)
117.         if (mcast_data[list[i]].level <= spellval
118.             && !spell_would_be_useless(mtmp, list[i]))
119.             return list[i];
120. 
121.     /* or return the first spell in the list */
122.     return list[0];
123. }
124. 
125. /* return values:
126.  * 1: successful spell
127.  * 0: unsuccessful spell
128.  */
129. int
130. castmu(
131.     struct monst *mtmp,   /* caster */
132.     struct attack *mattk, /* caster's current attack */
133.     boolean thinks_it_foundyou,    /* might be mistaken if displaced */
134.     boolean foundyou)              /* knows hero's precise location */
135. {
136.     int dmg, ml = mtmp->m_lev;
137.     int ret;
138.     int spellnum = 0;
139. 
140.     /* Three cases:
141.      * -- monster is attacking you.  Search for a useful spell.
142.      * -- monster thinks it's attacking you.  Search for a useful spell,
143.      *    without checking for undirected.  If the spell found is directed,
144.      *    it fails with cursetxt() and loss of mspec_used.
145.      * -- monster isn't trying to attack.  Select a spell once.  Don't keep
146.      *    searching; if that spell is not useful (or if it's directed),
147.      *    return and do something else.
148.      * Since most spells are directed, this means that a monster that isn't
149.      * attacking casts spells only a small portion of the time that an
150.      * attacking monster does.
151.      */
152.     if ((mattk->adtyp == AD_SPEL || mattk->adtyp == AD_CLRC) && ml) {
153.         int cnt = 40;
154. 
155.         do {
156.             spellnum = choose_monster_spell(mtmp, mattk->adtyp);
157.             /* not trying to attack?  don't allow directed spells */
158.             if (!thinks_it_foundyou) {
159.                 if (!is_undirected_spell(spellnum)
160.                     || spell_would_be_useless(mtmp, spellnum)) {
161.                     if (foundyou)
162.                         impossible(
163.                        "spellcasting monster found you and doesn't know it?");
164.                     return M_ATTK_MISS;
165.                 }
166.                 break;
167.             }
168.         } while (--cnt > 0
169.                  && spell_would_be_useless(mtmp, spellnum));
170.         if (cnt == 0)
171.             return M_ATTK_MISS;
172.     }
173. 
174.     /* monster unable to cast spells? */
175.     if (mtmp->mcan || mtmp->mspec_used || !ml
176.         || m_seenres(mtmp, cvt_adtyp_to_mseenres(mattk->adtyp))) {
177.         cursetxt(mtmp, is_undirected_spell(spellnum));
178.         return M_ATTK_MISS;
179.     }
180. 
181.     debugpline3("castmu:%s,lvl:%i,spell:%i", noit_Monnam(mtmp), ml, spellnum);
182. 
183.     if (mattk->adtyp == AD_SPEL || mattk->adtyp == AD_CLRC) {
184.         /* monst->m_lev is unsigned (uchar), monst->mspec_used is int */
185.         mtmp->mspec_used = (int) ((mtmp->m_lev < 8) ? (10 - mtmp->m_lev) : 2);
186.     }
187. 
188.     /* Monster can cast spells, but is casting a directed spell at the
189.      * wrong place?  If so, give a message, and return.
190.      * Do this *after* penalizing mspec_used.
191.      *
192.      * FIXME?
193.      *  Shouldn't wall of lava have a case similar to wall of water?
194.      *  And should cold damage hit water or lava instead of missing
195.      *  even when the caster has targeted the wrong spot?  Likewise
196.      *  for fire mis-aimed at ice.
197.      */
198.     if (!foundyou && thinks_it_foundyou
199.         && !is_undirected_spell(spellnum)) {
200.         pline_mon(mtmp, "%s casts a spell at %s!",
201.                  canseemon(mtmp) ? Monnam(mtmp) : "Something",
202.                  is_waterwall(mtmp->mux, mtmp->muy) ? "empty water"
203.                                                     : "thin air");
204.         return M_ATTK_MISS;
205.     }
206. 
207.     nomul(0);
208.     if (rn2(ml * 10) < (mtmp->mconf ? 100 : 20)) { /* fumbled attack */
209.         Soundeffect(se_air_crackles, 60);
210.         if (canseemon(mtmp) && !Deaf) {
211.             set_msg_xy(mtmp->mx, mtmp->my);
212.             pline_The("air crackles around %s.", mon_nam(mtmp));
213.         }
214.         return M_ATTK_MISS;
215.     }
216.     if (canspotmon(mtmp) || !is_undirected_spell(spellnum)) {
217.         pline_mon(mtmp, "%s casts a spell%s!",
218.                  canspotmon(mtmp) ? Monnam(mtmp) : "Something",
219.                  is_undirected_spell(spellnum) ? ""
220.                  : (Invis && !perceives(mtmp->data)
221.                     && !u_at(mtmp->mux, mtmp->muy))
222.                    ? " at a spot near you"
223.                    : (Displaced && !u_at(mtmp->mux, mtmp->muy))
224.                      ? " at your displaced image"
225.                      : " at you");
226.     }
227. 
228.     /*
229.      * As these are spells, the damage is related to the level
230.      * of the monster casting the spell.
231.      */
232.     if (!foundyou) {
233.         dmg = 0;
234.         if (mattk->adtyp != AD_SPEL && mattk->adtyp != AD_CLRC) {
235.             impossible(
236.               "%s casting non-hand-to-hand version of hand-to-hand spell %d?",
237.                        Monnam(mtmp), mattk->adtyp);
238.             return M_ATTK_MISS;
239.         }
240.     } else if (mattk->damd)
241.         dmg = d((int) ((ml / 2) + mattk->damn), (int) mattk->damd);
242.     else
243.         dmg = d((int) ((ml / 2) + 1), 6);
244.     if (Half_spell_damage)
245.         dmg = (dmg + 1) / 2;
246. 
247.     ret = M_ATTK_HIT;
248.     /*
249.      * FIXME: none of these hit the steed when hero is riding, nor do
250.      *  they inflict damage on carried items.
251.      */
252.     switch (mattk->adtyp) {
253.     case AD_FIRE:
254.         pline("You're enveloped in flames.");
255.         if (Fire_resistance) {
256.             shieldeff(u.ux, u.uy);
257.             pline("But you resist the effects.");
258.             monstseesu(M_SEEN_FIRE);
259.             dmg = 0;
260.         } else {
261.             monstunseesu(M_SEEN_FIRE);
262.         }
263.         burn_away_slime();
264.         /* burn up flammable items on the floor, melt ice terrain */
265.         mon_spell_hits_spot(mtmp, AD_FIRE, u.ux, u.uy);
266.         break;
267.     case AD_COLD:
268.         pline("You're covered in frost.");
269.         if (Cold_resistance) {
270.             shieldeff(u.ux, u.uy);
271.             pline("But you resist the effects.");
272.             monstseesu(M_SEEN_COLD);
273.             dmg = 0;
274.         } else {
275.             monstunseesu(M_SEEN_COLD);
276.         }
277.         /* freeze water or lava terrain */
278.         /* FIXME: mon_spell_hits_spot() uses zap_over_floor(); unlike with
279.          * fire, it does not target susceptible floor items with cold */
280.         mon_spell_hits_spot(mtmp, AD_COLD, u.ux, u.uy);
281.         break;
282.     case AD_MAGM:
283.         You("are hit by a shower of missiles!");
284.         if (Antimagic) {
285.             shieldeff(u.ux, u.uy);
286.             pline_The("missiles bounce off!");
287.             monstseesu(M_SEEN_MAGR);
288.             dmg = 0;
289.         } else {
290.             dmg = d((int) mtmp->m_lev / 2 + 1, 6);
291.             monstunseesu(M_SEEN_MAGR);
292.         }
293.         /* shower of magic missiles scuffs an engraving */
294.         mon_spell_hits_spot(mtmp, AD_MAGM, u.ux, u.uy);
295.         break;
296.     case AD_SPEL: /* wizard spell */
297.     case AD_CLRC: /* clerical spell */
298.         mcast_spell(mtmp, dmg, spellnum);
299.         dmg = 0; /* done by the spell casting functions */
300.         break;
301.     } /* switch */
302.     if (dmg)
303.         mdamageu(mtmp, dmg);
304.     return ret;
305. }
306. 
307. staticfn int
308. m_cure_self(struct monst *mtmp, int dmg)
309. {
310.     if (mtmp->mhp < mtmp->mhpmax) {
311.         if (canseemon(mtmp))
312.             pline_mon(mtmp, "%s looks better.", Monnam(mtmp));
313.         /* note: player healing does 6d4; this used to do 1d8 */
314.         healmon(mtmp, d(3, 6), 0);
315.         dmg = 0;
316.     }
317.     return dmg;
318. }
319. 
320. /* unlike the finger of death spell which behaves like a wand of death,
321.    this monster spell only attacks the hero */
322. void
323. touch_of_death(struct monst *mtmp)
324. {
325.     char kbuf[BUFSZ];
326.     int dmg = 50 + d(8, 6);
327.     int drain = dmg / 2;
328. 
329.     /* if we get here, we know that hero isn't magic resistant and isn't
330.        poly'd into an undead or demon */
331.     You_feel("drained...");
332.     (void) death_inflicted_by(kbuf, "the touch of death", mtmp);
333. 
334.     if (Upolyd) {
335.         u.mh = 0;
336.         rehumanize(); /* fatal iff Unchanging */
337.     } else if (drain >= u.uhpmax) {
338.         svk.killer.format = KILLED_BY;
339.         Strcpy(svk.killer.name, kbuf);
340.         done(DIED);
341.     } else {
342.         /* HP manipulation similar to poisoned(attrib.c) */
343.         int olduhp = u.uhp,
344.             uhpmin = minuhpmax(3),
345.             newuhpmax = u.uhpmax - drain;
346. 
347.         setuhpmax(max(newuhpmax, uhpmin), FALSE);
348.         dmg = adjuhploss(dmg, olduhp); /* reduce pending damage if uhp has
349.                                         * already been reduced due to drop
350.                                         * in uhpmax */
351.         losehp(dmg, kbuf, KILLED_BY);
352.     }
353.     svk.killer.name[0] = '\0'; /* not killed if we get here... */
354. }
355. 
356. /* give a reason for death by some monster spells */
357. char *
358. death_inflicted_by(
359.     char *outbuf,            /* assumed big enough; pm_names are short */
360.     const char *deathreason, /* cause of death */
361.     struct monst *mtmp)      /* monster who caused it */
362. {
363.     Strcpy(outbuf, deathreason);
364.     if (mtmp) {
365.         struct permonst *mptr = mtmp->data,
366.             *champtr = (ismnum(mtmp->cham)) ? &mons[mtmp->cham] : mptr;
367.         const char *realnm = pmname(champtr, Mgender(mtmp)),
368.             *fakenm = pmname(mptr, Mgender(mtmp));
369. 
370.         /* greatly simplified extract from done_in_by(), primarily for
371.            reason for death due to 'touch of death' spell; if mtmp is
372.            shape changed, it won't be a vampshifter or mimic since they
373.            can't cast spells */
374.         if (!type_is_pname(champtr) && !the_unique_pm(mptr))
375.             realnm = an(realnm);
376.         Sprintf(eos(outbuf), " inflicted by %s%s",
377.                 the_unique_pm(mptr) ? "the " : "", realnm);
378.         if (champtr != mptr)
379.             Sprintf(eos(outbuf), " imitating %s", an(fakenm));
380.     }
381.     return outbuf;
382. }
383. 
384. /*
385.  * Monster wizard and cleric spellcasting functions.
386.  */
387. 
388. staticfn void
389. mcast_death_touch(struct monst *mtmp)
390. {
391.     pline("Oh no, %s's using the touch of death!", mhe(mtmp));
392.     if (nonliving(gy.youmonst.data) || is_demon(gy.youmonst.data)) {
393.         You("seem no deader than before.");
394.     } else if (!Antimagic && rn2(mtmp->m_lev) > 12) {
395.         if (Hallucination) {
396.             You("have an out of body experience.");
397.         } else {
398.             touch_of_death(mtmp);
399.         }
400.         monstunseesu(M_SEEN_MAGR);
401.     } else {
402.         if (Antimagic) {
403.             shieldeff(u.ux, u.uy);
404.             monstseesu(M_SEEN_MAGR);
405.         }
406.         pline("Lucky for you, it didn't work!");
407.     }
408. }
409. 
410. staticfn void
411. mcast_clone_wiz(struct monst *mtmp)
412. {
413.     if (mtmp->iswiz && svc.context.no_of_wizards == 1) {
414.         pline("Double Trouble...");
415.         clonewiz();
416.     } else
417.         impossible("bad wizard cloning?");
418. }
419. 
420. staticfn void
421. mcast_summon_mons(struct monst *mtmp)
422. {
423.     int count = nasty(mtmp);
424. 
425.     if (!count) {
426.         ; /* nothing was created? */
427.     } else if (mtmp->iswiz) {
428.         SetVoice(mtmp, 0, 80, 0);
429.         verbalize("Destroy the thief, my pet%s!", plur(count));
430.     } else {
431.         boolean one = (count == 1);
432.         const char *mappear = one ? "A monster appears"
433.                                   : "Monsters appear";
434. 
435.         /* messages not quite right if plural monsters created but
436.            only a single monster is seen */
437.         if (Invis && !perceives(mtmp->data)
438.             && (mtmp->mux != u.ux || mtmp->muy != u.uy))
439.             pline("%s %s a spot near you!", mappear,
440.                   one ? "at" : "around");
441.         else if (Displaced && (mtmp->mux != u.ux || mtmp->muy != u.uy))
442.             pline("%s %s your displaced image!", mappear,
443.                   one ? "by" : "around");
444.         else
445.             pline("%s from nowhere!", mappear);
446.     }
447. }
448. 
449. staticfn void
450. mcast_destroy_armor(void)
451. {
452.     if (Antimagic) {
453.         shieldeff(u.ux, u.uy);
454.         monstseesu(M_SEEN_MAGR);
455.         pline("A field of force surrounds you!");
456.     } else if (!destroy_arm()) {
457.         Your("skin itches.");
458.     } else {
459.         /* monsters only realize you aren't magic-protected if armor is
460.            actually destroyed */
461.         monstunseesu(M_SEEN_MAGR);
462.     }
463. }
464. 
465. staticfn void
466. mcast_weaken_you(struct monst *mtmp, int dmg)
467. {
468.     if (Antimagic) {
469.         shieldeff(u.ux, u.uy);
470.         monstseesu(M_SEEN_MAGR);
471.         You_feel("momentarily weakened.");
472.     } else {
473.         char kbuf[BUFSZ];
474. 
475.         You("suddenly feel weaker!");
476.         dmg = mtmp->m_lev - 6;
477.         if (dmg < 1) /* paranoia since only chosen when m_lev is high */
478.             dmg = 1;
479.         if (Half_spell_damage)
480.             dmg = (dmg + 1) / 2;
481.         losestr(rnd(dmg),
482.                 death_inflicted_by(kbuf, "strength loss", mtmp),
483.                 KILLED_BY);
484.         svk.killer.name[0] = '\0'; /* not killed if we get here... */
485.         monstunseesu(M_SEEN_MAGR);
486.     }
487. }
488. 
489. staticfn void
490. mcast_disappear(struct monst *mtmp)
491. {
492.     if (!mtmp->minvis && !mtmp->invis_blkd) {
493.         if (canseemon(mtmp))
494.             pline_mon(mtmp, "%s suddenly %s!", Monnam(mtmp),
495.                       !See_invisible ? "disappears" : "becomes transparent");
496.         mon_set_minvis(mtmp, FALSE);
497.         if (cansee(mtmp->mx, mtmp->my) && !canspotmon(mtmp))
498.             map_invisible(mtmp->mx, mtmp->my);
499.     } else
500.         impossible("no reason for monster to cast disappear spell?");
501. }
502. 
503. staticfn void
504. mcast_stun_you(int dmg)
505. {
506.     if (Antimagic || Free_action) {
507.         shieldeff(u.ux, u.uy);
508.         monstseesu(M_SEEN_MAGR);
509.         if (!Stunned)
510.             You_feel("momentarily disoriented.");
511.         make_stunned(1L, FALSE);
512.     } else {
513.         You(Stunned ? "struggle to keep your balance." : "reel...");
514.         dmg = d(ACURR(A_DEX) < 12 ? 6 : 4, 4);
515.         if (Half_spell_damage)
516.             dmg = (dmg + 1) / 2;
517.         make_stunned((HStun & TIMEOUT) + (long) dmg, FALSE);
518.         monstunseesu(M_SEEN_MAGR);
519.     }
520. }
521. 
522. staticfn int
523. mcast_geyser(int dmg)
524. {
525.     /* this is physical damage (force not heat),
526.      * not magical damage or fire damage
527.      */
528.     pline("A sudden geyser slams into you from nowhere!");
529.     dmg = d(8, 6);
530.     if (Half_physical_damage)
531.         dmg = (dmg + 1) / 2;
532. #if 0   /* since inventory items aren't affected, don't include this */
533.         /* make floor items wet */
534.     water_damage_chain(level.objects[u.ux][u.uy], TRUE);
535. #endif
536.     return dmg;
537. }
538. 
539. staticfn int
540. mcast_fire_pillar(struct monst *mtmp, int dmg)
541. {
542.     int orig_dmg;
543. 
544.     pline("A pillar of fire strikes all around you!");
545.     orig_dmg = dmg = d(8, 6);
546.     if (Fire_resistance) {
547.         shieldeff(u.ux, u.uy);
548.         monstseesu(M_SEEN_FIRE);
549.         dmg = 0;
550.     } else {
551.         monstunseesu(M_SEEN_FIRE);
552.     }
553.     if (Half_spell_damage)
554.         dmg = (dmg + 1) / 2;
555.     burn_away_slime();
556.     (void) burnarmor(&gy.youmonst);
557.     /* item destruction dmg */
558.     (void) destroy_items(&gy.youmonst, AD_FIRE, orig_dmg);
559.     ignite_items(gi.invent);
560.     /* burn up flammable items on the floor, melt ice terrain */
561.     mon_spell_hits_spot(mtmp, AD_FIRE, u.ux, u.uy);
562.     return dmg;
563. }
564. 
565. staticfn int
566. mcast_lightning(struct monst *mtmp, int dmg)
567. {
568.     int orig_dmg;
569.     boolean reflects;
570. 
571.     Soundeffect(se_bolt_of_lightning, 80);
572.     pline("A bolt of lightning strikes down at you from above!");
573.     reflects = ureflects("It bounces off your %s%s.", "");
574.     orig_dmg = dmg = d(8, 6);
575.     if (reflects || Shock_resistance) {
576.         shieldeff(u.ux, u.uy);
577.         dmg = 0;
578.         if (reflects) {
579.             monstseesu(M_SEEN_REFL);
580.             return dmg;
581.         }
582.         monstunseesu(M_SEEN_REFL);
583.         monstseesu(M_SEEN_ELEC);
584.     } else {
585.         monstunseesu(M_SEEN_ELEC | M_SEEN_REFL);
586.     }
587.     if (Half_spell_damage)
588.         dmg = (dmg + 1) / 2;
589.     (void) destroy_items(&gy.youmonst, AD_ELEC, orig_dmg);
590.     /* lightning might destroy iron bars if hero is on such a spot;
591.        reflection protects terrain here [execution won't get here due
592.        to 'if (reflects) break' above] but hero resistance doesn't;
593.        do this before maybe blinding the hero via flashburn() */
594.     mon_spell_hits_spot(mtmp, AD_ELEC, u.ux, u.uy);
595.     /* blind hero; no effect if already blind */
596.     (void) flashburn((long) rnd(100), TRUE);
597.     return dmg;
598. }
599. 
600. staticfn int
601. mcast_psi_bolt(int dmg)
602. {
603.     /* prior to 3.4.0 Antimagic was setting the damage to 1--this
604.        made the spell virtually harmless to players with magic res. */
605.     if (Antimagic) {
606.         shieldeff(u.ux, u.uy);
607.         monstseesu(M_SEEN_MAGR);
608.         dmg = (dmg + 1) / 2;
609.     } else {
610.         monstunseesu(M_SEEN_MAGR);
611.     }
612.     if (dmg <= 5)
613.         You("get a slight %sache.", body_part(HEAD));
614.     else if (dmg <= 10)
615.         Your("brain is on fire!");
616.     else if (dmg <= 20)
617.         Your("%s suddenly aches painfully!", body_part(HEAD));
618.     else
619.         Your("%s suddenly aches very painfully!", body_part(HEAD));
620.     return dmg;
621. }
622. 
623. staticfn int
624. mcast_open_wounds(int dmg)
625. {
626.     if (Antimagic) {
627.         shieldeff(u.ux, u.uy);
628.         monstseesu(M_SEEN_MAGR);
629.         dmg = (dmg + 1) / 2;
630.     } else {
631.         monstunseesu(M_SEEN_MAGR);
632.     }
633.     if (dmg <= 5)
634.         Your("skin itches badly for a moment.");
635.     else if (dmg <= 10)
636.         pline("Wounds appear on your body!");
637.     else if (dmg <= 20)
638.         pline("Severe wounds appear on your body!");
639.     else
640.         Your("body is covered with painful wounds!");
641.     return dmg;
642. }
643. 
644. staticfn void
645. mcast_insects(struct monst *mtmp)
646. {
647.     /* Try for insects, and if there are none
648.        left, go for (sticks to) snakes.  -3. */
649.     struct permonst *pm = mkclass(S_ANT, 0);
650.     struct monst *mtmp2 = (struct monst *) 0;
651.     char whatbuf[QBUFSZ], let = (pm ? S_ANT : S_SNAKE);
652.     boolean success = FALSE, seecaster;
653.     int i, quan, oldseen, newseen;
654.     coord bypos;
655.     const char *fmt, *what;
656. 
657.     oldseen = monster_census(TRUE);
658.     quan = (mtmp->m_lev < 2) ? 1 : rnd((int) mtmp->m_lev / 2);
659.     if (quan < 3)
660.         quan = 3;
661.     for (i = 0; i <= quan; i++) {
662.         if (!enexto(&bypos, mtmp->mux, mtmp->muy, mtmp->data))
663.             return;
664.         if ((pm = mkclass(let, 0)) != 0
665.             && (mtmp2 = makemon(pm, bypos.x, bypos.y, MM_ANGRY | MM_NOMSG))
666.             != 0) {
667.             success = TRUE;
668.             mtmp2->msleeping = mtmp2->mpeaceful = mtmp2->mtame = 0;
669.             set_malign(mtmp2);
670.         }
671.     }
672.     newseen = monster_census(TRUE);
673. 
674.     /* not canspotmon() which includes unseen things sensed via warning */
675.     seecaster = canseemon(mtmp) || tp_sensemon(mtmp) || Detect_monsters;
676.     what = (let == S_SNAKE) ? "snakes" : "insects";
677.     if (Hallucination)
678.         what = makeplural(bogusmon(whatbuf, (char *) 0));
679. 
680.     fmt = 0;
681.     if (!seecaster) {
682.         if (newseen <= oldseen || Unaware) {
683.             /* unseen caster fails or summons unseen critters,
684.                or unconscious hero ("You dream that you hear...") */
685.             You_hear("someone summoning %s.", what);
686.         } else {
687.             char *arg;
688. 
689.             if (what != whatbuf)
690.                 what = strcpy(whatbuf, what);
691.             /* unseen caster summoned seen critter(s) */
692.             arg = (newseen == oldseen + 1) ? an(makesingular(what))
693.                                            : whatbuf;
694.             if (!Deaf) {
695.                 Soundeffect(se_someone_summoning, 100);
696.                 You_hear("someone summoning something, and %s %s.", arg,
697.                          vtense(arg, "appear"));
698.             } else {
699.                 pline("%s %s.", upstart(arg), vtense(arg, "appear"));
700.             }
701.         }
702. 
703.         /* seen caster, possibly producing unseen--or just one--critters;
704.            hero is told what the caster is doing and doesn't necessarily
705.            observe complete accuracy of that caster's results (in other
706.            words, no need to fuss with visibility or singularization;
707.            player is told what's happening even if hero is unconscious) */
708.     } else if (!success) {
709.         fmt = "%s casts at a clump of sticks, but nothing happens.%s";
710.         what = "";
711.     } else if (let == S_SNAKE) {
712.         fmt = "%s transforms a clump of sticks into %s!";
713.     } else if (Invis && !perceives(mtmp->data)
714.                && (mtmp->mux != u.ux || mtmp->muy != u.uy)) {
715.         fmt = "%s summons %s around a spot near you!";
716.     } else if (Displaced && (mtmp->mux != u.ux || mtmp->muy != u.uy)) {
717.         fmt = "%s summons %s around your displaced image!";
718.     } else {
719.         fmt = "%s summons %s!";
720.     }
721.     if (fmt) {
722.         DISABLE_WARNING_FORMAT_NONLITERAL;
723.         pline_mon(mtmp, fmt, Monnam(mtmp), what);
724.         RESTORE_WARNING_FORMAT_NONLITERAL;
725.     }
726. }
727. 
728. staticfn void
729. mcast_blind_you(void)
730. {
731.     /* note: resists_blnd() doesn't apply here */
732.     if (!Blinded) {
733.         int num_eyes = eyecount(gy.youmonst.data);
734. 
735.         pline("Scales cover your %s!", (num_eyes == 1)
736.                                        ? body_part(EYE)
737.                                        : makeplural(body_part(EYE)));
738.         make_blinded(Half_spell_damage ? 100L : 200L, FALSE);
739.         if (!Blind)
740.             Your1(vision_clears);
741.     } else
742.         impossible("no reason for monster to cast blindness spell?");
743. }
744. 
745. staticfn int
746. mcast_paralyze(struct monst *mtmp)
747. {
748.     int dmg = 0;
749. 
750.     if (Antimagic || Free_action) {
751.         shieldeff(u.ux, u.uy);
752.         monstseesu(M_SEEN_MAGR);
753.         if (gm.multi >= 0)
754.             You("stiffen briefly.");
755.         dmg = 1; /* to produce nomul(-1), not actual damage */
756.     } else {
757.         if (gm.multi >= 0)
758.             You("are frozen in place!");
759.         dmg = 4 + (int) mtmp->m_lev;
760.         if (Half_spell_damage)
761.             dmg = (dmg + 1) / 2;
762.         monstunseesu(M_SEEN_MAGR);
763.     }
764.     nomul(-dmg);
765.     gm.multi_reason = "paralyzed by a monster";
766.     gn.nomovemsg = 0;
767.     return dmg;
768. }
769. 
770. staticfn void
771. mcast_confuse_you(struct monst *mtmp)
772. {
773.     if (Antimagic) {
774.         shieldeff(u.ux, u.uy);
775.         monstseesu(M_SEEN_MAGR);
776.         You_feel("momentarily dizzy.");
777.     } else {
778.         boolean oldprop = !!Confusion;
779.         int dmg = (int) mtmp->m_lev;
780. 
781.         if (Half_spell_damage)
782.             dmg = (dmg + 1) / 2;
783.         make_confused(HConfusion + dmg, TRUE);
784.         if (Hallucination)
785.             You_feel("%s!", oldprop ? "trippier" : "trippy");
786.         else
787.             You_feel("%sconfused!", oldprop ? "more " : "");
788.         monstunseesu(M_SEEN_MAGR);
789.     }
790. }
791. 
792. /*
793.    If dmg is zero, then the monster is not casting at you.
794.    If the monster is intentionally not casting at you, we have previously
795.    called spell_would_be_useless() and spellnum should always be a valid
796.    undirected spell.
797.    If you modify either of these, be sure to change is_undirected_spell()
798.    and spell_would_be_useless().
799.  */
800. staticfn void
801. mcast_spell(struct monst *mtmp, int dmg, int spellnum)
802. {
803.     if (dmg < 0) {
804.         impossible("monster cast spell (%d) with negative dmg (%d)?",
805.                    spellnum, dmg);
806.         return;
807.     }
808.     if (dmg == 0 && !is_undirected_spell(spellnum)) {
809.         impossible("cast directed wizard spell (%d) with dmg=0?", spellnum);
810.         return;
811.     }
812. 
813.     switch (spellnum) {
814.     case MCAST_DEATH_TOUCH:
815.         mcast_death_touch(mtmp);
816.         dmg = 0;
817.         break;
818.     case MCAST_CLONE_WIZ:
819.         mcast_clone_wiz(mtmp);
820.         dmg = 0;
821.         break;
822.     case MCAST_SUMMON_MONS:
823.         mcast_summon_mons(mtmp);
824.         dmg = 0;
825.         break;
826.     case MCAST_AGGRAVATION:
827.         You_feel("that monsters are aware of your presence.");
828.         aggravate();
829.         dmg = 0;
830.         break;
831.     case MCAST_CURSE_ITEMS:
832.         You_feel("as if you need some help.");
833.         rndcurse();
834.         dmg = 0;
835.         break;
836.     case MCAST_DESTRY_ARMR:
837.         mcast_destroy_armor();
838.         dmg = 0;
839.         break;
840.     case MCAST_WEAKEN_YOU: /* drain strength */
841.         mcast_weaken_you(mtmp, dmg);
842.         dmg = 0;
843.         break;
844.     case MCAST_DISAPPEAR: /* makes self invisible */
845.         mcast_disappear(mtmp);
846.         dmg = 0;
847.         break;
848.     case MCAST_STUN_YOU:
849.         mcast_stun_you(dmg);
850.         dmg = 0;
851.         break;
852.     case MCAST_HASTE_SELF:
853.         mon_adjust_speed(mtmp, 1, (struct obj *) 0);
854.         dmg = 0;
855.         break;
856.     case MCAST_CURE_SELF:
857.         dmg = m_cure_self(mtmp, dmg);
858.         break;
859.     case MCAST_PSI_BOLT:
860.         dmg = mcast_psi_bolt(dmg);
861.         break;
862.     case MCAST_GEYSER:
863.         dmg = mcast_geyser(dmg);
864.         break;
865.     case MCAST_FIRE_PILLAR:
866.         dmg = mcast_fire_pillar(mtmp, dmg);
867.         break;
868.     case MCAST_LIGHTNING:
869.         dmg = mcast_lightning(mtmp, dmg);
870.         break;
871.     case MCAST_INSECTS:
872.         mcast_insects(mtmp);
873.         dmg = 0;
874.         break;
875.     case MCAST_BLIND_YOU:
876.         mcast_blind_you();
877.         dmg = 0;
878.         break;
879.     case MCAST_PARALYZE:
880.         dmg = mcast_paralyze(mtmp);
881.         break;
882.     case MCAST_CONFUSE_YOU:
883.         mcast_confuse_you(mtmp);
884.         dmg = 0;
885.         break;
886.     case MCAST_OPEN_WOUNDS:
887.         dmg = mcast_open_wounds(dmg);
888.         break;
889.     default:
890.         impossible("mcastu: invalid magic spell (%d)", spellnum);
891.         dmg = 0;
892.         break;
893.     }
894. 
895.     if (dmg)
896.         mdamageu(mtmp, dmg);
897. }
898. 
899. staticfn boolean
900. is_undirected_spell(int spellnum)
901. {
902.     if ((mcast_data[spellnum].flags & MCF_INDIRECT) != 0)
903.         return TRUE;
904.     return FALSE;
905. }
906. 
907. /* Some spells are useless under some circumstances. */
908. staticfn boolean
909. spell_would_be_useless(struct monst *mtmp, int spellnum)
910. {
911.     /* Some spells don't require the player to really be there and can be cast
912.      * by the monster when you're invisible, yet still shouldn't be cast when
913.      * the monster doesn't even think you're there.
914.      * This check isn't quite right because it always uses your real position.
915.      * We really want something like "if the monster could see mux, muy".
916.      */
917. 
918.     /* spell is only cast by hostile monsters */
919.     if ((mcast_data[spellnum].flags & MCF_HOSTILE) != 0) {
920.         if (mtmp->mpeaceful)
921.             return TRUE;
922.     }
923. 
924.     /* spell needs the monster to see hero */
925.     if ((mcast_data[spellnum].flags & MCF_SIGHT) != 0) {
926.         boolean mcouldseeu = couldsee(mtmp->mx, mtmp->my);
927. 
928.         if (!mcouldseeu)
929.             return TRUE;
930.     }
931. 
932.     switch (spellnum) {
933.     case MCAST_DEATH_TOUCH:
934.         if ((Antimagic || Hallucination) && !rn2(2))
935.             return TRUE;
936.         break;
937.     case MCAST_GEYSER:
938.         if (!rn2(5))
939.             return TRUE;
940.         break;
941.     case MCAST_CLONE_WIZ:
942.         /* only the Wizard is allowed to clone himself */
943.         if (!mtmp->iswiz || svc.context.no_of_wizards > 1)
944.             return TRUE;
945.         break;
946.     case MCAST_AGGRAVATION:
947.         /* aggravation (global wakeup) when everyone is already active */
948.         /* if nothing needs to be awakened then this spell is useless
949.            but caster might not realize that [chance to pick it then
950.            must be very small otherwise caller's many retry attempts
951.            will eventually end up picking it too often] */
952.         if (!has_aggravatables(mtmp))
953.             return rn2(100) ? TRUE : FALSE;
954.         break;
955.     case MCAST_HASTE_SELF:
956.         /* haste self when already fast */
957.         if (mtmp->permspeed == MFAST)
958.             return TRUE;
959.         break;
960.     case MCAST_DISAPPEAR:
961.         /* invisibility when already invisible */
962.         if (mtmp->minvis || mtmp->invis_blkd)
963.             return TRUE;
964.         /* peaceful monster won't cast invisibility if you can't see
965.            invisible,
966.            same as when monsters drink potions of invisibility.  This doesn't
967.            really make a lot of sense, but lets the player avoid hitting
968.            peaceful monsters by mistake */
969.         if (mtmp->mpeaceful && !See_invisible)
970.             return TRUE;
971.         break;
972.     case MCAST_CURE_SELF:
973.         /* healing when already healed */
974.         if (mtmp->mhp == mtmp->mhpmax)
975.             return TRUE;
976.         break;
977.     case MCAST_BLIND_YOU:
978.         if (Blinded)
979.             return TRUE;
980.         break;
981.     default:
982.         break;
983.     }
984.     return FALSE;
985. }
986. 
987. /* monster uses spell (ranged) */
988. int
989. buzzmu(struct monst *mtmp, struct attack *mattk)
990. {
991.     /* don't print constant stream of curse messages for 'normal'
992.        spellcasting monsters at range */
993.     if (!BZ_VALID_ADTYP(mattk->adtyp))
994.         return M_ATTK_MISS;
995. 
996.     if (mtmp->mcan || m_seenres(mtmp, cvt_adtyp_to_mseenres(mattk->adtyp))) {
997.         cursetxt(mtmp, FALSE);
998.         return M_ATTK_MISS;
999.     }
1000.     if (lined_up(mtmp) && rn2(3)) {
1001.         nomul(0);
1002.         if (canseemon(mtmp))
1003.             pline_mon(mtmp, "%s zaps you with a %s!", Monnam(mtmp),
1004.                   flash_str(BZ_OFS_AD(mattk->adtyp), FALSE));
1005.         gb.buzzer = mtmp;
1006.         buzz(BZ_M_SPELL(BZ_OFS_AD(mattk->adtyp)), (int) mattk->damn,
1007.              mtmp->mx, mtmp->my, sgn(gt.tbx), sgn(gt.tby));
1008.         gb.buzzer = 0;
1009.         return M_ATTK_HIT;
1010.     }
1011.     return M_ATTK_MISS;
1012. }
1013. 
1014. /*mcastu.c*/