Example #1
0
def announce(life, gist, public=False, trusted=False, group=None, filter_if=None, **kwargs):
	"""Sends `gist` to any known ALife. If `public`, then send to everyone."""
	if public:
		_announce_to = [LIFE[i] for i in LIFE if not i == life['id']]
	elif trusted:
		_announce_to = [life['know'][i]['life'] for i in life['know'] if life['know'][i]['alignment'] == 'trust']
	elif group:
		_announce_to = [LIFE[i] for i in groups.get_group(life, group)['members'] if not i == life['id']]
	else:
		_announce_to = [life['know'][i]['life'] for i in life['know'] if not judgement.is_target_dangerous(life, i)]
	
	for target in _announce_to:
		if not stats.can_talk_to(life, target['id']):
			continue
		
		if filter_if and filter_if(life):
			return False
		
		_radio = False
		if not sight.can_see_position(life, target['pos']):
			if lfe.get_all_inventory_items(life, matches=[{'name': 'radio'}]):
				_radio = True
			else:
				continue
	
		memory.create_question(life, target['id'], gist, **kwargs)
	
	return True
Example #2
0
def get_stance_towards(life, target_id):
    _know = brain.knows_alife_by_id(life, target_id)

    if _know:
        if judgement.can_trust(life, target_id):
            return 'friendly'
        elif judgement.is_target_dangerous(life, target_id):
            return 'hostile'
        else:
            return 'neutral'
    else:
        return 'neutral'
Example #3
0
def get_stance_towards(life, target_id):
	_know = brain.knows_alife_by_id(life, target_id)
	
	if _know:
		if judgement.can_trust(life, target_id):
			return 'friendly'
		elif judgement.is_target_dangerous(life, target_id):
			return 'hostile'
		else:
			return 'neutral'
	else:
		return 'neutral'
Example #4
0
def announce(life,
             gist,
             public=False,
             trusted=False,
             group=None,
             filter_if=None,
             **kwargs):
    """Sends `gist` to any known ALife. If `public`, then send to everyone."""
    if public:
        _announce_to = [LIFE[i] for i in LIFE if not i == life['id']]
    elif trusted:
        _announce_to = [
            life['know'][i]['life'] for i in life['know']
            if life['know'][i]['alignment'] == 'trust'
        ]
    elif group:
        _announce_to = [
            LIFE[i] for i in groups.get_group(life, group)['members']
            if not i == life['id']
        ]
    else:
        _announce_to = [
            life['know'][i]['life'] for i in life['know']
            if not judgement.is_target_dangerous(life, i)
        ]

    for target in _announce_to:
        if not stats.can_talk_to(life, target['id']):
            continue

        if filter_if and filter_if(target['id']):
            continue

        _radio = False
        if not sight.can_see_position(life, target['pos']):
            if lfe.get_all_inventory_items(life, matches=[{'name': 'radio'}]):
                _radio = True
            else:
                continue

        memory.create_question(life, target['id'], gist, **kwargs)

    return True
Example #5
0
def listen(life):
    for event in life['heard'][:]:
        if not event['from']['id'] in life['know']:
            pass

        if not brain.knows_alife(life, event['from']):
            brain.meet_alife(life, event['from'])

            logging.info(
                '%s learned about %s via listen.' %
                (' '.join(life['name']), ' '.join(event['from']['name'])))

        if event['gist'] == 'looks_hostile':
            #speech.communicate(life, 'surrender', matches=[{'id': event['from']['id']}])
            pass

        elif event['gist'] == 'camp_raid':
            print '*' * 10
            print 'RAID IN EFFECT!!!!!!!!!!'
            print '*' * 10
            _knows = brain.knows_alife(life, event['from'])
            _raid = raids.defend_camp(event['camp']['id'], life['id'])

            if _knows and not judgement.is_target_dangerous(
                    life, _knows['life']['id']):
                lfe.memory(life,
                           'heard about a camp raid',
                           camp=event['camp']['id'])
                _raid_score = judgement.judge_raid(life, event['raiders'],
                                                   event['camp']['id'])
                speech.announce(life, 'raid_score', raid_score=_raid_score)

        elif event['gist'] == 'raid_score':
            print life['name'], 'Got friendly raid score:', event['raid_score']

        elif event['gist'] == 'share_item_info':
            if event['item'] in ITEMS:
                if not brain.has_remembered_item(life, event['item']):
                    lfe.memory(life,
                               'heard about an item',
                               item=event['item'],
                               target=event['from']['id'])
                    brain.remember_item(life, ITEMS[event['item']])

        elif event['gist'] == 'camp_founder':
            lfe.memory(life,
                       'heard about camp',
                       camp=event['camp'],
                       target=event['founder'],
                       founder=event['founder'])

            print 'Thanks for the camp founder info!'

        elif event['gist'] == 'under_attack':
            _knows_attacker = True

            if life['id'] == event['attacker']:
                pass
            else:
                print life['name'], 'HEARD CALL FOR HELP FROM', event['from'][
                    'name']
                if not brain.knows_alife_by_id(life, event['attacker']):
                    brain.meet_alife(life, LIFE[event['attacker']])
                    _knows_attacker = False

                _target = brain.knows_alife_by_id(life, event['attacker'])
                _believes = judgement.believe_which_alife(
                    life, [event['from']['id'], event['attacker']])

                #SITUATION 1: We believe it
                if _believes == event['from']['id']:
                    lfe.memory(life,
                               'heard about attack',
                               attacker=event['attacker'],
                               target=event['from']['id'])
                    lfe.memory(life,
                               'target attacked victim',
                               target=event['attacker'],
                               victim=event['from']['id'])

                    if event['last_seen_at']:
                        _target['last_seen_at'] = event['last_seen_at'][:]
                    else:
                        _target['last_seen_at'] = event['from']['pos'][:]

                    judgement.judge_life(life, event['attacker'])
                else:
                    lfe.memory(life,
                               'reject under_attack: attacker is trusted',
                               attacker=event['attacker'],
                               target=event['from']['id'])

        elif event['gist'] == 'bit':
            #React to attack... this needs to be a function in stats.py
            if event['target'] == life['id']:
                pass
            else:
                _trust_sender = judgement.can_trust(life, event['from']['id'])

                if brain.knows_alife_by_id(life, event['target']):
                    _trust_target = judgement.can_trust(life,
                                                        event['target'],
                                                        low=5)
                else:
                    brain.meet_alife(life, LIFE[event['target']])
                    _trust_target = False

                if _trust_target and not _trust_sender and 1 == 4:
                    lfe.memory(life,
                               'trusted target attacked by',
                               victim=event['target'],
                               target=event['from']['id'])

        elif event['gist'] == 'consume_item':
            lfe.memory(life, 'consume_item', target=event['from']['id'])

        elif event['gist'] == 'call':
            if judgement.can_trust(life, event['from']['id']):
                speech.start_dialog(life,
                                    event['from']['id'],
                                    'call_accepted',
                                    remote=True)

        elif event['gist'] == 'order_attack':
            lfe.memory(life, 'ordered to attack', target=event['target'])

        elif event['gist'] == 'threw_an_item':
            print 'CHECK THIS HERE' * 100
            pass

        elif event['gist'] == '_group_leader_state_change':
            life['think_rate'] = 0

        elif event['gist'] == 'dialog':
            if not 'player' in life and not event['dialog_id'] in life[
                    'dialogs']:
                life['dialogs'].append(event['dialog_id'])

        else:
            logging.warning('Unhandled ALife context: %s' % event['gist'])

        life['heard'].remove(event)
Example #6
0
def setup(life):
	#TODO: Add these two values to an array called PANIC_STATES
	#if not alife_seen:
	#	return False
	#if brain.retrieve_from_memory(life, 'tension_spike') >= 10:
	#	lfe.say(life, '@n panics!', action=True)
	
	_potential_talking_targets = []
	for ai in life['seen']:
		if not stats.can_talk_to(life, ai):
			continue
		
		#print life['name'], LIFE[ai]['name'], judgement.get_tension_with(life, ai)
		
		if stats.has_attacked_self(life, ai):
			stats.react_to_attack(life, ai)
		elif 0<judgement.get_tension_with(life, ai)<=judgement.get_max_tension_with(life, ai):
			stats.react_to_tension(life, ai)
		else:
			#if not stats.desires_first_contact_with(life, ai) and not stats.desires_conversation_with(life, ai):
			#	continue
			if not stats.desires_conversation_with(life, ai):
				continue
	
			_potential_talking_targets.append(ai)
	
	if not _potential_talking_targets:
		if life['dialogs']:
			_dialog = life['dialogs'][0]
			dialog.process(life, _dialog)
		
		if not lfe.ticker(life, 'talk', 6):
			return False
	
	if lfe.get_all_inventory_items(life, matches=[{'type': 'radio'}]):
		for ai in life['know']:
			if ai in _potential_talking_targets:
				continue
			
			if not stats.can_talk_to(life, ai):
				continue
			
			_potential_talking_targets.append(ai)
	
	#TODO: Score these
	random.shuffle(_potential_talking_targets)
	
	for target in _potential_talking_targets:
		if life['dialogs']:
			break
		
		#if stats.desires_first_contact_with(life, target):
		#	memory.create_question(life, target, 'establish_relationship', ignore_if_said_in_last=-1)
		
		if memory.get_questions_for_target(life, target):
			_question = memory.ask_target_question(life, target)
			speech.start_dialog(life, target, _question['gist'], **_question['args'])
		elif memory.get_orders_for_target(life, target):
			speech.start_dialog(life, target, 'give_order')
		elif stats.wants_group_member(life, target):
			memory.create_question(life, target, 'recruit', ignore_if_said_in_last=-1, group_id=life['group'])
	
	if life['dialogs']:
		_dialog = life['dialogs'][0]
		dialog.process(life, _dialog)	
	
	if not judgement.is_safe(life) and lfe.ticker(life, 'call_for_help', 90, fire=True):
		_combat_targets = judgement.get_ready_combat_targets(life)
		
		if _combat_targets:
			if life['camp'] and camps.is_in_camp(life, lfe.get_current_camp(life)):
				_nearest_camp = camps.get_nearest_known_camp(life)
				raids.create_raid(_nearest_camp['id'], join=life['id'])
				raids.add_raiders(_nearest_camp['id'], _combat_targets)
				
				#TODO: Remove memory call
				speech.announce(life,
					'camp_raid',
					camp=_nearest_camp,
					raiders=_combat_targets)
			
			if life['group']:
				for target in _combat_targets:
					_last_seen_at = None
					_know = brain.knows_alife_by_id(life, target)
					
					if _know:
						_last_seen_at = _know['last_seen_at']
						
					groups.distribute(life, 'under_attack', attacker=target, last_seen_at=_last_seen_at)
		
		for target in judgement.get_ready_combat_targets(life):
			_last_seen_at = None
			_know = brain.knows_alife_by_id(life, target)
			
			if _know:
				_last_seen_at = _know['last_seen_at']

			speech.announce(life, 'attacked_by_hostile', trusted=True, target_id=target, last_seen_at=_last_seen_at)

	_visible_items = [life['know_items'][item] for item in life['know_items'] if not life['know_items'][item]['last_seen_time'] and not 'parent_id' in ITEMS[life['know_items'][item]['item']]]
	for ai in [life['know'][i] for i in life['know']]:
		if judgement.is_target_dangerous(life, ai['life']['id']):
			continue
		
		#if life['state'] == 'combat':
		#	break
		
		if ai['life']['state'] in ['hiding', 'hidden']:
			break
		
		if not stats.can_talk_to(life, ai['life']['id']):
			continue

		for item in _visible_items:
			#TODO: Check
			if brain.has_shared_item_with(life, ai['life'], item['item']):
				continue

			if not item['item'] in ITEMS:
				continue

			brain.share_item_with(life, ai['life'], item['item'])
			speech.communicate(life,
				'share_item_info',
				item=item['item'],
				matches=[ai['life']['id']])
Example #7
0
def listen(life):
	for event in life['heard'][:]:
		if not event['from']['id'] in life['know']:
			pass
		
		if not brain.knows_alife(life, event['from']):
			brain.meet_alife(life, event['from'])
			
			logging.info('%s learned about %s via listen.' % (' '.join(life['name']), ' '.join(event['from']['name'])))
		
		if event['gist'] == 'looks_hostile':
			#speech.communicate(life, 'surrender', matches=[{'id': event['from']['id']}])
			pass
		
		elif event['gist'] == 'camp_raid':
			print '*' * 10
			print 'RAID IN EFFECT!!!!!!!!!!'
			print '*' * 10
			_knows = brain.knows_alife(life, event['from'])
			_raid = raids.defend_camp(event['camp']['id'], life['id'])
			
			if _knows and not judgement.is_target_dangerous(life, _knows['life']['id']):
				lfe.memory(life, 'heard about a camp raid', camp=event['camp']['id'])
				_raid_score = judgement.judge_raid(life, event['raiders'], event['camp']['id'])
				speech.announce(life, 'raid_score', raid_score=_raid_score)
		
		elif event['gist'] == 'raid_score':
			print life['name'],'Got friendly raid score:', event['raid_score'] 

		elif event['gist'] == 'share_item_info':
			if event['item'] in ITEMS:
				if not brain.has_remembered_item(life, event['item']):
					lfe.memory(life, 'heard about an item',
						item=event['item'],
						target=event['from']['id'])
					brain.remember_item(life, ITEMS[event['item']])
		
		elif event['gist'] == 'camp_founder':
			lfe.memory(life, 'heard about camp',
				camp=event['camp'],
				target=event['founder'],
				founder=event['founder'])
			
			print 'Thanks for the camp founder info!'
		
		elif event['gist'] == 'under_attack':
			_knows_attacker = True
			
			if life['id'] == event['attacker']:
				pass
			else:
				print life['name'], 'HEARD CALL FOR HELP FROM', event['from']['name']
				if not brain.knows_alife_by_id(life, event['attacker']):
					brain.meet_alife(life, LIFE[event['attacker']])
					_knows_attacker = False
				
				_target = brain.knows_alife_by_id(life, event['attacker'])
				_believes = judgement.believe_which_alife(life, [event['from']['id'], event['attacker']])
	
				#SITUATION 1: We believe it
				if _believes == event['from']['id']:
					lfe.memory(life, 'heard about attack',
						attacker=event['attacker'],
						target=event['from']['id'])
					lfe.memory(life, 'target attacked victim',
						target=event['attacker'],
						victim=event['from']['id'])
					
					if event['last_seen_at']:
						_target['last_seen_at'] = event['last_seen_at'][:]
					else:
						_target['last_seen_at'] = event['from']['pos'][:]
					
					judgement.judge_life(life, event['attacker'])
				else:
					lfe.memory(life, 'reject under_attack: attacker is trusted',
						attacker=event['attacker'],
						target=event['from']['id'])
		
		elif event['gist'] == 'bit':
			#React to attack... this needs to be a function in stats.py
			if event['target'] == life['id']:
				pass
			else:
				_trust_sender = judgement.can_trust(life, event['from']['id'])
				
				if brain.knows_alife_by_id(life, event['target']):
					_trust_target = judgement.can_trust(life, event['target'], low=5)
				else:
					brain.meet_alife(life, LIFE[event['target']])
					_trust_target = False
				
				if _trust_target and not _trust_sender and 1==4:
					lfe.memory(life, 'trusted target attacked by',
					           victim=event['target'],
					           target=event['from']['id'])

		elif event['gist'] == 'consume_item':
			lfe.memory(life, 'consume_item', target=event['from']['id'])
		
		elif event['gist'] == 'call':
			if judgement.can_trust(life, event['from']['id']):
				speech.start_dialog(life, event['from']['id'], 'call_accepted', remote=True)
		
		elif event['gist'] == 'order_attack':
			lfe.memory(life, 'ordered to attack',
			           target=event['target'])
		
		elif event['gist'] == 'threw_an_item':
			print 'CHECK THIS HERE' * 100
			pass
		
		elif event['gist'] == '_group_leader_state_change':
			life['think_rate'] = 0
		
		elif event['gist'] == 'dialog':
			if not 'player' in life and not event['dialog_id'] in life['dialogs']:
				life['dialogs'].append(event['dialog_id'])
		
		else:
			logging.warning('Unhandled ALife context: %s' % event['gist'])
		
		life['heard'].remove(event)
Example #8
0
def setup(life):
	#TODO: Add these two values to an array called PANIC_STATES
	#if not alife_seen:
	#	return False
	#if brain.retrieve_from_memory(life, 'tension_spike') >= 10:
	#	lfe.say(life, '@n panics!', action=True)
	_needs_help = stats.is_injured(life)
	
	_potential_talking_targets = []
	for ai in life['seen']:
		if not stats.can_talk_to(life, ai):
			continue
		
		_relationship_change = stats.wants_alignment_change(life, ai)
		
		#if 'player' in LIFE[ai]:
		#	print life['name'], LIFE[ai]['name'], judgement.get_tension_with(life, ai)>judgement.get_max_tension_with(life, ai), _relationship_change
		
		if stats.has_attacked_self(life, ai):
			stats.react_to_attack(life, ai)
		elif judgement.get_tension_with(life, ai)>judgement.get_max_tension_with(life, ai):
			stats.react_to_tension(life, ai)
		elif _needs_help:
			if stats.desires_help_from(life, ai):
				stats.ask_for_help(life, ai)
		elif _relationship_change:
			speech.change_alignment(life, ai, _relationship_change)
		else:
			if not stats.desires_conversation_with(life, ai):
				continue
	
			_potential_talking_targets.append(ai)
	
	if not _potential_talking_targets:
		if life['dialogs']:
			_dialog = life['dialogs'][0]
			dialog.process(life, _dialog)
		
		if not lfe.ticker(life, 'talk', 6):
			return False
	
	if lfe.get_all_inventory_items(life, matches=[{'type': 'radio'}]):
		for ai in life['know']:
			if ai in _potential_talking_targets:
				continue
			
			if not stats.can_talk_to(life, ai):
				continue
			
			_potential_talking_targets.append(ai)
	
	#TODO: Score these
	random.shuffle(_potential_talking_targets)
	
	for target in _potential_talking_targets:
		if life['dialogs']:
			break
		
		#if stats.desires_first_contact_with(life, target):
		#	memory.create_question(life, target, 'establish_relationship', ignore_if_said_in_last=-1)
		
		if memory.get_questions_for_target(life, target) and numbers.distance(life['pos'], LIFE[target]['pos'])<=25:
			_question = memory.ask_target_question(life, target)
			speech.start_dialog(life, target, _question['gist'], **_question['args'])
		elif memory.get_orders_for_target(life, target):
			speech.start_dialog(life, target, 'give_order')
		#elif stats.wants_group_member(life, target):
		#	memory.create_question(life, target, 'recruit', ignore_if_said_in_last=-1, group_id=life['group'])
	
	if life['dialogs']:
		_dialog = life['dialogs'][0]
		dialog.process(life, _dialog)
	
	if not judgement.is_safe(life) and lfe.ticker(life, 'call_for_help', 90, fire=True):
		_combat_targets = judgement.get_ready_combat_targets(life)
		
		if _combat_targets:
			if life['camp'] and camps.is_in_camp(life, lfe.get_current_camp(life)):
				_nearest_camp = camps.get_nearest_known_camp(life)
				raids.create_raid(_nearest_camp['id'], join=life['id'])
				raids.add_raiders(_nearest_camp['id'], _combat_targets)
				
				#TODO: Remove memory call
				speech.announce(life,
					'camp_raid',
					camp=_nearest_camp,
					raiders=_combat_targets)
			
			if life['group']:
				for target in _combat_targets:
					_last_seen_at = None
					_know = brain.knows_alife_by_id(life, target)
					
					if _know:
						_last_seen_at = _know['last_seen_at']
						
					groups.distribute(life, 'under_attack', attacker=target, last_seen_at=_last_seen_at)
		
		for target in judgement.get_ready_combat_targets(life):
			_last_seen_at = None
			_know = brain.knows_alife_by_id(life, target)
			
			if _know:
				_last_seen_at = _know['last_seen_at']

			speech.announce(life,
			                'attacked_by_hostile',
			                trusted=True,
			                target_id=target,
			                filter_if=lambda life_id: brain.knows_alife_by_id(life, life_id)['last_seen_time']<=30,
			                last_seen_at=_last_seen_at,
			                ignore_if_said_in_last=150)

	_visible_items = [life['know_items'][item] for item in life['know_items'] if not life['know_items'][item]['last_seen_time'] and not 'parent_id' in ITEMS[life['know_items'][item]['item']]]
	for ai in [life['know'][i] for i in life['know']]:
		if judgement.is_target_dangerous(life, ai['life']['id']):
			continue
		
		#if life['state'] == 'combat':
		#	break
		
		if ai['life']['state'] in ['hiding', 'hidden']:
			break
		
		if not stats.can_talk_to(life, ai['life']['id']):
			continue

		for item in _visible_items:
			#TODO: Check
			if brain.has_shared_item_with(life, ai['life'], item['item']):
				continue

			if not item['item'] in ITEMS:
				continue

			brain.share_item_with(life, ai['life'], item['item'])
			speech.communicate(life,
				'share_item_info',
				item=item['item'],
				matches=[ai['life']['id']])