package io.anuke.mindustry.maps.generation;

import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import io.anuke.mindustry.content.blocks.Blocks;
import io.anuke.mindustry.content.blocks.DefenseBlocks;
import io.anuke.mindustry.content.blocks.UnitBlocks;
import io.anuke.mindustry.game.Team;
import io.anuke.mindustry.maps.generation.StructureFormat.StructBlock;
import io.anuke.mindustry.type.AmmoType;
import io.anuke.mindustry.type.Item;
import io.anuke.mindustry.world.Block;
import io.anuke.mindustry.world.Tile;
import io.anuke.mindustry.world.blocks.defense.turrets.ItemTurret;
import io.anuke.mindustry.world.blocks.defense.turrets.PowerTurret;
import io.anuke.ucore.util.Mathf;

import static io.anuke.mindustry.Vars.world;

public class FortressGenerator{
    private final static int minCoreDst = 60;

    private static Structure[] structures;
    private static Structure[] bases;

    private int enemyX, enemyY, coreX, coreY;
    private Team team;
    private Generation gen;

    private static void init(){
        if(structures != null) return;

        structures = new Structure[]{
        /*
            //tiny duo outpost
            new Structure(0.03f, Items.tungsten, "BAMADnR1bmdzdGVuLWRyaWxsAgADZHVvAQANdHVuZ3N0ZW4td2FsbAAAA2FpcgMFAQABAwEDAQMBAAEAAgMDAwIDAQABAAEBAQEBAQEA"),

            //basic outposts with duos
            new Structure(0.03f, Items.tungsten, "BAIAA2R1bwMADWNhcmJpZGUtZHJpbGwBAA10dW5nc3Rlbi13YWxsAAADYWlyBQUAAAEAAQABAAAAAQABAAIAAQABAAEAAgADAwIAAQABAAEAAgABAAEAAAABAAEAAQAAAA=="),

            //more advanced duo outpost
            new Structure(0.04f, Items.lead, "BwYADnR1bmdzdGVuLWRyaWxsAwADZHVvBAAIc3BsaXR0ZXIBAA10dW5nc3Rlbi13YWxsAgATdHVuZ3N0ZW4td2FsbC1sYXJnZQAAA2FpcgUACGNvbnZleW9yCQkAAAAAAQEBAQEBAQEBAgAAAAAAAAICAAEDAAQDAwACAgAAAAABAgACAAABAgUCAQEAAAAAAQABAgMAAQIBAgUCAQEBAQMAAQABAgQCBQMFAwYCBQEFAQQDAQABAgMAAQEBAQUAAQMBAwMAAQABAwICAAMBAQUAAQMCAgADAQMAAAAAAAIDAAQDAwAAAwADAAAAAAAAAQIBAwEDAQMBAwAAAAA="),

            //tungsten duo and drone outpost
            new Structure(0.02f, Items.tungsten, "BgEADXR1bmdzdGVuLXdhbGwEAA1jYXJiaWRlLWRyaWxsAwADZHVvAgATdHVuZ3N0ZW4td2FsbC1sYXJnZQAAA2FpcgUACWRyb25lLXBhZAUHAAAAAAEDAQMCAgAAAAAAAAAAAQADAAADAAMBAgAAAAABAAQBBQAAAAECAAAAAAEAAwAAAAAAAQIAAAAAAQABAQEBAQEBAg=="),

            //resupply point
            new Structure(0.03f, Items.lead, "BgEADXR1bmdzdGVuLXdhbGwCAAtzb2xhci1wYW5lbAUAA2R1bwMADnJlc3VwcGx5LXBvaW50AAADYWlyBAANY2FyYmlkZS1kcmlsbAUFAQABAAEAAAAAAAEAAgMBAQEBAQABAAMDBAEFAQEAAQACAwEBAQEBAAEBAQEBAQAAAAA="),

            //mini dagger outpost
            new Structure(0.02f, Items.lead, "CwIADXR1bmdzdGVuLXdhbGwGAAhjb252ZXlvcgUAA2R1bwgACmRhZ2dlci1wYWQBABN0dW5nc3Rlbi13YWxsLWxhcmdlBwAIc3BsaXR0ZXIDAA5yZXN1cHBseS1wb2ludAoADHJlcGFpci1wb2ludAkADnR1bmdzdGVuLWRyaWxsAAADYWlyBAALc29sYXItcGFuZWwHCQAAAAABAAABAgECAQIBAAAAAAEAAAMAAwADAwEEAAIBAgICAgAAAAAFAwYDBwMCAQIBBQECAgAAAAAIAAAACQMGAQYBBwMCAgAAAAAAAAAAAQMAAAIDBQECAgEAAAAKAQQBAAAAAAIBAgECAgAAAAACAQIBAgEAAAAAAAAAAA=="),

            //salvo outpost
            new Structure(0.02f, Items.tungsten, "BAIABXNhbHZvAwANY2FyYmlkZS1kcmlsbAAAA2FpcgEADGNhcmJpZGUtd2FsbAcHAAAAAAEDAQMBAwEDAAABAwEDAQMCAAAAAQMAAAEAAgAAAAAAAAABAwEDAQAAAAAAAwACAAAAAQIBAAEBAgAAAAAAAAABAgAAAQEAAAAAAQEBAQEBAAABAQEBAQEBAQAAAAA="),

            //advanced laser outpost
            new Structure(0.03f, null, "BQIABmxhbmNlcgEAEmNhcmJpZGUtd2FsbC1sYXJnZQQAEXNvbGFyLXBhbmVsLWxhcmdlAAADYWlyAwALc29sYXItcGFuZWwLCwAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAEAAAAAAAAAAAABAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAAAAAMAAwAAAAAAAwABAAAAAAABAAAAAgAAAAAAAAAAAAMAAAAAAAAAAAAAAAAAAAAAAAQAAAACAAAAAQAAAAAAAQAAAAMAAAAAAAAAAAAAAAAAAAAAAAAAAAADAAIAAAADAAMAAQAAAAAAAAAAAAEAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"),

            //2x interceptor outpost
            new Structure(0.02f, Items.lead, "CgMAEXNvbGFyLXBhbmVsLWxhcmdlBgADZHVvAgAPaW50ZXJjZXB0b3ItcGFkBAASY2FyYmlkZS13YWxsLWxhcmdlBwAOcmVzdXBwbHktcG9pbnQAAANhaXIFAAtzb2xhci1wYW5lbAEADGNhcmJpZGUtd2FsbAkAC2Rpc3RyaWJ1dG9yCAALbGFzZXItZHJpbGwKCAEAAQMBAwEDAQMBAwECAAABAAIDAAAAAAAAAAABAgAAAQAAAAAAAAADAQAAAQIBAgQDAAAFAAAAAAAAAAYCAQMAAAAABwAIAAAACQAAAAECBAMAAAEAAAAAAAABAAABAgAAAAAFAAAAAAAAAAYCAQIBAAIDAAAAAAMBAAABAgECAQAAAAAAAAAAAAAAAQIAAAEAAQEBAQEBAQEBAQEBAAA="),

            //resupply point (again)
            new Structure(0.02f, Items.lead, "BgEADXR1bmdzdGVuLXdhbGwCAAtzb2xhci1wYW5lbAUAA2R1bwMADnJlc3VwcGx5LXBvaW50AAADYWlyBAANY2FyYmlkZS1kcmlsbAUFAQABAAEAAAAAAAEAAgMBAQEBAQABAAMDBAEFAQEAAQACAwEBAQEBAAEBAQEBAQAAAAA="),

            //coal laser outpost
            new Structure(0.03f, null, "BgEADHRob3JpdW0td2FsbAMABmxhbmNlcgUAFGNvbWJ1c3Rpb24tZ2VuZXJhdG9yBAANY2FyYmlkZS1kcmlsbAAAA2FpcgIAC3NvbGFyLXBhbmVsBwcAAAEAAQABAQEBAQEBAAAAAQACAgMAAAACAAEAAAABAAICAAAAAAIAAQAAAAEAAQAEAQUAAQABAAAAAQACAAMBAAMCAAEAAAABAAIAAAMAAwIAAQAAAAEAAQABAwEDAQABAA=="),

            //ultimate laser outpost
            new Structure(0.02f, null, "BgMABmxhbmNlcgIAEmNhcmJpZGUtd2FsbC1sYXJnZQUAEXNvbGFyLXBhbmVsLWxhcmdlAAADYWlyBAALc29sYXItcGFuZWwBAAxjYXJiaWRlLXdhbGwPDwAAAAAAAAAAAAABAwIDAAABAwAAAAAAAAAAAAAAAAAAAAACAwAAAgMAAAAAAAACAwAAAgMAAAAAAAAAAAAAAQMAAAAAAAAAAAMDAAAAAAAAAAAAAAIDAAAAAAAAAgMAAAMDAAAEAwAAAAADAwAAAwMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIDAAAAAAAAAgMAAAMDAAAAAAUDAAAAAAAAAAAEAwAAAAABAwEDAAAAAAAAAAAAAAAAAAAAAAUDAAADAwAAAgMAAAIDAAADAwAAAAAAAAAABAMAAAAAAAAAAwAAAAAAAAAAAAAAAAAAAAAFAwAAAAAAAAAAAwMAAAIDAAABAwEDAgMAAAQDAAAAAAAAAAAFAwAAAAAAAAAAAAAAAAAAAAAAAAMDAAADAwAAAAAAAAAAAwMAAAIDAAAAAAAAAgMAAAAAAAAAAAAAAwMAAAQDAAAAAAAAAAAAAAAAAAAAAAIDAAACAwAAAAAAAAIDAAACAwAAAQMAAAAAAAABAwAAAAAAAAAAAgMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEDAAAAAAEDAAAAAAAAAAAAAA=="),

            //fabricator outpost
            new Structure(0.02f, Items.tungsten, "BgUADWNhcmJpZGUtZHJpbGwBABJ0aG9yaXVtLXdhbGwtbGFyZ2UCAAx0aG9yaXVtLXdhbGwEAANkdW8AAANhaXIDAA5mYWJyaWNhdG9yLXBhZAkJAAAAAAEDAAACAwEDAAAAAAAAAAACAwADAAMCAwADAAMCAgAAAQMAAAMCAAACAAMCAAABAwAAAAAAAAAAAAAEAgAAAAAAAgAAAgMCAAIBBAIFAAQCAgMCAgIDAQMAAAMCAAAEAgMCAAABAwAAAAAAAAAAAAACAQAAAAAAAgAAAAACAAEDAAECAQEDAAECAQAAAAAAAAAAAAACAwACAAAAAAAA"),
            */
        };

        bases = new Structure[]{
        /*
            //primitive 4-spawner base
            new Structure("CAQADXR1bmdzdGVuLXdhbGwFAANkdW8HAARjb3JlAQAKZGFnZ2VyLXBhZAYACHVubG9hZGVyAgAMcmVwYWlyLXBvaW50AAADYWlyAwALc29sYXItcGFuZWwJBwAAAQMAAAIDAQMAAAAAAAAAAAAAAwIAAAAAAAAEAAQBBAEEAQQBBAEEAgQABQIAAAAAAAAFAgQCBAAGAwAABwAAAAYDBAAEAAUCAAAAAAAABQIEAAQABAMEAwQDBAMEAAQAAAABAwAAAwIBAwAAAAAAAAAAAAACAwAAAAAAAA=="),

            //more advanced base, 8 spawners
            new Structure("CQIADXR1bmdzdGVuLXdhbGwGAANkdW8IAARjb3JlAQAKZGFnZ2VyLXBhZAcACHVubG9hZGVyBAATdHVuZ3N0ZW4td2FsbC1sYXJnZQUADHJlcGFpci1wb2ludAAAA2FpcgMAC3NvbGFyLXBhbmVsCwsAAAAAAQEAAAAAAAAAAAEBAAAAAAAAAAAAAAAAAAACAwMAAgMAAAAAAAAAAAEBAAAEAAAAAgMFAAIDBAAAAAEBAAAAAAAAAAAAAAYABwIGAAAAAAAAAAAAAAACAAIABgAAAAAAAAAGAAICAgAAAAAAAwAFAAcCAAAIAAAABwIFAAMAAAAAAAIAAgAGAAAAAAAAAAYAAgICAAAAAQEAAAQAAAAGAAcCBgAEAAAAAQEAAAAAAAAAAAAAAgEFAAIBAAAAAAAAAAAAAAAAAQEAAAIDAwACAwEBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=="),

            //rather advanced flyer base, no ground units, drones
            new Structure("DQIADXR1bmdzdGVuLXdhbGwHAAhjb252ZXlvcgMAA2R1bwwABGNvcmULAAh1bmxvYWRlcgEAE3R1bmdzdGVuLXdhbGwtbGFyZ2UGAA9pbnRlcmNlcHRvci1wYWQFAAhzcGxpdHRlcgoADHJlcGFpci1wb2ludAQABXNhbHZvAAADYWlyCAAJZHJvbmUtcGFkCQALc29sYXItcGFuZWwREQAAAAAAAAAAAAAAAAEAAAACAgEAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAgAAAAAAAwAAAAAAAgABAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAADAAUAAwAEAAAAAAAAAAAAAAAAAAAAAQAAAAYAAAAAAAAAAgMHAgIDAAAAAAYAAAABAAAAAAAAAAAAAAAAAAAAAgMHAgcDBQEHAQcCAgMAAAAAAAAAAAAAAAACAAQAAAACAwgAAAAJAAcCCQAIAAAAAgMEAAAAAgAAAAEAAAAAAAAABwMAAAAACgMLAgoDAAAAAAcBAAAAAAEAAAAAAAAAAwACAwcCCQAKAwAAAAAAAAoDCQAHAgIDAwAAAAAAAgADAAUABwMFAAcDCwIAAAwAAAALAwcBBQIHAQUAAwACAAEAAAADAAIDBwAJAAoDAAAAAAAACgMJAAcAAgMDAAEAAAAAAAAABAAAAAcDCAAAAAoDCwMKAwgAAAAHAQQAAAAAAAAAAAACAAAAAAACAwAAAAAJAAcACQAAAAAAAgMAAAAAAgAAAAAAAQAAAAYAAAACAwcABwMFAgcBBwACAwYAAAABAAAAAAAAAAAAAAAAAAAABAAAAAIDBwACAwQAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAADAAUDAwAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAACAAEAAAADAAEAAAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAA"),

            //advanced carbide base with fabricators, drones
            new Structure("Ew4ADXR1bmdzdGVuLXdhbGwHABFzb2xhci1wYW5lbC1sYXJnZRAACGNvbnZleW9yDAADZHVvEgAEY29yZQMACmRhZ2dlci1wYWQRAAh1bmxvYWRlcgkAD2ludGVyY2VwdG9yLXBhZA8ACHNwbGl0dGVyBgAGbGFuY2VyDQARdGl0YW5pdW0tY29udmV5b3IBABJjYXJiaWRlLXdhbGwtbGFyZ2ULAAxyZXBhaXItcG9pbnQIAAVzYWx2bwAAA2FpcgUACXRpdGFuLXBhZAoAC3NvbGFyLXBhbmVsAgAMY2FyYmlkZS13YWxsBAAOZmFicmljYXRvci1wYWQTFQAAAAAAAAAAAAAAAAAAAQMAAwAAAAAAAAEDAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAICAAAAAAIBAAEAAQAAAAAAAAABAAECAQAAAAACAgAAAAAAAAAAAAAAAAICAwAAAAICBAAAAAAABQAAAAQAAAACAAMAAAACAgAAAAAAAAAAAAAAAAICAAAAAAICAAAAAAAAAAAAAAAAAAACAAAAAAACAgAAAAAAAAAAAAAAAAEAAAABAAAABgAAAAAAAAAAAAYAAAABAAAAAQAAAAAAAAAAAAIAAQIAAAAAAAAAAAAAAAMAAwAABwAAAAAAAAAAAAAAAAAAAAECAAACAAIAAAAAAAEAAAAIAAAACQAAAAAAAAAAAAkAAAAIAAAAAQAAAAAAAAACAAIACAMAAAAAAAAAAAAAAAAAAAoACwAKAAAAAAAAAAAAAAAAAAgDAAACAAIAAAAAAAIADAMMAw0CDgAMAAAAAAAAAAwADgANAgwBDAECAAAAAAACAAIAAgMPAw0DDwAPAw8AEAMRAgAAEgAAABECEAEPAA8DDwANAQ8DAgMCAAIACAMAAAIADAMMAw0ADgAMAAAAAAAAAAwADgANAAwBDAECAAgDAAACAAIAAAAAAAEAAAAIAAAACQAAAAoACwAKAAkAAAAIAAAAAQAAAAAAAAACAAIAAQIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAECAAACAAIAAAIAAAEAAAABAAAABgAAAAAABwAAAAYAAAABAAAAAQAAAAAAAAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAwAAAAICBAAAAAAAAAAAAAQAAAACAAMAAAACAAAAAAAAAAAAAAAAAAIAAAAAAAICAAAAAAAABQAAAAAAAAACAAAAAAACAAAAAAAAAAAAAAAAAAIAAAAAAAICAQMAAwAAAAAAAAEDAAMCAAAAAAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAwAAAAAAAAADAAAAAAAAAAAAAAAAAAAAAA=="),

            //lots of titans and daggers
            new Structure("DAcACHNwbGl0dGVyBgADZHVvCwAEY29yZQMACmRhZ2dlci1wYWQKAAh1bmxvYWRlcggAEXRpdGFuaXVtLWNvbnZleW9yAgASY2FyYmlkZS13YWxsLWxhcmdlBQAFc2Fsdm8AAANhaXIEAAl0aXRhbi1wYWQBAAxjYXJiaWRlLXdhbGwJAAtkaXN0cmlidXRvchMTAAAAAAECAAAAAAAAAAAAAAECAAABAgAAAAAAAAAAAAABAgAAAAAAAAIDAAAAAAAAAAAAAAAAAQIBAAECAAAAAAAAAAAAAAIDAAAAAAECAAAAAAAAAAAAAAMAAAABAgEAAQIDAAAAAAAAAAAAAAAAAAECAAAAAAAAAAAAAAAAAAMAAAECAQABAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAFAwAABgAHAAYABQMAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAgCAQAAAAAAAAAAAAAAAAAAAA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AgDAAAAAAcDAAAFAAAABQAAAAcDAAAIAwAAAwEAAAEBAAAAAAAAAAAAAAAAAAAAAAAAAAAFAAAABQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAwAABQAKAAAACgAFAAgDAAAAAAAAAAAAAAAAAAAAAAIBAAAAAAcDAAAAAAAABQAJAAAACQAFAAAAAAAAAAcDAAAAAAIBAAAAAAAAAAAAAAAAAAADAQAABQMGAAAABgAFAQMBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAEAAAABAAEAAAAAAAAAAAAAAAAAAAAAAAAAAIBAAAAAAIBAAEBAQAAAQEAAAAAAQEAAAEBAAAAAAIBAAAAAAIBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAA=")
            */
        };
    }

    public void generate(Generation gen, Team team, int coreX, int coreY, int enemyX, int enemyY){
        init();

        this.enemyX = enemyX;
        this.enemyY = enemyY;
        this.coreX = coreX;
        this.coreY = coreY;
        this.gen = gen;
        this.team = team;

        genOutposts();
    }

    void genOutposts(){
        int padding = 10;
        int maxDifficulty = 13;
        Array<Structure> selected = new Array<>();
        Array<Rectangle> used = new Array<>();
        Rectangle rect = new Rectangle();
        Structure base = bases[Mathf.clamp((int)(bases.length * (float)gen.sector.difficulty / maxDifficulty) + gen.random.range(2), 0, bases.length-1)];

        int maxIndex = (int)(1 + ((float)gen.sector.difficulty / maxDifficulty * (structures.length-2)));

        for(int i = maxIndex/2; i < maxIndex; i++){
            selected.add(structures[Math.min(i, structures.length-1)]);
        }

        float baseChance = 0.8f / selected.size;

        used.add(new Rectangle(enemyX - base.width()/2, enemyY - base.height()/2, base.width(), base.height()));
        int elev = gen.tiles[enemyX][enemyY].getElevation();
        for(int x = 0; x < base.width(); x++){
            for(int y = 0; y < base.height(); y++){
                Tile tile = gen.tiles[enemyX - base.width()/2 + x][enemyY - base.height()/2 + y];
                StructBlock block = base.layout[x][y];
                Block result = fixBlock(block.block);
                tile.setElevation(elev);
                tile.setRotation(block.rotation);
                tile.setBlock(result, team);
            }
        }

        for(Structure struct : selected){
            for(int x = padding; x < gen.width - padding; x++){
                loop:
                for(int y = padding; y < gen.height - padding; y++){
                    rect.set(x - struct.layout.length, y - struct.layout[0].length, struct.layout.length, struct.layout[0].length);
                    if(Vector2.dst(x, y, coreX, coreY) > minCoreDst && Vector2.dst(x, y, enemyX, enemyY) > 30 && world.tile(x, y).floor().liquidDrop == null &&
                    (struct.ore == null || gen.tiles[x][y].floor().dropsItem(struct.ore)) && gen.random.chance(struct.chance * baseChance)){
                        for(Rectangle other : used){
                            if(other.overlaps(rect)){
                                continue loop;
                            }
                        }
                        used.add(new Rectangle(rect.x - 1, rect.y - 1, rect.width + 2, rect.height + 2));
                        int elevation = world.tile(x, y).getElevation();
                        for(int cx = 0; cx < struct.layout.length; cx++){
                            for(int cy = 0; cy < struct.layout[0].length; cy++){
                                int wx = x + cx - struct.layout.length/2;
                                int wy = y + cy - struct.layout[0].length/2;
                                StructBlock block = struct.layout[cx][cy];
                                Tile tile = world.tile(wx, wy);

                                Block result = fixBlock(block.block);

                                //resupply points should not be placed anymore, they confuse units
                                if(result != Blocks.air && tile.block().alwaysReplace){
                                    tile.setElevation(elevation);
                                    tile.setRotation(block.rotation);
                                    tile.setBlock(result, team);

                                    fill(tile);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    Block fixBlock(Block result){
        if(result == UnitBlocks.dronePad) result = DefenseBlocks.copperWallLarge;
        if(result == UnitBlocks.fabricatorPad) result = DefenseBlocks.copperWallLarge;
        return result;
    }

    void fill(Tile tile){
        Block block = tile.block();

        if(block instanceof PowerTurret){
            tile.entity.power.amount = block.powerCapacity;
        }else if(block instanceof ItemTurret){
            ItemTurret turret = (ItemTurret)block;
            AmmoType[] type = turret.getAmmoTypes();
            block.handleStack(type[0].item, block.acceptStack(type[0].item, 1000, tile, null), tile, null);
        }
    }

    static class Structure{
        public final StructBlock[][] layout;
        public final Item ore;
        public final float chance;

        public Structure(float chance, Item ore, String encoding){
            this.ore = ore;
            this.layout = StructureFormat.read(encoding);
            this.chance = chance;
        }

        public Structure(String encoding){
            this.ore = null;
            this.layout = StructureFormat.read(encoding);
            this.chance = 0;
        }

        int width(){
            return layout.length;
        }

        int height(){
            return layout[0].length;
        }
    }
}
