def shine(self): # shine light on environment; add light to lightmap assert (self.shone == False) # cannot shine if we already did self.shone = True rang = self.getLumens(self.lum) libtcod.map_compute_fov( # Get the tiles we can see rog.getfovmap(self.fovID), self.x, self.y, rang, light_walls=True, algo=libtcod.FOV_RESTRICTIVE) for x in range(max(0, self.x - rang), min(ROOMW, self.x + rang + 1)): for y in range(max(0, self.y - rang), min(ROOMH, self.y + rang + 1)): if (rog.in_range(self.x, self.y, x, y, rang) and libtcod.map_is_in_fov(rog.getfovmap(self.fovID), x, y)): dist = maths.dist(self.x, self.y, x, y) # F = L / 4 * pi * d^2 (formula for light dispersion) lux = self.lum // (12.5663706144 * (dist**2)) if lux: self.add_tile(x, y, lux) rog.tile_lighten(x, y, lux) # end for return True #success
def draw_map_panel(): global fov_recompute if fov_recompute: # recompute FOV if needed (the player moved or something) fov_recompute = False libtcod.map_compute_fov(fov_map, player.x, player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO) for y in xrange(MAP_HEIGHT): for x in xrange(MAP_WIDTH): visible = libtcod.map_is_in_fov(fov_map, x, y) wall = tile_map[x][y].block_sight if not visible: if tile_map[x][y].explored: if wall: libtcod.console_set_char_background(con, x, y, color_dark_wall, libtcod.BKGND_SET) else: libtcod.console_set_char_background(con, x, y, color_dark_ground, libtcod.BKGND_SET) else: if wall: libtcod.console_set_char_background(con, x, y, color_lit_wall, libtcod.BKGND_SET) else: libtcod.console_set_char_background(con, x, y, color_lit_ground, libtcod.BKGND_SET) tile_map[x][y].explored = True for object in objects: visible = libtcod.map_is_in_fov(fov_map, object.x, object.y) if (visible or (object.always_visible and tile_map[object.x][object.y].explored)) \ and object is not player: object.draw() player.draw()
def _recompute_fov(self): if config.recompute_fov_flag: config.recompute_fov_flag = False libtcod.map_compute_fov(self.fov_map, config.player.x, config.player.y, config.TORCH_RADIUS, config.FOV_LIGHT_WALLS, config.FOV_ALGO)
def render_all(): global color_dark_wall, color_light_wall global color_dark_ground, color_light_ground global fov_recompute if fov_recompute: # Recompute FOV if needed fov_recompute = False libtcod.map_compute_fov(fov_map, player.x, player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO) for y in range(MAP_HEIGHT): for x in range(MAP_WIDTH): visible = libtcod.map_is_in_fov(fov_map, x, y) wall = map[x][y].block_sight if not visible: if map[x][y].explored: if wall: libtcod.console_put_char_ex(con, x, y, '#', color_dark_wall, libtcod.BKGND_SET) #libtcod.console_set_char_background(con, x, y, color_dark_wall, libtcod.BKGND_SET) else: libtcod.console_put_char_ex(con, x, y, '.', color_dark_ground, libtcod.BKGND_SET) #libtcod.console_set_char_background(con, x, y, color_dark_ground, libtcod.BKGND_SET) else: if wall: libtcod.console_put_char_ex(con, x, y, '#', color_light_wall, libtcod.BKGND_SET) else: libtcod.console_put_char_ex(con, x, y, '.', color_light_ground, libtcod.BKGND_SET) map[x][y].explored = True # Draw all objects in the list for object in objects: if object != player: object.draw() player.draw() # Blit the contents of "con" to the root console libtcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0) # Prepare to render the GUI panel libtcod.console_set_default_background(panel, libtcod.black) libtcod.console_clear(panel) # Print the game messages y = 1 for (line, color) in game_msgs: libtcod.console_set_default_foreground(panel, color) libtcod.console_print_ex(panel, MSG_X, y, libtcod.BKGND_NONE, libtcod.LEFT, line) y += 1 # Show the player's stats render_bar(1, 1, BAR_WIDTH, 'HP', player.fighter.hp, player.fighter.max_hp, libtcod.light_red, libtcod.darker_red) # Display names of objects under the mouse libtcod.console_set_default_foreground(panel, libtcod.light_gray) libtcod.console_print_ex(panel, 1, 0, libtcod.BKGND_NONE, libtcod.LEFT, get_names_under_mouse()) # Blit the contents of "panel" to the root console libtcod.console_blit(panel, 0, 0, SCREEN_WIDTH, PANEL_HEIGHT, 0, 0, PANEL_Y)
def set_fov_elevation(self, player): elevation = self.elevation(player.pos.x, player.pos.y) self.fov_needs_recompute = True self.fov_map = libtcod.map_new(self.width, self.height) for y in range(self.height): for x in range(self.width): blocks_sight = (terrain_types[self.terrain[x][y]].blocks_sight or (self.region_elevations[self.region[x][y]] > elevation + 1)) libtcod.map_set_properties( self.fov_map, x, y, not blocks_sight, not terrain_types[self.terrain[x][y]].blocks) for obj in self.objects: if obj.blocks_sight or obj.blocks: blocks = obj.blocks or terrain_types[self.terrain[obj.pos.x][ obj.pos.y]].blocks blocks_sight = (obj.blocks_sight or terrain_types[self.terrain[ obj.pos.x][obj.pos.y]].blocks_sight or (self.region_elevations[self.region[x][y]] > elevation + 1)) libtcod.map_set_properties(self.fov_map, obj.pos.x, obj.pos.y, not blocks_sight, not blocks) libtcod.map_compute_fov(player.current_map.fov_map, player.x, player.y, config.TORCH_RADIUS, config.FOV_LIGHT_WALLS, config.FOV_ALGO)
def recompute_fov(self, clear_all=False): x,y = self.map.map_entrance if self.player is not None: x,y = self.player.pos libtcod.map_compute_fov( self.fov_map, x,y, player.Player.torch_radius, self.fov_light_walls, self.fov_algo ) for x,y, cell in self.map.iter_cells_with_coords(): visible = libtcod.map_is_in_fov(self.fov_map, x,y) if visible and not cell.explored: cell.explored = True color = libtcod.black #if True: if cell.explored: wall = cell.block_sight walkable = not cell.blocked if wall or walkable: color = { True: {True: self.color_light_wall, False: self.color_light_ground}, False: {True: self.color_dark_wall, False: self.color_dark_ground} }[visible][wall] elif not walkable: color = libtcod.Color(100,100,200) if cell.explored or clear_all: libtcod.console_set_char_background(self.con, x, y, color, libtcod.BKGND_SET)
def recalculate(self, level_, initial_location): """ Set this fov to the squares seen from initial_location on level_. level_ - The level on which the field of view is being calculated. initial_location - the viewpoint from which the field of view is being calculated. """ dimensions = level_.dimensions map = level_.sight_map tcod.map_compute_fov(map, initial_location[0], initial_location[1], FOV_RADIUS, True, tcod.FOV_SHADOW) border = set() fov_set = set() dude_set = set() for i in range(initial_location[0] - FOV_RADIUS, initial_location[0] + FOV_RADIUS + 1): for j in range(initial_location[1] - FOV_RADIUS, initial_location[1] + FOV_RADIUS + 1): if tcod.map_is_in_fov(map, i, j): fov_set.add((i, j)) if (i, j) != initial_location and (i, j) in level_.dudeLayer: dude_set.add(level_.dudeLayer[(i, j)]) self.__the_field = fov_set self.dudes = frozenset(dude_set)
def render_all(): global fov_map, color_dark_wall, color_light_wall global color_dark_ground, color_light_ground global fov_recompute if fov_recompute: fov_recompute = False libtcod.map_compute_fov(fov_map,player.x,player.y,TORCH_RADIUS,FOV_LIGHT_WALLS,FOV_ALGO) for y in range(MAP_HEIGHT): for x in range(MAP_WIDTH): visible = libtcod.map_is_in_fov(fov_map,x,y) wall = map[x][y].block_sight if not visible: if map[x][y].explored: if wall: libtcod.console_set_char_background(con,x,y,color_dark_wall,libtcod.BKGND_SET) else: libtcod.console_set_char_background(con,x,y,color_dark_ground,libtcod.BKGND_SET) #else, it's visible else: if wall: libtcod.console_set_char_background(con,x,y,color_light_wall,libtcod.BKGND_SET) else: libtcod.console_set_char_background(con,x,y,color_light_ground,libtcod.BKGND_SET) map[x][y].explored = True #draw all objects, with the player last for object in objects: if object != player: object.draw() player.draw() ################### ## GUI ELEMENTS #preparing to render to panel libtcod.console_set_default_background(panel, libtcod.black) libtcod.console_clear(panel) #display the message box y=1 for (line,color) in game_msgs: libtcod.console_set_default_foreground(panel,color) libtcod.console_print_ex(panel,MSG_X,y,libtcod.BKGND_NONE,libtcod.LEFT,line) y+=1 #show some stats #render_bar(1,1,BAR_WIDTH,'HP',player.body.hp,player.body.max_hp,libtcod.light_red,libtcod.darker_red) libtcod.console_set_default_foreground(panel,libtcod.light_gray) libtcod.console_print_ex(panel,1,0,libtcod.BKGND_NONE,libtcod.LEFT,get_names_under_mouse()) #blit the panel to the screen libtcod.console_blit(panel,0,0,SCREEN_WIDTH,PANEL_HEIGHT,0,0,PANEL_Y) #blit our off-screen console libtcod.console_blit(con,0,0,MAP_WIDTH,MAP_HEIGHT,0,0,0)
def create_position_map(squad): _coverage_positions = set() _known_targets = set() _known_squads = set() for member_id in squad['members']: _member = entities.get_entity(member_id) _x, _y = movement.get_position(_member) _sight = stats.get_vision(_member) if member_id in squad['member_position_maps']: _old_coverage_positions = squad['member_position_maps'][member_id].copy() else: _old_coverage_positions = set() _map_size = zones.get_active_size() squad['member_position_maps'][member_id] = set() squad['member_los_maps'][member_id] = tcod.map_new(_map_size[0], _map_size[1]) tcod.map_copy(zones.get_active_los_map(), squad['member_los_maps'][member_id]) _t = time.time() tcod.map_compute_fov(squad['member_los_maps'][member_id], _x, _y, radius=_sight, light_walls=False, algo=tcod.FOV_PERMISSIVE_2) for pos in shapes.circle(_x, _y, _sight): if pos[0] < 0 or pos[0] >= _map_size[0] or pos[1] < 0 or pos[1] >= _map_size[1]: continue if not tcod.map_is_in_fov(squad['member_los_maps'][member_id], pos[0], pos[1]): continue _coverage_positions.add(pos) squad['member_position_maps'][member_id].add(pos) #TODO: Do this elsewhere for target_id in _member['ai']['visible_targets']: if not target_id in entities.ENTITIES: continue _target = entities.get_entity(target_id) _known_squads.add((_target['ai']['faction'], _target['ai']['squad'])) #TODO: Is this right? _known_targets.update(_member['ai']['visible_targets']) _positions_to_remove = _old_coverage_positions - _coverage_positions squad['known_targets'].update(_known_targets) squad['known_squads'].update(_known_squads) squad['coverage_positions'].update(_coverage_positions) squad['coverage_positions'] = squad['coverage_positions'] - _positions_to_remove if squad['known_targets']: update_position_maps(squad) logging.debug('Updated local position map - requesting squad update') else: logging.debug('Updated local position map.')
def fov_update(x, y): global fov_recompute if fov_recompute: #recompute FOV if needed (the player moved or something) fov_recompute = False libtcod.map_compute_fov(maps.fov_map, x, y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO)
def render_all(): global fov_map, color_dark_wall, color_light_wall global color_dark_ground, color_light_ground global fov_recompute if fov_recompute: #recompute FOV if needed (the player moved or something) fov_recompute = False libtcod.map_compute_fov(fov_map, player.x, player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO) #go through all tiles, and set their background color according to the FOV for y in range(MAP_HEIGHT): for x in range(MAP_WIDTH): visible = libtcod.map_is_in_fov(fov_map, x, y) wall = map[x][y].block_sight if not visible: #it's out of the player's FOV if wall: libtcod.console_set_char_background(con, x, y, color_dark_wall, libtcod.BKGND_SET) else: libtcod.console_set_char_background(con, x, y, color_dark_ground, libtcod.BKGND_SET) else: #it's visible if wall: libtcod.console_set_char_background(con, x, y, color_light_wall, libtcod.BKGND_SET ) else: libtcod.console_set_char_background(con, x, y, color_light_ground, libtcod.BKGND_SET ) #draw all objects in the list for object in objects: object.draw() #blit the contents of "con" to the root console libtcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0)
def prepare_fov(self, light_walls=False): """Calculate light's distribution""" libtcod.map_compute_fov(self.__tcod_light_map, self._size.x // 2, self._size.y // 2, max(self._size.x // 2, self._size.y // 2) + 1, light_walls, libtcod.FOV_BASIC)
def target_monster(max_range=None): gui.message('Left-click on target, or right-click to cancel.', libtcod.light_cyan) #primitive function highlighting range. Ideally would be implemented in read-only attribute of tile rangemap = defn.fov_map libtcod.map_compute_fov(rangemap, defn.player.x, defn.player.y, max_range, defn.FOV_LIGHT_WALLS, defn.FOV_ALGO) #returns a clicked monster inside FOV up to a range, or None if right-clicked while True: (x, y) = target_tile(max_range) if x is None: #player cancelled #remove highlight for y in range(defn.MAP_HEIGHT): for x in range(defn.MAP_WIDTH): if libtcod.map_is_in_fov(rangemap, x, y): libtcod.console_set_char_background(defn.con, x, y, defn.dungeon[x][y].color, libtcod.BKGND_SET) return None #return the first clicked creature, otherwise continue looping for obj in defn.dungeon[x][y].objects: if obj.creature: return obj
def refresh(self): tcod_map = self.game.dungeon.tcod_map if self.entity is self.game.player: libtcod.map_compute_fov(tcod_map, self.entity.x, self.entity.y, PLAYER_FOV_RADIUS) else: libtcod.map_compute_fov(tcod_map, self.entity.x, self.entity.y, self.creature.perception) if self.entity is self.game.player: for x in range(len(self.fov_map)): for y in range(len(self.fov_map[0])): self.fov_map[x][y]=int(libtcod.map_is_in_fov(tcod_map, x,y)) else: self.clear() for x in range(self.entity.x-self.creature.perception, self.entity.x+self.creature.perception+1): for y in range(self.entity.y-self.creature.perception, self.entity.y+self.creature.perception+1): fov_num = int(libtcod.map_is_in_fov(tcod_map,x,y)) if fov_num: if (x-self.entity.x==0 or y-self.entity.y==0 or abs(x-self.entity.x)-abs(y-self.entity.y)==0): fov_num += 1 if util.distance(self.entity.x,self.entity.y,x,y)<=self.creature.perception//3: fov_num += 1 try: self.fov_map[x][y] = fov_num except IndexError: pass
def render(): # renders everything to the screen global fov_map, fov_recompute if fov_recompute: #recomputes FOV if needed (player move, etc) fov_recompute = False libtcod.map_compute_fov(fov_map, player.xcoord, player.ycoord, CONFIG.TORCH_RADIUS, CONFIG.FOV_LIGHT_WALLS, CONFIG.FOV_ALGO) # redraws the screen pygame.display.flip() color = BLACK screen.fill(color) # draws objects to the screen for object in Objects: object.draw() for monster in Monsters: if libtcod.map_is_in_fov(fov_map, monster.xcoord, monster.ycoord): monster.draw() player.draw() GUIrender()
def draw_console(player): """ Refreshes the map display and blits to the window. Sets or clears player.endangered. """ global _con current_map = player.current_map if current_map.is_outdoors and current_map.fov_elevation_changed: current_map.set_fov_elevation(player) current_map.fov_elevation_changed = False if current_map.fov_needs_recompute: libtcod.map_compute_fov( player.current_map.fov_map, player.x, player.y, config.TORCH_RADIUS, config.FOV_LIGHT_WALLS, config.FOV_ALGO) # Redraw if FOV (could have) changed. if current_map.is_outdoors: _draw_outdoors(player) else: _draw_indoors(player) # Draw all objects in the list, except the player. We want it to # always appear over all other objects, so it's drawn later. # (Could also achieve this by guaranteeing the player is always # the last object in current_map.objects.) for object in player.visible_objects: _draw_object(player, object) _draw_object(player, player) libtcod.console_blit(_con, 0, 0, config.MAP_PANEL_WIDTH, config.MAP_PANEL_HEIGHT, 0, 0, 0)
def draw(self): if game.fov_recompute: # recompute FOV if needed (the player moved or something) game.fov_recompute = False # yuck, shouldn't need to reference libtcod.map_compute_fov(self.fov_map, self.protagonist.coords[0], self.protagonist.coords[1], C.TORCH_RADIUS, C.FOV_LIGHT_WALLS, C.FOV_ALGORITHM) # go through all tiles, and set their background color according to the FOV for y in range(self.height): for x in range(self.width): visible = libtcod.map_is_in_fov(self.fov_map, x, y) if not C.DEBUG else True wall = self.map[x][y].block_sight if not visible: # if it's not visible right now, the player can only see it if it's explored if self.map[x][y].explored: if wall: libtcod.console_set_char_background(self.console, x, y, C.COLOR_DARK_WALL, libtcod.BKGND_SET) # libtcod.console_put_char_ex(self.console, x, y, 2242, C.COLOR_DARK_WALL, C.COLOR_BACKGROUND) else: libtcod.console_set_char_background(self.console, x, y, C.COLOR_DARK_GROUND, libtcod.BKGND_SET) # libtcod.console_put_char_ex(self.console, x, y, 2242, C.COLOR_DARK_GROUND, C.COLOR_BACKGROUND) else: # it's visible if wall: libtcod.console_set_char_background(self.console, x, y, C.COLOR_LIGHT_WALL, libtcod.BKGND_SET) # libtcod.console_put_char_ex(self.console, x, y, 2242, C.COLOR_LIGHT_WALL, C.COLOR_BACKGROUND) else: libtcod.console_set_char_background(self.console, x, y, C.COLOR_LIGHT_GROUND, libtcod.BKGND_SET) # libtcod.console_put_char_ex(self.console, x, y, 2242, C.COLOR_LIGHT_GROUND, C.COLOR_BACKGROUND) # since it's visible, explore it self.map[x][y].explored = True
def draw_console(player): """ Refreshes the map display and blits to the window. Sets or clears player.endangered. """ global _con current_map = player.current_map if current_map.fov_needs_recompute: # Recompute FOV if needed (the player moved or something in # the dungeon changed). libtcod.map_compute_fov( current_map.fov_map, player.x, player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO) _draw_fov_using_terrain(player) # Draw all objects in the list, except the player. We want it to # always appear over all other objects, so it's drawn later. # (Could also achieve this by guaranteeing the player is always # the last object in current_map.objects.) for object in player.visible_objects: _draw_object(player, object) _draw_object(player, player) libtcod.console_blit(_con, 0, 0, config.MAP_PANEL_WIDTH, config.MAP_PANEL_HEIGHT, 0, 0, 0)
def render_all(): global fov_map, color_dark_wall, color_light_wall global color_dark_ground, color_dark_wall global fov_recompute if fov_recompute: fov_recompute = False libtcod.map_compute_fov(fov_map, player.x, player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO) for y in range(MAP_HEIGHT): for x in range(MAP_WIDTH): visible = libtcod.map_is_in_fov(fov_map, x, y) wall = map[x][y].block_sight if not visible: if map[x][y].explored: if wall: libtcod.console_put_char_ex(con, x, y, '#', color_dark_wall, libtcod.black) else: libtcod.console_put_char_ex(con, x, y, '.', color_dark_ground, libtcod.black) else: if wall: libtcod.console_put_char_ex(con, x, y, '#', color_light_wall, libtcod.black) else: libtcod.console_put_char_ex(con, x, y, '.', color_light_ground, libtcod.black) map[x][y].explored = True for object in objects: object.draw() libtcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0)
def map_calculate_fov(): global FOV_CALCULATE if FOV_CALCULATE: FOV_CALCULATE = False libtcod.map_compute_fov(FOV_MAP, PLAYER.x, PLAYER.y, constants.LIGHT_RADIUS, constants.FOV_LIGHT_WALLS, constants.FOV_ALGO)
def renderAll(): global fovNeedsToBeRecomputed if fovNeedsToBeRecomputed: #If this is true, then we must recalculate the field of view and render the map. fovNeedsToBeRecomputed = False libtcod.map_compute_fov(fovMap, player.x, player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO) #Iterate through the list of map tiles and set their background colors. for y in range(MAP_HEIGHT): for x in range(MAP_WIDTH): visible = libtcod.map_is_in_fov(fovMap, x, y) wall = map[x][y].blockSight if not visible: #If a tile is out of the player's field of view... if map[x][y].explored: #...it will only be drawn if the player has explored it if wall: libtcod.console_set_char_background(con, x, y, cDarkWall, libtcod.BKGND_SET) else: libtcod.console_set_char_background(con, x, y, cDarkGround, libtcod.BKGND_SET) else: #If a tile is in the player's field of view... if wall: libtcod.console_set_char_background(con, x, y, cLitWall, libtcod.BKGND_SET) else: libtcod.console_set_char_background(con, x, y, cLitGround, libtcod.BKGND_SET) map[x][y].explored = True #Draw all objects in the list, except the player, which needs to be drawn last. for object in objects: if object != player: object.draw() player.draw() #Blit the contents of con to the root console. libtcod.console_blit(con, 0, 0, MAP_WIDTH, MAP_HEIGHT, 0, 0, 0) #Prepare to render the status panel. libtcod.console_set_default_background(panel, libtcod.black) libtcod.console_clear(panel) #Print the message log, one line at a time. y = 1 for (line, color) in messageLog: libtcod.console_set_default_foreground(panel, color) libtcod.console_print_ex(panel, MESSAGE_X, y, libtcod.BKGND_NONE, libtcod.LEFT, line) y += 1 #Show the player's stats renderStatusBar(1, 1, BAR_WIDTH, "Health", player.fighter.cond, player.fighter.hits, libtcod.red, libtcod.darkest_red) libtcod.console_print_ex(panel, 1, 3, libtcod.BKGND_NONE, libtcod.LEFT, "Dungeon Level: " + str(dungeonLevel)) #Display the names of objects under the mouse cursor. libtcod.console_set_default_foreground(panel, libtcod.light_gray) libtcod.console_print_ex(panel, 1, 0, libtcod.BKGND_NONE, libtcod.LEFT, getNamesUnderMouse()) #Blit the contents of panel to the root console. libtcod.console_blit(panel, 0, 0, SCREEN_WIDTH, PANEL_HEIGHT, 0, 0, PANEL_Y)
def target_tile(max_range=None): #return the position of a tile left-clicked in player's FOV (optionally in a range), or (None,None) if right-clicked. rangemap = defn.fov_map libtcod.map_compute_fov(rangemap, defn.player.x, defn.player.y, max_range, defn.FOV_LIGHT_WALLS, defn.FOV_ALGO) for y in range(defn.MAP_HEIGHT): for x in range(defn.MAP_WIDTH): if libtcod.map_is_in_fov(rangemap, x, y): libtcod.console_set_char_background(defn.con, x, y, defn.dungeon[x][y].color * libtcod.lightest_green, libtcod.BKGND_SET) while True: #render the screen. this erases the inventory and shows the names of objects under the mouse. libtcod.console_flush() libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS|libtcod.EVENT_MOUSE,defn.key,defn.mouse) game.render_all() (x, y) = (defn.mouse.cx, defn.mouse.cy) #accept the target if the player clicked in FOV, and in case a range is specified, if it's in that range if (defn.mouse.lbutton_pressed and libtcod.map_is_in_fov(defn.fov_map, x, y) and (max_range is None or defn.player.distance(x, y) <= max_range)): for j in range(defn.MAP_HEIGHT): for i in range(defn.MAP_WIDTH): if libtcod.map_is_in_fov(rangemap, i, j): libtcod.console_set_char_background(defn.con, i, j, defn.dungeon[i][j].color, libtcod.BKGND_SET) return (x, y) if defn.mouse.rbutton_pressed or defn.key.vk == libtcod.KEY_ESCAPE: for j in range(defn.MAP_HEIGHT): for i in range(defn.MAP_WIDTH): if libtcod.map_is_in_fov(rangemap, i, j): libtcod.console_set_char_background(defn.con, i, j, defn.dungeon[i][j].color, libtcod.BKGND_SET) return (None, None) #cancel if the player right-clicked or pressed Escape
def render_all(): global fov_map, color_dark_wall, color_light_wall, color_dark_ground, color_light_ground global fov_recompute, hunger, msg_index x_range = range(camera.x, camera.x + CAMERA_WIDTH) y_range = range(camera.y, camera.y + CAMERA_HEIGHT) if fov_recompute: # recompute FOV if need be (player movement or whatever) fov_recompute = False if 'fog' in map[player.x][player.y].mod_set: libtcod.map_compute_fov(fov_map, player.x, player.y, FOG_TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO) else: libtcod.map_compute_fov(fov_map, player.x, player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO) for y in y_range: for x in x_range: # uncomment this to return to exploration mode #visible = libtcod.map_is_in_fov(fov_map, x, y) visible = True wall = map[x][y].block_sight mods = map[x][y].mod_set if not visible: if map[x][y].explored: if wall: libtcod.console_set_char_background(con, x-camera.x, y-camera.y, color_dark_wall, libtcod.BKGND_SET) else: libtcod.console_set_char_background(con, x-camera.x, y-camera.y, color_dark_ground, libtcod.BKGND_SET) else: libtcod.console_set_char_background(con, x-camera.x, y-camera.y, libtcod.black, libtcod.BKGND_SET) else: if wall: #libtcod.console_set_default_foreground(con, libtcod.white) libtcod.console_set_char_background(con, x-camera.x, y-camera.y, color_light_wall, libtcod.BKGND_SET) else: # THIS ELIF IS FOR A DEBUG COLOR if map[x][y].h == 1: libtcod.console_set_char_background(con, x-camera.x, y-camera.y, color_blood, libtcod.BKGND_SET) else: libtcod.console_set_char_background(con, x-camera.x, y-camera.y, color_light_ground, libtcod.BKGND_SET) map[x][y].explored = True for obj in objects: # prevents drawing over the player if obj != player and (obj.x in x_range) and (obj.y in y_range): libtcod.console_set_default_foreground(con, obj.color) libtcod.console_put_char(con, obj.x-camera.x, obj.y-camera.y, obj.char, libtcod.BKGND_NONE) libtcod.console_set_default_foreground(con, player.color) libtcod.console_put_char(con, player.x-camera.x, player.y-camera.y, player.char, libtcod.BKGND_NONE) libtcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0)
def play_game(): global Key, Mouse, Fov_Recompute, Fov_Map, Message_Panel, Stats_Panel global Map_Objects Player_Action = None Mouse = libtcod.Mouse() Key = libtcod.Key() while not libtcod.console_is_window_closed(): libtcod.sys_check_for_event( libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, Key, Mouse) if Fov_Recompute: Fov_Recompute = False libtcod.map_compute_fov(Fov_Map, Player.x, Player.y, consts.TORCH_RADIUS, consts.FOV_LIGHT_WALLS, consts.FOV_ALGORITHM) render_all(Console, Stats_Panel, Message_Panel, Mouse, Fov_Map, Player, Map_Objects) libtcod.console_flush() for obj in Map_Objects: obj.clear(Console) libtcod.console_set_default_foreground(Console, libtcod.white) check_level_up() Player_Action = handle_keys(Console, Key, Player, Map_Objects, Message_Panel) if Player_Action == 'exit': save_game() break if Game_State == 'playing' and Player_Action != 'didnt-take-turn': for obj in Map_Objects: if obj.ai is not None: obj.ai.take_turn(Fov_Map, Map_Tiles, Map_Objects, Message_Panel, Player)
def render_all(): global fov_map, color_dark_wall, color_light_wall global color_dark_ground, color_light_ground global fov_recompute if fov_recompute: fov_recompute = False libtcod.map_compute_fov(fov_map, player.x, player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO) for y in range(MAP_HEIGHT): for x in range(MAP_WIDTH): visible = libtcod.map_is_in_fov(fov_map, x, y) tile = map[x][y].sort if not visible: if map[x][y].explored: if tile == 'wall': libtcod.console_set_back(con, x, y, color_dark_wall, libtcod.BKGND_SET) elif tile == 'metal1': libtcod.console_set_back(con, x, y, color_dark_metal1, libtcod.BKGND_SET) elif tile == 'metal2': libtcod.console_set_back(con, x, y, color_dark_metal2, libtcod.BKGND_SET) else: libtcod.console_set_back(con, x, y, color_dark_ground, libtcod.BKGND_SET) else: if tile == 'wall': libtcod.console_set_back(con, x, y, color_light_wall, libtcod.BKGND_SET) elif tile == 'metal1': libtcod.console_set_back(con, x, y, color_light_metal1, libtcod.BKGND_SET) elif tile == 'metal2': libtcod.console_set_back(con, x, y, color_light_metal2, libtcod.BKGND_SET) else: libtcod.console_set_back(con, x, y, color_light_ground, libtcod.BKGND_SET) map[x][y].explored = True for object in objects: if object != player: object.draw() player.draw() libtcod.console_blit(con, 0, 0, MAP_WIDTH, MAP_HEIGHT, 0, 0, 0) libtcod.console_set_background_color(panel, libtcod.black) libtcod.console_clear(panel) y = 1 for (line, color) in game_msgs: libtcod.console_set_foreground_color(panel, color) libtcod.console_print_left(panel, MSG_X, y, libtcod.BKGND_NONE, line) y += 1 render_bar(1, 1, BAR_WIDTH, 'John\'s Oxygen', player.spaceman.oxygen, player.spaceman.max_oxygen, libtcod.light_red, libtcod.darker_red) render_bar(1, 3, BAR_WIDTH, 'Adam\'s Oxygen', npc.spaceman.oxygen, npc.spaceman.max_oxygen, libtcod.light_magenta, libtcod.darker_magenta) libtcod.console_set_foreground_color(panel, libtcod.light_gray) libtcod.console_print_left(panel, 1, 0, libtcod.BKGND_NONE, get_names_under_mouse()) libtcod.console_blit(panel, 0, 0, SCREEN_WIDTH, PANEL_HEIGHT, 0, 0, PANEL_Y) for object in objects: object.clear()
def render_all(): global fov_map, color_dark_wall, color_light_wall global color_dark_ground, color_light_ground global fov_recompute if fov_recompute: # recompute FOV if needed (the player moved or something) fov_recompute = False libtcod.map_compute_fov(fov_map, player.x, player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO) # go through all tiles, and set their background color according to the FOV for y in range(MAP_HEIGHT): for x in range(MAP_WIDTH): visible = libtcod.map_is_in_fov(fov_map, x, y) wall = map[x][y].block_sight if not visible: # if it's not visible right now, the player can only see it if it's explored if map[x][y].explored: if wall: libtcod.console_set_char_background(con, x, y, color_dark_wall, libtcod.BKGND_SET) else: libtcod.console_set_char_background(con, x, y, color_dark_ground, libtcod.BKGND_SET) else: # it's visible if wall: libtcod.console_set_char_background(con, x, y, color_light_wall, libtcod.BKGND_SET) else: libtcod.console_set_char_background(con, x, y, color_light_ground, libtcod.BKGND_SET) # since it's visible, explore it map[x][y].explored = True # draw all objects in the list, except the player. we want it to # always appear over all other objects! so it's drawn later. for object in objects: if object != player: object.draw() player.draw() # blit the contents of "con" to the root console libtcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0) # prepare to render the GUI console libtcod.console_set_default_background(panel, libtcod.black) libtcod.console_clear(panel) # show the player's stats render_bar(1, 1, BAR_WIDTH, "HP", player.fighter.hp, player.fighter.max_hp, libtcod.light_red, libtcod.darker_red) # print the game messages, one line at a time y = 1 for (line, color) in game_msgs: libtcod.console_set_default_foreground(panel, color) libtcod.console_print_ex(panel, MSG_X, y, libtcod.BKGND_NONE, libtcod.LEFT, line) y += 1 libtcod.console_set_default_foreground(panel, libtcod.light_gray) libtcod.console_print_ex(panel, 1, 0, libtcod.BKGND_NONE, libtcod.LEFT, get_names_under_mouse()) # blit GUI to screen libtcod.console_blit(panel, 0, 0, SCREEN_WIDTH, PANEL_HEIGHT, 0, 0, PANEL_Y)
def cast_fireball(Game, user): (x,y) = (None, None) target = None target = closest_nonclan(data.TORCH_RADIUS, Game, user) if user is Game.player: #ask the player for a target tile to throw a fireball at message('Left-click a target tile for the fireball. Right-Click or ESC to cancel', Game, libtcod.light_cyan) (x,y) = target_tile(Game) #otherwise this is a mob elif target: if libtcod.map_is_in_fov(user.fighter.fov, target.x, target.y) and target.dungeon_level == user.dungeon_level: (x,y) = (target.x, target.y) if x is None or y is None: if user is Game.player: message('Cancelling fireball', Game, libtcod.red) else: message(user.name + ' cancels Fireball', Game, libtcod.red, False) return data.STATE_CANCELLED else: theDmg = roll_dice([[data.FIREBALL_DAMAGE/2, data.FIREBALL_DAMAGE*2]])[0] #create fireball fov based on x,y coords of target fov_map_fireball = Game.map[Game.dungeon_levelname].fov_map libtcod.map_compute_fov(fov_map_fireball, x, y, data.FIREBALL_RADIUS, data.FOV_LIGHT_WALLS, data.FOV_ALGO) for obj in Game.objects[Game.dungeon_levelname]: #damage all fighters within range if libtcod.map_is_in_fov(fov_map_fireball, obj.x, obj.y) and obj.fighter: message('The fireball explodes', Game, libtcod.orange) message(obj.name + ' is burned for '+ str(theDmg) + ' HP', Game, libtcod.orange) obj.fighter.take_damage(user, theDmg, Game)
def can_see(self, x, y, z): e = self.owner # Break out early if we are not on the same level if not e.z == z: return False lbt.map_compute_fov(self.fov_map, e.x, e.y, 10) return lbt.map_is_in_fov(self.fov_map, x, y)
def render_all(): libtcod.map_compute_fov(path_map, player.x, player.y, 0, True, libtcod.FOV_SHADOW) libtcod.console_set_background_color(map_con, libtcod.black) libtcod.console_clear(map_con) for y in range(MAP_HEIGHT): for x in range(MAP_WIDTH): if not libtcod.map_is_in_fov(path_map, x, y): if explore_map[x][y]: libtcod.console_set_back(map_con, x, y, tile_map[x][y].hidden_color, libtcod.BKGND_SET) else: libtcod.console_set_background_color(map_con, tile_map[x][y].vis_color) libtcod.console_set_foreground_color(map_con, tile_map[x][y].char_color) libtcod.console_put_char(map_con, x, y, tile_map[x][y].char, libtcod.BKGND_SET) explore_map[x][y] = True entities = items + creatures for entity in entities: entity.sprite.render() render_panel() render_inventory() blit_consoles()
def draw_console(player): """ Refreshes the map display and blits to the window. Sets or clears player.endangered. """ global _con current_map = player.current_map if current_map.is_outdoors and current_map.fov_elevation_changed: current_map.set_fov_elevation(player) current_map.fov_elevation_changed = False if current_map.fov_needs_recompute: libtcod.map_compute_fov(player.current_map.fov_map, player.x, player.y, config.TORCH_RADIUS, config.FOV_LIGHT_WALLS, config.FOV_ALGO) # Redraw if FOV (could have) changed. if current_map.is_outdoors: _draw_outdoors(player) else: _draw_indoors(player) # Draw all objects in the list, except the player. We want it to # always appear over all other objects, so it's drawn later. # (Could also achieve this by guaranteeing the player is always # the last object in current_map.objects.) for object in player.visible_objects: _draw_object(player, object) _draw_object(player, player) libtcod.console_blit(_con, 0, 0, config.MAP_PANEL_WIDTH, config.MAP_PANEL_HEIGHT, 0, 0, 0)
def recompute(self): if self.need_recompute: self.need_recompute = False pos = self.player.components['position'] libtcod.map_compute_fov(self.fov_map, pos.x, pos.y, self.torch_radius, self.fov_light_walls, self.fov_algo)
def render_all(): global color_dark_wall global color_dark_ground global color_light_ground global color_light_wall global fov_recompute if fov_recompute: fov_recompute = False libtcod.map_compute_fov(fov_map, player.x, player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO) for y in range(MAP_HEIGHT): for x in range(MAP_WIDTH): visible = libtcod.map_is_in_fov(fov_map, x, y) wall = map[x][y].block_sight if not visible: if map[x][y].explored: if wall: libtcod.console_set_char_background( con, x, y, color_dark_wall, libtcod.BKGND_SET) else: libtcod.console_set_char_background( con, x, y, color_dark_ground, libtcod.BKGND_SET) else: if wall: libtcod.console_set_char_background( con, x, y, color_light_wall, libtcod.BKGND_SET) else: libtcod.console_set_char_background( con, x, y, color_light_ground, libtcod.BKGND_SET) map[x][y].explored = True for object in objects: if object != player: object.draw() player.draw() libtcod.console_set_default_background(panel, libtcod.black) libtcod.console_clear(panel) y = 1 for (line, color) in game_msgs: libtcod.console_set_default_foreground(panel, color) libtcod.console_print_ex(panel, MSG_X, y, libtcod.BKGND_NONE, libtcod.LEFT, line) y += 1 render_bar(1, 1, BAR_WIDTH, 'HP', player.fighter.hp, player.fighter.max_hp, libtcod.light_red, libtcod.darker_red) libtcod.console_set_default_foreground(panel, libtcod.light_gray) libtcod.console_print_ex(panel, 1, 0, libtcod.BKGND_NONE, libtcod.LEFT, get_names_under_mouse()) libtcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0) libtcod.console_blit(panel, 0, 0, SCREEN_WIDTH, PANEL_HEIGHT, 0, 0, PANEL_Y)
def update_fov(self): """ Calculates the Field of Vision from the dungeon_map. """ x, y = self.parent.position.value sight_radius = self.parent.sight_radius.value libtcod.map_compute_fov(self.dungeon_map, x, y, sight_radius, True) self.last_sight_radius = sight_radius
def get_tiles_in_fov(self): libtcod.map_compute_fov(self.fov_map, self.player.x, self.player.y, self.fov_size, True, 0) return list( filter( lambda tile: libtcod.map_is_in_fov(self.fov_map, tile.x, tile.y ), self.currentMap.get_map_list()))
def map_calculate_fov(): global FOV_CALCULATE if INFORMATION.fov: INFORMATION.fov = False rogue.map_compute_fov(FOV_MAP, PLAYER.x, PLAYER.y, constants.TORCH_RADIUS, constants.FOV_LIGHT_WALLS, constants.FOV_ALGO)
def compute_fov(self, dungeonmap, xy, radius ): self.start_xy = xy - Pos(radius, radius) self.radius = radius # Copy visiblity information self._copy_from_dungeonmap(dungeonmap) fov_xy = xy - self.start_xy libtcod.map_compute_fov(self.map, fov_xy.x, fov_xy.y, radius, True, libtcod.FOV_PERMISSIVE_4)
def render_all(): global fov_map, color_dark_wall, color_light_wall global color_dark_ground, color_light_ground global fov_recompute #go through all the tiles and set their background color if fov_recompute: fov_recompute = False libtcod.map_compute_fov(fov_map, player.x, player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO) for y in range(MAP_HEIGHT): for x in range(MAP_WIDTH): visible = libtcod.map_is_in_fov(fov_map, x, y) wall = map[x][y].block_sight if not visible: #its waiting to be explored if map[x][y].explored: if wall: libtcod.console_set_char_background(con, x, y, color_dark_wall, libtcod.BKGND_SET) else: libtcod.console_set_char_background(con,x,y,color_dark_ground,libtcod.BKGND_SET) else: #its visiable if wall: libtcod.console_set_char_background(con, x, y, color_light_wall, libtcod.BKGND_SET) else: libtcod.console_set_char_background(con, x, y, color_light_ground, libtcod.BKGND_SET) map[x][y].explored = True #draw all objects in the list for object in objects: if object != player: object.draw() player.draw() libtcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0) libtcod.console_set_default_background(panel, libtcod.black) libtcod.console_clear(panel) y = 1 for (line, color) in game_msgs: libtcod.console_set_default_foreground(panel, color) libtcod.console_print_ex(panel, MSG_X, y, libtcod.BKGND_NONE, libtcod.LEFT, line) y += 1 render_bar(1,1, BAR_WIDTH, 'HP', player.fighter.hp, player.fighter.max_hp, libtcod.light_red, libtcod.darker_red) libtcod.console_blit(panel, 0, 0, SCREEN_WIDTH, PANEL_HEIGHT, 0, 0, PANEL_Y) libtcod.console_set_default_foreground(con, libtcod.white) libtcod.console_print_ex(0,1, SCREEN_HEIGHT -2, libtcod.BKGND_NONE,libtcod.LEFT, 'HP: ' + str(player.fighter.hp) + '/' + str(player.fighter.max_hp)) libtcod.console_set_default_foreground(panel, libtcod.light_gray) libtcod.console_print_ex(panel, 1, 0, libtcod.BKGND_NONE, libtcod.LEFT, get_name_under_mouse()) libtcod.console_blit(panel, 0, 0, SCREEN_WIDTH, PANEL_HEIGHT, 0, 0, PANEL_Y)
def render_all(): global fov_recompute for y in range(MAP_HEIGHT): for x in range(MAP_WIDTH): visible = libtcod.map_is_in_fov(fov_map, x, y) wall = map[x][y].block_sight if not visible: if map[x][y].explored: if wall: libtcod.console_put_char_ex(con, x, y, wall_tile, libtcod.grey, color_dark_ground) else: libtcod.console_set_char_background(con, x, y, color_dark_ground, libtcod.BKGND_SET) else: if wall: libtcod.console_put_char_ex(con, x, y, wall_tile, libtcod.white, color_light_ground) else: libtcod.console_set_char_background(con, x, y, color_light_ground, libtcod.BKGND_SET) map[x][y].explored = True for object in objects: if object != player: object.draw() player.draw() libtcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0) if fov_recompute: fov_recompute = False libtcod.map_compute_fov(fov_map, player.x, player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO) libtcod.console_set_default_foreground(con, libtcod.white) libtcod.console_set_default_background(panel, libtcod.black) libtcod.console_clear(panel) #messages y = 1 for (line, color) in game_msgs: libtcod.console_set_default_foreground(panel, color) libtcod.console_print_ex(panel, MSG_X, y, libtcod.BKGND_NONE, libtcod.LEFT, line) y += 1 #health bar render_bar(1, 1, BAR_WIDTH, 'HP', player.fighter.hp, player.fighter.max_hp, libtcod.light_red, libtcod.darker_red) #mana bar render_bar(1, 3, BAR_WIDTH, 'Mana', player.fighter.mana, player.fighter.max_mana, libtcod.light_blue, libtcod.darker_blue) #current dungeon level libtcod.console_print_ex(panel, 1, 5, libtcod.BKGND_NONE, libtcod.LEFT, 'Dungeon level ' + str(dungeon_level)) #names of objects under mouse libtcod.console_set_default_foreground(panel, libtcod.light_gray) libtcod.console_print_ex(panel, 1, 0, libtcod.BKGND_NONE, libtcod.LEFT, get_names_under_mouse()) libtcod.console_blit(panel, 0, 0, SCREEN_WIDTH, PANEL_HEIGHT, 0, 0, PANEL_Y)
def lights_recompute(): gamemap.brightness = dict() for light in lights: libtcod.map_compute_fov(myfov.fovmap, light.x, light.y, light.radius, False, myfov.algo) for x in range(light.x-light.radius,light.x+light.radius): for y in range(light.y-light.radius,light.y+light.radius): if libtcod.map_is_in_fov(myfov.fovmap,x,y): gamemap.addBrightness(x,y,light.getBrightness(x,y)) gamemap.colors[(x,y)] = light.color # change this later
def render_all(): global fov_recompute, fov_map #draw all objects in the list if fov_recompute: #recompute FOV if needed (the player moved or something) fov_recompute = False libtcod.map_compute_fov(fov_map, player.x, player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO) for y in range(map.h): for x in range(map.w): if (fov): visible = libtcod.map_is_in_fov(fov_map, x, y) else: visible = True wall = map.at(x, y).block_sight if not visible: if map.at(x, y).explored or not fov: if wall: libtcod.console_put_char_ex(con, x, y, '#', color_dark_wall, libtcod.black) else: libtcod.console_put_char_ex(con, x, y, '.', color_dark_ground, libtcod.black) else: map.at(x, y).explored = True if wall: libtcod.console_put_char_ex(con, x, y, '#', color_light_wall, BACK_COLOR) else: libtcod.console_put_char_ex(con, x, y, '.', color_light_ground, BACK_COLOR) for object in map.objects: if not object.fighter: object.draw(fov_map, fov) for object in map.objects: #combat objects on top if object.fighter: # (they cant occupy the same square) object.draw(fov_map, fov) libtcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0) #prepare to render the GUI panel libtcod.console_set_default_background(panel, libtcod.black) libtcod.console_clear(panel) #show the player's stats render_bar(1, 1, BAR_WIDTH, 'HP', player.fighter.hp, player.fighter.max_hp, libtcod.light_red, libtcod.darker_red) #display names of objects under the mouse libtcod.console_set_default_foreground(panel, libtcod.light_gray) libtcod.console_print_ex(panel, 1, 0, libtcod.BKGND_NONE, libtcod.LEFT, get_names_under_mouse()) #blit the contents of "panel" to the root console libtcod.console_blit(panel, 0, 0, SCREEN_WIDTH, PANEL_HEIGHT, 0, 0, PANEL_Y)
def refresh_fov_map(self): if self.fov_map is None: return libtcod.map_compute_fov(self.fov_map, self.x, self.y, self.torch_radius, True, 0)
def fov_map(max_range, Game, dude): #create fovmap for this dude fov_map_dude = libtcod.map_new(data.MAP_WIDTH, data.MAP_HEIGHT) for yy in range(data.MAP_HEIGHT): for xx in range(data.MAP_WIDTH): libtcod.map_set_properties(fov_map_dude, xx, yy, not Game.map[Game.dungeon_levelname].block_sight(xx, yy), not Game.map[Game.dungeon_levelname].blocked(xx, yy)) libtcod.map_compute_fov(fov_map_dude, dude.x, dude.y, max_range, data.FOV_LIGHT_WALLS, data.FOV_ALGO) return fov_map_dude
def render_all(): global color_dark_wall, color_light_wall global color_dark_ground, color_light_ground global fov_map, fov_recompute libtcod.console_set_default_foreground(con, libtcod.white) if fov_recompute: fov_recompute = False libtcod.map_compute_fov(fov_map, player.x, player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO) for y in range(MAP_HEIGHT): for x in range(MAP_WIDTH): visible = libtcod.map_is_in_fov(fov_map, x, y) wall = smap[x][y].block_sight if not visible: if smap[x][y].explored: if wall: libtcod.console_set_char_background(con, x, y, color_dark_wall, libtcod.BKGND_SET) else: libtcod.console_set_char_background(con, x, y, color_dark_ground, libtcod.BKGND_SET) else: if wall: libtcod.console_set_char_background(con, x, y, color_light_wall, libtcod.BKGND_SET) else: libtcod.console_set_char_background(con, x, y, color_light_ground, libtcod.BKGND_SET) smap[x][y].explored = True # draw junk, making player last for object in objects: if object != player: object.draw() player.draw() libtcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0) libtcod.console_set_default_foreground(con, libtcod.white) # Prep GUI Panel libtcod.console_set_default_background(panel, libtcod.black) libtcod.console_clear(panel) # Print messages y = 1 for (line, color) in game_msgs: libtcod.console_set_default_foreground(panel, color) libtcod.console_print_ex(panel, MSG_X, y, libtcod.BKGND_NONE, libtcod.LEFT, line) y += 1 # Show player stats render_bar(1, 1, BAR_WIDTH, "HP", player.fighter.hp, player.fighter.max_hp, libtcod.light_red, libtcod.darker_red) # Display names of objects under mouse libtcod.console_set_default_foreground(panel, libtcod.light_gray) libtcod.console_print_ex(panel, 1, 0, libtcod.BKGND_NONE, libtcod.LEFT, get_names_under_mouse()) # blit shit libtcod.console_blit(panel, 0, 0, SCREEN_WIDTH, PANEL_HEIGHT, 0, 0, PANEL_Y)
def playerReveal(self): sight=15 libtcod.map_compute_fov(self.blockedMap,self.player.x,self.player.y,sight,True) self.level[self.player.x][self.player.y].gotSeen() wx=cfg.WID2 wy=cfg.HGT2 for x in xrange(self.player.x-wx,self.player.x+wx): for y in xrange(self.player.y-wy,self.player.y+wy): if libtcod.map_is_in_fov(self.blockedMap, x, y): self.level[x][y].gotSeen()
def lights_recompute(): gamemap.brightness = dict() for light in lights: libtcod.map_compute_fov(myfov.fovmap, light.x, light.y, light.radius, False, myfov.algo) for x in range(light.x - light.radius, light.x + light.radius): for y in range(light.y - light.radius, light.y + light.radius): if libtcod.map_is_in_fov(myfov.fovmap, x, y): gamemap.addBrightness(x, y, light.getBrightness(x, y)) gamemap.colors[(x, y)] = light.color # change this later
def player_can_see(x, y): import player global fov_recompute, player_fov_calculated if not player_fov_calculated: libtcod.map_compute_fov(fov_player, player.instance.x, player.instance.y, consts.TORCH_RADIUS, consts.FOV_LIGHT_WALLS, consts.FOV_ALGO) player_fov_calculated = True return libtcod.map_is_in_fov(fov_player, x, y)
def get_fov(self, coords, map, distance): map_fov = libtcod.map_new(MAP_WIDTH, MAP_HEIGHT) for y in range(MAP_HEIGHT): for x in range(MAP_WIDTH): libtcod.map_set_properties(map_fov, x, y, not map.cells[y][x].block_path, not map.cells[y][x].block_path) libtcod.map_compute_fov(map_fov, coords[0], coords[1], distance, True, not libtcod.FOV_BASIC) return map_fov
def render_all(): global game_msgs, fov_recompute, dis_x, dis_y #draw all the map if fov_recompute: fov_recompute = False libtcod.map_compute_fov(fov_map, player.x, player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO) for y in range(MAP_HEIGHT): for x in range(MAP_WIDTH): visible = libtcod.map_is_in_fov(fov_map, x, y) if visible: libtcod.console_set_char_background(con, x, y, libtcod.darkest_grey, libtcod.BKGND_SET) map[x][y].draw(x,y) else: libtcod.console_set_char_background(con, x, y, libtcod.black, libtcod.BKGND_SET) libtcod.console_put_char(con, x, y, ' ', libtcod.BKGND_NONE) #draw all objects in the list for object in objects: if object != player: object.draw() player.draw() fov_recompute = True #blit the contents of "con" to the root console and present it #calculate the player point dis_x = player.x - DISPLAY_WIDTH / 2 dis_y = player.y - DISPLAY_HEIGHT / 2 if dis_x < 0: dis_x = 0 elif dis_x > MAP_WIDTH - DISPLAY_WIDTH: dis_x = MAP_WIDTH - DISPLAY_WIDTH if dis_y < 0: dis_y = 0 elif dis_y > MAP_HEIGHT - DISPLAY_HEIGHT: dis_y = MAP_HEIGHT - DISPLAY_HEIGHT libtcod.console_blit(con, dis_x, dis_y, DISPLAY_WIDTH, DISPLAY_HEIGHT, 0, 0, 0) libtcod.console_set_default_background(panel, libtcod.black) libtcod.console_clear(panel) #show the player's stats render_bar(1, 1, BAR_WIDTH, 'HP', player.fighter.hp, player.fighter.max_hp, libtcod.light_red, libtcod.darker_red) #show message right the bar y = 1 for (line, color) in game_msgs: libtcod.console_set_default_foreground(panel, color) libtcod.console_print_ex(panel, MSG_X, y, libtcod.BKGND_NONE, libtcod.LEFT, line) y += 1 libtcod.console_blit(panel, 0 , 0, SCREEN_WIDTH, PANEL_HEIGHT, 0 ,0, PANEL_Y) #show message at panel2 panel2_display()
def render_all(): """ Renders everything on screen. """ # Set globals. global fov_recompute # Clear root console. roguelib.console_clear(0) # Display mouse location if in FOV. mouse, key = check_for_key_events() if roguelib.map_is_in_fov(fov_map, mouse.cx, mouse.cy): roguelib.console_put_char_ex( 0, mouse.cx, mouse.cy, " ", None, eval(SETTING_DATA[b"Mouse Highlight"][b"COLOR"])) # Display objects. for obj in objects: visible = roguelib.map_is_in_fov(fov_map, obj.x, obj.y) # Out of FOV. if not visible: if obj.explored: roguelib.console_set_default_foreground(0, obj.color[1]) roguelib.console_put_char(0, obj.x, obj.y, obj.image, roguelib.BKGND_NONE) # In FOV. else: roguelib.console_set_default_foreground(0, obj.color[0]) roguelib.console_put_char(0, obj.x, obj.y, obj.image, roguelib.BKGND_NONE) obj.explored = True # Recompute FOV if fov_recompute is True. if fov_recompute: fov_recompute = False roguelib.map_compute_fov(fov_map, player.x, player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO) # Display player. roguelib.console_set_default_foreground(0, roguelib.white) roguelib.console_put_char(0, player.x, player.y, player.image, roguelib.BKGND_NONE) # Render the gui. render_gui() # Flush the root console. roguelib.console_flush()
def render_all(): global color_light_wall, color_dark_wall global color_light_ground, color_dark_ground global fov_map, fov_recompute if fov_recompute: fov_recompute = False libtcod.map_compute_fov(fov_map, player.x, player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO) #go through all tiles, and set their background color for y in range(MAP_HEIGHT): for x in range(MAP_WIDTH): visible = libtcod.map_is_in_fov(fov_map, x, y) wall = map[x][y].block_sight if not visible: #out of fov if map[x][y].explored: if wall: libtcod.console_put_char_ex(con,x,y,wall_tile,libtcod.grey, libtcod.black) else: libtcod.console_put_char_ex(con,x,y,floor_tile,libtcod.grey, libtcod.black) else: #in fov if wall: libtcod.console_put_char_ex(con,x,y,wall_tile,libtcod.white, libtcod.black) else: libtcod.console_put_char_ex(con,x,y,floor_tile,libtcod.white, libtcod.black) map[x][y].explored = True #draw all objects in the list for object in objects: if object != player: object.draw() player.draw() #blit the contents of "con" to the root console libtcod.console_blit(con, 0, 0, MAP_WIDTH, MAP_HEIGHT, 0, 0, 0) #prepare to render the GUI panel libtcod.console_set_default_background(panel, libtcod.black) libtcod.console_clear(panel) #print the messages one line at a time y = 1 for (line, color) in game_msgs: libtcod.console_set_default_foreground(panel, color) libtcod.console_print_ex(panel, MSG_X, y, libtcod.BKGND_NONE, libtcod.LEFT, line) y += 1 #show player stats render_bar(1, 1, BAR_WIDTH, 'HP', player.fighter.hp, player.fighter.max_hp, libtcod.light_red, libtcod.darker_red) #display name of objects under the mouse libtcod.console_set_default_foreground(panel, libtcod.light_grey) libtcod.console_print_ex(panel, 1, 0, libtcod.BKGND_NONE, libtcod.LEFT, get_names_under_mouse()) #blit the contents of "panel" to the root console libtcod.console_blit(panel,0,0,SCREEN_WIDTH,PANEL_HEIGHT,0,0,PANEL_Y)
def render_local(): global map_, fov_recompute if len(R.map_) > R.MAP_VIEW_WIDTH: cam_x = scrolling_map(you.x, R.MAP_VIEW_WIDTH_HALF + 1, R.MAP_VIEW_WIDTH, R.MAP_WIDTH) else: cam_x = 0 if len(R.map_[0]) > R.MAP_VIEW_HEIGHT: cam_y = scrolling_map(you.y, R.MAP_VIEW_HEIGHT_HALF, R.MAP_VIEW_HEIGHT, R.MAP_HEIGHT) else: cam_y = 0 # cam_x = scrolling_map(you.x, R.MAP_VIEW_WIDTH / 2, R.MAP_VIEW_WIDTH, R.MAP_WIDTH) # cam_y = scrolling_map(you.y, R.MAP_VIEW_HEIGHT / 2, R.MAP_VIEW_HEIGHT, R.MAP_HEIGHT) if fov_recompute: fov_recompute = False libtcod.map_compute_fov(R.locale.floors[you.depth].fov_map, you.x, you.y, 10, True, 0) for sc_y in range(R.MAP_VIEW_HEIGHT): #this refers to the SCREEN position. NOT map. for sc_x in range(R.MAP_VIEW_WIDTH): x = sc_x + cam_x y = sc_y + cam_y if sc_x < len(R.map_) and sc_y < len(R.map_[0]): # and x < len(R.map_) and y < len(R.map_[0]): #if it's within the bounds of the map. tile = R.locale.floors[you.depth].tiles[x][y] visible = libtcod.map_is_in_fov(R.locale.floors[you.depth].fov_map, x, y) if not visible: if tile.explored or debug_mode: libtcod.console_put_char_ex(con, x, y, tile.char, libtcod.dark_green, libtcod.dark_gray) else: libtcod.console_put_char_ex(con, x, y, " ", libtcod.black, libtcod.black) libtcod.console_set_char(con_char, x, y, " ") else: libtcod.console_put_char_ex(con, x, y, tile.char, libtcod.green, libtcod.light_grey) libtcod.console_set_char(con_char, x, y, " ") tile.explored = True else: libtcod.console_put_char_ex(con, x, y, " ", libtcod.black, libtcod.black) for objects in R.locale_obj: #if the tile is explored, then draw the object. if libtcod.map_is_in_fov(R.locale.floors[you.depth].fov_map, objects.x, objects.y): objects.draw(cam_x, cam_y) #if it's explored but out of sight range - draw faded! elif R.locale.floors[you.depth].tiles[objects.x][objects.y].explored == True: objects.draw_faded(cam_x, cam_y) you.draw(cam_x, cam_y) libtcod.console_blit(con, 0, 0, R.MAP_VIEW_WIDTH, R.MAP_VIEW_HEIGHT, 0, 0, 0) libtcod.console_blit(con_char, 0, 0, R.MAP_VIEW_WIDTH, R.MAP_VIEW_HEIGHT, 0, 0, 0, 1.0, 0.0) libtcod.console_blit(inf, 0, 0, R.INFO_BAR_WIDTH, R.SCREEN_HEIGHT, 0, R.MAP_VIEW_WIDTH, 0) libtcod.console_flush()
def processAllEntities(processor, player, entities, fov_map, mouse): """Runs all the entities through whatever processor is given""" actions = [] for entity in entities: actions += processor(entity, entities, fov_map, mouse) # NOTE: re-computing fov here for every entity. weird. but the reason is that # each entity has a chance to change the fov map (moving into / out of fov, creating shadows, affecting A*, etc) libtcod.map_compute_fov(fov_map, player['Position']['x'], player['Position']['y'], init.fov_radius, init.fov_light_walls, init.fov_algorithm) return actions
def playerReveal(self): sight = 15 libtcod.map_compute_fov(self.blockedMap, self.player.x, self.player.y, sight, True) self.level[self.player.x][self.player.y].gotSeen() wx = cfg.WID2 wy = cfg.HGT2 for x in xrange(self.player.x - wx, self.player.x + wx): for y in xrange(self.player.y - wy, self.player.y + wy): if libtcod.map_is_in_fov(self.blockedMap, x, y): self.level[x][y].gotSeen()
def recompute_fov(fov_map, x, y, radius, light_walls=True, algorithm=0): """ Recomputes the FOV using a libtcodpy algorithm. :param fov_map: The litcodpy map generated in initialise_fov(). :param x: Current player position. :param y: Current player position. :param radius: FOV radius. :param light_walls: Should the walls be lit? :param algorithm: Which libtcodpy algorithm to use. :return: """ libtcod.map_compute_fov(fov_map, x, y, radius, light_walls, algorithm)
def render_all(): global fov_map, color_dark_wall, color_light_wall global color_dark_ground, color_light_ground global fov_recompute, color_index1, color_index2, step if fov_recompute: #recompute FOV if needed (the player moved or something) fov_recompute = False libtcod.map_compute_fov(fov_map, player.x, player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO) #go through all tiles, and set their background color according to the FOV for y in range(MAP_HEIGHT): for x in range(MAP_WIDTH): visible = libtcod.map_is_in_fov(fov_map, x, y) wall = map[x][y].block_sight if not visible: #if it's not visible right now, the player can only see it if it's explored if map[x][y].explored: if wall: libtcod.console_set_char_background(con, x, y, color_dark_wall, libtcod.BKGND_SET) else: libtcod.console_set_char_background(con, x, y, color_dark_ground, libtcod.BKGND_SET) else: #it's visible if wall: libtcod.console_set_char_background(con, x, y, color_light_wall, libtcod.BKGND_SET ) else: libtcod.console_set_char_background(con, x, y, libtcod.Color(color_index2, 0, color_index1), libtcod.BKGND_SET ) #since it's visible, explore it map[x][y].explored = True # some fun color cycling with offset cosine waves. it cycles blue and red if color_index <= 255: step += .01 color_index1 = int( 255 / 8 *(math.cos(step) +1) ) color_index2 = int( 255 / 8 *(math.cos(step+3.14) +1) ) #draw all objects in the list, except the player. we want it to #always appear over all other objects! so it's drawn later. for object in objects: if object != player: object.draw() player.draw() #blit the contents of "con" to the root console libtcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0) #show the player's stats libtcod.console_set_default_foreground(con, libtcod.white) libtcod.console_print_ex(0, 1, SCREEN_HEIGHT - 2, libtcod.BKGND_NONE, libtcod.LEFT, 'HP: ' + str(player.fighter.hp) + '/' + str(player.fighter.max_hp)) libtcod.console_print_ex(0, SCREEN_WIDTH-2, 1, libtcod.BKGND_NONE, libtcod.RIGHT, 'hi there' + str(step) ) #playing around with placing text around the screen.
def monster_can_see_tile(monster, x, y): global player_fov_calculated if monster.elevation in fov_height.keys(): libtcod.map_compute_fov(fov_height[monster.elevation], monster.x, monster.y, consts.TORCH_RADIUS, consts.FOV_LIGHT_WALLS, consts.FOV_ALGO) player_fov_calculated = False set_fov_recompute() return libtcod.map_is_in_fov(fov_height[monster.elevation], x, y) else: return False # TODO: alternative here