示例#1
0
def make_target_surrender(entity):
    _last_seen_at = entity['ai']['life_memory'][
        entity['ai']['nearest_target']]['last_seen_at']

    movement.walk_to_position(entity, _last_seen_at[0], _last_seen_at[1],
                              zones.get_active_astar_map(),
                              zones.get_active_weight_map())
示例#2
0
文件: nodes.py 项目: penny64/r3-tdw
def create_walk_node(entity, x, y, clear=False):
    _walk_speed = chr(31)

    if clear:
        clear_path(entity)
        redraw_path(entity)

    _node = _create_node(entity,
                         x,
                         y,
                         draw_path=True,
                         passive=False,
                         name=_walk_speed,
                         callback_on_touch=False)

    entities.trigger_event(_node, 'set_char', char='O')

    entity['node_grid']['nodes'][
        _node['_id']]['callback'] = lambda: entities.trigger_event(
            entity,
            'move_to_position',
            x=_node['x'],
            y=_node['y'],
            astar_map=zones.get_active_astar_map(),
            weight_map=zones.get_active_weight_map())
示例#3
0
def _search_for_target(entity, target_id):
    _nodes = flags.get_flag(entity, 'search_nodes')

    if not _nodes:
        flags.delete_flag(entity, 'search_nodes')

        entities.trigger_event(entity,
                               'target_search_failed',
                               target_id=target_id)

        return

    _node_list = _nodes.keys()
    _node_list.sort()
    _node_x, _node_y = _nodes[_node_list[0]][0]
    _distance = numbers.distance(movement.get_position(entity),
                                 (_node_x, _node_y))

    if _distance <= 15 and life.can_see_position(entity, (_node_x, _node_y)):
        _nodes[_node_list[0]].remove((_node_x, _node_y))

        if not _nodes[_node_list[0]]:
            del _nodes[_node_list[0]]
    else:
        movement.walk_to_position(entity, _node_x, _node_y,
                                  zones.get_active_astar_map(),
                                  zones.get_active_weight_map())
示例#4
0
def handle_keyboard_input():
    if controls.get_input_char_pressed('\t'):
        if settings.TICK_MODE == 'strategy':
            if ui_panel.ACTIVE_MENU:
                ui_panel.close()
            else:
                ui_panel.show_inventory(entity)

        else:
            _x, _y = movement.get_position(entity)

            camera.set_pos(_x - constants.MAP_VIEW_WIDTH / 2,
                           _y - constants.MAP_VIEW_HEIGHT / 2)

    if controls.get_input_char_pressed('O'):
        settings.toggle_show_node_grid()

    elif controls.get_input_char_pressed('o'):
        settings.toggle_observer_mode()

    if controls.get_input_char_pressed('d'):
        _mx, _my = ui_cursor.get_map_position()

        for entity_id in entities.get_entity_group('life'):
            _entity = entities.get_entity(entity_id)

            if not numbers.distance(
                (_mx, _my), movement.get_position(_entity)):
                _x, _y = ui_cursor.get_screen_position()
                _menu = ui_menu.create(_x, _y, title='Debug')
                ui_menu.add_selectable(_menu, 'Show Metas',
                                       lambda: _show_metas(_entity))
                ui_menu.add_selectable(
                    _menu, 'Kill',
                    lambda: entities.trigger_event(_entity, 'kill'))
                ui_menu.add_selectable(_menu, 'Overwatch',
                                       lambda: ai_debugger.register(_entity))

                break

    if controls.get_input_char_pressed('l'):
        _x, _y = ui_cursor.get_screen_position()
        _mx, _my = ui_cursor.get_map_position()
        _weight = zones.get_active_weight_map()[_my, _mx]
        _menu = ui_menu.create(_x, _y, title='Tile Info')

        ui_menu.add_selectable(
            _menu, 'Warp', lambda: entities.trigger_event(
                entity, 'set_position', x=_mx, y=_my))
        ui_menu.add_selectable(
            _menu, 'Light', lambda: effects.light(_mx,
                                                  _my,
                                                  15,
                                                  r=random.uniform(1.0, 1.5),
                                                  g=random.uniform(1.0, 1.5),
                                                  light_map='static_lighting'))
        ui_menu.add_selectable(_menu, 'Weight: %s' % _weight, lambda: _)
示例#5
0
def handle_raid(entity, camp_id):
    entity['movement']['override_speed'] = 2  #60 * 1

    ai_factions.FACTIONS[entity['faction']]['money'] -= get_attack_cost(
        entity, camp_id)

    movement.walk_to_position(entity, camp_id[0], camp_id[1],
                              zones.get_active_astar_map(),
                              zones.get_active_weight_map())
示例#6
0
def find_cover(entity):
    _squad = entities.get_entity(ai_factions.FACTIONS[entity['ai']['faction']]
                                 ['squads'][entity['ai']['squad']])
    _cover_position = ai_squad_director.get_cover_position(
        _squad, entity['_id'])

    if not _cover_position or not numbers.distance(
            movement.get_position(entity), _cover_position):
        entities.trigger_event(entity, 'finish_turn')

        return

    movement.walk_to_position(entity, _cover_position[0], _cover_position[1],
                              zones.get_active_astar_map(),
                              zones.get_active_weight_map())
示例#7
0
def find_push_position(entity):
    _squad = entities.get_entity(ai_factions.FACTIONS[entity['ai']['faction']]
                                 ['squads'][entity['ai']['squad']])
    _x, _y = movement.get_position(entity)
    _push_position = ai_squad_director.get_push_position(_squad, entity['_id'])

    if not _push_position:
        return

    #if not numbers.distance((_x, _y), _push_position) and not entity['ai']['visible_targets']:
    #	entity['ai']['meta']['has_lost_target'] = True

    movement.walk_to_position(entity, _push_position[0], _push_position[1],
                              zones.get_active_astar_map(),
                              zones.get_active_weight_map())
示例#8
0
def _get_item(entity, item_id, hold=False, weight=None):
	_item = entities.get_entity(item_id)
	_x, _y = movement.get_position(_item)
	_distance = numbers.distance(movement.get_position(entity), (_x, _y))
	
	if weight:
		ai.set_meta_weight(entity, weight, 10*numbers.clip(_distance/30.0, 0, 1))
	
	if _distance:
		movement.walk_to_position(entity, _x, _y, zones.get_active_astar_map(), zones.get_active_weight_map(), smp=True)
	
	else:
		if hold:
			life.get_and_hold_item(entity, item_id)
		else:
			life.get_and_store_item(entity, item_id)
示例#9
0
def handle_movement_order(x, y):
	global WALK_PATH, WALK_DEST
	
	if not is_squad_member_selected() or (x, y) in zones.get_active_solids({}, ignore_calling_entity=True):
		return False
	
	_entity = get_selected_squad_member()
	
	movement.walk_to_position(_entity, x, y, zones.get_active_astar_map(), zones.get_active_weight_map())
	
	WALK_PATH = _entity['movement']['path']['positions']
	WALK_DEST = _entity['movement']['path']['destination']
	
	settings.set_tick_mode('normal')
	
	return True
示例#10
0
def handle_movement_order(x, y):
    global WALK_PATH, WALK_DEST

    if not is_squad_member_selected() or (x, y) in zones.get_active_solids(
        {}, ignore_calling_entity=True):
        return False

    _entity = get_selected_squad_member()

    movement.walk_to_position(_entity, x, y, zones.get_active_astar_map(),
                              zones.get_active_weight_map())

    WALK_PATH = _entity['movement']['path']['positions']
    WALK_DEST = _entity['movement']['path']['destination']

    settings.set_tick_mode('normal')

    return True
示例#11
0
def handle_mouse_movement(x, y):
	global WALK_PATH, WALK_DEST
	
	if ui_menu.get_active_menu() or ui_menu.DELAY or not is_squad_member_selected():
		return
	
	_x = x+camera.X
	_y = y+camera.Y
	
	if (_x, _y) in zones.get_active_solids({}, ignore_calling_entity=True, no_life=True):
		return
	
	_s_x, _s_y = movement.get_position(get_selected_squad_member())
	
	if (_x, _y) == WALK_DEST or (_x, _y) == (_s_x, _s_y):
		return
	
	WALK_PATH = pathfinding.astar((_s_x, _s_y), (_x, _y), zones.get_active_astar_map(), zones.get_active_weight_map())
	WALK_DEST = (_x, _y)
示例#12
0
def handle_keyboard_input():
	if controls.get_input_char_pressed('\t'):
		if settings.TICK_MODE == 'strategy':
			if ui_panel.ACTIVE_MENU:
				ui_panel.close()
			else:
				ui_panel.show_inventory(entity)
		
		else:
			_x, _y = movement.get_position(entity)
			
			camera.set_pos(_x - constants.MAP_VIEW_WIDTH/2, _y - constants.MAP_VIEW_HEIGHT/2)
	
	if controls.get_input_char_pressed('O'):
		settings.toggle_show_node_grid()
	
	elif controls.get_input_char_pressed('o'):
		settings.toggle_observer_mode()
	
	if controls.get_input_char_pressed('d'):
		_mx, _my = ui_cursor.get_map_position()
		
		for entity_id in entities.get_entity_group('life'):
			_entity = entities.get_entity(entity_id)
			
			if not numbers.distance((_mx, _my), movement.get_position(_entity)):
				_x, _y = ui_cursor.get_screen_position()
				_menu = ui_menu.create(_x, _y, title='Debug')
				ui_menu.add_selectable(_menu, 'Show Metas', lambda: _show_metas(_entity))
				ui_menu.add_selectable(_menu, 'Kill', lambda: entities.trigger_event(_entity, 'kill'))
				ui_menu.add_selectable(_menu, 'Overwatch', lambda: ai_debugger.register(_entity))
				
				break
	
	if controls.get_input_char_pressed('l'):
		_x, _y = ui_cursor.get_screen_position()
		_mx, _my = ui_cursor.get_map_position()
		_weight = zones.get_active_weight_map()[_my, _mx]
		_menu = ui_menu.create(_x, _y, title='Tile Info')
		
		ui_menu.add_selectable(_menu, 'Warp', lambda: entities.trigger_event(entity, 'set_position', x=_mx, y=_my))
		ui_menu.add_selectable(_menu, 'Light', lambda: effects.light(_mx, _my, 15, r=random.uniform(1.0, 1.5), g=random.uniform(1.0, 1.5), light_map='static_lighting'))
		ui_menu.add_selectable(_menu, 'Weight: %s' % _weight, lambda: _)
示例#13
0
def find_melee_position(entity):
    _target = entity['ai']['nearest_target']
    _x, _y = entity['ai']['life_memory'][_target]['last_seen_at']
    _closest_pos = {'pos': None, 'distance': 0}
    _solids = zones.get_active_solids(entity, ignore_entities=[_target])

    for x, y in [(_x - 1, _y), (_x + 1, _y), (_x, _y - 1), (_x, _y + 1),
                 (_x - 1, _y - 1), (_x + 1, _y - 1), (_x - 1, _y + 1),
                 (_x + 1, _y + 1)]:
        if (x, y) in _solids:
            continue

        _distance = numbers.distance(movement.get_position(entity), (x, y))

        if not _closest_pos['pos'] or _distance < _closest_pos['distance']:
            _closest_pos['distance'] = _distance
            _closest_pos['pos'] = (x, y)

    movement.walk_to_position(entity, _closest_pos['pos'][0],
                              _closest_pos['pos'][1],
                              zones.get_active_astar_map(),
                              zones.get_active_weight_map())
示例#14
0
def _search_for_target(entity, target_id):
	_nodes = flags.get_flag(entity, 'search_nodes')
	
	if not _nodes:
		flags.delete_flag(entity, 'search_nodes')
		
		entities.trigger_event(entity, 'target_search_failed', target_id=target_id)
		
		return
	
	_node_list = _nodes.keys()
	_node_list.sort()	
	_node_x, _node_y = _nodes[_node_list[0]][0]
	_distance = numbers.distance(movement.get_position(entity), (_node_x, _node_y))
	
	if _distance <= 15 and life.can_see_position(entity, (_node_x, _node_y)):
		_nodes[_node_list[0]].remove((_node_x, _node_y))
		
		if not _nodes[_node_list[0]]:
			del _nodes[_node_list[0]]
	else:
		movement.walk_to_position(entity, _node_x, _node_y, zones.get_active_astar_map(), zones.get_active_weight_map())
示例#15
0
def _get_item(entity, item_id, hold=False, weight=None):
    _item = entities.get_entity(item_id)
    _x, _y = movement.get_position(_item)
    _distance = numbers.distance(movement.get_position(entity), (_x, _y))

    if weight:
        ai.set_meta_weight(entity, weight,
                           10 * numbers.clip(_distance / 30.0, 0, 1))

    if _distance:
        movement.walk_to_position(entity,
                                  _x,
                                  _y,
                                  zones.get_active_astar_map(),
                                  zones.get_active_weight_map(),
                                  smp=True)

    else:
        if hold:
            life.get_and_hold_item(entity, item_id)
        else:
            life.get_and_store_item(entity, item_id)
示例#16
0
def handle_mouse_movement(x, y):
    global WALK_PATH, WALK_DEST

    if ui_menu.get_active_menu(
    ) or ui_menu.DELAY or not is_squad_member_selected():
        return

    _x = x + camera.X
    _y = y + camera.Y

    if (_x, _y) in zones.get_active_solids({},
                                           ignore_calling_entity=True,
                                           no_life=True):
        return

    _s_x, _s_y = movement.get_position(get_selected_squad_member())

    if (_x, _y) == WALK_DEST or (_x, _y) == (_s_x, _s_y):
        return

    WALK_PATH = pathfinding.astar((_s_x, _s_y), (_x, _y),
                                  zones.get_active_astar_map(),
                                  zones.get_active_weight_map())
    WALK_DEST = (_x, _y)
示例#17
0
def find_push_position(entity):
	_squad = entities.get_entity(ai_factions.FACTIONS[entity['ai']['faction']]['squads'][entity['ai']['squad']])
	_x, _y = movement.get_position(entity)
	_push_position = ai_squad_director.get_push_position(_squad, entity['_id'])
	
	if not _push_position:
		return
	
	#if not numbers.distance((_x, _y), _push_position) and not entity['ai']['visible_targets']:
	#	entity['ai']['meta']['has_lost_target'] = True
	
	movement.walk_to_position(entity, _push_position[0], _push_position[1], zones.get_active_astar_map(), zones.get_active_weight_map())
示例#18
0
def find_cover(entity):
	_squad = entities.get_entity(ai_factions.FACTIONS[entity['ai']['faction']]['squads'][entity['ai']['squad']])
	_cover_position = ai_squad_director.get_cover_position(_squad, entity['_id'])
	
	if not _cover_position or not numbers.distance(movement.get_position(entity), _cover_position):
		entities.trigger_event(entity, 'finish_turn')
	
		return
	
	movement.walk_to_position(entity, _cover_position[0], _cover_position[1], zones.get_active_astar_map(), zones.get_active_weight_map())
示例#19
0
def find_melee_position(entity):
	_target = entity['ai']['nearest_target']
	_x, _y = entity['ai']['life_memory'][_target]['last_seen_at']
	_closest_pos = {'pos': None, 'distance': 0}
	_solids = zones.get_active_solids(entity, ignore_entities=[_target])
	
	for x, y in [(_x-1, _y), (_x+1, _y), (_x, _y-1), (_x, _y+1), (_x-1, _y-1), (_x+1, _y-1), (_x-1, _y+1), (_x+1, _y+1)]:
		if (x, y) in _solids:
			continue
		
		_distance = numbers.distance(movement.get_position(entity), (x, y))
		
		if not _closest_pos['pos'] or _distance < _closest_pos['distance']:
			_closest_pos['distance'] = _distance
			_closest_pos['pos'] = (x, y)
	
	movement.walk_to_position(entity, _closest_pos['pos'][0], _closest_pos['pos'][1], zones.get_active_astar_map(), zones.get_active_weight_map())
示例#20
0
def make_target_surrender(entity):
    _last_seen_at = entity["ai"]["life_memory"][entity["ai"]["nearest_target"]]["last_seen_at"]

    movement.walk_to_position(
        entity, _last_seen_at[0], _last_seen_at[1], zones.get_active_astar_map(), zones.get_active_weight_map()
    )
示例#21
0
def handle_raid(entity, camp_id):
	entity['movement']['override_speed'] = 2#60 * 1
	
	ai_factions.FACTIONS[entity['faction']]['money'] -= get_attack_cost(entity, camp_id)
	
	movement.walk_to_position(entity, camp_id[0], camp_id[1], zones.get_active_astar_map(), zones.get_active_weight_map())
示例#22
0
文件: nodes.py 项目: penny64/r3-tdw
def draw_path(entity, x_mod=0, y_mod=0):
    _last_x, _last_y = (0, 0)
    _node_ids = entity['node_grid']['path'][:]
    _action_time_max = 0
    _surface_width = display.get_surface('nodes')['width']
    _surface_height = display.get_surface('nodes')['height']

    for node_id in _node_ids:
        _node = entity['node_grid']['nodes'][node_id]

        if not _last_x:
            _last_x, _last_y = movement.get_position(entity)

        if (_last_x, _last_y) == (_node['node']['x'], _node['node']['y']):
            continue

        _node['node']['busy_pos'] = []

        if _node['node']['draw_path'] and not _node['node']['path']:
            _path = pathfinding.astar((_last_x, _last_y),
                                      (_node['node']['x'], _node['node']['y']),
                                      zones.get_active_astar_map(),
                                      zones.get_active_weight_map())

            if (_node['node']['x'], _node['node']['y']) in _path:
                _path.remove((_node['node']['x'], _node['node']['y']))

            _node['node']['path'] = _path

        _move_cost = 0
        for pos in _node['node']['path']:
            for node_id in _node_ids:
                _check_node = entity['node_grid']['nodes'][node_id]['node']

                if not _check_node['action_time']:
                    continue

                if (_check_node['x'], _check_node['y']) == pos:
                    _action_time_max = _check_node['action_time']

            if _action_time_max and _move_cost <= _action_time_max:
                _color_mod = int(
                    round(200 * numbers.clip(
                        _move_cost / float(_action_time_max), .75, 1)))
                _color = (_color_mod, 0, 0)

                _node['node']['busy_pos'].append(pos)

            else:
                _color = (200, 200, 200)

            if _action_time_max:
                _move_cost += movement.get_move_cost(entity)

                if _move_cost >= _action_time_max:
                    _action_time_max = 0
                    _move_cost = 0

            if pos[0] - x_mod < 0 or pos[1] - y_mod < 0 or pos[
                    0] - x_mod >= _surface_width or pos[
                        1] - y_mod >= _surface_height:
                continue

            display.write_char('nodes',
                               pos[0] - x_mod,
                               pos[1] - y_mod,
                               chr(177),
                               fore_color=_color)

        if _node['node']['draw_path']:
            _last_x, _last_y = (_node['node']['x'], _node['node']['y'])
示例#23
0
def get_vantage_point(squad, member_id):
    _member = entities.get_entity(member_id)
    _member_pos = movement.get_position(_member)
    _best_vantage = {'position': None, 'score': 1000}
    _engage_range = flags.get_flag(_member, 'engage_distance')
    _min_engage_range = flags.get_flag(_member, 'min_engage_distance')

    if _member['movement']['path']['destination']:
        if _member['movement']['path']['destination'] in squad[
                'position_map_scores']:
            _scores = squad['position_map_scores'][_member['movement']['path']
                                                   ['destination']]
            _score = _scores['vantage']  # + _scores['member_coverage']
            _continue = False

            for target_id in squad['known_targets']:
                _last_known_position = _member['ai']['life_memory'][target_id][
                    'last_seen_at']
                _distance_to_target = numbers.distance(
                    _member['movement']['path']['destination'],
                    _last_known_position)

                if _distance_to_target < _min_engage_range:
                    _continue = True

                    break

            if not _continue and _scores[
                    'targets'] and _score >= _min_engage_range and _score <= _engage_range:
                return _member['movement']['path']['destination']

    for pos in squad['position_map_scores']:
        _scores = squad['position_map_scores'][pos]
        _dist = numbers.distance(_member_pos, pos)
        _score = _scores['vantage'] + _dist
        _continue = False

        if not _scores[
                'targets'] or _score - _dist < _min_engage_range or _score > _engage_range + _dist:
            continue

        for target_id in squad['known_targets']:
            _last_known_position = _member['ai']['life_memory'][target_id][
                'last_seen_at']
            _distance_to_target = numbers.distance(pos, _last_known_position)

            if _distance_to_target < _min_engage_range:
                _continue = True

                break

        if _continue:
            continue

        if _score < _best_vantage['score']:
            _astar_distance = len(
                pathfinding.astar(_member_pos, pos,
                                  zones.get_active_astar_map(),
                                  zones.get_active_weight_map()))
            _best_vantage['score'] = _score + _scores['member_coverage'] + int(
                round((_astar_distance * .15)))
            _best_vantage['position'] = pos[:]

    if not _best_vantage['position']:
        _member['ai']['meta']['has_firing_position'] = False

        entities.trigger_event(_member,
                               'create_timer',
                               time=60,
                               exit_callback=_reset_fire_position)

        return

    _x, _y = movement.get_position(_member)
    _member_positions = set()

    for squad_member_id in squad['members']:
        if squad_member_id == member_id:
            continue

        _member_positions.add(movement.get_position_via_id(squad_member_id))

    _v_p = _best_vantage['position']
    _friendly_fire = False

    for pos in shapes.line((_x, _y), _best_vantage['position']):
        if pos in _member_positions:
            _friendly_fire = True

            break

    if _friendly_fire:
        for n_pos in [(_v_p[0] - 1, _v_p[1] - 1), (_v_p[0], _v_p[1] - 1),
                      (_v_p[0] + 1, _v_p[1] - 1), (_v_p[0] - 1, _v_p[1]),
                      (_v_p[0] + 1, _v_p[1]), (_v_p[0] - 1, _v_p[1] + 1),
                      (_v_p[0], _v_p[1] + 1), (_v_p[0] + 1, _v_p[1] + 1)]:
            if not n_pos in squad['position_map_scores']:
                continue

            _break = False
            for nn_pos in shapes.line((_x, _y), n_pos):
                if nn_pos in _member_positions:
                    _break = True

                    break
            else:
                _v_p = n_pos

                break

            if _break:
                continue

    for coverage_pos in shapes.circle(_v_p[0], _v_p[1], 6):
        if not coverage_pos in squad['position_map_scores']:
            continue

        _c_dist = 15 * (1 - (numbers.distance(coverage_pos, (_x, _y)) / 6.0))

        squad['position_map_scores'][coverage_pos][
            'member_coverage'] += _c_dist

    squad['position_map_scores'][_v_p]['member_coverage'] += 6

    return _v_p
示例#24
0
文件: nodes.py 项目: witheld9/r3-tdw
def create_walk_node(entity, x, y, clear=False):
	_walk_speed = chr(31)
	
	if clear:
		clear_path(entity)
		redraw_path(entity)
	
	_node = _create_node(entity, x, y, draw_path=True, passive=False, name=_walk_speed, callback_on_touch=False)
	
	entities.trigger_event(_node, 'set_char', char='O')
	
	entity['node_grid']['nodes'][_node['_id']]['callback'] = lambda: entities.trigger_event(entity,
	                                                                                        'move_to_position',
	                                                                                        x=_node['x'],
	                                                                                        y=_node['y'],
	                                                                                        astar_map=zones.get_active_astar_map(),
	                                                                                        weight_map=zones.get_active_weight_map())
示例#25
0
def get_vantage_point(squad, member_id):
	_member = entities.get_entity(member_id)
	_member_pos = movement.get_position(_member)
	_best_vantage = {'position': None, 'score': 1000}
	_engage_range = flags.get_flag(_member, 'engage_distance')
	_min_engage_range = flags.get_flag(_member, 'min_engage_distance')
	
	if _member['movement']['path']['destination']:
		if _member['movement']['path']['destination'] in squad['position_map_scores']:
			_scores = squad['position_map_scores'][_member['movement']['path']['destination']]
			_score = _scores['vantage']# + _scores['member_coverage']
			_continue = False
			
			for target_id in squad['known_targets']:
				_last_known_position = _member['ai']['life_memory'][target_id]['last_seen_at']
				_distance_to_target = numbers.distance(_member['movement']['path']['destination'], _last_known_position)
				
				if _distance_to_target < _min_engage_range:
					_continue = True
					
					break
			
			if not _continue and _scores['targets'] and _score >= _min_engage_range and _score <= _engage_range:
				return _member['movement']['path']['destination']
	
	for pos in squad['position_map_scores']:
		_scores = squad['position_map_scores'][pos]
		_dist = numbers.distance(_member_pos, pos)
		_score = _scores['vantage'] + _dist
		_continue = False
		
		if not _scores['targets'] or _score - _dist < _min_engage_range or _score > _engage_range + _dist:
			continue
		
		for target_id in squad['known_targets']:
			_last_known_position = _member['ai']['life_memory'][target_id]['last_seen_at']
			_distance_to_target = numbers.distance(pos, _last_known_position)
			
			if _distance_to_target < _min_engage_range:
				_continue = True
				
				break
			
		if _continue:
			continue

		if _score < _best_vantage['score']:
			_astar_distance = len(pathfinding.astar(_member_pos, pos, zones.get_active_astar_map(), zones.get_active_weight_map()))
			_best_vantage['score'] = _score + _scores['member_coverage'] + int(round((_astar_distance * .15)))
			_best_vantage['position'] = pos[:]
	
	if not _best_vantage['position']:
		_member['ai']['meta']['has_firing_position'] = False
		
		entities.trigger_event(_member, 'create_timer', time=60, exit_callback=_reset_fire_position)
		
		return
	
	_x, _y = movement.get_position(_member)
	_member_positions = set()
	
	for squad_member_id in squad['members']:
		if squad_member_id == member_id:
			continue
		
		_member_positions.add(movement.get_position_via_id(squad_member_id))
	
	_v_p = _best_vantage['position']
	_friendly_fire = False
	
	for pos in shapes.line((_x, _y), _best_vantage['position']):
		if pos in _member_positions:
			_friendly_fire = True
			
			break

	if _friendly_fire:
		for n_pos in [(_v_p[0]-1, _v_p[1]-1), (_v_p[0], _v_p[1]-1), (_v_p[0]+1, _v_p[1]-1), (_v_p[0]-1, _v_p[1]), (_v_p[0]+1, _v_p[1]), (_v_p[0]-1, _v_p[1]+1), (_v_p[0], _v_p[1]+1), (_v_p[0]+1, _v_p[1]+1)]:
			if not n_pos in squad['position_map_scores']:
				continue
			
			_break = False
			for nn_pos in shapes.line((_x, _y), n_pos):
				if nn_pos in _member_positions:
					_break = True
					
					break
			else:
				_v_p = n_pos
				
				break
			
			if _break:
				continue
	
	for coverage_pos in shapes.circle(_v_p[0], _v_p[1], 6):
		if not coverage_pos in squad['position_map_scores']:
			continue
		
		_c_dist = 15 * (1 - (numbers.distance(coverage_pos, (_x, _y)) / 6.0))
		
		squad['position_map_scores'][coverage_pos]['member_coverage'] += _c_dist
	
	squad['position_map_scores'][_v_p]['member_coverage'] += 6
	
	return _v_p
示例#26
0
文件: nodes.py 项目: witheld9/r3-tdw
def draw_path(entity, x_mod=0, y_mod=0):
	_last_x, _last_y = (0, 0)
	_node_ids = entity['node_grid']['path'][:]
	_action_time_max = 0
	_surface_width = display.get_surface('nodes')['width']
	_surface_height = display.get_surface('nodes')['height']
	
	for node_id in _node_ids:
		_node = entity['node_grid']['nodes'][node_id]
		
		if not _last_x:
			_last_x, _last_y = movement.get_position(entity)
		
		if (_last_x, _last_y) == (_node['node']['x'], _node['node']['y']):
			continue
		
		_node['node']['busy_pos'] = []
		
		if _node['node']['draw_path'] and not _node['node']['path']:
			_path = pathfinding.astar((_last_x, _last_y), (_node['node']['x'], _node['node']['y']), zones.get_active_astar_map(), zones.get_active_weight_map())
			
			if (_node['node']['x'], _node['node']['y']) in _path:
				_path.remove((_node['node']['x'], _node['node']['y']))
			
			_node['node']['path'] = _path
		
		_move_cost = 0
		for pos in _node['node']['path']:
			for node_id in _node_ids:
				_check_node = entity['node_grid']['nodes'][node_id]['node']
				
				if not _check_node['action_time']:
					continue
				
				if (_check_node['x'], _check_node['y']) == pos:
					_action_time_max = _check_node['action_time']
			
			if _action_time_max and _move_cost <= _action_time_max:
				_color_mod = int(round(200*numbers.clip(_move_cost/float(_action_time_max), .75, 1)))
				_color = (_color_mod, 0, 0)
				
				_node['node']['busy_pos'].append(pos)
			
			else:
				_color = (200, 200, 200)
			
			if _action_time_max:
				_move_cost += movement.get_move_cost(entity)
				
				if _move_cost >= _action_time_max:
					_action_time_max = 0
					_move_cost = 0
			
			if pos[0]-x_mod < 0 or pos[1]-y_mod < 0 or pos[0]-x_mod >= _surface_width or pos[1]-y_mod >= _surface_height:
				continue
			
			display.write_char('nodes', pos[0]-x_mod, pos[1]-y_mod, chr(177), fore_color=_color)
		
		if _node['node']['draw_path']:
			_last_x, _last_y = (_node['node']['x'], _node['node']['y'])