def test_noise(): noise = libtcodpy.noise_new(1) libtcodpy.noise_set_type(noise, libtcodpy.NOISE_SIMPLEX) libtcodpy.noise_get(noise, [0]) libtcodpy.noise_get_fbm(noise, [0], 4) libtcodpy.noise_get_turbulence(noise, [0], 4) libtcodpy.noise_delete(noise)
def make_outdoor_map(): global map # Fill map with "unblocked" tiles data = libtcod.noise_get(height_map, [10, 10]) map = [[Tile(False, data=TILE_TYPE['GRASS_3']) for y in range(MAP_HEIGHT)] for x in range(MAP_WIDTH)] for y in range(MAP_HEIGHT): for x in range(MAP_WIDTH): data = libtcod.noise_get(height_map, [x * 0.1, y * 0.1], libtcod.NOISE_PERLIN) # print data if data > 0.2: data = TILE_TYPE['GRASS_1'] elif data < -0.2: data = TILE_TYPE['GRASS_3'] else: data = TILE_TYPE['GRASS_2'] map[y][x].data = data buildings = [] num_buildings = 0 for r in range(MAX_ROOMS): #random width and height w = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE) h = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE) #random position without going out of the boundaries of the map x = libtcod.random_get_int(0, 0, MAP_WIDTH - w - 1) y = libtcod.random_get_int(0, 0, MAP_HEIGHT - h - 1) #"Rect" class makes rectangles easier to work with new_room = Rect(x, y, w, h) #run through the other buildings and see if they intersect with this one failed = False for other_room in buildings: if new_room.intersect(other_room): failed = True break if not failed: #this means there are no intersections, so this room is valid #"paint" it to the map's tiles create_building(new_room) #center coordinates of new room, will be useful later (new_x, new_y) = new_room.center() if num_buildings == 0: #this is the first room, where the player starts at player.x = new_x player.y = new_y # Add some objects to the room place_objects(new_room) #finally, append the new room to the list buildings.append(new_room) num_buildings += 1
def make_outdoor_map(): global map # Fill map with "unblocked" tiles data = libtcod.noise_get(height_map, [10, 10]) map = [[ Tile(False, data=TILE_TYPE['GRASS_3']) for y in range(MAP_HEIGHT) ] for x in range(MAP_WIDTH) ] for y in range(MAP_HEIGHT): for x in range(MAP_WIDTH): data = libtcod.noise_get(height_map, [x*0.1, y*0.1], libtcod.NOISE_PERLIN) # print data if data > 0.2: data = TILE_TYPE['GRASS_1'] elif data < -0.2: data = TILE_TYPE['GRASS_3'] else: data = TILE_TYPE['GRASS_2'] map[y][x].data = data buildings = [] num_buildings = 0 for r in range(MAX_ROOMS): #random width and height w = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE) h = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE) #random position without going out of the boundaries of the map x = libtcod.random_get_int(0, 0, MAP_WIDTH - w - 1) y = libtcod.random_get_int(0, 0, MAP_HEIGHT - h - 1) #"Rect" class makes rectangles easier to work with new_room = Rect(x, y, w, h) #run through the other buildings and see if they intersect with this one failed = False for other_room in buildings: if new_room.intersect(other_room): failed = True break if not failed: #this means there are no intersections, so this room is valid #"paint" it to the map's tiles create_building(new_room) #center coordinates of new room, will be useful later (new_x, new_y) = new_room.center() if num_buildings == 0: #this is the first room, where the player starts at player.x = new_x player.y = new_y # Add some objects to the room place_objects(new_room) #finally, append the new room to the list buildings.append(new_room) num_buildings += 1
def __init__(self, x, y, world_width, world_height, world_noise, resource_noise): self.x = x self.y = y self.la = ((Decimal(self.y) * 180) / Decimal(world_height)) - 90 self.lo = ((Decimal(self.x) * 360) / Decimal(world_width)) - 180 self.char = '#' self.color = WHITE self.neighbors = {} # Sampling 3d noise to get the shape of the landmasses x, y, z = spherical_to_cartesian(self.la, self.lo, self.LANDMASS_SIZE) self.elevation = libtcod.noise_get_fbm( world_noise, [float(x), float(y), float(z)], self.DETAIL) # Sampling 3d noise to find out where to spawn resource nodes x, y, z = spherical_to_cartesian(self.la, self.lo, self.RESOURCE_DISTRIBUTION) if self.elevation > 0: self.resource_density = libtcod.noise_get( resource_noise, [float(x), float(y), float(z)]) else: self.resource_density = 0 for elev_range, elev_info in self.ELEVATIONS.items(): max_, min_ = elev_range if max_ >= self.elevation > min_: self.color = elev_info['color'] self.char = elev_info['char']
def noise_example(): noise1d = libtcod.noise_new(1) for s in range(10): x = s / float(5) v = libtcod.noise_get(noise1d, [x]) print(v) libtcod.noise_delete(noise1d)
def generateWater(self): #noise_octaves = 4.0 noise_zoom = 2.0 noise_hurst = libtcod.NOISE_DEFAULT_HURST noise_lacunarity = libtcod.NOISE_DEFAULT_LACUNARITY mapped = self.mappedArea noise = libtcod.noise_new(2, noise_hurst, noise_lacunarity) for y in range(self.height): for x in range(self.width): f = [noise_zoom * x / (self.width), noise_zoom * y / (self.height)] noisefloat = libtcod.noise_get(noise, f, libtcod.NOISE_PERLIN) if noisefloat >= float(0) and noisefloat <= 0.1: mapped[x][y].tileType = "water" mapped[x][y].blocked = False mapped[x][y].block_sight = False mapped[x][y].slows = True #Block the edges of the map so player can't crash the game for y in range(self.height): #mapped[0][y].tileType = None mapped[0][y].blocked = True mapped[self.width-1][y].blocked = True for x in range(self.width): mapped[x][0].blocked = True mapped[x][self.height-1].blocked = True
def create(self): w=self.width=cfg.OW_WIDTH h=self.height=cfg.OW_HEIGHT th=cfg.OW_TREE_THRES self.level=[[OverworldTile(j,i) for i in xrange(h)] for j in xrange(w)] self.console=libtcod.console_new(w,h) backColor=libtcod.Color(0, 0, 0) noise2d = libtcod.noise_new(2) for x in xrange(w): for y in xrange(h): zoom=0.09 f = [zoom * x,zoom * y] val = libtcod.noise_get(noise2d,f) c1=int((((val*-1)+1)/2)*30) c2=10+int(((val+1)/2)*20) if val>th: self.level[x][y].setChar(23) self.level[x][y].setColors(libtcod.Color(0, 200, 0),libtcod.Color(0, 0, 0)) self.level[x][y].setBlocked(True) else: self.level[x][y].setChar(176) self.level[x][y].setColors(libtcod.Color(0, c1, 0),libtcod.Color(0, c2, 0)) while len(self.pathable)<400: self.findPathable() self.clearUnpathableAreas() #self.findPathable() # Now a final scan for the full area # Place town town_pos=random.choice(self.pathable) town=OverworldTileEntity(town_pos[0],town_pos[1]) town.setColors(libtcod.Color(0, 100, 150),libtcod.Color(40, 40, 0)) self.tile_entity.append(town) self.town=town # Place dungeons for i in xrange(cfg.DUNGEONS): validLocation=False pos=None while not validLocation: validLocation=True pos=random.choice(self.pathable) for entity in self.tile_entity: if entity.position()==pos: validLocation=False dungeon=OverworldTileEntity(pos[0],pos[1]) dungeon.setColors(libtcod.Color(200, 0, 0),libtcod.Color(40, 0, 0)) self.tile_entity.append(dungeon) self.buildBlockedMap()
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 noise_2d(width, height): noise = libtcod.noise_new(2) noise_map = {} for x in range(width): for y in range(height): value = libtcod.noise_get(noise, [x * 0.06, y * 0.06]) noise_map[(x, y)] = value * 100 return noise_map
def generate(self, border_size = 1, air_buffer = 10): noise_2d = libtcod.noise_new(2) self.tiles = [[ Dirt(libtcod.noise_get(noise_2d,[x,y])*3) for y in range(self.height) ] for x in range(self.width) ] for x in range(self.width): for y in range(self.height): if x < border_size or y < border_size or x > ((self.width - 1) - border_size) or y > ((self.height - 1) - border_size): self.tiles[x][y] = Wall() elif y < border_size + air_buffer: self.tiles[x][y] = Air() self.update()
def get_noise_value(x, y, scale=16, type='FBM'): nx, ny = float(x) / scale, float(y) / scale if type == 'DEFAULT': pre_value = libtcod.noise_get(noise_field, (nx, ny) , libtcod.NOISE_PERLIN) elif type == 'FBM': pre_value = libtcod.noise_get_fbm(noise_field, (nx, ny), 8, libtcod.NOISE_DEFAULT) elif type == 'TURB': pre_value = libtcod.noise_get_turbulence(noise_field, (nx, ny), 1, libtcod.NOISE_PERLIN) return pre_value
def get_noise_value(x, y, scale=16, type='FBM'): nx, ny = float(x) / scale, float(y) / scale if type == 'DEFAULT': pre_value = libtcod.noise_get(noise_field, (nx, ny), libtcod.NOISE_PERLIN) elif type == 'FBM': pre_value = libtcod.noise_get_fbm(noise_field, (nx, ny), 8, libtcod.NOISE_DEFAULT) elif type == 'TURB': pre_value = libtcod.noise_get_turbulence(noise_field, (nx, ny), 1, libtcod.NOISE_PERLIN) return pre_value
def noisemap_color(based_on, x, y): # # Smoother, more like gradients you'd see in Brogue # f = [x*.2, y*.2] # noise_value = libtcod.noise_get(color_noise, f, 32) # color_multiplier = noise_value*.22+.9 # # Blotchier, looks better with subtler colors x = x * 1.0 y = y * 1.0 f = [x, y] noise_value = libtcod.noise_get(color_noise, f, 8) color_multiplier = noise_value * .1 + .92 final_color = based_on * color_multiplier return final_color
def generateDepth(self): #noise_octaves = 4.0 noise_zoom = 2.0 noise_hurst = libtcod.NOISE_DEFAULT_HURST noise_lacunarity = libtcod.NOISE_DEFAULT_LACUNARITY mapped = self.mappedArea water_color = libtcod.sky if self.mapType == "desert": water_level = 0.04 else: water_level = 0.07 noise = libtcod.noise_new(2, noise_hurst, noise_lacunarity) for y in range(self.height): for x in range(self.width): f = [noise_zoom * x / (self.width), noise_zoom * y / (self.height)] noisefloat = abs(libtcod.noise_get(noise, f, libtcod.NOISE_PERLIN)) if noisefloat >= float(0) and noisefloat <= water_level: tile = mapped[x][y] tile.tileType = "water" tile.blocked = False tile.block_sight = False tile.slows = True #Depth = Color darkness depth = int(10 - (noisefloat * 100)) tile.color = water_color - libtcod.Color(5 * depth, 5 * depth, 5 * depth) tile.height = 0 - depth else: tile = mapped[x][y] height = int(noisefloat / 0.03) if height > 12: height = 12 tile.height = height #Block the edges of the map so player can't crash the game for y in range(self.height): #mapped[0][y].tileType = None mapped[0][y].blocked = True mapped[self.width-1][y].blocked = True for x in range(self.width): mapped[x][0].blocked = True mapped[x][self.height-1].blocked = True
def get_cylindrical_projection(stars, width=360, height=180): """ Return a tcod console instance of width and height that renders an equirectangular projection of the given list of stars. """ console = tcod.console_new(width, height) for star in stars: azimuthal = int((star.azimuthal * width) / (2 * math.pi)) polar = int((star.polar / math.pi) * height) # Color Work rgb = temperature_to_rgb(random.uniform(4000, 20000)) brightness = 1.0 - star.radial * 0.75 color = tcod.Color(rgb[0], rgb[1], rgb[2]) (h, s, v) = tcod.color_get_hsv(color) tcod.color_set_hsv(color, h, s, brightness) tcod.console_put_char_ex(console, azimuthal, polar, star.sprite, color, tcod.black) # Background Texture noise3d = tcod.noise_new(3) for map_x in range(width): for map_y in range(height): azimuthal = (map_x / (width * 1.0)) * 2.0 * math.pi polar = (map_y / (height * 1.0)) * math.pi x = math.sin(polar) * math.cos(azimuthal) y = math.sin(polar) * math.sin(azimuthal) z = math.cos(polar) blue = int( tcod.noise_get_turbulence(noise3d, [x, y, z], 32.0) * 16.0 + 16.0) green = int(tcod.noise_get(noise3d, [x, y, z]) * 8.0 + 8.0) red = int(tcod.noise_get_fbm(noise3d, [x, y, z], 32.0) * 4.0 + 4.0) background = tcod.Color(red, green, blue) if map_y == height / 2 or map_x == 0 or map_x == width / 2: background = tcod.darkest_yellow tcod.console_set_char_background(console, map_x, map_y, background) tcod.noise_delete(noise3d) return console
def generateWater(self): #noise_octaves = 4.0 noise_zoom = 2.0 noise_hurst = libtcod.NOISE_DEFAULT_HURST noise_lacunarity = libtcod.NOISE_DEFAULT_LACUNARITY mapped = self.mappedArea water_color = libtcod.sky water_color1 = water_color - libtcod.Color(6, 6, 6) water_color2 = water_color1 - libtcod.Color(6, 6, 6) water_color3 = water_color2 - libtcod.Color(8, 8, 8) noise = libtcod.noise_new(2, noise_hurst, noise_lacunarity) for y in range(self.height): for x in range(self.width): f = [noise_zoom * x / (self.width), noise_zoom * y / (self.height)] noisefloat = abs(libtcod.noise_get(noise, f, libtcod.NOISE_PERLIN)) if noisefloat >= float(0) and noisefloat <= 0.08: tile = mapped[x][y] tile.tileType = "water" tile.blocked = False tile.block_sight = False tile.slows = True #Depth = Color darkness depth = 10 - (noisefloat * 100) if depth <= 5: tile.color = water_color elif 5 < depth <= 7: tile.color = water_color1 elif 7 < depth < 9: tile.color = water_color2 else: tile.color = water_color3 #Block the edges of the map so player can't crash the game for y in range(self.height): #mapped[0][y].tileType = None mapped[0][y].blocked = True mapped[self.width-1][y].blocked = True for x in range(self.width): mapped[x][0].blocked = True mapped[x][self.height-1].blocked = True
def map_init_noise(width, height): map_gen = [] noise = libtcodpy.noise_new(2) libtcodpy.noise_set_type(noise, libtcodpy.NOISE_SIMPLEX) for x in range(0, width): aux = [] for y in range(0, height): if libtcodpy.noise_get(noise, [(x + 1) / 3, y / 3]) > 0.6: aux.append( Tile(False, True, False, 0, GAME.tiles.image_at((0, 0, 32, 32)), GAME.tiles.image_at((0, 32, 32, 32)))) # WALL else: aux.append( Tile(True, False, True, 0, GAME.tiles.image_at((32, 0, 32, 32)), GAME.tiles.image_at((32, 32, 32, 32)))) #F LOOR map_gen.append(aux) map_gen = map_set_borders(map_gen, width - 1, height - 1) # BORDERS return map_gen
def create(self): w = self.width = cfg.OW_WIDTH h = self.height = cfg.OW_HEIGHT th = cfg.OW_TREE_THRES self.level = [[OverworldTile(j, i) for i in xrange(h)] for j in xrange(w)] self.console = libtcod.console_new(w, h) backColor = libtcod.Color(0, 0, 0) noise2d = libtcod.noise_new(2) for x in xrange(w): for y in xrange(h): zoom = 0.09 f = [zoom * x, zoom * y] val = libtcod.noise_get(noise2d, f) c1 = int((((val * -1) + 1) / 2) * 30) c2 = 10 + int(((val + 1) / 2) * 20) if val > th: self.level[x][y].setChar(23) self.level[x][y].setColors(libtcod.Color(0, 200, 0), libtcod.Color(0, 0, 0)) self.level[x][y].setBlocked(True) else: self.level[x][y].setChar(176) self.level[x][y].setColors(libtcod.Color(0, c1, 0), libtcod.Color(0, c2, 0)) while len(self.pathable) < 400: self.findPathable() self.clearUnpathableAreas() #self.findPathable() # Now a final scan for the full area # Place town town_pos = random.choice(self.pathable) town = OverworldTileEntity(town_pos[0], town_pos[1]) town.setColors(libtcod.Color(0, 100, 150), libtcod.Color(40, 40, 0)) self.tile_entity.append(town) self.town = town # Place dungeons for i in xrange(cfg.DUNGEONS): validLocation = False pos = None while not validLocation: validLocation = True pos = random.choice(self.pathable) for entity in self.tile_entity: if entity.position() == pos: validLocation = False dungeon = OverworldTileEntity(pos[0], pos[1]) dungeon.setColors(libtcod.Color(200, 0, 0), libtcod.Color(40, 0, 0)) self.tile_entity.append(dungeon) self.buildBlockedMap()
def area_cell_create(width, height, p_blocked, p_unblocked, nudge, steps, border=True, exits=0, connect=True, seed=lt.random_get_instance()): """ :param width: :param height: :param p_blocked: - At least what percent of a valid area must be unblocked spaces. :param p_unblocked: - At least what percent of a valid area must be blocked spaces. :param nudge: - Higher number means more blocked space. :param steps: - How many steps to run the automata. :param border: - Requires a solid border? :param exits: - If there's a solid border, how many exits should there be? :return: """ print "---" area = [[True for row in xrange(width)] for col in xrange(height)] # Create noise print "Generating noise..." area_noise = lt.noise_new(2, random=seed) for col in xrange(height): for row in xrange(width): if border: if row < 3 or row > width - 3: area[col][row] = 1 elif col < 3 or col > height - 3: area[col][row] = 1 else: coords = (col, row) n = lt.noise_get(area_noise, coords) area[col][row] = int(round(abs(n) + nudge, 0)) else: coords = (col, row) n = lt.noise_get(area_noise, coords) area[col][row] = int(round(abs(n) + nudge, 0)) visualize(area) # Add in exits. print "Adding exits..." if exits > 0: exitcoords = [] for direction in xrange(exits): row_size = random.randint(4, 10) col_size = random.randint(4, 10) if direction == 0: row_loc = random.randint(width/2 - width/4, width/2 + width/4) col_loc = height - col_size for col in xrange(col_size): for row in xrange(row_size): area[col_loc+col][row_loc+row] = 0 for row in xrange(row_size): coords = (height - 2, row_loc + row) area[coords[0] + 1][coords[1]] = -1 exitcoords.append(coords) visualize(area) # Begin cellular automata print "Running automata..." for i in xrange(steps): next_step = area for col in xrange(height): for row in xrange(width): neighbors = count_neighbors(col, row, area) # Check births and survivals. # Current rules are: B5678 / S45678. May want to add the ability to affect that too. if area[col][row] == 1 and neighbors < 4: next_step[col][row] = 0 elif area[col][row] == 0 and neighbors > 4: next_step[col][row] = 1 area = next_step visualize(area) # Mark as ground only those ground tiles which can be reached from an arbitrary point near the center OR an exit. print "Counting reach..." while True: if exits == 0: row = random.randint(width/2 - 10, width/2 + 10) col = random.randint(height/2 - 10, height/2 + 10) elif exits > 0: col, row = exitcoords.pop() if area[col][row] == 0: reachable(col, row, area, 1) break # Replace 0's (unreachable sections) with 1 (blocked). and 2's (reachable sections) # with 0 (unblocked), while counting how many there are. unblocked_n = 0. blocked_n = 0. for col in xrange(height): for row in xrange(width): if area[col][row] == 1 or (area[col][row] == 0 and connect): area[col][row] = 1 blocked_n += 1 elif area[col][row] > 1 or (area[col][row] == 0 and not connect): if col == 0 or col == height - 1 or row == 0 or row == width - 1: area[col][row] = -1 unblocked_n += 1 # Ensure the map fits our parameters. print "Testing parameters..." visualize(area) if unblocked_n <= width * height * p_unblocked: print "Not enough unblocked: ", unblocked_n, "<", width * height * p_unblocked if blocked_n <= width * height * p_blocked: print "Not enough blocked.", blocked_n, "<", width * height * p_blocked return False print "Area generated." return area
def render_all(): global fov_map, color_dark_wall, color_light_wall global color_dark_ground, color_light_ground global fov_recompute, fov_torchx 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) #torch flickers (using noise generator) fov_torchx += 0.2 tdx = [fov_torchx + 20.0] dx = libtcod.noise_get(fov_noise, tdx) * 1.5 tdx[0] += 30.0 dy = libtcod.noise_get(fov_noise, tdx) * 1.5 di = 0.2 * libtcod.noise_get(fov_noise, [fov_torchx]) # Iterate through rendering queue 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 # check if tile is a wall 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: # 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: base = color_dark_wall light = color_light_wall else: base = color_dark_ground light = color_light_ground #Let the torch actually flicker r = float(x - player.x + dx) * (x - player.x + dx) + \ (y - player.y + dy) * (y - player.y + dy) if r < SQUARED_TORCH_RADIUS: l = (SQUARED_TORCH_RADIUS - r) / SQUARED_TORCH_RADIUS + di if l < 0.0: l = 0.0 elif l > 1.0: l = 1.0 # alter base colors to simulate flickering torch base = libtcod.color_lerp(base, light, l) # actually draw the visible tile libtcod.console_set_char_background(con, x, y, base, libtcod.BKGND_SET) #since it's visible, it's explored 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 game messages y = 1 for (line, color) in game_msgs: libtcod.console_set_default_foreground(panel, color) libtcod.console_set_alignment(panel, libtcod.LEFT) libtcod.console_print(panel, MSG_X, y, 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) libtcod.console_set_alignment(panel, libtcod.LEFT) libtcod.console_print(panel, 1, 3, 'Dungeon level {}'.format(str(dungeon_level))) # Display names of objects under the mouse libtcod.console_set_default_foreground(panel, libtcod.light_grey) libtcod.console_print(panel, 1, 0, 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)
import libtcodpy as libtcod SCREEN_WIDTH = 16 SCREEN_HEIGHT = 16 noise2d = libtcod.noise_new(1) m = libtcod.heightmap_new(SCREEN_WIDTH, SCREEN_HEIGHT) for y in range(SCREEN_HEIGHT): for x in range(SCREEN_WIDTH): n = libtcod.noise_get(noise2d, [2.0 * x / SCREEN_WIDTH - 1.0, 2.0 * y / SCREEN_HEIGHT - 1.0]) n = (n + 1.0) / 2.0 libtcod.heightmap_set_value(m, x, y, n) for y in range(SCREEN_HEIGHT): for x in range(SCREEN_WIDTH): v = libtcod.heightmap_get_value(m, x, y) print(v)
def render_all(): global fov_map, color_dark_wall, color_light_wall global color_dark_ground, color_light_ground global fov_recompute, fov_torchx 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) #torch flickers (using noise generator) fov_torchx += 0.2 tdx = [fov_torchx + 20.0] dx = libtcod.noise_get(fov_noise, tdx) * 1.5 tdx[0] += 30.0 dy = libtcod.noise_get(fov_noise, tdx) * 1.5 di = 0.2 * libtcod.noise_get(fov_noise, [fov_torchx]) # Iterate through rendering queue 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 # check if tile is a wall 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: # 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: base = color_dark_wall light = color_light_wall else: base = color_dark_ground light = color_light_ground #Let the torch actually flicker r = float(x - player.x + dx) * (x - player.x + dx) + \ (y - player.y + dy) * (y - player.y + dy) if r < SQUARED_TORCH_RADIUS: l = (SQUARED_TORCH_RADIUS - r) / SQUARED_TORCH_RADIUS + di if l < 0.0: l = 0.0 elif l > 1.0: l = 1.0 # alter base colors to simulate flickering torch base = libtcod.color_lerp(base, light, l) # actually draw the visible tile libtcod.console_set_char_background(con, x, y, base, libtcod.BKGND_SET) #since it's visible, it's explored 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 game messages y = 1 for (line, color) in game_msgs: libtcod.console_set_default_foreground(panel, color) libtcod.console_set_alignment(panel, libtcod.LEFT) libtcod.console_print(panel, MSG_X, y, 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) libtcod.console_set_alignment(panel, libtcod.LEFT) libtcod.console_print(panel, 1, 3, 'Dungeon level {}'.format(str(dungeon_level))) # Display names of objects under the mouse libtcod.console_set_default_foreground(panel, libtcod.light_grey) libtcod.console_print(panel, 1, 0, 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_map(): # recompute FOV if needed (the player moved or something) libtcod.console_rect(0, game.MAP_X, game.MAP_Y, game.MAP_WIDTH, game.MAP_HEIGHT, True) if game.fov_recompute: find_map_viewport() fov_radius() initialize_fov(True) libtcod.map_compute_fov(game.fov_map, game.char.x, game.char.y, game.FOV_RADIUS, game.FOV_LIGHT_WALLS, game.FOV_ALGO) game.fov_recompute = False # 'torch' animation if game.fov_torch: game.fov_torchx += 0.2 tdx = [game.fov_torchx + 20.0] dx = libtcod.noise_get(game.fov_noise, tdx, libtcod.NOISE_SIMPLEX) * 1.5 tdx[0] += 30.0 dy = libtcod.noise_get(game.fov_noise, tdx, libtcod.NOISE_SIMPLEX) * 1.5 di = 0.4 * libtcod.noise_get(game.fov_noise, [game.fov_torchx], libtcod.NOISE_SIMPLEX) # go through all tiles, and set their background color according to the FOV for y in range(game.MAP_HEIGHT): for x in range(game.MAP_WIDTH): px = x + game.curx py = y + game.cury if not libtcod.map_is_in_fov(game.fov_map, px, py): if game.draw_map and game.current_map.tile_is_explored(px, py): if game.current_map.tile_is_animated(px, py): libtcod.console_put_char_ex(game.con, x, y, game.current_map.tile[px][py]['icon'], game.current_map.tile[px][py]['dark_color'], game.current_map.tile[px][py]['dark_back_color']) else: libtcod.console_put_char_ex(game.con, x, y, game.current_map.tile[px][py]['icon'], game.current_map.tile[px][py]['dark_color'], game.current_map.tile[px][py]['back_dark_color']) else: if not game.fov_torch: if 'animate' in game.current_map.tile[px][py] or 'duration' in game.current_map.tile[px][py]: (front, back, game.current_map.tile[px][py]['lerp']) = render_tiles_animations(px, py, game.current_map.tile[px][py]['color'], game.current_map.tile[px][py]['back_light_color'], game.current_map.tile[px][py]['back_dark_color'], game.current_map.tile[px][py]['lerp']) libtcod.console_put_char_ex(game.con, x, y, game.current_map.tile[px][py]['icon'], front, back) elif game.draw_map: libtcod.console_put_char_ex(game.con, x, y, game.current_map.tile[px][py]['icon'], game.current_map.tile[px][py]['color'], game.current_map.tile[px][py]['back_light_color']) else: base = game.current_map.tile[px][py]['back_light_color'] r = float(px - game.char.x + dx) * (px - game.char.x + dx) + (py - game.char.y + dy) * (py - game.char.y + dy) if r < game.SQUARED_TORCH_RADIUS: l = (game.SQUARED_TORCH_RADIUS - r) / game.SQUARED_TORCH_RADIUS + di if l < 0.0: l = 0.0 elif l > 1.0: l = 1.0 base = libtcod.color_lerp(base, libtcod.gold, l) libtcod.console_put_char_ex(game.con, x, y, game.current_map.tile[px][py]['icon'], game.current_map.tile[px][py]['color'], base) if not game.current_map.tile_is_explored(px, py): game.current_map.tile[px][py].update({'explored': True}) # draw all objects in the map (if in the map viewport), except the player who his drawn last for obj in reversed(game.current_map.objects): if obj.y in range(game.cury, game.cury + game.MAP_HEIGHT) and obj.x in range(game.curx, game.curx + game.MAP_WIDTH) and game.current_map.tile_is_explored(obj.x, obj.y) and obj.name != 'player': if game.draw_map and obj.entity is not None: if libtcod.map_is_in_fov(game.fov_map, obj.x, obj.y) and not obj.entity.is_identified(): skill = game.player.find_skill('Mythology') if (game.player.skills[skill].level * 0.8) + 20 >= roll_dice(1, 100): obj.entity.flags.append('identified') game.message.new('You properly identify the ' + obj.entity.unidentified_name + ' as ' + obj.entity.get_name(True) + '.', game.turns) game.player.skills[skill].gain_xp(3) if obj.entity is not None and not obj.entity.is_identified(): obj.draw(game.con, libtcod.white) else: obj.draw(game.con) game.char.draw(game.con) libtcod.console_blit(game.con, 0, 0, game.MAP_WIDTH, game.MAP_HEIGHT, 0, game.MAP_X, game.MAP_Y) game.draw_map = False # move the player if using mouse if game.mouse_move: if mouse_auto_move() and not libtcod.path_is_empty(game.path): game.char.x, game.char.y = libtcod.path_walk(game.path, True) game.fov_recompute = True game.player_move = True else: items_at_feet() game.mouse_move = False # check where is the mouse cursor if not in the act of moving while using the mouse if not game.mouse_move: (mx, my) = (game.mouse.cx - game.MAP_X, game.mouse.cy - 1) px = mx + game.curx py = my + game.cury game.path_dx = -1 game.path_dy = -1 if my in range(game.MAP_HEIGHT) and mx in range(game.MAP_WIDTH): libtcod.console_set_char_background(0, mx + game.MAP_X, my + 1, libtcod.white, libtcod.BKGND_SET) if game.current_map.tile_is_explored(px, py) and not game.current_map.tile_is_blocked(px, py): game.path_dx = px game.path_dy = py if game.mouse.lbutton_pressed: target = [obj for obj in game.current_map.objects if obj.y == py and obj.x == px and obj.entity] if target: mouse_auto_attack(px, py, target[0]) else: game.mouse_move = mouse_auto_move() # draw a line between the player and the mouse cursor if not game.current_map.tile_is_blocked(game.path_dx, game.path_dy): libtcod.path_compute(game.path, game.char.x, game.char.y, game.path_dx, game.path_dy) for i in range(libtcod.path_size(game.path)): x, y = libtcod.path_get(game.path, i) if (y - game.cury) in range(game.MAP_HEIGHT) and (x - game.curx) in range(game.MAP_WIDTH): libtcod.console_set_char_background(0, game.MAP_X + x - game.curx, game.MAP_Y + y - game.cury, libtcod.desaturated_yellow, libtcod.BKGND_SET) libtcod.console_set_default_foreground(0, libtcod.light_yellow) libtcod.console_print_rect(0, game.MAP_X, game.MAP_Y, game.MAP_WIDTH - 18, game.MAP_HEIGHT, get_names_under_mouse()) if game.debug.enable: libtcod.console_print_ex(0, game.MAP_X + game.MAP_WIDTH - 1, game.MAP_Y, libtcod.BKGND_NONE, libtcod.RIGHT, str(game.gametime.hour) + ':' + str(game.gametime.minute).rjust(2, '0') + ' (%3d fps)' % libtcod.sys_get_fps()) if game.hp_anim: render_floating_text_animations()