{
  "id": "037_guanzhong_foothold",
  "name": "Guanzhong Foothold",
  "objectives": {
    "victory": "Reach the Guanzhong foothold with Cao Cao, then defeat the western alliance rearguard.",
    "defeat": "Cao Cao is defeated or the battle reaches Turn 18."
  },
  "conditions": {
    "victory": { "type": "all_units_defeated", "team": "enemy", "after_event": "guanzhong_foothold_reached" },
    "defeat": [
      { "type": "any_officer_defeated", "team": "player", "officer_ids": ["cao_cao"] },
      { "type": "turn_reached", "turn": 18, "team": "player" }
    ]
  },
  "briefing": {
    "title": "Guanzhong Foothold",
    "location": "Guanzhong Plain, 211 CE",
    "lines": [
      "The Wei River crossing gives Cao Cao a road into Guanzhong, but a road is not yet a province.",
      "Ma Chao's rearguard and Han Sui's uncertain captains gather near the first foothold, where the western campaign can either settle or scatter."
    ],
    "conditional_lines": [
      {
        "campaign_flags": { "secured_guanzhong_foothold": true },
        "lines": [
          "The secured crossing has become a real foothold, giving Cao Cao a firm base for the next push across Guanzhong."
        ]
      },
      {
        "campaign_flags": { "split_western_alliance": true },
        "lines": [
          "The western alliance is split enough that every Ma clan order must first pass through suspicion."
        ]
      }
    ]
  },
  "shop": {
    "items": [
      "bean",
      "wine",
      "iron_sword",
      "training_spear",
      "short_bow",
      "hand_axe",
      "war_axe",
      "leather_armor",
      "iron_armor"
    ],
    "conditional_items": [
      {
        "campaign_flags": { "secured_guanzhong_foothold": true },
        "items": ["imperial_seal"]
      },
      {
        "campaign_flags": { "split_western_alliance": 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_ch37",
      "officer_id": "cao_cao",
      "team": "player",
      "pos": [1, 4]
    },
    {
      "unit_id": "guo_jia_ch37",
      "officer_id": "guo_jia",
      "team": "player",
      "requires_joined": true,
      "pos": [2, 4]
    },
    {
      "unit_id": "zhang_he_ch37",
      "officer_id": "zhang_he",
      "team": "player",
      "requires_joined": true,
      "pos": [2, 3]
    },
    {
      "unit_id": "xiahou_dun_ch37",
      "officer_id": "xiahou_dun",
      "team": "player",
      "pos": [1, 5]
    },
    {
      "unit_id": "dian_wei_ch37",
      "officer_id": "dian_wei",
      "team": "player",
      "requires_joined": true,
      "pos": [2, 5]
    },
    {
      "unit_id": "xiahou_yuan_ch37",
      "officer_id": "xiahou_yuan",
      "team": "player",
      "requires_joined": true,
      "pos": [3, 3]
    },
    {
      "unit_id": "cao_ren_ch37",
      "officer_id": "cao_ren",
      "team": "player",
      "requires_joined": true,
      "pos": [3, 4]
    },
    {
      "unit_id": "ma_chao_rearguard",
      "name": "Ma Chao Rearguard",
      "class_id": "elite_cavalry",
      "team": "enemy",
      "level": 35,
      "pos": [14, 4],
      "base": { "hp": 140, "mp": 16, "atk": 40, "def": 28, "int": 14, "agi": 29 }
    },
    {
      "unit_id": "han_sui_divided_captain",
      "name": "Han Sui Divided Captain",
      "class_id": "commander",
      "team": "enemy",
      "level": 34,
      "pos": [15, 5],
      "base": { "hp": 122, "mp": 48, "atk": 36, "def": 28, "int": 28, "agi": 20 }
    },
    {
      "unit_id": "guanzhong_gate_captain",
      "name": "Guanzhong Gate Captain",
      "class_id": "guard_captain",
      "team": "enemy",
      "level": 34,
      "pos": [12, 4],
      "base": { "hp": 100, "atk": 35, "def": 28 }
    },
    {
      "unit_id": "foothold_tactic_advisor",
      "name": "Foothold Tactic Advisor",
      "class_id": "military_advisor",
      "team": "enemy",
      "level": 34,
      "pos": [13, 5],
      "skills": ["blaze", "great_mend"],
      "base": { "hp": 88, "mp": 72, "atk": 8, "def": 17, "int": 37, "agi": 18 }
    },
    {
      "unit_id": "guanzhong_spear_north",
      "name": "Guanzhong Spear Guard",
      "class_id": "infantry",
      "team": "enemy",
      "level": 34,
      "pos": [12, 3],
      "base": { "hp": 90, "atk": 35, "def": 25 }
    },
    {
      "unit_id": "guanzhong_spear_south",
      "name": "Guanzhong Spear Guard",
      "class_id": "infantry",
      "team": "enemy",
      "level": 34,
      "pos": [12, 5],
      "base": { "hp": 90, "atk": 35, "def": 25 }
    },
    {
      "unit_id": "guanzhong_marksman",
      "name": "Guanzhong Marksman",
      "class_id": "marksman",
      "team": "enemy",
      "level": 34,
      "pos": [13, 2],
      "base": { "hp": 82, "atk": 36, "def": 13, "agi": 26 }
    }
  ],
  "events": [
    {
      "id": "opening",
      "once": true,
      "when": { "type": "battle_start" },
      "actions": [
        { "type": "log", "text": "Cao Cao pushes from the Wei River crossing toward a Guanzhong foothold as the western alliance strains." }
      ]
    },
    {
      "id": "opening_dialogue",
      "once": true,
      "when": { "type": "battle_begin" },
      "actions": [
        {
          "type": "dialogue",
          "lines": [
            { "speaker": "Cao Ren", "text": "The crossing behind us is firm. The foothold ahead is where the west begins to answer." },
            { "speaker": "Guo Jia", "text": "A foothold is not just earth. It is the moment the enemy starts measuring loss." },
            { "speaker": "Cao Cao", "text": "Then give them something worth measuring." }
          ]
        }
      ]
    },
    {
      "id": "secured_foothold_supplies_advance",
      "once": true,
      "when": { "type": "turn_start", "team": "player", "turn": 2, "campaign_flags": { "secured_guanzhong_foothold": true } },
      "actions": [
        {
          "type": "dialogue",
          "lines": [
            { "speaker": "Cao Ren", "text": "Supply is moving from the crossing into the foothold. The line can hold if Ma Chao turns back." },
            { "speaker": "Cao Cao", "text": "A held line invites the next decision." }
          ]
        }
      ]
    },
    {
      "id": "split_alliance_confuses_rearguard",
      "once": true,
      "when": { "type": "turn_start", "team": "enemy", "turn": 2, "campaign_flags": { "split_western_alliance": true } },
      "actions": [
        {
          "type": "dialogue",
          "lines": [
            { "speaker": "Han Sui Divided Captain", "text": "Ma Chao demands we cover his withdrawal, but Han Sui's seals say to preserve our own wing." },
            { "speaker": "Guo Jia", "text": "When two orders arrive together, neither arrives first." }
          ]
        },
        {
          "type": "spawn_deployment",
          "deployment": {
            "unit_id": "fractured_alliance_riders",
            "name": "Fractured Alliance Riders",
            "class_id": "cavalry",
            "team": "enemy",
            "level": 34,
            "pos": [16, 3],
            "base": { "hp": 82, "atk": 32, "def": 18, "agi": 23 }
          }
        }
      ]
    },
    {
      "id": "turn_4_ma_chao_rearguard",
      "once": true,
      "when": { "type": "turn_start", "team": "enemy", "turn": 4 },
      "actions": [
        {
          "type": "dialogue",
          "lines": [
            { "speaker": "Ma Chao Rearguard", "text": "If Cao Cao plants himself in Guanzhong, the west will have to fight on his road. Cut the foothold now!" },
            { "speaker": "Xiahou Yuan", "text": "He sees the same ground we do. Good." }
          ]
        }
      ]
    },
    {
      "id": "guanzhong_foothold_reached",
      "once": true,
      "when": { "type": "unit_reaches_tile", "team": "player", "officer_ids": ["cao_cao"], "pos": [10, 4] },
      "actions": [
        {
          "type": "set_objective",
          "victory": "The Guanzhong foothold is reached. Defeat the western alliance rearguard.",
          "defeat": "Cao Cao is defeated or the battle reaches Turn 18."
        },
        {
          "type": "dialogue",
          "lines": [
            { "speaker": "Cao Cao", "text": "This foothold is where Guanzhong begins to change hands. Break the rearguard." },
            { "speaker": "Ma Chao Rearguard", "text": "You have crossed a river, not conquered a people." }
          ]
        },
        {
          "type": "spawn_deployments",
          "deployments": [
            {
              "unit_id": "guanzhong_halberd_guard",
              "name": "Guanzhong Halberd Guard",
              "class_id": "guard_captain",
              "team": "enemy",
              "level": 34,
              "pos": [16, 4],
              "base": { "hp": 98, "atk": 35, "def": 28 }
            },
            {
              "unit_id": "foothold_arrow_screen",
              "name": "Foothold Arrow Screen",
              "class_id": "archer",
              "team": "enemy",
              "level": 34,
              "pos": [16, 5],
              "base": { "hp": 76, "atk": 34, "def": 11, "agi": 24 }
            }
          ]
        }
      ]
    },
    {
      "id": "turn_9_foothold_holds",
      "once": true,
      "when": { "type": "turn_start", "team": "player", "turn": 9 },
      "actions": [
        {
          "type": "dialogue",
          "lines": [
            { "speaker": "Zhang He", "text": "The foothold is taking shape. Their rearguard is fighting over yesterday's road." },
            { "speaker": "Cao Cao", "text": "Then make tomorrow's road ours." }
          ]
        }
      ]
    }
  ],
  "rewards": {
    "gold": 7200,
    "items": ["bean", "wine", "war_drum"]
  },
  "post_battle_dialogue": [
    { "speaker": "Guo Jia", "text": "The Guanzhong foothold stands. Ma Chao can still flee west, but the alliance now leaves tracks in different directions." },
    { "speaker": "Xiahou Yuan", "text": "We can secure the commandery and pull its stores into our line, or press Ma Chao before he rebuilds pride into another charge." },
    { "speaker": "Cao Cao", "text": "A province is held by grain or pursuit. Choose the first hand we close." }
  ],
  "post_battle_choices": [
    {
      "id": "secure_guanzhong_commandery",
      "label": "Secure Commandery",
      "description": "Cao Cao secures Guanzhong stores and commandery offices before the next advance.",
      "set_flags": { "secured_guanzhong_commandery": true, "pressed_ma_chao_retreat": false },
      "gold": 1100,
      "items": ["imperial_seal"]
    },
    {
      "id": "press_ma_chao_retreat",
      "label": "Press Retreat",
      "description": "Cao Cao presses Ma Chao's retreat before the western riders can rally.",
      "set_flags": { "secured_guanzhong_commandery": false, "pressed_ma_chao_retreat": true },
      "gold": 900,
      "items": ["war_drum"]
    }
  ]
}
