Пример #1
0
    bool GetOffsetArray_PatternSizeEvenArrayOdd_OnePadding(bool debug)
    {
        int pattern_size = 2;

        int[] input_linear = new int[]
        {
            1, 1, 0,
            1, 1, 0,
            1, 1, 0,
        };
        int[] output_expected = new int[]
        {
            1, 1, 0, 1,
            1, 1, 0, 1,
            1, 1, 0, 1,
            1, 1, 0, 1,
        };

        int[][] input_array = LinearArrayToSquare(input_linear, (int)Mathf.Sqrt(input_linear.Length));
        bool    result      = true;

        int[][] output_square = WFCInputOutput.GetOffsetArray(input_array, pattern_size);
        int[]   output_linear = SquareArrayToLinear(output_square);
        result = CompareLinearArrays(output_linear, output_expected);

        //Print the result if required
        if (debug)
        {
            Debug.Log("<color=blue> GetOffsetArray_PatternSizeEvenArrayOdd_OnePadding: " + result + "</color>\n" +
                      ReadIntArrayLinear(input_linear) + "\n" + ReadIntArrayLinear(output_linear) + "\n" + ReadIntArrayLinear(output_expected));
        }

        return(result);
    }
    IEnumerator Test()
    {
        //Get the tileset and dataset from the respective transforms
        unique_tileset = tileset_transform.GetComponentsInChildren<Tile>();
        dataset = dataset_transform.GetComponentsInChildren<Tile>();
        Debug.Log("<color=grey> Got tileset of size: " + unique_tileset.Length + ". Got dataset of size: " + dataset.Length + "</color>\n ");
        //input_unique = InputReader.GetInput(dataset);
        //Debug.Log("<color=green> Generated input: </color>\n " + ReadArrayInt(input_unique));
        input_constrained = InputReader.GetConstraintArray(dataset);
        Debug.Log("<color=green> Input grid: </color>\n " + ReadIntArraySquare(input_constrained));

        offset = WFCInputOutput.GetOffsetArray(input_constrained, pattern_size);
        Debug.Log("<color=green> Offset grid: </color>\n " + ReadIntArraySquare(offset));

        unique = WFCPattern.GetUniquePatterns(offset,pattern_size);
        pattern = WFCPattern.GetPatternArray(offset,pattern_size,unique);
        Debug.Log("<color=orange> Pattern grid output: </color>\n " + ReadIntArraySquare(pattern));

        unique = WFCPattern.GetNeighbors(pattern, pattern_size, unique);
        string log = "";
        for (int i = 0; i < unique.Count; i++)
            log += unique[i].GetValues() + "\n";
        Debug.Log("<color=orange> Unique patterns: </color> \n" + log);

        log = "";
        for (int i = 0; i < unique.Count; i++)
            log += unique[i].GetFrequency() + "\n";
        Debug.Log("<color=red> Frequency of unique patterns:</color> \n " + log);

        //log = "";
        //for (int i = 0; i < unique.Count; i++)
        //    log += unique[i].GetSides() + "\n";
        //Debug.Log("<color=red> Sides of unique patterns: \n</color> " + log);

        log = "";
        for (int i = 0; i < unique.Count; i++)
            log += "For pattern \n" + unique[i].GetValues() + "\n" + unique[i].GetNeighbors(unique) + "\n-----------\n";
        Debug.Log("<color=red> Neighbors of patterns: </color> " + log);

        //Output an array of patterns of X size according to a pattern list
        //Optional parameters to include a preset first cell
        yield return CollapseArray(output_size, unique);

        InstantiateOutput(output, unique_tileset, invalid_tile);

        Debug.Log("Finished test routine.");
        yield break;
    }
Пример #3
0
    bool TestTemplate(bool debug)
    {
        return(true);

        ///////////////////////////////////////////////         Input
        int pattern_size = 2;

        int[] input_linear = new int[]
        {
            1, 1, 0, 0,
            1, 1, 0, 0,
            1, 1, 0, 0,
            1, 1, 0, 0
        };
        int[][] input_array   = LinearArrayToSquare(input_linear, (int)Mathf.Sqrt(input_linear.Length));
        int[][] output_square = WFCInputOutput.GetOffsetArray(input_array, pattern_size);

        /////////////////////////////////////////               Expected output
        int[] output_expected = new int[]
        {
            1, 1, 0, 0, 1,
            1, 1, 0, 0, 1,
            1, 1, 0, 0, 1,
            1, 1, 0, 0, 1,
            1, 1, 0, 0, 1,
        };
        int[] output_linear = SquareArrayToLinear(output_square);

        //////////////////////////////////////                  Comparision
        bool result = true;

        result = CompareLinearArrays(output_linear, output_expected);

        //Print the result if required
        if (debug)
        {
            Debug.Log("<color=blue> GetCollapseArray_CollapseInitialCell_X: " + result + "</color>\n" +
                      ReadIntArrayLinear(input_linear) + "\n" + ReadIntArrayLinear(output_linear) + "\n" + ReadIntArrayLinear(output_expected));
        }

        return(result);
    }
    IEnumerator CollapseArray(int output_size, List<Pattern> all_patterns, int initial_pattern = -1, int initial_x = -1, int initial_y = -1)
    {
        //Setup useful lists and variables
        SetupCollapseArrays(output_size);

        if (all_patterns == null)
            Debug.LogError("Cannot collapse array with null patterns.");
        else if (all_patterns.Count <= 0)
            Debug.LogError("Cannot collapse array with no patterns.");
        else if (output_size <= 0)
            Debug.LogError("Cannot output array of invalid size.");
        else if (collapsing == null)
            Debug.LogError("Cannot collapse null collapsing array.");
        else if (entropy == null)
            Debug.LogError("Cannot collapse null entropy array.");

        #region WFC Algorithm
        //FIRST CELL COLLAPSE --------------------------------------------------------------------
        Vector2 initial_collapse = new Vector2(-1,-1);
        if (initial_pattern != -1)
            CollapseInitialCell(all_patterns,initial_pattern,initial_x, initial_y);
        else
            initial_collapse = WFCCollapse.CollapseHyperCell(collapsing, entropy, all_patterns);

        string log = ReadArrayList(collapsing);
        Debug.Log("<color=yellow> Tile array: </color> \n" + ReadIntArraySquare(input_constrained));
        Debug.Log("<color=cyan> Initial pattern array collapse: </color> \n" + log);

        //Log initial cell collapse
        output = WFCInputOutput.GetOutputArray(collapsing, unique, pattern_size);
        log = ReadIntArraySquare(output);
        Debug.Log("<color=green>Initial output array collapse: </color> \n" + log);

        //LOOP COLLAPSE --------------------------------------------------------------------
        //Loop until no left cells and result is valid
        bool is_valid = false;
        while (!is_valid)
        {
            for (int t = 0; t < 999; t++)
            {
                WFCCollapse.CollapseMostProbable(collapsing, entropy, all_patterns);

                //READ COLLAPSING
                //log = ReadArrayList(collapsing);
                //Debug.Log("<color=cyan> " + "Test" + " collapse: </color> \n" + log);

                //READ ENTROPY
                //log = ReadArray(entropy);
                // Debug.Log("<color=blue> " + "Test" + " collapse: </color> \n" + log);

                //READ OUTPUT
                output = WFCInputOutput.GetOutputArray(collapsing, unique, pattern_size);
                log = ReadIntArraySquare(output);
                //Debug.Log("<color=magenta> " + "Initial output array" + " collapse: </color> \n" + log);
                //result = InterpretOutput(output);

                //error here
                log = ReadArrayList(collapsing);
                Debug.Log("<color=green> " + t +" interpreted output array" + " collapse: </color> \n" + log);


                output = WFCInputOutput.GetOutputArray(collapsing, unique, pattern_size);
                yield return null;

                InstantiateOutput(output, unique_tileset, invalid_tile);
                yield return null;


                if (CheckValidity(entropy, output_size))
                    break;
            }
            yield return null;
            is_valid = CheckValidity(entropy, output_size);
        }
        //Creation was successful!
        //Generate the output
        output = WFCInputOutput.GetOutputArray(collapsing, unique, pattern_size);
        log = ReadIntArraySquare(output);
        Debug.Log("<color=magenta> " + "Output array" + " collapse: </color> \n" + log);
        #endregion
        yield break;
    }