"""Consumer/goal squads tolerate genuine casualties, retaining native carry rules.

Exact weight acceptance stays in the separate threshold route. No Pikmin health,
formation, carrying, treasure position or AP state is assigned here.
"""
from test_gameplay_settled_route import SettledRoute


class ConsumerRoute(SettledRoute):
    def squad_at_actor(self,location,color,count):
        self.clear_snow_enemies()
        _,actor,binding=self.physical(location)
        position=list(actor['position']);position[0]-=80;position[1]+=10
        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
        required=binding['min_carriers'];strength=10 if color==3 else 1
        for _ in range(4):
            state=self.playable();live=state['pikis']
            assert live and len(live)*strength>=required,'real casualties left insufficient carrier power'
            center=[sum(p['position'][i] for p in live)/len(live) 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)
            state=self.playable();owner=self.driver.active_navi(state)['actor_id']
            if all(p['kind']==color and p['action']==0 and p['navi_id']==owner for p in state['pikis']):break
        else:raise AssertionError('surviving real squad did not enter actual owned Formation')
        assert len(state['pikis'])*strength>=required
        self.record('actual-surviving-owned-formation-squad',state,color=color,requested=count,
                    surviving=len(state['pikis']),required_power=required,owner=owner)
        return actor,binding
