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