コード例 #1
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'])
コード例 #2
0
ファイル: movement.py プロジェクト: penny64/r3-tdw
def walk_to_position(entity, x, y, astar_map, weight_map, avoid=[], smp=-1):
    _start_position = get_position(entity)
    _target_position = (x, y)

    if _start_position == _target_position:
        return False

    if smp == True or (smp == -1
                       and numbers.distance(_start_position, _target_position)
                       > settings.SMP_MIN_PATH_DISTANCE):
        smp = settings.ALLOW_SMP

    if entity['movement']['path']['destination']:
        if not numbers.distance(entity['movement']['path']['destination'],
                                _target_position):
            return False

    if smp == True:
        pathfinding.astar_mp(_start_position,
                             _target_position,
                             astar_map,
                             weight_map,
                             lambda path: set_path(entity, path),
                             avoid=avoid)
    else:
        entity['movement']['path']['positions'] = pathfinding.astar(
            get_position(entity),
            _target_position,
            astar_map,
            weight_map,
            avoid=avoid)
        entity['movement']['path']['destination'] = _target_position
        entity['movement']['path']['refresh'] = False

    return True
コード例 #3
0
ファイル: zones.py プロジェクト: witheld9/r3-tdw
def path_node_set(node_set, start, end, weights=None, path=False, entity=None, avoid=[]):
	if not weights == None:
		_weights = weights
	else:
		_weights = node_set['weight_map']
		
	_n_avoid = []
	
	for p in avoid:
		_p_x, _p_y = p[0]-node_set['min_x'], p[1]-node_set['min_y']
		
		if _p_x < node_set['min_x'] or _p_y < node_set['min_y'] or _p_x >= node_set['max_x'] or _p_y >= node_set['max_y']:
			continue
		
		_n_avoid.append((_p_x, _p_y))
	
	_start = (int(round((start[0]-node_set['min_x']))), int(round((start[1]-node_set['min_y']))))
	_end = (int(round((end[0]-node_set['min_x']))), int(round((end[1]-node_set['min_y']))))
	_node_set_path = pathfinding.astar(_start, _end, node_set['astar_map'], _weights, avoid=_n_avoid)

	if not _node_set_path:
		return []
	
	if path:
		_actual_path = []
		_astar_map = get_active_astar_map()
		_weight_map = get_active_weight_map()
		_x, _y = movement.get_position(entity)
		
		for pos in _node_set_path:
			_actual_path.append((node_set['min_x']+pos[0], node_set['min_y']+pos[1]))
		
		print (_x, _y), _actual_path[0], (_x, _y) in avoid, _actual_path[0] in avoid
		print _astar_map[_actual_path[0][1], _actual_path[0][0]]
		_n_path = pathfinding.astar((_x, _y), _actual_path[0], _astar_map, _weight_map, avoid=avoid)
		_n_path.extend(_actual_path)
	
		return _n_path
	
	return _node_set_path
コード例 #4
0
ファイル: ui_squad_control.py プロジェクト: witheld9/r3-tdw
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)
コード例 #5
0
ファイル: movement.py プロジェクト: witheld9/r3-tdw
def walk_to_position(entity, x, y, astar_map, weight_map, avoid=[], smp=-1):
	_start_position = get_position(entity)
	_target_position = (x, y)
	
	if _start_position == _target_position:
		return False
	
	if smp == True or (smp == -1 and numbers.distance(_start_position, _target_position) > settings.SMP_MIN_PATH_DISTANCE):
		smp = settings.ALLOW_SMP
	
	if entity['movement']['path']['destination']:
		if not numbers.distance(entity['movement']['path']['destination'], _target_position):
			return False
	
	if smp == True:
		pathfinding.astar_mp(_start_position, _target_position, astar_map, weight_map,
		                     lambda path: set_path(entity, path), avoid=avoid)
	else:
		entity['movement']['path']['positions'] = pathfinding.astar(get_position(entity), _target_position, astar_map, weight_map, avoid=avoid)
		entity['movement']['path']['destination'] = _target_position
		entity['movement']['path']['refresh'] = False
	
	return True
コード例 #6
0
ファイル: ui_squad_control.py プロジェクト: penny64/r3-tdw
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)
コード例 #7
0
def path_node_set(node_set,
                  start,
                  end,
                  weights=None,
                  path=False,
                  entity=None,
                  avoid=[]):
    if not weights == None:
        _weights = weights
    else:
        _weights = node_set['weight_map']

    _n_avoid = []

    for p in avoid:
        _p_x, _p_y = p[0] - node_set['min_x'], p[1] - node_set['min_y']

        if _p_x < node_set['min_x'] or _p_y < node_set[
                'min_y'] or _p_x >= node_set['max_x'] or _p_y >= node_set[
                    'max_y']:
            continue

        _n_avoid.append((_p_x, _p_y))

    _start = (int(round((start[0] - node_set['min_x']))),
              int(round((start[1] - node_set['min_y']))))
    _end = (int(round(
        (end[0] - node_set['min_x']))), int(round(
            (end[1] - node_set['min_y']))))
    _node_set_path = pathfinding.astar(_start,
                                       _end,
                                       node_set['astar_map'],
                                       _weights,
                                       avoid=_n_avoid)

    if not _node_set_path:
        return []

    if path:
        _actual_path = []
        _astar_map = get_active_astar_map()
        _weight_map = get_active_weight_map()
        _x, _y = movement.get_position(entity)

        for pos in _node_set_path:
            _actual_path.append(
                (node_set['min_x'] + pos[0], node_set['min_y'] + pos[1]))

        print(_x, _y), _actual_path[0], (_x,
                                         _y) in avoid, _actual_path[0] in avoid
        print _astar_map[_actual_path[0][1], _actual_path[0][0]]
        _n_path = pathfinding.astar((_x, _y),
                                    _actual_path[0],
                                    _astar_map,
                                    _weight_map,
                                    avoid=avoid)
        _n_path.extend(_actual_path)

        return _n_path

    return _node_set_path
コード例 #8
0
def handle_mouse_pressed(x, y, button):
    global SELECTED_SQUAD, SELECTED_CAMP, MAP_PATH

    _s1, _s2 = entities.get_entity_group('squads')[:2]
    _m_x, _m_y = x / constants.MAP_CELL_SPACE, y / constants.MAP_CELL_SPACE

    if button == 1:
        _camp = MAP['grid'][_m_x, _m_y]

        if _camp['owned_by'] == 'Rogues' and not SELECTED_CAMP:
            SELECTED_CAMP = (_m_x, _m_y)

            set_draw_mode('camp_info')

        elif not _camp['owned_by'] == 'Rogues':
            if SELECTED_SQUAD:
                if _camp['owned_by']:
                    SELECTED_CAMP = (_m_x, _m_y)
                    MAP_PATH = pathfinding.astar(
                        movement.get_position_via_id(_s1), (_m_x, _m_y),
                        MAP['astar_map'], MAP['astar_weight_map'])

                    set_draw_mode('raid')

                elif _camp['is_ownable']:
                    SELECTED_CAMP = (_m_x, _m_y)

                    set_draw_mode('occupy')

                else:
                    SELECTED_SQUAD = None
                    SELECTED_CAMP = None

                    set_draw_mode('news')

            #set_draw_mode('camp_info')

        else:
            for squad_id in entities.get_entity_group('squads'):
                if squad_id == SELECTED_SQUAD:
                    continue

                _squad = entities.get_entity(squad_id)

                if not movement.get_position(_squad) == (_m_x, _m_y):
                    continue

                if not _squad['faction'] == 'Rogues':
                    continue

                SELECTED_SQUAD = squad_id
                SELECTED_CAMP = None

                set_draw_mode('squad_info')

                break

    elif button == 2:
        _camp = MAP['grid'][_m_x, _m_y]

        if not _camp['owned_by'] == 'Rogues':
            if SELECTED_SQUAD:
                if DRAW_MODE == 'raid':
                    entities.trigger_event(entities.get_entity(SELECTED_SQUAD),
                                           'raid',
                                           camp_id=(_m_x, _m_y))

                    SELECTED_SQUAD = None
                    SELECTED_CAMP = None

                    set_draw_mode('news')
コード例 #9
0
ファイル: ai_squad_director.py プロジェクト: witheld9/r3-tdw
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
コード例 #10
0
ファイル: world_strategy.py プロジェクト: witheld9/r3-tdw
def handle_mouse_pressed(x, y, button):
	global SELECTED_SQUAD, SELECTED_CAMP, MAP_PATH
	
	_s1, _s2 = entities.get_entity_group('squads')[:2]
	_m_x, _m_y = x / constants.MAP_CELL_SPACE, y / constants.MAP_CELL_SPACE
	
	if button == 1:
		_camp = MAP['grid'][_m_x, _m_y]
		
		if _camp['owned_by'] == 'Rogues' and not SELECTED_CAMP:
			SELECTED_CAMP = (_m_x, _m_y)
			
			set_draw_mode('camp_info')
		
		elif not _camp['owned_by'] == 'Rogues':
			if SELECTED_SQUAD:
				if _camp['owned_by']:
					SELECTED_CAMP = (_m_x, _m_y)
					MAP_PATH = pathfinding.astar(movement.get_position_via_id(_s1), (_m_x, _m_y), MAP['astar_map'], MAP['astar_weight_map'])
					
					set_draw_mode('raid')
				
				elif _camp['is_ownable']:
					SELECTED_CAMP = (_m_x, _m_y)
					
					set_draw_mode('occupy')
				
				else:
					SELECTED_SQUAD = None
					SELECTED_CAMP = None
					
					set_draw_mode('news')
				
			#set_draw_mode('camp_info')
		
		else:
			for squad_id in entities.get_entity_group('squads'):
				if squad_id == SELECTED_SQUAD:
					continue
				
				_squad = entities.get_entity(squad_id)
				
				if not movement.get_position(_squad) == (_m_x, _m_y):
					continue
				
				if not _squad['faction'] == 'Rogues':
					continue
				
				SELECTED_SQUAD = squad_id
				SELECTED_CAMP = None
				
				set_draw_mode('squad_info')
				
				break
	
	elif button == 2:
		_camp = MAP['grid'][_m_x, _m_y]
		
		if not _camp['owned_by'] == 'Rogues':
			if SELECTED_SQUAD:
				if DRAW_MODE == 'raid':
					entities.trigger_event(entities.get_entity(SELECTED_SQUAD), 'raid', camp_id=(_m_x, _m_y))
					
					SELECTED_SQUAD = None
					SELECTED_CAMP = None
					
					set_draw_mode('news')
コード例 #11
0
ファイル: ai_squad_director.py プロジェクト: penny64/r3-tdw
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
コード例 #12
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'])