def create_region_spawns(): #Runners #spawns.generate_group('runner_scout', amount=random alife.factions.generate() return False #Step 3: Rookie village _spawn_chunks = random.choice([t['rooms'] for t in WORLD_INFO['refs']['villages'][0]]) _rookie_village_members = spawns.generate_group('loner', amount=random.randint(7, 9), group_motive='survival', spawn_chunks=_spawn_chunks) for member in _rookie_village_members: alife.planner.remove_goal(member, 'discover') _dirt_road_start_chunk = maps.get_chunk(WORLD_INFO['refs']['dirt_road'][0]) _dirt_road_end_chunk = maps.get_chunk(WORLD_INFO['refs']['dirt_road'][len(WORLD_INFO['refs']['dirt_road'])-1]) for x in range(0, MAP_SIZE[0], WORLD_INFO['chunk_size']): for y in range(_dirt_road_end_chunk['pos'][1], _dirt_road_start_chunk['pos'][1], WORLD_INFO['chunk_size']): if x < _dirt_road_end_chunk['pos'][0]-(10*WORLD_INFO['chunk_size']) or x > _dirt_road_end_chunk['pos'][0]+(10*WORLD_INFO['chunk_size']): continue if random.randint(0, 125): continue _spawn_chunk = '%s,%s' % (x, y) for _alife in spawns.generate_group('feral dog', amount=random.randint(4, 6), spawn_chunks=[_spawn_chunk]): life.memory(_alife, 'focus_on_chunk', chunk_key=_spawn_chunk)
def path_along_reference(life, ref_type): _best_reference = _find_best_unknown_reference(life, ref_type)["reference"] if not _best_reference: print "NO BEST", ref_type return False _starting_chunk_key = find_nearest_key_in_reference(life, _best_reference) _chunk_path_keys = [] _directions = {} for neighbor_key in mapgen.get_neighbors_of_type(WORLD_INFO, _starting_chunk_key, ref_type, diagonal=True): if maps.get_chunk(neighbor_key) == lfe.get_current_chunk(life): continue _neighbor_pos = [int(val) + (WORLD_INFO["chunk_size"] / 2) for val in neighbor_key.split(",")] _cent = ( lfe.get_current_chunk(life)["pos"][0] + (WORLD_INFO["chunk_size"] / 2), lfe.get_current_chunk(life)["pos"][1] + (WORLD_INFO["chunk_size"] / 2), ) _neighbor_direction = numbers.direction_to(_cent, _neighbor_pos) _directions[_neighbor_direction] = {"key": neighbor_key, "score": 9999} _best_dir = {"dir": -1, "score": 0} for mod in range(-45, 361, 45): _new_dir = life["discover_direction"] + mod if _new_dir >= 360: _new_dir -= 360 if _new_dir in _directions: _score = 0 if _directions[_new_dir]["key"] in life["known_chunks"]: continue _score += (180 - (abs(_new_dir - life["discover_direction"]))) / 45 _score += life["discover_direction_history"].count(_new_dir) if _score >= _best_dir["score"]: if _score == _best_dir["score"]: _chunk = maps.get_chunk(_directions[_new_dir]["key"]) _best_dir["dir"] = _new_dir _best_dir["score"] = _score if _best_dir["dir"] == -1: return None # print _best_dir,_directions[_best_dir['dir']] life["discover_direction_history"].append(life["discover_direction"]) if len(life["discover_direction_history"]) >= 5: life["discover_direction_history"].pop(0) life["discover_direction"] = _best_dir["dir"] return _directions[_best_dir["dir"]]["key"]
def path_along_reference(life, ref_type): _reference = WORLD_INFO['references'][WORLD_INFO['reference_map']['roads'][0]] _visible_chunks = alife.brain.get_flag(life, 'visible_chunks') _starting_chunk_key = alife.chunks.get_nearest_chunk_in_list(life['pos'], _reference, check_these_chunks_first=_visible_chunks) _chunk_path_keys = [] _directions = {} for neighbor_key in mapgen.get_neighbors_of_type(WORLD_INFO, _starting_chunk_key, ref_type, diagonal=True): if maps.get_chunk(neighbor_key) == lfe.get_current_chunk(life): continue _neighbor_pos = [int(val)+(WORLD_INFO['chunk_size']/2) for val in neighbor_key.split(',')] _cent = (lfe.get_current_chunk(life)['pos'][0]+(WORLD_INFO['chunk_size']/2), lfe.get_current_chunk(life)['pos'][1]+(WORLD_INFO['chunk_size']/2)) _neighbor_direction = numbers.direction_to(_cent, _neighbor_pos) _directions[_neighbor_direction] = {'key': neighbor_key, 'score': 9999} _best_dir = {'dir': -1, 'score': 0} for mod in range(-45, 361, 45): _new_dir = life['discover_direction']+mod if _new_dir>=360: _new_dir -= 360 if _new_dir in _directions: _score = 0 if _directions[_new_dir]['key'] in life['known_chunks']: continue _score += (180-(abs(_new_dir-life['discover_direction'])))/45 _score += life['discover_direction_history'].count(_new_dir) if _score>=_best_dir['score']: if _score==_best_dir['score']: _chunk = maps.get_chunk(_directions[_new_dir]['key']) _best_dir['dir'] = _new_dir _best_dir['score'] = _score if _best_dir['dir'] == -1: return None #print _best_dir,_directions[_best_dir['dir']] life['discover_direction_history'].append(life['discover_direction']) if len(life['discover_direction_history'])>=5: life['discover_direction_history'].pop(0) life['discover_direction'] = _best_dir['dir'] return _directions[_best_dir['dir']]['key']
def randomize_item_spawns(): for chunk_key in WORLD_INFO['chunk_map']: _chunk = maps.get_chunk(chunk_key) if not 'spawn_items' in _chunk['flags']: continue for item in _chunk['flags']['spawn_items']: if random.uniform(0, 1) < item['rarity']: continue for i in range(item['amount']): _rand_pos = random.choice(_chunk['ground'])[:] _rand_pos.append(2) items.create_item(item['item'], position=_rand_pos) return False for building in WORLD_INFO['reference_map']['buildings']: _chunk_key = random.choice(alife.references.get_reference(building)) _chunk = maps.get_chunk(_chunk_key) if not _chunk['ground']: continue if random.randint(0, 100) >= 65: for i in range(0, 1 + random.randint(0, 3)): _rand_pos = random.choice(_chunk['ground']) items.create_item('.22 rifle', position=[_rand_pos[0], _rand_pos[1], 2]) items.create_item('.22 LR magazine', position=[_rand_pos[0], _rand_pos[1], 2]) for i in range(10): _rand_pos = random.choice(_chunk['ground']) items.create_item('.22 LR cartridge', position=[_rand_pos[0], _rand_pos[1], 2]) elif random.randint(0, 100) >= 40: _items = ['corn', 'soda'] for i in range(0, 1 + random.randint(0, 3)): _rand_pos = random.choice(_chunk['ground']) items.create_item(random.choice(_items), position=[_rand_pos[0], _rand_pos[1], 2]) for i in range(0, 1 + random.randint(0, 3)): _rand_pos = random.choice(_chunk['ground']) items.create_item(random.choice(RECRUIT_ITEMS), position=[_rand_pos[0], _rand_pos[1], 2])
def _find_nearest_reference(life, ref_type, skip_current=False, skip_known=False, skip_unknown=False, ignore_array=[]): _lowest = {'chunk_key': None, 'reference': None, 'distance': -1} for reference in WORLD_INFO['reference_map'][ref_type]: if reference in ignore_array: continue _nearest_key = find_nearest_key_in_reference(life, reference) if skip_current and maps.get_chunk(_nearest_key) == lfe.get_current_chunk(life): continue if skip_known and _nearest_key in life['known_chunks']: continue if skip_unknown and not _nearest_key in life['known_chunks']: continue _center = [int(val)+(WORLD_INFO['chunk_size']/2) for val in _nearest_key.split(',')] _distance = numbers.distance(life['pos'], _center) if not _lowest['chunk_key'] or _distance<_lowest['distance']: _lowest['distance'] = _distance _lowest['chunk_key'] = _nearest_key _lowest['reference'] = reference return _lowest
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 _scan_surroundings(center_chunk_key, chunk_size, vision, ignore_chunks=[], chunk_map=WORLD_INFO['chunk_map']): _center_chunk_pos = maps.get_chunk(center_chunk_key)['pos'] #_center_chunk_pos[0] = ((_center_chunk_pos[0]/chunk_size)*chunk_size)+(chunk_size/2) #_center_chunk_pos[1] = ((_center_chunk_pos[1]/chunk_size)*chunk_size)+(chunk_size/2) _chunks = set() _chunk_map = set(chunk_map.keys()) for _x_mod, _y_mod in render_los.draw_circle(0, 0, ((vision * 2) / chunk_size)): x_mod = _center_chunk_pos[0] + (_x_mod * chunk_size ) #(_x_mod/chunk_size)*chunk_size y_mod = _center_chunk_pos[1] + (_y_mod * chunk_size) #print x_mod, y_mod, _center_chunk_pos _chunk_key = '%s,%s' % (x_mod, y_mod) if _chunk_key in _chunks: continue if not ignore_chunks == 0 and _chunk_key in ignore_chunks: continue elif isinstance(ignore_chunks, list): ignore_chunks.append(_chunk_key) if chunk_map and not _chunk_key in chunk_map: continue _chunks.add(_chunk_key) return list(_chunks)
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 _scan_surroundings(center_chunk_key, chunk_size, vision, ignore_chunks=[], chunk_map=WORLD_INFO['chunk_map']): _center_chunk_pos = maps.get_chunk(center_chunk_key)['pos'] #_center_chunk_pos[0] = ((_center_chunk_pos[0]/chunk_size)*chunk_size)+(chunk_size/2) #_center_chunk_pos[1] = ((_center_chunk_pos[1]/chunk_size)*chunk_size)+(chunk_size/2) _chunks = set() _chunk_map = set(chunk_map.keys()) for _x_mod, _y_mod in render_los.draw_circle(0, 0, ((vision*2)/chunk_size)): x_mod = _center_chunk_pos[0]+(_x_mod*chunk_size) #(_x_mod/chunk_size)*chunk_size y_mod = _center_chunk_pos[1]+(_y_mod*chunk_size) #print x_mod, y_mod, _center_chunk_pos _chunk_key = '%s,%s' % (x_mod, y_mod) if _chunk_key in _chunks: continue if not ignore_chunks==0 and _chunk_key in ignore_chunks: continue elif isinstance(ignore_chunks, list): ignore_chunks.append(_chunk_key) if chunk_map and not _chunk_key in chunk_map: continue _chunks.add(_chunk_key) return list(_chunks)
def _find_nearest_reference(life, ref_type, skip_current=False, skip_known=False, skip_unknown=False, ignore_array=[]): _lowest = {"chunk_key": None, "reference": None, "distance": -1} for reference in WORLD_INFO["reference_map"][ref_type]: if reference in ignore_array: continue _nearest_key = find_nearest_key_in_reference(life, reference) if skip_current and maps.get_chunk(_nearest_key) == lfe.get_current_chunk(life): continue if skip_known and _nearest_key in life["known_chunks"]: continue if skip_unknown and not _nearest_key in life["known_chunks"]: continue _center = [int(val) + (WORLD_INFO["chunk_size"] / 2) for val in _nearest_key.split(",")] _distance = numbers.distance(life["pos"], _center) if not _lowest["chunk_key"] or _distance < _lowest["distance"]: _lowest["distance"] = _distance _lowest["chunk_key"] = _nearest_key _lowest["reference"] = reference return _lowest
def find_nearest_key_in_reference(life, reference_id, unknown=False, ignore_current=False, threshold=-1): _lowest = {'chunk_key': None, 'distance': 9000} #Dirty hack here... #We can use the list of visible chunks to find the nearest key in the reference #This is actually SLOWER if the NPC can't see any keys in the reference and a search #has to be done (the way we're doing it now.) _reference = get_reference(reference_id) for _key in _reference: if unknown and _key in life['known_chunks']: continue if ignore_current and lfe.get_current_chunk_id(life) == _key: print 'ignoring current' continue if not maps.get_chunk(_key)['ground']: continue _center = [int(val)+(WORLD_INFO['chunk_size']/2) for val in _key.split(',')] _distance = numbers.distance(life['pos'], _center) if not _lowest['chunk_key'] or _distance<_lowest['distance']: _lowest['distance'] = _distance _lowest['chunk_key'] = _key if threshold > -1 and _lowest['distance'] <= threshold: break return _lowest['chunk_key']
def randomize_item_spawns(): for building in WORLD_INFO['reference_map']['buildings']: _chunk_key = random.choice(alife.references.get_reference(building)) _chunk = maps.get_chunk(_chunk_key) if not _chunk['ground']: continue if random.randint(0, 100)>=65: for i in range(0, 1+random.randint(0, 3)): _rand_pos = random.choice(_chunk['ground']) items.create_item('.22 rifle', position=[_rand_pos[0], _rand_pos[1], 2]) items.create_item('.22 LR magazine', position=[_rand_pos[0], _rand_pos[1], 2]) for i in range(10): _rand_pos = random.choice(_chunk['ground']) items.create_item('.22 LR cartridge', position=[_rand_pos[0], _rand_pos[1], 2]) elif random.randint(0, 100)>=40: _items = ['corn', 'soda'] for i in range(0, 1+random.randint(0, 3)): _rand_pos = random.choice(_chunk['ground']) items.create_item(random.choice(_items), position=[_rand_pos[0], _rand_pos[1], 2]) for i in range(0, 1+random.randint(0, 3)): _rand_pos = random.choice(_chunk['ground']) items.create_item(random.choice(RECRUIT_ITEMS), position=[_rand_pos[0], _rand_pos[1], 2])
def travel_to_chunk(life, chunk_key): _chunk_pos = maps.get_chunk(chunk_key)['pos'] return travel_to_position(life, [ _chunk_pos[0] + WORLD_INFO['chunk_size'] / 2, _chunk_pos[1] + WORLD_INFO['chunk_size'] / 2, 2 ])
def position_is_in_chunk(position, chunk_id): _chunk = maps.get_chunk(chunk_id) if position[0] >= _chunk['pos'][0] and position[0] <= _chunk['pos'][0]+WORLD_INFO['chunk_size']\ and position[1] >= _chunk['pos'][1] and position[1] <= _chunk['pos'][1]+WORLD_INFO['chunk_size']: return True return False
def is_in_chunk(life, chunk_key): _chunk = maps.get_chunk(chunk_key) if life['pos'][0] >= _chunk['pos'][0] and life['pos'][0] <= _chunk['pos'][0]+WORLD_INFO['chunk_size']\ and life['pos'][1] >= _chunk['pos'][1] and life['pos'][1] <= _chunk['pos'][1]+WORLD_INFO['chunk_size']: return True 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 get_alife_in_chunk_matching(chunk_key, matching): _life = [] _chunk = maps.get_chunk(chunk_key) for alife in [LIFE[l] for l in _chunk['life']]: if logic.matches(alife, matching): _life.append(alife['id']) return _life
def set_focus_point(life, chunk_key): lfe.delete_memory(life, matches={'text': 'focus_on_chunk'}) lfe.memory(life, 'focus_on_chunk', chunk_key=chunk_key) if 'player' in life: _center_chunk_pos = maps.get_chunk(chunk_key)['pos'] _center_chunk_pos.append(2) logic.show_event('<Movement Order>', pos=_center_chunk_pos)
def randomize_item_spawns(): for chunk_key in WORLD_INFO['chunk_map']: _chunk = maps.get_chunk(chunk_key) if not 'spawn_items' in _chunk['flags']: continue for item in _chunk['flags']['spawn_items']: if random.uniform(0, 1)<item['rarity']: continue for i in range(item['amount']): _rand_pos = random.choice(_chunk['ground'])[:] _rand_pos.append(2) items.create_item(item['item'], position=_rand_pos) return False for building in WORLD_INFO['reference_map']['buildings']: _chunk_key = random.choice(alife.references.get_reference(building)) _chunk = maps.get_chunk(_chunk_key) if not _chunk['ground']: continue if random.randint(0, 100)>=65: for i in range(0, 1+random.randint(0, 3)): _rand_pos = random.choice(_chunk['ground']) items.create_item('.22 rifle', position=[_rand_pos[0], _rand_pos[1], 2]) items.create_item('.22 LR magazine', position=[_rand_pos[0], _rand_pos[1], 2]) for i in range(10): _rand_pos = random.choice(_chunk['ground']) items.create_item('.22 LR cartridge', position=[_rand_pos[0], _rand_pos[1], 2]) elif random.randint(0, 100)>=40: _items = ['corn', 'soda'] for i in range(0, 1+random.randint(0, 3)): _rand_pos = random.choice(_chunk['ground']) items.create_item(random.choice(_items), position=[_rand_pos[0], _rand_pos[1], 2]) for i in range(0, 1+random.randint(0, 3)): _rand_pos = random.choice(_chunk['ground']) items.create_item(random.choice(RECRUIT_ITEMS), position=[_rand_pos[0], _rand_pos[1], 2])
def set_focus_point(life, chunk_key): lfe.delete_memory(life, matches={"text": "focus_on_chunk"}) lfe.memory(life, "focus_on_chunk", chunk_key=chunk_key) if "player" in life: _center_chunk_pos = maps.get_chunk(chunk_key)["pos"] _center_chunk_pos.append(2) logic.show_event("<Movement Order>", pos=_center_chunk_pos)
def create_region_spawns(): #Runners #spawns.generate_group('runner_scout', amount=random alife.factions.generate() return False #Step 3: Rookie village _spawn_chunks = random.choice( [t['rooms'] for t in WORLD_INFO['refs']['villages'][0]]) _rookie_village_members = spawns.generate_group('loner', amount=random.randint( 7, 9), group_motive='survival', spawn_chunks=_spawn_chunks) for member in _rookie_village_members: alife.planner.remove_goal(member, 'discover') _dirt_road_start_chunk = maps.get_chunk(WORLD_INFO['refs']['dirt_road'][0]) _dirt_road_end_chunk = maps.get_chunk( WORLD_INFO['refs']['dirt_road'][len(WORLD_INFO['refs']['dirt_road']) - 1]) for x in range(0, MAP_SIZE[0], WORLD_INFO['chunk_size']): for y in range(_dirt_road_end_chunk['pos'][1], _dirt_road_start_chunk['pos'][1], WORLD_INFO['chunk_size']): if x < _dirt_road_end_chunk['pos'][0] - ( 10 * WORLD_INFO['chunk_size']) or x > _dirt_road_end_chunk[ 'pos'][0] + (10 * WORLD_INFO['chunk_size']): continue if random.randint(0, 125): continue _spawn_chunk = '%s,%s' % (x, y) for _alife in spawns.generate_group('feral dog', amount=random.randint(4, 6), spawn_chunks=[_spawn_chunk]): life.memory(_alife, 'focus_on_chunk', chunk_key=_spawn_chunk)
def judge_reference(life, reference_id, known_penalty=False): # TODO: Length _score = 0 _count = 0 _closest_chunk_key = {"key": None, "distance": -1} for key in references.get_reference(reference_id): if known_penalty and key in life["known_chunks"]: continue _count += 1 _chunk = maps.get_chunk(key) _chunk_center = ( _chunk["pos"][0] + (WORLD_INFO["chunk_size"] / 2), _chunk["pos"][1] + (WORLD_INFO["chunk_size"] / 2), ) _distance = numbers.distance(life["pos"], _chunk_center) if not _closest_chunk_key["key"] or _distance < _closest_chunk_key["distance"]: _closest_chunk_key["key"] = key _closest_chunk_key["distance"] = _distance # Judge: ALife for ai in _chunk["life"]: if ai == life["id"]: continue if not sight.can_see_target(life, ai): continue _knows = brain.knows_alife(life, LIFE[ai]) if not _knows: continue # How long since we've been here? # if key in life['known_chunks']: # _last_visit = numbers.clip(abs((life['known_chunks'][key]['last_visited']-WORLD_INFO['ticks'])/FPS), 2, 99999) # _score += _last_visit # else: # _score += WORLD_INFO['ticks']/FPS # Take length into account _score += _count # Subtract distance in chunks _score -= _closest_chunk_key["distance"] / WORLD_INFO["chunk_size"] # TODO: Average time since last visit (check every key in reference) # TODO: For tracking last visit use world ticks return _score
def judge_reference(life, reference_id, known_penalty=False): #TODO: Length _score = 0 _count = 0 _closest_chunk_key = {'key': None, 'distance': -1} for key in references.get_reference(reference_id): if known_penalty and key in life['known_chunks']: continue _count += 1 _chunk = maps.get_chunk(key) _chunk_center = (_chunk['pos'][0]+(WORLD_INFO['chunk_size']/2), _chunk['pos'][1]+(WORLD_INFO['chunk_size']/2)) _distance = bad_numbers.distance(life['pos'], _chunk_center) if not _closest_chunk_key['key'] or _distance<_closest_chunk_key['distance']: _closest_chunk_key['key'] = key _closest_chunk_key['distance'] = _distance #Judge: ALife for ai in _chunk['life']: if ai == life['id']: continue if not sight.can_see_target(life, ai): continue _knows = brain.knows_alife(life, LIFE[ai]) if not _knows: continue #How long since we've been here? #if key in life['known_chunks']: # _last_visit = bad_numbers.clip(abs((life['known_chunks'][key]['last_visited']-WORLD_INFO['ticks'])/FPS), 2, 99999) # _score += _last_visit #else: # _score += WORLD_INFO['ticks']/FPS #Take length into account _score += _count #Subtract distance in chunks _score -= _closest_chunk_key['distance']/WORLD_INFO['chunk_size'] #TODO: Average time since last visit (check every key in reference) #TODO: For tracking last visit use world ticks return _score
def guard_chunk(life, chunk_key): if 'guard_time' in life['state_flags'] and life['state_flags']['guard_time']: life['state_flags']['guard_time'] -= 1 return False _center_chunk_pos = maps.get_chunk(chunk_key)['pos'] _center_chunk_pos.append(2) _patrol_chunk_key = random.choice(chunks.get_visible_chunks_from(_center_chunk_pos, alife.sight.get_vision(life))) travel_to_chunk(life, _patrol_chunk_key) life['state_flags']['guard_time'] = random.randint(45, 60) return False
def find_nearest_key_in_reference_exact(position, reference): _lowest = {"chunk_key": None, "distance": 100} for _key in reference: if not maps.get_chunk(_key)["ground"]: continue _center = [int(val) + (WORLD_INFO["chunk_size"] / 2) for val in _key.split(",")] _distance = numbers.distance(position, _center) if not _lowest["chunk_key"] or _distance < _lowest["distance"]: _lowest["distance"] = _distance _lowest["chunk_key"] = _key return _lowest["chunk_key"]
def find_nearest_key_in_reference_exact(position, reference): _lowest = {'chunk_key': None, 'distance': 100} for _key in reference: if not maps.get_chunk(_key)['ground']: continue _center = [int(val)+(WORLD_INFO['chunk_size']/2) for val in _key.split(',')] _distance = numbers.distance(position, _center) if not _lowest['chunk_key'] or _distance<_lowest['distance']: _lowest['distance'] = _distance _lowest['chunk_key'] = _key return _lowest['chunk_key']
def can_see_chunk_from_pos(pos1, chunk_key, distance=True, vision=10): _fast_see = _can_see_chunk_quick(pos1, chunk_key, vision) if _fast_see: return _fast_see chunk = maps.get_chunk(chunk_key) for y in range(chunk['pos'][1], chunk['pos'][1]+WORLD_INFO['chunk_size']): for x in range(chunk['pos'][0], chunk['pos'][0]+WORLD_INFO['chunk_size']): if ((x-chunk['pos'][0] >= 0 and x-chunk['pos'][0] <= WORLD_INFO['chunk_size']-1) and y-chunk['pos'][1] in [0, WORLD_INFO['chunk_size']-1]) or\ ((y-chunk['pos'][1] >= 0 and y-chunk['pos'][1] <= WORLD_INFO['chunk_size']-1) and x-chunk['pos'][0] in [0, WORLD_INFO['chunk_size']-1]): _can_see = sight._can_see_position(pos1, (x, y), distance=distance, max_length=vision) if _can_see: return _can_see return False
def find_least_populated_key_in_reference(life, reference): _lowest = {'chunk_key': None, 'score': 0} for _key in reference: _chunk = maps.get_chunk(_key) _score = len(_chunk['life']) if chunks.is_in_chunk(life, _key) and _score == 1: _score = -1 if not _lowest['chunk_key'] or _score < _lowest['score']: _lowest['chunk_key'] = _key _lowest['score'] = _score if _score == -1: break return _lowest['chunk_key']
def find_least_populated_key_in_reference(life, reference): _lowest = {'chunk_key': None, 'score': 0} for _key in reference: _chunk = maps.get_chunk(_key) _score = len(_chunk['life']) if chunks.is_in_chunk(life, _key) and _score == 1: _score = -1 if not _lowest['chunk_key'] or _score<_lowest['score']: _lowest['chunk_key'] = _key _lowest['score'] = _score if _score == -1: break return _lowest['chunk_key']
def find_least_populated_key_in_reference(life, reference): _lowest = {"chunk_key": None, "score": 0} for _key in reference: _chunk = maps.get_chunk(_key) _score = len(_chunk["life"]) if chunks.is_in_chunk(life, _key) and _score == 1: _score = -1 if not _lowest["chunk_key"] or _score < _lowest["score"]: _lowest["chunk_key"] = _key _lowest["score"] = _score if _score == -1: break return _lowest["chunk_key"]
def _find_best_unknown_reference(life, ref_type): _best_reference = {'reference': None, 'score': -1} for reference in WORLD_INFO['reference_map'][ref_type]: _score = judgement.judge_reference(life, reference, known_penalty=True) if not _score: continue _chunk_key = find_nearest_key_in_reference(life, reference) if numbers.distance(life['pos'], maps.get_chunk(_chunk_key)['pos'])/WORLD_INFO['chunk_size']>10: continue if not _best_reference['reference'] or _score>_best_reference['score']: _best_reference['score'] = _score _best_reference['reference'] = reference _best_reference['chunk_key'] = _chunk_key return _best_reference
def _can_see_chunk_quick(start_pos, chunk_id, vision): chunk = maps.get_chunk(chunk_id) if not len(chunk['ground']): return False for pos in [(0, 0), (1, 0), (0, 1), (1, 1)]: _x = pos[0]*WORLD_INFO['chunk_size'] _y = pos[1]*WORLD_INFO['chunk_size'] if _x: _x -= 1 if _y: _y -= 1 _can_see = sight._can_see_position(start_pos, (chunk['pos'][0]+_x, chunk['pos'][1]+_y), max_length=vision) if _can_see: return _can_see return False
def _find_best_unknown_reference(life, ref_type): _best_reference = {"reference": None, "score": -1} for reference in WORLD_INFO["reference_map"][ref_type]: _score = judgement.judge_reference(life, reference, known_penalty=True) if not _score: continue # TODO: We do this twice (check in path_along_reference). Not good! if ( numbers.distance(life["pos"], maps.get_chunk(find_nearest_key_in_reference(life, reference))["pos"]) / WORLD_INFO["chunk_size"] > 10 ): continue if not _best_reference["reference"] or _score > _best_reference["score"]: _best_reference["score"] = _score _best_reference["reference"] = reference return _best_reference
def find_nearest_key_in_reference(life, reference_id, unknown=False, ignore_current=False, threshold=-1): _lowest = {"chunk_key": None, "distance": 9000} for _key in get_reference(reference_id): if unknown and _key in life["known_chunks"]: continue if ignore_current and lfe.get_current_chunk_id(life) == _key: print "ignoring current" if not maps.get_chunk(_key)["ground"]: continue _center = [int(val) + (WORLD_INFO["chunk_size"] / 2) for val in _key.split(",")] _distance = numbers.distance(life["pos"], _center) if not _lowest["chunk_key"] or _distance < _lowest["distance"]: _lowest["distance"] = _distance _lowest["chunk_key"] = _key if threshold > -1 and _lowest["distance"] <= threshold: break return _lowest["chunk_key"]
def look(life): if not 'CAN_SEE' in life['life_flags']: return False for target_id in life['know']: if life['know'][target_id]['last_seen_time']: life['know'][target_id]['last_seen_time'] += 1 life['know'][target_id]['time_visible'] = 0 else: life['know'][target_id]['time_visible'] += 1 if 'player' in life: if life['path'] or not brain.get_flag(life, 'visible_chunks'): if SETTINGS['smp']: _visible_chunks = post_scan_surroundings(life) else: _visible_chunks = scan_surroundings(life, judge=False, get_chunks=True, ignore_chunks=0) _chunks = [maps.get_chunk(c) for c in _visible_chunks] brain.flag(life, 'visible_chunks', value=_visible_chunks) elif 'player' in life: _visible_chunks = brain.get_flag(life, 'visible_chunks') _chunks = [maps.get_chunk(c) for c in _visible_chunks] else: #This is for optimizing. Be careful if you mess with this... _nearby_alife = {} for alife in LIFE.values(): if alife['id'] == life['id']: continue if bad_numbers.distance( life['pos'], alife['pos']) <= get_vision(life) and can_see_position( life, alife['pos']): _nearby_alife[alife['id']] = { 'pos': alife['pos'][:], 'stance': alife['stance'] } _last_nearby_alife = brain.get_flag(life, '_nearby_alife') if not _last_nearby_alife == _nearby_alife: brain.flag(life, '_nearby_alife', value=_nearby_alife) else: for target_id in life['seen']: if life['know'][target_id]['last_seen_time']: life['know'][target_id]['last_seen_time'] = 0 return False _chunks = [ maps.get_chunk(c) for c in brain.get_flag(life, 'visible_chunks') ] life['seen'] = [] life['seen_items'] = [] for item_uid in life['know_items']: life['know_items'][item_uid]['last_seen_time'] += 1 for target_id in life['know']: life['know'][target_id]['last_seen_time'] += 1 if life['know'][target_id]['last_seen_time'] >= 10 and not life[ 'know'][target_id]['escaped']: life['know'][target_id]['escaped'] = 1 if not 'player' in life: quick_look(life) return False for chunk in _chunks: judgement.judge_chunk_visually( life, '%s,%s' % (chunk['pos'][0], chunk['pos'][1])) judgement.judge_chunk_life( life, '%s,%s' % (chunk['pos'][0], chunk['pos'][1])) for ai in [LIFE[i] for i in chunk['life']]: if ai['id'] == life['id']: continue if not is_in_fov(life, ai['pos']): if ai['id'] in life['know']: life['know'][ai['id']]['time_visible'] = 0 continue if not ai['id'] in life['know']: brain.meet_alife(life, ai) _visibility = get_visiblity_of_position(life, ai['pos']) _stealth_coverage = get_stealth_coverage(ai) if _visibility < 1 - _stealth_coverage: continue life['seen'].append(ai['id']) if life['think_rate'] == life['think_rate_max']: lfe.create_and_update_self_snapshot(LIFE[ai['id']]) judgement.judge_life(life, ai['id']) if ai['dead']: if 'player' in life and not life['know'][ ai['id']]['dead'] and life['know'][ ai['id']]['last_seen_time'] > 25: logic.show_event('You discover the body of %s.' % ' '.join(ai['name']), life=ai) if life['know'][ai['id']]['group']: groups.remove_member(life, life['know'][ai['id']]['group'], ai['id']) life['know'][ai['id']]['group'] = None core.record_loss(1) life['know'][ai['id']]['dead'] = True elif ai['asleep']: life['know'][ai['id']]['asleep'] = True elif not ai['asleep']: life['know'][ai['id']]['asleep'] = False life['know'][ai['id']]['last_seen_time'] = 0 life['know'][ai['id']]['last_seen_at'] = ai['pos'][:] life['know'][ai['id']]['escaped'] = False life['know'][ai['id']]['state'] = ai['state'] life['know'][ai['id']]['state_tier'] = ai['state_tier'] if brain.alife_has_flag(life, ai['id'], 'search_map'): brain.unflag_alife(life, ai['id'], 'search_map') _chunk_id = lfe.get_current_chunk_id(ai) judgement.judge_chunk(life, _chunk_id, seen=True) for item in [ITEMS[i] for i in chunk['items'] if i in ITEMS]: if not is_in_fov(life, item['pos']): continue if not item['uid'] in life['know_items']: brain.remember_item(life, item) if items.is_item_owned(item['uid']): #TODO: This doesn't work because we are specifically checking chunks if item['owner'] and lfe.item_is_equipped( LIFE[item['owner']], item['uid']): life['know_items'][item['uid']]['last_seen_at'] = LIFE[ item['owner']]['pos'] life['know_items'][ item['uid']]['last_owned_by'] = item['owner'] life['know_items'][item['uid']]['last_seen_time'] = 0 continue life['seen_items'].append(item['uid']) life['know_items'][item['uid']]['last_seen_at'] = item['pos'][:] life['know_items'][item['uid']]['last_seen_time'] = 0 life['know_items'][item['uid']]['last_owned_by'] = None life['know_items'][item['uid']]['score'] = judgement.judge_item( life, item['uid']) life['know_items'][item['uid']]['lost'] = False
def get_chunk(chunk_key): return maps.get_chunk(chunk_key)
def get_global_flag(chunk_key, flag): if flag in maps.get_chunk(chunk_key)['flags']: return maps.get_chunk(chunk_key)['flags'][flag] return False
def flag_global(chunk_key, flag, value): maps.get_chunk(chunk_key)['flags'][flag] = value
def get_walkable_areas(chunk_id): return maps.get_chunk(chunk_id)['ground']
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