"""Strict weight and real buried/enemy routes with ordinary appraisal input."""
import asyncio
from pathlib import Path
import test_gameplay_actor_cases as cases
from test_loaded_gameplay_precise import PreciseRoute,EXTRA
from test_gameplay_arrival_policy import AppraisalArrival
from test_gameplay_real_server import make_parser


class AppraisalRoute(AppraisalArrival,PreciseRoute):
    def squad_at_actor(self,location,color,count):
        # This enemy-drop route is an identity/texture/delivery test. Its real
        # corpse can recruit carriers too; spare Reds avoid making that
        # incidental production behavior a false weight-threshold failure.
        # The separate EC 4/5 and 19/20 threshold commands remain exact.
        count=40 if location==72 and color==1 and count==20 else count
        if color!=1:return super().squad_at_actor(location,color,count)
        self.clear_snow_enemies()
        _,actor,binding=self.physical(location)
        position=list(actor['position']);position[0]-=25
        position=self.command('ground_query',position=position)['last_result']['position'];position[1]+=5
        self.command('actor_position',actor='navi',index=0,position=position)
        self.game.input(frames=20);self.playable()
        born=self.command('squad_replace',color=color,count=count,captain=0,position=position)
        assert born['last_result']['born']==count
        for _ in range(3):
            state=self.playable()
            assert len(state['pikis'])==count,'exact prepared Red squad lost actors before its threshold test'
            center=[sum(p['position'][i] for p in state['pikis'])/count for i in range(3)]
            self.driver.aim_at(self.game,center,radius=15)
            self.game.input(buttons=self.driver.B,frames=120);self.game.input(frames=8);self.playable()
        state=self.game.wait_state(lambda s:len(s['pikis'])==count and
            all(p['kind']==color and p['action']==0 and p['navi_id']==self.driver.active_navi(s)['actor_id']
                for p in s['pikis']),15,'exact grounded Red Formation squad')
        self.record('actual-grounded-exact-red-formation',state,requested=count,
            boundary='grounded captain/squad setup only; original min/max properties and actual native carry/Onyon required')
        return actor,binding


if __name__=='__main__':
    args=make_parser(__doc__).parse_args();args.case='loaded';cases.StableRoute=AppraisalRoute
    cases.EXTRA.extend(Path(__file__).with_name(n) for n in EXTRA)
    cases.EXTRA.extend(Path(__file__).with_name(n) for n in
        ('test_loaded_gameplay_precise.py','test_gameplay_arrival_policy.py','test_loaded_gameplay_appraisal.py'))
    asyncio.run(cases.main(args))
