@name Explosion on Demand
@inputs [Small Medium Large]:wirelink
@outputs Size Enemy:entity Attack EntityAttack Type:string AF
@persist Size Attack EntityAttack Type:string Enemy:entity
@trigger 
@model models/props_junk/PopCan01a.mdl

#Written by Captain Maim, STEAM_0:1:8017844
runOnChat(1)
#runOnTick(1)
interval(50)
O=owner()
E=entity()
Det="Detonate"

DetRange=50 #how close till it goes boom
#[
SR=50 #small charge blast range
MR=200 #medium charge blast range
LR=512 #Large charge blast range
]#

Com="zz"
S="s" #set to Small charge
M="m" #set to medium charge
L="l" #set to large charge
T="t" #triple charge
Ent="e" #detonate on entity glance
Norm="c" #detonate on chat only
Prop="r" #detonate when looking at props
Npc="n" #detonate when looking at NPCS
Play="p" #detonate when looking at players
Fire="f" #Detonate charge once
AutoFire="a" #constantly detonate once it reaches target
Help="h" #print command list
if(duped()|dupefinished()){reset()}
if(first()){
    Size=1 #set default charge size
    print("Type "+Com+Help+" to display help for E.O.D. weaponry.")
} 

Chat=O:lastSaid()
S0=Chat:find(S)
M0=Chat:find(M)
L0=Chat:find(L)
T0=Chat:find(T)
ENT=Chat:find(Ent)
NORM=Chat:find(Norm)
PROP=Chat:find(Prop)
NPC=Chat:find(Npc)
PLAY=Chat:find(Play)
FIRE=Chat:find(Fire)
COM=Chat:find(Com)
AUTOFIRE=Chat:find(AutoFire)
HELP=Chat:find(Help)

if(chatClk()&COM){
    if(S0){Size=1 print("Small charges set")}
    if(M0){Size=2 print("Medium charges set")}
    if(L0){Size=3 print("Large charges set")}
    if(T0){Size=4 print("Triple Charges Set!")}
    if(ENT){EntityAttack=1 print("detonating when you look at entities")}
    if(NORM){EntityAttack=0 print("only detonating on chat")}
    if(PROP){EntityAttack=2 Type="prop" print("Detonating when you look at props")}
    if(NPC){EntityAttack=3 Type="npc_" print("Detonating when you look at npcs")}
    if(PLAY){EntityAttack=3 Type="player" print("Detonating when you look at players")}
    if(FIRE){Attack=1}
    if(HELP){
        print("Explosion On Demand Command list:")
        print("type "+Com+" in chat plus any of the below functions to use.")
        print("add "+S+" to set explsion to small.")
        print("add "+M+" to set explosion to medium.")
        print("add "+L+" to set explosion to large.")
        print("add "+T+" to set explsion to triple, all 3 explosives are used.")
        print("add "+Ent+" to attack any entity you look at.")
        print("add "+Norm+" to attack only on chat command.")
        print("add "+Prop+" to attack any props you look at.")
        print("add "+Npc+" to attack any NPC's you look at.")
        print("add "+Play+" to attack any player you look at.")
        print("add "+Fire+" to cause a single explosion where you're looking.")
        print("add "+AutoFire+" to constantly attack the object you next look at.")
        print("add "+Help+" to repeat this message.")
    }
}
if(EntityAttack>1){TYPE=O:aimEntity():type():find(Type)}
if(EntityAttack==1){TYPE=O:aimEntity():id()}
if(EntityAttack==3&O:aimEntity()&TYPE){
    Enemy=O:aimEntity()
    #if(changed(Enemy)){print("Attacking "+Enemy:name()+" till next death.")}
}
if(!Enemy:isAlive()){Enemy=noentity()}

if((EntityAttack&TYPE|Enemy)|Attack){Attack=1}else{Attack=0}

if(COM&AUTOFIRE){
    AF=1
}else{AF=0}
Count=0
Mat="Models/effects/vol_light001"
if(Small:entity()){Count++ Small:entity():setAlpha(10) Small:entity():setMaterial(Mat)}
if(Medium:entity()){Count++ Medium:entity():setAlpha(10) Medium:entity():setMaterial(Mat)}
if(Large:entity()){Count++ Large:entity():setAlpha(10) Large:entity():setMaterial(Mat)}

for(C=1,Count){
    if(C==1){Bomb=Small:entity()}
    if(C==2){Bomb=Medium:entity()}
    if(C==3){Bomb=Large:entity()}
    if(Attack&((C==Size)|(Size==4))){
    
        if(Enemy){
            XYZ=Enemy:pos()+vec(0,0,Enemy:height()/2)
            
        }else{XYZ=O:aimPos()}
        
        if(XYZ:distance(Bomb:pos())<DetRange){ #if in range, detonate approprate charge
            if(Size==1|Size==4){Small[Det,number]=1} 
            if(Size==2|Size==4){Medium[Det,number]=1}
            if(Size==3|Size==4){Large[Det,number]=1}
        
            if(!AF){Attack=0}else{Attack=1}
        }
    }else{XYZ=O:pos()+vec(0,0,50+O:height())} #rest position
    Speed=100*(Bomb:mass()/3)
    Bomb:applyForce(Speed*(XYZ-Bomb:pos())-Bomb:vel()*Bomb:mass())
    #Arrest rotation
    InertiaConstant = 27.05307927383
    AngDist=ang()-Bomb:angles()
    Inertia = InertiaConstant*ang(Bomb:inertia():y(),Bomb:inertia():z(),Bomb:inertia():x())
    AngVel = AngDist/0.015
    Bomb:applyAngForce(Inertia*(AngVel-Bomb:angVel()))
}
