@name teddiursa_load
@inputs 
@outputs [Body Head Player Temp]:entity [AIMode AIStatus]:string 
@outputs [ROffset RDown BodyPos BodyVel TargPos Target BGround]:vector 
@persist [WHeight RHeight BMoving Distanc MVCheck Init RoamT Hostile FPath] BRanger:ranger
@trigger none

if (first())
{
    #include "2015_october/teddiursa_data"
    
    Entity  = entity()
    Factor  = 1
    Shading = 1
    MatTog  = 1

}
elseif (HC_Mode == "load")  
{
    loadHolo(Factor, MatTog, Shading, Entity)
}
elseif (HC_Mode == "pause")
{
    HC_Mode = "run"
    
    Body = holoEntity(1), holoUnparent(1)
    Head = holoEntity(3)

    #-
    AIMode = "Init"

    WHeight = 7.5*Factor
    RHeight = 35*Factor
    RDown = vec(0,0,-1)
    ROffset = vec(0,0,WHeight)
    Target = Body:pos() + randvec(-200,200):setZ(0)
    
    rangerFilter(players())
    rangerPersist(1)
    
    interval(500)

}
elseif (HC_Mode == "run")
{
    if (clk())
    {
        interval(90)

        BodyPos = Body:pos()
        BodyVel = Body:toLocal($BodyPos*66.566669 + BodyPos)
        BRanger = rangerOffset(RHeight, BodyPos + ROffset, RDown)
        BGround = BRanger:position()
    
        TargPos = Target - BodyPos
        Distanc = TargPos:setZ(0):length()
        MVCheck = Distanc >= 100 ? min(Distanc*0.1, 5 + Hostile*2) : 0
        BMoving = MVCheck > 0 ? BMoving + (BodyVel[1] / 7) : 0
    
        #-AI
        if (AIMode == "Init" & Init < 1) 
        { 
            Init += 0.05
            if (Init == 1)  
            {
                AIMode = "active"
                Target = Body:pos() + randvec(-200,200):setZ(0)
                Player = noentity()
    
                holoPos(1, Entity:pos() + vec(0,0,WHeight))
            }
        } 
        elseif (AIMode == "active") 
        {
            #-BOGIES & AI SWITCHING
            if (!Player & findCanQuery()) 
            {
                findInSphere(BodyPos, 250)
                findSortByDistance(BodyPos)
                findClipToClass("Player")
    
                Player = find()
            }
            PDCheck = (Player:pos() - BodyPos):setZ(0):length() <= 200
            GDCheck = BRanger:distance() >= RHeight-(5*Factor)
    
            if (PDCheck & !GDCheck)           { AIStatus = "stalk" } else { Player = noentity() }
            if (!PDCheck & !GDCheck)          { AIStatus = "roam", Hostile = 0 }
            if (changed(GDCheck) & GDCheck)   
            { 
                AIStatus = "float"
                FPath = random(1,2) == 1 ? 4 : 8
            }
            if (changed(Player) & Player)     { Hostile = random(100) > 50 ? 1 : 0 }
            if (changed(Player) & !Player)    { Hostile = 0 }
            if (changed(GDCheck) & !GDCheck)  
            { 
                Target = Body:pos() + randvec(-200,200):setZ(0)
            }
    
            #-FLOAT AI
            if (AIStatus == "float")    
            {
                Target = BodyPos + vec(sin(BMoving/8)*500, cos(BMoving/FPath)*500, -350)
            }
    
            #-STALK AI
            if (AIStatus == "stalk")    
            {
                Target = Player:shootPos()
            }
    
            #ROAM AI
            if (AIStatus == "roam")   
            {
                RoamT++
                if (RoamT > 45 & changed(RoamT) & BodyVel[1] < 1) 
                {
                    Target = BodyPos + vec(random(-200,200), random(-200,200), random(-20,20))
                    RoamT = 0
                }
            }
            
            #-HOLOGRAMS
            Rate = BMoving * 1.5
            Brt  = sin(curtime()*100)*!MVCheck*0.1
            Cos  = cos(Rate)*(MVCheck != 0)
            Sin  = sin(Rate)*(MVCheck != 0)
        
            holoPos(1, BodyPos:setZ(BGround[3] + WHeight + Brt*Factor) + Body:forward()*MVCheck*Factor)
            holoAng(1, slerp(quat(Body), quat(TargPos:setZ(0):toAngle()), 0.5*(MVCheck>0)):toAngle())
            holoAng(3, TargPos:toAngle())

            holoPos(8, Body:toWorld(vec(-Sin, 2, -6.25 - Cos/2 - Brt)*Factor))
            holoPos(9, Body:toWorld(vec(Sin, -2, -6.25 + Cos/2 - Brt)*Factor))
            holoAng(6, Body:toWorld(ang(Sin*25, 0, AIStatus == "float" ? 30 : -50)))
            holoAng(7, Body:toWorld(ang(-Sin*25, 0, AIStatus == "float" ? -30 : 50)))
            holoAng(2, Body:toWorld(ang(0, Cos*35, 0)))
        }
    }    
}

