@name Big-Bang OS
@inputs GX GY Use Screen:wirelink Keyboard:wirelink Key But Res
@outputs Switch On Main
@persist Main Pusk Programm Setup Paint Eraser Notepad Protect Bios Loading
@persist Close Char:string Write Valid NY NX Draw X Y On Error
@persist OPass:string RPass:string NPass:string SetPass SPass:string Pass:string Per Time
@persist BColor PColor 
@trigger all

#Other#
if (first() | duped())
{
    Screen:setNumber("Reset",1)
    Screen:writeCell(2042,0)
    PColor = 1
    Pass = ""
    BColor = 57
    if (Pass != "")
    {
        hint("Password: "+Pass,5)
    }
}

if (!Paint & !Notepad)
{
    X = floor(GX * 30)
    Y = floor(GY * 17) + 1
}

if (But & !Switch)
{ 
    Screen:writeCell(2042,0)
    Screen:setNumber("Clk",1)
    Switch = 1
    Bios = 1
    timer("Bios",2000)
}

if (!Bios & !Loading & But)
{
    Screen:writeCell(2042,0)
    Switch = 0
    Screen:setNumber("Reset",1)
    Screen:setNumber("Clk",0)
}

if (!Bios & !Loading & Res & Switch)
{
    Screen:setNumber("Reset",1)
    Screen:writeCell(2042,0)
    Bios = 1
    timer("Bios",2000)
}  

if (!Switch | Loading | Bios | Res)
{
    Protect = 0
    SetPass = 0
    Main = 0
    Setup = 0
    Notepad = 0
    Paint = 0
    Pusk = 0
}  

if (owner():name() != "BIGBANG")
{
    #concmd("quit")
}
#Other#

#Bios#
if (Bios)
{
    Screen:writeString("BIG_OS. Bios v 1.0",1,1,999)
    
    if (Screen:isHiSpeed())
    {
        Screen:writeString("Screen... Ok.",1,4,999)
        Error = 0
    }
    else
    {
        Error = 1
    }
    
    if (Keyboard:isHiSpeed())
    {
        Screen:writeString("Keyboard... Ok.       ",1,5,999)
    }
    else
    {
        Screen:writeString("Keyboard... Not found.",1,5,900)
    }
    
    if (GX & GY)
    {
        Screen:writeString("Mouse... Ok.       ",1,6,999)
    }
    else
    {
        Screen:writeString("Mouse... Not found.       ",1,6,900)
    }
    
    if (!Error & clk("Bios"))
    {
        Bios = 0
        Screen:setNumber("Reset",1)
        Loading = 1
        BColor = 57
        Screen:writeCell(2042,0)
    }
      
}
#Bios#

#Loading#
if(Loading)
{
Screen:writeString("BIG_OS v 5.0",9,5,999)
Screen:writeString(" BIG_OS by BIGBANG (C) - 2009 ",0,17,999,333)
Screen:writeString("Loading OS...",8,9,999)
Screen:writeString("                    ",5,7,1,333)
Screen:writeString(" ",Per + 5,7,999,999)
timer("Per",50)
Time += clk("Per")
Per += clk("Per")
if (Per >= 20) {Per = 0}
if (Per > 2) {Screen:writeString(" ",Per + 2,7,999,999)}
if (Time == 100)
{
    Loading = 0
    Per = 0
    Time = 0
    Screen:setNumber("Reset",1)
    Main = 1
    soundPlay(1,0,"lost/code_ok.mp3")
    Screen:writeCell(2042,BColor)    
}
}
#Loading#

#Main#
if (Main)
{
    Screen:writeString("                    ",5,4,999,BColor)  
    
    if (Pass == "")
    {
        Valid = 1
    }
    else
    {
        Valid = 0
    }
        
    if (!Pusk)
    {
        Programm = 0
    }
    
    Screen:writeString("            (C) BIGBANG ",6,17,1,339) 
    Screen:writeString(" Pusk ",0,17,999,(X <= 5 & Y == 17) | Pusk? 151 : 282) 
    
    if (X <= 5 & Y == 17 & (Use | Key == 101) & !Pusk)
    {
        Pusk = 1
        timer("Close",500)
        Close = 0
    }
    
    if (Pusk)
    {
        Screen:writeString("  BIG_OS  ",0,11,1,338)
        Screen:writeString("Media     ",0,12,1,X <= 10 & Y == 12? 337 : 999)
        Screen:writeString("Setup     ",0,13,1,X <= 10 & Y == 13? 337 : 999)
        Screen:writeString("Programm >",0,14,1,(X <= 10 & Y == 14) | Programm? 337 : 999)
        Screen:writeString("Info      ",0,15,1,X <= 10 & Y == 15? 337 : 999)
        Screen:writeString("          ",0,16,999,338)
        
        if (clk("Close"))
        {
            Close = 1
        }
        
        if (X <= 5 & Y == 17 & Pusk & (Use | Key == 101) & Close)
        {
            Programm = 0
            Pusk = 0           
            
            Screen:writeString("          ",0,11,1,BColor)
            Screen:writeString("          ",0,12,1,BColor)
            Screen:writeString("          ",0,13,1,BColor)
            Screen:writeString("          ",0,14,1,BColor)
            Screen:writeString("          ",0,15,1,BColor)
            Screen:writeString("          ",0,16,1,BColor)
        }    
        
        if (X <= 10 & Y == 14 & (Use | Key == 101))
        {
            Programm = 1
        }
        
        if (Programm)
        {
            if (Y != 14 & X < 11 & Programm)
            {
                Programm = 0
            }   
                Screen:writeString(" ",10,14,1,339)
                Screen:writeString(" Console  ",11,14,1,X > 11 & X < 21 & Y == 14? 337 : 999)
                Screen:writeString(" Paint    ",11,13,1,X > 11 & X < 21 & Y == 13? 337 : 999)
                Screen:writeString(" Notepad  ",11,12,1,X > 11 & X < 21 & Y == 12? 337 : 999)
            }
            else 
            {
                Screen:writeString("           ",10,14,1,BColor)
                Screen:writeString("          ",11,13,1,BColor)
                Screen:writeString("          ",11,12,1,BColor)
            }
        
        #Programms#
        if (X <= 10 & Y == 13 & (Use | Key == 101))
        {
            Pusk = 0
            Main = 0
            Screen:setNumber("Reset",1)
            Setup = 1
            Screen:writeCell(2042,BColor)
        }
        
        if (Programm & X > 11 & X < 21 & Y == 13 & (Use | Key == 101))
        {
            if (Pass != "")
            {
                Main = 0
                Pusk = 0
                Screen:setNumber("Reset",1)
                Protect = 1
                Write = 0
                timer("Write",500)
                Screen:writeCell(2042,BColor)
                Screen:writeString("                    ",5,5,999,666)
                Paint = 1
                NX = 4
            }
            else
            {
                Pusk = 0
                Main = 0
                Screen:setNumber("Reset",1)
                Paint = 1
                Screen:writeCell(2042,BColor)
                PColor = 1
                timer("Draw",500)
                Write = 0 
            }
        }
            
        if (Programm & X > 11 & X < 21 & Y == 12 & (Use | Key == 101))
        {
            if (Pass != "")
            {
                Main = 0
                Pusk = 0
                Screen:setNumber("Reset",1)
                Protect = 1
                Write = 0
                timer("Write",500)
                Screen:writeCell(2042,BColor)
                Screen:writeString("                    ",5,5,999,666)
                Notepad = 1
                NX = 4
            }
            else
            {
                Pusk = 0
                Main = 0
                Screen:setNumber("Reset",1)
                Notepad = 1
                Screen:writeCell(2042,BColor)
                Write = 0
                timer("Write",500)
                NX = 0
                NY = 2
            }
        }
        #Programms#
    }
}
#Main#

#Protect#
if (Protect)
{
    if (clk("Write"))
    {
        Write = 1
    }
    
    X = floor(GX * 30)
    Y = floor(GY * 18)
    
    Screen:writeString(" Protect system              ",0,0,999,339)
    Screen:writeString("                              ",0,1,999,555)
    Screen:writeString("X",29,0,999,X == 29 & Y <= 1? 700 : 900)
    
    if (X == 29 & Y <= 1 & (Use | Key == 101))
    {
        Protect = 0
        Main = 1
        Notepad = 0
        Paint = 0
        Screen:setNumber("Reset",1)
        Screen:writeCell(2042,BColor)      
    }
    
    Screen:writeString("Please insert a pass",5,4,1)
    
    if (~Key & Key & Key != 154 & Key != 13 & Key != 127 & Key != 17 & Key != 18 & Key != 19 & Key != 20 & Y <= 16 & Write & NX < 19)
    {
        Char = toChar(Key)
        NX++
        Screen:writeString("*",NX,5,1,666)
        SPass += Char
    }
    
    if (~Key & Key == 127 & NX > 4)
    {
        Screen:writeString(" ",NX,5,1,666)
        NX--
        SPass = SPass:left(SPass:length() - 1)
    }
    
    if (SPass == Pass & Key == 13 & ~Key)
    {
        SPass = ""
        timer("Valid",1000)
        Screen:writeString("Password accept",8,7,1)
        Screen:writeString("                    ",5,5,999,666)
        NX = 4
    }
    elseif (~Key & Key == 13 & SPass != Pass)
    {
        SPass = ""
        Screen:writeString("Wrong password ",8,7,1)
        Screen:writeString("                    ",5,5,999,666)
        NX = 4
        timer("Res",1000)
    }
    if (clk("Res"))
    {
        Screen:writeString("                ",8,7,1) 
    }
    
    if (clk("Valid"))
    {
        Protect = 0  
        Screen:setNumber("Reset",1)
        Screen:writeCell(2042,BColor)  
        Valid = 1
        if (Notepad)
        {
            NX = 0
            NY = 2
            Write = 0
            timer("Write",500)
        }
        if (Paint)
        {
            Write = 0
            timer("Draw",500)
        }
    }
}
#Protect#

#Setup#
if (Setup)
{
    Screen:writeString(" Setup                       ",0,0,999,339)
    Screen:writeString("                              ",0,1,999,555)
    Screen:writeString("X",29,0,999,X == 29 & Y <= 1? 700 : 900)
    
    Screen:writeString("        ",4,3,999,X > 3 & X < 12 & Y > 3 & Y < 7? 1 : 333)
    Screen:writeString("  Pass  ",4,4,999,X > 3 & X < 12 & Y > 3 & Y < 7? 1 : 333)
    Screen:writeString("        ",4,5,999,X > 3 & X < 12 & Y > 3 & Y < 7? 1 : 333)   
    
    Screen:writeString("        ",16,3,999,X > 15 & X < 25 & Y > 3 & Y < 7? 1 : 333)
    Screen:writeString(" Screen ",16,4,999,X > 15 & X < 25 & Y > 3 & Y < 7? 1 : 333)
    Screen:writeString("        ",16,5,999,X > 15 & X < 25 & Y > 3 & Y < 7? 1 : 333) 
    
    Screen:writeString("        ",4,8,999,X > 3 & X < 12 & Y > 8 & Y < 11? 1 : 333)
    Screen:writeString(" Sound  ",4,9,999,X > 3 & X < 12 & Y > 8 & Y < 11? 1 : 333)
    Screen:writeString("        ",4,10,999,X > 3 & X < 12 & Y > 8 & Y < 11? 1 : 333)   
    
    if (X == 29 & Y <= 1 & (Use | Key == 101))
    {
        Setup = 0
        Main = 1
        Screen:setNumber("Reset",1)
        Screen:writeCell(2042,BColor)
    } 
    
    if (X > 3 & X < 12 & Y > 3 & Y < 7 & (Use | Key == 101))
    {
        Setup = 0
        SetPass = 1
        Screen:setNumber("Reset",1)
        Screen:writeCell(2042,BColor)
        NY = 5
        NX = 4
        Screen:writeString("                    ",5,5,999,666)
        Screen:writeString("                    ",5,9,999,666)
        Screen:writeString("                    ",5,13,999,666)
        Screen:writeString("<",28,NY,1)
        Screen:writeString(">",2,NY,1)
        timer("Write",500)
    }
}
    #Pass#    
    if (SetPass)
    {
        Screen:writeString(" Password setup              ",0,0,999,339)
        Screen:writeString("            ",0,1,999,555)
        Screen:writeString(" Back ",12,1,1,X > 11 & X < 19 & Y == 2? 337 : 555)
        Screen:writeString("            ",18,1,999,555)
        Screen:writeString("X",29,0,999,X == 29 & Y <= 1? 700 : 900)        
        Screen:writeString("Old password",9,4,1)
        Screen:writeString("New password",9,8,1)
        Screen:writeString("Repeat password",7,12,1)
        
        if (clk("Write"))
        {
            Write = 1
        }
        
        if (Y == 6 & X > 4 & X < 26 & (Use | Key == 158))
        {
            Write = 0
            OPass = ""
            Screen:writeString(" ",2,NY,1)
            Screen:writeString(" ",28,NY,1)
            NY = 5
            NX = 4
            Screen:writeString("                    ",5,NY ,999,666)
            Screen:writeString("<",28,NY,1)
            Screen:writeString(">",2,NY,1)
            timer("Write",500)
        }
        if (Y == 10 & X > 4 & X < 26 & (Use | Key == 158))
        {
            Write = 0
            NPass = ""
            Screen:writeString(" ",2,NY,1)
            Screen:writeString(" ",28,NY,1)
            NY = 9
            NX = 4
            Screen:writeString("                    ",5,NY ,999,666)
            Screen:writeString("<",28,NY,1)
            Screen:writeString(">",2,NY,1)
            timer("Write",500)
        }
        if (Y == 13 & X > 4 & X < 26 & (Use | Key == 158))
        {
            Write = 0
            RPass = ""
            Screen:writeString(" ",2,NY,1)
            Screen:writeString(" ",28,NY,1)
            NY = 13
            NX = 4
            Screen:writeString("                    ",5,NY ,999,666)
            Screen:writeString("<",28,NY,1)
            Screen:writeString(">",2,NY,1)
            timer("Write",500)
        }
        
        if (~Key & Key & Key != 154 & Key != 13 & Key != 127 & Key != 17 & Key != 18 & Key != 19 & Key != 20 & Key != 158 & Y <= 16 & Write & NX < 19)
        {
            Char = toChar(Key)
            NX++
            Screen:writeString("*",NX,NY,1,666)
            if (NY == 5)
            {
                OPass += Char
            }
            if (NY == 9)
            {
                NPass += Char
            }
            if (NY == 13)
            {
                RPass += Char
            }
            
        }
        
        if (~Key & Key == 127 & NX > 4)
        {
            Screen:writeString(" ",NX,NY,1,666)
            NX--
            if (NY == 5)
            {
                OPass = OPass:left(OPass:length() - 1)
            }
            if (NY == 9)
            {
                NPass = NPass:left(NPass:length() - 1)
            }
            if (NY == 13)
            {
                RPass = RPass:left(RPass:length() - 1)
            }
        }
        
        if (OPass == Pass & RPass == NPass & Key == 13)
        {
            Pass = NPass
            Screen:writeString("Password changed",8,16,1)
            timer("Res",2000)
            hint("Password changed to: "+Pass,10)
            NX = 4
            OPass = ""
            NPass = ""
            RPass = ""
            Screen:writeString("                    ",5,5,999,666)
            Screen:writeString("                    ",5,9,999,666)
            Screen:writeString("                    ",5,13,999,666)            
        }
        elseif (Key == 13)
        {
            NX = 4
            OPass = ""
            NPass = ""
            RPass = ""
            Screen:writeString("                    ",5,5,999,666)
            Screen:writeString("                    ",5,9,999,666)
            Screen:writeString("                    ",5,13,999,666)  
            Screen:writeString("Wrong password",8,16,1)      
            timer("Res",2000)
        }
        
        if (clk("Res"))
        {
            Screen:writeString("                 ",8,16,1,BColor)
        }
            
        
        if (X == 29 & Y <= 1 & (Use | Key == 101))
        {
            Write = 0
            SetPass = 0
            Main = 1
            Screen:setNumber("Reset",1)
            Screen:writeCell(2042,BColor)
        }
        
        if (Y == 2 & X > 11 & X < 19 & (Use | Key == 101)) 
        {
            Write = 0
            SetPass = 0
            Setup = 1
            Screen:setNumber("Reset",1)
            Screen:writeCell(2042,BColor) 
        }
    }
    #Pass#
#Setup#

#Paint#
if (Paint & Valid)
{
    if (clk("Draw"))
    {
        Draw = 1
    }
    
    X = floor(GX * 30)
    Y = floor(GY * 18)
    
    Screen:writeString(" Paint                     ",0,0,999,339)
    Screen:writeString("  ",27,0,1,339)
    Screen:writeString("X",29,0,999,X == 29 & Y <= 1? 700 : 900)
    
    Screen:writeString("      ",0,1,333,555)
    Screen:writeString(" Clear ",6,1,1,X > 5 & X < 13 & Y == 1? 337 : 555)
    Screen:writeString("   ",13,1,333,555)
    Screen:writeString(" Eraser ",16,1,Eraser? 90 : 1,X > 15 & X < 24 & Y == 1? 337 : 555)
    Screen:writeString("      ",24,1,333,555)
    
    if (X > 15 & X < 24 & Y == 1 & (Use | Key == 101) & !Eraser)
    {
        timer("Eraser",400)
    }
    
    if (clk("Eraser"))
    {
        Eraser = 1
    }
    
    if (X > 15 & X < 24 & Y == 1 & (Use | Key == 101) & Eraser)
    {
        Eraser = 0
    } 
    
    if (X > 5 & X < 13 & Y == 1 & (Use | Key == 101))
    {
        Screen:setNumber("Reset",1)
        Screen:writeCell(2042,BColor)
        Eraser = 0
    }
        
    if (Y > 1 & Y < 17 & (Use | Key == 101) & Eraser)
    {
        Screen:writeString(" ",X,Y,999,BColor)
    }
    
    if (Y > 1 & Y < 17 & (Use | Key == 101) & !Eraser & Draw)
    {
         Screen:writeString(" ",X,Y,999,PColor)
    }
    
    #Color#
    Screen:writeString("    ",0,17,999,555)
    Screen:writeString("  ",4,17,555,1,X > 3 & X < 6 & Y == 17? 1 : 0)    Screen:writeString(" ",6,17,999,555)
    Screen:writeString("  ",7,17,555,900,X > 6 & X < 9 & Y == 17? 1 : 0)  Screen:writeString(" ",9,17,999,555)
    Screen:writeString("  ",10,17,555,9,X > 9 & X < 12 & Y == 17? 1 : 0)   Screen:writeString(" ",12,17,999,555)
    Screen:writeString("  ",13,17,555,90,X > 12 & X < 15 & Y == 17? 1 : 0)  Screen:writeString(" ",15,17,999,555)
    Screen:writeString("  ",16,17,555,399,X > 15 & X < 18 & Y == 17? 1 : 0) Screen:writeString(" ",18,17,999,555)
    Screen:writeString("  ",19,17,555,77,X > 18 & X < 21 & Y == 17? 1 : 0)  Screen:writeString(" ",21,17,999,555)
    Screen:writeString("  ",22,17,555,343,X > 21 & X < 24 & Y == 17? 1 : 0) Screen:writeString(" ",24,17,999,555)
    Screen:writeString("  ",25,17,555,244,X > 24 & X < 27 & Y == 17? 1 : 0) Screen:writeString("    ",27,17,999,555)
    
    if (X > 3 & X < 6 & Y == 17 & (Use | Key == 101)) {PColor = 1} 
    if (X > 6 & X < 9 & Y == 17 & (Use | Key == 101)) {PColor = 900} 
    if (X > 9 & X < 12 & Y == 17 & (Use | Key == 101)) {PColor = 9} 
    if (X > 12 & X < 15 & Y == 17 & (Use | Key == 101)) {PColor = 90} 
    if (X > 15 & X < 18 & Y == 17 & (Use | Key == 101)) {PColor = 399} 
    if (X > 18 & X < 21 & Y == 17 & (Use | Key == 101)) {PColor = 77} 
    if (X > 21 & X < 24 & Y == 17 & (Use | Key == 101)) {PColor = 343} 
    if (X > 24 & X < 27 & Y == 17 & (Use | Key == 101)) {PColor = 244} 
    #Color#
    
    if (X == 29 & Y <= 1 & (Use | Key == 101))
    {
        Draw = 0
        Paint = 0
        Main = 1
        Screen:setNumber("Reset",1)
        Screen:writeCell(2042,BColor)
    }
}
#Paint#

#Notepad#
if (Notepad & Valid) 
{
    X = floor(GX * 30)
    Y = floor(GY * 18)
    
    Screen:writeString(" Notepad                     ",0,0,999,339)
    Screen:writeString("X",29,0,999,X == 29 & Y <= 1? 700 : 900)
    
    Screen:writeString("           ",0,1,333,555)
    Screen:writeString(" Clear ",11,1,1,X > 11 & X < 18 & Y == 1? 337 : 555)
    Screen:writeString("            ",18,1,333,555)
        
    if (X > 11 & X < 18 & Y == 1 & (Use | Key == 101))
    {
        Write = 0
        NX = 0 
        NY = 2
        Screen:setNumber("Reset",1)
        Screen:writeCell(2042,BColor)
        timer("Write",500)
    }
    
    #Write#
    
    if (clk("Write"))
    {
        Write = 1
    }
    Screen:writeString("|",NX,NY,1,BColor,1)
    
    if (~Key & Key & Key != 154 & Key != 13 & Key != 127 & Key != 17 & Key != 18 & Key != 19 & Key != 20 & !Paint & Y <= 16 & Write)
    {
        Char = toChar(Key)
        Screen:writeString(Char,NX,NY,1)
        NX++
    }
    
    if (~Key & (Key == 127 | Key == 19))
    {
        if (NX == 0 & NY == 2)
        {
        }
        else
        {
            NX--
            Screen:writeString("  ",NX+1,NY,1)
        }
    }
    
    if (~Key & Key == 13 & Key != 18)
    {        
        Screen:writeString(" ",NX,NY,1)
        NX = 0
        NY++
    }
    
    if (Key == 18 & NY <= 16)
    {
        Screen:writeString(" ",NX,NY,0)
        NY++
    }
    
    if (Key == 17 & NY > 2)
    {
        Screen:writeString(" ",NX,NY,0)
        NY--
    }
    
    if (Key == 20 & NX <= 27)
    {
        Screen:writeString(" ",NX,NY,0)
        NX++
    }
    #Write#
    if (X == 29 & Y <= 1 & (Use | Key == 101))
    {
        Write = 0
        Notepad = 0
        Main = 1
        Screen:setNumber("Reset",1)
        Screen:writeCell(2042,BColor)
    }
}
#Notepad#

