def _generate_los(life, target, at, source_map, score_callback, invert=False, ignore_starting=False): #Step 1: Locate cover _cover = {'pos': None, 'score': 9000} #TODO: Unchecked Cython flag _x = bad_numbers.clip(at[0] - (MAP_WINDOW_SIZE[0] / 2), 0, MAP_SIZE[0]) _y = bad_numbers.clip(at[1] - (MAP_WINDOW_SIZE[1] / 2), 0, MAP_SIZE[1]) _top_left = (_x, _y, at[2]) target_los = render_los.render_los(source_map, at, top_left=_top_left, no_edge=False) for pos in render_los.draw_circle(life['pos'][0], life['pos'][1], 30): x = pos[0] - _top_left[0] y = pos[1] - _top_left[1] if pos[0] < 0 or pos[1] < 0 or pos[0] >= MAP_SIZE[0] or pos[ 1] >= MAP_SIZE[0]: continue if x < 0 or y < 0 or x >= target_los.shape[1] or y >= target_los.shape[ 0]: continue if life['pos'][0] - _top_left[0] >= target_los.shape[ 0] or life['pos'][1] - _top_left[1] >= target_los.shape[1]: continue if target_los[life['pos'][1] - _top_left[1], life['pos'][0] - _top_left[0]] == invert and not ignore_starting: _cover['pos'] = life['pos'][:] return False if source_map[pos[0]][pos[1]][at[2] + 1] or source_map[pos[0]][pos[1]][at[2] + 2]: continue if target_los[y, x] == invert: #TODO: Additional scores, like distance from target _score = score_callback(life, target['life'], pos) if _score < _cover['score']: _cover['score'] = _score _cover['pos'] = list(pos) if not _cover['pos']: print 'Nowhere to hide' return False return _cover
def _generate_los(life,target,at,source_map,score_callback,invert=False,ignore_starting=False): #Step 1: Locate cover _cover = {'pos': None,'score': 9000} #TODO: Unchecked Cython flag _x = bad_numbers.clip(at[0]-(MAP_WINDOW_SIZE[0]/2),0,MAP_SIZE[0]) _y = bad_numbers.clip(at[1]-(MAP_WINDOW_SIZE[1]/2),0,MAP_SIZE[1]) _top_left = (_x,_y,at[2]) target_los = render_los.render_los(source_map,at,top_left=_top_left,no_edge=False) for pos in render_los.draw_circle(life['pos'][0],life['pos'][1],30): x = pos[0]-_top_left[0] y = pos[1]-_top_left[1] if pos[0]<0 or pos[1]<0 or pos[0]>=MAP_SIZE[0] or pos[1]>=MAP_SIZE[0]: continue if x<0 or y<0 or x>=target_los.shape[1] or y>=target_los.shape[0]: continue if life['pos'][0]-_top_left[0]>=target_los.shape[0] or life['pos'][1]-_top_left[1]>=target_los.shape[1]: continue if target_los[life['pos'][1]-_top_left[1],life['pos'][0]-_top_left[0]]==invert and not ignore_starting: _cover['pos'] = life['pos'][:] return False if source_map[pos[0]][pos[1]][at[2]+1] or source_map[pos[0]][pos[1]][at[2]+2]: continue if target_los[y,x] == invert: #TODO: Additional scores, like distance from target _score = score_callback(life,target['life'],pos) if _score<_cover['score']: _cover['score'] = _score _cover['pos'] = list(pos) if not _cover['pos']: print 'Nowhere to hide' return False return _cover
def render_lights(size=MAP_WINDOW_SIZE, show_weather=True): if not SETTINGS['draw lights']: return False reset_lights(size=size) _weather_light = weather.get_lighting() #Not entirely my code. Made some changes to someone's code from libtcod's Python forum. RGB_LIGHT_BUFFER[0] = numpy.add(RGB_LIGHT_BUFFER[0], _weather_light[0]) RGB_LIGHT_BUFFER[1] = numpy.add(RGB_LIGHT_BUFFER[1], _weather_light[1]) RGB_LIGHT_BUFFER[2] = numpy.add(RGB_LIGHT_BUFFER[2], _weather_light[2]) (x, y) = SETTINGS['light mesh grid'] if show_weather: weather.generate_effects(size) _remove_lights = [] for light in WORLD_INFO['lights']: _x_range = light['pos'][0] - CAMERA_POS[0] _y_range = light['pos'][1] - CAMERA_POS[1] if _x_range <= -20 or _x_range >= size[0] + 20: continue if _y_range <= -20 or _y_range >= size[1] + 20: continue if not 'old_pos' in light: light['old_pos'] = (0, 0, -2) else: light['old_pos'] = light['pos'][:] if 'follow_item' in light: if not light['follow_item'] in ITEMS: _remove_lights.append(light) continue light['pos'] = items.get_pos(light['follow_item'])[:] _render_x = light['pos'][0] - CAMERA_POS[0] _render_y = light['pos'][1] - CAMERA_POS[1] _x = bad_numbers.clip(light['pos'][0] - (size[0] / 2), 0, MAP_SIZE[0]) _y = bad_numbers.clip(light['pos'][1] - (size[1] / 2), 0, MAP_SIZE[1]) _top_left = (_x, _y, light['pos'][2]) #TODO: Render only on move if not tuple(light['pos']) == tuple(light['old_pos']): light['los'] = cython_render_los.render_los( (light['pos'][0], light['pos'][1]), light['brightness'] * 2, view_size=size, top_left=_top_left) los = light['los'].copy() _x_scroll = _x - CAMERA_POS[0] _x_scroll_over = 0 _y_scroll = _y - CAMERA_POS[1] _y_scroll_over = 0 if _x_scroll < 0: _x_scroll_over = _x_scroll _x_scroll = los.shape[1] + _x_scroll if _y_scroll < 0: _y_scroll_over = _y_scroll _y_scroll = los.shape[0] + _y_scroll los = numpy.roll(los, _y_scroll, axis=0) los = numpy.roll(los, _x_scroll, axis=1) los[_y_scroll_over:_y_scroll, ] = 1 los[:, _x_scroll_over:_x_scroll] = 1 if SETTINGS['diffuse light']: _y, _x = diffuse_light((y, x)) (x, y) = numpy.meshgrid(_x, _y) sqr_distance = (x - (_render_x))**2.0 + (y - (_render_y))**2.0 brightness = bad_numbers.clip( random.uniform(light['brightness'] * light['shake'], light['brightness']), 0.01, 50) / sqr_distance brightness *= los #brightness *= LOS_BUFFER[0] #_mod = (abs((WORLD_INFO['length_of_day']/2)-WORLD_INFO['real_time_of_day'])/float(WORLD_INFO['length_of_day']))*5.0 #_mod = bad_numbers.clip(_mod-1, 0, 1) #(255*_mod, 165*_mod, 0*_mod) #print brightness #light['brightness'] = 25 #light['color'][0] = 255*(light['brightness']/255.0) #light['color'][1] = (light['brightness']/255.0) #light['color'][2] = 255*(light['brightness']/255.0) RGB_LIGHT_BUFFER[0] -= ( brightness.clip(0, 2) * (light['color'][0]) ) #numpy.subtract(RGB_LIGHT_BUFFER[0], light['color'][0]).clip(0, 255) RGB_LIGHT_BUFFER[1] -= ( brightness.clip(0, 2) * (light['color'][1]) ) #numpy.subtract(RGB_LIGHT_BUFFER[1], light['color'][1]).clip(0, 255) RGB_LIGHT_BUFFER[2] -= ( brightness.clip(0, 2) * (light['color'][2]) ) #numpy.subtract(RGB_LIGHT_BUFFER[2], light['color'][2]).clip(0, 255)
def render_lights(size=MAP_WINDOW_SIZE, show_weather=True): if not SETTINGS['draw lights']: return False reset_lights(size=size) _weather_light = weather.get_lighting() #Not entirely my code. Made some changes to someone's code from libtcod's Python forum. RGB_LIGHT_BUFFER[0] = numpy.add(RGB_LIGHT_BUFFER[0], _weather_light[0]) RGB_LIGHT_BUFFER[1] = numpy.add(RGB_LIGHT_BUFFER[1], _weather_light[1]) RGB_LIGHT_BUFFER[2] = numpy.add(RGB_LIGHT_BUFFER[2], _weather_light[2]) (x, y) = SETTINGS['light mesh grid'] if show_weather: weather.generate_effects(size) _remove_lights = [] for light in WORLD_INFO['lights']: _x_range = light['pos'][0]-CAMERA_POS[0] _y_range = light['pos'][1]-CAMERA_POS[1] if _x_range <= -20 or _x_range>=size[0]+20: continue if _y_range <= -20 or _y_range>=size[1]+20: continue if not 'old_pos' in light: light['old_pos'] = (0, 0, -2) else: light['old_pos'] = light['pos'][:] if 'follow_item' in light: if not light['follow_item'] in ITEMS: _remove_lights.append(light) continue light['pos'] = items.get_pos(light['follow_item'])[:] _render_x = light['pos'][0]-CAMERA_POS[0] _render_y = light['pos'][1]-CAMERA_POS[1] _x = numbers.clip(light['pos'][0]-(size[0]/2),0,MAP_SIZE[0]) _y = numbers.clip(light['pos'][1]-(size[1]/2),0,MAP_SIZE[1]) _top_left = (_x,_y,light['pos'][2]) #TODO: Render only on move if not tuple(light['pos']) == tuple(light['old_pos']): light['los'] = cython_render_los.render_los((light['pos'][0],light['pos'][1]), light['brightness']*2, view_size=size, top_left=_top_left) los = light['los'].copy() _x_scroll = _x-CAMERA_POS[0] _x_scroll_over = 0 _y_scroll = _y-CAMERA_POS[1] _y_scroll_over = 0 if _x_scroll<0: _x_scroll_over = _x_scroll _x_scroll = los.shape[1]+_x_scroll if _y_scroll<0: _y_scroll_over = _y_scroll _y_scroll = los.shape[0]+_y_scroll los = numpy.roll(los, _y_scroll, axis=0) los = numpy.roll(los, _x_scroll, axis=1) los[_y_scroll_over:_y_scroll,] = 1 los[:,_x_scroll_over:_x_scroll] = 1 if SETTINGS['diffuse light']: _y, _x = diffuse_light((y, x)) (x, y) = numpy.meshgrid(_x, _y) sqr_distance = (x - (_render_x))**2.0 + (y - (_render_y))**2.0 brightness = numbers.clip(random.uniform(light['brightness']*light['shake'], light['brightness']), 0.01, 50) / sqr_distance brightness *= los #brightness *= LOS_BUFFER[0] #_mod = (abs((WORLD_INFO['length_of_day']/2)-WORLD_INFO['real_time_of_day'])/float(WORLD_INFO['length_of_day']))*5.0 #_mod = numbers.clip(_mod-1, 0, 1) #(255*_mod, 165*_mod, 0*_mod) #print brightness #light['brightness'] = 25 #light['color'][0] = 255*(light['brightness']/255.0) #light['color'][1] = (light['brightness']/255.0) #light['color'][2] = 255*(light['brightness']/255.0) RGB_LIGHT_BUFFER[0] -= (brightness.clip(0, 2)*(light['color'][0]))#numpy.subtract(RGB_LIGHT_BUFFER[0], light['color'][0]).clip(0, 255) RGB_LIGHT_BUFFER[1] -= (brightness.clip(0, 2)*(light['color'][1]))#numpy.subtract(RGB_LIGHT_BUFFER[1], light['color'][1]).clip(0, 255) RGB_LIGHT_BUFFER[2] -= (brightness.clip(0, 2)*(light['color'][2]))#numpy.subtract(RGB_LIGHT_BUFFER[2], light['color'][2]).clip(0, 255)