levelw=80
levelh=20

create_world( levelw, levelh)

fill_floor("fl-leaves", 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-glass", i-1, line)
		elseif c == "o" then
			oxyd( i-1, line)
                elseif c == "!" then
			abyss(i-1,line)
--			fill_floor("fl-water", i-1,line, 1,1)
		elseif c=="z" then
			set_actor("ac-blackball", i-.5,line+.5, {player=0})
		elseif c=="+" then
			set_stone( "st-wood", i-1, line)
		elseif c=="`" then
			mirrorp(i-1,line,FALSE,TRUE, 2)
		elseif c=="/" then
			mirrorp(i-1,line,FALSE,TRUE, 4)
		elseif c=="5" then	
			set_item("it-coin", i-1,line,{value=1})
		elseif c=="4" then	
			set_item("it-coin", i-1,line,{value=3})
		elseif c=="P" then
			set_item("it-brush",i-1,line)
		elseif c=="a" then
			set_stone("st-switch", i-1,line, {action="callback",target="fa"})
		elseif c=="b" then
			set_stone("st-switch", i-1,line, {action="callback",target="fb"})
		elseif c=="c" then
			set_stone("st-switch", i-1,line, {action="callback",target="fc"})
		elseif c=="d" then
			set_stone("st-switch", i-1,line, {action="callback",target="fd"})
		elseif c=="e" then
			set_stone("st-switch", i-1,line, {action="callback",target="fe"})

		elseif c=="f" then
			set_stone("st-switch", i-1,line, {action="callback",target="ff"})
		elseif c=="g" then
			set_stone("st-switch", i-1,line, {action="callback",target="fg"})
		elseif c=="h" then
			set_stone("st-switch", i-1,line, {action="callback",target="fh"})
		elseif c=="i" then
			set_stone("st-switch", i-1,line, {action="callback",target="fi"})
		elseif c=="j" then
			set_stone("st-switch", i-1,line, {action="callback",target="fj"})
		elseif c=="k" then
			set_stone("st-switch", i-1,line, {action="callback",target="fk"})
		elseif c=="l" then
			set_stone("st-switch", i-1,line, {action="callback",target="fl"})
		elseif c=="m" then
			set_stone("st-switch", i-1,line, {action="callback",target="fm"})
		elseif c=="n" then
			set_stone("st-switch", i-1,line, {action="callback",target="fn"})
		elseif c=="O" then
			set_stone("st-switch", i-1,line, {action="callback",target="fo"})
		elseif c=="p" then
			set_stone("st-switch", i-1,line, {action="callback",target="fp"})
		elseif c=="q" then
			set_stone("st-switch", i-1,line, {action="callback",target="fq"})
		elseif c=="r" then
			set_stone("st-switch", i-1,line, {action="callback",target="fr"})
		elseif c=="s" then
			set_stone("st-switch", i-1,line, {action="callback",target="fs"})
		elseif c=="t" then
			set_stone("st-switch", i-1,line, {action="callback",target="ft"})
		elseif c=="u" then
			set_stone("st-switch", i-1,line, {action="callback",target="fu"})
		elseif c=="v" then
			set_stone("st-switch", i-1,line, {action="callback",target="fv"})
		elseif c=="w" then
			set_stone("st-switch", i-1,line, {action="callback",target="fw"})
		elseif c=="x" then
			set_stone("st-switch", i-1,line, {action="callback",target="fx"})
		elseif c=="y" then
			set_stone("st-switch", i-1,line, {action="callback",target="fy"})
		elseif c=="Z" then
			set_stone("st-switch", i-1,line, {action="callback",target="fz"})
		elseif c=="A" then
			doorh( i-1,line, {name="door1"})
		elseif c=="B" then
			doorh( i-1,line, {name="door2"})
		elseif c=="C" then
			doorh( i-1,line, {name="door3"})
		elseif c == "L" then
			abyss(i-1,line)
 			draw_stones("st-glass", {i-1,line},{1,1}, 1)
			set_item("it-magicwand",i-1,line)
		elseif c == "G" then
			fill_floor("fl-leaves", i-1,line,1,1)
			draw_stones("st-glass", {i-1,line},{1,0}, 1)
			set_item("it-magicwand",i-1,line)
		elseif c == "Y" then
			fill_floor("fl-sand", i-1,line,1,1)
			draw_stones("st-glass", {i-1,line},{1,0}, 1)
			set_item("it-magicwand",i-1,line)
		elseif c == "K" then
			fill_floor("fl-bluegreen", i-1,line,1,1)
			draw_stones("st-glass", {i-1,line},{1,0}, 1)
			set_item("it-magicwand",i-1,line)
		elseif c == "W" then
			fill_floor("fl-inverse", i-1,line, 1,1)
			draw_stones("st-glass", {i-1,line},{1,0}, 1)
			set_item("it-magicwand",i-1,line)
		elseif c=="I" then
			fill_floor("fl-ice_001", i-1,line, 1,1)
			draw_stones("st-glass", {i-1,line},{1,0}, 1)
			set_item("it-magicwand",i-1,line)
		elseif c=="T" then
			fill_floor("fl-marble", i-1,line, 1,1)
			draw_stones("st-glass", {i-1,line},{1,0}, 1)
			set_item("it-magicwand",i-1,line)
		elseif c=="R" then
			fill_floor("fl-wood", i-1,line, 1,1)
			draw_stones("st-glass", {i-1,line},{1,0}, 1)
			set_item("it-magicwand",i-1,line)
		elseif c=="S" then
			fill_floor("fl-space", i-1,line, 1,1)
		end
	end	
end

-- Floor:  " "
-- Border: "#"
-- Oxyd:   "o"

renderLine(00,"#######################################")
renderLine(01,"#                   #o               o#")
renderLine(02,"#    edcba          #########C#########")
renderLine(03,"#                   #########B#########")
renderLine(04,"#    jihgf          #########A#########")
renderLine(05,"#                                     #")
renderLine(06,"#    Onmlk  z                     D   #")
renderLine(07,"#                            ILT      #")
renderLine(08,"#    tsrqp                   SSK      #")
renderLine(09,"#                            GRY      #")
renderLine(10,"#    yxwvu                            #")
renderLine(11,"#                                    Z#")
renderLine(12,"#######################################")

oxyd_shuffle()

--document(34,6,"What do you want the first gate to do?")
--document(29,4,"Opposite")
--document(29,3,"If ten equals three, five equals four, two equals three, four equals four, and thirteen equals eight, what does nine equal?")
--document(29,2,"Congratulations!")

num1=0
through=0
function fa()
    if through==0 then
	num1=num1+1
	through=1
    getletter()
    elseif through==1 then
	num1=num1-1
	through=0
    getletter()
    end
end

throughb=0
function fb()
    if throughb==0 then
	num1=num1+2
	throughb=1
    getletter()
    elseif throughb==1 then
	num1=num1-2
	throughb=0
    getletter()
    end
end

throughc=0
function fc()
    if throughc==0 then
	num1=num1+4
	throughc=1
    getletter()
    elseif throughc==1 then
	num1=num1-4
	throughc=0
    getletter()
    end
end

throughd=0
function fd()
    if throughd==0 then
	num1=num1+8
	throughd=1
    getletter()
    elseif throughd==1 then
	num1=num1-8
	throughd=0
    getletter()
    end
end

throughe=0
function fe()
    if throughe==0 then
	num1=num1+16
	throughe=1
    getletter()
    elseif throughe==1 then
	num1=num1-16
	throughe=0
    getletter()
    end
end

num2=0
throughf=0
function ff()
    if throughf==0 then
	num2=num2+1
	throughf=1
    getletter()
    elseif throughf==1 then
	num2=num2-1
	throughf=0
    getletter()
    end
end

throughg=0
function fg()
    if throughg==0 then
	num2=num2+2
	throughg=1
    getletter()
    elseif throughg==1 then
	num2=num2-2
	throughg=0
    getletter()
    end
end

throughh=0
function fh()
    if throughh==0 then
	num2=num2+4
	throughh=1
    getletter()
    elseif throughh==1 then
	num2=num2-4
	throughh=0
    getletter()
    end
end

throughi=0
function fi()
    if throughi==0 then
	num2=num2+8
	throughi=1
    getletter()
    elseif throughi==1 then
	num2=num2-8
	throughi=0
    getletter()
    end
end

throughj=0
function fj()
    if throughj==0 then
	num2=num2+16
	throughj=1
    getletter()
    elseif throughj==1 then
	num2=num2-16
	throughj=0
    getletter()
    end
end

num3=0
throughk=0
function fk()
    if throughk==0 then
	num3=num3+1
	throughk=1
    getletter()
    elseif throughk==1 then
	num3=num3-1
	throughk=0
    getletter()
    end
end

throughl=0
function fl()
    if throughl==0 then
	num3=num3+2
	throughl=1
    getletter()
    elseif throughl==1 then
	num3=num3-2
	throughl=0
    getletter()
    end
end

throughm=0
function fm()
    if throughm==0 then
	num3=num3+4
	throughm=1
    getletter()
    elseif throughm==1 then
	num3=num3-4
	throughm=0
    getletter()
    end
end

throughn=0
function fn()
    if throughn==0 then
	num3=num3+8
	throughn=1
    getletter()
    elseif throughn==1 then
	num3=num3-8
	throughn=0
    getletter()
    end
end

througho=0
function fo()
    if througho==0 then
	num3=num3+16
	througho=1
    getletter()
    elseif througho==1 then
	num3=num3-16
	througho=0
    getletter()
    end
end

num4=0
throughp=0
function fp()
    if throughp==0 then
	num4=num4+1
	throughp=1
    getletter()
    elseif throughp==1 then
	num4=num4-1
	throughp=0
    getletter()
    end
end

throughq=0
function fq()
    if throughq==0 then
	num4=num4+2
	throughq=1
    getletter()
    elseif throughq==1 then
	num4=num4-2
	throughq=0
    getletter()
    end
end

throughr=0
function fr()
    if throughr==0 then
	num4=num4+4
	throughr=1
    getletter()
    elseif throughr==1 then
	num4=num4-4
	throughr=0
    getletter()
    end
end

throughs=0
function fs()
    if throughs==0 then
	num4=num4+8
	throughs=1
    getletter()
    elseif throughs==1 then
	num4=num4-8
	throughs=0
    getletter()
    end
end

throught=0
function ft()
    if throught==0 then
	num4=num4+16
	throught=1
    getletter()
    elseif throught==1 then
	num4=num4-16
	throught=0
    getletter()
    end
end

num5=0
throughu=0
function fu()
    if throughu==0 then
	num5=num5+1
	throughu=1
    getletter()
    elseif throughu==1 then
	num5=num5-1
	throughu=0
    getletter()
    end
end

throughv=0
function fv()
    if throughv==0 then
	num5=num5+2
	throughv=1
    getletter()
    elseif throughv==1 then
	num5=num5-2
	throughv=0
    getletter()
    end
end

throughw=0
function fw()
    if throughw==0 then
	num5=num5+4
	throughw=1
    getletter()
    elseif throughw==1 then
	num5=num5-4
	throughw=0
    getletter()
    end
end

throughx=0
function fx()
    if throughx==0 then
	num5=num5+8
	throughx=1
    getletter()
    elseif throughx==1 then
	num5=num5-8
	throughx=0
    getletter()
    end
end

throughy=0
function fy()
    if throughy==0 then
	num5=num5+16
	throughy=1
    getletter()
    elseif throughy==1 then
	num5=num5-16
	throughy=0
    getletter()
    end
end

door1=enigma.GetNamedObject("door1")
door2=enigma.GetNamedObject("door2")
door3=enigma.GetNamedObject("door3")

firstdooropen=0
seconddooropen=0
thirddooropen=0
fdo=0
sdo=0
tdo=0

function fz()
    document(34,6,"What do you want the first gate to do?")
    document(29,4,"Opposite")
    document(29,3,"If ten equals three, five equals four, two equals three, four equals four, and thirteen equals eight, what does nine equal?")
    document(29,2,"Congratulations!")
	if fdo==0 and sdo==0 and tdo==0 then	
	    if num1==15 and num2==16 and num3==5 and num4==14 and num5==29 and fdo==0 then
	    	enigma.SendMessage(door1, "open", nil)
	    	firstdooropen=1
	    	fdo=1
	    end
        end
	if fdo==1 and sdo==0 and tdo==0 then
	    if num1==3 and num2==12 and num3==15 and num4==19 and num5==5 and sdo==0 and fdo==1 then
		enigma.SendMessage(door2, "open", nil)
		seconddooropen=1
		firstdooropen=0
		sdo=1
	    end
	end
	if fdo==1 and sdo==1 and tdo==0 then
	    if num1==6 and num2==15 and num3==21 and num4==18 and num5==28 and tdo==0 and fdo==1 and sdo==1 then
	    	enigma.SendMessage(door3, "open", nil)
		seconddooropen=0
		thirddooropen=1
		tdo=1
	    end
	end
end

function getletter()
    if num1==1 then
	let1="A"
    end
    if num1==2 then
	let1="B"
    end
    if num1==3 then
	let1="C"
    end
    if num1==4 then
	let1="D"
    end
    if num1==5 then
	let1="E"
    end
    if num1==6 then
	let1="F"
    end
    if num1==7 then
	let1="G"
    end
    if num1==8 then
	let1="H"
    end
    if num1==9 then
	let1="I"
    end
    if num1==10 then
	let1="J"
    end
    if num1==11 then
	let1="K"
    end
    if num1==12 then
	let1="L"
    end
    if num1==13 then
	let1="M"
    end
    if num1==14 then
	let1="N"
    end
    if num1==15 then
	let1="O"
    end
    if num1==16 then
	let1="P"
    end
    if num1==17 then
	let1="Q"
    end
    if num1==18 then
	let1="R"
    end
    if num1==19 then
	let1="S"
    end
    if num1==20 then
	let1="T"
    end
    if num1==21 then
	let1="U"
    end
    if num1==22 then
	let1="V"
    end
    if num1==23 then
	let1="W"
    end
    if num1==24 then
	let1="X"
    end
    if num1==25 then
	let1="Y"
    end
    if num1==26 then
	let1="Z"
    end
    if num1==27 then
	let1=" "
    end
    if num1==28 then
	let1="."
    end
    if num1==29 then
	let1="!"
    end
    if num1==30 then
	let1="?"
    end
    if num1==31 then
	let1=","
    end
    if num2==1 then
	let2="A"
    end
    if num2==2 then
	let2="B"
    end
    if num2==3 then
	let2="C"
    end
    if num2==4 then
	let2="D"
    end
    if num2==5 then
	let2="E"
    end
    if num2==6 then
	let2="F"
    end
    if num2==7 then
	let2="G"
    end
    if num2==8 then
	let2="H"
    end
    if num2==9 then
	let2="I"
    end
    if num2==10 then
	let2="J"
    end
    if num2==11 then
	let2="K"
    end
    if num2==12 then
	let2="L"
    end
    if num2==13 then
	let2="M"
    end
    if num2==14 then
	let2="N"
    end
    if num2==15 then
	let2="O"
    end
    if num2==16 then
	let2="P"
    end
    if num2==17 then
	let2="Q"
    end
    if num2==18 then
	let2="R"
    end
    if num2==19 then
	let2="S"
    end
    if num2==20 then
	let2="T"
    end
    if num2==21 then
	let2="U"
    end
    if num2==22 then
	let2="V"
    end
    if num2==23 then
	let2="W"
    end
    if num2==24 then
	let2="X"
    end
    if num2==25 then
	let2="Y"
    end
    if num2==26 then
	let2="Z"
    end
    if num2==27 then
	let2=" "
    end
    if num2==28 then
	let2="."
    end
    if num2==29 then
	let2="!"
    end
    if num2==30 then
	let2="?"
    end
    if num2==31 then
	let2=","
    end
    if num3==1 then
	let3="A"
    end
    if num3==2 then
	let3="B"
    end
    if num3==3 then
	let3="C"
    end
    if num3==4 then
	let3="D"
    end
    if num3==5 then
	let3="E"
    end
    if num3==6 then
	let3="F"
    end
    if num3==7 then
	let3="G"
    end
    if num3==8 then
	let3="H"
    end
    if num3==9 then
	let3="I"
    end
    if num3==10 then
	let3="J"
    end
    if num3==11 then
	let3="K"
    end
    if num3==12 then
	let3="L"
    end
    if num3==13 then
	let3="M"
    end
    if num3==14 then
	let3="N"
    end
    if num3==15 then
	let3="O"
    end
    if num3==16 then
	let3="P"
    end
    if num3==17 then
	let3="Q"
    end
    if num3==18 then
	let3="R"
    end
    if num3==19 then
	let3="S"
    end
    if num3==20 then
	let3="T"
    end
    if num3==21 then
	let3="U"
    end
    if num3==22 then
	let3="V"
    end
    if num3==23 then
	let3="W"
    end
    if num3==24 then
	let3="X"
    end
    if num3==25 then
	let3="Y"
    end
    if num3==26 then
	let3="Z"
    end
    if num3==27 then
	let3=" "
    end
    if num3==28 then
	let3="."
    end
    if num3==29 then
	let3="!"
    end
    if num3==30 then
	let3="?"
    end
    if num3==31 then
	let3=","
    end
    if num4==1 then
	let4="A"
    end
    if num4==2 then
	let4="B"
    end
    if num4==3 then
	let4="C"
    end
    if num4==4 then
	let4="D"
    end
    if num4==5 then
	let4="E"
    end
    if num4==6 then
	let4="F"
    end
    if num4==7 then
	let4="G"
    end
    if num4==8 then
	let4="H"
    end
    if num4==9 then
	let4="I"
    end
    if num4==10 then
	let4="J"
    end
    if num4==11 then
	let4="K"
    end
    if num4==12 then
	let4="L"
    end
    if num4==13 then
	let4="M"
    end
    if num4==14 then
	let4="N"
    end
    if num4==15 then
	let4="O"
    end
    if num4==16 then
	let4="P"
    end
    if num4==17 then
	let4="Q"
    end
    if num4==18 then
	let4="R"
    end
    if num4==19 then
	let4="S"
    end
    if num4==20 then
	let4="T"
    end
    if num4==21 then
	let4="U"
    end
    if num4==22 then
	let4="V"
    end
    if num4==23 then
	let4="W"
    end
    if num4==24 then
	let4="X"
    end
    if num4==25 then
	let4="Y"
    end
    if num4==26 then
	let4="Z"
    end
    if num4==27 then
	let4=" "
    end
    if num4==28 then
	let4="."
    end
    if num4==29 then
	let4="!"
    end
    if num4==30 then
	let4="?"
    end
    if num4==31 then
	let4=","
    end
    if num5==1 then
	let5="A"
    end
    if num5==2 then
	let5="B"
    end
    if num5==3 then
	let5="C"
    end
    if num5==4 then
	let5="D"
    end
    if num5==5 then
	let5="E"
    end
    if num5==6 then
	let5="F"
    end
    if num5==7 then
	let5="G"
    end
    if num5==8 then
	let5="H"
    end
    if num5==9 then
	let5="I"
    end
    if num5==10 then
	let5="J"
    end
    if num5==11 then
	let5="K"
    end
    if num5==12 then
	let5="L"
    end
    if num5==13 then
	let5="M"
    end
    if num5==14 then
	let5="N"
    end
    if num5==15 then
	let5="O"
    end
    if num5==16 then
	let5="P"
    end
    if num5==17 then
	let5="Q"
    end
    if num5==18 then
	let5="R"
    end
    if num5==19 then
	let5="S"
    end
    if num5==20 then
	let5="T"
    end
    if num5==21 then
	let5="U"
    end
    if num5==22 then
	let5="V"
    end
    if num5==23 then
	let5="W"
    end
    if num5==24 then
	let5="X"
    end
    if num5==25 then
	let5="Y"
    end
    if num5==26 then
	let5="Z"
    end
    if num5==27 then
	let5=" "
    end
    if num5==28 then
	let5="."
    end
    if num5==29 then
	let5="!"
    end
    if num5==30 then
	let5="?"
    end
    if num5==31 then
	let5=","
    end
    if num1>0 and num2>0 and num3>0 and num4>0 and num5>0 then
	document(30,8,let1..let2..let3..let4..let5)
    end
end
