{
  "_comment": "PGS (PlayCraft Gameplay Schema) 参考骨架 - 经典塔防玩法。Agent 以此为起点编写或 fork 玩法规则。",
  "version": "1.0",
  "gameplayType": "tower_defence",
  "entities": {
    "grid": {
      "type": "tile-map",
      "description": "游戏地图格子",
      "fields": {
        "width":           { "type": "integer", "description": "地图列数" },
        "height":          { "type": "integer", "description": "地图行数" },
        "cells":           { "type": "array",   "description": "格子类型数组 CellType[][]，path=路径，buildable=可建，empty=不可建" },
        "path":            { "type": "array",   "description": "敌人行进路径坐标序列 {x, y}[]，从入口到出口" },
        "path_entry":      { "type": "vector2", "description": "敌人入口坐标" },
        "path_exit":       { "type": "vector2", "description": "敌人出口坐标" }
      }
    },
    "tower": {
      "type": "defense-unit",
      "description": "防御塔",
      "fields": {
        "id":           { "type": "string",  "description": "唯一ID" },
        "type":         { "type": "enum",    "values": ["arrow", "cannon", "magic"] },
        "position":     { "type": "vector2", "description": "格子坐标 {x, y}" },
        "level":        { "type": "integer", "range": [1, 3], "default": 1 },
        "damage":       { "type": "number",  "description": "每次攻击伤害" },
        "range":        { "type": "number",  "description": "攻击范围（格子数）" },
        "attack_speed": { "type": "number",  "description": "攻击间隔（毫秒）" },
        "last_attack_ms":{ "type": "number", "description": "上次攻击时间戳" },
        "splash_radius": { "type": "number", "default": 0, "description": "范围伤害半径（仅 cannon）" },
        "slow_factor":  { "type": "number",  "default": 1.0, "description": "减速因子（仅 magic，< 1.0）" },
        "cost":         { "type": "integer", "description": "建造费用（金币）" },
        "upgrade_cost": { "type": "integer", "description": "升级费用" },
        "sell_value":   { "type": "integer", "description": "出售返还金币（约建造价 50%）" }
      }
    },
    "enemy": {
      "type": "moving-unit",
      "description": "敌人单位，沿路径行进",
      "fields": {
        "id":           { "type": "string",  "description": "唯一ID" },
        "type":         { "type": "enum",    "values": ["basic", "fast", "armored", "boss"] },
        "hp":           { "type": "number",  "description": "当前血量" },
        "max_hp":       { "type": "number",  "description": "最大血量" },
        "speed":        { "type": "number",  "description": "移动速度（格子/秒）" },
        "path_index":   { "type": "integer", "description": "当前目标路径点索引" },
        "progress":     { "type": "number",  "range": [0, 1], "description": "到下一路径点的进度" },
        "position":     { "type": "vector2", "description": "当前插值位置（用于渲染）" },
        "reward":       { "type": "integer", "description": "击杀奖励金币" },
        "armor":        { "type": "number",  "default": 0, "description": "装甲值，减少物理伤害百分比" },
        "slow_until_ms":{ "type": "number",  "default": 0, "description": "减速效果持续到的时间戳" },
        "alive":        { "type": "boolean", "default": true }
      }
    },
    "waves": {
      "type": "spawn-config",
      "description": "波次定义列表",
      "fields": {
        "wave_index":      { "type": "integer", "description": "当前波次索引（0-based）" },
        "total_waves":     { "type": "integer" },
        "wave_in_progress":{ "type": "boolean" },
        "spawn_queue":     { "type": "array",   "description": "当前波次待生成的敌人队列 {type, count, interval_ms}[]" },
        "last_spawn_ms":   { "type": "number" },
        "definitions":     {
          "type": "array",
          "description": "每波次定义 [{enemies: [{type, count}], spawn_interval_ms}]"
        }
      }
    },
    "resources": {
      "type": "player-resources",
      "fields": {
        "gold":  { "type": "integer", "initial": 150, "description": "当前金币" },
        "lives": { "type": "integer", "initial": 20,  "description": "剩余生命" }
      }
    }
  },
  "rules": {
    "place_tower": {
      "description": "在可建格子放置防御塔",
      "conditions": [
        "目标格子类型 == buildable",
        "目标格子没有已有塔",
        "resources.gold >= tower_type.cost"
      ],
      "effect": "resources.gold -= cost; grid.cell = occupied; towers.push(new_tower)"
    },
    "upgrade_tower": {
      "description": "升级已有防御塔（最高3级）",
      "conditions": [
        "tower.level < 3",
        "resources.gold >= tower.upgrade_cost"
      ],
      "effect": "resources.gold -= upgrade_cost; tower.level++; tower.damage *= 1.5; tower.range += 0.5"
    },
    "sell_tower": {
      "description": "出售防御塔，返还部分金币",
      "effect": "resources.gold += tower.sell_value; 移除塔; 格子恢复 buildable"
    },
    "auto_attack": {
      "description": "每次 tick，塔检查冷却时间，若冷却结束则攻击范围内最近的敌人",
      "target_priority": "nearest_enemy_in_range",
      "cooldown_check": "current_ms - tower.last_attack_ms >= tower.attack_speed",
      "damage_formula": {
        "arrow":  "damage（单体，穿甲弱）",
        "cannon": "damage × (1 - armor_reduction)，splash_radius 范围内所有敌人受 50% 伤害",
        "magic":  "damage × (1 + armor_bonus_factor if armored)，命中后施加减速效果 slow_factor"
      }
    },
    "enemy_move": {
      "description": "每 tick 敌人按速度沿路径推进",
      "formula": {
        "advance": "progress += speed × (1 if not slowed else slow_factor) × dt / path_segment_length",
        "next_point": "if progress >= 1: path_index++; progress -= 1",
        "escape": "if path_index >= path.length: 敌人到达出口"
      }
    },
    "enemy_death": {
      "description": "hp <= 0 时敌人死亡，掉落金币",
      "effect": "enemy.alive = false; resources.gold += enemy.reward"
    },
    "enemy_escape": {
      "description": "敌人到达路径终点（出口），扣除生命",
      "effect": "resources.lives -= 1; remove enemy"
    },
    "wave_spawn": {
      "description": "按波次配置定时生成敌人到路径入口",
      "interval": "spawn_queue.current.interval_ms"
    }
  },
  "algorithm": {
    "description": "固定 tick 主循环（每 100ms）",
    "steps": [
      "1. 波次生成：若 wave_in_progress，按 spawn_interval 从 spawn_queue 取出敌人，添加到 path 入口",
      "2. 敌人移动：每个存活敌人按速度（含减速状态）沿路径推进",
      "3. 逃跑检测：到达出口的敌人扣除生命 lives--，检测是否 lives<=0 → 失败",
      "4. 塔攻击：检查每个塔的冷却，对范围内最近存活敌人发动攻击（含范围/减速效果）",
      "5. 死亡结算：hp<=0 的敌人标记 dead，掉落金币，从列表移除",
      "6. 波次完成检测：所有敌人死亡或逃跑 → 当前波次结束，准备下一波",
      "7. 胜利检测：all waves 完成 且 lives > 0 → 胜利"
    ]
  },
  "winCondition": {
    "type": "survive_all_waves",
    "description": "所有波次的敌人全部被消灭或逃跑处理完毕，且 resources.lives > 0"
  },
  "loseCondition": {
    "type": "lives_depleted",
    "description": "resources.lives <= 0（敌人到达出口次数超过初始生命值）"
  },
  "testCases": [
    {
      "id": "tc_place_tower_kills_enemy",
      "description": "放置弓箭塔后，敌人进入范围内被击杀，获得金币奖励",
      "initialState": {
        "grid": {
          "path": [{"x":0,"y":2},{"x":1,"y":2},{"x":2,"y":2},{"x":3,"y":2},{"x":4,"y":2}],
          "cells": "3x5 地图，path在y=2，buildable在y=1和y=3"
        },
        "towers": [],
        "enemies": [
          { "id": "e1", "type": "basic", "hp": 100, "max_hp": 100, "speed": 1, "path_index": 0, "progress": 0, "reward": 20, "armor": 0, "alive": true }
        ],
        "resources": { "gold": 150, "lives": 20 },
        "waves": { "wave_index": 0, "wave_in_progress": false }
      },
      "actions": [
        { "type": "place_tower", "position": {"x": 2, "y": 1}, "tower_type": "arrow" },
        { "type": "tick_game_loop", "times": 30, "deltaMs": 100 }
      ],
      "expectedState": {
        "enemies[e1].alive": false,
        "resources.gold": { "greaterThan": 100 },
        "resources.lives": 20
      }
    },
    {
      "id": "tc_enemy_escapes_loses_life",
      "description": "无塔时敌人到达出口，生命-1",
      "initialState": {
        "grid": {
          "path": [{"x":0,"y":2},{"x":1,"y":2},{"x":2,"y":2}]
        },
        "towers": [],
        "enemies": [
          { "id": "e1", "type": "basic", "hp": 100, "max_hp": 100, "speed": 3, "path_index": 0, "progress": 0.9, "reward": 20, "armor": 0, "alive": true }
        ],
        "resources": { "gold": 150, "lives": 20 }
      },
      "actions": [
        { "type": "tick_game_loop", "times": 5, "deltaMs": 100 }
      ],
      "expectedState": {
        "resources.lives": 19,
        "enemies": []
      }
    },
    {
      "id": "tc_wave_completion",
      "description": "波次通过：所有敌人被消灭，波次计数推进",
      "initialState": {
        "grid": {
          "path": [{"x":0,"y":2},{"x":1,"y":2},{"x":2,"y":2},{"x":3,"y":2}]
        },
        "towers": [
          { "id": "t1", "type": "arrow", "position": {"x":2,"y":1}, "level": 1, "damage": 30, "range": 2, "attack_speed": 500, "last_attack_ms": 0 }
        ],
        "enemies": [
          { "id": "e1", "type": "basic", "hp": 60, "max_hp": 100, "speed": 1, "path_index": 1, "progress": 0, "reward": 20, "armor": 0, "alive": true }
        ],
        "resources": { "gold": 100, "lives": 20 },
        "waves": { "wave_index": 0, "total_waves": 3, "wave_in_progress": true }
      },
      "actions": [
        { "type": "tick_game_loop", "times": 20, "deltaMs": 100 }
      ],
      "expectedState": {
        "enemies": [],
        "waves.wave_index": 1,
        "waves.wave_in_progress": false,
        "resources.gold": { "greaterThan": 100 }
      }
    },
    {
      "id": "tc_game_over_lives_zero",
      "description": "失败：生命值耗尽",
      "initialState": {
        "grid": {
          "path": [{"x":0,"y":2},{"x":1,"y":2}]
        },
        "towers": [],
        "enemies": [
          { "id": "e1", "type": "basic", "hp": 100, "max_hp": 100, "speed": 5, "path_index": 0, "progress": 0.9, "reward": 20, "armor": 0, "alive": true }
        ],
        "resources": { "gold": 0, "lives": 1 }
      },
      "actions": [
        { "type": "tick_game_loop", "times": 5, "deltaMs": 100 }
      ],
      "expectedState": {
        "resources.lives": 0,
        "gameOver": true,
        "win": false
      }
    }
  ]
}
