Esempio n. 1
0
File: ai.py Progetto: penny64/r3-tdw
def update_target_memory(entity, target_id, key, value):
    if target_id in entity['ai']['life_memory']:
        entity['ai']['life_memory'][target_id][key] = value

        if key == 'last_seen_at' and not target_id in entity['ai']['targets']:
            if ai_factions.is_enemy(entity, target_id):
                entity['ai']['targets'].add(target_id)
                entity['ai']['life_memory'][target_id]['searched_for'] = False
Esempio n. 2
0
def update_target_memory(entity, target_id, key, value):
    if target_id in entity["ai"]["life_memory"]:
        entity["ai"]["life_memory"][target_id][key] = value

        if key == "last_seen_at" and not target_id in entity["ai"]["targets"]:
            if ai_factions.is_enemy(entity, target_id):
                entity["ai"]["targets"].add(target_id)
                entity["ai"]["life_memory"][target_id]["searched_for"] = False
Esempio n. 3
0
def _handle_new_target(entity, target_id):
	if ai_factions.is_enemy(entity, target_id) and not len(entity['ai']['targets'] & entity['ai']['visible_life']):
		_can_see = target_id in [e for e in entity['ai']['life_memory'] if entity['ai']['life_memory'][e]['in_los']]
		
		#if _can_see:
		#	settings.set_tick_mode('strategy')
		
		if entity['ai']['life_memory'][target_id]['seen_time'] == 1:
			ui_director.focus_on_entity(entity, target_id, show_line=_can_see)
Esempio n. 4
0
def _handle_new_target(entity, target_id):
    if ai_factions.is_enemy(
            entity, target_id) and not len(entity['ai']['targets']
                                           & entity['ai']['visible_life']):
        _can_see = target_id in [
            e for e in entity['ai']['life_memory']
            if entity['ai']['life_memory'][e]['in_los']
        ]

        #if _can_see:
        #	settings.set_tick_mode('strategy')

        if entity['ai']['life_memory'][target_id]['seen_time'] == 1:
            ui_director.focus_on_entity(entity, target_id, show_line=_can_see)
Esempio n. 5
0
def create_life_interact_menu(entity, target_id):
	if not items.get_items_in_holder(entity, 'weapon'):
		return
	
	_target = entities.get_entity(target_id)
	_is_enemy = ai_factions.is_enemy(entity, target_id)
	_menu = ui_menu.create(ui_cursor.CURSOR['tile']['x']+2, ui_cursor.CURSOR['tile']['y']-4, title='Context')
	
	if not _is_enemy:
		if _target['missions']['inactive']:
			ui_menu.add_selectable(_menu, 'Talk', lambda: create_talk_menu(entity, target_id))
		
		ui_menu.add_selectable(_menu, 'Inquire', lambda: create_mission_menu(entity, target_id))
		ui_menu.add_selectable(_menu, 'Trade', lambda: create_mission_menu(entity, target_id))
	
	ui_menu.add_selectable(_menu, 'Shoot%s' % (' (Friendly fire)' * (not _is_enemy)), lambda: create_shoot_menu(entity, target_id))
Esempio n. 6
0
def share_life_memory_location(entity, target_id, life_id):
	_target = entities.get_entity(target_id)
	_is_life_enemy = ai_factions.is_enemy(entity, life_id)
	
	if not life_id in entity['ai']['life_memory'] or not entity['ai']['life_memory'][life_id]['last_seen_at']:
		entities.trigger_event(_target, 'receive_memory',
		                       member_id=entity['_id'],
		                       memory={},
		                       message='I don\'t know who you\'re talking about.')
	
	else:
		_memory = entity['ai']['life_memory'][life_id]
		
		entities.trigger_event(_target, 'receive_memory',
		                       member_id=entity['_id'],
		                       memory={life_id: {'last_seen_at': _memory['last_seen_at'][:]}},
		                       message='Oh yeah, I saw them recently.')
Esempio n. 7
0
def handle_player_start_of_turn(entity, squad_id):
	if ai_squads.get_assigned_squad(entity)['_id'] == squad_id:
		settings.set_tick_mode('strategy')
	
		_squad = entities.get_entity(squad_id)
		
		if not ai_factions.is_enemy(entity, _squad['leader']) and entity['_id'] == _squad['leader']:
			if _squad['meta']['is_squad_combat_ready']:
				_message = random.choice(['Locked and loaded.',
					                      'Nuke \'em!',
					                      'No mercy, boys...'])
			
			elif _squad['meta']['is_squad_overwhelmed']:
				_message = random.choice(['We\'re outnumbered!',
					                      'Fall back!'])
				
			else:
				return
			
			effects.message(_message, time=120, center=True)
Esempio n. 8
0
def handle_player_start_of_turn(entity, squad_id):
    if ai_squads.get_assigned_squad(entity)['_id'] == squad_id:
        settings.set_tick_mode('strategy')

        _squad = entities.get_entity(squad_id)

        if not ai_factions.is_enemy(
                entity,
                _squad['leader']) and entity['_id'] == _squad['leader']:
            if _squad['meta']['is_squad_combat_ready']:
                _message = random.choice(
                    ['Locked and loaded.', 'Nuke \'em!', 'No mercy, boys...'])

            elif _squad['meta']['is_squad_overwhelmed']:
                _message = random.choice(['We\'re outnumbered!', 'Fall back!'])

            else:
                return

            effects.message(_message, time=120, center=True)
Esempio n. 9
0
def create_life_interact_menu(entity, target_id):
    if not items.get_items_in_holder(entity, 'weapon'):
        return

    _target = entities.get_entity(target_id)
    _is_enemy = ai_factions.is_enemy(entity, target_id)
    _menu = ui_menu.create(ui_cursor.CURSOR['tile']['x'] + 2,
                           ui_cursor.CURSOR['tile']['y'] - 4,
                           title='Context')

    if not _is_enemy:
        if _target['missions']['inactive']:
            ui_menu.add_selectable(_menu, 'Talk',
                                   lambda: create_talk_menu(entity, target_id))

        ui_menu.add_selectable(_menu, 'Inquire',
                               lambda: create_mission_menu(entity, target_id))
        ui_menu.add_selectable(_menu, 'Trade',
                               lambda: create_mission_menu(entity, target_id))

    ui_menu.add_selectable(_menu,
                           'Shoot%s' % (' (Friendly fire)' * (not _is_enemy)),
                           lambda: create_shoot_menu(entity, target_id))
Esempio n. 10
0
def share_life_memory_location(entity, target_id, life_id):
    _target = entities.get_entity(target_id)
    _is_life_enemy = ai_factions.is_enemy(entity, life_id)

    if not life_id in entity['ai']['life_memory'] or not entity['ai'][
            'life_memory'][life_id]['last_seen_at']:
        entities.trigger_event(
            _target,
            'receive_memory',
            member_id=entity['_id'],
            memory={},
            message='I don\'t know who you\'re talking about.')

    else:
        _memory = entity['ai']['life_memory'][life_id]

        entities.trigger_event(
            _target,
            'receive_memory',
            member_id=entity['_id'],
            memory={life_id: {
                'last_seen_at': _memory['last_seen_at'][:]
            }},
            message='Oh yeah, I saw them recently.')
Esempio n. 11
0
def build_life_list(entity):
	entity['ai']['visible_targets'] = []
	_nearest_target = {'target_id': None, 'distance': 0}
	_solids = zones.get_active_solids(entity)
	_visible_life = set()
	_vision = stats.get_vision(entity)
	_visible_by_friendlies = [entities.get_entity(e)['ai']['visible_targets'] for e in entity['ai']['life_memory'] if not entity['ai']['life_memory'][e]['is_target'] and e in entities.ENTITIES]
	
	#Warning: Makes AI super smart
	_visible_by_friendlies = [item for sublist in _visible_by_friendlies for item in sublist]
	
	for entity_id in entities.get_entity_group('life'):
		if entity['_id'] == entity_id:
			continue
		
		_target = entities.get_entity(entity_id)
		
		if not entity_id in entity['ai']['life_memory']:
			life.create_life_memory(entity, entity_id)
		
		if not ai_factions.is_enemy(entity, entity_id):
			_visible = True
		
		else:
			if numbers.distance(movement.get_position(entity), movement.get_position(_target)) > _vision:
				if entity['ai']['life_memory'][entity_id]['can_see'] and ai_factions.is_enemy(entity, _target['_id']):
					entities.trigger_event(entity, 'target_lost', target_id=entity_id)
				
				if entity_id in _visible_by_friendlies:
					entity['ai']['life_memory'][entity_id]['in_los'] = False
					entity['ai']['life_memory'][entity_id]['can_see'] = True
					
					_visible = True
				else:
					entity['ai']['life_memory'][entity_id]['can_see'] = False
					entity['ai']['life_memory'][entity_id]['in_los'] = False
					
					if entity_id in entity['ai']['visible_life']:
						entity['ai']['visible_life'].remove(entity_id)
					
					_visible = False
				
			else:
				for pos in shapes.line(movement.get_position(entity), movement.get_position(_target)):
					if pos in _solids:
						if entity['ai']['life_memory'][entity_id]['can_see'] and ai_factions.is_enemy(entity, _target['_id']):
							entities.trigger_event(entity, 'target_lost', target_id=entity_id)
						
						if entity_id in _visible_by_friendlies:
							entity['ai']['life_memory'][entity_id]['in_los'] = False
							entity['ai']['life_memory'][entity_id]['can_see'] = True
							
							_visible = True
						
						else:
							entity['ai']['life_memory'][entity_id]['can_see'] = False
							entity['ai']['life_memory'][entity_id]['in_los'] = False
							
							if entity_id in entity['ai']['visible_life']:
								entity['ai']['visible_life'].remove(entity_id)
							
							_visible = False
						
						break
				else:
					entity['ai']['life_memory'][entity_id]['in_los'] = True
					_visible = True
		
		if not _visible and entity['ai']['life_memory'][entity_id]['seen_time'] > 0:
			_visible = True
			
			entity['ai']['life_memory'][entity_id]['in_los'] = False
			entity['ai']['life_memory'][entity_id]['can_see'] = True
			
			entity['ai']['life_memory'][entity_id]['seen_time'] -= 1
			
			if entity_id in entity['ai']['visible_life']:
				entity['ai']['visible_life'].remove(entity_id)
		
		elif not _visible and entity_id in entity['ai']['visible_life']:
			entity['ai']['visible_life'].remove(entity_id)
		
		if _visible:
			_previous_last_seen_at = entity['ai']['life_memory'][entity_id]['last_seen_at']
			_target_position = movement.get_position(_target)[:]
			
			entity['ai']['life_memory'][entity_id]['is_lost'] = False
			
			if entity['ai']['life_memory'][entity_id]['in_los']:
				if entity['ai']['life_memory'][entity_id]['seen_time'] < 30:
					entity['ai']['life_memory'][entity_id]['seen_time'] += 1
			
			else:
				entity['ai']['life_memory'][entity_id]['seen_time'] -= 1
				
				if entity['ai']['life_memory'][entity_id]['seen_time'] < 0 and entity_id in entity['ai']['visible_life']:
					entity['ai']['visible_life'].remove(entity_id)
					entity['ai']['life_memory'][entity_id]['in_los'] = False
			
			if movement.get_position(_target) == _previous_last_seen_at:
				_new_last_seen_at = _previous_last_seen_at
			
			else:
				_new_last_seen_at = _target_position
			
			_is_target = ai_factions.is_enemy(entity, _target['_id'])
			_profile = {'distance': numbers.distance(movement.get_position(entity), movement.get_position(_target)),
				        'is_target': _is_target,
				        'is_armed': items.get_items_in_holder(_target, 'weapon'),
			            'is_lost': False,
			            'can_see': True,
			            'last_seen_at': _new_last_seen_at,
			            'last_seen_velocity': None}
			
			if not entity_id in entity['ai']['visible_life']:
				entities.trigger_event(entity, 'new_target_spotted', target_id=entity_id)
			
			if entity['ai']['life_memory'][entity_id]['in_los']:
				entity['ai']['visible_life'].add(entity_id)
				
				if _is_target:
					ai_flow.register_combat(entity, entity_id)
			
			if _is_target:
				entity['ai']['targets'].add(entity_id)
				
				_distance = numbers.distance(movement.get_position(entity), movement.get_position_via_id(entity_id))
				
				if not _nearest_target['target_id'] or _distance < _nearest_target['distance']:
					_nearest_target['distance'] = _distance
					_nearest_target['target_id'] = entity_id
			
			if entity['ai']['life_memory'][entity_id]['last_seen_at']:
				_last_seen_at = entity['ai']['life_memory'][entity_id]['last_seen_at'][:]
				_velocity = (_profile['last_seen_at'][0]-_last_seen_at[0], _profile['last_seen_at'][1]-_last_seen_at[1])
				
				_profile['last_seen_velocity'] = _velocity
			else:
				_profile['last_seen_velocity'] = None
			
			if not entity['ai']['life_memory'][entity_id]['can_see'] and _is_target:
				_could_not_see_target_before = True
			else:
				_could_not_see_target_before = False
			
			entity['ai']['life_memory'][entity_id].update(_profile)
			
			if _could_not_see_target_before:
				entities.trigger_event(entity, 'target_found', target_id=entity_id)
	
	#TODO: What?
	#for t in entity['ai']['life_memory']:
	#	if not 'is_lost' in entity['ai']['life_memory'][t]:
	#		print entity['ai']['life_memory'][t]
	
	entity['ai']['visible_targets'] = list(entity['ai']['visible_life'] & entity['ai']['targets'])
	entity['ai']['targets_to_search'] = [t for t in entity['ai']['life_memory'].keys() if entity['ai']['life_memory'][t]['is_lost'] and not entity['ai']['life_memory'][t]['searched_for'] and t in entities.ENTITIES]
	
	if _nearest_target['target_id']:
		if not entity['ai']['nearest_target'] == _nearest_target['target_id']:
			entity['ai']['meta']['has_firing_position'] = True
		
		entity['ai']['nearest_target'] = _nearest_target['target_id']
	
	elif entity['ai']['targets']:
		for target_id in list(entity['ai']['targets']):
			_target = entity['ai']['life_memory'][target_id]
			
			if not target_id in entities.ENTITIES or _target['is_lost']:
				entity['ai']['targets'].remove(target_id)
				
				continue
			
			_distance = numbers.distance(movement.get_position(entity), _target['last_seen_at'])
			
			if not _nearest_target['target_id'] or _distance < _nearest_target['distance']:
				_nearest_target['target_id'] = target_id
				_nearest_target['distance'] = _distance
		
		if not entity['ai']['nearest_target'] == _nearest_target['target_id']:
			entity['ai']['meta']['has_firing_position'] = True
		
		entity['ai']['nearest_target'] = _nearest_target['target_id']