Ejemplo n.º 1
0
    public void SetupWorld(int w, int h)
    {
        jobQueue   = new JobQueue();
        characters = new List <Character>();
        furnitures = new List <Furniture>();

        inventoryManager = new InventoryManager();

        rooms = new List <Room>();
        rooms.Add(new Room(this));
        Width  = w;
        Height = h;

        tiles = new Tile[Width, Height];

        for (int x = 0; x < Width; x++)
        {
            for (int y = 0; y < Height; y++)
            {
                tiles[x, y] = new Tile(this, x, y);
                tiles[x, y].RegisterTileChangedCallback(OnTileChanged);
                tiles[x, y].room = GetOutsideRoom(); //room "0" is always outside
            }
        }
        tileGraph = new PathTileGraph(this);
        Debug.Log("world created with " + (Width * Height) + " tiles.");

        CreateFurniturePrototypes();
    }
Ejemplo n.º 2
0
    public void MakePathTest()
    {
        world.SetupPathFindingTest();

        PathTileGraph tileGraph = new PathTileGraph(world);

        PlaceInventory();
    }
Ejemplo n.º 3
0
    void SetupWorld(int width, int height)
    {
        Width  = width;
        Height = height;
        tiles  = new Tile[Width, Height];

        for (int x = 0; x < Width; x++)
        {
            for (int z = 0; z < Height; z++)
            {
                tiles[x, z] = new Tile(this, x, z);
            }
        }
        TileGraph = new PathTileGraph(this);
    }
Ejemplo n.º 4
0
 // This should be called whenever a change to the world
 // means that our old pathfinding info is invalid.
 public void InvalidateTileGraph()
 {
     TileGraph = null;
 }
Ejemplo n.º 5
0
    public PathAStar(World world, Tile start_tile, Tile end_tile)
    {
        //all walkable nodes
        PathTileGraph tile_graph = world.get_tile_graph();

        if (tile_graph == null)
        {
            Debug.LogError("the tile graph does not exist");
            this.failed = true;
            return;
        }

        PathNode <Tile> start_node = tile_graph.get_node_by_tile(start_tile);
        PathNode <Tile> end_node   = tile_graph.get_node_by_tile(end_tile);

        //ensure start end end tiles are in the graph
        if (start_node == null)
        {
            Debug.LogError("tile graph does not contain starting tile");
            this.failed = true;
            return;
        }

        if (end_node == null)
        {
            this.failed = true;
            Debug.LogError("tile graph does not contain ending tile");
            return;
        }

        List <PathNode <Tile> > closed_set = new List <PathNode <Tile> > ();
        //List<PathNode<Tile>> open_set = new List<PathNode<Tile>> ();
        BinaryHeap <PathNode <Tile> > open_set = new BinaryHeap <PathNode <Tile> >();

        Dictionary <PathNode <Tile>, PathNode <Tile> > came_from = new Dictionary <PathNode <Tile>, PathNode <Tile> > ();

        //set the base g-score for each node
        Bag <float> g_scores = new Bag <float> ();

        for (int i = 0; i < tile_graph.num_nodes(); i++)
        {
            PathNode <Tile> node = tile_graph.get_node_by_id(i);

            if (node == null)
            {
                continue;
            }

            g_scores.set(node.data.id, Mathf.Infinity);
        }
        //set g-score of starting node
        g_scores.set(start_tile.id, 0f);

        //set the base f-scores for each node
        Bag <float> f_scores = new Bag <float> ();

        for (int i = 0; i < tile_graph.num_nodes(); i++)
        {
            PathNode <Tile> node = tile_graph.get_node_by_id(i);

            if (node == null)
            {
                continue;
            }

            f_scores.set(node.data.id, Mathf.Infinity);
        }

        //set f-score of starting node
        f_scores.set(start_node.data.id, heuristic_cost_estimate(start_node, end_node));

        //add start node to open set
        open_set.add(f_scores [start_node.data.id], start_node);

        while (open_set.size > 0)
        {
            HeapCell <PathNode <Tile> > current = open_set.remove_first();

            if (current.data.data == end_node.data)
            {
                //we found the path, build it and return
                reconstruct_path(came_from, end_node);
                return;
            }

            //add current to closed set
            closed_set.Add(current.data);

            if (current.data.edges == null)
            {
                Debug.LogError("PathAStar :: Bad Edges...");
                continue;
            }

            foreach (PathEdge <Tile> neighbor in current.data.edges)
            {
                //continue if this edge is already in closed set
                if (closed_set.Contains(neighbor.node) == true)
                {
                    continue;
                }

                float tentative_g_score = g_scores [current.data.data.id] + dist_between(current.data, neighbor.node);

                int pos = heap_contains(neighbor.node, open_set);
                if (pos < 0)
                {
                    //f_scores.set (neighbor.node.data.id, tentative_g_score + heuristic_cost_estimate (neighbor.node, end_node));
                    //g_scores.set (neighbor.node.data.id, tentative_g_score);
                    open_set.add(f_scores [neighbor.node.data.id], neighbor.node);
                }
                else if (tentative_g_score >= g_scores [neighbor.node.data.id])
                {
                    continue;                     // not the best path
                }
                //set where you came from
                if (came_from.ContainsKey(neighbor.node) == false)
                {
                    came_from.Add(neighbor.node, current.data);
                }
                else
                {
                    came_from [neighbor.node] = current.data;
                }

                g_scores.set(neighbor.node.data.id, tentative_g_score);
                f_scores.set(neighbor.node.data.id, tentative_g_score + heuristic_cost_estimate(neighbor.node, end_node));
                open_set.add(f_scores [neighbor.node.data.id], neighbor.node);
            }
        }

        //failure
        failed = true;
    }
Ejemplo n.º 6
0
 void InvalidateTileGraph()
 {
     tileGraph = null;
 }
Ejemplo n.º 7
0
    public void DoPathfindingTest()
    {
        WorldController.Instance.World.SetupPathfindingExample();

        PathTileGraph tileGraph = new PathTileGraph(WorldController.Instance.World);
    }