@name tr_test_game3
@inputs 
@outputs TempRotate TempYaw
@persist [CurTime DeltaTime] Players:table Lines:table
@trigger none

if (first()) {
    
    #--------------------------------------------------
    #-- Delta time function
    
    function dt( Interval ) {
        interval(Interval)
        
        DeltaTime = curtime() - CurTime
        CurTime   = curtime()    
    }

    CurTime = curtime()


    #--------------------------------------------------
    #-- Check if line segments intersect
    
    function number lDir(PointA:vector, PointB:vector, PointC:vector) {
        return sign((PointB[1] - PointA[1])*(PointC[2] - PointA[2]) - (PointC[1] - PointA[1])*(PointB[2] - PointA[2]))
    }
    
    function number lineSegmentIntersection([LineAP1 LineAP2]:vector, [LineBP1 LineBP2]:vector) {
        return lDir(LineAP1, LineAP2, LineBP1) != lDir(LineAP1, LineAP2, LineBP2) & lDir(LineBP1, LineBP2, LineAP1) != lDir(LineBP1, LineBP2, LineAP2)
    }

    function array newLine( [PointA PointB]:vector ) {
        return array( PointA, PointB, (PointA - PointB):normalized():rotate(0, 90, 0) )
    }

    #--------------------------------------------------
    #-- Create new player table
    
    function table addPlayer( Player:entity ) {
        local SteamID = Player:steamID()
        local Hash = hash(SteamID)
        
        Players[SteamID, table] = table(
            "pEnt"  = Player,
            "pHash" = Hash,
            "pPos"  = entity():pos() + randvec(-50,50):setZ(0),
            "pDir"  = vec(1, 0, 0),
            "pYaw"  = 0,
            "pVel"  = 50,
           
            "debug"  = Hash + 1,
            
            "tempdel" = random(0.5, 1),
            "temptim" = curtime()
        )   
        
        Players[SteamID, table]["pNewLine", vector] = Players[SteamID, table]["pPos", vector]
        
        holoCreate(Hash)
        holoColor(Hash, hsv2rgb(360/Players:count(), 1, 1))
        holoAlpha(Hash, 100)
              
        Players[SteamID, table]["color", vector] = hsv2rgb(360/Players:count(), 1, 1)
        
        return Players[SteamID, table]
    }
    
    
    #--------------------------------------------------
    #-- Set player direction
    
    function table:setPlayerDir( AddDir ) {
        This["pYaw", number] = This["pYaw", number] + sign(AddDir)*90
        This["pDir", vector] = vec(1, 0, 0):rotate(0, This["pYaw", number], 0)
     
        #- debug
        This["debug", number] = This["debug", number] + 1
        holoCreate(This["debug", number], This["pPos", vector], vec(1/3))
        holoColor(This["debug", number], This["color", vector])
        holoAlpha(This["debug", number], 100)
        
        This["tempdel", number] = random(1, 2)
        This["temptim", number] = curtime()
    }

    
    #--------------------------------------------------
    #-- Push currrent player line
    
    function table:pushPlayerLine() {
        
    }


    #--------------------------------------------------
    #-- Update player table
    function table:updatePlayer() {

    }
    

    
    
    #--------------------------------------------------
    #-- Test
    
    addPlayer( owner() )

}

dt(90)

TempRotate = owner():keyPressed("up") - owner():keyPressed("down")
if (changed(TempRotate)) { TempYaw = TempYaw + TempRotate*90 }

for (I = 1, Lines:count()) {
    local LineP1 = Lines[I, array][1, vector]  
    local LineP2 = Lines[I, array][2, vector]   
    local LineN  = Lines[I, array][3, vector]
    
    foreach (K, Player:table = Players) {
        if (!Player["isDead", number]) {
            local NewPos = Player["pPos", vector] + Player["pDir", vector]*Player["pVel", number]*DeltaTime
            if (inrange(NewPos, LineP1, LineP2 + LineN*Player["pVel", number]*DeltaTime)) {
                print("wtf")
                Player["isDead", number] = 1
                continue    
            }
        }
    }
}

foreach (K, Player:table = Players) {
    if (!Player["isDead", number]) {
        if (Player["pYaw", number] != TempYaw) {
            Player["pDir", vector] = vec(1, 0, 0):rotate(0, TempYaw, 0)
            Player["pYaw", number] = TempYaw
    
            local CurrentLineStart  = Player["pNewLine", vector]
            local CurrentLineFinish = Player["pPos", vector]
    
            Player["debug", number] = Player["debug", number] + 1
            holoCreate(Player["debug", number], CurrentLineStart, vec(1/6))
            holoColor(Player["debug", number], vec4(255, 0, 0, 125))
    
            Player["debug", number] = Player["debug", number] + 1
            holoCreate(Player["debug", number], CurrentLineFinish, vec(1/3))
            holoColor(Player["debug", number], vec4(255, 255, 0, 125))     
            
            Player["pNewLine", vector] = Player["pPos", vector]
            
            Lines:pushArray( newLine( CurrentLineStart, CurrentLineFinish ) )   
        }
    
        Player["pPos", vector] = Player["pPos", vector] + Player["pDir", vector]*Player["pVel", number]*DeltaTime
        holoPos(Player["pHash", number], Player["pPos", vector])
    }
}
#[


if (inrange(Pos + Dir*Speed, Test[1, vector], Test[2, vector] - Test[3, vector]*Speed)) {
    #Pos = Pos + Dir*Speed
    #holoPos(3, Pos + Dir*(Speed/2) ) 
}
else {
    Pos = Pos + Dir*Speed
    holoPos(3, Pos)    
}

foreach (K, Player:table = Players) {
    
    if (Player["pYaw", number] != TempYaw) {
        Player["pDir", vector] = vec(1, 0, 0):rotate(0, TempYaw, 0)
        Player["pYaw", number] = TempYaw
        
        
        local CurrentLineStart  = Player["pNewLine", vector]
        local CurrentLineFinish = Player["pPos", vector]

        Player["debug", number] = Player["debug", number] + 1
        holoCreate(Player["debug", number], CurrentLineStart, vec(1/6))
        holoColor(Player["debug", number], vec4(255, 0, 0, 125))

        Player["debug", number] = Player["debug", number] + 1
        holoCreate(Player["debug", number], CurrentLineFinish, vec(1/3))
        holoColor(Player["debug", number], vec4(255, 255, 0, 125))
        
        Player["pNewLine", vector] = Player["pPos", vector] #+ Player["pDir", vector]*5
        
        Lines:pushArray( array(CurrentLineStart, CurrentLineFinish) )

    }

    local PrevPos = Player["pPos", vector]
    local NextPos = PrevPos + Player["pDir", vector]*Player["pVel", number]*DeltaTime
    
    holoPos(Player["pHash", number]-1, NextPos)
    holoAng(Player["pHash", number]-1, Player["pDir", vector]:toAngle())
    holoScaleUnits(Player["pHash", number]-1, vec(Player["pVel", number]*DeltaTime,1,1))
    
    local Check = 0
    for (I = 1, Lines:count()) {
        local Line2A = Lines[I, array][1, vector]    
        local Line2B = Lines[I, array][2, vector]
        
        if (lineSegmentIntersection(PrevPos, NextPos, Line2A, Line2B)) {
            Check++
            print("hit")
            break
        }
    }
    
    if (!Check) {
        Player["pPos", vector] = NextPos
        holoPos(Player["pHash", number], Player["pPos", vector])
    }

}
]#
