<?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="Panelled Puzzle II" el:subtitle="" el:id="20090308ral237"/>
      <el:version el:score="1" el:release="1" el:revision="$Revision: 179 $" el:status="released"/>
      <el:author  el:name="Ronald Lamprecht" el:email="ral@users.berlios.de"/>
      <el:copyright>Copyright © 2009 Ronald Lamprecht</el:copyright>
      <el:license el:type="GPL v2.0 or above" el:open="true"/>
      <el:compatibility el:enigma="1.10">
        <el:dependency el:path="lib/libmath" el:id="lib/libmath" el:release="1" el:preload="true"/>
      </el:compatibility>
      <el:modes el:easy="true" el:single="true" el:network="false"/>
      <el:score el:easy="5:27" el:difficult="7:35"/>
    </el:info>
    <el:luamain><![CDATA[
wo["ConserveLevel"] = true

ti[" "] = {"fl_blueslab_framed", "fl#", checkerboard=0} .. ti{"fl_platinum_framed", "fl#", friction=7, adhesion=2, checkerboard=1}
ti[":"] = {"fl_blueslab_framed", checkerboard=1} .. ti{"fl_platinum_framed", friction=7, adhesion=2, checkerboard=0}
ti["."] = {"fl_abyss"}
ti["+"] = {"fl_abyss", "oxydpos#"}
ti["#"] = {"st_panel", cluster=1}

ti["P"] = {"st_pull", "westpull"}

ti["trigger"] = {"it_trigger", "tr%%", invisible=true, target="counter", action="signal"}

ti["*"] = {"st_spitter", "spitter", secure=true}
ti["X"] = {"st_pull", "eastpull"} .. ti{"fl_redslab_framed"} .. ti{"it_trigger", invisible=true, target="spitter"}
ti["S"] = {"st_dispenser_extralife"}
ti["$"] = {"it_extralife"}

ti["@"] = {"#ac_marble"}

if wo["IsDifficult"] then
    ti["?"] = {"it_document", text="solutionseven"}
else
    ti["?"] = {"it_document", text="solutionseasy"}
end

resolver = res.autotile(ti, {"A", "O", "trigger"})

if wo["IsDifficult"] then
    w, h = wo(resolver, " ", {
    ".#....#....#....#.",
    "########..########",
    ".#ABCD#+..+#@   #.",
    ".#EFGH#.++.* ?  #.",
    ".#IJKL#.++.S    #.",
    ".#MNPO#+..+#  X #.",
    "########..########",
    ".#....#....#....#."
    })
else
    w, h = wo(resolver, " ", {
    ".#....#....#....#.",
    "########..########",
    ".#ABCD#+..+#@   #.",
    ".#EFGH#.++.* ?  #.",
    ".#IJPK#.++.S  X #.",
    ".#LMNO#+..+#    #.",
    "########..########",
    ".#....#....#....#."
    })
end

wo:add({"ot_wire", "wire", anchor1="westpull", anchor2="eastpull"})
wo:add({"ot_counter", "counter", state=15, target_0="conjure"})

connections = {"w","s","sw","e","ew","es","esw","n","nw","ns","nsw",
                "ne","new","nes","nesw"}

function parity(p)
    sum = 0
    for i = 2, 15 do
        for j = 1, i do
            sum = sum + cond(p[j] > p[i], 1, 0)
        end
    end
    return sum % 2
end

perm = lib.math.permutation(15)
if parity(perm) == 1 then
    perm[1], perm[2] = perm[2], perm[1]
end
state = 1  -- first odd run for difficult mode

for i = 1, 15 do
    wo[no["tr%"..i]] = {"st_puzzle", connections=connections[perm[i]]}
end

function conjure()
    if state == 1 and wo["IsDifficult"] then
        -- now we play an even parity
        perm = lib.math.permutation(15)
        if parity(perm) == 0 then
            perm[1], perm[2] = perm[2], perm[1]
        end
        for i = 1, 15 do
            wo[no["tr%"..i]] = {"st_puzzle", connections=connections[perm[i]]}
        end
        state = 0
        -- do indicate changed parity
        wo[no["fl#*"]] = ti[":"]
        wo[no["eastpull"]+{0,-1}] = {"it_document", text="solutionsodd"}
    else
        wo[no["oxydpos#*"]] = {"st_oxyd_d"}
        no["wire"]:kill()
    end
end

    ]]></el:luamain>
    <el:i18n>
      <el:string el:key="title">
        <el:english el:translate="false"/>
      </el:string>
      <el:string el:key="solutionseven">
        <el:english el:translate="true" >There are 19 fully connected puzzle piece arrangements, but just 9 of them are reachable from this first distribution.</el:english>
        <el:translation el:lang="de">Es gibt 19 Figuren mit voll verbundenen Puzzleteilen, aber ausgehend von der ersten Verteilung, können nur 9 dieser Figuren erreicht werden.</el:translation>
      </el:string>
      <el:string el:key="solutionsodd">
        <el:english el:translate="true" >This second distribution can just be converted to any of the other 10 arrangements.</el:english>
        <el:translation el:lang="de">Diese zweite Verteilung kann nun in jede der anderen 10 Figuren überführt werden.</el:translation>
      </el:string>
      <el:string el:key="solutionseasy">
        <el:english el:translate="true" >There are just two symmetric puzzle piece arrangements that both are reachable.</el:english>
        <el:translation el:lang="de">Es gibt nur zwei symmetrische Figuren aus den Puzzleteilen die beide erreichbar sind.</el:translation>
      </el:string>
    </el:i18n>
  </el:protected>
</el:level>
