@name Rock Monster
@inputs Find Run Alive Yaw Forward Angry
@outputs Turn Go Wait
@persist [Rocks]:array X Y Delay Flag Kill [Target Body]:entity
@trigger Find Reset Alive

Tick=40
interval(Tick)

Yaw*=10
Piece=150
Pi=3.141592
MoveFoot=array()
Go=0
Turn=0

if(owner():keyAttack1() & (owner():aimEntity():isPlayer()|owner():aimEntity():isNPC()) & owner():weapon()==entity(50))
{Target=owner():aimEntity()}

if(Angry&Target:isAlive())
{
    Go=(abs(angnorm((Target:pos()-vec(X,Y,0)):toAngle():yaw()-90-Yaw))<10 ? 1 : 0)
    Turn=3*sign((angnorm((Target:pos()-vec(X,Y,0)):toAngle()):yaw()-90-Yaw))
    if((Rocks[13,entity]:massCenter()-Target:pos()):length()<300&Wait<-100)
    {Kill=1,Wait=10}    
    
}
Wait-=1
if(Wait<0){Kill=0}

if(Find & ~Find)
{
    findByModel("models/props_c17/gravestone_cross001a.mdl")
    Rocks=findToArray()
    X=0
    Y=0
}
if(!Find & ~Find)
{
    findByModel("models/props_trainstation/pole_448Connection002b.mdl")
    Body=find()
}



X=(Forward != 0 & Alive ? X-Tick*Forward*sin(Yaw)/5 : X)
Y=(Forward != 0 & Alive ? Y+Tick*Forward*cos(Yaw)/5 : Y)

G=-5*sinr(Run)

if(!Alive&~Alive)
{
    for(X=1,15)
    {
        EE=Rocks[X,entity]
        EE:applyForce((randvec(200,300))*100*((X%2)*2-1))
        EE:applyAngForce(randvec(100,200):toAngle()*10000)
    }
    Body:applyForce(vec(0,0,100000))
    soundStop(17)
}

if(Alive & ~Alive)
{
    Delay=curtime()+10
    Body:soundPlay(17,0,"physics/concrete/concrete_block_scrape_rough_loop1.wav")
}
MoveSpeed=(curtime()<Delay ? vec(1,1,1)*((10-Delay+curtime())/10) : vec(1,1,1))
if(duped()){hint("rophl made this!",3)}
MoveAngSpeed=(curtime()<Delay ? 0.3 : 1)
if(Delay-curtime()<5){soundStop(17,3)}

Foot=array(vec(-350,225,25),vec(-350,-225,25),vec(350,-225,25),vec(350,225,25))
Shoulder=array(vec(-50,125,150),vec(-50,-125,150),vec(50,-125,150),vec(50,125,150))
DecP=array(vec(10,261,192),vec(8,118,248),vec(-5,-73,228))
DecA=array(ang(0,7,-90),ang(45,-90,0),ang(45,-90,0))

for(Leg=1,4)
{
    MoveFoot[Leg,vector]=(Foot[Leg,vector]+vec(0,
    ( cosr(Run+Pi*(Leg%2))>=0 ? 200*sinr(Run+Pi*(Leg%2)) : ((Run-Pi/2)%Pi)*(-400/Pi)+200)
    ,0)):rotate(0,Yaw,0)+vec(X,Y,0)
    
    rangerFilter(Rocks)
    Height=5025-rangerOffset(50000,MoveFoot[Leg,vector]+vec(0,0,5000),vec(0,0,-1)):distance()
    MoveFoot[Leg,vector]=MoveFoot[Leg,vector]+vec(0,0,Height+(100*cosr(Run+Pi*(Leg%2))>=0 ? 100*cosr(Run+Pi*(Leg%2)) : 0))
}

BodyPos=(MoveFoot[1,vector]+MoveFoot[2,vector]+MoveFoot[3,vector]+MoveFoot[4,vector])/4-vec(X,Y,100)
BodyPitch=asin(((MoveFoot[1,vector]:z()+MoveFoot[4,vector]:z())/2-(MoveFoot[2,vector]:z()+MoveFoot[3,vector]:z())/2)/250)

for(Leg=1,4)
{
    
    if(!Alive){break}    
    
    Shoulder[Leg,vector]=(Shoulder[Leg,vector]:rotate(0,Yaw+G,BodyPitch)+BodyPos)+vec(X,Y,0)
    
    if(sign((Run+Pi*(Leg%2))%(2*Pi)-Pi/2)==1 & sign((Run-0.04+Pi*(Leg%2))%(2*Pi)-Pi/2)==-1)
    {soundVolume(Leg,5),Rocks[Leg*3,entity]:soundPlay(Leg,2,"physics/concrete/boulder_impact_hard3.wav")}
    
    Vec=clamp(Shoulder[Leg,vector]-MoveFoot[Leg,vector],0,2.8*Piece)
    Len=Vec:length()
    Knee=(vec(Vec:y(),-Vec:x(),0):cross(Vec)):normalized()*sqrt(Piece*Piece-(Len-Piece)*(Len-Piece)/4)
    SPos=array(
        MoveFoot[Leg,vector],
        clamp(Vec,0,(Len-Piece)/2)+MoveFoot[Leg,vector]+Knee,
        clamp(Vec,0,Piece+(Len-Piece)/2)+MoveFoot[Leg,vector]+Knee,
        Shoulder[Leg,vector]
        )
    
    
    for(I=1,3)
    {
        EE=Rocks[I+3*Leg-3,entity]
        Pos=EE:pos()
        Ang=angnorm(EE:up():toAngle()+ang(0,0,EE:angles():pitch()))
        Vel=EE:vel()
        AngVel=EE:angVel()
        
        TarPos=(SPos[I,vector]+SPos[I+1,vector])/2
        TarAng=angnorm((SPos[I,vector]-SPos[I+1,vector]):toAngle())
        
        Q=quat(TarAng)/quat(Ang)
        Torque=EE:toLocal(rotationVector(Q)+EE:pos())
        
        EE:applyForce( ((TarPos-Pos)*5*MoveSpeed-Vel)*EE:mass() )
        EE:applyTorque((Torque*30*MoveAngSpeed - EE:angVelVector()*10)*EE:inertia() )
    }
    if(Leg==1)
    {
        Body:applyForce( (((vec(9,-6,162):rotate(0,Yaw+G,BodyPitch)+BodyPos)+vec(X,Y,0)-Body:massCenter())*5*MoveSpeed-Body:vel())*Body:mass() )
        Ang=angnorm(Body:angles())
        TarAng=ang(-90-BodyPitch,90+0*sinr(Run)+Yaw+G,0)
        Q=quat(TarAng)/quat(Ang)
        Torque=Body:toLocal(rotationVector(Q)+Body:pos())
        Body:applyTorque((Torque*10*MoveAngSpeed - Body:angVelVector())*Body:inertia())
        for(I=13,15)
        {
            EE=Rocks[I,entity]
            EE:applyForce( 
            ((
            (Kill&I==13 ? Target:pos() : DecP[I-12,vector]:rotate(0,Yaw+G,BodyPitch)+BodyPos+vec(X,Y,0))-EE:massCenter())*5*MoveSpeed-EE:vel())*EE:mass() )
            Ang=angnorm(EE:angles())
            TarAng=angnorm(DecA[I-12,angle]+ang((I!=13 ? BodyPitch : 0),Yaw+G,(I==13 ? BodyPitch : 0)))
            Q=quat(TarAng)/quat(Ang)
            Torque=EE:toLocal(rotationVector(Q)+EE:pos())
            EE:applyTorque((Torque*30*MoveAngSpeed-EE:angVelVector()*10)*EE:inertia())
        }
    }
}
#Body position is 59,-151,11
#
