@name NanoLathe
@inputs 
@outputs PositionFile:string PositionFile1:string PositionFile2:string Timer Waiting Wait Build:entity Final G T Can Prop:string Num I Start Pos:array Lathing S1 S2 S3 S1A S2A S3A
@persist S Solid MatSet Mat:string PropFile:string AngleFile:string Name:string Saving Loading Ang:array Holo:array Props:array Mats:array Sols:array ManipY ManipP ManipR MP MY MR H Y X Hoff Yoff Xoff Aim:vector SNAP Int IntSet SnapSet
@trigger 
runOnTick(1)
runOnChat(1)
if(first()){
    holoCreate(1)
    holoPos(1,entity():toWorld(vec(0,0,50)))
    holoParent(1,entity())
    Can=1
    I=0
    Num=1
    SNAP=45
    Int=10
    Mat=""
}
if(chatClk(owner())&owner():lastSaid()=="/sn"){
    hideChat(3)
    SnapSet=1
}
if(chatClk(owner())&owner():lastSaid()=="/int"){
    hideChat(3)
    IntSet=1
}
if(chatClk(owner())&owner():lastSaid()=="/mat"){
    hideChat(3)
    MatSet=1
}
if(chatClk(owner())&owner():lastSaid()=="/sol"){
    hideChat(3)
    Solid=!Solid
    hint("Prop will be Solid when spawned: "+!Solid,3)
}
if(IntSet==1&chatClk(owner())&owner():lastSaid()!="/int"){
    hideChat(3)
    Int=owner():lastSaid():toNumber()
    hint("Movement Snapping changed to "+Int+" GM Units",2)
    IntSet=0
}
if(SnapSet==1&chatClk(owner())&owner():lastSaid()!="/sn"){
    hideChat(3)
    SNAP=owner():lastSaid():toNumber()
    hint("Rotation Snapping changed to "+SNAP+" degrees",2)
    SnapSet=0
}
if(MatSet==1&chatClk(owner())&owner():lastSaid()!="/mat"){
    hideChat(3)
    Mat=owner():lastSaid()
    hint("Material changed to "+Mat,2)
    MatSet=0
}


if(chatClk(owner())&owner():lastSaid()=="/build"){
    hideChat(3)
    Waiting=1
}
if(chatClk(owner())&owner():lastSaid()=="/set"){Aim=owner():aimPos()
    hideChat(3)}
if(Waiting==1&chatClk(owner())&owner():lastSaid()!="/build"&Can==1){
    hideChat(3)
    Prop=owner():lastSaid()
    Waiting=0
    Wait=1
    holoCreate(4)
    holoModel(4,Prop)
    holoMaterial(4,"models/wireframe")
}
if(Wait==1){holoPos(4,Aim+vec(0,0,holoEntity(4):boxSize():z()/2)+vec(X,Y,H))
    holoAng(4,ang(MP,MY,MR))
    holoAlpha(4,255)
    if(chatClk(owner())&owner():lastSaid()=="/c"){Wait=0
        Prop=""
        Num=1
    }
    }
if(Wait==0){holoAlpha(4,0)}
if(Wait==1&changed(owner():keyWalk())&owner():keyWalk()==1){
    Pos:pushVector(Aim+vec(X,Y,H))
    Ang:pushAngle(ang(MP,MY,MR))
    Props:pushString(Prop)
    Mats:pushString(Mat)
    Sols:pushNumber(Solid)
    holoCreate(Num+5)
    holoModel(Num+5,Prop)
    holoMaterial(Num+5,"models/wireframe")
    holoColor(Num+5,vec(0,0,255))
    holoPos(Num+5,Aim+vec(0,0,holoEntity(4):boxSize():z()/2)+vec(X,Y,H))
    holoAng(Num+5,ang(MP,MY,MR))
    Holo:pushEntity(holoEntity(Num+5))
    Num+=1
    Timer+=0.85
}
if(Wait==1&chatClk(owner())&owner():lastSaid()=="/oz"){
    hideChat(3)
    ManipP=0
    ManipY=0
    ManipR=0
    Xoff=0
    Yoff=0
    Hoff=1
}
if(Wait==1&chatClk(owner())&owner():lastSaid()=="/ox"){
    hideChat(3)
    ManipP=0
    ManipY=0
    ManipR=0
    Yoff=0
    Hoff=0
    Xoff=1
}
if(Wait==1&chatClk(owner())&owner():lastSaid()=="/oy"){
    hideChat(3)
    ManipP=0
    ManipY=0
    ManipR=0
    Hoff=0
    Xoff=0
    Yoff=1
}
if(Wait==1&chatClk(owner())&owner():lastSaid()=="/y"){
    hideChat(3)
    ManipR=0
    ManipP=0
    ManipY=1
    Xoff=0
    Yoff=0
    Hoff=0
}
if(Wait==1&chatClk(owner())&owner():lastSaid()=="/p"){
    hideChat(3)
    ManipY=0
    ManipR=0
    ManipP=1
    Xoff=0
    Yoff=0
    Hoff=0
}
if(Wait==1&chatClk(owner())&owner():lastSaid()=="/r"){
    hideChat(3)
    ManipP=0
    ManipY=0
    ManipR=1
    Xoff=0
    Yoff=0
    Hoff=0
}
if(Wait==1&chatClk(owner())&owner():lastSaid()=="/n"){
    hideChat(3)
    ManipP=0
    ManipY=0
    ManipR=0
    Xoff=0
    Yoff=0
    Hoff=0
}
if(ManipP==1&changed(owner():keyAttack1())&owner():keyAttack1()==1){MP+=SNAP}
if(ManipP==1&changed(owner():keyAttack2())&owner():keyAttack2()==1){MP-=SNAP}
if(ManipY==1&changed(owner():keyAttack1())&owner():keyAttack1()==1){MY+=SNAP}
if(ManipY==1&changed(owner():keyAttack2())&owner():keyAttack2()==1){MY-=SNAP}
if(ManipR==1&changed(owner():keyAttack1())&owner():keyAttack1()==1){MR+=SNAP}
if(ManipR==1&changed(owner():keyAttack2())&owner():keyAttack2()==1){MR-=SNAP}
if(Hoff==1&changed(owner():keyAttack1())&owner():keyAttack1()==1){H+=Int}
if(Hoff==1&changed(owner():keyAttack2())&owner():keyAttack2()==1){H-=Int}
if(Yoff==1&changed(owner():keyAttack1())&owner():keyAttack1()==1){Y+=Int}
if(Yoff==1&changed(owner():keyAttack2())&owner():keyAttack2()==1){Y-=Int}
if(Xoff==1&changed(owner():keyAttack1())&owner():keyAttack1()==1){X+=Int}
if(Xoff==1&changed(owner():keyAttack2())&owner():keyAttack2()==1){X-=Int}

if(chatClk(owner())&owner():lastSaid()=="/start"){
    hideChat(3)
    Start=1
    ManipP=0
    ManipR=0
    ManipY=0
    MP=0
    MY=0
    MR=0
    Hoff=0
    Yoff=0
    Xoff=0
    H=0
    X=0
    Y=0
    hint("Started",2)
    hint("Estimated Time: "+Timer+" Sec",5)
    owner():soundPlay(0,9999,"ambience/labdrone1.wav")
}
if(I>=Pos:count()&Start==1){Start=0
    I=0
    Pos:clear()
    Ang:clear()
    Holo:clear()
    Props:clear()
    Mats:clear()
    Sols:clear()
    Wait=0
    Wait=0
    Prop=""
    Mat=""
    Solid=0
    Num=1
    Waiting=0
    Can=0
    Timer=0
    hint("Contruction Complete",2)
    soundStop(0)
}
if(Start==1&Can==1){
    I+=1
    Can=0
    holoDelete(I+5)
    Build=propSpawn(Props[I,string],Pos[I,vector],Ang[I,angle],1)
    Build:setMaterial(Mats[I,string])
    Build:setPos(Build:pos()+vec(0,0,Build:boxSize():z()/2))
    Build:propNotSolid(Sols[I,number])
    Build:setAlpha(0)
    S3A=0
    holoCreate(2)
    holoCreate(3)
    holoModel(2,Props[I,string])
    holoModel(3,Props[I,string])
    holoPos(2,Build:pos())
    holoPos(3,Build:pos())
    holoAng(2,Build:angles())
    holoAng(3,Build:angles())   
    holoMaterial(2,"models/wireframe")
    holoMaterial(3,"models/debug/debugwhite")
    holoAlpha(2,0)
    holoAlpha(3,0)
    holoColor(2,vec(0,0,0),0)
    holoColor(3,vec(0,0,0),0)
    Lathing=1
    Wait=0
    owner():soundPlay(1,3,"NOTA/BUILD1.WAV")

}
if(G<=0){T=1}
if(G<255&T==1){G+=10}
if(G>=255){T=0}
if(T==0){G-=10}
holoPos(2,Build:pos())
holoPos(3,Build:pos())
holoAng(2,Build:angles())
holoAng(3,Build:angles())
holoColor(2,vec(0,G,0),S1A)
holoColor(3,vec(0,G,0),S2A)
Build:setAlpha(S3A)
if(changed(Lathing)&Lathing==1){
    
    S1=1
    timer("stage2",200)
}
if(clk("stage2")){S2=1
    timer("stage3",200)
    }
if(clk("stage3")){S3=1
    timer("final",200)
    }
if(clk("final")){
    Final=1
    timer("done",250)
}
if(S1==1){
    S1A+=38
    if(S1A>255){S1A=255}
}
if(S2==1){
    S1=0
    S2A+=38
    if(S2A>255){S2A=255}
}
if(S3==1){
    S2=0
    S3A+=38
    if(S3A>255){S3A=255}
    S2A-=38
    if(S2A<0){S2A=0}
}
if(Final==1){
    S3=0
    S1A-=38
    if(S1A<0){S1A=0}
}
if(clk("done")){
    Build:propFreeze(0)
    Build:propFreeze(1)
    holoDelete(2)
    holoDelete(3)
    Final=0
    S1=0
    S2=0
    S3=0
    S1A=0
    S2A=0
    Lathing=0
    Wait=0
    Build=noentity()
    timer("again",100)
}
if(clk("again")){Can=1}
