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

#include <vml_ui.d>

char[] digit(int d) {
    if (d == 0) { return "0"; }
    if (d == 1) { return "1"; }
    if (d == 2) { return "2"; }
    if (d == 3) { return "3"; }
    if (d == 4) { return "4"; }
    if (d == 5) { return "5"; }
    if (d == 6) { return "6"; }
    if (d == 7) { return "7"; }
    if (d == 8) { return "8"; }
    return "9";
}

void drawNum(int x, int y, int v, int col, int sz) {
    int p1000 = v / 1000;
    int p100 = (v / 100) % 10;
    int p10 = (v / 10) % 10;
    int p1 = v % 10;
    int cx = x;
    if (p1000 > 0) {
        ui_2d_text(cx, y, digit(p1000), col, sz, 0);
        cx = cx + sz / 2;
        ui_2d_text(cx, y, digit(p100), col, sz, 0);
        cx = cx + sz / 2;
    } else if (p100 > 0) {
        ui_2d_text(cx, y, digit(p100), col, sz, 0);
        cx = cx + sz / 2;
    }
    if (p1000 > 0 || p100 > 0 || p10 > 0) {
        ui_2d_text(cx, y, digit(p10), col, sz, 0);
        cx = cx + sz / 2;
    }
    ui_2d_text(cx, y, digit(p1), col, sz, 0);
}

bool isWall(int cx, int cy) {
    if (cx < 0 || cy < 0 || cx > 20 || cy > 20) { return true; }
    if (cy == 0 || cy == 20) { return false; }
    if (cy % 2 == 0) { return false; }
    int b = cy / 2;
    if (b % 2 == 0) {
        if (cx == 20) { return false; }
    } else {
        if (cx == 0) { return false; }
    }
    return true;
}

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

    int small = w;
    if (h < small) { small = h; }

    // 顶部步数 + 21×21 迷宫 + 底部提示，全部按画布现算。
    int stat_fs = small / 16;
    if (stat_fs < 14) { stat_fs = 14; }
    if (stat_fs > 40) { stat_fs = 40; }
    int label_fs = small / 30;
    if (label_fs < 9) { label_fs = 9; }
    if (label_fs > 16) { label_fs = 16; }
    int hint_fs = small / 26;
    if (hint_fs < 9) { hint_fs = 9; }
    if (hint_fs > 16) { hint_fs = 16; }

    int stat_y = stat_fs + 4;
    int label_y = stat_y + label_fs + 4;
    int top = label_y + 6;
    int pad = w / 60;
    if (pad < 4) { pad = 4; }
    if (pad > 20) { pad = 20; }
    int bottom_y = h - 8;
    int bottom_top = h - (hint_fs + 12);
    int avail_h = bottom_top - top;
    if (avail_h < 42) { avail_h = 42; }
    int cellw = w - 2 * pad;
    if (avail_h < cellw) { cellw = avail_h; }
    cellw = cellw / 21;
    if (cellw < 3) { cellw = 3; }
    int ox = (w - 21 * cellw) / 2;
    int oy = top + (avail_h - 21 * cellw) / 2;
    if (oy < top) { oy = top; }

    int px = 1;
    int py = 0;
    int won = 0;
    int steps = 0;
    int frames = 0;
    int tid = ui_timer_set(120, 0);

    while (frames < 12000) {
        k = ui_2d_clear(-15724520);
        int x = 0;
        while (x < 21) {
            int y = 0;
            while (y < 21) {
                if (isWall(x, y)) {
                    k = ui_2d_rect(ox + x * cellw, oy + y * cellw, cellw, cellw, -15066590, 1, 0, 0);
                } else {
                    k = ui_2d_rect(ox + x * cellw, oy + y * cellw, cellw, cellw, -14399438, 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, -2069424, 1, 0, 2);
        k = ui_2d_rect(ox + px * cellw + 1, oy + py * cellw + 1, cellw - 2, cellw - 2, -10044566, 1, 0, 2);
        drawNum(pad, stat_y, steps, -1513232, stat_fs);
        k = ui_2d_text(pad, label_y, "步数", -6643536, label_fs, 0);
        if (won == 1) {
            k = ui_2d_text(w / 2, bottom_y, "走出来了！点一下重开", -10044566, hint_fs, 1);
        } else {
            k = ui_2d_text(w / 2, bottom_y, "方向键 / 点屏幕四边", -6643536, hint_fs, 1);
        }
        k = ui_2d_present();

        int msg = ui_msg_fetch_wait(0);
        if (msg == 10) { frames = 12000; }
        if (msg == 1) {
            int key = ui_msg_get_a();
            if (key == 27) { px = 1; py = 0; won = 0; steps = 0; }
            int nx = px;
            int 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 (!isWall(nx, ny)) {
                        px = nx;
                        py = ny;
                        steps = steps + 1;
                        if (px == 19 && py == 20) { won = 1; }
                    }
                }
            }
        }
        if (msg == 6) {
            int ax = ui_msg_get_a();
            int ay = ui_msg_get_b();
            if (won == 1) {
                px = 1; py = 0; won = 0; steps = 0;
            } else {
                int dl = ax;
                int dr = w - ax;
                int dt = ay;
                int db = h - ay;
                int nx2 = px;
                int ny2 = py;
                if (dl <= dr && dl <= dt && dl <= db) { nx2 = px - 1; }
                if (dl > dr && dr <= dt && dr <= db) { nx2 = px + 1; }
                if (dl > dr && dr > dt && dt <= db) { ny2 = py - 1; }
                if (dl > dr && dr > dt && dt > db) { ny2 = py + 1; }
                if (!isWall(nx2, ny2)) {
                    px = nx2;
                    py = ny2;
                    steps = steps + 1;
                    if (px == 19 && 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();
}
