示例#1
0
def explore_known_chunks(life):
    #Our first order of business is to figure out exactly what we're looking for.
    #There's a big difference between exploring the map looking for a purpose and
    #exploring the map with a purpose. Both will use similar routines but I wager
    #it'll actually be a lot harder to do it without there being some kind of goal
    #to at least start us in the right direction.

    #This function will kick in only if the ALife is idling, so looting is handled
    #automatically.

    #Note: Determining whether this fuction should run at all needs to be done inside
    #the module itself.
    _chunk_key = brain.retrieve_from_memory(life, 'explore_chunk')
    _chunk = maps.get_chunk(_chunk_key)

    if life['path'] and chunks.position_is_in_chunk(lfe.path_dest(life),
                                                    _chunk_key):
        return True

    if chunks.is_in_chunk(life,
                          '%s,%s' % (_chunk['pos'][0], _chunk['pos'][1])):
        life['known_chunks'][_chunk_key]['last_visited'] = WORLD_INFO['ticks']
        return False

    if not _chunk['ground']:
        return False

    _pos_in_chunk = random.choice(_chunk['ground'])

    lfe.walk_to(life, _pos_in_chunk)

    return True
示例#2
0
def find_target(life, target, distance=5, follow=False, call=True):
    _target = brain.knows_alife_by_id(life, target)
    _dist = bad_numbers.distance(life['pos'], _target['last_seen_at'])

    _can_see = sight.can_see_target(life, target)
    if _can_see and _dist <= distance:
        if follow:
            return True

        lfe.stop(life)

        return True

    if _target['escaped'] == 1:
        search_for_target(life, target)
        return False

    if not _can_see and sight.can_see_position(
            life, _target['last_seen_at']) and _dist < distance:
        if call:
            if not _target['escaped']:
                memory.create_question(life, target, 'GET_LOCATION')

            speech.communicate(life, 'call', matches=[target])

        _target['escaped'] = 1

        return False

    if not lfe.path_dest(life) == tuple(_target['last_seen_at'][:2]):
        lfe.walk_to(life, _target['last_seen_at'])

    return False
示例#3
0
def explore_known_chunks(life):
	#Our first order of business is to figure out exactly what we're looking for.
	#There's a big difference between exploring the map looking for a purpose and
	#exploring the map with a purpose. Both will use similar routines but I wager
	#it'll actually be a lot harder to do it without there being some kind of goal
	#to at least start us in the right direction.
	
	#This function will kick in only if the ALife is idling, so looting is handled
	#automatically.
	
	#Note: Determining whether this fuction should run at all needs to be done inside
	#the module itself.
	_chunk_key = brain.retrieve_from_memory(life, 'explore_chunk')
	_chunk = maps.get_chunk(_chunk_key)
	
	if life['path'] and chunks.position_is_in_chunk(lfe.path_dest(life), _chunk_key):
		return True
	
	if chunks.is_in_chunk(life, '%s,%s' % (_chunk['pos'][0], _chunk['pos'][1])):
		life['known_chunks'][_chunk_key]['last_visited'] = WORLD_INFO['ticks']
		return False
	
	if not _chunk['ground']:
		return False
	
	_pos_in_chunk = random.choice(_chunk['ground'])
	
	lfe.walk_to(life, _pos_in_chunk)
	
	return True
示例#4
0
def find_target(life, target, distance=5, follow=False, call=True):
    _target = brain.knows_alife_by_id(life, target)
    _dist = numbers.distance(life["pos"], _target["last_seen_at"])

    _can_see = sight.can_see_target(life, target)
    if _can_see and _dist <= distance:
        if follow:
            return True

        lfe.stop(life)

        return True

    if _target["escaped"] == 1:
        search_for_target(life, target)
        return False

    if not _can_see and sight.can_see_position(life, _target["last_seen_at"]) and _dist < distance:
        if call:
            if not _target["escaped"]:
                memory.create_question(life, target, "GET_LOCATION")

            speech.communicate(life, "call", matches=[target])

        _target["escaped"] = 1

        return False

    if not lfe.path_dest(life) == tuple(_target["last_seen_at"][:2]):
        lfe.walk_to(life, _target["last_seen_at"])

    return False
示例#5
0
def collect_nearby_wanted_items(life,
                                only_visible=True,
                                matches={'type': 'gun'}):
    _highest = {'item': None, 'score': -100000}
    _nearby = sight.find_known_items(life,
                                     matches=matches,
                                     only_visible=only_visible)

    for item in _nearby:
        _item = brain.get_remembered_item(life, item)
        _score = _item['score']
        _score -= bad_numbers.distance(life['pos'], ITEMS[item]['pos'])

        if not _highest['item'] or _score > _highest['score']:
            _highest['score'] = _score
            _highest['item'] = ITEMS[item]

    if not _highest['item']:
        return True

    _empty_hand = lfe.get_open_hands(life)

    if not _empty_hand:
        print 'No open hands, managing....'
        for item_uid in lfe.get_held_items(life):
            _container = lfe.can_put_item_in_storage(life, item_uid)

            lfe.add_action(life, {
                'action': 'storeitem',
                'item': item_uid,
                'container': _container
            },
                           200,
                           delay=lfe.get_item_access_time(life, item_uid))
        return False

    if life['pos'] == _highest['item']['pos']:
        lfe.clear_actions(life)

        for action in lfe.find_action(life,
                                      matches=[{
                                          'action': 'pickupholditem'
                                      }]):
            #print 'I was picking up something else...',_highest['item']['name']
            return False

        lfe.add_action(life, {
            'action': 'pickupholditem',
            'item': _highest['item']['uid'],
            'hand': random.choice(_empty_hand)
        },
                       200,
                       delay=lfe.get_item_access_time(life,
                                                      _highest['item']['uid']))
        lfe.lock_item(life, _highest['item']['uid'])
    else:
        lfe.walk_to(life, _highest['item']['pos'])

    return False
示例#6
0
def tick(life):
	if not 'shelter' in life['state_flags']:
		life['state_flags']['shelter'] = judgement.get_best_shelter(life)
	
	if not life['state_flags']['shelter'] in life['known_chunks']:
		judgement.judge_chunk(life, life['state_flags']['shelter'])
	
	if not chunks.get_flag(life, life['state_flags']['shelter'], 'shelter_cover'):
		return False
	
	if not list(life['pos'][:2]) in chunks.get_flag(life, life['state_flags']['shelter'], 'shelter_cover'):
		if not lfe.path_dest(life) or (not chunks.position_is_in_chunk(lfe.path_dest(life), life['state_flags']['shelter'])):
			_cover = chunks.get_flag(life, life['state_flags']['shelter'], 'shelter_cover')
			lfe.walk_to(life, random.choice(_cover))
示例#7
0
def search_for_target(life, target_id):
	#TODO: Variable size instead of hardcoded
	_know = brain.knows_alife_by_id(life, target_id)
	_size = 30
	
	if brain.alife_has_flag(life, target_id, 'search_map'):
		_search_map = brain.get_alife_flag(life, target_id, 'search_map')
	else:
		_search_map = maps.create_search_map(life, _know['last_seen_at'], _size)
		brain.flag_alife(life, target_id, 'search_map', value=_search_map)
		
		lfe.stop(life)
		lfe.walk_to(life, _know['last_seen_at'][:2])
	
	if life['path'] or lfe.find_action(life, matches=[{'action': 'move'}]):
		return False
	
	_lowest = {'score': -1, 'pos': None}
	_x_top_left = numbers.clip(_know['last_seen_at'][0]-(_size/2), 0, MAP_SIZE[0])
	_y_top_left = numbers.clip(_know['last_seen_at'][1]-(_size/2), 0, MAP_SIZE[1])
	
	for x in range(0, _size):
		_x = _x_top_left+x
		
		if _x >= MAP_SIZE[0]-1:
			continue
		
		for y in range(0, _size):
			_y = _y_top_left+y
			
			if _y >= MAP_SIZE[1]-1:
				continue
			
			if not _search_map[y, x]:
				continue
			
			if sight.can_see_position(life, (_x, _y)):
				_search_map[y, x] = 0
			
			if _search_map[y, x]>0 and (not _lowest['pos'] or _search_map[y, x] <= _lowest['score']):
				_lowest['score'] = _search_map[y, x]
				_lowest['pos'] = (_x, _y, x, y)

	if _lowest['pos']:
		x, y, _x, _y = _lowest['pos']
		
		if travel_to_position(life, (x, y, _know['last_seen_at'][2]), stop_on_sight=True):
			_search_map[_y, _x] = 0
	else:
		_know['escaped'] = 2
示例#8
0
def collect_nearby_wanted_items(life, only_visible=True, matches={"type": "gun"}):
    _highest = {"item": None, "score": -100000}
    _nearby = sight.find_known_items(life, matches=matches, only_visible=only_visible)

    for item in _nearby:
        _item = brain.get_remembered_item(life, item)
        _score = _item["score"]
        _score -= numbers.distance(life["pos"], ITEMS[item]["pos"])

        if not _highest["item"] or _score > _highest["score"]:
            _highest["score"] = _score
            _highest["item"] = ITEMS[item]

    if not _highest["item"]:
        return True

    _empty_hand = lfe.get_open_hands(life)

    if not _empty_hand:
        print "No open hands, managing...."
        for item_uid in lfe.get_held_items(life):
            _container = lfe.can_put_item_in_storage(life, item_uid)

            lfe.add_action(
                life,
                {"action": "storeitem", "item": item_uid, "container": _container},
                200,
                delay=lfe.get_item_access_time(life, item_uid),
            )
        return False

    if life["pos"] == _highest["item"]["pos"]:
        lfe.clear_actions(life)

        for action in lfe.find_action(life, matches=[{"action": "pickupholditem"}]):
            # print 'I was picking up something else...',_highest['item']['name']
            return False

        lfe.add_action(
            life,
            {"action": "pickupholditem", "item": _highest["item"]["uid"], "hand": random.choice(_empty_hand)},
            200,
            delay=lfe.get_item_access_time(life, _highest["item"]["uid"]),
        )
        lfe.lock_item(life, _highest["item"]["uid"])
    else:
        lfe.walk_to(life, _highest["item"]["pos"])

    return False
示例#9
0
def travel_to_position(life, pos, stop_on_sight=False, force=False):
    if not numbers.distance(life["pos"], pos):
        return True

    if stop_on_sight and sight.can_see_position(life, pos, get_path=True, ignore_z=True):
        lfe.stop(life)

        return True

    _dest = lfe.path_dest(life)
    if not force and _dest and tuple(_dest[:2]) == tuple(pos[:2]):
        return False

    lfe.walk_to(life, pos[:3])

    return False
示例#10
0
def collect_nearby_wanted_items(life, only_visible=True, matches={'type': 'gun'}):
	_highest = {'item': None,'score': -100000}
	_nearby = sight.find_known_items(life, matches=matches, only_visible=only_visible)
	
	for item in _nearby:
		_item = brain.get_remembered_item(life, item)
		_score = _item['score']
		_score -= bad_numbers.distance(life['pos'], ITEMS[item]['pos'])
		
		if not _highest['item'] or _score > _highest['score']:
			_highest['score'] = _score
			_highest['item'] = ITEMS[item]
	
	if not _highest['item']:
		return True
	
	_empty_hand = lfe.get_open_hands(life)
	
	if not _empty_hand:
		print 'No open hands, managing....'
		for item_uid in lfe.get_held_items(life):
			_container = lfe.can_put_item_in_storage(life, item_uid)
			
			lfe.add_action(life, {'action': 'storeitem',
				'item': item_uid,
			     'container': _container},
				200,
				delay=lfe.get_item_access_time(life, item_uid))
		return False
	
	if life['pos'] == _highest['item']['pos']:
		lfe.clear_actions(life)
		
		for action in lfe.find_action(life, matches=[{'action': 'pickupholditem'}]):
			#print 'I was picking up something else...',_highest['item']['name']
			return False
		
		lfe.add_action(life,{'action': 'pickupholditem',
			'item': _highest['item']['uid'],
			'hand': random.choice(_empty_hand)},
			200,
			delay=lfe.get_item_access_time(life, _highest['item']['uid']))
		lfe.lock_item(life, _highest['item']['uid'])
	else:
		lfe.walk_to(life, _highest['item']['pos'])
	
	return False
示例#11
0
def travel_to_position(life, pos, stop_on_sight=False, force=False):
    if not bad_numbers.distance(life['pos'], pos):
        return True

    if stop_on_sight and sight.can_see_position(
            life, pos, get_path=True, ignore_z=True):
        lfe.stop(life)

        return True

    _dest = lfe.path_dest(life)
    if not force and _dest and tuple(_dest[:2]) == tuple(pos[:2]):
        return False

    lfe.walk_to(life, pos[:3])

    return False
示例#12
0
def tick(life):
    if not 'shelter' in life['state_flags']:
        life['state_flags']['shelter'] = judgement.get_best_shelter(life)

    if not life['state_flags']['shelter'] in life['known_chunks']:
        judgement.judge_chunk(life, life['state_flags']['shelter'])

    if not chunks.get_flag(life, life['state_flags']['shelter'],
                           'shelter_cover'):
        return False

    if not list(life['pos'][:2]) in chunks.get_flag(
            life, life['state_flags']['shelter'], 'shelter_cover'):
        if not lfe.path_dest(life) or (not chunks.position_is_in_chunk(
                lfe.path_dest(life), life['state_flags']['shelter'])):
            _cover = chunks.get_flag(life, life['state_flags']['shelter'],
                                     'shelter_cover')
            lfe.walk_to(life, random.choice(_cover))
示例#13
0
def search_for_target(life, target_id):
    #TODO: Variable size instead of hardcoded
    _know = brain.knows_alife_by_id(life, target_id)
    _size = 30
    _timer = brain.get_flag(life, 'search_time')
    _chunk_path = alife.brain.get_flag(life, 'chunk_path')

    if _chunk_path:
        travel_to_position(life, _chunk_path['end'], force=True)

        return False

    if _timer > 0:
        brain.flag(life, 'search_time', _timer - 1)

        return False

    if brain.alife_has_flag(life, target_id, 'search_map'):
        _search_map = brain.get_alife_flag(life, target_id, 'search_map')
    else:
        _search_map = maps.create_search_map(life, _know['last_seen_at'],
                                             _size)
        brain.flag_alife(life, target_id, 'search_map', value=_search_map)

        lfe.walk_to(life, _know['last_seen_at'])
        brain.flag(life, 'search_time', 12)

        return False

    _lowest = {'score': -1, 'pos': None}
    _x_top_left = bad_numbers.clip(_know['last_seen_at'][0] - (_size / 2), 0,
                                   MAP_SIZE[0])
    _y_top_left = bad_numbers.clip(_know['last_seen_at'][1] - (_size / 2), 0,
                                   MAP_SIZE[1])

    for x in range(0, _size):
        _x = _x_top_left + x

        if _x >= MAP_SIZE[0] - 1:
            continue

        for y in range(0, _size):
            _y = _y_top_left + y

            if _y >= MAP_SIZE[1] - 1:
                continue

            if not _search_map[y, x]:
                continue

            if sight.can_see_position(life, (_x, _y, _know['last_seen_at'][2]),
                                      get_path=True) or not lfe.can_walk_to(
                                          life,
                                          (_x, _y, _know['last_seen_at'][2])):
                _search_map[y, x] = 0

            if _search_map[y, x] > 0 and (
                    not _lowest['pos']
                    or _search_map[y, x] < _lowest['score']):
                _lowest['score'] = _search_map[y, x]
                _lowest['pos'] = (_x, _y, x, y)

    if _lowest['pos']:
        x, y, _x, _y = _lowest['pos']

        if travel_to_position(life, (x, y, _know['last_seen_at'][2]),
                              stop_on_sight=False):
            _search_map[_y, _x] = 0

        brain.flag(
            life, 'search_time',
            bad_numbers.clip(
                bad_numbers.distance(life['pos'], (x, y)) * .75, 5, 16))
    else:
        _know['escaped'] = 2
示例#14
0
def search_for_target(life, target_id):
    # TODO: Variable size instead of hardcoded
    _know = brain.knows_alife_by_id(life, target_id)
    _size = 30
    _timer = brain.get_flag(life, "search_time")
    _chunk_path = alife.brain.get_flag(life, "chunk_path")

    if _chunk_path:
        travel_to_position(life, _chunk_path["end"], force=True)

        return False

    if _timer > 0:
        brain.flag(life, "search_time", _timer - 1)

        return False

    if brain.alife_has_flag(life, target_id, "search_map"):
        _search_map = brain.get_alife_flag(life, target_id, "search_map")
    else:
        _search_map = maps.create_search_map(life, _know["last_seen_at"], _size)
        brain.flag_alife(life, target_id, "search_map", value=_search_map)

        lfe.walk_to(life, _know["last_seen_at"])
        brain.flag(life, "search_time", 12)

        return False

    _lowest = {"score": -1, "pos": None}
    _x_top_left = numbers.clip(_know["last_seen_at"][0] - (_size / 2), 0, MAP_SIZE[0])
    _y_top_left = numbers.clip(_know["last_seen_at"][1] - (_size / 2), 0, MAP_SIZE[1])

    for x in range(0, _size):
        _x = _x_top_left + x

        if _x >= MAP_SIZE[0] - 1:
            continue

        for y in range(0, _size):
            _y = _y_top_left + y

            if _y >= MAP_SIZE[1] - 1:
                continue

            if not _search_map[y, x]:
                continue

            if sight.can_see_position(life, (_x, _y, _know["last_seen_at"][2]), get_path=True) or not lfe.can_walk_to(
                life, (_x, _y, _know["last_seen_at"][2])
            ):
                _search_map[y, x] = 0

            if _search_map[y, x] > 0 and (not _lowest["pos"] or _search_map[y, x] < _lowest["score"]):
                _lowest["score"] = _search_map[y, x]
                _lowest["pos"] = (_x, _y, x, y)

    if _lowest["pos"]:
        x, y, _x, _y = _lowest["pos"]

        if travel_to_position(life, (x, y, _know["last_seen_at"][2]), stop_on_sight=False):
            _search_map[_y, _x] = 0

        brain.flag(life, "search_time", numbers.clip(numbers.distance(life["pos"], (x, y)) * 0.75, 5, 16))
    else:
        _know["escaped"] = 2
示例#15
0
def search_for_target(life, target_id):
	#TODO: Variable size instead of hardcoded
	_know = brain.knows_alife_by_id(life, target_id)
	_size = 30
	_timer = brain.get_flag(life, 'search_time')
	_chunk_path = alife.brain.get_flag(life, 'chunk_path')
	
	if _chunk_path:
		travel_to_position(life, _chunk_path['end'], force=True)
		
		return False
	
	if _timer>0:
		brain.flag(life, 'search_time', _timer-1)
		
		return False
	
	if brain.alife_has_flag(life, target_id, 'search_map'):
		_search_map = brain.get_alife_flag(life, target_id, 'search_map')
	else:
		_search_map = maps.create_search_map(life, _know['last_seen_at'], _size)
		brain.flag_alife(life, target_id, 'search_map', value=_search_map)
		
		lfe.walk_to(life, _know['last_seen_at'])
		brain.flag(life, 'search_time', 12)
		
		return False
	
	_lowest = {'score': -1, 'pos': None}
	_x_top_left = bad_numbers.clip(_know['last_seen_at'][0]-(_size/2), 0, MAP_SIZE[0])
	_y_top_left = bad_numbers.clip(_know['last_seen_at'][1]-(_size/2), 0, MAP_SIZE[1])
	
	for x in range(0, _size):
		_x = _x_top_left+x
		
		if _x >= MAP_SIZE[0]-1:
			continue
		
		for y in range(0, _size):
			_y = _y_top_left+y
			
			if _y >= MAP_SIZE[1]-1:
				continue
			
			if not _search_map[y, x]:
				continue
			
			if sight.can_see_position(life, (_x, _y, _know['last_seen_at'][2]), get_path=True) or not lfe.can_walk_to(life, (_x, _y, _know['last_seen_at'][2])):
				_search_map[y, x] = 0
			
			if _search_map[y, x]>0 and (not _lowest['pos'] or _search_map[y, x] < _lowest['score']):
				_lowest['score'] = _search_map[y, x]
				_lowest['pos'] = (_x, _y, x, y)

	if _lowest['pos']:
		x, y, _x, _y = _lowest['pos']
		
		if travel_to_position(life, (x, y, _know['last_seen_at'][2]), stop_on_sight=False):
			_search_map[_y, _x] = 0
		
		brain.flag(life, 'search_time', bad_numbers.clip(bad_numbers.distance(life['pos'], (x, y))*.75, 5, 16))
	else:
		_know['escaped'] = 2