@name Pet Headcrab 
@persist [Headcrab Target]:entity Name:string Age
##-Start-## 
if(first()){
    ##-Start-##
    runOnTick(1)
    runOnChat(1)
    
    ##-Starting Age-##
    Age = 1
    
    ##-Sounds-##
    soundPlay(1,0,"ambient/machines/lab_loop1.wav")
    soundVolume(1,0.5)
    timer("Sound",1000)
}

##-Search for Head Crab-##
if(!Headcrab:isNPC()){
    print(_HUD_PRINTCENTER,"Searching For Headcrab...")
    findByClass("npc_headcrab")
    findIncludePlayerProps(owner())
    Headcrab = findClosest(entity():pos())
}

##-Get it to follow me-##
elseif(Headcrab:isNPC() & !Target:isAlive()){
    Headcrab:npcGoRun(owner():pos() + (Headcrab:pos() - owner():pos()):normalized()*70)
}

##-Headcrab Stuff-##
if(changed(Headcrab) & Headcrab:isNPC()){
    ##-Relationship-##
    Headcrab:npcRelationship(owner(),"like",999)
    Headcrab:npcRelationship("player","neutral",998)
    
    ##-Aesthetics-##
    entity():setColor(vec(0,255,255))
    print(_HUD_PRINTCENTER,"Headcrab Found!")
    soundPlay(1,0,"buttons/bell1.wav")
    printColor(vec(50,50,50),"[",vec(0,150,0),"Scruff Tech",vec(50,50,50),"]",vec(255,0,0),": Headcrab Found!",vec(255,255,255)," Please type a name for it in chat now by starting the message with",vec(255,255,0)," /name\n",vec(30,30,30),"This E2 can support multi-word names!\n",vec(80,80,80),"Once it is named it cannot be renamed (Without refreshing the E2 or removing the current one then spawning another)")
}
if(changed(Headcrab) & !Headcrab:isNPC()){
    ##-Entity Colour-##
    entity():setColor(vec(255,255,255))
    
    ##-Sounds-##
    soundPlay(1,0,"ambient/machines/lab_loop1.wav")
    soundVolume(1,0.5)
}

##-Naming-##
if(Name == ""){
    LS = owner():lastSaid():explode(" ")
    if(Headcrab:isNPC() & chatClk(owner()) & LS[1,string] == "/name"){
        ##-Name Setting-##
        hideChat(1)
        Name = owner():lastSaid():replace("/name ","")
        printColor(vec(50,50,50),"[",vec(0,150,0),"Scruff Tech",vec(50,50,50),"]",vec(255,255,255),": You have named your headcrab ",vec(0,255,255),Name,vec(255,255,255),"\nIt is now "+Age+" "+(Age == 1 ? "year" : "years")+" old and its age will increase in age every 30 seconds")
        owner():soundPlay(1,0,"garrysmod/content_downloaded.wav")
        
        ##-Headcrab Ageing Color-##
        Col = clamp(Age,1,105)
        Headcrab:setColor(vec(256-Col,256-Col,256-Col))  
              
        ##-Ageing-##
        timer("Age",30000)
    }
}

##-Ageing-##
if(clk("Age")){
    Age++
    Col = clamp(Age,1,105)
    Headcrab:setColor(vec(256-Col,256-Col,256-Col))
    hint("Its "+Name+"'s Birthday, he is now "+Age+" years old",5)
    owner():soundPlay(2,0,"garrysmod/save_load"+randint(1,4)+".wav")
    soundVolume(2,0.6)
    timer("Age",30000)
}

##-Name Display-##
if(owner():aimEntity() == Headcrab & Name != ""){
    print(_HUD_PRINTCENTER,"Name: "+Name+"\nAge: "+Age)
}

##-Random Cute Sounds-##
if(clk("Sound")){
    Sounds = array("npc/headcrab/idle"+randint(1,3)+".wav","npc/headcrab/alert1.wav","npc/headcrab_poison/ph_idle"+randint(1,3)+".wav")
    Headcrab:soundPlay(3,0,Sounds[randint(1,Sounds:count()),string])
    timer("Sound",randint(1000,7000))
}

##-Dead-##
if(changed(Headcrab:isAlive()) & !Headcrab:isAlive() & !first()){
    printColor(vec(50,50,50),"[",vec(0,150,0),"Scruff Tech",vec(50,50,50),"]",vec(255,255,255),": Your headcrab has died, how could you let that happen?")
    stoptimer("Age")
    Headcrab = noentity()
    Target = noentity()
    Name = ""
    Age = 1
}

##-Personal Defence-##
if(Name != ""){
if(owner():aimEntity():isNPC() & owner():aimEntity() != Headcrab | owner():aimEntity():isPlayer()){
    if(changed(owner():keyUse()) & owner():keyUse()){
        Target = owner():aimEntity()
        if(Target:isNPC()){
            Target:npcRelationship(Headcrab,"like",999)
        }
    }
}
if(Target:isAlive()){
    print(_HUD_PRINTCENTER,(Target:isPlayer() ? "Name: "+Target:name()+"\n" : "Targets ")+"HP: "+Target:health())
    Headcrab:npcSetTarget(Target)
    Headcrab:npcRelationship(Target,"hate",997)

    if(changed(Target:isAlive())){
        Headcrab:npcAttack()
    }
}
elseif(changed(Target) & !Target:isAlive()){
    Headcrab:npcSetTarget(owner())
    hint(Name+" has killed the target",5)
}    
}


