Ejemplo n.º 1
0
    public static CellEntranceExit Create(Cell cell, CellEntranceExit otherSide, ErrorCollector errors)
    {
        CellEntranceExit entranceExit = new CellEntranceExit();

        entranceExit.Other = otherSide;
        otherSide.Other    = entranceExit;

        entranceExit.Width = otherSide.Width;
        int tries = 0;

        entranceExit.Side = RandomDirection(otherSide.Side);
        while (!EntranceFits(cell, entranceExit))
        {
            if (tries > 10)
            {
                errors.Add(new System.Exception("Unable to fit the EntranceExit."));
                return(null);
            }
            entranceExit.Side = RandomDirection();
            tries++;
        }

        PositionRandomly(cell, entranceExit, errors);
        return(entranceExit);
    }
Ejemplo n.º 2
0
    private static void SetPosition(Cell cell, CellEntranceExit entrance, int x, int y, int offset, bool searchX)
    {
        Vector2Int position;

        if (searchX)
        {
            position = new Vector2Int(x + offset, y);
        }
        else
        {
            position = new Vector2Int(x, y + offset);
        }

        entrance.Position = position;


        for (int j = 0; j < entrance.Width; j++)
        {
            int x1 = x, y1 = y;
            if (searchX)
            {
                x1 += offset + j;
            }
            else
            {
                y1 += offset + j;
            }

            cell.Set(x1, y1, TileType.ENTRANCE);
            entrance.coordinates.Add(new Vector2Int(x1, y1));
        }
    }
Ejemplo n.º 3
0
    public static Path GeneratePath(Cell cell, CellEntranceExit entrance, CellEntranceExit exit, ErrorCollector errors)
    {
        // A*

        Path path     = null;
        bool complete = false;

        HashSet <Node> openNodes   = new HashSet <Node>();
        List <Node>    closedNodes = new List <Node>();


        Vector2Int target = exit.Position + (exit.Side.Opposite().Vector()) * 2;

        Node startNode = new Node(entrance);

        SetCost(startNode, target);
        openNodes.Add(startNode);

        int depth = 0;


        while (openNodes.Count > 0 && complete == false && depth < 100000f)
        {
            Node node = GetLowestCostNode(openNodes);
            openNodes.Remove(node);
            closedNodes.Add(node);

            List <Node> newNodes = PerformStep(cell, node, target);
            foreach (Node newNode in newNodes)
            {
                if (IsComplete(newNode, target))
                {
                    if (newNode.directionCombo < MINIMUM_COMBO)
                    {
                        continue;
                    }

                    path     = CreatePath(newNode, exit);
                    complete = true;
                }
                else
                {
                    openNodes.Add(newNode);
                }
            }
            //closedNodes.Add();
            depth++;
        }

        if (!complete)
        {
            errors.Add(new System.Exception("Could not generate a path! " + startNode.position + " -> " + target));
            path       = new Path();
            path.nodes = closedNodes;
        }


        return(path);
    }
Ejemplo n.º 4
0
    private void CreateRoad(Cell cell, CellEntranceExit entrance, CellEntranceExit exit, ErrorCollector errors)
    {
        Path path = PathGenerator.GeneratePath(cell, entrance, exit, errors);

        errors.GoBoom();

        Road road = new Road(path);

        cell.AddRoad(road);
    }
Ejemplo n.º 5
0
    private void GenerateIntersection(Cell cell)
    {
        int roadCount = Random.Range(3, 4);

        int  generateRoads = 0;
        uint width         = 2;

        ErrorCollector errors = new ErrorCollector();

        int tries = 0;

        // Create new paths
        while (generateRoads < roadCount && tries < 25)
        {
            errors.Clear();
            tries++;

            // Place entrance
            CellEntranceExit entrance = CellEntranceExit.Create(cell, width, errors);
            errors.GoBoom();

            // Place exit
            CellEntranceExit exit = null;
            int exitTries         = 0;
            while (exitTries < 5)
            {
                exitTries++;
                exit = CellEntranceExit.Create(cell, entrance, errors);
                errors.GoBoom();
                break;
                //if (errors.Count > 0)
                //{
                //  entrance.Remove(cell);
                //break;
                //}
            }
            errors.GoBoom();

            int roadTries = 0;
            while (roadTries < 20)
            {
                errors.Clear();
                roadTries++;
                CreateRoad(cell, entrance, exit, errors);
                errors.GoBoom();

                break;
            }

            generateRoads++;
        }
    }
Ejemplo n.º 6
0
    private static Path CreatePath(Node node, CellEntranceExit exit)
    {
        Path        path  = new Path();
        List <Node> nodes = new List <Node>();

        node = new Node(node, exit.Side.Vector());
        node = new Node(node, exit.Side.Vector());

        nodes.Add(node);
        while (node.parent != null)
        {
            node = node.parent;
            nodes.Add(node);
        }

        nodes.Reverse();
        path.nodes = nodes;
        return(path);
    }
Ejemplo n.º 7
0
    public static CellEntranceExit Create(Cell cell, uint width, ErrorCollector errors)
    {
        CellEntranceExit entrance = new CellEntranceExit();

        entrance.Width = width;
        int tries = 0;

        entrance.Side = RandomDirection();
        while (!EntranceFits(cell, entrance))
        {
            if (tries > 10)
            {
                errors.Add(new System.Exception("Unable to fit the entrance."));
                return(null);
            }
            entrance.Side = RandomDirection();
            tries++;
        }

        PositionRandomly(cell, entrance, errors);
        return(entrance);
    }
Ejemplo n.º 8
0
    private static void PositionRandomly(Cell cell, CellEntranceExit entrance, ErrorCollector errors)
    {
        int  x, y;
        bool searchX;

        switch (entrance.Side)
        {
        case Direction.NORTH:
            x       = 0;
            y       = (int)cell.Size - 1;
            searchX = true;
            break;

        case Direction.SOUTH:
            x       = 0;
            y       = 0;
            searchX = true;
            break;

        case Direction.WEST:
            x       = 0;
            y       = 0;
            searchX = false;
            break;

        case Direction.EAST:
            x       = (int)cell.Size - 1;
            y       = 0;
            searchX = false;
            break;

        default:
            return;
        }


        // Try to randomly place
        int tries = 0;

        while (tries < 10)
        {
            int count = 0;
            int index = Random.Range(EDGE_MARGIN, (int)(cell.Size - entrance.Width - EDGE_MARGIN));
            for (int i = 0; i < entrance.Width; i++)
            {
                int x1 = x, y1 = y;

                if (searchX)
                {
                    x1 += i + index;
                }
                else
                {
                    y1 += i + index;
                }

                if (cell.Get(x1, y1) == 0)
                {
                    count++;
                }
                else
                {
                    break;
                }
            }
            if (count == entrance.Width)
            {
                SetPosition(cell, entrance, x, y, index, searchX);
                return;
            }
            tries++;
        }

        // Give up and just place it in the first possible spot
        for (int i = EDGE_MARGIN; i < cell.Size - entrance.Width - EDGE_MARGIN; i++)
        {
            int count = 0;
            for (int j = 0; j < entrance.Width; j++)
            {
                int x1 = x, y1 = y;

                if (searchX)
                {
                    x1 += i + j;
                }
                else
                {
                    y1 += i + j;
                }

                if (cell.Get(x1, y1) == 0)
                {
                    count++;
                }
                else
                {
                    break;
                }
            }
            if (count == entrance.Width)
            {
                SetPosition(cell, entrance, x, y, i, searchX);
                return;
            }
        }
        errors.Add(new System.Exception("Unable to place the EntranceExit."));
    }
Ejemplo n.º 9
0
    private static bool EntranceFits(Cell cell, CellEntranceExit entrance)
    {
        int  x, y;
        bool searchX;

        switch (entrance.Side)
        {
        case Direction.NORTH:
            y       = (int)cell.Size - 1;
            x       = 0;
            searchX = true;
            break;

        case Direction.SOUTH:
            y       = 0;
            x       = 0;
            searchX = true;
            break;

        case Direction.EAST:
            y       = 0;
            x       = 0;
            searchX = false;
            break;

        case Direction.WEST:
            y       = 0;
            x       = (int)cell.Size - 1;
            searchX = false;
            break;

        default:
            return(false);
        }

        for (int i = EDGE_MARGIN; i < cell.Size - entrance.Width - EDGE_MARGIN; i++)
        {
            int count = 0;
            for (int j = 0; j < entrance.Width; j++)
            {
                int x1 = x, y1 = y;

                if (searchX)
                {
                    x1 += i + j;
                }
                else
                {
                    y1 += i + j;
                }

                if (cell.Get(x1, y1) == 0)
                {
                    count++;
                }
                else
                {
                    break;
                }
            }
            if (count == entrance.Width)
            {
                return(true);
            }
        }
        return(false);
    }
Ejemplo n.º 10
0
 public Node(CellEntranceExit entrance)
 {
     this.position = entrance.Position;
     direction     = entrance.Side.Opposite();
 }