Exemple #1
0
    void OnSceneGUI()
    {
        if (showDebugCoordinates || showDebugConnections)
        {
            PhysicalMap  physicalMap = this.targetInstance.GetPhysicalMap();
            VirtualMap[] virtualMaps = this.targetInstance.GetVirtualMaps();
            if (this.targetInstance.HasGeneratedDungeon())
            {
                if (physicalMap != null && virtualMaps.Length > 0)
                {
                    GUIStyle guiStyle = new GUIStyle();

                    if (showDebugCoordinates)
                    {
                        for (int i = 0; i < virtualMaps.Length; i++)
                        {
                            foreach (VirtualCell c in virtualMaps[i].GetAllCells())
                            {
                                Vector3 p = physicalMap.GetRealWorldPosition(c.location, i);
                                if (c.location.x % 2 == 1 && c.location.y % 2 == 1)
                                {
                                    guiStyle.normal.textColor = Color.yellow;
                                    int actual_x = (c.location.x - 1) / 2;
                                    int actual_y = (c.location.y - 1) / 2;
                                    Handles.Label(p, c.location.x + "," + c.location.y + "\n(" + actual_x + "," + actual_y + ")", guiStyle);
                                }
                                else
                                {
                                    guiStyle.normal.textColor = Color.white;
                                    Handles.Label(p, c.location.x + "," + c.location.y, guiStyle);
                                }
                            }
                        }
                    }

                    if (showDebugConnections)
                    {
                        for (int i = 0; i < virtualMaps.Length; i++)
                        {
                            VirtualMap          virtualMap          = virtualMaps[i];
                            List <CellLocation> unvisited_locations = new List <CellLocation>(virtualMap.WalkableLocations);
                            //Debug.Log("Starting from " + start_location);
                            int max_distance = virtualMap.GetMaximumDistance();

                            int stop_iter = 0;
                            while (unvisited_locations.Count > 0)
                            {
                                CellLocation start_location = unvisited_locations[0]; // We start from a walkable cell (doesn't matter which one)
                                VirtualCell  start_cell     = virtualMap.GetCell(start_location);

                                DrawConnections(start_cell, unvisited_locations, physicalMap, virtualMap, max_distance);

                                stop_iter++;
                                if (stop_iter == 100)
                                {
                                    DaedalusDebugUtils.Assert(false, "Looping in show debug connectsion!");
                                    break; // ERROR HERE!
                                }
                            }
                        }
                    }

                    // TEST path check
                    //VirtualMap test_virtualMap = virtualMaps[0];
                    //List<CellLocation> test_unvisited_locations = new List<CellLocation>(test_virtualMap.WalkableLocations);
                    //test_virtualMap.ExistsPathBetweenLocations(test_unvisited_locations[0], test_unvisited_locations[test_unvisited_locations.Count - 1]);
                }
            }
        }
    }
    public void ComputeStartAndEnd(VirtualMap map, VirtualMap vmapBelow)
    {
        List <CellLocation> iterable_locations;
        bool foundStart = false;

        if (forceStartAndEndInRooms)
        {
            iterable_locations = new List <CellLocation>(map.RoomWalkableLocations);
        }
        else
        {
            iterable_locations = new List <CellLocation>(map.WalkableLocations);
        }

        // Makes sure it is in some dead end, or in a room, if possible
        List <CellLocation> possible_start_locations = new List <CellLocation>();

        foreach (CellLocation l in iterable_locations)
        {
            if (map.IsDeadEnd(l) || map.IsInRoom(l))
            {
                possible_start_locations.Add(l);
            }
        }

        // If not possible, consider all locations equally
        if (possible_start_locations.Count == 0)
        {
            possible_start_locations = iterable_locations;
        }
        //		Debug.Log ("Possible start locations: " + possible_start_locations.Count);

        // TODO: Make an option for the start to be on the perimeter
//		foreach (CellLocation l in possible_start_locations) {
//			if (map.IsOnPerimeter(l) possible_start_locations
//		}


        // NOTE: we here find a start, but the algorithm already could have one! maybe use that?
        if (vmapBelow == null)
        {
            // Choose a random walkable cell as the starting point
            int index;
            index = DungeonGenerator.Random.Instance.Next(0, possible_start_locations.Count - 1);
            if (index != -1 && possible_start_locations.Count != 0)
            {
                map.start  = new CellLocation(possible_start_locations[index].x, possible_start_locations[index].y);
                foundStart = true;
            }
        }
        else
        {
            // Choose the cell above the below map's end as the starting point
            map.start  = vmapBelow.end;
            foundStart = true;
        }

        if (foundStart)
        {
            //Debug.Log ("CHOSEN START: " + map.start);
            // For this to work, we must compute the distance of all cells from the starting cell
            map.ComputeCellDistances(startCellLocation: map.start);

            // Choose a cell at a certain distance from the starting point as the ending point
            map.end = map.GetCellLocationInLimits(iterable_locations, minimumDistanceBetweenStartAndEnd / 100.0f * map.GetMaximumDistance(), maximumDistanceBetweenStartAndEnd / 100.0f * map.GetMaximumDistance());
        }

        //Debug.Log("START : " + map.start);
        //Debug.Log("END : " + map.end);
        //Debug.Log(map.GetWalkDistance(map.start,map.end));

        if (!foundStart)
        {
            Debug.LogError("Cannot find a suitable entrance!");
        }
        //DebugUtils.Assert(foundStart, "Cannot find a suitable entrance!");
    }