@name !add v2
@inputs 
@outputs 
@persist HoloCount State Where Repeat
@trigger 
interval(100)
runOnChat(1)

if (first()) {
    holoCreate(0)
    holoModel(0,"models/Barney.mdl")
}

if (changed(owner():keyWalk()) & owner():keyWalk()) {
    HoloCount++
    holoCreate(HoloCount)
    holoPos(HoloCount,owner():aimPos())
}

if(chatClk(owner())) {
    local Said = owner():lastSaid():lower()
    
    if(Said:find("!go")) {
        hideChat(1)
        State = 1
    }
    
    if(Said:find("!stop")) {
        hideChat(1)
        State = 0
    }
    
    if(Said:find("!reset")) {
        hideChat(1)
        Where = 0
        State = 0
        holoPos(0, entity():pos())
    }
    
    if(Said:find("!clear")) {
        hideChat(1)
        Where = 0
        State = 0
        holoPos(0, entity():pos())
        for(I = 1, HoloCount) {holoDelete(I)}
    }
    
    if(Said:find("!repeat")) {
        hideChat(1)
        Repeat = (Repeat ? 0 : 1)
        printColor(vec(0, 150, 0), "Repeat: ", vec(0, 255, 0), Repeat)
    }
}

if(changed(State)) {
    holoAnim(0, (State == 1 ? 38 : 2))
}

if(State == 1) {
    local Pos = holoEntity(Where):pos()
    local MePos = holoEntity(0):pos()
    local MeAng = holoEntity(0):angles()
    if(MePos:distance(Pos) < 5) {
        Where++
        if(Where > HoloCount) {
            Where = 0
            if(Repeat) {
                State = 1
            } else {
                State = 0
            }
        }
    } else {
        holoPos(0, holoEntity(0):pos() + holoEntity(0):forward() * 6.5)
        holoAng(0, MeAng:setYaw((Pos - MePos):toAngle():yaw()))
    }
}
