<?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="Welcome to the Machine" el:subtitle="" el:id="ralf03"/>
      <el:version el:score="1" el:release="1" el:revision="0" el:status="released"/>
      <el:author  el:name="Ralf Westram" el:email="amgine@reallysoft.de" el:homepage=""/>
      <el:copyright>Copyright © 2003 Ralf Westram</el:copyright>
      <el:license el:type="GPL v2.0 or above" el:open="true"/>
      <el:compatibility el:enigma="0.92">
       <el:dependency el:path="lib/ralf" el:id="lib/ralf" el:release="1" el:preload="true"/>
      </el:compatibility>
      <el:modes el:easy="false" el:single="true" el:network="false"/>
      <el:comments>
        <el:code>Lua 5.1 and XML converted by Leveladministrators</el:code>
      </el:comments>
      <el:score el:easy="-" el:difficult="-"/>
    </el:info>
    <el:luamain><![CDATA[    
first = 1

lightfloorface = "fl-sahara"
darkfloorface  = "fl-tigris"
bluefloorface  = "fl-bluegray"

stonefaces   = { "st-rock1", "st-rock6", "st-metal", "st-rock3"}
rotdirection = { 1, -1, 1, -1 }
switchface   = "st-switch"
borderface   = "st-rock5"

lightfloor   = cell{floor={face = lightfloorface}}
border       = cell{stone={face = borderface}}
--marble       = cell{item={face  = "ac-blackball", attr={player=0}, actor=1}}
marble       = cell{actor={face="ac-blackball", attr={player=0}}}

test = 0 -- test layout
easy = 1 -- 1=easy 0=hard

mx,my = 1,1

if (test==0) then
   floormap={
      "**   **",
      "*     *",
      "   !   ",
      "  !!!  ",
      "   !   ",
      "*     *",
      "**   **"
   }
   stonemap={
      "  #    ",
      " ##### ",
      "##   ##",
      " # O   ",
      " #   ##",
      " $# ## ",
      "  & #  "
   }
   metamap={
      "2413",
      "4324",
      "3112",
   }
   mx,my = 2,2

elseif (test==1) then
   easy = 1
   floormap={
      "* ! *",
      "  !  ",
      "!!!!!",
      "  !  ",
      "* ! *"
   }
   stonemap={
      " & & ",
      " # ##",
      "  O  ",
      "#$ # ",
      " # # "
   }
   metamap={
      "4231",
      "3421",
      "1234",
      "3142",
   }
   mx,my = 3,2

end

floorcells={}
floorcells["*"] = cell{parent = lightfloor}
floorcells[" "] = cell{floor={face = darkfloorface}}
floorcells["!"] = cell{floor={face = bluefloorface}}

-- ---------------
--     metamap
-- ---------------

usedmap   = nil
usedcells = nil
currmetacell = nil

function draw_used_metamap_oriented(x,y,flipx,flipy,rotate)
   draw_metamap_oriented(x,y,usedmap,usedcells,flipx, flipy,rotate)
end

metacellstate={1,2,3,4}
nextState={2,3,4,1} -- rotate
--nextState={2,1,4,3} -- toggle

function draw_metacell(x,y,metacellnum)
   currmetacell = metacellnum --store for switch callback
   draw_metamap_oriented(x,y,usedmap,usedcells,0,0,metacellstate[metacellnum])
end

metacells={}
metacells["1"] = cell{parent={{draw_metacell,1}}}
metacells["2"] = cell{parent={{draw_metacell,2}}}
metacells["3"] = cell{parent={{draw_metacell,3}}}
metacells["4"] = cell{parent={{draw_metacell,4}}}

function draw_my_metamap()
   draw_map(0,0,metamap,metacells)
end

-- ----------------
--     rotation
-- ----------------

function rot(cellnum)
   local oldstate = metacellstate[cellnum]
   local newstate = oldstate + rotdirection[cellnum]
   if (newstate < 1) then newstate = 4
   elseif (newstate > 4) then newstate = 1
   end

   metacellstate[cellnum] = newstate
   debug("rot cell #"..cellnum.." from state "..oldstate.." to "..newstate)

   draw_my_metamap()
end

function rot1() rot(1) end
function rot2() rot(2) end
function rot3() rot(3) end
function rot4() rot(4) end
function rot0() error("illegal rotation callback (0)")  end
function rot5() error("illegal rotation callback (5)")  end

function add_oxyd(x,y) if (first==1) then oxyd(x,y) end end
function add_stone(x,y) set_stone(stonefaces[currmetacell],x,y) end
function rem(x,y) if (first==0) then enigma.KillStone(x,y) end end

function repby_switch(x,y)
   rem(x,y)
   set_stone(switchface,x,y,{action="callback", target="rot"..currmetacell})
end
function repby_stone(x,y)
   rem(x,y)
   set_stone(stonefaces[currmetacell],x,y)
end
function repby_stone_or_gap(x,y)
   rem(x,y)
   if (currmetacell ~= 2) then
      set_stone(stonefaces[currmetacell],x,y)
   end
end


stonecells={}
stonecells["O"]=cell{parent={{add_oxyd}}}
stonecells[" "]=cell{parent={{rem}}}
stonecells["#"]=cell{parent={{repby_stone}}}
stonecells["$"]=cell{parent={{repby_switch}}}
if (easy==1) then
   stonecells["&"]=cell{parent={{repby_stone_or_gap}}}
else
   stonecells["&"]= stonecells["#"]
end

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- build the level

mapw,maph       = get_map_size(metamap)
blockw,blockh   = get_map_size(stonemap)
blockw2,blockh2 = get_map_size(floormap)

if ((blockh ~= blockh2) or (blockw ~= blockw2)) then
   error("floormap and stonemap must be of same size");
end

worldw,worldh = mapw*blockw+2,maph*blockh+2

create_world(worldw, worldh)
oxyd_default_flavor = "a"
enigma.ConserveLevel = FALSE

fill_world_func(lightfloor)
draw_border_func(border)

-- draw the floor
usedmap   = floormap
usedcells = floorcells
draw_my_metamap()

-- draw the items
usedmap = stonemap
usedcells = stonecells
draw_my_metamap()

first = 0

document(4*blockw,1*blockh,"text1")
document(1,2*blockh,"text2")

oxyd_shuffle()
display.SetFollowMode(display.FOLLOW_SMOOTH)
marble(mx*blockw+0.5,my*blockh+0.5)
    ]]></el:luamain>
    <el:i18n>
      <el:string el:key="title">
        <el:english el:translate="false"/>
      </el:string>
      <el:string el:key="text1">
        <el:english el:translate="true">Sections which have the same color always turn at the same time and in the same way.</el:english>
      </el:string> 
      <el:string el:key="text2">
        <el:english el:translate="true">Maintenance code: metal-stone-stone-stone-red-yellow-yellow-red-red-stone-stone.</el:english>
      </el:string> 
    </el:i18n>
  </el:protected>
</el:level>
