@name 3D Printer
@inputs [FuelRail OppRail TranPlate]:entity
@outputs
@persist [CurNozzlePos TarNozzlePos MP FirstCell]:vector RailDist CurCell CellSize
 
runOnTick(1)
 
function void debugHolo(P:vector) {
    holoCreate(randint(0,100), P, vec(0.1,0.1,0.1), ang(0,0,0), vec(255,0,0), "hqsphere")
}
 
function normal equal(N1:normal, N2:normal, E:normal) {
    if( abs(N1-N2) < E ) {
        return 1
    }
    else{
        return 0
    }
}
 
 
if(first() | dupefinished()) {
    NewPos = 0
    MP = (FuelRail:massCenter() + OppRail:massCenter())/2 - vec(0,0,2)
    CurNozzlePos = MP
    RailDist = abs(FuelRail:pos():y() - OppRail:pos():y())
   
   
    #Nozzle Plate
    holoCreate(1,CurNozzlePos)
    holoAng(1,ang(0,0,0))
    holoMaterial(1,"sprops/sprops_grid_orange_12x12")
    holoScaleUnits(1,vec(5,5,1))
    #
   
    #Nozzle Rail Guiders
    holoCreate(2,CurNozzlePos-vec(1.5,0,-0.5))
    holoAng(2,ang(0,0,90))
    holoScaleUnits(2,vec(1.5,1.5,3))
    holoModel(2,"hqcylinder")
    holoMaterial(2,"sprops/sprops_grid_12x12")
    holoParent(2,1)
   
    holoCreate(3,CurNozzlePos-vec(-1.5,0,-0.5))
    holoAng(3,ang(0,0,90))
    holoScaleUnits(3,vec(1.5,1.5,3))
    holoModel(3,"hqcylinder")
    holoMaterial(3,"sprops/sprops_grid_12x12")
    holoParent(3,1)
    #
   
    #Rail Guiders
    holoCreate(4,FuelRail:massCenter() - vec(0,0,1))
    holoAng(4,ang(90,0,0))
    holoScaleUnits(4,vec(2,2,5))
    holoModel(4,"hqcylinder")
    holoMaterial(4,"sprops/sprops_grid_12x12")
   
    holoCreate(5,OppRail:massCenter() - vec(0,0,1))
    holoAng(5,ang(90,0,0))
    holoScaleUnits(5,vec(2,2,5))
    holoModel(5,"hqcylinder")
    holoMaterial(5,"sprops/sprops_grid_12x12")
    #
   
    #Rails
    holoCreate(6, holoEntity(2):pos()+vec(0,0,0.3))
    holoAng(6,ang(0,0,90))
    holoScaleUnits(6,vec(0.5,0.5,RailDist))
    holoModel(6,"hqcylinder")
    holoColor(6,vec(0,0,0))
    holoParent(6,4)
   
    holoCreate(7, holoEntity(3):pos()+vec(0,0,0.3))
    holoAng(7,ang(0,0,90))
    holoScaleUnits(7,vec(0.5,0.5,RailDist))
    holoModel(7,"hqcylinder")
    holoColor(7,vec(0,0,0))
    holoParent(7,4)
    #
   
    CellSize = TranPlate:boxSize():x() / 4
    FirstCell = MP - vec(1.5*CellSize,1.5*CellSize,20)
    TarNozzlePos = FirstCell
    CurCell = 0
}
holoPos(1, CurNozzlePos)
holoPos(4, holoEntity(4):pos():setX(holoEntity(1):pos():x()))
holoPos(5, holoEntity(5):pos():setX(holoEntity(1):pos():x()))
 
 
if(CurNozzlePos:x() != TarNozzlePos:x() || CurNozzlePos:y() != TarNozzlePos:y()) {
    CX = CurNozzlePos:x()
    CY = CurNozzlePos:y()
    TX = TarNozzlePos:x()
    TY = TarNozzlePos:y()
    if(!equal(CX,TX, 0.5)) {
        if(CX < TX) {
            CurNozzlePos = CurNozzlePos:setX(CX + 0.4)    
        }
        else{
            CurNozzlePos = CurNozzlePos:setX(CX - 0.4)  
        }    
    }
    else{
        CurNozzlePos = CurNozzlePos:setX(TarNozzlePos:x())
        if(!equal(CY,TY,0.5)) {
            if(CY < TY) {
                CurNozzlePos = CurNozzlePos:setY(CY + 0.4)
            }
            else{
                CurNozzlePos = CurNozzlePos:setY(CY - 0.4)
            }  
        }
        else{
            CurNozzlePos = CurNozzlePos:setY(TarNozzlePos:y())
 
        }
    }
 
}
else{
    timer("AdvanceNozzle",1000)
}
 
if(clk("AdvanceNozzle")) {
    CurCell++
    if(CurCell > 15) {
        CurCell = 0
    }
    if(floor(CurCell/4) % 2 == 0) {
        TarNozzlePos = FirstCell + vec( (CurCell % 4) * CellSize, floor(CurCell / 4) * CellSize,0)
    }
    else{
        TarNozzlePos = FirstCell + vec( 4 * CellSize - (CurCell % 4) * CellSize, floor(CurCell / 4) * CellSize,0)
    }
}
