@name Grenadier
@persist [O Grenade]:entity Attached

if (first()){
    runOnTick(1)
    O=owner()
    function number entity:move(Vec:vector) {
        local V = Vec-This:pos()
        local V = V * V:length()
        This:applyForce((V-This:vel())*This:mass())
        return 1
    }
}

findIncludePlayerProps(O)
findByClass("npc_grenade_frag")
findSortByDistance(O:pos())
P=players()

if(find():pos():distance(O:pos()) < 200) {Grenade = find()}

Grenade:setMass(5)

if(changed(Grenade)){ 
    holoCreate(1, Grenade:pos())
    holoModel(1, "icosphere3")
    holoParent(1, Grenade)
    holoMaterial(1, "models/wireframe")
    holoColor(1, vec(random(255),random(255),random(255)))  
    holoScale(1, vec(1,1,1))
    O:soundPlay(10,10,"weapons/airstrike_fire_02.wav")
    Grenade:setTrails(100,102,5,"trails/physbeam",vec(random(255),random(255),random(255)),255) 
}

if (!Attached & Grenade:isValid()){
    for(I=1,P:count()){
        if(Grenade:pos():distance(P[I,entity]:pos()+vec(0,0,40)) < 50 & P[I,entity]!=O & P[I,entity]:isAlive()){
            holoParent(1, Grenade)
            Grenade:parentTo(P[I,entity])
            Attached = 1
        }    
    }
    Grenade:move(O:aimPos())
} elseif (!Grenade:isValid()) {Attached = 0}
