← 都来码 / Dolaima← 例程索引swift/app/pocketmon.swift▶ 在浏览器里试玩 原文

swift/app/pocketmon.swift · 32469 字节 · 下载原文

▶ 在浏览器里试玩

// 口袋妖怪风格对战 —— 用 **Swift** 写的手机小游戏
// A Pokémon-style battle — a small phone game written in **Swift**
//
// ⚠ 本示例是**玩法上的致敬**:三系(草 / 火 / 水)克制、四招选一、回合制对战。
// ⚠ This sample is a **gameplay homage**: three types (grass / fire / water) with a type chart,
//   但**不包含任何原作的角色名、形象或素材** —— 里面的三只小兽(叶芽兽 / 焰尾兽 / 水泡兽)
//   four moves to pick from, turn-based combat. It contains **no original character names, art or
//   assets** — the three little creatures here (Leafbud / Embertail / Bubblepaw) are drawn
//  全部由圆形、线条现场画出来,名字也是本文件自己起的。
//   from circles and lines at run time, and the names are made up by this file.
//
// 玩法:开局选一只小兽 → 遇到野生小兽 → 每回合从四个招式里选一个 → 打赢继续下一场,
// Gameplay: pick a creature -> meet a wild one -> each turn choose one of four moves -> win to go on,
// 打输回到选择界面。属性克制:火 → 草 → 水 → 火(克制 1.5 倍、被克 0.7 倍)。
// lose to return to the chooser. Type chart: fire > grass > water > fire (1.5x when strong, 0.7x when weak).
//
// ◆ 这个示例的重点:**回合状态机 + 数值公式 + 无素材的角色绘制**
// ◆ What this sample focuses on: **a turn state machine + a damage formula + asset-free characters**
//
//   ① 回合不是"点了就立刻出结果",而是 `phase` 一步步走:选招 → 我方动作(14 拍)→
//   ① A turn is not "click and the result appears": `phase` advances step by step —
//      敌方动作(14 拍)→ 回到选招。定时器只负责推进这几拍,所有判定都在 `battleTick` 一处。
//      choose -> player action (14 ticks) -> enemy action (14 ticks) -> back to choose. The timer
//      only advances those ticks; every decision lives in `battleTick`.
//
//   ② 伤害公式、克制倍数都是**整数运算**(`eff` 返回 15 / 10 / 7,最后除以 10),
//   ② The damage formula and the type multiplier are **integer-only** (`eff` returns 15 / 10 / 7
//      结果完全可复现;demo 模式就是靠这一点断言「克制 > 普通 > 被克」。
//      and the result is divided by 10), so it is fully reproducible; the demo mode asserts
//      "strong > neutral > weak" on exactly that.
//
//   ③ 角色完全**程序化绘制**:身体是圆、眼睛是圆、草系头顶两片叶子、火系三簇火苗、
//   ③ The characters are fully **procedural**: a circular body, circular eyes, two leaves for grass,
//      水系两片鳍 —— 没有任何图片资源要随包发。
//      three flame puffs for fire, two fins for water — no image assets ship with this file.
//
// ◆ 这个前端的写法约定(实测)
// ◆ Conventions of this front end (measured)
//
//   · 状态放在**一个**全局数组 `B` 里(历史上的"多全局数组互相踩"已修,这里仍保持单数组,
//   · State lives in **one** global array `B` (the old "several global arrays clobber each other"
//     因为数组下标分区比散落的标量更容易核对)。
//     bug is fixed, but a single array keeps the slot map easy to check).
//   · 字符串 `+` 可用;`println` 的字符串版本自带换行。
//   · string `+` works; the string version of `println` appends its own newline.
//   · 函数名避开 `add`/`min`/`max`/`abs` 这类会被链接进来的库版本顶掉的名字。
//   · function names avoid `add`/`min`/`max`/`abs`, which the linked library versions would take over.
//
// 跑法:手机 `vml run examples/swift/app/pocketmon.swift`;
// How to run: phone `vml run examples/swift/app/pocketmon.swift`;
//       桌面 `vmlcli Examples/swift/app/pocketmon.swift --screen 411x726 --frames /tmp/fr`
//       desktop `vmlcli Examples/swift/app/pocketmon.swift --screen 411x726 --frames /tmp/fr`
//       `--arg demo` 是无头验收模式:固定数值断言 + 一场固定对局 + 打印判据 + 自己退出。
//       `--arg demo` is the headless acceptance mode: fixed numeric assertions + one fixed battle +
//       printed checkpoints + self-exit.
//       ✅ 标准库 include **自动找**(2026-10-25 用户定):`#include <vml_ui.swift>` 即可,
//       ✅ The standard-library include is **auto-found** (user rule, 2026-10-25): just write
//         `#include <vml_ui.swift>`; `Lib/swift/` 与 `Lib/shared/` 由编译器自动搜索(手机上
//         `#include <vml_ui.swift>`; the compiler searches `Lib/swift/` and `Lib/shared/`
//         (示例不在 VML 树里也一样)。见 E339 与 `scripts/vml-include-lib-probe`。
//         itself, so it also works on devices. See E339 and `scripts/vml-include-lib-probe`.

#include <vml_ui.swift>

// 状态表:一只小兽占 8 格(本文件只用前 7 格),玩家 0..6、对手 8..14,界面状态 16 起。
// The state table: one creature takes 8 slots (this file uses 7), player at 0..6, foe at 8..14,
// and the UI state from 16 on.
var B = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
// 0 属性(0 草/1 火/2 水) 1 等级 2 当前HP 3 最大HP 4 攻击 5 防御 6 速度
// 0 type (0 grass / 1 fire / 2 water); 1 level; 2 HP; 3 max HP; 4 atk; 5 def; 6 speed
// 8..14 同上(对手)
// 8..14 the same for the foe
// 16 阶段 17 消息码 18 等待拍数 19 谁在动 20 连胜 21 帧上限 22 已跑拍数 23 语言
// 16 phase; 17 message code; 18 wait ticks; 19 who is acting; 20 streak; 21 frame limit; 22 frames; 23 language
// 24 屏宽 25 屏高 26 demo 标记 27 消息用的招式号
// 24 width; 25 height; 26 demo flag; 27 the move index used by the current message

// 阶段:0 选招 1 我方动作 2 敌方动作 3 战斗结束(留给后续) 4 选小兽 5 胜利 6 失败
// Phases: 0 choose a move; 1 player action; 2 foe action; 3 battle over (reserved); 4 choose a
// creature; 5 victory; 6 defeat
var gW = 0
var gH = 0

func t(zh: String, en: String) -> String {
    if B[23] == 0 { return zh }
    return en
}

func numToStr(v: Int) -> String {
    if v == 0 { return "0" }
    var s = ""
    var n = v
    if n < 0 { n = 0 - n }
    while n > 0 {
        var d = n % 10
        if d == 0 { s = "0" + s }
        else if d == 1 { s = "1" + s }
        else if d == 2 { s = "2" + s }
        else if d == 3 { s = "3" + s }
        else if d == 4 { s = "4" + s }
        else if d == 5 { s = "5" + s }
        else if d == 6 { s = "6" + s }
        else if d == 7 { s = "7" + s }
        else if d == 8 { s = "8" + s }
        else { s = "9" + s }
        n = n / 10
    }
    if v < 0 { s = "-" + s }
    return s
}

// ── 版面度量:**全部由当前画布算**,240x240 与 1024x1024 用同一套公式 ──────────
// ── Layout metrics: **everything from the current canvas**, the same formulas on 240x240 and 1024x1024 ──
func pmPad() -> Int {
    var p = gW / 40
    if p < 6 { p = 6 }
    if p > 16 { p = 16 }
    return p
}

func pmTitleSize() -> Int {
    var s = gH / 30
    if s < 16 { s = 16 }
    if s > 28 { s = 28 }
    return s
}

func pmSubSize() -> Int {
    var s = gH / 60
    if s < 9 { s = 9 }
    if s > 13 { s = 13 }
    return s
}

// 选择界面:三张卡的宽/位/高
// Chooser: width / position / height of the three cards
func pmCardW() -> Int {
    var cw = (gW - pmPad() * 4) / 3
    if cw < 60 { cw = 60 }
    if cw > 200 { cw = 200 }
    return cw
}

func pmCardX(i: Int) -> Int {
    var rowW = pmCardW() * 3 + pmPad() * 2
    return (gW - rowW) / 2 + i * (pmCardW() + pmPad())
}

func pmCardY() -> Int {
    // ui_2d_text 的 y 是文字底边 ⇒ 标题/副标题各留出自己的字高
    // ui_2d_text's y is the text bottom, so the title and subtitle each add their own size
    var titleBottom = pmPad() + pmTitleSize()
    var subBottom = titleBottom + 6 + pmSubSize()
    return subBottom + pmPad()
}

func pmCardH() -> Int {
    var ch = gH - pmCardY() - pmPad() * 3
    var cap = pmCardW() * 12 / 5
    if cap < ch { ch = cap }
    if ch < 80 { ch = 80 }
    return ch
}

// 战斗界面:信息框 / 小兽半径
// Battle screen: info box / creature radius
func pmInfoW() -> Int {
    var iw = gW * 44 / 100
    if iw < 110 { iw = 110 }
    var maxW = 180
    // 大画布上别让信息框缩成邮票:1024 上允许到 280 宽
    // Do not let the info box shrink to a stamp on a big canvas: allow up to 280px wide at 1024
    if gW >= 900 { maxW = 280 }
    if iw > maxW { iw = maxW }
    return iw
}

func pmInfoH() -> Int {
    var ih = gH / 10
    if ih < 44 { ih = 44 }
    if ih > 74 { ih = 74 }
    return ih
}

func pmCreatureR() -> Int {
    var r = gW
    if gH < r { r = gH }
    r = r / 14
    if r < 16 { r = 16 }
    if r > 60 { r = 60 }
    return r
}

// ── 名字 / 配色 / 招式表 ──
// ── Names / palette / move table ──
func monName(ty: Int) -> String {
    if ty == 0 { return t(zh: "叶芽兽", en: "Leafbud") }
    if ty == 1 { return t(zh: "焰尾兽", en: "Embertail") }
    return t(zh: "水泡兽", en: "Bubblepaw")
}

func typeName(ty: Int) -> String {
    if ty == 0 { return t(zh: "草", en: "Grass") }
    if ty == 1 { return t(zh: "火", en: "Fire") }
    if ty == 2 { return t(zh: "水", en: "Water") }
    return t(zh: "普通", en: "Normal")
}

// 招式名:每系三个攻击招(最后一个是对应属性的大招)+ 一个回复招
// Move names: three attacks per type (the last is that type's big hit) plus one heal
func moveName(ty: Int, i: Int) -> String {
    if i == 0 { return t(zh: "撞击", en: "Tackle") }
    if i == 3 { return t(zh: "休息", en: "Rest") }
    if ty == 0 {
        if i == 1 { return t(zh: "藤鞭", en: "Vine Whip") }
        return t(zh: "飞叶快刀", en: "Leaf Blade")     // power 55
        // power 55
    }
    if ty == 1 {
        if i == 1 { return t(zh: "火花", en: "Ember") }
        return t(zh: "火焰轮", en: "Flame Wheel")      // power 60
        // power 60
    }
    if i == 1 { return t(zh: "水枪", en: "Water Gun") }
    return t(zh: "泡沫光线", en: "Bubble Beam")        // power 55
    // power 55
}

// 招式的攻击属性:撞击是普通,其余跟着自身属性;休息不算属性(-1)
// A move's attacking type: Tackle is Normal, the rest follow the creature's own type,
// and Rest has none (-1)
func moveType(ty: Int, i: Int) -> Int {
    if i == 0 { return 3 }
    if i == 3 { return 0 - 1 }
    return ty
}

func movePower(ty: Int, i: Int) -> Int {
    if i == 0 { return 35 }
    if i == 3 { return 30 }        // 回复量
    // the heal amount
    if i == 1 { return 45 }
    if ty == 1 { return 60 }
    return 55
}

// 属性克制:返回 15 = 克制 1.5 倍、10 = 普通、7 = 被克 0.7 倍(都是 ×10 的整数)
// Type chart: returns 15 = strong (1.5x), 10 = neutral, 7 = weak (0.7x), all x10 integers
func eff(atkTy: Int, defTy: Int) -> Int {
    if atkTy == 3 { return 10 }                 // 普通招不克不弱
    // Normal moves are neutral
    if atkTy == 1 && defTy == 0 { return 15 }   // 火 → 草
    // fire -> grass
    if atkTy == 0 && defTy == 2 { return 15 }   // 草 → 水
    // grass -> water
    if atkTy == 2 && defTy == 1 { return 15 }   // 水 → 火
    // water -> fire
    if atkTy == 0 && defTy == 1 { return 7 }    // 火克草
    // fire resists grass
    if atkTy == 2 && defTy == 0 { return 7 }    // 草克水
    // grass resists water
    if atkTy == 1 && defTy == 2 { return 7 }    // 水克火
    // water resists fire
    return 10
}

// 伤害:整数公式(等级不参与,免得数字乱飘);克制按 ×10 的倍数最后除
// Damage: integer formula (level is not part of it, so the numbers stay stable); the x10
// multiplier is applied last
func calcDamage(power: Int, atk: Int, def: Int, el: Int) -> Int {
    var d = power * atk / (def + 8) / 2 + 2
    d = d * el / 10
    if d < 1 { d = 1 }
    return d
}

func isAlive(which: Int) -> Int {
    if which == 0 { if B[2] > 0 { return 1 } else { return 0 } }
    if B[10] > 0 { return 1 }
    return 0
}

func hpOf(which: Int) -> Int {
    if which == 0 { return B[2] }
    return B[10]
}

func maxHpOf(which: Int) -> Int {
    if which == 0 { return B[3] }
    return B[11]
}

func typeOf(which: Int) -> Int {
    if which == 0 { return B[0] }
    return B[8]
}

// 用一招(who:0 我方 / 1 敌方);返回打出的伤害或回复量(用于 demo 判据)
// Use one move (who: 0 = player / 1 = foe); returns the damage or healing done (for the demo checks)
func useMove(who: Int, mi: Int) -> Int {
    var base = 0
    if who == 1 { base = 8 }
    var ty = B[base]
    var atk = B[base + 4]
    var mty = moveType(ty: ty, i: mi)
    var pow = movePower(ty: ty, i: mi)

    if mi == 3 {
        // 回复:不超过最大 HP
        // Heal: never above max HP
        var hp = B[base + 2]
        var mx = B[base + 3]
        var before = hp
        hp = hp + pow
        if hp > mx { hp = mx }
        B[base + 2] = hp
        B[19] = who
        B[27] = mi
        return hp - before
    }

    // 目标
    // The target
    var tbase = 8
    if who == 1 { tbase = 0 }
    var dty = B[tbase]
    var el = eff(atkTy: mty, defTy: dty)
    var dmg = calcDamage(power: pow, atk: atk, def: B[tbase + 5], el: el)
    var thp = B[tbase + 2] - dmg
    if thp < 0 { thp = 0 }
    B[tbase + 2] = thp
    B[19] = who
    B[27] = mi
    // 消息码 1 = 普通 / 2 = 克制 / 3 = 被克
    // message code 1 = neutral / 2 = strong / 3 = weak
    if el > 10 { B[17] = 2 }
    else if el < 10 { B[17] = 3 }
    else { B[17] = 1 }
    return dmg
}

// ── 小兽 / 战斗 ──
// ── Creatures / battle ──
func makeMon(which: Int, ty: Int, level: Int) {
    var base = 0
    if which == 1 { base = 8 }
    B[base] = ty
    B[base + 1] = level
    B[base + 3] = 22 + level * 4
    B[base + 2] = B[base + 3]
    B[base + 4] = 8 + level * 2
    B[base + 5] = 5 + level
    B[base + 6] = 5 + level
}

func spawnFoe() {
    var lvl = 3 + B[20]
    if lvl > 12 { lvl = 12 }
    var ty = ui_math_rand(3)
    makeMon(which: 1, ty: ty, level: lvl)
}

func chooseStarter(ty: Int) {
    makeMon(which: 0, ty: ty, level: 5)
    B[20] = 0
    spawnFoe()
    B[16] = 0
    B[17] = 0
    B[18] = 0
    B[19] = 0
}

func chooseMove(mi: Int) {
    if B[16] != 0 { return }
    useMove(who: 0, mi: mi)
    B[16] = 1
    B[18] = 14
    ui_audio_beep(520, 40)
}

func foeAct() {
    var mi = 0
    if B[26] == 0 {
        // 血少时偶尔休息,其余随机挑攻击招
        // Rest occasionally when low, otherwise pick an attacking move at random
        if B[10] * 3 < B[11] && ui_math_rand(2) == 0 {
            mi = 3
        } else {
            mi = ui_math_rand(3)
        }
    }
    useMove(who: 1, mi: mi)
    ui_audio_beep(330, 45)
}

// 一拍:只在"等待"阶段推进;所有阶段迁移都收在这一处
// One tick: only the waiting phases advance; every phase transition lives here
func battleTick() {
    if B[16] == 1 {
        if B[18] > 0 {
            B[18] = B[18] - 1
            return
        }
        if B[10] <= 0 {
            B[16] = 5
            B[20] = B[20] + 1
            // 赢一场回一点血(不超上限)
            // Winning restores a third of max HP (capped)
            B[2] = B[2] + B[3] / 3
            if B[2] > B[3] { B[2] = B[3] }
            ui_audio_beep(1047, 220)
            return
        }
        foeAct()
        B[16] = 2
        B[18] = 14
        return
    }
    if B[16] == 2 {
        if B[18] > 0 {
            B[18] = B[18] - 1
            return
        }
        if B[2] <= 0 {
            B[16] = 6
            ui_audio_beep(131, 320)
            return
        }
        B[16] = 0
        return
    }
}

// ── 绘制 ──
// ── Drawing ──
func typeColor(ty: Int) -> Int {
    if ty == 0 { return -10640566 }      // 0xFF5DA34A 草绿
    // grass green
    if ty == 1 { return -2077901 }       // 0xFFE04B33 火红
    // fire red
    if ty == 2 { return -11625760 }      // 0xFF4E9AE0 水蓝
    // water blue
    return -6643536
}

func drawCreature(cx: Int, cy: Int, ty: Int, hurt: Int, r: Int) {
    var body = typeColor(ty: ty)
    if hurt != 0 { body = -1 }
    var belly = r * 3 / 5
    ui_2d_circle(cx, cy, r, body, 1, 0)
    ui_2d_circle(cx, cy + r / 4, belly, -867718, 1, 0)   // 肚皮
    // belly
    var ew = r / 5
    if ew < 2 { ew = 2 }
    var ex = r * 2 / 5
    var ey = r / 4
    ui_2d_circle(cx - ex, cy - ey, ew, -1, 1, 0)         // 眼白
    // eye whites
    ui_2d_circle(cx + ex, cy - ey, ew, -1, 1, 0)
    var pr = r / 10
    if pr < 1 { pr = 1 }
    ui_2d_circle(cx - ex, cy - ey, pr, -15066598, 1, 0)  // 瞳孔
    // pupils
    ui_2d_circle(cx + ex + pr / 2, cy - ey, pr, -15066598, 1, 0)
    if ty == 0 {
        // 两片叶子
        // two leaves
        var lr = r / 3
        if lr < 5 { lr = 5 }
        ui_2d_circle(cx - r * 2 / 5, cy - r, lr, -8405171, 1, 0)
        ui_2d_circle(cx + r * 2 / 5, cy - r, lr, -8405171, 1, 0)
    } else if ty == 1 {
        // 三簇火苗
        // three flame puffs
        var lr = r / 4
        if lr < 4 { lr = 4 }
        ui_2d_circle(cx - r * 2 / 5, cy - r - r / 10, lr, -20448, 1, 0)
        ui_2d_circle(cx, cy - r - r / 5, lr, -20448, 1, 0)
        ui_2d_circle(cx + r * 2 / 5, cy - r - r / 10, lr, -20448, 1, 0)
    } else {
        // 两片鳍
        // two fins
        var lr = r / 3
        if lr < 5 { lr = 5 }
        ui_2d_circle(cx - r, cy, lr, -6365968, 1, 0)
        ui_2d_circle(cx + r, cy, lr, -6365968, 1, 0)
    }
}

func drawBar(x: Int, y: Int, w: Int, hp: Int, mx: Int, color: Int) {
    ui_2d_rect(x, y, w, 12, -15066598, 1, 0, 6)
    var fw = 0
    if mx > 0 { fw = w * hp / mx }
    if fw < 0 { fw = 0 }
    ui_2d_rect(x + 1, y + 1, fw, 10, color, 1, 0, 5)
}

// 底部四个招式按钮的矩形(2×2);返回 x/y/w/h 由调用方按下标取
// The four move buttons at the bottom (2×2); the caller reads x/y/w/h by index
func btnH() -> Int {
    var bh = gH / 8
    if bh < 26 { bh = 26 }
    if bh > 56 { bh = 56 }
    return bh
}

// 两行按钮整体贴着屏幕底部,返回第一行按钮的上沿
// The two button rows hug the bottom of the screen; returns the top edge of the first row
func pmBtnTop() -> Int {
    return gH - pmPad() - (btnH() * 2 + pmPad())
}

func btnW() -> Int {
    return (gW - pmPad() * 3) / 2
}

func btnX(i: Int) -> Int {
    if i % 2 == 0 { return pmPad() }
    return pmPad() * 2 + btnW()
}

func btnY(i: Int) -> Int {
    if i < 2 { return pmBtnTop() }
    return pmBtnTop() + btnH() + pmPad()
}

func drawMoveButtons() {
    var ty = B[0]
    var i = 0
    while i < 4 {
        var x = btnX(i: i)
        var y = btnY(i: i)
        var w = btnW()
        var h = btnH()
        var mt = moveType(ty: ty, i: i)
        var c = -6643536
        if mt >= 0 { c = typeColor(ty: mt) }
        var nameSize = h / 3
        if nameSize < 9 { nameSize = 9 }
        if nameSize > 15 { nameSize = 15 }
        var pwSize = h / 5
        if pwSize < 7 { pwSize = 7 }
        if pwSize > 11 { pwSize = 11 }
        var tx = x + w / 10
        if tx - x < 6 { tx = x + 6 }
        ui_2d_rect(x, y, w, h, c, 1, 0, 10)
        // ui_2d_text 的 y 是底边:名字占按钮上部 38%,威力占下部 82%
        // ui_2d_text's y is the bottom: the name sits at 38% of the button, the power at 82%
        ui_2d_text(tx, y + h * 38 / 100, moveName(ty: ty, i: i), -1, nameSize, VML_ANCHOR_LEFT)
        ui_2d_text(tx, y + h * 82 / 100, t(zh: "威力 ", en: "PWR ") + numToStr(v: movePower(ty: ty, i: i)), -1513232, pwSize, VML_ANCHOR_LEFT)
        i = i + 1
    }
}

func drawMessage() {
    var pad = pmPad()
    var x = pad
    var y = pmBtnTop()
    var w = gW - pad * 2
    var h = btnH() * 2 + pmPad()
    ui_2d_rect(x, y, w, h, -14472648, 1, 0, 10)
    var who = B[19]
    var name = monName(ty: B[0])
    if who == 1 { name = monName(ty: B[8]) }
    var mi = B[27]
    var ty = B[0]
    if who == 1 { ty = B[8] }
    var line = name + t(zh: " 使用了 ", en: " used ") + moveName(ty: ty, i: mi)
    if B[17] == 2 { line = line + t(zh: "!效果拔群!", en: "! It's super effective!") }
    else if B[17] == 3 { line = line + t(zh: "……效果一般。", en: "...not very effective.") }
    if mi == 3 { line = name + t(zh: " 休息了一下。", en: " took a rest.") }
    var mSize = gW / 32
    if mSize < 8 { mSize = 8 }
    if mSize > 13 { mSize = 13 }
    ui_2d_text(x + 8, y + h * 65 / 100, line, -1513232, mSize, VML_ANCHOR_LEFT)
}

func draw() {
    ui_2d_clear(-9517336)
    ui_2d_rect(0, gH / 2, gW, gH / 2, -6365968, 1, 0, 0)
    ui_2d_rect(0, gH * 3 / 5, gW, gH * 2 / 5, -10640566, 1, 0, 0)

    if B[16] == 4 {
        // 选择界面:三张卡横排,宽高与字号都按画布算 —— 240x240 上每张 72px 宽也点得中
        // The chooser: three cards in a row, with size and fonts from the canvas —
        // even at 72px wide on 240x240 each card is still tappable.
        var cPad = pmPad()
        var cT = pmTitleSize()
        var cS = pmSubSize()
        var cTitleBottom = cPad + cT
        var cW = pmCardW()
        var cH = pmCardH()
        var cY = pmCardY()
        var cR = cW / 4
        if cR < 14 { cR = 14 }
        if cR > 45 { cR = 45 }
        var cName = cW / 6
        if cName < 10 { cName = 10 }
        if cName > 17 { cName = 17 }
        var cKind = cW / 8
        if cKind < 9 { cKind = 9 }
        if cKind > 14 { cKind = 14 }
        var cTap = cW / 10
        if cTap < 8 { cTap = 8 }
        if cTap > 11 { cTap = 11 }
        ui_2d_text(gW / 2, cTitleBottom, t(zh: "选择你的伙伴", en: "Choose your partner"), -1, cT, VML_ANCHOR_CENTER)
        ui_2d_text(gW / 2, cTitleBottom + 6 + cS, t(zh: "三系互相克制:火 > 草 > 水 > 火", en: "Type chart: Fire > Grass > Water > Fire"), -6643536, cS, VML_ANCHOR_CENTER)
        var i = 0
        while i < 3 {
            var x = pmCardX(i: i)
            ui_2d_rect(x, cY, cW, cH, -14472648, 1, 0, 14)
            drawCreature(cx: x + cW / 2, cy: cY + cH * 3 / 10, ty: i, hurt: 0, r: cR)
            ui_2d_text(x + cW / 2, cY + cH * 62 / 100, monName(ty: i), -1, cName, VML_ANCHOR_CENTER)
            ui_2d_text(x + cW / 2, cY + cH * 78 / 100, typeName(ty: i), typeColor(ty: i), cKind, VML_ANCHOR_CENTER)
            ui_2d_text(x + cW / 2, cY + cH * 92 / 100, t(zh: "点一下选择", en: "Tap to pick"), -6643536, cTap, VML_ANCHOR_CENTER)
            i = i + 1
        }
        ui_2d_present()
        return
    }

    // 我方在场地上(左下),对手在上(右上)—— 位置由信息框与底部按钮区夹出来
    // The player stands lower-left, the foe upper-right — positions are bracketed by the info boxes and button area
    var pad = pmPad()
    var iw = pmInfoW()
    var ih = pmInfoH()
    var cr = pmCreatureR()
    var pCx = gW / 4
    var fCx = gW * 3 / 4
    var foeY = pad
    var foeX = gW - iw - pad
    var pInfoY = pmBtnTop() - pad - ih
    var fCy = foeY + ih + pad + cr
    var minF = gH * 28 / 100
    if fCy < minF { fCy = minF }
    var pCy = pInfoY - pad - cr

    // 动作时向前冲一段:前 7 拍是位移,后 7 拍等结果
    // While acting, lunge forward: the first 7 ticks are the lunge, the rest wait for the result
    var lunge = 0
    if B[18] > 7 { lunge = B[18] - 7 }
    if lunge > 7 { lunge = 7 }
    var step = cr / 8
    if step < 1 { step = 1 }
    var pOff = 0
    var fOff = 0
    if B[16] == 1 { pOff = lunge * step }
    if B[16] == 2 { fOff = 0 - lunge * step }

    var pHurt = 0
    if B[16] == 2 && B[18] > 10 { pHurt = 1 }
    var fHurt = 0
    if B[16] == 1 && B[18] > 10 { fHurt = 1 }

    drawCreature(cx: fCx + fOff, cy: fCy, ty: B[8], hurt: fHurt, r: cr)
    drawCreature(cx: pCx + pOff, cy: pCy, ty: B[0], hurt: pHurt, r: cr)

    // 信息框内的字号(名字 / 等级 / HP)跟着框高走
    // Info-box font sizes (name / level / HP) follow the box height
    var nmSize = ih / 5
    if nmSize < 10 { nmSize = 10 }
    if nmSize > 16 { nmSize = 16 }
    var smSize = nmSize - 2
    var hpSize = ih / 7
    if hpSize < 8 { hpSize = 8 }
    if hpSize > 10 { hpSize = 10 }
    var barW = iw - 24

    // 对手信息(右上)
    // Foe info (upper right)
    ui_2d_rect(foeX, foeY, iw, ih, -14472648, 1, 0, 12)
    ui_2d_text(foeX + 8, foeY + ih * 30 / 100, monName(ty: B[8]), -1, nmSize, VML_ANCHOR_LEFT)
    ui_2d_text(foeX + iw - 8, foeY + ih * 30 / 100, t(zh: "Lv", en: "Lv") + numToStr(v: B[9]), -6643536, smSize, VML_ANCHOR_RIGHT)
    drawBar(x: foeX + 12, y: foeY + ih * 42 / 100, w: barW, hp: B[10], mx: B[11], color: typeColor(ty: B[8]))
    ui_2d_text(foeX + iw - 8, foeY + ih * 90 / 100, numToStr(v: B[10]) + "/" + numToStr(v: B[11]), -6643536, hpSize, VML_ANCHOR_RIGHT)

    // 我方信息(左下)
    // Player info (lower left)
    ui_2d_rect(pad, pInfoY, iw, ih, -14472648, 1, 0, 12)
    ui_2d_text(pad + 8, pInfoY + ih * 30 / 100, monName(ty: B[0]), -1, nmSize, VML_ANCHOR_LEFT)
    ui_2d_text(pad + iw - 8, pInfoY + ih * 30 / 100, t(zh: "Lv", en: "Lv") + numToStr(v: B[1]), -6643536, smSize, VML_ANCHOR_RIGHT)
    drawBar(x: pad + 12, y: pInfoY + ih * 42 / 100, w: barW, hp: B[2], mx: B[3], color: typeColor(ty: B[0]))
    ui_2d_text(pad + iw - 8, pInfoY + ih * 90 / 100, numToStr(v: B[2]) + "/" + numToStr(v: B[3]), -6643536, hpSize, VML_ANCHOR_RIGHT)

    // 连胜
    // Streak
    var streakSize = gH / 60
    if streakSize < 10 { streakSize = 10 }
    if streakSize > 13 { streakSize = 13 }
    ui_2d_text(pad, pad + streakSize, t(zh: "连胜 ", en: "Streak ") + numToStr(v: B[20]), -1513232, streakSize, VML_ANCHOR_LEFT)

    if B[16] == 0 {
        drawMoveButtons()
    } else if B[16] == 1 || B[16] == 2 {
        drawMessage()
    }

    var ovTitle = gH / 18
    if ovTitle < 18 { ovTitle = 18 }
    if ovTitle > 30 { ovTitle = 30 }
    var ovScore = gH / 25
    if ovScore < 14 { ovScore = 14 }
    if ovScore > 20 { ovScore = 20 }
    var ovHint = gH / 30
    if ovHint < 11 { ovHint = 11 }
    if ovHint > 15 { ovHint = 15 }
    if B[16] == 5 {
        ui_2d_rect(0, 0, gW, gH, -872415232, 1, 0, 0)
        ui_2d_text(gW / 2, gH / 2 - 40, t(zh: "战斗胜利!", en: "You win!"), -11409298, ovTitle, VML_ANCHOR_CENTER)
        ui_2d_text(gW / 2, gH / 2, t(zh: "连胜 ", en: "Streak ") + numToStr(v: B[20]), -16320, ovScore, VML_ANCHOR_CENTER)
        ui_2d_text(gW / 2, gH / 2 + 46, t(zh: "点任意处继续", en: "Tap anywhere to continue"), -1513232, ovHint, VML_ANCHOR_CENTER)
    }
    if B[16] == 6 {
        ui_2d_rect(0, 0, gW, gH, -872415232, 1, 0, 0)
        ui_2d_text(gW / 2, gH / 2 - 40, t(zh: "你的伙伴倒下了……", en: "Your partner fainted..."), -2077901, ovTitle, VML_ANCHOR_CENTER)
        ui_2d_text(gW / 2, gH / 2 + 10, t(zh: "点任意处重新选择", en: "Tap anywhere to pick again"), -1513232, ovHint, VML_ANCHOR_CENTER)
    }

    ui_2d_present()
}

// ── 无头验收:数值 + 一场固定对局 ──
// ── Headless acceptance: the numbers plus one fixed battle ──
func demoChecks() {
    // ① 克制关系:火打草 1.5 倍、草打火 0.7 倍(同一组输入只差属性)
    // (1) Type chart: fire vs grass is 1.5x and grass vs fire is 0.7x (same inputs, only the type differs)
    var dStrong = calcDamage(power: 50, atk: 14, def: 8, el: eff(atkTy: 1, defTy: 0))
    var dNeutral = calcDamage(power: 50, atk: 14, def: 8, el: eff(atkTy: 3, defTy: 0))
    var dWeak = calcDamage(power: 50, atk: 14, def: 8, el: eff(atkTy: 0, defTy: 1))
    println("pkm: strong=" + numToStr(v: dStrong) + " neutral=" + numToStr(v: dNeutral) + " weak=" + numToStr(v: dWeak))

    // ② 固定对局:火系玩家 vs 草系对手,敌方只剩 20 血 ⇒ 一发火焰轮(60)足够击倒
    // (2) Fixed battle: a fire player vs a grass foe at 20 HP ⇒ one Flame Wheel (60) is enough
    makeMon(which: 0, ty: 1, level: 5)
    makeMon(which: 1, ty: 0, level: 3)
    B[10] = 20
    B[16] = 0
    B[17] = 0
    B[27] = 0
    chooseMove(mi: 2)
    var tries = 0
    while B[16] != 5 && tries < 60 {
        battleTick()
        tries = tries + 1
    }
    println("pkm: foe-hp=" + numToStr(v: B[10]) + " phase=" + numToStr(v: B[16]) + " turns=" + numToStr(v: tries))

    // ③ 回复:草系玩家只剩 5 血,休息一次 ⇒ 回到 35(+30,且不超过上限)
    // (3) Healing: a grass player at 5 HP rests once ⇒ back to 35 (+30, capped by max HP)
    makeMon(which: 0, ty: 0, level: 5)
    B[2] = 5
    var healed = useMove(who: 0, mi: 3)
    println("pkm: heal=" + numToStr(v: healed) + " hp=" + numToStr(v: B[2]) + " max=" + numToStr(v: B[3]))
}

func main() {
    var MSG_KEYDOWN = 1
    var MSG_MOUSEDOWN = 4
    var MSG_TOUCHDOWN = 6
    var MSG_TIMER = 9
    var MSG_CLOSE = 10
    var MSG_RESIZE = 11
    var KEY_ESCAPE = 27
    var KEY_ENTER = 13
    var frameLimit = 0

    B[23] = ui_scr_get_language()
    if ui_arg_get_count() > 1 { B[26] = 1; frameLimit = 8 }

    ui_win_open_ex(t(zh: "口袋妖怪对战", en: "Monster Battle"), 0, 0, VML_WIN_PORTRAIT, VML_WIN_NO_GAMEPAD)
    ui_scr_lock_orientation(VML_LOCK_PORTRAIT)
    ui_scr_set_keep_on(1)

    gW = ui_scr_get_w()
    gH = ui_scr_get_h()
    if gW <= 0 { gW = 411 }
    if gH <= 0 { gH = 726 }

    if B[26] != 0 {
        demoChecks()
        // 演示完接一场正常的对战,让后面出的帧画面是战斗界面
        // Start a normal battle after the checks so the rendered frames show the battle screen
        chooseStarter(ty: 1)
        chooseMove(mi: 0)
    } else {
        B[16] = 4
    }
    draw()

    var tid = ui_timer_set(40, 0)

    while ui_win_is_closed() == 0 {
        var m = ui_msg_fetch_wait(40)
        if m == MSG_CLOSE { break }

        if m == MSG_TIMER {
            B[22] = B[22] + 1
            battleTick()
            draw()
            if frameLimit > 0 && B[22] >= frameLimit { break }
            continue
        }

        if m == MSG_RESIZE {
            gW = ui_scr_get_w()
            gH = ui_scr_get_h()
            if gW <= 0 { gW = 411 }
            if gH <= 0 { gH = 726 }
            draw()
            continue
        }

        if m == MSG_KEYDOWN {
            var k = ui_msg_get_a()
            if k == KEY_ESCAPE { break }
            if B[16] == 4 { continue }
            if B[16] == 5 {
                spawnFoe()
                B[16] = 0
                draw()
                continue
            }
            if B[16] == 6 {
                B[16] = 4
                draw()
                continue
            }
            if B[16] == 0 {
                if k >= 49 && k <= 52 { chooseMove(mi: k - 49); draw() }
            }
            continue
        }

        if m == MSG_TOUCHDOWN || m == MSG_MOUSEDOWN {
            var x = ui_msg_get_a()
            var y = ui_msg_get_b()
            if B[16] == 4 {
                var i = 0
                var cy0 = pmCardY()
                while i < 3 {
                    var bx = pmCardX(i: i)
                    if x >= bx && x <= bx + pmCardW() && y >= cy0 && y <= cy0 + pmCardH() {
                        chooseStarter(ty: i)
                        ui_audio_beep(660, 60)
                    }
                    i = i + 1
                }
                draw()
                continue
            }
            if B[16] == 5 {
                spawnFoe()
                B[16] = 0
                draw()
                continue
            }
            if B[16] == 6 {
                B[16] = 4
                draw()
                continue
            }
            if B[16] == 0 {
                var j = 0
                while j < 4 {
                    var bx2 = btnX(i: j)
                    var by2 = btnY(i: j)
                    if x >= bx2 && x <= bx2 + btnW() && y >= by2 && y <= by2 + btnH() {
                        chooseMove(mi: j)
                    }
                    j = j + 1
                }
                draw()
            }
            continue
        }
    }

    ui_timer_kill(tid)
    ui_scr_set_keep_on(0)
    ui_win_close()

    println("pkm: frames=" + numToStr(v: B[22]))
    println("pkm: done")
}