@name DivArmShop
@persist Rot CurRot
@persist Rail CurRail
@persist A1P CurA1P
@persist A2P CurA2P
@persist HP CurHP
@persist Speed Scale
@persist State:string
@outputs Grab Use 
@inputs Grabber:entity 

interval(100)

if(first()) {
    Speed = 4
    Scale = 0.8
    
    # Top yaw platform
    holoCreate(6,entity():toWorld(vec(0.324604034424,115.02613830566,6.224433898926) * Scale),vec(1) * Scale,entity():toWorld(ang(0,-90,90)))
    
    # Arm 1 pivot
    holoCreate(7,entity():toWorld(vec(-4.624655532837,115.02603912354,6.245582580566) * Scale),vec(1) * Scale,entity():toWorld(ang(0,-90,90)))
    holoModel(7,"models/hunter/blocks/cube025x025x025.mdl")
    holoParent(7,6)
    
    holoAng(7,entity():toWorld(ang(0,-90,90)))
    
    # Arm 2 pivot
    holoCreate(9,entity():toWorld(vec(-4.51922416687,114.33148956299,-59.242314338684) * Scale),vec(1) * Scale,entity():toWorld(ang(0,-90,90)))
    holoModel(9,"models/hunter/blocks/cube025x025x025.mdl")
    holoParent(9,7)
    
    # Hand Pivot
    holoCreate(11,entity():toWorld(vec(-69.480052947998,114.33006286621,-38.449909210205) * Scale),vec(1) * Scale,entity():toWorld(ang(0,-90,90)))
    holoModel(11,"models/hunter/blocks/cube025x025x025.mdl")
    holoParent(11,9)
    
    State = "idle"
    Grab = 0
    
    # Rail = Moving along the rail
    # Rot = Rotation
    # A1P = Left/Right
    # A2P = Hight of hand
    # HP = Rotation of hand
}

if(changed(State)) {
    if(State == "idle") {
        Rail = 110
        Rot = -90
        A1P = -40
        A2P = -10
        HP = -130
    }
    
    if(State == "door") {
        Rail = 71
        Rot = -90
        A1P = -70
        A2P = -10
        HP = -100
    }
    
    if(State == "move_door") {
        Rail = 71
        Rot = -90
        A1P = -70
        A2P = -10
        HP = -100
    }
    
    if(State == "shipment_output_1") {
        Rail = -72
        Rot = -90
        A1P = -50
        A2P = -20
        HP = -110
    }
    
    if(State == "shipment_output_2") {
        Rail = -72
        Rot = -90
        A1P = -60
        A2P = 20
        HP = -140
    }
    
    if(State == "shipment_output_3") {
        Rail = -29
        Rot = -90
        A1P = -50
        A2P = -20
        HP = -110
    }
    
    if(State == "shipment_output_4") {
        Rail = -29
        Rot = -90
        A1P = -60
        A2P = 20
        HP = -140
    }
    
    if(State == "shipment_drop") {
        Rail = 31
        Rot = 90
        A1P = -40
        A2P = -10
        HP = -130
    }
    
    if(State == "shipment_pullout_1") {
        Grab = 1
        Rail = -72
        Rot = -90
        A1P = -10
        A2P = -20
        HP = -150
    }
    
    if(State == "shipment_pullout_1_1") {
        Rail = 29
        Rot = 90
        A1P = -10
        A2P = -20
        HP = -150
    }
    
    if(State == "shipment_pullout_1_2") {
        Rail = 29
        Rot = 90
        A1P = -30
        A2P = 24
        HP = -174
    }
    
    if(State == "shipment_pullout_1_3") {
        Rail = 29
        Rot = 90
        A1P = -60
        A2P = 24
        HP = -144
    }
}

if(CurRail != Rail) {
    if(CurRail < Rail) {CurRail += Speed}
    else {CurRail -= Speed}
    
    local A = CurRail - Rail
    if(A % 4 != 0) {CurRail += (A < 0 ? 1 : -1)}
}

if(CurRot != Rot) {
    if(CurRot < Rot) {CurRot += Speed}
    else {CurRot -= Speed}
    
    local A = CurRot - Rot
    if(A % 4 != 0) {CurRot += (A < 0 ? 1 : -1)}
}

if(CurA1P != A1P) {
    if(CurA1P < A1P) {CurA1P += Speed}
    else {CurA1P -= Speed}
    
    local A = CurA1P - A1P
    if(A % 4 != 0) {CurA1P += (A < 0 ? 1 : -1)}
}

if(CurA2P != A2P) {
    if(CurA2P < A2P) {CurA2P += Speed}
    else {CurA2P -= Speed}
    
    local A = CurA2P - A2P
    if(A % 4 != 0) {CurA2P += (A < 0 ? 1 : -1)}
}

if(CurHP != HP) {
    if(CurHP < HP) {CurHP += Speed}
    else {CurHP -= Speed}
    
    local A = CurHP - HP
    if(A % 4 != 0) {CurHP += (A < 0 ? 1 : -1)}
}

holoPos(6, entity():toWorld(vec(0, CurRail, 3)))
holoAng(6, entity():toWorld(ang(0, CurRot, 0)))
holoAng(7, holoEntity(6):toWorld(ang(0, 0, CurA1P)))
holoAng(9, holoEntity(7):toWorld(ang(0, 0, CurA2P)))
holoAng(11, holoEntity(9):toWorld(ang(0, 0, CurHP)))

Grabber:setPos(holoEntity(11):toWorld(vec(-1, 0, 0) * Scale))
Grabber:setAng(holoEntity(11):toWorld(ang(0, 0, 90)))

if (changed(owner():keyWalk()) * owner():keyWalk()) {
    State = "shipment_output_1"
    timer("pullout",5000)
}

if (clk("pullout")) {State = "shipment_pullout_1" timer("checkout_1",3000)}
if (clk("checkout_1")) {State = "shipment_pullout_1_1" timer("checkout_2",3000)}
if (clk("checkout_2")) {State = "shipment_pullout_1_2" timer("checkout_3",3000)}
if (clk("checkout_3")) {State = "shipment_pullout_1_3" timer("dropgun",1000)}
if (clk("dropgun")) {Use = 1}

