#Commands:
#
#!select - selects the prop you are aiming at
#!save <name> - saves the selected prop and the props its constrainted too with the given name
#
@name Hologram maker
@inputs 
@outputs 
@persist [E2 Me]:entity CenterProp:entity Props:array Name:string Path:string N Ready SavingSpeed Size:vector
@trigger 

if(first()) {
    runOnTick(1)
    runOnChat(1)
    
    E2=entity()
    Me=owner()
    
    N=-1
    
    
    #Settings:
    SavingSpeed=100   #The speed in milliseconds it takes between each holo to save (on some servers you can have this very low)
    Size=vec(1,1,1)  #The size of the saved holos
    
}

if(chatClk(Me)) {
    if(Me:lastSaid()=="!select") {
        hideChat(1)
        
        if(Me:aimEntity():isValid()) {
            CenterProp=Me:aimEntity()
            
                print(4, "Center prop is set to "+CenterProp:model())
        }
    }
    elseif(Me:lastSaid():explode(" "):string(1)=="!save") {
        hideChat(1)
        
        if(CenterProp:isValid()) {
            Props=CenterProp:getConstraints()
            Props:insertEntity(1, CenterProp)
            
            Name=Me:lastSaid():sub(7)
            Path=">e2shared/Holograms/"+Name+".txt"
            
            fileWrite(Path, 
            
            "@name "+Name+"\n\n"+
            "if(first()) {\n\n"
            
            )
            
            N=0
            
            Ready=0
        }
    }
}

if(N>=0&&Ready==0) {
    timer("N", SavingSpeed)
}

if(clk("N")) {
    N+=1
    Prop=Props[N, entity]
    
    if(N==1) {
        Parent="entity()"
    }
    else {
        Parent="1"
    }
    
    if(Prop:getColor4()==vec4(255,255,255,255)) {
        Color=""
    }
    else {
        Color="    holoColor("+N+", vec4("+Prop:getColor4():x()+","+Prop:getColor4():y()+","+Prop:getColor4():z()+","+Prop:getColor4():w()+"))\n"
    }
    
    if(Prop:getMaterial()=="") {
        Material=""
    }
    else {
        Material="    holoMaterial("+N+", \""+Prop:getMaterial()+"\")\n"
    }
    
    if(Prop:getSkin()==0) {
        Skin=""
    }
    else {
        Skin="    holoSkin("+N+", "+Prop:getSkin()+")\n"
    }
    
    if(Size==vec(1,1,1)) {
        Scale=""
    }
    else {
        Scale="    holoScale("+N+", vec("+Size:x()+","+Size:y()+","+Size:z()+"))\n"
    }
    
    fileAppend(Path, 
    
    "    holoCreate("+N+")\n"+
    "    holoPos("+N+", entity():toWorld(vec("+round((Prop:pos():x()-CenterProp:pos():x())*Size:x(), 3)+","+round((Prop:pos():y()-CenterProp:pos():y())*Size:y(), 3)+","+round((Prop:pos():z()-CenterProp:pos():z())*Size:z(), 3)+")))\n"+
    "    holoAng("+N+", entity():toWorld(ang("+round(Prop:angles():pitch(), 3)+","+round(Prop:angles():yaw(), 3)+","+round(Prop:angles():roll(), 3)+")))\n"+
    "    holoModel("+N+", \""+Prop:model()+"\")\n"+
    Color+
    Material+
    Skin+
    Scale+
    "    holoParent("+N+", "+Parent+")\n\n"
        
    )
    
    print(4, "Hologram "+N+" Created!")
    
    if(N==Props:count()) {
        timer("Finish", SavingSpeed)
        stoptimer("N")
        
        N=-1
    }
}

if(clk("Finish")) {
    fileAppend(">e2shared/Holograms/"+Name+".txt", 
    
    "}"
    
    )
        
    print(4, "\""+Name+"\" was successfully created in "+Path)
    
    Ready=1
}
