def set_fov(self, mask, max_radius = 10): points = set() def visit(x, y): if (x, y) in mask: points.add((x,y)) return True return points.add((x,y)) fov(self.x, self.y, max_radius, visit) self.fov = Mask(points)
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 do_fov(cells): width, height = len(cells[0]), len(cells) def visit(x, y): if not (0 <= x < width and 0 <= y < height): raise IndexError('grid position (%d, %d) out of range' % (x, y)) if cells[y][x] == ' ': cells[y][x] = '.' return cells[y][x] == '#' for y, row in enumerate(cells): for x, cell in enumerate(row): if cell.isdigit() or cell == '@': fov(x, y, 1000 if cell == '@' else int(cell), visit)
def update_light(self): self.tag += 1 self.lights = 0 self.scans = 0 def visit(y, x): self.lights += 1 if (y, x) not in self.grid: return True cell = self.grid[y, x] cell.tag = self.tag cell.color = self.scans return self.grid[y, x].char == '#' def scan(*args): self.scans += 1 fov(self.y, self.x, self.radius, visit, scan if self.debug else None)
def __init__(self, coords=(0, 0), glyph=symbol.BAD_GLYPH, speed=72, max_HP=2, currentLevel=None, name="Unnamed", attack=1, defense=0, tags=None, char_level=1, passableTerrain=level.PASSABLE_TERRAIN): fixedobj.FixedObject.__init__(self, coords, glyph, currentLevel) self.name = name self.speed = speed self.passableTerrain = passableTerrain self.max_HP = max_HP self.cur_HP = self.max_HP self.attack = attack self.defense = defense self.char_level = char_level self.tags = tags if tags is not None else [] self.fov = fov.fov() self.conditions = {} # a dict whose keys are condition names,
def convertLog(self): if self.camera_chosen == 0: QMessageBox.information(self, 'Warning', 'Select a Camera!') elif self.log_type_chosen == 0: QMessageBox.information(self, 'Warning', 'Select Log Type!') elif self.log_chosen == 0: QMessageBox.information(self, 'Warning', 'Select Log File!') else: fov_values = fov.fov(self.flen, self.sensorw, self.sensorh) fov_horizontal = fov_values[0] fov_vertical = fov_values[1] amsl = self.amslInput.text() # if (self.log == 'DJI GO'): # converter.converter( # self.log_file, self.save_location, # fov_h, fov_w, amsl) # QMessageBox.information(self, 'Message', 'Log converted!') if (self.log == 'Litchi'): self.converted = litchiconverter.converter( self.log_file, self.save_location, fov_horizontal, fov_vertical, amsl) if (self.converted): QMessageBox.information(self, 'Message', 'Log converted!') else: QMessageBox.information(self, 'Message', 'No video in log file.')
def _render_los(map, pos, size, cython=False, life=None): #LOS times: #Raycast: 0.0453310012817 #Recursive Shadowcasting: 0.0119090080261 (worst case), 0.000200033187866 (best case) _start_time = time.time() _fov = fov.fov(pos, size) print time.time() - _start_time return _fov
def _render_los(map, pos, size, cython=False, life=None): #LOS times: #Raycast: 0.0453310012817 #Recursive Shadowcasting: 0.0119090080261 (worst case), 0.000200033187866 (best case) _start_time = time.time() _fov = fov.fov(pos, size) print time.time()-_start_time return _fov
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) * .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 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 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 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 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)