@name Based Leif and the Leif Effect
@outputs P:array
@persist [Wl]:array
@persist [E R S]:entity
@persist Dr I Mx St RT DT AT
if(first())
{
    E = entity()
    
    runOnTick(1)
    runOnChat(1)
    runOnLast(1)
    
    propSpawnUndo(1)
    
    Mx = 15
    
    Wl = array("STEAM_0:0:32759932", "STEAM_0:0:38933156", "STEAM_0:0:33723746", "STEAM_0:0:12760944", "STEAM_0:0:62947775")
    
    function wallDoor()
    {
        foreach(I, W:entity = P)
        {
            if(I==Dr)
            {
                W:setColor(255, 150, 150, 255)
            }else
            {
                W:setColor(255, 255, 255, 255)
            }
        }
    }
    
    function number wall(I)
    {
        if(!inrange(I, 1, Mx) | P[I, entity]:isValid() | !propCanCreate())
        {
            return 0
        }
        
        local N = (360/Mx)
        local TE = propSpawn("models/props_canal/locks_large.mdl", E:pos()+vec(cos(N*I)*56.25*Mx, sin(N*I)*56.25*Mx, 0), ang(0, 7+(N*I), 0), 1)
        TE:setMaterial("models/props_c17/metalladder003")
        TE:setMass(50000)
        P:pushEntity(TE)
        return 1
    }
    
    Dr = 1
    St = 2
	
    function number entity:validate()
    {
        if(!This:isPlayer())
        {
            return 0
        }
    
        local ID = This:steamID()
        foreach(I, S:string = Wl)
        {
            if(ID==S)
            {
                return 1
            }
        }
        
        return 0
    }
}

if(I<Mx)
{
    while(I < Mx & propCanCreate())
    {
        I++
        wall(I)
    }
    timer("tick", 500)
}else
{
    if(changed(I < Mx))
    {
        wallDoor()
    }
    
    if(clk("tick"))
    {
        print(4, toString(ops()))
        timer("tick", 500)
        
        local N = (360/Mx)
        foreach(I, W:entity = P)
        {
            local Ang = ang(0, 7+(N*I), 0)
            if(!W:isFrozen())
            {
                print(I)
                W:propFreeze(1)
                W:setPos(E:pos()+vec(cos(N*I)*56.25*Mx, sin(N*I)*56.25*Mx, 0))
                W:setAng(ang(0, 7+(N*I), 0))
            }
        }
        
        for(I = 1, Mx)
        {
            wall(I)
            wallDoor()
        }
        
        if(!R:isValid() & Mx==15)
        {
            R = propSpawn("models/hunter/plates/plate32x32.mdl", E:pos()+vec(0, 0, 450), 1)
            R:setColor(255, 255, 255, 100)
            R:setMaterial("phoenix_storms/mrref2")
        }
        
        if(!S:isValid() & Mx >= 15)
        {
            S = propSpawn("models/hunter/plates/plate4x32.mdl", P[2, entity]:toWorld(vec(-775, 0, 200)), P[2, entity]:toWorld(ang(0, 90, -25)), 1)
            S:setMaterial("phoenix_storms/mrref2")
        }
    }
    
    if(chatClk())
    {
        if(lastSpoke():validate())
        {
            local Ex = lastSpoke():lastSaid():lower():explode(" ")
            switch(Ex[1, string])
            {
                case "t", #Toggle
                    if(lastSpoke()==owner())
                    {
                        hideChat(1)
                    }
                    switch(Ex[2, string])
                				{
                    				case "d", #Door
                    				    DT = !DT
                            
                            local D = P[Dr, entity]
                            local N = (360/Mx)
                            D:setPos(E:pos()+vec(cos(N*Dr)*56.25*Mx, sin(N*Dr)*56.25*Mx, 300*DT))
                            wallDoor()
                    				break
                    					
                    				case "r", #Roof
                        			 RT = !RT
                            R:propNotSolid(RT)
                            R:setColor(255, 255, 255, !RT*100)
                    				break
                    					
                    				case "a", #Duno
                    			     AT = !AT
                    				break
                				}
                break
                
                case "d", #Change door
                    if(lastSpoke()==owner())
                    {
                        hideChat(1)
                    }
                    DT = 0
                    local D = P[Dr, entity]
                    local N = (360/Mx)
                    D:setPos(E:pos()+vec(cos(N*Dr)*56.25*Mx, sin(N*Dr)*56.25*Mx, 300*DT))
                    wallDoor()
                    
                    Dr = clamp(Ex[2, string]:toNumber(), 1, Mx)
                    
                    local D = P[Dr, entity]
                    local N = (360/Mx)
                    D:setPos(E:pos()+vec(cos(N*Dr)*56.25*Mx, sin(N*Dr)*56.25*Mx, 300*DT))
                    wallDoor()
                break
                
                case "s", #Change stairs
                    if(lastSpoke()==owner())
                    {
                        hideChat(1)
                    }
                    St = clamp(Ex[2, string]:toNumber(), 1, Mx)
                    
                    S:setPos(P[St, entity]:toWorld(vec(-775, 0, 200)))
                    S:setAng(P[St, entity]:toWorld(ang(0, 90, -25)))
                break
                
                case "a", #Add player
                    if(lastSpoke()==owner())
                    {
                        hideChat(1)
                        local TE = findPlayerByName(Ex[2, string])
                        if(!TE:validate())
                        {
                            Wl:pushString(TE:steamID())
                            hint("Pushing "+TE:name(), 5)
                            print(Wl)
                        }else
                        {
                            hint("Error.", 3)
                        }
                    }
                break
                
                case "r", #Remove player
                    if(lastSpoke()==owner())
                    {
                        hideChat(1)
                        local TE = findPlayerByName(Ex[2, string])
                        if(TE:validate())
                        {
                            Wl:remove(I)
                            hint("Removing "+TE:name(), 5)
                            print(Wl)
                        }else
                        {
                            hint("Error.", 3)
                        }
                    }
                break
                
                case "c", #Clear
                    if(lastSpoke()==owner())
                    {
                        hideChat(1)
                        selfDestruct()
                    }
                break
            }
        }
    }
}

if(last())
{
    R:propDelete()
    S:propDelete()
    P:propDelete()
}
