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 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: if cell.block_sight and cell.explored: libtcod.console_put_char_ex(self.con, x, y, '#', libtcod.white, color) else: libtcod.console_set_char_background(self.con, x, y, color, libtcod.BKGND_SET)
def targeting_overlay(con, mouse, player, game_map, fov_map, colors, targeting_item): if targeting_item != None: targeting_range = targeting_item.item.targeting_range targeting_range_offset = int(targeting_range / 2) area_of_effect = targeting_item.item.area_of_effect area_of_effect_offset = int(area_of_effect / 2) else: targeting_range = player.equipment.main_hand.item.targeting_range targeting_range_offset = int(targeting_range / 2) area_of_effect = player.equipment.main_hand.item.area_of_effect area_of_effect_offset = int(area_of_effect / 2) for y in range(targeting_range): for x in range(targeting_range): if game_map.tiles[player.x-x+targeting_range_offset][player.y-y+targeting_range_offset].explored and \ libtcod.map_is_in_fov(fov_map, player.x-x+targeting_range_offset, player.y-y+targeting_range_offset) and not \ game_map.tiles[player.x-x+targeting_range_offset][player.y-y+targeting_range_offset].blocked: libtcod.console_put_char_ex( con, player.x - x + targeting_range_offset, player.y - y + targeting_range_offset, ' ', libtcod.green, libtcod.green) if libtcod.map_is_in_fov(fov_map, mouse.cx, mouse.cy): for y in range(area_of_effect): for x in range(area_of_effect): if game_map.tiles[mouse.cx-x+area_of_effect_offset][mouse.cy-y+area_of_effect_offset].explored and \ libtcod.map_is_in_fov(fov_map, mouse.cx-x+area_of_effect_offset, mouse.cy-y+area_of_effect_offset): libtcod.console_put_char_ex( con, mouse.cx - x + area_of_effect_offset, mouse.cy - y + area_of_effect_offset, ' ', libtcod.red, libtcod.red)
def render_all(con, panel, entities, player, game_map, message_log, screen_width, screen_height, bar_width, panel_height, panel_y): for y in range(game_map.height): for x in range(game_map.width): wall = game_map.tiles[x][y].blocked if wall: libtcod.console_put_char_ex(con, x, y, '"', libtcod.white, libtcod.black) else: libtcod.console_put_char_ex(con, x, y, '_', libtcod.gray, libtcod.black) entities_in_render_order = sorted(entities, key=lambda x: x.render_order.value) for entity in entities_in_render_order: draw_entity(con, entity, game_map) 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 message in message_log.messages: libtcod.console_set_default_foreground(panel, message.color) libtcod.console_print_ex(panel, message_log.x, y, libtcod.BKGND_NONE, libtcod.LEFT, message.text) y += 1 render_bar(panel, 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)
def DrawSquare(x,y,size,color): #x and y are upper left coordinates, size is side of square, color is color of square for a in range(size): for b in range(size): libtcod.console_put_char_ex(None,x+a,y+b,219,color,libtcod.white) return
def scrollbar(con, x, y, start, size, length, reverse=False): fore1 = libtcod.darkest_grey fore2 = libtcod.darkest_grey if reverse: if start > 0: fore2 = libtcod.white if start + size < length: fore1 = libtcod.white else: if start > 0: fore1 = libtcod.white if start + size < length: fore2 = libtcod.white libtcod.console_put_char_ex(con, x, y, chr(24), fore1, libtcod.black) libtcod.console_put_char_ex(con, x, y + size - 1, chr(25), fore2, libtcod.black) for i in range(size - 2): libtcod.console_put_char_ex(con, x, y + i + 1, chr(179), libtcod.darkest_grey, libtcod.black) if length - size > 0: step = float(size - 2) / float(length - size) bar = int(math.ceil(start * step)) if bar < 1 and start == 0: bar = 1 if bar > size - 2 and start + size == length: bar = size - 2 if reverse: libtcod.console_put_char_ex(con, x, y + (size - 1) - bar, chr(179), libtcod.white, libtcod.black) else: libtcod.console_put_char_ex(con, x, y + bar, chr(179), libtcod.white, libtcod.black)
def console_put_char_ex(self,con,x,y,chr,cr,cg,cb,br,bg,bb): fore = libtcod.Color(cr,cg,cb) back = libtcod.Color(br,bg,bb) if con ==0: libtcod.console_put_char_ex(con,x,y,chr,fore,back) else: libtcod.console_put_char_ex(self.mConsole[con-1],x,y,chr,fore,back)
def cast_fireball(): global fov_map message('Left-click a target tile for the fireball, or right-click to cancel.', libtcod.light_cyan) (x, y) = target_tile() if x is None: return 'cancelled' message('The fireball explodes, burning everything within ' + str(FIREBALL_RADIUS) + ' tiles!', libtcod.orange) for obj in objects: # damage every fighter in range, including the player if obj.distance(x, y) <= FIREBALL_RADIUS and obj.fighter: if libtcod.map_is_in_fov(fov_map, obj.x, obj.y): if obj == player: msg1, msg2 ='', '' else: msg1, msg2 = 'The ', 's' message(msg1 + obj.name.capitalize() + ' get' + msg2 +' burned for ' + str(FIREBALL_DAMAGE) + ' hit points.', libtcod.orange) else: message('You hear a cry of pain') obj.fighter.take_damage(FIREBALL_DAMAGE) render_all() # display code for num in range(3): for x1 in range(x - num, x + num): for y1 in range(y - num, y + num): if not map[x1][y1].block_sight and libtcod.map_is_in_fov(fov_map, x1, y1): libtcod.console_put_char_ex(0, x1, y1, '*', libtcod.dark_orange, libtcod.red) libtcod.console_flush() time.sleep(0.1) time.sleep(0.25)
def render_all(): # Go through all Tiles, and set their character and color. for y in range(MAP_HEIGHT): for x in range(MAP_WIDTH): # Draw it. libtcod.console_put_char_ex(con, x, y, map[x][y].char, map[x][y].color, libtcod.black) # 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) # Prepare to render the GUI panel. libtcod.console_set_default_background(panel, libtcod.black) libtcod.console_clear(panel) # Show the player's stats. libtcod.console_set_default_foreground(panel, libtcod.red) libtcod.console_print_ex(panel, 1, 1, libtcod.BKGND_NONE, libtcod.LEFT, 'Lives: ' + str(player.lives) + '/' + str(player.max_lives)) libtcod.console_set_default_foreground(panel, libtcod.white) libtcod.console_print_ex(panel, 1, 3, libtcod.BKGND_NONE, libtcod.LEFT, 'Level ' + str(level_number)) # Blit the contents of "panel" to the root console. libtcod.console_blit(panel, 0, 0, SCREEN_WIDTH, PANEL_HEIGHT, 0, 0, PANEL_Y)
def _draw_indoors(player): """ Overly optimized: this code inlines Map.terrain_at(), Map.is_explored(), and ScreenCoords.toWorldCoords() in order to get a 2.5x speedup on large maps. """ libtcod.console_clear(_con) current_map = player.current_map pos = algebra.Location(0, 0) for screen_y in range(min(current_map.height, config.MAP_PANEL_HEIGHT)): pos.set(player.camera_position.x, player.camera_position.y + screen_y) for screen_x in range(min(current_map.width, config.MAP_PANEL_WIDTH)): # pos = ScreenCoords.toWorldCoords(player.camera_position, (screen_x, screen_y)) visible = libtcod.map_is_in_fov(current_map.fov_map, pos.x, pos.y) # terrain = current_map.terrain_at(pos) terrain = map.terrain_types[current_map.terrain[pos.x][pos.y]] if not visible: # if current_map.is_explored(pos): if current_map._explored[pos.x][pos.y]: libtcod.console_set_char_background( _con, screen_x, screen_y, terrain.unseen_color, libtcod.BKGND_SET) else: libtcod.console_put_char_ex(_con, screen_x, screen_y, terrain.icon, terrain.icon_color, terrain.seen_color) current_map.explore(pos) pos.x += 1
def draw(self): assert len(self.percentiles) == len( self.fgcolours), "HBar not configured correctly" # calculate text s = "" if self.show_numerator: if self.show_denominator: s = "%d/%d" % (self.value, self.max_value) else: s = "%d" % (self.value) if self.text is not None: s = "%s %s" % (self.text, s) s = self.text_align(s, self.size) # draw bar fv = self.value / self.max_value fg_idx = 0 for i in range(0, self.size): #calculate colour for this character f = (i + 1) / self.size col = self.bgcolour if f <= fv: # part of bar fg while f > self.percentiles[fg_idx]: fg_idx += 1 assert fg_idx < len( self.percentiles), "HBar not configured correctly" col = self.fgcolours[fg_idx] libtcod.console_put_char_ex(0, self.pos.x + i, self.pos.y, s[i], libtcod.white, col)
def render_all(): global color_light_wall global color_light_ground map_rng = libtcod.random_new_from_seed(1, algo=libtcod.RNG_MT) #go through all tiles, and set their background color for y in range(MAP_HEIGHT): for x in range(MAP_WIDTH): wall = dungeon_map[x][y].block_sight if wall: libtcod.console_put_char_ex( con, x, y, WALL_CONST, color_dark_wall, libtcod.light_grey * libtcod.random_get_float(map_rng, 0.7, 0.8)) else: libtcod.console_put_char_ex( con, x, y, FLOOR_CONST, color_dark_ground, libtcod.dark_grey * libtcod.random_get_float(map_rng, 0.2, 0.5)) #draw all objects in the list for obj in objects: obj.draw() #blit the contents of "con" to the root console libtcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0)
def draw(self): libtcod.console_clear(self.console) player = filter(lambda ent: ent[1].get_attribute(AttributeTag.Player), self.entity_manager.entities.iteritems())[0][1] player_position = player.get_attribute(AttributeTag.WorldPosition).data['value'] position_delta = Vec2d(0, 0) cur_action_number = 1 for queued_action in self.actions: if queued_action.type == ActionTag.ProgramMovement: position_delta += queued_action.data['value'] draw_info = player.get_attribute(AttributeTag.DrawInfo) target_position = player_position + position_delta libtcod.console_put_char_ex(self.console, target_position.x, target_position.y, str(cur_action_number), libtcod.blue, libtcod.black) elif queued_action.type == ActionTag.DamagePosition: target_position = None if 'relative' in queued_action.data: target_position = player_position + position_delta + queued_action.data['relative'] else: #assume there's an absolute position listed. proibably should be generating a nice error here but that can be done soon(TM) target_position = queued_action.data['absolute'] libtcod.console_put_char_ex(self.console, target_position.x, target_position.y, str(cur_action_number), libtcod.dark_red, libtcod.black) cur_action_number += 1 if cur_action_number != 0 and cur_action_number == 10: cur_action_number = 0 libtcod.console_blit(self.console, 0, 0, self.width, self.height, 0, self.world_x_start, self.world_y_start)
def redrawAll(): # Handles refreshing the screen, drawing tiles, NPCs, player, GUI... # Clear map screen. libtcod.console_clear(mapConsole) libtcod.console_clear(topGuiConsole) libtcod.console_clear(bottomGuiConsole) # Redraw tiles to map. drawTiles() # Redraw objects to map. drawObjects() # Redraw player to map. libtcod.console_put_char_ex(mapConsole, player.x, player.y, player.image, player.foreground, libtcod.BKGND_NONE) # Redraw mouse highlights and info. drawMouse() # Redraw GUI. drawGUI() # Blit consoles to root and flush to init changes. libtcod.console_blit(mapConsole, 0, 0, mapWidth, mapHeight, 0, 0, topGuiHeight) libtcod.console_blit(topGuiConsole, 0, 0, topGuiWidth, topGuiHeight, 0, 0, 0) libtcod.console_blit(bottomGuiConsole, 0, 0, botGuiWidth, botGuiHeight, 0, 0, screenHeight-botGuiHeight) libtcod.console_flush()
def clear(self): # erase the character that represents this object if TRADITIONAL_LOOK: tcod.console_put_char_ex(con, self.x, self.y, '.', tcod.white, colour_dark_ground) else: tcod.console_put_char(con, self.x, self.y, ' ', tcod.BKGND_NONE)
def build(self, con): if self.get_unit().is_ready(): self.set_idle(self.name_slot) else: self.set_used(self.name_slot) # if self. i = 0 temp = libtcod.console_new(self.width, self.height) print len(self.content) for elem in self.content: elem.build(temp) libtcod.console_blit(temp, 0, 0, elem.width, elem.height, con, 1, i) elem.set_pos(1, i) libtcod.console_put_char_ex(con, 0, i, chr(26), libtcod.white, libtcod.black) libtcod.console_clear(temp) i += 1
def build(self,con): #build the menu itself libtcod.console_print_ex(con,self.width/2,0,libtcod.BKGND_NONE,libtcod.CENTER,self.name) i=1 temp=libtcod.console_new(self.width,self.height) for elem in self.content: #print elem #print "built" dh=elem.build(temp) dh=elem.height #updated from dh libtcod.console_blit(temp,0,0,elem.width,dh,con,1,i) elem.set_pos(1,i) # print 'elem placed at', elem.pos libtcod.console_clear(temp) libtcod.console_put_char_ex(con,0,i,chr(26),libtcod.white,libtcod.black) # libtcod.console_set_char_background(con, 0, i, libtcod.blue) i+=dh #---------------7Drl---------------------------------- if self.height<i+1: #Bricolage, to change later on self.height=i+1
def _draw_indoors(player): """ Overly optimized: this code inlines Map.terrain_at(), Map.is_explored(), and ScreenCoords.toWorldCoords() in order to get a 2.5x speedup on large maps. """ libtcod.console_clear(_con) current_map = player.current_map pos = algebra.Location(0, 0) for screen_y in range(min(current_map.height, config.MAP_PANEL_HEIGHT)): pos.set(player.camera_position.x, player.camera_position.y + screen_y) for screen_x in range(min(current_map.width, config.MAP_PANEL_WIDTH)): # pos = ScreenCoords.toWorldCoords(player.camera_position, (screen_x, screen_y)) visible = libtcod.map_is_in_fov(current_map.fov_map, pos.x, pos.y) # terrain = current_map.terrain_at(pos) terrain = map.terrain_types[current_map.terrain[pos.x][pos.y]] if not visible: # if current_map.is_explored(pos): if current_map._explored[pos.x][pos.y]: libtcod.console_set_char_background(_con, screen_x, screen_y, terrain.unseen_color, libtcod.BKGND_SET) else: libtcod.console_put_char_ex(_con, screen_x, screen_y, terrain.icon, terrain.icon_color, terrain.seen_color) current_map.explore(pos) pos.x += 1
def draw(self): libtcod.console_clear(self.console) player = filter(lambda ent: ent[1].get_attribute(AttributeTag.Player), self.entity_manager.entities.iteritems())[0][1] player_position = player.get_attribute( AttributeTag.WorldPosition).data['value'] position_delta = Vec2d(0, 0) cur_action_number = 1 for queued_action in self.actions: if queued_action.type == ActionTag.ProgramMovement: position_delta += queued_action.data['value'] draw_info = player.get_attribute(AttributeTag.DrawInfo) target_position = player_position + position_delta libtcod.console_put_char_ex(self.console, target_position.x, target_position.y, str(cur_action_number), libtcod.blue, libtcod.black) elif queued_action.type == ActionTag.DamagePosition: target_position = None if 'relative' in queued_action.data: target_position = player_position + position_delta + queued_action.data[ 'relative'] else: #assume there's an absolute position listed. proibably should be generating a nice error here but that can be done soon(TM) target_position = queued_action.data['absolute'] libtcod.console_put_char_ex(self.console, target_position.x, target_position.y, str(cur_action_number), libtcod.dark_red, libtcod.black) cur_action_number += 1 if cur_action_number != 0 and cur_action_number == 10: cur_action_number = 0 libtcod.console_blit(self.console, 0, 0, self.width, self.height, 0, self.world_x_start, self.world_y_start)
def draw(self): """Draw this map tile""" # NB. this gets called a lot! if not self.is_visible: return if self.visible_to_player: if self.map.player.has_effect(StatusEffect.INFRAVISION) and not self.remains_in_place: c = libtcod.white else: c = self.light_colour # this is slow l = libtcod.color_get_hsv(c)[2] # this is copied from .light_level for performance if self.map.player.has_effect(StatusEffect.NIGHT_VISION): l = 1.0 - l c = libtcod.white - c if l > LightSource.INTENSITY_L_CLAMP: colour = self.colour * c symbol = self.symbol else: if self.has_been_seen and self.remains_in_place: colour = self.unseen_colour symbol = self.unseen_symbol else: return else: if self.has_been_seen and self.remains_in_place: colour = self.unseen_colour symbol = self.unseen_symbol else: return libtcod.console_put_char_ex(0, self.pos.x, self.pos.y, symbol, colour, libtcod.BKGND_NONE) self.has_been_seen = True
def clear(self, back_color): #Set the whole panel to the default background libtcod.console_set_default_background(self.console, back_color) for y in range(self.height - 1): for x in range(self.width - 1): libtcod.console_put_char_ex(self.console, x, y, ' ', color_dark_wall, color_dark_bg)
def render_all(): """The main rendering function.""" for y in range(MAP_HEIGHT): for x in range(MAP_WIDTH): wall = Game.map[x][y].blocked #TODO create FOV fall-off if wall: libtcod.console_put_char_ex(Game.con, x, y, '#', color_wall_f, color_wall_b) else: libtcod.console_put_char_ex(Game.con, x, y, ' ', color_ground_f, color_ground_b) #draw all objects in the list, except the player that is drawn AFTER everything else for ent in Game.entities: if ent != Game.player: ent.draw() Game.player.draw() #blit the contents of "con" to the root console libtcod.console_blit(Game.con, 0, 0, SCREEN_WIDTH, MAP_HEIGHT, 0, 0, 2) libtcod.console_print_ex(Game.top_panel, SCREEN_WIDTH/2, 0, libtcod.BKGND_NONE, libtcod.CENTER, GAME_TITLE) libtcod.console_print_ex(Game.top_panel, SCREEN_WIDTH/2, 1, libtcod.BKGND_NONE, libtcod.CENTER, 'v.' + VERSION + ' by magikmw') libtcod.console_blit(Game.top_panel, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0) libtcod.console_print_ex(Game.bottom_panel, SCREEN_WIDTH/2, 0, libtcod.BKGND_NONE, libtcod.CENTER, "Stage 14") libtcod.console_print_ex(Game.bottom_panel, 0, 1, libtcod.BKGND_NONE, libtcod.LEFT, "Magikmw: " + str(Game.score) + 'pts') libtcod.console_blit(Game.bottom_panel, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, MAP_HEIGHT+2)
def draw(self): assert len(self.percentiles)==len(self.fgcolours), "HBar not configured correctly" # calculate text s = "" if self.show_numerator: if self.show_denominator: s = "%d/%d"%(self.value,self.max_value) else: s = "%d"%(self.value) if self.text is not None: s = "%s %s"%(self.text,s) s = self.text_align(s,self.size) # draw bar fv = self.value/self.max_value fg_idx = 0 for i in range(0,self.size): #calculate colour for this character f = (i+1)/self.size col = self.bgcolour if f<=fv: # part of bar fg while f>self.percentiles[fg_idx]: fg_idx += 1 assert fg_idx < len(self.percentiles), "HBar not configured correctly" col = self.fgcolours[fg_idx] libtcod.console_put_char_ex(0, self.pos.x+i, self.pos.y, s[i], libtcod.white, col)
def fireball(x, y, radius): path = util.set_full_explore_map(game.current_map) libtcod.dijkstra_compute(path, x, y) for step in range(0, radius + 1): player_fov = False for i in range(-radius, radius + 1): for j in range(-radius, radius + 1): if libtcod.map_is_in_fov(game.fov_map, x + i, y + j) and libtcod.dijkstra_get_distance(path, x + i, y + j) <= step and libtcod.dijkstra_get_distance(path, x + i, y + j) >= 0: (front, back, lerp) = util.render_tiles_animations(x + i, y + j, libtcod.Color(160, 0, 0), libtcod.Color(64, 0, 0), libtcod.Color(0, 0, 0), round(libtcod.random_get_float(game.rnd, 0, 1), 1)) libtcod.console_put_char_ex(0, game.MAP_X + x - game.curx + i, game.MAP_Y + y - game.cury + j, '*', front, back) player_fov = True if player_fov: libtcod.console_flush() time.sleep(0.05) player_fov = False for obj in game.current_map.objects: damage = util.roll_dice(1, 10) if obj.name == 'player': if libtcod.dijkstra_get_distance(path, game.char.x, game.char.y) <= radius: game.message.new('You are hit by a fireball for ' + str(damage) + ' pts of damage!', game.turns, libtcod.Color(160, 0, 0)) game.player.take_damage(damage, 'a fireball') elif obj.entity: if libtcod.dijkstra_get_distance(path, obj.x, obj.y) <= radius: obj.entity.take_damage(obj.x, obj.y, damage, 'a fireball', True)
def update(self): ''' this version draw the map stored in the level, may become obsolete when Game_Shower will be updated ''' #TODO LATER: optimisation for Y in range(min(self.height, self.game.level.map.height)): for X in range(min(self.width, self.game.level.map.width)): libtcod.console_put_char_ex( self.parent().console, X + 1, Y + 1, self.game.level.map.get_tile(X, Y).char, self.game.level.map.get_tile(X, Y).color, self.game.level.map.get_tile(X, Y).background) for ent in self.game.get_entity(): libtcod.console_put_char_ex( self.parent().console, ent.x + 1, ent.y + 1, ent.char, ent.get_color(), self.game.level.map.get_tile(ent.x, ent.y).background) self.ui_layer()
def _draw_entity(entity, map, fov_map): if (libtcodpy.map_is_in_fov(fov_map, entity.x_pos, entity.y_pos) or (entity.always_visible and map.explored[entity.x_pos][entity.y_pos])): libtcodpy.console_set_default_foreground(_console, entity.symbol_color) libtcodpy.console_put_char_ex(_console, entity.x_pos, entity.y_pos, entity.symbol, libtcodpy.white, libtcodpy.black)
def build(self, con): for i in range(self.max()): libtcod.console_put_char_ex(con, i, 0, self.get_char(i), self.get_color(i), libtcod.black) self.width = self.max()
def place_doors(): #check all tiles for doorway-appropriate layout for x in range(config.MAP_WIDTH): for y in range(config.MAP_HEIGHT): #check for appropriate door placement: # # . # .+. or #+# # # . # if (config.map[x][y].blocked == False \ and config.map[x-1][y].blocked == False \ and config.map[x+1][y].blocked == False \ and config.map[x][y-1].blocked \ and config.map[x][y+1].blocked) \ or ( \ config.map[x][y].blocked == False \ and config.map[x][y-1].blocked == False \ and config.map[x][y+1].blocked == False \ and config.map[x-1][y].blocked \ and config.map[x+1][y].blocked): #random chance of door placement: 2% if libtcod.random_get_int(0, 0, 100) < 2: door_component = game.Door() door = game.Object(x, y, '+', 'a closed door', libtcod.Color(223, 223, 223), door=door_component) libtcod.console_put_char_ex(gfx.con, x, y, ' ', libtcod.white, gfx.color_light_ground) config.objects.append(door) config.map[x][y].blocked = True config.map[x][y].block_sight = True
def clear(self, Game): #erase char that represents this object (x, y) = to_camera_coordinates(self.x, self.y, Game) if x is not None and libtcod.map_is_in_fov(Game.player.fighter.fov, self.x, self.y): libtcod.console_put_char_ex(Game.con, x, y, data.GROUND_CHAR, libtcod.white, data.COLOR_LIGHT_GROUND)
def draw(self): """Draw this map tile""" # NB. this gets called a lot! if not self.is_visible: return if self.visible_to_player: if self.map.player.has_effect( StatusEffect.INFRAVISION) and not self.remains_in_place: c = libtcod.white else: c = self.light_colour # this is slow l = libtcod.color_get_hsv(c)[ 2] # this is copied from .light_level for performance if self.map.player.has_effect(StatusEffect.NIGHT_VISION): l = 1.0 - l c = libtcod.white - c if l > LightSource.INTENSITY_L_CLAMP: colour = self.colour * c symbol = self.symbol else: if self.has_been_seen and self.remains_in_place: colour = self.unseen_colour symbol = self.unseen_symbol else: return else: if self.has_been_seen and self.remains_in_place: colour = self.unseen_colour symbol = self.unseen_symbol else: return libtcod.console_put_char_ex(0, self.pos.x, self.pos.y, symbol, colour, libtcod.BKGND_NONE) self.has_been_seen = True
def draw(self): libtcod.console_clear(self.console) xp_loader.load_layer_to_console(self.console, self.bg_data['layer_data'][0]) player_libraries = self.entity_manager.get_entity_by_id( self.entity_manager.player_id).get_attribute( AttributeTag.Libraries).data['value'] for lib in range(4): #+1 here because range will go up to but not including the final screen tile needed for x in range(self.library_line_extent[0] - self.library_start_xy[0] + 1): libtcod.console_put_char_ex(self.console, self.library_start_xy[0] + x, self.library_start_xy[1] + lib, self.line_char, self.line_fg, self.line_bg) libname_xy = Vec2d(self.library_start_xy[0], self.library_start_xy[1] + lib) #TODO: move to config strings libname = 'lib_missing' print_color = self.line_fg if len(player_libraries) > lib: print_color = self.libname_fg libname = player_libraries[lib].name libtcod.console_set_default_foreground(self.console, print_color) libtcod.console_print(self.console, libname_xy[0], libname_xy[1], libname) libtcod.console_blit(self.console, 0, 0, self.width, self.height, 0, 0, 0)
def _draw_what_player_sees(self, pc): world=rog.world() seer=world.component_for_entity(pc, cmp.SenseSight) rang=seer.sight pos=world.component_for_entity(pc, cmp.Position) rend=world.component_for_entity(ent, cmp.Draw) for x in range( max(0, pc.x-rang), min(self.w, pc.x+rang+1) ): for y in range( max(0, pc.y-rang), min(self.h, pc.y+rang+1) ): canSee=False if not rog.in_range(pos.x,pos.y, x,y, rang): continue if not libtcod.map_is_in_fov(seer.fov_map, x,y): continue ent=self.thingat(x, y) if (self.get_light_value(x,y) == 0 and not rog.on(pc,NVISION) ): self._draw_silhouettes(pc, x,y, ent) continue if ent: libtcod.console_put_char( self.con_map_state, x,y, rend.char) libtcod.console_set_char_foreground( self.con_map_state, x,y, rend.color) self._apply_rendered_bgcol(x,y, ent) else: libtcod.console_put_char_ex(self.con_map_state, x,y, self.get_char(x, y), self.get_color(x, y), self.get_bgcolor(x, y))
def missile_attack(sx, sy, dx, dy, trap=False): cx, cy = sx, sy if sx == dx: char = '|' if sy == dy: char = chr(196) if (sx < dx and sy > dy) or (sx > dx and sy < dy): char = '/' if (sx < dx and sy < dy) or (sx > dx and sy > dy): char = '\\' path = util.set_full_explore_map(game.current_map, False) libtcod.path_compute(path, sx, sy, dx, dy) while not libtcod.path_is_empty(path): cx, cy = libtcod.path_walk(path, False) libtcod.console_blit(game.con, 0, 0, game.MAP_WIDTH, game.MAP_HEIGHT, 0, game.MAP_X, game.MAP_Y) libtcod.console_put_char_ex(0, game.MAP_X + cx - game.curx, game.MAP_Y + cy - game.cury, char, libtcod.light_gray, libtcod.black) libtcod.console_flush() time.sleep(0.05) if trap: for obj in game.current_map.objects: if obj.x == dx and obj.y == dy and not obj.item: damage = util.roll_dice(1, 6) if obj.name == 'player': game.message.new('You are hit by an arrow for ' + str(damage) + ' pts of damage!', game.turns, libtcod.Color(160, 0, 0)) game.player.take_damage(damage, 'an arrow trap') else: obj.entity.take_damage(obj.x, obj.y, damage, 'an arrow', True)
def display(grid, console): for y in range(60): for x in range(80): tile = grid[y][x] char = tile_to_char(tile) back = tile_to_colors(tile) front = tile_to_front(tile) tcod.console_put_char_ex(console, x, y, char, front, back)
def BiomeMap(Chars,Colors): for x in xrange(WORLD_WIDTH): for y in xrange(WORLD_HEIGHT): libtcod.console_put_char_ex( 0, x, y + SCREEN_HEIGHT/2 - WORLD_HEIGHT/2, Chars[x][y] , Colors[x][y], libtcod.black) libtcod.console_flush() return
def draw_entity(con, entity, fov_map, game_map, floor_tile): if libtcod.map_is_in_fov(fov_map, entity.x, entity.y) or (entity.stairs and game_map.tiles[entity.x][entity.y].explored): libtcod.console_set_default_foreground(con, entity.color) libtcod.console_put_char(con, entity.x, entity.y, entity.char, libtcod.BKGND_NONE) if not libtcod.map_is_in_fov(fov_map, entity.x, entity.y) and not entity.stairs and game_map.tiles[entity.x][entity.y].explored: libtcod.console_set_default_foreground(con, entity.color) libtcod.console_put_char_ex(con, entity.x, entity.y, floor_tile, libtcod.grey, libtcod.black)
def draw(self, con): if len(self.effects) > 0 and self.effects[-1].char: drawable = self.effects[-1] elif self.entity: drawable = self.entity else: drawable = self libtcod.console_put_char_ex(con, self.x, self.y, drawable.get_char(drawable.x-self.x,drawable.y-self.y), drawable.color, self.bg_color)
def PrecipGradMap(World): # ------------------------------------------------------------ Print Map (Surface Temperature Gradient) white -> cold red -> warm -------------------------------- for x in xrange(WORLD_WIDTH): for y in xrange(WORLD_HEIGHT): tempv = World[x][y].precip tempcolor = libtcod.color_lerp ( libtcod.white, libtcod.light_blue,tempv) libtcod.console_put_char_ex( 0, x, y + SCREEN_HEIGHT/2 - WORLD_HEIGHT/2, '\333' , tempcolor, libtcod.black) libtcod.console_flush() return
def HeightGradMap(World): # ------------------------------------------------------------ Print Map (Heightmap Gradient) ------------------------------------------------------------------- for x in xrange(WORLD_WIDTH): for y in xrange(WORLD_HEIGHT): hm_v = World[x][y].height HeightColor = libtcod.Color(255,255,255) libtcod.color_set_hsv(HeightColor,0,0,hm_v ) #Set lightness to hm_v so higher heightmap value -> "whiter" libtcod.console_put_char_ex( 0, x, y + SCREEN_HEIGHT/2 - WORLD_HEIGHT/2, '\333' , HeightColor, libtcod.black) libtcod.console_flush() return
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 clear_object(obj): global con #erase the character that represents this object if libtcod.map_is_in_fov(maps.fov_map, obj.x, obj.y): libtcod.console_put_char_ex(con, obj.x, obj.y, '.', libtcod.light_grey, libtcod.black) else: libtcod.console_put_char_ex(con, obj.x, obj.y, '.', libtcod.black, libtcod.black)
def ClearConsole(): for x in xrange(SCREEN_WIDTH): for y in xrange(SCREEN_HEIGHT): libtcod.console_put_char_ex( 0, x, y, ' ', libtcod.black, libtcod.black) libtcod.console_flush() return
def build(self,con): #TODO handle map bigger than window for Y in range(self.game.current_map.height): for X in range(self.game.current_map.width): libtcod.console_put_char_ex(con,X,Y, self.game.current_map.get_tile(X,Y).char, self.game.current_map.get_tile(X,Y).color, self.game.current_map.get_tile(X,Y).background)
def draw_messages(player, start_pos = 0): X, Y, W, H = 30, 40, 50, 10 libtcod.console_set_background_color(0, libtcod.black) libtcod.console_rect(0, X, Y, W, H, True, libtcod.BKGND_SET) libtcod.console_set_foreground_color(0, libtcod.white) if start_pos > 0: offset = 1 libtcod.console_print_left(0, X + 1, Y + 0, libtcod.BKGND_NONE, '? Too many messages; [any] to see more') else: offset = 0 try: for m, message in enumerate(player.message_log[start_pos:]): color = message.has_seen and libtcod.grey or libtcod.white libtcod.console_set_foreground_color(0, color) wrapped = wrap_text(message.text, W - 3, subsequent_indent = ' ') for line, text in enumerate(wrapped): if line == 0: if message.symbol1: libtcod.console_put_char_ex( 0, X + ((message.symbol2 is None) and 1 or 0), Y + offset, message.symbol1[0], message.symbol1[1] * (message.has_seen and 0.5 or 1), message.symbol1[2] * (message.has_seen and 0.5 or 1)) if message.symbol2: libtcod.console_put_char_ex( 0, X + 1, Y + offset, message.symbol2[0], message.symbol2[1] * (message.has_seen and 0.5 or 1), message.symbol2[2] * (message.has_seen and 0.5 or 1)) parsed_text, parse_data = parse_colors(text) libtcod.console_print_left(0, X + 3, Y + offset, libtcod.BKGND_NONE, parsed_text%parse_data) offset += 1 if offset >= H: if (not message.has_seen and line + 1 < len(wrapped)) or (m + 1 < len(player.message_log) and not player.message_log[start_pos+m+1].has_seen): raise TooManyMessages() else: raise LogIsFull() except LogIsFull: pass except TooManyMessages: draw_messages(player, start_pos + 1) return for message in player.message_log[start_pos:]: message.has_seen = True if start_pos > 0: libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) draw_messages(player)
def printmap(somemap): global torchconst torchconst += 0.2 flicker = libtcod.noise_get(noise1d,[torchconst]) * 0.1 for x,y in somemap.keys: if somemap.explored[(x,y)] and somemap.getVisible(x,y): tile = tiletochar(somemap.getTile(x,y),somemap.getBrightness(x,y) - flicker,True,somemap.getColor(x,y)) libtcod.console_put_char_ex(viewport.con,x,y,tile[0],tile[1],tile[2])
def BiomeMap(Chars, Colors): for x in xrange(WORLD_WIDTH): for y in xrange(WORLD_HEIGHT): libtcod.console_put_char_ex( 0, x, y + SCREEN_HEIGHT / 2 - WORLD_HEIGHT / 2, Chars[x][y], Colors[x][y], libtcod.black) libtcod.console_flush() return
def draw_at(self, console, x, y, visible): if visible: self.explored = True tcod.console_put_char_ex(console, x, y, self.character, self.color, self.background) elif self.explored: tcod.console_put_char_ex(console, x, y, self.character, self.fog_color, self.fog_background) else: tcod.console_put_char(console, x, y, ' ', tcod.BKGND_NONE)
def test_console_rexpaint_save_load(console, tmpdir, ch, fg, bg): libtcodpy.console_print(console, 0, 0, 'test') libtcodpy.console_put_char_ex(console, 1, 1, ch, fg, bg) xp_file = tmpdir.join('test.xp').strpath assert libtcodpy.console_save_xp(console, xp_file, 1) xp_console = libtcodpy.console_from_xp(xp_file) assert xp_console assertConsolesEqual(console, xp_console) assert libtcodpy.console_load_xp(None, xp_file) assertConsolesEqual(console, xp_console)
def load_layer_to_console(console, xp_file_layer): if not xp_file_layer['width'] or not xp_file_layer['height']: raise AttributeError('Attempted to call load_layer_to_console on data that didn\'t have a width or height key, check your data') for x in range(xp_file_layer['width']): for y in range(xp_file_layer['height']): cell_data = xp_file_layer['cells'][x][y] fore_color = libtcod.Color(cell_data['fore_r'], cell_data['fore_g'], cell_data['fore_b']) back_color = libtcod.Color(cell_data['back_r'], cell_data['back_g'], cell_data['back_b']) libtcod.console_put_char_ex(console, x, y, cell_data['keycode'], fore_color, back_color)
def ClearConsole(): for x in xrange(SCREEN_WIDTH): for y in xrange(SCREEN_HEIGHT): libtcod.console_put_char_ex(0, x, y, ' ', libtcod.black, libtcod.black) libtcod.console_flush() return
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 display_screen(self): # display current screen contents, including graphics layer if active for x in range(self.width): for y in range(self.height): libtcod.console_put_char_ex(self.con, x, y, self.screen[x][y], self.foreground, self.background) libtcod.console_blit(self.con, 0, 0, self.width, self.height, 0, 0, 0) if self.graphics_mode and self.img is not None: libtcod.image_set_key_color(self.img, self.background) libtcod.image_blit_2x(self.img, 0, 0, 0) libtcod.console_flush()
def build(self, con): if self.on: char = self.char col = self.color bkg = self.bkg_color else: char = self.sec_char col = self.sec_color bkg = self.sec_bkg_color libtcod.console_put_char_ex(con, 0, 0, char, col, bkg)
def draw(self, *args): if len(args) == 1: obj = args[0] libtcod.console_set_default_foreground(self.con, obj.color) libtcod.console_put_char(self.con, obj.x, obj.y, obj.char, libtcod.BKGND_NONE) elif len(args) == 3: color = args[0] x = args[1] y = args[2] libtcod.console_put_char_ex(self.con, x, y, 0, color, color)
def draw(self, camera, map, distance): fov_damping = 20 if not libtcod.map_is_in_fov(map.fov_map, self.x, self.y): f_color = self.fcolor * libtcod.dark_gray b_color = self.bcolor * libtcod.dark_gray else: f_color = self.fcolor b_color = self.bcolor * libtcod.Color( 255 - (distance * fov_damping), 255 - (distance * fov_damping), 255 - (distance * fov_damping) ) (t_x, t_y) = camera.to_camera_coordinates(self.x, self.y) libtcod.console_put_char_ex(0, t_x, t_y, str(self.char), f_color, b_color)
def _discover_place(self, x,y,ent=None): world=rog.world() ent = self.thingat(x,y) if ent and not world.component_for_entity(ent, Creature): draw=world.component_for_entity(ent, Draw): libtcod.console_put_char_ex( self.con_memories, x,y, draw.char, COL['dkgray'],COL['black'] ) else: libtcod.console_put_char_ex(self.con_memories, x,y, self.get_char(x,y), COL['dkgray'], COL['black'])
def TempGradMap( World ): # ------------------------------------------------------------ Print Map (Surface Temperature Gradient) white -> cold red -> warm -------------------------------- for x in xrange(WORLD_WIDTH): for y in xrange(WORLD_HEIGHT): tempv = World[x][y].temp tempcolor = libtcod.color_lerp(libtcod.white, libtcod.red, tempv) libtcod.console_put_char_ex( 0, x, y + SCREEN_HEIGHT / 2 - WORLD_HEIGHT / 2, '\333', tempcolor, libtcod.black) libtcod.console_flush() return
def DrainageGradMap( World ): # ------------------------------------------------------------ Print Map (Drainage Gradient) brown -> low white -> high -------------------------------- for x in xrange(WORLD_WIDTH): for y in xrange(WORLD_HEIGHT): drainv = World[x][y].drainage draincolor = libtcod.color_lerp(libtcod.darkest_orange, libtcod.white, drainv) libtcod.console_put_char_ex( 0, x, y + SCREEN_HEIGHT / 2 - WORLD_HEIGHT / 2, '\333', draincolor, libtcod.black) libtcod.console_flush() return