001package net.minecraft.world.gen.structure;
002
003import java.util.ArrayList;
004import java.util.Iterator;
005import java.util.List;
006import java.util.Random;
007
008public class StructureStrongholdPieces
009{
010    private static final StructureStrongholdPieceWeight[] pieceWeightArray = new StructureStrongholdPieceWeight[] {new StructureStrongholdPieceWeight(ComponentStrongholdStraight.class, 40, 0), new StructureStrongholdPieceWeight(ComponentStrongholdPrison.class, 5, 5), new StructureStrongholdPieceWeight(ComponentStrongholdLeftTurn.class, 20, 0), new StructureStrongholdPieceWeight(ComponentStrongholdRightTurn.class, 20, 0), new StructureStrongholdPieceWeight(ComponentStrongholdRoomCrossing.class, 10, 6), new StructureStrongholdPieceWeight(ComponentStrongholdStairsStraight.class, 5, 5), new StructureStrongholdPieceWeight(ComponentStrongholdStairs.class, 5, 5), new StructureStrongholdPieceWeight(ComponentStrongholdCrossing.class, 5, 4), new StructureStrongholdPieceWeight(ComponentStrongholdChestCorridor.class, 5, 4), new StructureStrongholdPieceWeight2(ComponentStrongholdLibrary.class, 10, 2), new StructureStrongholdPieceWeight3(ComponentStrongholdPortalRoom.class, 20, 1)};
011    private static List structurePieceList;
012    private static Class strongComponentType;
013    static int totalWeight = 0;
014    private static final StructureStrongholdStones strongholdStones = new StructureStrongholdStones((StructureStrongholdPieceWeight2)null);
015
016    /**
017     * sets up Arrays with the Structure pieces and their weights
018     */
019    public static void prepareStructurePieces()
020    {
021        structurePieceList = new ArrayList();
022        StructureStrongholdPieceWeight[] astructurestrongholdpieceweight = pieceWeightArray;
023        int i = astructurestrongholdpieceweight.length;
024
025        for (int j = 0; j < i; ++j)
026        {
027            StructureStrongholdPieceWeight structurestrongholdpieceweight = astructurestrongholdpieceweight[j];
028            structurestrongholdpieceweight.instancesSpawned = 0;
029            structurePieceList.add(structurestrongholdpieceweight);
030        }
031
032        strongComponentType = null;
033    }
034
035    private static boolean canAddStructurePieces()
036    {
037        boolean flag = false;
038        totalWeight = 0;
039        StructureStrongholdPieceWeight structurestrongholdpieceweight;
040
041        for (Iterator iterator = structurePieceList.iterator(); iterator.hasNext(); totalWeight += structurestrongholdpieceweight.pieceWeight)
042        {
043            structurestrongholdpieceweight = (StructureStrongholdPieceWeight)iterator.next();
044
045            if (structurestrongholdpieceweight.instancesLimit > 0 && structurestrongholdpieceweight.instancesSpawned < structurestrongholdpieceweight.instancesLimit)
046            {
047                flag = true;
048            }
049        }
050
051        return flag;
052    }
053
054    /**
055     * translates the PieceWeight class to the Component class
056     */
057    private static ComponentStronghold getStrongholdComponentFromWeightedPiece(Class par0Class, List par1List, Random par2Random, int par3, int par4, int par5, int par6, int par7)
058    {
059        Object object = null;
060
061        if (par0Class == ComponentStrongholdStraight.class)
062        {
063            object = ComponentStrongholdStraight.findValidPlacement(par1List, par2Random, par3, par4, par5, par6, par7);
064        }
065        else if (par0Class == ComponentStrongholdPrison.class)
066        {
067            object = ComponentStrongholdPrison.findValidPlacement(par1List, par2Random, par3, par4, par5, par6, par7);
068        }
069        else if (par0Class == ComponentStrongholdLeftTurn.class)
070        {
071            object = ComponentStrongholdLeftTurn.findValidPlacement(par1List, par2Random, par3, par4, par5, par6, par7);
072        }
073        else if (par0Class == ComponentStrongholdRightTurn.class)
074        {
075            object = ComponentStrongholdRightTurn.findValidPlacement(par1List, par2Random, par3, par4, par5, par6, par7);
076        }
077        else if (par0Class == ComponentStrongholdRoomCrossing.class)
078        {
079            object = ComponentStrongholdRoomCrossing.findValidPlacement(par1List, par2Random, par3, par4, par5, par6, par7);
080        }
081        else if (par0Class == ComponentStrongholdStairsStraight.class)
082        {
083            object = ComponentStrongholdStairsStraight.findValidPlacement(par1List, par2Random, par3, par4, par5, par6, par7);
084        }
085        else if (par0Class == ComponentStrongholdStairs.class)
086        {
087            object = ComponentStrongholdStairs.getStrongholdStairsComponent(par1List, par2Random, par3, par4, par5, par6, par7);
088        }
089        else if (par0Class == ComponentStrongholdCrossing.class)
090        {
091            object = ComponentStrongholdCrossing.findValidPlacement(par1List, par2Random, par3, par4, par5, par6, par7);
092        }
093        else if (par0Class == ComponentStrongholdChestCorridor.class)
094        {
095            object = ComponentStrongholdChestCorridor.findValidPlacement(par1List, par2Random, par3, par4, par5, par6, par7);
096        }
097        else if (par0Class == ComponentStrongholdLibrary.class)
098        {
099            object = ComponentStrongholdLibrary.findValidPlacement(par1List, par2Random, par3, par4, par5, par6, par7);
100        }
101        else if (par0Class == ComponentStrongholdPortalRoom.class)
102        {
103            object = ComponentStrongholdPortalRoom.findValidPlacement(par1List, par2Random, par3, par4, par5, par6, par7);
104        }
105
106        return (ComponentStronghold)object;
107    }
108
109    private static ComponentStronghold getNextComponent(ComponentStrongholdStairs2 par0ComponentStrongholdStairs2, List par1List, Random par2Random, int par3, int par4, int par5, int par6, int par7)
110    {
111        if (!canAddStructurePieces())
112        {
113            return null;
114        }
115        else
116        {
117            if (strongComponentType != null)
118            {
119                ComponentStronghold componentstronghold = getStrongholdComponentFromWeightedPiece(strongComponentType, par1List, par2Random, par3, par4, par5, par6, par7);
120                strongComponentType = null;
121
122                if (componentstronghold != null)
123                {
124                    return componentstronghold;
125                }
126            }
127
128            int j1 = 0;
129
130            while (j1 < 5)
131            {
132                ++j1;
133                int k1 = par2Random.nextInt(totalWeight);
134                Iterator iterator = structurePieceList.iterator();
135
136                while (iterator.hasNext())
137                {
138                    StructureStrongholdPieceWeight structurestrongholdpieceweight = (StructureStrongholdPieceWeight)iterator.next();
139                    k1 -= structurestrongholdpieceweight.pieceWeight;
140
141                    if (k1 < 0)
142                    {
143                        if (!structurestrongholdpieceweight.canSpawnMoreStructuresOfType(par7) || structurestrongholdpieceweight == par0ComponentStrongholdStairs2.strongholdPieceWeight)
144                        {
145                            break;
146                        }
147
148                        ComponentStronghold componentstronghold1 = getStrongholdComponentFromWeightedPiece(structurestrongholdpieceweight.pieceClass, par1List, par2Random, par3, par4, par5, par6, par7);
149
150                        if (componentstronghold1 != null)
151                        {
152                            ++structurestrongholdpieceweight.instancesSpawned;
153                            par0ComponentStrongholdStairs2.strongholdPieceWeight = structurestrongholdpieceweight;
154
155                            if (!structurestrongholdpieceweight.canSpawnMoreStructures())
156                            {
157                                structurePieceList.remove(structurestrongholdpieceweight);
158                            }
159
160                            return componentstronghold1;
161                        }
162                    }
163                }
164            }
165
166            StructureBoundingBox structureboundingbox = ComponentStrongholdCorridor.func_74992_a(par1List, par2Random, par3, par4, par5, par6);
167
168            if (structureboundingbox != null && structureboundingbox.minY > 1)
169            {
170                return new ComponentStrongholdCorridor(par7, par2Random, structureboundingbox, par6);
171            }
172            else
173            {
174                return null;
175            }
176        }
177    }
178
179    private static StructureComponent getNextValidComponent(ComponentStrongholdStairs2 par0ComponentStrongholdStairs2, List par1List, Random par2Random, int par3, int par4, int par5, int par6, int par7)
180    {
181        if (par7 > 50)
182        {
183            return null;
184        }
185        else if (Math.abs(par3 - par0ComponentStrongholdStairs2.getBoundingBox().minX) <= 112 && Math.abs(par5 - par0ComponentStrongholdStairs2.getBoundingBox().minZ) <= 112)
186        {
187            ComponentStronghold componentstronghold = getNextComponent(par0ComponentStrongholdStairs2, par1List, par2Random, par3, par4, par5, par6, par7 + 1);
188
189            if (componentstronghold != null)
190            {
191                par1List.add(componentstronghold);
192                par0ComponentStrongholdStairs2.field_75026_c.add(componentstronghold);
193            }
194
195            return componentstronghold;
196        }
197        else
198        {
199            return null;
200        }
201    }
202
203    static StructureComponent getNextValidComponentAccess(ComponentStrongholdStairs2 par0ComponentStrongholdStairs2, List par1List, Random par2Random, int par3, int par4, int par5, int par6, int par7)
204    {
205        return getNextValidComponent(par0ComponentStrongholdStairs2, par1List, par2Random, par3, par4, par5, par6, par7);
206    }
207
208    static Class setComponentType(Class par0Class)
209    {
210        strongComponentType = par0Class;
211        return par0Class;
212    }
213
214    static StructureStrongholdStones getStrongholdStones()
215    {
216        return strongholdStones;
217    }
218}