@name Ragdoll Wonder
@inputs IMode PodEnt:entity Bear Elev
@persist IMode_a:array
@outputs BONE CCpos:vector CCdir:vector CCvel:vector
@persist Ent:entity P:entity FindT Emd:string
@persist Head LH RH LL RL DT UT HeadLegsDist BSon
@persist Force
@persist Ev
@persist Mode:string
## Panic Mode
@persist ShakeT KoT
## Stand Mode
@persist LegsLvl StandF StandM SThr SRL SLL
## Stalk Mode
@persist Stalk
## Cover Mode
@persist LHCover:vector RHCover:vector RHR:ranger LHR:ranger
## Pain Sounds
@persist Pain_a:array Pain_s Pain_pitch SSon
@persist TestT
@model models/gibs/metal_gib4.mdl
@outputs PE:vector PE_l:vector PE_d:vector
#Under Water
@persist UWT Dead
#On Fire
@persist FireSoundT Ignited
@trigger none
###
##
#
#Ragdoll manipulation script made by SilVeR/Silverman/Silver822
#Please do not steal
#
###
####




interval(25)
if(duped()){reset()}
if(clk() & TestT <= 2){TestT+=0.025}
IMode_a:setString(1,"Stand")
IMode_a:setString(2,"Panic")
IMode_a:setString(3,"Cover")
IMode_a:setString(-1,"")
TestT = clamp(TestT,0,2)
Emd = Ent:model():lower()
if(TestT == 0.1){
    BSon = 1
    SSon = 1
    Pain_pitch = randint(95,105)
    print("Model:")
    print(Emd)
    print("Voice Pitch:")
    print(toString(Pain_pitch-100))
    print("[---------------------------]")
if(Emd:find("/male") |
    Emd:find("police")|
    Emd:find("barney")|
    Emd:find("breen")|
    Emd:find("gman")|
    Emd:find("kleiner")|
    Emd:find("monk")|

    Emd:find("odessa"))
    {
Head = 10
DT = 0 ,UT = 1
LH = 4 ,RH = 7
LL = 13 ,RL = 14
HeadLegsDist = 71.5
print("Bone System: Male")
}elseif(Emd:find("/female") |
        Emd:find("mossman")|
        Emd:find("alyx")
        ){
Head = 10
DT = 0 ,UT = 1
LH = 4 ,RH = 7
LL = 13 ,RL = 14
HeadLegsDist = 65
print("Bone System: Female")
}elseif(Emd:find("combine")){ #combine_soldier
Head = 10
DT = 0
UT = 1
LH = 4 ,RH = 7
LL = 13 ,RL = 14
HeadLegsDist = 72
print("Bone System: Combine")
}elseif(Emd:find("zombie/classic")){
Head = 1
UT = 1 ,DT = 0
LH = 7 ,RH = 15
LL = 14 ,RL = 11
HeadLegsDist = 60
print("Bone System: Zombie(Classic)")
}elseif(Emd:find("vortigaunt")){
Head = 17
DT = 0 ,UT = 1
LH = 5 ,RH = 8
LL = 16 ,RL = 12
HeadLegsDist = 75
print("Bone System: Vortigaunt")
}elseif(Emd:find("spy")){
Head = 15
DT = 6 ,UT = 7
LH = 11, RH = 14
LL = 5, RL = 16
HeadLegsDist = 76
print("Bone System: Team Fortress 2(Spy)")
}elseif(Emd:find("sniper")){
Head = 14
DT = 11 ,UT = 7
LH = 12, RH = 15
LL = 6, RL = 3
HeadLegsDist = 74
print("Bone System: Team Fortress 2(Sniper)")
}else{
print("Bone System: Not found for this model")
BSon = 0
}
}

#BONE = entity():owner():aimBone():index()
Force = 500
Ent = entity():isWeldedTo()
Ev = Ent:vel():length()
## Pain Sounds
if(TestT==0.1){
if(Emd:find("/male") |
    Emd:find("odessa")|
    Emd:find("gman")|
    Emd:find("breen")|
    Emd:find("eli")
    ){
    Pain_a:setString(0,"vo/npc/male01/pain01.wav")
    Pain_a:setString(1,"vo/npc/male01/pain02.wav")
    Pain_a:setString(2,"vo/npc/male01/pain03.wav")
    Pain_a:setString(3,"vo/npc/male01/pain04.wav")
    Pain_a:setString(4,"vo/npc/male01/pain05.wav")
    Pain_a:setString(5,"vo/npc/male01/pain06.wav")
    Pain_a:setString(6,"vo/npc/male01/pain07.wav")
    Pain_a:setString(7,"ambient/voices/m_scream1.wav")
    print("Sound Set: Male")
}elseif(Emd:find("/female")|
        Emd:find("alyx")|
        Emd:find("mossman")
){
    Pain_a:setString(0,"vo/npc/female01/pain01.wav")
    Pain_a:setString(1,"vo/npc/female01/pain02.wav")
    Pain_a:setString(2,"vo/npc/female01/pain03.wav")
    Pain_a:setString(3,"vo/npc/female01/pain04.wav")
    Pain_a:setString(4,"vo/npc/female01/pain05.wav")
    Pain_a:setString(5,"vo/npc/female01/pain06.wav")
    Pain_a:setString(6,"vo/npc/female01/pain07.wav")
    Pain_a:setString(7,"vo/npc/female01/pain08.wav")
    Pain_a:setString(8,"vo/npc/female01/pain09.wav")
    print("Sound Set: Female")
}elseif(Emd:find("combine_soldier") |
        Emd:find("police")){
    Pain_a:setString(0,"npc/combine_soldier/pain1.wav")
    Pain_a:setString(1,"npc/combine_soldier/pain2.wav")
    Pain_a:setString(2,"npc/combine_soldier/pain3.wav")
    Pain_a:setString(3,"npc/metropolice/pain1.wav")
    Pain_a:setString(4,"npc/metropolice/pain2.wav")
    Pain_a:setString(5,"npc/metropolice/pain3.wav")
    Pain_a:setString(6,"npc/metropolice/pain4.wav")
    print("Sound Set: Combine")
}elseif(Emd:find("monk")){
    Pain_a:setString(0,"vo/ravenholm/monk_pain01.wav")
    Pain_a:setString(1,"vo/ravenholm/monk_pain02.wav")
    Pain_a:setString(2,"vo/ravenholm/monk_pain03.wav")
    Pain_a:setString(3,"vo/ravenholm/monk_pain04.wav")
    Pain_a:setString(4,"vo/ravenholm/monk_pain05.wav")
    Pain_a:setString(5,"vo/ravenholm/monk_pain06.wav")
    Pain_a:setString(6,"vo/ravenholm/monk_pain07.wav")
    Pain_a:setString(7,"vo/ravenholm/monk_pain08.wav")
    Pain_a:setString(8,"vo/ravenholm/monk_pain09.wav")
    Pain_a:setString(9,"vo/ravenholm/monk_pain10.wav")
    Pain_a:setString(10,"vo/ravenholm/monk_pain1.wav")
    Pain_a:setString(11,"vo/ravenholm/monk_pain12.wav")
    print("Sound Set: Father Grigori")
}elseif(Emd:find("zombie")){
    Pain_a:setString(0,"npc/zombie/zombie_pain1.wav")
    Pain_a:setString(1,"npc/zombie/zombie_pain2.wav")  
    Pain_a:setString(2,"npc/zombie/zombie_pain3.wav")
    Pain_a:setString(3,"npc/zombie/zombie_pain4.wav")
    Pain_a:setString(4,"npc/zombie/zombie_pain5.wav")
    Pain_a:setString(5,"npc/zombie/zombie_pain6.wav")
    print("Sound Set: Zombie(Classic)")
}else{
    print("Sound Set: Not found for this model")
    SSon = 0
}
}
##


### Modes Setup
Mode = "None"

if(Ev > 500){Mode = "Panic" ShakeT+=0.25}

if($Ev > 150){
if(KoT==0){
    KoT+= 1.5
}
KoT+= abs(Ev)/1250
}
if(Ev < 10){
    KoT-= 0.0125 + (10-Ev)/100
}
if(clk()){ShakeT-=0.025, KoT -= 0.025}
ShakeT = clamp(ShakeT,0,2)
KoT = clamp(KoT,0,30)

if(ShakeT>0){Mode = "Panic"}
    
if(KoT==0&ShakeT==0){Mode = "Stand"}


## Various Things

if($Ev< -65 | $Ev > 300){
ShakeT-=1.5
KoT+= 0.5
if(Pain_s == 0 & !Dead){
    soundPlay("Pain",0,Pain_a:string(randint(0,Pain_a:count())))
    soundVolume("Pain",999)
    soundPitch("Pain",Pain_pitch)
    Pain_s = 0.5
}
}
if(clk()){Pain_s-=0.025}
Pain_s = clamp(Pain_s,0,9999)




FireSoundT-=0.025
FireSoundT = clamp(FireSoundT,0,FireSoundT)

rangerIgnoreWorld(1)
rangerHitWater(1)
rangerFilter(Ent)

if(Ent:isUnderWater() & 
rangerOffset(Ent:bone(Head):pos(),Ent:bone(Head):pos() 
- vec(0,0,200)):distance() < 5){
    
    
    UWT+=0.025
    Mode = "Panic"
}elseif(Ent:isOnFire()){
    UWT+=0.025
    Fired = 1
    Mode = "Panic"
        if(Pain_s == 0 & !Dead){
            soundPlay("Pain",0,Pain_a:string(randint(0,Pain_a:count())))
            soundVolume("Pain",999)
            soundPitch("Pain",Pain_pitch)
            Pain_s = 0.5
        }
    KoT+= 0.25

}else{
    UWT-= 0.025/2
}
UWT = clamp(UWT,0,45)
if(UWT>=15){
    Dead = 1
    Mode = ""
}elseif(UWT==0){
    Dead = 0
}
if(Fired){
    #Val = UWT/25 * 255
   # Ent:setColor(255 - Val,255 - Val,255 - Val)
}

if(IMode){
    Mode = IMode_a:string(IMode)
}
## Modes
if(Mode == "Panic"){
    Ent:bone(LH):applyAngForce(ang(
    random(-10,10),
    random(-10,10),
    random(-10,10))*Force)

    Ent:bone(RH):applyAngForce(ang(
    random(-10,10),
    random(-10,10),
    random(-10,10))*Force)
    
    Ent:bone(LL):applyAngForce(ang(
    random(-10,10),
    random(-10,10),
    random(-10,10))*Force)

    Ent:bone(RL):applyAngForce(ang(
    random(-10,10),
    random(-10,10),
    random(-10,10))*Force)
    
    Ent:bone(DT):applyAngForce(ang(
    random(-10,10),
    random(-10,10),
    random(-10,10))*Force*2)
    
    Ent:bone(UT):applyAngForce(ang(
    random(-10,10),
    random(-10,10),
    random(-10,10))*Force*2)
}
rangerIgnoreWorld(0)
rangerHitWater(0)
if(Mode == "Stand"){
    StandM+= 0.025/4
    rangerFilter(Ent)
    LegsLvl = rangerOffset(Ent:bone(Head):pos(),
    Ent:bone(Head):pos()-vec(0,0,100)):distance()
    rangerFilter(Ent)
    SRL = rangerOffset(Ent:bone(RL):pos(),
    Ent:bone(RL):pos() - vec(0,0,100)):distance()
    rangerFilter(Ent)
    SLL = rangerOffset(Ent:bone(LL):pos(),
    Ent:bone(LL):pos() - vec(0,0,100)):distance()
   
    StandF = HeadLegsDist - LegsLvl
    StandM = clamp(StandM,0,1)
    rangerFilter(Ent)
    if(LegsLvl<100 & (SRL < 15 | SLL < 15)){
    SThr =  StandF+$StandF*8
    SThr = clamp(SThr,-50,50)
    Ent:bone(UT):applyForce(vec(0,0,160)*
    SThr*StandM)
    Ent:bone(Head):applyForce(vec(0,0,20)*
    SThr*StandM)
    Ent:applyForce(-Ent:vel()*1)
    }
}else{
StandM = 0
}

if(Mode == "Cover"){
rangerFilter(Ent)
RHR = rangerOffset(50,Ent:bone(LH):pos(),Ent:bone(LH):right())
LHCover = Ent:bone(LH):pos() - 
(Ent:bone(0):pos()+Ent:bone(0):up()*50+Ent:bone(0):forward()*20)
if(RHR:distance() < 50){ 
    Ent:bone(LH):applyForce(Ent:bone(LH):forward()*100)
}
    #Ent:bone(RH):applyForce(-(RHCover+$RHCover*1)*20)
}

    
## Cam Controller
CCpos = Ent:bone(Head):pos()+Ent:bone(Head):right()*3
CCdir = Ent:bone(Head):right()*1
CCvel = Ent:bone(Head):vel()
## Control
PE = PodEnt:driver():eye()
if(PE){
    PE_d = PE - PE_l
    PE_l = PE
    Ent:bone(Head):applyAngForce(ang($Bear,$Elev,0)*25)
    
    
}
    
