def scan_surroundings(life, initial=False, _chunks=[], ignore_chunks=[], judge=True, get_chunks=False, visible_check=True): if _chunks: _chunk_keys = set(_chunks) else: _chunk_keys = set() if SETTINGS['smp']: fov.fov(life['pos'], get_vision(life), get_chunks=True, life_id=life['id']) else: life['fov'] = fov.fov(life['pos'], get_vision(life), callback=lambda pos: _chunk_keys.add( chunks.get_chunk_key_at(pos))) return list(_chunk_keys)
def manage_combat(life, group_id): if has_flag(life, group_id, 'confident'): _was_confident = get_flag(life, group_id, 'confident') if _was_confident == stats.is_confident(life) and not lfe.ticker(life, 'decision_wait', 16): return False flag(life, group_id, 'confident', stats.is_confident(life)) _existing_friendlies = get_flag(life, group_id, 'friendlies') _existing_targets = get_flag(life, group_id, 'targets') _last_focal_point = get_flag(life, group_id, 'last_focal_point') if not _existing_friendlies: _existing_friendlies = {} if not _existing_targets: _existing_targets = {} for life_id in get_group(life, group_id)['members']: if not life_id in _existing_friendlies: _existing_friendlies[life_id] = {'updated': -900} flag(life, group_id, 'friendlies', _existing_friendlies) _checked_targets = [] for target_id in judgement.get_threats(life): if target_id in _existing_targets: _existing_targets[target_id]['time'] = 0 else: _existing_targets[target_id] = {'time': 0, 'pos': brain.knows_alife_by_id(life, target_id)['last_seen_at'][:]} _checked_targets.append(target_id) _enemy_focal_pos = None for target_id in _existing_targets: if not _enemy_focal_pos: _enemy_focal_pos = _existing_targets[target_id]['pos'][:] else: _enemy_focal_pos = bad_numbers.lerp_velocity(_enemy_focal_pos, _existing_targets[target_id]['pos'], 0.5) if target_id in _checked_targets: continue _existing_targets[target_id]['time'] += 1 if _existing_targets[target_id]['time']>100: del _existing_targets[target_id] continue _hostile_chunks = get_flag(life, group_id, 'hostile_chunks') _previous_visible_chunks = brain.get_flag(life, 'group_combat_vis_chunks') if _previous_visible_chunks and _previous_visible_chunks['from_pos'] == life['pos']: _visible_chunks = _previous_visible_chunks['visible_chunks'] else: _visible_chunks = chunks.get_visible_chunks_from(life['pos'], life['vision_max']*.75) brain.flag(life, 'group_combat_vis_chunks', value={'from_pos': life['pos'][:], 'visible_chunks': _visible_chunks}) if _enemy_focal_pos: lfe.clear_ticker(life, 'group_command_reset') if not _last_focal_point or bad_numbers.distance(_enemy_focal_pos, _last_focal_point)>30: _hostile_chunks = chunks.get_visible_chunks_from((int(round(_enemy_focal_pos[0])), int(round(_enemy_focal_pos[1])), 2), life['vision_max']*1.5) flag(life, group_id, 'hostile_chunks', _hostile_chunks) flag(life, group_id, 'visible_chunks', _visible_chunks) flag(life, group_id, 'last_focal_point', _enemy_focal_pos) else: _ticker = lfe.ticker(life, 'group_command_reset', 48) if get_stage(life, group_id) == STAGE_ATTACKING: if _ticker: set_stage(life, group_id, STAGE_FORMING) flag(life, group_id, 'friendlies', None) flag(life, group_id, 'strategy', None) else: manage_strategy(life, group_id) return False if not get_stage(life, group_id) == STAGE_ATTACKING: speech.announce_combat_to_group(life, group_id) set_stage(life, group_id, STAGE_ATTACKING) if not lfe.ticker(life, 'group_command_rate', 3): return False _orig_visible_chunks = _visible_chunks[:] #TODO: Check distance to threat for hostile_chunk_key in _hostile_chunks: if hostile_chunk_key in _visible_chunks: _visible_chunks.remove(hostile_chunk_key) #TODO: Additional stages: PLANNING, EXECUTING if _visible_chunks and stats.is_confident(life): for target_id in order_spread_out(life, group_id, _visible_chunks, filter_by=lambda life_id: WORLD_INFO['ticks']-_existing_friendlies[life_id]['updated']>100): _existing_friendlies[target_id]['updated'] = WORLD_INFO['ticks'] else: _distant_chunk = {'distance': -1, 'chunk_key': None} _unchecked_members = get_group(life, group_id)['members'][:] for chunk_key in _orig_visible_chunks: _distance = bad_numbers.distance((int(round(_enemy_focal_pos[0])), int(round(_enemy_focal_pos[1]))), chunks.get_chunk(chunk_key)['pos']) _distance *= bad_numbers.clip(bad_numbers.distance(life['pos'], _enemy_focal_pos), 1, 35)/35.0 if chunk_key in _visible_chunks: _distance *= 2 for member_id in _unchecked_members: if life['id'] == member_id: continue _target = brain.knows_alife_by_id(life, member_id) if _target['last_seen_time'] <= 25 and chunks.get_chunk_key_at(_target['last_seen_at']) == chunk_key: _distance *= (2.5*(1-(bad_numbers.clip(_target['last_seen_time'], 0, 25)/25.0))) if _distance>_distant_chunk['distance']: _distant_chunk['distance'] = _distance _distant_chunk['chunk_key'] = chunk_key if _distant_chunk['chunk_key']: for target_id in order_move_to(life, group_id, _distant_chunk['chunk_key'], filter_by=lambda life_id: WORLD_INFO['ticks']-_existing_friendlies[life_id]['updated']>100): _existing_friendlies[target_id]['updated'] = WORLD_INFO['ticks'] return False
def hide(life, targets): _target_positions = [] _avoid_positions = [] _zones = [zones.get_zone_at_coords(life['pos'])] if lfe.find_action(life, [{ 'action': 'dijkstra_move', 'reason': 'escaping' }]): if not lfe.ticker(life, 'escaping', 6): return False #What can the targets see? for target_id in targets: _target = brain.knows_alife_by_id(life, target_id) _zone = zones.get_zone_at_coords(_target['last_seen_at']) if not _zone in _zones: _zones.append(_zone) fov.fov(_target['last_seen_at'], sight.get_vision(_target['life']), callback=lambda pos: _avoid_positions.append(pos)) #What can we see? _can_see_positions = [] fov.fov(life['pos'], sight.get_vision(life), callback=lambda pos: _can_see_positions.append(pos)) #If there are no visible targets, we could be running away from a position we were attacked from _cover_exposed_at = brain.get_flag(life, 'cover_exposed_at') if _cover_exposed_at: _avoid_exposed_cover_positions = set() for pos in _cover_exposed_at[:]: if tuple(pos[:2]) in _can_see_positions: _cover_exposed_at.remove(pos) continue fov.fov( pos, int(round(sight.get_vision(life) * .25)), callback=lambda pos: _avoid_exposed_cover_positions.add(pos)) for pos in _avoid_exposed_cover_positions: if not pos in _avoid_positions: _avoid_positions.append(pos) else: print 'Something went wrong' return False #Overlay the two, finding positions we can see but the target can't for pos in _can_see_positions[:]: if pos in _avoid_positions: _can_see_positions.remove(pos) continue #Get rid of positions that are too close for target_id in targets: _target = brain.knows_alife_by_id(life, target_id) if bad_numbers.distance(_target['last_seen_at'], pos) < 4: _can_see_positions.remove(pos) break #Now scan for cover to prevent hiding in the open for pos in _can_see_positions[:]: if chunks.get_chunk(chunks.get_chunk_key_at(pos))['max_z'] == 2: _can_see_positions.remove(pos) if not _can_see_positions: if life['pos'] in _cover_exposed_at: _cover_exposed_at.remove(life['pos']) return False if lfe.find_action(life, [{ 'action': 'dijkstra_move', 'goals': _can_see_positions[:] }]): return True lfe.stop(life) lfe.add_action( life, { 'action': 'dijkstra_move', 'rolldown': True, 'zones': _zones, 'goals': _can_see_positions[:], 'reason': 'escaping' }, 200)
def scan_surroundings(life, initial=False, _chunks=[], ignore_chunks=[], judge=True, get_chunks=False, visible_check=True): if _chunks: _chunk_keys = set(_chunks) else: _chunk_keys = set() if SETTINGS['smp']: fov.fov(life['pos'], get_vision(life), get_chunks=True, life_id=life['id']) else: life['fov'] = fov.fov(life['pos'], get_vision(life), callback=lambda pos: _chunk_keys.add(chunks.get_chunk_key_at(pos))) return list(_chunk_keys)
def hide(life, targets): _target_positions = [] _avoid_positions = [] _zones = [zones.get_zone_at_coords(life["pos"])] if lfe.find_action(life, [{"action": "dijkstra_move", "reason": "escaping"}]): if not lfe.ticker(life, "escaping", 6): return False # What can the targets see? for target_id in targets: _target = brain.knows_alife_by_id(life, target_id) _zone = zones.get_zone_at_coords(_target["last_seen_at"]) if not _zone in _zones: _zones.append(_zone) fov.fov( _target["last_seen_at"], sight.get_vision(_target["life"]), callback=lambda pos: _avoid_positions.append(pos), ) # What can we see? _can_see_positions = [] fov.fov(life["pos"], sight.get_vision(life), callback=lambda pos: _can_see_positions.append(pos)) # If there are no visible targets, we could be running away from a position we were attacked from _cover_exposed_at = brain.get_flag(life, "cover_exposed_at") if _cover_exposed_at: _avoid_exposed_cover_positions = set() for pos in _cover_exposed_at[:]: if tuple(pos[:2]) in _can_see_positions: _cover_exposed_at.remove(pos) continue fov.fov( pos, int(round(sight.get_vision(life) * 0.25)), callback=lambda pos: _avoid_exposed_cover_positions.add(pos), ) for pos in _avoid_exposed_cover_positions: if not pos in _avoid_positions: _avoid_positions.append(pos) else: print "Something went wrong" return False # Overlay the two, finding positions we can see but the target can't for pos in _can_see_positions[:]: if pos in _avoid_positions: _can_see_positions.remove(pos) continue # Get rid of positions that are too close for target_id in targets: _target = brain.knows_alife_by_id(life, target_id) if numbers.distance(_target["last_seen_at"], pos) < 4: _can_see_positions.remove(pos) break # Now scan for cover to prevent hiding in the open for pos in _can_see_positions[:]: if chunks.get_chunk(chunks.get_chunk_key_at(pos))["max_z"] == 2: _can_see_positions.remove(pos) if not _can_see_positions: if life["pos"] in _cover_exposed_at: _cover_exposed_at.remove(life["pos"]) return False if lfe.find_action(life, [{"action": "dijkstra_move", "goals": _can_see_positions[:]}]): return True lfe.stop(life) lfe.add_action( life, { "action": "dijkstra_move", "rolldown": True, "zones": _zones, "goals": _can_see_positions[:], "reason": "escaping", }, 200, )
def manage_combat(life, group_id): if has_flag(life, group_id, 'confident'): _was_confident = get_flag(life, group_id, 'confident') if _was_confident == stats.is_confident(life) and not lfe.ticker( life, 'decision_wait', 16): return False flag(life, group_id, 'confident', stats.is_confident(life)) _existing_friendlies = get_flag(life, group_id, 'friendlies') _existing_targets = get_flag(life, group_id, 'targets') _last_focal_point = get_flag(life, group_id, 'last_focal_point') if not _existing_friendlies: _existing_friendlies = {} if not _existing_targets: _existing_targets = {} for life_id in get_group(life, group_id)['members']: if not life_id in _existing_friendlies: _existing_friendlies[life_id] = {'updated': -900} flag(life, group_id, 'friendlies', _existing_friendlies) _checked_targets = [] for target_id in judgement.get_threats(life): if target_id in _existing_targets: _existing_targets[target_id]['time'] = 0 else: _existing_targets[target_id] = { 'time': 0, 'pos': brain.knows_alife_by_id(life, target_id)['last_seen_at'][:] } _checked_targets.append(target_id) _enemy_focal_pos = None for target_id in _existing_targets: if not _enemy_focal_pos: _enemy_focal_pos = _existing_targets[target_id]['pos'][:] else: _enemy_focal_pos = bad_numbers.lerp_velocity( _enemy_focal_pos, _existing_targets[target_id]['pos'], 0.5) if target_id in _checked_targets: continue _existing_targets[target_id]['time'] += 1 if _existing_targets[target_id]['time'] > 100: del _existing_targets[target_id] continue _hostile_chunks = get_flag(life, group_id, 'hostile_chunks') _previous_visible_chunks = brain.get_flag(life, 'group_combat_vis_chunks') if _previous_visible_chunks and _previous_visible_chunks[ 'from_pos'] == life['pos']: _visible_chunks = _previous_visible_chunks['visible_chunks'] else: _visible_chunks = chunks.get_visible_chunks_from( life['pos'], life['vision_max'] * .75) brain.flag(life, 'group_combat_vis_chunks', value={ 'from_pos': life['pos'][:], 'visible_chunks': _visible_chunks }) if _enemy_focal_pos: lfe.clear_ticker(life, 'group_command_reset') if not _last_focal_point or bad_numbers.distance( _enemy_focal_pos, _last_focal_point) > 30: _hostile_chunks = chunks.get_visible_chunks_from( (int(round(_enemy_focal_pos[0])), int(round(_enemy_focal_pos[1])), 2), life['vision_max'] * 1.5) flag(life, group_id, 'hostile_chunks', _hostile_chunks) flag(life, group_id, 'visible_chunks', _visible_chunks) flag(life, group_id, 'last_focal_point', _enemy_focal_pos) else: _ticker = lfe.ticker(life, 'group_command_reset', 48) if get_stage(life, group_id) == STAGE_ATTACKING: if _ticker: set_stage(life, group_id, STAGE_FORMING) flag(life, group_id, 'friendlies', None) flag(life, group_id, 'strategy', None) else: manage_strategy(life, group_id) return False if not get_stage(life, group_id) == STAGE_ATTACKING: speech.announce_combat_to_group(life, group_id) set_stage(life, group_id, STAGE_ATTACKING) if not lfe.ticker(life, 'group_command_rate', 3): return False _orig_visible_chunks = _visible_chunks[:] #TODO: Check distance to threat for hostile_chunk_key in _hostile_chunks: if hostile_chunk_key in _visible_chunks: _visible_chunks.remove(hostile_chunk_key) #TODO: Additional stages: PLANNING, EXECUTING if _visible_chunks and stats.is_confident(life): for target_id in order_spread_out( life, group_id, _visible_chunks, filter_by=lambda life_id: WORLD_INFO[ 'ticks'] - _existing_friendlies[life_id]['updated'] > 100): _existing_friendlies[target_id]['updated'] = WORLD_INFO['ticks'] else: _distant_chunk = {'distance': -1, 'chunk_key': None} _unchecked_members = get_group(life, group_id)['members'][:] for chunk_key in _orig_visible_chunks: _distance = bad_numbers.distance( (int(round( _enemy_focal_pos[0])), int(round(_enemy_focal_pos[1]))), chunks.get_chunk(chunk_key)['pos']) _distance *= bad_numbers.clip( bad_numbers.distance(life['pos'], _enemy_focal_pos), 1, 35) / 35.0 if chunk_key in _visible_chunks: _distance *= 2 for member_id in _unchecked_members: if life['id'] == member_id: continue _target = brain.knows_alife_by_id(life, member_id) if _target['last_seen_time'] <= 25 and chunks.get_chunk_key_at( _target['last_seen_at']) == chunk_key: _distance *= (2.5 * (1 - (bad_numbers.clip( _target['last_seen_time'], 0, 25) / 25.0))) if _distance > _distant_chunk['distance']: _distant_chunk['distance'] = _distance _distant_chunk['chunk_key'] = chunk_key if _distant_chunk['chunk_key']: for target_id in order_move_to( life, group_id, _distant_chunk['chunk_key'], filter_by=lambda life_id: WORLD_INFO['ticks'] - _existing_friendlies[life_id]['updated'] > 100): _existing_friendlies[target_id]['updated'] = WORLD_INFO[ 'ticks'] return False
def escape(life, targets): _target_positions = [] _avoid_positions = [] _zones = [zones.get_zone_at_coords(life['pos'])] if lfe.find_action(life, [{'action': 'dijkstra_move', 'reason': 'escaping'}]): if not lfe.ticker(life, 'escaping', 4): return False #What can the targets see? for target_id in targets: _target = brain.knows_alife_by_id(life, target_id) _zone = zones.get_zone_at_coords(_target['last_seen_at']) if not _zone in _zones: _zones.append(_zone) fov.fov(_target['last_seen_at'], sight.get_vision(_target['life']), callback=lambda pos: _avoid_positions.append(pos)) #What can we see? _can_see_positions = [] fov.fov(life['pos'], sight.get_vision(life), callback=lambda pos: _can_see_positions.append(pos)) #If there are no visible targets, we could be running away from a position we were attacked from _cover_exposed_at = brain.get_flag(life, 'cover_exposed_at') if _cover_exposed_at: _avoid_exposed_cover_positions = set() for pos in _cover_exposed_at[:]: if tuple(pos[:2]) in _can_see_positions: print 'ok!!!'*20 _cover_exposed_at.remove(pos) continue fov.fov(pos, int(round(sight.get_vision(life)*.25)), callback=lambda pos: _avoid_exposed_cover_positions.add(pos)) for pos in _avoid_exposed_cover_positions: if not pos in _avoid_positions: _avoid_positions.append(pos) #Overlay the two, finding positions we can see but the target can't for pos in _can_see_positions[:]: if pos in _avoid_positions: _can_see_positions.remove(pos) continue #Get rid of positions that are too close for target_id in targets: _target = brain.knows_alife_by_id(life, target_id) #TODO: Unhardcode 15 if numbers.distance(_target['last_seen_at'], pos)<10: _can_see_positions.remove(pos) break #Now scan for cover to prevent hiding in the open for pos in _can_see_positions[:]: if chunks.get_chunk(chunks.get_chunk_key_at(pos))['max_z'] == 2: _can_see_positions.remove(pos) #for target_id in targets: #_target = brain.knows_alife_by_id(life, target_id) #_target_positions.append(_target['last_seen_at'][:]) #_zone = zones.get_zone_at_coords(_target['last_seen_at']) #if not _zone in _zones: # _zones.append(_zone) #for chunk_key in chunks.get_visible_chunks_from(_target['last_seen_at'], sight.get_vision(_target['life'])): # if chunk_key in _visible_target_chunks: # continue # _visible_target_chunks.append(chunk_key) #for friendly_id in life['seen']: # _chunk_key = lfe.get_current_chunk_id(LIFE[friendly_id]) # # if not _chunk_key in _visible_target_chunks: # _visible_target_chunks.append(_chunk_key) #if not _target_positions: # return False #TODO: #combat: For lower limit in return_score_in_range, use range of weapon #_cover = zones.dijkstra_map(life['pos'], # _avoid_positions, # _zones, # avoid_chunks=[], # return_score_in_range=[1, 5]) # sight.get_vision(life) #_cover = [(c[0], c[1], life['pos'][2]) for c in _cover] #if not _cover: # return False #_zones = [zones.get_zone_at_coords(life['pos'])] #for _pos in _cover: # _zone = zones.get_zone_at_coords(_pos) # if not _zone in _zones: # _zones.append(_zone) if not _can_see_positions: return False if lfe.find_action(life, [{'action': 'dijkstra_move', 'goals': _can_see_positions[:]}]): return True lfe.stop(life) lfe.add_action(life, {'action': 'dijkstra_move', 'rolldown': True, 'zones': _zones, 'goals': _can_see_positions[:], 'reason': 'escaping'}, 999)