{
  "id": "040_hanzhong_gate",
  "name": "Hanzhong Gate",
  "objectives": {
    "victory": "Reach Zhang Lu's gate with Cao Cao, then defeat the Hanzhong gate guard.",
    "defeat": "Cao Cao is defeated or the battle reaches Turn 18."
  },
  "conditions": {
    "victory": { "type": "all_units_defeated", "team": "enemy", "after_event": "hanzhong_gate_reached" },
    "defeat": [
      { "type": "any_officer_defeated", "team": "player", "officer_ids": ["cao_cao"] },
      { "type": "turn_reached", "turn": 18, "team": "player" }
    ]
  },
  "briefing": {
    "title": "Hanzhong Gate",
    "location": "Hanzhong Gate, Qinling Passes, 215 CE",
    "lines": [
      "The Hanzhong approach is open, but Zhang Lu's gate still controls the mountain road into the basin.",
      "Cao Cao's army must decide whether Hanzhong opens through measured siege lines or a blow fast enough to unsettle Zhang Lu's priests."
    ],
    "conditional_lines": [
      {
        "campaign_flags": { "secured_hanzhong_approach": true },
        "lines": [
          "Securing the approach gives Cao Cao room to bring up ladders, stores, and patient pressure before the gate battle."
        ]
      },
      {
        "campaign_flags": { "pressed_zhang_lu_gate": true },
        "lines": [
          "Pressing Zhang Lu's gate keeps the defenders off balance, but Hanzhong's beacon guards are already scrambling to answer."
        ]
      }
    ]
  },
  "shop": {
    "items": [
      "bean",
      "wine",
      "iron_sword",
      "training_spear",
      "short_bow",
      "hand_axe",
      "war_axe",
      "leather_armor",
      "iron_armor"
    ],
    "conditional_items": [
      {
        "campaign_flags": { "secured_hanzhong_approach": true },
        "items": ["imperial_seal"]
      },
      {
        "campaign_flags": { "pressed_zhang_lu_gate": true },
        "items": ["war_drum"]
      }
    ]
  },
  "roster": {
    "max_units": 7,
    "required_officers": ["cao_cao", "guo_jia"]
  },
  "formation": {
    "cells": [[1, 3], [1, 4], [1, 5], [2, 2], [2, 3], [2, 4], [2, 5], [2, 6], [3, 2], [3, 3], [3, 4], [3, 5], [3, 6]]
  },
  "map": {
    "width": 18,
    "height": 11,
    "terrain": [
      "GGGGHHGGGGGGHHGGGG",
      "GGFFHHGGRRGGHHFFGG",
      "GGRRRRRRRRRRRRFFGG",
      "GGGFGHGGGGRRGGGGGG",
      "RRRRRRRRRRRRRRRRRR",
      "GGGGGHHGGGRRGFFGGG",
      "GGHHGFFFGGFFFGGHHG",
      "GGHHGFFFGGFFFGGHHG",
      "GGGGGGFFGGHHHGGGGG",
      "GGFFGGGGGGHHHFFGGG",
      "GGGGGGGGGGGGGGGGGG"
    ]
  },
  "deployments": [
    {
      "unit_id": "cao_cao_ch40",
      "officer_id": "cao_cao",
      "team": "player",
      "pos": [1, 4]
    },
    {
      "unit_id": "guo_jia_ch40",
      "officer_id": "guo_jia",
      "team": "player",
      "requires_joined": true,
      "pos": [2, 4]
    },
    {
      "unit_id": "zhang_he_ch40",
      "officer_id": "zhang_he",
      "team": "player",
      "requires_joined": true,
      "pos": [2, 3]
    },
    {
      "unit_id": "xiahou_dun_ch40",
      "officer_id": "xiahou_dun",
      "team": "player",
      "pos": [1, 5]
    },
    {
      "unit_id": "dian_wei_ch40",
      "officer_id": "dian_wei",
      "team": "player",
      "requires_joined": true,
      "pos": [2, 5]
    },
    {
      "unit_id": "xiahou_yuan_ch40",
      "officer_id": "xiahou_yuan",
      "team": "player",
      "requires_joined": true,
      "pos": [3, 3]
    },
    {
      "unit_id": "cao_ren_ch40",
      "officer_id": "cao_ren",
      "team": "player",
      "requires_joined": true,
      "pos": [3, 4]
    },
    {
      "unit_id": "zhang_lu_gate_guard",
      "name": "Zhang Lu Gate Guard",
      "class_id": "commander",
      "team": "enemy",
      "level": 37,
      "pos": [14, 4],
      "base": { "hp": 130, "mp": 52, "atk": 38, "def": 30, "int": 31, "agi": 21 }
    },
    {
      "unit_id": "yangping_pass_captain",
      "name": "Yangping Pass Captain",
      "class_id": "guard_captain",
      "team": "enemy",
      "level": 37,
      "pos": [12, 4],
      "base": { "hp": 106, "atk": 38, "def": 31 }
    },
    {
      "unit_id": "hanzhong_gate_champion",
      "name": "Hanzhong Gate Champion",
      "class_id": "champion",
      "team": "enemy",
      "level": 37,
      "pos": [15, 3],
      "base": { "hp": 134, "mp": 12, "atk": 40, "def": 28, "int": 12, "agi": 25 }
    },
    {
      "unit_id": "gate_ritual_advisor",
      "name": "Gate Ritual Advisor",
      "class_id": "military_advisor",
      "team": "enemy",
      "level": 37,
      "pos": [13, 5],
      "skills": ["blaze", "great_mend"],
      "base": { "hp": 94, "mp": 78, "atk": 8, "def": 19, "int": 40, "agi": 19 }
    },
    {
      "unit_id": "hanzhong_gate_spear_north",
      "name": "Hanzhong Gate Spear",
      "class_id": "infantry",
      "team": "enemy",
      "level": 37,
      "pos": [12, 3],
      "base": { "hp": 96, "atk": 38, "def": 28 }
    },
    {
      "unit_id": "hanzhong_gate_spear_south",
      "name": "Hanzhong Gate Spear",
      "class_id": "infantry",
      "team": "enemy",
      "level": 37,
      "pos": [12, 5],
      "base": { "hp": 96, "atk": 38, "def": 28 }
    },
    {
      "unit_id": "hanzhong_gate_marksman",
      "name": "Hanzhong Gate Marksman",
      "class_id": "marksman",
      "team": "enemy",
      "level": 37,
      "pos": [13, 2],
      "base": { "hp": 88, "atk": 39, "def": 15, "agi": 28 }
    }
  ],
  "events": [
    {
      "id": "opening",
      "once": true,
      "when": { "type": "battle_start" },
      "actions": [
        { "type": "log", "text": "Cao Cao's army reaches Zhang Lu's mountain gate, where Hanzhong's defenders gather beneath the pass beacons." }
      ]
    },
    {
      "id": "opening_dialogue",
      "once": true,
      "when": { "type": "battle_begin" },
      "actions": [
        {
          "type": "dialogue",
          "lines": [
            { "speaker": "Xiahou Yuan", "text": "That gate is less a wall than a cliff with banners." },
            { "speaker": "Guo Jia", "text": "Then do not argue with the cliff. Find the men holding it up." },
            { "speaker": "Cao Cao", "text": "The gate opens when their certainty breaks." }
          ]
        }
      ]
    },
    {
      "id": "secured_approach_builds_siege",
      "once": true,
      "when": { "type": "turn_start", "team": "player", "turn": 2, "campaign_flags": { "secured_hanzhong_approach": true } },
      "actions": [
        {
          "type": "dialogue",
          "lines": [
            { "speaker": "Cao Ren", "text": "The secured approach has brought ladders and shield carts into position." },
            { "speaker": "Cao Cao", "text": "Good. Preparation makes even stone nervous." }
          ]
        }
      ]
    },
    {
      "id": "pressed_gate_summons_defenders",
      "once": true,
      "when": { "type": "turn_start", "team": "enemy", "turn": 2, "campaign_flags": { "pressed_zhang_lu_gate": true } },
      "actions": [
        {
          "type": "dialogue",
          "lines": [
            { "speaker": "Zhang Lu Gate Guard", "text": "Cao Cao presses before the rites are complete. Call every beacon guard to the gate." },
            { "speaker": "Zhang He", "text": "Their haste answers ours. The gate will be crowded with fear." }
          ]
        },
        {
          "type": "spawn_deployment",
          "deployment": {
            "unit_id": "beacon_gate_riders",
            "name": "Beacon Gate Riders",
            "class_id": "cavalry",
            "team": "enemy",
            "level": 37,
            "pos": [16, 3],
            "base": { "hp": 88, "atk": 35, "def": 19, "agi": 25 }
          }
        }
      ]
    },
    {
      "id": "turn_4_gate_defiance",
      "once": true,
      "when": { "type": "turn_start", "team": "enemy", "turn": 4 },
      "actions": [
        {
          "type": "dialogue",
          "lines": [
            { "speaker": "Gate Ritual Advisor", "text": "The mountain tests the unworthy. Hold until their confidence thins." },
            { "speaker": "Guo Jia", "text": "They dress delay as faith. Cut the delay and the robes follow." }
          ]
        }
      ]
    },
    {
      "id": "hanzhong_gate_reached",
      "once": true,
      "when": { "type": "unit_reaches_tile", "team": "player", "officer_ids": ["cao_cao"], "pos": [10, 4] },
      "actions": [
        {
          "type": "set_objective",
          "victory": "Zhang Lu's gate is reached. Defeat the Hanzhong gate guard.",
          "defeat": "Cao Cao is defeated or the battle reaches Turn 18."
        },
        {
          "type": "dialogue",
          "lines": [
            { "speaker": "Cao Cao", "text": "This is the gate. Break its guard and Hanzhong must speak plainly." },
            { "speaker": "Zhang Lu Gate Guard", "text": "Hanzhong speaks through the pass, and the pass says no." }
          ]
        },
        {
          "type": "spawn_deployments",
          "deployments": [
            {
              "unit_id": "gate_halberd_guard",
              "name": "Gate Halberd Guard",
              "class_id": "guard_captain",
              "team": "enemy",
              "level": 37,
              "pos": [16, 4],
              "base": { "hp": 104, "atk": 38, "def": 31 }
            },
            {
              "unit_id": "gate_arrow_screen",
              "name": "Gate Arrow Screen",
              "class_id": "archer",
              "team": "enemy",
              "level": 37,
              "pos": [16, 5],
              "base": { "hp": 82, "atk": 37, "def": 12, "agi": 26 }
            }
          ]
        }
      ]
    },
    {
      "id": "turn_9_gate_shakes",
      "once": true,
      "when": { "type": "turn_start", "team": "player", "turn": 9 },
      "actions": [
        {
          "type": "dialogue",
          "lines": [
            { "speaker": "Xiahou Yuan", "text": "The gate line is shaking. Their priests are shouting louder than their captains." },
            { "speaker": "Cao Cao", "text": "Then the captains can no longer hear victory. Finish it." }
          ]
        }
      ]
    }
  ],
  "rewards": {
    "gold": 7800,
    "items": ["bean", "wine", "imperial_seal"]
  },
  "post_battle_dialogue": [
    { "speaker": "Guo Jia", "text": "The Hanzhong gate is broken. Zhang Lu can still bargain, but he can no longer pretend the mountain alone decides." },
    { "speaker": "Xiahou Yuan", "text": "Zhang Lu can submit now, or force us to pacify Hanzhong pass by pass." },
    { "speaker": "Cao Cao", "text": "A surrender accepted quickly becomes a road. A province pacified slowly becomes a lesson." }
  ],
  "post_battle_choices": [
    {
      "id": "accept_zhang_lu_submission",
      "label": "Accept Submission",
      "description": "Cao Cao accepts Zhang Lu's submission before Hanzhong can rally around another defense.",
      "set_flags": { "accepted_zhang_lu_submission": true, "pacified_hanzhong": false },
      "gold": 1250,
      "items": ["imperial_seal"]
    },
    {
      "id": "pacify_hanzhong",
      "label": "Pacify Hanzhong",
      "description": "Cao Cao pacifies Hanzhong by force so no mountain faction can reopen the pass war.",
      "set_flags": { "accepted_zhang_lu_submission": false, "pacified_hanzhong": true },
      "gold": 1050,
      "items": ["war_drum"]
    }
  ]
}
