#@name pacman4
@inputs 
@outputs [CurRow CurCol] PacMan [DirRow DirCol] Char:string
@persist Holo Entity:entity MazeTable:table [Row Col] [SizeW SizeH SizeC Height] [CurRow CurCol] PacMan [DirRow DirCol]
@trigger none

if (first()) {

    Maze = "
        # # # # # # # # # # # # # # # # # # # # # # #
        # . . . . . . . . . . # . . . . . . . . . . #
        # . # # # . # # # # . # . # # # # . # # # . #
        # . # # # . # # # # . # . # # # # . # # # . #
        # . . . . . . . . . . . . . . . . . . . . . #
        # . # # # . # . # # # # # # # . # . # # # . #
        # . . . . . # . # # # # # # # . # . . . . . #
        # # # # # . # . . . . # . . . . # . # # # # #
        # # # # # . # # # # - # - # # # # . # # # # #
        # # # # # . # - - - - - - - - - # . # # # # #
        # # # # # . # - # # # - # # # - # . # # # # #
        T - - - - . - - # M M - M M # - - . - - - - T
        # # # # # . # - # # # # # # # - # . # # # # #
        # # # # # . # - - - - - - - - - # . # # # # #
        # # # # # . # - # # # # # # # - # . # # # # #
        # # # # # . # - # # # # # # # - # . # # # # #
        # . . . . . . . . . . # . . . . . . . . . . #
        # . # # # . # # # # . # . # # # # . # # # . #
        # . . . # . . . . . . P . . . . . . # . . . #
        # # # . # . # . # # # # # # # . # . # . # # #
        # . . . . . # . . . . # . . . . # . . . . . #
        # . # # # # # # # # . # . # # # # # # # # . #
        # . # # # # # # # # . # . # # # # # # # # . #
        # . . . . . . . . . . . . . . . . . . . . . #
        # # # # # # # # # # # # # # # # # # # # # # #
        ":replaceRE("%s+", "")

    MazeTable = table()
    for (I = 0, 24) {
        local Sub = Maze:sub(I*23 + 1, I*23 + 23)
        MazeTable[I + 1, array] = Sub:explode("")
    }
    
    function number holoCreateFromTopLeft( Pos:vector, Scale:vector, Color:vector4 ) {
        Holo++
        
        holoCreate(Holo)
        holoParent(Holo, Entity)
        holoScale(Holo, Scale/12)
        holoPos(Holo, Entity:toWorld( Pos - (Scale/2):setZ(0) ))
        holoAng(Holo, Entity:angles())
        holoColor(Holo, Color)
        #holoMaterial(Holo, "models/cheeze/wires/metal_texture")
        holoDisableShading(Holo, 1)
     
        return Holo
    }
    
    Entity = entity()
    
    SizeW = 23
    SizeH = 25
    SizeC = 2
    Height = 1
    
    WallColor = vec4(0, 50, 225, 255)
    
    #-- Background
    holoCreateFromTopLeft(vec(0, 0, -1), vec(SizeH*SizeC, SizeW*SizeC, 1), vec4(30,30,30,255))
    
    #-- Exterior walls
    holoCreateFromTopLeft(vec(0, 0, 0), vec(SizeC, SizeW*SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-(SizeH - 1)*SizeC, 0, 0), vec(SizeC, SizeW*SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-SizeC, 0, 0), vec(10*SizeC, SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-12*SizeC, 0, 0), vec(12*SizeC, SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-SizeC, -(SizeW - 1)*SizeC, 0), vec(10*SizeC, SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-12*SizeC, -(SizeW - 1)*SizeC, 0), vec(12*SizeC, SizeC, Height), WallColor)
    
    #-- Interior walls
    holoCreateFromTopLeft(vec(-7*SizeC, -SizeC, 0), vec(4*SizeC, 4*SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-7*SizeC, -(23 - 5)*SizeC, 0), vec(4*SizeC, 4*SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-12*SizeC, -SizeC, 0), vec(4*SizeC, 4*SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-12*SizeC, -(23 - 5)*SizeC, 0), vec(4*SizeC, 4*SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-2*SizeC, -2*SizeC, 0), vec(2*SizeC, 3*SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-2*SizeC, -(22 - 4)*SizeC, 0), vec(2*SizeC, 3*SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-2*SizeC, -6*SizeC, 0), vec(2*SizeC, 4*SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-2*SizeC, -(22 - 9)*SizeC, 0), vec(2*SizeC, 4*SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-5*SizeC, -2*SizeC, 0), vec(SizeC, 3*SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-5*SizeC, -(22 - 4)*SizeC, 0), vec(SizeC, 3*SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-SizeC, -11*SizeC, 0), vec(3*SizeC, SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-5*SizeC, -6*SizeC, 0), vec(6*SizeC, SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-5*SizeC, -16*SizeC, 0), vec(6*SizeC, SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-8*SizeC, -7*SizeC, 0), vec(SizeC, 3*SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-8*SizeC, -13*SizeC, 0), vec(SizeC, 3*SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-5*SizeC, -8*SizeC, 0), vec(2*SizeC, 7*SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-7*SizeC, -11*SizeC, 0), vec(2*SizeC, SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-10*SizeC, -8*SizeC, 0), vec(SizeC, 3*SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-10*SizeC, -12*SizeC, 0), vec(SizeC, 3*SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-11*SizeC, -8*SizeC, 0), vec(SizeC, SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-11*SizeC, -14*SizeC, 0), vec(SizeC, SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-12*SizeC, -8*SizeC, 0), vec(SizeC, 7*SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-12*SizeC, -6*SizeC, 0), vec(4*SizeC, SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-12*SizeC, -16*SizeC, 0), vec(4*SizeC, SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-14*SizeC, -8*SizeC, 0), vec(2*SizeC, 7*SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-16*SizeC, -11*SizeC, 0), vec(2*SizeC, SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-17*SizeC, -2*SizeC, 0), vec(SizeC, 3*SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-18*SizeC, -4*SizeC, 0), vec(2*SizeC, SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-17*SizeC, -6*SizeC, 0), vec(SizeC, 4*SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-19*SizeC, -SizeC, 0), vec(SizeC, 2*SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-17*SizeC, -18*SizeC, 0), vec(SizeC, 3*SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-18*SizeC, -18*SizeC, 0), vec(2*SizeC, SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-17*SizeC, -13*SizeC, 0), vec(SizeC, 4*SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-19*SizeC, -20*SizeC, 0), vec(SizeC, 2*SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-21*SizeC, -2*SizeC, 0), vec(2*SizeC, 8*SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-19*SizeC, -6*SizeC, 0), vec(2*SizeC, SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-21*SizeC, -13*SizeC, 0), vec(2*SizeC, 8*SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-19*SizeC, -16*SizeC, 0), vec(2*SizeC, SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-19*SizeC, -8*SizeC, 0), vec(SizeC, 7*SizeC, Height), WallColor)
    holoCreateFromTopLeft(vec(-20*SizeC, -11*SizeC, 0), vec(3*SizeC, SizeC, Height), WallColor)

    Holo++
    holoCreate(Holo)
    holoScale(Holo, vec(sqrt(SizeC + 1)/12))
    holoParent(Holo, Entity)
    holoModel(Holo, "hq_sphere")
    holoDisableShading(Holo, 1)
    holoMaterial(Holo, "models/cheeze/wires/metal_texture")  
    holoColor(Holo, vec(255, 255, 0)) 
    
    PacMan = Holo 
    
    DirRow = 0
    DirCol = 0
    interval(15)
}


if (Row < 25) {
    interval(15)
    
    while (holoCanCreate() & Col < 23) {
        Col++
        
        local Char = MazeTable[Row, array][Col, string]
        if (Char == ".") {
            
#[            Holo++
            holoCreate(Holo)
            holoScale(Holo, vec(sqrt(SizeC - 1)/12))
            holoParent(Holo, Entity)
            holoPos(Holo, Entity:toWorld(vec(-(Row - 0.5)*SizeC , -(Col - 0.5)*SizeC, 0)))
            holoModel(Holo, "sphere")
            holoDisableShading(Holo, 1)
            holoMaterial(Holo, "models/cheeze/wires/metal_texture")   ]#
        }
        if (Char == "P") {
            holoPos(PacMan, Entity:toWorld(vec(-(Row - 0.5)*SizeC , -(Col - 0.5)*SizeC, 0)))
            CurRow = Row
            CurCol = Col
        }
    }
    
    if (Col >= 23) {
        Row++
        Col = 0
    }
    
    if (Row >= 25) {
        print(Holo)    
    }
}
else {
    interval(90)
    
    local NS = owner():keyPressed("down") - owner():keyPressed("up") 
    local EW = owner():keyPressed("right") - owner():keyPressed("left")
    
    DirRow = NS
    DirCol = EW
    #DirRow = (changed(NS) & NS) ? NS : DirRow
    #DirCol = (changed(EW) & EW) ? EW : DirCol
    
    #if (changed(DirRow) & DirRow) { DirCol = 0 }
    #if (changed(DirCol) & DirCol) { DirRow = 0 }
    
    #if (DirRow) { DirCol = 0 }
    #if (DirCol) { DirRow = 0 }
    
    local NextRow = CurRow + DirRow
    local NextCol = CurCol + DirCol
    
    if (DirCol) {
        local Char = MazeTable[CurRow, array][CurCol + DirCol, string]
        if (Char != "#") {
            CurCol += DirCol  
            DirRow = 0  
        }
    }
    if (DirRow) {
        local Char = MazeTable[CurRow + DirRow, array][CurCol, string]
        if (Char != "#") {
            CurRow += DirRow   
            DirCol = 0 
        }
    }

    holoPos(PacMan, Entity:toWorld(vec(-(CurRow - 0.5)*SizeC , -(CurCol - 0.5)*SizeC, 0)))
}
