<?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="Spaceship Control" el:subtitle="Bound to the blue nowhere" el:id="safalra01"/>
      <el:version el:score="1" el:release="1" el:revision="1" el:status="released"/>
      <el:author el:name="Safalra" el:email="" el:homepage="http://www.safalra.com/"/>
      <el:copyright>Copyright © 2007 Safalra (Stephen Morley)</el:copyright>
      <el:license el:type="special" el:open="true">
        The author of this level, Safalra (Stephen Morley), irrevocably releases all rights to the level, with the intention of it becoming part of the public domain. Because the level is released into the public domain, it comes with no warranty either expressed or implied, to the extent permitted by law.
      </el:license>
      <el:compatibility el:enigma="0.92"/>
      <el:modes el:easy="false" el:single="true" el:network="false"/>
      <el:score el:easy="-" el:difficult="-"/>
    </el:info>
    <el:luamain><![CDATA[
create_world(20,13)

fill_floor("fl-space")
fill_floor("fl-rough-red",7,3,6,6)

draw_stones("st-rock1",{6,2},{1,0},8)
draw_stones("st-rock1",{6,9},{1,0},8)
draw_stones("st-rock1",{8,10},{1,0},4)
draw_stones("st-rock1",{6,3},{0,1},6)
draw_stones("st-rock1",{13,3},{0,1},6)

set_stone("st-glass1_hole",9,8)
set_stone("st-glass1",10,8)

n=0
for i=4,7 do
    for j=8,11 do
        n=n+1
        set_stone("st-fourswitch",j,i,{name=("switch"..n),action="callback",target=("is"..n)})
    end
end

oxyd(9,9)
oxyd(10,9)

set_actor("ac-blackball",10,3.5)

-- Initialise the puzzle with random switch states
function init_puzzle()
    a = random(1, 4)
    repeat
        b = random(1, 4)
    until (b ~= a)
    repeat
        c = random(1, 4)
    until (c ~= a)
    repeat
        d = random(1, 4)
    until (d ~= b and d ~= c)
    repeat
        e = random(1, 4)
    until (e ~= d and e ~= b)
    t = {nil,nil,nil,nil,nil,a,b,nil,nil,c,d,nil,nil,nil,e,nil}
    for i=1, 16 do
        if t[i] == nil then
            t[i] = random(1, 4)
        end
    end
    return t
end

s = init_puzzle()

-- Functions for the switches
function is1() s[1]=s[1]+1; update() end
function is2() s[2]=s[2]+1; update() end
function is3() s[3]=s[3]+1; update() end
function is4() s[4]=s[4]+1; update() end
function is5() s[5]=s[5]+1; update() end
function is6() s[6]=s[6]+1; update() end
function is7() s[7]=s[7]+1; update() end
function is8() s[8]=s[8]+1; update() end
function is9() s[9]=s[9]+1; update() end
function is10() s[10]=s[10]+1; update() end
function is11() s[11]=s[11]+1; update() end
function is12() s[12]=s[12]+1; update() end
function is13() s[13]=s[13]+1; update() end
function is14() s[14]=s[14]+1; update() end
function is15() s[15]=s[15]+1; update() end
function is16() s[16]=s[16]+1; update() end

-- check if the four switches at i, j, k, l are in different states
function check(i,j,k,l)
    c={false,false,false,false}
    c[s[i]]=true
    c[s[j]]=true
    c[s[k]]=true
    c[s[l]]=true
    return c[1] and c[2] and c[3] and c[4]
end

function update()
    for i=1,16 do
        if s[i]>4 then s[i]=s[i]-4 end
    end
    done=true

    for i=1,4 do
        -- Check all cols
        if check(i,i+4,i+8,i+12) then
            f="fl-rough-blue"
        else
            f="fl-rough-red"
            done=false
        end
        if enigma.GetKind(enigma.GetFloor(i+7,3))~=f then
            set_floor(f,i+7,3)
            set_floor(f,i+7,8)
        end

        -- Check all rows
        if check(4*i-3,4*i-2,4*i-1,4*i) then
            f="fl-rough-blue"
        else
            f="fl-rough-red"
            done=false
        end
        if enigma.GetKind(enigma.GetFloor(7,i+3))~=f then
            set_floor(f,7,i+3)
            set_floor(f,12,i+3)
        end

        -- Check all corners
        if check(1,2,5,6) then
            f="fl-rough-blue"
        else
            f="fl-rough-red"
            done=false
        end
        if enigma.GetKind(enigma.GetFloor(7,3))~=f then
            set_floor(f,7,3)
        end
        if check(3,4,7,8) then
            f="fl-rough-blue"
        else
            f="fl-rough-red"
            done=false
        end
        if enigma.GetKind(enigma.GetFloor(12,3))~=f then
            set_floor(f,12,3)
        end
        if check(9,10,13,14) then
            f="fl-rough-blue"
        else
            f="fl-rough-red"
            done=false
        end
        if enigma.GetKind(enigma.GetFloor(7,8))~=f then
            set_floor(f,7,8)
        end
        if check(11,12,15,16) then
            f="fl-rough-blue"
        else
            f="fl-rough-red"
            done=false
        end
        if enigma.GetKind(enigma.GetFloor(12,8))~=f then
            set_floor(f,12,8)
        end
    end
    if done then set_stone("st-glass1_hole",10,8) end
end

-- Initialise the switches
for i=1, 16 do
    e = s[i]-1
    s[i] = 1
    for times = 1, e do
        enigma.SendMessage(enigma.GetNamedObject("switch"..tostring(i)),"trigger",nil)
    end
end
    ]]></el:luamain>
    <el:i18n>
      <el:string el:key="title">
        <el:english el:translate="true"/>
      </el:string>
      <el:string el:key="subtitle">
        <el:english el:translate="true"/>
      </el:string>
    </el:i18n>
  </el:protected>
</el:level>

