def get_distance_to_target(life, target_id): target = brain.knows_alife_by_id(life, target_id) _goals = [target['last_seen_at']] _zones = [zones.get_zone_at_coords(target['last_seen_at'])] _zone = zones.get_zone_at_coords(life['pos']) if not _zone in _zones: _zones.append(_zone) return zones.dijkstra_map(life['pos'], _goals, _zones, return_score=True)
def get_distance_to_target(life, target_id): target = brain.knows_alife_by_id(life, target_id) _goals = [target["last_seen_at"]] _zones = [zones.get_zone_at_coords(target["last_seen_at"])] _zone = zones.get_zone_at_coords(life["pos"]) if not _zone in _zones: _zones.append(_zone) return zones.dijkstra_map(life["pos"], _goals, _zones, return_score=True)
def get_closest_target(life, targets): if targets: _target_positions, _zones = get_target_positions_and_zones(life, targets) else: #TODO: Dude, what? movement.find_target(life, targets, call=False) return False _targets_too_far = [] _closest_target = {'target_id': None, 'score': 9999} for t in [brain.knows_alife_by_id(life, t_id) for t_id in targets]: _distance = numbers.distance(life['pos'], t['last_seen_at']) #NOTE: Hardcoding this for optimization reasons. if _distance>=100: targets.remove(t['life']['id']) _targets_too_far.append(t['life']['id']) if _distance < _closest_target['score']: _closest_target['score'] = _distance _closest_target['target_id'] = t['life']['id'] if not _targets_too_far: _path_to_nearest = zones.dijkstra_map(life['pos'], _target_positions, _zones) if not _path_to_nearest: _path_to_nearest = [life['pos'][:]] if not _path_to_nearest: logging.error('%s lost known/visible target.' % ' '.join(life['name'])) return False _target_pos = list(_path_to_nearest[len(_path_to_nearest)-1]) #else: # _target_pos = life['pos'][:] # _target_positions.append(_target_pos) target = None if _target_pos in _target_positions: for _target in [brain.knows_alife_by_id(life, t) for t in targets]: if _target_pos == _target['last_seen_at']: target = _target break else: print 'THIS IS MUCH QUICKER!!!' * 10 target = brain.knows_alife_by_id(life, _closest_target['target_id']) return target
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 explode(item): if not item['type'] == 'explosive': return False logging.debug('The %s (item %s) explodes!' % (item['name'], item['uid'])) #TODO: Don't breathe this! item['pos'] = get_pos(item['uid']) if item['damage']['force']: effects.create_explosion(item['pos'], item['damage']['force']) if SETTINGS['controlling'] and alife.sight.can_see_position(LIFE[SETTINGS['controlling']], item['pos']): gfx.message('%s explodes!' % get_name(item)) logic.show_event('%s explodes!' % get_name(item), item=item, delay=0) #elif bad_numbers.distance( #TODO: Dirty hack for life_id in LIFE: _limbs = LIFE[life_id]['body'].keys() if not _limbs: continue _force = bad_numbers.clip((item['damage']['force']*2)-bad_numbers.distance(LIFE[life_id]['pos'], item['pos']), 0, 100) if not _force: continue _known_item = alife.brain.remembers_item(LIFE[life_id], item) _direction = bad_numbers.direction_to(item['pos'], LIFE[life_id]['pos']) #TODO: Intelligent(?) limb groups? _distance = bad_numbers.distance(LIFE[life_id]['pos'], item['pos'])/2 for i in range(_force-_distance): _limb = random.choice(_limbs) for _attached_limb in life.get_all_attached_limbs(LIFE[life_id], _limb): if _attached_limb in _limbs: _limbs.remove(_attached_limb) #_limb = random.choice(LIFE[life_id]['body'].keys()) if _known_item and _known_item['last_seen_time'] < 100 and _known_item['last_owned_by']: life.memory(LIFE[life_id], 'blown_up_by', target=_known_item['last_owned_by']) #for _limb in _limbs: life.add_wound(LIFE[life_id], _limb, force_velocity=bad_numbers.velocity(_direction, _force*2)) if not _limbs: break life.push(LIFE[life_id], _direction, _force) if 'player' in LIFE[life_id]: life.say(LIFE[life_id], '@n are thrown by the explosion!', action=True) else: life.say(LIFE[life_id], '@n is thrown by the explosion!', action=True) if 'fire' in item['damage']: _circle = drawing.draw_circle(item['pos'], item['radius']) _zone = zones.get_zone_at_coords(item['pos']) if _zone: for pos in zones.dijkstra_map(item['pos'], [item['pos']], [_zone], return_score_in_range=[0, item['damage']['fire']]): if not pos in _circle: continue if not maps.position_is_in_map(pos): continue for life_id in LIFE_MAP[pos[0]][pos[1]]: for _visible_item in [get_item_from_uid(i) for i in life.get_all_visible_items(LIFE[life_id])]: if not 'CAN_BURN' in _visible_item['flags']: continue burn(_visible_item, item['damage']['fire']) if not random.randint(0, 4): effects.create_fire((pos[0], pos[1], item['pos'][2]), intensity=item['damage']['fire']/2) _dist = bad_numbers.distance(item['pos'], pos)/2 if not random.randint(0, _dist) or not _dist: effects.create_ash(pos) if gfx.position_is_in_frame(pos): _render_pos = gfx.get_render_position(pos) gfx.refresh_view_position(_render_pos[0], _render_pos[1], 'map') #if item['uid'] in ITEMS and ITEMS[item['uid']]['owner'] and item['uid'] in LIFE[ITEMS[item['uid']]['owner']]['inventory']: delete_item(item)
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 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
def explode(item): if not item['type'] == 'explosive': return False logging.debug('The %s (item %s) explodes!' % (item['name'], item['uid'])) #TODO: Don't breathe this! item['pos'] = get_pos(item['uid']) if item['damage']['force']: effects.create_explosion(item['pos'], item['damage']['force']) if SETTINGS['controlling'] and alife.sight.can_see_position( LIFE[SETTINGS['controlling']], item['pos']): gfx.message('%s explodes!' % get_name(item)) logic.show_event('%s explodes!' % get_name(item), item=item, delay=0) #elif bad_numbers.distance( #TODO: Dirty hack for life_id in LIFE: _limbs = LIFE[life_id]['body'].keys() if not _limbs: continue _force = bad_numbers.clip( (item['damage']['force'] * 2) - bad_numbers.distance(LIFE[life_id]['pos'], item['pos']), 0, 100) if not _force: continue _known_item = alife.brain.remembers_item(LIFE[life_id], item) _direction = bad_numbers.direction_to(item['pos'], LIFE[life_id]['pos']) #TODO: Intelligent(?) limb groups? _distance = bad_numbers.distance(LIFE[life_id]['pos'], item['pos']) / 2 for i in range(_force - _distance): _limb = random.choice(_limbs) for _attached_limb in life.get_all_attached_limbs( LIFE[life_id], _limb): if _attached_limb in _limbs: _limbs.remove(_attached_limb) #_limb = random.choice(LIFE[life_id]['body'].keys()) if _known_item and _known_item[ 'last_seen_time'] < 100 and _known_item['last_owned_by']: life.memory(LIFE[life_id], 'blown_up_by', target=_known_item['last_owned_by']) #for _limb in _limbs: life.add_wound(LIFE[life_id], _limb, force_velocity=bad_numbers.velocity( _direction, _force * 2)) if not _limbs: break life.push(LIFE[life_id], _direction, _force) if 'player' in LIFE[life_id]: life.say(LIFE[life_id], '@n are thrown by the explosion!', action=True) else: life.say(LIFE[life_id], '@n is thrown by the explosion!', action=True) if 'fire' in item['damage']: _circle = drawing.draw_circle(item['pos'], item['radius']) _zone = zones.get_zone_at_coords(item['pos']) if _zone: for pos in zones.dijkstra_map( item['pos'], [item['pos']], [_zone], return_score_in_range=[0, item['damage']['fire']]): if not pos in _circle: continue if not maps.position_is_in_map(pos): continue for life_id in LIFE_MAP[pos[0]][pos[1]]: for _visible_item in [ get_item_from_uid(i) for i in life.get_all_visible_items(LIFE[life_id]) ]: if not 'CAN_BURN' in _visible_item['flags']: continue burn(_visible_item, item['damage']['fire']) if not random.randint(0, 4): effects.create_fire((pos[0], pos[1], item['pos'][2]), intensity=item['damage']['fire'] / 2) _dist = bad_numbers.distance(item['pos'], pos) / 2 if not random.randint(0, _dist) or not _dist: effects.create_ash(pos) if gfx.position_is_in_frame(pos): _render_pos = gfx.get_render_position(pos) gfx.refresh_view_position(_render_pos[0], _render_pos[1], 'map') #if item['uid'] in ITEMS and ITEMS[item['uid']]['owner'] and item['uid'] in LIFE[ITEMS[item['uid']]['owner']]['inventory']: delete_item(item)