@name NPC Control V4 <By MHGR>
@inputs 
@outputs OwnerAimPos:vector NPC:entity Latching Latched Following 
@persist NPC:entity HoloAng 
@trigger 
interval(200)

###Owner Properties###
Owner = owner()
        
        #Positioning
        OwnerPos = owner():pos()                                                    ##Owner's position
        OwnerAimPos = owner():aimPos()                                              ##The position of where the owner is aiming
        OwnerAimEnt = owner():aimEntity()                                           ##The entity that the owner is aiming at
        
        #Chat
        OwnerCommand = owner():lastSaid()                                           ##The last thing the Owner has said in chat
        OwnerArrayCommand = owner():lastSaid():explode(" ")                         ##For things like attacking players by name

###NPC Properties###
        #Positioning
        NPCPos = NPC:pos()                                                          ##NPC's Position
        #Targetting
        Target = entity()                                                           ##Used for attacking by naming
        
###NPC Status###
        Latching = 0                                                                ##Am I in the process of latching on to an NPC?
        Latched = 0                                                                 ##Am I latched to an NPC?**This will be checked every command
        Attacking = 0                                                               ##Is the NPC attacking?
        Moving2Target = 0                                                           ##Has the NPC been ordered to move to a position?                                                         ##Is the NPC following its owner?

###NPC Movement###
        MMarker = vec()                                                             ##This is used to mark where the NPC should move
        NPCDirMMarker = NPCPos - MMarker                                            ##Line from NPC to Movement Marker 
        NPCDirOwner = NPCPos - OwnerPos                                             ##Line from NPC to Owner's positon                       
        
###Holographics###
        HoloAng = HoloAng + 20                                                      ##For rotation of holographic indicators
        HoloColor = vec(0, 0, 0)                                                    ##This is to help indicate the mood of the NPC


#################Let's get to business!#################




################################
####Mood;- I can't get this working right now, but I will later.
################################
##This will change the color of the hologram over the NPC's head according to its mood
HoloColor = vec(150, 255, 150)




################################
####Latching;- The process that this E2 chip uses to designate the NPC that it will be controlling.
################################
#The command itself
        runOnChat(1)
        if (OwnerCommand == "-latch") {
            Latching = 1
            if (OwnerAimEnt:isNPC() & Latched == 0) {
            #Creating Holographic Indicator##
                holoCreate(0)
                holoModel(0, "icosphere")
                holoPos(0, (OwnerAimEnt:pos() + vec(0,0,80)))
                holoAng(0, ang(0, HoloAng, HoloAng))
                holoScale(0, vec(1, 1, 1))
                holoColor(0, vec(200,255,255))
                holoAlpha(0, 150)
                holoRenderFX(0, 2)
            #If <Use> is pressed then latch on to that NPC
                if (owner():keyUse())    {
                    NPC = OwnerAimEnt
                    Latching = 0
                    NPC:npcRelationship("player","neutral",900)                    ##Make the newly latched NPC neutral to all players
                    NPC:npcRelationship("owner","like",999)                        ##Make the newly latched NPC like its owner
                    #sNPC:npcRelationship("npc","neutral",900)                       ##Make the newly latched NPC neutral to all other NPCs
                }
            }
                else {holoDelete(0)}
        }       else {holoDelete(0)}

#Update the "Latched" variable
if (!NPC) {Latched = 0} else {Latched = 1 Latching = 0}


#Make a holo-indicator over latched NPC
if (NPC) {
    holoCreate(1)
    holoPos(1, (NPC:pos() + vec(0,0,80)))
    holoScale(1, vec(0.5, 0.5, 0.5))
    holoColor(1, HoloColor)
    holoModel(1, "icosphere")
    holoAng(1, ang(0, HoloAng, HoloAng))
    holoRenderFX(1, 2)
} else {holoDelete(1)}


                       
            
################################
####Movement;- Pretty self-explanatory I guess.
################################ 
                                                        
interval(200) 
if (Latched) {                                                                     ##Make sure that we have an NPC in the first place       

###FOLLOWING
if (OwnerCommand == "-follow") {
    if (NPCDirOwner:length() > 350) {NPC:npcGoRun(OwnerPos)} 
    if (NPCDirOwner:length() <= 350) {NPC:npcGoWalk(OwnerPos)} 
    if (NPCDirOwner:length() < 150) {NPC:npcStop()}
}

###TARGETED MOVING                                                                 ##This here is my attempt at making an NPC move where I aim
if (OwnerCommand == "-go") {
    MMarker = OwnerAimPos
    #Move to Movement Marker
    if (NPCDirMMarker:length() > 200) {NPC:npcGoRun(MMarker)} 
    if (NPCDirMMarker:length() <= 200) {NPC:npcGoWalk(MMarker)} 
    if (NPCDirMMarker:length() < 100) {NPC:npcStop()}
    #Make holo-indicator
    holoCreate(2)
    holoPos(2, (MMarker + vec(0,0,30)))
    holoModel(2, "hqcone")
    holoColor(2, vec(150, 150, 255))
    holoAng(2, ang(0, HoloAng, 180))
} else {holoDelete(2)}
}

###STOP                                                                            ##Stops the NPC in its tracks

if (OwnerCommand == "-stop") {
    NPC:npcStop()
}




################################
####Attacking;- Pretty self-explanatory I guess.
################################     

interval(200)

##ATTACKING BY AIM
if ((OwnerCommand == "-attack") & (OwnerAimEnt != NPC)) {
    NPC:npcRelationship(OwnerAimEnt, "hate", 1000)                              ##This makes the NPC percieve the target as an enemy, so it
    if (OwnerAimEnt:isAlive()){                                                 ##automatically attacks it.
    #Make holo-indicator                                                        
    holoCreate(3)
    holoPos(3, (OwnerAimEnt:pos() + vec(0,0,90)))
    holoModel(3, "pyramid")
    holoAng(3, ang(0,HoloAng,180))
    holoColor(3, vec(255, 150, 150))
    HoloColor = vec(255, 150, 150)
} else {holoDelete(3)} 
} else {holoDelete(3)}

##ATTACKING BY NAME                                                             ##Type "-attack: <player>" to have the NPC seek out a 
if (OwnerArrayCommand[1,string]=="-attack:")                                    ##specific player
{
    Target=findPlayerByName(OwnerArrayCommand[2,string])
    NPC:npcRelationship(Target,"hate",1000)
    HoloColor = vec(255, 150, 150)
}

if (OwnerCommand=="-forgive")                                                   ##To call off an attack from a player, type "-forgive"
{       
    if (Target == owner()) {NPC:npcRelationship(Target,"like",999)}
    if (Target != owner()) {NPC:npcRelationship(Target,"neutral",900)}
    HoloColor = vec(150, 255, 150)
}

if (!Target:isAlive()) {Attacking = 0}

##WEAPON EQUIPPING                                                              ##Some weapons are omitted because either NPCs don't use them
if (OwnerCommand == "-equip crowbar") {NPC:npcGiveWeapon("crowbar")}            ##or I don't know what the weapon is called in the code
if (OwnerCommand == "-equip pistol") {NPC:npcGiveWeapon("pistol")}
if (OwnerCommand == "-equip smg1") {NPC:npcGiveWeapon("smg1")}
if (OwnerCommand == "-equip ar2") {NPC:npcGiveWeapon("ar2")}
if (OwnerCommand == "-equip shotgun") {NPC:npcGiveWeapon("shotgun")}
if (OwnerCommand == "-equip rpg") {NPC:npcGiveWeapon("rpg")}
if (OwnerCommand == "-equip stunstick") {NPC:npcGiveWeapon("stunstick")}



    

if (first()) {
print("How to use:")
print("type '-latch' then press your <USE> key on an NPC that you would like to assume control of.")
print("####COMMANDS####")
print("-follow | -go | -stop")
print("-attack | -attack: <player> | -forgive")
print("Made by MHGR")
}s