@name Turret for ZEOS
@inputs 
@outputs Rad Rebuild Mode M1 M2 Count
@persist [H O E]:entity Decay R:array T:table Rb 
@persist Fire Boom Pos:vector Angle:angle Radius
@model 

if(first() | duped()){
    runOnTick(1)
    runOnLast(1)
    runOnChat(1)
if(entity():isWeldedTo()){ E = entity():isWeldedTo() }else{ E = entity() }
O = owner()
H = holoCreate(1,E:toWorld(E:boxCenter()+vec(48,0,0)),vec(9,9,24)/12,E:toWorld(ang(90,0,0)))
holoModel(1,"hq_cylinder")
holoMaterial(1,"models/props_combine/prtl_sky_sheet")
holoParent(1,E)
Decay = 30
soundPlay("start",0,"vehicles/tank_readyfire1.wav")
for(I=2,4){
    holoCreate(I)
    holoAlpha(I,0)
}
Radius = 500
}
Shoot = O:keyAttack2() & O:keyUse() & O:isCrouch()
if(changed(Shoot) & Shoot){
    Fire = 1
    Boom = 100
    E:soundPlay("kaboom",0,"weapons/awp/awp1.wav") #"weapons/explode5.wav")
    soundPitch("kaboom",85)
    soundVolume("kaboom",500)
    Rad = 0
    Angle = H:angles()
    holoUnparent(1)
}

if(Fire==1){
    Pos = H:toWorld(vec(0,0,Boom))
    holoPos(1,Pos)
    Can = H:pos():distance(E:pos())>500
    #Angle = clamp(Angle-ang(0,0,0),-ang(180,180,180),ang(180,180,180))
    holoAng(1,H:toWorld(ang(0,0,Can/5)))
    Boom = clamp(Boom - Boom/Decay,20,100)
    Ro = rangerOffset(12,H:toWorld(vec(0,0,12)),H:up())
    if(Ro:hit()){ Fire = 2 }
}

if(changed(Fire) & Fire==2){
    holoModel(1,"hq_icosphere")
    Rad = 512
    H:soundPlay("boom",0,"weapons/explode1.wav")
    for(I=1,4){
        holoEntity(I):setTrails(40,40,10,"trails/smoke",vec(),255)
        if(I>1){
            holoPos(I,H:pos())
        }
    }
    findByClass("prop_*")
    findClipToSphere(H:pos(),Radius)
    R = findToArray()
    Count = players():count()
    for(I=1,Count){
        Ent = players()[I,entity]
        if(Ent:pos():distance(H:pos())<=Radius){
            R:pushEntity(Ent)
        }
    }   
    Count = T["count",number] = R:count()
    Mode = 1
}
if(Mode==1 & Count){
    while(M1<Count & perf()){
        M1++
        Ent = R[M1,entity]
        if(!Ent:isPlayer() & Ent:type()!="prop_ragdoll"){
            #Ent:destroy()
            if(Ent:hasConstraints()){
                Ent:removeAllConstraints()
            }
            Ent:deparent()
            Ent:noCollideAll(1)
            Ent:propFreeze(0)
            T["ent"+M1,entity] = Ent
            T["pos"+M1,vector] = Ent:pos()
            T["ang"+M1,angle] = Ent:angles()
        }
        if(M1>=Count){
            Mode=0
            timer("kersplode",500)
        }
    }
}
if(clk("kersplode")){
    Mode=2
}

if(Mode==2 & Count){
    while(M2<Count & perf()){
        M2++
        Ent = R[M2,entity]
        Dist = Radius-Ent:pos():distance(H:pos())
        if(Ent:isPlayer()){
            Ent:applyPlayerForce((Ent:toWorld(vec(0,0,20))-H:pos())*Dist/20)
        }else{
            Ragdoll = Ent:type()=="prop_ragdoll" ? 3 : 1
            Ent:applyForce((Ent:pos()-H:pos())*Ent:mass()*(Dist*Ragdoll))
        }
        if(M2>=Count){ Mode = 0 }
    }
}

if(Rad){
    Rad = Rad-Rad/20
    holoScale(1,vec()+(512-Rad)/12)
    holoAlpha(1,Rad)
}
if(Fire==2 & inrange(Rad,100,475)){
    for(I=2,4){
        holoPos(I,holoEntity(I):pos()+vec(randint(-50,50),randint(-50,50),5))
    }
}

if(chatClk(O)){
    Lsx = O:lastSaid():lower():explode(" ")
    if(Lsx[1,string]=="rebuild"){
        print("trying to rebuild")
        Rebuild = 1
        Count = T["count",number]
    }
}

if(Rebuild){
    while(Rb<Count & perf()){
        Rb++
        Ent = T["ent"+Rb,entity]
        if(Ent){
            Ent:propFreeze(1)
            Ent:setPos(T["pos"+Rb,vector])
            Ent:setAng(T["ang"+Rb,angle])
            if(Rb!=1){
                Ent:weldTo(T["ent"+1,entity],1)
            }else{ Ent:setMass(5000) }
        }
    }
    if(Rb==Count){ Rebuild = 0 , reset() }
}
