<?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="Rainbow Adjacencies" el:subtitle="" el:id="20090422ais523001"/>
      <el:version el:score="1" el:release="1" el:revision="2" el:status="released"/>
      <el:author  el:name="Alex Smith" el:email="ais523@bham.ac.uk" el:homepage=""/>
      <el:copyright>Copyright © 2009 Alex Smith</el:copyright>
      <el:license el:type="GPL v2.0 or above" el:open="true"/>
      <el:compatibility el:enigma="1.10"/>
      <el:modes el:easy="false" el:single="true" el:network="false"/>
      <el:score el:easy="-" el:difficult="1:15"/>
    </el:info>
    <el:luamain><![CDATA[
ti[" "] = {"fl_fake_abyss"}
ti["#"] = {"st_rawglass"}
ti["x"] = {"st_oxyd_b"}
ti["R"] = {"fl_red", _r="R"}
ti["O"] = {"fl_sand", _r="O"}
ti["Y"] = {"fl_sahara", _r="Y"}
ti["G"] = {"fl_lawn", _r="G"}
ti["B"] = {"fl_ice", _r="B"}
ti["I"] = {"fl_aquamarine", _r="I"}
ti["V"] = {"fl_redslab", _r="V"}
ti["r"] = ti["R"] .. {"it_trigger", invisible=true, target="colchange"}
ti[">"] = {"st_laser_e", state=ON}
ti["Z"] = {"st_mirror_slab", orientation=EAST}
ti["N"] = {"st_mirror_slab", orientation=WEST}
ti["n"] = {"st_mirror_slab", orientation=WEST, transparent=true}
ti["|"] = {"it_strip_ns"}
ti["-"] = {"it_strip_ew"}
ti["="] = ti["#"] .. ti["-"]
ti["i"] = ti["#"] .. ti["|"]
ti["@"] = {"#ac_marble"}

w, h = wo(ti, " ", {
    ">NZNZNZnxi#i#i#i#i#",
    "#ROYGBIV=r-r-r-r-r=",
    "#       #|@| | | |#",
    "#RRRRRRR=r-r-r-r-r=",
    "# OOOOOO#| | | | |#",
    "#  YYYYY=r-r-r-r-r=",
    "#   GGGG#| | | | |#",
    "#    BBB=r-r-r-r-r=",
    "#     II#| | | | |#",
    "#      V=r-r-r-r-r=",
    "#NZNZNZNxi#i#i#i#i#"})

cells = {}

function setcol(x,y,c)
    cells[((x+5)%5)+(((y+5)%5)*5)] = c
end

function getcol(x,y)
    local r = cells[((x+5)%5)+(((y+5)%5)*5)]
    if r == nil then r = 0 end
    return r
end

function connected(x)
    local mycells = {}
    local onfirst = true
    for rep = 1, 25 do
        for a = 0, 4 do for b = 0, 4 do
            if getcol(a,b) == x then
                if onfirst then
                    mycells[a+b*5] = 1
                    onfirst = false
                end
                if mycells[((a+6)%5)+(((b+5)%5)*5)] == 1 then mycells[a+b*5] = 1 end
                if mycells[((a+4)%5)+(((b+5)%5)*5)] == 1 then mycells[a+b*5] = 1 end
                if mycells[((a+5)%5)+(((b+6)%5)*5)] == 1 then mycells[a+b*5] = 1 end
                if mycells[((a+5)%5)+(((b+4)%5)*5)] == 1 then mycells[a+b*5] = 1 end
                if rep == 25 and mycells[a+b*5] ~= 1 then return false end
            end
        end end
    end
    return true
end

function touching(x,y)
    for a = 0, 4 do for b = 0, 4 do
        if getcol(a,b) == x then
            if getcol(a+1,b) == y then return true end
            if getcol(a-1,b) == y then return true end
            if getcol(a,b+1) == y then return true end
            if getcol(a,b-1) == y then return true end
        end
    end end
    return false
end

function crossupdate()
    local v
    for x = 0, 6 do for y = 0, 6 do
        wo[{x+1,y+3}] = {"it_cross"}
        v = true
        if x > y then
            v = touching(x,y)
        elseif x == y then
            v = connected(x)
        end
        if v then it({x+1,y+3}):kill() end
    end end
end

function colchange(sig, target)
    local nc
    local x
    local y
    if sig then
        if     fl(target)["_r"] == "R" then wo[target]=ti["O"] nc=1
        elseif fl(target)["_r"] == "O" then wo[target]=ti["Y"] nc=2
        elseif fl(target)["_r"] == "Y" then wo[target]=ti["G"] nc=3
        elseif fl(target)["_r"] == "G" then wo[target]=ti["B"] nc=4
        elseif fl(target)["_r"] == "B" then wo[target]=ti["I"] nc=5
        elseif fl(target)["_r"] == "I" then wo[target]=ti["V"] nc=6
        elseif fl(target)["_r"] == "V" then wo[target]=ti["R"] nc=0
        end
        x,y = target:xy()
        x = (x-9) / 2
        y = (y-1) / 2
        setcol(x,y,nc)
        wo[po(target)+{-1,0}] = {"it_strip_ew"}
        if nc ~= getcol(x-1,y) then it(po(target)+{-1,0}):kill() end
        wo[po(target)+{ 1,0}] = {"it_strip_ew"}
        if nc ~= getcol(x+1,y) then it(po(target)+{ 1,0}):kill() end
        wo[po(target)+{0,-1}] = {"it_strip_ns"}
        if nc ~= getcol(x,y-1) then it(po(target)+{0,-1}):kill() end
        wo[po(target)+{0, 1}] = {"it_strip_ns"}
        if nc ~= getcol(x,y+1) then it(po(target)+{0, 1}):kill() end
        if x == 0 then
            wo[po(target)+{ 9,0}] = {"it_strip_ew"}
            if nc ~= getcol(x-1,y) then it(po(target)+{ 9,0}):kill() end
        end
        if x == 4 then
            wo[po(target)+{-9,0}] = {"it_strip_ew"}
            if nc ~= getcol(x+1,y) then it(po(target)+{-9,0}):kill() end
        end
        if y == 0 then
            wo[po(target)+{0, 9}] = {"it_strip_ns"}
            if nc ~= getcol(x,y-1) then it(po(target)+{0, 9}):kill() end
        end
        if y == 4 then
            wo[po(target)+{0,-9}] = {"it_strip_ns"}
            if nc ~= getcol(x,y+1) then it(po(target)+{0,-9}):kill() end
        end
        crossupdate()
    end
end

crossupdate()
 ]]></el:luamain>
    <el:i18n>
      <el:string el:key="title">
        <el:english el:translate="true"/>
      </el:string>
    </el:i18n>
  </el:protected>
</el:level>
