コード例 #1
0
ファイル: test_battle.py プロジェクト: AtnesNess/the-tale
    def test_mob_actor(self):
        mob = mobs_storage.get_random_mob(self.hero)
        mob.health = 10
        mob.abilities.add(RUN_UP_PUSH.get_id())

        actor = battle.Actor(mob, BattleContext())

        self.assertEqual(mob.initiative, actor.initiative)
        self.assertEqual(mob.name, actor.name)
        self.assertEqual(mob.utg_name, actor.utg_name)
        self.assertEqual(mob.basic_damage, actor.basic_damage)
        self.assertEqual(mob.health, actor.health)
        self.assertEqual(mob.max_health, actor.max_health)

        self.assertEqual(actor.change_health(-5), -5)
        self.assertEqual(actor.health, 5)

        self.assertEqual(actor.change_health(-50), -5)
        self.assertEqual(actor.health, 0)

        self.assertEqual(actor.change_health(actor.max_health + 50),
                         actor.max_health)
        self.assertEqual(actor.health, actor.max_health)

        hit_selected = False
        run_up_push_selected = False
        vampire_strike_selected = False
        for i in range(100):
            ability = actor.choose_ability()

            if ability.get_id() == HIT.get_id():
                hit_selected = True
            elif ability.get_id() == RUN_UP_PUSH.get_id():
                run_up_push_selected = True
            elif ability.get_id() == VAMPIRE_STRIKE.get_id():
                vampire_strike_selected = True

        self.assertTrue(hit_selected)
        self.assertTrue(run_up_push_selected)
        self.assertTrue(vampire_strike_selected)

        self.storage._test_save()
コード例 #2
0
ファイル: test_battle.py プロジェクト: ruckysolis/the-tale
    def test_mob_actor(self):
        mob = mobs_storage.get_random_mob(self.hero)
        mob.health = 10
        mob.abilities.add(RUN_UP_PUSH.get_id())

        actor = battle.Actor(mob, BattleContext())

        self.assertEqual(mob.initiative, actor.initiative)
        self.assertEqual(mob.name, actor.name)
        self.assertEqual(mob.utg_name, actor.utg_name)
        self.assertEqual(mob.basic_damage, actor.basic_damage)
        self.assertEqual(mob.health, actor.health)
        self.assertEqual(mob.max_health, actor.max_health)

        self.assertEqual(actor.change_health(-5), -5)
        self.assertEqual(actor.health, 5)

        self.assertEqual(actor.change_health(-50), -5)
        self.assertEqual(actor.health, 0)

        self.assertEqual(actor.change_health(actor.max_health+50), actor.max_health)
        self.assertEqual(actor.health, actor.max_health)

        hit_selected = False
        run_up_push_selected = False
        vampire_strike_selected = False
        for i in xrange(100):
            ability = actor.choose_ability()

            if ability.get_id() == HIT.get_id():
               hit_selected = True
            elif ability.get_id() == RUN_UP_PUSH.get_id():
                run_up_push_selected = True
            elif ability.get_id() == VAMPIRE_STRIKE.get_id():
                vampire_strike_selected = True

        self.assertTrue(hit_selected)
        self.assertTrue(run_up_push_selected)
        self.assertTrue(vampire_strike_selected)

        self.storage._test_save()
コード例 #3
0
ファイル: test_battle.py プロジェクト: angru/the-tale
class BattleTests(TestsBase):

    @mock.patch('the_tale.game.heroes.objects.Hero.additional_abilities', [VAMPIRE_STRIKE(level=1)])
    def test_hero_actor(self):
        self.hero.health = 10
        self.hero.abilities.add(RUN_UP_PUSH.get_id())

        actor = battle.Actor(self.hero, BattleContext())

        self.assertEqual(self.hero.initiative, actor.initiative)
        self.assertEqual(self.hero.name, actor.name)
        self.assertEqual(self.hero.utg_name, actor.utg_name)
        self.assertEqual(self.hero.basic_damage, actor.basic_damage)
        self.assertEqual(self.hero.health, actor.health)
        self.assertEqual(self.hero.max_health, actor.max_health)

        self.assertEqual(actor.change_health(-5), -5)
        self.assertEqual(actor.health, 5)

        self.assertEqual(actor.change_health(-50), -5)
        self.assertEqual(actor.health, 0)

        self.assertEqual(actor.change_health(actor.max_health+50), actor.max_health)
        self.assertEqual(actor.health, actor.max_health)

        hit_selected = False
        run_up_push_selected = False
        vampire_strike_selected = False
        for i in xrange(100):
            ability = actor.choose_ability()

            if ability.get_id() == HIT.get_id():
               hit_selected = True
            elif ability.get_id() == RUN_UP_PUSH.get_id():
                run_up_push_selected = True
            elif ability.get_id() == VAMPIRE_STRIKE.get_id():
                vampire_strike_selected = True

        self.assertTrue(hit_selected)
        self.assertTrue(run_up_push_selected)
        self.assertTrue(vampire_strike_selected)

        self.storage._test_save()

    def test_choose_ability__additional_abilities(self):
        from the_tale.game.heroes.habilities import ABILITIES
        all_abilities = [ability(level=ability.MAX_LEVEL) for ability in ABILITIES.values()]

        active_abilities = set(ability.get_id() for ability in all_abilities if ability.activation_type.is_ACTIVE)

        self.hero.health = 1 # allow regeneration

        actor = battle.Actor(self.hero, BattleContext())

        chosen_abilities = set()

        with mock.patch('the_tale.game.heroes.objects.Hero.additional_abilities', all_abilities):
            for i in xrange(1000):
                chosen_abilities.add(actor.choose_ability().get_id())

        self.assertEqual(active_abilities, chosen_abilities)


    def test_choose_ability__additional_companion_abilities(self):
        from the_tale.game.heroes.habilities import ABILITIES
        from the_tale.game.companions.abilities import effects as companions_effects
        from the_tale.game.companions.abilities import container as abilities_container
        from the_tale.game.companions import logic as companions_logic
        from the_tale.game.companions import relations as companions_relations

        abilities = [ability for ability in companions_effects.ABILITIES.records
                     if ( isinstance(ability.effect, companions_effects.BaseBattleAbility) and
                          ability.effect.ABILITY.get_id() != 'hit' )]
        companion_ability = random.choice(abilities)

        all_abilities = [ability(level=ability.MAX_LEVEL)
                         for ability in ABILITIES.values()
                         if ability.get_id() != companion_ability.effect.ABILITY.get_id()]

        active_abilities = set(ability.get_id() for ability in ABILITIES.values() if ability.ACTIVATION_TYPE.is_ACTIVE)

        companion_record = companions_logic.create_random_companion_record(u'battle',
                                                                           abilities=abilities_container.Container(start=(companion_ability,)),
                                                                           state=companions_relations.STATE.ENABLED)
        self.hero.set_companion(companions_logic.create_companion(companion_record))
        self.hero.health = 1 # allow regeneration

        actor = battle.Actor(self.hero, BattleContext())

        chosen_abilities = set()

        # mock abilities modify_attribute instead of hereos, since we test correct work of it
        def modify_attribute(self, modifier, value):
            if modifier.is_ADDITIONAL_ABILITIES:
                return all_abilities
            return value

        with mock.patch('the_tale.game.heroes.habilities.AbilitiesPrototype.modify_attribute', modify_attribute):
            for i in xrange(1000):
                chosen_abilities.add(actor.choose_ability().get_id())

        self.assertEqual(len(active_abilities), len(chosen_abilities) + 1)
        self.assertEqual(active_abilities - set([companion_ability.effect.ABILITY.get_id()]), chosen_abilities)

    def test_initiative_change(self):
        actor = battle.Actor(self.hero, BattleContext())
        actor.context.use_initiative([2])
        self.assertEqual(actor.initiative, self.hero.initiative*2)

    @mock.patch('the_tale.game.mobs.prototypes.MobPrototype.additional_abilities', [VAMPIRE_STRIKE(level=1)])
    def test_mob_actor(self):
        mob = mobs_storage.get_random_mob(self.hero)
        mob.health = 10
        mob.abilities.add(RUN_UP_PUSH.get_id())

        actor = battle.Actor(mob, BattleContext())

        self.assertEqual(mob.initiative, actor.initiative)
        self.assertEqual(mob.name, actor.name)
        self.assertEqual(mob.utg_name, actor.utg_name)
        self.assertEqual(mob.basic_damage, actor.basic_damage)
        self.assertEqual(mob.health, actor.health)
        self.assertEqual(mob.max_health, actor.max_health)

        self.assertEqual(actor.change_health(-5), -5)
        self.assertEqual(actor.health, 5)

        self.assertEqual(actor.change_health(-50), -5)
        self.assertEqual(actor.health, 0)

        self.assertEqual(actor.change_health(actor.max_health+50), actor.max_health)
        self.assertEqual(actor.health, actor.max_health)

        hit_selected = False
        run_up_push_selected = False
        vampire_strike_selected = False
        for i in xrange(100):
            ability = actor.choose_ability()

            if ability.get_id() == HIT.get_id():
               hit_selected = True
            elif ability.get_id() == RUN_UP_PUSH.get_id():
                run_up_push_selected = True
            elif ability.get_id() == VAMPIRE_STRIKE.get_id():
                vampire_strike_selected = True

        self.assertTrue(hit_selected)
        self.assertTrue(run_up_push_selected)
        self.assertTrue(vampire_strike_selected)

        self.storage._test_save()

    def test_process_effects(self):
        actor = battle.Actor(self.hero, BattleContext())

        actor.context.use_damage_queue_fire([p.Damage(50, 50), p.Damage(50, 50)])
        actor.context.use_damage_queue_poison([p.Damage(50, 50), p.Damage(50, 50)])
        actor.context.on_own_turn()

        actor.context.use_incoming_damage_modifier(physic=1.0, magic=0.8)
        actor.process_effects(self.hero)
        self.assertEqual(self.hero.health, self.hero.max_health - 180)

        actor.context.on_own_turn()
        actor.context.use_incoming_damage_modifier(physic=1.2, magic=1.0)
        actor.process_effects(self.hero)
        self.assertEqual(self.hero.health, self.hero.max_health - 180 - 220)


    def check_first_strike(self, actor_1, actor_2, turn, expected_actors):
        with mock.patch('the_tale.game.actions.battle.strike') as strike:
            for i in xrange(100):
                actor_1.context.turn = turn
                actor_2.context.turn = turn
                battle.make_turn(actor_1, actor_2, self.hero)

        self.assertEqual(set(id(call[1]['attacker']) for call in strike.call_args_list), expected_actors)

    def test_first_strike__no_actors(self):
        actor_1, actor_2 = self.get_actors()
        self.check_first_strike(actor_1, actor_2, turn=0, expected_actors=set((id(actor_1), id(actor_2))))

    def test_first_strike__actor_1(self):
        actor_1, actor_2 = self.get_actors()
        actor_1.context.use_first_strike()
        self.check_first_strike(actor_1, actor_2, turn=0, expected_actors=set((id(actor_1),)))

    def test_first_strike__actor_2(self):
        actor_1, actor_2 = self.get_actors()
        actor_2.context.use_first_strike()
        self.check_first_strike(actor_1, actor_2, turn=0, expected_actors=set((id(actor_2),)))

    def test_first_strike__actor_1__not_first_turns(self):
        actor_1, actor_2 = self.get_actors()
        actor_1.context.use_first_strike()
        self.check_first_strike(actor_1, actor_2, turn=1, expected_actors=set((id(actor_1), id(actor_2))))

    def test_first_strike__actor_2__not_first_turns(self):
        actor_1, actor_2 = self.get_actors()
        actor_2.context.use_first_strike()
        self.check_first_strike(actor_1, actor_2, turn=1, expected_actors=set((id(actor_1), id(actor_2))))

    def test_first_strike__actors_1_and_2(self):
        actor_1, actor_2 = self.get_actors()
        actor_1.context.use_first_strike()
        actor_2.context.use_first_strike()
        self.check_first_strike(actor_1, actor_2, turn=0, expected_actors=set((id(actor_1), id(actor_2))))

    def test_make_turn__initialize_contexts_on_first_turn(self):
        actor_1, actor_2 = self.get_actors()

        with mock.patch('the_tale.game.actions.battle.strike') as strike:
            with mock.patch('the_tale.game.actions.battle.Actor.update_context') as update_context:
                battle.make_turn(actor_1, actor_2, self.hero)

        self.assertEqual(strike.call_count, 1)
        self.assertEqual(update_context.call_args_list, [mock.call(actor_2), mock.call(actor_1)])


    @mock.patch('the_tale.game.actions.contexts.battle.BattleContext._on_every_turn', mock.Mock())
    def test_last_chance__attacker(self):
        actor_1, actor_2 = self.get_actors()
        actor_1.change_health(-actor_1.health)
        self.assertEqual(actor_1.health, 0)
        actor_1.context.use_last_chance_probability(1.0)
        battle.strike(actor_1, actor_2, mock.Mock())
        self.assertEqual(actor_1.health, 1)

    @mock.patch('the_tale.game.actions.contexts.battle.BattleContext._on_every_turn', mock.Mock())
    def test_last_chance__defender(self):
        actor_1, actor_2 = self.get_actors()
        actor_2.change_health(-actor_2.health)
        self.assertEqual(actor_2.health, 0)

        actor_2.context.use_last_chance_probability(1.0)
        battle.strike(actor_1, actor_2, mock.Mock())
        self.assertEqual(actor_2.health, 1)

    @mock.patch('the_tale.game.actions.contexts.battle.BattleContext._on_every_turn', mock.Mock())
    def test_last_chance__second_use(self):
        actor_1, actor_2 = self.get_actors()
        actor_1.context.use_last_chance_probability(1.0)

        actor_1.change_health(-actor_1.health)
        self.assertEqual(actor_1.health, 0)
        battle.strike(actor_1, actor_2, mock.Mock())
        self.assertEqual(actor_1.health, 1)

        actor_1.change_health(-actor_1.health)
        self.assertEqual(actor_1.health, 0)
        battle.strike(actor_1, actor_2, mock.Mock())
        self.assertEqual(actor_1.health, 1)