#include <vml_ui.r>
# 迷宫 —— 用 **R** 写的手机小游戏（探索）
# Maze -- a small phone game written in **R** (exploration)
#
# 玩法：方向键 / 点屏幕四边移动，走到右下角红格即通关；ESC 重开。
# 公式化蛇形迷宫（偶数行整行通道、奇数行交替在左右端开口）。
#
# 跑法 —— 手机：vml run examples/r/maze.r
# 跑法 —— 桌面：vmlcli Examples/r/maze.r --screen 480x640 --frames /tmp/fr

c_bg <- -15724520
c_txt <- -1513232
c_dim <- -6643536
c_ok <- -11409298
c_wall <- -15066590
c_path <- -14399438
c_goal <- -2069424

is_wall <- function(cx, cy) {
    if (cx < 0) { return(1) }
    if (cy < 0) { return(1) }
    if (cx > 20) { return(1) }
    if (cy > 20) { return(1) }
    if (cy == 0) { return(0) }
    if (cy == 20) { return(0) }
    if (cy %% 2 == 0) { return(0) }
    b <- as.integer((cy) / 2)
    if (b %% 2 == 0) {
        if (cx == 20) { return(0) }
    } else {
        if (cx == 0) { return(0) }
    }
    return(1)
}

k <- ui_win_open("迷宫", 0, 0)
k <- ui_scr_set_keep_on(1)
w <- ui_scr_get_w()
h <- ui_scr_get_h()
if (w <= 0) { w <- 360 }
if (h <= 0) { h <- 620 }

# 布局按画布现算：字号随 min(W,H) 缩放，迷宫吃掉中间剩余高度。
mn <- w
if (h < mn) { mn <- h }
scn <- as.integer(mn * 100 / 360)
if (scn < 55) { scn <- 55 }
if (scn > 240) { scn <- 240 }
pad <- as.integer(mn / 20)
if (pad < 6) { pad <- 6 }
if (pad > 24) { pad <- 24 }
f_stat <- as.integer(18 * scn / 100)
if (f_stat < 8) { f_stat <- 8 }
f_small <- as.integer(11 * scn / 100)
if (f_small < 8) { f_small <- 8 }
stat_y <- pad + as.integer(f_stat / 2)
lab_y <- pad + f_stat + as.integer(f_small / 2)
gtop <- pad + f_stat + f_small + pad
avail <- h - gtop - f_small - pad
cellw <- as.integer((w - 2 * pad) / 21)
ch2 <- as.integer(avail / 21)
if (ch2 < cellw) { cellw <- ch2 }
if (cellw < 3) { cellw <- 3 }
ox <- as.integer((w - 21 * cellw) / 2)
oy <- gtop
hint_y <- h - pad - as.integer(f_small / 2)

px <- 1
py <- 0
won <- 0
steps <- 0
frames <- 0
tid <- ui_timer_set(120, 0)

while (frames < 12000) {
    k <- ui_2d_clear(c_bg)
    x <- 0
    while (x < 21) {
        y <- 0
        while (y < 21) {
            if (is_wall(x, y) == 1) {
                k <- ui_2d_rect(ox + x * cellw, oy + y * cellw, cellw, cellw, c_wall, 1, 0, 0)
            } else {
                k <- ui_2d_rect(ox + x * cellw, oy + y * cellw, cellw, cellw, c_path, 1, 0, 0)
            }
            y <- y + 1
        }
        x <- x + 1
    }
    k <- ui_2d_rect(ox + 19 * cellw + 1, oy + 20 * cellw + 1, cellw - 2, cellw - 2, c_goal, 1, 0, 2)
    k <- ui_2d_rect(ox + px * cellw + 1, oy + py * cellw + 1, cellw - 2, cellw - 2, c_ok, 1, 0, 2)
    k <- ui_2d_text(pad, stat_y, sprintf("%d", steps), c_txt, f_stat, 0)
    k <- ui_2d_text(pad, lab_y, "步数", c_dim, f_small, 0)
    if (won == 1) {
        k <- ui_2d_text(pad, hint_y, "走出来了！点一下重开", c_ok, f_small, 0)
    } else {
        k <- ui_2d_text(pad, hint_y, "方向键 / 点屏幕四边", c_dim, f_small, 0)
    }
    k <- ui_2d_present()

    msg <- ui_msg_fetch_wait(0)
    if (msg == 10) { frames <- 12000 }
    if (msg == 1) {
        key <- ui_msg_get_a()
        if (key == 27) { px <- 1; py <- 0; won <- 0; steps <- 0 }
        nx <- px
        ny <- py
        if (key == 37) { nx <- px - 1 }
        if (key == 39) { nx <- px + 1 }
        if (key == 38) { ny <- py - 1 }
        if (key == 40) { ny <- py + 1 }
        if (won == 0) {
            if (nx != px || ny != py) {
                if (is_wall(nx, ny) == 0) {
                    px <- nx
                    py <- ny
                    steps <- steps + 1
                    if (px == 19) { if (py == 20) { won <- 1 } }
                }
            }
        }
    }
    if (msg == 6) {
        ax <- ui_msg_get_a()
        ay <- ui_msg_get_b()
        if (won == 1) {
            px <- 1; py <- 0; won <- 0; steps <- 0
        } else {
            dl <- ax
            dr <- w - ax
            dt <- ay
            db <- h - ay
            nx <- px
            ny <- py
            if (dl <= dr) { if (dl <= dt) { if (dl <= db) { nx <- px - 1 } } }
            if (dl > dr) { if (dr <= dt) { if (dr <= db) { nx <- px + 1 } } }
            if (dl > dr) { if (dr > dt) { if (dt <= db) { ny <- py - 1 } } }
            if (dl > dr) { if (dr > dt) { if (dt > db) { ny <- py + 1 } } }
            if (is_wall(nx, ny) == 0) {
                px <- nx
                py <- ny
                steps <- steps + 1
                if (px == 19) { if (py == 20) { won <- 1 } }
            }
        }
    }
    if (msg == 9) { frames <- frames + 1 }
}

k <- ui_timer_kill(tid)
k <- ui_scr_set_keep_on(0)
k <- ui_win_close()
