Source:NetHack 5.0.0/src/nhlsel.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to nhlsel.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/nhlsel.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	nhlua.c	$NHDT-Date: 1769840272 2026/01/30 22:17:52 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.69 $ */
2. /*      Copyright (c) 2018 by Pasi Kallinen */
3. /* NetHack may be freely redistributed.  See license for details. */
4. 
5. #include "hack.h"
6. #include "sp_lev.h"
7. 
8. 
9. struct selectionvar *l_selection_check(lua_State *, int);
10. staticfn struct selectionvar *l_selection_push_new(lua_State *);
11. 
12. /* lua_CFunction prototypes */
13. staticfn int l_selection_new(lua_State *);
14. staticfn int l_selection_clone(lua_State *);
15. staticfn int l_selection_numpoints(lua_State *);
16. staticfn int l_selection_getpoint(lua_State *);
17. staticfn int l_selection_setpoint(lua_State *);
18. staticfn int l_selection_filter_percent(lua_State *);
19. staticfn int l_selection_rndcoord(lua_State *);
20. staticfn int l_selection_room(lua_State *);
21. staticfn int l_selection_getbounds(lua_State *);
22. staticfn boolean params_sel_2coords(lua_State *, struct selectionvar **,
23.                                   coordxy *, coordxy *, coordxy *, coordxy *);
24. staticfn int l_selection_line(lua_State *);
25. staticfn int l_selection_randline(lua_State *);
26. staticfn int l_selection_rect(lua_State *);
27. staticfn int l_selection_fillrect(lua_State *);
28. staticfn int l_selection_grow(lua_State *);
29. staticfn int l_selection_filter_mapchar(lua_State *);
30. staticfn int l_selection_match(lua_State *);
31. staticfn int l_selection_flood(lua_State *);
32. staticfn int l_selection_circle(lua_State *);
33. staticfn int l_selection_ellipse(lua_State *);
34. staticfn int l_selection_gradient(lua_State *);
35. staticfn int l_selection_iterate(lua_State *);
36. staticfn int l_selection_size_description(lua_State *L);
37. staticfn int l_selection_gc(lua_State *);
38. staticfn int l_selection_not(lua_State *);
39. staticfn int l_selection_and(lua_State *);
40. staticfn int l_selection_or(lua_State *);
41. staticfn int l_selection_xor(lua_State *);
42. /* There doesn't seem to be a point in having a l_selection_add since it would
43.  * do the same thing as l_selection_or. The addition operator is mapped to
44.  * l_selection_or. */
45. staticfn int l_selection_sub(lua_State *);
46. #if 0
47. /* the following do not appear to currently be
48.    used and because they are static, the OSX
49.    compiler is complaining about them. I've
50.    if ifdef'd out the prototype here and the
51.    function body below.
52.  */
53. staticfn int l_selection_ipairs(lua_State *);
54. staticfn struct selectionvar *l_selection_to(lua_State *, int);
55. #endif
56. 
57. struct selectionvar *
58. l_selection_check(lua_State *L, int index)
59. {
60.     struct selectionvar *sel;
61. 
62.     luaL_checktype(L, index, LUA_TUSERDATA);
63.     sel = (struct selectionvar *) luaL_checkudata(L, index, "selection");
64.     if (!sel)
65.         nhl_error(L, "Selection error");
66.     return sel;
67. }
68. 
69. staticfn int
70. l_selection_gc(lua_State *L)
71. {
72.     struct selectionvar *sel = l_selection_check(L, 1);
73. 
74.     if (sel)
75.         selection_free(sel, FALSE);
76.     return 0;
77. }
78. 
79. #if 0
80. staticfn struct selectionvar *
81. l_selection_to(lua_State *L, int index)
82. {
83.     struct selectionvar *sel
84.                            = (struct selectionvar *) lua_touserdata(L, index);
85. 
86.     if (!sel)
87.         nhl_error(L, "Selection error");
88.     return sel;
89. }
90. #endif
91. 
92. /* push a new selection into lua stack, return the selectionvar */
93. staticfn struct selectionvar *
94. l_selection_push_new(lua_State *L)
95. {
96.     struct selectionvar *tmp = selection_new();
97.     struct selectionvar *sel
98.    = (struct selectionvar *) lua_newuserdata(L, sizeof (struct selectionvar));
99. 
100.     luaL_getmetatable(L, "selection");
101.     lua_setmetatable(L, -2);
102. 
103.     *sel = *tmp;
104.     sel->map = dupstr(tmp->map);
105.     selection_free(tmp, TRUE);
106. 
107.     return sel;
108. }
109. 
110. /* push a copy of selectionvar tmp to lua stack */
111. void
112. l_selection_push_copy(lua_State *L, struct selectionvar *tmp)
113. {
114.     struct selectionvar *sel
115.    = (struct selectionvar *) lua_newuserdata(L, sizeof (struct selectionvar));
116. 
117.     luaL_getmetatable(L, "selection");
118.     lua_setmetatable(L, -2);
119. 
120.     *sel = *tmp;
121.     sel->map = dupstr(tmp->map);
122. }
123. 
124. 
125. /* local sel = selection.new(); */
126. staticfn int
127. l_selection_new(lua_State *L)
128. {
129.     (void) l_selection_push_new(L);
130.     return 1;
131. }
132. 
133. /* Replace the topmost selection in the stack with a clone of it. */
134. /* local sel = selection.clone(sel); */
135. staticfn int
136. l_selection_clone(lua_State *L)
137. {
138.     struct selectionvar *sel = l_selection_check(L, 1);
139.     struct selectionvar *tmp;
140. 
141.     (void) l_selection_new(L);
142.     tmp = l_selection_check(L, 2);
143.     if (tmp->map)
144.         free(tmp->map);
145.     *tmp = *sel;
146.     tmp->map = dupstr(sel->map);
147.     return 1;
148. }
149. 
150. DISABLE_WARNING_UNREACHABLE_CODE
151. 
152. /* selection.set(sel, x, y); */
153. /* selection.set(sel, x, y, value); */
154. /* local sel = selection.set(); */
155. /* local sel = sel:set(); */
156. /* local sel = selection.set(sel); */
157. /* TODO: allow setting multiple coords at once: set({x1,y1},{x2,y2},...); */
158. staticfn int
159. l_selection_setpoint(lua_State *L)
160. {
161.     struct selectionvar *sel = (struct selectionvar *) 0;
162.     coordxy x = -1, y = -1;
163.     int val = 1;
164.     int argc = lua_gettop(L);
165.     long crd = 0L;
166. 
167.     if (argc == 0) {
168.         (void) l_selection_new(L);
169.     } else if (argc == 1) {
170.         sel = l_selection_check(L, 1);
171.     } else if (argc == 2) {
172.         x = (coordxy) luaL_checkinteger(L, 1);
173.         y = (coordxy) luaL_checkinteger(L, 2);
174.         lua_pop(L, 2);
175.         (void) l_selection_new(L);
176.         sel = l_selection_check(L, 1);
177.     } else {
178.         sel = l_selection_check(L, 1);
179.         x = (coordxy) luaL_checkinteger(L, 2);
180.         y = (coordxy) luaL_checkinteger(L, 3);
181.         val = (int) luaL_optinteger(L, 4, 1);
182.     }
183. 
184.     if (!sel || !sel->map) {
185.         nhl_error(L, "Selection setpoint error");
186.         /*NOTREACHED*/
187.         return 0;
188.     }
189. 
190.     if (x == -1 && y == -1)
191.         crd = SP_COORD_PACK_RANDOM(0);
192.     else
193.         crd = SP_COORD_PACK(x,y);
194.     get_location_coord(&x, &y, ANY_LOC,
195.                        gc.coder ? gc.coder->croom : NULL, crd);
196.     selection_setpoint(x, y, sel, val);
197.     lua_settop(L, 1);
198.     return 1;
199. }
200. 
201. /* local numpoints = selection.numpoints(sel); */
202. staticfn int
203. l_selection_numpoints(lua_State *L)
204. {
205.     struct selectionvar *sel = l_selection_check(L, 1);
206.     coordxy x, y;
207.     int ret = 0;
208.     NhRect rect = cg.zeroNhRect;
209. 
210.     selection_getbounds(sel, &rect);
211. 
212.     for (x = rect.lx; x <= rect.hx; x++)
213.         for (y = rect.ly; y <= rect.hy; y++)
214.             if (selection_getpoint(x, y, sel))
215.                 ret++;
216. 
217.     lua_settop(L, 0);
218.     lua_pushinteger(L, ret);
219.     return 1;
220. }
221. 
222. /* local value = selection.get(sel, x, y); */
223. staticfn int
224. l_selection_getpoint(lua_State *L)
225. {
226.     struct selectionvar *sel = l_selection_check(L, 1);
227.     coordxy x, y;
228.     lua_Integer ix, iy;
229.     int val;
230.     long crd;
231. 
232.     lua_remove(L, 1); /* sel */
233.     if (!nhl_get_xy_params(L, &ix, &iy)) {
234.         nhl_error(L, "l_selection_getpoint: Incorrect params");
235.         /*NOTREACHED*/
236.         return 0;
237.     }
238.     x = (coordxy) ix;
239.     y = (coordxy) iy;
240. 
241.     if (x == -1 && y == -1)
242.         crd = SP_COORD_PACK_RANDOM(0);
243.     else
244.         crd = SP_COORD_PACK(x,y);
245.     get_location_coord(&x, &y, ANY_LOC,
246.                        gc.coder ? gc.coder->croom : NULL, crd);
247. 
248.     val = selection_getpoint(x, y, sel);
249.     lua_settop(L, 0);
250.     lua_pushnumber(L, val);
251.     return 1;
252. }
253. 
254. RESTORE_WARNING_UNREACHABLE_CODE
255. 
256. /* local s = selection.negate(sel); */
257. /* local s = selection.negate(); */
258. /* local s = sel:negate(); */
259. staticfn int
260. l_selection_not(lua_State *L)
261. {
262.     int argc = lua_gettop(L);
263.     struct selectionvar *sel, *sel2;
264. 
265.     if (argc == 0) {
266.         (void) l_selection_new(L);
267.         sel = l_selection_check(L, 1);
268.         selection_clear(sel, 1);
269.     } else {
270.         (void) l_selection_check(L, 1);
271.         (void) l_selection_clone(L);
272.         sel2 = l_selection_check(L, 2);
273.         selection_not(sel2);
274.         lua_remove(L, 1);
275.     }
276.     return 1;
277. }
278. 
279. /* local sel = selection.area(4,5, 40,10) & selection.rect(7,8, 60,14); */
280. staticfn int
281. l_selection_and(lua_State *L)
282. {
283.     int x, y;
284.     struct selectionvar *sela = l_selection_check(L, 1);
285.     struct selectionvar *selb = l_selection_check(L, 2);
286.     struct selectionvar *selr = l_selection_push_new(L);
287.     NhRect rect = cg.zeroNhRect;
288. 
289.     rect_bounds(sela->bounds, selb->bounds, &rect);
290. 
291.     for (x = rect.lx; x <= rect.hx; x++)
292.         for (y = rect.ly; y <= rect.hy; y++) {
293.             int val = (selection_getpoint(x, y, sela)
294.                        & selection_getpoint(x, y, selb));
295. 
296.             selection_setpoint(x, y, selr, val);
297.         }
298. 
299.     lua_remove(L, 1);
300.     lua_remove(L, 1);
301.     return 1;
302. }
303. 
304. /* local sel = selection.area(4,5, 40,10) | selection.rect(7,8, 60,14); */
305. staticfn int
306. l_selection_or(lua_State *L)
307. {
308.     int x,y;
309.     struct selectionvar *sela = l_selection_check(L, 1);
310.     struct selectionvar *selb = l_selection_check(L, 2);
311.     struct selectionvar *selr = l_selection_push_new(L);
312.     NhRect rect = cg.zeroNhRect;
313. 
314.     rect_bounds(sela->bounds, selb->bounds, &rect);
315. 
316.     for (x = rect.lx; x <= rect.hx; x++)
317.         for (y = rect.ly; y <= rect.hy; y++) {
318.             int val = (selection_getpoint(x, y, sela)
319.                        | selection_getpoint(x, y, selb));
320. 
321.             selection_setpoint(x, y, selr, val);
322.         }
323.     selr->bounds = rect;
324. 
325.     lua_remove(L, 1);
326.     lua_remove(L, 1);
327.     return 1;
328. }
329. 
330. /* local sel = selection.area(4,5, 40,10) ~ selection.rect(7,8, 60,14); */
331. staticfn int
332. l_selection_xor(lua_State *L)
333. {
334.     int x,y;
335.     struct selectionvar *sela = l_selection_check(L, 1);
336.     struct selectionvar *selb = l_selection_check(L, 2);
337.     struct selectionvar *selr = l_selection_push_new(L);
338.     NhRect rect = cg.zeroNhRect;
339. 
340.     rect_bounds(sela->bounds, selb->bounds, &rect);
341. 
342.     for (x = rect.lx; x <= rect.hx; x++)
343.         for (y = rect.ly; y <= rect.hy; y++) {
344.             int val = (selection_getpoint(x, y, sela)
345.                        ^ selection_getpoint(x, y, selb));
346. 
347.             selection_setpoint(x, y, selr, val);
348.         }
349.     /* this may have created a smaller or irregular selection with
350.      * bounds_dirty set to true - update its boundaries */
351.     selection_recalc_bounds(selr);
352. 
353.     lua_remove(L, 1);
354.     lua_remove(L, 1);
355.     return 1;
356. }
357. 
358. /* local sel = selection.area(10,10, 20,20) - selection.area(14,14, 17,17)
359.  *   - i.e. points that are in A but not in B */
360. staticfn int
361. l_selection_sub(lua_State *L)
362. {
363.     int x,y;
364.     struct selectionvar *sela = l_selection_check(L, 1);
365.     struct selectionvar *selb = l_selection_check(L, 2);
366.     struct selectionvar *selr = l_selection_push_new(L);
367.     NhRect rect = cg.zeroNhRect;
368. 
369.     rect_bounds(sela->bounds, selb->bounds, &rect);
370. 
371.     for (x = rect.lx; x <= rect.hx; x++)
372.         for (y = rect.ly; y <= rect.hy; y++) {
373.             coordxy a_pt = selection_getpoint(x, y, sela);
374.             coordxy b_pt = selection_getpoint(x, y, selb);
375.             int val = (a_pt ^ b_pt) & a_pt;
376.             selection_setpoint(x, y, selr, val);
377.         }
378.     /* this may have created a smaller or irregular selection with
379.      * bounds_dirty set to true - update its boundaries */
380.     selection_recalc_bounds(selr);
381. 
382.     lua_remove(L, 1);
383.     lua_remove(L, 1);
384.     return 1;
385. }
386. 
387. /* local s = selection.percentage(sel, 50); */
388. staticfn int
389. l_selection_filter_percent(lua_State *L)
390. {
391.     int argc = lua_gettop(L);
392.     struct selectionvar *sel = l_selection_check(L, 1);
393.     int p = (int) luaL_checkinteger(L, 2);
394.     struct selectionvar *tmp;
395. 
396.     tmp = selection_filter_percent(sel, p);
397.     lua_pop(L, argc);
398.     l_selection_push_copy(L, tmp);
399.     selection_free(tmp, TRUE);
400. 
401.     return 1;
402. }
403. 
404. /* local pt = selection.rndcoord(sel); */
405. /* local pt = selection.rndcoord(sel, 1); */
406. staticfn int
407. l_selection_rndcoord(lua_State *L)
408. {
409.     struct selectionvar *sel = l_selection_check(L, 1);
410.     int removeit = (int) luaL_optinteger(L, 2, 0);
411.     coordxy x = -1, y = -1;
412.     selection_rndcoord(sel, &x, &y, removeit);
413.     if (!(x == -1 && y == -1)) {
414.         update_croom();
415.         if (gc.coder && gc.coder->croom) {
416.             x -= gc.coder->croom->lx;
417.             y -= gc.coder->croom->ly;
418.         } else {
419.             x -= gx.xstart;
420.             y -= gy.ystart;
421.         }
422.     }
423.     lua_settop(L, 0);
424.     lua_newtable(L);
425.     nhl_add_table_entry_int(L, "x", x);
426.     nhl_add_table_entry_int(L, "y", y);
427.     return 1;
428. }
429. 
430. /* local s = selection.room(); */
431. staticfn int
432. l_selection_room(lua_State *L)
433. {
434.     struct selectionvar *sel;
435.     int argc = lua_gettop(L);
436.     struct mkroom *croom = NULL;
437. 
438.     if (argc == 1) {
439.         int i = luaL_checkinteger(L, -1);
440. 
441.         croom = (i >= 0 && i < svn.nroom) ? &svr.rooms[i] : NULL;
442.     }
443. 
444.     sel = selection_from_mkroom(croom);
445. 
446.     l_selection_push_copy(L, sel);
447.     selection_free(sel, TRUE);
448. 
449.     return 1;
450. }
451. 
452. /* local rect = sel:bounds(); */
453. staticfn int
454. l_selection_getbounds(lua_State *L)
455. {
456.     struct selectionvar *sel = l_selection_check(L, 1);
457.     NhRect rect = cg.zeroNhRect;
458. 
459.     selection_getbounds(sel, &rect);
460.     lua_settop(L, 0);
461.     lua_newtable(L);
462.     nhl_add_table_entry_int(L, "lx", rect.lx);
463.     nhl_add_table_entry_int(L, "ly", rect.ly);
464.     nhl_add_table_entry_int(L, "hx", rect.hx);
465.     nhl_add_table_entry_int(L, "hy", rect.hy);
466.     return 1;
467. }
468. 
469. /* internal function to get a selection and 4 integer values from lua stack.
470.    removes the integers from the stack.
471.    returns TRUE if params are good.
472. */
473. /* function(selection, x1,y1, x2,y2) */
474. /* selection:function(x1,y1, x2,y2) */
475. staticfn boolean
476. params_sel_2coords(lua_State *L, struct selectionvar **sel,
477.                    coordxy *x1, coordxy *y1, coordxy *x2, coordxy *y2)
478. {
479.     int argc = lua_gettop(L);
480. 
481.     if (argc == 4) {
482.         (void) l_selection_new(L);
483.         *x1 = (coordxy) luaL_checkinteger(L, 1);
484.         *y1 = (coordxy) luaL_checkinteger(L, 2);
485.         *x2 = (coordxy) luaL_checkinteger(L, 3);
486.         *y2 = (coordxy) luaL_checkinteger(L, 4);
487.         *sel = l_selection_check(L, 5);
488.         lua_remove(L, 1);
489.         lua_remove(L, 1);
490.         lua_remove(L, 1);
491.         lua_remove(L, 1);
492.         return TRUE;
493.     } else if (argc == 5) {
494.         *sel = l_selection_check(L, 1);
495.         *x1 = (coordxy) luaL_checkinteger(L, 2);
496.         *y1 = (coordxy) luaL_checkinteger(L, 3);
497.         *x2 = (coordxy) luaL_checkinteger(L, 4);
498.         *y2 = (coordxy) luaL_checkinteger(L, 5);
499.         lua_pop(L, 4);
500.         return TRUE;
501.     }
502.     return FALSE;
503. }
504. 
505. /* local s = selection.line(sel, x1,y1, x2,y2); */
506. /* local s = selection.line(x1,y1, x2,y2); */
507. /* s:line(x1,y1, x2,y2); */
508. staticfn int
509. l_selection_line(lua_State *L)
510. {
511.     struct selectionvar *sel = NULL;
512.     coordxy x1, y1, x2, y2;
513. 
514.     if (!params_sel_2coords(L, &sel, &x1, &y1, &x2, &y2)) {
515.         nhl_error(L, "selection.line: illegal arguments");
516.     }
517. 
518.     get_location_coord(&x1, &y1, ANY_LOC, gc.coder ? gc.coder->croom : NULL,
519.                        SP_COORD_PACK(x1, y1));
520.     get_location_coord(&x2, &y2, ANY_LOC, gc.coder ? gc.coder->croom : NULL,
521.                        SP_COORD_PACK(x2, y2));
522. 
523.     (void) l_selection_clone(L);
524.     sel = l_selection_check(L, 2);
525.     selection_do_line(x1,y1,x2,y2, sel);
526.     return 1;
527. }
528. 
529. /* local s = selection.rect(sel, x1,y1, x2,y2); */
530. staticfn int
531. l_selection_rect(lua_State *L)
532. {
533.     struct selectionvar *sel = NULL;
534.     coordxy x1, y1, x2, y2;
535. 
536.     if (!params_sel_2coords(L, &sel, &x1, &y1, &x2, &y2)) {
537.         nhl_error(L, "selection.rect: illegal arguments");
538.     }
539. 
540.     get_location_coord(&x1, &y1, ANY_LOC, gc.coder ? gc.coder->croom : NULL,
541.                        SP_COORD_PACK(x1, y1));
542.     get_location_coord(&x2, &y2, ANY_LOC, gc.coder ? gc.coder->croom : NULL,
543.                        SP_COORD_PACK(x2, y2));
544. 
545.     (void) l_selection_clone(L);
546.     sel = l_selection_check(L, 2);
547.     selection_do_line(x1, y1, x2, y1, sel);
548.     selection_do_line(x1, y1, x1, y2, sel);
549.     selection_do_line(x2, y1, x2, y2, sel);
550.     selection_do_line(x1, y2, x2, y2, sel);
551.     return 1;
552. }
553. 
554. /* local s = selection.fillrect(sel, x1,y1, x2,y2); */
555. /* local s = selection.fillrect(x1,y1, x2,y2); */
556. /* s:fillrect(x1,y1, x2,y2); */
557. /* selection.area(x1,y1, x2,y2); */
558. staticfn int
559. l_selection_fillrect(lua_State *L)
560. {
561.     struct selectionvar *sel = NULL;
562.     int y;
563.     coordxy x1, y1, x2, y2;
564. 
565.     if (!params_sel_2coords(L, &sel, &x1, &y1, &x2, &y2)) {
566.         nhl_error(L, "selection.fillrect: illegal arguments");
567.     }
568. 
569.     get_location_coord(&x1, &y1, ANY_LOC, gc.coder ? gc.coder->croom : NULL,
570.                        SP_COORD_PACK(x1, y1));
571.     get_location_coord(&x2, &y2, ANY_LOC, gc.coder ? gc.coder->croom : NULL,
572.                        SP_COORD_PACK(x2, y2));
573. 
574.     (void) l_selection_clone(L);
575.     sel = l_selection_check(L, 2);
576.     if (x1 == x2) {
577.         for (y = y1; y <= y2; y++)
578.             selection_setpoint(x1, y, sel, 1);
579.     } else {
580.         for (y = y1; y <= y2; y++)
581.             selection_do_line(x1, y, x2, y, sel);
582.     }
583.     return 1;
584. }
585. 
586. /* local s = selection.randline(sel, x1,y1, x2,y2, roughness); */
587. /* local s = selection.randline(x1,y1, x2,y2, roughness); */
588. /* TODO: selection.randline(x1,y1, x2,y2, roughness); */
589. /* TODO: selection.randline({x1,y1}, {x2,y2}, roughness); */
590. staticfn int
591. l_selection_randline(lua_State *L)
592. {
593.     int argc = lua_gettop(L);
594.     struct selectionvar *sel;
595.     coordxy x1 = 0, y1 = 0, x2 = 0, y2 = 0;
596.     int roughness = 7;
597. 
598.     if (argc == 6) {
599.         (void) l_selection_check(L, 1);
600.         x1 = (coordxy) luaL_checkinteger(L, 2);
601.         y1 = (coordxy) luaL_checkinteger(L, 3);
602.         x2 = (coordxy) luaL_checkinteger(L, 4);
603.         y2 = (coordxy) luaL_checkinteger(L, 5);
604.         roughness = (int) luaL_checkinteger(L, 6);
605.         lua_pop(L, 5);
606.     } else if (argc == 5 && lua_type(L, 1) == LUA_TNUMBER) {
607.         x1 = (coordxy) luaL_checkinteger(L, 1);
608.         y1 = (coordxy) luaL_checkinteger(L, 2);
609.         x2 = (coordxy) luaL_checkinteger(L, 3);
610.         y2 = (coordxy) luaL_checkinteger(L, 4);
611.         roughness = (int) luaL_checkinteger(L, 5);
612.         lua_pop(L, 5);
613.         (void) l_selection_new(L);
614.         (void) l_selection_check(L, 1);
615.     }
616. 
617.     get_location_coord(&x1, &y1, ANY_LOC, gc.coder ? gc.coder->croom : NULL,
618.                        SP_COORD_PACK(x1, y1));
619.     get_location_coord(&x2, &y2, ANY_LOC, gc.coder ? gc.coder->croom : NULL,
620.                        SP_COORD_PACK(x2, y2));
621. 
622.     (void) l_selection_clone(L);
623.     sel = l_selection_check(L, 2);
624.     selection_do_randline(x1, y1, x2, y2, roughness, 12, sel);
625.     return 1;
626. }
627. 
628. /* local s = selection.grow(sel); */
629. /* local s = selection.grow(sel, "north"); */
630. staticfn int
631. l_selection_grow(lua_State *L)
632. {
633.     static const char *const growdirs[] = {
634.         "all", "random", "north", "west", "east", "south", NULL
635.     };
636.     static const int growdirs2i[] = {
637.         W_ANY, W_RANDOM, W_NORTH, W_WEST, W_EAST, W_SOUTH, 0
638.     };
639.     struct selectionvar *sel;
640.     int dir, argc = lua_gettop(L);
641. 
642.     (void) l_selection_check(L, 1);
643.     dir = growdirs2i[luaL_checkoption(L, 2, "all", growdirs)];
644. 
645.     if (argc == 2)
646.         lua_pop(L, 1); /* get rid of growdir */
647. 
648.     (void) l_selection_clone(L);
649.     sel = l_selection_check(L, 2);
650.     selection_do_grow(sel, dir);
651.     return 1;
652. }
653. 
654. 
655. /* local s = selection.filter_mapchar(sel, mapchar, lit); */
656. staticfn int
657. l_selection_filter_mapchar(lua_State *L)
658. {
659.     int argc = lua_gettop(L);
660.     struct selectionvar *sel = l_selection_check(L, 1);
661.     char *mapchr = dupstr(luaL_checkstring(L, 2));
662.     coordxy typ = check_mapchr(mapchr);
663.     int lit = (int) luaL_optinteger(L, 3, -2); /* TODO: special lit values */
664.     struct selectionvar *tmp;
665. 
666.     if (typ == INVALID_TYPE)
667.         nhl_error(L, "Erroneous map char");
668. 
669.     tmp = selection_filter_mapchar(sel, typ, lit);
670.     lua_pop(L, argc);
671.     l_selection_push_copy(L, tmp);
672.     selection_free(tmp, TRUE);
673. 
674.     if (mapchr)
675.         free(mapchr);
676. 
677.     return 1;
678. }
679. 
680. /* local s = selection.match(...); */
681. staticfn int
682. l_selection_match(lua_State *L)
683. {
684.     int argc = lua_gettop(L);
685.     struct selectionvar *sel = (struct selectionvar *) 0;
686.     struct mapfragment *mf = (struct mapfragment *) 0;
687.     int x, y;
688. 
689.     if (argc == 1) {
690.         const char *err;
691.         char *mapstr = dupstr(luaL_checkstring(L, 1));
692.         lua_pop(L, 1);
693.         (void) l_selection_new(L);
694.         sel = l_selection_check(L, 1);
695. 
696.         mf = mapfrag_fromstr(mapstr);
697.         free(mapstr);
698. 
699.         if ((err = mapfrag_error(mf)) != NULL) {
700.             nhl_error(L, err);
701.             /*NOTREACHED*/
702.         }
703. 
704.     } else {
705.         nhl_error(L, "wrong parameters");
706.         /*NOTREACHED*/
707.     }
708. 
709.     for (y = 0; y <= sel->hei; y++)
710.         for (x = 1; x < sel->wid; x++)
711.             selection_setpoint(x, y, sel, mapfrag_match(mf, x,y) ? 1 : 0);
712. 
713.     /* unless the (0, 1) coordinate is a match, this would wind up with a
714.        selection with lx=COLNO, hx=0, etc, so fix the boundaries */
715.     selection_recalc_bounds(sel);
716. 
717.     mapfrag_free(&mf);
718. 
719.     return 1;
720. }
721. 
722. 
723. /* local s = selection.floodfill(x,y); */
724. /* local s = selection.floodfill(x,y, diagonals); */
725. staticfn int
726. l_selection_flood(lua_State *L)
727. {
728.     int argc = lua_gettop(L);
729.     struct selectionvar *sel = (struct selectionvar *) 0;
730.     coordxy x = 0, y = 0;
731.     boolean diagonals = FALSE;
732. 
733.     if (argc == 2 || argc == 3) {
734.         x = (coordxy) luaL_checkinteger(L, 1);
735.         y = (coordxy) luaL_checkinteger(L, 2);
736.         if (argc == 3)
737.             diagonals = lua_toboolean(L, 3);
738.         lua_pop(L, argc);
739.         (void) l_selection_new(L);
740.         sel = l_selection_check(L, 1);
741.     } else {
742.         nhl_error(L, "wrong parameters");
743.         /*NOTREACHED*/
744.     }
745. 
746.     get_location_coord(&x, &y, ANY_LOC, gc.coder ? gc.coder->croom : NULL,
747.                        SP_COORD_PACK(x, y));
748. 
749.     if (isok(x, y)) {
750.         set_floodfillchk_match_under(levl[x][y].typ);
751.         selection_floodfill(sel, x, y, diagonals);
752.     }
753.     return 1;
754. }
755. 
756. 
757. /* local s = selection.circle(x,y, radius); */
758. /* local s = selection.circle(x, y, radius, filled); */
759. /* local s = selection.circle(sel, x, y, radius); */
760. /* local s = selection.circle(sel, x, y, radius, filled); */
761. staticfn int
762. l_selection_circle(lua_State *L)
763. {
764.     int argc = lua_gettop(L);
765.     struct selectionvar *sel = (struct selectionvar *) 0;
766.     coordxy x = 0, y = 0;
767.     int r = 0, filled = 0;
768. 
769.     if (argc == 3) {
770.         x = (coordxy) luaL_checkinteger(L, 1);
771.         y = (coordxy) luaL_checkinteger(L, 2);
772.         r = (int) luaL_checkinteger(L, 3);
773.         lua_pop(L, 3);
774.         (void) l_selection_new(L);
775.         sel = l_selection_check(L, 1);
776.         filled = 0;
777.     } else if (argc == 4 && lua_type(L, 1) == LUA_TNUMBER) {
778.         x = (coordxy) luaL_checkinteger(L, 1);
779.         y = (coordxy) luaL_checkinteger(L, 2);
780.         r = (int) luaL_checkinteger(L, 3);
781.         filled = (int) luaL_checkinteger(L, 4); /* TODO: boolean*/
782.         lua_pop(L, 4);
783.         (void) l_selection_new(L);
784.         sel = l_selection_check(L, 1);
785.     } else if (argc == 4 || argc == 5) {
786.         sel = l_selection_check(L, 1);
787.         x = (coordxy) luaL_checkinteger(L, 2);
788.         y = (coordxy) luaL_checkinteger(L, 3);
789.         r = (int) luaL_checkinteger(L, 4);
790.         filled = (int) luaL_optinteger(L, 5, 0); /* TODO: boolean */
791.     } else {
792.         nhl_error(L, "wrong parameters");
793.         /*NOTREACHED*/
794.     }
795. 
796.     get_location_coord(&x, &y, ANY_LOC, gc.coder ? gc.coder->croom : NULL,
797.                        SP_COORD_PACK(x, y));
798. 
799.     selection_do_ellipse(sel, x, y, r, r, !filled);
800. 
801.     lua_settop(L, 1);
802.     return 1;
803. }
804. 
805. /* local s = selection.ellipse(x, y, radius1, radius2); */
806. /* local s = selection.ellipse(x, y, radius1, radius2, filled); */
807. /* local s = selection.ellipse(sel, x, y, radius1, radius2); */
808. /* local s = selection.ellipse(sel, x, y, radius1, radius2, filled); */
809. staticfn int
810. l_selection_ellipse(lua_State *L)
811. {
812.     int argc = lua_gettop(L);
813.     struct selectionvar *sel = (struct selectionvar *) 0;
814.     coordxy x = 0, y = 0;
815.     int r1 = 0, r2 = 0, filled = 0;
816. 
817.     if (argc == 4) {
818.         x = (coordxy) luaL_checkinteger(L, 1);
819.         y = (coordxy) luaL_checkinteger(L, 2);
820.         r1 = (int) luaL_checkinteger(L, 3);
821.         r2 = (int) luaL_checkinteger(L, 4);
822.         lua_pop(L, 4);
823.         (void) l_selection_new(L);
824.         sel = l_selection_check(L, 1);
825.         filled = 0;
826.     } else if (argc == 5 && lua_type(L, 1) == LUA_TNUMBER) {
827.         x = (coordxy) luaL_checkinteger(L, 1);
828.         y = (coordxy) luaL_checkinteger(L, 2);
829.         r1 = (int) luaL_checkinteger(L, 3);
830.         r2 = (int) luaL_checkinteger(L, 4);
831.         filled = (int) luaL_optinteger(L, 5, 0); /* TODO: boolean */
832.         lua_pop(L, 5);
833.         (void) l_selection_new(L);
834.         sel = l_selection_check(L, 1);
835.     } else if (argc == 5 || argc == 6) {
836.         sel = l_selection_check(L, 1);
837.         x = (coordxy) luaL_checkinteger(L, 2);
838.         y = (coordxy) luaL_checkinteger(L, 3);
839.         r1 = (int) luaL_checkinteger(L, 4);
840.         r2 = (int) luaL_checkinteger(L, 5);
841.         filled = (int) luaL_optinteger(L, 6, 0); /* TODO: boolean */
842.     } else {
843.         nhl_error(L, "wrong parameters");
844.         /*NOTREACHED*/
845.     }
846. 
847.     get_location_coord(&x, &y, ANY_LOC, gc.coder ? gc.coder->croom : NULL,
848.                        SP_COORD_PACK(x, y));
849. 
850.     selection_do_ellipse(sel, x, y, r1, r2, !filled);
851. 
852.     lua_settop(L, 1);
853.     return 1;
854. }
855. 
856. /* Gradients are versatile enough, with so many independently optional
857.  * arguments, that it doesn't seem helpful to provide a non-table form with
858.  * non-obvious argument order. */
859. /* selection.gradient({ type = "radial", x = 3, y = 5, x2 = 10, y2 = 12,
860.  *                      mindist = 4, maxdist = 10, limited = false });    */
861. staticfn int
862. l_selection_gradient(lua_State *L)
863. {
864.     int argc = lua_gettop(L);
865.     struct selectionvar *sel = (struct selectionvar *) 0;
866.     /* if x2 and y2 aren't set, the gradient has a single center point of x,y;
867.      * if they are set, the gradient is centered on a (x,y) to (x2,y2) line */
868.     coordxy x = 0, y = 0, x2 = -1, y2 = -1;
869.     /* points are always added within mindist of the center; the chance for a
870.      * point between mindist and maxdist to be added to the selection starts
871.      * at 100% at mindist and decreases linearly to 0% at maxdist */
872.     coordxy mindist = 0, maxdist = 0;
873.     long type = 0;
874.     static const char *const gradtypes[] = {
875.         "radial", "square", NULL
876.     };
877.     static const int gradtypes2i[] = {
878.         SEL_GRADIENT_RADIAL, SEL_GRADIENT_SQUARE, -1
879.     };
880. 
881.     if (argc == 1 && lua_type(L, 1) == LUA_TTABLE) {
882.         lcheck_param_table(L);
883.         type = gradtypes2i[get_table_option(L, "type", "radial", gradtypes)];
884.         x = (coordxy) get_table_int(L, "x");
885.         y = (coordxy) get_table_int(L, "y");
886.         x2 = (coordxy) get_table_int_opt(L, "x2", -1);
887.         y2 = (coordxy) get_table_int_opt(L, "y2", -1);
888.         cvt_to_abscoord(&x, &y);
889.         cvt_to_abscoord(&x2, &y2);
890.         /* maxdist is required because there's no obvious default value for
891.          * it, whereas mindist has an obvious default of 0 */
892.         maxdist = get_table_int(L, "maxdist");
893.         mindist = get_table_int_opt(L, "mindist", 0);
894. 
895.         lua_pop(L, 1);
896.         (void) l_selection_new(L);
897.         sel = l_selection_check(L, 1);
898.     } else {
899.         nhl_error(L, "selection.gradient requires table argument");
900.         /* NOTREACHED */
901.     }
902. 
903.     /* someone might conceivably want to draw a gradient somewhere off-map. So
904.      * the only coordinate that's "illegal" for that is (-1,-1).
905.      * If a level designer really needs to draw a gradient line using that
906.      * coordinate, they can do so by setting regular x and y to -1. */
907.     if (x2 == -1 && y2 == -1) {
908.         x2 = x;
909.         y2 = y;
910.     }
911. 
912.     selection_do_gradient(sel, x, y, x2, y2, type, mindist, maxdist);
913.     lua_settop(L, 1);
914.     return 1;
915. }
916. 
917. DISABLE_WARNING_UNREACHABLE_CODE
918. 
919. /* sel:iterate(function(x,y) ... end);
920.  * The x, y coordinates passed to the function are map- or room-relative
921.  * rather than absolute, unless there has been no previous map or room
922.  * defined.
923.  */
924. staticfn int
925. l_selection_iterate(lua_State *L)
926. {
927.     int argc = lua_gettop(L);
928.     struct selectionvar *sel = (struct selectionvar *) 0;
929.     int x, y;
930.     NhRect rect = cg.zeroNhRect;
931. 
932.     if (argc == 2 && lua_type(L, 2) == LUA_TFUNCTION) {
933.         sel = l_selection_check(L, 1);
934.         selection_getbounds(sel, &rect);
935.         for (y = rect.ly; y <= rect.hy; y++) {
936.             for (x = max(1,rect.lx); x <= rect.hx; x++)
937.                 if (selection_getpoint(x, y, sel)) {
938.                     coordxy tmpx = x, tmpy = y;
939.                     cvt_to_relcoord(&tmpx, &tmpy);
940.                     lua_pushvalue(L, 2);
941.                     lua_pushinteger(L, tmpx);
942.                     lua_pushinteger(L, tmpy);
943.                     if (nhl_pcall_handle(L, 2, 0, "l_selection_iterate",
944.                                          NHLpa_impossible)) {
945.                         /* abort loops to prevent possible error cascade */
946.                         goto out;
947.                     }
948.                 }
949.             lua_gc(L, LUA_GCCOLLECT, 0);
950.         }
951.     } else {
952.         nhl_error(L, "wrong parameters");
953.         /*NOTREACHED*/
954.     }
955.  out:
956.     return 0;
957. }
958. 
959. /* local txt = sel:describe_size(); */
960. /* gives a textual description of the selection size */
961. staticfn int
962. l_selection_size_description(lua_State *L)
963. {
964.     int argc = lua_gettop(L);
965. 
966.     if (argc == 1) {
967.         struct selectionvar *sel = l_selection_check(L, 1);
968.         char buf[BUFSZ];
969. 
970.         lua_pushstring(L, selection_size_description(sel, buf));
971.         return 1;
972.     } else {
973.         nhl_error(L, "wrong parameters");
974.         /*NOTREACHED*/
975.     }
976.     return 0;
977. }
978. 
979. RESTORE_WARNING_UNREACHABLE_CODE
980. 
981. static const struct luaL_Reg l_selection_methods[] = {
982.     { "new", l_selection_new },
983.     { "clone", l_selection_clone },
984.     { "get", l_selection_getpoint },
985.     { "set", l_selection_setpoint },
986.     { "numpoints", l_selection_numpoints },
987.     { "negate", l_selection_not },
988.     { "percentage", l_selection_filter_percent },
989.     { "rndcoord", l_selection_rndcoord },
990.     { "line", l_selection_line },
991.     { "randline", l_selection_randline },
992.     { "rect", l_selection_rect },
993.     { "fillrect", l_selection_fillrect },
994.     { "area", l_selection_fillrect },
995.     { "grow", l_selection_grow },
996.     { "filter_mapchar", l_selection_filter_mapchar },
997.     { "match", l_selection_match },
998.     { "floodfill", l_selection_flood },
999.     { "circle", l_selection_circle },
1000.     { "ellipse", l_selection_ellipse },
1001.     { "gradient", l_selection_gradient },
1002.     { "iterate", l_selection_iterate },
1003.     { "bounds", l_selection_getbounds },
1004.     { "room", l_selection_room },
1005.     { "describe_size", l_selection_size_description },
1006.     { NULL, NULL }
1007. };
1008. 
1009. static const luaL_Reg l_selection_meta[] = {
1010.     { "__gc", l_selection_gc },
1011.     { "__unm", l_selection_not },
1012.     { "__band", l_selection_and },
1013.     { "__bor", l_selection_or },
1014.     { "__bxor", l_selection_xor },
1015.     { "__bnot", l_selection_not },
1016.     { "__add", l_selection_or }, /* this aliases + to be the same as | */
1017.     { "__sub", l_selection_sub },
1018.     /* TODO: http://lua-users.org/wiki/MetatableEvents
1019.        { "__ipairs", l_selection_ipairs },
1020.     */
1021.     { NULL, NULL }
1022. };
1023. 
1024. int
1025. l_selection_register(lua_State *L)
1026. {
1027.     /* Table of instance methods and static methods. */
1028.     luaL_newlib(L, l_selection_methods);
1029. 
1030.     /* metatable = { __name = "selection", __gc = l_selection_gc } */
1031.     luaL_newmetatable(L, "selection");
1032.     luaL_setfuncs(L, l_selection_meta, 0);
1033. 
1034.     /* metatable.__index points at the selection method table. */
1035.     lua_pushvalue(L, -2);
1036.     lua_setfield(L, -2, "__index");
1037. 
1038.     /* Don't let lua code mess with the real metatable.
1039.        Instead offer a fake one that only contains __gc. */
1040.     luaL_newlib(L, l_selection_meta);
1041.     lua_setfield(L, -2, "__metatable");
1042. 
1043.     /* We don't need the metatable anymore. It's safe in the
1044.        Lua registry for use by luaL_setmetatable. */
1045.     lua_pop(L, 1);
1046. 
1047.     /* global selection = the method table we created at the start */
1048.     lua_setglobal(L, "selection");
1049. 
1050.     return 0;
1051. }