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 sleeping_gas(x, y, radius, duration): path = util.set_full_explore_map(game.current_map) libtcod.dijkstra_compute(path, x, y) for i in range(-radius, radius + 1): for j in range(-radius, radius + 1): if libtcod.dijkstra_get_distance(path, x + i, y + j) <= radius and libtcod.dijkstra_get_distance(path, x + i, y + j) >= 0: game.current_map.tile[x + i][y + j].update({'icon': game.current_map.tile[x + i][y + j]['icon'], 'back_light_color': libtcod.Color(115, 220, 225), 'back_dark_color': libtcod.Color(0, 143, 189), 'lerp': round(libtcod.random_get_float(game.rnd, 0, 1), 1), 'duration': game.turns + duration, 'type': 'sleep_gas'}) for obj in game.current_map.objects: if obj.item is None: if obj.x == x + i and obj.y == y + j: if obj.name == 'player': game.message.new('You are caught in a sleeping cloud!', game.turns) if 'sleep' not in game.player.flags: dice = util.roll_dice(1, 50) if dice > game.player.wisdom + (game.player.karma / 2): game.message.new('You fall asleep!', game.turns, libtcod.Color(0, 143, 189)) game.player.flags.append('sleep') else: if libtcod.map_is_in_fov(game.fov_map, obj.x, obj.y): game.message.new(obj.entity.article.capitalize() + obj.entity.get_name() + ' is caught in a sleeping cloud!', game.turns) if 'sleep' not in obj.entity.flags: dice = util.roll_dice(1, 3) if dice == 3: if libtcod.map_is_in_fov(game.fov_map, obj.x, obj.y): game.message.new(obj.entity.article.capitalize() + obj.entity.get_name() + ' falls asleep!', game.turns) obj.entity.flags.append('sleep')
def create_map_images(self, mode=0): if mode == 0: print 'Creating images....' t0 = libtcod.sys_elapsed_seconds() con = libtcod.console_new(game.WORLDMAP_WIDTH, game.WORLDMAP_HEIGHT) self.map_image_small = libtcod.image_new(game.WORLDMAP_WIDTH, game.WORLDMAP_HEIGHT) self.create_map_legend(con, mode) libtcod.image_scale(self.map_image_small, (game.SCREEN_WIDTH - 2) * 2, (game.SCREEN_HEIGHT - 2) * 2) if mode == 0: while self.player_positionx == 0: start = self.randomize('int', 0, (game.WORLDMAP_WIDTH * game.WORLDMAP_HEIGHT) - 1, 3) if int(self.hm_list[start] * 1000) in range(int(game.terrain['Forest']['elevation'] * 1000), int(game.terrain['Forest']['maxelev'] * 1000)): self.player_positionx = start % game.WORLDMAP_WIDTH self.player_positiony = start / game.WORLDMAP_WIDTH self.originx = self.player_positionx self.originy = self.player_positiony path = self.set_dijkstra_map() for y in range(game.WORLDMAP_HEIGHT): for x in range(game.WORLDMAP_WIDTH): dist = libtcod.dijkstra_get_distance(path, x, y) if dist > self.max_distance: self.max_distance = int(round(dist)) #libtcod.image_put_pixel(self.map_image_small, self.player_positionx, self.player_positiony, libtcod.white) if mode == 2: self.map_image_big = libtcod.image_from_console(con) libtcod.image_save(self.map_image_big, 'maps/worldmap-' + game.player.name + '.png') self.map_image_big = None libtcod.console_delete(con) if mode == 0: t1 = libtcod.sys_elapsed_seconds() print ' done! (%.3f seconds)' % (t1 - t0)
def set_threat_level(self, posx, posy, path=None): if path is None: path = self.set_dijkstra_map() dist = libtcod.dijkstra_get_distance(path, posx, posy) tlevel = int(math.ceil(dist / (self.max_distance / (game.MAX_THREAT_LEVEL + 2)))) if tlevel > game.MAX_THREAT_LEVEL: tlevel = game.MAX_THREAT_LEVEL return tlevel
def test_dijkstra(map_): path = libtcodpy.dijkstra_new(map_) libtcodpy.dijkstra_compute(path, *POINT_A) assert not libtcodpy.dijkstra_path_set(path, *POINT_C) assert libtcodpy.dijkstra_get_distance(path, *POINT_C) == -1 assert libtcodpy.dijkstra_path_set(path, *POINT_B) assert libtcodpy.dijkstra_size(path) assert not libtcodpy.dijkstra_is_empty(path) libtcodpy.dijkstra_reverse(path) for i in range(libtcodpy.dijkstra_size(path)): x, y = libtcodpy.dijkstra_get(path, i) while (x, y) != (None, None): x, y = libtcodpy.dijkstra_path_walk(path) libtcodpy.dijkstra_delete(path)