//short names for collections of biome_tags that should be valid for the biome
const BIOMETAGGROUPS = {
    land: {
        none_of: [
            "ocean",
            "river"
        ]
    }
};
const STRUCTURES = [
    {
        path: "wonder_tech:oil_vein",
        biomeGroup: "land",
        yOffset: 0,
        footprint: [
            15,
            15
        ],
        weight: 1,
        placeUnderwater: false,
        minSpawnHeight: 64
    }
];
/**
 * List of ores and their spawn rules
 * @param {string} block The block ID to place
 * @param {number} tries Number of attempts to seed this ore
 * @param {number} 0.0-1.0 chance for the try to succeed
 * @param {number} min_size The smallest amount of blocks to place in the vein
 * @param {number} max_size The largest amount of blocks to place in the vein
 * @param {number} max_height The highest point in the chunk to seed the vein
 * @param {number} min_height The lowest point in the chunk to seed the vein
 * @param {number} height_offset -0.5-0.5 the point in the height range from the center to spawn with highest frequency
 * @param {number} distribution_fallof How sharp the dropoff of spawn range from the center. 1 is even distribution
 */ export const ORES_OVERWORLD = [];
export const ORES_NETHER = [];
export const ORES_END = [];
/**
 * Return a random number weighted towards 0.5
 * @param {number} offset A number between -0.5 and 0.5 to shift the peak of the curve towards
 */ export function guassianRand(offset, falloff) {
    if (offset == undefined) offset = 0;
    if (falloff == undefined) falloff = 2;
    var res = 0;
    var i = falloff;
    while(i--)res += Math.max(0, Math.min(1, Math.random() + offset));
    return res / falloff;
}
/**
* A JSON object with a list of structures and rules about their spawning
* @param {Array[string]} biomeTags a list of biome tags the entity has
* @return {path, chunkSize} path The path to the structure file
*/ export function rollStructure(biomeTags) {
    //build the list of biometaggroups and whether they are valid
    var validBiomeTagGroups = {};
    var chosenBiomeGroup = "none";
    for(var key in BIOMETAGGROUPS){
        validBiomeTagGroups[key] = {};
        validBiomeTagGroups[key].valid = undefined;
    }
    //work out which biometagroup we are in
    for(var key in validBiomeTagGroups){
        var allOfLength = 0;
        var allOfCount = 0;
        var allOfValid = true;
        var anyOfValid = false;
        if (BIOMETAGGROUPS[key].any_of == undefined) anyOfValid = true;
        var noneOfValid = true;
        if (BIOMETAGGROUPS[key].all_of != undefined) allOfLength = BIOMETAGGROUPS[key].all_of.length;
        for (var biomeTag of biomeTags){
            var tag = biomeTag.replace("biometag_", "");
            if (BIOMETAGGROUPS[key].any_of != undefined && BIOMETAGGROUPS[key].any_of.includes(tag) && validBiomeTagGroups[key].valid != false) {
                anyOfValid = true;
            }
            if (BIOMETAGGROUPS[key].all_of != undefined && BIOMETAGGROUPS[key].all_of.includes(tag)) {
                allOfCount++;
            }
            if (BIOMETAGGROUPS[key].none_of != undefined && BIOMETAGGROUPS[key].none_of.includes(tag)) {
                noneOfValid = false;
                break;
            }
        }
        if (allOfCount != allOfLength) {
            allOfValid = false;
        }
        validBiomeTagGroups[key].valid = allOfValid && anyOfValid && noneOfValid;
        //if the group is valid, use it
        if (validBiomeTagGroups[key].valid) {
            chosenBiomeGroup = key;
            //once a valid group is found, don't check the rest
            var structurePool = [];
            var totalWeights = 0;
            for (var structure of STRUCTURES){
                if (structure["biomeGroup"] == chosenBiomeGroup) {
                    var placeUnderwater = structure.placeUnderwater == undefined ? false : structure.placeUnderwater;
                    var minSpawnHeight = structure.minSpawnHeight == undefined ? 0 : structure.minSpawnHeight;
                    var maxSpawnHeight = structure.maxSpawnHeight == undefined ? 256 : structure.maxSpawnHeight;
                    structurePool.push({
                        path: structure.path,
                        min: totalWeights + 1,
                        max: totalWeights + structure.weight,
                        yOffset: -structure.yOffset,
                        footprint: structure.footprint,
                        placeUnderwater: placeUnderwater,
                        minSpawnHeight,
                        maxSpawnHeight
                    });
                    totalWeights += structure["weight"];
                }
            }
            var roll = Math.round(Math.random() * totalWeights) + 1;
            for (var struct of structurePool){
                if (roll >= struct.min && roll <= struct.max) {
                    var obj = {
                        path: struct.path,
                        footprint: struct.footprint,
                        yOffset: -struct.yOffset,
                        placeUnderwater: struct.placeUnderwater,
                        minSpawnHeight: struct.minSpawnHeight,
                        maxSpawnHeight: struct.maxSpawnHeight
                    };
                    return obj;
                }
            }
            break;
        }
    }
    obj = {
        path: "",
        footprint: [
            0,
            0
        ],
        yOffset: -0,
        placeUnderwater: false,
        minSpawnHeight: 0,
        maxSpawnHeight: 256
    };
    return obj;
}
