def path_from_to(self, ox, oy, dx, dy): path = libtcod.path_new_using_function(MAP_WIDTH, MAP_HEIGHT, walk_compute, self.tiles, 1.41) libtcod.path_compute(path, ox, oy, dx, dy) return path
def test_astar_callback(map_, path_callback): astar = libtcodpy.path_new_using_function( libtcodpy.map_get_width(map_), libtcodpy.map_get_height(map_), path_callback, ) libtcodpy.path_compute(astar, *POINTS_AB) libtcodpy.path_delete(astar)
def move_towards(self, target_x, target_y): #Upgraded the move_towards to use the libtcod path algorythms and solving a bug #where the monsters "waited" near the doors #path = libtcod.path_new_using_map(fov_map,1) path = libtcod.path_new_using_function(MAP_WIDTH, MAP_HEIGHT, path_func, map, 1) libtcod.path_compute(path, self.x, self.y, target_x, target_y) (end_x,end_y) = libtcod.path_get(path, 0) dx = end_x - self.x dy = end_y - self.y self.move(dx, dy)
def initialize_fov(self): ##============================================================================ self.fov_recompute = True self.logger.log.info('Initializing FoV.') self.fov_map = libtcod.map_new(MAP_WIDTH, MAP_HEIGHT) self.path = libtcod.path_new_using_function(MAP_WIDTH,MAP_HEIGHT,path_callback,self) for y in range(MAP_HEIGHT): for x in range(MAP_WIDTH): libtcod.map_set_properties(self.fov_map, x, y, not self.Map.map[x][y].blocked, not self.Map.map[x][y].block_sight) self.gEngine.map_set_properties(x,y,not self.Map.map[x][y].blocked, not self.Map.map[x][y].block_sight) self.gEngine.console_clear(self.con)#unexplored areas start black (which is the default background color)
def __init__(self, size, avoid_solid_objects=False, consider_unexplored_blocked=False, allow_digging=False): self.path = libtcod.path_new_using_function(size.w, size.h, self.path_func, 1) self.consider_unexplored_blocked = consider_unexplored_blocked self.allow_digging = allow_digging self.avoid_solid_objects = avoid_solid_objects self._path_func = None
def Action_MoveTwoardsPlayer(self, attack = False, InternalParam = {}): #Adjaceny Test print "Adjacency Test" pX, pY = GameState.player.x, GameState.player.y dist = math.sqrt(math.pow(pX - self.x,2) + math.pow(pY - self.y,2)) if math.fabs(pX - self.x) < 1 and math.fabs(pY - self.y) <= 1: self.entity.melee_attack_entity(GameState.player.entity) return #if self.ObjectAdjacency(GameState.player): # self.entity.melee_attack_entity(GameState.player.entity) # return print "Passed" #Pov Test print "POV Test" if(self.Event_IsPlayerInPOV()): self.TargetLastSeenLocation = (GameState.player.x, GameState.player.y) if not self.move_towards(GameState.player.x, GameState.player.y): self.clearPath() self.TargetLastSeenPath = libtcod.path_new_using_function(cfg.MAP_WIDTH,cfg.MAP_WIDTH, self.path_func,(self.x,self.y)) libtcod.path_compute(self.TargetLastSeenPath,self.x,self.y,GameState.player.x,GameState.player.y) #self.TargetLastSeenPath = libtcod.path_new_using_map(GameState.fov_map) #Move Twoards Player elif self.TargetLastSeenPath and self.TargetLastSeenLocation != None: self.clearPath() self.TargetLastSeenPath = libtcod.path_new_using_function(cfg.MAP_WIDTH,cfg.MAP_WIDTH, self.path_func,(self.x,self.y)) x, y = self.TargetLastSeenLocation libtcod.path_compute(self.TargetLastSeenPath,self.x,self.y,x,y) if self.TargetLastSeenPath != None: x, y = self.TargetLastSeenLocation if(self.x, self.y) == (x,y): print "Giving Up Chase" self.clearPath() self.TargetLastSeenLocation = None return x,y = libtcod.path_walk(self.TargetLastSeenPath,False) if x != None and y != None: self.move_towards(x,y) print "Passed"
def initialize_fov(update=False): # print 'Loading/Generating map chunks...' # t0 = libtcod.sys_elapsed_seconds() game.traps = [] if not update: game.fov_map = libtcod.map_new(game.current_map.map_width, game.current_map.map_height) for y in range(game.current_map.map_height): for x in range(game.current_map.map_width): libtcod.map_set_properties(game.fov_map, x, y, not 'block_sight' in game.current_map.tile[x][y], 'explored' in game.current_map.tile[x][y] and not 'blocked' in game.current_map.tile[x][y]) else: for y in range(game.char.y - game.FOV_RADIUS, game.char.y + game.FOV_RADIUS): for x in range(game.char.x - game.FOV_RADIUS, game.char.x + game.FOV_RADIUS): if y < game.current_map.map_height and x < game.current_map.map_width: libtcod.map_set_properties(game.fov_map, x, y, not 'block_sight' in game.current_map.tile[x][y], 'explored' in game.current_map.tile[x][y] and not 'blocked' in game.current_map.tile[x][y]) if 'invisible' in game.current_map.tile[x][y] and game.current_map.tile[x][y]['type'] == 'trap' and libtcod.map_is_in_fov(game.fov_map, x, y): game.traps.append((x, y)) # compute paths using a*star algorithm game.path = libtcod.path_new_using_function(game.current_map.map_width, game.current_map.map_height, path_func) libtcod.path_compute(game.path, game.char.x, game.char.y, game.path_dx, game.path_dy)
def __init__(self, size, avoid_solid_objects = False, consider_unexplored_blocked = False, allow_digging = False): self.path = libtcod.path_new_using_function(size.w, size.h, self.path_func, 1) self.consider_unexplored_blocked = consider_unexplored_blocked self.allow_digging = allow_digging self.avoid_solid_objects = avoid_solid_objects self._path_func = None
def generate_river(): river_start_x = 0 river_start_y = 0 river_end_x = MAP_WIDTH - 1 river_end_y = MAP_HEIGHT - 1 river_noise = [[0 for y in range(MAP_WIDTH)] for x in range(MAP_HEIGHT)] for i in range(MAP_HEIGHT): river_noise[i] = map(lambda a: (2+a)**4, noise_map[i]) # scaled up def river_cost(xFrom, yFrom, xTo, yTo, river_noise): return river_noise[yTo][xTo] path = libtcod.path_new_using_function(MAP_WIDTH, MAP_HEIGHT, river_cost, river_noise) # wish I could just use # (lambda xFrom, yFrom, xTo, yTo, river_noise: river_noise[xTo][yTo]) libtcod.path_compute(path, river_start_x, river_start_y, river_end_x, river_end_y) while not libtcod.path_is_empty(path): x, y = libtcod.path_walk(path, True) g.level_map[y][x] = Tile(tile_types.SHALLOW_WATER) # make a wider river by making all the neighbors water # we need to initialize these separately, but I don't know why x_in_min = False x_in_max = False y_in_min = False y_in_max = False if x-1 >= 0: x_in_min = True if x+1 < MAP_WIDTH: x_in_max = True if y-1 >= 0: y_in_min = True if y+1 < MAP_HEIGHT: y_in_max = True # left if x_in_min: g.level_map[y][x-1] = Tile(tile_types.SHALLOW_WATER) # bottom left if y_in_min: g.level_map[y-1][x-1] = Tile(tile_types.SHALLOW_WATER) # top left if y_in_max: g.level_map[y+1][x-1] = Tile(tile_types.SHALLOW_WATER) # right if x_in_max: g.level_map[y][x+1] = Tile(tile_types.SHALLOW_WATER) # bottom right if y_in_min: g.level_map[y-1][x+1] = Tile(tile_types.SHALLOW_WATER) # top right if y_in_max: g.level_map[y+1][x+1] = Tile(tile_types.SHALLOW_WATER) # bottom if y_in_min: g.level_map[y-1][x] = Tile(tile_types.SHALLOW_WATER) # top if y_in_max: g.level_map[y+1][x] = Tile(tile_types.SHALLOW_WATER) # Iterate through all the tiles. Remove trees on shallow water tiles. for j in range(MAP_HEIGHT): for i in range(MAP_WIDTH): if g.level_map[j][i].type == tile_types.SHALLOW_WATER: for feature in g.terrain_features: if feature.x == i and feature.y == j: g.terrain_features.remove(feature)
def map_init_dungeon(width, height): def path_cost(xFrom, yFrom, xTo, yTo, alg_array): if alg_array[xTo][yTo] == 0: return 1 if alg_array[xTo][yTo] == 3: return 0.01 else: return 10 room_prefabs_10x10 = [] f = open('resources/map_prefabs/map_prefabs[10x10].csv', 'r').read().split('\n') # 10x10 for i in range(len(f[0]) // 10): for j in range(len(f) // 10): room = '' for y in range(10): for x in range(10): room += f[j * 10 + x][i * 10 + y] room_prefabs_10x10.append(room) room_prefabs_5x5 = [] f = open('resources/map_prefabs/map_prefabs[5x5].csv', 'r').read().split('\n') # 10x10 for i in range(len(f[0]) // 5): for j in range(len(f) // 5): room = '' for y in range(5): for x in range(5): room += f[j * 5 + x][i * 5 + y] room_prefabs_5x5.append(room) monsters_pool = [[game_content.m_slime], []] alg_array = [[0 for j in range(height)] for i in range(width)] terrain = [[0 for j in range(height)] for i in range(width)] items = [] entities = [] creatures = [] rooms = [] room_exits = [] room_connections = [] rooms_size = [(10, 10), (5, 5)] rooms.append((width // 2 - 3, height // 2 - 3, 6, 6)) for x in range(width // 2 - 3, width // 2 + 3): for y in range(height // 2 - 3, height // 2 + 3): if y == height // 2 and (x == width // 2 - 3 or x == width // 2 + 3): alg_array[x][y] = 7 room_exits.append((x, y, -1)) else: alg_array[x][y] = 2 available_spots = [ (x, y) for x in range(width) for y in range(height) if x > 6 and x < width - 12 and y > 6 and y < height - 12 ] for x in range(len(available_spots)): append = True i, j = available_spots.pop(random.randint(0, len(available_spots) - 1)) w, h = random.choice(rooms_size) newRoom = (i, j, w, h) #X, Y, W, H for room in rooms: if util.rectangle_intersects(newRoom, room): append = False if append == True: rooms.append(newRoom) for roomIndex in range(len(rooms))[0:]: room = rooms[roomIndex] if room[2] == 10 and room[3] == 10: room_layout = random.choice(room_prefabs_10x10) for x in range(room[2]): for y in range(room[3]): alg_array[x + room[0]][y + room[1]] = int( room_layout[x * 10 + y]) if int(room_layout[x * 10 + y]) == 7: room_exits.append( (x + room[0], y + room[1], roomIndex)) elif room[2] == 5 and room[3] == 5: room_layout = random.choice(room_prefabs_5x5) for x in range(room[2]): for y in range(room[3]): alg_array[x + room[0]][y + room[1]] = int( room_layout[x * 5 + y]) if int(room_layout[x * 5 + y]) == 7: room_exits.append( (x + room[0], y + room[1], roomIndex)) for exit_init in room_exits: path = libtcodpy.path_new_using_function(width, height, path_cost, alg_array, 0) other_exits = sorted([ exit_other for exit_other in room_exits if exit_other[2] != exit_init[2] and ( exit_other[2], exit_init[2]) not in room_connections ], key=lambda e: util.simpledistance( (exit_init[0], exit_init[1]), (e[0], e[1]))) if len(other_exits) > 0: exit_end = other_exits[0] else: exit_end = sorted([ exit_other for exit_other in room_exits if exit_other[2] != exit_init[2] ], key=lambda e: util.simpledistance( (exit_init[0], exit_init[1]), (e[0], e[1])))[0] room_connections.append((exit_init[2], exit_end[2])) room_connections.append((exit_end[2], exit_init[2])) libtcodpy.path_compute(path, exit_init[0], exit_init[1], exit_end[0], exit_end[1]) for i in range(libtcodpy.path_size(path) - 1): x, y = libtcodpy.path_get(path, i) alg_array[x][y] = 3 for x in range(len(alg_array)): for y in range(len(alg_array[x])): if alg_array[x][y] in [0, 1]: terrain[x][y] = game_content.t_cave_wall(x, y) else: terrain[x][y] = game_content.t_cave_floor(x, y) if alg_array[x][y] == 4: creatures.append( random.choice(monsters_pool[GAME.level])(x, y)) if alg_array[x][y] == 7: entities.append( game_content.n_door( x, y, game_content.SPRITESHEET_ENTITIES.image_at( (0, 32, 32, 32)), game_content.SPRITESHEET_ENTITIES.image_at( (32, 32, 32, 32), colorkey=game_constants.COLOR_COLORKEY))) terrain[x][y].passable = False terrain[x][y].transparent = False return terrain, items, entities, creatures
def path_new_sound(self, pathData): return libtcod.path_new_using_function( ROOMW, ROOMH, self.path_get_cost_sound, pathData, 1.41 )
def path_new_movement(self, pathData): return libtcod.path_new_using_function( ROOMW, ROOMH, self.path_get_cost_movement, pathData, 1.41 )