Source:NetHack 5.0.0/src/selvar.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to selvar.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/selvar.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	selvar.c	$NHDT-Date: 1769840272 2026/01/30 22:17:52 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.4 $ */
2. /* Copyright (c) 2024 by Pasi Kallinen */
3. /* NetHack may be freely redistributed.  See license for details. */
4. 
5. #include "hack.h"
6. #include "selvar.h"
7. #include "sp_lev.h"
8. 
9. staticfn boolean sel_flood_havepoint(coordxy, coordxy, coordxy *, coordxy *,
10.                                     int);
11. staticfn long line_dist_coord(long, long, long, long, long, long);
12. 
13. /* selection */
14. struct selectionvar *
15. selection_new(void)
16. {
17.     struct selectionvar *tmps = (struct selectionvar *) alloc(sizeof *tmps);
18. 
19.     tmps->wid = COLNO;
20.     tmps->hei = ROWNO;
21.     tmps->bounds_dirty = FALSE;
22.     tmps->bounds.lx = COLNO;
23.     tmps->bounds.ly = ROWNO;
24.     tmps->bounds.hx = tmps->bounds.hy = 0;
25.     tmps->map = (char *) alloc((COLNO * ROWNO) + 1);
26.     (void) memset(tmps->map, 1, (COLNO * ROWNO));
27.     tmps->map[(COLNO * ROWNO)] = '\0';
28. 
29.     return tmps;
30. }
31. 
32. void
33. selection_free(struct selectionvar *sel, boolean freesel)
34. {
35.     if (sel) {
36.         if (sel->map)
37.             free(sel->map);
38.         sel->map = NULL;
39.         if (freesel)
40.             free((genericptr_t) sel);
41.         else
42.             (void) memset((genericptr_t) sel, 0, sizeof *sel);
43.     }
44. }
45. 
46. /* clear selection, setting all locations to value val */
47. void
48. selection_clear(struct selectionvar *sel, int val)
49. {
50.     (void) memset(sel->map, 1 + val, (COLNO * ROWNO));
51.     if (val) {
52.         sel->bounds.lx = 0;
53.         sel->bounds.ly = 0;
54.         sel->bounds.hx = COLNO - 1;
55.         sel->bounds.hy = ROWNO - 1;
56.     } else {
57.         sel->bounds.lx = COLNO;
58.         sel->bounds.ly = ROWNO;
59.         sel->bounds.hx = sel->bounds.hy = 0;
60.     }
61.     sel->bounds_dirty = FALSE;
62. }
63. 
64. struct selectionvar *
65. selection_clone(struct selectionvar *sel)
66. {
67.     struct selectionvar *tmps = (struct selectionvar *) alloc(sizeof *tmps);
68. 
69.     *tmps = *sel;
70.     tmps->map = dupstr(sel->map);
71. 
72.     return tmps;
73. }
74. 
75. /* get boundary rect of selection sel into b */
76. void
77. selection_getbounds(struct selectionvar *sel, NhRect *b)
78. {
79.     if (!sel || !b)
80.         return;
81. 
82.     selection_recalc_bounds(sel);
83. 
84.     if (sel->bounds.lx >= sel->wid) {
85.         b->lx = 0;
86.         b->ly = 0;
87.         b->hx = COLNO - 1;
88.         b->hy = ROWNO - 1;
89.     } else {
90.         b->lx = sel->bounds.lx;
91.         b->ly = sel->bounds.ly;
92.         b->hx = sel->bounds.hx;
93.         b->hy = sel->bounds.hy;
94.     }
95. }
96. 
97. /* recalc the boundary of selection, if necessary */
98. void
99. selection_recalc_bounds(struct selectionvar *sel)
100. {
101.     coordxy x, y;
102.     NhRect r;
103. 
104.     if (!sel->bounds_dirty)
105.         return;
106. 
107.     sel->bounds.lx = COLNO;
108.     sel->bounds.ly = ROWNO;
109.     sel->bounds.hx = sel->bounds.hy = 0;
110. 
111.     r.lx = r.ly = r.hx = r.hy = -1;
112. 
113.     /* left */
114.     for (x = 0; x < sel->wid; x++) {
115.         for (y = 0; y < sel->hei; y++) {
116.             if (selection_getpoint(x, y, sel)) {
117.                 r.lx = x;
118.                 break;
119.             }
120.         }
121.         if (r.lx > -1)
122.             break;
123.     }
124. 
125.     if (r.lx > -1) {
126.         /* right */
127.         for (x = sel->wid-1; x >= r.lx; x--) {
128.             for (y = 0; y < sel->hei; y++) {
129.                 if (selection_getpoint(x, y, sel)) {
130.                     r.hx = x;
131.                     break;
132.                 }
133.             }
134.             if (r.hx > -1)
135.                 break;
136.         }
137. 
138.         /* top */
139.         for (y = 0; y < sel->hei; y++) {
140.             for (x = r.lx; x <= r.hx; x++) {
141.                 if (selection_getpoint(x, y, sel)) {
142.                     r.ly = y;
143.                     break;
144.                 }
145.             }
146.             if (r.ly > -1)
147.                 break;
148.         }
149. 
150.         /* bottom */
151.         for (y = sel->hei-1; y >= r.ly; y--) {
152.             for (x = r.lx; x <= r.hx; x++) {
153.                 if (selection_getpoint(x, y, sel)) {
154.                     r.hy = y;
155.                     break;
156.                 }
157.             }
158.             if (r.hy > -1)
159.                 break;
160.         }
161.         sel->bounds = r;
162.     }
163. 
164.     sel->bounds_dirty = FALSE;
165. }
166. 
167. coordxy
168. selection_getpoint(
169.     coordxy x, coordxy y,
170.     struct selectionvar *sel)
171. {
172.     if (!sel || !sel->map)
173.         return 0;
174.     if (x < 0 || y < 0 || x >= sel->wid || y >= sel->hei)
175.         return 0;
176. 
177.     return (sel->map[sel->wid * y + x] - 1);
178. }
179. 
180. void
181. selection_setpoint(
182.     coordxy x, coordxy y,
183.     struct selectionvar *sel,
184.     int c)
185. {
186.     if (!sel || !sel->map)
187.         return;
188.     if (x < 0 || y < 0 || x >= sel->wid || y >= sel->hei)
189.         return;
190. 
191.     if (c && !sel->bounds_dirty) {
192.         if (sel->bounds.lx > x)
193.             sel->bounds.lx = x;
194.         if (sel->bounds.ly > y)
195.             sel->bounds.ly = y;
196.         if (sel->bounds.hx < x)
197.             sel->bounds.hx = x;
198.         if (sel->bounds.hy < y)
199.             sel->bounds.hy = y;
200. 
201.     /* only set bounds_dirty if changing a point from 1 to 0; if changing
202.        a point from 0 to 0, nothing has really changed with the bounds */
203.     } else if (sel->map[sel->wid * y + x] != 0) {
204.         sel->bounds_dirty = TRUE;
205.     }
206. 
207.     sel->map[sel->wid * y + x] = (char) (c + 1);
208. }
209. 
210. struct selectionvar *
211. selection_not(struct selectionvar *s)
212. {
213.     int x, y;
214.     NhRect tmprect = cg.zeroNhRect;
215. 
216.     for (x = 0; x < s->wid; x++)
217.         for (y = 0; y < s->hei; y++)
218.             selection_setpoint(x, y, s, selection_getpoint(x, y, s) ? 0 : 1);
219.     selection_getbounds(s, &tmprect);
220.     return s;
221. }
222. 
223. struct selectionvar *
224. selection_filter_percent(
225.     struct selectionvar *ov,
226.     int percent)
227. {
228.     int x, y;
229.     struct selectionvar *ret;
230.     NhRect rect = cg.zeroNhRect;
231. 
232.     if (!ov)
233.         return NULL;
234. 
235.     ret = selection_new();
236. 
237.     selection_getbounds(ov, &rect);
238. 
239.     for (x = rect.lx; x <= rect.hx; x++)
240.         for (y = rect.ly; y <= rect.hy; y++)
241.             if (selection_getpoint(x, y, ov) && (rn2(100) < percent))
242.                 selection_setpoint(x, y, ret, 1);
243. 
244.     return ret;
245. }
246. 
247. struct selectionvar *
248. selection_filter_mapchar(struct selectionvar *ov,  xint16 typ, int lit)
249. {
250.     int x, y;
251.     struct selectionvar *ret;
252.     NhRect rect = cg.zeroNhRect;
253. 
254.     if (!ov)
255.         return NULL;
256. 
257.     ret = selection_new();
258. 
259.     selection_getbounds(ov, &rect);
260. 
261.     for (x = rect.lx; x <= rect.hx; x++)
262.         for (y = rect.ly; y <= rect.hy; y++)
263.             if (selection_getpoint(x, y, ov)
264.                 && match_maptyps(typ, levl[x][y].typ)) {
265.                 switch (lit) {
266.                 default:
267.                 case -2:
268.                     selection_setpoint(x, y, ret, 1);
269.                     break;
270.                 case -1:
271.                     selection_setpoint(x, y, ret, rn2(2));
272.                     break;
273.                 case 0:
274.                 case 1:
275.                     if (levl[x][y].lit == (unsigned int) lit)
276.                         selection_setpoint(x, y, ret, 1);
277.                     break;
278.                 }
279.             }
280.     return ret;
281. }
282. 
283. int
284. selection_rndcoord(
285.     struct selectionvar *ov,
286.     coordxy *x, coordxy *y,
287.     boolean removeit)
288. {
289.     int idx = 0;
290.     int c;
291.     int dx, dy;
292.     NhRect rect = cg.zeroNhRect;
293. 
294.     selection_getbounds(ov, &rect);
295. 
296.     for (dx = rect.lx; dx <= rect.hx; dx++)
297.         for (dy = rect.ly; dy <= rect.hy; dy++)
298.             if (selection_getpoint(dx, dy, ov))
299.                 idx++;
300. 
301.     if (idx) {
302.         c = rn2(idx);
303.         for (dx = rect.lx; dx <= rect.hx; dx++)
304.             for (dy = rect.ly; dy <= rect.hy; dy++)
305.                 if (selection_getpoint(dx, dy, ov)) {
306.                     if (!c) {
307.                         *x = dx;
308.                         *y = dy;
309.                         if (removeit)
310.                             selection_setpoint(dx, dy, ov, 0);
311.                         return 1;
312.                     }
313.                     c--;
314.                 }
315.     }
316.     *x = *y = -1;
317.     return 0;
318. }
319. 
320. void
321. selection_do_grow(struct selectionvar *ov, int dir)
322. {
323.     coordxy x, y;
324.     struct selectionvar *tmp;
325.     NhRect rect = cg.zeroNhRect;
326. 
327.     if (!ov)
328.         return;
329. 
330.     tmp = selection_new();
331. 
332.     if (dir == W_RANDOM)
333.         dir = random_wdir();
334. 
335.     selection_getbounds(ov, &rect);
336. 
337.     for (x = max(0, rect.lx-1); x <= min(COLNO-1, rect.hx+1); x++)
338.         for (y = max(0, rect.ly-1); y <= min(ROWNO-1, rect.hy+1); y++) {
339.             /* note:  dir is a mask of multiple directions, but the only
340.                way to specify diagonals is by including the two adjacent
341.                orthogonal directions, which effectively specifies three-
342.                way growth [WEST|NORTH => WEST plus WEST|NORTH plus NORTH] */
343.             if (((dir & W_WEST) && selection_getpoint(x + 1, y, ov))
344.                 || (((dir & (W_WEST | W_NORTH)) == (W_WEST | W_NORTH))
345.                     && selection_getpoint(x + 1, y + 1, ov))
346.                 || ((dir & W_NORTH) && selection_getpoint(x, y + 1, ov))
347.                 || (((dir & (W_NORTH | W_EAST)) == (W_NORTH | W_EAST))
348.                     && selection_getpoint(x - 1, y + 1, ov))
349.                 || ((dir & W_EAST) && selection_getpoint(x - 1, y, ov))
350.                 || (((dir & (W_EAST | W_SOUTH)) == (W_EAST | W_SOUTH))
351.                     && selection_getpoint(x - 1, y - 1, ov))
352.                 || ((dir & W_SOUTH) && selection_getpoint(x, y - 1, ov))
353.                 ||  (((dir & (W_SOUTH | W_WEST)) == (W_SOUTH | W_WEST))
354.                      && selection_getpoint(x + 1, y - 1, ov))) {
355.                 selection_setpoint(x, y, tmp, 1);
356.             }
357.         }
358. 
359.     selection_getbounds(tmp, &rect);
360. 
361.     for (x = rect.lx; x <= rect.hx; x++)
362.         for (y = rect.ly; y <= rect.hy; y++)
363.             if (selection_getpoint(x, y, tmp))
364.                 selection_setpoint(x, y, ov, 1);
365. 
366.     selection_free(tmp, TRUE);
367. }
368. 
369. staticfn int (*selection_flood_check_func)(coordxy, coordxy);
370. 
371. void
372. set_selection_floodfillchk(int (*f)(coordxy, coordxy))
373. {
374.     selection_flood_check_func = f;
375. }
376. 
377. /* check whethere <x,y> is already in xs[],ys[] */
378. staticfn boolean
379. sel_flood_havepoint(
380.     coordxy x, coordxy y,
381.     coordxy xs[], coordxy ys[],
382.     int n)
383. {
384.     coordxy xx = x, yy = y;
385. 
386.     while (n > 0) {
387.         --n;
388.         if (xs[n] == xx && ys[n] == yy)
389.             return TRUE;
390.     }
391.     return FALSE;
392. }
393. 
394. void
395. selection_floodfill(
396.     struct selectionvar *ov,
397.     coordxy x, coordxy y,
398.     boolean diagonals)
399. {
400.     struct selectionvar *tmp = selection_new();
401. #define SEL_FLOOD_STACK (COLNO * ROWNO)
402. #define SEL_FLOOD(nx, ny) \
403.     do {                                      \
404.         if (idx < SEL_FLOOD_STACK) {          \
405.             dx[idx] = (nx);                   \
406.             dy[idx] = (ny);                   \
407.             idx++;                            \
408.         } else                                \
409.             panic(floodfill_stack_overrun);   \
410.     } while (0)
411. #define SEL_FLOOD_CHKDIR(mx, my, sel) \
412.     do {                                                        \
413.         if (isok((mx), (my))                                    \
414.             && (*selection_flood_check_func)((mx), (my))        \
415.             && !selection_getpoint((mx), (my), (sel))           \
416.             && !sel_flood_havepoint((mx), (my), dx, dy, idx))   \
417.             SEL_FLOOD((mx), (my));                              \
418.     } while (0)
419.     static const char floodfill_stack_overrun[] = "floodfill stack overrun";
420.     int idx = 0;
421.     coordxy dx[SEL_FLOOD_STACK];
422.     coordxy dy[SEL_FLOOD_STACK];
423. 
424.     if (selection_flood_check_func == (int (*)(coordxy, coordxy)) 0) {
425.         selection_free(tmp, TRUE);
426.         return;
427.     }
428.     SEL_FLOOD(x, y);
429.     do {
430.         idx--;
431.         x = dx[idx];
432.         y = dy[idx];
433.         if (isok(x, y)) {
434.             selection_setpoint(x, y, ov, 1);
435.             selection_setpoint(x, y, tmp, 1);
436.         }
437.         SEL_FLOOD_CHKDIR((x + 1), y, tmp);
438.         SEL_FLOOD_CHKDIR((x - 1), y, tmp);
439.         SEL_FLOOD_CHKDIR(x, (y + 1), tmp);
440.         SEL_FLOOD_CHKDIR(x, (y - 1), tmp);
441.         if (diagonals) {
442.             SEL_FLOOD_CHKDIR((x + 1), (y + 1), tmp);
443.             SEL_FLOOD_CHKDIR((x - 1), (y - 1), tmp);
444.             SEL_FLOOD_CHKDIR((x - 1), (y + 1), tmp);
445.             SEL_FLOOD_CHKDIR((x + 1), (y - 1), tmp);
446.         }
447.     } while (idx > 0);
448. #undef SEL_FLOOD
449. #undef SEL_FLOOD_STACK
450. #undef SEL_FLOOD_CHKDIR
451.     selection_free(tmp, TRUE);
452. }
453. 
454. /* McIlroy's Ellipse Algorithm */
455. void
456. selection_do_ellipse(
457.     struct selectionvar *ov,
458.     int xc, int yc,
459.     int a, int b,
460.     int filled)
461. { /* e(x,y) = b^2*x^2 + a^2*y^2 - a^2*b^2 */
462.     int x = 0, y = b;
463.     long a2 = (long) a * a, b2 = (long) b * b;
464.     long crit1 = -(a2 / 4 + a % 2 + b2);
465.     long crit2 = -(b2 / 4 + b % 2 + a2);
466.     long crit3 = -(b2 / 4 + b % 2);
467.     long t = -a2 * y; /* e(x+1/2,y-1/2) - (a^2+b^2)/4 */
468.     long dxt = 2 * b2 * x, dyt = -2 * a2 * y;
469.     long d2xt = 2 * b2, d2yt = 2 * a2;
470.     long width = 1;
471.     long i;
472. 
473.     if (!ov)
474.         return;
475. 
476.     filled = !filled;
477. 
478.     if (!filled) {
479.         while (y >= 0 && x <= a) {
480.             selection_setpoint(xc + x, yc + y, ov, 1);
481.             if (x != 0 || y != 0)
482.                 selection_setpoint(xc - x, yc - y, ov, 1);
483.             if (x != 0 && y != 0) {
484.                 selection_setpoint(xc + x, yc - y, ov, 1);
485.                 selection_setpoint(xc - x, yc + y, ov, 1);
486.             }
487.             if (t + b2 * x <= crit1       /* e(x+1,y-1/2) <= 0 */
488.                 || t + a2 * y <= crit3) { /* e(x+1/2,y) <= 0 */
489.                 x++;
490.                 dxt += d2xt;
491.                 t += dxt;
492.             } else if (t - a2 * y > crit2) { /* e(x+1/2,y-1) > 0 */
493.                 y--;
494.                 dyt += d2yt;
495.                 t += dyt;
496.             } else {
497.                 x++;
498.                 dxt += d2xt;
499.                 t += dxt;
500.                 y--;
501.                 dyt += d2yt;
502.                 t += dyt;
503.             }
504.         }
505.     } else {
506.         while (y >= 0 && x <= a) {
507.             if (t + b2 * x <= crit1       /* e(x+1,y-1/2) <= 0 */
508.                 || t + a2 * y <= crit3) { /* e(x+1/2,y) <= 0 */
509.                 x++;
510.                 dxt += d2xt;
511.                 t += dxt;
512.                 width += 2;
513.             } else if (t - a2 * y > crit2) { /* e(x+1/2,y-1) > 0 */
514.                 for (i = 0; i < width; i++)
515.                     selection_setpoint(xc - x + i, yc - y, ov, 1);
516.                 if (y != 0)
517.                     for (i = 0; i < width; i++)
518.                         selection_setpoint(xc - x + i, yc + y, ov, 1);
519.                 y--;
520.                 dyt += d2yt;
521.                 t += dyt;
522.             } else {
523.                 for (i = 0; i < width; i++)
524.                     selection_setpoint(xc - x + i, yc - y, ov, 1);
525.                 if (y != 0)
526.                     for (i = 0; i < width; i++)
527.                         selection_setpoint(xc - x + i, yc + y, ov, 1);
528.                 x++;
529.                 dxt += d2xt;
530.                 t += dxt;
531.                 y--;
532.                 dyt += d2yt;
533.                 t += dyt;
534.                 width += 2;
535.             }
536.         }
537.     }
538. }
539. 
540. /* square of distance from line segment (x1,y1, x2,y2) to point (x3,y3) */
541. staticfn long
542. line_dist_coord(long x1, long y1, long x2, long y2, long x3, long y3)
543. {
544.     long px = x2 - x1;
545.     long py = y2 - y1;
546.     long s = px * px + py * py;
547.     long x, y, dx, dy, distsq = 0;
548.     float lu = 0;
549. 
550.     if (x1 == x2 && y1 == y2)
551.         return dist2(x1, y1, x3, y3);
552. 
553.     lu = ((x3 - x1) * px + (y3 - y1) * py) / (float) s;
554.     if (lu > 1)
555.         lu = 1;
556.     else if (lu < 0)
557.         lu = 0;
558. 
559.     x = x1 + lu * px;
560.     y = y1 + lu * py;
561.     dx = x - x3;
562.     dy = y - y3;
563.     distsq = dx * dx + dy * dy;
564. 
565.     return distsq;
566. }
567. 
568. /* guts of l_selection_gradient */
569. void
570. selection_do_gradient(
571.     struct selectionvar *ov,
572.     long x, long y,
573.     long x2,long y2,
574.     long gtyp,
575.     long mind, long maxd)
576. {
577.     long dx, dy, dofs;
578. 
579.     if (mind > maxd) {
580.         long tmp = mind;
581.         mind = maxd;
582.         maxd = tmp;
583.     }
584. 
585.     dofs = maxd * maxd - mind * mind;
586.     if (dofs < 1)
587.         dofs = 1;
588. 
589.     switch (gtyp) {
590.     default:
591.         impossible("Unrecognized gradient type! Defaulting to radial...");
592.         FALLTHROUGH;
593.         /* FALLTHRU */
594.     case SEL_GRADIENT_RADIAL: {
595.         for (dx = 0; dx < COLNO; dx++)
596.             for (dy = 0; dy < ROWNO; dy++) {
597.                 long d0 = line_dist_coord(x, y, x2, y2, dx, dy);
598. 
599.                 if (d0 <= mind * mind
600.                     || (d0 <= maxd * maxd && d0 - mind * mind < rn2(dofs)))
601.                     selection_setpoint(dx, dy, ov, 1);
602.             }
603.         break;
604.     }
605.     case SEL_GRADIENT_SQUARE: {
606.         for (dx = 0; dx < COLNO; dx++)
607.             for (dy = 0; dy < ROWNO; dy++) {
608.                 long d1 = line_dist_coord(x, y, x2, y2, x, dy);
609.                 long d2 = line_dist_coord(x, y, x2, y2, dx, y);
610.                 long d3 = line_dist_coord(x, y, x2, y2, x2, dy);
611.                 long d4 = line_dist_coord(x, y, x2, y2, dx, y2);
612.                 long d5 = line_dist_coord(x, y, x2, y2, dx, dy);
613.                 long d0 = min(d5, min(max(d1, d2), max(d3, d4)));
614. 
615.                 if (d0 <= mind * mind
616.                     || (d0 <= maxd * maxd && d0 - mind * mind < rn2(dofs)))
617.                     selection_setpoint(dx, dy, ov, 1);
618.             }
619.         break;
620.     } /*case*/
621.     } /*switch*/
622. }
623. 
624. /* bresenham line algo */
625. void
626. selection_do_line(
627.     coordxy x1, coordxy y1,
628.     coordxy x2, coordxy y2,
629.     struct selectionvar *ov)
630. {
631.     int d0, dx, dy, ai, bi, xi, yi;
632. 
633.     if (x1 < x2) {
634.         xi = 1;
635.         dx = x2 - x1;
636.     } else {
637.         xi = -1;
638.         dx = x1 - x2;
639.     }
640.     if (y1 < y2) {
641.         yi = 1;
642.         dy = y2 - y1;
643.     } else {
644.         yi = -1;
645.         dy = y1 - y2;
646.     }
647. 
648.     selection_setpoint(x1, y1, ov, 1);
649. 
650.     if (!dx && !dy) {
651.         /* single point - already all done */
652.         ;
653.     } else if (dx > dy) {
654.         ai = (dy - dx) * 2;
655.         bi = dy * 2;
656.         d0 = bi - dx;
657.         do {
658.             if (d0 >= 0) {
659.                 y1 += yi;
660.                 d0 += ai;
661.             } else
662.                 d0 += bi;
663.             x1 += xi;
664.             selection_setpoint(x1, y1, ov, 1);
665.         } while (x1 != x2);
666.     } else {
667.         ai = (dx - dy) * 2;
668.         bi = dx * 2;
669.         d0 = bi - dy;
670.         do {
671.             if (d0 >= 0) {
672.                 x1 += xi;
673.                 d0 += ai;
674.             } else
675.                 d0 += bi;
676.             y1 += yi;
677.             selection_setpoint(x1, y1, ov, 1);
678.         } while (y1 != y2);
679.     }
680. }
681. 
682. void
683. selection_do_randline(
684.     coordxy x1, coordxy y1,
685.     coordxy x2, coordxy y2,
686.     schar rough,
687.     schar rec,
688.     struct selectionvar *ov)
689. {
690.     int mx, my;
691.     int dx, dy;
692. 
693.     if (rec < 1 || (x2 == x1 && y2 == y1))
694.         return;
695. 
696.     if (rough > max(abs(x2 - x1), abs(y2 - y1)))
697.         rough = max(abs(x2 - x1), abs(y2 - y1));
698. 
699.     if (rough < 2) {
700.         mx = ((x1 + x2) / 2);
701.         my = ((y1 + y2) / 2);
702.     } else {
703.         do {
704.             dx = rn2(rough) - (rough / 2);
705.             dy = rn2(rough) - (rough / 2);
706.             mx = ((x1 + x2) / 2) + dx;
707.             my = ((y1 + y2) / 2) + dy;
708.         } while ((mx > COLNO - 1 || mx < 0 || my < 0 || my > ROWNO - 1));
709.     }
710. 
711.     if (!selection_getpoint(mx, my, ov)) {
712.         selection_setpoint(mx, my, ov, 1);
713.     }
714. 
715.     rough = (rough * 2) / 3;
716. 
717.     rec--;
718. 
719.     selection_do_randline(x1, y1, mx, my, rough, rec, ov);
720.     selection_do_randline(mx, my, x2, y2, rough, rec, ov);
721. 
722.     selection_setpoint(x2, y2, ov, 1);
723. }
724. 
725. void
726. selection_iterate(
727.     struct selectionvar *ov,
728.     select_iter_func func,
729.     genericptr_t arg)
730. {
731.     coordxy x, y;
732.     NhRect rect = cg.zeroNhRect;
733. 
734.     if (!ov)
735.         return;
736. 
737.     selection_getbounds(ov, &rect);
738. 
739.     for (x = rect.lx; x <= rect.hx; x++)
740.         for (y = rect.ly; y <= rect.hy; y++)
741.             if (isok(x,y) && selection_getpoint(x, y, ov))
742.                 (*func)(x, y, arg);
743. }
744. 
745. /* selection is not rectangular, or has holes in it */
746. boolean
747. selection_is_irregular(struct selectionvar *sel)
748. {
749.     coordxy x, y;
750.     NhRect rect = cg.zeroNhRect;
751. 
752.     selection_getbounds(sel, &rect);
753. 
754.     for (x = rect.lx; x <= rect.hx; x++)
755.         for (y = rect.ly; y <= rect.hy; y++)
756.             if (isok(x,y) && !selection_getpoint(x, y, sel))
757.                 return TRUE;
758. 
759.     return FALSE;
760. }
761. 
762. /* return a description of the selection size */
763. char *
764. selection_size_description(struct selectionvar *sel, char *buf)
765. {
766.     NhRect rect = cg.zeroNhRect;
767.     coordxy dx, dy;
768. 
769.     selection_getbounds(sel, &rect);
770.     dx = rect.hx - rect.lx + 1;
771.     dy = rect.hy - rect.ly + 1;
772.     Sprintf(buf, "%s %i by %i",
773.             selection_is_irregular(sel) ? "irregularly shaped"
774.             : (dx == dy) ? "square"
775.               : "rectangular",
776.             dx, dy);
777.     return buf;
778. }
779. 
780. struct selectionvar *
781. selection_from_mkroom(struct mkroom *croom)
782. {
783.     struct selectionvar *sel = selection_new();
784.     coordxy x, y;
785.     unsigned rmno;
786. 
787.     if (!croom && gc.coder && gc.coder->croom)
788.         croom = gc.coder->croom;
789.     if (!croom)
790.         return sel;
791. 
792.     rmno = (unsigned)((croom - svr.rooms) + ROOMOFFSET);
793.     for (y = croom->ly; y <= croom->hy; y++)
794.         for (x = croom->lx; x <= croom->hx; x++)
795.             if (isok(x, y) && !levl[x][y].edge
796.                 && levl[x][y].roomno == rmno)
797.                 selection_setpoint(x, y, sel, 1);
798.     return sel;
799. }
800. 
801. void
802. selection_force_newsyms(struct selectionvar *sel)
803. {
804.     coordxy x, y;
805. 
806.     for (x = 1; x < sel->wid; x++)
807.         for (y = 0; y < sel->hei; y++)
808.             if (selection_getpoint(x, y, sel))
809.                 newsym_force(x, y);
810. }
811. 
812. /*selvar.c*/