{
  "id": "008_wan_castle_escape",
  "name": "Wan Castle Escape",
  "objectives": {
    "victory": "Move Cao Cao to the western road marker.",
    "defeat": "Cao Cao or Cao Ang is defeated, or the battle reaches Turn 12."
  },
  "conditions": {
    "victory": {
      "type": "unit_reaches_tile",
      "team": "player",
      "officer_ids": ["cao_cao"],
      "cells": [[1, 4], [0, 4]]
    },
    "defeat": [
      { "type": "any_officer_defeated", "team": "player", "officer_ids": ["cao_cao"] },
      { "type": "any_unit_defeated", "unit_ids": ["cao_ang_ch8"] },
      { "type": "turn_reached", "turn": 12, "team": "player" }
    ]
  },
  "briefing": {
    "title": "Wan Castle Escape",
    "location": "Wancheng, 197 CE",
    "lines": [
      "Zhang Xiu welcomes Cao Cao into Wan Castle, but Jia Xu's counsel turns the banquet into a trap.",
      "The eastern gate is collapsing behind the army. Escort Cao Ang and move Cao Cao to the western road marker before the ambush closes."
    ],
    "conditional_lines": [
      {
        "campaign_flags": { "secured_imperial_court": true },
        "lines": [
          "The imperial court's seal has opened a few guarded roads, giving Cao Cao one narrow escape lane through the west."
        ]
      },
      {
        "campaign_flags": { "pursued_li_jue_remnants": true },
        "lines": [
          "The army's hard pursuit after the imperial escort keeps the vanguard sharp, but Zhang Xiu's scouts know Cao Cao's marching habits."
        ]
      }
    ],
    "camp_conversations": [
      {
        "id": "wan_escape_route_council",
        "group": "officer",
        "officer_id": "cao_cao",
        "label": "Cao Cao - Escape Route",
        "speaker": "Cao Cao",
        "summary": "Mark the western road before Wan Castle closes around the column.",
        "lines": [
          {
            "speaker": "Cao Cao",
            "side": "left",
            "text": "A banquet that falls silent has already drawn its blade. If Zhang Xiu closes the east, we leave by the west."
          },
          {
            "speaker": "Cao Ang",
            "side": "right",
            "text": "Then I will keep the road beside you clear. Wan Castle will not take our banner in a hallway."
          }
        ]
      },
      {
        "id": "wan_dian_wei_rear_guard",
        "group": "officer",
        "officer_id": "dian_wei",
        "label": "Dian Wei - Rear Guard",
        "speaker": "Dian Wei",
        "summary": "Assign Dian Wei to hold the rear while Cao Cao and Cao Ang cut west.",
        "lines": [
          {
            "speaker": "Dian Wei",
            "side": "right",
            "text": "Gates are honest things. They either open or they break. I can work with both."
          },
          {
            "speaker": "Cao Cao",
            "side": "left",
            "text": "Hold the rear only long enough to keep Cao Ang moving. A brave death is still a cost I will not pay lightly."
          }
        ]
      },
      {
        "id": "wan_court_seal_postern",
        "campaign_flags": { "secured_imperial_court": true },
        "group": "topic",
        "label": "Court Seal Postern",
        "summary": "Use imperial authority to open a narrow support route.",
        "lines": [
          {
            "speaker": "Escort Road Sutler",
            "side": "right",
            "text": "The court seal still opens doors, even in a castle that wants them shut. I can move one food bundle through the west postern."
          },
          {
            "speaker": "Cao Ren",
            "side": "left",
            "text": "One bundle is enough if it reaches the men cutting the road."
          }
        ],
        "effects": [
          {
            "type": "grant_item",
            "item_id": "bean",
            "count": 1,
            "text": "The court seal postern adds a Bean to the field supplies."
          }
        ]
      },
      {
        "id": "wan_hard_pursuit_drums",
        "campaign_flags": { "pursued_li_jue_remnants": true },
        "group": "topic",
        "label": "Hard Pursuit Drums",
        "summary": "Issue wine to the vanguard whose pace is known by enemy scouts.",
        "lines": [
          {
            "speaker": "Xiahou Yuan",
            "side": "right",
            "text": "Zhang Xiu's scouts know our old marching path because we used it hard after Li Jue. They will expect speed."
          },
          {
            "speaker": "Dian Wei",
            "side": "left",
            "text": "Then give the vanguard wine and change the rhythm. Let them chase the sound they expected."
          }
        ],
        "effects": [
          {
            "type": "grant_item",
            "item_id": "wine",
            "count": 1,
            "text": "The hard pursuit drums add Wine to the field supplies."
          }
        ]
      }
    ]
  },
  "shop": {
    "items": [
      "bean",
      "panacea",
      "wine",
      "iron_sword",
      "training_spear",
      "short_bow",
      "hand_axe",
      "leather_armor",
      "iron_armor"
    ],
    "conditional_items": [
      {
        "campaign_flags": { "secured_imperial_court": true },
        "items": ["imperial_seal"]
      },
      {
        "campaign_flags": { "pursued_li_jue_remnants": true },
        "items": ["war_drum"]
      }
    ],
    "merchant": {
      "name": "Wan Castle Sutler",
      "lines": [
        "Buy quietly. The walls have learned to listen since the banquet began.",
        "If the west road opens, carry only what keeps men moving: beans, wine, and blades already in hand."
      ]
    }
  },
  "roster": {
    "max_units": 5,
    "required_officers": ["cao_cao", "dian_wei"],
    "required_units": ["cao_ang_ch8"]
  },
  "formation": {
    "cells": [[10, 3], [10, 4], [10, 5], [11, 3], [11, 4], [11, 5], [12, 3], [12, 4], [12, 5]]
  },
  "map": {
    "width": 15,
    "height": 10,
    "background": "res://art/backgrounds/wan_castle_escape.png",
    "terrain": [
      "GGGGGHHGGGFFFFG",
      "GGFFGHHGGGFFFFG",
      "GGFFRRRRRRGGGGG",
      "GGGGRGGGGGRRRGG",
      "RRRRRRRRRRRRRRR",
      "GGGGRGGGGGRRRGG",
      "GGFFRRRGGGFFFFG",
      "GGFFGHHGGGFFFFG",
      "GGGGGHHGGGWWGGG",
      "GGGGGGGGGGWWGGG"
    ]
  },
  "deployments": [
    {
      "unit_id": "cao_cao_ch8",
      "officer_id": "cao_cao",
      "team": "player",
      "pos": [12, 4]
    },
    {
      "unit_id": "cao_ang_ch8",
      "name": "Cao Ang",
      "class_id": "cavalry",
      "team": "player",
      "level": 4,
      "controllable": false,
      "persist_progression": false,
      "ai_target_priority": 5,
      "pos": [12, 5],
      "base": { "hp": 32, "mp": 2, "atk": 9, "def": 6, "int": 6, "agi": 8 }
    },
    {
      "unit_id": "dian_wei_ch8",
      "officer_id": "dian_wei",
      "team": "player",
      "requires_joined": true,
      "pos": [11, 4]
    },
    {
      "unit_id": "xiahou_dun_ch8",
      "officer_id": "xiahou_dun",
      "team": "player",
      "pos": [12, 3]
    },
    {
      "unit_id": "xiahou_yuan_ch8",
      "officer_id": "xiahou_yuan",
      "team": "player",
      "requires_joined": true,
      "pos": [11, 3]
    },
    {
      "unit_id": "cao_ren_ch8",
      "officer_id": "cao_ren",
      "team": "player",
      "requires_joined": true,
      "pos": [11, 5]
    },
    {
      "unit_id": "zhang_xiu_vanguard",
      "name": "Zhang Xiu Vanguard",
      "class_id": "cavalry",
      "team": "enemy",
      "level": 8,
      "pos": [6, 4],
      "base": { "hp": 52, "atk": 17, "def": 9, "agi": 12 }
    },
    {
      "unit_id": "wan_castle_guard_north",
      "name": "Wan Castle Guard",
      "class_id": "infantry",
      "team": "enemy",
      "level": 8,
      "pos": [5, 3],
      "base": { "hp": 46, "atk": 15, "def": 10 }
    },
    {
      "unit_id": "wan_castle_guard_south",
      "name": "Wan Castle Guard",
      "class_id": "infantry",
      "team": "enemy",
      "level": 8,
      "pos": [5, 5],
      "base": { "hp": 46, "atk": 15, "def": 10 }
    },
    {
      "unit_id": "jia_xu_strategist",
      "name": "Jia Xu Strategist",
      "class_id": "strategist",
      "team": "enemy",
      "level": 8,
      "pos": [4, 6],
      "skills": ["blaze"],
      "base": { "hp": 38, "mp": 22, "atk": 7, "def": 6, "int": 14 }
    },
    {
      "unit_id": "wan_castle_archer",
      "name": "Wan Castle Archer",
      "class_id": "archer",
      "team": "enemy",
      "level": 8,
      "pos": [6, 2],
      "base": { "hp": 34, "atk": 13, "def": 6 }
    },
    {
      "unit_id": "southern_ambusher",
      "name": "Southern Ambusher",
      "class_id": "bandit",
      "team": "enemy",
      "level": 8,
      "pos": [7, 7],
      "base": { "hp": 42, "atk": 15, "def": 7 }
    }
  ],
  "events": [
    {
      "id": "opening",
      "once": true,
      "when": { "type": "battle_start" },
      "actions": [
        { "type": "log", "text": "Wan Castle's gates close behind Cao Cao's column." }
      ]
    },
    {
      "id": "opening_dialogue",
      "once": true,
      "when": { "type": "battle_begin" },
      "actions": [
        {
          "type": "dialogue",
          "lines": [
            { "speaker": "Cao Cao", "text": "The music stopped too cleanly. This is Jia Xu's work." },
            { "speaker": "Dian Wei", "text": "Then I hold the gate while you cut west." },
            { "speaker": "Cao Ang", "text": "Father, the road is narrow. I will stay close." }
          ]
        }
      ]
    },
    {
      "id": "turn_2_gate_closes",
      "once": true,
      "when": { "type": "turn_start", "team": "enemy", "turn": 2 },
      "actions": [
        {
          "type": "dialogue",
          "lines": [
            { "speaker": "Zhang Xiu", "text": "Close the eastern gate. Cao Cao leaves Wan Castle only through our blades." },
            { "speaker": "Jia Xu", "text": "Do not chase the guards. Strike the road and separate him from his son." }
          ]
        },
        {
          "type": "set_objective",
          "victory": "Break through the western road and move Cao Cao to the escape marker.",
          "defeat": "Cao Cao or Cao Ang is defeated, or the battle reaches Turn 12."
        },
        {
          "type": "set_ai_target_priority",
          "unit_id": "cao_ang_ch8",
          "priority": 10,
          "text": "Cao Ang is exposed as the ambush closes."
        },
        {
          "type": "spawn_deployments",
          "deployments": [
            {
              "unit_id": "east_gate_rider_north",
              "name": "East Gate Rider",
              "class_id": "cavalry",
              "team": "enemy",
              "level": 8,
              "pos": [13, 2],
              "base": { "hp": 40, "atk": 15, "def": 8, "agi": 10 }
            },
            {
              "unit_id": "east_gate_rider_south",
              "name": "East Gate Rider",
              "class_id": "cavalry",
              "team": "enemy",
              "level": 8,
              "pos": [13, 6],
              "base": { "hp": 40, "atk": 15, "def": 8, "agi": 10 }
            }
          ]
        }
      ]
    },
    {
      "id": "court_escape_lane",
      "once": true,
      "when": { "type": "turn_start", "team": "player", "turn": 3, "campaign_flags": { "secured_imperial_court": true } },
      "actions": [
        {
          "type": "dialogue",
          "lines": [
            { "speaker": "Imperial Guard", "text": "The court seal opens the west postern. We can delay their riders for a moment." },
            { "speaker": "Cao Cao", "text": "Then one moment is enough. Keep the road open." }
          ]
        },
        {
          "type": "spawn_deployment",
          "deployment": {
            "unit_id": "imperial_guard_support_ch8",
            "name": "Imperial Guard",
            "class_id": "infantry",
            "team": "player",
            "level": 6,
            "controllable": false,
            "persist_progression": false,
            "pos": [9, 4],
            "base": { "hp": 34, "atk": 10, "def": 9, "int": 6, "agi": 6 }
          }
        }
      ]
    },
    {
      "id": "remnant_scout_pressure",
      "once": true,
      "when": { "type": "turn_start", "team": "enemy", "turn": 3, "campaign_flags": { "pursued_li_jue_remnants": true } },
      "actions": [
        {
          "type": "dialogue",
          "lines": [
            { "speaker": "Wan Scout", "text": "That is the same vanguard that chased Li Jue's remnants. Cut off their old marching path." },
            { "speaker": "Xiahou Dun", "text": "They know our pace. Then we change it by force." }
          ]
        },
        {
          "type": "spawn_deployments",
          "deployments": [
            {
              "unit_id": "remnant_scout_rider",
              "name": "Remnant Scout Rider",
              "class_id": "cavalry",
              "team": "enemy",
              "level": 8,
              "pos": [10, 1],
              "base": { "hp": 38, "atk": 14, "def": 7, "agi": 11 }
            },
            {
              "unit_id": "remnant_scout_archer",
              "name": "Remnant Scout Archer",
              "class_id": "archer",
              "team": "enemy",
              "level": 8,
              "pos": [10, 7],
              "base": { "hp": 32, "atk": 13, "def": 5 }
            }
          ]
        }
      ]
    },
    {
      "id": "midroad_trap",
      "once": true,
      "when": { "type": "unit_reaches_tile", "team": "player", "pos": [8, 4] },
      "actions": [
        {
          "type": "dialogue",
          "lines": [
            { "speaker": "Cao Ang", "text": "Archers in the trees!" },
            { "speaker": "Dian Wei", "text": "Stay behind the shields. I will make them aim at me." }
          ]
        },
        {
          "type": "spawn_deployments",
          "deployments": [
            {
              "unit_id": "midroad_wan_archer_north",
              "name": "Wan Ambush Archer",
              "class_id": "archer",
              "team": "enemy",
              "level": 8,
              "pos": [8, 2],
              "base": { "hp": 32, "atk": 13, "def": 5 }
            },
            {
              "unit_id": "midroad_wan_archer_south",
              "name": "Wan Ambush Archer",
              "class_id": "archer",
              "team": "enemy",
              "level": 8,
              "pos": [8, 6],
              "base": { "hp": 32, "atk": 13, "def": 5 }
            }
          ]
        }
      ]
    },
    {
      "id": "turn_6_last_push",
      "once": true,
      "when": { "type": "turn_start", "team": "player", "turn": 6 },
      "actions": [
        {
          "type": "dialogue",
          "lines": [
            { "speaker": "Cao Cao", "text": "The western road is close. Do not spend lives chasing revenge inside these walls." },
            { "speaker": "Dian Wei", "text": "Then I spend steel instead. Move." }
          ]
        }
      ]
    }
  ],
  "rewards": {
    "gold": 1200,
    "items": ["bean", "wine", "iron_sword"]
  },
  "post_battle_dialogue": [
    { "speaker": "Cao Ang", "text": "The road is clear. I thought Wan Castle would swallow us." },
    { "speaker": "Dian Wei", "text": "It tried. Its teeth broke first." },
    { "speaker": "Cao Cao", "text": "A trap survived is still a lesson. We will remember Jia Xu." }
  ],
  "post_battle_choices": [
    {
      "id": "hold_wan_gate",
      "label": "Hold the Gate",
      "description": "Dian Wei holds the rear long enough to recover armor and baggage.",
      "set_flags": { "held_wan_gate": true, "swift_wan_escape": false },
      "gold": 100,
      "items": ["iron_armor"]
    },
    {
      "id": "swift_wan_withdrawal",
      "label": "Swift Withdrawal",
      "description": "Cao Cao leaves the baggage behind to keep the army intact.",
      "set_flags": { "held_wan_gate": false, "swift_wan_escape": true },
      "gold": 250,
      "items": ["bean", "wine", "short_bow"]
    }
  ]
}
