Source:NetHack 5.0.0/src/region.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to region.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/region.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	region.c	$NHDT-Date: 1727251269 2024/09/25 08:01:09 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.104 $ */
2. /* Copyright (c) 1996 by Jean-Christophe Collet  */
3. /* NetHack may be freely redistributed.  See license for details. */
4. 
5. #include "hack.h"
6. 
7. /*
8.  * This should really go into the level structure, but
9.  * I'll start here for ease. It *WILL* move into the level
10.  * structure eventually.
11.  */
12. 
13. #define NO_CALLBACK (-1)
14. 
15. void free_region(NhRegion *);
16. #ifndef SFCTOOL
17. boolean inside_gas_cloud(genericptr, genericptr);
18. boolean expire_gas_cloud(genericptr, genericptr);
19. boolean inside_rect(NhRect *, int, int);
20. NhRegion *create_region(NhRect *, int);
21. void add_rect_to_reg(NhRegion *, NhRect *);
22. void add_mon_to_reg(NhRegion *, struct monst *);
23. void remove_mon_from_reg(NhRegion *, struct monst *);
24. boolean mon_in_region(NhRegion *, struct monst *);
25. 
26. #if 0
27. NhRegion *clone_region(NhRegion *);
28. #endif
29. void add_region(NhRegion *);
30. void remove_region(NhRegion *);
31. 
32. #if 0
33. void replace_mon_regions(struct monst *,struct monst *);
34. void remove_mon_from_regions(struct monst *);
35. NhRegion *create_msg_region(coordxy,coordxy,coordxy,coordxy, const char *,
36.                            const char *);
37. boolean enter_force_field(genericptr,genericptr);
38. NhRegion *create_force_field(coordxy,coordxy,int,long);
39. #endif
40. 
41. staticfn void reset_region_mids(NhRegion *);
42. staticfn boolean is_hero_inside_gas_cloud(void);
43. staticfn void make_gas_cloud(NhRegion *, int, boolean) NONNULLARG1;
44. 
45. static const callback_proc callbacks[] = {
46. #define INSIDE_GAS_CLOUD 0
47.     inside_gas_cloud,
48. #define EXPIRE_GAS_CLOUD 1
49.     expire_gas_cloud
50. };
51. 
52. /* Should be inlined. */
53. boolean
54. inside_rect(NhRect *r, int x, int y)
55. {
56.     return (boolean) (x >= r->lx && x <= r->hx && y >= r->ly && y <= r->hy);
57. }
58. 
59. /*
60.  * Check if a point is inside a region.
61.  */
62. boolean
63. inside_region(NhRegion *reg, int x, int y)
64. {
65.     int i;
66. 
67.     if (reg == (NhRegion *) 0 || !inside_rect(&(reg->bounding_box), x, y))
68.         return FALSE;
69.     for (i = 0; i < reg->nrects; i++)
70.         if (inside_rect(&(reg->rects[i]), x, y))
71.             return TRUE;
72.     return FALSE;
73. }
74. 
75. /*
76.  * Create a region. It does not activate it.
77.  */
78. NhRegion *
79. create_region(NhRect *rects, int nrect)
80. {
81.     int i;
82.     NhRegion *reg;
83. 
84.     reg = (NhRegion *) alloc(sizeof(NhRegion));
85.     (void) memset((genericptr_t) reg, 0, sizeof(NhRegion));
86.     /* Determines bounding box */
87.     if (nrect > 0) {
88.         reg->bounding_box = rects[0];
89.     } else {
90.         reg->bounding_box.lx = COLNO;
91.         reg->bounding_box.ly = ROWNO;
92.         reg->bounding_box.hx = 0; /* 1 */
93.         reg->bounding_box.hy = 0;
94.     }
95.     reg->nrects = nrect;
96.     reg->rects = (nrect > 0) ? (NhRect *) alloc(nrect * sizeof (NhRect)) : 0;
97.     for (i = 0; i < nrect; i++) {
98.         if (rects[i].lx < reg->bounding_box.lx)
99.             reg->bounding_box.lx = rects[i].lx;
100.         if (rects[i].ly < reg->bounding_box.ly)
101.             reg->bounding_box.ly = rects[i].ly;
102.         if (rects[i].hx > reg->bounding_box.hx)
103.             reg->bounding_box.hx = rects[i].hx;
104.         if (rects[i].hy > reg->bounding_box.hy)
105.             reg->bounding_box.hy = rects[i].hy;
106.         reg->rects[i] = rects[i];
107.     }
108.     reg->ttl = -1L; /* Defaults */
109.     reg->attach_2_u = FALSE;
110.     reg->attach_2_m = 0;
111.     /* reg->attach_2_o = NULL; */
112.     reg->enter_msg = (const char *) 0;
113.     reg->leave_msg = (const char *) 0;
114.     reg->expire_f = NO_CALLBACK;
115.     reg->enter_f = NO_CALLBACK;
116.     reg->can_enter_f = NO_CALLBACK;
117.     reg->leave_f = NO_CALLBACK;
118.     reg->can_leave_f = NO_CALLBACK;
119.     reg->inside_f = NO_CALLBACK;
120.     clear_hero_inside(reg);
121.     clear_heros_fault(reg);
122.     reg->n_monst = 0;
123.     reg->max_monst = 0;
124.     reg->monsters = (unsigned *) 0;
125.     reg->arg = cg.zeroany;
126.     return reg;
127. }
128. 
129. /*
130.  * Add rectangle to region.
131.  */
132. void
133. add_rect_to_reg(NhRegion *reg, NhRect *rect)
134. {
135.     NhRect *tmp_rect;
136. 
137.     tmp_rect = (NhRect *) alloc((reg->nrects + 1) * sizeof (NhRect));
138.     if (reg->nrects > 0) {
139.         (void) memcpy((genericptr_t) tmp_rect, (genericptr_t) reg->rects,
140.                       reg->nrects * sizeof (NhRect));
141.         free((genericptr_t) reg->rects);
142.     }
143.     tmp_rect[reg->nrects] = *rect;
144.     reg->nrects++;
145.     reg->rects = tmp_rect;
146.     /* Update bounding box if needed */
147.     if (reg->bounding_box.lx > rect->lx)
148.         reg->bounding_box.lx = rect->lx;
149.     if (reg->bounding_box.ly > rect->ly)
150.         reg->bounding_box.ly = rect->ly;
151.     if (reg->bounding_box.hx < rect->hx)
152.         reg->bounding_box.hx = rect->hx;
153.     if (reg->bounding_box.hy < rect->hy)
154.         reg->bounding_box.hy = rect->hy;
155. }
156. 
157. /*
158.  * Add a monster to the region
159.  */
160. void
161. add_mon_to_reg(NhRegion *reg, struct monst *mon)
162. {
163.     int i;
164.     unsigned *tmp_m;
165. 
166.     /* if this is a long worm, it might already be present in the region;
167.        only include it once no matter how segments the region contains */
168.     if (mon_in_region(reg, mon)) {
169.         if (mon->data != &mons[PM_LONG_WORM])
170.             impossible("add_mon_to_reg: %s [#%u] already in region.",
171.                        m_monnam(mon), mon->m_id);
172.         return;
173.     }
174.     if (reg->max_monst <= reg->n_monst) {
175.         tmp_m = (unsigned *) alloc(sizeof (unsigned)
176.                                    * (reg->max_monst + MONST_INC));
177.         if (reg->max_monst > 0) {
178.             for (i = 0; i < reg->max_monst; i++)
179.                 tmp_m[i] = reg->monsters[i];
180.             free((genericptr_t) reg->monsters);
181.         }
182.         reg->monsters = tmp_m;
183.         reg->max_monst += MONST_INC;
184.     }
185.     reg->monsters[reg->n_monst++] = mon->m_id;
186. }
187. 
188. /*
189.  * Remove a monster from the region list (it left or died...)
190.  */
191. void
192. remove_mon_from_reg(NhRegion *reg, struct monst *mon)
193. {
194.     int i;
195. 
196.     for (i = 0; i < reg->n_monst; i++)
197.         if (reg->monsters[i] == mon->m_id) {
198.             reg->n_monst--;
199.             reg->monsters[i] = reg->monsters[reg->n_monst];
200.             return;
201.         }
202. }
203. 
204. /*
205.  * Check if a monster is inside the region.
206.  * It's probably quicker to check with the region internal list
207.  * than to check for coordinates.
208.  */
209. boolean
210. mon_in_region(NhRegion *reg, struct monst *mon)
211. {
212.     int i;
213. 
214.     for (i = 0; i < reg->n_monst; i++)
215.         if (reg->monsters[i] == mon->m_id)
216.             return TRUE;
217.     return FALSE;
218. }
219. 
220. #if 0
221. /* not yet used */
222. 
223. /*
224.  * Clone (make a standalone copy) the region.
225.  */
226. NhRegion *
227. clone_region(NhRegion *reg)
228. {
229.     NhRegion *ret_reg;
230.     unsigned *m_id_list;
231.     short i;
232. 
233.     ret_reg = create_region(reg->rects, reg->nrects);
234.     ret_reg->ttl = reg->ttl;
235.     ret_reg->attach_2_u = reg->attach_2_u;
236.     ret_reg->attach_2_m = reg->attach_2_m;
237.  /* ret_reg->attach_2_o = reg->attach_2_o; */
238.     ret_reg->expire_f = reg->expire_f;
239.     ret_reg->enter_f = reg->enter_f;
240.     ret_reg->can_enter_f = reg->can_enter_f;
241.     ret_reg->leave_f = reg->leave_f;
242.     ret_reg->can_leave_f = reg->can_leave_f;
243.     ret_reg->player_flags = reg->player_flags; /* set/clear_hero_inside,&c*/
244.     ret_reg->n_monst = reg->n_monst;
245.     ret_reg->max_monst = reg->max_monst;
246.     if (reg->max_monst > 0) {
247.         m_id_list = (unsigned *) alloc(reg->max_monst * sizeof (unsigned));
248.         for (i = 0; i < reg->max_monst; ++i)
249.             m_id_list[i] = reg->monsters[i];
250.     } else
251.         m_id_list = (unsigned *) 0;
252.     ret_reg->monsters = m_id_list;
253.     return ret_reg;
254. }
255. 
256. #endif /*0*/
257. #endif /* !SFCTOOL */
258. 
259. /*
260.  * Free mem from region.
261.  */
262. void
263. free_region(NhRegion *reg)
264. {
265.     if (reg) {
266.         if (reg->rects)
267.             free((genericptr_t) reg->rects);
268.         if (reg->monsters)
269.             free((genericptr_t) reg->monsters);
270.         if (reg->enter_msg)
271.             free((genericptr_t) reg->enter_msg);
272.         if (reg->leave_msg)
273.             free((genericptr_t) reg->leave_msg);
274.         free((genericptr_t) reg);
275.     }
276. }
277. 
278. #ifndef SFCTOOL
279. /*
280.  * Add a region to the list.
281.  * This actually activates the region.
282.  */
283. void
284. add_region(NhRegion *reg)
285. {
286.     NhRegion **tmp_reg;
287.     int i, j;
288. 
289.     if (gm.max_regions <= svn.n_regions) {
290.         tmp_reg = gr.regions;
291.         gr.regions =
292.             (NhRegion **) alloc((gm.max_regions + 10) * sizeof (NhRegion *));
293.         if (gm.max_regions > 0) {
294.             (void) memcpy((genericptr_t) gr.regions, (genericptr_t) tmp_reg,
295.                           gm.max_regions * sizeof (NhRegion *));
296.             free((genericptr_t) tmp_reg);
297.         }
298.         gm.max_regions += 10;
299.     }
300.     gr.regions[svn.n_regions] = reg;
301.     svn.n_regions++;
302.     /* Check for monsters inside the region */
303.     for (i = reg->bounding_box.lx; i <= reg->bounding_box.hx; i++)
304.         for (j = reg->bounding_box.ly; j <= reg->bounding_box.hy; j++) {
305.             struct monst *mtmp;
306.             boolean is_inside = FALSE;
307. 
308.             /* Some regions can cross the level boundaries */
309.             if (!isok(i, j))
310.                 continue;
311.             if (inside_region(reg, i, j)) {
312.                 is_inside = TRUE;
313.                 /* if there's a monster here, add it to the region */
314.                 if ((mtmp = m_at(i, j)) != 0
315. #if 0
316.                     /* leave this bit (to exclude long worm tails) out;
317.                        assume that worms use "cutaneous respiration" (they
318.                        breath through their skin rather than nose/gills/&c)
319.                        so their tails are susceptible to poison gas */
320.                     && mtmp->mx == i && mtmp->my == j
321. #endif
322.                     ) {
323.                     add_mon_to_reg(reg, mtmp);
324.                 }
325.             }
326.             if (reg->visible) {
327.                 if (is_inside)
328.                     block_point(i, j);
329.                 if (cansee(i, j))
330.                     newsym(i, j);
331.             }
332.         }
333.     /* Check for player now... */
334.     if (inside_region(reg, u.ux, u.uy))
335.         set_hero_inside(reg);
336.     else
337.         clear_hero_inside(reg);
338. }
339. 
340. /*
341.  * Remove a region from the list & free it.
342.  */
343. void
344. remove_region(NhRegion *reg)
345. {
346.     int i, x, y;
347. 
348.     for (i = 0; i < svn.n_regions; i++)
349.         if (gr.regions[i] == reg)
350.             break;
351.     if (i == svn.n_regions)
352.         return;
353. 
354.     /* remove region before potential newsym() calls, but don't free it yet */
355.     if (--svn.n_regions != i)
356.         gr.regions[i] = gr.regions[svn.n_regions];
357.     gr.regions[svn.n_regions] = (NhRegion *) 0;
358. 
359.     /* Update screen if necessary */
360.     reg->ttl = -2L; /* for visible_region_at */
361.     if (reg->visible) {
362.         int pass;
363.         boolean tmp_uinwater = u.uinwater;
364. 
365.         /* need to process the region's spots twice, first unblocking all
366.            locations which no longer block line-of-sight, then redrawing
367.            spots within revised line-of-sight; skip second pass if blind */
368.         for (pass = 1; pass <= (Blind ? 1 : 2); ++pass) {
369.             u.uinwater = (pass == 1) ? 0 : tmp_uinwater;
370. 
371.             for (x = reg->bounding_box.lx; x <= reg->bounding_box.hx; x++)
372.                 for (y = reg->bounding_box.ly; y <= reg->bounding_box.hy; y++)
373.                     if (isok(x, y) && inside_region(reg, x, y)) {
374.                         if (pass == 1) {
375.                             if (!does_block(x, y, &levl[x][y]))
376.                                 unblock_point(x, y);
377.                         } else { /* pass==2 */
378.                             if (cansee(x, y))
379.                                 newsym(x, y);
380.                         }
381.                     }
382.         }
383.         u.uinwater = tmp_uinwater;
384.     }
385.     free_region(reg);
386. }
387. #endif /* !SFCTOOL */
388. 
389. /*
390.  * Remove all regions and clear all related data.  This must be done
391.  * when changing level, for instance.
392.  */
393. void
394. clear_regions(void)
395. {
396.     int i;
397. 
398.     for (i = 0; i < svn.n_regions; i++)
399.         free_region(gr.regions[i]);
400.     svn.n_regions = 0;
401.     if (gm.max_regions > 0)
402.         free((genericptr_t) gr.regions);
403.     gm.max_regions = 0;
404.     gr.regions = (NhRegion **) 0;
405. }
406. 
407. #ifndef SFCTOOL
408. /*
409.  * This function is called every turn.
410.  * It makes the regions age, if necessary and calls the appropriate
411.  * callbacks when needed.
412.  */
413. void
414. run_regions(void)
415. {
416.     int i, j, k;
417.     int f_indx;
418. 
419.     /* reset some messaging variables */
420.     gg.gas_cloud_diss_within = FALSE;
421.     gg.gas_cloud_diss_seen = 0;
422. 
423.     /* End of life ? */
424.     /* Do it backward because the array will be modified */
425.     for (i = svn.n_regions - 1; i >= 0; i--) {
426.         if (gr.regions[i]->ttl == 0L) {
427.             if ((f_indx = gr.regions[i]->expire_f) == NO_CALLBACK
428.                 || (*callbacks[f_indx])(gr.regions[i], (genericptr_t) 0))
429.                 remove_region(gr.regions[i]);
430.         }
431.     }
432. 
433.     /* Process remaining regions */
434.     for (i = 0; i < svn.n_regions; i++) {
435.         /* Make the region age */
436.         if (gr.regions[i]->ttl > 0L)
437.             gr.regions[i]->ttl--;
438.         /* Check if player is inside region */
439.         f_indx = gr.regions[i]->inside_f;
440.         if (f_indx != NO_CALLBACK && hero_inside(gr.regions[i]))
441.             (void) (*callbacks[f_indx])(gr.regions[i], (genericptr_t) 0);
442.         /* Check if any monster is inside region */
443.         if (f_indx != NO_CALLBACK) {
444.             for (j = 0; j < gr.regions[i]->n_monst; j++) {
445.                 struct monst *mtmp =
446.                     find_mid(gr.regions[i]->monsters[j], FM_FMON);
447. 
448.                 if (!mtmp || DEADMONSTER(mtmp)
449.                     || (*callbacks[f_indx])(gr.regions[i], mtmp)) {
450.                     /* The monster died, remove it from list */
451.                     k = (gr.regions[i]->n_monst -= 1);
452.                     gr.regions[i]->monsters[j] = gr.regions[i]->monsters[k];
453.                     gr.regions[i]->monsters[k] = 0;
454.                     --j; /* current slot has been reused; recheck it next */
455.                 }
456.             }
457.         }
458.     }
459. 
460.     if (gg.gas_cloud_diss_within) {
461.         pline_The("gas cloud around you dissipates.");
462.         /* normally won't see additional dissipation when within */
463.         /* FIXME? this assumes that additional dissipation is close by */
464.         if (u.xray_range <= 1)
465.             gg.gas_cloud_diss_seen = 0;
466.         gg.gas_cloud_diss_within = FALSE;
467.     }
468.     if (gg.gas_cloud_diss_seen) {
469.         You_see("%s gas cloud%s dissipate.",
470.                 (gg.gas_cloud_diss_seen == 1) ? "a" : "some",
471.                 plur(gg.gas_cloud_diss_seen));
472.         gg.gas_cloud_diss_seen = 0;
473.     }
474. }
475. 
476. /*
477.  * check whether player enters/leaves one or more regions.
478.  */
479. boolean
480. in_out_region(coordxy x, coordxy y)
481. {
482.     int i, f_indx = 0;
483. 
484.     /* First check if hero can do the move */
485.     for (i = 0; i < svn.n_regions; i++) {
486.         if (gr.regions[i]->attach_2_u)
487.             continue;
488.         if (inside_region(gr.regions[i], x, y)
489.             ? (!hero_inside(gr.regions[i])
490.                && (f_indx = gr.regions[i]->can_enter_f) != NO_CALLBACK)
491.             : (hero_inside(gr.regions[i])
492.                && (f_indx = gr.regions[i]->can_leave_f) != NO_CALLBACK)) {
493.             if (!(*callbacks[f_indx])(gr.regions[i], (genericptr_t) 0))
494.                 return FALSE;
495.         }
496.     }
497. 
498.     /* Callbacks for the regions hero does leave */
499.     for (i = 0; i < svn.n_regions; i++) {
500.         if (gr.regions[i]->attach_2_u)
501.             continue;
502.         if (hero_inside(gr.regions[i])
503.             && !inside_region(gr.regions[i], x, y)) {
504.             clear_hero_inside(gr.regions[i]);
505.             if (gr.regions[i]->leave_msg != (const char *) 0)
506.                 pline1(gr.regions[i]->leave_msg);
507.             if ((f_indx = gr.regions[i]->leave_f) != NO_CALLBACK)
508.                 (void) (*callbacks[f_indx])(gr.regions[i], (genericptr_t) 0);
509.         }
510.     }
511. 
512.     /* Callbacks for the regions hero does enter */
513.     for (i = 0; i < svn.n_regions; i++) {
514.         if (gr.regions[i]->attach_2_u)
515.             continue;
516.         if (!hero_inside(gr.regions[i])
517.             && inside_region(gr.regions[i], x, y)) {
518.             set_hero_inside(gr.regions[i]);
519.             if (gr.regions[i]->enter_msg != (const char *) 0)
520.                 pline1(gr.regions[i]->enter_msg);
521.             if ((f_indx = gr.regions[i]->enter_f) != NO_CALLBACK)
522.                 (void) (*callbacks[f_indx])(gr.regions[i], (genericptr_t) 0);
523.         }
524.     }
525. 
526.     return TRUE;
527. }
528. 
529. /*
530.  * check whether a monster enters/leaves one or more regions.
531.  */
532. boolean
533. m_in_out_region(struct monst *mon, coordxy x, coordxy y)
534. {
535.     int i, f_indx = 0;
536. 
537.     /* First check if mon can do the move */
538.     for (i = 0; i < svn.n_regions; i++) {
539.         if (gr.regions[i]->attach_2_m == mon->m_id)
540.             continue;
541.         if (inside_region(gr.regions[i], x, y)
542.             ? (!mon_in_region(gr.regions[i], mon)
543.                && (f_indx = gr.regions[i]->can_enter_f) != NO_CALLBACK)
544.             : (mon_in_region(gr.regions[i], mon)
545.                && (f_indx = gr.regions[i]->can_leave_f) != NO_CALLBACK)) {
546.             if (!(*callbacks[f_indx])(gr.regions[i], mon))
547.                 return FALSE;
548.         }
549.     }
550. 
551.     /* Callbacks for the regions mon does leave */
552.     for (i = 0; i < svn.n_regions; i++) {
553.         if (gr.regions[i]->attach_2_m == mon->m_id)
554.             continue;
555.         if (mon_in_region(gr.regions[i], mon)
556.             && !inside_region(gr.regions[i], x, y)) {
557.             remove_mon_from_reg(gr.regions[i], mon);
558.             if ((f_indx = gr.regions[i]->leave_f) != NO_CALLBACK)
559.                 (void) (*callbacks[f_indx])(gr.regions[i], mon);
560.         }
561.     }
562. 
563.     /* Callbacks for the regions mon does enter */
564.     for (i = 0; i < svn.n_regions; i++) {
565.         if (gr.regions[i]->attach_2_m == mon->m_id)
566.             continue;
567.         if (!mon_in_region(gr.regions[i], mon)
568.             && inside_region(gr.regions[i], x, y)) {
569.             add_mon_to_reg(gr.regions[i], mon);
570.             if ((f_indx = gr.regions[i]->enter_f) != NO_CALLBACK)
571.                 (void) (*callbacks[f_indx])(gr.regions[i], mon);
572.         }
573.     }
574. 
575.     return TRUE;
576. }
577. 
578. /*
579.  * Checks player's regions after a teleport for instance.
580.  */
581. void
582. update_player_regions(void)
583. {
584.     int i;
585. 
586.     for (i = 0; i < svn.n_regions; i++)
587.         if (!gr.regions[i]->attach_2_u
588.             && inside_region(gr.regions[i], u.ux, u.uy))
589.             set_hero_inside(gr.regions[i]);
590.         else
591.             clear_hero_inside(gr.regions[i]);
592. }
593. 
594. /*
595.  * Ditto for a specified monster.
596.  */
597. void
598. update_monster_region(struct monst *mon)
599. {
600.     int i;
601. 
602.     for (i = 0; i < svn.n_regions; i++) {
603.         if (inside_region(gr.regions[i], mon->mx, mon->my)) {
604.             if (!mon_in_region(gr.regions[i], mon))
605.                 add_mon_to_reg(gr.regions[i], mon);
606.         } else {
607.             if (mon_in_region(gr.regions[i], mon))
608.                 remove_mon_from_reg(gr.regions[i], mon);
609.         }
610.     }
611. }
612. 
613. #if 0
614. /* not yet used */
615. 
616. /*
617.  * Change monster pointer in gr.regions
618.  * This happens, for instance, when a monster grows and
619.  * need a new structure (internally that is).
620.  */
621. void
622. replace_mon_regions(monold, monnew)
623. struct monst *monold, *monnew;
624. {
625.     int i;
626. 
627.     for (i = 0; i < svn.n_regions; i++)
628.         if (mon_in_region(gr.regions[i], monold)) {
629.             remove_mon_from_reg(gr.regions[i], monold);
630.             add_mon_to_reg(gr.regions[i], monnew);
631.         }
632. }
633. 
634. /*
635.  * Remove monster from all regions it was in (ie monster just died)
636.  */
637. void
638. remove_mon_from_regions(struct monst *mon)
639. {
640.     int i;
641. 
642.     for (i = 0; i < svn.n_regions; i++)
643.         if (mon_in_region(gr.regions[i], mon))
644.             remove_mon_from_reg(gr.regions[i], mon);
645. }
646. 
647. #endif /*0*/
648. 
649. /* per-turn damage inflicted by visible region; hides details from caller */
650. int
651. reg_damg(NhRegion *reg)
652. {
653.     int damg = (!reg->visible || reg->ttl == -2L) ? 0 : reg->arg.a_int;
654. 
655.     return damg;
656. }
657. 
658. /* check whether current level has any visible regions */
659. boolean
660. any_visible_region(void)
661. {
662.     int i;
663. 
664.     for (i = 0; i < svn.n_regions; i++) {
665.         if (!gr.regions[i]->visible || gr.regions[i]->ttl == -2L)
666.             continue;
667.         return TRUE;
668.     }
669.     return FALSE;
670. }
671. 
672. /* for the wizard mode #timeout command */
673. void
674. visible_region_summary(winid win)
675. {
676.     NhRegion *reg;
677.     char buf[BUFSZ], typbuf[QBUFSZ];
678.     int i, damg, hdr_done = 0;
679.     const char *fldsep = iflags.menu_tab_sep ? "\t" : "  ";
680. 
681.     for (i = 0; i < svn.n_regions; i++) {
682.         reg = gr.regions[i];
683.         if (!reg->visible || reg->ttl == -2L)
684.             continue;
685. 
686.         if (!hdr_done++) {
687.             putstr(win, 0, "");
688.             putstr(win, 0, "Visible regions");
689.         }
690.         /*
691.          * TODO? sort the regions by time-to-live or by bounding box.
692.          */
693. 
694.         /* we display relative time (turns left) rather than absolute
695.            (the turn when region will go away);
696.            since time-to-live has already been decremented, regions
697.            which are due to timeout on the next turn have ttl==0;
698.            adding 1 is intended to make the display be less confusing */
699.         Sprintf(buf, "%5ld", reg->ttl + 1L);
700.         damg = reg->arg.a_int;
701.         if (damg)
702.             Sprintf(typbuf, "poison gas (%d)", damg);
703.         else
704.             Strcpy(typbuf, "vapor");
705.         Sprintf(eos(buf), "%s%-16s", fldsep, typbuf);
706.         Sprintf(eos(buf), "%s@[%d,%d..%d,%d]", fldsep,
707.                 reg->bounding_box.lx, reg->bounding_box.ly,
708.                 reg->bounding_box.hx, reg->bounding_box.hy);
709.         putstr(win, 0, buf);
710.     }
711. }
712. 
713. /*
714.  * Check if a spot is under a visible region (eg: gas cloud).
715.  * Returns NULL if not, otherwise returns region.
716.  */
717. NhRegion *
718. visible_region_at(coordxy x, coordxy y)
719. {
720.     int i;
721. 
722.     for (i = 0; i < svn.n_regions; i++) {
723.         if (!gr.regions[i]->visible || gr.regions[i]->ttl == -2L)
724.             continue;
725.         if (inside_region(gr.regions[i], x, y))
726.             return gr.regions[i];
727.     }
728.     return (NhRegion *) 0;
729. }
730. 
731. void
732. show_region(NhRegion *reg, coordxy x, coordxy y)
733. {
734.     show_glyph(x, y, reg->glyph);
735. }
736. 
737. /**
738.  * save_regions :
739.  */
740. void
741. save_regions(NHFILE *nhfp)
742. {
743.     NhRegion *r;
744.     int i, j;
745.     unsigned n;
746. 
747.     if (!update_file(nhfp))
748.         goto skip_lots;
749.     /* timestamp */
750.     Sfo_long(nhfp, &svm.moves, "region-tmstamp");
751.     Sfo_int(nhfp, &svn.n_regions, "region-region_count");
752. 
753.     for (i = 0; i < svn.n_regions; i++) {
754.         r = gr.regions[i];
755.         Sfo_nhrect(nhfp, &r->bounding_box, "region-bounding_box");
756.         Sfo_short(nhfp, &r->nrects, "region-nrects");
757.         for (j = 0; j < r->nrects; j++) {
758.             Sfo_nhrect(nhfp, &r->rects[j], "region-rect");
759.         }
760.         Sfo_boolean(nhfp, &r->attach_2_u, "region-attach_2_u");
761.         Sfo_unsigned(nhfp, &r->attach_2_m, "region-attach_2_m");
762.         n = 0;
763.         n = !r->enter_msg ? 0U : (unsigned) strlen(r->enter_msg);
764.         Sfo_unsigned(nhfp, &n, "region-enter_msg_length");
765.         if (n > 0) {
766.             Sfo_char(nhfp, (char *) r->enter_msg,
767.                      "region-enter_msg", (int) n);
768.         }
769.         n = !r->leave_msg ? 0U : (unsigned) strlen(r->leave_msg);
770.         Sfo_unsigned(nhfp, &n, "region-leave_msg_length");
771.         if (n > 0) {
772.             Sfo_char(nhfp, (char *) r->leave_msg, "region-leave_msg", (int) n);
773.         }
774.         Sfo_long(nhfp, &r->ttl, "region-ttl");
775.         Sfo_short(nhfp, &r->expire_f, "region-expire_f");
776.         Sfo_short(nhfp, &r->can_enter_f, "region-can_enter_f");
777.         Sfo_short(nhfp, &r->enter_f, "region-enter_f");
778.         Sfo_short(nhfp, &r->can_leave_f, "region-can_leave_f");
779.         Sfo_short(nhfp, &r->leave_f, "region-leave_f");
780.         Sfo_short(nhfp, &r->inside_f, "region-inside_f");
781.         Sfo_unsigned(nhfp, &r->player_flags, "region-player_flags");
782.         Sfo_short(nhfp, &r->n_monst, "region-monster_count");
783. 
784.         for (j = 0; j < r->n_monst; j++) {
785.             Sfo_unsigned(nhfp, &r->monsters[j], "region-monster");
786.         }
787.         Sfo_boolean(nhfp, &r->visible, "region-visible");
788.         Sfo_int(nhfp, &r->glyph, "region-glyph");
789.         Sfo_any(nhfp, &r->arg, "region-arg");
790.     }
791. 
792.  skip_lots:
793.     if (release_data(nhfp))
794.         clear_regions();
795. }
796. #endif /* !SFCTOOL */
797. 
798. void
799. rest_regions(NHFILE *nhfp)
800. {
801.     NhRegion *r;
802.     int i, j;
803.     unsigned n = 0;
804.     long tmstamp = 0L;
805.     char *msg_buf;
806.     boolean ghostly = (nhfp->ftype == NHF_BONESFILE);
807. 
808.     clear_regions(); /* Just for security */
809.     Sfi_long(nhfp, &tmstamp, "region-tmstamp");
810.     if (ghostly)
811.         tmstamp = 0;
812.     else
813.         tmstamp = (svm.moves - tmstamp);
814.     Sfi_int(nhfp, &svn.n_regions, "region-region_count");
815.     gm.max_regions = svn.n_regions;
816.     if (svn.n_regions > 0)
817.         gr.regions = (NhRegion **) alloc(svn.n_regions * sizeof (NhRegion *));
818.     for (i = 0; i < svn.n_regions; i++) {
819.         r = gr.regions[i] = (NhRegion *) alloc(sizeof (NhRegion));
820.         Sfi_nhrect(nhfp, &r->bounding_box, "region-bounding box");
821.         Sfi_short(nhfp, &r->nrects, "region-nrects");
822.         if (r->nrects > 0)
823.             r->rects = (NhRect *) alloc(r->nrects * sizeof (NhRect));
824.         else
825.             r->rects = (NhRect *) 0;
826.         for (j = 0; j < r->nrects; j++) {
827.            Sfi_nhrect(nhfp, &r->rects[j], "region-rect");
828.         }
829. 
830.         Sfi_boolean(nhfp, &r->attach_2_u, "region-attach_2_u");
831.         Sfi_unsigned(nhfp, &r->attach_2_m, "region-attach_2_m");
832.         Sfi_unsigned(nhfp, &n, "region-enter_msg_length");
833.         if (n > 0) {
834.             msg_buf = (char *) alloc(n + 1);
835.             Sfi_char(nhfp, msg_buf, "region-enter_msg", n);
836.             msg_buf[n] = '\0';
837.         } else {
838.             msg_buf = (char *) 0;
839.         }
840.         r->enter_msg = (const char *) msg_buf;
841. 
842.         Sfi_unsigned(nhfp, &n, "region-leave_msg_length");
843.         if (n > 0) {
844.             msg_buf = (char *) alloc(n + 1);
845.             Sfi_char(nhfp, msg_buf, "region-leave_msg", n);
846.             msg_buf[n] = '\0';
847.             r->leave_msg = (const char *) msg_buf;
848.         } else {
849.             msg_buf = (char *) 0;
850.         }
851.         r->leave_msg = (const char *) msg_buf;
852. 
853.         Sfi_long(nhfp, &r->ttl, "region-ttl");
854.         /* check for expired region */
855.         if (r->ttl >= 0L)
856.             r->ttl = (r->ttl > tmstamp) ? r->ttl - tmstamp : 0L;
857.         Sfi_short(nhfp, &r->expire_f, "region-expire_f");
858.         Sfi_short(nhfp, &r->can_enter_f, "region-can_enter_f");
859.         Sfi_short(nhfp, &r->enter_f, "region-enter_f");
860.         Sfi_short(nhfp, &r->can_leave_f, "region-can_leave_f");
861.         Sfi_short(nhfp, &r->leave_f, "region-leave_f");
862.         Sfi_short(nhfp, &r->inside_f, "region-inside_f");
863.         Sfi_unsigned(nhfp, &r->player_flags, "region-player_flags");
864.         if (ghostly) { /* settings pertained to old player */
865.             clear_hero_inside(r);
866.             clear_heros_fault(r);
867.         }
868.         Sfi_short(nhfp, &r->n_monst, "region-monster_count");
869.         if (r->n_monst > 0)
870.             r->monsters = (unsigned *) alloc(r->n_monst * sizeof (unsigned));
871.         else
872.             r->monsters = (unsigned *) 0;
873.         r->max_monst = r->n_monst;
874.         for (j = 0; j < r->n_monst; j++) {
875.             Sfi_unsigned(nhfp, &r->monsters[j], "region-monster");
876.         }
877.         Sfi_boolean(nhfp, &r->visible, "region-visible");
878.         Sfi_int(nhfp, &r->glyph, "region-glyph");
879.         Sfi_any(nhfp, &r->arg, "region-arg");
880.     }
881. #ifndef SFCTOOL
882.     /* remove expired regions, do not trigger the expire_f callback (yet!);
883.        also update monster lists if this data is coming from a bones file */
884.     for (i = svn.n_regions - 1; i >= 0; i--) {
885.         r = gr.regions[i];
886.         if (r->ttl == 0L)
887.             remove_region(r);
888.         else if (ghostly && r->n_monst > 0)
889.             reset_region_mids(r);
890.     }
891. #endif /* !SFCTOOL */
892. }
893. 
894. #ifndef SFCTOOL
895. DISABLE_WARNING_FORMAT_NONLITERAL
896. 
897. /* to support '#stats' wizard-mode command */
898. void
899. region_stats(
900.     const char *hdrfmt,
901.     char *hdrbuf,
902.     long *count,
903.     long *size)
904. {
905.     NhRegion *rg;
906.     int i;
907. 
908.     /* other stats formats take one parameter; this takes two */
909.     Sprintf(hdrbuf, hdrfmt, (long) sizeof (NhRegion), (long) sizeof (NhRect));
910.     *count = (long) svn.n_regions; /* might be 0 even tho max_regions isn't */
911.     *size = (long) gm.max_regions * (long) sizeof (NhRegion);
912.     for (i = 0; i < svn.n_regions; ++i) {
913.         rg = gr.regions[i];
914.         *size += (long) rg->nrects * (long) sizeof (NhRect);
915.         if (rg->enter_msg)
916.             *size += (long) (strlen(rg->enter_msg) + 1);
917.         if (rg->leave_msg)
918.             *size += (long) (strlen(rg->leave_msg) + 1);
919.         *size += (long) rg->max_monst * (long) sizeof *rg->monsters;
920.     }
921.     /* ? */
922. }
923. 
924. RESTORE_WARNING_FORMAT_NONLITERAL
925. 
926. /* update monster IDs for region being loaded from bones; `ghostly' implied */
927. staticfn void
928. reset_region_mids(NhRegion *reg)
929. {
930.     int i = 0, n = reg->n_monst;
931.     unsigned *mid_list = reg->monsters;
932. 
933.     while (i < n)
934.         if (!lookup_id_mapping(mid_list[i], &mid_list[i])) {
935.             /* shrink list to remove missing monster; order doesn't matter */
936.             mid_list[i] = mid_list[--n];
937.         } else {
938.             /* move on to next monster */
939.             ++i;
940.         }
941.     reg->n_monst = n;
942.     return;
943. }
944. 
945. #if 0
946. /* not yet used */
947. 
948. /*--------------------------------------------------------------*
949.  *                                                              *
950.  *                      Create Region with just a message       *
951.  *                                                              *
952.  *--------------------------------------------------------------*/
953. 
954. NhRegion *
955. create_msg_region(
956.     coordxy x, coordxy y, coordxy w, coordxy h,
957.     const char *msg_enter, const char *msg_leave)
958. {
959.     NhRect tmprect;
960.     NhRegion *reg = create_region((NhRect *) 0, 0);
961. 
962.     if (msg_enter)
963.         reg->enter_msg = dupstr(msg_enter);
964.     if (msg_leave)
965.         reg->leave_msg = dupstr(msg_leave);
966.     tmprect.lx = x;
967.     tmprect.ly = y;
968.     tmprect.hx = x + w;
969.     tmprect.hy = y + h;
970.     add_rect_to_reg(reg, &tmprect);
971.     reg->ttl = -1L;
972.     return reg;
973. }
974. 
975. 
976. /*--------------------------------------------------------------*
977.  *                                                              *
978.  *                      Force Field Related Cod                 *
979.  *                      (unused yet)                            *
980.  *--------------------------------------------------------------*/
981. 
982. boolean
983. enter_force_field(genericptr_t p1, genericptr_t p2)
984. {
985.     struct monst *mtmp;
986. 
987.     if (p2 == (genericptr_t) 0) { /* That means the player */
988.         if (!Blind)
989.             You("bump into %s.  Ouch!",
990.                 Hallucination ? "an invisible tree"
991.                               : "some kind of invisible wall");
992.         else
993.             pline("Ouch!");
994.     } else {
995.         mtmp = (struct monst *) p2;
996.         if (canseemon(mtmp))
997.             pline("%s bumps into %s!", Monnam(mtmp), something);
998.     }
999.     return FALSE;
1000. }
1001. 
1002. NhRegion *
1003. create_force_field(coordxy x, coordxy y, int radius, long ttl)
1004. {
1005.     int i;
1006.     NhRegion *ff;
1007.     int nrect;
1008.     NhRect tmprect;
1009. 
1010.     ff = create_region((NhRect *) 0, 0);
1011.     nrect = radius;
1012.     tmprect.lx = x;
1013.     tmprect.hx = x;
1014.     tmprect.ly = y - (radius - 1);
1015.     tmprect.hy = y + (radius - 1);
1016.     for (i = 0; i < nrect; i++) {
1017.         add_rect_to_reg(ff, &tmprect);
1018.         tmprect.lx--;
1019.         tmprect.hx++;
1020.         tmprect.ly++;
1021.         tmprect.hy--;
1022.     }
1023.     ff->ttl = ttl;
1024.     if (!gi.in_mklev && !svc.context.mon_moving)
1025.         set_heros_fault(ff); /* assume player has created it */
1026.  /* ff->can_enter_f = enter_force_field; */
1027.  /* ff->can_leave_f = enter_force_field; */
1028.     add_region(ff);
1029.     return ff;
1030. }
1031. 
1032. #endif /*0*/
1033. 
1034. /*--------------------------------------------------------------*
1035.  *                                                              *
1036.  *                      Gas cloud related code                  *
1037.  *                                                              *
1038.  *--------------------------------------------------------------*/
1039. 
1040. /*
1041.  * Here is an example of an expire function that may prolong
1042.  * region life after some mods...
1043.  */
1044. /*ARGSUSED*/
1045. boolean
1046. expire_gas_cloud(genericptr_t p1, genericptr_t p2 UNUSED)
1047. {
1048.     NhRegion *reg;
1049.     int damage, pass;
1050.     coordxy x, y;
1051. 
1052.     reg = (NhRegion *) p1;
1053.     damage = reg->arg.a_int;
1054. 
1055.     /* If it was a thick cloud, it dissipates a little first */
1056.     if (damage >= 5) {
1057.         damage /= 2; /* It dissipates, let's do less damage */
1058.         reg->arg = cg.zeroany;
1059.         reg->arg.a_int = damage;
1060.         reg->ttl = 2L; /* Here's the trick : reset ttl */
1061.         return FALSE;  /* THEN return FALSE, means "still there" */
1062.     }
1063. 
1064.     /* The cloud no longer blocks vision.  cansee() checks shouldn't be made
1065.        until all blocked spots have been unblocked, so we need two passes */
1066.     for (pass = 1; pass <= (Blind ? 1 : 2); ++pass) {
1067.         for (x = reg->bounding_box.lx; x <= reg->bounding_box.hx; x++) {
1068.             for (y = reg->bounding_box.ly; y <= reg->bounding_box.hy; y++) {
1069.                 if (inside_region(reg, x, y)) {
1070.                     if (pass == 1) {
1071.                         if (!does_block(x, y, &levl[x][y]))
1072.                             unblock_point(x, y);
1073.                     } else { /* pass==2 */
1074.                         if (!u.uswallow) {
1075.                             if (u_at(x, y))
1076.                                 gg.gas_cloud_diss_within = TRUE;
1077.                             else if (cansee(x, y))
1078.                                 gg.gas_cloud_diss_seen++;
1079.                         }
1080.                     }
1081.                 }
1082.             }
1083.         }
1084.     }
1085. 
1086.     return TRUE; /* OK, it's gone, you can free it! */
1087. }
1088. 
1089. /* returns True if p2 is killed by region p1, False otherwise */
1090. boolean
1091. inside_gas_cloud(genericptr_t p1, genericptr_t p2)
1092. {
1093.     NhRegion *reg = (NhRegion *) p1;
1094.     struct monst *mtmp = (struct monst *) p2;
1095.     struct monst *umon = mtmp ? mtmp : &gy.youmonst;
1096.     int dam = reg->arg.a_int;
1097. 
1098.     /*
1099.      * Gas clouds can't be targeted at water locations, but they can
1100.      * start next to water and spread over it.
1101.      */
1102. 
1103.     /* fog clouds maintain gas clouds, even poisonous ones */
1104.     if (reg->ttl < 20 && umon && umon->data == &mons[PM_FOG_CLOUD])
1105.         reg->ttl += 5;
1106. 
1107.     if (dam < 1)
1108.         return FALSE; /* if no damage then there's nothing to do here... */
1109. 
1110.     if (!mtmp) { /* hero is indicated by Null rather than by &youmonst */
1111.         if (m_poisongas_ok(&gy.youmonst) == M_POISONGAS_OK)
1112.             return FALSE;
1113.         if (!Blind) {
1114.             Your("%s sting.", makeplural(body_part(EYE)));
1115.             make_blinded(1L, FALSE);
1116.         }
1117.         if (!Poison_resistance) {
1118.             pline("%s is burning your %s!", Something,
1119.                   makeplural(body_part(LUNG)));
1120.             You("cough and spit blood!");
1121.             wake_nearto(u.ux, u.uy, 2);
1122.             dam = Maybe_Half_Phys(rnd(dam) + 5);
1123.             if (Half_gas_damage) /* worn towel */
1124.                 dam = (dam + 1) / 2;
1125.             losehp(dam, "gas cloud", KILLED_BY_AN);
1126.             monstunseesu(M_SEEN_POISON);
1127.             return FALSE;
1128.         } else {
1129.             You("cough!");
1130.             wake_nearto(u.ux, u.uy, 2);
1131.             monstseesu(M_SEEN_POISON);
1132.             return FALSE;
1133.         }
1134.     } else { /* A monster is inside the cloud */
1135.         mtmp = (struct monst *) p2;
1136. 
1137.         if (m_poisongas_ok(mtmp) != M_POISONGAS_OK) {
1138.             if (!is_silent(mtmp->data)) {
1139.                 if (cansee(mtmp->mx, mtmp->my)
1140.                     || (distu(mtmp->mx, mtmp->my) < 8))
1141.                     pline("%s coughs!", Monnam(mtmp));
1142.                 wake_nearto(mtmp->mx, mtmp->my, 2);
1143.             }
1144.             if (heros_fault(reg))
1145.                 setmangry(mtmp, TRUE);
1146.             if (haseyes(mtmp->data) && mtmp->mcansee) {
1147.                 mtmp->mblinded = 1;
1148.                 mtmp->mcansee = 0;
1149.             }
1150.             if (resists_poison(mtmp))
1151.                 return FALSE;
1152.             mtmp->mhp -= rnd(dam) + 5;
1153.             if (DEADMONSTER(mtmp)) {
1154.                 if (heros_fault(reg))
1155.                     killed(mtmp);
1156.                 else
1157.                     monkilled(mtmp, "gas cloud", AD_DRST);
1158.                 if (DEADMONSTER(mtmp)) { /* not lifesaved */
1159.                     return TRUE;
1160.                 }
1161.             }
1162.         }
1163.     }
1164.     return FALSE; /* Monster is still alive */
1165. }
1166. 
1167. staticfn boolean
1168. is_hero_inside_gas_cloud(void)
1169. {
1170.     int i;
1171. 
1172.     for (i = 0; i < svn.n_regions; i++)
1173.         if (hero_inside(gr.regions[i])
1174.             && gr.regions[i]->inside_f == INSIDE_GAS_CLOUD)
1175.             return TRUE;
1176.     return FALSE;
1177. }
1178. 
1179. /* details of gas cloud creation which are common to create_gas_cloud()
1180.    and create_gas_cloud_selection() */
1181. staticfn void
1182. make_gas_cloud(
1183.     NhRegion *cloud,
1184.     int damage,
1185.     boolean inside_cloud)
1186. {
1187.     if (!gi.in_mklev && !svc.context.mon_moving)
1188.         set_heros_fault(cloud); /* assume player has created it */
1189.     cloud->inside_f = INSIDE_GAS_CLOUD;
1190.     cloud->expire_f = EXPIRE_GAS_CLOUD;
1191.     cloud->arg = cg.zeroany;
1192.     cloud->arg.a_int = damage;
1193.     cloud->visible = TRUE;
1194.     cloud->glyph = cmap_to_glyph(damage ? S_poisoncloud : S_cloud);
1195.     add_region(cloud);
1196. 
1197.     if (!gi.in_mklev && !inside_cloud && is_hero_inside_gas_cloud()) {
1198.         You("are enveloped in a cloud of %s!",
1199.             /* FIXME: "steam" is wrong if this cloud is just the trail of
1200.                a fog cloud's movement; changing to "vapor" would handle
1201.                that but seems a step backward when it really is steam */
1202.             damage ? "noxious gas" : "steam");
1203.         iflags.last_msg = PLNMSG_ENVELOPED_IN_GAS;
1204.     }
1205. }
1206. 
1207. /* Create a gas cloud which starts at (x,y) and grows outward from it via
1208.  * breadth-first search.
1209.  * cloudsize is the number of squares the cloud will attempt to fill.
1210.  * damage is how much it deals to afflicted creatures. */
1211. #define MAX_CLOUD_SIZE 150
1212. NhRegion *
1213. create_gas_cloud(
1214.     coordxy x, coordxy y,
1215.     int cloudsize,
1216.     int damage)
1217. {
1218.     NhRegion *cloud;
1219.     int i, j;
1220.     NhRect tmprect;
1221. 
1222.     /* store visited coords */
1223.     coordxy xcoords[MAX_CLOUD_SIZE];
1224.     coordxy ycoords[MAX_CLOUD_SIZE];
1225.     xcoords[0] = x;
1226.     ycoords[0] = y;
1227.     int curridx;
1228.     int newidx = 1; /* initial spot is already taken */
1229.     boolean inside_cloud = is_hero_inside_gas_cloud();
1230. 
1231.     /* a single-point cloud on hero and it deals no damage.
1232.        probably a natural cause of being polyed. don't message about it */
1233.     if (!svc.context.mon_moving && u_at(x, y) && cloudsize == 1
1234.         && (!damage
1235.             || (damage && m_poisongas_ok(&gy.youmonst) == M_POISONGAS_OK)))
1236.         inside_cloud = TRUE;
1237. 
1238.     if (cloudsize > MAX_CLOUD_SIZE) {
1239.         impossible("create_gas_cloud: cloud too large (%d)!", cloudsize);
1240.         cloudsize = MAX_CLOUD_SIZE;
1241.     }
1242. 
1243.     for (curridx = 0; curridx < newidx; curridx++) {
1244.         if (newidx >= cloudsize)
1245.             break;
1246.         int xx = xcoords[curridx];
1247.         int yy = ycoords[curridx];
1248.         /* Do NOT check for if there is already a gas cloud created at some
1249.          * other time at this position. They can overlap. */
1250. 
1251.         /* Primitive Fisher-Yates-Knuth shuffle to randomize the order of
1252.          * directions chosen. */
1253.         coord dirs[4] = { {0, -1}, {0, 1}, {-1, 0}, {1, 0} };
1254.         for (i = 4; i > 0; --i) {
1255.             coordxy swapidx = rn2(i);
1256.             coord tmp = dirs[swapidx];
1257. 
1258.             dirs[swapidx] = dirs[i - 1];
1259.             dirs[i - 1] = tmp;
1260.         }
1261.         int nvalid = 0; /* # of valid adjacent spots */
1262.         for (i = 0; i < 4; ++i) {
1263.             /* try all 4 cardinal directions */
1264.             int dx = dirs[i].x, dy = dirs[i].y;
1265.             boolean isunpicked = TRUE;
1266. 
1267.             if (valid_cloud_pos(xx + dx, yy + dy)) {
1268.                 nvalid++;
1269.                 /* don't pick a location we've already picked */
1270.                 for (j = 0; j < newidx; ++j) {
1271.                     if (xcoords[j] == xx + dx && ycoords[j] == yy + dy) {
1272.                         isunpicked = FALSE;
1273.                         break;
1274.                     }
1275.                 }
1276.                 /* randomly disrupt the natural breadth-first search, so that
1277.                  * clouds released in open spaces don't always tend towards a
1278.                  * rhombus shape */
1279.                 if (nvalid == 4 && !rn2(2))
1280.                     continue;
1281. 
1282.                 if (isunpicked) {
1283.                     xcoords[newidx] = xx + dx;
1284.                     ycoords[newidx] = yy + dy;
1285.                     newidx++;
1286.                 }
1287.             }
1288.             if (newidx >= cloudsize) {
1289.                 /* don't try further directions */
1290.                 break;
1291.             }
1292.         }
1293.     }
1294.     /* We have now either filled up xcoord and ycoord entirely or run out
1295.        of space.  In either case, newidx is the correct total number of
1296.        coordinates inserted. */
1297.     cloud = create_region((NhRect *) 0, 0);
1298.     for (i = 0; i < newidx; ++i) {
1299.         tmprect.lx = tmprect.hx = xcoords[i];
1300.         tmprect.ly = tmprect.hy = ycoords[i];
1301.         add_rect_to_reg(cloud, &tmprect);
1302.     }
1303.     cloud->ttl = rn1(3, 4);
1304.     /* If cloud was constrained in small space, give it more time to live. */
1305.     cloud->ttl = (cloud->ttl * cloudsize) / newidx;
1306. 
1307.     make_gas_cloud(cloud, damage, inside_cloud);
1308.     return cloud;
1309. }
1310. 
1311. /* create a single gas cloud from selection */
1312. NhRegion *
1313. create_gas_cloud_selection(
1314.     struct selectionvar *sel,
1315.     int damage)
1316. {
1317.     NhRegion *cloud;
1318.     NhRect tmprect;
1319.     coordxy x, y;
1320.     NhRect r = cg.zeroNhRect;
1321.     boolean inside_cloud = is_hero_inside_gas_cloud();
1322. 
1323.     selection_getbounds(sel, &r);
1324. 
1325.     cloud = create_region((NhRect *) 0, 0);
1326.     for (x = r.lx; x <= r.hx; x++)
1327.         for (y = r.ly; y <= r.hy; y++)
1328.             if (selection_getpoint(x, y, sel)) {
1329.                 tmprect.lx = tmprect.hx = x;
1330.                 tmprect.ly = tmprect.hy = y;
1331.                 add_rect_to_reg(cloud, &tmprect);
1332.             }
1333. 
1334.     make_gas_cloud(cloud, damage, inside_cloud);
1335.     return cloud;
1336. }
1337. 
1338. 
1339. /* for checking troubles during prayer; is hero at risk? */
1340. boolean
1341. region_danger(void)
1342. {
1343.     int i, f_indx, n = 0;
1344. 
1345.     for (i = 0; i < svn.n_regions; i++) {
1346.         /* only care about regions that hero is in */
1347.         if (!hero_inside(gr.regions[i]))
1348.             continue;
1349.         f_indx = gr.regions[i]->inside_f;
1350.         /* the only type of region we understand is gas_cloud */
1351.         if (f_indx == INSIDE_GAS_CLOUD) {
1352.             /* completely harmless if you don't need to breathe */
1353.             if (nonliving(gy.youmonst.data) || Breathless)
1354.                 continue;
1355.             /* minor inconvenience if you're poison resistant;
1356.                not harmful enough to be a prayer-level trouble */
1357.             if (Poison_resistance)
1358.                 continue;
1359.             ++n;
1360.         }
1361.     }
1362.     return n ? TRUE : FALSE;
1363. }
1364. 
1365. /* for fixing trouble at end of prayer;
1366.    danger detected at start of prayer might have expired by now */
1367. void
1368. region_safety(void)
1369. {
1370.     NhRegion *r = 0;
1371.     int i, f_indx, n = 0;
1372. 
1373.     for (i = 0; i < svn.n_regions; i++) {
1374.         /* only care about regions that hero is in */
1375.         if (!hero_inside(gr.regions[i]))
1376.             continue;
1377.         f_indx = gr.regions[i]->inside_f;
1378.         /* the only type of region we understand is gas_cloud */
1379.         if (f_indx == INSIDE_GAS_CLOUD) {
1380.             if (!n++ && gr.regions[i]->ttl >= 0)
1381.                 r = gr.regions[i];
1382.         }
1383.     }
1384. 
1385.     if (n > 1 || (n == 1 && !r)) {
1386.         /* multiple overlapping cloud regions or non-expiring one */
1387.         (void) safe_teleds(TELEDS_NO_FLAGS);
1388.         /* maybe there's no safe place available; must get hero out of danger
1389.            or prayer's "fix all troubles" result will get stuck in a loop */
1390.         if (region_danger()) {
1391.             set_itimeout(&HMagical_breathing, (long) (d(4, 4) + 4));
1392.             /* not already Breathless or wouldn't be in region danger */
1393.             You_feel("able to breathe.");
1394.         }
1395.     } else if (r) {
1396.         remove_region(r);
1397.         pline_The("gas cloud enveloping you dissipates.");
1398.     } else {
1399.         /* cloud dissipated on its own, so nothing needs to be done */
1400.         pline_The("gas cloud has dissipated.");
1401.     }
1402.     /* maybe cure blindness too */
1403.     if (BlindedTimeout == 1L)
1404.         make_blinded(0L, TRUE);
1405. }
1406. #endif /* !SFCTOOL */
1407. 
1408. /*region.c*/