示例#1
0
def find_shelter(life, group_id):
    _group = get_group(life, group_id)
    _shelter = judgement.get_best_shelter(life)

    if _shelter:
        set_shelter(life, group_id, chunks.get_chunk(_shelter)['reference'])
        announce(life, group_id, 'found_shelter')
    else:
        if get_stage(life, group_id) in [STAGE_FORMING]:
            set_stage(life, group_id, STAGE_SETTLING)
示例#2
0
文件: groups.py 项目: flags/Reactor-3
def find_shelter(life, group_id):
	_group = get_group(life, group_id)
	_shelter = judgement.get_best_shelter(life)
	
	if _shelter:
		set_shelter(life, group_id, chunks.get_chunk(_shelter)['reference'])
		announce(life, group_id, 'found_shelter')
	else:
		if get_stage(life, group_id) in [STAGE_FORMING]:
			set_stage(life, group_id, STAGE_SETTLING)
示例#3
0
def find_shelter(life, group_id):
	_group = get_group(life, group_id)
	_shelter = judgement.get_best_shelter(life)
	
	if _shelter:
		set_shelter(life, group_id, chunks.get_chunk(_shelter)['reference'])
		announce(life, group_id, 'found_shelter')
	else:
		if not get_stage(life, group_id) == STAGE_SETTLING:
			set_stage(life, group_id, STAGE_SETTLING)
			announce(life, group_id, 'update_group_stage')
示例#4
0
def manage_jobs(life, group_id):
    _shelter = get_shelter(life, group_id)

    if not _shelter:
        return False

    if not get_stage(life, group_id) == STAGE_SETTLED:
        return False

    if get_flag(life, life['group'], 'guard_chunk_keys'):
        return False

    _guard_chunk_keys = []
    _potential_guard_chunk_keys = []
    _group_members = get_group(life, life['group'])['members']
    _shelter_chunks = references.get_reference(_shelter)[:]

    #TODO: This is horrible... like the worst possible way to do this
    for chunk_key in WORLD_INFO['chunk_map']:
        if chunk_key in _shelter_chunks:
            _shelter_chunks.remove(chunk_key)
            continue

        if bad_numbers.distance(life['pos'],
                                chunks.get_chunk(chunk_key)['pos']) > 50:
            continue

        _potential_guard_chunk_keys.append(chunk_key)

    if not _potential_guard_chunk_keys:
        return False

    for member_id in _group_members:
        if member_id == life['id']:
            continue

        _chunk_key = _potential_guard_chunk_keys.pop(
            random.randint(0,
                           len(_potential_guard_chunk_keys) - 1))
        _guard_chunk_keys.append(_chunk_key)
        lfe.memory(LIFE[member_id], 'focus_on_chunk', chunk_key=_chunk_key)

        if not _potential_guard_chunk_keys:
            break

    flag(life, life['group'], 'guard_chunk_keys', _guard_chunk_keys)
示例#5
0
文件: groups.py 项目: flags/Reactor-3
def manage_jobs(life, group_id):
	_shelter = get_shelter(life, group_id)
	
	if not _shelter:
		return False
	
	if not get_stage(life, group_id) == STAGE_SETTLED:
		return False
	
	if get_flag(life, life['group'], 'guard_chunk_keys'):
		return False
	
	_guard_chunk_keys = []
	_potential_guard_chunk_keys = []
	_group_members = get_group(life, life['group'])['members']
	_shelter_chunks = references.get_reference(_shelter)[:]
	
	#TODO: This is horrible... like the worst possible way to do this
	for chunk_key in WORLD_INFO['chunk_map']:
		if chunk_key in _shelter_chunks:
			_shelter_chunks.remove(chunk_key)
			continue
		
		if bad_numbers.distance(life['pos'], chunks.get_chunk(chunk_key)['pos'])>50:
			continue
		
		_potential_guard_chunk_keys.append(chunk_key)
	
	if not _potential_guard_chunk_keys:
		return False
	
	for member_id in _group_members:
		if member_id == life['id']:
			continue
		
		_chunk_key = _potential_guard_chunk_keys.pop(random.randint(0, len(_potential_guard_chunk_keys)-1))
		_guard_chunk_keys.append(_chunk_key)
		lfe.memory(LIFE[member_id], 'focus_on_chunk', chunk_key=_chunk_key)
		
		if not _potential_guard_chunk_keys:
			break
	
	flag(life, life['group'], 'guard_chunk_keys', _guard_chunk_keys)
示例#6
0
def quick_look(life):
    _life = []
    _items = []
    _current_chunk = lfe.get_current_chunk_id(life)
    _current_chunk_pos = chunks.get_chunk(_current_chunk)['pos']
    _x_chunk_min = bad_numbers.clip(
        _current_chunk_pos[0] -
        ((get_vision(life) / WORLD_INFO['chunk_size']) *
         WORLD_INFO['chunk_size']), 0, MAP_SIZE[0] - WORLD_INFO['chunk_size'])
    _y_chunk_min = bad_numbers.clip(
        _current_chunk_pos[1] -
        ((get_vision(life) / WORLD_INFO['chunk_size']) *
         WORLD_INFO['chunk_size']), 0, MAP_SIZE[1] - WORLD_INFO['chunk_size'])
    _x_chunk_max = bad_numbers.clip(
        _current_chunk_pos[0] +
        ((get_vision(life) / WORLD_INFO['chunk_size']) *
         WORLD_INFO['chunk_size']), 0, MAP_SIZE[0] - WORLD_INFO['chunk_size'])
    _y_chunk_max = bad_numbers.clip(
        _current_chunk_pos[1] +
        ((get_vision(life) / WORLD_INFO['chunk_size']) *
         WORLD_INFO['chunk_size']), 0, MAP_SIZE[1] - WORLD_INFO['chunk_size'])
    _has_ready_weapon = combat.has_ready_weapon(life)

    for y in range(_y_chunk_min, _y_chunk_max, WORLD_INFO['chunk_size']):
        for x in range(_x_chunk_min, _x_chunk_max, WORLD_INFO['chunk_size']):
            _chunk_key = '%s,%s' % (x, y)
            _chunk = chunks.get_chunk(_chunk_key)

            for life_id in _chunk['life']:
                ai = LIFE[life_id]

                if ai['dead']:
                    continue

                if life_id == life['id']:
                    continue

                if not can_see_position(life, LIFE[life_id]['pos']):
                    continue

                _visibility = get_visiblity_of_position(life, ai['pos'])
                _stealth_coverage = get_stealth_coverage(ai)

                if _visibility < 1 - _stealth_coverage:
                    continue

                if not ai['id'] in life['know']:
                    brain.meet_alife(life, ai)

                life['seen'].append(ai['id'])

                if life['think_rate'] == life['think_rate_max']:
                    lfe.create_and_update_self_snapshot(LIFE[ai['id']])
                    judgement.judge_life(life, ai['id'])

                if ai['dead']:
                    if life['know'][ai['id']]['group']:
                        groups.remove_member(life,
                                             life['know'][ai['id']]['group'],
                                             ai['id'])
                        life['know'][ai['id']]['group'] = None

                    life['know'][ai['id']]['dead'] = True
                elif ai['asleep']:
                    life['know'][ai['id']]['asleep'] = True
                elif not ai['asleep']:
                    life['know'][ai['id']]['asleep'] = False

                life['know'][ai['id']]['last_seen_time'] = 0
                life['know'][ai['id']]['last_seen_at'] = ai['pos'][:]
                life['know'][ai['id']]['escaped'] = False
                life['know'][ai['id']]['state'] = ai['state']
                life['know'][ai['id']]['state_tier'] = ai['state_tier']

                if brain.alife_has_flag(life, ai['id'], 'search_map'):
                    brain.unflag_alife(life, ai['id'], 'search_map')

                _chunk_id = lfe.get_current_chunk_id(ai)
                judgement.judge_chunk(life, _chunk_id, seen=True)

                _life.append(life_id)

            for item_uid in _chunk['items']:
                if not item_uid in ITEMS:
                    continue

                item = ITEMS[item_uid]

                if not item['uid'] in life['know_items']:
                    brain.remember_item(life, item)

                if items.is_item_owned(item['uid']):
                    continue

                #	#TODO: This doesn't work because we are specifically checking chunks
                #	if item['owner'] and lfe.item_is_equipped(LIFE[item['owner']], item['uid']):
                #		life['know_items'][item['uid']]['last_seen_at'] = LIFE[item['owner']]['pos']
                #		life['know_items'][item['uid']]['last_owned_by'] = item['owner']
                #		life['know_items'][item['uid']]['last_seen_time'] = 0
                #
                #	continue

                if not can_see_position(life, item['pos']):
                    continue

                if not item['uid'] in life['know_items']:
                    brain.remember_item(life, item)

                life['seen_items'].append(item['uid'])
                life['know_items'][
                    item['uid']]['last_seen_at'] = item['pos'][:]
                life['know_items'][item['uid']]['last_seen_time'] = 0
                life['know_items'][item['uid']]['last_owned_by'] = None
                life['know_items'][
                    item['uid']]['score'] = judgement.judge_item(
                        life, item['uid'])
                life['know_items'][item['uid']]['lost'] = False

                _items.append(item_uid)
示例#7
0
文件: groups.py 项目: flags/Reactor-3
def manage_combat(life, group_id):
	if has_flag(life, group_id, 'confident'):
		_was_confident = get_flag(life, group_id, 'confident')
		
		if _was_confident == stats.is_confident(life) and not lfe.ticker(life, 'decision_wait', 16):
			return False
	
	flag(life, group_id, 'confident', stats.is_confident(life))
	
	_existing_friendlies = get_flag(life, group_id, 'friendlies')
	_existing_targets = get_flag(life, group_id, 'targets')
	_last_focal_point = get_flag(life, group_id, 'last_focal_point')
	
	if not _existing_friendlies:
		_existing_friendlies = {}
	
	if not _existing_targets:
		_existing_targets = {}
	
	for life_id in get_group(life, group_id)['members']:
		if not life_id in _existing_friendlies:
			_existing_friendlies[life_id] = {'updated': -900}
	
	flag(life, group_id, 'friendlies', _existing_friendlies)
	
	_checked_targets = []
	for target_id in judgement.get_threats(life):
		if target_id in _existing_targets:
			_existing_targets[target_id]['time'] = 0
		else:
			_existing_targets[target_id] = {'time': 0, 'pos': brain.knows_alife_by_id(life, target_id)['last_seen_at'][:]}
		
		_checked_targets.append(target_id)

	_enemy_focal_pos = None

	for target_id in _existing_targets:
		if not _enemy_focal_pos:
			_enemy_focal_pos = _existing_targets[target_id]['pos'][:]
		else:
			_enemy_focal_pos = bad_numbers.lerp_velocity(_enemy_focal_pos, _existing_targets[target_id]['pos'], 0.5)
		
		if target_id in _checked_targets:
			continue
		
		_existing_targets[target_id]['time'] += 1
		
		if _existing_targets[target_id]['time']>100:
			del _existing_targets[target_id]
			
			continue
	
	_hostile_chunks = get_flag(life, group_id, 'hostile_chunks')
	_previous_visible_chunks = brain.get_flag(life, 'group_combat_vis_chunks')
	
	if _previous_visible_chunks and _previous_visible_chunks['from_pos'] == life['pos']:
		_visible_chunks = _previous_visible_chunks['visible_chunks']
	else:
		_visible_chunks = chunks.get_visible_chunks_from(life['pos'], life['vision_max']*.75)
		
		brain.flag(life, 'group_combat_vis_chunks', value={'from_pos': life['pos'][:],
		                                                   'visible_chunks': _visible_chunks})
	
	if _enemy_focal_pos:
		lfe.clear_ticker(life, 'group_command_reset')
		
		if not _last_focal_point or bad_numbers.distance(_enemy_focal_pos, _last_focal_point)>30:
			_hostile_chunks = chunks.get_visible_chunks_from((int(round(_enemy_focal_pos[0])), int(round(_enemy_focal_pos[1])), 2), life['vision_max']*1.5)
			
			flag(life, group_id, 'hostile_chunks', _hostile_chunks)
			flag(life, group_id, 'visible_chunks', _visible_chunks)
			flag(life, group_id, 'last_focal_point', _enemy_focal_pos)
			
	else:
		_ticker = lfe.ticker(life, 'group_command_reset', 48)
		
		if get_stage(life, group_id) == STAGE_ATTACKING:
			if _ticker:
				set_stage(life, group_id, STAGE_FORMING)
				flag(life, group_id, 'friendlies', None)
				flag(life, group_id, 'strategy', None)
			else:
				manage_strategy(life, group_id)
		
		return False
	
	if not get_stage(life, group_id) == STAGE_ATTACKING:
		speech.announce_combat_to_group(life, group_id)
		set_stage(life, group_id, STAGE_ATTACKING)
	
	if not lfe.ticker(life, 'group_command_rate', 3):
		return False
	
	_orig_visible_chunks = _visible_chunks[:]
	
	#TODO: Check distance to threat
	for hostile_chunk_key in _hostile_chunks:
		if hostile_chunk_key in _visible_chunks:
			_visible_chunks.remove(hostile_chunk_key)
		
	#TODO: Additional stages: PLANNING, EXECUTING
	if _visible_chunks and stats.is_confident(life):
		for target_id in order_spread_out(life, group_id, _visible_chunks, filter_by=lambda life_id: WORLD_INFO['ticks']-_existing_friendlies[life_id]['updated']>100):
			_existing_friendlies[target_id]['updated'] = WORLD_INFO['ticks']
	else:
		_distant_chunk = {'distance': -1, 'chunk_key': None}
		_unchecked_members = get_group(life, group_id)['members'][:]
		
		for chunk_key in _orig_visible_chunks:
			_distance = bad_numbers.distance((int(round(_enemy_focal_pos[0])), int(round(_enemy_focal_pos[1]))), chunks.get_chunk(chunk_key)['pos'])
			_distance *= bad_numbers.clip(bad_numbers.distance(life['pos'], _enemy_focal_pos), 1, 35)/35.0
			
			if chunk_key in _visible_chunks:
				_distance *= 2
			
			for member_id in _unchecked_members:
				if life['id'] == member_id:
					continue
				
				_target = brain.knows_alife_by_id(life, member_id)
				
				if _target['last_seen_time'] <= 25 and chunks.get_chunk_key_at(_target['last_seen_at']) == chunk_key:
					_distance *= (2.5*(1-(bad_numbers.clip(_target['last_seen_time'], 0, 25)/25.0)))
			
			if _distance>_distant_chunk['distance']:
				_distant_chunk['distance'] = _distance
				_distant_chunk['chunk_key'] = chunk_key
		
		if _distant_chunk['chunk_key']:
			for target_id in order_move_to(life, group_id, _distant_chunk['chunk_key'], filter_by=lambda life_id: WORLD_INFO['ticks']-_existing_friendlies[life_id]['updated']>100):
				_existing_friendlies[target_id]['updated'] = WORLD_INFO['ticks']
		
		return False
示例#8
0
def hide(life, targets):
    _target_positions = []
    _avoid_positions = []
    _zones = [zones.get_zone_at_coords(life['pos'])]

    if lfe.find_action(life, [{
            'action': 'dijkstra_move',
            'reason': 'escaping'
    }]):
        if not lfe.ticker(life, 'escaping', 6):
            return False

    #What can the targets see?
    for target_id in targets:
        _target = brain.knows_alife_by_id(life, target_id)
        _zone = zones.get_zone_at_coords(_target['last_seen_at'])

        if not _zone in _zones:
            _zones.append(_zone)

        fov.fov(_target['last_seen_at'],
                sight.get_vision(_target['life']),
                callback=lambda pos: _avoid_positions.append(pos))

    #What can we see?
    _can_see_positions = []
    fov.fov(life['pos'],
            sight.get_vision(life),
            callback=lambda pos: _can_see_positions.append(pos))

    #If there are no visible targets, we could be running away from a position we were attacked from
    _cover_exposed_at = brain.get_flag(life, 'cover_exposed_at')

    if _cover_exposed_at:
        _avoid_exposed_cover_positions = set()

        for pos in _cover_exposed_at[:]:
            if tuple(pos[:2]) in _can_see_positions:
                _cover_exposed_at.remove(pos)

                continue

            fov.fov(
                pos,
                int(round(sight.get_vision(life) * .25)),
                callback=lambda pos: _avoid_exposed_cover_positions.add(pos))

        for pos in _avoid_exposed_cover_positions:
            if not pos in _avoid_positions:
                _avoid_positions.append(pos)
    else:
        print 'Something went wrong'

        return False

    #Overlay the two, finding positions we can see but the target can't
    for pos in _can_see_positions[:]:
        if pos in _avoid_positions:
            _can_see_positions.remove(pos)
            continue

        #Get rid of positions that are too close
        for target_id in targets:
            _target = brain.knows_alife_by_id(life, target_id)

            if bad_numbers.distance(_target['last_seen_at'], pos) < 4:
                _can_see_positions.remove(pos)
                break

    #Now scan for cover to prevent hiding in the open
    for pos in _can_see_positions[:]:
        if chunks.get_chunk(chunks.get_chunk_key_at(pos))['max_z'] == 2:
            _can_see_positions.remove(pos)

    if not _can_see_positions:
        if life['pos'] in _cover_exposed_at:
            _cover_exposed_at.remove(life['pos'])

        return False

    if lfe.find_action(life, [{
            'action': 'dijkstra_move',
            'goals': _can_see_positions[:]
    }]):
        return True

    lfe.stop(life)
    lfe.add_action(
        life, {
            'action': 'dijkstra_move',
            'rolldown': True,
            'zones': _zones,
            'goals': _can_see_positions[:],
            'reason': 'escaping'
        }, 200)
示例#9
0
文件: sight.py 项目: flags/Reactor-3
def quick_look(life):
	_life = []
	_items = []
	_current_chunk = lfe.get_current_chunk_id(life)
	_current_chunk_pos = chunks.get_chunk(_current_chunk)['pos']
	_x_chunk_min = bad_numbers.clip(_current_chunk_pos[0]-((get_vision(life)/WORLD_INFO['chunk_size'])*WORLD_INFO['chunk_size']), 0, MAP_SIZE[0]-WORLD_INFO['chunk_size'])
	_y_chunk_min = bad_numbers.clip(_current_chunk_pos[1]-((get_vision(life)/WORLD_INFO['chunk_size'])*WORLD_INFO['chunk_size']), 0, MAP_SIZE[1]-WORLD_INFO['chunk_size'])
	_x_chunk_max = bad_numbers.clip(_current_chunk_pos[0]+((get_vision(life)/WORLD_INFO['chunk_size'])*WORLD_INFO['chunk_size']), 0, MAP_SIZE[0]-WORLD_INFO['chunk_size'])
	_y_chunk_max = bad_numbers.clip(_current_chunk_pos[1]+((get_vision(life)/WORLD_INFO['chunk_size'])*WORLD_INFO['chunk_size']), 0, MAP_SIZE[1]-WORLD_INFO['chunk_size'])
	_has_ready_weapon = combat.has_ready_weapon(life)
	
	for y in range(_y_chunk_min, _y_chunk_max, WORLD_INFO['chunk_size']):
		for x in range(_x_chunk_min, _x_chunk_max, WORLD_INFO['chunk_size']):
			_chunk_key = '%s,%s' % (x, y)
			_chunk = chunks.get_chunk(_chunk_key)
			
			for life_id in _chunk['life']:
				ai = LIFE[life_id]
				
				if ai['dead']:
					continue
				
				if life_id == life['id']:
					continue
				
				if not can_see_position(life, LIFE[life_id]['pos']):
					continue
				
				_visibility = get_visiblity_of_position(life, ai['pos'])
				_stealth_coverage = get_stealth_coverage(ai)
				
				if _visibility < 1-_stealth_coverage:
					continue
				
				if not ai['id'] in life['know']:
					brain.meet_alife(life, ai)
				
				life['seen'].append(ai['id'])
				
				if life['think_rate'] == life['think_rate_max']:
					lfe.create_and_update_self_snapshot(LIFE[ai['id']])
					judgement.judge_life(life, ai['id'])
				
				if ai['dead']:
					if life['know'][ai['id']]['group']:
						groups.remove_member(life, life['know'][ai['id']]['group'], ai['id'])
						life['know'][ai['id']]['group'] = None
					
					life['know'][ai['id']]['dead'] = True
				elif ai['asleep']:
					life['know'][ai['id']]['asleep'] = True
				elif not ai['asleep']:
					life['know'][ai['id']]['asleep'] = False
				
				life['know'][ai['id']]['last_seen_time'] = 0
				life['know'][ai['id']]['last_seen_at'] = ai['pos'][:]
				life['know'][ai['id']]['escaped'] = False
				life['know'][ai['id']]['state'] = ai['state']
				life['know'][ai['id']]['state_tier'] = ai['state_tier']
				
				if brain.alife_has_flag(life, ai['id'], 'search_map'):
					brain.unflag_alife(life, ai['id'], 'search_map')
				
				_chunk_id = lfe.get_current_chunk_id(ai)
				judgement.judge_chunk(life, _chunk_id, seen=True)
				
				_life.append(life_id)
			
			for item_uid in _chunk['items']:
				if not item_uid in ITEMS:
					continue
				
				item = ITEMS[item_uid]
				
				if not item['uid'] in life['know_items']:
					brain.remember_item(life, item)
	
				if items.is_item_owned(item['uid']):
					continue
				
				#	#TODO: This doesn't work because we are specifically checking chunks
				#	if item['owner'] and lfe.item_is_equipped(LIFE[item['owner']], item['uid']):
				#		life['know_items'][item['uid']]['last_seen_at'] = LIFE[item['owner']]['pos']
				#		life['know_items'][item['uid']]['last_owned_by'] = item['owner']
				#		life['know_items'][item['uid']]['last_seen_time'] = 0
				#	
				#	continue
				
				if not can_see_position(life, item['pos']):
					continue
				
				if not item['uid'] in life['know_items']:
					brain.remember_item(life, item)
				
				life['seen_items'].append(item['uid'])
				life['know_items'][item['uid']]['last_seen_at'] = item['pos'][:]
				life['know_items'][item['uid']]['last_seen_time'] = 0
				life['know_items'][item['uid']]['last_owned_by'] = None
				life['know_items'][item['uid']]['score'] = judgement.judge_item(life, item['uid'])
				life['know_items'][item['uid']]['lost'] = False
				
				_items.append(item_uid)
示例#10
0
def hide(life, targets):
    _target_positions = []
    _avoid_positions = []
    _zones = [zones.get_zone_at_coords(life["pos"])]

    if lfe.find_action(life, [{"action": "dijkstra_move", "reason": "escaping"}]):
        if not lfe.ticker(life, "escaping", 6):
            return False

            # What can the targets see?
    for target_id in targets:
        _target = brain.knows_alife_by_id(life, target_id)
        _zone = zones.get_zone_at_coords(_target["last_seen_at"])

        if not _zone in _zones:
            _zones.append(_zone)

        fov.fov(
            _target["last_seen_at"],
            sight.get_vision(_target["life"]),
            callback=lambda pos: _avoid_positions.append(pos),
        )

        # What can we see?
    _can_see_positions = []
    fov.fov(life["pos"], sight.get_vision(life), callback=lambda pos: _can_see_positions.append(pos))

    # If there are no visible targets, we could be running away from a position we were attacked from
    _cover_exposed_at = brain.get_flag(life, "cover_exposed_at")

    if _cover_exposed_at:
        _avoid_exposed_cover_positions = set()

        for pos in _cover_exposed_at[:]:
            if tuple(pos[:2]) in _can_see_positions:
                _cover_exposed_at.remove(pos)

                continue

            fov.fov(
                pos,
                int(round(sight.get_vision(life) * 0.25)),
                callback=lambda pos: _avoid_exposed_cover_positions.add(pos),
            )

        for pos in _avoid_exposed_cover_positions:
            if not pos in _avoid_positions:
                _avoid_positions.append(pos)
    else:
        print "Something went wrong"

        return False

        # Overlay the two, finding positions we can see but the target can't
    for pos in _can_see_positions[:]:
        if pos in _avoid_positions:
            _can_see_positions.remove(pos)
            continue

            # Get rid of positions that are too close
        for target_id in targets:
            _target = brain.knows_alife_by_id(life, target_id)

            if numbers.distance(_target["last_seen_at"], pos) < 4:
                _can_see_positions.remove(pos)
                break

                # Now scan for cover to prevent hiding in the open
    for pos in _can_see_positions[:]:
        if chunks.get_chunk(chunks.get_chunk_key_at(pos))["max_z"] == 2:
            _can_see_positions.remove(pos)

    if not _can_see_positions:
        if life["pos"] in _cover_exposed_at:
            _cover_exposed_at.remove(life["pos"])

        return False

    if lfe.find_action(life, [{"action": "dijkstra_move", "goals": _can_see_positions[:]}]):
        return True

    lfe.stop(life)
    lfe.add_action(
        life,
        {
            "action": "dijkstra_move",
            "rolldown": True,
            "zones": _zones,
            "goals": _can_see_positions[:],
            "reason": "escaping",
        },
        200,
    )
示例#11
0
def manage_combat(life, group_id):
    if has_flag(life, group_id, 'confident'):
        _was_confident = get_flag(life, group_id, 'confident')

        if _was_confident == stats.is_confident(life) and not lfe.ticker(
                life, 'decision_wait', 16):
            return False

    flag(life, group_id, 'confident', stats.is_confident(life))

    _existing_friendlies = get_flag(life, group_id, 'friendlies')
    _existing_targets = get_flag(life, group_id, 'targets')
    _last_focal_point = get_flag(life, group_id, 'last_focal_point')

    if not _existing_friendlies:
        _existing_friendlies = {}

    if not _existing_targets:
        _existing_targets = {}

    for life_id in get_group(life, group_id)['members']:
        if not life_id in _existing_friendlies:
            _existing_friendlies[life_id] = {'updated': -900}

    flag(life, group_id, 'friendlies', _existing_friendlies)

    _checked_targets = []
    for target_id in judgement.get_threats(life):
        if target_id in _existing_targets:
            _existing_targets[target_id]['time'] = 0
        else:
            _existing_targets[target_id] = {
                'time': 0,
                'pos': brain.knows_alife_by_id(life,
                                               target_id)['last_seen_at'][:]
            }

        _checked_targets.append(target_id)

    _enemy_focal_pos = None

    for target_id in _existing_targets:
        if not _enemy_focal_pos:
            _enemy_focal_pos = _existing_targets[target_id]['pos'][:]
        else:
            _enemy_focal_pos = bad_numbers.lerp_velocity(
                _enemy_focal_pos, _existing_targets[target_id]['pos'], 0.5)

        if target_id in _checked_targets:
            continue

        _existing_targets[target_id]['time'] += 1

        if _existing_targets[target_id]['time'] > 100:
            del _existing_targets[target_id]

            continue

    _hostile_chunks = get_flag(life, group_id, 'hostile_chunks')
    _previous_visible_chunks = brain.get_flag(life, 'group_combat_vis_chunks')

    if _previous_visible_chunks and _previous_visible_chunks[
            'from_pos'] == life['pos']:
        _visible_chunks = _previous_visible_chunks['visible_chunks']
    else:
        _visible_chunks = chunks.get_visible_chunks_from(
            life['pos'], life['vision_max'] * .75)

        brain.flag(life,
                   'group_combat_vis_chunks',
                   value={
                       'from_pos': life['pos'][:],
                       'visible_chunks': _visible_chunks
                   })

    if _enemy_focal_pos:
        lfe.clear_ticker(life, 'group_command_reset')

        if not _last_focal_point or bad_numbers.distance(
                _enemy_focal_pos, _last_focal_point) > 30:
            _hostile_chunks = chunks.get_visible_chunks_from(
                (int(round(_enemy_focal_pos[0])),
                 int(round(_enemy_focal_pos[1])), 2), life['vision_max'] * 1.5)

            flag(life, group_id, 'hostile_chunks', _hostile_chunks)
            flag(life, group_id, 'visible_chunks', _visible_chunks)
            flag(life, group_id, 'last_focal_point', _enemy_focal_pos)

    else:
        _ticker = lfe.ticker(life, 'group_command_reset', 48)

        if get_stage(life, group_id) == STAGE_ATTACKING:
            if _ticker:
                set_stage(life, group_id, STAGE_FORMING)
                flag(life, group_id, 'friendlies', None)
                flag(life, group_id, 'strategy', None)
            else:
                manage_strategy(life, group_id)

        return False

    if not get_stage(life, group_id) == STAGE_ATTACKING:
        speech.announce_combat_to_group(life, group_id)
        set_stage(life, group_id, STAGE_ATTACKING)

    if not lfe.ticker(life, 'group_command_rate', 3):
        return False

    _orig_visible_chunks = _visible_chunks[:]

    #TODO: Check distance to threat
    for hostile_chunk_key in _hostile_chunks:
        if hostile_chunk_key in _visible_chunks:
            _visible_chunks.remove(hostile_chunk_key)

    #TODO: Additional stages: PLANNING, EXECUTING
    if _visible_chunks and stats.is_confident(life):
        for target_id in order_spread_out(
                life,
                group_id,
                _visible_chunks,
                filter_by=lambda life_id: WORLD_INFO[
                    'ticks'] - _existing_friendlies[life_id]['updated'] > 100):
            _existing_friendlies[target_id]['updated'] = WORLD_INFO['ticks']
    else:
        _distant_chunk = {'distance': -1, 'chunk_key': None}
        _unchecked_members = get_group(life, group_id)['members'][:]

        for chunk_key in _orig_visible_chunks:
            _distance = bad_numbers.distance(
                (int(round(
                    _enemy_focal_pos[0])), int(round(_enemy_focal_pos[1]))),
                chunks.get_chunk(chunk_key)['pos'])
            _distance *= bad_numbers.clip(
                bad_numbers.distance(life['pos'], _enemy_focal_pos), 1,
                35) / 35.0

            if chunk_key in _visible_chunks:
                _distance *= 2

            for member_id in _unchecked_members:
                if life['id'] == member_id:
                    continue

                _target = brain.knows_alife_by_id(life, member_id)

                if _target['last_seen_time'] <= 25 and chunks.get_chunk_key_at(
                        _target['last_seen_at']) == chunk_key:
                    _distance *= (2.5 * (1 - (bad_numbers.clip(
                        _target['last_seen_time'], 0, 25) / 25.0)))

            if _distance > _distant_chunk['distance']:
                _distant_chunk['distance'] = _distance
                _distant_chunk['chunk_key'] = chunk_key

        if _distant_chunk['chunk_key']:
            for target_id in order_move_to(
                    life,
                    group_id,
                    _distant_chunk['chunk_key'],
                    filter_by=lambda life_id: WORLD_INFO['ticks'] -
                    _existing_friendlies[life_id]['updated'] > 100):
                _existing_friendlies[target_id]['updated'] = WORLD_INFO[
                    'ticks']

        return False
示例#12
0
def escape(life, targets):
	_target_positions = []
	_avoid_positions = []
	_zones = [zones.get_zone_at_coords(life['pos'])]
	
	if lfe.find_action(life, [{'action': 'dijkstra_move', 'reason': 'escaping'}]):
		if not lfe.ticker(life, 'escaping', 4):
			return False
	
	#What can the targets see?
	for target_id in targets:
		_target = brain.knows_alife_by_id(life, target_id)
		_zone = zones.get_zone_at_coords(_target['last_seen_at'])
		
		if not _zone in _zones:
			_zones.append(_zone)
		
		fov.fov(_target['last_seen_at'], sight.get_vision(_target['life']), callback=lambda pos: _avoid_positions.append(pos))
	
	#What can we see?
	_can_see_positions = []
	fov.fov(life['pos'], sight.get_vision(life), callback=lambda pos: _can_see_positions.append(pos))
	
	#If there are no visible targets, we could be running away from a position we were attacked from
	_cover_exposed_at = brain.get_flag(life, 'cover_exposed_at')
	
	if _cover_exposed_at:
		_avoid_exposed_cover_positions = set()
		
		for pos in _cover_exposed_at[:]:
			if tuple(pos[:2]) in _can_see_positions:
				print 'ok!!!'*20
				_cover_exposed_at.remove(pos)
				continue
			
			fov.fov(pos, int(round(sight.get_vision(life)*.25)), callback=lambda pos: _avoid_exposed_cover_positions.add(pos))
		
		for pos in _avoid_exposed_cover_positions:
			if not pos in _avoid_positions:
				_avoid_positions.append(pos)
	
	#Overlay the two, finding positions we can see but the target can't
	for pos in _can_see_positions[:]:
		if pos in _avoid_positions:
			_can_see_positions.remove(pos)
			continue
	
		#Get rid of positions that are too close
		for target_id in targets:
			_target = brain.knows_alife_by_id(life, target_id)
			
			#TODO: Unhardcode 15
			if numbers.distance(_target['last_seen_at'], pos)<10:
				_can_see_positions.remove(pos)
				break
	
	#Now scan for cover to prevent hiding in the open
	for pos in _can_see_positions[:]:
		if chunks.get_chunk(chunks.get_chunk_key_at(pos))['max_z'] == 2:
			_can_see_positions.remove(pos)
	
	#for target_id in targets:
		#_target = brain.knows_alife_by_id(life, target_id)
		#_target_positions.append(_target['last_seen_at'][:])
		#_zone = zones.get_zone_at_coords(_target['last_seen_at'])
		
		#if not _zone in _zones:
		#	_zones.append(_zone)
		
		#for chunk_key in chunks.get_visible_chunks_from(_target['last_seen_at'], sight.get_vision(_target['life'])):
		#	if chunk_key in _visible_target_chunks:
		#		continue
			
		#	_visible_target_chunks.append(chunk_key)
	
	#for friendly_id in life['seen']:
	#	_chunk_key = lfe.get_current_chunk_id(LIFE[friendly_id])
	#	
	#	if not _chunk_key in _visible_target_chunks:
	#		_visible_target_chunks.append(_chunk_key)
	
	#if not _target_positions:
	#	return False
	
	#TODO: #combat: For lower limit in return_score_in_range, use range of weapon
	#_cover = zones.dijkstra_map(life['pos'],
	#                            _avoid_positions,
	#                            _zones,
	#                            avoid_chunks=[],
	#                            return_score_in_range=[1, 5]) # sight.get_vision(life)
	#_cover = [(c[0], c[1], life['pos'][2]) for c in _cover]
	#if not _cover:
	#	return False
	
	#_zones = [zones.get_zone_at_coords(life['pos'])]
	#for _pos in _cover:
	#	_zone = zones.get_zone_at_coords(_pos)
		
	#	if not _zone in _zones:
	#		_zones.append(_zone)
	
	if not _can_see_positions:
		return False
	
	if lfe.find_action(life, [{'action': 'dijkstra_move', 'goals': _can_see_positions[:]}]):
		return True
	
	lfe.stop(life)
	lfe.add_action(life, {'action': 'dijkstra_move',
	                      'rolldown': True,
	                      'zones': _zones,
	                      'goals': _can_see_positions[:],
	                      'reason': 'escaping'},
	               999)