"""Resume a real test card using normal sprout plucking and recruitment.

A prior native extinction/save may leave a genuine planted Red sprout instead
of live authored day-one actors. Boot that selected card normally, pluck its
existing sprout through PAD, then recruit through ordinary B before the already
disclosed later-scene fixture. No Pikmin, story flags, rewards or card data are
assigned by this entry policy; it makes no fresh five-authored-Pikmin claim.
"""
import asyncio
from pathlib import Path
import test_equipment_gameplay_real_server as equipment
import test_equipment_sr_combat_setup as combat
from test_gameplay_settled_route import SettledRoute
from test_gameplay_real_server import make_parser


def resumed_red_entry(game,driver):
    entry=driver.boot_day1(game,timeout=300)
    route=SettledRoute(game,driver);state=route.playable()
    retained_sprouts=[]
    if not any(p['kind']==1 for p in state['pikis']):
        assert state['day']>0 and state['sprouts'],'resumed native card needs its actual existing Red sprout'
        retained_sprouts=state['sprouts']
        driver.pluck_sprouts(game,timeout=120)
        state=route.playable()
    reds=[p for p in state['pikis'] if p['kind']==1]
    assert reds,'actual PAD plucking must produce a living native Red Pikmin'
    center=[sum(p['position'][i] for p in reds)/len(reds) for i in range(3)]
    driver.walk_to(game,center,radius=35,timeout=45)
    driver.aim_at(game,center,radius=15)
    game.input(buttons=driver.B,frames=120);route.playable()
    game.input(frames=90);state=route.playable()
    owner=driver.active_navi(state)['actor_id']
    recruited=[p for p in state['pikis'] if p['kind']==1 and p['action']==0 and p['navi_id']==owner]
    assert recruited,'ordinary B must recruit a real Red into the active captain Formation'
    return {'entry':entry,'state':state,'retained_native_sprouts':retained_sprouts,
        'actual_recruited_red_ids':[p['actor_id'] for p in recruited],'boundary':__doc__}


async def main(args):
    assert args.resume_checkpoint,'this entry policy is for the retained official same-runtime continuation'
    equipment.initial_red_intro=resumed_red_entry
    equipment.HELPERS.append(Path(__file__))
    await combat.main(args)


if __name__=='__main__':
    parser=make_parser(__doc__);parser.add_argument('--resume-checkpoint',type=Path)
    asyncio.run(main(parser.parse_args()))
