@name 3D - Scanner
@inputs Holo:wirelink
@outputs Acounter Scan
@persist Radius Pos:vector E:entity TE:entity ACounter Counter HoloCount SavedCounter Res ResZ SavedWhen Scan
@trigger 

if (first()) {
    runOnChat(1)
    Res = 25
    HoloCount = 5
    Holo["Active",number]=1
    Holo[2]=1
    Holo[3]=10
    Holo[4]=1500
}

if (chatClk()) {
    Said = lastSaid():lower()
    Spoke = lastSpoke()
    When = lastSaidWhen()
    if (Spoke == owner() & When != SavedWhen) {
        if (Said:sub(1,4) == "!res") {
            if (Res < 10) {
                Res = Said:sub(5,Said:length()):toNumber()
            } else {
                Res = 10
            }
        }
        
        if (Said == "!scan" & Spoke == owner()) {
            ACounter = 0
            HoloCount = 5
            TE = owner():aimEntity()
            E = entity()
            Pos = TE:pos()
            Radius = TE:boxMin():distance(TE:boxMax())/2+1
            Scan = 1
            timer("Scan",100)
        }
    }
    SavedWhen = When
}

if (Scan & clk("Scan")) {
    if (ACounter < Res) {
        ACounter++
        Counter = 0
        Alfa = 360*ACounter/Res
        for (Counter = 0,Res,1) {
            Beta = 360*Counter/Res
            X = Radius*cos(Beta)*cos(Alfa)
            Y = Radius*sin(Beta)
            Z = Radius*sin(Alfa)
            Vec = vec(X,Y,Z)
            RD = rangerOffset(Pos+Vec,Pos)
            if (RD:hit()) {
                Holo[HoloCount]=Holo:entity():toWorld(RD:entity():toLocal(RD:position())):x()
                Holo[HoloCount+1]=Holo:entity():toWorld(RD:entity():toLocal(RD:position())):y()
                Holo[HoloCount+2]=Holo:entity():toWorld(RD:entity():toLocal(RD:position())):z()+Radius+10
                HoloCount+= 3
            }
        }
    } else {
    Scan = 0
    }
    timer("Scan",100)
}
