<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<el:level xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://enigma-game.org/schema/level/1 level.xsd" xmlns:el="http://enigma-game.org/schema/level/1">
  <el:protected >
    <el:info el:type="level">
      <el:identity el:title="Turnstiles for Two" el:subtitle="Rhapsody on Turnstiles in Blue and Green" el:id="20070314ral189"/>
      <el:version el:score="1" el:release="1" el:revision="$Revision: 64 $" el:status="released"/>
      <el:author  el:name="Ronald Lamprecht" el:email="ral@users.berlios.de"/>
      <el:copyright>Copyright © 2006 Ronald Lamprecht</el:copyright>
      <el:license el:type="GPL v2.0 or above" el:open="true"/>
      <el:compatibility el:enigma="1.00"/>
      <el:modes el:easy="true" el:single="true" el:network="false"/>
      <el:comments>
        <el:credits el:showinfo="true" el:showstart="false">Thanks to Nat Pryce for his libs and code examples</el:credits>
        <el:code>Lua 5.1</el:code>
      </el:comments>
      <el:score el:easy="12:29" el:difficult="16:36"/>
    </el:info>
    <el:luamain><![CDATA[
enigma.ConserveLevel = FALSE
math_floor = floor

set_oxyd = oxyd
oxyds = {}
oxyd_num = 0

function oxyd( x, y, tiles )
    local o = set_oxyd( x, y )
    oxyd_num = oxyd_num + 1
    oxyds[oxyd_num] = o
    return o
end

function nothing(x, y, tiles)
    -- create nothing
end

function horizontal_bolder(x,y,tiles)
    if random() >= 0.5 then
        return set_stone( "st-bolder-w", x, y )
    else
        return set_stone( "st-bolder-e", x, y )
    end
end

function vertical_bolder(x,y,tiles)
    if random() >= 0.5 then
        return set_stone( "st-bolder-n", x, y )
    else
        return set_stone( "st-bolder-s", x, y )
    end
end

function checkerboard_floor( type1, type2, attribs1, attribs2 )
    return function( x, y, tiles )
        if mod(x,2) == mod(y,2) then
            return set_floor( type1, x, y, attribs1 or {} )
        else
            return set_floor( type2, x, y, attribs2 or {} )
        end
    end
end

function floor( floor_type, attribs )
    return function( x, y, tiles )
        return set_floor( floor_type, x, y, attribs or {} )
    end
end

function stone( stone_type, attribs )
    return function( x, y, tiles )
        return set_stone( stone_type, x, y, attribs or {} )
    end
end

function item( item_type, attribs )
    return function( x, y, tiles )
        return set_item( item_type, x, y, attribs or {} )
    end
end

function actor( actor_type, attribs )
    return function( x, y, tiles )
        return set_actor( actor_type, x+0.5, y+0.5, attribs or {} )
    end
end

function gradient( gradient_type )
    return function( x, y, tiles )
        return set_floor( "fl-gradient", x, y, {type=gradient_type} )
    end
end

function inherit(tile_type)
    return function( x, y, tiles )
        return create_tile( tiles, x, y, tile_type )
    end
end

function group( array, constructor )
    return function( x, y, tiles )
        object = constructor( x, y, tiles )
        tinsert( array, object )
        return object
    end
end

function difficulty( d, constructor )
    if options.Difficulty == d then
        return constructor
    else
        return nothing
    end
end

function create_world_from_map( tiles, map )
    create_world( strlen(map[1]), getn(map) )
    
    for y = 1,getn(map) do
        local line = map[y]
        for x = 1,strlen(line) do
            local tile = strchar(strbyte(line,x))
            create_tile( tiles, x-1, y-1, tile )
        end
    end
end

function create_tile( tiles, x, y, tile_type )
    local constructors = tiles[tile_type]

    if constructors then
    for i = 1,getn(constructors) do
        constructors[i](x,y,tiles)
    end
    else
    error("invalid tile identifier " .. tile_type )
    end
end

oxyd_default_flavor = "a"

tiles = {}
tiles[" "] = {floor("fl-sahara")}
tiles["~"] = {floor("fl-water")}
tiles["v"] = {floor("fl-acwhite")}
tiles["b"] = {floor("fl-acblack")}
tiles["#"] = {inherit(" "), stone("st-wood_001")}
tiles["H"] = {inherit(" "), stone("st-wood")}
tiles["x"] = {inherit(" "), oxyd}
tiles["N"] = {inherit("~"), stone("st-turnstile-n")}
tiles["n"] = {inherit(" "), stone("st-turnstile-n")}
tiles["S"] = {inherit("~"), stone("st-turnstile-s")}
tiles["s"] = {inherit(" "), stone("st-turnstile-s")}
tiles["W"] = {inherit("~"), stone("st-turnstile-w")}
tiles["w"] = {inherit(" "), stone("st-turnstile-w")}
tiles["E"] = {inherit("~"), stone("st-turnstile-e")}
tiles["e"] = {inherit(" "), stone("st-turnstile-e")}
tiles["T"] = {inherit("~"), stone("st-turnstile-green")}
tiles["J"] = {inherit(" "), stone("st-oneway_white-e")}
tiles["I"] = {inherit(" "), stone("st-oneway_white-n")}
tiles["K"] = {inherit(" "), stone("st-oneway_white-s")}
tiles["k"] = {inherit(" "), stone("st-oneway_black-s")}
tiles["B"] = {inherit(" "), stone("st-black3")}
tiles["V"] = {inherit(" "), stone("st-white3")}
tiles["p"] = {inherit(" "), stone("st-brake")}
tiles["R"] = {inherit("~"), stone("st-rotator-right")}
tiles["r"] = {inherit(" "), stone("st-rotator_move-right")}
tiles["L"] = {inherit("~"), stone("st-rotator-left")}
tiles["ch"] = {inherit(" "), item("it-cherry")}
tiles["G"] = {inherit("ch"), stone("st-glass")}
tiles["U"] = {inherit(" "), item("it-vortex-closed", {name="vortex1",targetx=7, targety=17})}
tiles["u"] = {inherit(" "), item("it-vortex-closed", {targetx=7, targety=17})}


tiles["yb"] = {inherit(" "), item("it-yinyang")}
tiles["yw"] = {inherit(" "), item("it-yinyang")}

tiles["1"] = {inherit("yb"), actor("ac-blackball", {essential=1})}
tiles["2"] = {inherit("yw"), actor("ac-whiteball", {essential=1})}
tiles["8"] = {inherit(" "), actor("ac-top",{force=0, range=0})}

if difficult then
    tiles["P"] = tiles[" "]
else
    tiles["P"] = {inherit(" "), item("it-pin")}
end

create_world_from_map( tiles, {
"####################",
"#x ~  x  JJ  x  ~ x#",
"#  ~     ~~     ~  #",
"#  ~     ~~  n  ~  #",
"#BB~~~T~~~~~~T~~~BB#",
"#  R  s      s  ~  #",
"# b~b  P2  1P  vNv #",
"# wTe          wTe #",
"# vSv    n     vSv #",
"#~~~~I~~~T~R~~k~~~~#",
"#     ~      ~     #",
"# ~~~~~ r    ~~~~~ #",
"# ~ B ~   p  ~ V ~ #",
"# ~xBwTe    wTeVx~ #",
"# ~ B ~    r ~ V ~ #",
"# ~~~~~U     ~~~~~ #",
"# N   ~~R~T~~~   N #",
"#wT x ~u  s  ~ x Te#",
"# S   ~      ~   S #",
"# ~~~~~R    L~~~~~ #",
"# ~   ~      ~   ~ #",
"# ~   Te    wT   ~ #",
"# ~   ~      ~   ~ #",
"#V~~~~~  n   ~~~~~B#",
"#     ~~WTE~~~     #",
"#~~      s      ####",
"#x~      ~~~  ###x##",
"# ~      ~x~  #VVVV#",
"# ~  ~~~V~B~  #GGGG#",
"#wTe ~xB N V  #    #",
"# ~~ ~~~~T~~~~   w #",
"# ~~   B S Vx~  r  #",
"# L~~  ~V~B~~~     #",
"# ~8~  ~x~  N   G  #",
"L T~L  ~~~  T    T #",
"# s B       S      #",
"####################"
})
SendMessage(enigma.GetNamedObject("vortex1"), "open")

repeat
    local good_oxyds = 0
    oxyd_shuffle()
    for i = 1, 4 do
        local c = enigma.GetAttrib(oxyds[i], "color")
        for j = 9, 14 do
            if (c == enigma.GetAttrib(oxyds[j], "color")) then
                good_oxyds = good_oxyds + 1
            end
        end
    end
until good_oxyds >= 2

c = math.random(8) - 1
set_oxyd( 4, 21, oxyd_default_flavor, c)
set_oxyd(15, 21, oxyd_default_flavor, c)
if (c < 7) then
    for i = 1, 14 do
        if (c == (enigma.GetAttrib(oxyds[i], "color") + 0)) then
            enigma.SetAttrib(oxyds[i], "color", "7")
        end
    end
end

display.SetFollowMode (display.FOLLOW_SCROLLING )
    ]]></el:luamain>
    <el:i18n>
      <el:string el:key="title">
        <el:english el:translate="false"/>
      </el:string>
    </el:i18n>
  </el:protected>
</el:level>
