#HOW TO USE THIS EXPRESSION!!!
#Step 1: Slap it on any random prop you want
#Step 2: Use the following commands to manipulate that prop at your will. (type in chat)
#"!" or "! [name]"   (! used by itself causes the prop to fly at the closest player / NPC)
#"/orbit [0 or 1]" causes the prop to move in a circle
#"/spin [0 or 1]" causes the prop to spin (doesn't work too well with the larger props, try a saw blade.)
#"/me" togglable, causes the prop to float above your head
#"/h #" change the float height
#"/cross [0 or 1]" causes the prop to follow your crosshair
#"/stab [0 or 1]" toggles automatic stabilization
#"/on" and "/off" Obvious!
        
@name Force Test
@persist TV:vector EV:vector Stabilize On Cursor Me Attack Run Orbit Alive SV:vector Height Spin

interval(0.01)
        
runOnChat(1)

#Set it up in the beginning
EE=entity():isWeldedTo(1),EA=(EE:angles()*5+EE:angVel())*5
EV=EE:massCenter(),ER=EE:right(),EF=EE:forward(),EU=EE:up(),O = owner(),
Lev=sqrt((EE:inertia():length()^2)/3)
LS = O:lastSaid()

#Figure out all the different modes
if(chatClk(O) & LS == "/kill") {selfDestructAll()}

if(chatClk(O) & LS:left(2) == "/h") {Height = LS:sub(4, LS:length()):toNumber()}

if(chatClk(O) & LS:left(6) == "/orbit") {Orbit=!Orbit}

if(chatClk(O) & LS:left(1) == "!") {
    if(LS == "!") {
        findIncludeClass("npc_*")
        findIncludeClass("player")
        findExcludePlayer(O)
        Fsphere = findInSphere(O:pos(), 50000)
        if(Fsphere > 0) {
            T = findClosest(O:aimPos())
            Attack = 1
            Run = 0
            Me = 0
        } else {
            Attack = 0
            Me = 1
            Cursor = 0
            Run = 0
        }
    } else {
        Name = LS:sub(2, LS:length())
        T = findPlayerByName(Name)
        Attack = 1
        Run = 0
        Me = 0
        Cursor = 0
    }
} elseif(clk("A") | clk("B")) {
    Attack = 0
    Run = 1
    Me = 1
    Cursor = 0
   
    if(EE:type() == "prop_physics") {
        Stabilize = 1
    }
}

if(chatClk(O) & LS == "/cross 1") {
    Cursor = 1
    Attack = 0
    Me = 0
} elseif(chatClk(O) & LS == "/cross 0") {
    Cursor = 0
    SV = EV
}

if(chatClk(O) & LS == "/spin 1") {
    Spin = 1
} elseif(chatClk(O) & LS == "/spin 0") {
    Spin = 0
}

if(chatClk(O) & LS == "/me") {
    if(Me) {
        Me = 0
        Cursor = 0
        Attack = 0
        SV = EV
    } else {
        Me = 1
        Cursor = 0
        Attack = 0
    }
}

if(chatClk(O) & LS == "/stab 1") {
    Stabilize = 1
} elseif(chatClk(O) & LS == "/stab 0") {
    Stabilize = 0
}

if(chatClk(O) & LS == "/on") {
    On = 1
} elseif(chatClk(O) & LS == "/off") {
    On = 0
}
####################################

if (first()) {
    SV = EV
    Cursor = 0
    Spin = 0
    On = 1
    Height = EE:radius()
   
    if(EE:type() == "prop_physics") {
        EE:setMass(250)
        Stabilize = 1
    } elseif(EE:type() == "prop_ragdoll") {
        EE:setMass(250)
        Stabilize = 0
    } else {
        EE:setMass(250)
        Stabilize = 1
    }
   
   
}

#Apply different things and shit
if(On) {
    if(Orbit) {
        Circle = vec(Height*sin(curtime()*50), Height*cos(curtime()*50), 0)
    } else {
        Circle = vec(0, 0, 0)
    }
   
    if(Stabilize) {
        EE:applyOffsetForce( EU*(EA:pitch()), EV+EF * Lev)
        EE:applyOffsetForce(-EU*(EA:pitch()), EV-EF * Lev)
        EE:applyOffsetForce( EU*(EA:roll()) , EV+ER * Lev)
        EE:applyOffsetForce(-EU*(EA:roll()) , EV-ER * Lev)
    }
    
    if(Cursor) {
        TV = O:aimPos() + vec(0, 0, Height) + Circle - EV - EE:vel()/2
        #print(toString(Height))
    }
   
    if(!Cursor & !Me & !Attack) {
        TV = SV + Circle - EV + vec(0, 0, Height) - EE:vel()/2
    }
   
    if(Me) {
        TV = O:pos() + Circle - EV + vec(0, 0, O:height() + 25 + Height) - EE:vel()/2
    }
    if(Attack&(T:isPlayer() | T:isNPC())) {
        if(T:isAlive()) {
            #print(Name)
   TV=(T:pos()+vec(0,0,T:height())-EV)*2000
            Run = 1
           
            Stabilize = 0
            timer("A", T:pos():distance(EV) * 15)
        } elseif(T:isAlive() == 0) {
            timer("B", 250)
            TV = vec(0, 0, -50000000)
        }
    }
   
    EE:applyForce(TV*EE:mass())
   
    if(Spin) {
        EE:applyOffsetForce( ER * (360 * 3 + EE:angVel():yaw()), EV + EF * Lev)
        EE:applyOffsetForce(-ER * (360 * 3 + EE:angVel():yaw()), EV - EF * Lev)
    }
}




