@name Grenade Launcher
@inputs [Gren1 Gren2 Gren3 Gren4 Forcer]:wirelink
@outputs [GP Ents Ents2]:array Spacer Count Dir:vector Targ:entity
@persist [GP Ents Ents2]:array Spacer Count
@trigger 
@model models/props_junk/PopCan01a.mdl
runOnTick(1)
O=owner()
Fuse=2500 #Change this to adjust the grenade fuse
Force=entity():isConstrainedTo()

if(first()|dupefinished()){
    Forcer:entity():setMass(50)
    Ents=array(
        Gren1:entity(),
        Gren2:entity(),
        Gren3:entity(),
        Gren4:entity()
        #Forcer:entity()
    )
    Ents2=Ents:clone()

}
GP=array()
for(C=1,4){
    Spacer=5
    GP[5-C,vector]=Force:pos()+Force:forward()*20-Force:forward()*Spacer*C+Force:up()*10
    #holoCreate(5-C,GP[C,vector],vec(1,1,1)*0.2,ang(),vec(1,1,1)+64*C)
}   
if(first()){GP2=GP:clone() Ents2=Ents:clone()}
R=25
PAng=O:eyeAngles():yaw()-45
GP[5,vector]=O:shootPos()+vec(R*cos(PAng),R*sin(PAng),-5)
timer("fire",1000)
#if(clk("fire")){print("Bloop")}
if((O:weapon():type()=="weapon_physcannon")&clk("fire")){
    
    if(O:keyUse()&(Count<=4)){
        
        Count++
        print( _HUD_PRINTCENTER,"Firing Grenade: "+Count)
        
        Forcer["Velocity",number]=750 #how much kick is the grenade launcher going to have
        
        Ents2:remove(1)
        
        timer(toString(Count),Fuse) #Fuse timer, this tells the grenades when to blow up
        timer("drop"+Count,1000)
        
    }else{
        
        Forcer["Velocity",number]=0
        
    }
    if(Count>4){timer("reload",Fuse+500)}
    
    if(O:keyReload()|clk("reload")){
        
        Count=0
        print( _HUD_PRINTCENTER,"Reloading Grenades")
        
        Ents2=Ents:clone()
        
    }
}else{Forcer["Velocity",number]=0}

if(clk("drop"+Count)){
    print("bloop")
    for(C=1,4){
        if(Ents[C,entity]:isOnGround()&(Count==C)){print("stopping timer "+C) stoptimer(toString(Count))}   
        
    }
}

#Impact sensor
Range=100

Targ=noentity()
if(!Count){Grenade=O:pos() Range=1}
if(Count==1){Grenade=Gren1:entity():pos()}
if(Count==2){Grenade=Gren2:entity():pos()}
if(Count==3){Grenade=Gren3:entity():pos()}
if(Count==4){Grenade=Gren4:entity():pos()}
findInSphere(Grenade,Range)
findSortByDistance(Grenade)
findIncludeClass("player")
findIncludeClass("npc")
findExcludePlayer(O)

Targ=findResult(1)
if(Targ){
    if(Count==1){Gren1["Detonate",number]=1 stoptimer(toString(Count))}
    if(Count==2){Gren2["Detonate",number]=1 stoptimer(toString(Count))}
    if(Count==3){Gren3["Detonate",number]=1 stoptimer(toString(Count))}
    if(Count==4){Gren4["Detonate",number]=1 stoptimer(toString(Count))}
    timer("reset",20)
}



#This is the detonator, it is triggered by a fuse timer
for(C=1,4){
    if(clk(toString(C))){
        
        if(C==1){Gren1["Detonate",number]=1}    
        if(C==2){Gren2["Detonate",number]=1}
        if(C==3){Gren3["Detonate",number]=1}
        if(C==4){Gren4["Detonate",number]=1}
        timer("reset",20)
    }
    if(clk("reset")){
        if(C==1){Gren1["Detonate",number]=0}    
        if(C==2){Gren2["Detonate",number]=0}
        if(C==3){Gren3["Detonate",number]=0}
        if(C==4){Gren4["Detonate",number]=0}
    
    }
}

for(C=1,5){
    Speed=10
    E=Ents2[C,entity]
    XYZ=GP[C,vector]
    if(C==5){
        E=Forcer:entity()
        XYZ=O:shootPos()+O:right()*40+O:forward()*20
        
    }
    E:applyForce((Speed*(XYZ-E:pos())-E:vel())*E:mass()+O:vel())



## slerp swaps between two quaternions with the angles built in ##
## the value I goes between 0 and 1, swapping between the directional angle and the other ##
## AltAng is the angle that the grabber will rotate to when you hold Use ##
Dir = E:pos()-O:aimPos()
#Q = quat(Dir:toAngle())*qRotation(vec(0,0,1),180) #X
#Q = quat(Dir:toAngle())*qRotation(vec(0,1,0),-90) #Z
#Q = quat(Dir:toAngle())*qRotation(vec(0,0,1),90) #Y
#Q = quat(Dir:toAngle())*qRotation(vec(0,0,1),-90) #-Y
Q = quat(Dir:toAngle())*qRotation(vec(0,1,0),-90)
E:applyTorque((150*E:toLocal(rotationVector(Q/quat(E))+E:pos()) - 12*E:angVelVector())*E:inertia())

    
}
