{
  "id": "009_xiapi_siege",
  "name": "Xiapi Siege",
  "objectives": {
    "victory": "Hold through the flood-gate operation, then defeat Lu Bu's army.",
    "defeat": "Cao Cao is defeated or the battle reaches Turn 13."
  },
  "conditions": {
    "victory": { "type": "all_units_defeated", "team": "enemy", "after_event": "turn_3_flood_gates" },
    "defeat": [
      { "type": "any_officer_defeated", "team": "player", "officer_ids": ["cao_cao"] },
      { "type": "turn_reached", "turn": 13, "team": "player" }
    ]
  },
  "briefing": {
    "title": "Xiapi Siege",
    "location": "Xiapi, 198 CE",
    "lines": [
      "Lu Bu has withdrawn behind Xiapi's wet walls, trusting his cavalry and Chen Gong's counsel to break the siege.",
      "Cao Cao orders the river channels opened. Survive the countercharge until the flood-gate signals confirm that Lu Bu's army has been forced into the open."
    ],
    "conditional_lines": [
      {
        "campaign_flags": { "held_wan_gate": true },
        "lines": [
          "The baggage recovered at Wan Castle keeps the siege line supplied with heavier shields and dry bowstrings."
        ]
      },
      {
        "campaign_flags": { "swift_wan_escape": true },
        "lines": [
          "The army reached Xiapi quickly after leaving Wan Castle behind, but some siege stores are still catching up."
        ]
      }
    ]
  },
  "shop": {
    "items": [
      "bean",
      "panacea",
      "wine",
      "iron_sword",
      "training_spear",
      "short_bow",
      "hand_axe",
      "leather_armor",
      "iron_armor"
    ],
    "conditional_items": [
      {
        "campaign_flags": { "held_wan_gate": true },
        "items": ["war_drum"]
      },
      {
        "campaign_flags": { "swift_wan_escape": true },
        "items": ["war_axe"]
      }
    ]
  },
  "roster": {
    "max_units": 5,
    "required_officers": ["cao_cao", "dian_wei"]
  },
  "formation": {
    "cells": [[1, 3], [1, 4], [1, 5], [2, 2], [2, 3], [2, 4], [2, 5], [2, 6], [3, 4]]
  },
  "map": {
    "width": 15,
    "height": 10,
    "terrain": [
      "GGGGGHHGGGWWGGG",
      "GGGFFHHGGGWWGGG",
      "GGGFFRRRRGGGGGG",
      "GGGGGRRRGGFFFGG",
      "RRRRRRRRRRRRRRR",
      "GGGGGRRRGGFFFGG",
      "GGHHGFFFGGGRRGG",
      "GGHHGFFFGGGWWGG",
      "GGGGGGFFGGGWWGG",
      "GGGGGGGGGGGGGGG"
    ]
  },
  "deployments": [
    {
      "unit_id": "cao_cao_ch9",
      "officer_id": "cao_cao",
      "team": "player",
      "pos": [1, 4]
    },
    {
      "unit_id": "dian_wei_ch9",
      "officer_id": "dian_wei",
      "team": "player",
      "requires_joined": true,
      "pos": [2, 4]
    },
    {
      "unit_id": "xiahou_dun_ch9",
      "officer_id": "xiahou_dun",
      "team": "player",
      "pos": [1, 5]
    },
    {
      "unit_id": "xiahou_yuan_ch9",
      "officer_id": "xiahou_yuan",
      "team": "player",
      "requires_joined": true,
      "pos": [2, 3]
    },
    {
      "unit_id": "cao_ren_ch9",
      "officer_id": "cao_ren",
      "team": "player",
      "requires_joined": true,
      "pos": [2, 5]
    },
    {
      "unit_id": "lu_bu_xiapi",
      "name": "Lu Bu",
      "class_id": "elite_cavalry",
      "team": "enemy",
      "level": 9,
      "pos": [11, 4],
      "base": { "hp": 64, "atk": 21, "def": 11, "agi": 13 }
    },
    {
      "unit_id": "chen_gong_strategist",
      "name": "Chen Gong",
      "class_id": "strategist",
      "team": "enemy",
      "level": 9,
      "pos": [10, 6],
      "skills": ["blaze"],
      "base": { "hp": 40, "mp": 24, "atk": 7, "def": 6, "int": 15 }
    },
    {
      "unit_id": "gao_shun_trap_line",
      "name": "Gao Shun Line",
      "class_id": "infantry",
      "team": "enemy",
      "level": 9,
      "pos": [9, 5],
      "base": { "hp": 50, "atk": 16, "def": 11 }
    },
    {
      "unit_id": "xiapi_rider_north",
      "name": "Xiapi Rider",
      "class_id": "cavalry",
      "team": "enemy",
      "level": 8,
      "pos": [9, 3],
      "base": { "hp": 42, "atk": 15, "def": 8, "agi": 11 }
    },
    {
      "unit_id": "xiapi_rider_south",
      "name": "Xiapi Rider",
      "class_id": "cavalry",
      "team": "enemy",
      "level": 8,
      "pos": [10, 5],
      "base": { "hp": 42, "atk": 15, "def": 8, "agi": 11 }
    },
    {
      "unit_id": "xiapi_wall_archer",
      "name": "Wall Archer",
      "class_id": "archer",
      "team": "enemy",
      "level": 8,
      "pos": [12, 2],
      "base": { "hp": 34, "atk": 13, "def": 6 }
    },
    {
      "unit_id": "xiapi_gate_guard",
      "name": "Gate Guard",
      "class_id": "infantry",
      "team": "enemy",
      "level": 8,
      "pos": [12, 6],
      "base": { "hp": 46, "atk": 15, "def": 10 }
    }
  ],
  "events": [
    {
      "id": "opening",
      "once": true,
      "when": { "type": "battle_start" },
      "actions": [
        { "type": "log", "text": "The siege lines tighten around Xiapi." }
      ]
    },
    {
      "id": "opening_dialogue",
      "once": true,
      "when": { "type": "battle_begin" },
      "actions": [
        {
          "type": "dialogue",
          "lines": [
            { "speaker": "Cao Cao", "text": "Lu Bu trusts his speed even behind walls. We will make the ground choose against him." },
            { "speaker": "Xiahou Dun", "text": "The channels are ready. We only need to hold the line until the signal rises." },
            { "speaker": "Dian Wei", "text": "Then I hold the road. Let the horsemen find me first." }
          ]
        }
      ]
    },
    {
      "id": "turn_2_lu_bu_charge",
      "once": true,
      "when": { "type": "turn_start", "team": "enemy", "turn": 2 },
      "actions": [
        {
          "type": "dialogue",
          "lines": [
            { "speaker": "Lu Bu", "text": "Cao Cao hides behind ditches and mud. Riders, break the siege line before the water rises!" },
            { "speaker": "Chen Gong", "text": "Strike the commander if the line bends. The flood plan dies with him." }
          ]
        },
        {
          "type": "spawn_deployments",
          "deployments": [
            {
              "unit_id": "lu_bu_flank_rider_north",
              "name": "Flank Rider",
              "class_id": "cavalry",
              "team": "enemy",
              "level": 8,
              "pos": [6, 1],
              "base": { "hp": 40, "atk": 15, "def": 8, "agi": 11 }
            },
            {
              "unit_id": "lu_bu_flank_rider_south",
              "name": "Flank Rider",
              "class_id": "cavalry",
              "team": "enemy",
              "level": 8,
              "pos": [6, 8],
              "base": { "hp": 40, "atk": 15, "def": 8, "agi": 11 }
            }
          ]
        }
      ]
    },
    {
      "id": "turn_3_flood_gates",
      "once": true,
      "when": { "type": "turn_start", "team": "player", "turn": 3 },
      "actions": [
        { "type": "log", "text": "The flood-gate signal rises around Xiapi." },
        {
          "type": "dialogue",
          "lines": [
            { "speaker": "Scout", "text": "The water is moving. Xiapi's outer detachments are falling back from the low roads!" },
            { "speaker": "Cao Cao", "text": "Good. Now Lu Bu must fight where we can reach him." }
          ]
        },
        {
          "type": "set_objective",
          "victory": "Defeat Lu Bu, Chen Gong, and the remaining Xiapi defenders.",
          "defeat": "Cao Cao is defeated or the battle reaches Turn 13."
        },
        {
          "type": "spawn_deployments",
          "deployments": [
            {
              "unit_id": "flooded_gate_guard",
              "name": "Sluice Gate Guard",
              "class_id": "infantry",
              "team": "enemy",
              "level": 8,
              "pos": [8, 4],
              "base": { "hp": 42, "atk": 14, "def": 9 }
            },
            {
              "unit_id": "flooded_wall_archer",
              "name": "Sluice Wall Archer",
              "class_id": "archer",
              "team": "enemy",
              "level": 8,
              "pos": [8, 6],
              "base": { "hp": 32, "atk": 13, "def": 5 }
            }
          ]
        }
      ]
    },
    {
      "id": "wan_gate_baggage_support",
      "once": true,
      "when": { "type": "turn_start", "team": "player", "turn": 4, "campaign_flags": { "held_wan_gate": true } },
      "actions": [
        {
          "type": "dialogue",
          "lines": [
            { "speaker": "Cao Ren", "text": "The baggage wagons from Wan have reached the siege line. The reserves are steady." },
            { "speaker": "Cao Cao", "text": "Then press forward carefully. We can afford patience." }
          ]
        }
      ]
    },
    {
      "id": "swift_wan_counterraid",
      "once": true,
      "when": { "type": "turn_start", "team": "enemy", "turn": 4, "campaign_flags": { "swift_wan_escape": true } },
      "actions": [
        {
          "type": "dialogue",
          "lines": [
            { "speaker": "Chen Gong", "text": "Cao Cao came quickly, but speed thins a siege line. Strike the loose wagons." },
            { "speaker": "Xiahou Yuan", "text": "Their raiders found the supply road. I will cut them off." }
          ]
        },
        {
          "type": "spawn_deployment",
          "deployment": {
            "unit_id": "supply_road_raider",
            "name": "Supply Road Raider",
            "class_id": "bandit",
            "team": "enemy",
            "level": 8,
            "pos": [4, 7],
            "base": { "hp": 40, "atk": 14, "def": 7, "agi": 9 }
          }
        }
      ]
    },
    {
      "id": "turn_6_lu_bu_cornered",
      "once": true,
      "when": { "type": "turn_start", "team": "player", "turn": 6 },
      "actions": [
        {
          "type": "dialogue",
          "lines": [
            { "speaker": "Cao Cao", "text": "His cavalry has nowhere clean to run. Close the ring and do not let Chen Gong buy another hour." },
            { "speaker": "Dian Wei", "text": "Then the road ends here." }
          ]
        }
      ]
    }
  ],
  "rewards": {
    "gold": 1400,
    "items": ["bean", "wine", "war_axe"],
    "join_officers": ["guo_jia"]
  },
  "post_battle_dialogue": [
    { "speaker": "Xiahou Dun", "text": "Xiapi is broken. Lu Bu's riders will not trouble Yan Province again." },
    { "speaker": "Dian Wei", "text": "Fast horses drown the same as slow ones." },
    { "speaker": "Guo Jia", "text": "Lu Bu's fall leaves Yuan Shao as the real measure of the north." },
    { "speaker": "Cao Cao", "text": "Speed without judgment is only a longer fall. We turn now from survival to rule." }
  ],
  "post_battle_choices": [
    {
      "id": "integrate_xu_province",
      "label": "Integrate Xu Province",
      "description": "Cao Cao places officials to secure the newly won territory.",
      "set_flags": { "integrated_xu_province": true, "pressed_northern_campaign": false },
      "gold": 200,
      "items": ["iron_armor"]
    },
    {
      "id": "press_northern_campaign",
      "label": "Press North",
      "description": "The army prepares to turn its momentum toward the northern rivals.",
      "set_flags": { "integrated_xu_province": false, "pressed_northern_campaign": true },
      "gold": 100,
      "items": ["wine", "war_drum"]
    }
  ]
}
