{
  "id": "038_ma_chao_retreat",
  "name": "Ma Chao Retreat",
  "objectives": {
    "victory": "Reach Ma Chao's retreat road with Cao Cao, then defeat the western rearguard.",
    "defeat": "Cao Cao is defeated or the battle reaches Turn 18."
  },
  "conditions": {
    "victory": { "type": "all_units_defeated", "team": "enemy", "after_event": "retreat_road_reached" },
    "defeat": [
      { "type": "any_officer_defeated", "team": "player", "officer_ids": ["cao_cao"] },
      { "type": "turn_reached", "turn": 18, "team": "player" }
    ]
  },
  "briefing": {
    "title": "Ma Chao Retreat",
    "location": "Western Guanzhong, 211 CE",
    "lines": [
      "The Guanzhong foothold holds, and Ma Chao's riders begin to give ground toward the western roads.",
      "Cao Cao must decide whether the retreat becomes a clean pursuit or a wider pacification before Hanzhong draws the next line."
    ],
    "conditional_lines": [
      {
        "campaign_flags": { "secured_guanzhong_commandery": true },
        "lines": [
          "Secured commandery stores give Cao Cao a steadier pursuit column and enough grain to keep pressure on the retreat."
        ]
      },
      {
        "campaign_flags": { "pressed_ma_chao_retreat": true },
        "lines": [
          "The hard pursuit keeps Ma Chao's riders from fully reforming, but it stretches Cao Cao's front toward rougher western roads."
        ]
      }
    ]
  },
  "shop": {
    "items": [
      "bean",
      "wine",
      "iron_sword",
      "training_spear",
      "short_bow",
      "hand_axe",
      "war_axe",
      "leather_armor",
      "iron_armor"
    ],
    "conditional_items": [
      {
        "campaign_flags": { "secured_guanzhong_commandery": true },
        "items": ["imperial_seal"]
      },
      {
        "campaign_flags": { "pressed_ma_chao_retreat": 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_ch38",
      "officer_id": "cao_cao",
      "team": "player",
      "pos": [1, 4]
    },
    {
      "unit_id": "guo_jia_ch38",
      "officer_id": "guo_jia",
      "team": "player",
      "requires_joined": true,
      "pos": [2, 4]
    },
    {
      "unit_id": "zhang_he_ch38",
      "officer_id": "zhang_he",
      "team": "player",
      "requires_joined": true,
      "pos": [2, 3]
    },
    {
      "unit_id": "xiahou_dun_ch38",
      "officer_id": "xiahou_dun",
      "team": "player",
      "pos": [1, 5]
    },
    {
      "unit_id": "dian_wei_ch38",
      "officer_id": "dian_wei",
      "team": "player",
      "requires_joined": true,
      "pos": [2, 5]
    },
    {
      "unit_id": "xiahou_yuan_ch38",
      "officer_id": "xiahou_yuan",
      "team": "player",
      "requires_joined": true,
      "pos": [3, 3]
    },
    {
      "unit_id": "cao_ren_ch38",
      "officer_id": "cao_ren",
      "team": "player",
      "requires_joined": true,
      "pos": [3, 4]
    },
    {
      "unit_id": "ma_chao_retreat_guard",
      "name": "Ma Chao Retreat Guard",
      "class_id": "elite_cavalry",
      "team": "enemy",
      "level": 36,
      "pos": [14, 4],
      "base": { "hp": 144, "mp": 16, "atk": 41, "def": 29, "int": 14, "agi": 30 }
    },
    {
      "unit_id": "pang_de_rear_column",
      "name": "Pang De Rear Column",
      "class_id": "champion",
      "team": "enemy",
      "level": 35,
      "pos": [15, 3],
      "base": { "hp": 128, "mp": 12, "atk": 38, "def": 26, "int": 12, "agi": 24 }
    },
    {
      "unit_id": "western_road_captain",
      "name": "Western Road Captain",
      "class_id": "guard_captain",
      "team": "enemy",
      "level": 35,
      "pos": [12, 4],
      "base": { "hp": 102, "atk": 36, "def": 29 }
    },
    {
      "unit_id": "retreat_tactic_advisor",
      "name": "Retreat Tactic Advisor",
      "class_id": "military_advisor",
      "team": "enemy",
      "level": 35,
      "pos": [13, 5],
      "skills": ["blaze", "great_mend"],
      "base": { "hp": 90, "mp": 74, "atk": 8, "def": 18, "int": 38, "agi": 18 }
    },
    {
      "unit_id": "retreat_spear_north",
      "name": "Retreat Spear Guard",
      "class_id": "infantry",
      "team": "enemy",
      "level": 35,
      "pos": [12, 3],
      "base": { "hp": 92, "atk": 36, "def": 26 }
    },
    {
      "unit_id": "retreat_spear_south",
      "name": "Retreat Spear Guard",
      "class_id": "infantry",
      "team": "enemy",
      "level": 35,
      "pos": [12, 5],
      "base": { "hp": 92, "atk": 36, "def": 26 }
    },
    {
      "unit_id": "western_road_marksman",
      "name": "Western Road Marksman",
      "class_id": "marksman",
      "team": "enemy",
      "level": 35,
      "pos": [13, 2],
      "base": { "hp": 84, "atk": 37, "def": 14, "agi": 27 }
    }
  ],
  "events": [
    {
      "id": "opening",
      "once": true,
      "when": { "type": "battle_start" },
      "actions": [
        { "type": "log", "text": "Cao Cao presses the western road as Ma Chao's retreat guard tries to keep Guanzhong from closing behind them." }
      ]
    },
    {
      "id": "opening_dialogue",
      "once": true,
      "when": { "type": "battle_begin" },
      "actions": [
        {
          "type": "dialogue",
          "lines": [
            { "speaker": "Xiahou Yuan", "text": "Ma Chao is falling back, but not broken. His rear guard still turns like a blade." },
            { "speaker": "Guo Jia", "text": "A retreat that chooses where to stop is still a plan." },
            { "speaker": "Cao Cao", "text": "Then take the choice away." }
          ]
        }
      ]
    },
    {
      "id": "commandery_stores_feed_pursuit",
      "once": true,
      "when": { "type": "turn_start", "team": "player", "turn": 2, "campaign_flags": { "secured_guanzhong_commandery": true } },
      "actions": [
        {
          "type": "dialogue",
          "lines": [
            { "speaker": "Cao Ren", "text": "Guanzhong stores are feeding the column. The pursuit can stay ordered." },
            { "speaker": "Cao Cao", "text": "Order is how pursuit becomes conquest." }
          ]
        }
      ]
    },
    {
      "id": "hard_pursuit_catches_rear",
      "once": true,
      "when": { "type": "turn_start", "team": "enemy", "turn": 2, "campaign_flags": { "pressed_ma_chao_retreat": true } },
      "actions": [
        {
          "type": "dialogue",
          "lines": [
            { "speaker": "Ma Chao Retreat Guard", "text": "Cao Cao is too close. Turn the rear column and buy the west one more road." },
            { "speaker": "Zhang He", "text": "The pursuit has touched their spine. Expect the rearguard to snap back." }
          ]
        },
        {
          "type": "spawn_deployment",
          "deployment": {
            "unit_id": "ma_clan_rear_riders",
            "name": "Ma Clan Rear Riders",
            "class_id": "cavalry",
            "team": "enemy",
            "level": 35,
            "pos": [16, 3],
            "base": { "hp": 84, "atk": 33, "def": 18, "agi": 24 }
          }
        }
      ]
    },
    {
      "id": "turn_4_western_road_narrows",
      "once": true,
      "when": { "type": "turn_start", "team": "enemy", "turn": 4 },
      "actions": [
        {
          "type": "dialogue",
          "lines": [
            { "speaker": "Pang De Rear Column", "text": "The road narrows ahead. Hold them here or the retreat becomes a rout." },
            { "speaker": "Xiahou Yuan", "text": "He knows the road better than we do. That makes speed matter." }
          ]
        }
      ]
    },
    {
      "id": "retreat_road_reached",
      "once": true,
      "when": { "type": "unit_reaches_tile", "team": "player", "officer_ids": ["cao_cao"], "pos": [10, 4] },
      "actions": [
        {
          "type": "set_objective",
          "victory": "Ma Chao's retreat road is reached. Defeat the western rearguard.",
          "defeat": "Cao Cao is defeated or the battle reaches Turn 18."
        },
        {
          "type": "dialogue",
          "lines": [
            { "speaker": "Cao Cao", "text": "This road decides whether Ma Chao retreats or escapes. Break the rearguard." },
            { "speaker": "Ma Chao Retreat Guard", "text": "The west is not yours while one horse still turns." }
          ]
        },
        {
          "type": "spawn_deployments",
          "deployments": [
            {
              "unit_id": "retreat_halberd_guard",
              "name": "Retreat Halberd Guard",
              "class_id": "guard_captain",
              "team": "enemy",
              "level": 35,
              "pos": [16, 4],
              "base": { "hp": 100, "atk": 36, "def": 29 }
            },
            {
              "unit_id": "western_rear_arrow_screen",
              "name": "Western Rear Arrow Screen",
              "class_id": "archer",
              "team": "enemy",
              "level": 35,
              "pos": [16, 5],
              "base": { "hp": 78, "atk": 35, "def": 12, "agi": 25 }
            }
          ]
        }
      ]
    },
    {
      "id": "turn_9_retreat_frays",
      "once": true,
      "when": { "type": "turn_start", "team": "player", "turn": 9 },
      "actions": [
        {
          "type": "dialogue",
          "lines": [
            { "speaker": "Zhang He", "text": "Their retreat is losing its rhythm." },
            { "speaker": "Cao Cao", "text": "Then strike before rhythm becomes distance." }
          ]
        }
      ]
    }
  ],
  "rewards": {
    "gold": 7400,
    "items": ["bean", "wine", "imperial_seal"]
  },
  "post_battle_dialogue": [
    { "speaker": "Guo Jia", "text": "Ma Chao's retreat guard is broken. Guanzhong can be settled, or Hanzhong can be watched before Zhang Lu makes his own terms." },
    { "speaker": "Xiahou Yuan", "text": "Pacifying the west will quiet the roads. Preparing Hanzhong will keep the next gate from closing." },
    { "speaker": "Cao Cao", "text": "The west is never silent for long. Choose where our next silence is made." }
  ],
  "post_battle_choices": [
    {
      "id": "pacify_guanzhong",
      "label": "Pacify Guanzhong",
      "description": "Cao Cao pacifies Guanzhong's commanderies before looking toward Hanzhong.",
      "set_flags": { "pacified_guanzhong": true, "prepared_hanzhong_front": false },
      "gold": 1150,
      "items": ["imperial_seal"]
    },
    {
      "id": "prepare_hanzhong_front",
      "label": "Prepare Hanzhong",
      "description": "Cao Cao prepares the Hanzhong front before Zhang Lu can harden the passes.",
      "set_flags": { "pacified_guanzhong": false, "prepared_hanzhong_front": true },
      "gold": 950,
      "items": ["war_drum"]
    }
  ]
}
