This file is indexed.

/usr/share/games/enigma/api2init.lua is in enigma-data 1.10~~pre-alpha+r2236-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
------------------------------------------------------------------------
-- Copyright (C) 2008,2009,2010,2011 Ronald Lamprecht
--
-- 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.
--
------------------------------------------------------------------------

-------------------------------------------------
-- Level API 2 as of Enigma 1.10 compatibility --
-------------------------------------------------

-----------------------------------------------
-- Use Enigma internal random implementation --
-----------------------------------------------

math.random = en.random
random = math.random
-- randseed is issued by Enigma application and must not be disturbed
math.randomseed = function () end

-----------------------------------------------
-- Semi strict Lua - global typo detection   --
-----------------------------------------------
-- Limit Lua code read access to prior 'declared' global variables,
-- that have been registered by a regular assignment (even assigning nil will
-- do). C code will read access globals without check.
-- This section is a slightly modified version of Lua's etc/strict.lua.
-- We call it semi strict, as we still allow level authors to declare new
-- globals inside a function. We just want to catch typos in global constants.
do
    local mt = getmetatable(_G)
    if mt == nil then
        mt = {}
        setmetatable(_G, mt)
    end

    mt.__declared = {}

    local function what ()
      local d = debug.getinfo(3, "S")
      return d and d.what or "C"
    end

    mt.__newindex = function (t, n, v)
        if not mt.__declared[n] then
            mt.__declared[n] = true
        end
        rawset(t, n, v)
    end

    mt.__index = function (t, n)
        if not mt.__declared[n]  and what() ~= "C"  then
            error("variable '"..n.."' is not declared", 2)
        end
        return rawget(t, n)
    end
end
-----------------------------
-- Helper functions        --
-----------------------------

function enigma.settile(key, pos)
    wo[pos] = en.ti[key]
end

---------------
-- Constants --
----------------

-- state
OFF       = 0
ON        = 1
CLOSED    = 0
OPEN      = 1
IDLE      = 0
ACTIVE    = 1
INACTIVE  = 2
OXYDPAIR  = 2
LIGHT     = 0
MEDIUM    = 1
DARK      = 2
FLOODING  = 1
INVISIBLE = -1
SMALL     = 0
LARGE     = 2
YIN       = 0
YANG      = 1
EMPTY     = 0
FULL      = 2
BROKEN    = 1
BREAKING  = 2
CW  = 0
CCW = 1

-- color
BLACK  = 0
WHITE  = 1
BLUE   = 2
YELLOW = 3
NOCOLOR = nil

-- controllers
CTRL_NONE    = 0
CTRL_YIN     = 1
CTRL_YANG    = 2
CTRL_YINYANG = 3

-- oxyd color
OXYD_BLUE   =  0
OXYD_RED    =  1
OXYD_GREEN  =  2
OXYD_YELLOW =  3
OXYD_CYAN   =  4
OXYD_PURPLE =  5
OXYD_WHITE  =  6
OXYD_BLACK  =  7
OXYD_GRAY   =  8
OXYD_ORANGE =  9
OXYD_PINE   = 10
OXYD_BROWN  = 11
OXYD_AUTO   = -1
OXYD_FAKE   = -2
OXYD_QUAKE  = -3
OXYD_BOLD   = -4

-- direction/orientations
RANDOMDIR = -2
NODIR     = -1
WEST      =  0
SOUTH     =  1
EAST      =  2
NORTH     =  3
NORTHWEST =  4
SOUTHWEST =  5
SOUTHEAST =  6
NORTHEAST =  7

-- alternative orientation notation for mirrors
BACKSLASH  = 0
HORIZONTAL = 1
SLASH      = 2
VERTICAL   = 3

-- direction offsets
N = po(0, -1)
E = po(1, 0)
S = po(0, 1)
W = po(-1, 0)
NE = N + E
NW = N + W
SE = S + E
SW = S + W
NNE = N + NE
ENE = E + NE
ESE = E + SE
SSE = S + SE
SSW = S + SW
WSW = W + SW
WNW = W + NW
NNW = N + NW

-- neighbors

NEIGHBORS_4     = W .. S .. E .. N
NEIGHBORS_8     = NEIGHBORS_4 .. NW .. SW ..SE .. NE
NEIGHBORS_CHESS = NNE .. ENE .. ESE .. SSE .. SSW .. WSW .. WNW .. NNW

-- convertion orientation numbers to direction vectors
ORI2DIR = {[WEST]=W, [SOUTH]=S, [EAST]=E, [NORTH]=N, [NODIR]=po(0,0),
           [NORTHWEST]=NW, [SOUTHWEST]=SW, [SOUTHEAST]=SE, [NORTHEAST]=NE}

-- essential
DISPENSABLE   = 0
INDISPENSABLE = 1
PERKIND       = 2

-- meditation
MEDITATION_CALDERA = -3
MEDITATION_HOLLOW  = -2
MEDITATION_DENT    = -1
MEDITATION_BUMP    =  1
MEDITATION_HILL    =  2
MEDITATION_VOLCANO =  3

-- glasses
SPOT_NOTHING        =   0
SPOT_DEATH          =   1
SPOT_HOLLOW         =   2
SPOT_ACTORIMPULSE   =   4
SPOT_SENSOR         =   8
SPOT_LIGHTPASSENGER =  16
SPOT_TRAP           =  32

-- coinslot
COIN_IGNORE = -1
COIN_REJECT = -2

-- rubberbands
AUTOLENGTH = -1

-- subsoil
SUBSOIL_ABYSS = 0
SUBSOIL_WATER = 1
SUBSOIL_AUTO  = 2

-- follower
FOLLOW_NO     = 0
FOLLOW_SCROLL = 1
FOLLOW_FLIP   = 2

FOLLOW_FULLSCREEN = po(19, 12)
FOLLOW_HALFSCREEN = po(9.5, 6)

-- map transformations, aka. read directions
MAP_IDENT            = {index = 1}
MAP_ROT_CW           = {index = 2}
MAP_ROT_180          = {index = 3}
MAP_ROT_CCW          = {index = 4}
MAP_FLIP_BACKSLASH   = {index = 5}
MAP_FLIP_HORIZONTAL  = {index = 6}
MAP_FLIP_SLASH       = {index = 7}
MAP_FLIP_VERTICAL    = {index = 8}
MAP_COUNT = 8
-- Note: The sequence is of importance for lib.map.transformation_compose.
MAP_ALL = {MAP_IDENT, MAP_ROT_CW, MAP_ROT_180, MAP_ROT_CCW, MAP_FLIP_BACKSLASH,
           MAP_FLIP_HORIZONTAL, MAP_FLIP_SLASH, MAP_FLIP_VERTICAL}

for _, transformation in ipairs(MAP_ALL) do
    setmetatable(transformation, {
        __mul = (function() error("You have to load libmap to use \"*\" on map transformations.", 2) end),
        __pow = (function() error("You have to load libmap to use \"^\" on map transformations.", 2) end),
        __unm = (function() error("You have to load libmap to use \"-\" on map transformations.", 2) end),
        _type = "maptransformation"})
end

---------------------
-- Utility Methods --
---------------------

wo:_register("drawMap",
    function (world, resolver, anchor, arg3, arg4, arg5)
        -- world, resolver, (position|object|table), string, (table|map), [int]
        -- world, resolver, (position|object|table), map, [int]
        assert_type(world, "wo:drawMap self (world)", 2, "world")
        assert_bool(is_resolver(resolver), "wo:drawMap first argument is no valid resolver", 2)
        assert_type(anchor, "wo:drawMap second argument (anchor)", 2, "position", "valid object", "table")
        assert_type(arg3, "wo:drawMap third argument (ignorekey or map)", 2, "string", "map")
        local origin = po(anchor)   -- either convert or make a working position copy
        -- Analyse arguments 4 to 6
        local ignore, map, readdir
        if (etype(arg3) == "map") then
            assert_type(arg4, "wo:drawMap fourth argument (read direction)", 2, "nil", "maptransformation")
            map = arg3
            ignore = map.defaultkey
            readdir = arg4 or MAP_IDENT
        else
            assert_type(arg4, "wo:drawMap fourth argument (map)", 2, "table", "map")
            assert_type(arg5, "wo:drawMap fifth argument (read direction)", 2, "nil", "maptransformation")
            ignore = arg3
            map = arg4
            readdir = arg5 or MAP_IDENT
            assert_bool((etype(map) ~= "map") or (string.len(map.defaultkey) == string.len(ignore)),
                "wo:drawMap: Ignore key and default key differ in length.", 2)
        end
        local len = string.len(ignore)
        -- Prepare read direction rotation
        local w, h = 0, 0
        local function rot(x, y)
            -- The difference of this function to the one in libmap
            -- results among others from different coordinate origins
            -- and different application of rot.
            return ({[MAP_IDENT]           = {x,         y},
                     [MAP_ROT_CW]          = {h + 1 - y, x},
                     [MAP_ROT_180]         = {w + 1 - x, h + 1 - y},
                     [MAP_ROT_CCW]         = {y,         w + 1 - x},
                     [MAP_FLIP_HORIZONTAL] = {w + 1 - x, y},
                     [MAP_FLIP_VERTICAL]   = {x,         h + 1 - y},
                     [MAP_FLIP_SLASH]      = {y,         x},
                     [MAP_FLIP_BACKSLASH]  = {h + 1 - y, w + 1 - x} })[readdir]
        end
        if readdir ~= MAP_IDENT then
            -- Calculate height and width for rotation if neccessary
            h = #map
            for y = 1, h do
                w = math.max(w, string.len(map[y])/len)
            end
        end
        -- Draw map
        for y=1, #map do
            local line = map[y]
            local linelen = string.len(line)
            assert_bool(math.fmod(linelen, len) == 0, "wo:drawMap: Map line ".. y .. " with odd length.", 2)
            for x = 1, linelen/len do
                local key = string.sub(line, len*(x-1)+1, len*x)
                if key ~= ignore then
                    local p = {origin.x - 1, origin.y - 1}
                    if readdir == MAP_IDENT then
                        p = {p[1] + x, p[2] + y}
                    else
                        p = {p[1] + (rot(x,y))[1], p[2] + (rot(x,y))[2]}
                    end
                    tile = world:_evaluate(resolver, key, p[1], p[2])
                    if tile then
                        world[p] = tile
                    else
                        error("wo:drawMap: undefined tile '" .. key .. "' at "
                              .. p[1] .. ", " .. p[2] .. "(in submap at "
                              .. x .. ", ".. y .. ").")
                    end
                end
            end
        end
        -- Call resolver finalizers from bottom to top
        local function finalizeResolvers(context)
            if type(context) == "table" then
                finalizeResolvers(context[3])
                local finalizer = context[2]
                if type(finalizer) == "function" then
                     finalizer(context)
                end
            end
        end
        finalizeResolvers(resolver)
    end
)

wo:_register("drawBorder",
    function (world, arg1, arg2, arg3, arg4, arg5)
        -- world, (position|object|table), width, height, (tile|table|key, resolver)
        -- world, (position|object|table), (position|object|table), (tile|table|key, resolver)
        assert_type(world, "wo:drawBorder self (world)", 2, "world")
        assert_type(arg1, "wo:drawBorder first argument (upperleft edge)", 2, "position", "valid object", "table")
        assert_type(arg2, "wo:drawBorder second argument (width or lowerright edge)", 2, "position", "valid object", "table", "positive integer")
        local origin = po(arg1)
        local dest, tile, resolver
        local isTile = true
        if etype(arg2) == "number" then
            assert_type(arg3, "wo:drawBorder third argument (height)", 2, "positive integer")
            dest = po(origin.x + arg2 - 1, origin.y + arg3 - 1)
            tile = arg4
            resolver = arg5
        else
            dest = po(arg2)
            tile = arg3
            resolver = arg4
        end
        assert_type(tile, "wo:drawBorder tile/key argument error", 2, "tile", "table", "string")
        if etype(tile) == "string" then
            assert_bool(is_resolver(resolver), "wo:drawBorder resolver argument error", 2)
            isTile = false
        end
        for x = origin.x, dest.x do
            wo[{x, origin.y}] = tile
            if origin.y ~= dest.y then
                if isTile then
                    wo[{x, dest.y}] = tile
                else
                    wo[{x, dest.y}] = world:_evaluate(resolver, tile, x, dest.y)
                end
            end
        end
        for y = origin.y + 1, dest.y -1 do
            wo[{origin.x, y}] = tile
            if origin.x ~= dest.x then
                if isTile then
                    wo[{dest.x, y}] = tile
                else
                    wo[{dest.x, y}] = world:_evaluate(resolver, tile, dest.x, y)
                end
            end
        end
    end
)

wo:_register("drawRect",
    function (world, arg1, arg2, arg3, arg4, arg5)
        -- world, (position|object|table), width, height, (tile|table|key, resolver)
        -- world, (position|object|table), (position|object|table), (tile|table|key, resolver)
        assert_type(world, "wo:drawRect self (world)", 2, "world")
        assert_type(arg1, "wo:drawRect first argument (upperleft edge)", 2, "position", "valid object", "table")
        assert_type(arg2, "wo:drawRect second argument (width or lowerright edge)", 2, "position", "valid object", "table", "positive integer")
        local origin = po(arg1)
        local dest, tile, resolver
        local isTile = true
        if etype(arg2) == "number" then
            assert_type(arg3, "wo:drawRect third argument (height)", 2, "positive integer")
            dest = po(origin.x + arg2 - 1, origin.y + arg3 - 1)
            tile = arg4
            resolver = arg5
        else
            dest = po(arg2)
            tile = arg3
            resolver = arg4
        end
        assert_type(tile, "wo:drawRect tile/key argument error", 2, "tile", "table", "string")
        if etype(tile) == "string" then
            assert_bool(is_resolver(resolver), "wo:drawRect resolver argument error", 2)
            isTile = false
        end

        for x = origin.x, dest.x do
            for y = origin.y, dest.y do
                if isTile then
                    wo[{x, y}] = tile
                else
                    wo[{x, y}] = world:_evaluate(resolver, tile, x, y)
                end
            end
        end
    end
)

------------------------------
-- Global utility functions --
------------------------------

-- A wrapper of "if" to resemble the ternary ?:-function.
-- Note that this function evaluates both IFTRUE as well as IFFALSE, e.g.
--     cond(t == 0, 1/t, error("Division by zero"))
-- will evaluate the error-function and thus halt for any T.
-- Hence: Make sure there are no sideeffects in IFTRUE and IFFALSE!
function cond(condition, iftrue, iffalse)
    if condition then
        return iftrue
    else
        return iffalse
    end
end

---------------
-- Libraries --
---------------

lib = {}
setmetatable(lib, {__index =
    function (table, key)
        if type(key) == "string" then
            error("Library function named '"..key.."' not existing. A typo or a missing library dependency may cause this fault.", 2)
        else
            error("Library function access with a key of type '"..type(key).."'. A name is mandatory.", 2)
        end
    end
})

---------------
-- Resolvers --
---------------

res = {}
setmetatable(res, {__index =
    function (table, key)
        if type(key) == "string" then
            error("Resolver named '"..key.."' not existing. A typo or a missing library dependency may cause this fault.", 2)
        else
            error("Resolver access with a key of type '"..type(key).."'. A name is mandatory.", 2)
        end
    end
})
res.metatable = {_type="resolver", _resolver=true}

function res.random_implementation(context, evaluator, key, x, y)
    for hit_itr, hit_pair in ipairs(context[4]) do
        if key == hit_pair[1] then
            local super = nil
            if hit_pair[2] ~= nil then
                super = evaluator(context[3], hit_pair[2], x, y)
            end
            local repl_key
            local r = math.random() * context[6]
            local i = 1
            local s = context[5][i][2]
            while s < r do
                i = i + 1
                s = s + context[5][i][2]
            end
            repl_key = context[5][i][1]
            local replacement = evaluator(context[3], repl_key, x, y)
            if super == nil then
                return replacement
            elseif type(super) == "table" then
                return ti(super) .. replacement
            else
                return super .. replacement
            end
        end
    end
    -- key not found
    return evaluator(context[3], key, x, y)
end

function res.random(subresolver, hits, replacements)
    -- syntax: hits = key | {key, [key]*, [{key, superkey}]*}
    --         replacements = {key, [key]*, [{key, frequency}]*}
    assert_bool(is_resolver(subresolver), "res.random first argument (subresolver)", 2)
    assert_type(hits, "res.random second argument (hits)", 2, "non-empty string", "table")
    assert_type(replacements, "res.random third argument (replacements)", 2, "string", "table")
    local hit_table = {}
    if type(hits) == "string" then
        hit_table[1] = {hits, nil}
    else  -- table
        for i, v in ipairs(hits) do
            assert_type(v, "res.random second argument (hits), entry " .. i, 2, "non-empty string", "table")
            if type(v) == "string" then
                hit_table[i] = {v, nil}
            elseif type(v) == "table" then
                assert_type(v[1], "res.random second argument (hits), key in entry " .. i, 2, "non-empty string")
                assert_type(v[2], "res.random second argument (hits), superkey in entry " .. i, 2, "nil", "string")
                hit_table[i] = v
            end
        end
    end
    local repl_table = {}
    if type(replacements) == "string" then
        repl_table[1] = {replacements, 1}
    else  -- table
        for i, v in ipairs(replacements) do
            assert_type(v, "res.random third argument (replacements), entry " .. i, 2, "string", "table")
            if type(v) == "string" then
                repl_table[i] = {v, 1}
            else  -- table of key and frequency
                assert_type(v[1], "res.random third argument (replacements), key in entry " .. i, 2, "string")
                assert_type(v[2], "res.random third argument (replacements), frequency in entry " .. i, 2, "non-negative")
                repl_table[i] = v
            end
        end
    end
    local repl_sum = 0
    for i, v in ipairs(repl_table) do
        repl_sum = repl_sum + v[2]
    end
    local context = {res.random_implementation, nil, subresolver, hit_table,
                     repl_table, repl_sum}
    setmetatable(context, res.metatable)
    return context
end

function res.autotile_newtile(context, evaluator, key, x, y, template, substitution)
    local decl = template:_declaration()  -- get a deep copy
    local result
    for i, tile in ipairs(decl) do
        if type(tile) == "string" then
            local subkey = string.gsub(tile, "%%%%", substitution)
            if result == nil then
                result = evaluator(context[3], subkey, x, y)
            else
                result = result .. evaluator(context[3], subkey, x, y)
            end
        else
            local at = {}   -- attribute table
            for key, val in pairs(tile) do   -- a table
                if type(val) ~= "table" then
                    if type(val) == "string" then
                        at[key] = string.gsub(val, "%%%%", "%%"..substitution)
                    else
                        at[key] = val
                    end
                else
                    local tt = {}
                    for j, token in ipairs(val) do
                        if type(token) == "string" then
                            tt[j] = string.gsub(token, "%%%%", "%%"..substitution)
                        else
                            tt[j] = token
                        end
                    end
                    at[key] = tt
                end
            end
            if result == nil then
                result = ti(at)
            else
                result = result .. ti(at)
            end
        end
    end
    ti[key] = result
end

function res.autotile_implementation(context, evaluator, key, x, y)
    for i, rule in ipairs (context[4]) do
        if #rule >= 3 then   -- from, to substitution
            --
            local first = string.byte(rule[1], #rule[1])
            local last = string.byte(rule[2], #rule[2])
            local candidate = string.byte(key, #key)
            local offset = 1
            if #rule == 4 then
                offset = rule[4]
            end

            if #rule[1] == #key and string.sub(key, 1, -2) == string.sub(rule[1], 1, -2)
                    and first <= candidate and candidate <= last then
                local tile = evaluator(context[3], key, x, y)
                if tile == nil then
                    res.autotile_newtile(context, evaluator, key, x, y, ti[rule[3]], candidate - first + offset)
                    return ti[key]
                else
                    return tile
                end
            end
        elseif string.find(key, rule[1], 1, true) == 1 then
            -- prefix based substitution
            local tile = evaluator(context[3], key, x, y)
            if tile == nil then
                res.autotile_newtile(context, evaluator, key, x, y, ti[rule[2]], string.sub(key, #(rule[1]) + 1))
                return ti[key]
            else
                return tile
            end
        end
    end
    return evaluator(context[3], key, x, y)
end

function res.autotile(subresolver, ...)
    -- syntax: ... = <{prefixkey, template} | {firstkey, lastkey, template[, offset]}>
    -- context: [4] = table with unmodified rule tables
    assert_bool(is_resolver(subresolver), "res.autotile first argument (subresolver)", 2)
    local args = {...}
    for i, rule in ipairs(args) do
        assert_type(rule, "res.autotile argument " .. (i+1) .. " (rule " .. i .. ")", 2, "table")
        assert_bool((#rule >= 2) and (#rule <= 4), "res.autotile rule "..i.." has wrong number of arguments (must be 2, 3, or 4).", 2)
        local template_pos = 2
        local string_pos = {1,2}
        if #rule >= 3  then
            template_pos = 3
            string_pos = {1,2,3}
        end

        for j, num in ipairs(string_pos) do
            assert_type(rule[string_pos[num]], "res.autotile rule " .. i .. ", position " .. num, 2, "string")
        end
        if #rule == 4 then
            assert_type(rule[4], "res.autotile rule " .. i .. ", position 4 (offset)", 2, "integer")
        end
        if #rule >= 3 then
            local first = string.byte(rule[1], #rule[1])
            local last  = string.byte(rule[2], #rule[2])
            assert_bool(#rule[1] == #rule[2] and string.sub(rule[2], 1, -2) == string.sub(rule[1], 1, -2) and first <= last,
                "res.autotile: Rule " .. i .. " has bad range start-end strings.", 2)
        end
        assert_bool(ti[rule[template_pos]], "res.autotile: Missing template tile '" .. rule[template_pos] .. "'", 2)
    end
    local context = {res.autotile_implementation, nil, subresolver, args}
    setmetatable(context, res.metatable)
    return context
end

function res.composer_implementation(context, evaluator, key, x, y)
    local tile = evaluator(context[3], key, x, y)
    if tile ~= nil then
        return tile
    end
    local sequence = context[4]
    if sequence == nil then
        sequence = "123456789"
    end
    -- try to compose tile
    for i = 1, #key do
        local subkey = ""
        local ic = "" .. i
        for j = 1, #key do
            if sequence:sub(j, j) == ic then
                subkey = subkey .. key:sub(j, j)
            else
                subkey = subkey .. " "
            end
        end
        if subkey ~= string.rep(" ", #key) then
            local subtile = evaluator(context[3], subkey, x, y)
            if subtile == nil then
                return nil
            end
            if tile == nil then
                tile = subtile
            else
                tile = tile .. subtile
            end
        end
    end
    return tile
end

function res.composer(subresolver, ...)
    assert_bool(is_resolver(subresolver), "res.composer first argument (subresolver)", 2)
    local args = {...}
    local sequence = nil
    assert_bool(#args <= 1, "res.composer: Too many arguments (must be 1 or 2)." ,2)
    if #args == 1 then
        assert_type(args[1], "res.composer second argument (sequence)", 2, "non-empty string")
        sequence = args[1]
    end
    local context = {res.composer_implementation, nil, subresolver, sequence}
    setmetatable(context, res.metatable)
    return context
end

--------------------
-- Error handling --
--------------------

function etype(object)
    local t = type(object)
    if t == "userdata" then
        return usertype(object)
    elseif t == "table" then
        return (getmetatable(object) or {})._type or t
    end
    return t
end

function is_resolver(resolver)
    local t = etype(resolver)
    return (t == "tiles") or (t == "function") or (t == "resolver") or
        ((type(resolver) == "table") and getmetatable(resolver) and getmetatable(resolver)["_resolver"])
end

assert_type = function (object, objname, level, ...)
    -- assert_type throws an error if object does not fulfill one of the
    -- conditions in "...". Possible conditions are the lua- and usertype
    -- names and "map" (i.e. the results of etype), plus the strings
    -- "integer", "natural" (i.e. integer >= 0), "positive" (> 0), and
    -- "non-negative" (>= 0).
    local conditions = {...}
    local fulfilled = false
    for k = 1, #conditions do
        local condition = conditions[k]
        fulfilled = fulfilled
            or  (condition == etype(object))
            or ((condition == "integer") and (type(object) == "number") and (math.ceil(object) == object))
            or ((condition == "positive") and (type(object) == "number") and (object > 0))
            or ((condition == "non-negative") and (type(object) == "number") and (object >= 0))
            or ((condition == "natural") and (type(object) == "number") and (math.ceil(object) == object) and (object >= 0))
            or ((condition == "positive integer") and (type(object) == "number") and (math.ceil(object) == object) and (object > 0))
            or ((condition == "non-empty string") and (type(object) == "string") and (object ~= ""))
            or ((condition == "any table") and (type(object) == "table"))
            or ((condition == "valid object") and (etype(object) == "object") and (-object))
    end
    if not fulfilled then
        for k = 1, #conditions do
            if type(conditions[k]) ~= "string" then
                error("Error in type assertion: Wrong argument for assert_type, must be string.", 2)
            end
        end
        if type(objname) ~= "string" then
            error("Error in type assertion: Object name missing for assert_type.", 2)
        end
        local s = ""
        if objname ~= "" then
            s = " for " .. objname
        end
        if #conditions == 0 then
            error("Unconditional assertion" .. s .. ".", (level or 1) + 1)
        end
        s = "Wrong type" .. s ..", is " .. etype(object) ..", must be "
        if #conditions == 1 then
            s = s .. conditions[1] .. "."
        else
            s = s .. "one of "
            for k = 1, #conditions - 1 do
                s = s .. conditions[k] .. ", "
            end
            s = s .. conditions[#conditions] .. "."
        end
        error(s, (level or 1) + 1)
    end
end

assert_bool = function (bool, message, level)
    -- assert_bool throws an error with message MESSAGE if BOOL is false or nil.
    if not bool then
        if message and (type(message) == "string") and (message ~= "") then
            error(message, (level or 1) + 1)
        else
            error("Anonymous assertion failed.", (level or 1) + 1)
        end
    end
end