def init_fov(self): libtcod.map_clear(self.fov_map) #self.fov_map = libtcod.map_new(self.map.width, self.map.height) for x,y,cell in self.map.iter_cells_with_coords(): libtcod.map_set_properties(self.fov_map, x,y, not cell.block_sight, not cell.blocked )
def test_map(): map = libtcodpy.map_new(16, 16) assert libtcodpy.map_get_width(map) == 16 assert libtcodpy.map_get_height(map) == 16 libtcodpy.map_copy(map, map) libtcodpy.map_clear(map) libtcodpy.map_set_properties(map, 0, 0, True, True) assert libtcodpy.map_is_transparent(map, 0, 0) assert libtcodpy.map_is_walkable(map, 0, 0) libtcodpy.map_is_in_fov(map, 0, 0) libtcodpy.map_delete(map)
def buildTcodMap( self ): self.tcodmap = tcod.map_new( self.width, self.height ) tcod.map_clear( self.tcodmap, True, True ) for y in range( self.height ): i = self.I( 0, y ) for x in range( self.width ): flags = self.tileData[ i ] tcod.map_set_properties( self.tcodmap, x, y, True, ( flags & Constant.BLOCKED ) == 0 ) i += 1
def __init__(self, width, height, pathimage): self.Map = [] self.FOVMap = Libtcod.map_new(width, height) self.PathImage = pathimage Libtcod.map_clear(self.FOVMap, transparent = True, walkable = True) self.Width = width self.Height = height c = -1 for y in range(height): self.Map.append([]) for x in range(width): Global.Tiles.append(Tile()) c += 1 Global.Tiles[c].X = x Global.Tiles[c].Y = y self.Map[y].append(Global.Tiles[c]) if Libtcod.image_get_pixel(self.PathImage, x, y) == Libtcod.white: Libtcod.map_set_properties(self.FOVMap, x, y, True, False)
if init: init = False hearts = 5 alive = True score = 0 if restart: restart = False punish = False speed = [1, 0] snake = [] snack = [] bait = [] grow = False growth = 0 grid = tcod.map_new(CW, CH) tcod.map_clear(grid, True, True) path = tcod.path_new_using_map(grid, 0) found = False for x in range(5): o = Obj(CW / 2 - x, CH / 2) snake.append(o) tcod.map_set_properties(grid, CW / 2 - x, CH / 2, False, False) if alive: # render snack tcod.console_set_default_foreground(CON, tcod.green) for s in snack: # collision if s.x == snake[0].x and s.y == snake[0].y: snack.remove(s) grow = True score += len(snake)
def reset_map(self, pos=None): """Reset light map. If pos is a list of Positions, only reset those areas""" if not self.light_enabled: libtcod.image_clear(self.__tcod_light_image, libtcod.black) libtcod.image_set_key_color(self.__tcod_light_image, libtcod.black) return assert not self.pos is None and not self.map is None, "resetting LightSource that is not placed on map" # [re-]calculating FOV of light within its map if pos is None: libtcod.map_clear(self.__tcod_light_map, False, False) cov = {} for o in self.map.find_all_within_r(self, Transparent, self.radius): # if there's something here already and it blocks light, light is blocked at pos if cov.get(o.pos, True): cov[o.pos] = not o.blocks_light() for (p, is_transparent) in cov.items(): # we're using the walkable bit to show that there is a tile that could be lit libtcod.map_set_properties(self.__tcod_light_map, self.radius + p.x - self.pos.x, self.radius + p.y - self.pos.y, is_transparent, True) else: if not isinstance(pos, list): pos = [pos] skip_calc = True for p in pos: if self.pos.distance_to(p) > self.radius: # pos isn't covered by this light; do nothing pass else: skip_calc = False is_transparent = True for o in self.map.find_all_at_pos(p): if isinstance(o, Transparent) and o.blocks_light(): is_transparent = False break libtcod.map_set_properties(self.__tcod_light_map, self.radius + p.x - self.pos.x, self.radius + p.y - self.pos.y, is_transparent, True) if skip_calc: # all pos were outside of light radius! return self.prepare_fov( False ) # TODO: calculate both True and False; use True only if light in LOS of player # use FOV data to create an image of light intensity, masked by opaque tiles # can optimise based on pos P: only need to recalculate area X # --- ---XX --- --XXX # / \ / XX / \ / XXX do this by splitting into quarters # | P| | PX | | | XXX and working out which to recalculate # | L | | L | | LP | | LPXX based on P-L # | | | | | | | XXX # \ / \ / \ / \ XXX # --- --- --- --XXX libtcod.image_clear(self.__tcod_light_image, libtcod.black) libtcod.image_set_key_color(self.__tcod_light_image, libtcod.black) r = self.radius rd2 = r / 2 i1 = self.raw_light_colour * self.intensity for x in range(r * 2 + 1): for y in range(r * 2 + 1): #print("(%d,%d)"%(x,y)) if libtcod.map_is_in_fov(self.__tcod_light_map, x, y): d = hypot(r - x, r - y) if d > rd2: libtcod.image_put_pixel(self.__tcod_light_image, x, y, i1 * (1.0 - (d - rd2) / rd2)) #print(" %s %s"%(d,i1*(1.0-(d-rd2)/rd2))) else: libtcod.image_put_pixel(self.__tcod_light_image, x, y, i1)
def get_fovmap(self,level): if level >= len(self.fovmaps): levelmap = mp.get_level(level) self.fovmaps.append(libtcod.map_new(len(levelmap), len(levelmap[0]))) libtcod.map_clear(self.fovmaps[-1]) return self.fovmaps[level]
def reset_map(self, pos=None): """Reset light map. If pos is a list of Positions, only reset those areas""" if not self.light_enabled: libtcod.image_clear(self.__tcod_light_image, libtcod.black) libtcod.image_set_key_color(self.__tcod_light_image, libtcod.black) return assert not self.pos is None and not self.map is None, "resetting LightSource that is not placed on map" # [re-]calculating FOV of light within its map if pos is None: libtcod.map_clear(self.__tcod_light_map, False, False) cov = {} for o in self.map.find_all_within_r(self, Transparent, self.radius): # if there's something here already and it blocks light, light is blocked at pos if cov.get(o.pos, True): cov[o.pos] = not o.blocks_light() for (p, is_transparent) in cov.items(): # we're using the walkable bit to show that there is a tile that could be lit libtcod.map_set_properties(self.__tcod_light_map, self.radius + p.x - self.pos.x, self.radius + p.y - self.pos.y, is_transparent, True) else: if not isinstance(pos, list): pos = [pos] skip_calc = True for p in pos: if self.pos.distance_to(p) > self.radius: # pos isn't covered by this light; do nothing pass else: skip_calc = False is_transparent = True for o in self.map.find_all_at_pos(p): if isinstance(o, Transparent) and o.blocks_light(): is_transparent = False break libtcod.map_set_properties(self.__tcod_light_map, self.radius + p.x - self.pos.x, self.radius + p.y - self.pos.y, is_transparent, True) if skip_calc: # all pos were outside of light radius! return self.prepare_fov(False) # TODO: calculate both True and False; use True only if light in LOS of player # use FOV data to create an image of light intensity, masked by opaque tiles # can optimise based on pos P: only need to recalculate area X # --- ---XX --- --XXX # / \ / XX / \ / XXX do this by splitting into quarters # | P| | PX | | | XXX and working out which to recalculate # | L | | L | | LP | | LPXX based on P-L # | | | | | | | XXX # \ / \ / \ / \ XXX # --- --- --- --XXX libtcod.image_clear(self.__tcod_light_image, libtcod.black) libtcod.image_set_key_color(self.__tcod_light_image, libtcod.black) r = self.radius rd2 = r / 2 i1 = self.raw_light_colour * self.intensity for x in range(r * 2 + 1): for y in range(r * 2 + 1): #print("(%d,%d)"%(x,y)) if libtcod.map_is_in_fov(self.__tcod_light_map, x, y): d = hypot(r - x, r - y) if d > rd2: libtcod.image_put_pixel(self.__tcod_light_image, x, y, i1 * (1.0 - (d - rd2) / rd2)) #print(" %s %s"%(d,i1*(1.0-(d-rd2)/rd2))) else: libtcod.image_put_pixel(self.__tcod_light_image, x, y, i1)
def render_all(): global fov_map, color_dark_wall, color_light_wall global color_dark_ground, color_light_ground #global fov_recompute if GameState.lighting_recompute or GameState.fov_recompute: for y in range(cfg.MAP_HEIGHT): for x in range(cfg.MAP_WIDTH): GameState.player.floor.Tiles[x][y].currentLight = lightsOffValue for o in GameState.objects: if (o.lightSource != None): libtcod.map_clear(GameState.fov_light_map) libtcod.map_compute_fov(GameState.fov_map, o.x, o.y, 0, cfg.FOV_LIGHT_WALLS, cfg.FOV_ALGO) else: continue for y in range(cfg.MAP_HEIGHT): for x in range(cfg.MAP_WIDTH): visible = libtcod.map_is_in_fov(GameState.fov_map, x, y) wall = GameState.player.floor.Tiles[x][y].block_sight distance = math.sqrt(pow(o.x - x,2) + pow(o.y - y,2)) if visible: #it's visible if wall: if distance != 0: GameState.player.floor.Tiles[x][y].currentLight += o.lightSource.value / distance else: GameState.player.floor.Tiles[x][y].currentLight += o.lightSource.value / 1 else: if distance != 0: GameState.player.floor.Tiles[x][y].currentLight += o.lightSource.value / distance else: GameState.player.floor.Tiles[x][y].currentLight += o.lightSource.value / 1 #since it's visible, explore it #GameState.player.floor.Tiles[x][y].explored = True GameState.lighting_recompute = False GameState.fov_recompute = True if GameState.fov_recompute: #recompute FOV if needed (the player moved or something) GameState.fov_recompute = False libtcod.map_compute_fov(GameState.fov_map, GameState.player.x, GameState.player.y, cfg.TORCH_RADIUS, cfg.FOV_LIGHT_WALLS, cfg.FOV_ALGO) #go through all tiles, and set their background color according to the FOV for y in range(cfg.MAP_HEIGHT): for x in range(cfg.MAP_WIDTH): light = Lights.LightValue() visible = libtcod.map_is_in_fov(GameState.fov_map, x, y) wall = GameState.player.floor.Tiles[x][y].block_sight light = GameState.player.floor.Tiles[x][y].currentLight + GameState.player.floor.Tiles[x][y].NaturalLight if light.Brightness < 1: visible = False if not visible: #if it's not visible right now, the player can only see it if it's explored if GameState.player.floor.Tiles[x][y].explored: if wall: libtcod.console_set_default_foreground(cfg.con, libtcod.Color(255,255,255)) libtcod.console_set_char_background(cfg.con, x, y, libtcod.Color(0,0,0), libtcod.BKGND_SET) libtcod.console_set_char(cfg.con, x, y,"#") else: libtcod.console_set_char_background(cfg.con, x, y, libtcod.Color(0,0,0), libtcod.BKGND_SET) libtcod.console_set_default_foreground(cfg.con, libtcod.Color(255,255,255)) libtcod.console_set_char(cfg.con, x, y,".") else: #it's visible if wall: libtcod.console_set_char(cfg.con, x, y," ") libtcod.console_set_char_background(cfg.con, x, y, (light/2).lightToLibtcodColor(), libtcod.BKGND_SET ) else: libtcod.console_set_char(cfg.con, x, y," ") libtcod.console_set_char_background(cfg.con, x, y, light.lightToLibtcodColor(), libtcod.BKGND_SET ) #since it's visible, explore it if(light.Brightness >= 1): GameState.player.floor.Tiles[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 GameState.objects: if object != GameState.player: object.draw() GameState.player.draw() #blit the contents of "con" to the root console libtcod.console_blit(cfg.con, 0, 0, cfg.MAP_WIDTH, cfg.MAP_HEIGHT, 0, 0, 0) #prepare to render the GUI panel libtcod.console_set_default_background(cfg.panel, libtcod.black) libtcod.console_clear(cfg.panel) #print the game messages, one line at a time y = 1 for (line, color) in GameState.game_msgs: libtcod.console_set_default_foreground(cfg.panel, color) libtcod.console_print_ex(cfg.panel, cfg.MSG_X, y, libtcod.BKGND_NONE, libtcod.LEFT,line) y += 1 #show the player's stats #TODO: Fix HP Bar render_bar(1, 1, cfg.BAR_WIDTH, 'HP', GameState.player.entity.hitPoints, GameState.player.entity.maxHitPoints, libtcod.light_red, libtcod.darker_red) libtcod.console_print_ex(cfg.panel, 1, 3, libtcod.BKGND_NONE, libtcod.LEFT, 'Dungeon level ' + str(GameState.dungeon.floors.index(GameState.player.floor))) #display names of objects under the mouse libtcod.console_set_default_foreground(cfg.panel, libtcod.light_gray) #libtcod.console_print_ex(cfg.panel, 1, 0, libtcod.BKGND_NONE, libtcod.LEFT, get_names_under_mouse()) libtcod.console_print_ex(cfg.panel, 1, 0, libtcod.BKGND_NONE, libtcod.LEFT, get_light_under_mouse()) #blit the contents of "panel" to the root console libtcod.console_blit(cfg.panel, 0, 0, cfg.SCREEN_WIDTH, cfg.PANEL_HEIGHT, 0, 0, cfg.PANEL_Y)