<?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="Now What?" el:subtitle="" el:id="duffy127"/>
      <el:version el:score="1" el:release="2" el:revision="2" el:status="released"/>
      <el:author  el:name="Jacob Scott" el:email="" el:homepage=""/>
      <el:copyright>Copyright © 2004 Jacob Scott</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:comments><el:code>New API converted by R. Bourquin</el:code></el:comments>
      <el:score el:easy="-" el:difficult="-"/>
    </el:info>
    <el:luamain><![CDATA[
wo["ConserveLevel"] = false

ti["  "] = {"fl_pinkbumps"}
ti["##"] = {"st_redbrown"}
ti["||"] = {"st_door", flavor="a", name="firstdoor"}
ti["--"] = {"st_door", flavor="a", name="finaldoors#"}
ti["**"] = ti({"st_magic_oxyda"}) .. {"it_cross"}
ti["trigger"] = {"it_trigger", name="trigger%%", target="trigger_callback"}
ti["stone"] = {"st_door", flavor="a", state=OPEN, name="stone%%"}
ti["OO"] = {"st_oxyd", flavor="a"}
ti["@@"] = {"#ac_marble_black"}

wo(res.autotile(ti, {"S", "stone"}, {"t", "trigger"}),  "  ",  {
    "########################################",
    "##        ta  tb  tc  td  te      ##OO##",
    "##      taSA  SB  SC  SD  SEte    ##--##",
    "##                                ##  ##",
    "##      tpSF  SG  SH  SI  SJtf    ##  ##",
    "##                                ##  ##",
    "##tq  @@toSK  SL  SM  SN  SOtg    ||  **",
    "##                                ##  ##",
    "##      tnSP  SQ  SR  SS  STth    ##  ##",
    "##                                ##  ##",
    "##      tmSU  SV  SW  SX  SYti    ##--##",
    "##        tm  tl  tk  tj  ti      ##OO##",
    "########################################"})

wo:shuffleOxyd()

-- The "neighbours" are defined by the position offsets:
local neighbours = 2*N .. 2*E .. 2*S .. 2*W .. 4*N .. 4*E .. 4*S .. 4*W

-- The group with all doors we can act on:
local doors = no["stone*"]
local closed = grp({no["stone%G"], no["stone%H"], no["stone%N"], no["stone%R"], no["stone%Q"]})

-- The following doors are initially closed:
closed["state"] = CLOSED

-- Ending condition group:
local ec = grp({no["stone%A"], no["stone%E"], no["stone%G"], no["stone%I"], no["stone%M"], no["stone%Q"], no["stone%S"], no["stone%U"], no["stone%Y"]})

function trigger_callback(value, sender)
    if value == false then
        return
    end
    center = doors:nearest(sender)
    if sender["name"] == "trigger%q" then
        center = no["stone%M"]
    end

    local actives = doors * st(center + neighbours)  -- get the doors we want to toggle
    closed = (closed + actives) - (closed * actives)  -- update closed door group
    actives:toggle()

    -- check ending conditions:
    if doors == closed then
        no["firstdoor"]:open()
    end
    if no["firstdoor"].state == OPEN and closed == ec then
        no["finaldoors#*"]:open()
    end
end
 ]]></el:luamain>
    <el:i18n>
      <el:string el:key="title">
        <el:english el:translate="false"/>
      </el:string>
    </el:i18n>
  </el:protected>
</el:level>
