コード例 #1
0
    private void PickBestRoomLocation(VirtualMap map, VirtualRoom r)
    {//, int roomNumber){
        // Traverse all floor cells checking for the best position for a room
        int best_score = 1000000;
        int current_score;
        List <CellLocation> best_locations = new List <CellLocation>();
        List <CellLocation> locations      = new List <CellLocation>(map.floorCells);

        foreach (CellLocation map_l in locations)
        {
            r.leftCorner = map_l;
            if (IsRoomLocationValid(map, r))
            {
                current_score = 0;       // Lower is better
                for (int i = 0; i < r.Width; i++)
                {
                    for (int j = 0; j < r.Height; j++)
                    {
                        CellLocation possible_room_l = new CellLocation(r.leftCorner.x + 2 * i, r.leftCorner.y + 2 * j);
                        //						Debug.Log("Possible room l: " + possible_room_l);

                        // Corridor vicinity: good
                        if (map.IsSurroundedByWalls(possible_room_l) && map.HasAdjacentFloor(possible_room_l))
                        {
                            current_score += 1;
                        }

                        bool corridorOverlap = map.IsFloor(possible_room_l);

                        // Corridor overlap: bad (at default)
                        if (!forceRoomTransversal && corridorOverlap)
                        {
                            current_score += 3;
                        }

                        // or good if we want the room in the middle!
                        else if (forceRoomTransversal && !corridorOverlap)
                        {
                            current_score += 3;
                        }

                        // Room overlap: very very bad
                        if (map.IsRoomFloor(possible_room_l))
                        {
                            current_score += 100;
                        }

                        // If multi-storey, the first room should be placed above another room!
                        //						if (roomNumber == 0 && !belowMap.isRoomFloor(possible_room_l)) current_score += 5;

                        // TODO: may be more efficient to exit now if the score is already low enough!
                    }
                }

                if (current_score == 0)
                {
                    continue;                     // Zero is not a valid score, as it means the room is isolated
                }
                if (current_score == best_score)
                {
                    best_locations.Add(map_l);
                }
                else if (current_score < best_score)
                {
                    best_score = current_score;
                    best_locations.Clear();
                    best_locations.Add(map_l);
                }
            }
        }

        if (best_locations.Count == 0)
        {
            r.leftCorner = new CellLocation(-1, -1);
        }
        else
        {
            r.leftCorner = best_locations[DungeonGenerator.Random.Instance.Next(0, best_locations.Count - 1)];
        }
    }