package net.minecraft.world.level.levelgen.structure;

import net.minecraft.world.level.block.state.StateHolder;
import java.util.Collections;
import net.minecraft.util.Tuple;
import javax.annotation.Nullable;
import java.util.Iterator;
import com.google.common.collect.Lists;
import net.minecraft.world.entity.monster.AbstractIllager;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.SpawnGroupData;
import net.minecraft.world.entity.MobSpawnType;
import net.minecraft.world.entity.monster.Vindicator;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.monster.Evoker;
import net.minecraft.world.level.storage.loot.BuiltInLootTables;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.core.Direction;
import net.minecraft.world.level.block.ChestBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureProcessor;
import net.minecraft.world.level.levelgen.structure.templatesystem.BlockIgnoreProcessor;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructurePlaceSettings;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.levelgen.feature.StructurePieceType;
import net.minecraft.world.level.block.Mirror;
import java.util.Random;
import java.util.List;
import net.minecraft.world.level.block.Rotation;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureManager;

public class WoodlandMansionPieces {
    public static void generateMansion(final StructureManager cva, final BlockPos fu, final Rotation cap, final List<WoodlandMansionPiece> list, final Random random) {
        final MansionGrid c6 = new MansionGrid(random);
        final MansionPiecePlacer d7 = new MansionPiecePlacer(cva, random);
        d7.createMansion(fu, cap, list, c6);
    }
    
    public static class WoodlandMansionPiece extends TemplateStructurePiece {
        private final String templateName;
        private final Rotation rotation;
        private final Mirror mirror;
        
        public WoodlandMansionPiece(final StructureManager cva, final String string, final BlockPos fu, final Rotation cap) {
            this(cva, string, fu, cap, Mirror.NONE);
        }
        
        public WoodlandMansionPiece(final StructureManager cva, final String string, final BlockPos fu, final Rotation cap, final Mirror bzj) {
            super(StructurePieceType.WOODLAND_MANSION_PIECE, 0);
            this.templateName = string;
            this.templatePosition = fu;
            this.rotation = cap;
            this.mirror = bzj;
            this.loadTemplate(cva);
        }
        
        public WoodlandMansionPiece(final StructureManager cva, final CompoundTag le) {
            super(StructurePieceType.WOODLAND_MANSION_PIECE, le);
            this.templateName = le.getString("Template");
            this.rotation = Rotation.valueOf(le.getString("Rot"));
            this.mirror = Mirror.valueOf(le.getString("Mi"));
            this.loadTemplate(cva);
        }
        
        private void loadTemplate(final StructureManager cva) {
            final StructureTemplate cve3 = cva.getOrCreate(new ResourceLocation("woodland_mansion/" + this.templateName));
            final StructurePlaceSettings cvb4 = new StructurePlaceSettings().setIgnoreEntities(true).setRotation(this.rotation).setMirror(this.mirror).addProcessor(BlockIgnoreProcessor.STRUCTURE_BLOCK);
            this.setup(cve3, this.templatePosition, cvb4);
        }
        
        @Override
        protected void addAdditionalSaveData(final CompoundTag le) {
            super.addAdditionalSaveData(le);
            le.putString("Template", this.templateName);
            le.putString("Rot", this.placeSettings.getRotation().name());
            le.putString("Mi", this.placeSettings.getMirror().name());
        }
        
        @Override
        protected void handleDataMarker(final String string, final BlockPos fu, final LevelAccessor bqc, final Random random, final BoundingBox ctd) {
            if (string.startsWith("Chest")) {
                final Rotation cap7 = this.placeSettings.getRotation();
                BlockState cfj8 = Blocks.CHEST.defaultBlockState();
                if ("ChestWest".equals(string)) {
                    cfj8 = ((StateHolder<O, BlockState>)cfj8).<Comparable, Direction>setValue((Property<Comparable>)ChestBlock.FACING, cap7.rotate(Direction.WEST));
                }
                else if ("ChestEast".equals(string)) {
                    cfj8 = ((StateHolder<O, BlockState>)cfj8).<Comparable, Direction>setValue((Property<Comparable>)ChestBlock.FACING, cap7.rotate(Direction.EAST));
                }
                else if ("ChestSouth".equals(string)) {
                    cfj8 = ((StateHolder<O, BlockState>)cfj8).<Comparable, Direction>setValue((Property<Comparable>)ChestBlock.FACING, cap7.rotate(Direction.SOUTH));
                }
                else if ("ChestNorth".equals(string)) {
                    cfj8 = ((StateHolder<O, BlockState>)cfj8).<Comparable, Direction>setValue((Property<Comparable>)ChestBlock.FACING, cap7.rotate(Direction.NORTH));
                }
                this.createChest(bqc, ctd, random, fu, BuiltInLootTables.WOODLAND_MANSION, cfj8);
            }
            else {
                AbstractIllager bbj7 = null;
                switch (string) {
                    case "Mage": {
                        bbj7 = EntityType.EVOKER.create(bqc.getLevel());
                        break;
                    }
                    case "Warrior": {
                        bbj7 = EntityType.VINDICATOR.create(bqc.getLevel());
                        break;
                    }
                    default: {
                        return;
                    }
                }
                bbj7.setPersistenceRequired();
                bbj7.moveTo(fu, 0.0f, 0.0f);
                bbj7.finalizeSpawn(bqc, bqc.getCurrentDifficultyAt(bbj7.blockPosition()), MobSpawnType.STRUCTURE, null, null);
                bqc.addFreshEntity(bbj7);
                bqc.setBlock(fu, Blocks.AIR.defaultBlockState(), 2);
            }
        }
    }
    
    static class PlacementData {
        public Rotation rotation;
        public BlockPos position;
        public String wallType;
        
        private PlacementData() {
        }
    }
    
    static class MansionPiecePlacer {
        private final StructureManager structureManager;
        private final Random random;
        private int startX;
        private int startY;
        
        public MansionPiecePlacer(final StructureManager cva, final Random random) {
            this.structureManager = cva;
            this.random = random;
        }
        
        public void createMansion(final BlockPos fu, final Rotation cap, final List<WoodlandMansionPiece> list, final MansionGrid c) {
            final PlacementData e6 = new PlacementData();
            e6.position = fu;
            e6.rotation = cap;
            e6.wallType = "wall_flat";
            final PlacementData e7 = new PlacementData();
            this.entrance(list, e6);
            e7.position = e6.position.above(8);
            e7.rotation = e6.rotation;
            e7.wallType = "wall_window";
            if (!list.isEmpty()) {}
            final SimpleGrid g8 = c.baseGrid;
            final SimpleGrid g9 = c.thirdFloorGrid;
            this.startX = c.entranceX + 1;
            this.startY = c.entranceY + 1;
            final int integer10 = c.entranceX + 1;
            final int integer11 = c.entranceY;
            this.traverseOuterWalls(list, e6, g8, Direction.SOUTH, this.startX, this.startY, integer10, integer11);
            this.traverseOuterWalls(list, e7, g8, Direction.SOUTH, this.startX, this.startY, integer10, integer11);
            final PlacementData e8 = new PlacementData();
            e8.position = e6.position.above(19);
            e8.rotation = e6.rotation;
            e8.wallType = "wall_window";
            boolean boolean13 = false;
            for (int integer12 = 0; integer12 < g9.height && !boolean13; ++integer12) {
                for (int integer13 = g9.width - 1; integer13 >= 0 && !boolean13; --integer13) {
                    if (MansionGrid.isHouse(g9, integer13, integer12)) {
                        e8.position = e8.position.relative(cap.rotate(Direction.SOUTH), 8 + (integer12 - this.startY) * 8);
                        e8.position = e8.position.relative(cap.rotate(Direction.EAST), (integer13 - this.startX) * 8);
                        this.traverseWallPiece(list, e8);
                        this.traverseOuterWalls(list, e8, g9, Direction.SOUTH, integer13, integer12, integer13, integer12);
                        boolean13 = true;
                    }
                }
            }
            this.createRoof(list, fu.above(16), cap, g8, g9);
            this.createRoof(list, fu.above(27), cap, g9, null);
            if (!list.isEmpty()) {}
            final FloorRoomCollection[] arr14 = { new FirstFloorRoomCollection(), new SecondFloorRoomCollection(), new ThirdFloorRoomCollection() };
            for (int integer13 = 0; integer13 < 3; ++integer13) {
                final BlockPos fu2 = fu.above(8 * integer13 + ((integer13 == 2) ? 3 : 0));
                final SimpleGrid g10 = c.floorRooms[integer13];
                final SimpleGrid g11 = (integer13 == 2) ? g9 : g8;
                final String string19 = (integer13 == 0) ? "carpet_south_1" : "carpet_south_2";
                final String string20 = (integer13 == 0) ? "carpet_west_1" : "carpet_west_2";
                for (int integer14 = 0; integer14 < g11.height; ++integer14) {
                    for (int integer15 = 0; integer15 < g11.width; ++integer15) {
                        if (g11.get(integer15, integer14) == 1) {
                            BlockPos fu3 = fu2.relative(cap.rotate(Direction.SOUTH), 8 + (integer14 - this.startY) * 8);
                            fu3 = fu3.relative(cap.rotate(Direction.EAST), (integer15 - this.startX) * 8);
                            list.add(new WoodlandMansionPiece(this.structureManager, "corridor_floor", fu3, cap));
                            if (g11.get(integer15, integer14 - 1) == 1 || (g10.get(integer15, integer14 - 1) & 0x800000) == 0x800000) {
                                list.add(new WoodlandMansionPiece(this.structureManager, "carpet_north", fu3.relative(cap.rotate(Direction.EAST), 1).above(), cap));
                            }
                            if (g11.get(integer15 + 1, integer14) == 1 || (g10.get(integer15 + 1, integer14) & 0x800000) == 0x800000) {
                                list.add(new WoodlandMansionPiece(this.structureManager, "carpet_east", fu3.relative(cap.rotate(Direction.SOUTH), 1).relative(cap.rotate(Direction.EAST), 5).above(), cap));
                            }
                            if (g11.get(integer15, integer14 + 1) == 1 || (g10.get(integer15, integer14 + 1) & 0x800000) == 0x800000) {
                                list.add(new WoodlandMansionPiece(this.structureManager, string19, fu3.relative(cap.rotate(Direction.SOUTH), 5).relative(cap.rotate(Direction.WEST), 1), cap));
                            }
                            if (g11.get(integer15 - 1, integer14) == 1 || (g10.get(integer15 - 1, integer14) & 0x800000) == 0x800000) {
                                list.add(new WoodlandMansionPiece(this.structureManager, string20, fu3.relative(cap.rotate(Direction.WEST), 1).relative(cap.rotate(Direction.NORTH), 1), cap));
                            }
                        }
                    }
                }
                final String string21 = (integer13 == 0) ? "indoors_wall_1" : "indoors_wall_2";
                final String string22 = (integer13 == 0) ? "indoors_door_1" : "indoors_door_2";
                final List<Direction> list2 = Lists.newArrayList();
                for (int integer16 = 0; integer16 < g11.height; ++integer16) {
                    for (int integer17 = 0; integer17 < g11.width; ++integer17) {
                        boolean boolean14 = integer13 == 2 && g11.get(integer17, integer16) == 3;
                        if (g11.get(integer17, integer16) == 2 || boolean14) {
                            final int integer18 = g10.get(integer17, integer16);
                            final int integer19 = integer18 & 0xF0000;
                            final int integer20 = integer18 & 0xFFFF;
                            boolean14 = (boolean14 && (integer18 & 0x800000) == 0x800000);
                            list2.clear();
                            if ((integer18 & 0x200000) == 0x200000) {
                                for (final Direction fz31 : Direction.Plane.HORIZONTAL) {
                                    if (g11.get(integer17 + fz31.getStepX(), integer16 + fz31.getStepZ()) == 1) {
                                        list2.add(fz31);
                                    }
                                }
                            }
                            Direction fz32 = null;
                            if (!list2.isEmpty()) {
                                fz32 = list2.get(this.random.nextInt(list2.size()));
                            }
                            else if ((integer18 & 0x100000) == 0x100000) {
                                fz32 = Direction.UP;
                            }
                            BlockPos fu4 = fu2.relative(cap.rotate(Direction.SOUTH), 8 + (integer16 - this.startY) * 8);
                            fu4 = fu4.relative(cap.rotate(Direction.EAST), -1 + (integer17 - this.startX) * 8);
                            if (MansionGrid.isHouse(g11, integer17 - 1, integer16) && !c.isRoomId(g11, integer17 - 1, integer16, integer13, integer20)) {
                                list.add(new WoodlandMansionPiece(this.structureManager, (fz32 == Direction.WEST) ? string22 : string21, fu4, cap));
                            }
                            if (g11.get(integer17 + 1, integer16) == 1 && !boolean14) {
                                final BlockPos fu5 = fu4.relative(cap.rotate(Direction.EAST), 8);
                                list.add(new WoodlandMansionPiece(this.structureManager, (fz32 == Direction.EAST) ? string22 : string21, fu5, cap));
                            }
                            if (MansionGrid.isHouse(g11, integer17, integer16 + 1) && !c.isRoomId(g11, integer17, integer16 + 1, integer13, integer20)) {
                                BlockPos fu5 = fu4.relative(cap.rotate(Direction.SOUTH), 7);
                                fu5 = fu5.relative(cap.rotate(Direction.EAST), 7);
                                list.add(new WoodlandMansionPiece(this.structureManager, (fz32 == Direction.SOUTH) ? string22 : string21, fu5, cap.getRotated(Rotation.CLOCKWISE_90)));
                            }
                            if (g11.get(integer17, integer16 - 1) == 1 && !boolean14) {
                                BlockPos fu5 = fu4.relative(cap.rotate(Direction.NORTH), 1);
                                fu5 = fu5.relative(cap.rotate(Direction.EAST), 7);
                                list.add(new WoodlandMansionPiece(this.structureManager, (fz32 == Direction.NORTH) ? string22 : string21, fu5, cap.getRotated(Rotation.CLOCKWISE_90)));
                            }
                            if (integer19 == 65536) {
                                this.addRoom1x1(list, fu4, cap, fz32, arr14[integer13]);
                            }
                            else if (integer19 == 131072 && fz32 != null) {
                                final Direction fz33 = c.get1x2RoomDirection(g11, integer17, integer16, integer13, integer20);
                                final boolean boolean15 = (integer18 & 0x400000) == 0x400000;
                                this.addRoom1x2(list, fu4, cap, fz33, fz32, arr14[integer13], boolean15);
                            }
                            else if (integer19 == 262144 && fz32 != null && fz32 != Direction.UP) {
                                Direction fz33 = fz32.getClockWise();
                                if (!c.isRoomId(g11, integer17 + fz33.getStepX(), integer16 + fz33.getStepZ(), integer13, integer20)) {
                                    fz33 = fz33.getOpposite();
                                }
                                this.addRoom2x2(list, fu4, cap, fz33, fz32, arr14[integer13]);
                            }
                            else if (integer19 == 262144 && fz32 == Direction.UP) {
                                this.addRoom2x2Secret(list, fu4, cap, arr14[integer13]);
                            }
                        }
                    }
                }
            }
        }
        
        private void traverseOuterWalls(final List<WoodlandMansionPiece> list, final PlacementData e, final SimpleGrid g, Direction fz, final int integer5, final int integer6, final int integer7, final int integer8) {
            int integer9 = integer5;
            int integer10 = integer6;
            final Direction fz2 = fz;
            do {
                if (!MansionGrid.isHouse(g, integer9 + fz.getStepX(), integer10 + fz.getStepZ())) {
                    this.traverseTurn(list, e);
                    fz = fz.getClockWise();
                    if (integer9 == integer7 && integer10 == integer8 && fz2 == fz) {
                        continue;
                    }
                    this.traverseWallPiece(list, e);
                }
                else if (MansionGrid.isHouse(g, integer9 + fz.getStepX(), integer10 + fz.getStepZ()) && MansionGrid.isHouse(g, integer9 + fz.getStepX() + fz.getCounterClockWise().getStepX(), integer10 + fz.getStepZ() + fz.getCounterClockWise().getStepZ())) {
                    this.traverseInnerTurn(list, e);
                    integer9 += fz.getStepX();
                    integer10 += fz.getStepZ();
                    fz = fz.getCounterClockWise();
                }
                else {
                    integer9 += fz.getStepX();
                    integer10 += fz.getStepZ();
                    if (integer9 == integer7 && integer10 == integer8 && fz2 == fz) {
                        continue;
                    }
                    this.traverseWallPiece(list, e);
                }
            } while (integer9 != integer7 || integer10 != integer8 || fz2 != fz);
        }
        
        private void createRoof(final List<WoodlandMansionPiece> list, final BlockPos fu, final Rotation cap, final SimpleGrid g4, @Nullable final SimpleGrid g5) {
            for (int integer7 = 0; integer7 < g4.height; ++integer7) {
                for (int integer8 = 0; integer8 < g4.width; ++integer8) {
                    BlockPos fu2 = fu;
                    fu2 = fu2.relative(cap.rotate(Direction.SOUTH), 8 + (integer7 - this.startY) * 8);
                    fu2 = fu2.relative(cap.rotate(Direction.EAST), (integer8 - this.startX) * 8);
                    final boolean boolean10 = g5 != null && MansionGrid.isHouse(g5, integer8, integer7);
                    if (MansionGrid.isHouse(g4, integer8, integer7) && !boolean10) {
                        list.add(new WoodlandMansionPiece(this.structureManager, "roof", fu2.above(3), cap));
                        if (!MansionGrid.isHouse(g4, integer8 + 1, integer7)) {
                            final BlockPos fu3 = fu2.relative(cap.rotate(Direction.EAST), 6);
                            list.add(new WoodlandMansionPiece(this.structureManager, "roof_front", fu3, cap));
                        }
                        if (!MansionGrid.isHouse(g4, integer8 - 1, integer7)) {
                            BlockPos fu3 = fu2.relative(cap.rotate(Direction.EAST), 0);
                            fu3 = fu3.relative(cap.rotate(Direction.SOUTH), 7);
                            list.add(new WoodlandMansionPiece(this.structureManager, "roof_front", fu3, cap.getRotated(Rotation.CLOCKWISE_180)));
                        }
                        if (!MansionGrid.isHouse(g4, integer8, integer7 - 1)) {
                            final BlockPos fu3 = fu2.relative(cap.rotate(Direction.WEST), 1);
                            list.add(new WoodlandMansionPiece(this.structureManager, "roof_front", fu3, cap.getRotated(Rotation.COUNTERCLOCKWISE_90)));
                        }
                        if (!MansionGrid.isHouse(g4, integer8, integer7 + 1)) {
                            BlockPos fu3 = fu2.relative(cap.rotate(Direction.EAST), 6);
                            fu3 = fu3.relative(cap.rotate(Direction.SOUTH), 6);
                            list.add(new WoodlandMansionPiece(this.structureManager, "roof_front", fu3, cap.getRotated(Rotation.CLOCKWISE_90)));
                        }
                    }
                }
            }
            if (g5 != null) {
                for (int integer7 = 0; integer7 < g4.height; ++integer7) {
                    for (int integer8 = 0; integer8 < g4.width; ++integer8) {
                        BlockPos fu2 = fu;
                        fu2 = fu2.relative(cap.rotate(Direction.SOUTH), 8 + (integer7 - this.startY) * 8);
                        fu2 = fu2.relative(cap.rotate(Direction.EAST), (integer8 - this.startX) * 8);
                        final boolean boolean10 = MansionGrid.isHouse(g5, integer8, integer7);
                        if (MansionGrid.isHouse(g4, integer8, integer7) && boolean10) {
                            if (!MansionGrid.isHouse(g4, integer8 + 1, integer7)) {
                                final BlockPos fu3 = fu2.relative(cap.rotate(Direction.EAST), 7);
                                list.add(new WoodlandMansionPiece(this.structureManager, "small_wall", fu3, cap));
                            }
                            if (!MansionGrid.isHouse(g4, integer8 - 1, integer7)) {
                                BlockPos fu3 = fu2.relative(cap.rotate(Direction.WEST), 1);
                                fu3 = fu3.relative(cap.rotate(Direction.SOUTH), 6);
                                list.add(new WoodlandMansionPiece(this.structureManager, "small_wall", fu3, cap.getRotated(Rotation.CLOCKWISE_180)));
                            }
                            if (!MansionGrid.isHouse(g4, integer8, integer7 - 1)) {
                                BlockPos fu3 = fu2.relative(cap.rotate(Direction.WEST), 0);
                                fu3 = fu3.relative(cap.rotate(Direction.NORTH), 1);
                                list.add(new WoodlandMansionPiece(this.structureManager, "small_wall", fu3, cap.getRotated(Rotation.COUNTERCLOCKWISE_90)));
                            }
                            if (!MansionGrid.isHouse(g4, integer8, integer7 + 1)) {
                                BlockPos fu3 = fu2.relative(cap.rotate(Direction.EAST), 6);
                                fu3 = fu3.relative(cap.rotate(Direction.SOUTH), 7);
                                list.add(new WoodlandMansionPiece(this.structureManager, "small_wall", fu3, cap.getRotated(Rotation.CLOCKWISE_90)));
                            }
                            if (!MansionGrid.isHouse(g4, integer8 + 1, integer7)) {
                                if (!MansionGrid.isHouse(g4, integer8, integer7 - 1)) {
                                    BlockPos fu3 = fu2.relative(cap.rotate(Direction.EAST), 7);
                                    fu3 = fu3.relative(cap.rotate(Direction.NORTH), 2);
                                    list.add(new WoodlandMansionPiece(this.structureManager, "small_wall_corner", fu3, cap));
                                }
                                if (!MansionGrid.isHouse(g4, integer8, integer7 + 1)) {
                                    BlockPos fu3 = fu2.relative(cap.rotate(Direction.EAST), 8);
                                    fu3 = fu3.relative(cap.rotate(Direction.SOUTH), 7);
                                    list.add(new WoodlandMansionPiece(this.structureManager, "small_wall_corner", fu3, cap.getRotated(Rotation.CLOCKWISE_90)));
                                }
                            }
                            if (!MansionGrid.isHouse(g4, integer8 - 1, integer7)) {
                                if (!MansionGrid.isHouse(g4, integer8, integer7 - 1)) {
                                    BlockPos fu3 = fu2.relative(cap.rotate(Direction.WEST), 2);
                                    fu3 = fu3.relative(cap.rotate(Direction.NORTH), 1);
                                    list.add(new WoodlandMansionPiece(this.structureManager, "small_wall_corner", fu3, cap.getRotated(Rotation.COUNTERCLOCKWISE_90)));
                                }
                                if (!MansionGrid.isHouse(g4, integer8, integer7 + 1)) {
                                    BlockPos fu3 = fu2.relative(cap.rotate(Direction.WEST), 1);
                                    fu3 = fu3.relative(cap.rotate(Direction.SOUTH), 8);
                                    list.add(new WoodlandMansionPiece(this.structureManager, "small_wall_corner", fu3, cap.getRotated(Rotation.CLOCKWISE_180)));
                                }
                            }
                        }
                    }
                }
            }
            for (int integer7 = 0; integer7 < g4.height; ++integer7) {
                for (int integer8 = 0; integer8 < g4.width; ++integer8) {
                    BlockPos fu2 = fu;
                    fu2 = fu2.relative(cap.rotate(Direction.SOUTH), 8 + (integer7 - this.startY) * 8);
                    fu2 = fu2.relative(cap.rotate(Direction.EAST), (integer8 - this.startX) * 8);
                    final boolean boolean10 = g5 != null && MansionGrid.isHouse(g5, integer8, integer7);
                    if (MansionGrid.isHouse(g4, integer8, integer7) && !boolean10) {
                        if (!MansionGrid.isHouse(g4, integer8 + 1, integer7)) {
                            final BlockPos fu3 = fu2.relative(cap.rotate(Direction.EAST), 6);
                            if (!MansionGrid.isHouse(g4, integer8, integer7 + 1)) {
                                final BlockPos fu4 = fu3.relative(cap.rotate(Direction.SOUTH), 6);
                                list.add(new WoodlandMansionPiece(this.structureManager, "roof_corner", fu4, cap));
                            }
                            else if (MansionGrid.isHouse(g4, integer8 + 1, integer7 + 1)) {
                                final BlockPos fu4 = fu3.relative(cap.rotate(Direction.SOUTH), 5);
                                list.add(new WoodlandMansionPiece(this.structureManager, "roof_inner_corner", fu4, cap));
                            }
                            if (!MansionGrid.isHouse(g4, integer8, integer7 - 1)) {
                                list.add(new WoodlandMansionPiece(this.structureManager, "roof_corner", fu3, cap.getRotated(Rotation.COUNTERCLOCKWISE_90)));
                            }
                            else if (MansionGrid.isHouse(g4, integer8 + 1, integer7 - 1)) {
                                BlockPos fu4 = fu2.relative(cap.rotate(Direction.EAST), 9);
                                fu4 = fu4.relative(cap.rotate(Direction.NORTH), 2);
                                list.add(new WoodlandMansionPiece(this.structureManager, "roof_inner_corner", fu4, cap.getRotated(Rotation.CLOCKWISE_90)));
                            }
                        }
                        if (!MansionGrid.isHouse(g4, integer8 - 1, integer7)) {
                            BlockPos fu3 = fu2.relative(cap.rotate(Direction.EAST), 0);
                            fu3 = fu3.relative(cap.rotate(Direction.SOUTH), 0);
                            if (!MansionGrid.isHouse(g4, integer8, integer7 + 1)) {
                                final BlockPos fu4 = fu3.relative(cap.rotate(Direction.SOUTH), 6);
                                list.add(new WoodlandMansionPiece(this.structureManager, "roof_corner", fu4, cap.getRotated(Rotation.CLOCKWISE_90)));
                            }
                            else if (MansionGrid.isHouse(g4, integer8 - 1, integer7 + 1)) {
                                BlockPos fu4 = fu3.relative(cap.rotate(Direction.SOUTH), 8);
                                fu4 = fu4.relative(cap.rotate(Direction.WEST), 3);
                                list.add(new WoodlandMansionPiece(this.structureManager, "roof_inner_corner", fu4, cap.getRotated(Rotation.COUNTERCLOCKWISE_90)));
                            }
                            if (!MansionGrid.isHouse(g4, integer8, integer7 - 1)) {
                                list.add(new WoodlandMansionPiece(this.structureManager, "roof_corner", fu3, cap.getRotated(Rotation.CLOCKWISE_180)));
                            }
                            else if (MansionGrid.isHouse(g4, integer8 - 1, integer7 - 1)) {
                                final BlockPos fu4 = fu3.relative(cap.rotate(Direction.SOUTH), 1);
                                list.add(new WoodlandMansionPiece(this.structureManager, "roof_inner_corner", fu4, cap.getRotated(Rotation.CLOCKWISE_180)));
                            }
                        }
                    }
                }
            }
        }
        
        private void entrance(final List<WoodlandMansionPiece> list, final PlacementData e) {
            final Direction fz4 = e.rotation.rotate(Direction.WEST);
            list.add(new WoodlandMansionPiece(this.structureManager, "entrance", e.position.relative(fz4, 9), e.rotation));
            e.position = e.position.relative(e.rotation.rotate(Direction.SOUTH), 16);
        }
        
        private void traverseWallPiece(final List<WoodlandMansionPiece> list, final PlacementData e) {
            list.add(new WoodlandMansionPiece(this.structureManager, e.wallType, e.position.relative(e.rotation.rotate(Direction.EAST), 7), e.rotation));
            e.position = e.position.relative(e.rotation.rotate(Direction.SOUTH), 8);
        }
        
        private void traverseTurn(final List<WoodlandMansionPiece> list, final PlacementData e) {
            e.position = e.position.relative(e.rotation.rotate(Direction.SOUTH), -1);
            list.add(new WoodlandMansionPiece(this.structureManager, "wall_corner", e.position, e.rotation));
            e.position = e.position.relative(e.rotation.rotate(Direction.SOUTH), -7);
            e.position = e.position.relative(e.rotation.rotate(Direction.WEST), -6);
            e.rotation = e.rotation.getRotated(Rotation.CLOCKWISE_90);
        }
        
        private void traverseInnerTurn(final List<WoodlandMansionPiece> list, final PlacementData e) {
            e.position = e.position.relative(e.rotation.rotate(Direction.SOUTH), 6);
            e.position = e.position.relative(e.rotation.rotate(Direction.EAST), 8);
            e.rotation = e.rotation.getRotated(Rotation.COUNTERCLOCKWISE_90);
        }
        
        private void addRoom1x1(final List<WoodlandMansionPiece> list, final BlockPos fu, final Rotation cap, final Direction fz, final FloorRoomCollection b) {
            Rotation cap2 = Rotation.NONE;
            String string8 = b.get1x1(this.random);
            if (fz != Direction.EAST) {
                if (fz == Direction.NORTH) {
                    cap2 = cap2.getRotated(Rotation.COUNTERCLOCKWISE_90);
                }
                else if (fz == Direction.WEST) {
                    cap2 = cap2.getRotated(Rotation.CLOCKWISE_180);
                }
                else if (fz == Direction.SOUTH) {
                    cap2 = cap2.getRotated(Rotation.CLOCKWISE_90);
                }
                else {
                    string8 = b.get1x1Secret(this.random);
                }
            }
            BlockPos fu2 = StructureTemplate.getZeroPositionWithTransform(new BlockPos(1, 0, 0), Mirror.NONE, cap2, 7, 7);
            cap2 = cap2.getRotated(cap);
            fu2 = fu2.rotate(cap);
            final BlockPos fu3 = fu.offset(fu2.getX(), 0, fu2.getZ());
            list.add(new WoodlandMansionPiece(this.structureManager, string8, fu3, cap2));
        }
        
        private void addRoom1x2(final List<WoodlandMansionPiece> list, final BlockPos fu, final Rotation cap, final Direction fz4, final Direction fz5, final FloorRoomCollection b, final boolean boolean7) {
            if (fz5 == Direction.EAST && fz4 == Direction.SOUTH) {
                final BlockPos fu2 = fu.relative(cap.rotate(Direction.EAST), 1);
                list.add(new WoodlandMansionPiece(this.structureManager, b.get1x2SideEntrance(this.random, boolean7), fu2, cap));
            }
            else if (fz5 == Direction.EAST && fz4 == Direction.NORTH) {
                BlockPos fu2 = fu.relative(cap.rotate(Direction.EAST), 1);
                fu2 = fu2.relative(cap.rotate(Direction.SOUTH), 6);
                list.add(new WoodlandMansionPiece(this.structureManager, b.get1x2SideEntrance(this.random, boolean7), fu2, cap, Mirror.LEFT_RIGHT));
            }
            else if (fz5 == Direction.WEST && fz4 == Direction.NORTH) {
                BlockPos fu2 = fu.relative(cap.rotate(Direction.EAST), 7);
                fu2 = fu2.relative(cap.rotate(Direction.SOUTH), 6);
                list.add(new WoodlandMansionPiece(this.structureManager, b.get1x2SideEntrance(this.random, boolean7), fu2, cap.getRotated(Rotation.CLOCKWISE_180)));
            }
            else if (fz5 == Direction.WEST && fz4 == Direction.SOUTH) {
                final BlockPos fu2 = fu.relative(cap.rotate(Direction.EAST), 7);
                list.add(new WoodlandMansionPiece(this.structureManager, b.get1x2SideEntrance(this.random, boolean7), fu2, cap, Mirror.FRONT_BACK));
            }
            else if (fz5 == Direction.SOUTH && fz4 == Direction.EAST) {
                final BlockPos fu2 = fu.relative(cap.rotate(Direction.EAST), 1);
                list.add(new WoodlandMansionPiece(this.structureManager, b.get1x2SideEntrance(this.random, boolean7), fu2, cap.getRotated(Rotation.CLOCKWISE_90), Mirror.LEFT_RIGHT));
            }
            else if (fz5 == Direction.SOUTH && fz4 == Direction.WEST) {
                final BlockPos fu2 = fu.relative(cap.rotate(Direction.EAST), 7);
                list.add(new WoodlandMansionPiece(this.structureManager, b.get1x2SideEntrance(this.random, boolean7), fu2, cap.getRotated(Rotation.CLOCKWISE_90)));
            }
            else if (fz5 == Direction.NORTH && fz4 == Direction.WEST) {
                BlockPos fu2 = fu.relative(cap.rotate(Direction.EAST), 7);
                fu2 = fu2.relative(cap.rotate(Direction.SOUTH), 6);
                list.add(new WoodlandMansionPiece(this.structureManager, b.get1x2SideEntrance(this.random, boolean7), fu2, cap.getRotated(Rotation.CLOCKWISE_90), Mirror.FRONT_BACK));
            }
            else if (fz5 == Direction.NORTH && fz4 == Direction.EAST) {
                BlockPos fu2 = fu.relative(cap.rotate(Direction.EAST), 1);
                fu2 = fu2.relative(cap.rotate(Direction.SOUTH), 6);
                list.add(new WoodlandMansionPiece(this.structureManager, b.get1x2SideEntrance(this.random, boolean7), fu2, cap.getRotated(Rotation.COUNTERCLOCKWISE_90)));
            }
            else if (fz5 == Direction.SOUTH && fz4 == Direction.NORTH) {
                BlockPos fu2 = fu.relative(cap.rotate(Direction.EAST), 1);
                fu2 = fu2.relative(cap.rotate(Direction.NORTH), 8);
                list.add(new WoodlandMansionPiece(this.structureManager, b.get1x2FrontEntrance(this.random, boolean7), fu2, cap));
            }
            else if (fz5 == Direction.NORTH && fz4 == Direction.SOUTH) {
                BlockPos fu2 = fu.relative(cap.rotate(Direction.EAST), 7);
                fu2 = fu2.relative(cap.rotate(Direction.SOUTH), 14);
                list.add(new WoodlandMansionPiece(this.structureManager, b.get1x2FrontEntrance(this.random, boolean7), fu2, cap.getRotated(Rotation.CLOCKWISE_180)));
            }
            else if (fz5 == Direction.WEST && fz4 == Direction.EAST) {
                final BlockPos fu2 = fu.relative(cap.rotate(Direction.EAST), 15);
                list.add(new WoodlandMansionPiece(this.structureManager, b.get1x2FrontEntrance(this.random, boolean7), fu2, cap.getRotated(Rotation.CLOCKWISE_90)));
            }
            else if (fz5 == Direction.EAST && fz4 == Direction.WEST) {
                BlockPos fu2 = fu.relative(cap.rotate(Direction.WEST), 7);
                fu2 = fu2.relative(cap.rotate(Direction.SOUTH), 6);
                list.add(new WoodlandMansionPiece(this.structureManager, b.get1x2FrontEntrance(this.random, boolean7), fu2, cap.getRotated(Rotation.COUNTERCLOCKWISE_90)));
            }
            else if (fz5 == Direction.UP && fz4 == Direction.EAST) {
                final BlockPos fu2 = fu.relative(cap.rotate(Direction.EAST), 15);
                list.add(new WoodlandMansionPiece(this.structureManager, b.get1x2Secret(this.random), fu2, cap.getRotated(Rotation.CLOCKWISE_90)));
            }
            else if (fz5 == Direction.UP && fz4 == Direction.SOUTH) {
                BlockPos fu2 = fu.relative(cap.rotate(Direction.EAST), 1);
                fu2 = fu2.relative(cap.rotate(Direction.NORTH), 0);
                list.add(new WoodlandMansionPiece(this.structureManager, b.get1x2Secret(this.random), fu2, cap));
            }
        }
        
        private void addRoom2x2(final List<WoodlandMansionPiece> list, final BlockPos fu, final Rotation cap, final Direction fz4, final Direction fz5, final FloorRoomCollection b) {
            int integer8 = 0;
            int integer9 = 0;
            Rotation cap2 = cap;
            Mirror bzj11 = Mirror.NONE;
            if (fz5 == Direction.EAST && fz4 == Direction.SOUTH) {
                integer8 = -7;
            }
            else if (fz5 == Direction.EAST && fz4 == Direction.NORTH) {
                integer8 = -7;
                integer9 = 6;
                bzj11 = Mirror.LEFT_RIGHT;
            }
            else if (fz5 == Direction.NORTH && fz4 == Direction.EAST) {
                integer8 = 1;
                integer9 = 14;
                cap2 = cap.getRotated(Rotation.COUNTERCLOCKWISE_90);
            }
            else if (fz5 == Direction.NORTH && fz4 == Direction.WEST) {
                integer8 = 7;
                integer9 = 14;
                cap2 = cap.getRotated(Rotation.COUNTERCLOCKWISE_90);
                bzj11 = Mirror.LEFT_RIGHT;
            }
            else if (fz5 == Direction.SOUTH && fz4 == Direction.WEST) {
                integer8 = 7;
                integer9 = -8;
                cap2 = cap.getRotated(Rotation.CLOCKWISE_90);
            }
            else if (fz5 == Direction.SOUTH && fz4 == Direction.EAST) {
                integer8 = 1;
                integer9 = -8;
                cap2 = cap.getRotated(Rotation.CLOCKWISE_90);
                bzj11 = Mirror.LEFT_RIGHT;
            }
            else if (fz5 == Direction.WEST && fz4 == Direction.NORTH) {
                integer8 = 15;
                integer9 = 6;
                cap2 = cap.getRotated(Rotation.CLOCKWISE_180);
            }
            else if (fz5 == Direction.WEST && fz4 == Direction.SOUTH) {
                integer8 = 15;
                bzj11 = Mirror.FRONT_BACK;
            }
            BlockPos fu2 = fu.relative(cap.rotate(Direction.EAST), integer8);
            fu2 = fu2.relative(cap.rotate(Direction.SOUTH), integer9);
            list.add(new WoodlandMansionPiece(this.structureManager, b.get2x2(this.random), fu2, cap2, bzj11));
        }
        
        private void addRoom2x2Secret(final List<WoodlandMansionPiece> list, final BlockPos fu, final Rotation cap, final FloorRoomCollection b) {
            final BlockPos fu2 = fu.relative(cap.rotate(Direction.EAST), 1);
            list.add(new WoodlandMansionPiece(this.structureManager, b.get2x2Secret(this.random), fu2, cap, Mirror.NONE));
        }
    }
    
    static class MansionGrid {
        private final Random random;
        private final SimpleGrid baseGrid;
        private final SimpleGrid thirdFloorGrid;
        private final SimpleGrid[] floorRooms;
        private final int entranceX;
        private final int entranceY;
        
        public MansionGrid(final Random random) {
            this.random = random;
            final int integer3 = 11;
            this.entranceX = 7;
            this.entranceY = 4;
            (this.baseGrid = new SimpleGrid(11, 11, 5)).set(this.entranceX, this.entranceY, this.entranceX + 1, this.entranceY + 1, 3);
            this.baseGrid.set(this.entranceX - 1, this.entranceY, this.entranceX - 1, this.entranceY + 1, 2);
            this.baseGrid.set(this.entranceX + 2, this.entranceY - 2, this.entranceX + 3, this.entranceY + 3, 5);
            this.baseGrid.set(this.entranceX + 1, this.entranceY - 2, this.entranceX + 1, this.entranceY - 1, 1);
            this.baseGrid.set(this.entranceX + 1, this.entranceY + 2, this.entranceX + 1, this.entranceY + 3, 1);
            this.baseGrid.set(this.entranceX - 1, this.entranceY - 1, 1);
            this.baseGrid.set(this.entranceX - 1, this.entranceY + 2, 1);
            this.baseGrid.set(0, 0, 11, 1, 5);
            this.baseGrid.set(0, 9, 11, 11, 5);
            this.recursiveCorridor(this.baseGrid, this.entranceX, this.entranceY - 2, Direction.WEST, 6);
            this.recursiveCorridor(this.baseGrid, this.entranceX, this.entranceY + 3, Direction.WEST, 6);
            this.recursiveCorridor(this.baseGrid, this.entranceX - 2, this.entranceY - 1, Direction.WEST, 3);
            this.recursiveCorridor(this.baseGrid, this.entranceX - 2, this.entranceY + 2, Direction.WEST, 3);
            while (this.cleanEdges(this.baseGrid)) {}
            (this.floorRooms = new SimpleGrid[3])[0] = new SimpleGrid(11, 11, 5);
            this.floorRooms[1] = new SimpleGrid(11, 11, 5);
            this.floorRooms[2] = new SimpleGrid(11, 11, 5);
            this.identifyRooms(this.baseGrid, this.floorRooms[0]);
            this.identifyRooms(this.baseGrid, this.floorRooms[1]);
            this.floorRooms[0].set(this.entranceX + 1, this.entranceY, this.entranceX + 1, this.entranceY + 1, 8388608);
            this.floorRooms[1].set(this.entranceX + 1, this.entranceY, this.entranceX + 1, this.entranceY + 1, 8388608);
            this.thirdFloorGrid = new SimpleGrid(this.baseGrid.width, this.baseGrid.height, 5);
            this.setupThirdFloor();
            this.identifyRooms(this.thirdFloorGrid, this.floorRooms[2]);
        }
        
        public static boolean isHouse(final SimpleGrid g, final int integer2, final int integer3) {
            final int integer4 = g.get(integer2, integer3);
            return integer4 == 1 || integer4 == 2 || integer4 == 3 || integer4 == 4;
        }
        
        public boolean isRoomId(final SimpleGrid g, final int integer2, final int integer3, final int integer4, final int integer5) {
            return (this.floorRooms[integer4].get(integer2, integer3) & 0xFFFF) == integer5;
        }
        
        @Nullable
        public Direction get1x2RoomDirection(final SimpleGrid g, final int integer2, final int integer3, final int integer4, final int integer5) {
            for (final Direction fz8 : Direction.Plane.HORIZONTAL) {
                if (this.isRoomId(g, integer2 + fz8.getStepX(), integer3 + fz8.getStepZ(), integer4, integer5)) {
                    return fz8;
                }
            }
            return null;
        }
        
        private void recursiveCorridor(final SimpleGrid g, final int integer2, final int integer3, final Direction fz, final int integer5) {
            if (integer5 <= 0) {
                return;
            }
            g.set(integer2, integer3, 1);
            g.setif(integer2 + fz.getStepX(), integer3 + fz.getStepZ(), 0, 1);
            for (int integer6 = 0; integer6 < 8; ++integer6) {
                final Direction fz2 = Direction.from2DDataValue(this.random.nextInt(4));
                if (fz2 != fz.getOpposite()) {
                    if (fz2 != Direction.EAST || !this.random.nextBoolean()) {
                        final int integer7 = integer2 + fz.getStepX();
                        final int integer8 = integer3 + fz.getStepZ();
                        if (g.get(integer7 + fz2.getStepX(), integer8 + fz2.getStepZ()) == 0 && g.get(integer7 + fz2.getStepX() * 2, integer8 + fz2.getStepZ() * 2) == 0) {
                            this.recursiveCorridor(g, integer2 + fz.getStepX() + fz2.getStepX(), integer3 + fz.getStepZ() + fz2.getStepZ(), fz2, integer5 - 1);
                            break;
                        }
                    }
                }
            }
            final Direction fz3 = fz.getClockWise();
            final Direction fz2 = fz.getCounterClockWise();
            g.setif(integer2 + fz3.getStepX(), integer3 + fz3.getStepZ(), 0, 2);
            g.setif(integer2 + fz2.getStepX(), integer3 + fz2.getStepZ(), 0, 2);
            g.setif(integer2 + fz.getStepX() + fz3.getStepX(), integer3 + fz.getStepZ() + fz3.getStepZ(), 0, 2);
            g.setif(integer2 + fz.getStepX() + fz2.getStepX(), integer3 + fz.getStepZ() + fz2.getStepZ(), 0, 2);
            g.setif(integer2 + fz.getStepX() * 2, integer3 + fz.getStepZ() * 2, 0, 2);
            g.setif(integer2 + fz3.getStepX() * 2, integer3 + fz3.getStepZ() * 2, 0, 2);
            g.setif(integer2 + fz2.getStepX() * 2, integer3 + fz2.getStepZ() * 2, 0, 2);
        }
        
        private boolean cleanEdges(final SimpleGrid g) {
            boolean boolean3 = false;
            for (int integer4 = 0; integer4 < g.height; ++integer4) {
                for (int integer5 = 0; integer5 < g.width; ++integer5) {
                    if (g.get(integer5, integer4) == 0) {
                        int integer6 = 0;
                        integer6 += (isHouse(g, integer5 + 1, integer4) ? 1 : 0);
                        integer6 += (isHouse(g, integer5 - 1, integer4) ? 1 : 0);
                        integer6 += (isHouse(g, integer5, integer4 + 1) ? 1 : 0);
                        integer6 += (isHouse(g, integer5, integer4 - 1) ? 1 : 0);
                        if (integer6 >= 3) {
                            g.set(integer5, integer4, 2);
                            boolean3 = true;
                        }
                        else if (integer6 == 2) {
                            int integer7 = 0;
                            integer7 += (isHouse(g, integer5 + 1, integer4 + 1) ? 1 : 0);
                            integer7 += (isHouse(g, integer5 - 1, integer4 + 1) ? 1 : 0);
                            integer7 += (isHouse(g, integer5 + 1, integer4 - 1) ? 1 : 0);
                            integer7 += (isHouse(g, integer5 - 1, integer4 - 1) ? 1 : 0);
                            if (integer7 <= 1) {
                                g.set(integer5, integer4, 2);
                                boolean3 = true;
                            }
                        }
                    }
                }
            }
            return boolean3;
        }
        
        private void setupThirdFloor() {
            final List<Tuple<Integer, Integer>> list2 = Lists.newArrayList();
            final SimpleGrid g3 = this.floorRooms[1];
            for (int integer4 = 0; integer4 < this.thirdFloorGrid.height; ++integer4) {
                for (int integer5 = 0; integer5 < this.thirdFloorGrid.width; ++integer5) {
                    final int integer6 = g3.get(integer5, integer4);
                    final int integer7 = integer6 & 0xF0000;
                    if (integer7 == 131072 && (integer6 & 0x200000) == 0x200000) {
                        list2.add(new Tuple<Integer, Integer>(integer5, integer4));
                    }
                }
            }
            if (list2.isEmpty()) {
                this.thirdFloorGrid.set(0, 0, this.thirdFloorGrid.width, this.thirdFloorGrid.height, 5);
                return;
            }
            final Tuple<Integer, Integer> aek4 = list2.get(this.random.nextInt(list2.size()));
            int integer5 = g3.get(aek4.getA(), aek4.getB());
            g3.set(aek4.getA(), aek4.getB(), integer5 | 0x400000);
            final Direction fz6 = this.get1x2RoomDirection(this.baseGrid, aek4.getA(), aek4.getB(), 1, integer5 & 0xFFFF);
            final int integer7 = aek4.getA() + fz6.getStepX();
            final int integer8 = aek4.getB() + fz6.getStepZ();
            for (int integer9 = 0; integer9 < this.thirdFloorGrid.height; ++integer9) {
                for (int integer10 = 0; integer10 < this.thirdFloorGrid.width; ++integer10) {
                    if (!isHouse(this.baseGrid, integer10, integer9)) {
                        this.thirdFloorGrid.set(integer10, integer9, 5);
                    }
                    else if (integer10 == aek4.getA() && integer9 == aek4.getB()) {
                        this.thirdFloorGrid.set(integer10, integer9, 3);
                    }
                    else if (integer10 == integer7 && integer9 == integer8) {
                        this.thirdFloorGrid.set(integer10, integer9, 3);
                        this.floorRooms[2].set(integer10, integer9, 8388608);
                    }
                }
            }
            final List<Direction> list3 = Lists.newArrayList();
            for (final Direction fz7 : Direction.Plane.HORIZONTAL) {
                if (this.thirdFloorGrid.get(integer7 + fz7.getStepX(), integer8 + fz7.getStepZ()) == 0) {
                    list3.add(fz7);
                }
            }
            if (list3.isEmpty()) {
                this.thirdFloorGrid.set(0, 0, this.thirdFloorGrid.width, this.thirdFloorGrid.height, 5);
                g3.set(aek4.getA(), aek4.getB(), integer5);
                return;
            }
            final Direction fz8 = list3.get(this.random.nextInt(list3.size()));
            this.recursiveCorridor(this.thirdFloorGrid, integer7 + fz8.getStepX(), integer8 + fz8.getStepZ(), fz8, 4);
            while (this.cleanEdges(this.thirdFloorGrid)) {}
        }
        
        private void identifyRooms(final SimpleGrid g1, final SimpleGrid g2) {
            final List<Tuple<Integer, Integer>> list4 = Lists.newArrayList();
            for (int integer5 = 0; integer5 < g1.height; ++integer5) {
                for (int integer6 = 0; integer6 < g1.width; ++integer6) {
                    if (g1.get(integer6, integer5) == 2) {
                        list4.add(new Tuple<Integer, Integer>(integer6, integer5));
                    }
                }
            }
            Collections.shuffle(list4, this.random);
            int integer5 = 10;
            for (final Tuple<Integer, Integer> aek7 : list4) {
                final int integer7 = aek7.getA();
                final int integer8 = aek7.getB();
                if (g2.get(integer7, integer8) == 0) {
                    int integer9 = integer7;
                    int integer10 = integer7;
                    int integer11 = integer8;
                    int integer12 = integer8;
                    int integer13 = 65536;
                    if (g2.get(integer7 + 1, integer8) == 0 && g2.get(integer7, integer8 + 1) == 0 && g2.get(integer7 + 1, integer8 + 1) == 0 && g1.get(integer7 + 1, integer8) == 2 && g1.get(integer7, integer8 + 1) == 2 && g1.get(integer7 + 1, integer8 + 1) == 2) {
                        ++integer10;
                        ++integer12;
                        integer13 = 262144;
                    }
                    else if (g2.get(integer7 - 1, integer8) == 0 && g2.get(integer7, integer8 + 1) == 0 && g2.get(integer7 - 1, integer8 + 1) == 0 && g1.get(integer7 - 1, integer8) == 2 && g1.get(integer7, integer8 + 1) == 2 && g1.get(integer7 - 1, integer8 + 1) == 2) {
                        --integer9;
                        ++integer12;
                        integer13 = 262144;
                    }
                    else if (g2.get(integer7 - 1, integer8) == 0 && g2.get(integer7, integer8 - 1) == 0 && g2.get(integer7 - 1, integer8 - 1) == 0 && g1.get(integer7 - 1, integer8) == 2 && g1.get(integer7, integer8 - 1) == 2 && g1.get(integer7 - 1, integer8 - 1) == 2) {
                        --integer9;
                        --integer11;
                        integer13 = 262144;
                    }
                    else if (g2.get(integer7 + 1, integer8) == 0 && g1.get(integer7 + 1, integer8) == 2) {
                        ++integer10;
                        integer13 = 131072;
                    }
                    else if (g2.get(integer7, integer8 + 1) == 0 && g1.get(integer7, integer8 + 1) == 2) {
                        ++integer12;
                        integer13 = 131072;
                    }
                    else if (g2.get(integer7 - 1, integer8) == 0 && g1.get(integer7 - 1, integer8) == 2) {
                        --integer9;
                        integer13 = 131072;
                    }
                    else if (g2.get(integer7, integer8 - 1) == 0 && g1.get(integer7, integer8 - 1) == 2) {
                        --integer11;
                        integer13 = 131072;
                    }
                    int integer14 = this.random.nextBoolean() ? integer9 : integer10;
                    int integer15 = this.random.nextBoolean() ? integer11 : integer12;
                    int integer16 = 2097152;
                    if (!g1.edgesTo(integer14, integer15, 1)) {
                        integer14 = ((integer14 == integer9) ? integer10 : integer9);
                        integer15 = ((integer15 == integer11) ? integer12 : integer11);
                        if (!g1.edgesTo(integer14, integer15, 1)) {
                            integer15 = ((integer15 == integer11) ? integer12 : integer11);
                            if (!g1.edgesTo(integer14, integer15, 1)) {
                                integer14 = ((integer14 == integer9) ? integer10 : integer9);
                                integer15 = ((integer15 == integer11) ? integer12 : integer11);
                                if (!g1.edgesTo(integer14, integer15, 1)) {
                                    integer16 = 0;
                                    integer14 = integer9;
                                    integer15 = integer11;
                                }
                            }
                        }
                    }
                    for (int integer17 = integer11; integer17 <= integer12; ++integer17) {
                        for (int integer18 = integer9; integer18 <= integer10; ++integer18) {
                            if (integer18 == integer14 && integer17 == integer15) {
                                g2.set(integer18, integer17, 0x100000 | integer16 | integer13 | integer5);
                            }
                            else {
                                g2.set(integer18, integer17, integer13 | integer5);
                            }
                        }
                    }
                    ++integer5;
                }
            }
        }
    }
    
    static class SimpleGrid {
        private final int[][] grid;
        private final int width;
        private final int height;
        private final int valueIfOutside;
        
        public SimpleGrid(final int integer1, final int integer2, final int integer3) {
            this.width = integer1;
            this.height = integer2;
            this.valueIfOutside = integer3;
            this.grid = new int[integer1][integer2];
        }
        
        public void set(final int integer1, final int integer2, final int integer3) {
            if (integer1 >= 0 && integer1 < this.width && integer2 >= 0 && integer2 < this.height) {
                this.grid[integer1][integer2] = integer3;
            }
        }
        
        public void set(final int integer1, final int integer2, final int integer3, final int integer4, final int integer5) {
            for (int integer6 = integer2; integer6 <= integer4; ++integer6) {
                for (int integer7 = integer1; integer7 <= integer3; ++integer7) {
                    this.set(integer7, integer6, integer5);
                }
            }
        }
        
        public int get(final int integer1, final int integer2) {
            if (integer1 >= 0 && integer1 < this.width && integer2 >= 0 && integer2 < this.height) {
                return this.grid[integer1][integer2];
            }
            return this.valueIfOutside;
        }
        
        public void setif(final int integer1, final int integer2, final int integer3, final int integer4) {
            if (this.get(integer1, integer2) == integer3) {
                this.set(integer1, integer2, integer4);
            }
        }
        
        public boolean edgesTo(final int integer1, final int integer2, final int integer3) {
            return this.get(integer1 - 1, integer2) == integer3 || this.get(integer1 + 1, integer2) == integer3 || this.get(integer1, integer2 + 1) == integer3 || this.get(integer1, integer2 - 1) == integer3;
        }
    }
    
    abstract static class FloorRoomCollection {
        private FloorRoomCollection() {
        }
        
        public abstract String get1x1(final Random random);
        
        public abstract String get1x1Secret(final Random random);
        
        public abstract String get1x2SideEntrance(final Random random, final boolean boolean2);
        
        public abstract String get1x2FrontEntrance(final Random random, final boolean boolean2);
        
        public abstract String get1x2Secret(final Random random);
        
        public abstract String get2x2(final Random random);
        
        public abstract String get2x2Secret(final Random random);
    }
    
    static class FirstFloorRoomCollection extends FloorRoomCollection {
        private FirstFloorRoomCollection() {
        }
        
        @Override
        public String get1x1(final Random random) {
            return "1x1_a" + (random.nextInt(5) + 1);
        }
        
        @Override
        public String get1x1Secret(final Random random) {
            return "1x1_as" + (random.nextInt(4) + 1);
        }
        
        @Override
        public String get1x2SideEntrance(final Random random, final boolean boolean2) {
            return "1x2_a" + (random.nextInt(9) + 1);
        }
        
        @Override
        public String get1x2FrontEntrance(final Random random, final boolean boolean2) {
            return "1x2_b" + (random.nextInt(5) + 1);
        }
        
        @Override
        public String get1x2Secret(final Random random) {
            return "1x2_s" + (random.nextInt(2) + 1);
        }
        
        @Override
        public String get2x2(final Random random) {
            return "2x2_a" + (random.nextInt(4) + 1);
        }
        
        @Override
        public String get2x2Secret(final Random random) {
            return "2x2_s1";
        }
    }
    
    static class SecondFloorRoomCollection extends FloorRoomCollection {
        private SecondFloorRoomCollection() {
        }
        
        @Override
        public String get1x1(final Random random) {
            return "1x1_b" + (random.nextInt(4) + 1);
        }
        
        @Override
        public String get1x1Secret(final Random random) {
            return "1x1_as" + (random.nextInt(4) + 1);
        }
        
        @Override
        public String get1x2SideEntrance(final Random random, final boolean boolean2) {
            if (boolean2) {
                return "1x2_c_stairs";
            }
            return "1x2_c" + (random.nextInt(4) + 1);
        }
        
        @Override
        public String get1x2FrontEntrance(final Random random, final boolean boolean2) {
            if (boolean2) {
                return "1x2_d_stairs";
            }
            return "1x2_d" + (random.nextInt(5) + 1);
        }
        
        @Override
        public String get1x2Secret(final Random random) {
            return "1x2_se" + (random.nextInt(1) + 1);
        }
        
        @Override
        public String get2x2(final Random random) {
            return "2x2_b" + (random.nextInt(5) + 1);
        }
        
        @Override
        public String get2x2Secret(final Random random) {
            return "2x2_s1";
        }
    }
    
    static class ThirdFloorRoomCollection extends SecondFloorRoomCollection {
        private ThirdFloorRoomCollection() {
        }
    }
}
