Example #1
0
    /**
     * Finds all hexes that are adjacent to any hex in prev_layer at a given radius from the hexes in prev_layer, that
     * do not exist in total, with no duplicates. Ideally, prev_layer is a subset of total. Passing a non-positive
     * radius will yield no change. Variation should be a float between 0.0 and 1.0 inclusive coupled with a static decay
     * value that will reduce the probability on susequent layer calls. Contrary to logic, the greater the variability
     * the greater the chance of a uniform shape being formed from the hexes in total.
     */
    private void addNextLayer(List<HexScript> prev_layer, List<HexScript> total, int radius, Probability variation)
    {
        // based case
        if (radius <= 0) { return; }

        List<HexScript> cur_layer = new List<HexScript>();
        // Find hexes, which have not already been found, adjacent to any hex in the previous layer
        foreach (HexScript hex in prev_layer) {
            // find all adjacent hexes
            for (int pos = 0; pos < 6; ++pos) {
                HexScript adj_hex = adjacentHexTo(hex, pos);

                if ( (variation == null || UnityEngine.Random.value <= variation.getProbability()) && adj_hex != null && !total.Contains(adj_hex)) {
                    cur_layer.Add(adj_hex);
                    total.Add(adj_hex); // add to total as well
                }
            }
        }
        // avoid cutting off the generation of the area because of an empty layer
        if (cur_layer.Count == 0) { cur_layer = prev_layer; }

        if (variation != null) { variation.reduce(); }
        // find the next layer
        addNextLayer(cur_layer, total, radius - 1, variation);
    }