示例#1
0
def test_shoot_at():
    # This test greatly increases the testing duration because the tested
    # methods involve various sleeps
    hp = 10
    a = Unit(hp, 100, 10, 1, 'Test', 'Alfa', '', BallisticPistol(), [],
             [])
    b = Unit(hp, 50, 10, 1, 'Test', 'Bravo', '', None, [], [])

    # randomness is difficult to test
    test_fulfilled = False
    while not test_fulfilled:
        if a.shoot_at(b):
            # the method reported success, so unit a should have hit
            assert b.hp < hp
            test_fulfilled = True
        # else: the method reported a miss, try again
        # reload the weapon; weapon.reload() is not used because it sleeps
        a.weapon.ammo = a.weapon.clip_size

    b.hp = hp
    a.aim = 0
    test_fulfilled = False
    while not test_fulfilled:
        if a.shoot_at(b):
            # the method reported a hit, reset hp
            b.hp = hp
            # reload the weapon; weapon.reload() is not used because it sleeps
            a.weapon.ammo = a.weapon.clip_size
        else:
            assert b.hp == hp
            test_fulfilled = True
示例#2
0
def test_aim_at():
    aim_value = 50
    a = Unit(10, aim_value, 10, 1, 'Test', 'Alfa', '', BallisticPistol(), [],
             [])
    b = Unit(10, aim_value, 10, 1, 'Test', 'Bravo', '', None, [], [])
    # Without modifiers, hit chance is equal to the aim value
    b.cover = COVER_NONE
    assert a.aim_at(b) == aim_value

    # cover is discounted from the aim, a flanked target is easier to hit
    for cover in [COVER_FULL, COVER_HALF, COVER_FLANKED]:
        b.cover = cover
        assert a.aim_at(b) == aim_value - cover
    b.cover = COVER_NONE

    # a scope gives an aim bonus
    a.items = [ITEM_SCOPE]
    assert a.aim_at(b) == aim_value + 10
    a.items = []

    # carbines give an aim bonus
    for carbine in [BallisticCarbine, LaserCarbine, PlasmaCarbine]:
        a.weapon = carbine()
        assert a.aim_at(b) == aim_value + 10
    a.weapon = BallisticPistol()

    # minimum hit chance is 5%
    a.aim = 0
    assert a.aim_at(b) == 5
    # maximum hit chance is 95%
    a.aim = 100
    assert a.aim_at(b) == 95
    # an aim higher than the maximum is allowed, if the resulting chance is
    # at maximum 95%
    b.cover = COVER_HALF
    assert a.aim_at(b) == 100 - COVER_HALF
    a.aim = aim_value

    # boni and modifiers stack correctly
    a.weapon = BallisticCarbine()
    a.items = [ITEM_SCOPE]
    b.cover = COVER_HALF
    assert a.aim_at(b) == aim_value + 10 + 10 - COVER_HALF