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))
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.clear_actions(life) lfe.add_action(life, {'action': 'move','to': _target['last_seen_at'][:2]}, 200) return False
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
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
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
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.clear_actions(life) lfe.add_action(life, {"action": "move", "to": _pos_in_chunk}, 200) return True
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))
def position_to_attack(life, target): if lfe.find_action(life, [{'action': 'dijkstra_move', 'reason': 'positioning for attack'}]): if not lfe.ticker(life, 'attack_position', 4): return False _target_positions, _zones = combat.get_target_positions_and_zones(life, [target]) _nearest_target_score = zones.dijkstra_map(life['pos'], _target_positions, _zones, return_score=True) #TODO: Short or long-range weapon? #if _nearest_target_score >= sight.get_vision(life)/2: if not sight.can_see_position(life, brain.knows_alife_by_id(life, target)['last_seen_at'], block_check=True, strict=True) or sight.view_blocked_by_life(life, _target_positions[0], allow=[target]): print life['name'], 'changing position for combat...', life['name'], LIFE[target]['name'] _avoid_positions = [] for life_id in life['seen']: if life_id == target or life['id'] == life_id: continue if alife.judgement.can_trust(life, life_id): _avoid_positions.append(lfe.path_dest(LIFE[life_id])) else: _avoid_positions.append(brain.knows_alife_by_id(life, life_id)['last_seen_at']) _cover = _target_positions _zones = [] for pos in _cover: _zone = zones.get_zone_at_coords(pos) if not _zone in _zones: _zones.append(_zone) if not lfe.find_action(life, [{'action': 'dijkstra_move', 'orig_goals': _cover[:], 'avoid_positions': _avoid_positions}]): lfe.stop(life) lfe.add_action(life, {'action': 'dijkstra_move', 'rolldown': True, 'goals': _cover[:], 'orig_goals': _cover[:], 'avoid_positions': _avoid_positions, 'reason': 'positioning for attack'}, 999) return False else: return False elif life['path']: lfe.stop(life) return True
def travel_to_position(life, pos, stop_on_sight=False): if stop_on_sight and sight.can_see_position(life, pos): return True if not numbers.distance(life['pos'], pos): return True _dest = lfe.path_dest(life) if _dest and tuple(_dest[:2]) == tuple(pos[:2]): return False lfe.clear_actions(life) lfe.add_action(life,{'action': 'move','to': (pos[0],pos[1])},200) return False
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
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
def ranged_combat(life, targets): _target = get_closest_target(life, targets) if not _target: for target_id in targets: if brain.knows_alife_by_id(life, target_id)['escaped']: continue brain.knows_alife_by_id(life, target_id)['escaped'] = 1 logging.error('No target for ranged combat.') return False if not life['path'] or not numbers.distance(lfe.path_dest(life), _target['last_seen_at']) == 0: movement.position_to_attack(life, _target['life']['id']) if sight.can_see_position(life, _target['last_seen_at'], block_check=True, strict=True) and not sight.view_blocked_by_life(life, _target['last_seen_at'], allow=[_target['life']['id']]): if sight.can_see_position(life, _target['life']['pos']): if not len(lfe.find_action(life, matches=[{'action': 'shoot'}])): for i in range(weapons.get_rounds_to_fire(weapons.get_weapon_to_fire(life))): lfe.add_action(life,{'action': 'shoot', 'target': _target['last_seen_at'], 'target_id': _target['life']['id'], 'limb': 'chest'}, 5000, delay=int(round(life['recoil']-stats.get_recoil_recovery_rate(life)))) else: lfe.memory(life,'lost sight of %s' % (' '.join(_target['life']['name'])), target=_target['life']['id']) _target['escaped'] = 1 for send_to in judgement.get_trusted(life): speech.communicate(life, 'target_missing', target=_target['life']['id'], matches=[send_to]) else: print life['name'], 'waiting...' return False
def ranged_combat(life, targets): _target = brain.knows_alife_by_id(life, get_closest_target(life, targets)) #if not _target: # for target_id in targets: # if brain.knows_alife_by_id(life, target_id)['escaped']: # continue # # brain.knows_alife_by_id(life, target_id)['escaped'] = 1 # # logging.error('No target for ranged combat.') # # return False _engage_distance = get_engage_distance(life) _path_dest = lfe.path_dest(life) if not _path_dest: _path_dest = life['pos'][:] _target_distance = bad_numbers.distance(life['pos'], _target['last_seen_at']) #Get us near the target #if _target['last_seen_at']: movement.position_to_attack(life, _target['life']['id'], _engage_distance) if sight.can_see_position(life, _target['last_seen_at']): if _target_distance <= _engage_distance: if sight.can_see_position(life, _target['life']['pos']): if not sight.view_blocked_by_life(life, _target['life']['pos'], allow=[_target['life']['id']]): lfe.clear_actions(life) if not len(lfe.find_action(life, matches=[{'action': 'shoot'}])) and _target['time_visible']>2: for i in range(weapons.get_rounds_to_fire(weapons.get_weapon_to_fire(life))): lfe.add_action(life, {'action': 'shoot', 'target': _target['last_seen_at'], 'target_id': _target['life']['id'], 'limb': 'chest'}, 300, delay=int(round(life['recoil']-stats.get_recoil_recovery_rate(life)))) else: _friendly_positions, _friendly_zones = get_target_positions_and_zones(life, judgement.get_trusted(life)) _friendly_zones.append(zones.get_zone_at_coords(life['pos'])) _friendly_positions.append(life['pos'][:]) if not lfe.find_action(life, [{'action': 'dijkstra_move', 'orig_goals': [_target['life']['pos'][:]], 'avoid_positions': _friendly_positions}]): lfe.add_action(life, {'action': 'dijkstra_move', 'rolldown': True, 'zones': _friendly_zones, 'goals': [_target['life']['pos'][:]], 'orig_goals': [_target['life']['pos'][:]], 'avoid_positions': _friendly_positions, 'reason': 'combat_position'}, 100) else: lfe.memory(life,'lost sight of %s' % (' '.join(_target['life']['name'])), target=_target['life']['id']) _target['escaped'] = 1 for send_to in judgement.get_trusted(life): speech.communicate(life, 'target_missing', target=_target['life']['id'], matches=[send_to]) #else: #print life['name'] #_friendly_positions, _friendly_zones = get_target_positions_and_zones(life, judgement.get_trusted(life)) #_friendly_zones.append(zones.get_zone_at_coords(life['pos'])) #_friendly_positions.append(life['pos'][:]) #if not lfe.find_action(life, [{'action': 'dijkstra_move', 'orig_goals': [_target['life']['pos'][:]], 'avoid_positions': _friendly_positions}]): # lfe.add_action(life, {'action': 'dijkstra_move', # 'rolldown': True, # 'zones': _friendly_zones, # 'goals': [_target['life']['pos'][:]], # 'orig_goals': [_target['life']['pos'][:]], # 'avoid_positions': _friendly_positions, # 'reason': 'combat_position'}, # 100) # # print '2' else: return False
def ranged_combat(life, targets): _target = brain.knows_alife_by_id(life, get_closest_target(life, targets)) #if not _target: # for target_id in targets: # if brain.knows_alife_by_id(life, target_id)['escaped']: # continue # # brain.knows_alife_by_id(life, target_id)['escaped'] = 1 # # logging.error('No target for ranged combat.') # # return False _engage_distance = get_engage_distance(life) _path_dest = lfe.path_dest(life) if not _path_dest: _path_dest = life['pos'][:] _target_distance = bad_numbers.distance(life['pos'], _target['last_seen_at']) #Get us near the target #if _target['last_seen_at']: movement.position_to_attack(life, _target['life']['id'], _engage_distance) if sight.can_see_position(life, _target['last_seen_at']): if _target_distance <= _engage_distance: if sight.can_see_position(life, _target['life']['pos']): if not sight.view_blocked_by_life( life, _target['life']['pos'], allow=[_target['life']['id']]): lfe.clear_actions(life) if not len( lfe.find_action( life, matches=[{ 'action': 'shoot' }])) and _target['time_visible'] > 2: for i in range( weapons.get_rounds_to_fire( weapons.get_weapon_to_fire(life))): lfe.add_action( life, { 'action': 'shoot', 'target': _target['last_seen_at'], 'target_id': _target['life']['id'], 'limb': 'chest' }, 300, delay=int( round( life['recoil'] - stats.get_recoil_recovery_rate(life)))) else: _friendly_positions, _friendly_zones = get_target_positions_and_zones( life, judgement.get_trusted(life)) _friendly_zones.append( zones.get_zone_at_coords(life['pos'])) _friendly_positions.append(life['pos'][:]) if not lfe.find_action( life, [{ 'action': 'dijkstra_move', 'orig_goals': [_target['life']['pos'][:]], 'avoid_positions': _friendly_positions }]): lfe.add_action( life, { 'action': 'dijkstra_move', 'rolldown': True, 'zones': _friendly_zones, 'goals': [_target['life']['pos'][:]], 'orig_goals': [_target['life']['pos'][:]], 'avoid_positions': _friendly_positions, 'reason': 'combat_position' }, 100) else: lfe.memory(life, 'lost sight of %s' % (' '.join(_target['life']['name'])), target=_target['life']['id']) _target['escaped'] = 1 for send_to in judgement.get_trusted(life): speech.communicate(life, 'target_missing', target=_target['life']['id'], matches=[send_to]) #else: #print life['name'] #_friendly_positions, _friendly_zones = get_target_positions_and_zones(life, judgement.get_trusted(life)) #_friendly_zones.append(zones.get_zone_at_coords(life['pos'])) #_friendly_positions.append(life['pos'][:]) #if not lfe.find_action(life, [{'action': 'dijkstra_move', 'orig_goals': [_target['life']['pos'][:]], 'avoid_positions': _friendly_positions}]): # lfe.add_action(life, {'action': 'dijkstra_move', # 'rolldown': True, # 'zones': _friendly_zones, # 'goals': [_target['life']['pos'][:]], # 'orig_goals': [_target['life']['pos'][:]], # 'avoid_positions': _friendly_positions, # 'reason': 'combat_position'}, # 100) # # print '2' else: return False
def judge_chunk(life, chunk_id, visited=False, seen=False, checked=True, investigate=False): if lfe.ticker(life, "judge_tick", 30): return False chunk = maps.get_chunk(chunk_id) _score = 0 if not chunk_id in life["known_chunks"]: life["known_chunks"][chunk_id] = { "last_visited": -1, "last_seen": -1, "last_checked": -1, "discovered_at": WORLD_INFO["ticks"], "flags": {}, "life": [], } _camp = chunks.get_global_flag(chunk_id, "camp") if _camp and not _camp in life["known_camps"]: camps.discover_camp(life, _camp) _known_chunk = life["known_chunks"][chunk_id] if seen: _known_chunk["last_seen"] = WORLD_INFO["ticks"] if visited: _known_chunk["last_visited"] = WORLD_INFO["ticks"] _known_chunk["last_seen"] = WORLD_INFO["ticks"] if checked: _known_chunk["last_checked"] = WORLD_INFO["ticks"] _trusted = 0 for _target in life["know"].values(): if not _target["last_seen_at"]: continue _is_here = False _actually_here = False if chunks.position_is_in_chunk(_target["last_seen_at"], chunk_id) and not _target["life"]["path"]: _is_here = True elif ( not _target["last_seen_time"] and _target["life"]["path"] and chunks.position_is_in_chunk(lfe.path_dest(_target["life"]), chunk_id) ): _is_here = True if chunks.position_is_in_chunk(_target["life"]["pos"], chunk_id): _actually_here = True if _is_here: if not _target["life"]["id"] in _known_chunk["life"]: _known_chunk["life"].append(_target["life"]["id"]) if is_target_dangerous(life, _target["life"]["id"]): _score -= 10 elif life["group"] and groups.is_leader(life, life["group"], _target["life"]["id"]): _trusted += _target["trust"] else: if _target["life"]["id"] in _known_chunk["life"]: _known_chunk["life"].remove(_target["life"]["id"]) if investigate and not visited: chunks.flag(life, chunk_id, "investigate", True) elif visited and chunks.get_flag(life, chunk_id, "investigate"): chunks.unflag(life, chunk_id, "investigate") if chunks.get_flag(life, chunk_id, "investigate"): _score += 5 # for camp in life['known_camps']: # if not chunk_id in camps.get_camp(camp)['reference']: # continue # if not life['camp'] == camp['id']: # continue # if stats.desires_shelter(life): # _score += judge_camp(life, life['camp']) if lfe.execute_raw(life, "discover", "remember_shelter"): judge_shelter(life, chunk_id) # if stats.desires_interaction(life): # _score += _trusted if seen: pass # TODO: Still a good idea... maybe use for shelter? # for item in chunk['items']: # _item = brain.remember_known_item(life, item) # if _item: # _score += _item['score'] life["known_chunks"][chunk_id]["score"] = _score return _score
def position_to_attack(life, target, engage_distance): if lfe.find_action(life, [{ 'action': 'dijkstra_move', 'reason': 'positioning for attack' }]): if not lfe.ticker(life, 'attack_position', 4): return False _target_positions, _zones = combat.get_target_positions_and_zones( life, [target]) _can_see = alife.sight.can_see_position(life, _target_positions[0], get_path=True) _distance = bad_numbers.distance(life['pos'], _target_positions[0]) if _can_see and len(_can_see) < engage_distance * .85: if life['path']: lfe.stop(life) elif _distance < engage_distance * .9: _avoid_positions = set() _target_area = set() for life_id in alife.judgement.get_trusted(life, visible=False, only_recent=True): fov.fov(LIFE[life_id]['pos'], int(round(sight.get_vision(life) * .25)), callback=lambda pos: _avoid_positions.add(pos)) fov.fov(_target_positions[0], int(round(sight.get_vision(life) * .15)), callback=lambda pos: _target_area.add(pos)) _min_view_distance = int(round(sight.get_vision(life) * .25)) _max_view_distance = int(round(sight.get_vision(life) * .5)) _attack_positions = set( zones.dijkstra_map( life['pos'], _target_positions, _zones, rolldown=True, return_score_in_range=[_min_view_distance, _max_view_distance])) _attack_positions = _attack_positions - _target_area if not _attack_positions: return False if not lfe.find_action(life, [{ 'action': 'dijkstra_move', 'orig_goals': list(_attack_positions), 'avoid_positions': list(_avoid_positions) }]): lfe.stop(life) lfe.add_action( life, { 'action': 'dijkstra_move', 'rolldown': True, 'goals': [ list(p) for p in random.sample(_attack_positions, len(_attack_positions) / 2) ], 'orig_goals': list(_attack_positions), 'avoid_positions': list(_avoid_positions), 'reason': 'positioning for attack' }, 999) return False else: _can_see_positions = set() _target_area = set() _avoid_positions = set() fov.fov(life['pos'], int(round(sight.get_vision(life) * .75)), callback=lambda pos: _can_see_positions.add(pos)) fov.fov(_target_positions[0], int(round(sight.get_vision(life) * .75)), callback=lambda pos: _target_area.add(pos)) for life_id in alife.judgement.get_trusted(life, visible=False, only_recent=True): _path_dest = lfe.path_dest(LIFE[life_id]) if not _path_dest: continue if len(_path_dest) == 2: _path_dest = list(_path_dest[:]) _path_dest.append(LIFE[life_id]['pos'][2]) fov.fov(_path_dest, 5, callback=lambda pos: _avoid_positions.add(pos)) _avoid_positions = list(_avoid_positions) _sneak_positions = _can_see_positions - _target_area _move_positions = zones.dijkstra_map(LIFE[target]['pos'], list(_sneak_positions), _zones, rolldown=True) if not _move_positions: travel_to_position(life, list(_target_positions[0])) return False if not lfe.find_action(life, [{ 'action': 'dijkstra_move', 'orig_goals': _move_positions, 'avoid_positions': _avoid_positions }]): lfe.stop(life) lfe.add_action( life, { 'action': 'dijkstra_move', 'rolldown': True, 'goals': [list(p) for p in _move_positions], 'orig_goals': _move_positions, 'avoid_positions': _avoid_positions, 'reason': 'positioning for attack' }, 999) return False return True
def judge_chunk(life, chunk_id, visited=False, seen=False, checked=True, investigate=False): if lfe.ticker(life, 'judge_tick', 30): return False chunk = maps.get_chunk(chunk_id) _score = 0 if not chunk_id in life['known_chunks']: life['known_chunks'][chunk_id] = {'last_visited': -1, 'last_seen': -1, 'last_checked': -1, 'discovered_at': WORLD_INFO['ticks'], 'flags': {}, 'life': []} _camp = chunks.get_global_flag(chunk_id, 'camp') if _camp and not _camp in life['known_camps']: camps.discover_camp(life, _camp) _known_chunk = life['known_chunks'][chunk_id] if seen: _known_chunk['last_seen'] = WORLD_INFO['ticks'] if visited: _known_chunk['last_visited'] = WORLD_INFO['ticks'] _known_chunk['last_seen'] = WORLD_INFO['ticks'] if checked: _known_chunk['last_checked'] = WORLD_INFO['ticks'] _trusted = 0 for _target in life['know'].values(): if not _target['last_seen_at']: continue _is_here = False _actually_here = False if chunks.position_is_in_chunk(_target['last_seen_at'], chunk_id) and not _target['life']['path']: _is_here = True elif not _target['last_seen_time'] and _target['life']['path'] and chunks.position_is_in_chunk(lfe.path_dest(_target['life']), chunk_id): _is_here = True if chunks.position_is_in_chunk(_target['life']['pos'], chunk_id): _actually_here = True if _is_here: if not _target['life']['id'] in _known_chunk['life']: _known_chunk['life'].append(_target['life']['id']) if is_target_dangerous(life, _target['life']['id']): _score -= 10 elif life['group'] and groups.is_leader(life, life['group'], _target['life']['id']): _trusted += _target['trust'] else: if _target['life']['id'] in _known_chunk['life']: _known_chunk['life'].remove(_target['life']['id']) if investigate and not visited: chunks.flag(life, chunk_id, 'investigate', True) elif visited and chunks.get_flag(life, chunk_id, 'investigate'): chunks.unflag(life, chunk_id, 'investigate') if chunks.get_flag(life, chunk_id, 'investigate'): _score += 5 #for camp in life['known_camps']: # if not chunk_id in camps.get_camp(camp)['reference']: # continue #if not life['camp'] == camp['id']: # continue #if stats.desires_shelter(life): # _score += judge_camp(life, life['camp']) if lfe.execute_raw(life, 'discover', 'remember_shelter'): judge_shelter(life, chunk_id) #if stats.desires_interaction(life): # _score += _trusted if seen: pass #TODO: Still a good idea... maybe use for shelter? #for item in chunk['items']: # _item = brain.remember_known_item(life, item) # if _item: # _score += _item['score'] life['known_chunks'][chunk_id]['score'] = _score return _score
def position_to_attack(life, target, engage_distance): if lfe.find_action(life, [{"action": "dijkstra_move", "reason": "positioning for attack"}]): if not lfe.ticker(life, "attack_position", 4): return False _target_positions, _zones = combat.get_target_positions_and_zones(life, [target]) _can_see = alife.sight.can_see_position(life, _target_positions[0], get_path=True) _distance = numbers.distance(life["pos"], _target_positions[0]) if _can_see and len(_can_see) < engage_distance * 0.85: if life["path"]: lfe.stop(life) elif _distance < engage_distance * 0.9: _avoid_positions = set() _target_area = set() for life_id in alife.judgement.get_trusted(life, visible=False, only_recent=True): fov.fov( LIFE[life_id]["pos"], int(round(sight.get_vision(life) * 0.25)), callback=lambda pos: _avoid_positions.add(pos), ) fov.fov( _target_positions[0], int(round(sight.get_vision(life) * 0.15)), callback=lambda pos: _target_area.add(pos) ) _min_view_distance = int(round(sight.get_vision(life) * 0.25)) _max_view_distance = int(round(sight.get_vision(life) * 0.5)) _attack_positions = set( zones.dijkstra_map( life["pos"], _target_positions, _zones, rolldown=True, return_score_in_range=[_min_view_distance, _max_view_distance], ) ) _attack_positions = _attack_positions - _target_area if not _attack_positions: return False if not lfe.find_action( life, [ { "action": "dijkstra_move", "orig_goals": list(_attack_positions), "avoid_positions": list(_avoid_positions), } ], ): lfe.stop(life) lfe.add_action( life, { "action": "dijkstra_move", "rolldown": True, "goals": [list(p) for p in random.sample(_attack_positions, len(_attack_positions) / 2)], "orig_goals": list(_attack_positions), "avoid_positions": list(_avoid_positions), "reason": "positioning for attack", }, 999, ) return False else: _can_see_positions = set() _target_area = set() _avoid_positions = set() fov.fov( life["pos"], int(round(sight.get_vision(life) * 0.75)), callback=lambda pos: _can_see_positions.add(pos) ) fov.fov( _target_positions[0], int(round(sight.get_vision(life) * 0.75)), callback=lambda pos: _target_area.add(pos) ) for life_id in alife.judgement.get_trusted(life, visible=False, only_recent=True): _path_dest = lfe.path_dest(LIFE[life_id]) if not _path_dest: continue if len(_path_dest) == 2: _path_dest = list(_path_dest[:]) _path_dest.append(LIFE[life_id]["pos"][2]) fov.fov(_path_dest, 5, callback=lambda pos: _avoid_positions.add(pos)) _avoid_positions = list(_avoid_positions) _sneak_positions = _can_see_positions - _target_area _move_positions = zones.dijkstra_map(LIFE[target]["pos"], list(_sneak_positions), _zones, rolldown=True) if not _move_positions: travel_to_position(life, list(_target_positions[0])) return False if not lfe.find_action( life, [{"action": "dijkstra_move", "orig_goals": _move_positions, "avoid_positions": _avoid_positions}] ): lfe.stop(life) lfe.add_action( life, { "action": "dijkstra_move", "rolldown": True, "goals": [list(p) for p in _move_positions], "orig_goals": _move_positions, "avoid_positions": _avoid_positions, "reason": "positioning for attack", }, 999, ) return False return True