@name XBOW
@inputs Fire
@persist AllowFire CloseRad Rad Skew Fier2 Bend
@persist [E Base]:entity
@persist Ang:angle
@model models/hunter/blocks/cube025x025x025.mdl
if(first())
{
    #Bolt Holo Model = models/crossbow_bolt.mdl
    #Bolt Prop Model = models/hunter/plates/plate05.mdl
    runOnTick(1)
    runOnChat(1)
    Rad = 35
    Bend = 15
    holoCreate(1)
    holoModel(1,"models/weapons/w_crossbow.mdl")
    holoPos(1,entity():toWorld(vec(0,0,7.5)))
    holoParent(1,entity())
    Base = holoEntity(1)
}

Fire2 = owner():keyReload()

CloseRad = owner():pos():distance(entity():pos())<100

if(CloseRad)
{
    Ang = (owner():aimPos()-holoEntity(1):pos()):toAngle()
}

holoAng(1,Ang)

AllowFire = !E:isValid()

if(CloseRad)
{
    if(AllowFire)
    {
        if(changed(Fire2)&Fire2)
        {
            E = propSpawn("models/hunter/plates/plate05.mdl",Base:toWorld(vec(30,0,3)),Base:angles()+ang(0,90,0),0)
            #E:propGravity(0)
            E:setColor(0,0,0,0)
            E:setMass(75)
            E:setTrails(5,0,3,"trails/smoke",vec(255,0,0),255)
            holoCreate(3)
            holoModel(3,"models/crossbow_bolt.mdl")
            holoPos(3,E:toWorld(vec(0,0,1)))
            holoAng(3,E:angles()-ang(0,90,0))
            holoParent(3,E)
            E:applyForce(Base:forward()*10^5)
            timer("expl",5000)
        }
    }
}

if(!AllowFire)
{
    E:applyForce(holoEntity(3):forward()*10^5)
    E:applyAngForce(ang(0,0,Bend)+ang(random(-1,1),random(-1,1),random(-1,1)))
    findByClass("player")
    findSortByDistance(holoEntity(3):pos())
    findExcludePlayer(owner())
    Explode = find():pos():distance(holoEntity(3):pos())<100&find()!=owner()
    rangerFilter(array(Base,E))
    R = rangerOffset(holoEntity(3):toWorld(vec(Rad,Rad,Rad/3)),holoEntity(3):toWorld(-vec(0,Rad,Rad/3)))
    if(R:hit())
    {
        Skew = 1
        RE = R:entity()
        E:propFreeze(1)
        if(RE:type()!=""&!RE:isPlayer())
        {
            E:parentTo(RE)
        }
    }
    
    if(changed(Skew)&Skew)
    {
        E:soundPlay("skew",1,"/weapons/crossbow/bolt_skewer1.wav")
        soundPlay("eskew",1,"/weapons/3rd_degree_hit_0"+round(random(1,4))+".wav")
        E:propFreeze(1)
    }else
    {
        E:propFreeze(0)
    }
    
    if(clk("expl")|R:hit()|Explode)
    {
        stoptimer("expl")
        P = propSpawn("models/props_phx/amraam.mdl",holoEntity(3):pos(),1)
        P:propBreak()
        E:propDelete()
        holoDelete(3)
    }
}

if(changed(AllowFire))
{
    if(AllowFire)
    {
        soundPlay("load",1,"/weapons/crossbow/bolt_load"+round(random(1,2))+".wav")
        Skew = 0
    }else
    {
        soundPlay("fire",1,"/weapons/crossbow/fire1.wav")
    }
}
