コード例 #1
0
ファイル: ai.py プロジェクト: witheld9/r3-tdw
def _human_logic(entity):
    _t = time.time()
    ai_visuals.build_item_list(entity)
    ai_visuals.build_life_list(entity)

    if ai_flow.is_flow_active() and not ai_flow.can_act(entity):
        return

    if timers.has_timer_with_name(entity, "passout"):
        return

        # if not ai_squads.is_active(ai_squads.get_assigned_squad(entity)) or entity['stats']['action_points'] <= 0:
        # 	return

    _old_meta = entity["ai"]["meta"].copy()

    entity["ai"]["meta"]["sees_item_type_weapon"] = len(entity["ai"]["visible_items"]["weapon"]) > 0
    entity["ai"]["meta"]["sees_item_type_ammo"] = len(entity["ai"]["visible_items"]["ammo"]) > 0
    entity["ai"]["meta"]["sees_item_type_container"] = len(entity["ai"]["visible_items"]["container"]) > 0
    entity["ai"]["meta"]["has_weapon"] = len(items.get_items_in_holder(entity, "weapon")) > 0
    entity["ai"]["meta"]["has_ammo"] = len(items.get_items_matching(entity, {"type": "ammo"})) > 0
    entity["ai"]["meta"]["has_container"] = len(items.get_items_matching(entity, {"type": "container"})) > 0
    entity["ai"]["meta"]["weapon_loaded"] = (
        len(
            [
                w
                for w in items.get_items_in_holder(entity, "weapon")
                if entities.get_entity(w)["flags"]["ammo"]["value"] > 0
            ]
        )
        > 0
    )
    entity["ai"]["meta"]["in_engagement"] = (
        len([t for t in entity["ai"]["targets"] if not entity["ai"]["life_memory"][t]["is_lost"]]) > 0
    )
    entity["ai"]["meta"]["has_lost_target"] = len(entity["ai"]["targets_to_search"]) > 0
    entity["ai"]["meta"]["in_enemy_los"] = (
        len([t for t in entity["ai"]["targets"] if entity["ai"]["life_memory"][t]["in_los"]]) > 0
    )
    entity["ai"]["meta"]["has_needs"] = (
        not entity["ai"]["meta"]["has_weapon"]
        or not entity["ai"]["meta"]["has_container"]
        or not entity["ai"]["meta"]["weapon_loaded"]
    )
    entity["ai"]["meta"]["is_injured"] = skeleton.has_critical_injury(entity)

    if not entity["ai"]["meta"] == _old_meta:
        entities.trigger_event(entity, "meta_change")

    if entity["ai"]["meta"]["in_engagement"]:
        _target = entity["ai"]["nearest_target"]
        _target_distance = numbers.distance(movement.get_position_via_id(_target), movement.get_position(entity))
        _engage_distance = stats.get_vision(entity) * 0.75
        _weapon = entities.get_entity(items.get_items_in_holder(entity, "weapon")[0])
        _engage_distance = numbers.clip(
            _engage_distance - (flags.get_flag(_weapon, "accuracy") * 3), 1, stats.get_vision(entity)
        )
        _min_engage_distance = 3

        if _weapon["stats"]["kind"] == "explosive":
            _engage_distance /= 2
            _min_engage_distance = 8

        entities.trigger_event(entity, "set_flag", flag="engage_distance", value=_engage_distance)
        entities.trigger_event(entity, "set_flag", flag="min_engage_distance", value=_min_engage_distance)

        # NOTE: Mirror change in ai_logic!
        entity["ai"]["meta"]["in_firing_range"] = (
            _target_distance <= _engage_distance and _target_distance >= _min_engage_distance
        )

        if entity["ai"]["meta"]["in_enemy_los"]:
            if flags.has_flag(entity, "search_nodes"):
                flags.delete_flag(entity, "search_nodes")

            entity["ai"]["meta"]["is_in_melee_range"] = _target_distance == 1

    else:
        entity["ai"]["meta"]["is_target_near"] = False
        entity["ai"]["meta"]["in_firing_range"] = False

    entity["ai"]["meta"]["is_target_armed"] = (
        len([t for t in entity["ai"]["targets"] if entity["ai"]["life_memory"][t]["is_armed"]]) > 0
    )
    entity["ai"]["meta"]["is_panicked"] = (
        not entity["ai"]["meta"]["weapon_loaded"] and entity["ai"]["meta"]["is_target_armed"]
    )

    if entity["ai"]["is_player"]:
        return

        # TODO: Experimental!
        # if entity['ai']['meta'] == _old_meta:
        # print 'Something changed...'

        # return

    if (
        timers.has_timer_with_name(entity, "shoot")
        or entity["movement"]["path"]["positions"]
        or timers.has_timer_with_name(entity, "move")
    ):
        # print 'Clearing existing action...'
        return

    _goap = _handle_goap(entity)

    if not _goap:
        entity["ai"]["current_action"] = "idle"

        entities.trigger_event(entity, "finish_turn")
        entities.trigger_event(entity, "stop")

        # print
        # print entity['stats']['name'], 'no possible action'
        # print

        # for meta_name in entity['ai']['meta']:
        # 	print meta_name, '\t', entity['ai']['meta'][meta_name]

        return

    _plan = _goap[0]

    if not entity["ai"]["last_action"] == _plan["actions"][0]["name"]:
        if entity["_id"] in ai_debugger.WATCHING:
            logging.info("%s: %s -> %s" % (entity["_id"], entity["ai"]["last_action"], _plan["actions"][0]["name"]))

            # TODO: Only do this if the action requires movement changes
        entities.trigger_event(entity, "stop")

        entity["ai"]["last_action"] = _plan["actions"][0]["name"]

        # print entity['stats']['name'], _plan['actions'][0]['name']

    _plan["planner"].trigger_callback(entity, _plan["actions"][0]["name"])

    # print time.time() - _t

    entity["ai"]["current_action"] = _plan["actions"][0]["name"]
コード例 #2
0
ファイル: ai.py プロジェクト: penny64/r3-tdw
def _human_logic(entity):
    _t = time.time()
    ai_visuals.build_item_list(entity)
    ai_visuals.build_life_list(entity)

    if ai_flow.is_flow_active() and not ai_flow.can_act(entity):
        return

    if timers.has_timer_with_name(entity, 'passout'):
        return

    #if not ai_squads.is_active(ai_squads.get_assigned_squad(entity)) or entity['stats']['action_points'] <= 0:
    #	return

    _old_meta = entity['ai']['meta'].copy()

    entity['ai']['meta']['sees_item_type_weapon'] = len(
        entity['ai']['visible_items']['weapon']) > 0
    entity['ai']['meta']['sees_item_type_ammo'] = len(
        entity['ai']['visible_items']['ammo']) > 0
    entity['ai']['meta']['sees_item_type_container'] = len(
        entity['ai']['visible_items']['container']) > 0
    entity['ai']['meta']['has_weapon'] = len(
        items.get_items_in_holder(entity, 'weapon')) > 0
    entity['ai']['meta']['has_ammo'] = len(
        items.get_items_matching(entity, {'type': 'ammo'})) > 0
    entity['ai']['meta']['has_container'] = len(
        items.get_items_matching(entity, {'type': 'container'})) > 0
    entity['ai']['meta']['weapon_loaded'] = len([
        w for w in items.get_items_in_holder(entity, 'weapon')
        if entities.get_entity(w)['flags']['ammo']['value'] > 0
    ]) > 0
    entity['ai']['meta']['in_engagement'] = len([
        t for t in entity['ai']['targets']
        if not entity['ai']['life_memory'][t]['is_lost']
    ]) > 0
    entity['ai']['meta']['has_lost_target'] = len(
        entity['ai']['targets_to_search']) > 0
    entity['ai']['meta']['in_enemy_los'] = len([
        t for t in entity['ai']['targets']
        if entity['ai']['life_memory'][t]['in_los']
    ]) > 0
    entity['ai']['meta']['has_needs'] = not entity['ai']['meta'][
        'has_weapon'] or not entity['ai']['meta'][
            'has_container'] or not entity['ai']['meta']['weapon_loaded']
    entity['ai']['meta']['is_injured'] = skeleton.has_critical_injury(entity)

    if not entity['ai']['meta'] == _old_meta:
        entities.trigger_event(entity, 'meta_change')

    if entity['ai']['meta']['in_engagement']:
        _target = entity['ai']['nearest_target']
        _target_distance = numbers.distance(
            movement.get_position_via_id(_target),
            movement.get_position(entity))
        _engage_distance = stats.get_vision(entity) * .75
        _weapon = entities.get_entity(
            items.get_items_in_holder(entity, 'weapon')[0])
        _engage_distance = numbers.clip(
            _engage_distance - (flags.get_flag(_weapon, 'accuracy') * 3), 1,
            stats.get_vision(entity))
        _min_engage_distance = 3

        if _weapon['stats']['kind'] == 'explosive':
            _engage_distance /= 2
            _min_engage_distance = 8

        entities.trigger_event(entity,
                               'set_flag',
                               flag='engage_distance',
                               value=_engage_distance)
        entities.trigger_event(entity,
                               'set_flag',
                               flag='min_engage_distance',
                               value=_min_engage_distance)

        #NOTE: Mirror change in ai_logic!
        entity['ai']['meta'][
            'in_firing_range'] = _target_distance <= _engage_distance and _target_distance >= _min_engage_distance

        if entity['ai']['meta']['in_enemy_los']:
            if flags.has_flag(entity, 'search_nodes'):
                flags.delete_flag(entity, 'search_nodes')

            entity['ai']['meta']['is_in_melee_range'] = _target_distance == 1

    else:
        entity['ai']['meta']['is_target_near'] = False
        entity['ai']['meta']['in_firing_range'] = False

    entity['ai']['meta']['is_target_armed'] = len([
        t for t in entity['ai']['targets']
        if entity['ai']['life_memory'][t]['is_armed']
    ]) > 0
    entity['ai']['meta']['is_panicked'] = (
        not entity['ai']['meta']['weapon_loaded']
        and entity['ai']['meta']['is_target_armed'])

    if entity['ai']['is_player']:
        return

    #TODO: Experimental!
    #if entity['ai']['meta'] == _old_meta:
    #print 'Something changed...'

    #return

    if timers.has_timer_with_name(
            entity, 'shoot'
    ) or entity['movement']['path']['positions'] or timers.has_timer_with_name(
            entity, 'move'):
        #print 'Clearing existing action...'
        return

    _goap = _handle_goap(entity)

    if not _goap:
        entity['ai']['current_action'] = 'idle'

        entities.trigger_event(entity, 'finish_turn')
        entities.trigger_event(entity, 'stop')

        #print
        #print entity['stats']['name'], 'no possible action'
        #print

        #for meta_name in entity['ai']['meta']:
        #	print meta_name, '\t', entity['ai']['meta'][meta_name]

        return

    _plan = _goap[0]

    if not entity['ai']['last_action'] == _plan['actions'][0]['name']:
        if entity['_id'] in ai_debugger.WATCHING:
            logging.info('%s: %s -> %s' %
                         (entity['_id'], entity['ai']['last_action'],
                          _plan['actions'][0]['name']))

        #TODO: Only do this if the action requires movement changes
        entities.trigger_event(entity, 'stop')

        entity['ai']['last_action'] = _plan['actions'][0]['name']

    #print entity['stats']['name'], _plan['actions'][0]['name']

    _plan['planner'].trigger_callback(entity, _plan['actions'][0]['name'])

    #print time.time() - _t

    entity['ai']['current_action'] = _plan['actions'][0]['name']
コード例 #3
0
ファイル: ai.py プロジェクト: witheld9/r3-tdw
def _animal_logic(entity):
    if timers.has_timer_with_name(entity, "passout"):
        return

    ai_visuals.build_item_list(entity)
    ai_visuals.build_life_list(entity)

    _old_meta = entity["ai"]["meta"].copy()

    entity["ai"]["meta"]["in_engagement"] = len(entity["ai"]["targets"]) > 0
    entity["ai"]["meta"]["in_enemy_los"] = (
        len([t for t in entity["ai"]["targets"] if entity["ai"]["life_memory"][t]["in_los"]]) > 0
    )

    if not entity["ai"]["meta"] == _old_meta:
        entities.trigger_event(entity, "meta_change")

    if entity["ai"]["meta"]["in_engagement"]:
        _target = entity["ai"]["nearest_target"]
        _target_distance = numbers.distance(movement.get_position_via_id(_target), movement.get_position(entity))

        entity["ai"]["meta"]["is_target_near"] = _target_distance <= 25

        if not entity["ai"]["meta"]["in_enemy_los"] and life.can_see_position(
            entity, entity["ai"]["life_memory"][_target]["last_seen_at"]
        ):
            entity["ai"]["meta"]["has_lost_target"] = entity["ai"]["meta"]["is_target_near"]

        elif entity["ai"]["meta"]["in_enemy_los"]:
            if flags.has_flag(entity, "search_nodes"):
                flags.delete_flag(entity, "search_nodes")

            entity["ai"]["meta"]["is_in_melee_range"] = _target_distance == 1

    else:
        entity["ai"]["meta"]["is_target_near"] = False

    entity["ai"]["meta"]["is_target_armed"] = (
        len([t for t in entity["ai"]["targets"] if entity["ai"]["life_memory"][t]["is_armed"]]) > 0
    )
    # entity['ai']['meta']['is_panicked'] = skeleton.has_critical_injury(entity)
    entity["ai"]["meta"]["is_injured"] = skeleton.has_critical_injury(entity)
    entity["ai"]["meta"]["is_panicked"] = entity["ai"]["meta"]["is_injured"]

    if entity["ai"]["is_player"]:
        return

    _goap = _handle_goap(entity)

    if not _goap:
        entity["ai"]["current_action"] = "idle"

        return

    _plan = _goap[0]
    _plan["planner"].trigger_callback(entity, _plan["actions"][0]["name"])
    # print time.time() - _t

    if not entity["ai"]["last_action"] == _plan["actions"][0]["name"]:
        logging.debug("%s: %s -> %s" % (entity["_id"], entity["ai"]["last_action"], _plan["actions"][0]["name"]))

        entity["ai"]["last_action"] = _plan["actions"][0]["name"]

    entity["ai"]["current_action"] = _plan["actions"][0]["name"]
コード例 #4
0
ファイル: ai.py プロジェクト: penny64/r3-tdw
def _animal_logic(entity):
    if timers.has_timer_with_name(entity, 'passout'):
        return

    ai_visuals.build_item_list(entity)
    ai_visuals.build_life_list(entity)

    _old_meta = entity['ai']['meta'].copy()

    entity['ai']['meta']['in_engagement'] = len(entity['ai']['targets']) > 0
    entity['ai']['meta']['in_enemy_los'] = len([
        t for t in entity['ai']['targets']
        if entity['ai']['life_memory'][t]['in_los']
    ]) > 0

    if not entity['ai']['meta'] == _old_meta:
        entities.trigger_event(entity, 'meta_change')

    if entity['ai']['meta']['in_engagement']:
        _target = entity['ai']['nearest_target']
        _target_distance = numbers.distance(
            movement.get_position_via_id(_target),
            movement.get_position(entity))

        entity['ai']['meta']['is_target_near'] = _target_distance <= 25

        if not entity['ai']['meta']['in_enemy_los'] and life.can_see_position(
                entity, entity['ai']['life_memory'][_target]['last_seen_at']):
            entity['ai']['meta']['has_lost_target'] = entity['ai']['meta'][
                'is_target_near']

        elif entity['ai']['meta']['in_enemy_los']:
            if flags.has_flag(entity, 'search_nodes'):
                flags.delete_flag(entity, 'search_nodes')

            entity['ai']['meta']['is_in_melee_range'] = _target_distance == 1

    else:
        entity['ai']['meta']['is_target_near'] = False

    entity['ai']['meta']['is_target_armed'] = len([
        t for t in entity['ai']['targets']
        if entity['ai']['life_memory'][t]['is_armed']
    ]) > 0
    #entity['ai']['meta']['is_panicked'] = skeleton.has_critical_injury(entity)
    entity['ai']['meta']['is_injured'] = skeleton.has_critical_injury(entity)
    entity['ai']['meta']['is_panicked'] = entity['ai']['meta']['is_injured']

    if entity['ai']['is_player']:
        return

    _goap = _handle_goap(entity)

    if not _goap:
        entity['ai']['current_action'] = 'idle'

        return

    _plan = _goap[0]
    _plan['planner'].trigger_callback(entity, _plan['actions'][0]['name'])
    #print time.time() - _t

    if not entity['ai']['last_action'] == _plan['actions'][0]['name']:
        logging.debug('%s: %s -> %s' %
                      (entity['_id'], entity['ai']['last_action'],
                       _plan['actions'][0]['name']))

        entity['ai']['last_action'] = _plan['actions'][0]['name']

    entity['ai']['current_action'] = _plan['actions'][0]['name']