예제 #1
0
    def load_map_template(self, template_name):
        # Open map template
        with open(path.join(assets.map_folder, template_name), "rt") as f:

            # Construct tiles from map file
            for y, row in enumerate(f):
                row = row.strip("\n")
                # count map height
                self.tile_height += 1
                self.tile_width = 0

                for x, tile in enumerate(row):
                    # count map width (lazy mf)
                    self.tile_width += 1

                    # fog is everywhere
                    self.fog_data[(x, y)] = tiles.Fog(self, (x, y))

                    new_tile = None
                    if tile == "T":  # Forest
                        new_tile = tiles.Forest(self, (x, y))
                        new_tile.add_resource(entities.WildTree, 5)

                    elif tile == "V":  # Water
                        new_tile = tiles.Water(self, (x, y))
                        self.unpassable_tiles.append((x, y))

                    elif tile == "G":  # Bog
                        new_tile = tiles.Bog(self, (x, y))

                    elif tile == "B":  # Mountain
                        new_tile = tiles.Mountain(self, (x, y))
                        self.unpassable_tiles.append((x, y))

                    elif tile == "M":  # Ground
                        new_tile = tiles.Ground(self, (x, y))

                    self.tile_data[(x, y)] = new_tile

        for i in range(0, 1000):
            tile = self.get_random_background_tile()
            while not tile.passable:
                tile = self.get_random_background_tile()
            tile.add_resource(entities.WildIronOre, 1)

        self.width = self.tile_width * g_vars["Game"]["TileSize"]
        self.height = self.tile_height * g_vars["Game"]["TileSize"]
        camera.set_resolution(self.width, self.height)

        self.weighted_graph = alg.WeightedGraph(self)
예제 #2
0
 def astar_cost(self):
     path, cost = alg.Astar(alg.WeightedGraph(self), self.custom_start,
                            self.custom_goal)
     return cost
예제 #3
0
    def update(self):
        self.sprite_group_all.update()

        # catch inputs
        keystate = pg.key.get_pressed()
        if keystate[pg.K_ESCAPE]:
            pg.event.post(pg.event.Event(pg.QUIT))

        if keystate[pg.K_TAB]:
            self.sprite_group_all.empty()
            self.tilemap.randomize_start_goal(self)

        if keystate[pg.K_KP0]:
            self.neural.train()

        if keystate[pg.K_KP1]:
            self.neural.save(assets.neural_network_model_folder)

        if keystate[pg.K_KP2]:
            self.neural.load(assets.neural_network_model_folder)

        if keystate[pg.K_q]: # BFS
            self.pathqueue = None
            self.path = alg.BFS(alg.SquareGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal)

        if keystate[pg.K_a]: # Visual BFS
            self.path = alg.BFS(alg.SquareGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal)
            self.visual_helper()

        if keystate[pg.K_w]: # DFS
            self.pathqueue = None
            self.path = alg.DFS(alg.SquareGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal)

        if keystate[pg.K_s]: # Visual DFS
            self.path = alg.DFS(alg.SquareGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal)
            self.visual_helper()

        if keystate[pg.K_e]: # Dijkstra
            self.pathqueue = None
            self.path = alg.Dijkstra(alg.WeightedGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal)

        if keystate[pg.K_d]: # Visual Dijkstra
            self.path = alg.Dijkstra(alg.WeightedGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal)
            self.visual_helper()

        if keystate[pg.K_z]: # Dijkstra - only path
            self.pathqueue = None
            d_path = alg.Dijkstra(alg.WeightedGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal)
            self.path = alg.ReconstructPath(d_path, self.tilemap.custom_start, self.tilemap.custom_goal)

        if keystate[pg.K_x]: # Visual Dijkstra - only path
            d_path = alg.Dijkstra(alg.WeightedGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal)
            self.path = alg.ReconstructPath(d_path, self.tilemap.custom_start, self.tilemap.custom_goal)
            self.visual_helper(True)

        if keystate[pg.K_r]: # Astar
            self.pathqueue = None
            self.path, cost = alg.Astar(alg.WeightedGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal)

        if keystate[pg.K_f]: # Visual Astar
            self.path, cost = alg.Astar(alg.WeightedGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal)
            self.visual_helper()

        if keystate[pg.K_c]: # Astar - only path
            self.pathqueue = None
            d_path, cost = alg.Astar(alg.WeightedGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal)
            self.path = alg.ReconstructPath(d_path, self.tilemap.custom_start, self.tilemap.custom_goal)

        if keystate[pg.K_v]: # Visual Astar - only path
            d_path, cost = alg.Astar(alg.WeightedGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal)
            self.path = alg.ReconstructPath(d_path, self.tilemap.custom_start, self.tilemap.custom_goal)
            self.visual_helper(True)

        if keystate[pg.K_t]: # Astar NN
            self.pathqueue = None
            self.path, cost = alg.Astar(alg.WeightedGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal, self.neural)

        if keystate[pg.K_g]: # Visual Astar NN
            self.path, cost = alg.Astar(alg.WeightedGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal, self.neural)
            self.visual_helper()

        if keystate[pg.K_p]: # test

            num_tests=50

            def bfs_test():
                self.sprite_group_all.empty()
                self.tilemap.randomize_start_goal(self)
                self.pathqueue = None
                self.path = alg.BFS(alg.SquareGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal)

            def dfs_test():
                self.sprite_group_all.empty()
                self.tilemap.randomize_start_goal(self)
                self.pathqueue = None
                self.path = alg.DFS(alg.SquareGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal)

            def dijkstra_test():
                self.sprite_group_all.empty()
                self.tilemap.randomize_start_goal(self)
                self.pathqueue = None
                self.path = alg.Dijkstra(alg.WeightedGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal)

            def astar_test():
                self.sprite_group_all.empty()
                self.tilemap.randomize_start_goal(self)
                self.pathqueue = None
                self.path, cost = alg.Astar(alg.WeightedGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal)

            def neural_test():
                self.sprite_group_all.empty()
                self.tilemap.randomize_start_goal(self)
                self.pathqueue = None
                self.path, cost = alg.Astar(alg.WeightedGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal, self.neural)

            bfs_time = timeit.timeit(bfs_test, number=num_tests) / num_tests
            print("BFS: " + str(bfs_time))
            dfs_time = timeit.timeit(dfs_test, number=num_tests) / num_tests
            print("DFS: " + str(dfs_time))
            # dijkstra_time = timeit.timeit(dijkstra_test, number=num_tests) / num_tests
            # print("Dijkstra: " + str(dijkstra_time))
            astar_time = timeit.timeit(astar_test, number=num_tests) / num_tests
            print("Astar: " + str(astar_time))
            neural_time = timeit.timeit(neural_test, number=num_tests) / num_tests
            print("Neural: " + str(neural_time) + "\n\n")
예제 #4
0
 def neural_test():
     self.sprite_group_all.empty()
     self.tilemap.randomize_start_goal(self)
     self.pathqueue = None
     self.path, cost = alg.Astar(alg.WeightedGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal, self.neural)
예제 #5
0
 def dijkstra_test():
     self.sprite_group_all.empty()
     self.tilemap.randomize_start_goal(self)
     self.pathqueue = None
     self.path = alg.Dijkstra(alg.WeightedGraph(self.tilemap), self.tilemap.custom_start, self.tilemap.custom_goal)