@name Caber Dude
@inputs WalkSpace:entity Explosive:wirelink
@persist [MinPos MaxPos Pos]:vector Target:entity
@persist Anim:string Debounce Sounds:array
@persist RespawnState

if (first()) {
    Explosive:entity():setAlpha(0)
    holoCreate(1)
    holoModel(1,"models/Humans/Group03/male_07.mdl")
    #holoModel(1,"models/player/demo.mdl")
    Anim="ACT_IDLE_ANGRY_MELEE"
    holoAng(1,ang())
    
    holoCreate(2)
    holoModel(2,"models/weapons/c_models/c_caber/c_caber.mdl")
    holoScale(2,vec(0.7))
    holoPos(2,holoEntity(1):attachmentPos("anim_attachment_RH")+
        holoEntity(1):angles():forward()*-1.2+
        holoEntity(1):angles():up()*1+
        holoEntity(1):angles():right()*1
    )
    #holoMaterial(2,"models/shiny")
    #holoColor(2,vec(255,0,0))
    holoAng(2,holoEntity(1):attachmentAng("anim_attachment_RH")+ang(80,90,70))
    holoParentAttachment(2,holoEntity(1),"anim_attachment_RH")
    
    Sounds=array(
        "vo/npc/male01/answer01.wav", # that's you all over
        "vo/npc/male01/answer02.wav", # i won't hold it against ya
        "vo/npc/male01/answer03.wav", # figures
        "vo/npc/male01/answer04.wav", # try not to dwell on it
        "vo/npc/male01/answer10.wav", # i wouldn't say that too louuuddd
        "vo/npc/male01/answer13.wav", # i'm with you
        "vo/npc/male01/answer14.wav", # you and me both
        "vo/npc/male01/answer22.wav", # you never know
        "vo/npc/male01/answer25.wav", # how bout that
        "vo/npc/male01/answer32.wav", # right on
        "vo/npc/male01/answer33.wav", # no argument there
        "vo/npc/male01/answer34.wav", # don't forget hawaii
        "vo/npc/male01/answer35.wav", # try not to let it get to ya
        "vo/npc/male01/answer40.wav", # there's a first time for everything
        "vo/npc/male01/evenodds.wav", # let's even the odds a little
        "vo/npc/male01/fantastic01.wav", # fantastic
        "vo/npc/male01/fantastic02.wav", # fantastic
        "vo/npc/male01/nice.wav" # nice
    )
    
    ## BASE ANIMATIONS
    # ACT_IDLE
    # ACT_WALK
    # ACT_RUN
    
    ## MELEE ANIMATIONS
    # ACT_MELEE_ATTACK_SWING
    # ACT_IDLE_ANGRY_MELEE
    
    ## TRAIN ANIMATIONS
    # d1_t01_TrainRide_Sit_Idle
    # d1_t01_TrainRide_Stand
    # d1_t01_TrainRide_Sit_Exit
    # d1_t01_TrainRide_Stand_Exit
    
    ## SMG ANIMATIONS
    # ACT_RANGE_ATTACK_SMG1_LOW
    
    ## RIFLE ANIMATIONS
    
    # ACT_WALK_AIM_RIFLE
    local Base=WalkSpace:pos()
    local Offset=WalkSpace:aabbSize()/2
    local Min=Base-Offset
    local Max=Base+Offset
    MinPos=minVec(Min,Max)
    MaxPos=maxVec(Min,Max)
    Pos=Base
} elseif (!Debounce) {
    findInBox(MinPos,MaxPos)
    findIncludeClass("player")
    Player=findClosest(Pos)
    if (Player:isPlayer()) {
        local SoundID=randint(10)
        local Sound=Sounds[randint(Sounds:count()),string]
        holoEntity(1):soundPlay(SoundID,0,Sound)
        local Ang=(Player:pos()-Pos):toAngle()*ang(0,1,1)
        Pos=Pos+Ang:forward()*15
        if (Pos:distance(Player:pos())<40) {
            soundPurge()
            holoEntity(1):soundPlay(0,0,"vo/npc/male01/whoops01.wav")
            Debounce=1
            Target=Player
            holoAnim(1,"ACT_MELEE_ATTACK_SWING")
            holoEntity(1):soundPlay(3,0,"weapons/cbar_miss1_crit.wav")
            timer("Explode",500)
        }
        holoAng(1,Ang)
        Anim="ACT_RUN"
    } else {
        Anim="ACT_IDLE_ANGRY_MELEE"
    }
    
    Pos=clamp(Pos,MinPos,MaxPos)
    holoPos(1,Pos)
    
    if (changed(Anim)) {
        holoAnim(1,Anim)
    }
}

if (clk("Explode")) {
    holoEntity(2):soundPlay(3,0,"ambient/explosions/explode_4.wav")
    Explosive:entity():setPos(holoEntity(2):pos())
    Explosive["Detonate",number]=1
    Explosive:entity():setAlpha(0)
    #concmd("rp_arrest "+Target:name())
    holoAlpha(1,0)
    holoAlpha(2,0)
    timer("Respawn",5000)
    Debounce=1
}

if (clk("Respawn")) {
    Explosive["Detonate",number]=0
    Explosive:entity():setAlpha(0)
    WalkSpace:soundPlay(1,0,"vo/halloween_boss/knight_spawn.wav")
    holoEntity(1):soundPlay(2,0,"vo/npc/male01/holddownspot01.wav")
    holoAlpha(1,255)
    holoAlpha(2,255)
    RespawnState=60
    holoPos(1,WalkSpace:pos()-vec(0,0,RespawnState))
    timer("RespawnUp",10)
}

if (clk("RespawnUp")) {
    if (RespawnState>0) {
        RespawnState-=2
        holoPos(1,WalkSpace:pos()-vec(0,0,RespawnState))
        timer("RespawnUp",100)
    } else {
        Anim="ACT_IDLE_ANGRY_MELEE"
        holoAnim(1,Anim)
        Pos=WalkSpace:pos()
        Debounce=0
    }
}
interval(100)
