File: ant_lua.txt

package info (click to toggle)
enigma 1.30%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 76,132 kB
  • sloc: xml: 162,251; cpp: 67,393; ansic: 28,606; makefile: 1,986; sh: 1,298; yacc: 288; perl: 84; sed: 16
file content (2263 lines) | stat: -rw-r--r-- 81,503 bytes parent folder | download | duplicates (5)
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
ant.lua documentation
Petr Machata, ant_39@centrum.cz

This document describes usage of ant.lua, the collection of helper
functions for Enigma map designers.


================================================================================
0] PREFACE
================================================================================

0.1 Revision history
--------------------------------------------------------------------------------

version 0.1.4: 2004-02-09:
  Several typos fixed
  add_multitag removed

version 0.1.3: 2003-06-19:
  Proofread, some typos fixed, some sentences changed
  Added chapter 'Puzzle kinds'

version 0.1.2: 2003-05-11:
  Added chapter 'Multiple wholes per one target'
  Changes in a chapter 'use_cells'

version 0.1.1: 2003-04-25:
  Number of typos fixed

version 0.1: 2003-04-13:
  this covers ant.lua for upcoming Enigma 0.8
  first release of document


0.2 Table of contents
--------------------------------------------------------------------------------

0] PREFACE
   0.1 Revision history
   0.2 Table of contents

1] INTRODUCTION
   1.1 What is this document
   1.2 How to contact author
   1.3 Distribution policy
   1.4 New versions of this document
   1.5 Bugs

2] ABOUT ant.lua
   2.1 What is ant.lua
   2.2 What is it good for?
   2.3 How to use it

3] VISUAL MAP DESIGN
   3.1 Simple things first
      3.1.1 What is it, how to use it
      3.1.2 Using cell{}
      3.1.3 Parents of cell{}
      3.1.4 Drawing map
   3.2 More complicated then
      3.2.1 Default parents
      3.2.2 Default cell meanings
      3.2.4 use_cells
      3.2.3 Layered maps
      3.2.4 Drawing per partes
      3.2.5 Multichar maps
   3.3 cell{} to depth
      3.3.1 Checker floor
      3.3.2 Random floor
      3.3.3 Curried function construction

4] FUNCTIONAL APPROACH
   4.1 Fills, borders, ...
   4.2 init.lua counterparts - set_, draw_
   4.3 Coordinate mangling

5] OBJECT GROUPS
   5.1 Introduction
   5.2 Common multiples
      5.2.1 Multielement functions
      5.2.2 Group actions
      5.2.3 Rubber bands
   5.3 Generic multielements
      5.3.1 add_multicell
      5.3.2 add_multiobject
   5.4 Worm holes
      5.4.1 Forewords
      5.4.2 Technical background
      5.4.3 Setting up cell{} functions
      5.4.4 Setting up the map
      5.4.5 Post-execution code
      5.4.6 Multiple wholes per one target
   5.5 Railways
      5.5.1 What are railways
      5.5.2 Technical background
      5.5.3 Setting up cell{} functions
      5.5.4 Binding the train to railway
      5.5.5 Setting up the map
   5.6 Puzzles
      5.6.1 Forewords
      5.6.2 Technical background
      5.6.3 Setting up puzzle
      5.6.4 Fake puzzles
      5.6.5 Puzzle kinds
   5.7 Slopes
      5.7.1 Forewords
      5.7.2 Technical background
      5.7.3 Setting up cell{} functions
      5.7.4 Setting up the map
      5.7.5 Post-execution code
      5.7.6 Mixing several slopes
      5.7.7 Fake slopes
      5.7.8 Invert slopes
   5.8 Afterwords

6] HELPER FUNCTIONS
   6.1 Debugging
      6.1.1 Warnings
      6.1.2 Debugs
   6.2 Clone table
   6.3 Sending messages


================================================================================
[1] INTRODUCTION
================================================================================

1.1 What is this document
--------------------------------------------------------------------------------

This document tries to uncover hidden treasures of ant.lua, the library
of helper functions for Enigma level designers. If one level is
created with help of this, I reached my goal.

Please, no great expectations. I'm not very experienced in English and
I have never written such a document before. I did my best, but you
know how it is...


1.2 How to contact author
--------------------------------------------------------------------------------

Any questions, bug reports, misunderstandings, typos, flames and
praises, regarding both this documentation and ant.lua library, please
send to:

    my e-mail:        ant_39 at centrum.cz
    enigma-devel ML:  enigma-devel at nongnu.org


1.3 Distribution policy
--------------------------------------------------------------------------------

This documentation is covered by GNU GPL, v.2 or later. Interpret the
document's source text as the 'program' and adhere to the following
terms:

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA


1.4 New versions of this document
--------------------------------------------------------------------------------

Most probably, any new version will be located directly at Enigma CVS
repository. If it isn't there, ant.lua was probably discarded from
source tree either, and so the documentation is obsolete.


1.5 Bugs
--------------------------------------------------------------------------------

There are several things missing in the document yet. There could be a
complete reference of ant.lua, though I'm not very sure whether
ant.lua itself is not better source (it's quite well commented, imho).

Not all examples were tested. I often rely on my knowledge, so if
something doesn't work, the bug doesn't have to be in library.

Besides this, the document should be completely rewritten by someone
better in English. This is utopia, so we simply have to get enough
with this.



================================================================================
2] ABOUT ant.lua
================================================================================

2.1 What is ant.lua
--------------------------------------------------------------------------------

ant.lua is lua module that should help level designers to create their
maps. Its acronym stands for `All Neccessary Tools'*. At this time,
graphical level editor is being worked on, or maybe the works already
pushed it to somehow usable stage. In older times there was nothing
alike, so enigma maps were created by manually placing stones and
floor squares to their places in a simple programming language. This
tool made it a way more comfortable to me.

It's possible that the editor will make this module useless, so
further development will stop. It will probably stay in enigma source
repository in future, as many maps and also some other libraries
(ralf.lua for example) use it.

*) Well, not really. It's named thus just because it's originator's
 nickname was ant_39. But it *could* stand for this :)


2.2 What is it good for?
--------------------------------------------------------------------------------

Well, for creating enigma maps.

Now seriously. Basic idea behind creating maps to enigma is that you
use a function every time you want to place a stone, every time you
place a floor square, etc. You draw the level procedurally, use
functions for filling, distribute items, stones, floors, actors,
etc. There are loops, well known from programming languages (while,
repeat, for, for each), but level creation process is not very simple
even with them.

For this reason the level developers tend to use some sort of visual
map creation. You draw the level to a simple string grid, tell to the
library that all #'s are stone walls and all *'s are say puzzle
stones, and then call a function. It reads your definitions, looks to
string map that you have just made, and turns them to ugly LUA code,
that in turn changes to beautiful and precious elements of Enigma
world.


2.3 How to use it
--------------------------------------------------------------------------------

Let's look at how a simple map can look like, if it's created with
ant.lua. First, how does it look without it:

      enigma.ConserveLevel = TRUE
      levelw = 20
      levelh = 13

      create_world(levelw, levelh)
      oxyd_default_flavor = "a"       -- Default flavor for oxyd stones.

      draw_border("st-brownie")
      fill_floor("fl-hay", 0,0, level_width,level_height)

      set_stone("st-fart", levelw-1,0, {name="fart"})
      set_stone("st-timer", 0,0, {action="trigger", target="fart", interval=10})

      oxyd(3,3)
      oxyd(level_width-4,level_height-4)
      oxyd(level_width-4, 3)
      oxyd(3,level_height-4)
      oxyd_shuffle()

      set_actor("ac-blackball", 10,6.5, {player=0})

This is a welcome map for Enigma. First level in Enigma level
package. You have to have a great imagination to create maps in such a
way. You have to use precious coordinates and dozens of commands,
nothing very simple. Great for small levels and fast stings, but
complicated levels are difficult to create this way.

Using ant.lua, the code turns to look this way:

      Require("levels/ant.lua")

      cells={}
      cells[" "]=cell{floor="fl-sand"}
      cells["#"]=cell{stone="st-greenbrown"}
      cells["~"]=cell{stone={"st-timer", {action="trigger", target="fart", interval=10}}}
      cells["*"]=cell{stone={"st-fart", {name="fart"}}}

      level = {
         "~##################*",
         "#                  #",
         "#                  #",
         "#  0            0  #",
         "#                  #",
         "#                  #",
         "#         O        #",
         "#                  #",
         "#                  #",
         "#  0            0  #",
         "#                  #",
         "#                  #",
         "####################"
      }

      oxyd_default_flavor = "a"
      set_default_parent(cells[" "])
      create_world_by_map(level)
      oxyd_shuffle()


Generally, you will save no lines of code, but make things look
clear. This will ease maintenance works, debugging (yes, even in lua
maps there can be bugs...) and so on. If other people look to your
code, it will be clear to them what have you meant, where to look for
particular elements, and so on.

At the very top of file, you have to include ant.lua with the Require
function. Then you prepare name space of your elements, this time it's
called cells. You may have several maps in your file, each of them
using different name space, so that "#" in one map is stone wall and
in other map it's metal block. This is seldom used, but it's possible.

Then, you occupy name space with cell keys. In our example, there are
four keys - "#" for green-brown stone, " " for sand floor, "~" for
timer stone, that triggers fart each 10 seconds, and "*", the fart
stone itself.

After cells{}, there is a map itself. It incorporates all of the
declared elements (cell keys), plus element "O" (letter o), which
automatically stands for black marble (if you don't override it), and
element "0" (a zero), which is oxyd stone (again, if you don't change
it).

Finally, oxyd flavor is selected (the way oxyd stones will look like),
default parent is set up, map is created, oxyd stones are mixed. All
done.

We will look at default parent later on. You should know for now, that
this is a default "base" element for each map cell. The command itself
tells to ant.lua: "let there be sand floor everywhere, if not said
otherwise".

Well, this is enough for now. If you are interested, read on, really
interesting things are yet to be said. It you are not, I'm sorry for
bothering you. I really didn't meant to.


================================================================================
3] VISUAL MAP DESIGN
================================================================================

3.1 Simple things first
--------------------------------------------------------------------------------

3.1.1 What is it, how to use it
...............................

The thing that you have just seen was an example of visual map
design. You draw the map to the string grid, declare which key
produces which element, and let ant.lua draw the thing. The way how
your string map looks like is very near to how the world will look
like once it gets rendered, thus visual.

ant.lua has also the features that make your life easier even if you
want to use procedural way of map design. These are not subject of
this chapter and are to be discussed later.


3.1.2 Using cell{}
..................

Once more, look at the declaration of cell keys:

      cells={}
      cells[" "]=cell{floor="fl-sand"}
      cells["#"]=cell{stone="st-greenbrown"}
      cells["~"]=cell{stone={"st-timer", {action="trigger", target="fart", interval=10}}}
      cells["*"]=cell{stone={"st-fart", {name="fart"}}}

First you create table of cell functions. Then you fill it with keys,
and each key bind with its meaning. The core part of this declaration
is function cell(). It's to be seen on each declaration line, and it
can be used to declare any combination of floor, stone, item and
actor. It can also "inherit" from other cell function, which will be
discussed later.

Generally, there are four ways how to use cell function:

      cells[" "]=cell{floor="fl-sand"}
      cells["#"]=cell{stone="st-rock2"}
      cells["s"]=cell{item= "it-spring2"}
      cells["o"]=cell{actor="ac-whiteball-small"}

That is, you can create any part of Enigma world with cell
functions. You can also mix things together, like this:

      cells["&"]=cell{floor="fl-water", stone="st-grate1"}

Each stone, actor, item and floor may have an attributes. Attributes
of an object affect its behavior. For example, look at switch
stone. This stone can make an action. It can open doors, turn boulders,
turn on lasers etc. Zillion of things. Attributes say what exactly
will it do. Or timer stone (we had one in an example above). Timer
stone does an action in exactly specified intervals. Kind of action and
the interval are driven, surprisingly, by an attributes.

Attributes of element are defined this way:

      cells["~"]=cell{stone={"st-timer", {action="trigger", target="fart", interval=10}}}
      cells["*"]=cell{stone={"st-fart", {name="fart"}}}

Syntax is as follows:

      something = cell{stone={"st-something", {attrib1=value1, attrib2=value2, ...}}}

And similar for floors and others.

Knowledge of attributes is basic for customizing behavior of the
objects. You can get it by looking at a
/enigma-dir/doc/CREATING-LEVELS, or, better, by studying Enigma
sources. Particularly the file /enigma-dir/src/objects.cc and
/enigma-dir/src/items.cc. I believe that a better documentation is
underway.


3.1.3 Parents of cell{}
.......................

Mechanism of parents provides mechanism that arranges cell keys to
hierarchical trees. Imagine this: you want to have two surfaces in
your map. A grass and a sand. Also you would like to have a grate
stone over each of the surfaces. You can do something like this:

      cells[" "]=cell{floor="fl-sand"}
      cells["."]=cell{floor="fl-leaves"}
      cells["x"]=cell{floor="fl-sand",   stone="st-grate1"}
      cells["X"]=cell{floor="fl-leaves", stone="st-grate1"}

or, better:

      cells[" "]=cell{floor="fl-sand"}   -- sand alone
      cells["."]=cell{floor="fl-leaves"} -- grass alone
      cells["x"]=cell{parent=cells[" "], stone="st-grate1"} -- grate over sand
      cells["X"]=cell{parent=cells["."], stone="st-grate1"} -- grate over leaves

This is better, because once you change the surface (let's say you
like fl-rough better) you have to change it in just one place. But
still, it can be improved a bit:

      cells[" "]=cell{floor="fl-sand"}   -- sand alone
      cells["."]=cell{floor="fl-leaves"} -- grass alone
      cells["g"]=cell{stone="st-grate1"} -- grate alone
      cells["x"]=cell{parent={cells[" "], cells["g"]}}
      cells["X"]=cell{parent={cells["."], cells["g"]}}

And this is perfectly generic. You can replace st-grate1 with
st-grate2 and everything changes by itself. You replace fl-sand with
fl-rough, and sand changes to rough floor everywhere.

This is one point where procedural way of creating levels is
better. In map, you have to differ between grate on sand and grate on
leaves, like here:

      level = {
         "####################",
         "#....     x    ....#",
         "#XXXXxxxxxx    ....#",
         "#....    xxxxxxXXXX#",
         "#....    x     ....#",
         "####################"
      }

Hierarchy:

        cells[" "]  \
                     > -> cells["x"]
        cells["g"]  <
                     > -> cells["X"]
        cells["."]  /

You can create a way more complicated trees. You may not create
circular inheritance (A has parent B, whereas B has parent A). LUA
won't let you do so, but it wouldn't be very wise to do it either.

(In fact, it is possible to workaround it and create such a
circularity, but it brings no extra functionality, except for some
stack overflows)

In fact, parent function may be any function that accepts x and y as
it's first two arguments. For example:

         function pr(x,y)
            print(x, y)
         end

         ... some LUA code ...

         cells["w"] = cell{parent={pr, cells["p"]}}

This code writes x and y coordinates of element "w" for each place
where element is located. This example itself is good for nothing,
there are better usages of this principle. We will discuss them later,
in a chapter about multiples.


3.1.4 Drawing map
.................

You already saw an example map at the beginning of this document.
Well, that was exactly how the maps are created. You make up the
string grid, where each char represents one square of Enigma
world. Map may be of any size, but it should be at least 20x13
squares, one screen map.

At the end of level file, there is usually located a combo of level
creating commands. Let's look:

      oxyd_default_flavor = "a"
      set_default_parent(cells[" "])
      create_world_by_map(level)
      oxyd_shuffle()

More about oxyd_default_flavor variable should be said at
/enigma-dir/doc/functions.html. Generally, this variable controls how
do the oxyd stones look like. At the time of writing this, it's
possible to select between "a", "b", "c", and (surprisingly) "d". It's
expectable that next flavor will be "e", should there be any at all.

Default parent will be discussed later.

Then, there is create_world_by_map. There are several more approaches
available, plus you can select a name space of cells. Full command
looks this way:

      create_world_by_map(level, cells)

What means, use the map "level". Meaning of chars of this map is
described in a table named "cells". However, you may omit the second
argument. If you do so, ant.lua will automatically pick a table named
"cells". If there is none, a warning will be displayed, and empty
table used instead.


3.2 More complicated then
--------------------------------------------------------------------------------

3.2.1 Default parents
.....................

Now we get to promised default parent. You already know that each cell
can have a parent. Any function at all can become a parent of some
cell, but it's usual to use another cells as a parents. This makes
level designing somehow better to understand and once written levels
are easier to maintain.

This is everything very nice, but imagine a level built up completely
on sand. Every floor square is sand. What happens? Look:

      cells[" "] = cell{floor="fl-sand"}
      cells["#"] = cell{parent=cells[" "], stone="st-rock3"}
      cells["D"] = cell{parent=cells[" "], stone="st-death"}
      cells["W"] = cell{parent=cells[" "], stone="st-wood"}
      ...

It's boring cut-and-paste. If a single function is automatically
parent of everything, there is no need to declare it this way. You can
simply put down:

      set_default_parent(cells[" "])

Well, what if there is a grassy field in the middle of all that sand?
No problem. Default parents get executed before any other parents, and
these are in turn executed before any other cell elements. That means,
that common parents always override default parents, and common cell
elements (like stone, floor, ...) always override any parent. So, this
is perfectly legal:

      cells[" "] = cell{floor="fl-sand"}
      cells["."] = cell{floor="fl-leaves"}
      cells[":"] = cell{parent=cells["."], floor="fl-himalaya"}
      ...
      set_default_parent(cells[" "])

It works as expected: all spaces in map are sand, and it's OK, as sand
is default parent. All dots mean grass, even if default parent is
sand. And all colons are replaced with himalayas stone floor, even if
grass is declared as a parent and sand as a default parent.

In fact, this is both power and weakness of the system. Parents are
executed every time. They're all functions, so the engine doesn't know
what exactly they're doing. It's waste of time - all default parents
are executed for each cell every time it gets displayed, and all cell
common parents the same. The engine doesn't know that nine out of ten
parents set floor, and so he sets floor nine times.


3.2.2 Default cell meanings
...........................

After first ten levels or so, you note that you use the same symbols
for particular entities. For example letter 'O' for black marble,
number '0' for oxyd stone, '#' for stone wall and 'D' for death
stone. This made me do something like common cell meaning.

Default meanings save time and code in case that you use the component
in standard way. You don't have to declare such a component and
ant.lua automatically pick its default meaning. For example, look
again to the welcome.lua example:

      cells={}
      cells[" "]=cell{floor="fl-sand"}
      cells["#"]=cell{stone="st-greenbrown"}
      cells["~"]=cell{stone={"st-timer", {action="trigger", target="fart", interval=10}}}
      cells["*"]=cell{stone={"st-fart", {name="fart"}}}

      level = {
         "~##################*",
         "#                  #",
         "#                  #",
         "#  0            0  #",
         "#                  #",
         "#                  #",
         "#         O        #",
         "#                  #",
         "#                  #",
         "#  0            0  #",
         "#                  #",
         "#                  #",
         "####################"
      }

The cells '0' and 'O' are not declared at all, still they may be used
at a string map. That is because 'O' and '0' are bound to black marble
actor and oxyd stone by default. There are more of a kind:

      '.' stands for "fl-abyss"
      'o' stands for "ac-whiteball-small"
      'W' stands for "st-wood"
      'B' stands for "st-block"
      'D' stands for "st-death"
      '=' stands for "st-glass"
      'X' stands for "st-grate"

And maybe some more will be declared in future. Look to ant.lua, to
section 'MEANINGS FOR COMMON CELL KEYS'.

Besides this, there is a bunch of map moods and modes. These moods and
modes map some other cell meanings, if you turn them on. If you write
this into your level file:

      meditation_mode()

you turn on meditation mode. In this mode, letter 'O' has a special
meaning of a floor pit (hollow) that has to be occupied by a small
white marble for the level to be finished.

Another modes and their bindings:

      multiplayer_mode()
        '1' stands for black marble
        '2' stands for white marble
        each of these marbles gets an it-yinyang automatically, so that
        the player can switch between the marbles. Letter 'O' has still
        its meaning of black marble without a yinyang item.

      grass_mode()
        '#' stands for "st-rock1"
        ' ' stands for "fl-leaves"

      metal_mode()
        '#' stands for "st-rock2"
        ' ' stands for "fl-metal"

It's alike that more modes are to come in future.

If you want to override the common meaning, no problem. Just declare
the element in your cells table. ant.lua will look for default meaning
only in case it doesn't find it there.


3.2.4 use_cells
...............

When you rely on a default cell meanings, you happen to miss many of
default meanings in your cells table. For example, you don't have to
declare cells["#"] in metal_mode, as '#' stands for metal stone
already.

Well, but what if you want to use given cell as a parent? What if you
want to place an actor to non-default floor. There is a sand
everywhere in your map, so it's reasonable to use the sand for default
parent. But you need an actor on metal floor. Typical construction
looks like this:

      cells = {}
      cells["_"]=cell{floor="fl-metal"}
      cells["*"]=cell{parent={cells["_"], cells["O"]}}

Ha! But there is no cells["O"], because you use default meanings. And
who wants to declare cells["O"] - there are default meanings for you
not to have to do this. Well, there is a function that helps you in
such a situations:

      cells = {}
      use_cells("O")
      cells["_"]=cell{floor="fl-metal"}
      cells["*"]=cell{parent={cells["_"], cells["O"]}}

That's it. Even better, if you have only one actor in your map, you
can override default meaning:

      cells = {}
      use_cells("O")
      cells["_"]=cell{floor="fl-metal"}
      cells["O"]=cell{parent={cells["_"], cells["O"]}}

The function can also get a cellfuncs-table as an argument. This is
particularly necessary if you have your definitions in a table with
the name different from 'cells'.

      cells2 = {}
      use_cells(cells2, "O", "D")

It's not very common to use 'use_cells' in map, but it happens from
time to time.


3.2.3 Layered maps
..................

It's also possible to map surfaces, stones and items separately.
Three maps are created then, and you write:

      create_world_by_map(floors, fcells)
      draw_map(0, 0, stones, scells)
      draw_map(0, 0, items,  icells)

If you have only one table of cell meanings, named 'cells', you may
write this, as 'cells' is being looked for by default:

      create_world_by_map(floors)
      draw_map(0, 0, stones)
      draw_map(0, 0, items)

The only tricky thing to avoid is default parent - you may not use
default parents in layered maps, or, the default parent may not change
map itself. Imagine sand floor for the default parent. You let ant.lua
draw floors, and it's OK. Then you let draw stones, and all floors are
overridden by default parent. Beware. Or, draw layers in reverse
order.


3.2.4 Drawing per partes
........................

Currently, it's possible to draw a map as a whole, by one command, or,
for more precious drawing, draw a part of map, or draw just the
squares you want to be drawn.

These functions are participating in drawing process. They're
organized so that the most low-level function is first and the most
high-level last.

      function render_key(rx, ry, key, cellfuncs)
      function get_cell_by_xy(mx, my, map)
      function render_map_cell(rx0, ry0, mx, my, map, cellfuncs)
      function draw_map_portion(rx0, ry0, mxy0, mxy1, map, cellfuncs)
      function draw_map(rx0, ry0, map, cellfuncs)
      function prepare_world_by_map(map)
      function create_world_by_map(map, cellfuncs)

Now we'll look on those functions briefly.

  render_key: render one square of world
    rx, ry: which square to render
    key:    the cell key, like in string map
    cellfuncs: table of cell functions, may be omitted
    *example: render_key(5,15, '#')
              render_key(5,15, ' ', cells)

  get_cell_by_xy: get a key at given map location
    mx, my: coordinates of key at map
    map:    string map

  render_map_cell: draw given square of map
    rx0, ry0: where should be left top corner of map located in world
    mx, my:   coordinates of the cell in map
    map:      the map
    cellfuncs:table of cell functions, may be omitted

  draw_map_portion: draw given part of map
    rx0, ry0: where should be left top corner of map located in world
    mxy0:     {mx0,my0} -> coordinates of left top corner of portion
    mxy1:     {mx1,my1} -> coordinates of right bottom corner
    map:      string map
    cellfuncs:table of cell functions, may be omitted
    *example: draw_map_portion(0,0, {2,5}, {10,7}, level, cells)

  draw_map: draw whole map
    rx0, ry0: world coordinates of left top corner of map
    map:      string map to be drawn
    cellfuncs:table of cell functions, may be omitted

  prepare_world_by_map: create Enigma world with the size by given map
    map: string map, which the size of the world is taken from

  create_world_by_map: create and draw Enigma world
    map:       string map of the world
    cellfuncs: table of cell functions, may be omitted


3.2.5 Multichar maps
....................

It could happen that you need to use the map, where each cell is
defined by more than one character. You may simply create the map so
complex, that you run out of chars. You may create the map, where each
key is composed of three chars, first declaring floor type, second
stone, third item or actor. Generally it's not used, but there is a
possibility to do so. Functions in ant.lua can work with such a maps.

There is only one thing that you have to do, to let ant.lua know what
key width you are using, that is, how many chars are the keys composed
of. That thing is:

      set_cell_key_width(w)

where 'w' stands for the width of key. By default it's 1, and you can
use any positive whole number that you want.

String maps have to reflect cell key width. If you have got a cell key
width of 2, strings in map simply cannot have lengths like 7 or
13. Let's convert our example map to multichar:

      Require("levels/ant.lua")

      cells={}
      cells["  "]=cell{floor="fl-sand"}
      cells["##"]=cell{stone="st-greenbrown"}
      cells["~~"]=cell{stone={"st-timer", {action="trigger", target="fart", interval=10}}}
      cells["**"]=cell{stone={"st-fart", {name="fart"}}}

      level = {
         "~~####################################**",
         "##                                    ##",
         "##                                    ##",
         "##    00                        00    ##",
         "##                                    ##",
         "##                                    ##",
         "##                  OO                ##",
         "##                                    ##",
         "##                                    ##",
         "##    00                        00    ##",
         "##                                    ##",
         "##                                    ##",
         "########################################"
      }

      oxyd_default_flavor = "a"
      set_cell_key_width(2)
      set_default_parent(cells["  "])
      create_world_by_map(level)
      oxyd_shuffle()

How are default cell meanings parsed in case of multichar maps? Well,
there may be some default meanings, but it's not alike. However, the
engine tries. It it doesn't find any meaning among both common cell
functions table and default meanings, it picks first char and tries
again. So '00' becomes oxyd stone, as well as '0$' or anything else
with zero as first char.

Behavior of some ant.lua functions may be changed by the fact that the
map is multichar. For example, get_map_width() returns correct map
size, even if strings in map are twice (three times, ...)  longer.
get_cell_by_xy() gives back correct cell key, not the char located at,
say, [7,15], but the string of two (three, ...) chars located at map
position [7,15]. And so on...


3.3 cell{} to depth
--------------------------------------------------------------------------------

3.3.1 Checker floor
...................

Sometimes you want to create something as basic as a checkerboard
floor. It's, in my opinion, nice design element. You would expect this
to be no problem, but...

Well, it generally is not a problem in procedural way of map design.
But if you have to draw the checkerboard into string map, you soon
find out terrible truth. It's boring and map looks messy.

I tried, too. And this experience made me to create a mechanism to
draw checkerboard floors. This mechanism may be happily used also to
create checkerboard stones or anything, but most common it's used for
creating floors, thus its name.

Syntax is as follows:

      cells={}
      cells[";"]=cell{floor="fl-tigris"}
      cells[","]=cell{floor="fl-sahara"}
      cells[" "]=cell{{{checkerfloor,{cells[","], cells[";"]}}}}

It's kinda LISPy, due to special syntax that we will discuss later
on. In fact, this is special case of calling parent. Also, you could
write something like this:

      cells[" "]=cell{parent={{checkerfloor, {cells[","], cells[";"]}}}}

and it is the same.

By default, a grid of 1x1 square is done, checkerboard squares have
the size of one precious Enigma world stone. There are arguments, that
change this:

      sahara=     cell{floor="fl-sahara"}
      tigris=     cell{floor="fl-tigris"}
      solidfloor= cell{{{checkerfloor,{sahara,tigris; side=2, offset=1}}}}

Argument 'side' changes the size of the square: here each of the
squares takes up area of four stones: 2x2.

Argument 'offset' does exactly what it sounds like: it 'shifts' the
grid. By default, left top corner of first square of checkerboard is
aligned with left top corner of world (square coordinates [0,0]).
Offset of 1 moves it to the square [1,1].

Also you can create asymmetric constructions. You may specify also
these attributes:

      sidex: to specify width of squares
      sidey: to specify height of squares
      offsetx: to shift the grid to right
      offsety: to shift the grit down


3.3.2 Random floor
..................

Random floor is a similar case to checkerboards. You need one once
upon a time and creating such a floor by hand is boring and not so
very random as one wishes. That was the reason behind creating random
floor parent.

Just like the checker floor, also this parent may be used to construct
anything random. Random stones, random items, you choose.

Random floor is constructed like in this example:

      normal = cell{floor="fl-rough"}
      invert = cell{floor="fl-inverse"}
      tiles  = cell{{{randomfloor, {normal, invert}}}}

You see, the syntax is very similar to that used in checker floor
parent. Soon, you will see this is no coincidence.

You can declare as many random elements as you want:

      normal = cell{floor="fl-rough"}
      invert = cell{floor="fl-inverse"}
      sahara = cell{floor="fl-sahara"}
      tigris = cell{floor="fl-tigris"}
      tiles  = cell{{{randomfloor, {normal, invert, sahara, tigris}}}}

Great, you think. What if I want one item to occur more often? The
answer is item occurrence factor. Each item may be followed by a
number, which declares how often the tile will occur compared to
others. If you omit the number (like in above examples), factor of 1
is default. See the example:


      tigris = cell{floor="fl-tigris"}
      samba  = cell{floor="fl-samba"}
      stone  = cell{floor="fl-stone"}
      cells[" "] = cell{{{randomfloor, tigris, 3, samba, 1, stone, 20}}}

In this case, the floor tiles will be picked in ratio 3:1:20. Most
often, the stone floor will occur, approximately twenty times more
often than samba floor. Tigris floor will occur less often,
approximately three times more than samba.

A function 'random' is used to pick random number. Result of the
function may be modified by the function 'randomseed', which initiates
random seed generator:

      randomseed(666) -- a truly evil landscape
      randomseed(date"%d%H%M%S") -- 'real' randomness


3.3.3 Curried function construction
...................................

Let's look at a parent functions again. Cell parents provide interface
to call another functions. In fact, common cell construction:

      tigris = cell{floor="fl-tigris"}

just assigns a function to name 'tigris'. Later on, you may write
things like this:

      tigris(5, 15)

and this means 'place a tigris floor to the square located at
[5,15]'. Just as simple. If you construct a cell function like this:

      cells["%"] = cell{parent=tigris, stone="st-glass"}

you just let the function cells["%"] execute the function tigris
before everything else. Note, cells["%"] is also function, so you can
happen to write things like cells["%"](5, 15)!

The rule is: each function may be used as an parent. If it accepts
some arguments, first two of them have to be (x,y). Like here, in the
example we've seen before:

      function pr(x,y) print(x, y); end
      cells["w"] = cell{parent=pr}

If you enclose them to the curly brackets, you can call several
parents at one run:

      cells["w"] = cell{parent={pf1, pf2, pf3}}

Finally, you may omit leading 'parent=' - if ant.lua finds no 'parent'
assignment, it automatically looks for the first item of the
table. So, this is perfectly legal:

      cells["w"] = cell{{pf1, pf2, pf3}}

Another problem is passing arguments to parent functions. Let's look
to randomfloor parent example once more:

      tiles  = cell{{{randomfloor, {normal, invert}}}}

The same could be written also this way:

      tiles  = cell{parent={{randomfloor, {normal, invert}}}}

Let's convert it to syntactical rule:

      tiles  = cell{parent={{function_name, argument}}}

And we are done. If you enclose parent to double curly brackets, it
becomes curry function construction. First element of inner table is
function name, all other elements are function arguments. If you omit
leading 'parent=', you'll get to triple-curly-encapsulation, rather
common in maps with ant.lua:

      function pr(x,y, greet, name)
         print(greet..", "..name.." from ["..x..","..y.."]!")
      end

      hallo = cell{{{pr, "Hallo", "world"}}}

      hallo(2,3)

This code chunk will produce: "Hallo, world from [2,3]!"

Still not at the end. This construction lets you create so called
curried constructions. Curried functions are used in some programming
languages (Haskell for example). What ant.lua provides is far from
curried functions known from such a languages, but it's somehow
similar. Look at this to see what's going on:

      hallo0 = cell{{{pr, "Hallo", "world"}}}
      hallo1 = cell{{{pr, "Hallo"}}}
      hallo2 = cell{{{hallo1, "world"}}}

      hallo0(2,3)
      hallo1(2,3, "world")
      hallo2(2,3)
      pr(2,3, "Hallo", "world")

Of course, all these function calls produce the same output.

Curried function calls are rather common in maps that use ant.lua.
Later on we'll take a look at 'object multiples' - this thing is
completely build on top of curried function call. And features like
railway generator, puzzle generator or slope generator stand on object
groups in turn. It could be said that most features of ant.lua use
curried call.


================================================================================
4] FUNCTIONAL APPROACH
================================================================================

4.1 Fills, borders, ...
--------------------------------------------------------------------------------

There are several functions for drawing Enigma maps in init.lua. Most
of them (if not all) have their counterparts in ant.lua. The difference
is, that init.lua functions work with stone/floor/item names, but
ant.lua function works with functions.

Let's look at how are the basic function, filling the world and
drawing border, declared:

      function fill_world_func(fillfunc, x0, y0, w, h)
      function draw_border_func(fillfunc, x0, y0, w, h)
      function draw_func_corners(fillfunc, x0, y0, w, h)

You see, first argument is a function, then there are coordinates of
area to be filled. The area may actually be omitted completely, and
then whole world is filled/bordered at once. Function
'draw_func_corners' behaves similarly to 'draw_border_func', except
that it draws only the four corners of given area.

As to the 'fillfunc', this can be any function at all, but preferably
it should look something like this:

      function fill(x,y)
         do_something(x,y)
      end

This function will be executed for each cell at given area (that is
for the fill_world function), or for each cell of the border of given
area (that is for draw_border function).

Note, that also cell{} function have got the form of func(x,y), so you
can happily use them:

      floor0 = cell{floor="fl-himalaya"}
      stone0 = cell{stone="st-rock4"}
      actor0 = cell{actor={"ac-blackball", {player=0}}}

      create_world(20, 13)
      fill_world_func(floor0)
      draw_border_func(stone0)
      actor0(5,5)

And we are done. Note that if you haven't got a string map, you must
use common function create_world(w,h).

How to fill/border/cornerify just a given portion of map? Well, use
x0, y0, x, h arguments of function:

      fill_world_func(floor0, 39, 1, 19, 11)

init.lua provides a single function to let you draw a checkerboard of
selected floor kinds. In ant.lua there is no function alike. It's
possible to do it this way:

      normal = cell{floor="fl-normal"}
      invers = cell{floor="fl-inverse"}
      checker= cell{{{checkerfloor, {normal, invers}}}}
      fill_world_func(checker)

This mechanism is much more generic. You use the same filling function
as usual, just add the function that generates checker floor. Note
that creating random floor is a matter of rewriting 'checkerfloor' to
'randomfloor', and you can create checkers/randoms from floors,
stones, items and even an actors.


4.2 init.lua counterparts - set_, draw_
--------------------------------------------------------------------------------

The library also provides functions similar to set_ and draw_functions
from init.lua. Group of 'set_' functions in init.lua was aimed to
place a single item/stone/floor/actor to given position. As usual,
ant.lua does the same with a single function set_funcs:

      function set_funcs(fillfunc, poslist)

Where 'fillfuncs' is a function to be executed on given positions, and
'poslist' are those positions. Look at examples:

      set_funcs(doorA, {{2,1},{2,11},{10,1},{10,11}})
      set_funcs(fakeoxyd, {{1,1},{1,11}})
      set_funcs(oxyd, {{18,1},{18,11}})

Argument 'poslist' is really a list of positions, as you expected. You
got the idea. It's also possible to execute several functions at once,
though this is seldom used:

      set_funcs({abyss, doorA}, {{2,1},{2,11},{10,1},{10,11}})

Another useful function is draw_funcs. This is similar to draw_floor,
draw_items and others from init.lua. Draw_funcs executes given
function at several places in one row:

      function draw_func(fillfunc, {x0,y0}, {dx,dy}, steps)

Real life examples follow:

      draw_func(stone, {3,2}, {0,1}, 11)
      draw_func(abyss, {13,0}, {0,1}, 13)

First in list is function to be executed, then starting location
follows, then increment and finally number of steps to proceed. First
line of above example says: "take a function 'stone' and call it for
x,y coordinates beginning at {3,2}, and ending up at {3,12}". So,
given {dx,dy} is continually added to initial location {x0,y0} exactly
'steps' times, and for each location the function is called.

More general syntax is this:

      function draw_func(fillfunc, xylist, dxdylist, steps)

This syntax allows you to use several {x0,y0} locations or several
{dx,dy} increments. Let's look at examples:

      draw_func(stone, {{1,1},{5,1}}, {0,1}, 10)

Two vertical rows of 'stones' are drawn, each consisting of ten stone
blocks, the first starting at {1,1}, second at {5,1}.

      draw_func(stone, {0,0}, {{0,1}, {1,0}} 10)

Two rows of ten stones are drawn, one horizontal and one vertical,
both starting at {0,0}.

Also you can use table of functions, if you want to execute several
function for each cell:

      draw_func({floor0, stone0}, {0,0}, {0,1}, 10)

Last function of this sub-chapter is 'ngon' drawing function. It's
particularly useful for placing several actors to circular/
triangular/ pentagonal/ any other polygonal settings. Basic syntax
follows:

      function ngon_funcs(fillfunc, xylist, radiuslist, count)

* fillfuncs: this is, as usual, a function or several functions in a table.
* xylist: this is {x0,y0} coordinates of a polygon center. You may
  also specify several locations, if you want to create several
  polygons, like in draw_func.
* radiuslist: this is a single number, if you want to create a single
  polygon, or a table of numbers, if you want to create several
  concentric polygons.
* count: a number of elements in polygon

A real-world example:

      ngon_funcs(actor, {10,10}, 2.25, 3)

To turn whole polygon by a given angle, include that angle as a last
function argument:

      ngon_funcs(actor, {10,10}, 2.25, 3, 60)

And finally, if you want to place stones, floors and items, you have
to round their coordinates (you simply cannot place a floor tile to
{1.63, 7.25}). This is done via 'roundfunc' syntax:

      function ngon_funcs(fillfunc, xylist, radiuslist, count, alpha0, roundfunc)

For example:

      ngon_funcs(actor, {10,10}, 2.25, 3, 60, floor)

'floor' is a name of a mathematical function, not a function to draw
Enigma floor. You could also use for example 'ceil'. To be precious,
you can use any function, that accepts one argument and results it's
modified value:

      function f0(x) return x*x end
      ngon_funcs(actor, {10,10}, 2.25, 3, 60, f0)

But I have no idea what would this be good for :)


4.3 Coordinate mangling
--------------------------------------------------------------------------------

Very impressive feature of whole cell{} function mechanism is, that it
provides a way to transform a coordinates automatically for you. Maybe
you know the problem with placing the items to lower and right edges
of map - one never knows what exactly to subtract from level_width to
get the right value.

Look at the piece of code from the welcome-map example:

      oxyd(3,3)
      oxyd(level_width-4,level_height-4)
      oxyd(level_width-4, 3)
      oxyd(3,level_height-4)

What this piece actually does, is that it places the four oxyd stones
to location [3,3] relatively to the four corners of map. Even this
line of code:

      oxyd(level_width-4,level_height-4)

Actually means 'place the oxyd three from bottom and three from
right', or something alike.

In ant.lua code, you can simply write:

      oxyd( 3, 3)
      oxyd(-3,-3)
      oxyd(-3, 3)
      oxyd( 3,-3)

As we all know that negative coordinates don't exist in Enigma,
ant.lua converts those so that they become relative to lower/right
edges of map. Simple as that.

What is even better? It's possible to use coordinates list:

      oxyd({{3,3}, {-3,-3}, {-3,3}, {3,-3}})

This is good enough, but for the cases like this one, where you need
to place some entity to the four corners of imaginary rectangle, there
is a function:

      draw_func_corners(fillfunc, x0, y0, w, h)

The tricky thing is that the function requires 'w' and 'h'
arguments. This means that you cannot simply write:

      draw_func_corners(oxyd, 3, 3, -3, -3)

as this time, '-3' means 'three squares less than width of the
map'. Use this instead:

      draw_func_corners(oxyd, 3, 3, -6, -6)


================================================================================
5] OBJECT GROUPS
================================================================================

5.1 Introduction
--------------------------------------------------------------------------------

What are object groups? What are they good for? Generally, object
group is nothing but a table filled with some special data. If those
data are interpreted correctly, they may happen to become stones,
actors, floors, coordinates or other meaningful constructions.

Imagine that you want to build a map. There are four doors, one in
each level corner. And one switch in a center. Now, how do you get all
doors open at the same time, after someone switches the button? You
use object group, so-called multiple. That multiple holds all the
doors at one table, and it can open/close them upon button switch. The
advantage is, that you can add as many doors as you want to map, and
all will open at once.

Another example: a rubber bands. If you want to connect several
actors, or actors and stones with rubber bands, you add them to
multiple and after the level is drawn, you bind them together with one
command. Let's look at some real-world example:

      cells["O"]=cell{{{add_multiactor, "ac-blackball", actors, {player=0}, 2}}}
      cells["%"]=cell{{{add_multistone, "st-rock3", stones}}}
      ... code,map,stuff ...
      create_world_by_map(level)
      add_rubber_bands(actors, stones, -10, 4)

That's all. No matter how much actors are there, no matter how much
stones are they to be bound to. We'll talk about rubber bands more
later. Now, as you got the idea what is it good for, let's move on.


5.2 Common multiples
--------------------------------------------------------------------------------

5.2.1 Multielement functions
............................

There are four basic object multiples in ant.lua:

      add_multistone(x, y, face, group, attribs)
      add_multifloor(x, y, face, group, attribs)
      add_multiitem(x, y, face, group, attribs)
      add_multiactor(x, y, face, group, attribs, actor_mode)

Basic difference between them is obvious.

Step one when creating a multiple is to define a group. It's usual to
store the object to logical groups - doors that should open at once
have to be in one group, actors to be bound by rubber bands have to be
in another. To define a group, do this:

      group = {}

That's it. This has to be done for each group, so that LUA has a space
to add objects to. The above example should look like this:

      actors={}
      stones={}
      cells["O"]=cell{{{add_multiactor, "ac-blackball", actors, {player=0}, 2}}}
      cells["%"]=cell{{{add_multistone, "st-rock3", stones}}}

This example also shows a step two. You have to create a rule to fill
a multiple with objects. In above example, the group named 'actors' is
filled up with the rule add_multiactor ("ac-blackball", the black
marble) and 'stones' group is filled up with stones "st-rock3".

It's possible to mix up several kinds of elements in one group. For
example, it's perfectly reasonable if you store a doors and bridges in
one group, and then let them all open/close at once.

      openables={}
      cells["-"]=cell{{{add_multistone, "st-door", openables, {type="h"}}}}
      cells["|"]=cell{{{add_multistone, "st-door", openables, {type="v"}}}}
      cells["T"]=cell{{{add_multifloor, "fl-bridge", openables, {name="bridgeA"}}}}

Moreover, it's also possible to have one object placed in several
groups. I've never need such a thing, but it's well possible to do so:

      openables={}
      stones={}
      cells["-(1)"]=cell{{{add_multistone, "st-door", openables, {type="h"}}}}
      cells["-(2)"]=cell{{{add_multistone, "st-door", stones, {type="h"}}}}
      cells["-"]=   cell{parent={cells["-(1)"], cells["-(2)"]}}
      cells["D"]=   cell{{{add_multistone, "st-death", stones}}}
      cells["T"]=   cell{{{add_multifloor, "fl-bridge", openables, {name="bridgeA"}}}}

In this example, the element "-" is added both to 'openables' and
'stones'. Like I said, this is not very common :)


5.2.2 Group actions
...................

Fine, so we know everything about creating a group, yet there was no
example of real usage. The simplest usage is to send a message to all
elements in group. In one of above examples, the group 'openables'
grouped together all the elements that could work with a 'openclose'
message:

      send_group_message(openables, "open", nil)

This function has the same syntax and semantics as a 'send_message'
from init.lua, it just accepts a group of objects at a first
place. You can use this on groups of objects only. Since now, there
were only the object groups, that is, the groups made up of stones,
floors, items and actors. But this will change soon.

Sending a message is not the only thing to be done with object
group. You can also change the attribute of grouped objects. As in the
above case, you can only do this with game elements - stones, floors,
you know. This is done this way:

      set_group_attribs(bolders, {direction=EAST})

First argument is object group, second the attributes to be changed
(like in 'set_attribs' from init.lua).


5.2.3 Rubber bands
..................

Creating a rubber bands is kinda tricky in a string-map-based
level. How to mark which objects should be rubber-banded together?
Answer is object groups.

Basically, you need two groups for this, one with the objects 'from'
which the rubber band be made, and another with the object 'to' which
the rubber band will be made. In fact, you can well use the same group
for both, and it's often used.

There are several rubber-banding functions. We'll go through all of
them.

First, imagine you want to rubber band each actors with each
bolder. Actors are in first group, bolders in another. You can do it
this way:

      add_rubber_bands(actors, bolders, 5, 0)

That is, bind each actor with each bolder by rubber band of length 0
and force 5. In a special case of one bolder in 'bolders' group, all
actors are bound to a single bolder. In another special case of a
single actor in a 'actors' group, poor actor is bound to all
bolders. And finally, if there is a single actor and a single bolder,
they are simply bound together.

Another useful construction is to create a rubber band pairs. This
time, you really need two different groups. First object from first
group is then bound with first object from second group, second from
first group with second from second group, and so on:

      add_rubber_band_pairs(actors, blocks, 10, 0)

It's really useful to have both the groups populated by same number of
elements. Above example is picked from a meditation landscape, where
each of the small white marbles is bound to one stone block.

Last rubber banding function is 'rubber_band_circle'. This time you
only need a single group. First object of the group is bound with
second, the second is bound with third, and so on. Last one is then
bound with the first:

      rubber_band_circle(actors, 10, 2)

In Enigma, you are only allowed to bind the actor and stone or the
actor and actor. Because of this, the group in this case has to be
made up of the actors only, or, more exactly, the two neighboring
elements may not be stone-stone. It's not possible to bind the actor
to floor or item, so avoid using those object in rubber band
constructions completely.


5.3 Generic multielements
--------------------------------------------------------------------------------

Besides multielements that represent Enigma game objects, there are
several so-called generic multielements. Those usually cannot accept
multimessages, nor can their attributes be changed by
change_group_attribs. These multielements are used in special ant.lua
utilities - train-, wormhole- and slope-generator.

Everything that was said about common multielements is true for
generic multielements, too. They are stored in a table, that has to be
declared, so the basic construction looks like this:

      group={}
      cells["!"]=cell{{{add_*, group, ...}}}

Where add_* stands for the multielement function, group is group which
the multielement should be given to, and '...' stands for 'another
arguments'.

Generic multielement functions are these:

      function add_multicell(x, y, group, tag)
      function add_multiobject(x, y, group, func)

In upcoming chapters, each of them will be talked about a bit.


5.3.1 add_multicell
...................

Each multicell element holds three values: two coordinates (x,y) and a
'tag' value. For example:

      slopes={}
      cells["*"]=cell{{{add_multicell, slopes, -1}}}

This example will add an element to the 'slopes' table for each
asterisk in your string map. Every such element will be tagged to -1.

add_multicell element is the most widely used one.


5.3.2 add_multiobject
.....................

Another multielement is add_multiobject. This is used rather seldom,
much much lesser than add_multicell. Multiobject function is similar
to multicell, except for the tag, which is 'function' this time:

      function add_multiobject(x, y, group, func)

Given function gets executed with the (x,y) coordinates, and its
result is stored to given table. Real-world examples are rather
obscure constructions like this one:

      cells = {}
      use_cells(cells, "O")
      cells["O"]=cell{{{add_multiobject, actors, cells["O"]}}}

cells["O"] is a function, we all know. This function has in fact a
return value. Simply said, cell{} function that places an actor return
this actor. Cell function that creates a stone return this stone. And
so on. Because of use_cells(cells, "O") the default meaning for "O"
will be added to cells[] table. And add_multiobject will thus add its
return value, that is just created actor, to the 'actors' table.

In fact, the function can be anything. It doesn't have to result in
Enigma object, like in above example. It may well return string or
number, or even the table. It's up to you, the level designer, if you
find use for this construction.


5.4 Worm holes
--------------------------------------------------------------------------------

5.4.1 Forewords
...............

Worm hole pairs are nice map addition, seen in many maps across all
Enigma map packages. However if you want to create such a pair by
traditional resources, you find out that it's far from the comfort of
moving letters across the string map. You have to change attributes of
given worm hole directly - by choosing another pair of numbers. And as
I really am a lazy person, the first thing to do when creating map
with wormholes, was to add a generator that would just simplify this
task.


5.4.2 Technical background
..........................

Wormholes in Enigma have a simple purpose. If an actor enters
wormhole, it's moved to arbitrary location in Enigma world. Thus, each
worm hole has to be fed up with coordinates of target.

The work of wormhole generator stands on top of object groups. You
feed the generator with a table of worm hole items and a table of worm
hole targets, and it puts them into pairs and create appropriate items
with the right attributes.  Both the worm holes and their targets are
special table of multicells.

Basic scheme is this:

* declare group for wholes and wtargets
* declare whole symbols and wtarget symbols for cells[]
* let the map be drawn
* render worm holes


5.4.3 Setting up cell{} functions
.................................

At first, you have to declare the groups:

      holes={}
      targets={}

Now add a wormhole pairs. There is function that does this for you,
called 'worm_hole_pair':

      worm_hole_pair(cellfuncs, whole_cell, tgt_cell, whole_parent, tgt_parent, whole_grp, tgt_grp, tagnumber)

* cellfuncs is a table of cell functions (usually cells[])
* whole_cell is a letter that will stand for worm hole on string map
* tgt_cell is a letter that will stand for a worm hole target
* whole_parent is a function to be used as a parent of worm hole cell
* tgt_parent is a function to be used as a parent of worm hole target
* whole_grp is a group of worm holes
* tgt_grp is table of worm hole targets
* tagnumber is unique number that identifies the wormhole-target pair

For example, the setting might look this way:

      worm_hole_pair(cells, "A", "a", cells[" "], cells[" "], holes, targets, 1)
      worm_hole_pair(cells, "B", "b", cells["_"], cells["_"], holes, targets, 2)
      worm_hole_pair(cells, "C", "c", cells["_"], cells["_"], holes, targets, 3)

If you need some special fine-tuning, you can write the same this way
(in the example, there is a "A"-"a" pair defined):

      cellfuncs["A"] = cell{{cells["_"], {add_multicell, holes,   1}}}
      cellfuncs["a"] = cell{{cells[" "], {add_multicell, targets, 1}}}


5.4.4 Setting up the map
........................

In map, you simply place the whole-letters and wtarget-letters to
their places, so that they represent desired actor hyper-jumps.

      level = {
         "###################",
         "#A       #       a#",
         "#        #        #",
         "#        #        #",
         "#b       #       B#",
         "###################"
      }


5.4.5 Post-execution code
.........................

When map is drawn, the mission doesn't end yet. You now have the
holes{} and targets{} tables filled with reasonable informations. Now,
the worm hole generator can transform it to worm holes for you:

      create_world_by_map(level)
      render_wormholes(holes, targets, {strength=10, range=5})

And we're done. The important thing is to call generator after the
world is drawn.


5.4.6 Multiple wholes per one target
....................................

If you want several worm holes to move actor to a single location,
just give them same tag-numbers:

      wholes={}
      wtgts={}
      worm_hole_pair(cells, "A", "b", cells[" "], cells[" "], wholes, wtgts, 2)
      worm_hole_pair(cells, "B", "d", cells[" "], cells[" "], wholes, wtgts, 4)
      worm_hole_pair(cells, "C", "b", cells[" "], cells[" "], wholes, wtgts, 2)
      worm_hole_pair(cells, "D", "a", cells[" "], cells[" "], wholes, wtgts, 1)
      worm_hole_pair(cells, "E", "c", cells[" "], cells[" "], wholes, wtgts, 3)
      worm_hole_pair(cells, "F", "d", cells[" "], cells[" "], wholes, wtgts, 4)
      worm_hole_pair(cells, "G", "c", cells[" "], cells[" "], wholes, wtgts, 3)
      worm_hole_pair(cells, "H", "a", cells[" "], cells[" "], wholes, wtgts, 1)

In the example, worm holes "A" and "C" move actor to target denoted by
"b". Please note that it's *tag-number* what is important, and that tag
numbers have to match with the letter of target location (all pairs
tagged with number 2 have the same target letter: "b").



5.5 Railways
--------------------------------------------------------------------------------

5.5.1 What are railways
.......................

Railways, or trains, are the constructions in Enigma maps, that...
that actually behave like a train. Train has fixed, predefined
journey, a rail. On this rail there is arbitrary number of vehicles
moving, each of them "drawing" Enigma tiles (usually floors). It's
usual that "train" is composed of two vehicles: one that sets up the
floor and another that replaces it with abyss, water or other lethal
surface. Trains are actually incorporated to three of my Enigma maps:
ant08 (Mourning Palace), ant10 (Circularity) and ant11 (Cannonball).
Go on and look at them if you want to find out what's going on
exactly.


5.5.2 Technical background
..........................

Each train (that is a vehicle/railway combination) is defined by two
tables:

* table of cells that the railway consists of
* table of engines - path constructors and destructors

Basic idea is, that in each tick, each of engines is moved on to the
next cell of the railway.  Each engine marks its rail - every field it
enters is marked with its tag number, causing that it never can step
on this field again (this prevents the engine from loosing its
direction).  So, the engine tagged with '1' can step only to fields,
that are not marked with '1', and similarly for '0'.

Engines with tag '1' are called 'constructors', and those are
'locomotive' of the train. Engines tagged with '0' are destructors,
and they are acting as a last wagon of a train. Their mission is to
erase tagnumber, so that locomotive can move to the field later, when
it visits it again.

The path should be circular, and one field thick. In fact, engines are
always trying to keep their direction, so that they do not turn until
they have to. Thus it could be possible to create a two-fields thick
railway. It's not very simple though, and I never tried it. The train
will fail if it goes into the tight corner - it cannot invert its
direction.


5.5.3 Setting up cell{} functions
.................................

To setup a rail, you need four cells. One for train constructor, one
for train destructor, one for the body of train and one for the
pathway. Example of a typical construction follows:

      path = {}
      loco = {}
      cells["!"]=cell{parent={cells["."], {add_multicell, path, 0}}}
      cells["_"]=cell{parent={cells["!"], {add_multicell, path, 1}, cells["'"]}}
      cells["c"]=cell{parent={cells["_"], {add_multicell, loco, construct}}}
      cells["d"]=cell{parent={cells["!"], {add_multicell, loco, destruct}}}

That means:
* cells marked "!" are part of path and are tagged to zero
* cells marked "_" are also part of path, but are tagged to 1 instead
* cells marked "c" are engines, and are tagged by function 'construct'
* cells marked "d" are also engines, but are tagged by function 'destruct'

Note that one of parents of cells["!"] function is cells["."],
denoting that outside the train, there is abyss (or whatever happens
to be under cells["."]). Similarly, cells["_"] has cells["'"] as a
parent, and this is how the train body will look like. This is how the
path and train looks like after startup, after the train starts to
move, 'construct' and 'destruct' functions drive what will be
displayed! If you want to simple setup, do this:

      path = {}
      loco = {}
      cells["."]=cell{how does the railway look like}
      cells["'"]=cell{how does the train look like}
      cells["!"]=cell{parent={cells["."], {add_multicell, path, 0}}}
      cells["_"]=cell{parent={cells["!"], {add_multicell, path, 1}, cells["'"]}}
      cells["c"]=cell{parent={cells["_"], {add_multicell, loco, cells["'"]}}}
      cells["d"]=cell{parent={cells["!"], {add_multicell, loco, cells["."]}}}

This is not very wise, as cells[] functions are bloated with
functionality. Remember all these curried constructions and the like -
they provide much power, but this drawbacks in rather slow execution
(up to four times slower than init.lua functions). Both 'construct'
and 'destruct' functions are called repeatedly each tick, for each
move the train does. The faster they are, the better. In my opinion,
it's better to create hard-core functions that do the dirty work fast
enough:

      function construct(x, y) set_floor("fl-normal", x, y) end
      function destruct(x, y)  set_floor("fl-abyss", x, y) end

      cells["."]=construct
      cells["'"]=destruct
      cells["!"]=cell{parent={cells["."], {add_multicell, path, 0}}}
      cells["_"]=cell{parent={cells["!"], {add_multicell, path, 1}, cells["'"]}}
      cells["c"]=cell{parent={cells["_"], {add_multicell, loco, construct}}}
      cells["d"]=cell{parent={cells["!"], {add_multicell, loco, destruct}}}


5.5.4 Binding the train to railway
..................................

Now you have the cells declared, but still the train is not ready. You
have to make up the function that moves engines on the railway each
time it's called. Don't worry, it is as simple as this:

      rail = new_rail(loco, path)

Now, each time the rail() function gets called, it moves all the
engines ahead, processing 'construct' and 'destruct' functions. To do
this repeatedly (your train should move on fluently), let the function
be called by timer stone:

      cells["~"]=cell{stone={"st-timer", {action="callback", target="rail", interval=0.15}}}

Ready. Now just draw the map and you are done.


5.5.5 Setting up the map
........................

In your string map, the path will be denoted by exclamation marks, the
train by underscores, locomotives by 'c' and last wagons by 'd'. Don't
forget to add a timer stone '~' to make your train move!

Result map could look this way:

      level = {
         "####################",
         "#!!!!!!!  !!c___d!!#",
         "#!     !!!!       !#",
         "#!!!!!         !!!!#",
         "#    !!!!!!!!!!!   #",
         "###################~"
      }

And that is all. Congratulations, your map just got a train.


5.6 Puzzles
--------------------------------------------------------------------------------

5.6.1 Forewords
...............

Puzzles are popular map constructions overall. The puzzle consists of
several 'puzzle stones', that may only be moved together. You can
create constructions of dozens puzzle stones as well as two or three
stones big ones.

Creating puzzles is not complicated at all, it's simply boring. It
means that you have to declare several cells to become several kinds
of puzzle stones - one with sockets to left and down, one with up and
right, one with left, down and right, one with... You got it. But if
you want to create so called 'complete cluster' (each stone of puzzle
has all the sockets connected to other stones), it's just a matter of
dumb work to make up a cluster elements out of the cluster
layout. This is where ant.lua brings help.

In fact, it's somehow possible to create also open clusters, but this
feature is not very strong and generally you better rely on boring
hard-coding cell functions.


5.6.2 Technical background
..........................

The puzzle generator aims at creating complete puzzle clusters. It
needs a table, in which there are coordinates of a puzzle stones,
reads this table and places right puzzle elements to Enigma world. In
the table, there is in fact a 'layout' of a puzzle cluster.

The step-by-step recipe to create a puzzle cluster is here:

* declare a layout table
* declare a cell function that represents cell stone
* draw a map
* render puzzles


5.6.3 Setting up puzzle
.......................

You have to declare a table where LUA will store the layout in. This
is done in usual way:

      puzzles = {}

Next, declare a cell function:

      cells["*"]=cell{{{add_multicell, puzzles}}}

Now draw the string map:

      level = {
         "####################",
         "#                  #",
         "#  ***   ***       #",
         "#  * ***** *       #",
         "#***       ***     #",
         "####################"
      }

And create world out of it:

      create_world_by_map(level)

Like in case of wormholes, in this moment the table of puzzles is
filled with information about layout of puzzle. You just have to pass
this information to puzzle generator:

      render_puzzles(puzzles)

Done!


5.6.4 Fake puzzles
..................

There is one more feature of the puzzle generator, that can help you
in creating incomplete puzzles. You can declare a cell to act as if
there is a puzzle stone, but in fact no stone is rendered there.
Effectively this will make neighboring stones to open thir sockets to
this field.

To set up this fake puzzle stone, declare the puzzle cell this way:

      cells["&"]=cell{{{add_multicell, puzzles, 2}}}

Then the puzzle won't be generated on given map element, but it's
neighbors will have their sockets opened in this direction like there
should be one.


5.6.5 Puzzle kinds
..................

There are several puzzle kinds in enigma. There is a classic puzzle,
an Oxyd1 compatible puzzle, plus 'st-bigbrick' stone that is actually
being constructed like a puzzle (it has nearly the same attributes as
'st-puzzle' does).

To let level designer choose a puzzle kind, it's possible to pass a
'kind' argument to a render_puzzles() function. If this argument is
omitted, ant.lua automatically picks a 'puzzle' kind. If it's
supplied, it has to be a function with this interface:

      function some_puzzle(x, y, connections)

In init.lua, there are currently (at time of writing this) two
functions with necessary syntax and semantics: puzzle() and puzzle2()
(for Oxyd1 compatible puzzles). The call then looks this way:

      render_puzzles(puzzles1)
      render_puzzles(puzzles2, puzzle2)

You could declare your own rendering function. For example:

      function bigbrick(x, y, conn)
         set_stone("st-bigbrick", x, y, {connections=conn})
      end

      render_puzzles(puzzles3, bigbrick)


5.7 Slopes
--------------------------------------------------------------------------------

5.7.1 Forewords
...............

Slope is Enigma floor kind, that pushes the actor to move in one
direction -- gradiented floor. Gradients can create really big
constructions, beveled areas, both sunken and raised.

With slopes, its very similar to puzzles. Both the fact, that
declaring cell functions is boring, and the way the slopes are
generated.


5.7.2 Technical background
..........................

Slope generator can create a wide variety of slope settings, but some
things it's simply unable to do. It has no intelligence, it's just
automaton that reads input data and based on them it decides what kind
of slope will be on this or that place.

It works on a pattern matching basis. Each cell setup is compared with
a table of patterns, the nearest one is picked up and we hope it's the
right one. It happens that some complicated settings are just not
parsed the right way, or there are ambiguities. There is no simple
remedy for this situation, than to add another pattern to patterns
database. Most of the time, the patterns match just well and slopes
are happily (and correctly) generated.

Next thing is setting up where is the center of desired shape. The
program needs to know how to shape slopes, whether "up-down" or
"down-up". The point that declares where is the "upside" of sloped
shape is called 'pivot'.

The basic step-by-step recipe follows:

* create table for slopes and pivots
* create cell functions for slopes and pivots
* create map
* let the pivot be spread
* render slopes

Most of work is done by ant.lua for you, but there are some steps that
may require some skill and experience.


5.7.3 Setting up cell{} functions
.................................

Tables for cells and pivots are created in common fashion:

      slopes={}
      pivots={}

Into 'slopes', the overall layout of the shape will be placed. This is
similar table to the one used in puzzle generator. In 'pivots' table,
the locations of 'central points' are stored.

Next, cell functions are created:

      cells["*"]=cell{{{add_multicell, slopes, 1}}}
      cells["&"]=cell{{{add_multicell, pivots, slopes}, cells[" "]}}

You see, add_multicell is used once more. Tag '1' has meaning of 'here
be the slope'. Cell '&' is tagged by the slopes table. This way the
pivots are bound to slopes, so that it is possible to lay several
concentric slope shapes.


5.7.4 Setting up the map
........................

Now the map is populated with the asterisks, representing the slope
boundary, and one or more pivot ampersands, placed *INSIDE* that
boundary.

Placing pivot into closed boundary is critical, otherwise the stack
overflows, or others errors occur.

      level = {
         "####################",
         "#  **************  #",
         "#  *&           *  #",
         "#  *            *  #",
         "#  **************  #",
         "####################"
      }


5.7.5 Post-execution code
.........................

After the map is drawn, the tables are fed up with information about
the shape. Now two steps have to be done: the pivot has to be spread,
so that the whole boundary gets filled, and program can effectively
differ between "up" and "down". Then, the slope can be rendered.

Piece of code that processes the thing looks right like this:

      create_world_by_map(level)
      spread_tag(pivots)
      render_slopes(slopes)

That's it.


5.7.6 Mixing several slopes
...........................

Sometimes you want to create several slopes where one bounds
another. You could need to create two slopes overlapping. It can
happen (it happens) that you need to create a slope that the program
cannot render correctly. In all such cases, you should break your
slope to several parts, and place each one to separate table:

      slopes1={}
      slopes2={}
      pivots1={}
      pivots2={}

      cells["*"]=cell{{{add_multicell, slopes1, 1}}}
      cells["@"]=cell{{{add_multicell, slopes2, 1}}}
      cells["&"]=cell{{{add_multicell, pivots1, slopes1}, cells[" "]}}
      cells["%"]=cell{{{add_multicell, pivots2, slopes2}, cells[" "]}}
      cells["^"]=cell{parent={cells["*"],cells["@"]}}

You have to reflect this in your map:

      level = {
         "####################",
         "#  **********      #",
         "#  *@@@@@@@@^@@@   #",
         "#  *@&%     *  @   #",
         "#  *@       *  @   #",
         "#  *@@@@@@@@^@@@   #",
         "#  **********      #",
         "####################"
      }

Rendered by Enigma, this doesn't look very nice, as one slope just
overrides another. Well, at least you got the idea how it works.


5.7.7 Fake slopes
.................

Like in puzzles, you can create also fake slopes. Fake slope is a
cell, that behaves like a slope, but it never gets rendered. Fake
slopes are tagged by the number '2':

      cells["+"]=cell{{{add_multicell, slopes, 2}}}


5.7.8 Invert slopes
...................

If you want to create inverted-slope (southwest instead of northeast
and so on), tag it with a number -1:

      cells["+"]=cell{{{add_multicell, slopes, -1}}}

You can also create a whole boundary invert, by using 'invert'
argument in 'render_slopes' function:

      render_slopes(slopes, -1)

Actually you can pass anything non-nil as invert argument, and whole
boundary gets inverted. If there are invert cells in boundary, the are
drawn 'normally' - that is, double inversion results to original
state, like in De Morgan's.


5.8 Afterwords
--------------------------------------------------------------------------------

It's perfectly possible to mix up generators of ant.lua in map. Not
just several instances on one generator, like two trains or two
slopes. It's of course possible to mix up trains with slopes and
puzzles. Just remember, that some constructions are static. If you are
creating engine with slope in center, that would move over the map,
you don't need the slope generator. It cannot help you, unless you
want to spread tag and render slopes each round over and over.

Well, this was the core of ant.lua. There are a few helper functions
besides this, but in fact ant.lua will provide you no better
functionality.


================================================================================
6] HELPER FUNCTIONS
================================================================================

6.1 Debugging
--------------------------------------------------------------------------------

It's a simple fact that even in enigma maps there tend to be errors.
ant.lua can help you in map designing by providing warning and error
messages each time some ill argument gets passed or something. If you
debug, you may use several ant.lua functions that provide warning
and debug messages.

The functions follow:

      function warning(text)
      function be_pedantic(mode)
      function debug(text)
      function debug_mode()
      function debug_mode_off()

6.1.1 Warnings
..............

This function displays a warning message in a following format:

      warning: [ant.lua]: <text>

Where <text> is replaced by your text. If you want no warnings to
occur, that is, if a warning means that the map is broken and there is
no need to even try if it works, you may turn on pedantic mode:

      be_pedantic()

In pedantic mode, each warning turns to error. If an error occurs
during the map load time, the map never gets loaded.

Pedantic mode is turned off this way:

      be_pedantic(0)


6.1.2 Debugs
............

ant.lua provides a number of debugging messages. These inform you
about the code execution and about some checkpoints that were
passed. If you find yourself in doubt whether a function gets executed
at all, turn on debug mode:

      debug_mode()

In debug mode, the messages like this one:

      debug: [ant.lua]: creating world [20x13]

Appear on your terminal every once in a while. You can produce your
own debug messages, should you want:

      debug("beer overflow: fridge limit reached (improbable)")

I use this when creating some really complicated maps with lots of LUA
experiments in. You can leave the messages in a file even after the
debug, just remove the debug_mode() line. I remove them though, as I
like clean code :).


6.2 Clone table
--------------------------------------------------------------------------------

This function makes a copy of a table. It's just a shallow copy
though, only the first layer of values is copied, nested tables stay
intact.

      cf = clone_table(cellfuncs)


6.3 Sending messages
--------------------------------------------------------------------------------

Sending messages is a common way how to make one object do the things
like opening, closing, switching on and off, triggering etc. As long
as you use the triggers and switches for this purpose, everything is
OK. If you want to send a message on your own, you have to write
things like this:

      enigma.SendMessage(enigma.GetNamedObject("doorA"), "open", nil)

There is a function that provides this in a simple interface:

      send_message_named(objname, message, third)

Well, in fact I do not know what the 'third' argument means, but I
never saw anyone to pass anything other than 'nil' here. Above example
changes this way:

      send_message_named("doorA", "open", nil)

Much better.

Besides this, it's possible to send a message to the group of objects.
This is covered in chapter [5.2.2 Group actions].

================================================================================
~end of ant_lua.txt~