-- Sven Siggelkow 2003
-- ESPRIT #083
-- problem: orientation switching of the oneways is not properly possible. 
--          set_attrib does work but only for the direction, the image shows still the initial arrow.
--          if it is fixed, you can correct it.  

levelw = 20
levelh = 13
create_world(levelw, levelh)
oxyd_default_flavor = "a"
set_actor("ac-blackball", 15.5,6.5, {player=0})
fill_floor("fl-black",0,0,levelw,levelh)

function renderLine( line, pattern)
    for i=1, strlen(pattern) do
      local c = strsub( pattern, i, i)
      if c ==   "#" then
         set_stone("st-blocker", i-1,line)
      elseif c=="s" then
         set_stone("st-switch_black", i-1,line)
      elseif c=="1" then
         set_stone("st-switch_black", i-1,line,{name="s1s", target="s1", action="callback"})
      elseif c=="2" then
         set_stone("st-switch_black", i-1,line,{name="s2s", target="s2", action="callback"})
      elseif c=="3" then
         set_stone("st-switch_black", i-1,line,{target="s3", action="callback"})
      elseif c=="4" then
         set_stone("st-switch_black", i-1,line,{target="s4", action="callback"})
      elseif c=="5" then
         set_stone("st-switch_black", i-1,line,{name="s5s", target="s5", action="callback"})
      elseif c=="6" then
         set_stone("st-switch_black", i-1,line,{name="s6s", target="s6", action="callback"})
      elseif c=="7" then
         set_stone("st-switch_black", i-1,line,{target="s7", action="callback"})
      elseif c=="8" then
         set_stone("st-switch_black", i-1,line,{target="s8", action="callback"})
      elseif c=="9" then
         set_stone("st-switch_black", i-1,line,{name="s9s", target="s9", action="callback"})
      elseif c=="A" then
         set_stone("st-switch_black", i-1,line,{target="sA", action="callback"})
      elseif c=="B" then
         set_stone("st-switch_black", i-1,line,{target="sB", action="callback"})
      elseif c=="C" then
         set_stone("st-switch_black", i-1,line,{target="sC", action="callback"})
      elseif c=="D" then
         set_stone("st-switch_black", i-1,line,{target="sD", action="callback"})
      elseif c=="E" then
         set_stone("st-switch_black", i-1,line,{target="sE", action="callback"})
      elseif c==">" then
         set_stone("st-oneway_black-e", i-1,line)
      elseif c=="|" then
         set_stone("st-oneway_black-s", i-1,line)
      elseif c=="T" then
         set_stone("st-oneway_black", i-1,line,{orientation=EAST})
         --set_item("it-trigger", i-1,line, {action="callback", target="sT"})
      elseif c=="a" then
         set_stone("st-oneway_black", i-1,line,{name="a", orientation=SOUTH})
      elseif c=="b" then
         set_stone("st-oneway_black", i-1,line,{name="b", orientation=EAST})
      elseif c=="f" then
         set_stone("st-oneway_black", i-1,line,{name="f", orientation=EAST})
      elseif c=="h" then
         set_stone("st-oneway_black", i-1,line,{name="h", orientation=EAST})
      elseif c=="o" then
         set_stone("st-oneway_black", i-1,line,{name="o", orientation=SOUTH})
      elseif c=="p" then
         set_stone("st-oneway_black", i-1,line,{name="p", orientation=SOUTH})
      elseif c=="c" then
         set_stone("st-oneway_black", i-1,line,{name="c", orientation=EAST})
      elseif c=="d" then
         set_stone("st-oneway_black", i-1,line,{name="d", orientation=EAST})
      elseif c=="e" then
         set_stone("st-oneway_black", i-1,line,{name="e", orientation=EAST})
      elseif c=="l" then
         set_stone("st-oneway_black", i-1,line,{name="l", orientation=SOUTH})
      elseif c=="n" then
         set_stone("st-oneway_black", i-1,line,{name="n", orientation=SOUTH})
      elseif c =="w" then                                                               
         set_item("it-wormhole",i-1,line, {name="warp",targetx="18.5",targety="6.5",strength=0})
      elseif c=="O" then
	 oxyd(i-1,line)         
        end
    end  
end
--               01234567890123456789
renderLine(00 , "####################")
renderLine(01 , "####################")
renderLine(02 , "#    f > > c b   # O")
renderLine(03 , "#    1|s|2o3|s   # #")
renderLine(04 , "#    T > d > >   # O")  
renderLine(05 , "#    s|4|5p6|7   # #")
renderLine(06 , "#w   > e > > >   # #")
renderLine(07 , "#    8|s|9as|s   # #")
renderLine(08 , "#    h > > > >   # O")
renderLine(09 , "#    A|B|ClDnE   # #")
renderLine(10 , "#    > > > > >   # O")
renderLine(11 , "####################")
renderLine(12 , "####################") 
--               01234567890123456789

oxyd_shuffle()

-- direction   | > > > > > > | | | |
-- oneway #    a b c d e f h l n o p
local flags = {0,0,0,0,0,0,0,0,0,0,0}
-- i           1 2 3 4 5 6 7 8 9 0 1

--   switch #     1 2 5 6 9
local switches = {0,0,0,0,0}
-- i              1 2 3 4 5

function s1() -- 1->5
%switches[1] = 1 - %switches[1]  -- store switch state, because this switch will be switched!
if %switches[3] == 0 then enigma.SendMessage( enigma.GetNamedObject("s5s"), "on", 1)  -- Schalter umlegen.
	else enigma.SendMessage( enigma.GetNamedObject("s5s"), "off", 0)
	end
s5()
end

function s2() -- 2->a
%switches[2] = 1 - %switches[2]
%flags[1] = 1 - %flags[1]
if %flags[1] == 0 then
	set_stone("st-oneway_black", 10,7,{orientation=SOUTH})
    else
	set_stone("st-oneway_black", 10,7,{orientation=NORTH})
end
end

function s3() -- 3->p
%flags[11] = 1 - %flags[11]
if %flags[11] == 0 then
	set_stone("st-oneway_black", 10,5,{orientation=SOUTH})
    else
	set_stone("st-oneway_black", 10,5,{orientation=NORTH})
end
end

function s4() -- 4->h
%flags[7] = 1 - %flags[7]
if %flags[7] == 0 then
	set_stone("st-oneway_black", 5,8,{orientation=EAST})
    else
	set_stone("st-oneway_black", 5,8,{orientation=WEST})
end
end

function s5() -- 5->b
%switches[3] = 1 - %switches[3]
%flags[2] = 1 - %flags[2]
if %flags[2] == 0 then
	set_stone("st-oneway_black", 13,2,{orientation=EAST})
    else
	set_stone("st-oneway_black", 13,2,{orientation=WEST})
end
end


function s6() -- 6->c
%switches[4] = 1 - %switches[4]
%flags[3] = 1 - %flags[3]
if %flags[3] == 0 then
	set_stone("st-oneway_black", 11,2,{orientation=EAST})
    else
	set_stone("st-oneway_black", 11,2,{orientation=WEST})
end
end

function s7() -- 7->9
if %switches[5] == 0 then enigma.SendMessage( enigma.GetNamedObject("s9s"), "on", 1)  -- Schalter umlegen.
	else enigma.SendMessage( enigma.GetNamedObject("s9s"), "off", 0)
	end
s9()	
end

function s8() -- 8->d
%flags[4] = 1 - %flags[4]
if %flags[4] == 0 then
	set_stone("st-oneway_black", 9,4,{orientation=EAST})
    else
	set_stone("st-oneway_black", 9,4,{orientation=WEST})
end
end

function s9() -- 9->l + 6
%switches[5] = 1 - %switches[5]
%flags[8] = 1 - %flags[8]
if %flags[8] == 0 then
	set_stone("st-oneway_black", 10,9,{orientation=SOUTH})
    else
	set_stone("st-oneway_black", 10,9,{orientation=NORTH})
end
if %switches[4] == 0 then enigma.SendMessage( enigma.GetNamedObject("s6s"), "on", 1)  -- Schalter umlegen.
	else enigma.SendMessage( enigma.GetNamedObject("s6s"), "off", 0)
	end
s6()	
end

function sA() -- A->h + 2
%flags[7] = 1 - %flags[7]
if %flags[7] == 0 then
	set_stone("st-oneway_black", 5,8,{orientation=EAST})
    else
	set_stone("st-oneway_black", 5,8,{orientation=WEST})
end
if %switches[2] == 0 then enigma.SendMessage( enigma.GetNamedObject("s2s"), "on", 1)  -- Schalter umlegen.
	else enigma.SendMessage( enigma.GetNamedObject("s2s"), "off", 0)
	end
s2()	
end

function sB() -- B->f + e + 1
%flags[5] = 1 - %flags[5]
%flags[6] = 1 - %flags[6]
if %flags[6] == 0 then
	set_stone("st-oneway_black", 5,2,{orientation=EAST})
    else
	set_stone("st-oneway_black", 5,2,{orientation=WEST})
end
if %flags[5] == 0 then
	set_stone("st-oneway_black", 7,6,{orientation=EAST})
    else
	set_stone("st-oneway_black", 7,6,{orientation=WEST})
end
if %switches[1] == 0 then enigma.SendMessage( enigma.GetNamedObject("s1s"), "on", 1)  -- Schalter umlegen.
	else enigma.SendMessage( enigma.GetNamedObject("s1s"), "off", 0)
	end
s1()
end

function sC() -- C->d
%flags[4] = 1 - %flags[4]
if %flags[4] == 0 then
	set_stone("st-oneway_black", 9,4,{orientation=EAST})
    else
	set_stone("st-oneway_black", 9,4,{orientation=WEST})
end
end

function sD() -- D->n
%flags[9] = 1 - %flags[9]
if %flags[9] == 0 then
	set_stone("st-oneway_black", 12,9,{orientation=SOUTH})
    else
	set_stone("st-oneway_black", 12,9,{orientation=NORTH})
end
end

function sE() -- E->a + b
%flags[1] = 1 - %flags[1]
%flags[2] = 1 - %flags[2]
if %flags[1] == 0 then
	set_stone("st-oneway_black", 10,7,{orientation=SOUTH})
    else
	set_stone("st-oneway_black", 10,7,{orientation=NORTH})
end
if %flags[2] == 0 then
	set_stone("st-oneway_black", 13,2,{orientation=EAST})
    else
	set_stone("st-oneway_black", 13,2,{orientation=WEST})
end
end

--function sT()
--%flags[10] = 1 - %flags[10]
--if %flags[10] == 0 then
--	set_stone("st-oneway_black", 10,3,{orientation=SOUTH})
--    else
--	set_stone("st-oneway_black", 10,3,{orientation=NORTH})
--end
--end





