# Engine test: campaigns (subflow per scenario, persist: campaign, reset per scenario, end
# conditions scoped to the scenario, and reading the scenario outcome via results).
game: campaign
label: Mini campaign (test)
num_players: 1
mode: competitive
description: >
  Test game for campaigns. Three scenarios in a row. Each scenario starts the hero at 2 hp and the
  monster at 2 hp (not kept between scenarios, `reset`). Attack: a 1-in-2 chance to hit (monster
  -1), otherwise the hero loses 1 hp. A potion (+1 hp) costs 1 gold. Gold and scenarios won carry
  over (`persist: campaign`); each scenario won earns 1 gold. The trade-off: spend gold now or save
  it for a later scenario. Payoff = number of scenarios won.
constants: {SCENARIOS: 3}
state:
  vars:
    gold: {scope: global, type: int, init: 1, persist: campaign}
    won: {scope: global, type: int, init: 0, persist: campaign}
    played: {scope: global, type: int, init: 0, persist: campaign}
    hp: {scope: global, type: int, init: 2}
    monster: {scope: global, type: int, init: 2}
flow:
  id: campaign
  label: Campaign
  kind: loop
  until_x: "played >= SCENARIOS"
  children:
    - {id: play, label: Scenario, kind: subflow, ref: scenario}
    - {id: wrap_up, label: Wrap up, kind: step, do: wrap_up}
flows:
  scenario:
    id: scenario
    kind: sequence
    children:
      - {id: set_up, label: New scenario, kind: step, do: set_up}
      - id: fight
        label: Fight
        kind: loop
        until: "until an end condition scoped to the scenario ends it"
        until_x: "False"
        children:
          - {id: turn, label: Turn, kind: step, actor: hero, actor_x: "0", do: [attack, potion]}
actions:
  set_up: {effect: [{op: reset, scope: scenario}]}
  attack:
    effect:
      - {op: roll, die_x: "['hit', 'miss']", bind: r}
      - op: if
        cond: hit
        cond_x: "r == 'hit'"
        then: [{op: add, var: monster, amount: -1}]
        else: [{op: add, var: hp, amount: -1}]
  potion:
    pre_x: "gold >= 1"
    effect: [{op: add, var: gold, amount: -1}, {op: add, var: hp, amount: 1}]
  wrap_up:
    effect:
      - op: if
        cond: scenario won
        cond_x: "results.get('scenario') == 'win'"
        then: [{op: add, var: won, amount: 1}, {op: add, var: gold, amount: 1}]
      - {op: add, var: played, amount: 1}
end:
  - {id: monster_defeated, scope: scenario, if: "monster defeated", if_x: "monster <= 0", result: win}
  - {id: hero_defeated, scope: scenario, if: "hero defeated", if_x: "hp <= 0", result: loss}
  - {id: campaign_over, if: "all scenarios played; payoff = number won", if_x: "played >= SCENARIOS", result: score, returns_x: "[won]"}
