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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185
|
// This file is part of Golly.
// See docs/License.html for the copyright notice.
/*
Golly uses a statically embedded Lua interpreter to execute .lua scripts.
Here is the official Lua copyright notice:
Copyright 1994-2015 Lua.org, PUC-Rio.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "wx/wxprec.h" // for compilers that support precompilation
#ifndef WX_PRECOMP
#include "wx/wx.h" // for all others include the necessary headers
#endif
#include "wx/filename.h" // for wxFileName
#include "wx/dir.h" // for wxDir
#include "bigint.h"
#include "lifealgo.h"
#include "qlifealgo.h"
#include "hlifealgo.h"
#include "readpattern.h"
#include "writepattern.h"
#include "wxgolly.h" // for wxGetApp, mainptr, viewptr, statusptr, stopwatch
#include "wxmain.h" // for mainptr->...
#include "wxselect.h" // for Selection
#include "wxview.h" // for viewptr->...
#include "wxstatus.h" // for statusptr->...
#include "wxutils.h" // for Warning, Note, GetString, SaveChanges, etc
#include "wxprefs.h" // for gollydir, etc
#include "wxinfo.h" // for ShowInfo
#include "wxhelp.h" // for ShowHelp
#include "wxundo.h" // for currlayer->undoredo->...
#include "wxalgos.h" // for *_ALGO, CreateNewUniverse, etc
#include "wxlayer.h" // for AddLayer, currlayer, currindex, etc
#include "wxoverlay.h" // for curroverlay->...
#include "wxscript.h" // for inscript, abortmsg, GSF_*, etc
#include "wxlua.h"
#include "lua.hpp" // Lua header files for C++
// -----------------------------------------------------------------------------
// some useful macros
#define CHECK_RGB(r,g,b,cmd) \
if (r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255) { \
char msg[128]; \
sprintf(msg, "%s error: bad rgb value (%d,%d,%d)", cmd, r, g, b); \
GollyError(L, msg); \
}
#ifdef __WXMAC__
// use decomposed UTF8 so fopen will work
#define FILENAME wxString(filename,wxConvUTF8).fn_str()
#else
#define FILENAME filename
#endif
// use UTF8 for the encoding conversion from Lua string to wxString
#define LUA_ENC wxConvUTF8
#ifdef AUTORELEASE
/* these macros are used to avoid memory leaks on recent Mac OS versions
(probably 10.9+) if a script command calls Refresh; eg:
local g = golly()
for n = 1, math.maxinteger do
g.show(tostring(n))
end
*/
#define BEGIN_AUTORELEASE @autoreleasepool {
#define END_AUTORELEASE }
#else
#define BEGIN_AUTORELEASE
#define END_AUTORELEASE
#endif
// -----------------------------------------------------------------------------
static bool aborted = false; // stop the current script?
static void CheckEvents(lua_State* L)
{
// this routine is called at the start of every g_* function so we can
// detect user events (eg. hitting the stop button or escape key)
if (allowcheck) wxGetApp().Poller()->checkevents();
if (insideYield) return;
// we're outside Yield so safe to do a longjmp from lua_error
if (aborted) {
// AbortLuaScript was called
lua_pushstring(L, abortmsg);
lua_error(L);
}
}
// -----------------------------------------------------------------------------
static void GollyError(lua_State* L, const char* errmsg)
{
// handle an error detected in a g_* function;
// note that luaL_error will prepend file path and line number info
luaL_error(L, "\n%s", errmsg);
}
// -----------------------------------------------------------------------------
static bool CheckBoolean(lua_State* L, int arg)
{
luaL_checktype(L, arg, LUA_TBOOLEAN);
return lua_toboolean(L, arg) ? true : false;
}
// -----------------------------------------------------------------------------
static int g_open(lua_State* L)
{
CheckEvents(L);
const char* filename = luaL_checkstring(L, 1);
bool remember = false;
if (lua_gettop(L) > 1) remember = CheckBoolean(L, 2);
const char* err;
BEGIN_AUTORELEASE
err = GSF_open(wxString(filename, LUA_ENC), remember ? 1 : 0);
END_AUTORELEASE
if (err) GollyError(L, err);
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_save(lua_State* L)
{
CheckEvents(L);
const char* filename = luaL_checkstring(L, 1);
const char* format = luaL_checkstring(L, 2);
bool remember = false;
if (lua_gettop(L) > 2) remember = CheckBoolean(L, 3);
const char* err = GSF_save(wxString(filename, LUA_ENC), format, remember ? 1 : 0);
if (err) GollyError(L, err);
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_opendialog(lua_State* L)
{
CheckEvents(L);
const char* title = "Choose a file";
const char* filetypes = "All files (*)|*";
const char* initialdir = "";
const char* initialfname = "";
bool mustexist = true;
if (lua_gettop(L) > 0) title = luaL_checkstring(L, 1);
if (lua_gettop(L) > 1) filetypes = luaL_checkstring(L, 2);
if (lua_gettop(L) > 2) initialdir = luaL_checkstring(L, 3);
if (lua_gettop(L) > 3) initialfname = luaL_checkstring(L, 4);
if (lua_gettop(L) > 4) mustexist = CheckBoolean(L, 5);
wxString wxs_title(title, LUA_ENC);
wxString wxs_filetypes(filetypes, LUA_ENC);
wxString wxs_initialdir(initialdir, LUA_ENC);
wxString wxs_initialfname(initialfname, LUA_ENC);
wxString wxs_result = wxEmptyString;
if (wxs_initialdir.IsEmpty()) wxs_initialdir = wxFileName::GetCwd();
if (wxs_filetypes == wxT("dir")) {
// let user choose a directory
wxDirDialog dirdlg(NULL, wxs_title, wxs_initialdir, wxDD_NEW_DIR_BUTTON);
if (dirdlg.ShowModal() == wxID_OK) {
wxs_result = dirdlg.GetPath();
if (wxs_result.Last() != wxFILE_SEP_PATH) wxs_result += wxFILE_SEP_PATH;
}
} else {
// let user choose a file
wxFileDialog opendlg(NULL, wxs_title, wxs_initialdir, wxs_initialfname, wxs_filetypes,
wxFD_OPEN | (mustexist ? wxFD_FILE_MUST_EXIST : 0) );
if (opendlg.ShowModal() == wxID_OK) wxs_result = opendlg.GetPath();
}
viewptr->ResetMouseDown();
lua_pushstring(L, (const char*)wxs_result.mb_str(LUA_ENC));
return 1; // result is a string
}
// -----------------------------------------------------------------------------
static int g_savedialog(lua_State* L)
{
CheckEvents(L);
const char* title = "Choose a save location and filename";
const char* filetypes = "All files (*)|*";
const char* initialdir = "";
const char* initialfname = "";
bool suppressprompt = false;
if (lua_gettop(L) > 0) title = luaL_checkstring(L, 1);
if (lua_gettop(L) > 1) filetypes = luaL_checkstring(L, 2);
if (lua_gettop(L) > 2) initialdir = luaL_checkstring(L, 3);
if (lua_gettop(L) > 3) initialfname = luaL_checkstring(L, 4);
if (lua_gettop(L) > 4) suppressprompt = CheckBoolean(L, 5);
wxString wxs_title(title, LUA_ENC);
wxString wxs_filetypes(filetypes, LUA_ENC);
wxString wxs_initialdir(initialdir, LUA_ENC);
wxString wxs_initialfname(initialfname, LUA_ENC);
if (wxs_initialdir.IsEmpty()) wxs_initialdir = wxFileName::GetCwd();
wxFileDialog savedlg(NULL, wxs_title, wxs_initialdir, wxs_initialfname, wxs_filetypes,
wxFD_SAVE | (suppressprompt ? 0 : wxFD_OVERWRITE_PROMPT));
wxString wxs_savefname = wxEmptyString;
if (savedlg.ShowModal() == wxID_OK) wxs_savefname = savedlg.GetPath();
viewptr->ResetMouseDown();
lua_pushstring(L, (const char*)wxs_savefname.mb_str(LUA_ENC));
return 1; // result is a string
}
// -----------------------------------------------------------------------------
static const char* ExtractCellArray(lua_State* L, lifealgo* universe, bool shift = false)
{
// extract cell array from given universe
lua_newtable(L);
if ( !universe->isEmpty() ) {
bigint top, left, bottom, right;
universe->findedges(&top, &left, &bottom, &right);
if ( viewptr->OutsideLimits(top, left, bottom, right) ) {
return "Universe is too big to extract all cells!";
}
bool multistate = universe->NumCellStates() > 2;
int itop = top.toint();
int ileft = left.toint();
int ibottom = bottom.toint();
int iright = right.toint();
int cx, cy;
int v = 0;
int arraylen = 0;
for ( cy=itop; cy<=ibottom; cy++ ) {
for ( cx=ileft; cx<=iright; cx++ ) {
int skip = universe->nextcell(cx, cy, v);
if (skip >= 0) {
// found next live cell in this row
cx += skip;
if (shift) {
// shift cells so that top left cell of bounding box is at 0,0
lua_pushinteger(L, cx - ileft); lua_rawseti(L, -2, ++arraylen);
lua_pushinteger(L, cy - itop ); lua_rawseti(L, -2, ++arraylen);
} else {
lua_pushinteger(L, cx); lua_rawseti(L, -2, ++arraylen);
lua_pushinteger(L, cy); lua_rawseti(L, -2, ++arraylen);
}
if (multistate) {
lua_pushinteger(L, v); lua_rawseti(L, -2, ++arraylen);
}
} else {
cx = iright; // done this row
}
}
}
if (multistate && arraylen > 0 && (arraylen & 1) == 0) {
// add padding zero so the cell array has an odd number of ints
// (this is how we distinguish multi-state arrays from one-state arrays;
// the latter always have an even number of ints)
lua_pushinteger(L, 0); lua_rawseti(L, -2, ++arraylen);
}
}
return NULL;
}
// -----------------------------------------------------------------------------
static int g_load(lua_State* L)
{
CheckEvents(L);
const char* filename = luaL_checkstring(L, 1);
// create temporary universe of same type as current universe
lifealgo* tempalgo = CreateNewUniverse(currlayer->algtype, false);
// readpattern will call setrule
// read pattern into temporary universe
const char* err = readpattern(FILENAME, *tempalgo);
if (err) {
// try all other algos until readpattern succeeds
for (int i = 0; i < NumAlgos(); i++) {
if (i != currlayer->algtype) {
delete tempalgo;
tempalgo = CreateNewUniverse(i, false);
err = readpattern(FILENAME, *tempalgo);
if (!err) break;
}
}
}
if (err) {
delete tempalgo;
GollyError(L, err);
}
// convert pattern into a cell array, shifting cell coords so that the
// bounding box's top left cell is at 0,0
err = ExtractCellArray(L, tempalgo, true);
delete tempalgo;
if (err) GollyError(L, err);
return 1; // result is a cell array
}
// -----------------------------------------------------------------------------
static int g_store(lua_State* L)
{
CheckEvents(L);
luaL_checktype(L, 1, LUA_TTABLE); // cell array
int len = luaL_len(L, 1);
const char* filename = luaL_checkstring(L, 2);
// create temporary universe of same type as current universe
lifealgo* tempalgo = CreateNewUniverse(currlayer->algtype, false);
const char* err = tempalgo->setrule(currlayer->algo->getrule());
if (err) tempalgo->setrule(tempalgo->DefaultRule());
// copy cell array into temporary universe
bool multistate = (len & 1) == 1;
int ints_per_cell = multistate ? 3 : 2;
int num_cells = len / ints_per_cell;
for (int n = 0; n < num_cells; n++) {
int item = ints_per_cell * n;
lua_rawgeti(L, 1, item+1); int x = lua_tointeger(L,-1); lua_pop(L,1);
lua_rawgeti(L, 1, item+2); int y = lua_tointeger(L,-1); lua_pop(L,1);
// check if x,y is outside bounded grid
err = GSF_checkpos(tempalgo, x, y);
if (err) {
delete tempalgo;
GollyError(L, err);
}
if (multistate) {
lua_rawgeti(L, 1, item+3); int state = lua_tointeger(L,-1); lua_pop(L,1);
if (tempalgo->setcell(x, y, state) < 0) {
tempalgo->endofpattern();
delete tempalgo;
GollyError(L, "store error: state value is out of range.");
}
} else {
tempalgo->setcell(x, y, 1);
}
}
tempalgo->endofpattern();
// write pattern to given file in RLE/XRLE format
bigint top, left, bottom, right;
tempalgo->findedges(&top, &left, &bottom, &right);
pattern_format format = savexrle ? XRLE_format : RLE_format;
// if grid is bounded then force XRLE_format so that position info is recorded
if (tempalgo->gridwd > 0 || tempalgo->gridht > 0) format = XRLE_format;
err = writepattern(FILENAME, *tempalgo, format, no_compression,
top.toint(), left.toint(), bottom.toint(), right.toint());
delete tempalgo;
if (err) GollyError(L, err);
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_setdir(lua_State* L)
{
CheckEvents(L);
const char* dirname = luaL_checkstring(L, 1);
const char* newdir = luaL_checkstring(L, 2);
const char* err;
BEGIN_AUTORELEASE
err = GSF_setdir(dirname, wxString(newdir, LUA_ENC));
END_AUTORELEASE
if (err) GollyError(L, err);
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_getdir(lua_State* L)
{
CheckEvents(L);
const char* dirname = luaL_checkstring(L, 1);
const char* dirstring = GSF_getdir(dirname);
if (dirstring == NULL) GollyError(L, "getdir error: unknown directory name.");
lua_pushstring(L, dirstring);
return 1; // result is a string
}
// -----------------------------------------------------------------------------
static int g_getfiles(lua_State* L)
{
CheckEvents(L);
const char* dirname = luaL_checkstring(L, 1);
wxString dirpath = wxString(dirname, LUA_ENC);
if (!wxFileName::DirExists(dirpath)) {
GollyError(L, "getfiles error: given directory does not exist.");
}
lua_newtable(L);
int arraylen = 0;
wxDir dir(dirpath);
if (dir.IsOpened()) {
wxString filename;
// get all files
bool more = dir.GetFirst(&filename, wxEmptyString, wxDIR_FILES | wxDIR_HIDDEN);
while (more) {
lua_pushstring(L, (const char*)filename.mb_str(LUA_ENC));
lua_rawseti(L, -2, ++arraylen);
more = dir.GetNext(&filename);
}
// now get all directories and append platform-specific path separator
more = dir.GetFirst(&filename, wxEmptyString, wxDIR_DIRS | wxDIR_HIDDEN);
while (more) {
filename += wxFILE_SEP_PATH;
lua_pushstring(L, (const char*)filename.mb_str(LUA_ENC));
lua_rawseti(L, -2, ++arraylen);
more = dir.GetNext(&filename);
}
}
return 1; // result is a table of strings
}
// -----------------------------------------------------------------------------
static int g_getpath(lua_State* L)
{
CheckEvents(L);
lua_pushstring(L, GSF_getpath());
return 1; // result is a string
}
// -----------------------------------------------------------------------------
static int g_getinfo(lua_State* L)
{
CheckEvents(L);
lua_pushstring(L, GSF_getinfo());
return 1; // result is a string
}
// -----------------------------------------------------------------------------
static int g_new(lua_State* L)
{
CheckEvents(L);
BEGIN_AUTORELEASE
mainptr->NewPattern(wxString(luaL_checkstring(L, 1), LUA_ENC));
DoAutoUpdate();
END_AUTORELEASE
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_cut(lua_State* L)
{
CheckEvents(L);
if (viewptr->SelectionExists()) {
BEGIN_AUTORELEASE
viewptr->CutSelection();
DoAutoUpdate();
END_AUTORELEASE
} else {
GollyError(L, "cut error: no selection.");
}
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_copy(lua_State* L)
{
CheckEvents(L);
if (viewptr->SelectionExists()) {
BEGIN_AUTORELEASE
viewptr->CopySelection();
DoAutoUpdate();
END_AUTORELEASE
} else {
GollyError(L, "copy error: no selection.");
}
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_clear(lua_State* L)
{
CheckEvents(L);
int where = luaL_checkinteger(L, 1);
if (viewptr->SelectionExists()) {
BEGIN_AUTORELEASE
if (where == 0)
viewptr->ClearSelection();
else
viewptr->ClearOutsideSelection();
DoAutoUpdate();
END_AUTORELEASE
} else {
GollyError(L, "clear error: no selection.");
}
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_paste(lua_State* L)
{
CheckEvents(L);
int x = luaL_checkinteger(L, 1);
int y = luaL_checkinteger(L, 2);
const char* mode = luaL_checkstring(L, 3);
const char* err;
BEGIN_AUTORELEASE
err = GSF_paste(x, y, mode);
END_AUTORELEASE
if (err) GollyError(L, err);
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_shrink(lua_State* L)
{
CheckEvents(L);
bool remove_if_empty = false;
if (lua_gettop(L) > 0) remove_if_empty = CheckBoolean(L, 1);
if (viewptr->SelectionExists()) {
BEGIN_AUTORELEASE
currlayer->currsel.Shrink(false, remove_if_empty);
// false == don't fit in viewport
DoAutoUpdate();
END_AUTORELEASE
} else {
GollyError(L, "shrink error: no selection.");
}
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_randfill(lua_State* L)
{
CheckEvents(L);
int perc = luaL_checkinteger(L, 1);
if (perc < 1 || perc > 100) {
GollyError(L, "randfill error: percentage must be from 1 to 100.");
}
if (viewptr->SelectionExists()) {
BEGIN_AUTORELEASE
int oldperc = randomfill;
randomfill = perc;
viewptr->RandomFill();
randomfill = oldperc;
DoAutoUpdate();
END_AUTORELEASE
} else {
GollyError(L, "randfill error: no selection.");
}
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_flip(lua_State* L)
{
CheckEvents(L);
int direction = luaL_checkinteger(L, 1);
if (viewptr->SelectionExists()) {
BEGIN_AUTORELEASE
viewptr->FlipSelection(direction != 0); // 1 = top-bottom
DoAutoUpdate();
END_AUTORELEASE
} else {
GollyError(L, "flip error: no selection.");
}
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_rotate(lua_State* L)
{
CheckEvents(L);
int direction = luaL_checkinteger(L, 1);
if (viewptr->SelectionExists()) {
BEGIN_AUTORELEASE
viewptr->RotateSelection(direction == 0); // 0 = clockwise
DoAutoUpdate();
END_AUTORELEASE
} else {
GollyError(L, "rotate error: no selection.");
}
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_parse(lua_State* L)
{
CheckEvents(L);
const char* s = luaL_checkstring(L, 1);
// defaults for optional params
int x0 = 0;
int y0 = 0;
int axx = 1;
int axy = 0;
int ayx = 0;
int ayy = 1;
if (lua_gettop(L) > 1) x0 = luaL_checkinteger(L, 2);
if (lua_gettop(L) > 2) y0 = luaL_checkinteger(L, 3);
if (lua_gettop(L) > 3) axx = luaL_checkinteger(L, 4);
if (lua_gettop(L) > 4) axy = luaL_checkinteger(L, 5);
if (lua_gettop(L) > 5) ayx = luaL_checkinteger(L, 6);
if (lua_gettop(L) > 6) ayy = luaL_checkinteger(L, 7);
lua_newtable(L);
int arraylen = 0;
int x = 0;
int y = 0;
if (strchr(s, '*')) {
// parsing 'visual' format
int c = *s++;
while (c) {
switch (c) {
case '\n': if (x) { x = 0; y++; } break;
case '.': x++; break;
case '*':
lua_pushinteger(L, x0 + x * axx + y * axy); lua_rawseti(L, -2, ++arraylen);
lua_pushinteger(L, y0 + x * ayx + y * ayy); lua_rawseti(L, -2, ++arraylen);
x++;
break;
}
c = *s++;
}
} else {
// parsing RLE format; first check if multi-state data is present
bool multistate = false;
const char* p = s;
while (*p) {
char c = *p++;
if ((c == '.') || ('p' <= c && c <= 'y') || ('A' <= c && c <= 'X')) {
multistate = true;
break;
}
}
int prefix = 0;
bool done = false;
int c = *s++;
while (c && !done) {
if (isdigit(c))
prefix = 10 * prefix + (c - '0');
else {
prefix += (prefix == 0);
switch (c) {
case '!': done = true; break;
case '$': x = 0; y += prefix; break;
case 'b': x += prefix; break;
case '.': x += prefix; break;
case 'o':
for (int k = 0; k < prefix; k++, x++) {
lua_pushinteger(L, x0 + x * axx + y * axy); lua_rawseti(L, -2, ++arraylen);
lua_pushinteger(L, y0 + x * ayx + y * ayy); lua_rawseti(L, -2, ++arraylen);
if (multistate) {
lua_pushinteger(L, 1); lua_rawseti(L, -2, ++arraylen);
}
}
break;
default:
if (('p' <= c && c <= 'y') || ('A' <= c && c <= 'X')) {
// multistate must be true
int state;
if (c < 'p') {
state = c - 'A' + 1;
} else {
state = 24 * (c - 'p' + 1);
c = *s++;
if ('A' <= c && c <= 'X') {
state = state + c - 'A' + 1;
} else {
// be forgiving and treat 'p'..'y' like 'o'
state = 1;
s--;
}
}
for (int k = 0; k < prefix; k++, x++) {
lua_pushinteger(L, x0 + x * axx + y * axy); lua_rawseti(L, -2, ++arraylen);
lua_pushinteger(L, y0 + x * ayx + y * ayy); lua_rawseti(L, -2, ++arraylen);
lua_pushinteger(L, state); lua_rawseti(L, -2, ++arraylen);
}
}
}
prefix = 0;
}
c = *s++;
}
if (multistate && arraylen > 0 && (arraylen & 1) == 0) {
// add padding zero
lua_pushinteger(L, 0); lua_rawseti(L, -2, ++arraylen);
}
}
return 1; // result is a cell array
}
// -----------------------------------------------------------------------------
static int g_transform(lua_State* L)
{
CheckEvents(L);
luaL_checktype(L, 1, LUA_TTABLE); // cell array
int x0 = luaL_checkinteger(L, 2);
int y0 = luaL_checkinteger(L, 3);
// defaults for optional params
int axx = 1;
int axy = 0;
int ayx = 0;
int ayy = 1;
if (lua_gettop(L) > 3) axx = luaL_checkinteger(L, 4);
if (lua_gettop(L) > 4) axy = luaL_checkinteger(L, 5);
if (lua_gettop(L) > 5) ayx = luaL_checkinteger(L, 6);
if (lua_gettop(L) > 6) ayy = luaL_checkinteger(L, 7);
lua_newtable(L);
int arraylen = 0;
bool multistate = (luaL_len(L, 1) & 1) == 1;
int ints_per_cell = multistate ? 3 : 2;
int num_cells = luaL_len(L, 1) / ints_per_cell;
for (int n = 0; n < num_cells; n++) {
int item = ints_per_cell * n;
lua_rawgeti(L, 1, item+1); int x = lua_tointeger(L,-1); lua_pop(L,1);
lua_rawgeti(L, 1, item+2); int y = lua_tointeger(L,-1); lua_pop(L,1);
lua_pushinteger(L, x0 + x * axx + y * axy); lua_rawseti(L, -2, ++arraylen);
lua_pushinteger(L, y0 + x * ayx + y * ayy); lua_rawseti(L, -2, ++arraylen);
if (multistate) {
lua_rawgeti(L, 1, item+3); int state = lua_tointeger(L,-1); lua_pop(L,1);
lua_pushinteger(L, state); lua_rawseti(L, -2, ++arraylen);
}
}
if (multistate && arraylen > 0 && (arraylen & 1) == 0) {
// add padding zero
lua_pushinteger(L, 0); lua_rawseti(L, -2, ++arraylen);
}
return 1; // result is a cell array
}
// -----------------------------------------------------------------------------
static int g_evolve(lua_State* L)
{
CheckEvents(L);
luaL_checktype(L, 1, LUA_TTABLE); // cell array
int ngens = luaL_checkinteger(L, 2);
if (ngens < 0) {
GollyError(L, "evolve error: number of generations is negative.");
}
// create a temporary universe of same type as current universe
lifealgo* tempalgo = CreateNewUniverse(currlayer->algtype, false);
const char* err = tempalgo->setrule(currlayer->algo->getrule());
if (err) tempalgo->setrule(tempalgo->DefaultRule());
// copy cell array into temporary universe
bool multistate = (luaL_len(L, 1) & 1) == 1;
int ints_per_cell = multistate ? 3 : 2;
int num_cells = luaL_len(L, 1) / ints_per_cell;
for (int n = 0; n < num_cells; n++) {
int item = ints_per_cell * n;
lua_rawgeti(L, 1, item+1); int x = lua_tointeger(L,-1); lua_pop(L,1);
lua_rawgeti(L, 1, item+2); int y = lua_tointeger(L,-1); lua_pop(L,1);
// check if x,y is outside bounded grid
err = GSF_checkpos(tempalgo, x, y);
if (err) {
delete tempalgo;
GollyError(L, err);
}
if (multistate) {
lua_rawgeti(L, 1, item+3); int state = lua_tointeger(L,-1); lua_pop(L,1);
if (tempalgo->setcell(x, y, state) < 0) {
tempalgo->endofpattern();
delete tempalgo;
GollyError(L, "evolve error: state value is out of range.");
}
} else {
tempalgo->setcell(x, y, 1);
}
}
tempalgo->endofpattern();
// advance pattern by ngens
mainptr->generating = true;
if (tempalgo->unbounded && (tempalgo->gridwd > 0 || tempalgo->gridht > 0)) {
// a bounded grid must use an increment of 1 so we can call
// CreateBorderCells and DeleteBorderCells around each step()
tempalgo->setIncrement(1);
while (ngens > 0) {
if (!tempalgo->CreateBorderCells()) break;
tempalgo->step();
if (!tempalgo->DeleteBorderCells()) break;
ngens--;
}
} else {
tempalgo->setIncrement(ngens);
tempalgo->step();
}
mainptr->generating = false;
// convert new pattern into a cell array
err = ExtractCellArray(L, tempalgo);
delete tempalgo;
if (err) GollyError(L, err);
return 1; // result is a cell array
}
// -----------------------------------------------------------------------------
static const char* BAD_STATE = "putcells error: state value is out of range.";
static int g_putcells(lua_State* L)
{
CheckEvents(L);
luaL_checktype(L, 1, LUA_TTABLE); // cell array
// defaults for optional params
int x0 = 0;
int y0 = 0;
int axx = 1;
int axy = 0;
int ayx = 0;
int ayy = 1;
// default for mode is 'or'; 'xor' mode is also supported;
// for a one-state array 'copy' mode currently has the same effect as 'or' mode
// because there is no bounding box to set dead cells, but a multi-state array can
// have dead cells so in that case 'copy' mode is not the same as 'or' mode
const char* mode = "or";
if (lua_gettop(L) > 1) x0 = luaL_checkinteger(L, 2);
if (lua_gettop(L) > 2) y0 = luaL_checkinteger(L, 3);
if (lua_gettop(L) > 3) axx = luaL_checkinteger(L, 4);
if (lua_gettop(L) > 4) axy = luaL_checkinteger(L, 5);
if (lua_gettop(L) > 5) ayx = luaL_checkinteger(L, 6);
if (lua_gettop(L) > 6) ayy = luaL_checkinteger(L, 7);
if (lua_gettop(L) > 7) mode = luaL_checkstring(L, 8);
wxString modestr = wxString(mode, LUA_ENC);
if ( !( modestr.IsSameAs(wxT("or"), false)
|| modestr.IsSameAs(wxT("xor"), false)
|| modestr.IsSameAs(wxT("copy"), false)
|| modestr.IsSameAs(wxT("and"), false)
|| modestr.IsSameAs(wxT("not"), false)) ) {
GollyError(L, "putcells error: unknown mode.");
}
// save cell changes if undo/redo is enabled and script isn't constructing a pattern
bool savecells = allowundo && !currlayer->stayclean;
// use ChangeCell below and combine all changes due to consecutive setcell/putcells
// if (savecells) SavePendingChanges();
bool multistate = (luaL_len(L, 1) & 1) == 1;
int ints_per_cell = multistate ? 3 : 2;
int num_cells = luaL_len(L, 1) / ints_per_cell;
const char* err = NULL;
bool pattchanged = false;
lifealgo* curralgo = currlayer->algo;
if (modestr.IsSameAs(wxT("copy"), false)) {
// TODO: find bounds of cell array and call ClearRect here (to be added to wxedit.cpp)
}
if (modestr.IsSameAs(wxT("and"), false)) {
if (!curralgo->isEmpty()) {
int newstate = 1;
for (int n = 0; n < num_cells; n++) {
int item = ints_per_cell * n;
lua_rawgeti(L, 1, item+1); int x = lua_tointeger(L,-1); lua_pop(L,1);
lua_rawgeti(L, 1, item+2); int y = lua_tointeger(L,-1); lua_pop(L,1);
int newx = x0 + x * axx + y * axy;
int newy = y0 + x * ayx + y * ayy;
// check if newx,newy is outside bounded grid
err = GSF_checkpos(curralgo, newx, newy);
if (err) break;
int oldstate = curralgo->getcell(newx, newy);
if (multistate) {
// multi-state arrays can contain dead cells so newstate might be 0
lua_rawgeti(L, 1, item+3); newstate = lua_tointeger(L,-1); lua_pop(L,1);
}
if (newstate != oldstate && oldstate > 0) {
curralgo->setcell(newx, newy, 0);
if (savecells) ChangeCell(newx, newy, oldstate, 0);
pattchanged = true;
}
}
}
} else if (modestr.IsSameAs(wxT("xor"), false)) {
// loop code is duplicated here to allow 'or' case to execute faster
int numstates = curralgo->NumCellStates();
for (int n = 0; n < num_cells; n++) {
int item = ints_per_cell * n;
lua_rawgeti(L, 1, item+1); int x = lua_tointeger(L,-1); lua_pop(L,1);
lua_rawgeti(L, 1, item+2); int y = lua_tointeger(L,-1); lua_pop(L,1);
int newx = x0 + x * axx + y * axy;
int newy = y0 + x * ayx + y * ayy;
// check if newx,newy is outside bounded grid
err = GSF_checkpos(curralgo, newx, newy);
if (err) break;
int oldstate = curralgo->getcell(newx, newy);
int newstate;
if (multistate) {
// multi-state arrays can contain dead cells so newstate might be 0
lua_rawgeti(L, 1, item+3); newstate = lua_tointeger(L,-1); lua_pop(L,1);
if (newstate == oldstate) {
if (oldstate != 0) newstate = 0;
} else {
newstate = newstate ^ oldstate;
// if xor overflows then don't change current state
if (newstate >= numstates) newstate = oldstate;
}
if (newstate != oldstate) {
// paste (possibly transformed) cell into current universe
if (curralgo->setcell(newx, newy, newstate) < 0) {
err = BAD_STATE;
break;
}
if (savecells) ChangeCell(newx, newy, oldstate, newstate);
pattchanged = true;
}
} else {
// one-state arrays only contain live cells
newstate = 1 - oldstate;
// paste (possibly transformed) cell into current universe
if (curralgo->setcell(newx, newy, newstate) < 0) {
err = BAD_STATE;
break;
}
if (savecells) ChangeCell(newx, newy, oldstate, newstate);
pattchanged = true;
}
}
} else {
bool notmode = modestr.IsSameAs(wxT("not"), false);
bool ormode = modestr.IsSameAs(wxT("or"), false);
int newstate = notmode ? 0 : 1;
int maxstate = curralgo->NumCellStates() - 1;
for (int n = 0; n < num_cells; n++) {
int item = ints_per_cell * n;
lua_rawgeti(L, 1, item+1); int x = lua_tointeger(L,-1); lua_pop(L,1);
lua_rawgeti(L, 1, item+2); int y = lua_tointeger(L,-1); lua_pop(L,1);
int newx = x0 + x * axx + y * axy;
int newy = y0 + x * ayx + y * ayy;
// check if newx,newy is outside bounded grid
err = GSF_checkpos(curralgo, newx, newy);
if (err) break;
int oldstate = curralgo->getcell(newx, newy);
if (multistate) {
// multi-state arrays can contain dead cells so newstate might be 0
lua_rawgeti(L, 1, item+3); newstate = lua_tointeger(L,-1); lua_pop(L,1);
if (notmode) newstate = maxstate - newstate;
if (ormode && newstate == 0) newstate = oldstate;
}
if (newstate != oldstate) {
// paste (possibly transformed) cell into current universe
if (curralgo->setcell(newx, newy, newstate) < 0) {
err = BAD_STATE;
break;
}
if (savecells) ChangeCell(newx, newy, oldstate, newstate);
pattchanged = true;
}
}
}
if (pattchanged) {
BEGIN_AUTORELEASE
curralgo->endofpattern();
MarkLayerDirty();
DoAutoUpdate();
END_AUTORELEASE
}
if (err) GollyError(L, err);
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_getcells(lua_State* L)
{
CheckEvents(L);
luaL_checktype(L, 1, LUA_TTABLE); // rect array with 0 or 4 ints
lua_newtable(L);
int arraylen = 0;
int numints = luaL_len(L, 1);
if (numints == 0) {
// return empty cell array
} else if (numints == 4) {
lua_rawgeti(L, 1, 1); int ileft = luaL_checkinteger(L,-1); lua_pop(L,1);
lua_rawgeti(L, 1, 2); int itop = luaL_checkinteger(L,-1); lua_pop(L,1);
lua_rawgeti(L, 1, 3); int wd = luaL_checkinteger(L,-1); lua_pop(L,1);
lua_rawgeti(L, 1, 4); int ht = luaL_checkinteger(L,-1); lua_pop(L,1);
const char* err = GSF_checkrect(ileft, itop, wd, ht);
if (err) GollyError(L, err);
int iright = ileft + wd - 1;
int ibottom = itop + ht - 1;
int cx, cy;
int v = 0;
lifealgo* curralgo = currlayer->algo;
bool multistate = curralgo->NumCellStates() > 2;
for ( cy=itop; cy<=ibottom; cy++ ) {
for ( cx=ileft; cx<=iright; cx++ ) {
int skip = curralgo->nextcell(cx, cy, v);
if (skip >= 0) {
// found next live cell in this row
cx += skip;
if (cx <= iright) {
lua_pushinteger(L, cx); lua_rawseti(L, -2, ++arraylen);
lua_pushinteger(L, cy); lua_rawseti(L, -2, ++arraylen);
if (multistate) {
lua_pushinteger(L, v); lua_rawseti(L, -2, ++arraylen);
}
}
} else {
cx = iright; // done this row
}
}
}
if (multistate && arraylen > 0 && (arraylen & 1) == 0) {
// add padding zero
lua_pushinteger(L, 0); lua_rawseti(L, -2, ++arraylen);
}
} else {
GollyError(L, "getcells error: array must be {} or {x,y,wd,ht}.");
}
return 1; // result is a cell array
}
// -----------------------------------------------------------------------------
// maybe only use algo->getcells method if algo is hash-based???!!!
// (needs more thought and more testing)
#if 0
static int g_getcells2(lua_State* L)
{
CheckEvents(L);
luaL_checktype(L, 1, LUA_TTABLE); // rect array with 0 or 4 ints
lua_newtable(L);
int arraylen = 0;
int numints = luaL_len(L, 1);
if (numints == 0) {
// return empty cell array
} else if (numints == 4) {
lua_rawgeti(L, 1, 1); int ileft = luaL_checkinteger(L,-1); lua_pop(L,1);
lua_rawgeti(L, 1, 2); int itop = luaL_checkinteger(L,-1); lua_pop(L,1);
lua_rawgeti(L, 1, 3); int wd = luaL_checkinteger(L,-1); lua_pop(L,1);
lua_rawgeti(L, 1, 4); int ht = luaL_checkinteger(L,-1); lua_pop(L,1);
const char* err = GSF_checkrect(ileft, itop, wd, ht);
if (err) GollyError(L, err);
int iright = ileft + wd - 1;
int ibottom = itop + ht - 1;
int cx, cy;
lifealgo* curralgo = currlayer->algo;
bool multistate = curralgo->NumCellStates() > 2;
unsigned char* cellmem = (unsigned char*) malloc(wd * ht);
if (cellmem == NULL) {
GollyError(L, "getcells error: not enough memory.");
}
curralgo->getcells(cellmem, ileft, itop, wd, ht);
unsigned char* cellptr = cellmem;
for ( cy=itop; cy<=ibottom; cy++ ) {
for ( cx=ileft; cx<=iright; cx++ ) {
unsigned char state = *cellptr++;
if (state > 0) {
// found next live cell
lua_pushinteger(L, cx); lua_rawseti(L, -2, ++arraylen);
lua_pushinteger(L, cy); lua_rawseti(L, -2, ++arraylen);
if (multistate) {
lua_pushinteger(L, state); lua_rawseti(L, -2, ++arraylen);
}
}
}
}
if (multistate && arraylen > 0 && (arraylen & 1) == 0) {
// add padding zero
lua_pushinteger(L, 0); lua_rawseti(L, -2, ++arraylen);
}
free(cellmem);
} else {
GollyError(L, "getcells error: array must be {} or {x,y,wd,ht}.");
}
return 1; // result is a cell array
}
#endif // #if 0
// -----------------------------------------------------------------------------
static int g_join(lua_State* L)
{
CheckEvents(L);
luaL_checktype(L, 1, LUA_TTABLE);
luaL_checktype(L, 2, LUA_TTABLE);
bool multi1 = (luaL_len(L, 1) & 1) == 1;
bool multi2 = (luaL_len(L, 2) & 1) == 1;
bool multiout = multi1 || multi2;
int ints_per_cell, num_cells;
int x, y, state;
lua_newtable(L);
int arraylen = 0;
// append 1st array
ints_per_cell = multi1 ? 3 : 2;
num_cells = luaL_len(L, 1) / ints_per_cell;
for (int n = 0; n < num_cells; n++) {
int item = ints_per_cell * n;
lua_rawgeti(L, 1, item+1); x = lua_tointeger(L,-1); lua_pop(L,1);
lua_rawgeti(L, 1, item+2); y = lua_tointeger(L,-1); lua_pop(L,1);
if (multi1) {
lua_rawgeti(L, 1, item+3); state = lua_tointeger(L,-1); lua_pop(L,1);
} else {
state = 1;
}
lua_pushinteger(L, x); lua_rawseti(L, -2, ++arraylen);
lua_pushinteger(L, y); lua_rawseti(L, -2, ++arraylen);
if (multiout) {
lua_pushinteger(L, state); lua_rawseti(L, -2, ++arraylen);
}
}
// append 2nd array
ints_per_cell = multi2 ? 3 : 2;
num_cells = luaL_len(L, 2) / ints_per_cell;
for (int n = 0; n < num_cells; n++) {
int item = ints_per_cell * n;
lua_rawgeti(L, 2, item+1); x = lua_tointeger(L,-1); lua_pop(L,1);
lua_rawgeti(L, 2, item+2); y = lua_tointeger(L,-1); lua_pop(L,1);
if (multi2) {
lua_rawgeti(L, 2, item+3); state = lua_tointeger(L,-1); lua_pop(L,1);
} else {
state = 1;
}
lua_pushinteger(L, x); lua_rawseti(L, -2, ++arraylen);
lua_pushinteger(L, y); lua_rawseti(L, -2, ++arraylen);
if (multiout) {
lua_pushinteger(L, state); lua_rawseti(L, -2, ++arraylen);
}
}
if (multiout && arraylen > 0 && (arraylen & 1) == 0) {
// add padding zero
lua_pushinteger(L, 0); lua_rawseti(L, -2, ++arraylen);
}
return 1; // result is a cell array
}
// -----------------------------------------------------------------------------
static int g_hash(lua_State* L)
{
CheckEvents(L);
// arg must be a table with 4 ints
luaL_checktype(L, 1, LUA_TTABLE);
int numints = luaL_len(L, 1);
if (numints != 4) {
GollyError(L, "hash error: array must have 4 integers.");
}
lua_rawgeti(L, 1, 1); int x = luaL_checkinteger(L,-1); lua_pop(L,1);
lua_rawgeti(L, 1, 2); int y = luaL_checkinteger(L,-1); lua_pop(L,1);
lua_rawgeti(L, 1, 3); int wd = luaL_checkinteger(L,-1); lua_pop(L,1);
lua_rawgeti(L, 1, 4); int ht = luaL_checkinteger(L,-1); lua_pop(L,1);
const char* err = GSF_checkrect(x, y, wd, ht);
if (err) GollyError(L, err);
lua_pushinteger(L, GSF_hash(x, y, wd, ht));
return 1; // result is an integer
}
// -----------------------------------------------------------------------------
static int g_getclip(lua_State* L)
{
CheckEvents(L);
if (!mainptr->ClipboardHasText()) {
GollyError(L, "getclip error: no pattern in clipboard.");
}
// create a temporary layer for storing the clipboard pattern
Layer* templayer = CreateTemporaryLayer();
if (!templayer) {
GollyError(L, "getclip error: failed to create temporary layer.");
}
// read clipboard pattern into temporary universe and set edges
// (not a minimal bounding box if pattern is empty or has empty borders)
bigint top, left, bottom, right;
if ( viewptr->GetClipboardPattern(templayer, &top, &left, &bottom, &right) ) {
if ( viewptr->OutsideLimits(top, left, bottom, right) ) {
delete templayer;
GollyError(L, "getclip error: pattern is too big.");
}
int itop = top.toint();
int ileft = left.toint();
int ibottom = bottom.toint();
int iright = right.toint();
int wd = iright - ileft + 1;
int ht = ibottom - itop + 1;
// push pattern's width and height (not necessarily the minimal bounding box
// because the pattern might have empty borders, or it might even be empty)
lua_pushinteger(L, wd);
lua_pushinteger(L, ht);
// now push cell array
lua_newtable(L);
int arraylen = 0;
// extract cells from templayer
lifealgo* tempalgo = templayer->algo;
bool multistate = tempalgo->NumCellStates() > 2;
int cx, cy;
int v = 0;
for ( cy=itop; cy<=ibottom; cy++ ) {
for ( cx=ileft; cx<=iright; cx++ ) {
int skip = tempalgo->nextcell(cx, cy, v);
if (skip >= 0) {
// found next live cell in this row
cx += skip;
// shift cells so that top left cell of bounding box is at 0,0
lua_pushinteger(L, cx - ileft); lua_rawseti(L, -2, ++arraylen);
lua_pushinteger(L, cy - itop ); lua_rawseti(L, -2, ++arraylen);
if (multistate) {
lua_pushinteger(L, v); lua_rawseti(L, -2, ++arraylen);
}
} else {
cx = iright; // done this row
}
}
}
// if no live cells then return {wd,ht} rather than {wd,ht,0}
if (multistate && arraylen > 2 && (arraylen & 1) == 0) {
// add padding zero
lua_pushinteger(L, 0); lua_rawseti(L, -2, ++arraylen);
}
delete templayer;
} else {
delete templayer;
GollyError(L, "getclip error: failed to get clipboard pattern.");
}
return 3; // result is wd, ht, cell array
}
// -----------------------------------------------------------------------------
static int g_select(lua_State* L)
{
CheckEvents(L);
// arg must be a table with 0 or 4 ints
luaL_checktype(L, 1, LUA_TTABLE);
BEGIN_AUTORELEASE
int numints = luaL_len(L, 1);
if (numints == 0) {
// remove any existing selection
GSF_select(0, 0, 0, 0);
} else if (numints == 4) {
lua_rawgeti(L, 1, 1); int x = luaL_checkinteger(L,-1); lua_pop(L,1);
lua_rawgeti(L, 1, 2); int y = luaL_checkinteger(L,-1); lua_pop(L,1);
lua_rawgeti(L, 1, 3); int wd = luaL_checkinteger(L,-1); lua_pop(L,1);
lua_rawgeti(L, 1, 4); int ht = luaL_checkinteger(L,-1); lua_pop(L,1);
const char* err = GSF_checkrect(x, y, wd, ht);
if (err) GollyError(L, err);
GSF_select(x, y, wd, ht);
} else {
GollyError(L, "select error: array must be {} or {x,y,wd,ht}.");
}
DoAutoUpdate();
END_AUTORELEASE
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_getrect(lua_State* L)
{
CheckEvents(L);
lua_newtable(L);
if (!currlayer->algo->isEmpty()) {
bigint top, left, bottom, right;
currlayer->algo->findedges(&top, &left, &bottom, &right);
if (viewptr->OutsideLimits(top, left, bottom, right)) {
GollyError(L, "getrect error: pattern is too big.");
}
int x = left.toint();
int y = top.toint();
int wd = right.toint() - x + 1;
int ht = bottom.toint() - y + 1;
lua_pushinteger(L, x); lua_rawseti(L, -2, 1);
lua_pushinteger(L, y); lua_rawseti(L, -2, 2);
lua_pushinteger(L, wd); lua_rawseti(L, -2, 3);
lua_pushinteger(L, ht); lua_rawseti(L, -2, 4);
}
return 1; // result is a table (empty or with 4 ints)
}
// -----------------------------------------------------------------------------
static int g_getselrect(lua_State* L)
{
CheckEvents(L);
lua_newtable(L);
if (viewptr->SelectionExists()) {
if (currlayer->currsel.TooBig()) {
GollyError(L, "getselrect error: selection is too big.");
}
int x, y, wd, ht;
currlayer->currsel.GetRect(&x, &y, &wd, &ht);
lua_pushinteger(L, x); lua_rawseti(L, -2, 1);
lua_pushinteger(L, y); lua_rawseti(L, -2, 2);
lua_pushinteger(L, wd); lua_rawseti(L, -2, 3);
lua_pushinteger(L, ht); lua_rawseti(L, -2, 4);
}
return 1; // result is a table (empty or with 4 ints)
}
// -----------------------------------------------------------------------------
static int g_setcell(lua_State* L)
{
CheckEvents(L);
int x = luaL_checkinteger(L, 1);
int y = luaL_checkinteger(L, 2);
int state = luaL_checkinteger(L, 3);
const char* err;
BEGIN_AUTORELEASE
err = GSF_setcell(x, y, state);
END_AUTORELEASE
if (err) GollyError(L, err);
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_getcell(lua_State* L)
{
CheckEvents(L);
int x = luaL_checkinteger(L, 1);
int y = luaL_checkinteger(L, 2);
// check if x,y is outside bounded grid
const char* err = GSF_checkpos(currlayer->algo, x, y);
if (err) GollyError(L, err);
lua_pushinteger(L, currlayer->algo->getcell(x, y));
return 1; // result is an integer
}
// -----------------------------------------------------------------------------
static int g_setcursor(lua_State* L)
{
CheckEvents(L);
const char* newcursor = luaL_checkstring(L, 1);
const char* oldcursor = CursorToString(currlayer->curs);
wxCursor* cursptr = StringToCursor(newcursor);
if (cursptr) {
BEGIN_AUTORELEASE
viewptr->SetCursorMode(cursptr);
// see the cursor change, including button in edit bar
mainptr->UpdateUserInterface();
END_AUTORELEASE
} else {
GollyError(L, "setcursor error: unknown cursor string.");
}
// return old cursor (simplifies saving and restoring cursor)
lua_pushstring(L, oldcursor);
return 1; // result is a string
}
// -----------------------------------------------------------------------------
static int g_getcursor(lua_State* L)
{
CheckEvents(L);
lua_pushstring(L, CursorToString(currlayer->curs));
return 1; // result is a string
}
// -----------------------------------------------------------------------------
static int g_empty(lua_State* L)
{
CheckEvents(L);
lua_pushboolean(L, currlayer->algo->isEmpty());
return 1; // result is a boolean
}
// -----------------------------------------------------------------------------
static int g_run(lua_State* L)
{
CheckEvents(L);
int ngens = luaL_checkinteger(L, 1);
if (ngens > 0 && !currlayer->algo->isEmpty()) {
BEGIN_AUTORELEASE
if (ngens > 1) {
bigint saveinc = currlayer->algo->getIncrement();
currlayer->algo->setIncrement(ngens);
mainptr->NextGeneration(true); // step by ngens
currlayer->algo->setIncrement(saveinc);
} else {
mainptr->NextGeneration(false); // step 1 gen
}
DoAutoUpdate();
END_AUTORELEASE
}
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_step(lua_State* L)
{
CheckEvents(L);
if (!currlayer->algo->isEmpty()) {
BEGIN_AUTORELEASE
mainptr->NextGeneration(true); // step by current increment
DoAutoUpdate();
END_AUTORELEASE
}
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_setstep(lua_State* L)
{
CheckEvents(L);
int exp = luaL_checkinteger(L, 1);
BEGIN_AUTORELEASE
mainptr->SetStepExponent(exp);
DoAutoUpdate();
END_AUTORELEASE
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_getstep(lua_State* L)
{
CheckEvents(L);
lua_pushinteger(L, currlayer->currexpo);
return 1; // result is an integer
}
// -----------------------------------------------------------------------------
static int g_setbase(lua_State* L)
{
CheckEvents(L);
int base = luaL_checkinteger(L, 1);
if (base < 2) base = 2;
if (base > MAX_BASESTEP) base = MAX_BASESTEP;
currlayer->currbase = base;
BEGIN_AUTORELEASE
mainptr->SetGenIncrement();
DoAutoUpdate();
END_AUTORELEASE
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_getbase(lua_State* L)
{
CheckEvents(L);
lua_pushinteger(L, currlayer->currbase);
return 1; // result is an integer
}
// -----------------------------------------------------------------------------
static int g_advance(lua_State* L)
{
CheckEvents(L);
int where = luaL_checkinteger(L, 1);
int ngens = luaL_checkinteger(L, 2);
if (ngens > 0) {
if (viewptr->SelectionExists()) {
BEGIN_AUTORELEASE
while (ngens > 0) {
ngens--;
if (where == 0)
currlayer->currsel.Advance();
else
currlayer->currsel.AdvanceOutside();
}
DoAutoUpdate();
END_AUTORELEASE
} else {
GollyError(L, "advance error: no selection.");
}
}
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_reset(lua_State* L)
{
CheckEvents(L);
if (currlayer->algo->getGeneration() != currlayer->startgen) {
BEGIN_AUTORELEASE
mainptr->ResetPattern();
DoAutoUpdate();
END_AUTORELEASE
}
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_setgen(lua_State* L)
{
CheckEvents(L);
const char* genstring = luaL_checkstring(L, 1);
const char* err;
BEGIN_AUTORELEASE
err = GSF_setgen(genstring);
END_AUTORELEASE
if (err) GollyError(L, err);
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_getgen(lua_State* L)
{
CheckEvents(L);
char sepchar = '\0';
if (lua_gettop(L) > 0) {
const char* s = luaL_checkstring(L, 1);
sepchar = s[0];
}
lua_pushstring(L, currlayer->algo->getGeneration().tostring(sepchar));
return 1; // result is a string
}
// -----------------------------------------------------------------------------
static int g_getpop(lua_State* L)
{
CheckEvents(L);
char sepchar = '\0';
if (lua_gettop(L) > 0) {
const char* s = luaL_checkstring(L, 1);
sepchar = s[0];
}
lua_pushstring(L, currlayer->algo->getPopulation().tostring(sepchar));
return 1; // result is a string
}
// -----------------------------------------------------------------------------
static int g_numstates(lua_State* L)
{
CheckEvents(L);
lua_pushinteger(L, currlayer->algo->NumCellStates());
return 1; // result is an integer
}
// -----------------------------------------------------------------------------
static int g_numalgos(lua_State* L)
{
CheckEvents(L);
lua_pushinteger(L, NumAlgos());
return 1; // result is an integer
}
// -----------------------------------------------------------------------------
static int g_setalgo(lua_State* L)
{
CheckEvents(L);
const char* algostring = luaL_checkstring(L, 1);
BEGIN_AUTORELEASE
const char* err = GSF_setalgo(algostring);
if (err) GollyError(L, err);
END_AUTORELEASE
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_getalgo(lua_State* L)
{
CheckEvents(L);
int index = currlayer->algtype;
if (lua_gettop(L) > 0) index = luaL_checkinteger(L, 1);
if (index < 0 || index >= NumAlgos()) {
char msg[64];
sprintf(msg, "getalgo error: bad index (%d)", index);
GollyError(L, msg);
}
lua_pushstring(L, GetAlgoName(index));
return 1; // result is a string
}
// -----------------------------------------------------------------------------
static int g_setrule(lua_State* L)
{
CheckEvents(L);
const char* rulestring = luaL_checkstring(L, 1);
BEGIN_AUTORELEASE
const char* err = GSF_setrule(rulestring);
if (err) GollyError(L, err);
END_AUTORELEASE
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_getrule(lua_State* L)
{
CheckEvents(L);
lua_pushstring(L, currlayer->algo->getrule());
return 1; // result is a string
}
// -----------------------------------------------------------------------------
static int g_getwidth(lua_State* L)
{
CheckEvents(L);
lua_pushinteger(L, currlayer->algo->gridwd);
return 1; // result is an integer
}
// -----------------------------------------------------------------------------
static int g_getheight(lua_State* L)
{
CheckEvents(L);
lua_pushinteger(L, currlayer->algo->gridht);
return 1; // result is an integer
}
// -----------------------------------------------------------------------------
static int g_setpos(lua_State* L)
{
CheckEvents(L);
const char* x = luaL_checkstring(L, 1);
const char* y = luaL_checkstring(L, 2);
BEGIN_AUTORELEASE
const char* err = GSF_setpos(x, y);
if (err) GollyError(L, err);
END_AUTORELEASE
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_getpos(lua_State* L)
{
CheckEvents(L);
char sepchar = '\0';
if (lua_gettop(L) > 0) {
const char* s = luaL_checkstring(L, 1);
sepchar = s[0];
}
bigint bigx, bigy;
viewptr->GetPos(bigx, bigy);
// return position as x,y strings
lua_pushstring(L, bigx.tostring(sepchar));
lua_pushstring(L, bigy.tostring(sepchar));
return 2; // result is 2 strings
}
// -----------------------------------------------------------------------------
static int g_setmag(lua_State* L)
{
CheckEvents(L);
int mag = luaL_checkinteger(L, 1);
BEGIN_AUTORELEASE
viewptr->SetMag(mag);
DoAutoUpdate();
END_AUTORELEASE
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_getmag(lua_State* L)
{
CheckEvents(L);
lua_pushinteger(L, viewptr->GetMag());
return 1; // result is an integer
}
// -----------------------------------------------------------------------------
static int g_fit(lua_State* L)
{
CheckEvents(L);
BEGIN_AUTORELEASE
viewptr->FitPattern();
DoAutoUpdate();
END_AUTORELEASE
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_fitsel(lua_State* L)
{
CheckEvents(L);
if (viewptr->SelectionExists()) {
BEGIN_AUTORELEASE
viewptr->FitSelection();
DoAutoUpdate();
END_AUTORELEASE
} else {
GollyError(L, "fitsel error: no selection.");
}
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_visrect(lua_State* L)
{
CheckEvents(L);
// arg must be a table with 4 ints
luaL_checktype(L, 1, LUA_TTABLE);
int numints = luaL_len(L, 1);
if (numints != 4) {
GollyError(L, "visrect error: array must have 4 integers.");
}
lua_rawgeti(L, 1, 1); int x = luaL_checkinteger(L,-1); lua_pop(L,1);
lua_rawgeti(L, 1, 2); int y = luaL_checkinteger(L,-1); lua_pop(L,1);
lua_rawgeti(L, 1, 3); int wd = luaL_checkinteger(L,-1); lua_pop(L,1);
lua_rawgeti(L, 1, 4); int ht = luaL_checkinteger(L,-1); lua_pop(L,1);
const char* err = GSF_checkrect(x, y, wd, ht);
if (err) GollyError(L, err);
bigint left = x;
bigint top = y;
bigint right = x + wd - 1;
bigint bottom = y + ht - 1;
int visible = viewptr->CellVisible(left, top) &&
viewptr->CellVisible(right, bottom);
lua_pushboolean(L, visible);
return 1; // result is a boolean
}
// -----------------------------------------------------------------------------
static int g_setview(lua_State* L)
{
CheckEvents(L);
int wd = luaL_checkinteger(L, 1);
int ht = luaL_checkinteger(L, 2);
if (wd < 0) wd = 0;
if (ht < 0) ht = 0;
int currwd, currht;
bigview->GetClientSize(&currwd, &currht);
if (currwd < 0) currwd = 0;
if (currht < 0) currht = 0;
BEGIN_AUTORELEASE
int mnwd, mnht;
mainptr->GetSize(&mnwd, &mnht);
mainptr->SetSize(mnwd + (wd - currwd), mnht + (ht - currht));
END_AUTORELEASE
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_getview(lua_State* L)
{
CheckEvents(L);
int index = -1;
if (lua_gettop(L) > 0) {
index = luaL_checkinteger(L, 1);
if (index < 0 || index >= numlayers) {
char msg[64];
sprintf(msg, "getview error: bad index (%d)", index);
GollyError(L, msg);
}
}
int wd, ht;
if (index == -1) {
bigview->GetClientSize(&wd, &ht);
} else {
if (numlayers > 1 && tilelayers) {
// tilerect values are only valid when multiple layers are tiled
wd = GetLayer(index)->tilerect.width;
ht = GetLayer(index)->tilerect.height;
} else {
bigview->GetClientSize(&wd, &ht);
}
}
if (wd < 0) wd = 0;
if (ht < 0) ht = 0;
lua_pushinteger(L, wd);
lua_pushinteger(L, ht);
return 2; // result is 2 integers (width and height)
}
// -----------------------------------------------------------------------------
static int g_update(lua_State* L)
{
CheckEvents(L);
BEGIN_AUTORELEASE
GSF_update();
END_AUTORELEASE
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_autoupdate(lua_State* L)
{
CheckEvents(L);
autoupdate = CheckBoolean(L, 1);
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_addlayer(lua_State* L)
{
CheckEvents(L);
if (numlayers >= MAX_LAYERS) {
GollyError(L, "addlayer error: no more layers can be added.");
} else {
BEGIN_AUTORELEASE
AddLayer();
DoAutoUpdate();
END_AUTORELEASE
}
// return index of new layer
lua_pushinteger(L, currindex);
return 1; // result is an integer
}
// -----------------------------------------------------------------------------
static int g_clone(lua_State* L)
{
CheckEvents(L);
if (numlayers >= MAX_LAYERS) {
GollyError(L, "clone error: no more layers can be added.");
} else {
BEGIN_AUTORELEASE
CloneLayer();
DoAutoUpdate();
END_AUTORELEASE
}
// return index of new layer
lua_pushinteger(L, currindex);
return 1; // result is an integer
}
// -----------------------------------------------------------------------------
static int g_duplicate(lua_State* L)
{
CheckEvents(L);
if (numlayers >= MAX_LAYERS) {
GollyError(L, "duplicate error: no more layers can be added.");
} else {
BEGIN_AUTORELEASE
DuplicateLayer();
DoAutoUpdate();
END_AUTORELEASE
}
// return index of new layer
lua_pushinteger(L, currindex);
return 1; // result is an integer
}
// -----------------------------------------------------------------------------
static int g_dellayer(lua_State* L)
{
CheckEvents(L);
if (numlayers <= 1) {
GollyError(L, "dellayer error: there is only one layer.");
} else {
BEGIN_AUTORELEASE
DeleteLayer();
DoAutoUpdate();
END_AUTORELEASE
}
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_movelayer(lua_State* L)
{
CheckEvents(L);
int fromindex = luaL_checkinteger(L, 1);
int toindex = luaL_checkinteger(L, 2);
if (fromindex < 0 || fromindex >= numlayers) {
char msg[64];
sprintf(msg, "movelayer error: bad fromindex (%d)", fromindex);
GollyError(L, msg);
}
if (toindex < 0 || toindex >= numlayers) {
char msg[64];
sprintf(msg, "movelayer error: bad toindex (%d)", toindex);
GollyError(L, msg);
}
BEGIN_AUTORELEASE
MoveLayer(fromindex, toindex);
DoAutoUpdate();
END_AUTORELEASE
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_setlayer(lua_State* L)
{
CheckEvents(L);
int index = luaL_checkinteger(L, 1);
if (index < 0 || index >= numlayers) {
char msg[64];
sprintf(msg, "setlayer error: bad index (%d)", index);
GollyError(L, msg);
}
BEGIN_AUTORELEASE
SetLayer(index);
DoAutoUpdate();
END_AUTORELEASE
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_getlayer(lua_State* L)
{
CheckEvents(L);
lua_pushinteger(L, currindex);
return 1; // result is an integer
}
// -----------------------------------------------------------------------------
static int g_numlayers(lua_State* L)
{
CheckEvents(L);
lua_pushinteger(L, numlayers);
return 1; // result is an integer
}
// -----------------------------------------------------------------------------
static int g_maxlayers(lua_State* L)
{
CheckEvents(L);
lua_pushinteger(L, MAX_LAYERS);
return 1; // result is an integer
}
// -----------------------------------------------------------------------------
static int g_setname(lua_State* L)
{
CheckEvents(L);
const char* name = luaL_checkstring(L, 1);
int index = currindex;
if (lua_gettop(L) > 1) index = luaL_checkinteger(L, 2);
if (index < 0 || index >= numlayers) {
char msg[64];
sprintf(msg, "setname error: bad index (%d)", index);
GollyError(L, msg);
}
BEGIN_AUTORELEASE
GSF_setname(wxString(name, LUA_ENC), index);
END_AUTORELEASE
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_getname(lua_State* L)
{
CheckEvents(L);
int index = currindex;
if (lua_gettop(L) > 0) index = luaL_checkinteger(L, 1);
if (index < 0 || index >= numlayers) {
char msg[64];
sprintf(msg, "getname error: bad index (%d)", index);
GollyError(L, msg);
}
lua_pushstring(L, (const char*)GetLayer(index)->currname.mb_str(LUA_ENC));
return 1; // result is a string
}
// -----------------------------------------------------------------------------
static int g_setcolors(lua_State* L)
{
CheckEvents(L);
luaL_checktype(L, 1, LUA_TTABLE);
BEGIN_AUTORELEASE
int len = luaL_len(L, 1);
if (len == 0) {
// restore default colors in current layer and its clones
UpdateLayerColors();
} else if (len == 6) {
// create gradient from r1,g1,b1 to r2,g2,b2
lua_rawgeti(L, 1, 1); int r1 = lua_tointeger(L,-1); lua_pop(L,1);
lua_rawgeti(L, 1, 2); int g1 = lua_tointeger(L,-1); lua_pop(L,1);
lua_rawgeti(L, 1, 3); int b1 = lua_tointeger(L,-1); lua_pop(L,1);
lua_rawgeti(L, 1, 4); int r2 = lua_tointeger(L,-1); lua_pop(L,1);
lua_rawgeti(L, 1, 5); int g2 = lua_tointeger(L,-1); lua_pop(L,1);
lua_rawgeti(L, 1, 6); int b2 = lua_tointeger(L,-1); lua_pop(L,1);
CHECK_RGB(r1, g1, b1, "setcolors");
CHECK_RGB(r2, g2, b2, "setcolors");
currlayer->fromrgb.Set(r1, g1, b1);
currlayer->torgb.Set(r2, g2, b2);
CreateColorGradient();
UpdateIconColors();
UpdateCloneColors();
} else if (len % 4 == 0) {
int i = 0;
while (i < len) {
lua_rawgeti(L, 1, ++i); int s = lua_tointeger(L,-1); lua_pop(L,1);
lua_rawgeti(L, 1, ++i); int r = lua_tointeger(L,-1); lua_pop(L,1);
lua_rawgeti(L, 1, ++i); int g = lua_tointeger(L,-1); lua_pop(L,1);
lua_rawgeti(L, 1, ++i); int b = lua_tointeger(L,-1); lua_pop(L,1);
CHECK_RGB(r, g, b, "setcolors");
if (s == -1) {
// set all LIVE states to r,g,b (best not to alter state 0)
for (s = 1; s < currlayer->algo->NumCellStates(); s++) {
currlayer->cellr[s] = r;
currlayer->cellg[s] = g;
currlayer->cellb[s] = b;
}
} else {
if (s < 0 || s >= currlayer->algo->NumCellStates()) {
char msg[64];
sprintf(msg, "setcolors error: bad state (%d)", s);
GollyError(L, msg);
} else {
currlayer->cellr[s] = r;
currlayer->cellg[s] = g;
currlayer->cellb[s] = b;
}
}
}
UpdateIconColors();
UpdateCloneColors();
} else {
GollyError(L, "setcolors error: array length is not a multiple of 4.");
}
DoAutoUpdate();
END_AUTORELEASE
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_getcolors(lua_State* L)
{
CheckEvents(L);
int state = -1;
if (lua_gettop(L) > 0) state = luaL_checkinteger(L, 1);
lua_newtable(L);
if (state == -1) {
// return colors for ALL states, including state 0
int tindex = 0;
for (state = 0; state < currlayer->algo->NumCellStates(); state++) {
lua_pushinteger(L, state); lua_rawseti(L, -2, ++tindex);
lua_pushinteger(L, currlayer->cellr[state]); lua_rawseti(L, -2, ++tindex);
lua_pushinteger(L, currlayer->cellg[state]); lua_rawseti(L, -2, ++tindex);
lua_pushinteger(L, currlayer->cellb[state]); lua_rawseti(L, -2, ++tindex);
}
} else if (state >= 0 && state < currlayer->algo->NumCellStates()) {
lua_pushinteger(L, state); lua_rawseti(L, -2, 1);
lua_pushinteger(L, currlayer->cellr[state]); lua_rawseti(L, -2, 2);
lua_pushinteger(L, currlayer->cellg[state]); lua_rawseti(L, -2, 3);
lua_pushinteger(L, currlayer->cellb[state]); lua_rawseti(L, -2, 4);
} else {
char msg[64];
sprintf(msg, "getcolors error: bad state (%d)", state);
GollyError(L, msg);
}
return 1; // result is a table
}
// -----------------------------------------------------------------------------
static int g_overlaytable(lua_State* L)
{
CheckEvents(L);
const char* result = NULL;
int nresults = 0;
// check the argument is a table
luaL_checktype(L, 1, LUA_TTABLE);
// get the size of the table
int n = (int)lua_rawlen(L, 1);
// check if the table contains any elements
if (n > 0) {
// get the command name
lua_rawgeti(L, 1, 1);
const char* cmd = lua_tostring(L, -1);
lua_pop(L, 1);
// check the command name was a string
if (cmd) {
result = curroverlay->DoOverlayTable(cmd, L, n, &nresults);
} else {
result = "ERR:ovtable command name must be a string";
}
} else {
result = "ERR:missing ovtable command";
}
if (result == NULL) return nresults; // no error so return any results
if (result[0] == 'E' && result[1] == 'R' && result[2] == 'R' ) {
std::string msg = "ovtable error: ";
msg += result + 4; // skip past "ERR:"
GollyError(L, msg.c_str());
}
return 0; // error so return nothing
}
// -----------------------------------------------------------------------------
static int g_overlay(lua_State* L)
{
CheckEvents(L);
const char* cmd = luaL_checkstring(L, 1);
const char* result;
if (strcmp(cmd, "update") == 0) {
// this is the only overlay command that calls Refresh
BEGIN_AUTORELEASE
result = curroverlay->DoOverlayCommand(cmd);
END_AUTORELEASE
} else {
result = curroverlay->DoOverlayCommand(cmd);
}
if (result == NULL) return 0; // no error and no result
if (result[0] == 'E' && result[1] == 'R' && result[2] == 'R' ) {
std::string msg = "overlay error: ";
msg += result + 4; // skip past "ERR:"
GollyError(L, msg.c_str());
}
lua_pushstring(L, result);
return 1; // result is a string
}
// -----------------------------------------------------------------------------
static int g_os(lua_State* L)
{
CheckEvents(L);
lua_pushstring(L, GSF_os());
return 1; // result is a string
}
// -----------------------------------------------------------------------------
static int g_millisecs(lua_State* L)
{
CheckEvents(L);
#if wxCHECK_VERSION(2,9,3)
wxLongLong t = stopwatch->TimeInMicro();
double d = t.ToDouble() / 1000.0L;
lua_pushnumber(L, (lua_Number) d);
#else
lua_pushnumber(L, (lua_Number) stopwatch->Time());
#endif
return 1; // result is a floating point number
}
// -----------------------------------------------------------------------------
static int g_sleep(lua_State* L)
{
CheckEvents(L);
int ms = luaL_checkinteger(L, 1);
wxMilliSleep(ms);
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_setoption(lua_State* L)
{
CheckEvents(L);
const char* optname = luaL_checkstring(L, 1);
int newval = luaL_checkinteger(L, 2);
int oldval;
BEGIN_AUTORELEASE
if (!GSF_setoption(optname, newval, &oldval)) {
GollyError(L, "setoption error: unknown option.");
}
END_AUTORELEASE
// return old value (simplifies saving and restoring settings)
lua_pushinteger(L, oldval);
return 1; // result is an integer
}
// -----------------------------------------------------------------------------
static int g_getoption(lua_State* L)
{
CheckEvents(L);
const char* optname = luaL_checkstring(L, 1);
int optval;
if (!GSF_getoption(optname, &optval)) {
GollyError(L, "getoption error: unknown option.");
}
lua_pushinteger(L, optval);
return 1; // result is an integer
}
// -----------------------------------------------------------------------------
static int g_setcolor(lua_State* L)
{
CheckEvents(L);
const char* colname = luaL_checkstring(L, 1);
int r = luaL_checkinteger(L, 2);
int g = luaL_checkinteger(L, 3);
int b = luaL_checkinteger(L, 4);
CHECK_RGB(r, g, b, "setcolor");
wxColor newcol(r, g, b);
wxColor oldcol;
BEGIN_AUTORELEASE
if (!GSF_setcolor(colname, newcol, oldcol)) {
GollyError(L, "setcolor error: unknown color.");
}
END_AUTORELEASE
// return old r,g,b values (simplifies saving and restoring colors)
lua_pushinteger(L, oldcol.Red());
lua_pushinteger(L, oldcol.Green());
lua_pushinteger(L, oldcol.Blue());
return 3; // result is 3 ints
}
// -----------------------------------------------------------------------------
static int g_getcolor(lua_State* L)
{
CheckEvents(L);
const char* colname = luaL_checkstring(L, 1);
wxColor color;
if (!GSF_getcolor(colname, color)) {
GollyError(L, "getcolor error: unknown color.");
}
// return r,g,b values
lua_pushinteger(L, color.Red());
lua_pushinteger(L, color.Green());
lua_pushinteger(L, color.Blue());
return 3; // result is 3 ints
}
// -----------------------------------------------------------------------------
static int g_savechanges(lua_State* L)
{
CheckEvents(L);
wxString query = wxString(luaL_checkstring(L, 1), LUA_ENC);
wxString msg = wxString(luaL_checkstring(L, 2), LUA_ENC);
int answer = SaveChanges(query, msg);
switch (answer) {
case 2: {
// user selected Yes (or Save on Mac)
lua_pushstring(L, "yes");
break;
}
case 1: {
// user selected No (or Don't Save on Mac)
lua_pushstring(L, "no");
break;
}
default: {
// answer == 0 (user selected Cancel)
lua_pushstring(L, "cancel");
}
}
return 1; // result is a string
}
// -----------------------------------------------------------------------------
static int g_settitle(lua_State* L)
{
CheckEvents(L);
// set scripttitle to avoid MainFrame::SetWindowTitle changing title
scripttitle = wxString(luaL_checkstring(L, 1), LUA_ENC);
BEGIN_AUTORELEASE
mainptr->SetTitle(scripttitle);
END_AUTORELEASE
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_setclipstr(lua_State* L)
{
CheckEvents(L);
mainptr->CopyTextToClipboard(wxString(luaL_checkstring(L, 1), LUA_ENC));
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_getclipstr(lua_State* L)
{
CheckEvents(L);
wxTextDataObject data;
if (!mainptr->GetTextFromClipboard(&data)) {
data.SetText(wxEmptyString);
}
wxString clipstr = data.GetText();
lua_pushstring(L, (const char*)clipstr.mb_str(LUA_ENC));
return 1; // result is a string
}
// -----------------------------------------------------------------------------
static int g_getstring(lua_State* L)
{
CheckEvents(L);
const char* prompt = luaL_checkstring(L, 1);
const char* initial = lua_gettop(L) > 1 ? luaL_checkstring(L, 2) : "";
const char* title = lua_gettop(L) > 2 ? luaL_checkstring(L, 3) : "";
wxString result;
if (!GetString(wxString(title, LUA_ENC),
wxString(prompt, LUA_ENC),
wxString(initial, LUA_ENC),
result)) {
// user hit Cancel button so abort script
lua_pushstring(L, abortmsg);
lua_error(L);
}
lua_pushstring(L, (const char*)result.mb_str(LUA_ENC));
return 1; // result is a string
}
// -----------------------------------------------------------------------------
static int g_getxy(lua_State* L)
{
CheckEvents(L);
BEGIN_AUTORELEASE
statusptr->CheckMouseLocation(mainptr->infront); // sets mousepos
END_AUTORELEASE
if (viewptr->showcontrols) mousepos = wxEmptyString;
lua_pushstring(L, (const char*)mousepos.mb_str(LUA_ENC));
return 1; // result is a string
}
// -----------------------------------------------------------------------------
static int g_getevent(lua_State* L)
{
CheckEvents(L);
bool get = true;
if (lua_gettop(L) > 0) get = CheckBoolean(L, 1);
wxString event;
GSF_getevent(event, get ? 1 : 0);
lua_pushstring(L, (const char*)event.mb_str(LUA_ENC));
return 1; // result is a string
}
// -----------------------------------------------------------------------------
static int g_doevent(lua_State* L)
{
CheckEvents(L);
const char* event = luaL_checkstring(L, 1);
BEGIN_AUTORELEASE
if (event[0]) {
const char* err = GSF_doevent(wxString(event, LUA_ENC));
if (err) GollyError(L, err);
}
END_AUTORELEASE
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_show(lua_State* L)
{
CheckEvents(L);
// do NOT call luaL_checkstring when inscript is false
// (chaos will ensue if an error is detected)
const char* msg = luaL_checkstring(L, 1);
BEGIN_AUTORELEASE
inscript = false;
statusptr->DisplayMessage(wxString(msg, LUA_ENC));
inscript = true;
// make sure status bar is visible
if (!showstatus) mainptr->ToggleStatusBar();
END_AUTORELEASE
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_error(lua_State* L)
{
CheckEvents(L);
// do NOT call luaL_checkstring when inscript is false
// (chaos will ensue if an error is detected)
const char* msg = luaL_checkstring(L, 1);
BEGIN_AUTORELEASE
inscript = false;
statusptr->ErrorMessage(wxString(msg, LUA_ENC));
inscript = true;
// make sure status bar is visible
if (!showstatus) mainptr->ToggleStatusBar();
END_AUTORELEASE
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_warn(lua_State* L)
{
CheckEvents(L);
const char* msg = luaL_checkstring(L, 1);
bool showCancel = true;
if (lua_gettop(L) > 1) showCancel = CheckBoolean(L, 2);
Warning(wxString(msg, LUA_ENC), showCancel);
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_note(lua_State* L)
{
CheckEvents(L);
const char* msg = luaL_checkstring(L, 1);
bool showCancel = true;
if (lua_gettop(L) > 1) showCancel = CheckBoolean(L, 2);
Note(wxString(msg, LUA_ENC), showCancel);
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_help(lua_State* L)
{
CheckEvents(L);
ShowHelp(wxString(luaL_checkstring(L, 1), LUA_ENC));
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_check(lua_State* L)
{
// CheckEvents(L);
// don't call CheckEvents here otherwise we can't safely write code like
// if g.getlayer() == target then
// g.check(false)
// ... do stuff to target layer ...
// g.check(true)
allowcheck = CheckBoolean(L, 1);
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_continue(lua_State* L)
{
// do NOT call CheckEvents here
aborted = false; // continue executing any remaining code
// if not empty, error will be displayed when script ends
scripterr = wxString(luaL_checkstring(L, 1), LUA_ENC);
return 0; // no result
}
// -----------------------------------------------------------------------------
static int g_exit(lua_State* L)
{
// script will terminate so no point calling CheckEvents here
if (lua_gettop(L) == 0) {
GSF_exit(wxEmptyString);
} else {
GSF_exit(wxString(luaL_checkstring(L, 1), LUA_ENC));
}
lua_pushstring(L, abortmsg);
lua_error(L);
return 0; // never get here (lua_error does a longjmp)
}
// -----------------------------------------------------------------------------
static const struct luaL_Reg gollyfuncs [] = {
// filing
{ "open", g_open }, // open given pattern/script/rule/html file
{ "save", g_save }, // save pattern in given file using given format
{ "opendialog", g_opendialog }, // return input path and filename chosen by user
{ "savedialog", g_savedialog }, // return output path and filename chosen by user
{ "load", g_load }, // read pattern file and return cell array
{ "store", g_store }, // write cell array to a file (in RLE format)
{ "setdir", g_setdir }, // set location of specified directory
{ "getdir", g_getdir }, // return location of specified directory
{ "getfiles", g_getfiles }, // return array of files in specified directory
{ "getpath", g_getpath }, // return the path to the current opened pattern
{ "getinfo", g_getinfo }, // return comments from pattern file
// editing
{ "new", g_new }, // create new universe and set window title
{ "cut", g_cut }, // cut selection to clipboard
{ "copy", g_copy }, // copy selection to clipboard
{ "clear", g_clear }, // clear inside/outside selection
{ "paste", g_paste }, // paste clipboard pattern at x,y using given mode
{ "shrink", g_shrink }, // shrink selection
{ "randfill", g_randfill }, // randomly fill selection to given percentage
{ "flip", g_flip }, // flip selection top-bottom or left-right
{ "rotate", g_rotate }, // rotate selection 90 deg clockwise or anticlockwise
{ "parse", g_parse }, // parse RLE or Life 1.05 string and return cell array
{ "transform", g_transform }, // apply an affine transformation to cell array
{ "evolve", g_evolve }, // generate pattern contained in given cell array
{ "putcells", g_putcells }, // paste given cell array into current universe
{ "getcells", g_getcells }, // return cell array in given rectangle
// { "getcells2", g_getcells2 }, // experimental version (needs more thought!!!)
{ "join", g_join }, // return concatenation of given cell arrays
{ "hash", g_hash }, // return hash value for pattern in given rectangle
{ "getclip", g_getclip }, // return pattern in clipboard (as wd, ht, cell array)
{ "select", g_select }, // select {x, y, wd, ht} rectangle or remove if {}
{ "getrect", g_getrect }, // return pattern rectangle as {} or {x, y, wd, ht}
{ "getselrect", g_getselrect }, // return selection rectangle as {} or {x, y, wd, ht}
{ "setcell", g_setcell }, // set given cell to given state
{ "getcell", g_getcell }, // get state of given cell
{ "setcursor", g_setcursor }, // set cursor (returns old cursor)
{ "getcursor", g_getcursor }, // return current cursor
// control
{ "empty", g_empty }, // return true if universe is empty
{ "run", g_run }, // run current pattern for given number of gens
{ "step", g_step }, // run current pattern for current step
{ "setstep", g_setstep }, // set step exponent
{ "getstep", g_getstep }, // return current step exponent
{ "setbase", g_setbase }, // set base step
{ "getbase", g_getbase }, // return current base step
{ "advance", g_advance }, // advance inside/outside selection by given gens
{ "reset", g_reset }, // restore starting pattern
{ "setgen", g_setgen }, // set current generation to given string
{ "getgen", g_getgen }, // return current generation as string
{ "getpop", g_getpop }, // return current population as string
{ "numstates", g_numstates }, // return number of cell states in current universe
{ "numalgos", g_numalgos }, // return number of algorithms
{ "setalgo", g_setalgo }, // set current algorithm using given string
{ "getalgo", g_getalgo }, // return name of given or current algorithm
{ "setrule", g_setrule }, // set current rule using given string
{ "getrule", g_getrule }, // return current rule
{ "getwidth", g_getwidth }, // return width of universe (0 if unbounded)
{ "getheight", g_getheight }, // return height of universe (0 if unbounded)
// viewing
{ "setpos", g_setpos }, // move given cell to middle of viewport
{ "getpos", g_getpos }, // return x,y position of cell in middle of viewport
{ "setmag", g_setmag }, // set magnification (0=1:1, 1=1:2, -1=2:1, etc)
{ "getmag", g_getmag }, // return current magnification
{ "fit", g_fit }, // fit entire pattern in viewport
{ "fitsel", g_fitsel }, // fit selection in viewport
{ "visrect", g_visrect }, // return true if given rect is completely visible
{ "setview", g_setview }, // set pixel dimensions of viewport
{ "getview", g_getview }, // get pixel dimensions of viewport
{ "update", g_update }, // update display (viewport and status bar)
{ "autoupdate", g_autoupdate }, // update display after each change to universe?
// layers
{ "addlayer", g_addlayer }, // add a new layer
{ "clone", g_clone }, // add a cloned layer (shares universe)
{ "duplicate", g_duplicate }, // add a duplicate layer (copies universe)
{ "dellayer", g_dellayer }, // delete current layer
{ "movelayer", g_movelayer }, // move given layer to new index
{ "setlayer", g_setlayer }, // switch to given layer
{ "getlayer", g_getlayer }, // return index of current layer
{ "numlayers", g_numlayers }, // return current number of layers
{ "maxlayers", g_maxlayers }, // return maximum number of layers
{ "setname", g_setname }, // set name of given layer
{ "getname", g_getname }, // get name of given layer
{ "setcolors", g_setcolors }, // set color(s) used in current layer
{ "getcolors", g_getcolors }, // get color(s) used in current layer
{ "overlay", g_overlay }, // do an overlay command from a string
{ "ovtable", g_overlaytable }, // do an overlay command from a table
// miscellaneous
{ "os", g_os }, // return the current OS (Windows/Mac/Linux)
{ "millisecs", g_millisecs }, // return elapsed time since Golly started, in millisecs
{ "sleep", g_sleep }, // sleep for the given number of millisecs
{ "savechanges", g_savechanges }, // show standard save changes dialog and return answer
{ "settitle", g_settitle }, // set window title to given string
{ "setoption", g_setoption }, // set given option to new value (and return old value)
{ "getoption", g_getoption }, // return current value of given option
{ "setcolor", g_setcolor }, // set given color to new r,g,b (returns old r,g,b)
{ "getcolor", g_getcolor }, // return r,g,b values of given color
{ "setclipstr", g_setclipstr }, // set the clipboard contents to a given string value
{ "getclipstr", g_getclipstr }, // retrieve the contents of the clipboard as a string
{ "getstring", g_getstring }, // display dialog box and get string from user
{ "getxy", g_getxy }, // return current grid location of mouse
{ "getevent", g_getevent }, // return keyboard/mouse event or empty string if none
{ "doevent", g_doevent }, // pass given keyboard/mouse event to Golly to handle
{ "show", g_show }, // show given string in status bar
{ "error", g_error }, // beep and show given string in status bar
{ "warn", g_warn }, // show given string in warning dialog
{ "note", g_note }, // show given string in note dialog
{ "help", g_help }, // show given HTML file in help window
{ "check", g_check }, // allow event checking?
{ "continue", g_continue }, // continue executing code after a pcall error
{ "exit", g_exit }, // exit script with optional error message
{NULL, NULL}
};
// -----------------------------------------------------------------------------
static int create_golly_table(lua_State* L)
{
// create a table with our g_* functions and register them
luaL_newlib(L, gollyfuncs);
return 1;
}
// -----------------------------------------------------------------------------
// we want to allow a Lua script to call another Lua script via g_open
// so we use lua_level to detect if a Lua state has already been created
static int lua_level = 0;
static lua_State* L = NULL;
void RunLuaScript(const wxString& filepath)
{
if (lua_level == 0) {
aborted = false;
L = luaL_newstate();
luaL_openlibs(L);
// we want our g_* functions to be called from Lua as g.*
lua_pushcfunction(L, create_golly_table); lua_setglobal(L, "golly");
// It would be nice if we could do this now:
//
// luaL_dostring(L, "local g = golly()");
//
// But it doesn't work because g goes out of scope, so users
// will have to start their scripts with that line.
// Note that we could do this:
//
// luaL_dostring(L, "g = golly()");
//
// But it's ~10% slower to access functions because g is global.
// append gollydir/Scripts/Lua/?.lua and gollydir/Scripts/Lua/?/init.lua
// to package.path so scripts can do things like this:
// local gp = require "gplus"
// local gpt = require "gplus.text" ('.' will be changed to path separator)
wxString luadir = gollydir;
luadir += wxT("Scripts");
luadir += wxFILE_SEP_PATH;
luadir += wxT("Lua");
luadir += wxFILE_SEP_PATH;
wxString pstring = wxT("package.path = package.path..';");
pstring += luadir;
pstring += wxT("?.lua;");
pstring += luadir;
pstring += wxT("?");
pstring += wxFILE_SEP_PATH;
pstring += wxT("init.lua'");
// convert any backslashes to "\\" to avoid "\" being treated as escape char
// (definitely necessary on Windows because wxFILE_SEP_PATH is a backslash)
pstring.Replace(wxT("\\"), wxT("\\\\"));
luaL_dostring(L, (const char*)pstring.mb_str(LUA_ENC));
}
lua_level++;
// don't use wxConvUTF8 in next line because caller has already converted
// filepath to decomposed UTF8 if on a Mac
if (luaL_dofile(L, (const char*)filepath.mb_str(wxConvLocal))) {
scripterr += wxString(lua_tostring(L,-1), LUA_ENC);
scripterr += wxT("\n");
// scripterr is checked at the end of RunScript in wxscript.cpp
lua_pop(L,1);
}
lua_level--;
if (lua_level == 0) {
lua_close(L);
}
}
// -----------------------------------------------------------------------------
void AbortLuaScript()
{
// calling lua_error here causes nasty problems (presumably due to doing
// a longjmp from inside Yield) so we set the aborted flag and check it
// in CheckEvents only after Yield has finished
aborted = true;
}
// -----------------------------------------------------------------------------
void FinishLuaScripting()
{
// no need to do anything
}
|