Ejemplo n.º 1
0
def res_random(inst, res):
    """Randomly choose one of the sub-results to execute.

    The "chance" value defines the percentage chance for any result to be
    chosen. "weights" defines the weighting for each result. Wrap a set of
    results in a "group" property block to treat them as a single result to be
    executed in order.
    """
    # Note: 'global' results like "Has" won't delete themselves!
    # Instead they're replaced by 'dummy' results that don't execute.
    # Otherwise the chances would be messed up.
    seed, chance, weight, results = res.value
    random.seed("random_case_{}:{}_{}_{}".format(seed, inst["targetname", ""], inst["origin"], inst["angles"]))
    if random.randrange(100) > chance:
        return

    ind = random.choice(weight)
    choice = results[ind]  # type: Property
    if choice.name == "group":
        for sub_res in choice.value:
            should_del = Condition.test_result(inst, sub_res)
            if should_del is RES_EXHAUSTED:
                # This Result doesn't do anything!
                sub_res.name = "nop"
                sub_res.value = None
    else:
        should_del = Condition.test_result(inst, choice)
        if should_del is RES_EXHAUSTED:
            choice.name = "nop"
            choice.value = None
def res_random(inst: Entity, res: Property):
    """Randomly choose one of the sub-results to execute.

    The "chance" value defines the percentage chance for any result to be
    chosen. "weights" defines the weighting for each result. Wrap a set of
    results in a "group" property block to treat them as a single result to be
    executed in order.
    """
    # Note: 'global' results like "Has" won't delete themselves!
    # Instead they're replaced by 'dummy' results that don't execute.
    # Otherwise the chances would be messed up.
    seed, chance, weight, results = res.value
    random.seed('random_case_{}:{}_{}_{}'.format(
        seed,
        inst['targetname', ''],
        inst['origin'],
        inst['angles'],
    ))
    if random.randrange(100) > chance:
        return

    ind = random.choice(weight)
    choice = results[ind]  # type: Property
    if choice.name == 'group':
        for sub_res in choice.value:
            should_del = Condition.test_result(
                inst,
                sub_res,
            )
            if should_del is RES_EXHAUSTED:
                # This Result doesn't do anything!
                sub_res.name = 'nop'
                sub_res.value = None
    else:
        should_del = Condition.test_result(
            inst,
            choice,
        )
        if should_del is RES_EXHAUSTED:
            choice.name = 'nop'
            choice.value = None
Ejemplo n.º 3
0
def res_random(inst: Entity, res: Property) -> None:
    """Randomly choose one of the sub-results to execute.

    The `chance` value defines the percentage chance for any result to be
    chosen. `weights` defines the weighting for each result. Both are
    comma-separated, matching up with the results following. Wrap a set of
    results in a `group` property block to treat them as a single result to be
    executed in order.
    """
    # Note: 'global' results like "Has" won't delete themselves!
    # Instead they're replaced by 'dummy' results that don't execute.
    # Otherwise the chances would be messed up.
    seed, chance, weight, results = res.value  # type: str, float, List[int], List[Property]

    set_random_seed(inst, seed)
    if random.randrange(100) > chance:
        return

    ind = random.choice(weight)
    choice = results[ind]
    if choice.name == 'nop':
        pass
    elif choice.name == 'group':
        for sub_res in choice:
            should_del = Condition.test_result(
                inst,
                sub_res,
            )
            if should_del is RES_EXHAUSTED:
                # This Result doesn't do anything!
                sub_res.name = 'nop'
                sub_res.value = None
    else:
        should_del = Condition.test_result(
            inst,
            choice,
        )
        if should_del is RES_EXHAUSTED:
            choice.name = 'nop'
            choice.value = None
Ejemplo n.º 4
0
def res_random(inst: Entity, res: Property) -> None:
    """Randomly choose one of the sub-results to execute.

    The "chance" value defines the percentage chance for any result to be
    chosen. "weights" defines the weighting for each result. Wrap a set of
    results in a "group" property block to treat them as a single result to be
    executed in order.
    """
    # Note: 'global' results like "Has" won't delete themselves!
    # Instead they're replaced by 'dummy' results that don't execute.
    # Otherwise the chances would be messed up.
    seed, chance, weight, results = res.value  # type: str, float, List[int], List[Property]

    set_random_seed(inst, seed)
    if random.randrange(100) > chance:
        return

    ind = random.choice(weight)
    choice = results[ind]
    if choice.name == 'nop':
        pass
    elif choice.name == 'group':
        for sub_res in choice:
            should_del = Condition.test_result(
                inst,
                sub_res,
            )
            if should_del is RES_EXHAUSTED:
                # This Result doesn't do anything!
                sub_res.name = 'nop'
                sub_res.value = None
    else:
        should_del = Condition.test_result(
            inst,
            choice,
        )
        if should_del is RES_EXHAUSTED:
            choice.name = 'nop'
            choice.value = None