def melee_combat(life, targets): _target = get_closest_target(life, targets) if not _target: logging.error('No target for melee combat.') return False if sight.can_see_position(life, _target['last_seen_at'], block_check=True, strict=True): _can_see = sight.can_see_position(life, _target['life']['pos'], get_path=True) if _can_see: if len(_can_see)>1: movement.find_target(life, _target['life']['id'], distance=1, follow=True) else: melee.fight(life, _target['life']['id']) 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: return False
def is_target_lost(life, target_id): _know = brain.knows_alife_by_id(life, target_id) if sight.can_see_position(life, _know['last_seen_at']): if sight.can_see_position(life, _know['life']['pos']): return False else: return True return False
def is_target_lost(life, target_id): _know = brain.knows_alife_by_id(life, target_id) if sight.can_see_position(life, _know["last_seen_at"]): if sight.can_see_position(life, _know["life"]["pos"]): return False else: return True 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 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 get_matching_remembered_items(life, matches, no_owner=False, active=True, only_visible=False): _matched_items = [] if 'type' in matches and matches['type'] in life['known_items_type_cache']: _remembered_items = [life['know_items'][i] for i in life['known_items_type_cache'][matches['type']]] else: _remembered_items = life['know_items'].values() for item in _remembered_items: _item = ITEMS[item['item']] if get_item_flag(life, _item, 'ignore'): continue if active and 'offloaded' in item: continue if no_owner and item['last_owned_by']: continue if only_visible and not sight.can_see_position(life, _item['pos']): continue if _item['lock']: continue if 'parent' in _item and _item['parent']: continue if logic.matches(_item, matches): _matched_items.append(item['item']) return _matched_items
def conditions(life, alife_seen, alife_not_seen, targets_seen, targets_not_seen, source_map): RETURN_VALUE = STATE_UNCHANGED _mode = None if lfe.execute_raw(life, 'state', 'combat'): _mode = 'combat' if not _mode and lfe.execute_raw(life, 'state', 'hunt'): _mode = 'hunt' if not _mode: return False if not lfe.execute_raw(life, 'combat', 'ranged') and not lfe.execute_raw(life, 'combat', 'melee'): return False if not life['state'] == STATE: life['state_flags'] = {} stats.battle_cry(life) if gfx.position_is_in_frame(life['pos']) and SETTINGS['controlling']: _can_see = sight.can_see_position(life, LIFE[SETTINGS['controlling']]['pos']) if _can_see: _knows = brain.knows_alife_by_id(life, SETTINGS['controlling']) if _knows and judgement.can_trust(life, SETTINGS['controlling']): if lfe.ticker(life, 'enter_combat_message', 3, fire=True): logic.show_event('%s readies up.' % ' '.join(life['name']), life=life) RETURN_VALUE = STATE_CHANGE brain.flag(life, 'combat_mode', value=_mode) return RETURN_VALUE
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
def _spread(noise): for alife in LIFE.values(): if alife['dead']: continue _can_see = False if sight.can_see_position(alife, noise['pos']): _can_see = True _dist = bad_numbers.distance(noise['pos'], alife['pos']) if _dist>noise['volume']: continue update_targets_around_noise(alife, noise) _direction_to = bad_numbers.direction_to(alife['pos'], noise['pos']) _direction_string = language.get_real_direction(_direction_to) #TODO: Check walls between positions #TODO: Add memory if not _can_see or not noise['skip_on_visual']: if _dist >=noise['volume']/2: if 'player' in alife: gfx.message(random.choice(FAR_TEXT).replace('@t', noise['text'][1]).replace('@d', _direction_string), style='sound') else: if 'player' in alife: gfx.message(random.choice(FAR_TEXT).replace('@t', noise['text'][0]).replace('@d', _direction_string), style='sound')
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 get_visible_walkable_areas(life, chunk_id): chunk = maps.get_chunk(chunk_id) _walkable = [] for pos in chunk['ground']: if sight.can_see_position(life, pos): _walkable.append(pos) return _walkable
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 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
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 react_to_tension(life, life_id): if brain.knows_alife_by_id(life, life_id)['alignment'] in ['hostile']: return False if life['group'] and not groups.is_leader( life, life['group'], life['id']) and groups.get_leader( life, life['group']): if sight.can_see_target(life, groups.get_leader( life, life['group'])) and sight.can_see_target( LIFE[life_id], groups.get_leader(life, life['group'])): return False _disarm = brain.get_alife_flag(life, life_id, 'disarm') if _disarm: #For now... if not sight.can_see_position(life, LIFE[life_id]['pos']): groups.announce(life, life['group'], 'attacked_by_hostile', filter_if=lambda life_id: brain.knows_alife_by_id( life, life_id)['last_seen_time'] <= 30, target_id=life_id) return False for item_uid in lfe.get_all_visible_items(LIFE[life_id]): if ITEMS[item_uid]['type'] == 'gun': break else: brain.unflag_alife(life, life_id, 'disarm') speech.start_dialog(life, life_id, 'clear_drop_weapon') return False _time_elapsed = WORLD_INFO['ticks'] - _disarm if _time_elapsed > 135 and not speech.has_sent(life, life_id, 'threaten'): speech.start_dialog(life, life_id, 'threaten') speech.send(life, life_id, 'threaten') elif _time_elapsed > 185: speech.start_dialog(life, life_id, 'establish_hostile') elif not speech.has_sent(life, life_id, 'confront'): speech.start_dialog(life, life_id, 'confront') speech.send(life, life_id, 'confront')
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 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
def conditions(life, alife_seen, alife_not_seen, targets_seen, targets_not_seen, source_map): RETURN_VALUE = STATE_UNCHANGED _mode = None if lfe.execute_raw(life, 'state', 'combat'): _mode = 'combat' if not _mode and lfe.execute_raw(life, 'state', 'hunt'): _mode = 'hunt' if not _mode: return False if not lfe.execute_raw(life, 'combat', 'ranged') and not lfe.execute_raw( life, 'combat', 'melee'): return False if not life['state'] == STATE: life['state_flags'] = {} stats.battle_cry(life) if gfx.position_is_in_frame(life['pos']) and SETTINGS['controlling']: _can_see = sight.can_see_position( life, LIFE[SETTINGS['controlling']]['pos']) if _can_see: _knows = brain.knows_alife_by_id(life, SETTINGS['controlling']) if _knows and judgement.can_trust(life, SETTINGS['controlling']): if lfe.ticker(life, 'enter_combat_message', 3, fire=True): logic.show_event('%s readies up.' % ' '.join(life['name']), life=life) RETURN_VALUE = STATE_CHANGE brain.flag(life, 'combat_mode', value=_mode) return RETURN_VALUE
def get_matching_remembered_items(life, matches, no_owner=False, active=True, only_visible=False): _matched_items = [] if 'type' in matches and matches['type'] in life['known_items_type_cache']: _remembered_items = [ life['know_items'][i] for i in life['known_items_type_cache'][matches['type']] ] else: _remembered_items = life['know_items'].values() for item in _remembered_items: _item = ITEMS[item['item']] if get_item_flag(life, _item, 'ignore'): continue if active and 'offloaded' in item: continue if no_owner and item['last_owned_by']: continue if only_visible and not sight.can_see_position(life, _item['pos']): continue if _item['lock']: continue if 'parent' in _item and _item['parent']: continue if logic.matches(_item, matches): _matched_items.append(item['item']) return _matched_items
def react_to_tension(life, life_id): if brain.knows_alife_by_id(life, life_id)['alignment'] in ['hostile']: return False if life['group'] and not groups.is_leader(life, life['group'], life['id']) and groups.get_leader(life, life['group']): if sight.can_see_target(life, groups.get_leader(life, life['group'])) and sight.can_see_target(LIFE[life_id], groups.get_leader(life, life['group'])): return False _disarm = brain.get_alife_flag(life, life_id, 'disarm') if _disarm: #For now... if not sight.can_see_position(life, LIFE[life_id]['pos']): groups.announce(life, life['group'], 'attacked_by_hostile', filter_if=lambda life_id: brain.knows_alife_by_id(life, life_id)['last_seen_time']<=30, target_id=life_id) return False for item_uid in lfe.get_all_visible_items(LIFE[life_id]): if ITEMS[item_uid]['type'] == 'gun': break else: brain.unflag_alife(life, life_id, 'disarm') speech.start_dialog(life, life_id, 'clear_drop_weapon') return False _time_elapsed = WORLD_INFO['ticks']-_disarm if _time_elapsed>135 and not speech.has_sent(life, life_id, 'threaten'): speech.start_dialog(life, life_id, 'threaten') speech.send(life, life_id, 'threaten') elif _time_elapsed>185: speech.start_dialog(life, life_id, 'establish_hostile') elif not speech.has_sent(life, life_id, 'confront'): speech.start_dialog(life, life_id, 'confront') speech.send(life, life_id, 'confront')
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 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
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 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
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