@name PropEngineCreator
@inputs 
@outputs 
@persist Pistons I EngineDat:table Props:array
@trigger 
runOnChat(1)
interval(500)

Chip = entity()

if (chatClk(owner())) {
    local Said = owner():lastSaid():lower()
    
    if (Said:sub(1,4) == "!v12") {
        hideChat(1)
        EngineDat = table(
            array(#Shaft
                "models/sprops/cylinders/size_3/cylinder_6x36.mdl"
                ,vec()
                ,ang(0,0,-90)
                ,vec(255,0,0)
            )
            ,array(#Base
                "models/sprops/rectangles/size_2/rect_12x36x3.mdl"
                ,vec()
                ,ang(0,90,0)
                ,vec(0,255,0)
            )
        )
        
        Pistons = 12
        ThrusterDist = 10
        for (N = 0,Pistons/2) {
            for (I=-1,1,2) {
                EngineDat:pushArray(array(
                    "models/sprops/cylinders/size_3/cylinder_6x3.mdl"
                    ,vec(ThrusterDist*I,N*6-15,ThrusterDist)
                    ,ang(45*I,0,0)
                    ,vec(0,0,255)
                ))
            }
        }
        
        for (N = 1,Pistons+2) {
            local R = EngineDat[N,array]
            holoCreate(N)
            holoModel(N,R[1,string])
            holoPos(N,Chip:toWorld(R[2,vector]))
            holoAng(N,Chip:toWorld(R[3,angle]))
            holoColor(N,R[4,vector])
            holoAlpha(N,200)
            holoParent(N,Chip)
        }
    }
    
    if (Said:sub(1,3) == "!go") {
        hideChat(1)
        I = 1
    }
    
    if (Said:sub(1,2) == "!r") {
        hideChat(1)
        propDeleteAll()
        I = 0
    }
}

StrokeLength = 3

if (I & I <= Pistons+2) {
    local R = EngineDat[I,array]
    local E = propSpawn(R[1,string],Chip:toWorld(R[2,vector]),Chip:toWorld(R[3,angle]),1)
    Props:pushEntity(E)
    noCollideAll(E,1)
    holoDelete(I)
    if (E) {
        if(I > 2){
            E:sliderTo(0,Props[2,entity],0,0,vec(),vec(floor((I-3)/2)*6-15,0,0),1,"cable/cable2")
            
            local A = (I - 3) * 60
            Props[1,entity]:setAng(Chip:toWorld(ang(-A+90,0,-90)))
            
            E:ropeTo(0,Props[1,entity],0,0,vec(),vec(cos(A)*StrokeLength,sin(A)*StrokeLength,floor((I-3)/2)*6-15),0,0,0,0,"cable/cable2",1)
            
            if(I == Pistons+2){
                Props[1,entity]:axisTo(0,Props[2,entity],0,0,vec(),vec(),0,0,0,1,vec(0,0,1))
            }
        }
        I++
    }
    timer("remove",10000)
}

if (clk("remove")) {
    reset()
}
